VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/asn1/asn1-cursor.cpp@ 74672

Last change on this file since 74672 was 74672, checked in by vboxsync, 7 years ago

IPRT/asn1: Hacked code into handling the necessary indefinite length stuff from apple. bugref:9232

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/VBox-3.0/src/VBox/Runtime/common/asn1/asn1-basics.cpp58652,​70973
    /branches/VBox-3.2/src/VBox/Runtime/common/asn1/asn1-basics.cpp66309,​66318
    /branches/VBox-4.0/src/VBox/Runtime/common/asn1/asn1-basics.cpp70873
    /branches/VBox-4.1/src/VBox/Runtime/common/asn1/asn1-basics.cpp74233,​78414,​78691,​81841,​82127,​85941,​85944-85947,​85949-85950,​85953,​86701,​86728,​87009
    /branches/VBox-4.2/src/VBox/Runtime/common/asn1/asn1-basics.cpp86229-86230,​86234,​86529,​91503-91504,​91506-91508,​91510,​91514-91515,​91521
    /branches/VBox-4.3/src/VBox/Runtime/common/asn1/asn1-basics.cpp91223
    /branches/VBox-4.3/trunk/src/VBox/Runtime/common/asn1/asn1-basics.cpp91223
    /branches/andy/draganddrop/src/VBox/Runtime/common/asn1/asn1-basics.cpp90781-91268
    /branches/andy/guestctrl20/src/VBox/Runtime/common/asn1/asn1-basics.cpp78916,​78930
    /branches/dsen/gui/src/VBox/Runtime/common/asn1/asn1-basics.cpp79076-79078,​79089,​79109-79110,​79112-79113,​79127-79130,​79134,​79141,​79151,​79155,​79157-79159,​79193,​79197
    /branches/dsen/gui2/src/VBox/Runtime/common/asn1/asn1-basics.cpp79224,​79228,​79233,​79235,​79258,​79262-79263,​79273,​79341,​79345,​79354,​79357,​79387-79388,​79559-79569,​79572-79573,​79578,​79581-79582,​79590-79591,​79598-79599,​79602-79603,​79605-79606,​79632,​79635,​79637,​79644
    /branches/dsen/gui3/src/VBox/Runtime/common/asn1/asn1-basics.cpp79645-79692
File size: 27.6 KB
Line 
1/* $Id: asn1-cursor.cpp 74672 2018-10-08 12:08:51Z 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.215389.xyz. 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/asm.h>
35#include <iprt/alloca.h>
36#include <iprt/err.h>
37#include <iprt/string.h>
38#include <iprt/ctype.h>
39
40#include <iprt/formats/asn1.h>
41
42
43/*********************************************************************************************************************************
44* Defined Constants And Macros *
45*********************************************************************************************************************************/
46/** @def RTASN1_MAX_NESTING
47 * The maximum nesting depth we allow. This limit is enforced to avoid running
48 * out of stack due to malformed ASN.1 input.
49 *
50 * For reference, 'RTSignTool verify-exe RTSignTool.exe', requires a value of 15
51 * to work without hitting the limit for signatures with simple timestamps, and
52 * 23 (amd64/rel = ~3KB) for the new microsoft timestamp counter signatures.
53 */
54#ifdef IN_RING3
55# define RTASN1_MAX_NESTING 64
56#else
57# define RTASN1_MAX_NESTING 32
58#endif
59
60
61
62RTDECL(PRTASN1CURSOR) RTAsn1CursorInitPrimary(PRTASN1CURSORPRIMARY pPrimaryCursor, void const *pvFirst, uint32_t cb,
63 PRTERRINFO pErrInfo, PCRTASN1ALLOCATORVTABLE pAllocator, uint32_t fFlags,
64 const char *pszErrorTag)
65{
66 pPrimaryCursor->Cursor.pbCur = (uint8_t const *)pvFirst;
67 pPrimaryCursor->Cursor.cbLeft = cb;
68 pPrimaryCursor->Cursor.fFlags = (uint8_t)fFlags; Assert(fFlags <= UINT8_MAX);
69 pPrimaryCursor->Cursor.cDepth = 0;
70 pPrimaryCursor->Cursor.abReserved[0] = 0;
71 pPrimaryCursor->Cursor.abReserved[1] = 0;
72 pPrimaryCursor->Cursor.pPrimary = pPrimaryCursor;
73 pPrimaryCursor->Cursor.pUp = NULL;
74 pPrimaryCursor->Cursor.pszErrorTag = pszErrorTag;
75 pPrimaryCursor->pErrInfo = pErrInfo;
76 pPrimaryCursor->pAllocator = pAllocator;
77 return &pPrimaryCursor->Cursor;
78}
79
80
81RTDECL(int) RTAsn1CursorInitSub(PRTASN1CURSOR pParent, uint32_t cb, PRTASN1CURSOR pChild, const char *pszErrorTag)
82{
83 AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
84 AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
85
86 pChild->pbCur = pParent->pbCur;
87 pChild->cbLeft = cb;
88 pChild->fFlags = pParent->fFlags & ~RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
89 pChild->cDepth = pParent->cDepth + 1;
90 AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
91 pChild->abReserved[0] = 0;
92 pChild->abReserved[1] = 0;
93 pChild->pPrimary = pParent->pPrimary;
94 pChild->pUp = pParent;
95 pChild->pszErrorTag = pszErrorTag;
96
97 AssertReturn(pParent->cbLeft >= cb, VERR_ASN1_INTERNAL_ERROR_3);
98 pParent->pbCur += cb;
99 pParent->cbLeft -= cb;
100
101 return VINF_SUCCESS;
102}
103
104
105RTDECL(int) RTAsn1CursorInitSubFromCore(PRTASN1CURSOR pParent, PRTASN1CORE pAsn1Core,
106 PRTASN1CURSOR pChild, const char *pszErrorTag)
107{
108 AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
109 AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
110
111 pChild->pbCur = pAsn1Core->uData.pu8;
112 pChild->cbLeft = pAsn1Core->cb;
113 pChild->fFlags = pParent->fFlags & ~RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
114 pChild->cDepth = pParent->cDepth + 1;
115 AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
116 pChild->abReserved[0] = 0;
117 pChild->abReserved[1] = 0;
118 pChild->pPrimary = pParent->pPrimary;
119 pChild->pUp = pParent;
120 pChild->pszErrorTag = pszErrorTag;
121
122 return VINF_SUCCESS;
123}
124
125
126RTDECL(int) RTAsn1CursorSetInfoV(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, va_list va)
127{
128 PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
129 if (pErrInfo)
130 {
131 /* Format the message. */
132 RTErrInfoSetV(pErrInfo, rc, pszMsg, va);
133
134 /* Add the prefixes. This isn't the fastest way, but it's the one
135 which eats the least stack. */
136 char *pszBuf = pErrInfo->pszMsg;
137 size_t cbBuf = pErrInfo->cbMsg;
138 if (pszBuf && cbBuf > 32)
139 {
140 size_t cbMove = strlen(pszBuf) + 1;
141
142 /* Make sure there is a ': '. */
143 bool fFirst = false;
144 if (pszMsg[0] != '%' || pszMsg[1] != 's' || pszMsg[2] != ':')
145 {
146 if (cbMove + 2 < cbBuf)
147 {
148 memmove(pszBuf + 2, pszBuf, cbMove);
149 pszBuf[0] = ':';
150 pszBuf[1] = ' ';
151 cbMove += 2;
152 fFirst = true;
153 }
154 }
155
156 /* Add the prefixes from the cursor chain. */
157 while (pCursor)
158 {
159 if (pCursor->pszErrorTag)
160 {
161 size_t cchErrorTag = strlen(pCursor->pszErrorTag);
162 if (cchErrorTag + !fFirst + cbMove > cbBuf)
163 break;
164 memmove(pszBuf + cchErrorTag + !fFirst, pszBuf, cbMove);
165 memcpy(pszBuf, pCursor->pszErrorTag, cchErrorTag);
166 if (!fFirst)
167 pszBuf[cchErrorTag] = '.';
168 cbMove += cchErrorTag + !fFirst;
169 fFirst = false;
170 }
171 pCursor = pCursor->pUp;
172 }
173 }
174 }
175
176 return rc;
177}
178
179
180RTDECL(int) RTAsn1CursorSetInfo(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, ...)
181{
182 va_list va;
183 va_start(va, pszMsg);
184 rc = RTAsn1CursorSetInfoV(pCursor, rc, pszMsg, va);
185 va_end(va);
186 return rc;
187}
188
189
190RTDECL(bool) RTAsn1CursorIsEnd(PRTASN1CURSOR pCursor)
191{
192 if (pCursor->cbLeft == 0)
193 return true;
194 if (!(pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
195 return false;
196 return pCursor->cbLeft >= 2
197 && pCursor->pbCur[0] == 0
198 && pCursor->pbCur[1] == 0;
199}
200
201
202RTDECL(int) RTAsn1CursorCheckEnd(PRTASN1CURSOR pCursor)
203{
204 if (pCursor->cbLeft == 0)
205 return VINF_SUCCESS;
206
207 if (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
208 {
209 /*
210 * If we've got two zeros here we're good. This helps us handle apple code
211 * signatures, where most of the big structures are of indefinite length.
212 * The problem here is when rtCrPkcs7ContentInfo_DecodeExtra works the
213 * octet string, it appears as if there extra padding at the end.
214 *
215 * It is of course possible that ASN.1 assumes we will parse the content of
216 * that octet string as if it were an ASN.1 substructure, looking for the
217 * end-of-content sequence and propage that up. However, this works for now.
218 */
219 if (pCursor->cbLeft >= 2)
220 {
221 if ( pCursor->pbCur[0] == 0
222 && pCursor->pbCur[1] == 0)
223 return VINF_SUCCESS;
224 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
225 "%u (%#x) bytes left over [indef: %.*Rhxs]",
226 pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
227 }
228 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
229 "%u (%#x) bytes left over [indef len]", pCursor->cbLeft, pCursor->cbLeft);
230 }
231 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
232 "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
233}
234
235
236/**
237 * Worker for RTAsn1CursorCheckSeqEnd and RTAsn1CursorCheckSetEnd.
238 */
239static int rtAsn1CursorCheckSeqOrSetEnd(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
240{
241 if (pCursor->cbLeft == 0)
242 return VINF_SUCCESS;
243
244 if (pAsn1Core->fFlags & RTASN1CORE_F_INDEFINITE_LENGTH)
245 {
246 if (pCursor->cbLeft >= 2)
247 {
248 if ( pCursor->pbCur[0] == 0
249 && pCursor->pbCur[1] == 0)
250 {
251 pAsn1Core->cb = (uint32_t)(pCursor->pbCur - pAsn1Core->uData.pu8);
252 pCursor->cbLeft -= 2;
253 pCursor->pbCur += 2;
254
255 PRTASN1CURSOR pParentCursor = pCursor->pUp;
256 if ( pParentCursor
257 && (pParentCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
258 {
259 pParentCursor->pbCur -= pCursor->cbLeft;
260 pParentCursor->cbLeft += pCursor->cbLeft;
261 return VINF_SUCCESS;
262 }
263
264 if (pCursor->cbLeft == 0)
265 return VINF_SUCCESS;
266
267 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
268 "%u (%#x) bytes left over (parent not indefinite length)", pCursor->cbLeft, pCursor->cbLeft);
269 }
270 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END, "%u (%#x) bytes left over [indef: %.*Rhxs]",
271 pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
272 }
273 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
274 "1 byte left over, expected two for indefinite length end-of-content sequence");
275 }
276
277 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
278 "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
279
280}
281
282
283RTDECL(int) RTAsn1CursorCheckSeqEnd(PRTASN1CURSOR pCursor, PRTASN1SEQUENCECORE pSeqCore)
284{
285 return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSeqCore->Asn1Core);
286}
287
288
289RTDECL(int) RTAsn1CursorCheckSetEnd(PRTASN1CURSOR pCursor, PRTASN1SETCORE pSetCore)
290{
291 return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSetCore->Asn1Core);
292}
293
294
295RTDECL(PRTASN1ALLOCATION) RTAsn1CursorInitAllocation(PRTASN1CURSOR pCursor, PRTASN1ALLOCATION pAllocation)
296{
297 pAllocation->cbAllocated = 0;
298 pAllocation->cReallocs = 0;
299 pAllocation->uReserved0 = 0;
300 pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
301 return pAllocation;
302}
303
304
305RTDECL(PRTASN1ARRAYALLOCATION) RTAsn1CursorInitArrayAllocation(PRTASN1CURSOR pCursor, PRTASN1ARRAYALLOCATION pAllocation,
306 size_t cbEntry)
307{
308 Assert(cbEntry >= sizeof(RTASN1CORE));
309 Assert(cbEntry < _1M);
310 Assert(RT_ALIGN_Z(cbEntry, sizeof(void *)) == cbEntry);
311 pAllocation->cbEntry = (uint32_t)cbEntry;
312 pAllocation->cPointersAllocated = 0;
313 pAllocation->cEntriesAllocated = 0;
314 pAllocation->cResizeCalls = 0;
315 pAllocation->uReserved0 = 0;
316 pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
317 return pAllocation;
318}
319
320
321RTDECL(int) RTAsn1CursorReadHdr(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
322{
323 /*
324 * Initialize the return structure in case of failure.
325 */
326 pAsn1Core->uTag = 0;
327 pAsn1Core->fClass = 0;
328 pAsn1Core->uRealTag = 0;
329 pAsn1Core->fRealClass = 0;
330 pAsn1Core->cbHdr = 0;
331 pAsn1Core->cb = 0;
332 pAsn1Core->fFlags = 0;
333 pAsn1Core->uData.pv = NULL;
334 pAsn1Core->pOps = NULL;
335
336 /*
337 * The header has at least two bytes: Type & length.
338 */
339 if (pCursor->cbLeft >= 2)
340 {
341 uint32_t uTag = pCursor->pbCur[0];
342 uint32_t cb = pCursor->pbCur[1];
343 pCursor->cbLeft -= 2;
344 pCursor->pbCur += 2;
345
346 pAsn1Core->uRealTag = pAsn1Core->uTag = uTag & ASN1_TAG_MASK;
347 pAsn1Core->fRealClass = pAsn1Core->fClass = uTag & ~ASN1_TAG_MASK;
348 pAsn1Core->cbHdr = 2;
349 if ((uTag & ASN1_TAG_MASK) == ASN1_TAG_USE_LONG_FORM)
350 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_LONG_TAG,
351 "%s: Implement parsing of tags > 30: %#x (length=%#x)", pszErrorTag, uTag, cb);
352
353 /* Extended length field? */
354 if (cb & RT_BIT(7))
355 {
356 if (cb != RT_BIT(7))
357 {
358 /* Definite form. */
359 uint8_t cbEnc = cb & 0x7f;
360 if (cbEnc > pCursor->cbLeft)
361 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
362 "%s: Extended BER length field longer than available data: %#x vs %#x (uTag=%#x)",
363 pszErrorTag, cbEnc, pCursor->cbLeft, uTag);
364 switch (cbEnc)
365 {
366 case 1:
367 cb = pCursor->pbCur[0];
368 break;
369 case 2:
370 cb = RT_MAKE_U16(pCursor->pbCur[1], pCursor->pbCur[0]);
371 break;
372 case 3:
373 cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0], 0);
374 break;
375 case 4:
376 cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[3], pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0]);
377 break;
378 default:
379 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
380 "%s: Too long/short extended BER length field: %#x (uTag=%#x)",
381 pszErrorTag, cbEnc, uTag);
382 }
383 pCursor->cbLeft -= cbEnc;
384 pCursor->pbCur += cbEnc;
385 pAsn1Core->cbHdr += cbEnc;
386
387 /* Check the length encoding efficiency (T-REC-X.690-200811 10.1, 9.1). */
388 if (pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER))
389 {
390 if (cb <= 0x7f)
391 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
392 "%s: Invalid DER/CER length encoding: cbEnc=%u cb=%#x uTag=%#x",
393 pszErrorTag, cbEnc, cb, uTag);
394 uint8_t cbNeeded;
395 if (cb <= 0x000000ff) cbNeeded = 1;
396 else if (cb <= 0x0000ffff) cbNeeded = 2;
397 else if (cb <= 0x00ffffff) cbNeeded = 3;
398 else cbNeeded = 4;
399 if (cbNeeded != cbEnc)
400 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
401 "%s: Invalid DER/CER length encoding: cb=%#x uTag=%#x cbEnc=%u cbNeeded=%u",
402 pszErrorTag, cb, uTag, cbEnc, cbNeeded);
403 }
404 }
405 /* Indefinite form. */
406 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
407 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_INDEFINITE_LENGTH,
408 "%s: Indefinite length form not allowed in DER mode (uTag=%#x).", pszErrorTag, uTag);
409 else if (!(uTag & ASN1_TAGFLAG_CONSTRUCTED))
410 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
411 "%s: Indefinite BER/CER encoding is for non-constructed tag (uTag=%#x)", pszErrorTag, uTag);
412 else if ( uTag != (ASN1_TAG_SEQUENCE | ASN1_TAGFLAG_CONSTRUCTED)
413 && uTag != (ASN1_TAG_SET | ASN1_TAGFLAG_CONSTRUCTED)
414 && (uTag & (ASN1_TAGFLAG_CONSTRUCTED | ASN1_TAGCLASS_CONTEXT))
415 != (ASN1_TAGFLAG_CONSTRUCTED | ASN1_TAGCLASS_CONTEXT) )
416 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
417 "%s: Indefinite BER/CER encoding not supported for this tag (uTag=%#x)", pszErrorTag, uTag);
418 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
419 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
420 "%s: Nested indefinite BER/CER encoding. (uTag=%#x)", pszErrorTag, uTag);
421 else if (pCursor->cbLeft < 2)
422 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
423 "%s: Too little data left for indefinite BER/CER encoding (uTag=%#x)", pszErrorTag, uTag);
424 else
425 {
426 pCursor->fFlags |= RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
427 pAsn1Core->fFlags |= RTASN1CORE_F_INDEFINITE_LENGTH;
428 cb = pCursor->cbLeft - 2; /* tentatively for sequences and sets, definite for others */
429 }
430 }
431 /* else if (cb == 0 && uTag == 0) { end of content } - callers handle this */
432
433 /* Check if the length makes sense. */
434 if (cb > pCursor->cbLeft)
435 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH,
436 "%s: BER value length out of bounds: %#x (max=%#x uTag=%#x)",
437 pszErrorTag, cb, pCursor->cbLeft, uTag);
438
439 pAsn1Core->fFlags |= RTASN1CORE_F_PRESENT | RTASN1CORE_F_DECODED_CONTENT;
440 pAsn1Core->cb = cb;
441 pAsn1Core->uData.pv = (void *)pCursor->pbCur;
442 return VINF_SUCCESS;
443 }
444
445 if (pCursor->cbLeft)
446 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TOO_LITTLE_DATA_LEFT,
447 "%s: Too little data left to form a valid BER header", pszErrorTag);
448 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NO_MORE_DATA,
449 "%s: No more data reading BER header", pszErrorTag);
450}
451
452
453RTDECL(int) RTAsn1CursorMatchTagClassFlagsEx(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
454 bool fString, uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
455{
456 if (pAsn1Core->uTag == uTag)
457 {
458 if (pAsn1Core->fClass == fClass)
459 return VINF_SUCCESS;
460 if ( fString
461 && pAsn1Core->fClass == (fClass | ASN1_TAGFLAG_CONSTRUCTED))
462 {
463 if (!(pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER)))
464 return VINF_SUCCESS;
465 if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
466 {
467 if (pAsn1Core->cb > 1000)
468 return VINF_SUCCESS;
469 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
470 "%s: Constructed %s only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
471 pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
472 }
473 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
474 "%s: DER encoding does not allow constructed %s (cb=%#x uTag=%#x fClass=%#x)",
475 pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
476 }
477 }
478
479 if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
480 {
481 pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
482 pAsn1Core->uRealTag = uTag;
483 pAsn1Core->fRealClass = fClass;
484 return VINF_SUCCESS;
485 }
486
487 return RTAsn1CursorSetInfo(pCursor, pAsn1Core->uTag != uTag ? VERR_ASN1_CURSOR_TAG_MISMATCH : VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
488 "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
489 pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
490}
491
492
493
494static int rtAsn1CursorGetXxxxCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uTag, uint8_t fClass,
495 PRTASN1CORE pAsn1Core, PRTASN1CURSOR pRetCursor,
496 const char *pszErrorTag, const char *pszWhat)
497{
498 int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, pszErrorTag);
499 if (RT_SUCCESS(rc))
500 {
501 if ( pAsn1Core->uTag == uTag
502 && pAsn1Core->fClass == fClass)
503 rc = VINF_SUCCESS;
504 else if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
505 {
506 pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
507 pAsn1Core->uRealTag = uTag;
508 pAsn1Core->fRealClass = fClass;
509 rc = VINF_SUCCESS;
510 }
511 else
512 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
513 "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
514 pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
515 rc = RTAsn1CursorInitSub(pCursor, pAsn1Core->cb, pRetCursor, pszErrorTag);
516 if (RT_SUCCESS(rc))
517 {
518 pAsn1Core->fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
519 return VINF_SUCCESS;
520 }
521 }
522 return rc;
523}
524
525
526RTDECL(int) RTAsn1CursorGetSequenceCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
527 PRTASN1SEQUENCECORE pSeqCore, PRTASN1CURSOR pSeqCursor, const char *pszErrorTag)
528{
529 return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SEQUENCE, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
530 &pSeqCore->Asn1Core, pSeqCursor, pszErrorTag, "sequence");
531}
532
533
534RTDECL(int) RTAsn1CursorGetSetCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
535 PRTASN1SETCORE pSetCore, PRTASN1CURSOR pSetCursor, const char *pszErrorTag)
536{
537 return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SET, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
538 &pSetCore->Asn1Core, pSetCursor, pszErrorTag, "set");
539}
540
541
542RTDECL(int) RTAsn1CursorGetContextTagNCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uExpectedTag,
543 PCRTASN1COREVTABLE pVtable, PRTASN1CONTEXTTAG pCtxTag, PRTASN1CURSOR pCtxTagCursor,
544 const char *pszErrorTag)
545{
546 int rc = rtAsn1CursorGetXxxxCursor(pCursor, fFlags, uExpectedTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED,
547 &pCtxTag->Asn1Core, pCtxTagCursor, pszErrorTag, "ctx tag");
548 pCtxTag->Asn1Core.pOps = pVtable;
549 return rc;
550}
551
552
553RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
554{
555 uint32_t cbSavedLeft = pCursor->cbLeft;
556 uint8_t const *pbSavedCur = pCursor->pbCur;
557 uint8_t const fSavedFlags = pCursor->fFlags;
558 PRTERRINFO const pErrInfo = pCursor->pPrimary->pErrInfo;
559 pCursor->pPrimary->pErrInfo = NULL;
560
561 int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, "peek");
562
563 pCursor->pPrimary->pErrInfo = pErrInfo;
564 pCursor->pbCur = pbSavedCur;
565 pCursor->cbLeft = cbSavedLeft;
566 pCursor->fFlags = fSavedFlags;
567 return rc;
568}
569
570
571RTDECL(bool) RTAsn1CursorIsNextEx(PRTASN1CURSOR pCursor, uint32_t uTag, uint8_t fClass)
572{
573 RTASN1CORE Asn1Core;
574 int rc = RTAsn1CursorPeek(pCursor, &Asn1Core);
575 if (RT_SUCCESS(rc))
576 return uTag == Asn1Core.uTag
577 && fClass == Asn1Core.fClass;
578 return false;
579}
580
581
582/** @name Legacy Interfaces.
583 * @{ */
584RTDECL(int) RTAsn1CursorGetCore(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
585{
586 return RTAsn1Core_DecodeAsn1(pCursor, fFlags, pAsn1Core, pszErrorTag);
587}
588
589
590RTDECL(int) RTAsn1CursorGetNull(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1NULL pNull, const char *pszErrorTag)
591{
592 return RTAsn1Null_DecodeAsn1(pCursor, fFlags, pNull, pszErrorTag);
593}
594
595
596RTDECL(int) RTAsn1CursorGetInteger(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1INTEGER pInteger, const char *pszErrorTag)
597{
598 return RTAsn1Integer_DecodeAsn1(pCursor, fFlags, pInteger, pszErrorTag);
599}
600
601
602RTDECL(int) RTAsn1CursorGetBoolean(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BOOLEAN pBoolean, const char *pszErrorTag)
603{
604 return RTAsn1Boolean_DecodeAsn1(pCursor, fFlags, pBoolean, pszErrorTag);
605}
606
607
608RTDECL(int) RTAsn1CursorGetObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId, const char *pszErrorTag)
609{
610 return RTAsn1ObjId_DecodeAsn1(pCursor, fFlags, pObjId, pszErrorTag);
611}
612
613
614RTDECL(int) RTAsn1CursorGetTime(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pTime, const char *pszErrorTag)
615{
616 return RTAsn1Time_DecodeAsn1(pCursor, fFlags, pTime, pszErrorTag);
617}
618
619
620RTDECL(int) RTAsn1CursorGetBitStringEx(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pBitString,
621 const char *pszErrorTag)
622{
623 return RTAsn1BitString_DecodeAsn1Ex(pCursor, fFlags, cMaxBits, pBitString, pszErrorTag);
624}
625
626
627RTDECL(int) RTAsn1CursorGetBitString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BITSTRING pBitString, const char *pszErrorTag)
628{
629 return RTAsn1BitString_DecodeAsn1(pCursor, fFlags, pBitString, pszErrorTag);
630}
631
632
633RTDECL(int) RTAsn1CursorGetOctetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OCTETSTRING pOctetString,
634 const char *pszErrorTag)
635{
636 return RTAsn1OctetString_DecodeAsn1(pCursor, fFlags, pOctetString, pszErrorTag);
637}
638
639
640RTDECL(int) RTAsn1CursorGetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
641{
642 return RTAsn1String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
643}
644
645
646RTDECL(int) RTAsn1CursorGetIa5String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
647{
648 return RTAsn1Ia5String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
649}
650
651
652RTDECL(int) RTAsn1CursorGetUtf8String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
653{
654 return RTAsn1Utf8String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
655}
656
657
658RTDECL(int) RTAsn1CursorGetBmpString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
659{
660 return RTAsn1BmpString_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
661}
662
663
664RTDECL(int) RTAsn1CursorGetDynType(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag)
665{
666 return RTAsn1DynType_DecodeAsn1(pCursor, fFlags, pDynType, pszErrorTag);
667}
668/** @} */
669
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette