VirtualBox

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

Last change on this file since 78018 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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