1 | /* $Id: spc-sanity.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Microsoft SPC / Authenticode, Sanity Checkers.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2024 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/spc.h>
|
---|
43 |
|
---|
44 | #include <iprt/assert.h>
|
---|
45 | #include <iprt/err.h>
|
---|
46 | #include <iprt/uuid.h>
|
---|
47 |
|
---|
48 | #include "spc-internal.h"
|
---|
49 |
|
---|
50 |
|
---|
51 | RTDECL(int) RTCrSpcIndirectDataContent_CheckSanityEx(PCRTCRSPCINDIRECTDATACONTENT pIndData,
|
---|
52 | PCRTCRPKCS7SIGNEDDATA pSignedData,
|
---|
53 | uint32_t fFlags,
|
---|
54 | PRTERRINFO pErrInfo)
|
---|
55 | {
|
---|
56 | /*
|
---|
57 | * Match up the digest algorithms (page 8, v1.0).
|
---|
58 | */
|
---|
59 | if (pSignedData->SignerInfos.cItems != 1)
|
---|
60 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_NOT_EXACTLY_ONE_SIGNER_INFOS,
|
---|
61 | "SpcIndirectDataContent expects SignedData to have exactly one SignerInfos entries, found: %u",
|
---|
62 | pSignedData->SignerInfos.cItems);
|
---|
63 | if (pSignedData->DigestAlgorithms.cItems != 1)
|
---|
64 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_NOT_EXACTLY_ONE_DIGEST_ALGO,
|
---|
65 | "SpcIndirectDataContent expects SignedData to have exactly one DigestAlgorithms entries, found: %u",
|
---|
66 | pSignedData->DigestAlgorithms.cItems);
|
---|
67 |
|
---|
68 | if (RTCrX509AlgorithmIdentifier_Compare(&pIndData->DigestInfo.DigestAlgorithm, /** @todo not entirely sure about this check... */
|
---|
69 | &pSignedData->SignerInfos.papItems[0]->DigestAlgorithm) != 0)
|
---|
70 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_SIGNED_IND_DATA_DIGEST_ALGO_MISMATCH,
|
---|
71 | "SpcIndirectDataContent DigestInfo and SignerInfos algorithms mismatch: %s vs %s",
|
---|
72 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId,
|
---|
73 | pSignedData->SignerInfos.papItems[0]->DigestAlgorithm.Algorithm.szObjId);
|
---|
74 |
|
---|
75 | if (RTCrX509AlgorithmIdentifier_Compare(&pIndData->DigestInfo.DigestAlgorithm,
|
---|
76 | pSignedData->DigestAlgorithms.papItems[0]) != 0)
|
---|
77 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_IND_DATA_DIGEST_ALGO_NOT_IN_DIGEST_ALGOS,
|
---|
78 | "SpcIndirectDataContent DigestInfo and SignedData.DigestAlgorithms[0] mismatch: %s vs %s",
|
---|
79 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId,
|
---|
80 | pSignedData->DigestAlgorithms.papItems[0]->Algorithm.szObjId);
|
---|
81 |
|
---|
82 | if (fFlags & RTCRSPCINDIRECTDATACONTENT_SANITY_F_ONLY_KNOWN_HASH)
|
---|
83 | {
|
---|
84 | if ( RTCrX509AlgorithmIdentifier_GetDigestType(&pIndData->DigestInfo.DigestAlgorithm, true /*fPureDigestsOnly*/)
|
---|
85 | == RTDIGESTTYPE_INVALID)
|
---|
86 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_UNKNOWN_DIGEST_ALGO,
|
---|
87 | "SpcIndirectDataContent DigestAlgortihm is not known: %s",
|
---|
88 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId);
|
---|
89 | }
|
---|
90 |
|
---|
91 | uint32_t cbDigest = RTCrX509AlgorithmIdentifier_GetDigestSize(&pIndData->DigestInfo.DigestAlgorithm,
|
---|
92 | true /*fPureDigestsOnly*/);
|
---|
93 | if ( pIndData->DigestInfo.Digest.Asn1Core.cb != cbDigest
|
---|
94 | && (cbDigest != UINT32_MAX || (fFlags & RTCRSPCINDIRECTDATACONTENT_SANITY_F_ONLY_KNOWN_HASH)))
|
---|
95 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_IND_DATA_DIGEST_SIZE_MISMATCH,
|
---|
96 | "SpcIndirectDataContent Digest size mismatch with algorithm: %u, expected %u (%s)",
|
---|
97 | pIndData->DigestInfo.Digest.Asn1Core.cb, cbDigest,
|
---|
98 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId);
|
---|
99 |
|
---|
100 | /*
|
---|
101 | * Data.
|
---|
102 | */
|
---|
103 | if (fFlags & RTCRSPCINDIRECTDATACONTENT_SANITY_F_PE_IMAGE)
|
---|
104 | {
|
---|
105 | if ( pIndData->Data.enmType != RTCRSPCAAOVTYPE_PE_IMAGE_DATA
|
---|
106 | || RTAsn1ObjId_CompareWithString(&pIndData->Data.Type, RTCRSPCPEIMAGEDATA_OID) != 0)
|
---|
107 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_EXPECTED_PE_IMAGE_DATA,
|
---|
108 | "SpcIndirectDataContent.Data.Type is %s, expected %s (SpcPeImageData) [enmType=%d]",
|
---|
109 | pIndData->Data.Type.szObjId, RTCRSPCPEIMAGEDATA_OID, pIndData->Data.enmType);
|
---|
110 | if ( pIndData->Data.uValue.pPeImage
|
---|
111 | || !RTCrSpcPeImageData_IsPresent(pIndData->Data.uValue.pPeImage) )
|
---|
112 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_DATA_NOT_PRESENT,
|
---|
113 | "SpcIndirectDataContent.Data.uValue/PEImage is missing");
|
---|
114 |
|
---|
115 | if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_MONIKER
|
---|
116 | && RTCrSpcSerializedObject_IsPresent(pIndData->Data.uValue.pPeImage->T0.File.u.pMoniker) )
|
---|
117 | {
|
---|
118 | PCRTCRSPCSERIALIZEDOBJECT pObj = pIndData->Data.uValue.pPeImage->T0.File.u.pMoniker;
|
---|
119 |
|
---|
120 | if (pObj->Uuid.Asn1Core.cb != sizeof(RTUUID))
|
---|
121 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_BAD_MONIKER_UUID,
|
---|
122 | "SpcIndirectDataContent...MonikerT1.Uuid incorrect size: %u, expected %u.",
|
---|
123 | pObj->Uuid.Asn1Core.cb, sizeof(RTUUID));
|
---|
124 | if (RTUuidCompareStr(pObj->Uuid.Asn1Core.uData.pUuid, RTCRSPCSERIALIZEDOBJECT_UUID_STR) != 0)
|
---|
125 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_UNKNOWN_MONIKER_UUID,
|
---|
126 | "SpcIndirectDataContent...MonikerT1.Uuid mismatch: %RTuuid, expected %s.",
|
---|
127 | pObj->Uuid.Asn1Core.uData.pUuid, RTCRSPCSERIALIZEDOBJECT_UUID_STR);
|
---|
128 |
|
---|
129 | if (pObj->enmType != RTCRSPCSERIALIZEDOBJECTTYPE_ATTRIBUTES)
|
---|
130 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_BAD_MONIKER_CHOICE,
|
---|
131 | "SpcIndirectDataContent...pMoniker->enmType=%d, expected %d.",
|
---|
132 | pObj->enmType, RTCRSPCSERIALIZEDOBJECTTYPE_ATTRIBUTES);
|
---|
133 | if (!pObj->u.pData)
|
---|
134 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_MONIKER_BAD_DATA,
|
---|
135 | "SpcIndirectDataContent...pMoniker->pData is NULL.");
|
---|
136 |
|
---|
137 | uint32_t cPageHashTabs = 0;
|
---|
138 | for (uint32_t i = 0; i < pObj->u.pData->cItems; i++)
|
---|
139 | {
|
---|
140 | PCRTCRSPCSERIALIZEDOBJECTATTRIBUTE pAttr = pObj->u.pData->papItems[i];
|
---|
141 | if ( RTAsn1ObjId_CompareWithString(&pAttr->Type, RTCRSPC_PE_IMAGE_HASHES_V1_OID) == 0
|
---|
142 | || RTAsn1ObjId_CompareWithString(&pAttr->Type, RTCRSPC_PE_IMAGE_HASHES_V2_OID) == 0 )
|
---|
143 | {
|
---|
144 | cPageHashTabs++;
|
---|
145 | AssertPtr(pAttr->u.pPageHashes->pData);
|
---|
146 | }
|
---|
147 | else
|
---|
148 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_PEIMAGE_UNKNOWN_ATTRIBUTE,
|
---|
149 | "SpcIndirectDataContent...MonikerT1 unknown attribute %u: %s.",
|
---|
150 | i, pAttr->Type.szObjId);
|
---|
151 | }
|
---|
152 | if (cPageHashTabs > 0)
|
---|
153 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_PEIMAGE_MULTIPLE_HASH_TABS,
|
---|
154 | "SpcIndirectDataContent...MonikerT1 multiple page hash attributes (%u).", cPageHashTabs);
|
---|
155 |
|
---|
156 | }
|
---|
157 | else if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_FILE
|
---|
158 | && RTCrSpcString_IsPresent(&pIndData->Data.uValue.pPeImage->T0.File.u.pT2->File) )
|
---|
159 | {
|
---|
160 | /* Could check for "<<<Obsolete>>>" here, but it's really irrelevant. */
|
---|
161 | }
|
---|
162 | else if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_URL
|
---|
163 | && RTAsn1String_IsPresent(pIndData->Data.uValue.pPeImage->T0.File.u.pUrl) )
|
---|
164 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_URL_UNEXPECTED,
|
---|
165 | "SpcIndirectDataContent.Data.uValue.pPeImage->File is an URL, expected object Moniker or File.");
|
---|
166 | else
|
---|
167 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_NO_CONTENT,
|
---|
168 | "SpcIndirectDataContent.Data.uValue.pPeImage->File has no content");
|
---|
169 | }
|
---|
170 |
|
---|
171 | return VINF_SUCCESS;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | /*
|
---|
176 | * Generate the standard core code.
|
---|
177 | */
|
---|
178 | #include <iprt/asn1-generator-sanity.h>
|
---|
179 |
|
---|