VirtualBox

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

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

© 2023 Oracle
ContactPrivacy policyTerms of Use