1 | /* $Id: spc-sanity.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - Microsoft SPC / Authenticode, 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/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_QueryDigestType(&pIndData->DigestInfo.DigestAlgorithm) == RTDIGESTTYPE_INVALID)
|
---|
85 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_UNKNOWN_DIGEST_ALGO,
|
---|
86 | "SpcIndirectDataContent DigestAlgortihm is not known: %s",
|
---|
87 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId);
|
---|
88 | }
|
---|
89 |
|
---|
90 | uint32_t cbDigest = RTCrX509AlgorithmIdentifier_QueryDigestSize(&pIndData->DigestInfo.DigestAlgorithm);
|
---|
91 | if ( pIndData->DigestInfo.Digest.Asn1Core.cb != cbDigest
|
---|
92 | && (cbDigest != UINT32_MAX || (fFlags & RTCRSPCINDIRECTDATACONTENT_SANITY_F_ONLY_KNOWN_HASH)))
|
---|
93 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_IND_DATA_DIGEST_SIZE_MISMATCH,
|
---|
94 | "SpcIndirectDataContent Digest size mismatch with algorithm: %u, expected %u (%s)",
|
---|
95 | pIndData->DigestInfo.Digest.Asn1Core.cb, cbDigest,
|
---|
96 | pIndData->DigestInfo.DigestAlgorithm.Algorithm.szObjId);
|
---|
97 |
|
---|
98 | /*
|
---|
99 | * Data.
|
---|
100 | */
|
---|
101 | if (fFlags & RTCRSPCINDIRECTDATACONTENT_SANITY_F_PE_IMAGE)
|
---|
102 | {
|
---|
103 | if ( pIndData->Data.enmType != RTCRSPCAAOVTYPE_PE_IMAGE_DATA
|
---|
104 | || RTAsn1ObjId_CompareWithString(&pIndData->Data.Type, RTCRSPCPEIMAGEDATA_OID) != 0)
|
---|
105 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_EXPECTED_PE_IMAGE_DATA,
|
---|
106 | "SpcIndirectDataContent.Data.Type is %s, expected %s (SpcPeImageData) [enmType=%d]",
|
---|
107 | pIndData->Data.Type.szObjId, RTCRSPCPEIMAGEDATA_OID, pIndData->Data.enmType);
|
---|
108 | if ( pIndData->Data.uValue.pPeImage
|
---|
109 | || !RTCrSpcPeImageData_IsPresent(pIndData->Data.uValue.pPeImage) )
|
---|
110 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_DATA_NOT_PRESENT,
|
---|
111 | "SpcIndirectDataContent.Data.uValue/PEImage is missing");
|
---|
112 |
|
---|
113 | if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_MONIKER
|
---|
114 | && RTCrSpcSerializedObject_IsPresent(pIndData->Data.uValue.pPeImage->T0.File.u.pMoniker) )
|
---|
115 | {
|
---|
116 | PCRTCRSPCSERIALIZEDOBJECT pObj = pIndData->Data.uValue.pPeImage->T0.File.u.pMoniker;
|
---|
117 |
|
---|
118 | if (pObj->Uuid.Asn1Core.cb != sizeof(RTUUID))
|
---|
119 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_BAD_MONIKER_UUID,
|
---|
120 | "SpcIndirectDataContent...MonikerT1.Uuid incorrect size: %u, expected %u.",
|
---|
121 | pObj->Uuid.Asn1Core.cb, sizeof(RTUUID));
|
---|
122 | if (RTUuidCompareStr(pObj->Uuid.Asn1Core.uData.pUuid, RTCRSPCSERIALIZEDOBJECT_UUID_STR) != 0)
|
---|
123 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_UNKNOWN_MONIKER_UUID,
|
---|
124 | "SpcIndirectDataContent...MonikerT1.Uuid mismatch: %RTuuid, expected %s.",
|
---|
125 | pObj->Uuid.Asn1Core.uData.pUuid, RTCRSPCSERIALIZEDOBJECT_UUID_STR);
|
---|
126 |
|
---|
127 | if (pObj->enmType != RTCRSPCSERIALIZEDOBJECTTYPE_ATTRIBUTES)
|
---|
128 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_BAD_MONIKER_CHOICE,
|
---|
129 | "SpcIndirectDataContent...pMoniker->enmType=%d, expected %d.",
|
---|
130 | pObj->enmType, RTCRSPCSERIALIZEDOBJECTTYPE_ATTRIBUTES);
|
---|
131 | if (!pObj->u.pData)
|
---|
132 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_MONIKER_BAD_DATA,
|
---|
133 | "SpcIndirectDataContent...pMoniker->pData is NULL.");
|
---|
134 |
|
---|
135 | uint32_t cPageHashTabs = 0;
|
---|
136 | for (uint32_t i = 0; i < pObj->u.pData->cItems; i++)
|
---|
137 | {
|
---|
138 | PCRTCRSPCSERIALIZEDOBJECTATTRIBUTE pAttr = pObj->u.pData->papItems[i];
|
---|
139 | if ( RTAsn1ObjId_CompareWithString(&pAttr->Type, RTCRSPC_PE_IMAGE_HASHES_V1_OID) == 0
|
---|
140 | || RTAsn1ObjId_CompareWithString(&pAttr->Type, RTCRSPC_PE_IMAGE_HASHES_V2_OID) == 0 )
|
---|
141 | {
|
---|
142 | cPageHashTabs++;
|
---|
143 | AssertPtr(pAttr->u.pPageHashes->pData);
|
---|
144 | }
|
---|
145 | else
|
---|
146 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_PEIMAGE_UNKNOWN_ATTRIBUTE,
|
---|
147 | "SpcIndirectDataContent...MonikerT1 unknown attribute %u: %s.",
|
---|
148 | i, pAttr->Type.szObjId);
|
---|
149 | }
|
---|
150 | if (cPageHashTabs > 0)
|
---|
151 | return RTErrInfoSetF(pErrInfo, VERR_CR_SPC_PEIMAGE_MULTIPLE_HASH_TABS,
|
---|
152 | "SpcIndirectDataContent...MonikerT1 multiple page hash attributes (%u).", cPageHashTabs);
|
---|
153 |
|
---|
154 | }
|
---|
155 | else if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_FILE
|
---|
156 | && RTCrSpcString_IsPresent(&pIndData->Data.uValue.pPeImage->T0.File.u.pT2->File) )
|
---|
157 | {
|
---|
158 | /* Could check for "<<<Obsolete>>>" here, but it's really irrelevant. */
|
---|
159 | }
|
---|
160 | else if ( pIndData->Data.uValue.pPeImage->T0.File.enmChoice == RTCRSPCLINKCHOICE_URL
|
---|
161 | && RTAsn1String_IsPresent(pIndData->Data.uValue.pPeImage->T0.File.u.pUrl) )
|
---|
162 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_URL_UNEXPECTED,
|
---|
163 | "SpcIndirectDataContent.Data.uValue.pPeImage->File is an URL, expected object Moniker or File.");
|
---|
164 | else
|
---|
165 | return RTErrInfoSet(pErrInfo, VERR_CR_SPC_PEIMAGE_NO_CONTENT,
|
---|
166 | "SpcIndirectDataContent.Data.uValue.pPeImage->File has no content");
|
---|
167 | }
|
---|
168 |
|
---|
169 | return VINF_SUCCESS;
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /*
|
---|
174 | * Generate the standard core code.
|
---|
175 | */
|
---|
176 | #include <iprt/asn1-generator-sanity.h>
|
---|
177 |
|
---|