VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkcs7-verify.cpp@ 52533

Last change on this file since 52533 was 51776, checked in by vboxsync, 11 years ago

build fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 24.8 KB
Line 
1/* $Id: pkcs7-verify.cpp 51776 2014-07-01 19:47:57Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - PKCS \#7, Verification
4 */
5
6/*
7 * Copyright (C) 2006-2014 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/pkcs7.h>
33
34#include <iprt/err.h>
35#include <iprt/string.h>
36#include <iprt/crypto/digest.h>
37#include <iprt/crypto/pkix.h>
38#include <iprt/crypto/store.h>
39#include <iprt/crypto/x509.h>
40
41#ifdef IPRT_WITH_OPENSSL
42# include "internal/iprt-openssl.h"
43# include <openssl/pkcs7.h>
44# include <openssl/x509.h>
45# include <openssl/err.h>
46#endif
47
48
49
50#ifdef IPRT_WITH_OPENSSL
51static int rtCrPkcs7VerifySignedDataUsingOpenSsl(PCRTCRPKCS7CONTENTINFO pContentInfo, uint32_t fFlags,
52 RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
53 void const *pvContent, uint32_t cbContent, PRTERRINFO pErrInfo)
54{
55 /*
56 * Verify using OpenSSL.
57 */
58 int rcOssl;
59 unsigned char const *pbRawContent = RTASN1CORE_GET_RAW_ASN1_PTR(&pContentInfo->SeqCore.Asn1Core);
60 PKCS7 *pOsslPkcs7 = NULL;
61 if (d2i_PKCS7(&pOsslPkcs7, &pbRawContent, RTASN1CORE_GET_RAW_ASN1_SIZE(&pContentInfo->SeqCore.Asn1Core)) == pOsslPkcs7)
62 {
63 STACK_OF(X509) *pAddCerts = NULL;
64 if (hAdditionalCerts != NIL_RTCRSTORE)
65 rcOssl = RTCrStoreConvertToOpenSslCertStack(hAdditionalCerts, 0, (void **)&pAddCerts);
66 else
67 {
68 pAddCerts = sk_X509_new_null();
69 rcOssl = RT_LIKELY(pAddCerts != NULL) ? VINF_SUCCESS : VERR_NO_MEMORY;
70 }
71 if (RT_SUCCESS(rcOssl))
72 {
73 for (uint32_t i = 0; i < pContentInfo->u.pSignedData->Certificates.cItems; i++)
74 rtCrOpenSslAddX509CertToStack(pAddCerts, &pContentInfo->u.pSignedData->Certificates.paItems[i]);
75
76
77 X509_STORE *pTrustedCerts = NULL;
78 if (hTrustedCerts != NIL_RTCRSTORE)
79 rcOssl = RTCrStoreConvertToOpenSslCertStore(hTrustedCerts, 0, (void **)&pTrustedCerts);
80 if (RT_SUCCESS(rcOssl))
81 {
82 rtCrOpenSslInit();
83
84 BIO *pBioContent = BIO_new_mem_buf((void *)pvContent, cbContent);
85 if (pBioContent)
86 {
87 uint32_t fOsslFlags = PKCS7_NOCHAIN;
88 fOsslFlags |= PKCS7_NOVERIFY; // temporary hack.
89 if (PKCS7_verify(pOsslPkcs7, pAddCerts, pTrustedCerts, pBioContent, NULL /*out*/, fOsslFlags))
90 rcOssl = VINF_SUCCESS;
91 else
92 {
93 rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_OSSL_VERIFY_FAILED, "PKCS7_verify failed: ");
94 if (pErrInfo)
95 ERR_print_errors_cb(rtCrOpenSslErrInfoCallback, pErrInfo);
96 }
97 BIO_free(pBioContent);
98 }
99 if (pTrustedCerts)
100 X509_STORE_free(pTrustedCerts);
101 }
102 else
103 rcOssl = RTErrInfoSet(pErrInfo, rcOssl, "RTCrStoreConvertToOpenSslCertStack failed");
104 if (pAddCerts)
105 sk_X509_pop_free(pAddCerts, X509_free);
106 }
107 else
108 rcOssl = RTErrInfoSet(pErrInfo, rcOssl, "RTCrStoreConvertToOpenSslCertStack failed");
109 PKCS7_free(pOsslPkcs7);
110 }
111 else
112 rcOssl = RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_OSSL_D2I_FAILED, "d2i_PKCS7 failed");
113
114 return rcOssl;
115}
116#endif /* IPRT_WITH_OPENSSL */
117
118
119/**
120 * @callback_method_impl{RTCRPKCS7VERIFYCERTCALLBACK,
121 * Default implementation that checks for the DigitalSignature KeyUsage bit.}
122 */
123RTDECL(int) RTCrPkcs7VerifyCertCallbackDefault(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
124 void *pvUser, PRTERRINFO pErrInfo)
125{
126 /*
127 * Check for the digital signature key usage.
128 */
129 if ( (pCert->TbsCertificate.T3.fFlags & RTCRX509TBSCERTIFICATE_F_PRESENT_KEY_USAGE)
130 && !(pCert->TbsCertificate.T3.fKeyUsage & RTCRX509CERT_KEY_USAGE_F_DIGITAL_SIGNATURE))
131 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_KEY_USAGE_MISMATCH, "fKeyUsage=%#x, missing %#x",
132 pCert->TbsCertificate.T3.fKeyUsage, RTCRX509CERT_KEY_USAGE_F_DIGITAL_SIGNATURE);
133 return VINF_SUCCESS;
134}
135
136
137/**
138 * @callback_method_impl{RTCRPKCS7VERIFYCERTCALLBACK,
139 * Standard code signing. Use this for Microsoft SPC.}
140 */
141RTDECL(int) RTCrPkcs7VerifyCertCallbackCodeSigning(PCRTCRX509CERTIFICATE pCert, RTCRX509CERTPATHS hCertPaths,
142 void *pvUser, PRTERRINFO pErrInfo)
143{
144 /*
145 * Check for the digital signature key usage. Not required to be present.
146 */
147 if (pCert->TbsCertificate.T3.fFlags & RTCRX509TBSCERTIFICATE_F_PRESENT_KEY_USAGE)
148 {
149 if (!(pCert->TbsCertificate.T3.fKeyUsage & RTCRX509CERT_KEY_USAGE_F_DIGITAL_SIGNATURE))
150 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_KEY_USAGE_MISMATCH, "fKeyUsage=%#x, missing %#x",
151 pCert->TbsCertificate.T3.fKeyUsage, RTCRX509CERT_KEY_USAGE_F_DIGITAL_SIGNATURE);
152 }
153
154 /*
155 * Check the extended key usage bits if present.
156 */
157 if (!(pCert->TbsCertificate.T3.fFlags & RTCRX509TBSCERTIFICATE_F_PRESENT_EXT_KEY_USAGE))
158 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_KEY_USAGE_MISMATCH, "No extended key usage certificate attribute.");
159 if (!(pCert->TbsCertificate.T3.fExtKeyUsage & RTCRX509CERT_EKU_F_CODE_SIGNING))
160 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_KEY_USAGE_MISMATCH, "fExtKeyUsage=%#x, missing %#x",
161 pCert->TbsCertificate.T3.fExtKeyUsage, RTCRX509CERT_EKU_F_CODE_SIGNING);
162 return VINF_SUCCESS;
163}
164
165
166/**
167 * Deals with authenticated attributes.
168 *
169 * When authenticated attributes are present (checked by caller) we must:
170 * - fish out the content type and check it against the content inof,
171 * - fish out the message digest among and check it against *phDigest,
172 * - compute the message digest of the authenticated attributes and
173 * replace *phDigest with this for the signature verification.
174 *
175 * @returns IPRT status code.
176 * @param pSignerInfo The signer info being verified.
177 * @param pSignedData The signed data.
178 * @param phDigest On input this is the digest of the content. On
179 * output it will (on success) be a reference to
180 * the message digest of the authenticated
181 * attributes. The input reference is consumed.
182 * The caller shall release the output reference.
183 * @param fFlags Flags.
184 * @param pErrInfo Extended error info, optional.
185 */
186static int rtCrPkcs7VerifySignerInfoAuthAttribs(PCRTCRPKCS7SIGNERINFO pSignerInfo, PCRTCRPKCS7SIGNEDDATA pSignedData,
187 PRTCRDIGEST phDigest, uint32_t fFlags, PRTERRINFO pErrInfo)
188{
189 /*
190 * Scan the attributes and validate the two required attributes
191 * (RFC-2315, chapter 9.2, fourth bullet). Checking that we've got exactly
192 * one of each of them is checked by the santiy checker function, so we'll
193 * just assert that it did it's job here.
194 */
195 uint32_t cContentTypes = 0;
196 uint32_t cMessageDigests = 0;
197 uint32_t i = pSignerInfo->AuthenticatedAttributes.cItems;
198 while (i-- > 0)
199 {
200 PCRTCRPKCS7ATTRIBUTE pAttrib = &pSignerInfo->AuthenticatedAttributes.paItems[i];
201
202 if (RTAsn1ObjId_CompareWithString(&pAttrib->Type, RTCR_PKCS9_ID_CONTENT_TYPE_OID) == 0)
203 {
204 AssertReturn(!cContentTypes, VERR_CR_PKCS7_INTERNAL_ERROR);
205 AssertReturn(pAttrib->enmType == RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS, VERR_CR_PKCS7_INTERNAL_ERROR);
206 AssertReturn(pAttrib->uValues.pObjIds->cItems == 1, VERR_CR_PKCS7_INTERNAL_ERROR);
207
208 if (RTAsn1ObjId_Compare(&pAttrib->uValues.pObjIds->paItems[0], &pSignedData->ContentInfo.ContentType) != 0)
209 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_CONTENT_TYPE_ATTRIB_MISMATCH,
210 "Expected content-type %s, found %s",
211 &pAttrib->uValues.pObjIds->paItems[0], pSignedData->ContentInfo.ContentType.szObjId);
212 cContentTypes++;
213 }
214 else if (RTAsn1ObjId_CompareWithString(&pAttrib->Type, RTCR_PKCS9_ID_MESSAGE_DIGEST_OID) == 0)
215 {
216 AssertReturn(!cMessageDigests, VERR_CR_PKCS7_INTERNAL_ERROR);
217 AssertReturn(pAttrib->enmType == RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS, VERR_CR_PKCS7_INTERNAL_ERROR);
218 AssertReturn(pAttrib->uValues.pOctetStrings->cItems == 1, VERR_CR_PKCS7_INTERNAL_ERROR);
219
220 if (!RTCrDigestMatch(*phDigest,
221 pAttrib->uValues.pOctetStrings->paItems[0].Asn1Core.uData.pv,
222 pAttrib->uValues.pOctetStrings->paItems[0].Asn1Core.cb))
223 {
224 size_t cbHash = RTCrDigestGetHashSize(*phDigest);
225 if (cbHash != pAttrib->uValues.pOctetStrings->paItems[0].Asn1Core.cb)
226 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_MESSAGE_DIGEST_ATTRIB_MISMATCH,
227 "Authenticated message-digest attribute mismatch: cbHash=%#zx cbValue=%#x",
228 cbHash, pAttrib->uValues.pOctetStrings->paItems[0].Asn1Core.cb);
229 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_MESSAGE_DIGEST_ATTRIB_MISMATCH,
230 "Authenticated message-digest attribute mismatch (cbHash=%#zx):\n"
231 "signed: %.*Rhxs\n"
232 "our: %.*Rhxs\n",
233 cbHash,
234 cbHash, pAttrib->uValues.pOctetStrings->paItems[0].Asn1Core.uData.pv,
235 cbHash, RTCrDigestGetHash(*phDigest));
236 }
237 cMessageDigests++;
238 }
239 }
240
241 AssertReturn(cContentTypes == 1, VERR_CR_PKCS7_INTERNAL_ERROR);
242 AssertReturn(cMessageDigests == 1, VERR_CR_PKCS7_INTERNAL_ERROR);
243
244 /*
245 * Calculate the digest of the the authenticated attributes for use in the
246 * signature validation.
247 */
248 if ( pSignerInfo->DigestAlgorithm.Parameters.enmType != RTASN1TYPE_NULL
249 && pSignerInfo->DigestAlgorithm.Parameters.enmType != RTASN1TYPE_NOT_PRESENT)
250 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_DIGEST_PARAMS_NOT_IMPL, "Digest algorithm has unsupported parameters");
251
252 RTCRDIGEST hDigest;
253 int rc = RTCrDigestCreateByObjId(&hDigest, &pSignerInfo->DigestAlgorithm.Algorithm);
254 if (RT_SUCCESS(rc))
255 {
256 RTCrDigestRelease(*phDigest);
257 *phDigest = hDigest;
258
259 /* ASSUMES that the attributes are encoded according to DER. */
260 uint8_t const *pbData = (uint8_t const *)RTASN1CORE_GET_RAW_ASN1_PTR(&pSignerInfo->AuthenticatedAttributes.SetCore.Asn1Core);
261 uint32_t cbData = RTASN1CORE_GET_RAW_ASN1_SIZE(&pSignerInfo->AuthenticatedAttributes.SetCore.Asn1Core);
262 uint8_t bSetOfTag = ASN1_TAG_SET | ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED;
263 rc = RTCrDigestUpdate(hDigest, &bSetOfTag, sizeof(bSetOfTag)); /* Replace the implict tag with a SET-OF tag. */
264 if (RT_SUCCESS(rc))
265 rc = RTCrDigestUpdate(hDigest, pbData + sizeof(bSetOfTag), cbData - sizeof(bSetOfTag)); /* Skip the implicit tag. */
266 if (RT_SUCCESS(rc))
267 rc = RTCrDigestFinal(hDigest, NULL, 0);
268 }
269 return rc;
270}
271
272
273/**
274 * Verifies one signature on a PKCS \#7 SignedData.
275 *
276 * @returns IPRT status code.
277 * @param pSignerInfo The signature.
278 * @param pSignedData The SignedData.
279 * @param pahDigests Array of content digests that runs parallel to
280 * pSignedData->DigestAlgorithms.
281 * @param fFlags Verficiation flags.
282 * @param hAdditionalCerts Store containing optional certificates,
283 * optional.
284 * @param hTrustedCerts Store containing trusted certificates, required.
285 * @param pValidationTime The time we're supposed to validate the
286 * certificates chains at.
287 * @param pfnVerifyCert Signing certificate verification callback.
288 * @param pvUser Callback parameter.
289 * @param pErrInfo Where to store additional error details,
290 * optional.
291 */
292static int rtCrPkcs7VerifySignerInfo(PCRTCRPKCS7SIGNERINFO pSignerInfo, PCRTCRPKCS7SIGNEDDATA pSignedData,
293 PRTCRDIGEST pahDigests, uint32_t fFlags, RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
294 PCRTTIMESPEC pValidationTime, RTCRPKCS7VERIFYCERTCALLBACK pfnVerifyCert, void *pvUser,
295 PRTERRINFO pErrInfo)
296{
297 /*
298 * Check for counter signatures with timestamp. Verify the signature for
299 * the current time if not present.
300 */
301 /** @todo timestamp counter signatures. */
302
303 /*
304 * Locate the certificate used for signing.
305 */
306 PCRTCRCERTCTX pSignerCertCtx = NULL;
307 PCRTCRX509CERTIFICATE pSignerCert = NULL;
308 RTCRSTORE hSignerCertSrc = hTrustedCerts;
309 if (hSignerCertSrc != NIL_RTCRSTORE)
310 pSignerCertCtx = RTCrStoreCertByIssuerAndSerialNo(hSignerCertSrc, &pSignerInfo->IssuerAndSerialNumber.Name,
311 &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
312 if (!pSignerCertCtx)
313 {
314 hSignerCertSrc = hAdditionalCerts;
315 if (hSignerCertSrc != NIL_RTCRSTORE)
316 pSignerCertCtx = RTCrStoreCertByIssuerAndSerialNo(hSignerCertSrc, &pSignerInfo->IssuerAndSerialNumber.Name,
317 &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
318 }
319 if (pSignerCertCtx)
320 pSignerCert = pSignerCertCtx->pCert;
321 else
322 {
323 hSignerCertSrc = NULL;
324 pSignerCert = RTCrX509Certificates_FindByIssuerAndSerialNumber(&pSignedData->Certificates,
325 &pSignerInfo->IssuerAndSerialNumber.Name,
326 &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
327 if (!pSignerCert)
328 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNED_DATA_CERT_NOT_FOUND,
329 "Certificate not found: serial=%.*Rhxs",
330 pSignerInfo->IssuerAndSerialNumber.SerialNumber.Asn1Core.cb,
331 pSignerInfo->IssuerAndSerialNumber.SerialNumber.Asn1Core.uData.pv);
332 }
333
334 /*
335 * If not a trusted certificate, we'll have to build certificate paths
336 * and verify them. If no valid paths are found, this step will fail.
337 */
338 int rc = VINF_SUCCESS;
339 if ( hSignerCertSrc == NIL_RTCRSTORE
340 || hSignerCertSrc != hTrustedCerts)
341 {
342 RTCRX509CERTPATHS hCertPaths;
343 rc = RTCrX509CertPathsCreate(&hCertPaths, pSignerCert);
344 if (RT_SUCCESS(rc))
345 {
346 rc = RTCrX509CertPathsSetValidTimeSpec(hCertPaths, pValidationTime);
347 if (hTrustedCerts != NIL_RTCRSTORE && RT_SUCCESS(rc))
348 rc = RTCrX509CertPathsSetTrustedStore(hCertPaths, hTrustedCerts);
349 if (hAdditionalCerts != NIL_RTCRSTORE && RT_SUCCESS(rc))
350 rc = RTCrX509CertPathsSetUntrustedStore(hCertPaths, hAdditionalCerts);
351 if (pSignedData->Certificates.cItems > 0 && RT_SUCCESS(rc))
352 rc = RTCrX509CertPathsSetUntrustedArray(hCertPaths,
353 pSignedData->Certificates.paItems,
354 pSignedData->Certificates.cItems);
355 if (RT_SUCCESS(rc))
356 {
357 rc = RTCrX509CertPathsBuild(hCertPaths, pErrInfo);
358 if (RT_SUCCESS(rc))
359 rc = RTCrX509CertPathsValidateAll(hCertPaths, NULL, pErrInfo);
360
361 /*
362 * Check that the certificate purpose and whatnot matches what
363 * is being signed.
364 */
365 if (RT_SUCCESS(rc))
366 rc = pfnVerifyCert(pSignerCert, hCertPaths, pvUser, pErrInfo);
367 }
368 else
369 RTErrInfoSetF(pErrInfo, rc, "Error configuring path builder: %Rrc", rc);
370 RTCrX509CertPathsRelease(hCertPaths);
371 }
372 }
373 /*
374 * Check that the certificate purpose matches what is signed.
375 */
376 else
377 rc = pfnVerifyCert(pSignerCert, NIL_RTCRX509CERTPATHS, pvUser, pErrInfo);
378
379 /*
380 * Find the digest that is signed and reference it so we can replace it
381 * below if necessary.
382 */
383 RTCRDIGEST hDigest = NIL_RTCRDIGEST;
384 uint32_t iDigest = pSignedData->DigestAlgorithms.cItems;
385 while (iDigest-- > 0)
386 if (RTCrX509AlgorithmIdentifier_Compare(&pSignedData->DigestAlgorithms.paItems[iDigest],
387 &pSignerInfo->DigestAlgorithm) == 0)
388 {
389 hDigest = pahDigests[iDigest];
390 uint32_t cRefs = RTCrDigestRetain(hDigest);
391 AssertStmt(cRefs != UINT32_MAX, cRefs = NIL_RTCRDIGEST; rc = VERR_CR_PKCS7_INTERNAL_ERROR);
392 break;
393 }
394 if (hDigest == NIL_RTCRDIGEST && RT_SUCCESS(rc))
395 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_DIGEST_ALGO_NOT_FOUND_IN_LIST,
396 "SignerInfo.DigestAlgorithm %s not found.",
397 pSignerInfo->DigestAlgorithm.Algorithm.szObjId);
398
399 /*
400 * If there are authenticated attributes, we've got more work before we
401 * can verify the signature.
402 */
403 if ( RT_SUCCESS(rc)
404 && RTCrPkcs7Attributes_IsPresent(&pSignerInfo->AuthenticatedAttributes))
405 rc = rtCrPkcs7VerifySignerInfoAuthAttribs(pSignerInfo, pSignedData, &hDigest, fFlags, pErrInfo);
406
407 /*
408 * Verify the signature.
409 */
410 if (RT_SUCCESS(rc))
411 {
412 RTCRPKIXSIGNATURE hSignature;
413 rc = RTCrPkixSignatureCreateByObjId(&hSignature,
414 &pSignerCert->TbsCertificate.SubjectPublicKeyInfo.Algorithm.Algorithm,
415 false /*fSigning*/,
416 &pSignerCert->TbsCertificate.SubjectPublicKeyInfo.SubjectPublicKey,
417 &pSignerInfo->DigestEncryptionAlgorithm.Parameters);
418 if (RT_SUCCESS(rc))
419 {
420 /** @todo Check that DigestEncryptionAlgorithm is compatible with hSignature
421 * (this is not vital). */
422 rc = RTCrPkixSignatureVerifyOctetString(hSignature, hDigest, &pSignerInfo->EncryptedDigest);
423 if (RT_FAILURE(rc))
424 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNATURE_VERIFICATION_FAILED,
425 "Signature verficiation failed: %Rrc", rc);
426 RTCrPkixSignatureRelease(hSignature);
427 }
428 else
429 rc = RTErrInfoSetF(pErrInfo, rc, "Failure to instantiate public key algorithm [IPRT]: %s (%s)",
430 pSignerCert->TbsCertificate.SubjectPublicKeyInfo.Algorithm.Algorithm.szObjId,
431 pSignerInfo->DigestEncryptionAlgorithm.Algorithm.szObjId);
432 }
433
434 RTCrDigestRelease(hDigest);
435 RTCrCertCtxRelease(pSignerCertCtx);
436 return rc;
437}
438
439
440RTDECL(int) RTCrPkcs7VerifySignedData(PCRTCRPKCS7CONTENTINFO pContentInfo, uint32_t fFlags,
441 RTCRSTORE hAdditionalCerts, RTCRSTORE hTrustedCerts,
442 PCRTTIMESPEC pValidationTime, PRTCRPKCS7VERIFYCERTCALLBACK pfnVerifyCert, void *pvUser,
443 PRTERRINFO pErrInfo)
444{
445 /*
446 * Check the input.
447 */
448 if (pfnVerifyCert)
449 AssertPtrReturn(pfnVerifyCert, VERR_INVALID_POINTER);
450 else
451 pfnVerifyCert = RTCrPkcs7VerifyCertCallbackDefault;
452
453 if (!RTCrPkcs7ContentInfo_IsSignedData(pContentInfo))
454 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_NOT_SIGNED_DATA, "Not PKCS #7 SignedData.");
455 PCRTCRPKCS7SIGNEDDATA pSignedData = pContentInfo->u.pSignedData;
456 int rc = RTCrPkcs7SignedData_CheckSanity(pSignedData, 0, pErrInfo, "");
457 if (RT_FAILURE(rc))
458 return rc;
459
460 /*
461 * Hash the content info.
462 */
463 /* Exactly what the content is, for some stupid reason unnecessarily
464 complicated. Figure it out here as we'll need it for the OpenSSL code
465 path as well. */
466 void const *pvContent = pSignedData->ContentInfo.Content.Asn1Core.uData.pv;
467 uint32_t cbContent = pSignedData->ContentInfo.Content.Asn1Core.cb;
468 if (pSignedData->ContentInfo.Content.pEncapsulated)
469 {
470 pvContent = pSignedData->ContentInfo.Content.pEncapsulated->uData.pv;
471 cbContent = pSignedData->ContentInfo.Content.pEncapsulated->cb;
472 }
473
474 /* Check that there aren't too many or too few hash algorithms for our
475 implementation and purposes. */
476 RTCRDIGEST ahDigests[2];
477 uint32_t const cDigests = pSignedData->DigestAlgorithms.cItems;
478 if (!cDigests) /** @todo we might have to support this... */
479 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_NO_DIGEST_ALGORITHMS, "No digest algorithms");
480
481 if (cDigests > RT_ELEMENTS(ahDigests))
482 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_TOO_MANY_DIGEST_ALGORITHMS,
483 "Too many digest algorithm: cAlgorithms=%u", cDigests);
484
485 /* Create the message digest calculators. */
486 rc = VERR_CR_PKCS7_NO_DIGEST_ALGORITHMS;
487 uint32_t i;
488 for (i = 0; i < cDigests; i++)
489 {
490 rc = RTCrDigestCreateByObjId(&ahDigests[i], &pSignedData->DigestAlgorithms.paItems[i].Algorithm);
491 if (RT_FAILURE(rc))
492 {
493 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_DIGEST_CREATE_ERROR, "Error creating digest for '%s': %Rrc",
494 pSignedData->DigestAlgorithms.paItems[i].Algorithm.szObjId, rc);
495 break;
496 }
497 }
498 if (RT_SUCCESS(rc))
499 {
500 /* Hash the content. */
501 for (i = 0; i < cDigests && RT_SUCCESS(rc); i++)
502 {
503 rc = RTCrDigestUpdate(ahDigests[i], pvContent, cbContent);
504 if (RT_SUCCESS(rc))
505 rc = RTCrDigestFinal(ahDigests[i], NULL, 0);
506 }
507 if (RT_SUCCESS(rc))
508 {
509 /*
510 * Validate the signed infos.
511 */
512 rc = VERR_CR_PKCS7_NO_SIGNER_INFOS;
513 for (i = 0; i < pSignedData->SignerInfos.cItems; i++)
514 {
515 rc = rtCrPkcs7VerifySignerInfo(&pSignedData->SignerInfos.paItems[i], pSignedData, ahDigests,
516 fFlags, hAdditionalCerts, hTrustedCerts, pValidationTime,
517 pfnVerifyCert, pvUser, pErrInfo);
518 if (RT_FAILURE(rc))
519 break;
520 }
521 }
522 else
523 rc = RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_DIGEST_CALC_ERROR,
524 "Hashing content failed unexpectedly (i=%u): %Rrc", i, rc);
525
526 /* Clean up digests. */
527 i = cDigests;
528 }
529 while (i-- > 0)
530 {
531 int rc2 = RTCrDigestRelease(ahDigests[i]);
532 AssertRC(rc2);
533 }
534
535
536#ifdef IPRT_WITH_OPENSSL
537 /*
538 * Verify using OpenSSL and combine the results (should be identical).
539 */
540 int rcOssl = rtCrPkcs7VerifySignedDataUsingOpenSsl(pContentInfo, fFlags, hAdditionalCerts, hTrustedCerts,
541 pvContent, cbContent, RT_SUCCESS(rc) ? pErrInfo : NULL);
542 if (RT_SUCCESS(rcOssl) && RT_SUCCESS(rc))
543 return rc;
544// AssertMsg(RT_FAILURE_NP(rcOssl) && RT_FAILURE_NP(rc), ("%Rrc, %Rrc\n", rcOssl, rc));
545 if (RT_FAILURE(rc))
546 return rc;
547 return rcOssl;
548#else
549 return rc;
550#endif
551}
552
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