VirtualBox

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

Last change on this file since 98103 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

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