1 | /* $Id: asn1-cursor.cpp 52600 2014-09-04 22:59:00Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - ASN.1, Basic Operations.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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 | * Header Files *
|
---|
29 | *******************************************************************************/
|
---|
30 | #define RT_STRICT
|
---|
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 | * Global Variables *
|
---|
62 | *******************************************************************************/
|
---|
63 | static char const g_achDigits[11] = "0123456789";
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | RTDECL(PRTASN1CURSOR) RTAsn1CursorInitPrimary(PRTASN1CURSORPRIMARY pPrimaryCursor, void const *pvFirst, uint32_t cb,
|
---|
68 | PRTERRINFO pErrInfo, PCRTASN1ALLOCATORVTABLE pAllocator, uint32_t fFlags,
|
---|
69 | const char *pszErrorTag)
|
---|
70 | {
|
---|
71 | pPrimaryCursor->Cursor.pbCur = (uint8_t const *)pvFirst;
|
---|
72 | pPrimaryCursor->Cursor.cbLeft = cb;
|
---|
73 | pPrimaryCursor->Cursor.fFlags = fFlags;
|
---|
74 | pPrimaryCursor->Cursor.cDepth = 0;
|
---|
75 | pPrimaryCursor->Cursor.abReserved[0] = 0;
|
---|
76 | pPrimaryCursor->Cursor.abReserved[1] = 0;
|
---|
77 | pPrimaryCursor->Cursor.pPrimary = pPrimaryCursor;
|
---|
78 | pPrimaryCursor->Cursor.pUp = NULL;
|
---|
79 | pPrimaryCursor->Cursor.pszErrorTag = pszErrorTag;
|
---|
80 | pPrimaryCursor->pErrInfo = pErrInfo;
|
---|
81 | pPrimaryCursor->pAllocator = pAllocator;
|
---|
82 | return &pPrimaryCursor->Cursor;
|
---|
83 | }
|
---|
84 |
|
---|
85 |
|
---|
86 | RTDECL(int) RTAsn1CursorInitSub(PRTASN1CURSOR pParent, uint32_t cb, PRTASN1CURSOR pChild, const char *pszErrorTag)
|
---|
87 | {
|
---|
88 | AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
89 | AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
|
---|
90 |
|
---|
91 | pChild->pbCur = pParent->pbCur;
|
---|
92 | pChild->cbLeft = cb;
|
---|
93 | pChild->fFlags = pParent->fFlags;
|
---|
94 | pChild->cDepth = pParent->cDepth + 1;
|
---|
95 | AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
|
---|
96 | pChild->abReserved[0] = 0;
|
---|
97 | pChild->abReserved[1] = 0;
|
---|
98 | pChild->pPrimary = pParent->pPrimary;
|
---|
99 | pChild->pUp = pParent;
|
---|
100 | pChild->pszErrorTag = pszErrorTag;
|
---|
101 |
|
---|
102 | AssertReturn(pParent->cbLeft >= cb, VERR_ASN1_INTERNAL_ERROR_3);
|
---|
103 | pParent->pbCur += cb;
|
---|
104 | pParent->cbLeft -= cb;
|
---|
105 |
|
---|
106 | return VINF_SUCCESS;
|
---|
107 | }
|
---|
108 |
|
---|
109 |
|
---|
110 | RTDECL(int) RTAsn1CursorInitSubFromCore(PRTASN1CURSOR pParent, PRTASN1CORE pAsn1Core,
|
---|
111 | PRTASN1CURSOR pChild, const char *pszErrorTag)
|
---|
112 | {
|
---|
113 | AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
|
---|
114 | AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
|
---|
115 |
|
---|
116 | pChild->pbCur = pAsn1Core->uData.pu8;
|
---|
117 | pChild->cbLeft = pAsn1Core->cb;
|
---|
118 | pChild->fFlags = pParent->fFlags;
|
---|
119 | pChild->cDepth = pParent->cDepth + 1;
|
---|
120 | AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
|
---|
121 | pChild->abReserved[0] = 0;
|
---|
122 | pChild->abReserved[1] = 0;
|
---|
123 | pChild->pPrimary = pParent->pPrimary;
|
---|
124 | pChild->pUp = pParent;
|
---|
125 | pChild->pszErrorTag = pszErrorTag;
|
---|
126 |
|
---|
127 | return VINF_SUCCESS;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | RTDECL(int) RTAsn1CursorSetInfoV(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, va_list va)
|
---|
132 | {
|
---|
133 | PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
|
---|
134 | if (pErrInfo)
|
---|
135 | {
|
---|
136 | /* Format the message. */
|
---|
137 | RTErrInfoSetV(pErrInfo, rc, pszMsg, va);
|
---|
138 |
|
---|
139 | /* Add the prefixes. This isn't the fastest way, but it's the one
|
---|
140 | which eats the least stack. */
|
---|
141 | char *pszBuf = pErrInfo->pszMsg;
|
---|
142 | size_t cbBuf = pErrInfo->cbMsg;
|
---|
143 | if (pszBuf && cbBuf > 32)
|
---|
144 | {
|
---|
145 | size_t cbMove = strlen(pszBuf) + 1;
|
---|
146 |
|
---|
147 | /* Make sure there is a ': '. */
|
---|
148 | bool fFirst = false;
|
---|
149 | if (pszMsg[0] != '%' || pszMsg[1] != 's' || pszMsg[2] != ':')
|
---|
150 | {
|
---|
151 | if (cbMove + 2 < cbBuf)
|
---|
152 | {
|
---|
153 | memmove(pszBuf + 2, pszBuf, cbMove);
|
---|
154 | pszBuf[0] = ':';
|
---|
155 | pszBuf[1] = ' ';
|
---|
156 | cbMove += 2;
|
---|
157 | fFirst = true;
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* Add the prefixes from the cursor chain. */
|
---|
162 | while (pCursor)
|
---|
163 | {
|
---|
164 | if (pCursor->pszErrorTag)
|
---|
165 | {
|
---|
166 | size_t cchErrorTag = strlen(pCursor->pszErrorTag);
|
---|
167 | if (cchErrorTag + !fFirst + cbMove > cbBuf)
|
---|
168 | break;
|
---|
169 | memmove(pszBuf + cchErrorTag + !fFirst, pszBuf, cbMove);
|
---|
170 | memcpy(pszBuf, pCursor->pszErrorTag, cchErrorTag);
|
---|
171 | if (!fFirst)
|
---|
172 | pszBuf[cchErrorTag] = '.';
|
---|
173 | cbMove += cchErrorTag + !fFirst;
|
---|
174 | fFirst = false;
|
---|
175 | }
|
---|
176 | pCursor = pCursor->pUp;
|
---|
177 | }
|
---|
178 | }
|
---|
179 | }
|
---|
180 |
|
---|
181 | return rc;
|
---|
182 | }
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(int) RTAsn1CursorSetInfo(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, ...)
|
---|
186 | {
|
---|
187 | va_list va;
|
---|
188 | va_start(va, pszMsg);
|
---|
189 | rc = RTAsn1CursorSetInfoV(pCursor, rc, pszMsg, va);
|
---|
190 | va_end(va);
|
---|
191 | return rc;
|
---|
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(int) RTAsn1CursorReadHdr(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
|
---|
215 | {
|
---|
216 | /*
|
---|
217 | * Initialize the return structure in case of failure.
|
---|
218 | */
|
---|
219 | pAsn1Core->uTag = 0;
|
---|
220 | pAsn1Core->fClass = 0;
|
---|
221 | pAsn1Core->uRealTag = 0;
|
---|
222 | pAsn1Core->fRealClass = 0;
|
---|
223 | pAsn1Core->cbHdr = 0;
|
---|
224 | pAsn1Core->cb = 0;
|
---|
225 | pAsn1Core->fFlags = 0;
|
---|
226 | pAsn1Core->uData.pv = NULL;
|
---|
227 | pAsn1Core->pOps = NULL;
|
---|
228 |
|
---|
229 | /*
|
---|
230 | * The header has at least two bytes: Type & length.
|
---|
231 | */
|
---|
232 | if (pCursor->cbLeft >= 2)
|
---|
233 | {
|
---|
234 | uint32_t uTag = pCursor->pbCur[0];
|
---|
235 | uint32_t cb = pCursor->pbCur[1];
|
---|
236 | pCursor->cbLeft -= 2;
|
---|
237 | pCursor->pbCur += 2;
|
---|
238 |
|
---|
239 | pAsn1Core->uRealTag = pAsn1Core->uTag = uTag & ASN1_TAG_MASK;
|
---|
240 | pAsn1Core->fRealClass = pAsn1Core->fClass = uTag & ~ASN1_TAG_MASK;
|
---|
241 | pAsn1Core->cbHdr = 2;
|
---|
242 | if ((uTag & ASN1_TAG_MASK) == ASN1_TAG_USE_LONG_FORM)
|
---|
243 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_LONG_TAG,
|
---|
244 | "%s: Implement parsing of tags > 30: %#x (length=%#x)", pszErrorTag, uTag, cb);
|
---|
245 |
|
---|
246 | /* Extended length field? */
|
---|
247 | if (cb & RT_BIT(7))
|
---|
248 | {
|
---|
249 | if (cb != RT_BIT(7))
|
---|
250 | {
|
---|
251 | /* Definite form. */
|
---|
252 | uint8_t cbEnc = cb & 0x7f;
|
---|
253 | if (cbEnc > pCursor->cbLeft)
|
---|
254 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
255 | "%s: Extended BER length field longer than available data: %#x vs %#x (uTag=%#x)",
|
---|
256 | pszErrorTag, cbEnc, pCursor->cbLeft, uTag);
|
---|
257 | switch (cbEnc)
|
---|
258 | {
|
---|
259 | case 1:
|
---|
260 | cb = pCursor->pbCur[0];
|
---|
261 | break;
|
---|
262 | case 2:
|
---|
263 | cb = RT_MAKE_U16(pCursor->pbCur[1], pCursor->pbCur[0]);
|
---|
264 | break;
|
---|
265 | case 3:
|
---|
266 | cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0], 0);
|
---|
267 | break;
|
---|
268 | case 4:
|
---|
269 | cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[3], pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0]);
|
---|
270 | break;
|
---|
271 | default:
|
---|
272 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
273 | "%s: Too long/short extended BER length field: %#x (uTag=%#x)",
|
---|
274 | pszErrorTag, cbEnc, uTag);
|
---|
275 | }
|
---|
276 | pCursor->cbLeft -= cbEnc;
|
---|
277 | pCursor->pbCur += cbEnc;
|
---|
278 | pAsn1Core->cbHdr += cbEnc;
|
---|
279 |
|
---|
280 | /* Check the length encoding efficiency (T-REC-X.690-200811 10.1, 9.1). */
|
---|
281 | if (pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER))
|
---|
282 | {
|
---|
283 | if (cb <= 0x7f)
|
---|
284 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
285 | "%s: Invalid DER/CER length encoding: cbEnc=%u cb=%#x uTag=%#x",
|
---|
286 | pszErrorTag, cbEnc, cb, uTag);
|
---|
287 | uint8_t cbNeeded;
|
---|
288 | if (cb <= 0x000000ff) cbNeeded = 1;
|
---|
289 | else if (cb <= 0x0000ffff) cbNeeded = 2;
|
---|
290 | else if (cb <= 0x00ffffff) cbNeeded = 3;
|
---|
291 | else cbNeeded = 4;
|
---|
292 | if (cbNeeded != cbEnc)
|
---|
293 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
|
---|
294 | "%s: Invalid DER/CER length encoding: cb=%#x uTag=%#x cbEnc=%u cbNeeded=%u",
|
---|
295 | pszErrorTag, cb, uTag, cbEnc, cbNeeded);
|
---|
296 | }
|
---|
297 | }
|
---|
298 | /* Indefinite form. */
|
---|
299 | else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
|
---|
300 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_IDEFINITE_LENGTH,
|
---|
301 | "%s: Indefinite length form not allowed in DER mode (uTag=%#x).", pszErrorTag, uTag);
|
---|
302 | else
|
---|
303 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_IDEFINITE_LENGTH_NOT_SUP,
|
---|
304 | "%s: Indefinite BER/CER length not supported (uTag=%#x)", pszErrorTag, uTag);
|
---|
305 | }
|
---|
306 |
|
---|
307 | /* Check if the length makes sense. */
|
---|
308 | if (cb > pCursor->cbLeft)
|
---|
309 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH,
|
---|
310 | "%s: BER value length out of bounds: %#x (max=%#x uTag=%#x)",
|
---|
311 | pszErrorTag, cb, pCursor->cbLeft, uTag);
|
---|
312 |
|
---|
313 | pAsn1Core->fFlags |= RTASN1CORE_F_PRESENT | RTASN1CORE_F_DECODED_CONTENT;
|
---|
314 | pAsn1Core->cb = cb;
|
---|
315 | pAsn1Core->uData.pv = (void *)pCursor->pbCur;
|
---|
316 | return VINF_SUCCESS;
|
---|
317 | }
|
---|
318 |
|
---|
319 | if (pCursor->cbLeft)
|
---|
320 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TOO_LITTLE_DATA_LEFT,
|
---|
321 | "%s: Too little data left to form a valid BER header", pszErrorTag);
|
---|
322 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NO_MORE_DATA,
|
---|
323 | "%s: No more data reading BER header", pszErrorTag);
|
---|
324 | }
|
---|
325 |
|
---|
326 |
|
---|
327 | RTDECL(int) RTAsn1CursorMatchTagClassFlagsEx(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
|
---|
328 | bool fString, uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
|
---|
329 | {
|
---|
330 | if (pAsn1Core->uTag == uTag)
|
---|
331 | {
|
---|
332 | if (pAsn1Core->fClass == fClass)
|
---|
333 | return VINF_SUCCESS;
|
---|
334 | if ( fString
|
---|
335 | && pAsn1Core->fClass == (fClass | ASN1_TAGFLAG_CONSTRUCTED))
|
---|
336 | {
|
---|
337 | if (!(pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER)))
|
---|
338 | return VINF_SUCCESS;
|
---|
339 | if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
|
---|
340 | {
|
---|
341 | if (pAsn1Core->cb > 1000)
|
---|
342 | return VINF_SUCCESS;
|
---|
343 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
344 | "%s: Constructed %s only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
|
---|
345 | pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
|
---|
346 | }
|
---|
347 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
348 | "%s: DER encoding does not allow constructed %s (cb=%#x uTag=%#x fClass=%#x)",
|
---|
349 | pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
|
---|
354 | {
|
---|
355 | pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
|
---|
356 | pAsn1Core->uRealTag = uTag;
|
---|
357 | pAsn1Core->fRealClass = fClass;
|
---|
358 | return VINF_SUCCESS;
|
---|
359 | }
|
---|
360 |
|
---|
361 | return RTAsn1CursorSetInfo(pCursor, pAsn1Core->uTag != uTag ? VERR_ASN1_CURSOR_TAG_MISMATCH : VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
|
---|
362 | "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
|
---|
363 | pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 |
|
---|
368 | static int rtAsn1CursorGetXxxxCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uTag, uint8_t fClass,
|
---|
369 | PRTASN1CORE pAsn1Core, PRTASN1CURSOR pRetCursor,
|
---|
370 | const char *pszErrorTag, const char *pszWhat)
|
---|
371 | {
|
---|
372 | int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, pszErrorTag);
|
---|
373 | if (RT_SUCCESS(rc))
|
---|
374 | {
|
---|
375 | if ( pAsn1Core->uTag == uTag
|
---|
376 | && pAsn1Core->fClass == fClass)
|
---|
377 | rc = VINF_SUCCESS;
|
---|
378 | else if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
|
---|
379 | {
|
---|
380 | pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
|
---|
381 | pAsn1Core->uRealTag = uTag;
|
---|
382 | pAsn1Core->fRealClass = fClass;
|
---|
383 | rc = VINF_SUCCESS;
|
---|
384 | }
|
---|
385 | else
|
---|
386 | return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
|
---|
387 | "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
|
---|
388 | pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
|
---|
389 | rc = RTAsn1CursorInitSub(pCursor, pAsn1Core->cb, pRetCursor, pszErrorTag);
|
---|
390 | if (RT_SUCCESS(rc))
|
---|
391 | {
|
---|
392 | pAsn1Core->fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
|
---|
393 | return VINF_SUCCESS;
|
---|
394 | }
|
---|
395 | }
|
---|
396 | return rc;
|
---|
397 | }
|
---|
398 |
|
---|
399 |
|
---|
400 | RTDECL(int) RTAsn1CursorGetSequenceCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
|
---|
401 | PRTASN1SEQUENCECORE pSeqCore, PRTASN1CURSOR pSeqCursor, const char *pszErrorTag)
|
---|
402 | {
|
---|
403 | return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SEQUENCE, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
404 | &pSeqCore->Asn1Core, pSeqCursor, pszErrorTag, "sequence");
|
---|
405 | }
|
---|
406 |
|
---|
407 |
|
---|
408 | RTDECL(int) RTAsn1CursorGetSetCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
|
---|
409 | PRTASN1SETCORE pSetCore, PRTASN1CURSOR pSetCursor, const char *pszErrorTag)
|
---|
410 | {
|
---|
411 | return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SET, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
412 | &pSetCore->Asn1Core, pSetCursor, pszErrorTag, "set");
|
---|
413 | }
|
---|
414 |
|
---|
415 |
|
---|
416 | RTDECL(int) RTAsn1CursorGetContextTagNCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uExpectedTag,
|
---|
417 | PRTASN1CONTEXTTAG pCtxTag, PRTASN1CURSOR pCtxTagCursor, const char *pszErrorTag)
|
---|
418 | {
|
---|
419 | return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, uExpectedTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED,
|
---|
420 | &pCtxTag->Asn1Core, pCtxTagCursor, pszErrorTag, "ctx tag");
|
---|
421 | }
|
---|
422 |
|
---|
423 |
|
---|
424 | RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
|
---|
425 | {
|
---|
426 | uint32_t cbSavedLeft = pCursor->cbLeft;
|
---|
427 | uint8_t const *pbSavedCur = pCursor->pbCur;
|
---|
428 | PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
|
---|
429 | pCursor->pPrimary->pErrInfo = NULL;
|
---|
430 |
|
---|
431 | int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, "peek");
|
---|
432 |
|
---|
433 | pCursor->pPrimary->pErrInfo = pErrInfo;
|
---|
434 | pCursor->pbCur = pbSavedCur;
|
---|
435 | pCursor->cbLeft = cbSavedLeft;
|
---|
436 | return rc;
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | RTDECL(bool) RTAsn1CursorIsNextEx(PRTASN1CURSOR pCursor, uint32_t uTag, uint8_t fClass)
|
---|
441 | {
|
---|
442 | RTASN1CORE Asn1Core;
|
---|
443 | int rc = RTAsn1CursorPeek(pCursor, &Asn1Core);
|
---|
444 | if (RT_SUCCESS(rc))
|
---|
445 | return uTag == Asn1Core.uTag
|
---|
446 | && fClass == Asn1Core.fClass;
|
---|
447 | return false;
|
---|
448 | }
|
---|
449 |
|
---|
450 |
|
---|
451 | /** @name Legacy Interfaces.
|
---|
452 | * @{ */
|
---|
453 | RTDECL(int) RTAsn1CursorGetCore(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
|
---|
454 | {
|
---|
455 | return RTAsn1Core_DecodeAsn1(pCursor, fFlags, pAsn1Core, pszErrorTag);
|
---|
456 | }
|
---|
457 |
|
---|
458 |
|
---|
459 | RTDECL(int) RTAsn1CursorGetNull(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1NULL pNull, const char *pszErrorTag)
|
---|
460 | {
|
---|
461 | return RTAsn1Null_DecodeAsn1(pCursor, fFlags, pNull, pszErrorTag);
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | RTDECL(int) RTAsn1CursorGetInteger(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1INTEGER pInteger, const char *pszErrorTag)
|
---|
466 | {
|
---|
467 | return RTAsn1Integer_DecodeAsn1(pCursor, fFlags, pInteger, pszErrorTag);
|
---|
468 | }
|
---|
469 |
|
---|
470 |
|
---|
471 | RTDECL(int) RTAsn1CursorGetBoolean(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BOOLEAN pBoolean, const char *pszErrorTag)
|
---|
472 | {
|
---|
473 | return RTAsn1Boolean_DecodeAsn1(pCursor, fFlags, pBoolean, pszErrorTag);
|
---|
474 | }
|
---|
475 |
|
---|
476 |
|
---|
477 | RTDECL(int) RTAsn1CursorGetObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId, const char *pszErrorTag)
|
---|
478 | {
|
---|
479 | return RTAsn1ObjId_DecodeAsn1(pCursor, fFlags, pObjId, pszErrorTag);
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | RTDECL(int) RTAsn1CursorGetTime(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pTime, const char *pszErrorTag)
|
---|
484 | {
|
---|
485 | return RTAsn1Time_DecodeAsn1(pCursor, fFlags, pTime, pszErrorTag);
|
---|
486 | }
|
---|
487 |
|
---|
488 |
|
---|
489 | RTDECL(int) RTAsn1CursorGetBitStringEx(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pBitString,
|
---|
490 | const char *pszErrorTag)
|
---|
491 | {
|
---|
492 | return RTAsn1BitString_DecodeAsn1Ex(pCursor, fFlags, cMaxBits, pBitString, pszErrorTag);
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | RTDECL(int) RTAsn1CursorGetBitString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BITSTRING pBitString, const char *pszErrorTag)
|
---|
497 | {
|
---|
498 | return RTAsn1BitString_DecodeAsn1(pCursor, fFlags, pBitString, pszErrorTag);
|
---|
499 | }
|
---|
500 |
|
---|
501 |
|
---|
502 | RTDECL(int) RTAsn1CursorGetOctetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OCTETSTRING pOctetString,
|
---|
503 | const char *pszErrorTag)
|
---|
504 | {
|
---|
505 | return RTAsn1OctetString_DecodeAsn1(pCursor, fFlags, pOctetString, pszErrorTag);
|
---|
506 | }
|
---|
507 |
|
---|
508 |
|
---|
509 | RTDECL(int) RTAsn1CursorGetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
510 | {
|
---|
511 | return RTAsn1String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | RTDECL(int) RTAsn1CursorGetIa5String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
516 | {
|
---|
517 | return RTAsn1Ia5String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
518 | }
|
---|
519 |
|
---|
520 |
|
---|
521 | RTDECL(int) RTAsn1CursorGetUtf8String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
522 | {
|
---|
523 | return RTAsn1Utf8String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | RTDECL(int) RTAsn1CursorGetBmpString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
|
---|
528 | {
|
---|
529 | return RTAsn1BmpString_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | RTDECL(int) RTAsn1CursorGetDynType(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag)
|
---|
534 | {
|
---|
535 | return RTAsn1DynType_DecodeAsn1(pCursor, fFlags, pDynType, pszErrorTag);
|
---|
536 | }
|
---|
537 | /** @} */
|
---|
538 |
|
---|