VirtualBox

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

Last change on this file since 84379 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • 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.2 KB
Line 
1/* $Id: asn1-cursor.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - ASN.1, Basic Operations.
4 */
5
6/*
7 * Copyright (C) 2006-2020 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/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 pPrimaryCursor->pbFirst = (uint8_t const *)pvFirst;
78 return &pPrimaryCursor->Cursor;
79}
80
81
82RTDECL(int) RTAsn1CursorInitSub(PRTASN1CURSOR pParent, uint32_t cb, PRTASN1CURSOR pChild, const char *pszErrorTag)
83{
84 AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
85 AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
86
87 pChild->pbCur = pParent->pbCur;
88 pChild->cbLeft = cb;
89 pChild->fFlags = pParent->fFlags & ~RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
90 pChild->cDepth = pParent->cDepth + 1;
91 AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
92 pChild->abReserved[0] = 0;
93 pChild->abReserved[1] = 0;
94 pChild->pPrimary = pParent->pPrimary;
95 pChild->pUp = pParent;
96 pChild->pszErrorTag = pszErrorTag;
97
98 AssertReturn(pParent->cbLeft >= cb, VERR_ASN1_INTERNAL_ERROR_3);
99 pParent->pbCur += cb;
100 pParent->cbLeft -= cb;
101
102 return VINF_SUCCESS;
103}
104
105
106RTDECL(int) RTAsn1CursorInitSubFromCore(PRTASN1CURSOR pParent, PRTASN1CORE pAsn1Core,
107 PRTASN1CURSOR pChild, const char *pszErrorTag)
108{
109 AssertReturn(pParent->pPrimary, VERR_ASN1_INTERNAL_ERROR_1);
110 AssertReturn(pParent->pbCur, VERR_ASN1_INTERNAL_ERROR_2);
111
112 pChild->pbCur = pAsn1Core->uData.pu8;
113 pChild->cbLeft = pAsn1Core->cb;
114 pChild->fFlags = pParent->fFlags & ~RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
115 pChild->cDepth = pParent->cDepth + 1;
116 AssertReturn(pChild->cDepth < RTASN1_MAX_NESTING, VERR_ASN1_TOO_DEEPLY_NESTED);
117 pChild->abReserved[0] = 0;
118 pChild->abReserved[1] = 0;
119 pChild->pPrimary = pParent->pPrimary;
120 pChild->pUp = pParent;
121 pChild->pszErrorTag = pszErrorTag;
122
123 return VINF_SUCCESS;
124}
125
126
127RTDECL(int) RTAsn1CursorSetInfoV(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, va_list va)
128{
129 PRTERRINFO pErrInfo = pCursor->pPrimary->pErrInfo;
130 if (pErrInfo)
131 {
132 /* Format the message. */
133 RTErrInfoSetV(pErrInfo, rc, pszMsg, va);
134
135 /* Add the prefixes. This isn't the fastest way, but it's the one
136 which eats the least stack. */
137 char *pszBuf = pErrInfo->pszMsg;
138 size_t cbBuf = pErrInfo->cbMsg;
139 if (pszBuf && cbBuf > 32)
140 {
141 size_t cbMove = strlen(pszBuf) + 1;
142
143 /* Make sure there is a ': '. */
144 bool fFirst = false;
145 if (pszMsg[0] != '%' || pszMsg[1] != 's' || pszMsg[2] != ':')
146 {
147 if (cbMove + 2 < cbBuf)
148 {
149 memmove(pszBuf + 2, pszBuf, cbMove);
150 pszBuf[0] = ':';
151 pszBuf[1] = ' ';
152 cbMove += 2;
153 fFirst = true;
154 }
155 }
156
157 /* Add the prefixes from the cursor chain. */
158 while (pCursor)
159 {
160 if (pCursor->pszErrorTag)
161 {
162 size_t cchErrorTag = strlen(pCursor->pszErrorTag);
163 if (cchErrorTag + !fFirst + cbMove > cbBuf)
164 break;
165 memmove(pszBuf + cchErrorTag + !fFirst, pszBuf, cbMove);
166 memcpy(pszBuf, pCursor->pszErrorTag, cchErrorTag);
167 if (!fFirst)
168 pszBuf[cchErrorTag] = '.';
169 cbMove += cchErrorTag + !fFirst;
170 fFirst = false;
171 }
172 pCursor = pCursor->pUp;
173 }
174 }
175 }
176
177 return rc;
178}
179
180
181RTDECL(int) RTAsn1CursorSetInfo(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, ...)
182{
183 va_list va;
184 va_start(va, pszMsg);
185 rc = RTAsn1CursorSetInfoV(pCursor, rc, pszMsg, va);
186 va_end(va);
187 return rc;
188}
189
190
191RTDECL(bool) RTAsn1CursorIsEnd(PRTASN1CURSOR pCursor)
192{
193 if (pCursor->cbLeft == 0)
194 return true;
195 if (!(pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
196 return false;
197 return pCursor->cbLeft >= 2
198 && pCursor->pbCur[0] == 0
199 && pCursor->pbCur[1] == 0;
200}
201
202
203RTDECL(int) RTAsn1CursorCheckEnd(PRTASN1CURSOR pCursor)
204{
205 if (!(pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
206 {
207 if (pCursor->cbLeft == 0)
208 return VINF_SUCCESS;
209 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
210 "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
211 }
212
213 /*
214 * There must be exactly two zero bytes here.
215 */
216 if (pCursor->cbLeft == 2)
217 {
218 if ( pCursor->pbCur[0] == 0
219 && pCursor->pbCur[1] == 0)
220 return VINF_SUCCESS;
221 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
222 "%u (%#x) bytes left over [indef: %.*Rhxs]",
223 pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
224 }
225 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
226 "%u (%#x) byte(s) left over, exepcted exactly two zero bytes [indef len]",
227 pCursor->cbLeft, pCursor->cbLeft);
228}
229
230
231/**
232 * Worker for RTAsn1CursorCheckSeqEnd and RTAsn1CursorCheckSetEnd.
233 */
234static int rtAsn1CursorCheckSeqOrSetEnd(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
235{
236 if (!(pAsn1Core->fFlags & RTASN1CORE_F_INDEFINITE_LENGTH))
237 {
238 if (pCursor->cbLeft == 0)
239 return VINF_SUCCESS;
240 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
241 "%u (%#x) bytes left over", pCursor->cbLeft, pCursor->cbLeft);
242 }
243
244 if (pCursor->cbLeft >= 2)
245 {
246 if ( pCursor->pbCur[0] == 0
247 && pCursor->pbCur[1] == 0)
248 {
249 pAsn1Core->cb = (uint32_t)(pCursor->pbCur - pAsn1Core->uData.pu8);
250 pCursor->cbLeft -= 2;
251 pCursor->pbCur += 2;
252
253 PRTASN1CURSOR pParentCursor = pCursor->pUp;
254 if ( pParentCursor
255 && (pParentCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH))
256 {
257 pParentCursor->pbCur -= pCursor->cbLeft;
258 pParentCursor->cbLeft += pCursor->cbLeft;
259 return VINF_SUCCESS;
260 }
261
262 if (pCursor->cbLeft == 0)
263 return VINF_SUCCESS;
264
265 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
266 "%u (%#x) bytes left over (parent not indefinite length)", pCursor->cbLeft, pCursor->cbLeft);
267 }
268 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END, "%u (%#x) bytes left over [indef: %.*Rhxs]",
269 pCursor->cbLeft, pCursor->cbLeft, RT_MIN(pCursor->cbLeft, 16), pCursor->pbCur);
270 }
271 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NOT_AT_END,
272 "1 byte left over, expected two for indefinite length end-of-content sequence");
273}
274
275
276RTDECL(int) RTAsn1CursorCheckSeqEnd(PRTASN1CURSOR pCursor, PRTASN1SEQUENCECORE pSeqCore)
277{
278 return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSeqCore->Asn1Core);
279}
280
281
282RTDECL(int) RTAsn1CursorCheckSetEnd(PRTASN1CURSOR pCursor, PRTASN1SETCORE pSetCore)
283{
284 return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pSetCore->Asn1Core);
285}
286
287
288RTDECL(int) RTAsn1CursorCheckOctStrEnd(PRTASN1CURSOR pCursor, PRTASN1OCTETSTRING pOctetString)
289{
290 return rtAsn1CursorCheckSeqOrSetEnd(pCursor, &pOctetString->Asn1Core);
291}
292
293
294RTDECL(PRTASN1ALLOCATION) RTAsn1CursorInitAllocation(PRTASN1CURSOR pCursor, PRTASN1ALLOCATION pAllocation)
295{
296 pAllocation->cbAllocated = 0;
297 pAllocation->cReallocs = 0;
298 pAllocation->uReserved0 = 0;
299 pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
300 return pAllocation;
301}
302
303
304RTDECL(PRTASN1ARRAYALLOCATION) RTAsn1CursorInitArrayAllocation(PRTASN1CURSOR pCursor, PRTASN1ARRAYALLOCATION pAllocation,
305 size_t cbEntry)
306{
307 Assert(cbEntry >= sizeof(RTASN1CORE));
308 Assert(cbEntry < _1M);
309 Assert(RT_ALIGN_Z(cbEntry, sizeof(void *)) == cbEntry);
310 pAllocation->cbEntry = (uint32_t)cbEntry;
311 pAllocation->cPointersAllocated = 0;
312 pAllocation->cEntriesAllocated = 0;
313 pAllocation->cResizeCalls = 0;
314 pAllocation->uReserved0 = 0;
315 pAllocation->pAllocator = pCursor->pPrimary->pAllocator;
316 return pAllocation;
317}
318
319
320RTDECL(int) RTAsn1CursorReadHdr(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
321{
322 /*
323 * Initialize the return structure in case of failure.
324 */
325 pAsn1Core->uTag = 0;
326 pAsn1Core->fClass = 0;
327 pAsn1Core->uRealTag = 0;
328 pAsn1Core->fRealClass = 0;
329 pAsn1Core->cbHdr = 0;
330 pAsn1Core->cb = 0;
331 pAsn1Core->fFlags = 0;
332 pAsn1Core->uData.pv = NULL;
333 pAsn1Core->pOps = NULL;
334
335 /*
336 * The header has at least two bytes: Type & length.
337 */
338 if (pCursor->cbLeft >= 2)
339 {
340 uint32_t uTag = pCursor->pbCur[0];
341 uint32_t cb = pCursor->pbCur[1];
342 pCursor->cbLeft -= 2;
343 pCursor->pbCur += 2;
344
345 pAsn1Core->uRealTag = pAsn1Core->uTag = uTag & ASN1_TAG_MASK;
346 pAsn1Core->fRealClass = pAsn1Core->fClass = uTag & ~ASN1_TAG_MASK;
347 pAsn1Core->cbHdr = 2;
348 if ((uTag & ASN1_TAG_MASK) == ASN1_TAG_USE_LONG_FORM)
349 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_LONG_TAG,
350 "%s: Implement parsing of tags > 30: %#x (length=%#x)", pszErrorTag, uTag, cb);
351
352 /* Extended length field? */
353 if (cb & RT_BIT(7))
354 {
355 if (cb != RT_BIT(7))
356 {
357 /* Definite form. */
358 uint8_t cbEnc = cb & 0x7f;
359 if (cbEnc > pCursor->cbLeft)
360 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
361 "%s: Extended BER length field longer than available data: %#x vs %#x (uTag=%#x)",
362 pszErrorTag, cbEnc, pCursor->cbLeft, uTag);
363 switch (cbEnc)
364 {
365 case 1:
366 cb = pCursor->pbCur[0];
367 break;
368 case 2:
369 cb = RT_MAKE_U16(pCursor->pbCur[1], pCursor->pbCur[0]);
370 break;
371 case 3:
372 cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0], 0);
373 break;
374 case 4:
375 cb = RT_MAKE_U32_FROM_U8(pCursor->pbCur[3], pCursor->pbCur[2], pCursor->pbCur[1], pCursor->pbCur[0]);
376 break;
377 default:
378 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
379 "%s: Too long/short extended BER length field: %#x (uTag=%#x)",
380 pszErrorTag, cbEnc, uTag);
381 }
382 pCursor->cbLeft -= cbEnc;
383 pCursor->pbCur += cbEnc;
384 pAsn1Core->cbHdr += cbEnc;
385
386 /* Check the length encoding efficiency (T-REC-X.690-200811 10.1, 9.1). */
387 if (pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER))
388 {
389 if (cb <= 0x7f)
390 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
391 "%s: Invalid DER/CER length encoding: cbEnc=%u cb=%#x uTag=%#x",
392 pszErrorTag, cbEnc, cb, uTag);
393 uint8_t cbNeeded;
394 if (cb <= 0x000000ff) cbNeeded = 1;
395 else if (cb <= 0x0000ffff) cbNeeded = 2;
396 else if (cb <= 0x00ffffff) cbNeeded = 3;
397 else cbNeeded = 4;
398 if (cbNeeded != cbEnc)
399 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH_ENCODING,
400 "%s: Invalid DER/CER length encoding: cb=%#x uTag=%#x cbEnc=%u cbNeeded=%u",
401 pszErrorTag, cb, uTag, cbEnc, cbNeeded);
402 }
403 }
404 /* Indefinite form. */
405 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_DER)
406 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_INDEFINITE_LENGTH,
407 "%s: Indefinite length form not allowed in DER mode (uTag=%#x).", pszErrorTag, uTag);
408 else if (!(uTag & ASN1_TAGFLAG_CONSTRUCTED))
409 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
410 "%s: Indefinite BER/CER encoding is for non-constructed tag (uTag=%#x)", pszErrorTag, uTag);
411 else if ( uTag != (ASN1_TAG_SEQUENCE | ASN1_TAGFLAG_CONSTRUCTED)
412 && uTag != (ASN1_TAG_SET | ASN1_TAGFLAG_CONSTRUCTED)
413 && (uTag & (ASN1_TAGFLAG_CONSTRUCTED | ASN1_TAGCLASS_CONTEXT))
414 != (ASN1_TAGFLAG_CONSTRUCTED | ASN1_TAGCLASS_CONTEXT) )
415 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
416 "%s: Indefinite BER/CER encoding not supported for this tag (uTag=%#x)", pszErrorTag, uTag);
417 else if (pCursor->fFlags & RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH)
418 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
419 "%s: Nested indefinite BER/CER encoding. (uTag=%#x)", pszErrorTag, uTag);
420 else if (pCursor->cbLeft < 2)
421 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_INDEFINITE_LENGTH,
422 "%s: Too little data left for indefinite BER/CER encoding (uTag=%#x)", pszErrorTag, uTag);
423 else
424 {
425 pCursor->fFlags |= RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH;
426 pAsn1Core->fFlags |= RTASN1CORE_F_INDEFINITE_LENGTH;
427 cb = pCursor->cbLeft; /* Start out with the whole sequence, adjusted later upon reach the end. */
428 }
429 }
430 /* else if (cb == 0 && uTag == 0) { end of content } - callers handle this */
431
432 /* Check if the length makes sense. */
433 if (cb > pCursor->cbLeft)
434 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_BAD_LENGTH,
435 "%s: BER value length out of bounds: %#x (max=%#x uTag=%#x)",
436 pszErrorTag, cb, pCursor->cbLeft, uTag);
437
438 pAsn1Core->fFlags |= RTASN1CORE_F_PRESENT | RTASN1CORE_F_DECODED_CONTENT;
439 pAsn1Core->cb = cb;
440 pAsn1Core->uData.pv = (void *)pCursor->pbCur;
441 return VINF_SUCCESS;
442 }
443
444 if (pCursor->cbLeft)
445 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_TOO_LITTLE_DATA_LEFT,
446 "%s: Too little data left to form a valid BER header", pszErrorTag);
447 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_NO_MORE_DATA,
448 "%s: No more data reading BER header", pszErrorTag);
449}
450
451
452RTDECL(int) RTAsn1CursorMatchTagClassFlagsEx(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
453 bool fString, uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
454{
455 if (pAsn1Core->uTag == uTag)
456 {
457 if (pAsn1Core->fClass == fClass)
458 return VINF_SUCCESS;
459 if ( fString
460 && pAsn1Core->fClass == (fClass | ASN1_TAGFLAG_CONSTRUCTED))
461 {
462 if (!(pCursor->fFlags & (RTASN1CURSOR_FLAGS_DER | RTASN1CURSOR_FLAGS_CER)))
463 return VINF_SUCCESS;
464 if (pCursor->fFlags & RTASN1CURSOR_FLAGS_CER)
465 {
466 if (pAsn1Core->cb > 1000)
467 return VINF_SUCCESS;
468 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
469 "%s: Constructed %s only allowed for >1000 byte in CER encoding: cb=%#x uTag=%#x fClass=%#x",
470 pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
471 }
472 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
473 "%s: DER encoding does not allow constructed %s (cb=%#x uTag=%#x fClass=%#x)",
474 pszErrorTag, pszWhat, pAsn1Core->cb, pAsn1Core->uTag, pAsn1Core->fClass);
475 }
476 }
477
478 if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
479 {
480 pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
481 pAsn1Core->uRealTag = uTag;
482 pAsn1Core->fRealClass = fClass;
483 return VINF_SUCCESS;
484 }
485
486 return RTAsn1CursorSetInfo(pCursor, pAsn1Core->uTag != uTag ? VERR_ASN1_CURSOR_TAG_MISMATCH : VERR_ASN1_CURSOR_TAG_FLAG_CLASS_MISMATCH,
487 "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
488 pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
489}
490
491
492
493static int rtAsn1CursorGetXxxxCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uTag, uint8_t fClass,
494 PRTASN1CORE pAsn1Core, PRTASN1CURSOR pRetCursor,
495 const char *pszErrorTag, const char *pszWhat)
496{
497 int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, pszErrorTag);
498 if (RT_SUCCESS(rc))
499 {
500 if ( pAsn1Core->uTag == uTag
501 && pAsn1Core->fClass == fClass)
502 rc = VINF_SUCCESS;
503 else if (fFlags & RTASN1CURSOR_GET_F_IMPLICIT)
504 {
505 pAsn1Core->fFlags |= RTASN1CORE_F_TAG_IMPLICIT;
506 pAsn1Core->uRealTag = uTag;
507 pAsn1Core->fRealClass = fClass;
508 rc = VINF_SUCCESS;
509 }
510 else
511 return RTAsn1CursorSetInfo(pCursor, VERR_ASN1_CURSOR_ILLEGAL_CONSTRUCTED_STRING,
512 "%s: Unexpected %s type/flags: %#x/%#x (expected %#x/%#x)",
513 pszErrorTag, pszWhat, pAsn1Core->uTag, pAsn1Core->fClass, uTag, fClass);
514 rc = RTAsn1CursorInitSub(pCursor, pAsn1Core->cb, pRetCursor, pszErrorTag);
515 if (RT_SUCCESS(rc))
516 {
517 pAsn1Core->fFlags |= RTASN1CORE_F_PRIMITE_TAG_STRUCT;
518 return VINF_SUCCESS;
519 }
520 }
521 return rc;
522}
523
524
525RTDECL(int) RTAsn1CursorGetSequenceCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
526 PRTASN1SEQUENCECORE pSeqCore, PRTASN1CURSOR pSeqCursor, const char *pszErrorTag)
527{
528 return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SEQUENCE, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
529 &pSeqCore->Asn1Core, pSeqCursor, pszErrorTag, "sequence");
530}
531
532
533RTDECL(int) RTAsn1CursorGetSetCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
534 PRTASN1SETCORE pSetCore, PRTASN1CURSOR pSetCursor, const char *pszErrorTag)
535{
536 return rtAsn1CursorGetXxxxCursor(pCursor, fFlags, ASN1_TAG_SET, ASN1_TAGCLASS_UNIVERSAL | ASN1_TAGFLAG_CONSTRUCTED,
537 &pSetCore->Asn1Core, pSetCursor, pszErrorTag, "set");
538}
539
540
541RTDECL(int) RTAsn1CursorGetContextTagNCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uExpectedTag,
542 PCRTASN1COREVTABLE pVtable, PRTASN1CONTEXTTAG pCtxTag, PRTASN1CURSOR pCtxTagCursor,
543 const char *pszErrorTag)
544{
545 int rc = rtAsn1CursorGetXxxxCursor(pCursor, fFlags, uExpectedTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED,
546 &pCtxTag->Asn1Core, pCtxTagCursor, pszErrorTag, "ctx tag");
547 pCtxTag->Asn1Core.pOps = pVtable;
548 return rc;
549}
550
551
552RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core)
553{
554 uint32_t cbSavedLeft = pCursor->cbLeft;
555 uint8_t const *pbSavedCur = pCursor->pbCur;
556 uint8_t const fSavedFlags = pCursor->fFlags;
557 PRTERRINFO const pErrInfo = pCursor->pPrimary->pErrInfo;
558 pCursor->pPrimary->pErrInfo = NULL;
559
560 int rc = RTAsn1CursorReadHdr(pCursor, pAsn1Core, "peek");
561
562 pCursor->pPrimary->pErrInfo = pErrInfo;
563 pCursor->pbCur = pbSavedCur;
564 pCursor->cbLeft = cbSavedLeft;
565 pCursor->fFlags = fSavedFlags;
566 return rc;
567}
568
569
570RTDECL(bool) RTAsn1CursorIsNextEx(PRTASN1CURSOR pCursor, uint32_t uTag, uint8_t fClass)
571{
572 RTASN1CORE Asn1Core;
573 int rc = RTAsn1CursorPeek(pCursor, &Asn1Core);
574 if (RT_SUCCESS(rc))
575 return uTag == Asn1Core.uTag
576 && fClass == Asn1Core.fClass;
577 return false;
578}
579
580
581/** @name Legacy Interfaces.
582 * @{ */
583RTDECL(int) RTAsn1CursorGetCore(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1CORE pAsn1Core, const char *pszErrorTag)
584{
585 return RTAsn1Core_DecodeAsn1(pCursor, fFlags, pAsn1Core, pszErrorTag);
586}
587
588
589RTDECL(int) RTAsn1CursorGetNull(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1NULL pNull, const char *pszErrorTag)
590{
591 return RTAsn1Null_DecodeAsn1(pCursor, fFlags, pNull, pszErrorTag);
592}
593
594
595RTDECL(int) RTAsn1CursorGetInteger(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1INTEGER pInteger, const char *pszErrorTag)
596{
597 return RTAsn1Integer_DecodeAsn1(pCursor, fFlags, pInteger, pszErrorTag);
598}
599
600
601RTDECL(int) RTAsn1CursorGetBoolean(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BOOLEAN pBoolean, const char *pszErrorTag)
602{
603 return RTAsn1Boolean_DecodeAsn1(pCursor, fFlags, pBoolean, pszErrorTag);
604}
605
606
607RTDECL(int) RTAsn1CursorGetObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId, const char *pszErrorTag)
608{
609 return RTAsn1ObjId_DecodeAsn1(pCursor, fFlags, pObjId, pszErrorTag);
610}
611
612
613RTDECL(int) RTAsn1CursorGetTime(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pTime, const char *pszErrorTag)
614{
615 return RTAsn1Time_DecodeAsn1(pCursor, fFlags, pTime, pszErrorTag);
616}
617
618
619RTDECL(int) RTAsn1CursorGetBitStringEx(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pBitString,
620 const char *pszErrorTag)
621{
622 return RTAsn1BitString_DecodeAsn1Ex(pCursor, fFlags, cMaxBits, pBitString, pszErrorTag);
623}
624
625
626RTDECL(int) RTAsn1CursorGetBitString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BITSTRING pBitString, const char *pszErrorTag)
627{
628 return RTAsn1BitString_DecodeAsn1(pCursor, fFlags, pBitString, pszErrorTag);
629}
630
631
632RTDECL(int) RTAsn1CursorGetOctetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OCTETSTRING pOctetString,
633 const char *pszErrorTag)
634{
635 return RTAsn1OctetString_DecodeAsn1(pCursor, fFlags, pOctetString, pszErrorTag);
636}
637
638
639RTDECL(int) RTAsn1CursorGetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
640{
641 return RTAsn1String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
642}
643
644
645RTDECL(int) RTAsn1CursorGetIa5String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
646{
647 return RTAsn1Ia5String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
648}
649
650
651RTDECL(int) RTAsn1CursorGetUtf8String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
652{
653 return RTAsn1Utf8String_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
654}
655
656
657RTDECL(int) RTAsn1CursorGetBmpString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag)
658{
659 return RTAsn1BmpString_DecodeAsn1(pCursor, fFlags, pString, pszErrorTag);
660}
661
662
663RTDECL(int) RTAsn1CursorGetDynType(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag)
664{
665 return RTAsn1DynType_DecodeAsn1(pCursor, fFlags, pDynType, pszErrorTag);
666}
667/** @} */
668
Note: See TracBrowser for help on using the repository browser.

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