1 | /* $Id: pkcs7-asn1-decoder.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Crypto - PKCS \#7, Decoder for ASN.1.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2022 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 | #include <iprt/crypto/spc.h>
|
---|
47 | #include <iprt/crypto/tsp.h>
|
---|
48 |
|
---|
49 | #include "pkcs7-internal.h"
|
---|
50 |
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * PKCS #7 ContentInfo
|
---|
54 | */
|
---|
55 | typedef enum RTCRPKCS7CONTENTINFOCHOICE
|
---|
56 | {
|
---|
57 | RTCRPKCS7CONTENTINFOCHOICE_INVALID = 0,
|
---|
58 | RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN,
|
---|
59 | RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA,
|
---|
60 | RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT,
|
---|
61 | RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO,
|
---|
62 | RTCRPKCS7CONTENTINFOCHOICE_END,
|
---|
63 | RTCRPKCS7CONTENTINFOCHOICE_32BIT_HACK = 0x7fffffff
|
---|
64 | } RTCRPKCS7CONTENTINFOCHOICE;
|
---|
65 |
|
---|
66 | static int rtCrPkcs7ContentInfo_DecodeExtra(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTCRPKCS7CONTENTINFO pThis,
|
---|
67 | const char *pszErrorTag)
|
---|
68 | {
|
---|
69 | RT_NOREF_PV(fFlags); RT_NOREF_PV(pszErrorTag);
|
---|
70 | pThis->u.pCore = NULL;
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * Figure the type.
|
---|
74 | */
|
---|
75 | RTCRPKCS7CONTENTINFOCHOICE enmChoice;
|
---|
76 | size_t cbContent = 0;
|
---|
77 | if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRPKCS7SIGNEDDATA_OID) == 0)
|
---|
78 | {
|
---|
79 | enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA;
|
---|
80 | cbContent = sizeof(*pThis->u.pSignedData);
|
---|
81 | }
|
---|
82 | else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRSPCINDIRECTDATACONTENT_OID) == 0)
|
---|
83 | {
|
---|
84 | enmChoice = RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT;
|
---|
85 | cbContent = sizeof(*pThis->u.pIndirectDataContent);
|
---|
86 | }
|
---|
87 | else if (RTAsn1ObjId_CompareWithString(&pThis->ContentType, RTCRTSPTSTINFO_OID) == 0)
|
---|
88 | {
|
---|
89 | enmChoice = RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO;
|
---|
90 | cbContent = sizeof(*pThis->u.pTstInfo);
|
---|
91 | }
|
---|
92 | else
|
---|
93 | {
|
---|
94 | enmChoice = RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN;
|
---|
95 | cbContent = 0;
|
---|
96 | }
|
---|
97 |
|
---|
98 | int rc = VINF_SUCCESS;
|
---|
99 | if (enmChoice != RTCRPKCS7CONTENTINFOCHOICE_UNKNOWN)
|
---|
100 | {
|
---|
101 | /*
|
---|
102 | * Detect CMS octet string format and open the content cursor.
|
---|
103 | *
|
---|
104 | * Current we don't have any octent string content which, they're all
|
---|
105 | * sequences, which make detection so much simpler.
|
---|
106 | */
|
---|
107 | PRTASN1OCTETSTRING pOctetString = &pThis->Content;
|
---|
108 | RTASN1CURSOR ContentCursor;
|
---|
109 | rc = RTAsn1CursorInitSubFromCore(pCursor, &pThis->Content.Asn1Core, &ContentCursor, "Content");
|
---|
110 | if ( RT_SUCCESS(rc)
|
---|
111 | && RTAsn1CursorIsNextEx(&ContentCursor, ASN1_TAG_OCTET_STRING, ASN1_TAGFLAG_PRIMITIVE | ASN1_TAGCLASS_UNIVERSAL))
|
---|
112 | {
|
---|
113 | rc = RTAsn1MemAllocZ(&pThis->Content.EncapsulatedAllocation, (void **)&pThis->Content.pEncapsulated,
|
---|
114 | sizeof(*pOctetString));
|
---|
115 | if (RT_SUCCESS(rc))
|
---|
116 | {
|
---|
117 | pThis->pCmsContent = pOctetString = (PRTASN1OCTETSTRING)pThis->Content.pEncapsulated;
|
---|
118 | rc = RTAsn1OctetString_DecodeAsn1(&ContentCursor, 0, pOctetString, "CmsContent");
|
---|
119 | if (RT_SUCCESS(rc))
|
---|
120 | rc = RTAsn1CursorCheckEnd(&ContentCursor);
|
---|
121 | if (RT_SUCCESS(rc))
|
---|
122 | rc = RTAsn1CursorInitSubFromCore(pCursor, &pOctetString->Asn1Core, &ContentCursor, "CmsContent");
|
---|
123 | }
|
---|
124 | }
|
---|
125 | if (RT_SUCCESS(rc))
|
---|
126 | {
|
---|
127 | /*
|
---|
128 | * Allocate memory for the decoded content.
|
---|
129 | */
|
---|
130 | rc = RTAsn1MemAllocZ(&pOctetString->EncapsulatedAllocation, (void **)&pOctetString->pEncapsulated, cbContent);
|
---|
131 | if (RT_SUCCESS(rc))
|
---|
132 | {
|
---|
133 | pThis->u.pCore = pOctetString->pEncapsulated;
|
---|
134 |
|
---|
135 | /*
|
---|
136 | * Decode it.
|
---|
137 | */
|
---|
138 | switch (enmChoice)
|
---|
139 | {
|
---|
140 | case RTCRPKCS7CONTENTINFOCHOICE_SIGNED_DATA:
|
---|
141 | rc = RTCrPkcs7SignedData_DecodeAsn1(&ContentCursor, 0, pThis->u.pSignedData, "SignedData");
|
---|
142 | break;
|
---|
143 | case RTCRPKCS7CONTENTINFOCHOICE_SPC_INDIRECT_DATA_CONTENT:
|
---|
144 | rc = RTCrSpcIndirectDataContent_DecodeAsn1(&ContentCursor, 0, pThis->u.pIndirectDataContent,
|
---|
145 | "IndirectDataContent");
|
---|
146 | break;
|
---|
147 | case RTCRPKCS7CONTENTINFOCHOICE_TSP_TST_INFO:
|
---|
148 | rc = RTCrTspTstInfo_DecodeAsn1(&ContentCursor, 0, pThis->u.pTstInfo, "TstInfo");
|
---|
149 | break;
|
---|
150 | default:
|
---|
151 | AssertFailed();
|
---|
152 | rc = VERR_IPE_NOT_REACHED_DEFAULT_CASE;
|
---|
153 | break;
|
---|
154 | }
|
---|
155 | if (RT_SUCCESS(rc))
|
---|
156 | rc = RTAsn1CursorCheckOctStrEnd(&ContentCursor, &pThis->Content);
|
---|
157 | if (RT_SUCCESS(rc))
|
---|
158 | return VINF_SUCCESS;
|
---|
159 |
|
---|
160 | RTAsn1MemFree(&pOctetString->EncapsulatedAllocation, pOctetString->pEncapsulated);
|
---|
161 | pOctetString->pEncapsulated = NULL;
|
---|
162 | pThis->u.pCore = NULL;
|
---|
163 | }
|
---|
164 | }
|
---|
165 | }
|
---|
166 | return rc;
|
---|
167 | }
|
---|
168 |
|
---|
169 |
|
---|
170 | /*
|
---|
171 | * Generate the code.
|
---|
172 | */
|
---|
173 | #include <iprt/asn1-generator-asn1-decoder.h>
|
---|
174 |
|
---|