VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/crypto/pkcs7-sanity.cpp@ 57444

Last change on this file since 57444 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.2 KB
Line 
1/* $Id: pkcs7-sanity.cpp 57358 2015-08-14 15:16:38Z vboxsync $ */
2/** @file
3 * IPRT - Crypto - PKCS \#7, Sanity Checkers.
4 */
5
6/*
7 * Copyright (C) 2006-2015 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
37//#include <iprt/stream.h>
38
39#include "pkcs7-internal.h"
40
41
42static int rtCrPkcs7SignedData_CheckSanityExtra(PCRTCRPKCS7SIGNEDDATA pSignedData, uint32_t fFlags,
43 PRTERRINFO pErrInfo, const char *pszErrorTag)
44{
45 bool const fAuthenticode = RT_BOOL(fFlags & RTCRPKCS7SIGNEDDATA_SANITY_F_AUTHENTICODE);
46
47 //RTAsn1Dump(&pSignedData->SeqCore.Asn1Core, 0, 0, RTAsn1DumpStrmPrintfV, g_pStdOut);
48
49 if ( RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V1) != 0
50 && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V3) != 0
51 && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V4) != 0
52 && RTAsn1Integer_UnsignedCompareWithU32(&pSignedData->Version, RTCRPKCS7SIGNEDDATA_V5) != 0)
53 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNED_DATA_VERSION, "SignedData version is %llu, expected %u",
54 pSignedData->Version.uValue.u, RTCRPKCS7SIGNEDDATA_V1);
55
56 /*
57 * DigestAlgorithms.
58 */
59 if (pSignedData->DigestAlgorithms.cItems == 0) /** @todo this might be too strict */
60 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_SIGNED_DATA_NO_DIGEST_ALGOS, "SignedData.DigestAlgorithms is empty");
61 if (pSignedData->DigestAlgorithms.cItems != 1 && fAuthenticode)
62 return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_NOT_EXACTLY_ONE_DIGEST_ALGO,
63 "SignedData.DigestAlgorithms has more than one algorithm (%u)",
64 pSignedData->DigestAlgorithms.cItems);
65
66 if (fFlags & RTCRPKCS7SIGNEDDATA_SANITY_F_ONLY_KNOWN_HASH)
67 for (uint32_t i = 0; i < pSignedData->DigestAlgorithms.cItems; i++)
68 {
69 if (RTCrX509AlgorithmIdentifier_QueryDigestType(&pSignedData->DigestAlgorithms.paItems[i]) == RTDIGESTTYPE_INVALID)
70 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_UNKNOWN_DIGEST_ALGORITHM,
71 "SignedData.DigestAlgorithms[%i] is not known: %s",
72 i, pSignedData->DigestAlgorithms.paItems[i].Algorithm.szObjId);
73 if (pSignedData->DigestAlgorithms.paItems[i].Parameters.enmType != RTASN1TYPE_NULL)
74 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_DIGEST_PARAMS_NOT_IMPL,
75 "SignedData.DigestAlgorithms[%i] has parameters: tag=%u",
76 i, pSignedData->DigestAlgorithms.paItems[i].Parameters.u.Core.uTag);
77 }
78
79 /*
80 * Certificates.
81 */
82 if ( (fFlags & RTCRPKCS7SIGNEDDATA_SANITY_F_SIGNING_CERT_PRESENT)
83 && pSignedData->Certificates.cItems == 0)
84 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_NO_CERTIFICATES,
85 "SignedData.Certifcates is empty, expected at least one certificate");
86
87 /*
88 * Crls.
89 */
90 if (fAuthenticode && RTAsn1Core_IsPresent(&pSignedData->Crls))
91 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_EXPECTED_NO_CRLS,
92 "SignedData.Crls is not empty as expected for authenticode.");
93 /** @todo check Crls when they become important. */
94
95 /*
96 * SignerInfos.
97 */
98 if (pSignedData->SignerInfos.cItems == 0)
99 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_NO_SIGNER_INFOS, "SignedData.SignerInfos is empty?");
100 if (fAuthenticode && pSignedData->SignerInfos.cItems != 1)
101 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_EXPECTED_ONE_SIGNER_INFO,
102 "SignedData.SignerInfos should have one entry for authenticode: %u",
103 pSignedData->SignerInfos.cItems);
104
105 for (uint32_t i = 0; i < pSignedData->SignerInfos.cItems; i++)
106 {
107 PCRTCRPKCS7SIGNERINFO pSignerInfo = &pSignedData->SignerInfos.paItems[i];
108
109 if (RTAsn1Integer_UnsignedCompareWithU32(&pSignerInfo->Version, RTCRPKCS7SIGNERINFO_V1) != 0)
110 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNER_INFO_VERSION,
111 "SignedData.SignerInfos[%u] version is %llu, expected %u",
112 i, pSignerInfo->Version.uValue.u, RTCRPKCS7SIGNERINFO_V1);
113
114 /* IssuerAndSerialNumber. */
115 int rc = RTCrX509Name_CheckSanity(&pSignerInfo->IssuerAndSerialNumber.Name, 0, pErrInfo,
116 "SignedData.SignerInfos[#].IssuerAndSerialNumber.Name");
117 if (RT_FAILURE(rc))
118 return rc;
119
120 if (pSignerInfo->IssuerAndSerialNumber.SerialNumber.Asn1Core.cb == 0)
121 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNER_INFO_NO_ISSUER_SERIAL_NO,
122 "SignedData.SignerInfos[%u].IssuerAndSerialNumber.SerialNumber is missing (zero length)", i);
123
124 PCRTCRX509CERTIFICATE pCert;
125 pCert = RTCrPkcs7SetOfCerts_FindX509ByIssuerAndSerialNumber(&pSignedData->Certificates,
126 &pSignerInfo->IssuerAndSerialNumber.Name,
127 &pSignerInfo->IssuerAndSerialNumber.SerialNumber);
128 if (!pCert && (fFlags & RTCRPKCS7SIGNEDDATA_SANITY_F_SIGNING_CERT_PRESENT))
129 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNER_CERT_NOT_SHIPPED,
130 "SignedData.SignerInfos[%u].IssuerAndSerialNumber not found in T0.Certificates", i);
131
132 /* DigestAlgorithm */
133 uint32_t j = 0;
134 while ( j < pSignedData->DigestAlgorithms.cItems
135 && RTCrX509AlgorithmIdentifier_Compare(&pSignedData->DigestAlgorithms.paItems[j],
136 &pSignerInfo->DigestAlgorithm) != 0)
137 j++;
138 if (j >= pSignedData->DigestAlgorithms.cItems)
139 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_DIGEST_ALGO_NOT_FOUND_IN_LIST,
140 "SignedData.SignerInfos[%u].DigestAlgorithm (%s) not found in SignedData.DigestAlgorithms",
141 i, pSignerInfo->DigestAlgorithm.Algorithm.szObjId);
142
143 /* Digest encryption algorithm. */
144#if 0 /** @todo Unimportant: Seen timestamp signatures specifying pkcs1-Sha256WithRsaEncryption in SignerInfo and just RSA in the certificate. Figure out how to compare the two. */
145 if ( pCert
146 && RTCrX509AlgorithmIdentifier_Compare(&pSignerInfo->DigestEncryptionAlgorithm,
147 &pCert->TbsCertificate.SubjectPublicKeyInfo.Algorithm) != 0)
148 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_SIGNER_INFO_DIGEST_ENCRYPT_MISMATCH,
149 "SignedData.SignerInfos[%u].DigestEncryptionAlgorithm (%s) mismatch with certificate (%s)",
150 i, pSignerInfo->DigestEncryptionAlgorithm.Algorithm.szObjId,
151 pCert->TbsCertificate.SubjectPublicKeyInfo.Algorithm.Algorithm.szObjId);
152#endif
153
154 /* Authenticated attributes we know. */
155 if (RTCrPkcs7Attributes_IsPresent(&pSignerInfo->AuthenticatedAttributes))
156 {
157 bool fFoundContentInfo = false;
158 bool fFoundMessageDigest = false;
159 for (j = 0; j < pSignerInfo->AuthenticatedAttributes.cItems; j++)
160 {
161 PCRTCRPKCS7ATTRIBUTE pAttrib = &pSignerInfo->AuthenticatedAttributes.paItems[j];
162 if (RTAsn1ObjId_CompareWithString(&pAttrib->Type, RTCR_PKCS9_ID_CONTENT_TYPE_OID) == 0)
163 {
164 if (fFoundContentInfo)
165 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_MISSING_CONTENT_TYPE_ATTRIB,
166 "Multiple authenticated content-type attributes.");
167 fFoundContentInfo = true;
168 AssertReturn(pAttrib->enmType == RTCRPKCS7ATTRIBUTETYPE_OBJ_IDS, VERR_INTERNAL_ERROR_3);
169 if (pAttrib->uValues.pObjIds->cItems != 1)
170 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_BAD_CONTENT_TYPE_ATTRIB,
171 "Expected exactly one value for content-type attrib, found: %u",
172 pAttrib->uValues.pObjIds->cItems);
173 }
174 else if (RTAsn1ObjId_CompareWithString(&pAttrib->Type, RTCR_PKCS9_ID_MESSAGE_DIGEST_OID) == 0)
175 {
176 if (fFoundMessageDigest)
177 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_MISSING_MESSAGE_DIGEST_ATTRIB,
178 "Multiple authenticated message-digest attributes.");
179 fFoundMessageDigest = true;
180 AssertReturn(pAttrib->enmType == RTCRPKCS7ATTRIBUTETYPE_OCTET_STRINGS, VERR_INTERNAL_ERROR_3);
181 if (pAttrib->uValues.pOctetStrings->cItems != 1)
182 return RTErrInfoSetF(pErrInfo, VERR_CR_PKCS7_BAD_CONTENT_TYPE_ATTRIB,
183 "Expected exactly one value for message-digest attrib, found: %u",
184 pAttrib->uValues.pOctetStrings->cItems);
185 }
186 else
187 AssertReturn(pAttrib->enmType == RTCRPKCS7ATTRIBUTETYPE_UNKNOWN, VERR_INTERNAL_ERROR_3);
188 }
189
190 if (!fFoundContentInfo)
191 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_MISSING_CONTENT_TYPE_ATTRIB,
192 "Missing authenticated content-type attribute.");
193 if (!fFoundMessageDigest)
194 return RTErrInfoSet(pErrInfo, VERR_CR_PKCS7_MISSING_MESSAGE_DIGEST_ATTRIB,
195 "Missing authenticated message-digest attribute.");
196 }
197 }
198
199 return VINF_SUCCESS;
200}
201
202
203/*
204 * Generate the code.
205 */
206#include <iprt/asn1-generator-sanity.h>
207
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