1 | /* $Id: asn1-ut-dyntype-decode.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, Dynamic Type, Decoding.
|
---|
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/asn1.h>
|
---|
43 |
|
---|
44 | #include <iprt/err.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 | #include <iprt/formats/asn1.h>
|
---|
48 |
|
---|
49 |
|
---|
50 |
|
---|
51 | RTDECL(int) RTAsn1DynType_DecodeAsn1(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag)
|
---|
52 | {
|
---|
53 | RT_ZERO(*pDynType);
|
---|
54 |
|
---|
55 | Assert(!(fFlags & RTASN1CURSOR_GET_F_IMPLICIT)); RT_NOREF_PV(fFlags);
|
---|
56 | uint32_t cbSavedLeft = pCursor->cbLeft;
|
---|
57 | uint8_t const *pbSavedCur = pCursor->pbCur;
|
---|
58 |
|
---|
59 | int rc = RTAsn1CursorReadHdr(pCursor, &pDynType->u.Core, pszErrorTag);
|
---|
60 | if (RT_SUCCESS(rc))
|
---|
61 | {
|
---|
62 | pDynType->enmType = RTASN1TYPE_CORE;
|
---|
63 |
|
---|
64 | if (pDynType->u.Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_PRIMITIVE))
|
---|
65 | {
|
---|
66 | switch (pDynType->u.Core.uTag)
|
---|
67 | {
|
---|
68 | case ASN1_TAG_BOOLEAN:
|
---|
69 | pDynType->enmType = RTASN1TYPE_BOOLEAN;
|
---|
70 | break;
|
---|
71 | case ASN1_TAG_INTEGER:
|
---|
72 | pDynType->enmType = RTASN1TYPE_INTEGER;
|
---|
73 | break;
|
---|
74 | //case ASN1_TAG_ENUMERATED:
|
---|
75 | // pDynType->enmType = RTASN1TYPE_ENUMERATED;
|
---|
76 | // break;
|
---|
77 | //case ASN1_TAG_REAL:
|
---|
78 | // pDynType->enmType = RTASN1TYPE_REAL;
|
---|
79 | // break;
|
---|
80 | case ASN1_TAG_BIT_STRING:
|
---|
81 | pDynType->enmType = RTASN1TYPE_BIT_STRING;
|
---|
82 | break;
|
---|
83 | case ASN1_TAG_OCTET_STRING:
|
---|
84 | pDynType->enmType = RTASN1TYPE_OCTET_STRING;
|
---|
85 | break;
|
---|
86 | case ASN1_TAG_NULL:
|
---|
87 | pDynType->enmType = RTASN1TYPE_NULL;
|
---|
88 | break;
|
---|
89 | case ASN1_TAG_SEQUENCE:
|
---|
90 | RT_ZERO(*pDynType);
|
---|
91 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 SEQUENCE shall be constructed.");
|
---|
92 | case ASN1_TAG_SET:
|
---|
93 | RT_ZERO(*pDynType);
|
---|
94 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 SET shall be constructed.");
|
---|
95 | case ASN1_TAG_OID:
|
---|
96 | pDynType->enmType = RTASN1TYPE_OBJID;
|
---|
97 | break;
|
---|
98 | //case ASN1_TAG_RELATIVE_OID:
|
---|
99 | // pDynType->enmType = RTASN1TYPE_RELATIVE_OBJID;
|
---|
100 | // break;
|
---|
101 | case ASN1_TAG_UTC_TIME:
|
---|
102 | case ASN1_TAG_GENERALIZED_TIME:
|
---|
103 | pDynType->enmType = RTASN1TYPE_TIME;
|
---|
104 | break;
|
---|
105 | case ASN1_TAG_UTF8_STRING:
|
---|
106 | case ASN1_TAG_NUMERIC_STRING:
|
---|
107 | case ASN1_TAG_PRINTABLE_STRING:
|
---|
108 | case ASN1_TAG_T61_STRING:
|
---|
109 | case ASN1_TAG_VIDEOTEX_STRING:
|
---|
110 | case ASN1_TAG_IA5_STRING:
|
---|
111 | case ASN1_TAG_GRAPHIC_STRING:
|
---|
112 | case ASN1_TAG_VISIBLE_STRING:
|
---|
113 | case ASN1_TAG_UNIVERSAL_STRING:
|
---|
114 | case ASN1_TAG_GENERAL_STRING:
|
---|
115 | case ASN1_TAG_BMP_STRING:
|
---|
116 | pDynType->enmType = RTASN1TYPE_STRING;
|
---|
117 | break;
|
---|
118 | //case ASN1_TAG_CHARACTER_STRING:
|
---|
119 | // pDynType->enmType = RTASN1TYPE_CHARACTER_STRING;
|
---|
120 | // break;
|
---|
121 |
|
---|
122 | default:
|
---|
123 | RT_ZERO(*pDynType);
|
---|
124 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_TAG_NOT_IMPL,
|
---|
125 | "Primitive tag %u (%#x) not implemented.",
|
---|
126 | pDynType->u.Core.uTag, pDynType->u.Core.uTag);
|
---|
127 | }
|
---|
128 | }
|
---|
129 | else if (pDynType->u.Core.fClass == (ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED))
|
---|
130 | switch (pDynType->u.Core.uTag)
|
---|
131 | {
|
---|
132 | case ASN1_TAG_BOOLEAN:
|
---|
133 | RT_ZERO(*pDynType);
|
---|
134 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 BOOLEAN shall be primitive.");
|
---|
135 | case ASN1_TAG_INTEGER:
|
---|
136 | RT_ZERO(*pDynType);
|
---|
137 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 BOOLEAN shall be primitive.");
|
---|
138 | case ASN1_TAG_ENUMERATED:
|
---|
139 | RT_ZERO(*pDynType);
|
---|
140 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 ENUMERATED shall be primitive.");
|
---|
141 | case ASN1_TAG_REAL:
|
---|
142 | RT_ZERO(*pDynType);
|
---|
143 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 REAL shall be primitive.");
|
---|
144 | case ASN1_TAG_BIT_STRING:
|
---|
145 | pDynType->enmType = RTASN1TYPE_BIT_STRING;
|
---|
146 | break;
|
---|
147 | case ASN1_TAG_OCTET_STRING:
|
---|
148 | pDynType->enmType = RTASN1TYPE_OCTET_STRING;
|
---|
149 | break;
|
---|
150 | case ASN1_TAG_NULL:
|
---|
151 | RT_ZERO(*pDynType);
|
---|
152 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 NULL shall be primitive.");
|
---|
153 | case ASN1_TAG_SEQUENCE:
|
---|
154 | #if 0
|
---|
155 | pDynType->enmType = RTASN1TYPE_SEQUENCE_CORE;
|
---|
156 | pDynType->u.SeqCore.Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
|
---|
157 | RTAsn1CursorSkip(pCursor, pDynType->u.Core.cb);
|
---|
158 | return VINF_SUCCESS;
|
---|
159 | #else
|
---|
160 | pDynType->enmType = RTASN1TYPE_CORE;
|
---|
161 | #endif
|
---|
162 | break;
|
---|
163 | case ASN1_TAG_SET:
|
---|
164 | #if 0
|
---|
165 | pDynType->enmType = RTASN1TYPE_SET_CORE;
|
---|
166 | pDynType->u.SeqCore.Asn1Core.fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
|
---|
167 | RTAsn1CursorSkip(pCursor, pDynType->u.Core.cb);
|
---|
168 | return VINF_SUCCESS;
|
---|
169 | #else
|
---|
170 | pDynType->enmType = RTASN1TYPE_CORE;
|
---|
171 | #endif
|
---|
172 | break;
|
---|
173 | case ASN1_TAG_OID:
|
---|
174 | RT_ZERO(*pDynType);
|
---|
175 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 OBJECT ID shall be primitive.");
|
---|
176 | case ASN1_TAG_RELATIVE_OID:
|
---|
177 | RT_ZERO(*pDynType);
|
---|
178 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_BAD_TAG, "ASN.1 RELATIVE OID shall be primitive.");
|
---|
179 |
|
---|
180 | case ASN1_TAG_UTF8_STRING:
|
---|
181 | case ASN1_TAG_NUMERIC_STRING:
|
---|
182 | case ASN1_TAG_PRINTABLE_STRING:
|
---|
183 | case ASN1_TAG_T61_STRING:
|
---|
184 | case ASN1_TAG_VIDEOTEX_STRING:
|
---|
185 | case ASN1_TAG_IA5_STRING:
|
---|
186 | case ASN1_TAG_GRAPHIC_STRING:
|
---|
187 | case ASN1_TAG_VISIBLE_STRING:
|
---|
188 | case ASN1_TAG_UNIVERSAL_STRING:
|
---|
189 | case ASN1_TAG_GENERAL_STRING:
|
---|
190 | case ASN1_TAG_BMP_STRING:
|
---|
191 | pDynType->enmType = RTASN1TYPE_STRING;
|
---|
192 | break;
|
---|
193 | //case ASN1_TAG_CHARACTER_STRING:
|
---|
194 | // pDynType->enmType = RTASN1TYPE_CHARACTER_STRING;
|
---|
195 | // break;
|
---|
196 |
|
---|
197 | default:
|
---|
198 | RT_ZERO(*pDynType);
|
---|
199 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_DYNTYPE_TAG_NOT_IMPL,
|
---|
200 | "Constructed tag %u (%#x) not implemented.",
|
---|
201 | pDynType->u.Core.uTag, pDynType->u.Core.uTag);
|
---|
202 | }
|
---|
203 | else
|
---|
204 | Assert(pDynType->enmType == RTASN1TYPE_CORE);
|
---|
205 |
|
---|
206 | /*
|
---|
207 | * Restore the cursor and redo with specific type.
|
---|
208 | */
|
---|
209 | pCursor->pbCur = pbSavedCur;
|
---|
210 | pCursor->cbLeft = cbSavedLeft;
|
---|
211 |
|
---|
212 | switch (pDynType->enmType)
|
---|
213 | {
|
---|
214 | case RTASN1TYPE_INTEGER:
|
---|
215 | rc = RTAsn1Integer_DecodeAsn1(pCursor, 0, &pDynType->u.Integer, pszErrorTag);
|
---|
216 | break;
|
---|
217 | case RTASN1TYPE_BOOLEAN:
|
---|
218 | rc = RTAsn1Boolean_DecodeAsn1(pCursor, 0, &pDynType->u.Boolean, pszErrorTag);
|
---|
219 | break;
|
---|
220 | case RTASN1TYPE_OBJID:
|
---|
221 | rc = RTAsn1ObjId_DecodeAsn1(pCursor, 0, &pDynType->u.ObjId, pszErrorTag);
|
---|
222 | break;
|
---|
223 | case RTASN1TYPE_BIT_STRING:
|
---|
224 | rc = RTAsn1BitString_DecodeAsn1(pCursor, 0, &pDynType->u.BitString, pszErrorTag);
|
---|
225 | break;
|
---|
226 | case RTASN1TYPE_OCTET_STRING:
|
---|
227 | rc = RTAsn1OctetString_DecodeAsn1(pCursor, 0, &pDynType->u.OctetString, pszErrorTag);
|
---|
228 | break;
|
---|
229 | case RTASN1TYPE_NULL:
|
---|
230 | rc = RTAsn1Null_DecodeAsn1(pCursor, 0, &pDynType->u.Asn1Null, pszErrorTag);
|
---|
231 | break;
|
---|
232 | case RTASN1TYPE_TIME:
|
---|
233 | rc = RTAsn1Time_DecodeAsn1(pCursor, 0, &pDynType->u.Time, pszErrorTag);
|
---|
234 | break;
|
---|
235 | case RTASN1TYPE_STRING:
|
---|
236 | rc = RTAsn1String_DecodeAsn1(pCursor, 0, &pDynType->u.String, pszErrorTag);
|
---|
237 | break;
|
---|
238 | case RTASN1TYPE_CORE:
|
---|
239 | rc = RTAsn1Core_DecodeAsn1(pCursor, 0, &pDynType->u.Core, pszErrorTag);
|
---|
240 | break;
|
---|
241 | default:
|
---|
242 | AssertFailedReturn(VERR_INTERNAL_ERROR_4);
|
---|
243 | }
|
---|
244 | if (RT_SUCCESS(rc))
|
---|
245 | return rc;
|
---|
246 | }
|
---|
247 | RT_ZERO(*pDynType);
|
---|
248 | return rc;
|
---|
249 | }
|
---|
250 |
|
---|