VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkix-signature-rsa.cpp@ 99122

Last change on this file since 99122 was 98103, checked in by vboxsync, 2 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.7 KB
Line 
1/* $Id: pkix-signature-rsa.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - Public Key Signature Schema Algorithm, RSA Providers.
4 */
5
6/*
7 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "internal/iprt.h"
42#include <iprt/crypto/rsa.h>
43
44#include <iprt/bignum.h>
45#include <iprt/err.h>
46#include <iprt/mem.h>
47#include <iprt/string.h>
48#include <iprt/crypto/digest.h>
49#include <iprt/crypto/pkix.h>
50
51#include "rsa-internal.h"
52#include "pkix-signature-builtin.h"
53#include "key-internal.h"
54
55
56/*********************************************************************************************************************************
57* Structures and Typedefs *
58*********************************************************************************************************************************/
59/**
60 * RSA signature provider instance.
61 */
62typedef struct RTCRPKIXSIGNATURERSA
63{
64 /** Set if we're signing, clear if verifying. */
65 bool fSigning;
66
67 /** Temporary big number for use when signing or verifiying. */
68 RTBIGNUM TmpBigNum1;
69 /** Temporary big number for use when signing or verifiying. */
70 RTBIGNUM TmpBigNum2;
71
72 /** Scratch space for decoding the key. */
73 union
74 {
75 /** Public key. */
76 RTCRRSAPUBLICKEY PublicKey;
77 /** Private key. */
78 RTCRRSAPRIVATEKEY PrivateKey;
79 /** Scratch area where we assemble the signature. */
80 uint8_t abSignature[RTCRRSA_MAX_MODULUS_BITS / 8 * 2];
81 } Scratch;
82} RTCRPKIXSIGNATURERSA;
83/** Pointer to an RSA signature provider instance. */
84typedef RTCRPKIXSIGNATURERSA *PRTCRPKIXSIGNATURERSA;
85
86
87/*********************************************************************************************************************************
88* Global Variables *
89*********************************************************************************************************************************/
90/** @name Pre-encoded DigestInfo DER sequences.
91 * @{ */
92static const uint8_t g_abMd2[] =
93{/* { { 1.2.840.113549.2.2 (MD2), NULL }, hash octet-string } */
94 0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x02, 0x05,0x00, 0x04,0x10
95};
96static const uint8_t g_abMd4[] =
97{/* { { 1.2.840.113549.2.4 (MD4), NULL }, hash octet-string } */
98 0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x04, 0x05,0x00, 0x04,0x10
99};
100static const uint8_t g_abMd5[] =
101{/* { { 1.2.840.113549.2.5 (MD5), NULL }, hash octet-string } */
102 0x30,0x20, 0x30,0x0c, 0x06,0x08,0x2a,0x86,0x48,0x86,0xf7,0x0d,0x02,0x05, 0x05,0x00, 0x04,0x10
103};
104static const uint8_t g_abSha1[] =
105{/* { { 1.3.14.3.2.26 (SHA-1), NULL }, hash octet-string } */
106 0x30,0x21, 0x30,0x09, 0x06,0x05,0x2b,0x0e,0x03,0x02,0x1a, 0x05,0x00, 0x04,0x14
107};
108static const uint8_t g_abSha256[] =
109{/* { { 2.16.840.1.101.3.4.2.1 (SHA-256), NULL }, hash octet-string } */
110 0x30,0x31, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x01, 0x05,0x00, 0x04,0x20
111};
112static const uint8_t g_abSha384[] =
113{/* { { 2.16.840.1.101.3.4.2.2 (SHA-384), NULL }, hash octet-string } */
114 0x30,0x41, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x02, 0x05,0x00, 0x04,0x30
115};
116static const uint8_t g_abSha512[] =
117{/* { { 2.16.840.1.101.3.4.2.3 (SHA-512), NULL }, hash octet-string } */
118 0x30,0x51, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x03, 0x05,0x00, 0x04,0x40
119};
120static const uint8_t g_abSha224[] =
121{/* { { 2.16.840.1.101.3.4.2.4 (SHA-224), NULL }, hash octet-string } */
122 0x30,0x2d, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x04, 0x05,0x00, 0x04,0x1c
123};
124static const uint8_t g_abSha512t224[] =
125{/* { { 2.16.840.1.101.3.4.2.5 (SHA-512T224), NULL }, hash octet-string } */
126 0x30,0x2d, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x05, 0x05,0x00, 0x04,0x1c
127};
128static const uint8_t g_abSha512t256[] =
129{/* { { 2.16.840.1.101.3.4.2.6 (SHA-512T256), NULL }, hash octet-string } */
130 0x30,0x31, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x06, 0x05,0x00, 0x04,0x20
131};
132static const uint8_t g_abSha3t224[] =
133{/* { { 2.16.840.1.101.3.4.2.7 (SHA3-224), NULL }, hash octet-string } */
134 0x30,0x2d, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x07, 0x05,0x00, 0x04,0x1c
135};
136static const uint8_t g_abSha3t256[] =
137{/* { { 2.16.840.1.101.3.4.2.8 (SHA3-256), NULL }, hash octet-string } */
138 0x30,0x31, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x08, 0x05,0x00, 0x04,0x20
139};
140static const uint8_t g_abSha3t384[] =
141{/* { { 2.16.840.1.101.3.4.2.9 (SHA3-384), NULL }, hash octet-string } */
142 0x30,0x41, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x09, 0x05,0x00, 0x04,0x30
143};
144static const uint8_t g_abSha3t512[] =
145{/* { { 2.16.840.1.101.3.4.2.10 (SHA3-512), NULL }, hash octet-string } */
146 0x30,0x51, 0x30,0x0d, 0x06,0x09,0x60,0x86,0x48,0x01,0x65,0x03,0x04,0x02,0x0a, 0x05,0x00, 0x04,0x40
147};
148/** @} */
149
150/** Lookup array for the pre-encoded DigestInfo DER sequences. */
151static struct
152{
153 RTDIGESTTYPE enmDigest;
154 const uint8_t *pb;
155 size_t cb;
156} const g_aDigestInfos[] =
157{
158 { RTDIGESTTYPE_SHA1, g_abSha1, sizeof(g_abSha1) },
159 { RTDIGESTTYPE_SHA256, g_abSha256, sizeof(g_abSha256) },
160 { RTDIGESTTYPE_SHA512, g_abSha512, sizeof(g_abSha512) },
161 { RTDIGESTTYPE_MD2, g_abMd2, sizeof(g_abMd2) },
162 { RTDIGESTTYPE_MD4, g_abMd4, sizeof(g_abMd4) },
163 { RTDIGESTTYPE_MD5, g_abMd5, sizeof(g_abMd5) },
164 { RTDIGESTTYPE_SHA384, g_abSha384, sizeof(g_abSha384) },
165 { RTDIGESTTYPE_SHA224, g_abSha224, sizeof(g_abSha224) },
166 { RTDIGESTTYPE_SHA512T224, g_abSha512t224, sizeof(g_abSha512t224)},
167 { RTDIGESTTYPE_SHA512T256, g_abSha512t256, sizeof(g_abSha512t256)},
168 { RTDIGESTTYPE_SHA3_224, g_abSha3t224, sizeof(g_abSha3t224) },
169 { RTDIGESTTYPE_SHA3_256, g_abSha3t256, sizeof(g_abSha3t256) },
170 { RTDIGESTTYPE_SHA3_384, g_abSha3t384, sizeof(g_abSha3t384) },
171 { RTDIGESTTYPE_SHA3_512, g_abSha3t512, sizeof(g_abSha3t512) },
172};
173
174
175/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnInit} */
176static DECLCALLBACK(int) rtCrPkixSignatureRsa_Init(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, void *pvOpaque,
177 bool fSigning, RTCRKEY hKey, PCRTASN1DYNTYPE pParams)
178{
179 RT_NOREF_PV(pDesc); RT_NOREF_PV(pvState); RT_NOREF_PV(pvOpaque);
180
181 if (pParams)
182 return VERR_CR_PKIX_SIGNATURE_TAKES_NO_PARAMETERS;
183
184 RTCRKEYTYPE enmKeyType = RTCrKeyGetType(hKey);
185 if (fSigning)
186 AssertReturn(enmKeyType == RTCRKEYTYPE_RSA_PRIVATE, VERR_CR_PKIX_NOT_RSA_PRIVATE_KEY);
187 else
188 AssertReturn(enmKeyType == RTCRKEYTYPE_RSA_PUBLIC, VERR_CR_PKIX_NOT_RSA_PUBLIC_KEY);
189
190 PRTCRPKIXSIGNATURERSA pThis = (PRTCRPKIXSIGNATURERSA)pvState;
191 pThis->fSigning = fSigning;
192
193 return VINF_SUCCESS;
194}
195
196
197/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnReset} */
198static DECLCALLBACK(int) rtCrPkixSignatureRsa_Reset(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, bool fSigning)
199{
200 PRTCRPKIXSIGNATURERSA pThis = (PRTCRPKIXSIGNATURERSA)pvState;
201 RT_NOREF_PV(fSigning); RT_NOREF_PV(pDesc);
202 Assert(pThis->fSigning == fSigning); NOREF(pThis);
203 return VINF_SUCCESS;
204}
205
206
207/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnDelete} */
208static DECLCALLBACK(void) rtCrPkixSignatureRsa_Delete(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, bool fSigning)
209{
210 PRTCRPKIXSIGNATURERSA pThis = (PRTCRPKIXSIGNATURERSA)pvState;
211 RT_NOREF_PV(fSigning); RT_NOREF_PV(pDesc);
212 Assert(pThis->fSigning == fSigning);
213 NOREF(pThis);
214}
215
216
217/**
218 * Common worker for rtCrPkixSignatureRsa_Verify and
219 * rtCrPkixSignatureRsa_Sign that encodes an EMSA-PKCS1-V1_5 signature in
220 * the scratch area.
221 *
222 * This function is referred to as EMSA-PKCS1-v1_5-ENCODE(M,k) in RFC-3447 and
223 * is described in section 9.2
224 *
225 * @returns IPRT status code.
226 * @param pThis The RSA signature provider instance.
227 * @param hDigest The digest which hash to turn into a signature.
228 * @param cbEncodedMsg The desired encoded message length.
229 * @param fNoDigestInfo If true, skip the DigestInfo and encode the digest
230 * without any prefix like described in v1.5 (RFC-2313)
231 * and observed with RSA+MD5 signed timestamps. If
232 * false, include the prefix like v2.0 (RFC-2437)
233 * describes in step in section 9.2.1
234 * (EMSA-PKCS1-v1_5)
235 *
236 * @remarks Must preserve informational status codes!
237 */
238static int rtCrPkixSignatureRsa_EmsaPkcs1V15Encode(PRTCRPKIXSIGNATURERSA pThis, RTCRDIGEST hDigest, size_t cbEncodedMsg,
239 bool fNoDigestInfo)
240{
241 AssertReturn(cbEncodedMsg * 2 <= sizeof(pThis->Scratch), VERR_CR_PKIX_INTERNAL_ERROR);
242
243 /*
244 * Figure out which hash and select the associate prebaked DigestInfo.
245 */
246 RTDIGESTTYPE const enmDigest = RTCrDigestGetType(hDigest);
247 AssertReturn(enmDigest != RTDIGESTTYPE_INVALID && enmDigest != RTDIGESTTYPE_UNKNOWN, VERR_CR_PKIX_UNKNOWN_DIGEST_TYPE);
248 uint8_t const *pbDigestInfoStart = NULL;
249 size_t cbDigestInfoStart = 0;
250 for (uint32_t i = 0; i < RT_ELEMENTS(g_aDigestInfos); i++)
251 if (g_aDigestInfos[i].enmDigest == enmDigest)
252 {
253 pbDigestInfoStart = g_aDigestInfos[i].pb;
254 cbDigestInfoStart = g_aDigestInfos[i].cb;
255 break;
256 }
257 if (!pbDigestInfoStart)
258 return VERR_CR_PKIX_UNKNOWN_DIGEST_TYPE;
259
260 /*
261 * Get the hash size and verify that it matches what we've got in the
262 * precooked DigestInfo. ASSUMES less that 256 bytes of hash.
263 */
264 uint32_t const cbHash = RTCrDigestGetHashSize(hDigest);
265 AssertReturn(cbHash > 0 && cbHash < _16K, VERR_OUT_OF_RANGE);
266 AssertReturn(cbHash == pbDigestInfoStart[cbDigestInfoStart - 1], VERR_CR_PKIX_INTERNAL_ERROR);
267
268 if (fNoDigestInfo)
269 cbDigestInfoStart = 0;
270
271 if (cbDigestInfoStart + cbHash + 11 > cbEncodedMsg)
272 return VERR_CR_PKIX_HASH_TOO_LONG_FOR_KEY;
273
274 /*
275 * Encode the message the first part of the scratch area.
276 */
277 uint8_t *pbDst = &pThis->Scratch.abSignature[0];
278 pbDst[0] = 0x00;
279 pbDst[1] = 0x01; /* BT - block type, see RFC-2313. */
280 size_t cbFFs = cbEncodedMsg - cbHash - cbDigestInfoStart - 3;
281 memset(&pbDst[2], 0xff, cbFFs);
282 pbDst += cbFFs + 2;
283 *pbDst++ = 0x00;
284 memcpy(pbDst, pbDigestInfoStart, cbDigestInfoStart);
285 pbDst += cbDigestInfoStart;
286 /* Note! Must preserve informational status codes from this call . */
287 int rc = RTCrDigestFinal(hDigest, pbDst, cbHash);
288 if (RT_SUCCESS(rc))
289 {
290 pbDst += cbHash;
291 Assert((size_t)(pbDst - &pThis->Scratch.abSignature[0]) == cbEncodedMsg);
292 }
293 return rc;
294}
295
296
297
298/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnVerify} */
299static DECLCALLBACK(int) rtCrPkixSignatureRsa_Verify(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, RTCRKEY hKey,
300 RTCRDIGEST hDigest, void const *pvSignature, size_t cbSignature)
301{
302 PRTCRPKIXSIGNATURERSA pThis = (PRTCRPKIXSIGNATURERSA)pvState;
303 RT_NOREF_PV(pDesc);
304 Assert(!pThis->fSigning);
305 if (cbSignature > sizeof(pThis->Scratch) / 2)
306 return VERR_CR_PKIX_SIGNATURE_TOO_LONG;
307
308 /*
309 * Get the key bits we need.
310 */
311 Assert(RTCrKeyGetType(hKey) == RTCRKEYTYPE_RSA_PUBLIC);
312 PRTBIGNUM pModulus = &hKey->u.RsaPublic.Modulus;
313 PRTBIGNUM pExponent = &hKey->u.RsaPublic.Exponent;
314
315 /*
316 * 8.2.2.1 - Length check. (RFC-3447)
317 */
318 if (cbSignature != RTBigNumByteWidth(pModulus))
319 return VERR_CR_PKIX_INVALID_SIGNATURE_LENGTH;
320
321 /*
322 * 8.2.2.2 - RSA verification / Decrypt the signature.
323 */
324 /* a) s = OS2IP(S) -- Convert signature to integer. */
325 int rc = RTBigNumInit(&pThis->TmpBigNum1, RTBIGNUMINIT_F_ENDIAN_BIG | RTBIGNUMINIT_F_UNSIGNED,
326 pvSignature, cbSignature);
327 if (RT_FAILURE(rc))
328 return rc;
329 /* b) RSAVP1 - 5.2.2.2: Range check (0 <= s < n). */
330 if (RTBigNumCompare(&pThis->TmpBigNum1, pModulus) < 0)
331 {
332 if (RTBigNumCompareWithU64(&pThis->TmpBigNum1, 0) >= 0)
333 {
334 /* b) RSAVP1 - 5.2.2.3: s^e mod n */
335 rc = RTBigNumInitZero(&pThis->TmpBigNum2, 0);
336 if (RT_SUCCESS(rc))
337 {
338 rc = RTBigNumModExp(&pThis->TmpBigNum2, &pThis->TmpBigNum1, pExponent, pModulus);
339 if (RT_SUCCESS(rc))
340 {
341 /* c) EM' = I2OSP(m, k) -- Convert the result to bytes. */
342 uint32_t cbDecrypted = RTBigNumByteWidth(&pThis->TmpBigNum2) + 1; /* 1 = leading zero byte */
343 if (cbDecrypted <= sizeof(pThis->Scratch) / 2)
344 {
345 uint8_t *pbDecrypted = &pThis->Scratch.abSignature[sizeof(pThis->Scratch) / 2];
346 rc = RTBigNumToBytesBigEndian(&pThis->TmpBigNum2, pbDecrypted, cbDecrypted);
347 if (RT_SUCCESS(rc))
348 {
349 /*
350 * 8.2.2.3 - Build a hopefully identical signature using hDigest.
351 */
352 rc = rtCrPkixSignatureRsa_EmsaPkcs1V15Encode(pThis, hDigest, cbDecrypted, false /* fNoDigestInfo */);
353 if (RT_SUCCESS(rc))
354 {
355 /*
356 * 8.2.2.4 - Compare the two.
357 */
358 if (memcmp(&pThis->Scratch.abSignature[0], pbDecrypted, cbDecrypted) == 0)
359 { /* No rc = VINF_SUCCESS here, mustpreserve informational status codes from digest. */ }
360 else
361 {
362 /*
363 * Try again without digestinfo. This style signing has been
364 * observed in Vista timestamp counter signatures (Thawte).
365 */
366 rc = rtCrPkixSignatureRsa_EmsaPkcs1V15Encode(pThis, hDigest, cbDecrypted,
367 true /* fNoDigestInfo */);
368 if (RT_SUCCESS(rc))
369 {
370 if (memcmp(&pThis->Scratch.abSignature[0], pbDecrypted, cbDecrypted) == 0)
371 { /* No rc = VINF_SUCCESS here, mustpreserve informational status codes from digest. */ }
372 else
373 rc = VERR_CR_PKIX_SIGNATURE_MISMATCH;
374 }
375 }
376 }
377 }
378 }
379 else
380 rc = VERR_CR_PKIX_SIGNATURE_TOO_LONG;
381 }
382 RTBigNumDestroy(&pThis->TmpBigNum2);
383 }
384 }
385 else
386 rc = VERR_CR_PKIX_SIGNATURE_NEGATIVE;
387 }
388 else
389 rc = VERR_CR_PKIX_SIGNATURE_GE_KEY;
390 RTBigNumDestroy(&pThis->TmpBigNum1);
391 return rc;
392}
393
394
395/** @impl_interface_method{RTCRPKIXSIGNATUREDESC,pfnSign} */
396static DECLCALLBACK(int) rtCrPkixSignatureRsa_Sign(PCRTCRPKIXSIGNATUREDESC pDesc, void *pvState, RTCRKEY hKey,
397 RTCRDIGEST hDigest, void *pvSignature, size_t *pcbSignature)
398{
399 PRTCRPKIXSIGNATURERSA pThis = (PRTCRPKIXSIGNATURERSA)pvState;
400 RT_NOREF_PV(pDesc);
401 Assert(pThis->fSigning);
402
403 /*
404 * Get the key bits we need.
405 */
406 Assert(RTCrKeyGetType(hKey) == RTCRKEYTYPE_RSA_PRIVATE);
407 PRTBIGNUM pModulus = &hKey->u.RsaPrivate.Modulus;
408 PRTBIGNUM pExponent = &hKey->u.RsaPrivate.PrivateExponent;
409
410 /*
411 * Calc signature length and return if destination buffer isn't big enough.
412 */
413 size_t const cbDst = *pcbSignature;
414 size_t const cbEncodedMsg = RTBigNumByteWidth(pModulus);
415 *pcbSignature = cbEncodedMsg;
416 if (cbEncodedMsg > sizeof(pThis->Scratch) / 2)
417 return VERR_CR_PKIX_SIGNATURE_TOO_LONG;
418 if (!pvSignature || cbDst < cbEncodedMsg)
419 return VERR_BUFFER_OVERFLOW;
420
421 /*
422 * 8.1.1.1 - EMSA-PSS encoding. (RFC-3447)
423 */
424 int rcRetSuccess;
425 int rc = rcRetSuccess = rtCrPkixSignatureRsa_EmsaPkcs1V15Encode(pThis, hDigest, cbEncodedMsg, false /* fNoDigestInfo */);
426 if (RT_SUCCESS(rc))
427 {
428 /*
429 * 8.1.1.2 - RSA signature.
430 */
431 /* a) m = OS2IP(EM) -- Convert the encoded message (EM) to integer. */
432 rc = RTBigNumInit(&pThis->TmpBigNum1, RTBIGNUMINIT_F_ENDIAN_BIG | RTBIGNUMINIT_F_UNSIGNED,
433 pThis->Scratch.abSignature, cbEncodedMsg);
434 if (RT_SUCCESS(rc))
435 {
436 /* b) s = RSASP1(K, m = EM) - 5.2.1.1: Range check (0 <= m < n). */
437 if (RTBigNumCompare(&pThis->TmpBigNum1, pModulus) < 0)
438 {
439 /* b) s = RSAVP1(K, m = EM) - 5.2.1.2.a: s = m^d mod n */
440 rc = RTBigNumInitZero(&pThis->TmpBigNum2, 0);
441 if (RT_SUCCESS(rc))
442 {
443 rc = RTBigNumModExp(&pThis->TmpBigNum2, &pThis->TmpBigNum1, pExponent, pModulus);
444 if (RT_SUCCESS(rc))
445 {
446 /* c) S = I2OSP(s, k) -- Convert the result to bytes. */
447 rc = RTBigNumToBytesBigEndian(&pThis->TmpBigNum2, pvSignature, cbEncodedMsg);
448 AssertStmt(RT_SUCCESS(rc) || rc != VERR_BUFFER_OVERFLOW, rc = VERR_CR_PKIX_INTERNAL_ERROR);
449
450 /* Make sure we return the informational status code from the digest on success. */
451 if (rc == VINF_SUCCESS && rcRetSuccess != VINF_SUCCESS)
452 rc = rcRetSuccess;
453 }
454 RTBigNumDestroy(&pThis->TmpBigNum2);
455 }
456 }
457 else
458 rc = VERR_CR_PKIX_SIGNATURE_GE_KEY;
459 RTBigNumDestroy(&pThis->TmpBigNum1);
460 }
461 }
462 return rc;
463}
464
465
466
467
468/** RSA alias ODIs. */
469static const char * const g_apszHashWithRsaAliases[] =
470{
471 RTCR_PKCS1_MD2_WITH_RSA_OID,
472 RTCR_PKCS1_MD4_WITH_RSA_OID,
473 RTCR_PKCS1_MD5_WITH_RSA_OID,
474 RTCR_PKCS1_SHA1_WITH_RSA_OID,
475 RTCR_PKCS1_SHA256_WITH_RSA_OID,
476 RTCR_PKCS1_SHA384_WITH_RSA_OID,
477 RTCR_PKCS1_SHA512_WITH_RSA_OID,
478 RTCR_PKCS1_SHA224_WITH_RSA_OID,
479 RTCR_PKCS1_SHA512T224_WITH_RSA_OID,
480 RTCR_PKCS1_SHA512T256_WITH_RSA_OID,
481 /* Note: Note quite sure about these OIW oddballs. */
482 "1.3.14.3.2.11" /* OIW rsaSignature */,
483 "1.3.14.3.2.14" /* OIW mdc2WithRSASignature */,
484 "1.3.14.3.2.15" /* OIW shaWithRSASignature */,
485 "1.3.14.3.2.24" /* OIW md2WithRSASignature */,
486 "1.3.14.3.2.25" /* OIW md5WithRSASignature */,
487 "1.3.14.3.2.29" /* OIW sha1WithRSASignature */,
488 NULL
489};
490
491
492/** RSA descriptor. */
493DECL_HIDDEN_CONST(RTCRPKIXSIGNATUREDESC const) g_rtCrPkixSigningHashWithRsaDesc =
494{
495 "RSASSA-PKCS1-v1_5",
496 RTCR_PKCS1_RSA_OID,
497 g_apszHashWithRsaAliases,
498 sizeof(RTCRPKIXSIGNATURERSA),
499 0,
500 0,
501 rtCrPkixSignatureRsa_Init,
502 rtCrPkixSignatureRsa_Reset,
503 rtCrPkixSignatureRsa_Delete,
504 rtCrPkixSignatureRsa_Verify,
505 rtCrPkixSignatureRsa_Sign,
506};
507
508
509/**
510 * Worker for RTCrRsaPublicKey_CanHandleDigestType and
511 * RTCrRsaPrivateKey_CanHandleDigestType.
512 *
513 * We implement these two functions here because we've already got the
514 * DigestInfo sizes nicely lined up here.
515 */
516static bool rtCrRsa_CanHandleDigestType(int32_t cModulusBits, RTDIGESTTYPE enmDigestType, PRTERRINFO pErrInfo)
517{
518 /*
519 * ASSUME EMSA-PKCS1-v1_5 padding scheme (RFC-8017 section 9.2):
520 * - 11 byte padding prefix (00, 01, 8 times ff)
521 * - digest info der sequence for rsaWithXxxxEncryption
522 * - the hash value.
523 */
524 for (uint32_t i = 0; i < RT_ELEMENTS(g_aDigestInfos); i++)
525 if (g_aDigestInfos[i].enmDigest == enmDigestType)
526 {
527 size_t const cbHash = RTCrDigestTypeToHashSize(enmDigestType);
528 AssertBreak(cbHash > 0);
529
530 size_t cbMsg = 11 + g_aDigestInfos[i].cb + cbHash;
531 if ((ssize_t)cbMsg <= cModulusBits / 8)
532 return true;
533 RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_INVALID_SIGNATURE_LENGTH, "cModulusBits=%d cbMsg=%u", cModulusBits, cbMsg);
534 return false;
535 }
536 RTErrInfoSetF(pErrInfo, VERR_CR_PKIX_UNKNOWN_DIGEST_TYPE, "%s", RTCrDigestTypeToName(enmDigestType));
537 return false;
538}
539
540
541RTDECL(bool) RTCrRsaPublicKey_CanHandleDigestType(PCRTCRRSAPUBLICKEY pRsaPublicKey, RTDIGESTTYPE enmDigestType,
542 PRTERRINFO pErrInfo)
543{
544 if (RTCrRsaPublicKey_IsPresent(pRsaPublicKey))
545 return rtCrRsa_CanHandleDigestType(RTAsn1Integer_UnsignedLastBit(&pRsaPublicKey->Modulus) + 1, enmDigestType, pErrInfo);
546 return false;
547}
548
549
550RTDECL(bool) RTCrRsaPrivateKey_CanHandleDigestType(PCRTCRRSAPRIVATEKEY pRsaPrivateKey, RTDIGESTTYPE enmDigestType,
551 PRTERRINFO pErrInfo)
552{
553 if (RTCrRsaPrivateKey_IsPresent(pRsaPrivateKey))
554 return rtCrRsa_CanHandleDigestType(RTAsn1Integer_UnsignedLastBit(&pRsaPrivateKey->Modulus) + 1, enmDigestType, pErrInfo);
555 return false;
556}
557
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette