VirtualBox

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

Last change on this file since 93115 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

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