1 | /* $Id: asn1-cursor.cpp 73662 2018-08-14 16:13:43Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, Basic Operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2017 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include "internal/iprt.h"
|
---|
32 | #include <iprt/asn1.h>
|
---|
33 |
|
---|
34 | #include <iprt/alloca.h>
|
---|
35 | #include <iprt/err.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/ctype.h>
|
---|
38 |
|
---|
39 | #include <iprt/formats/asn1.h>
|
---|
40 |
|
---|
41 |
|
---|
42 | /*********************************************************************************************************************************
|
---|
43 | * Defined Constants And Macros *
|
---|
44 | *********************************************************************************************************************************/
|
---|
45 | /** @def RTASN1_MAX_NESTING
|
---|
46 | * The maximum nesting depth we allow. This limit is enforced to avoid running
|
---|
47 | * out of stack due to malformed ASN.1 input.
|
---|
48 | *
|
---|
49 | * For reference, 'RTSignTool verify-exe RTSignTool.exe', requires a value of 15
|
---|
50 | * to work without hitting the limit for signatures with simple timestamps, and
|
---|
51 | * 23 (amd64/rel = ~3KB) for the new microsoft timestamp counter signatures.
|
---|
52 | */
|
---|
53 | #ifdef IN_RING3
|
---|
54 | # define RTASN1_MAX_NESTING 64
|
---|
55 | #else
|
---|
56 | # define RTASN1_MAX_NESTING 32
|
---|
57 | #endif
|
---|
58 |
|
---|
59 |
|
---|
60 |
|
---|
61 | RTDECL(PRTASN1CURSOR) RTAsn1CursorInitPrimary(PRTASN1CURSORPRIMARY pPrimaryCursor, void const *pvFirst, uint32_t cb,
|
---|
62 | PRTERRINFO pErrInfo, PCRTASN1ALLOCATORVTABLE pAllocator, uint32_t fFlags,
|
---|
63 | const char *pszErrorTag)
|
---|
64 | {
|
---|
65 | pPrimaryCursor->Cursor.pbCur = (uint8_t const *)pvFirst;
|
---|
66 | pPrimaryCursor->Cursor.cbLeft = cb;
|
---|
67 | pPrimaryCursor->Cursor.fFlags = (uint8_t)fFlags; Assert(fFlags <= UINT8_MAX);
|
---|
68 | pPrimaryCursor->Cursor.cDepth = 0;
|
---|
69 | pPrimaryCursor->Cursor.abReserved[0] = 0;
|
---|
70 | pPrimaryCursor->Cursor.abReserved[1] = 0;
|
---|
71 | pPrimaryCursor->Cursor.pPrimary = pPrimaryCursor;
|
---|
72 | pPrimaryCursor->Cursor.pUp = NULL;
|
---|
73 | pPrimaryCursor->Cursor.pszErrorTag = pszErrorTag;
|
---|
74 | pPrimaryCursor->pErrInfo = pErrInfo;
|
---|
75 | pPrimaryCursor->pAllocator = pAllocator;
|
---|
76 | return &pPrimaryCursor->Cursor;
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | RTDECL(int) RTAsn1CursorInitSub(PRTASN1CURSOR pParent, uint32_t cb, PRTASN1CURSOR pChild, const char *pszErrorTag)
|
---|
81 | {
|
---|
82 | AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
83 | AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
|
---|
84 |
|
---|
85 | pChild->pbCur = pParent->pbCur;
|
---|
86 | pChild->cbLeft = cb;
|
---|
87 | pChild->fFlags = pParent->fFlags;
|
---|
88 | pChild->cDepth = pParent->cDepth + 1;
|
---|
89 | AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
|
---|
90 | pChild->abReserved[0] = 0;
|
---|
91 | pChild->abReserved[1] = 0;
|
---|
92 | pChild->pPrimary = pParent->pPrimary;
|
---|
93 | pChild->pUp = pParent;
|
---|
94 | pChild->pszErrorTag = pszErrorTag;
|
---|
95 |
|
---|
96 | AssertReturn(pParent->cbLeft >= cb, VERR_ASN1_INTERNAL_ERROR_3);
|
---|
97 | pParent->pbCur += cb;
|
---|
98 | pParent->cbLeft -= cb;
|
---|
99 |
|
---|
100 | return VINF_SUCCESS;
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | RTDECL(int) RTAsn1CursorInitSubFromCore(PRTASN1CURSOR pParent, PRTASN1CORE pAsn1Core,
|
---|
105 | PRTASN1CURSOR pChild, const char *pszErrorTag)
|
---|
106 | {
|
---|
107 | AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
108 | AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
|
---|
109 |
|
---|
110 | pChild->pbCur = pAsn1Core->uData.pu8;
|
---|
111 | pChild->cbLeft = pAsn1Core->cb;
|
---|
112 | pChild->fFlags = pParent->fFlags;
|
---|
113 | pChild->cDepth = pParent->cDepth + 1;
|
---|
114 | AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
|
---|
115 | pChild->abReserved[0] = 0;
|
---|
116 | pChild->abReserved[1] = 0;
|
---|
117 | pChild->pPrimary = pParent->pPrimary;
|
---|
118 | pChild->pUp = pParent;
|
---|
119 | pChild->pszErrorTag = pszErrorTag;
|
---|
120 |
|
---|
121 | return VINF_SUCCESS;
|
---|
122 | }
|
---|
123 |
|
---|
124 |
|
---|
125 | RTDECL(int) RTAsn1CursorSetInfoV(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, va_list va)
|
---|
126 | {
|
---|
127 | PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
|
---|
128 | if (pErrInfo)
|
---|
129 | {
|
---|
130 | /* Format the message. */
|
---|
131 | RTErrInfoSetV(pErrInfo, rc, pszMsg, va);
|
---|
132 |
|
---|
133 | /* Add the prefixes. This isn't the fastest way, but it's the one
|
---|
134 | which eats the least stack. */
|
---|
135 | char *pszBuf = pErrInfo->pszMsg;
|
---|
136 | size_t cbBuf = pErrInfo->cbMsg;
|
---|
137 | if (pszBuf && cbBuf > 32)
|
---|
138 | {
|
---|
139 | size_t cbMove = strlen(pszBuf) + 1;
|
---|
140 |
|
---|
141 | /* Make sure there is a ': '. */
|
---|
142 | bool fFirst = false;
|
---|
143 | if (pszMsg[0] != '%' || pszMsg[1] != 's' || pszMsg[2] != ':')
|
---|
144 | {
|
---|
145 | if (cbMove + 2 < cbBuf)
|
---|
146 | {
|
---|
147 | memmove(pszBuf + 2, pszBuf, cbMove);
|
---|
148 | pszBuf[0] = ':';
|
---|
149 | pszBuf[1] = ' ';
|
---|
150 | cbMove += 2;
|
---|
151 | fFirst = true;
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /* Add the prefixes from the cursor chain. */
|
---|
156 | while (pCursor)
|
---|
157 | {
|
---|
158 | if (pCursor->pszErrorTag)
|
---|
159 | {
|
---|
160 | size_t cchErrorTag = strlen(pCursor->pszErrorTag);
|
---|
161 | if (cchErrorTag + !fFirst + cbMove > cbBuf)
|
---|
162 | break;
|
---|
163 | memmove(pszBuf + cchErrorTag + !fFirst, pszBuf, cbMove);
|
---|
164 | memcpy(pszBuf, pCursor->pszErrorTag, cchErrorTag);
|
---|
165 | if (!fFirst)
|
---|
166 | pszBuf[cchErrorTag] = '.';
|
---|
167 | cbMove += cchErrorTag + !fFirst;
|
---|
168 | fFirst = false;
|
---|
169 | }
|
---|
170 | pCursor = pCursor->pUp;
|
---|
171 | }
|
---|
172 | }
|
---|
173 | }
|
---|
174 |
|
---|
175 | return rc;
|
---|
176 | }
|
---|
177 |
|
---|
178 |
|
---|
179 | RTDECL(int) RTAsn1CursorSetInfo(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, ...)
|
---|
180 | {
|
---|
181 | va_list va;
|
---|
182 | va_start(va, pszMsg);
|
---|
183 | rc = RTAsn1CursorSetInfoV(pCursor, rc, pszMsg, va);
|
---|
184 | va_end(va);
|
---|
185 | return rc;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | RTDECL(bool) RTAsn1CursorIsEnd(PRTASN1CURSOR pCursor)
|
---|
190 | {
|
---|
191 | return pCursor->cbLeft == 0;
|
---|
192 | }
|
---|
193 |
|
---|
194 |
|
---|
195 | RTDECL(int) RTAsn1CursorCheckEnd(PRTASN1CURSOR pCursor)
|
---|
196 | {
|
---|
197 | if (pCursor->cbLeft == 0)
|
---|
198 | return VINF_SUCCESS;
|
---|
199 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
|
---|
200 | "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
|
---|
201 | }
|
---|
202 |
|
---|
203 |
|
---|
204 | RTDECL(PRTASN1ALLOCATION) RTAsn1CursorInitAllocation(PRTASN1CURSOR pCursor, PRTASN1ALLOCATION pAllocation)
|
---|
205 | {
|
---|
206 | pAllocation->cbAllocated = 0;
|
---|
207 | pAllocation->cReallocs = 0;
|
---|
208 | pAllocation->uReserved0 = 0;
|
---|
209 | pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
|
---|
210 | return pAllocation;
|
---|
211 | }
|
---|
212 |
|
---|
213 |
|
---|
214 | RTDECL(PRTASN1ARRAYALLOCATION) RTAsn1CursorInitArrayAllocation(PRTASN1CURSOR pCursor, PRTASN1ARRAYALLOCATION pAllocation,
|
---|
215 | size_t cbEntry)
|
---|
216 | {
|
---|
217 | Assert(cbEntry >= sizeof(RTASN1CORE));
|
---|
218 | Assert(cbEntry < _1M);
|
---|
219 | Assert(RT_ALIGN_Z(cbEntry, sizeof(void *)) == cbEntry);
|
---|
220 | pAllocation->cbEntry = (uint32_t)cbEntry;
|
---|
221 | pAllocation->cPointersAllocated = 0;
|
---|
222 | pAllocation->cEntriesAllocated = 0;
|
---|
223 | pAllocation->cResizeCalls = 0;
|
---|
224 | pAllocation->uReserved0 = 0;
|
---|
225 | pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
|
---|
226 | return pAllocation;
|
---|
227 | }
|
---|
228 |
|
---|
229 |
|
---|
230 | RTDECL(int) RTAsn1CursorReadHdr(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
|
---|
231 | {
|
---|
232 | /*
|
---|
233 | * Initialize the return structure in case of failure.
|
---|
234 | */
|
---|
235 | pAsn1Core->uTag = 0;
|
---|
236 | pAsn1Core->fClass = 0;
|
---|
237 | pAsn1Core->uRealTag = 0;
|
---|
238 | pAsn1Core->fRealClass = 0;
|
---|
239 | pAsn1Core->cbHdr = 0;
|
---|
240 | pAsn1Core->cb = 0;
|
---|
241 | pAsn1Core->fFlags = 0;
|
---|
242 | pAsn1Core->uData.pv = NULL;
|
---|
243 | pAsn1Core->pOps = NULL;
|
---|
244 |
|
---|
245 | /*
|
---|
246 | * The header has at least two bytes: Type & length.
|
---|
247 | */
|
---|
248 | if (pCursor->cbLeft >= 2)
|
---|
249 | {
|
---|
250 | uint32_t uTag = pCursor->pbCur[0];
|
---|
251 | uint32_t cb = pCursor->pbCur[1];
|
---|
252 | pCursor->cbLeft -= 2;
|
---|
253 | pCursor->pbCur += 2;
|
---|
254 |
|
---|
255 | pAsn1Core->uRealTag = pAsn1Core->uTag = uTag & ASN1_TAG_MASK;
|
---|
256 | pAsn1Core->fRealClass = pAsn1Core->fClass = uTag & ~ASN1_TAG_MASK;
|
---|
257 | pAsn1Core->cbHdr = 2;
|
---|
258 | if ((uTag & ASN1_TAG_MASK) == ASN1_TAG_USE_LONG_FORM)
|
---|
259 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_LONG_TAG,
|
---|
260 | "%s: Implement parsing of tags > 30: %#x (length=%#x)", pszErrorTag, uTag, cb);
|
---|
261 |
|
---|
262 | /* Extended length field? */
|
---|
263 | if (cb & RT_BIT(7))
|
---|
264 | {
|
---|
265 | if (cb != RT_BIT(7))
|
---|
266 | {
|
---|
267 | /* Definite form. */
|
---|
268 | uint8_t cbEnc = cb & 0x7f;
|
---|
269 | if (cbEnc > pCursor->cbLeft)
|
---|
270 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
271 | "%s: Extended BER length field longer than available data: %#x vs %#x (uTag=%#x)",
|
---|
272 | pszErrorTag, cbEnc, pCursor->cbLeft, uTag);
|
---|
273 | switch (cbEnc)
|
---|
274 | {
|
---|
275 | case 1:
|
---|
276 | cb = pCursor->pbCur[0];
|
---|
277 | break;
|
---|
278 | case 2:
|
---|
279 | cb = RT_MAKE_U16(pCursor->pbCur[1], pCursor->pbCur[0]);
|
---|
280 | break;
|
---|
281 | case 3:
|
---|
282 | cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0], 0);
|
---|
283 | break;
|
---|
284 | case 4:
|
---|
285 | cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[3], pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0]);
|
---|
286 | break;
|
---|
287 | default:
|
---|
288 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
289 | "%s: Too long/short extended BER length field: %#x (uTag=%#x)",
|
---|
290 | pszErrorTag, cbEnc, uTag);
|
---|
291 | }
|
---|
292 | pCursor->cbLeft -= cbEnc;
|
---|
293 | pCursor->pbCur += cbEnc;
|
---|
294 | pAsn1Core->cbHdr += cbEnc;
|
---|
295 |
|
---|
296 | /* Check the length encoding efficiency (T-REC-X.690-200811 10.1, 9.1). */
|
---|
297 | if (pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER))
|
---|
298 | {
|
---|
299 | if (cb <= 0x7f)
|
---|
300 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
301 | "%s: Invalid DER/CER length encoding: cbEnc=%u cb=%#x uTag=%#x",
|
---|
302 | pszErrorTag, cbEnc, cb, uTag);
|
---|
303 | uint8_t cbNeeded;
|
---|
304 | if (cb <= 0x000000ff) cbNeeded = 1;
|
---|
305 | else if (cb <= 0x0000ffff) cbNeeded = 2;
|
---|
306 | else if (cb <= 0x00ffffff) cbNeeded = 3;
|
---|
307 | else cbNeeded = 4;
|
---|
308 | if (cbNeeded != cbEnc)
|
---|
309 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
310 | "%s: Invalid DER/CER length encoding: cb=%#x uTag=%#x cbEnc=%u cbNeeded=%u",
|
---|
311 | pszErrorTag, cb, uTag, cbEnc, cbNeeded);
|
---|
312 | }
|
---|
313 | }
|
---|
314 | /* Indefinite form. */
|
---|
315 | else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
|
---|
316 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_IDEFINITE_LENGTH,
|
---|
317 | "%s: Indefinite length form not allowed in DER mode (uTag=%#x).", pszErrorTag, uTag);
|
---|
318 | else
|
---|
319 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_IDEFINITE_LENGTH_NOT_SUP,
|
---|
320 | "%s: Indefinite BER/CER length not supported (uTag=%#x)", pszErrorTag, uTag);
|
---|
321 | }
|
---|
322 |
|
---|
323 | /* Check if the length makes sense. */
|
---|
324 | if (cb > pCursor->cbLeft)
|
---|
325 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH,
|
---|
326 | "%s: BER value length out of bounds: %#x (max=%#x uTag=%#x)",
|
---|
327 | pszErrorTag, cb, pCursor->cbLeft, uTag);
|
---|
328 |
|
---|
329 | pAsn1Core->fFlags |= RTASN1CORE_F_PRESENT | RTASN1CORE_F_DECODED_CONTENT;
|
---|
330 | pAsn1Core->cb = cb;
|
---|
331 | pAsn1Core->uData.pv = (void *)pCursor->pbCur;
|
---|
332 | return VINF_SUCCESS;
|
---|
333 | }
|
---|
334 |
|
---|
335 | if (pCursor->cbLeft)
|
---|
336 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TOO_LITTLE_DATA_LEFT,
|
---|
337 | "%s: Too little data left to form a valid BER header", pszErrorTag);
|
---|
338 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NO_MORE_DATA,
|
---|
339 | "%s: No more data reading BER header", pszErrorTag);
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | RTDECL(int) RTAsn1CursorMatchTagClassFlagsEx(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
|
---|
344 | bool fString, uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
|
---|
345 | {
|
---|
346 | if (pAsn1Core->uTag == uTag)
|
---|
347 | {
|
---|
348 | if (pAsn1Core->fClass == fClass)
|
---|
349 | return VINF_SUCCESS;
|
---|
350 | if ( fString
|
---|
351 | && pAsn1Core->fClass == (fClass | ASN1_TAGFLAG_CONSTRUCTED))
|
---|
352 | {
|
---|
353 | if (!(pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER)))
|
---|
354 | return VINF_SUCCESS;
|
---|
355 | if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
|
---|
356 | {
|
---|
357 | if (pAsn1Core->cb > 1000)
|
---|
358 | return VINF_SUCCESS;
|
---|
359 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
360 | "%s: Constructed %s only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
|
---|
361 | pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
|
---|
362 | }
|
---|
363 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
364 | "%s: DER encoding does not allow constructed %s (cb=%#x uTag=%#x fClass=%#x)",
|
---|
365 | pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
|
---|
370 | {
|
---|
371 | pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
|
---|
372 | pAsn1Core->uRealTag = uTag;
|
---|
373 | pAsn1Core->fRealClass = fClass;
|
---|
374 | return VINF_SUCCESS;
|
---|
375 | }
|
---|
376 |
|
---|
377 | return RTAsn1CursorSetInfo(pCursor, pAsn1Core->uTag != uTag ? VERR_ASN1_CURSOR_TAG_MISMATCH : VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
|
---|
378 | "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
|
---|
379 | pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
|
---|
380 | }
|
---|
381 |
|
---|
382 |
|
---|
383 |
|
---|
384 | static int rtAsn1CursorGetXxxxCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uTag, uint8_t fClass,
|
---|
385 | PRTASN1CORE pAsn1Core, PRTASN1CURSOR pRetCursor,
|
---|
386 | const char *pszErrorTag, const char *pszWhat)
|
---|
387 | {
|
---|
388 | int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, pszErrorTag);
|
---|
389 | if (RT_SUCCESS(rc))
|
---|
390 | {
|
---|
391 | if ( pAsn1Core->uTag == uTag
|
---|
392 | && pAsn1Core->fClass == fClass)
|
---|
393 | rc = VINF_SUCCESS;
|
---|
394 | else if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
|
---|
395 | {
|
---|
396 | pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
|
---|
397 | pAsn1Core->uRealTag = uTag;
|
---|
398 | pAsn1Core->fRealClass = fClass;
|
---|
399 | rc = VINF_SUCCESS;
|
---|
400 | }
|
---|
401 | else
|
---|
402 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
403 | "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
|
---|
404 | pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
|
---|
405 | rc = RTAsn1CursorInitSub(pCursor, pAsn1Core->cb, pRetCursor, pszErrorTag);
|
---|
406 | if (RT_SUCCESS(rc))
|
---|
407 | {
|
---|
408 | pAsn1Core->fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
|
---|
409 | return VINF_SUCCESS;
|
---|
410 | }
|
---|
411 | }
|
---|
412 | return rc;
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | RTDECL(int) RTAsn1CursorGetSequenceCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
|
---|
417 | PRTASN1SEQUENCECORE pSeqCore, PRTASN1CURSOR pSeqCursor, const char *pszErrorTag)
|
---|
418 | {
|
---|
419 | return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SEQUENCE, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
420 | &pSeqCore->Asn1Core, pSeqCursor, pszErrorTag, "sequence");
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | RTDECL(int) RTAsn1CursorGetSetCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
|
---|
425 | PRTASN1SETCORE pSetCore, PRTASN1CURSOR pSetCursor, const char *pszErrorTag)
|
---|
426 | {
|
---|
427 | return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SET, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
428 | &pSetCore->Asn1Core, pSetCursor, pszErrorTag, "set");
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | RTDECL(int) RTAsn1CursorGetContextTagNCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uExpectedTag,
|
---|
433 | PCRTASN1COREVTABLE pVtable, PRTASN1CONTEXTTAG pCtxTag, PRTASN1CURSOR pCtxTagCursor,
|
---|
434 | const char *pszErrorTag)
|
---|
435 | {
|
---|
436 | int rc = rtAsn1CursorGetXxxxCursor(pCursor, fFlags, uExpectedTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
437 | &pCtxTag->Asn1Core, pCtxTagCursor, pszErrorTag, "ctx tag");
|
---|
438 | pCtxTag->Asn1Core.pOps = pVtable;
|
---|
439 | return rc;
|
---|
440 | }
|
---|
441 |
|
---|
442 |
|
---|
443 | RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
|
---|
444 | {
|
---|
445 | uint32_t cbSavedLeft = pCursor->cbLeft;
|
---|
446 | uint8_t const *pbSavedCur = pCursor->pbCur;
|
---|
447 | PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
|
---|
448 | pCursor->pPrimary->pErrInfo = NULL;
|
---|
449 |
|
---|
450 | int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, "peek");
|
---|
451 |
|
---|
452 | pCursor->pPrimary->pErrInfo = pErrInfo;
|
---|
453 | pCursor->pbCur = pbSavedCur;
|
---|
454 | pCursor->cbLeft = cbSavedLeft;
|
---|
455 | return rc;
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | RTDECL(bool) RTAsn1CursorIsNextEx(PRTASN1CURSOR pCursor, uint32_t uTag, uint8_t fClass)
|
---|
460 | {
|
---|
461 | RTASN1CORE Asn1Core;
|
---|
462 | int rc = RTAsn1CursorPeek(pCursor, &Asn1Core);
|
---|
463 | if (RT_SUCCESS(rc))
|
---|
464 | return uTag == Asn1Core.uTag
|
---|
465 | && fClass == Asn1Core.fClass;
|
---|
466 | return false;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 | /** @name Legacy Interfaces.
|
---|
471 | * @{ */
|
---|
472 | RTDECL(int) RTAsn1CursorGetCore(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
|
---|
473 | {
|
---|
474 | return RTAsn1Core_DecodeAsn1(pCursor, fFlags, pAsn1Core, pszErrorTag);
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | RTDECL(int) RTAsn1CursorGetNull(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1NULL pNull, const char *pszErrorTag)
|
---|
479 | {
|
---|
480 | return RTAsn1Null_DecodeAsn1(pCursor, fFlags, pNull, pszErrorTag);
|
---|
481 | }
|
---|
482 |
|
---|
483 |
|
---|
484 | RTDECL(int) RTAsn1CursorGetInteger(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1INTEGER pInteger, const char *pszErrorTag)
|
---|
485 | {
|
---|
486 | return RTAsn1Integer_DecodeAsn1(pCursor, fFlags, pInteger, pszErrorTag);
|
---|
487 | }
|
---|
488 |
|
---|
489 |
|
---|
490 | RTDECL(int) RTAsn1CursorGetBoolean(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BOOLEAN pBoolean, const char *pszErrorTag)
|
---|
491 | {
|
---|
492 | return RTAsn1Boolean_DecodeAsn1(pCursor, fFlags, pBoolean, pszErrorTag);
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | RTDECL(int) RTAsn1CursorGetObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId, const char *pszErrorTag)
|
---|
497 | {
|
---|
498 | return RTAsn1ObjId_DecodeAsn1(pCursor, fFlags, pObjId, pszErrorTag);
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | RTDECL(int) RTAsn1CursorGetTime(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pTime, const char *pszErrorTag)
|
---|
503 | {
|
---|
504 | return RTAsn1Time_DecodeAsn1(pCursor, fFlags, pTime, pszErrorTag);
|
---|
505 | }
|
---|
506 |
|
---|
507 |
|
---|
508 | RTDECL(int) RTAsn1CursorGetBitStringEx(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pBitString,
|
---|
509 | const char *pszErrorTag)
|
---|
510 | {
|
---|
511 | return RTAsn1BitString_DecodeAsn1Ex(pCursor, fFlags, cMaxBits, pBitString, pszErrorTag);
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | RTDECL(int) RTAsn1CursorGetBitString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BITSTRING pBitString, const char *pszErrorTag)
|
---|
516 | {
|
---|
517 | return RTAsn1BitString_DecodeAsn1(pCursor, fFlags, pBitString, pszErrorTag);
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | RTDECL(int) RTAsn1CursorGetOctetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OCTETSTRING pOctetString,
|
---|
522 | const char *pszErrorTag)
|
---|
523 | {
|
---|
524 | return RTAsn1OctetString_DecodeAsn1(pCursor, fFlags, pOctetString, pszErrorTag);
|
---|
525 | }
|
---|
526 |
|
---|
527 |
|
---|
528 | RTDECL(int) RTAsn1CursorGetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
529 | {
|
---|
530 | return RTAsn1String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | RTDECL(int) RTAsn1CursorGetIa5String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
535 | {
|
---|
536 | return RTAsn1Ia5String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
537 | }
|
---|
538 |
|
---|
539 |
|
---|
540 | RTDECL(int) RTAsn1CursorGetUtf8String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
541 | {
|
---|
542 | return RTAsn1Utf8String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
543 | }
|
---|
544 |
|
---|
545 |
|
---|
546 | RTDECL(int) RTAsn1CursorGetBmpString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
547 | {
|
---|
548 | return RTAsn1BmpString_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | RTDECL(int) RTAsn1CursorGetDynType(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag)
|
---|
553 | {
|
---|
554 | return RTAsn1DynType_DecodeAsn1(pCursor, fFlags, pDynType, pszErrorTag);
|
---|
555 | }
|
---|
556 | /** @} */
|
---|
557 |
|
---|