VirtualBox

source: vbox/trunk/include/iprt/asn1.h@ 95929

Last change on this file since 95929 was 95658, checked in by vboxsync, 2 years ago

IPRT/RTAsn1: Added a RTAsn1DynType_SetToNull function. bugref:8691

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 99.1 KB
Line 
1/** @file
2 * IPRT - Abstract Syntax Notation One (ASN.1).
3 */
4
5/*
6 * Copyright (C) 2006-2022 Oracle Corporation
7 *
8 * This file is part of VirtualBox Open Source Edition (OSE), as
9 * available from http://www.virtualbox.org. This file is free software;
10 * you can redistribute it and/or modify it under the terms of the GNU
11 * General Public License (GPL) as published by the Free Software
12 * Foundation, in version 2 as it comes in the "COPYING" file of the
13 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
14 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15 *
16 * The contents of this file may alternatively be used under the terms
17 * of the Common Development and Distribution License Version 1.0
18 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
19 * VirtualBox OSE distribution, in which case the provisions of the
20 * CDDL are applicable instead of those of the GPL.
21 *
22 * You may elect to license modified versions of this file under the
23 * terms and conditions of either the GPL or the CDDL or both.
24 */
25
26#ifndef IPRT_INCLUDED_asn1_h
27#define IPRT_INCLUDED_asn1_h
28#ifndef RT_WITHOUT_PRAGMA_ONCE
29# pragma once
30#endif
31
32#include <iprt/time.h>
33#include <iprt/stdarg.h>
34#include <iprt/errcore.h>
35#include <iprt/formats/asn1.h>
36
37
38RT_C_DECLS_BEGIN
39
40/** @defgroup grp_rt_asn1 RTAsn1 - Abstract Syntax Notation One
41 * @ingroup grp_rt
42 * @{
43 */
44
45
46/** Pointer to ASN.1 allocation information. */
47typedef struct RTASN1ALLOCATION *PRTASN1ALLOCATION;
48/** Pointer to ASN.1 array allocation information. */
49typedef struct RTASN1ARRAYALLOCATION *PRTASN1ARRAYALLOCATION;
50/** Pointer to a ASN.1 byte decoder cursor. */
51typedef struct RTASN1CURSOR *PRTASN1CURSOR;
52
53
54/**
55 * Sketch of a custom ASN.1 allocator virtual method table.
56 *
57 * Any information required by the allocator should be associated with this
58 * structure, i.e. use this as a kind of parent class. This saves storage in
59 * RTASN1ALLOCATORINFO and possibly reduces the number of parameters by one.
60 */
61typedef struct RTASN1ALLOCATORVTABLE
62{
63 /**
64 * Free a chunk of memory allocated by this allocator.
65 *
66 * @returns IPRT status code.
67 * @param pThis Pointer to the vtable structure.
68 * @param pAllocation Pointer to the allocation info structure.
69 * @param pv Pointer to the memory that shall be freed. Not NULL.
70 */
71 DECLCALLBACKMEMBER(void, pfnFree,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ALLOCATION pAllocation,
72 void *pv));
73 /**
74 * Allocates a chunk of memory, all initialized to zero.
75 *
76 * @returns IPRT status code.
77 * @param pThis Pointer to the vtable structure.
78 * @param pAllocation Pointer to the allocation info structure.
79 * @param ppv Where to store the pointer on success.
80 * @param cb The minimum number of bytes to allocate. The actual
81 * number of bytes allocated shall be stored in
82 * pInfo->cbAllocated on success.
83 */
84 DECLCALLBACKMEMBER(int, pfnAlloc,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ALLOCATION pAllocation,
85 void **ppv, size_t cb));
86 /**
87 * Reallocates a memory allocation.
88 *
89 * New memory does not need to be initialized, the caller takes care of that.
90 *
91 * This will not need to deal with free (@a cbNew == 0) or the initial
92 * allocation (@a pvOld == NULL), those calls will be directed to pfnFree and
93 * pfnAlloc respectively.
94 *
95 * @returns IPRT status code.
96 * @param pThis Pointer to the vtable structure.
97 * @param pAllocation Pointer to the allocation info structure.
98 * @param pvOld Pointer to the current allocation. Shall remain
99 * valid on failure, but may be invalid on success.
100 * @param ppvNew Where to store the pointer on success. Shall not be
101 * touched, except on successful returns.
102 * @param cbNew The new minimum allocation size. The actual number
103 * of bytes allocated shall be stored in
104 * pInfo->cbAllocated on success.
105 */
106 DECLCALLBACKMEMBER(int, pfnRealloc,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ALLOCATION pAllocation,
107 void *pvOld, void **ppvNew, size_t cbNew));
108
109 /**
110 * Frees an array allocation (the array an all instances in it).
111 *
112 * @returns IPRT status code.
113 * @param pThis Pointer to the vtable structure.
114 * @param pAllocation Pointer to the allocation info structure.
115 * @param papvArray Pointer to the pointer array to be freed. Not NULL.
116 */
117 DECLCALLBACKMEMBER(void, pfnFreeArray,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ARRAYALLOCATION pAllocation,
118 void **papvArray));
119 /**
120 * Grows the array to at least @a cMinEntries.
121 *
122 * The entries are initalized with ZEROs.
123 *
124 * @returns IPRT status code.
125 * @param pThis Pointer to the vtable structure.
126 * @param pAllocation Pointer to the allocation info structure.
127 * @param ppapvArray Pointer to the pointer to the array to be grown (or
128 * allocated).
129 * @param cMinEntries The minimum number of entries (array size and
130 * instantiated entries) that must be available
131 * on successful return.
132 */
133 DECLCALLBACKMEMBER(int, pfnGrowArray,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ARRAYALLOCATION pAllocation,
134 void ***ppapvArray, uint32_t cMinEntries));
135 /**
136 * Shrinks the array (depends on allocator policy).
137 *
138 * If memory isn't freed, the implementation must fill the entries being
139 * shredded with ZEROs so the growth optimizations in RTAsn1MemResizeArray
140 * returns ZEROed entries.
141 *
142 * @returns IPRT status code.
143 * @param pThis Pointer to the vtable structure.
144 * @param pAllocation Pointer to the allocation info structure.
145 * @param ppapvArray Pointer to the pointer to the array to shrunk.
146 * @param cNew The new entry count.
147 * @param cCurrent The new entry count.
148 */
149 DECLCALLBACKMEMBER(void, pfnShrinkArray,(struct RTASN1ALLOCATORVTABLE const *pThis, PRTASN1ARRAYALLOCATION pAllocation,
150 void ***ppapvArray, uint32_t cNew, uint32_t cCurrent));
151} RTASN1ALLOCATORVTABLE;
152/** Pointer to an ASN.1 allocator vtable. */
153typedef RTASN1ALLOCATORVTABLE *PRTASN1ALLOCATORVTABLE;
154/** Pointer to a const ASN.1 allocator vtable. */
155typedef RTASN1ALLOCATORVTABLE const *PCRTASN1ALLOCATORVTABLE;
156
157/** The default ASN.1 allocator. */
158extern RTDATADECL(RTASN1ALLOCATORVTABLE const) g_RTAsn1DefaultAllocator;
159
160/** The Electric Fence ASN.1 allocator. */
161extern RTDATADECL(RTASN1ALLOCATORVTABLE const) g_RTAsn1EFenceAllocator;
162
163/** The safer ASN.1 allocator for sensitive data. */
164extern RTDATADECL(RTASN1ALLOCATORVTABLE const) g_RTAsn1SaferAllocator;
165
166
167/**
168 * Allocation information.
169 */
170typedef struct RTASN1ALLOCATION
171{
172 /** The number of bytes currently allocated. */
173 uint32_t cbAllocated;
174 /** Number of realloc calls. */
175 uint16_t cReallocs;
176 /** Reserved / padding. */
177 uint16_t uReserved0;
178 /** Allocator vtable, NULL for the default allocator. */
179 PCRTASN1ALLOCATORVTABLE pAllocator;
180} RTASN1ALLOCATION;
181
182
183/**
184 * Pointer array allocation information.
185 *
186 * Used by SET OF and SEQUENCE OF structures (typically automatically
187 * generated).
188 */
189typedef struct RTASN1ARRAYALLOCATION
190{
191 /** The size of the array entry. */
192 uint32_t cbEntry;
193 /** The size of the pointer array allocation. */
194 uint32_t cPointersAllocated;
195 /** Number of entry instances allocated. This can be greater than the
196 * official array size. */
197 uint32_t cEntriesAllocated;
198 /** Number of array resizing calls (for increasing growth rate).
199 * Maintained by RTAsn1MemResizeArray(). */
200 uint16_t cResizeCalls;
201 /** Reserved / padding. */
202 uint16_t uReserved0;
203 /** Allocator vtable, NULL for the default allocator. */
204 PCRTASN1ALLOCATORVTABLE pAllocator;
205} RTASN1ARRAYALLOCATION;
206
207
208/**
209 * Allocate a block of zero initialized memory.
210 *
211 * @returns IPRT status code.
212 * @param pAllocation The allocation record (initialized by
213 * RTAsn1CursorInitAllocation or similar).
214 * @param ppvMem Where to return the pointer to the block.
215 * @param cbMem The minimum number of bytes to allocate.
216 */
217RTDECL(int) RTAsn1MemAllocZ(PRTASN1ALLOCATION pAllocation, void **ppvMem, size_t cbMem);
218
219/**
220 * Allocates a block of memory initialized to the content of @a pvSrc.
221 *
222 * @returns IPRT status code.
223 * @param pAllocation The allocation record (initialized by
224 * RTAsn1CursorInitAllocation or similar).
225 * @param ppvMem Where to return the pointer to the block.
226 * @param pvSrc The source memory.
227 * @param cbMem The minimum number of bytes to allocate.
228 */
229RTDECL(int) RTAsn1MemDup(PRTASN1ALLOCATION pAllocation, void **ppvMem, void const *pvSrc, size_t cbMem);
230
231/**
232 * Free a memory block.
233 *
234 * @param pAllocation The allocation record (initialized by
235 * RTAsn1CursorInitAllocation or similar).
236 * @param pv The memory block to free. NULL will be ignored.
237 */
238RTDECL(void) RTAsn1MemFree(PRTASN1ALLOCATION pAllocation, void *pv);
239
240/**
241 * Initalize an allocation.
242 *
243 * @returns pAllocation
244 * @param pAllocation The allocation record (initialized by
245 * RTAsn1CursorInitAllocation or similar).
246 * @param pAllocator The allocator
247 */
248RTDECL(PRTASN1ALLOCATION) RTAsn1MemInitAllocation(PRTASN1ALLOCATION pAllocation, PCRTASN1ALLOCATORVTABLE pAllocator);
249
250/**
251 * Initalize an array allocation.
252 *
253 * @returns pAllocation
254 * @param pAllocation The allocation record (initialized by
255 * RTAsn1CursorInitAllocation or similar).
256 * @param pAllocator The allocator
257 * @param cbEntry The entry size.
258 */
259RTDECL(PRTASN1ARRAYALLOCATION) RTAsn1MemInitArrayAllocation(PRTASN1ARRAYALLOCATION pAllocation,
260 PCRTASN1ALLOCATORVTABLE pAllocator, size_t cbEntry);
261
262/**
263 * Resize an array with zero initialized memory.
264 *
265 * @returns IPRT status code.
266 * @param pAllocation The allocation record (initialized by
267 * RTAsn1CursorInitAllocation or similar).
268 * @param ppapvArray Pointer to the variable pointing to the array. This is
269 * both input and output. Remains valid on failure.
270 * @param cCurrent The current entry count. (Relevant for zero
271 * initialization of the new entries.)
272 * @param cNew The new entry count.
273 */
274RTDECL(int) RTAsn1MemResizeArray(PRTASN1ARRAYALLOCATION pAllocation, void ***ppapvArray, uint32_t cCurrent, uint32_t cNew);
275
276/**
277 * Frees an array and all its entries.
278 *
279 * @param pAllocation The array allocation record (initialized by
280 * RTAsn1CursorInitArrayAllocation or similar).
281 * @param papvArray The array to free. NULL is ignored.
282 */
283RTDECL(void) RTAsn1MemFreeArray(PRTASN1ARRAYALLOCATION pAllocation, void **papvArray);
284
285
286/** Pointer to a core ASN.1 encoding info structure. */
287typedef struct RTASN1CORE *PRTASN1CORE;
288/** Pointer to a const core ASN.1 encoding info structure. */
289typedef struct RTASN1CORE const *PCRTASN1CORE;
290
291RTDECL(int) RTAsn1ContentAllocZ(struct RTASN1CORE *pAsn1Core, size_t cb, PCRTASN1ALLOCATORVTABLE pAllocator);
292RTDECL(int) RTAsn1ContentDup(struct RTASN1CORE *pAsn1Core, void const *pvSrc, size_t cbSrc, PCRTASN1ALLOCATORVTABLE pAllocator);
293RTDECL(int) RTAsn1ContentReallocZ(struct RTASN1CORE *pAsn1Core, size_t cb, PCRTASN1ALLOCATORVTABLE pAllocator);
294RTDECL(void) RTAsn1ContentFree(struct RTASN1CORE *pAsn1Core);
295
296
297
298/**
299 * ASN.1 object enumeration callback.
300 *
301 * @returns IPRT status code. VINF_SUCCESS continues the enumberation, all
302 * others quit it and is returned to the caller's caller.
303 * @param pAsn1Core The ASN.1 object we're called back about.
304 * @param pszName The member name. Array member names ends with
305 * '[#]'.
306 * @param uDepth The current depth.
307 * @param pvUser Callback user parameter.
308 */
309typedef DECLCALLBACKTYPE(int, FNRTASN1ENUMCALLBACK,(struct RTASN1CORE *pAsn1Core, const char *pszName, uint32_t uDepth,
310 void *pvUser));
311/** Pointer to an ASN.1 object enumeration callback. */
312typedef FNRTASN1ENUMCALLBACK *PFNRTASN1ENUMCALLBACK;
313
314/**
315 * ASN.1 object encoding writer callback.
316 *
317 * @returns IPRT status code.
318 * @param pvBuf Pointer to the bytes to output.
319 * @param cbToWrite The number of bytes to write.
320 * @param pvUser Callback user parameter.
321 * @param pErrInfo Where to store extended error info. Optional.
322 */
323typedef DECLCALLBACKTYPE(int, FNRTASN1ENCODEWRITER,(const void *pvBuf, size_t cbToWrite, void *pvUser, PRTERRINFO pErrInfo));
324/** Pointer to an ASN.1 encoding writer callback. */
325typedef FNRTASN1ENCODEWRITER *PFNRTASN1ENCODEWRITER;
326
327/** @name ASN.1 Vtable Method Types
328 * @{ */
329
330/**
331 * Destructor.
332 *
333 * RTAsn1Destroy will first destroy all children by recursive calls to pfnEnum,
334 * afterwards it will call this method to release any memory or other resources
335 * associated with this object. The memory backing the object structure shall
336 * not be freed by this method.
337 *
338 * @param pThisCore Pointer to the ASN.1 core to destroy.
339 */
340typedef DECLCALLBACKTYPE(void, FNRTASN1COREVTDTOR,(PRTASN1CORE pThisCore));
341/** Pointer to a FNRTASN1COREVTDTOR method. */
342typedef FNRTASN1COREVTDTOR *PFNRTASN1COREVTDTOR;
343
344/**
345 * Enumerate members (not necessary for primitive objects).
346 *
347 * @returns IPRT status code, any non VINF_SUCCESS value stems from pfnCallback.
348 * @param pThisCore Pointer to the ASN.1 core to enumerate members of.
349 * @param pfnCallback The callback.
350 * @param uDepth The depth of this object. Children are at +1.
351 * @param pvUser Callback user argument.
352 */
353typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTENUM,(PRTASN1CORE pThisCore, PFNRTASN1ENUMCALLBACK pfnCallback,
354 uint32_t uDepth, void *pvUser));
355/** Pointer to a FNRTASN1COREVTENUM method. */
356typedef FNRTASN1COREVTENUM *PFNRTASN1COREVTENUM;
357
358/**
359 * Clone method.
360 *
361 * @param pThisCore Pointer to the ASN.1 core to initialize as a clone
362 * of pSrcClone. (The caller is responsible for making
363 * sure there is sufficent space and such.)
364 * @param pSrcCore The object to clone.
365 * @param pAllocator The allocator to use.
366 */
367typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTCLONE,(PRTASN1CORE pThisCore, PCRTASN1CORE pSrcCore,
368 PCRTASN1ALLOCATORVTABLE pAllocator));
369/** Pointer to a FNRTASN1COREVTCLONE method. */
370typedef FNRTASN1COREVTCLONE *PFNRTASN1COREVTCLONE;
371
372/**
373 * Compare method.
374 *
375 * The caller makes sure both cores are present and have the same Vtable.
376 *
377 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
378 * @param pLeftCore Pointer to the ASN.1 core of the left side object.
379 * @param pRightCore Pointer to the ASN.1 core of the right side object.
380 */
381typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTCOMPARE,(PCRTASN1CORE pLeftCore, PCRTASN1CORE pRightCore));
382/** Pointer to a FNRTASN1COREVTCOMPARE method. */
383typedef FNRTASN1COREVTCOMPARE *PFNRTASN1COREVTCOMPARE;
384
385/**
386 * Check sanity method.
387 *
388 * @returns IPRT status code.
389 * @param pThisCore Pointer to the ASN.1 core of the object to check out.
390 * @param fFlags See RTASN1_CHECK_SANITY_F_XXX.
391 * @param pErrInfo Where to return additional error details. Optional.
392 * @param pszErrorTag Tag for the additional error details.
393 */
394typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTCHECKSANITY,(PCRTASN1CORE pThisCore, uint32_t fFlags,
395 PRTERRINFO pErrInfo, const char *pszErrorTag));
396/** Pointer to a FNRTASN1COREVTCHECKSANITY method. */
397typedef FNRTASN1COREVTCHECKSANITY *PFNRTASN1COREVTCHECKSANITY;
398
399/**
400 * Optional encoding preparations.
401 *
402 * On successful return, the pThisCore->cb value shall be valid and up to date.
403 * Will be called for any present object, including ones with default values and
404 * similar.
405 *
406 * @returns IPRT status code
407 * @param pThisCore Pointer to the ASN.1 core to enumerate members of.
408 * @param fFlags Encoding flags, RTASN1ENCODE_F_XXX.
409 * @param pErrInfo Where to return extra error information. Optional.
410 */
411typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTENCODEPREP,(PRTASN1CORE pThisCore, uint32_t fFlags, PRTERRINFO pErrInfo));
412/** Pointer to a FNRTASN1COREVTENCODEWRITE method. */
413typedef FNRTASN1COREVTENCODEPREP *PFNRTASN1COREVTENCODEPREP;
414
415/**
416 * Optional encoder writer.
417 *
418 * This writes the header as well as all the content. Will be called for any
419 * present object, including ones with default values and similar.
420 *
421 * @returns IPRT status code.
422 * @param pThisCore Pointer to the ASN.1 core to enumerate members of.
423 * @param fFlags Encoding flags, RTASN1ENCODE_F_XXX.
424 * @param pfnWriter The output writer function.
425 * @param pvUser The user context for the writer function.
426 * @param pErrInfo Where to return extra error information. Optional.
427 */
428typedef DECLCALLBACKTYPE(int, FNRTASN1COREVTENCODEWRITE,(PRTASN1CORE pThisCore, uint32_t fFlags, PFNRTASN1ENCODEWRITER pfnWriter,
429 void *pvUser, PRTERRINFO pErrInfo));
430/** Pointer to a FNRTASN1COREVTENCODEWRITE method. */
431typedef FNRTASN1COREVTENCODEWRITE *PFNRTASN1COREVTENCODEWRITE;
432/** @} */
433
434/** Mask of common flags. These will be propagated during sanity checking.
435 * Bits not in this mask are type specfic. */
436#define RTASN1_CHECK_SANITY_F_COMMON_MASK UINT32_C(0xffff0000)
437
438/**
439 * ASN.1 core vtable.
440 */
441typedef struct RTASN1COREVTABLE
442{
443 /** The name. */
444 const char *pszName;
445 /** Size of the structure. */
446 uint32_t cbStruct;
447 /** The default tag, UINT8_MAX if not applicable. */
448 uint8_t uDefaultTag;
449 /** The default class and flags. */
450 uint8_t fDefaultClass;
451 /** Reserved for later / alignment. */
452 uint16_t uReserved;
453 /** @copydoc FNRTASN1COREVTDTOR */
454 PFNRTASN1COREVTDTOR pfnDtor;
455 /** @copydoc FNRTASN1COREVTENUM */
456 PFNRTASN1COREVTENUM pfnEnum;
457 /** @copydoc FNRTASN1COREVTCLONE */
458 PFNRTASN1COREVTCLONE pfnClone;
459 /** @copydoc FNRTASN1COREVTCOMPARE */
460 PFNRTASN1COREVTCOMPARE pfnCompare;
461 /** @copydoc FNRTASN1COREVTCHECKSANITY */
462 PFNRTASN1COREVTCHECKSANITY pfnCheckSanity;
463 /** @copydoc FNRTASN1COREVTENCODEPREP */
464 PFNRTASN1COREVTENCODEPREP pfnEncodePrep;
465 /** @copydoc FNRTASN1COREVTENUM */
466 PFNRTASN1COREVTENCODEWRITE pfnEncodeWrite;
467} RTASN1COREVTABLE;
468/** Pointer to an ASN.1 allocator vtable. */
469typedef struct RTASN1COREVTABLE *PRTASN1COREVTABLE;
470/** Pointer to a const ASN.1 allocator vtable. */
471typedef RTASN1COREVTABLE const *PCRTASN1COREVTABLE;
472
473
474/** @name Helper macros for prototyping standard functions for an ASN.1 type.
475 * @{ */
476
477#define RTASN1TYPE_STANDARD_PROTOTYPES_NO_GET_CORE(a_TypeNm, a_DeclMacro, a_ImplExtNm) \
478 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_Init)(RT_CONCAT(P,a_TypeNm) pThis, PCRTASN1ALLOCATORVTABLE pAllocator); \
479 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_Clone)(RT_CONCAT(P,a_TypeNm) pThis, RT_CONCAT(PC,a_TypeNm) pSrc, \
480 PCRTASN1ALLOCATORVTABLE pAllocator); \
481 a_DeclMacro(void) RT_CONCAT(a_ImplExtNm,_Delete)(RT_CONCAT(P,a_TypeNm) pThis); \
482 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_Enum)(RT_CONCAT(P,a_TypeNm) pThis, PFNRTASN1ENUMCALLBACK pfnCallback, \
483 uint32_t uDepth, void *pvUser); \
484 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_Compare)(RT_CONCAT(PC,a_TypeNm) pLeft, RT_CONCAT(PC,a_TypeNm) pRight); \
485 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, RT_CONCAT(P,a_TypeNm) pThis,\
486 const char *pszErrorTag); \
487 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_CheckSanity)(RT_CONCAT(PC,a_TypeNm) pThis, uint32_t fFlags, \
488 PRTERRINFO pErrInfo, const char *pszErrorTag)
489
490
491#define RTASN1TYPE_STANDARD_PROTOTYPES(a_TypeNm, a_DeclMacro, a_ImplExtNm, a_Asn1CoreNm) \
492 DECL_FORCE_INLINE(PRTASN1CORE) RT_CONCAT(a_ImplExtNm,_GetAsn1Core)(RT_CONCAT(PC,a_TypeNm) pThis) \
493 { return (PRTASN1CORE)&pThis->a_Asn1CoreNm; } \
494 DECLINLINE(bool) RT_CONCAT(a_ImplExtNm,_IsPresent)(RT_CONCAT(PC,a_TypeNm) pThis) \
495 { return pThis && RTASN1CORE_IS_PRESENT(&pThis->a_Asn1CoreNm); } \
496 RTASN1TYPE_STANDARD_PROTOTYPES_NO_GET_CORE(a_TypeNm, a_DeclMacro, a_ImplExtNm)
497
498
499/** Aliases two ASN.1 types, no method aliases. */
500#define RTASN1TYPE_ALIAS_TYPE_ONLY(a_TypeNm, a_AliasType) \
501 typedef a_AliasType a_TypeNm; \
502 typedef a_TypeNm *RT_CONCAT(P,a_TypeNm); \
503 typedef a_TypeNm const *RT_CONCAT(PC,a_TypeNm)
504
505/** Aliases two ASN.1 types and methods. */
506#define RTASN1TYPE_ALIAS(a_TypeNm, a_AliasType, a_ImplExtNm, a_AliasExtNm) \
507 typedef a_AliasType a_TypeNm; \
508 typedef a_TypeNm *RT_CONCAT(P,a_TypeNm); \
509 \
510 DECLINLINE(PRTASN1CORE) RT_CONCAT(a_ImplExtNm,_GetAsn1Core)(a_TypeNm const *pThis) \
511 { return RT_CONCAT(a_AliasExtNm,_GetAsn1Core)(pThis); } \
512 DECLINLINE(bool) RT_CONCAT(a_ImplExtNm,_IsPresent)(a_TypeNm const *pThis) \
513 { return RT_CONCAT(a_AliasExtNm,_IsPresent)(pThis); } \
514 \
515 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_Init)(RT_CONCAT(P,a_TypeNm) pThis, PCRTASN1ALLOCATORVTABLE pAllocator) \
516 { return RT_CONCAT(a_AliasExtNm,_Init)(pThis, pAllocator); } \
517 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_Clone)(RT_CONCAT(P,a_TypeNm) pThis, a_TypeNm const *pSrc, \
518 PCRTASN1ALLOCATORVTABLE pAllocator) \
519 { return RT_CONCAT(a_AliasExtNm,_Clone)(pThis, pSrc, pAllocator); } \
520 DECLINLINE(void) RT_CONCAT(a_ImplExtNm,_Delete)(RT_CONCAT(P,a_TypeNm) pThis) \
521 { RT_CONCAT(a_AliasExtNm,_Delete)(pThis); } \
522 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_Enum)(a_TypeNm *pThis, PFNRTASN1ENUMCALLBACK pfnCallback, \
523 uint32_t uDepth, void *pvUser) \
524 { return RT_CONCAT(a_AliasExtNm,_Enum)(pThis, pfnCallback, uDepth, pvUser); } \
525 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_Compare)(a_TypeNm const *pLeft, a_TypeNm const *pRight) \
526 { return RT_CONCAT(a_AliasExtNm,_Compare)(pLeft, pRight); } \
527 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_DecodeAsn1)(PRTASN1CURSOR pCursor, uint32_t fFlags, RT_CONCAT(P,a_TypeNm) pThis,\
528 const char *pszErrorTag) \
529 { return RT_CONCAT(a_AliasExtNm,_DecodeAsn1)(pCursor, fFlags, pThis, pszErrorTag); } \
530 DECLINLINE(int) RT_CONCAT(a_ImplExtNm,_CheckSanity)(a_TypeNm const *pThis, uint32_t fFlags, \
531 PRTERRINFO pErrInfo, const char *pszErrorTag) \
532 { return RT_CONCAT(a_AliasExtNm,_CheckSanity)(pThis, fFlags, pErrInfo, pszErrorTag); } \
533 \
534 typedef a_TypeNm const *RT_CONCAT(PC,a_TypeNm)
535
536/** @} */
537
538
539/**
540 * Core ASN.1 structure for storing encoding details and data location.
541 *
542 * This is used as a 'parent' for all other decoded ASN.1 based structures.
543 */
544typedef struct RTASN1CORE
545{
546 /** The tag.
547 * @remarks 32-bit should be enough for everyone... We don't currently
548 * implement decoding tags larger than 30 anyway. :-) */
549 uint32_t uTag;
550 /** Tag class and flags (ASN1_TAGCLASS_XXX and ASN1_TAGFLAG_XXX). */
551 uint8_t fClass;
552 /** The real tag value for IMPLICT tag overrides. */
553 uint8_t uRealTag;
554 /** The real class value for IMPLICT tag overrides. */
555 uint8_t fRealClass;
556 /** The size of the tag and length ASN.1 header. */
557 uint8_t cbHdr;
558 /** Length. */
559 uint32_t cb;
560 /** IPRT flags (RTASN1CORE_F_XXX). */
561 uint32_t fFlags;
562 /** Pointer to the data.
563 * After decoding this generally points to the encoded data content. When
564 * preparting something for encoding or otherwise constructing things in memory,
565 * this generally points heap memory or read-only constants.
566 * @sa RTAsn1ContentAllocZ, RTAsn1ContentReallocZ, RTAsn1ContentDup,
567 * RTAsn1ContentFree. */
568 RTCPTRUNION uData;
569 /** Pointer to the virtual method table for this object. Optional. */
570 PCRTASN1COREVTABLE pOps;
571} RTASN1CORE;
572/** The Vtable for a RTASN1CORE structure when not in some way use used as a
573 * parent type/class. */
574extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1Core_Vtable;
575
576RTASN1TYPE_STANDARD_PROTOTYPES_NO_GET_CORE(RTASN1CORE, RTDECL, RTAsn1Core);
577
578/** @name RTASN1CORE_F_XXX - Flags for RTASN1CORE::fFlags
579 * @{ */
580/** Present/valid. */
581#define RTASN1CORE_F_PRESENT RT_BIT_32(0)
582/** Not present in stream, using default value. */
583#define RTASN1CORE_F_DEFAULT RT_BIT_32(1)
584/** The tag was overriden by an implict context tag or some such thing,
585 * RTASN1CORE::uImplicitTag hold the universal tag value if one exists. */
586#define RTASN1CORE_F_TAG_IMPLICIT RT_BIT_32(2)
587/** Primitive tag with the corresponding RTASN1XXX struct. */
588#define RTASN1CORE_F_PRIMITE_TAG_STRUCT RT_BIT_32(3)
589/** Dummy node typically used with choices, has children, not encoded, must be
590 * ignored. */
591#define RTASN1CORE_F_DUMMY RT_BIT_32(4)
592/** Allocated content (pointed to by uData).
593 * The content should is still be considered 104% read-only by anyone other
594 * than then type methods (pOps and associates). */
595#define RTASN1CORE_F_ALLOCATED_CONTENT RT_BIT_32(5)
596/** Decoded content (pointed to by uData).
597 * Mutually exclusive with RTASN1CORE_F_ALLOCATED_CONTENT. If neither is
598 * set, uData might be NULL or point to some shared static memory for
599 * frequently used values. */
600#define RTASN1CORE_F_DECODED_CONTENT RT_BIT_32(6)
601/** Indefinite length, still pending. */
602#define RTASN1CORE_F_INDEFINITE_LENGTH RT_BIT_32(7)
603/** @} */
604
605
606/** Checks whether an ASN.1 core object present in some way (default data,
607 * decoded data, ...). */
608#define RTASN1CORE_IS_PRESENT(a_pAsn1Core) ( RT_BOOL((a_pAsn1Core)->fFlags) )
609
610/** Checks whether an ASN.1 core object is a dummy object (and is present). */
611#define RTASN1CORE_IS_DUMMY(a_pAsn1Core) ( RT_BOOL((a_pAsn1Core)->fFlags & RTASN1CORE_F_DUMMY) )
612
613/**
614 * Calculates pointer to the raw ASN.1 record.
615 *
616 * ASSUMES that it's decoded content and that cbHdr and uData are both valid.
617 *
618 * @returns Byte pointer to the first tag byte.
619 * @param a_pAsn1Core The ASN.1 core.
620 */
621#define RTASN1CORE_GET_RAW_ASN1_PTR(a_pAsn1Core) ( (a_pAsn1Core)->uData.pu8 - (a_pAsn1Core)->cbHdr )
622
623/**
624 * Calculates the length of the raw ASN.1 record to go with the
625 * RTASN1CORE_GET_RAW_ASN1_PTR() result.
626 *
627 * ASSUMES that it's decoded content and that cbHdr and uData are both valid.
628 *
629 * @returns Size in bytes (uint32_t).
630 * @param a_pAsn1Core The ASN.1 core.
631 */
632#define RTASN1CORE_GET_RAW_ASN1_SIZE(a_pAsn1Core) ( (a_pAsn1Core)->cbHdr + (a_pAsn1Core)->cb )
633
634/**
635 * Retrievs the tag or implicit tag depending on the RTASN1CORE_F_TAG_IMPLICIT
636 * flag.
637 *
638 * @returns The ASN.1 tag of the object.
639 * @param a_pAsn1Core The ASN.1 core.
640 */
641#define RTASN1CORE_GET_TAG(a_pAsn1Core) ( !((a_pAsn1Core)->fFlags & RTASN1CORE_F_TAG_IMPLICIT) ? (a_pAsn1Core)->uTag : (a_pAsn1Core)->uRealTag )
642
643
644DECL_FORCE_INLINE(PRTASN1CORE) RTAsn1Core_GetAsn1Core(PCRTASN1CORE pThis)
645{
646 return (PRTASN1CORE)pThis;
647}
648
649
650DECL_FORCE_INLINE(bool) RTAsn1Core_IsPresent(PCRTASN1CORE pThis)
651{
652 return pThis && RTASN1CORE_IS_PRESENT(pThis);
653}
654
655
656RTDECL(int) RTAsn1Core_InitEx(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass, PCRTASN1COREVTABLE pOps, uint32_t fFlags);
657/**
658 * Initialize the ASN.1 core object representation to a default value.
659 *
660 * @returns VINF_SUCCESS
661 * @param pAsn1Core The ASN.1 core.
662 * @param uTag The tag number.
663 * @param fClass The tag class and flags.
664 */
665RTDECL(int) RTAsn1Core_InitDefault(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass);
666RTDECL(int) RTAsn1Core_CloneContent(PRTASN1CORE pThis, PCRTASN1CORE pSrc, PCRTASN1ALLOCATORVTABLE pAllocator);
667RTDECL(int) RTAsn1Core_CloneNoContent(PRTASN1CORE pThis, PCRTASN1CORE pSrc);
668RTDECL(int) RTAsn1Core_SetTagAndFlags(PRTASN1CORE pAsn1Core, uint32_t uTag, uint8_t fClass);
669RTDECL(int) RTAsn1Core_ChangeTag(PRTASN1CORE pAsn1Core, uint32_t uTag);
670RTDECL(void) RTAsn1Core_ResetImplict(PRTASN1CORE pThis);
671RTDECL(int) RTAsn1Core_CompareEx(PCRTASN1CORE pLeft, PCRTASN1CORE pRight, bool fIgnoreTagAndClass);
672
673
674/**
675 * Dummy ASN.1 object for use in choices and similar non-sequence structures.
676 *
677 * This allows hooking up destructors, enumerators and such, as well as not
678 * needing custom code for sequence-of / set-of collections.
679 */
680typedef struct RTASN1DUMMY
681{
682 /** Core ASN.1. */
683 RTASN1CORE Asn1Core;
684} RTASN1DUMMY;
685/** Pointer to a dummy record. */
686typedef RTASN1DUMMY *PRTASN1DUMMY;
687
688
689/**
690 * Initalizes a dummy ASN.1 object.
691 *
692 * @returns VINF_SUCCESS.
693 * @param pThis The dummy object.
694 */
695RTDECL(int) RTAsn1Dummy_InitEx(PRTASN1DUMMY pThis);
696
697/**
698 * Standard compliant initalizer.
699 *
700 * @returns VINF_SUCCESS.
701 * @param pThis The dummy object.
702 * @param pAllocator Ignored.
703 */
704DECLINLINE(int) RTAsn1Dummy_Init(PRTASN1DUMMY pThis, PCRTASN1ALLOCATORVTABLE pAllocator)
705{
706 NOREF(pAllocator);
707 return RTAsn1Dummy_InitEx(pThis);
708}
709
710
711/**
712 * ASN.1 sequence core (IPRT representation).
713 */
714typedef struct RTASN1SEQUENCECORE
715{
716 /** Core ASN.1 encoding details. */
717 RTASN1CORE Asn1Core;
718} RTASN1SEQUENCECORE;
719/** Pointer to an ASN.1 sequence core (IPRT representation). */
720typedef RTASN1SEQUENCECORE *PRTASN1SEQUENCECORE;
721/** Pointer to a const ASN.1 sequence core (IPRT representation). */
722typedef RTASN1SEQUENCECORE const *PCRTASN1SEQUENCECORE;
723
724RTDECL(int) RTAsn1SequenceCore_Init(PRTASN1SEQUENCECORE pSeqCore, PCRTASN1COREVTABLE pVtable);
725RTDECL(int) RTAsn1SequenceCore_Clone(PRTASN1SEQUENCECORE pSeqCore, PCRTASN1COREVTABLE pVtable, PCRTASN1SEQUENCECORE pSrc);
726
727/**
728 * ASN.1 sequence-of core (IPRT representation).
729 */
730#if 0
731typedef struct RTASN1SEQOFCORE
732{
733 /** Core ASN.1 encoding details. */
734 RTASN1CORE Asn1Core;
735} RTASN1SEQUENCECORE;
736/** Pointer to an ASN.1 sequence-of core (IPRT representation). */
737typedef RTASN1SEQUENCECORE *PRTASN1SEQUENCECORE;
738/** Pointer to a const ASN.1 sequence-of core (IPRT representation). */
739typedef RTASN1SEQUENCECORE const *PCRTASN1SEQUENCECORE;
740#else
741# define RTASN1SEQOFCORE RTASN1SEQUENCECORE
742# define PRTASN1SEQOFCORE PRTASN1SEQUENCECORE
743# define PCRTASN1SEQOFCORE PCRTASN1SEQUENCECORE
744#endif
745RTDECL(int) RTAsn1SeqOfCore_Init(PRTASN1SEQOFCORE pThis, PCRTASN1COREVTABLE pVtable);
746RTDECL(int) RTAsn1SeqOfCore_Clone(PRTASN1SEQOFCORE pThis, PCRTASN1COREVTABLE pVtable, PCRTASN1SEQOFCORE pSrc);
747
748
749/** Defines the typedefs and prototypes for a generic sequence-of/set-of type. */
750#define RTASN1_IMPL_GEN_SEQ_OR_SET_OF_TYPEDEFS_AND_PROTOS(a_CoreType, a_CoreMember, \
751 a_ThisType, a_ItemType, a_DeclMacro, a_ImplExtNm) \
752 typedef struct a_ThisType \
753 { \
754 /** Sequence/set core. */ \
755 a_CoreType a_CoreMember; \
756 /** The array allocation tracker. */ \
757 RTASN1ARRAYALLOCATION Allocation; \
758 /** Items in the array. */ \
759 uint32_t cItems; \
760 /** Array. */ \
761 RT_CONCAT(P,a_ItemType) *papItems; \
762 } a_ThisType; \
763 typedef a_ThisType *RT_CONCAT(P,a_ThisType); \
764 typedef a_ThisType const *RT_CONCAT(PC,a_ThisType); \
765 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_Erase)(RT_CONCAT(P,a_ThisType) pThis, uint32_t iPosition); \
766 a_DeclMacro(int) RT_CONCAT(a_ImplExtNm,_InsertEx)(RT_CONCAT(P,a_ThisType) pThis, uint32_t iPosition, \
767 RT_CONCAT(PC,a_ItemType) pToClone, \
768 PCRTASN1ALLOCATORVTABLE pAllocator, uint32_t *piActualPos); \
769 /** Appends entry with default content, returns index or negative error code. */ \
770 DECLINLINE(int32_t) RT_CONCAT(a_ImplExtNm,_Append)(RT_CONCAT(P,a_ThisType) pThis) \
771 { \
772 uint32_t uPos = pThis->cItems; \
773 int rc = RT_CONCAT(a_ImplExtNm,_InsertEx)(pThis, uPos, NULL /*pToClone*/, pThis->Allocation.pAllocator, &uPos); \
774 if (RT_SUCCESS(rc)) \
775 return (int32_t)uPos; \
776 return rc; \
777 } \
778 RTASN1TYPE_STANDARD_PROTOTYPES(a_ThisType, a_DeclMacro, a_ImplExtNm, a_CoreMember.Asn1Core)
779
780/** Defines the typedefs and prototypes for a generic sequence-of type. */
781#define RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(a_SeqOfType, a_ItemType, a_DeclMacro, a_ImplExtNm) \
782 RTASN1_IMPL_GEN_SEQ_OR_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQUENCECORE, SeqCore, a_SeqOfType, a_ItemType, a_DeclMacro, a_ImplExtNm)
783
784
785/**
786 * ASN.1 set core (IPRT representation).
787 */
788typedef struct RTASN1SETCORE
789{
790 /** Core ASN.1 encoding details. */
791 RTASN1CORE Asn1Core;
792} RTASN1SETCORE;
793/** Pointer to an ASN.1 set core (IPRT representation). */
794typedef RTASN1SETCORE *PRTASN1SETCORE;
795/** Pointer to a const ASN.1 set core (IPRT representation). */
796typedef RTASN1SETCORE const *PCRTASN1SETCORE;
797
798RTDECL(int) RTAsn1SetCore_Init(PRTASN1SETCORE pThis, PCRTASN1COREVTABLE pVtable);
799RTDECL(int) RTAsn1SetCore_Clone(PRTASN1SETCORE pThis, PCRTASN1COREVTABLE pVtable, PCRTASN1SETCORE pSrc);
800
801/**
802 * ASN.1 set-of core (IPRT representation).
803 */
804#if 0
805typedef struct RTASN1SETOFCORE
806{
807 /** Core ASN.1 encoding details. */
808 RTASN1CORE Asn1Core;
809} RTASN1SETUENCECORE;
810/** Pointer to an ASN.1 set-of core (IPRT representation). */
811typedef RTASN1SETUENCECORE *PRTASN1SETUENCECORE;
812/** Pointer to a const ASN.1 set-of core (IPRT representation). */
813typedef RTASN1SETUENCECORE const *PCRTASN1SETUENCECORE;
814#else
815# define RTASN1SETOFCORE RTASN1SETCORE
816# define PRTASN1SETOFCORE PRTASN1SETCORE
817# define PCRTASN1SETOFCORE PCRTASN1SETCORE
818#endif
819RTDECL(int) RTAsn1SetOfCore_Init(PRTASN1SETOFCORE pThis, PCRTASN1COREVTABLE pVtable);
820RTDECL(int) RTAsn1SetOfCore_Clone(PRTASN1SETOFCORE pThis, PCRTASN1COREVTABLE pVtable, PCRTASN1SETOFCORE pSrc);
821
822
823/** Defines the typedefs and prototypes for a generic set-of type. */
824#define RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(a_SetOfType, a_ItemType, a_DeclMacro, a_ImplExtNm) \
825 RTASN1_IMPL_GEN_SEQ_OR_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETCORE, SetCore, a_SetOfType, a_ItemType, a_DeclMacro, a_ImplExtNm)
826
827
828/*
829 * Declare sets and sequences of the core structure.
830 */
831RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFCORES, RTASN1CORE, RTDECL, RTAsn1SeqOfCores);
832RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFCORES, RTASN1CORE, RTDECL, RTAsn1SetOfCores);
833
834
835/**
836 * ASN.1 null (IPRT representation).
837 */
838typedef struct RTASN1NULL
839{
840 /** Core ASN.1 encoding details. */
841 RTASN1CORE Asn1Core;
842} RTASN1NULL;
843/** Pointer to an ASN.1 null (IPRT representation). */
844typedef RTASN1NULL *PRTASN1NULL;
845/** Pointer to a const ASN.1 null (IPRT representation). */
846typedef RTASN1NULL const *PCRTASN1NULL;
847/** The Vtable for a RTASN1NULL structure. */
848extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1Null_Vtable;
849
850RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1NULL, RTDECL, RTAsn1Null, Asn1Core);
851
852
853/**
854 * ASN.1 integer (IPRT representation).
855 */
856typedef struct RTASN1INTEGER
857{
858 /** Core ASN.1 encoding details. */
859 RTASN1CORE Asn1Core;
860 /** The unsigned C representation of the 64 least significant bits.
861 * @note A ASN.1 integer doesn't define signed/unsigned and can have any
862 * length you like. Thus, the user needs to check the size and
863 * preferably use the access APIs for signed numbers. */
864 RTUINT64U uValue;
865} RTASN1INTEGER;
866/** Pointer to an ASN.1 integer (IPRT representation). */
867typedef RTASN1INTEGER *PRTASN1INTEGER;
868/** Pointer to a const ASN.1 integer (IPRT representation). */
869typedef RTASN1INTEGER const *PCRTASN1INTEGER;
870/** The Vtable for a RTASN1INTEGER structure. */
871extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1Integer_Vtable;
872
873RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1INTEGER, RTDECL, RTAsn1Integer, Asn1Core);
874
875/**
876 * Initializes an interger object to a default value.
877 * @returns VINF_SUCCESS.
878 * @param pInteger The integer object representation.
879 * @param uValue The default value (unsigned 64-bit).
880 * @param pAllocator The allocator (pro forma).
881 */
882RTDECL(int) RTAsn1Integer_InitDefault(PRTASN1INTEGER pInteger, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator);
883
884RTDECL(int) RTAsn1Integer_InitU64(PRTASN1INTEGER pThis, uint64_t uValue, PCRTASN1ALLOCATORVTABLE pAllocator);
885
886/**
887 * Get the most significat bit that's set (1).
888 *
889 * @returns 0-base bit number, -1 if all clear.
890 * @param pInteger The integer to check.
891 */
892RTDECL(int32_t) RTAsn1Integer_UnsignedLastBit(PCRTASN1INTEGER pInteger);
893
894/**
895 * Compares two ASN.1 unsigned integers.
896 *
897 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
898 * @param pLeft The first ASN.1 integer.
899 * @param pRight The second ASN.1 integer.
900 */
901RTDECL(int) RTAsn1Integer_UnsignedCompare(PCRTASN1INTEGER pLeft, PCRTASN1INTEGER pRight);
902
903/**
904 * Compares an ASN.1 unsigned integer with a uint64_t.
905 *
906 * @returns 0 if equal, -1 if @a pInteger is smaller, 1 if @a pInteger is
907 * larger.
908 * @param pInteger The ASN.1 integer to treat as unsigned.
909 * @param u64Const The uint64_t constant to compare with.
910 */
911RTDECL(int) RTAsn1Integer_UnsignedCompareWithU64(PCRTASN1INTEGER pInteger, uint64_t u64Const);
912
913/**
914 * Compares an ASN.1 unsigned integer with a uint32_t.
915 *
916 * @returns 0 if equal, -1 if @a pInteger is smaller, 1 if @a pInteger is
917 * larger.
918 * @param pInteger The ASN.1 integer to treat as unsigned.
919 * @param u32Const The uint32_t constant to compare with.
920 * @remarks We don't bother with U16 and U8 variants, just use this instead.
921 */
922RTDECL(int) RTAsn1Integer_UnsignedCompareWithU32(PCRTASN1INTEGER pInteger, uint32_t u32Const);
923
924
925/**
926 * Initializes a big integer number from an ASN.1 integer.
927 *
928 * @returns IPRT status code.
929 * @param pInteger The ASN.1 integer.
930 * @param pBigNum The big integer number structure to initialize.
931 * @param fBigNumInit Subset of RTBIGNUMINIT_F_XXX that concerns
932 * senitivity, signedness and endianness.
933 */
934RTDECL(int) RTAsn1Integer_ToBigNum(PCRTASN1INTEGER pInteger, PRTBIGNUM pBigNum, uint32_t fBigNumInit);
935RTDECL(int) RTAsn1Integer_FromBigNum(PRTASN1INTEGER pThis, PCRTBIGNUM pBigNum, PCRTASN1ALLOCATORVTABLE pAllocator);
936
937/**
938 * Converts the integer to a string.
939 *
940 * This will produce a hex represenation of the number. If it fits in 64-bit, a
941 * C style hex number will be produced. If larger than 64-bit, it will be
942 * printed as a space separated string of hex bytes.
943 *
944 * @returns IPRT status code.
945 * @param pThis The ASN.1 integer.
946 * @param pszBuf The output buffer.
947 * @param cbBuf The buffer size.
948 * @param fFlags Flags reserved for future exploits. MBZ.
949 * @param pcbActual Where to return the amount of buffer space used
950 * (i.e. including terminator). Optional.
951 *
952 * @remarks Currently assume unsigned number.
953 */
954RTDECL(int) RTAsn1Integer_ToString(PCRTASN1INTEGER pThis, char *pszBuf, size_t cbBuf, uint32_t fFlags, size_t *pcbActual);
955
956RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFINTEGERS, RTASN1INTEGER, RTDECL, RTAsn1SeqOfIntegers);
957RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFINTEGERS, RTASN1INTEGER, RTDECL, RTAsn1SetOfIntegers);
958
959
960
961/**
962 * ASN.1 boolean (IPRT representation).
963 */
964typedef struct RTASN1BOOLEAN
965{
966 /** Core ASN.1 encoding details. */
967 RTASN1CORE Asn1Core;
968 /** The boolean value. */
969 bool fValue;
970} RTASN1BOOLEAN;
971/** Pointer to the IPRT representation of an ASN.1 boolean. */
972typedef RTASN1BOOLEAN *PRTASN1BOOLEAN;
973/** Pointer to the const IPRT representation of an ASN.1 boolean. */
974typedef RTASN1BOOLEAN const *PCRTASN1BOOLEAN;
975/** The Vtable for a RTASN1BOOLEAN structure. */
976extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1Boolean_Vtable;
977
978RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1BOOLEAN, RTDECL, RTAsn1Boolean, Asn1Core);
979
980/**
981 * Initializes a boolean object to a default value.
982 * @returns VINF_SUCCESS
983 * @param pBoolean The boolean object representation.
984 * @param fValue The default value.
985 * @param pAllocator The allocator (pro forma).
986 */
987RTDECL(int) RTAsn1Boolean_InitDefault(PRTASN1BOOLEAN pBoolean, bool fValue, PCRTASN1ALLOCATORVTABLE pAllocator);
988RTDECL(int) RTAsn1Boolean_Set(PRTASN1BOOLEAN pThis, bool fValue);
989
990RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFBOOLEANS, RTASN1BOOLEAN, RTDECL, RTAsn1SeqOfBooleans);
991RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFBOOLEANS, RTASN1BOOLEAN, RTDECL, RTAsn1SetOfBooleans);
992
993
994
995/**
996 * ASN.1 UTC and Generalized Time (IPRT representation).
997 *
998 * The two time types only differs in the precision the render (UTC time being
999 * the one for which you go "WTF were they thinking?!!" for in 2014).
1000 */
1001typedef struct RTASN1TIME
1002{
1003 /** The core structure, either ASN1_TAG_UTC_TIME or
1004 * ASN1_TAG_GENERALIZED_TIME. */
1005 RTASN1CORE Asn1Core;
1006 /** The exploded time. */
1007 RTTIME Time;
1008} RTASN1TIME;
1009/** Pointer to an IPRT representation of ASN.1 UTC/Generalized time. */
1010typedef RTASN1TIME *PRTASN1TIME;
1011/** Pointer to a const IPRT representation of ASN.1 UTC/Generalized time. */
1012typedef RTASN1TIME const *PCRTASN1TIME;
1013/** The Vtable for a RTASN1TIME structure. */
1014extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1Time_Vtable;
1015
1016RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1TIME, RTDECL, RTAsn1Time, Asn1Core);
1017
1018RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1TIME, RTDECL, RTAsn1UtcTime, Asn1Core);
1019RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1TIME, RTDECL, RTAsn1GeneralizedTime, Asn1Core);
1020
1021/**
1022 * Compares two ASN.1 time values.
1023 *
1024 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
1025 * @param pLeft The first ASN.1 time object.
1026 * @param pTsRight The second time to compare.
1027 */
1028RTDECL(int) RTAsn1Time_CompareWithTimeSpec(PCRTASN1TIME pLeft, PCRTTIMESPEC pTsRight);
1029
1030/**
1031 * Extended init function that lets you select the kind of time object (UTC or
1032 * generalized).
1033 */
1034RTDECL(int) RTAsn1Time_InitEx(PRTASN1TIME pThis, uint32_t uTag, PCRTASN1ALLOCATORVTABLE pAllocator);
1035
1036/**
1037 * Combines RTAsn1Time_InitEx() and RTAsn1Time_SetTime().
1038 */
1039RTDECL(int) RTAsn1Time_InitWithTime(PRTASN1TIME pThis, uint32_t uTag, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIME pTime);
1040
1041/**
1042 * Sets the ASN.1 time value to @a pTime.
1043 *
1044 * @returns IPRT status code.
1045 * @param pThis The ASN.1 time object to modify.
1046 * @param pAllocator The allocator to use.
1047 * @param pTime The time to set.
1048 */
1049RTDECL(int) RTAsn1Time_SetTime(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIME pTime);
1050
1051/**
1052 * Sets the ASN.1 time value to @a pTimeSpec.
1053 *
1054 * @returns IPRT status code.
1055 * @param pThis The ASN.1 time object to modify.
1056 * @param pAllocator The allocator to use.
1057 * @param pTimeSpec The time to set.
1058 */
1059RTDECL(int) RTAsn1Time_SetTimeSpec(PRTASN1TIME pThis, PCRTASN1ALLOCATORVTABLE pAllocator, PCRTTIMESPEC pTimeSpec);
1060
1061/** @name Predicate macros for determing the exact type of RTASN1TIME.
1062 * @{ */
1063/** True if UTC time. */
1064#define RTASN1TIME_IS_UTC_TIME(a_pAsn1Time) ((a_pAsn1Time)->Asn1Core.uTag == ASN1_TAG_UTC_TIME)
1065/** True if generalized time. */
1066#define RTASN1TIME_IS_GENERALIZED_TIME(a_pAsn1Time) ((a_pAsn1Time)->Asn1Core.uTag == ASN1_TAG_GENERALIZED_TIME)
1067/** @} */
1068
1069RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFTIMES, RTASN1TIME, RTDECL, RTAsn1SeqOfTimes);
1070RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFTIMES, RTASN1TIME, RTDECL, RTAsn1SetOfTimes);
1071
1072
1073
1074/**
1075 * ASN.1 object identifier (IPRT representation).
1076 */
1077typedef struct RTASN1OBJID
1078{
1079 /** Core ASN.1 encoding details. */
1080 RTASN1CORE Asn1Core;
1081 /** Coverning the paComponents memory allocation if there isn't enough room in
1082 * szObjId for both the dottet string and the component values. */
1083 RTASN1ALLOCATION Allocation;
1084 /** Pointer to an array with the component values.
1085 * This may point within szObjId if there is enough space for both there. */
1086 uint32_t const *pauComponents;
1087 /** The number of components in the object identifier.
1088 * This ASSUMES that nobody will be ever needing more than 255 components. */
1089 uint8_t cComponents;
1090 /** The dotted string representation of the object identifier.
1091 * If there is sufficient space after the string, we will place the array that
1092 * paComponents points to here and/or the raw content bytes (Asn1Core.uData).
1093 *
1094 * An analysis of dumpasn1.cfg, hl7.org and our own _OID defines indicates
1095 * that we need space for at least 10 components and 30-something chars. We've
1096 * allocated 87 bytes, which we ASSUME should be enough for everyone. */
1097 char szObjId[87];
1098} RTASN1OBJID;
1099/** Pointer to an ASN.1 object identifier representation. */
1100typedef RTASN1OBJID *PRTASN1OBJID;
1101/** Pointer to a const ASN.1 object identifier representation. */
1102typedef RTASN1OBJID const *PCRTASN1OBJID;
1103/** The Vtable for a RTASN1OBJID structure. */
1104extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1ObjId_Vtable;
1105
1106RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1OBJID, RTDECL, RTAsn1ObjId, Asn1Core);
1107
1108RTDECL(int) RTAsn1ObjId_InitFromString(PRTASN1OBJID pThis, const char *pszObjId, PCRTASN1ALLOCATORVTABLE pAllocator);
1109RTDECL(int) RTAsn1ObjId_SetFromString(PRTASN1OBJID pThis, const char *pszObjId, PCRTASN1ALLOCATORVTABLE pAllocator);
1110
1111/**
1112 * Compares an ASN.1 object identifier with a dotted object identifier string.
1113 *
1114 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
1115 * @param pThis The ASN.1 object identifier.
1116 * @param pszRight The dotted object identifier string.
1117 */
1118RTDECL(int) RTAsn1ObjId_CompareWithString(PCRTASN1OBJID pThis, const char *pszRight);
1119
1120/**
1121 * Checks if an ASN.1 object identifier starts with the given dotted object
1122 * identifier string.
1123 *
1124 * The matching is only successful if the given string matches matches the last
1125 * component completely.
1126 *
1127 * @returns true / false.
1128 * @param pThis The ASN.1 object identifier.
1129 * @param pszStartsWith The dotted object identifier string.
1130 */
1131RTDECL(bool) RTAsn1ObjId_StartsWith(PCRTASN1OBJID pThis, const char *pszStartsWith);
1132
1133RTDECL(uint8_t) RTAsn1ObjIdCountComponents(PCRTASN1OBJID pThis);
1134RTDECL(uint32_t) RTAsn1ObjIdGetComponentsAsUInt32(PCRTASN1OBJID pThis, uint8_t iComponent);
1135RTDECL(uint32_t) RTAsn1ObjIdGetLastComponentsAsUInt32(PCRTASN1OBJID pThis);
1136
1137RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFOBJIDS, RTASN1OBJID, RTDECL, RTAsn1SeqOfObjIds);
1138RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFOBJIDS, RTASN1OBJID, RTDECL, RTAsn1SetOfObjIds);
1139RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFOBJIDSEQS, RTASN1SEQOFOBJIDS, RTDECL, RTAsn1SetOfObjIdSeqs);
1140
1141
1142/**
1143 * ASN.1 bit string (IPRT representation).
1144 */
1145typedef struct RTASN1BITSTRING
1146{
1147 /** Core ASN.1 encoding details. */
1148 RTASN1CORE Asn1Core;
1149 /** The number of bits. */
1150 uint32_t cBits;
1151 /** The max number of bits (given at decoding / construction). */
1152 uint32_t cMaxBits;
1153 /** Pointer to the bits. */
1154 RTCPTRUNION uBits;
1155 /** Pointer to user structure encapsulated in this string, if dynamically
1156 * allocated the EncapsulatedAllocation member can be used to track it and
1157 * trigger automatic cleanup on object destruction. If EncapsulatedAllocation
1158 * is zero, any object pointed to will only be deleted. */
1159 PRTASN1CORE pEncapsulated;
1160 /** Allocation tracking structure for pEncapsulated. */
1161 RTASN1ALLOCATION EncapsulatedAllocation;
1162} RTASN1BITSTRING;
1163/** Pointer to the IPRT representation of an ASN.1 bit string. */
1164typedef RTASN1BITSTRING *PRTASN1BITSTRING;
1165/** Pointer to the const IPRT representation of an ASN.1 bit string. */
1166typedef RTASN1BITSTRING const *PCRTASN1BITSTRING;
1167/** The Vtable for a RTASN1BITSTRING structure. */
1168extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1BitString_Vtable;
1169
1170RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1BITSTRING, RTDECL, RTAsn1BitString, Asn1Core);
1171
1172/**
1173 * Calculates pointer to the first bit.
1174 *
1175 * @returns Byte pointer to the first bit.
1176 * @param a_pBitString The ASN.1 bit string.
1177 */
1178#define RTASN1BITSTRING_GET_BIT0_PTR(a_pBitString) ( &(a_pBitString)->Asn1Core.uData.pu8[1] )
1179
1180/**
1181 * Calculates the size in bytes.
1182 *
1183 * @returns Rounded up size in bytes.
1184 * @param a_pBitString The ASN.1 bit string.
1185 */
1186#define RTASN1BITSTRING_GET_BYTE_SIZE(a_pBitString) ( ((a_pBitString)->cBits + 7U) >> 3 )
1187
1188RTDECL(int) RTAsn1BitString_InitWithData(PRTASN1BITSTRING pThis, void const *pvSrc, uint32_t cSrcBits,
1189 PCRTASN1ALLOCATORVTABLE pAllocator);
1190RTDECL(int) RTAsn1BitString_DecodeAsn1Ex(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pThis,
1191 const char *pszErrorTag);
1192RTDECL(uint64_t) RTAsn1BitString_GetAsUInt64(PCRTASN1BITSTRING pThis);
1193RTDECL(int) RTAsn1BitString_RefreshContent(PRTASN1BITSTRING pThis, uint32_t fFlags,
1194 PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo);
1195RTDECL(bool) RTAsn1BitString_AreContentBitsValid(PCRTASN1BITSTRING pThis, uint32_t fFlags);
1196
1197RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFBITSTRINGS, RTASN1BITSTRING, RTDECL, RTAsn1SeqOfBitStrings);
1198RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFBITSTRINGS, RTASN1BITSTRING, RTDECL, RTAsn1SetOfBitStrings);
1199
1200
1201/**
1202 * ASN.1 octet string (IPRT representation).
1203 */
1204typedef struct RTASN1OCTETSTRING
1205{
1206 /** Core ASN.1 encoding details. */
1207 RTASN1CORE Asn1Core;
1208 /** Pointer to user structure encapsulated in this string.
1209 *
1210 * If dynamically allocated the EncapsulatedAllocation member can be used to
1211 * track it and trigger automatic cleanup on object destruction. If
1212 * EncapsulatedAllocation is zero, any object pointed to will only be
1213 * deleted. */
1214 PRTASN1CORE pEncapsulated;
1215 /** Allocation tracking structure for pEncapsulated. */
1216 RTASN1ALLOCATION EncapsulatedAllocation;
1217} RTASN1OCTETSTRING;
1218/** Pointer to the IPRT representation of an ASN.1 octet string. */
1219typedef RTASN1OCTETSTRING *PRTASN1OCTETSTRING;
1220/** Pointer to the const IPRT representation of an ASN.1 octet string. */
1221typedef RTASN1OCTETSTRING const *PCRTASN1OCTETSTRING;
1222/** The Vtable for a RTASN1OCTETSTRING structure. */
1223extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1OctetString_Vtable;
1224
1225RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1OCTETSTRING, RTDECL, RTAsn1OctetString, Asn1Core);
1226
1227RTDECL(int) RTAsn1OctetString_AllocContent(PRTASN1OCTETSTRING pThis, void const *pvSrc, size_t cb,
1228 PCRTASN1ALLOCATORVTABLE pAllocator);
1229RTDECL(int) RTAsn1OctetString_SetContent(PRTASN1OCTETSTRING pThis, void const *pvSrc, size_t cbSrc,
1230 PCRTASN1ALLOCATORVTABLE pAllocator);
1231RTDECL(bool) RTAsn1OctetString_AreContentBytesValid(PCRTASN1OCTETSTRING pThis, uint32_t fFlags);
1232RTDECL(int) RTAsn1OctetString_RefreshContent(PRTASN1OCTETSTRING pThis, uint32_t fFlags,
1233 PCRTASN1ALLOCATORVTABLE pAllocator, PRTERRINFO pErrInfo);
1234
1235RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFOCTETSTRINGS, RTASN1OCTETSTRING, RTDECL, RTAsn1SeqOfOctetStrings);
1236RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFOCTETSTRINGS, RTASN1OCTETSTRING, RTDECL, RTAsn1SetOfOctetStrings);
1237
1238
1239/**
1240 * ASN.1 string (IPRT representation).
1241 * All char string types except 'character string (29)'.
1242 */
1243typedef struct RTASN1STRING
1244{
1245 /** Core ASN.1 encoding details. */
1246 RTASN1CORE Asn1Core;
1247 /** Allocation tracking for pszUtf8. */
1248 RTASN1ALLOCATION Allocation;
1249 /** If conversion to UTF-8 was requested, we cache that here. */
1250 char const *pszUtf8;
1251 /** The length (chars, not code points) of the above UTF-8 string if
1252 * present. */
1253 uint32_t cchUtf8;
1254} RTASN1STRING;
1255/** Pointer to the IPRT representation of an ASN.1 string. */
1256typedef RTASN1STRING *PRTASN1STRING;
1257/** Pointer to the const IPRT representation of an ASN.1 string. */
1258typedef RTASN1STRING const *PCRTASN1STRING;
1259/** The Vtable for a RTASN1STRING structure. */
1260extern RTDATADECL(RTASN1COREVTABLE const) g_RTAsn1String_Vtable;
1261
1262RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1String, Asn1Core);
1263
1264/** @name String type predicate macros.
1265 * @{ */
1266#define RTASN1STRING_IS_NUMERIC(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_NUMERIC_STRING )
1267#define RTASN1STRING_IS_PRINTABLE(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_PRINTABLE_STRING )
1268#define RTASN1STRING_IS_T61(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_T61_STRING )
1269#define RTASN1STRING_IS_VIDEOTEX(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_VIDEOTEX_STRING )
1270#define RTASN1STRING_IS_VISIBLE(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_VISIBLE_STRING )
1271#define RTASN1STRING_IS_IA5(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_IA5_STRING )
1272#define RTASN1STRING_IS_GRAPHIC(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_GRAPHIC_STRING )
1273#define RTASN1STRING_IS_GENERAL(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_GENERAL_STRING )
1274/** UTF-8. */
1275#define RTASN1STRING_IS_UTF8(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_UTF8_STRING )
1276/** UCS-2. */
1277#define RTASN1STRING_IS_BMP(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_BMP_STRING )
1278/** UCS-4. */
1279#define RTASN1STRING_IS_UNIVERSAL(a_pAsn1String) ( RTASN1CORE_GET_TAG(&(a_pAsn1String)->Asn1Core) == ASN1_TAG_UNIVERSAL_STRING )
1280/** @} */
1281
1282RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1NumericString, Asn1Core);
1283RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1PrintableString, Asn1Core);
1284RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1T61String, Asn1Core);
1285RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1VideoTexString, Asn1Core);
1286RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1VisibleString, Asn1Core);
1287RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1Ia5String, Asn1Core);
1288RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1GraphicString, Asn1Core);
1289RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1GeneralString, Asn1Core);
1290RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1Utf8String, Asn1Core);
1291RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1BmpString, Asn1Core);
1292RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1STRING, RTDECL, RTAsn1UniversalString, Asn1Core);
1293
1294RTDECL(int) RTAsn1String_InitWithValue(PRTASN1STRING pThis, const char *pszUtf8Value, PCRTASN1ALLOCATORVTABLE pAllocator);
1295RTDECL(int) RTAsn1String_InitEx(PRTASN1STRING pThis, uint32_t uTag, void const *pvValue, size_t cbValue,
1296 PCRTASN1ALLOCATORVTABLE pAllocator);
1297
1298/**
1299 * Compares two strings values, extended version.
1300 *
1301 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
1302 * @param pLeft The first string.
1303 * @param pRight The second string.
1304 * @param fTypeToo Set if the string types must match, false if
1305 * not.
1306 */
1307RTDECL(int) RTAsn1String_CompareEx(PCRTASN1STRING pLeft, PCRTASN1STRING pRight, bool fTypeToo);
1308RTDECL(int) RTAsn1String_CompareValues(PCRTASN1STRING pLeft, PCRTASN1STRING pRight);
1309
1310/**
1311 * Compares a ASN.1 string object with an UTF-8 string.
1312 *
1313 * @returns 0 if equal, -1 if @a pThis is smaller, 1 if @a pThis is larger.
1314 * @param pThis The ASN.1 string object.
1315 * @param pszString The UTF-8 string.
1316 * @param cchString The length of @a pszString, or RTSTR_MAX.
1317 */
1318RTDECL(int) RTAsn1String_CompareWithString(PCRTASN1STRING pThis, const char *pszString, size_t cchString);
1319
1320/**
1321 * Queries the UTF-8 length of an ASN.1 string object.
1322 *
1323 * This differs from RTAsn1String_QueryUtf8 in that it won't need to allocate
1324 * memory for the converted string, but just calculates the length.
1325 *
1326 * @returns IPRT status code.
1327 * @param pThis The ASN.1 string object.
1328 * @param pcch Where to return the string length.
1329 */
1330RTDECL(int) RTAsn1String_QueryUtf8Len(PCRTASN1STRING pThis, size_t *pcch);
1331
1332/**
1333 * Queries the UTF-8 string for an ASN.1 string object.
1334 *
1335 * This may fail as it may require memory to be allocated for storing the
1336 * string.
1337 *
1338 * @returns IPRT status code.
1339 * @param pString The ASN.1 string object. This is a const
1340 * parameter for making life easier on the caller,
1341 * however be aware that the object may be modified
1342 * by this call!
1343 * @param ppsz Where to return the pointer to the UTF-8 string.
1344 * Optional.
1345 * @param pcch Where to return the length (in 8-bit chars) to
1346 * of the UTF-8 string. Optional.
1347 */
1348RTDECL(int) RTAsn1String_QueryUtf8(PCRTASN1STRING pString, const char **ppsz, size_t *pcch);
1349RTDECL(int) RTAsn1String_RecodeAsUtf8(PRTASN1STRING pThis, PCRTASN1ALLOCATORVTABLE pAllocator);
1350
1351RTASN1_IMPL_GEN_SEQ_OF_TYPEDEFS_AND_PROTOS(RTASN1SEQOFSTRINGS, RTASN1STRING, RTDECL, RTAsn1SeqOfStrings);
1352RTASN1_IMPL_GEN_SET_OF_TYPEDEFS_AND_PROTOS(RTASN1SETOFSTRINGS, RTASN1STRING, RTDECL, RTAsn1SetOfStrings);
1353
1354
1355
1356/**
1357 * ASN.1 generic context specific tag (IPRT representation).
1358 *
1359 * Normally used to tag something that's optional, version specific or such.
1360 *
1361 * For the purpose of documenting the format with typedefs as well as possibly
1362 * making it a little more type safe, there's a set of typedefs for the most
1363 * commonly used tag values defined. These typedefs have are identical to
1364 * RTASN1CONTEXTTAG, except from the C++ type system point of view.
1365 */
1366typedef struct RTASN1CONTEXTTAG
1367{
1368 /** Core ASN.1 encoding details. */
1369 RTASN1CORE Asn1Core;
1370} RTASN1CONTEXTTAG;
1371/** Pointer to an ASN.1 context tag (IPRT thing). */
1372typedef RTASN1CONTEXTTAG *PRTASN1CONTEXTTAG;
1373/** Pointer to a const ASN.1 context tag (IPRT thing). */
1374typedef RTASN1CONTEXTTAG const *PCRTASN1CONTEXTTAG;
1375
1376RTDECL(int) RTAsn1ContextTagN_Init(PRTASN1CONTEXTTAG pThis, uint32_t uTag, PCRTASN1COREVTABLE pVtable);
1377RTDECL(int) RTAsn1ContextTagN_Clone(PRTASN1CONTEXTTAG pThis, PCRTASN1CONTEXTTAG pSrc, uint32_t uTag);
1378
1379
1380/** @internal */
1381#define RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(a_uTag) \
1382 typedef struct RT_CONCAT(RTASN1CONTEXTTAG,a_uTag) { RTASN1CORE Asn1Core; } RT_CONCAT(RTASN1CONTEXTTAG,a_uTag); \
1383 typedef RT_CONCAT(RTASN1CONTEXTTAG,a_uTag) *RT_CONCAT(PRTASN1CONTEXTTAG,a_uTag); \
1384 DECLINLINE(int) RT_CONCAT3(RTAsn1ContextTag,a_uTag,_Init)(RT_CONCAT(PRTASN1CONTEXTTAG,a_uTag) pThis, \
1385 PCRTASN1COREVTABLE pVtable, PCRTASN1ALLOCATORVTABLE pAllocator) \
1386 { \
1387 NOREF(pAllocator); \
1388 return RTAsn1ContextTagN_Init((PRTASN1CONTEXTTAG)pThis, a_uTag, pVtable); \
1389 } \
1390 DECLINLINE(int) RT_CONCAT3(RTAsn1ContextTag,a_uTag,_Clone)(RT_CONCAT(PRTASN1CONTEXTTAG,a_uTag) pThis, \
1391 RT_CONCAT(RTASN1CONTEXTTAG,a_uTag) const *pSrc) \
1392 { return RTAsn1ContextTagN_Clone((PRTASN1CONTEXTTAG)pThis, (PCRTASN1CONTEXTTAG)pSrc, a_uTag); } \
1393 typedef RT_CONCAT(RTASN1CONTEXTTAG,a_uTag) const *RT_CONCAT(PCRTASN1CONTEXTTAG,a_uTag)
1394RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(0);
1395RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(1);
1396RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(2);
1397RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(3);
1398RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(4);
1399RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(5);
1400RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(6);
1401RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE(7);
1402#undef RTASN1CONTEXTTAG_DO_TYPEDEF_AND_INLINE
1403
1404/** Helper for comparing optional context tags.
1405 * This will return if both are not present or if their precense differs.
1406 * @internal */
1407#define RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, a_uTag) \
1408 do { \
1409 /* type checks */ \
1410 RT_CONCAT(PCRTASN1CONTEXTTAG,a_uTag) const pMyLeftInternal = (a_pLeft); \
1411 RT_CONCAT(PCRTASN1CONTEXTTAG,a_uTag) const pMyRightInternal = (a_pRight); \
1412 (a_iDiff) = (int)RTASN1CORE_IS_PRESENT(&pMyLeftInternal->Asn1Core) \
1413 - (int)RTASN1CORE_IS_PRESENT(&pMyRightInternal->Asn1Core); \
1414 if ((a_iDiff) || !RTASN1CORE_IS_PRESENT(&pMyLeftInternal->Asn1Core)) return iDiff; \
1415 } while (0)
1416
1417/** @name Helpers for comparing optional context tags.
1418 * This will return if both are not present or if their precense differs.
1419 * @{ */
1420#define RTASN1CONTEXTTAG0_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 0)
1421#define RTASN1CONTEXTTAG1_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 1)
1422#define RTASN1CONTEXTTAG2_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 2)
1423#define RTASN1CONTEXTTAG3_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 3)
1424#define RTASN1CONTEXTTAG4_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 4)
1425#define RTASN1CONTEXTTAG5_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 5)
1426#define RTASN1CONTEXTTAG6_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 6)
1427#define RTASN1CONTEXTTAG7_COMPARE_PRESENT_RETURN(a_iDiff, a_pLeft, a_pRight) RTASN1CONTEXTTAG_COMPARE_PRESENT_RETURN_INTERNAL(a_iDiff, a_pLeft, a_pRight, 7)
1428/** @} */
1429
1430
1431/**
1432 * Type information for dynamically bits (see RTASN1DYNTYPE).
1433 */
1434typedef enum RTASN1TYPE
1435{
1436 /** Not present. */
1437 RTASN1TYPE_NOT_PRESENT = 0,
1438 /** Generic ASN.1 for unknown tag/class. */
1439 RTASN1TYPE_CORE,
1440 /** ASN.1 NULL. */
1441 RTASN1TYPE_NULL,
1442 /** ASN.1 integer. */
1443 RTASN1TYPE_INTEGER,
1444 /** ASN.1 boolean. */
1445 RTASN1TYPE_BOOLEAN,
1446 /** ASN.1 character string. */
1447 RTASN1TYPE_STRING,
1448 /** ASN.1 octet string. */
1449 RTASN1TYPE_OCTET_STRING,
1450 /** ASN.1 bite string. */
1451 RTASN1TYPE_BIT_STRING,
1452 /** ASN.1 UTC or Generalize time. */
1453 RTASN1TYPE_TIME,
1454#if 0
1455 /** ASN.1 sequence core. */
1456 RTASN1TYPE_SEQUENCE_CORE,
1457 /** ASN.1 set core. */
1458 RTASN1TYPE_SET_CORE,
1459#endif
1460 /** ASN.1 object identifier. */
1461 RTASN1TYPE_OBJID,
1462 /** End of valid types. */
1463 RTASN1TYPE_END,
1464 /** Type size hack. */
1465 RTASN1TYPE_32BIT_HACK = 0x7fffffff
1466} RTASN1TYPE;
1467
1468
1469/**
1470 * ASN.1 dynamic type record.
1471 */
1472typedef struct RTASN1DYNTYPE
1473{
1474 /** Alternative interpretation provided by a user.
1475 * Before destroying this object, the user must explicitly free this and set
1476 * it to NULL, otherwise there will be memory leaks. */
1477 PRTASN1CORE pUser;
1478 /** The type of data we've got here. */
1479 RTASN1TYPE enmType;
1480 /** Union with data of the type dictated by enmType. */
1481 union
1482 {
1483 /** RTASN1TYPE_CORE. */
1484 RTASN1CORE Core;
1485 /** RTASN1TYPE_NULL. */
1486 RTASN1NULL Asn1Null;
1487 /** RTASN1TYPE_INTEGER. */
1488 RTASN1INTEGER Integer;
1489 /** RTASN1TYPE_BOOLEAN. */
1490 RTASN1BOOLEAN Boolean;
1491 /** RTASN1TYPE_STRING. */
1492 RTASN1STRING String;
1493 /** RTASN1TYPE_OCTET_STRING. */
1494 RTASN1OCTETSTRING OctetString;
1495 /** RTASN1TYPE_BIT_STRING. */
1496 RTASN1BITSTRING BitString;
1497 /** RTASN1TYPE_TIME. */
1498 RTASN1TIME Time;
1499#if 0
1500 /** RTASN1TYPE_SEQUENCE_CORE. */
1501 RTASN1SEQUENCECORE SeqCore;
1502 /** RTASN1TYPE_SET_CORE. */
1503 RTASN1SETCORE SetCore;
1504#endif
1505 /** RTASN1TYPE_OBJID. */
1506 RTASN1OBJID ObjId;
1507 } u;
1508} RTASN1DYNTYPE;
1509/** Pointer to an ASN.1 dynamic type record. */
1510typedef RTASN1DYNTYPE *PRTASN1DYNTYPE;
1511/** Pointer to a const ASN.1 dynamic type record. */
1512typedef RTASN1DYNTYPE const *PCRTASN1DYNTYPE;
1513RTASN1TYPE_STANDARD_PROTOTYPES(RTASN1DYNTYPE, RTDECL, RTAsn1DynType, u.Core);
1514RTDECL(int) RTAsn1DynType_SetToNull(PRTASN1DYNTYPE pThis);
1515
1516
1517/** @name Virtual Method Table Based API
1518 * @{ */
1519/**
1520 * Calls the destructor of the ASN.1 object.
1521 *
1522 * @param pThisCore The IPRT representation of an ASN.1 object.
1523 */
1524RTDECL(void) RTAsn1VtDelete(PRTASN1CORE pThisCore);
1525
1526/**
1527 * Deep enumeration of all descendants.
1528 *
1529 * @returns IPRT status code, any non VINF_SUCCESS value stems from pfnCallback.
1530 * @param pThisCore Pointer to the ASN.1 core to enumerate members of.
1531 * @param pfnCallback The callback.
1532 * @param uDepth The depth of this object. Children are at +1.
1533 * @param pvUser Callback user argument.
1534 * @param fDepthFirst When set, recurse into child objects before calling
1535 * pfnCallback on then. When clear, the child object
1536 * is first
1537 */
1538RTDECL(int) RTAsn1VtDeepEnum(PRTASN1CORE pThisCore, bool fDepthFirst, uint32_t uDepth,
1539 PFNRTASN1ENUMCALLBACK pfnCallback, void *pvUser);
1540
1541/**
1542 * Clones @a pSrcCore onto @a pThisCore.
1543 *
1544 * The caller must be sure that @a pSrcCore and @a pThisCore are of the same
1545 * types.
1546 *
1547 * @returns IPRT status code.
1548 * @param pThisCore Pointer to the ASN.1 core to clone onto. This shall
1549 * be uninitialized.
1550 * @param pSrcCore Pointer to the ASN.1 core to clone.
1551 * @param pAllocator The allocator to use.
1552 */
1553RTDECL(int) RTAsn1VtClone(PRTASN1CORE pThisCore, PRTASN1CORE pSrcCore, PCRTASN1ALLOCATORVTABLE pAllocator);
1554
1555/**
1556 * Compares two objects.
1557 *
1558 * @returns 0 if equal, -1 if @a pLeft is smaller, 1 if @a pLeft is larger.
1559 * @param pLeftCore Pointer to the ASN.1 core of the left side object.
1560 * @param pRightCore Pointer to the ASN.1 core of the right side object.
1561 */
1562RTDECL(int) RTAsn1VtCompare(PCRTASN1CORE pLeftCore, PCRTASN1CORE pRightCore);
1563
1564/**
1565 * Check sanity.
1566 *
1567 * A primary criteria is that the object is present and initialized.
1568 *
1569 * @returns IPRT status code.
1570 * @param pThisCore Pointer to the ASN.1 core of the object to check out.
1571 * @param fFlags See RTASN1_CHECK_SANITY_F_XXX.
1572 * @param pErrInfo Where to return additional error details. Optional.
1573 * @param pszErrorTag Tag for the additional error details.
1574 */
1575RTDECL(int) RTAsn1VtCheckSanity(PCRTASN1CORE pThisCore, uint32_t fFlags,
1576 PRTERRINFO pErrInfo, const char *pszErrorTag);
1577/** @} */
1578
1579
1580/** @defgroup rp_asn1_encode RTAsn1Encode - ASN.1 Encoding
1581 * @{ */
1582
1583/** @name RTASN1ENCODE_F_XXX
1584 * @{ */
1585/** Use distinguished encoding rules (DER) to encode the object. */
1586#define RTASN1ENCODE_F_DER UINT32_C(0x00000001)
1587/** Use base encoding rules (BER) to encode the object.
1588 * This is currently the same as DER for practical reasons. */
1589#define RTASN1ENCODE_F_BER RTASN1ENCODE_F_DER
1590/** Mask of valid encoding rules. */
1591#define RTASN1ENCODE_F_RULE_MASK UINT32_C(0x00000007)
1592/** @} */
1593
1594
1595/**
1596 * Recalculates cbHdr of and ASN.1 object.
1597 *
1598 * @returns IPRT status code.
1599 * @retval VINF_ASN1_NOT_ENCODED if the header size is zero (default value,
1600 * whatever).
1601 * @param pAsn1Core The object in question.
1602 * @param fFlags Valid combination of the RTASN1ENCODE_F_XXX
1603 * flags. Must include the encoding type.
1604 * @param pErrInfo Extended error info. Optional.
1605 */
1606RTDECL(int) RTAsn1EncodeRecalcHdrSize(PRTASN1CORE pAsn1Core, uint32_t fFlags, PRTERRINFO pErrInfo);
1607
1608/**
1609 * Prepares the ASN.1 structure for encoding.
1610 *
1611 * The preparations is mainly calculating accurate object size, but may also
1612 * involve operations like recoding internal UTF-8 strings to the actual ASN.1
1613 * format and other things that may require memory to allocated/reallocated.
1614 *
1615 * @returns IPRT status code
1616 * @param pRoot The root of the ASN.1 object tree to encode.
1617 * @param fFlags Valid combination of the RTASN1ENCODE_F_XXX
1618 * flags. Must include the encoding type.
1619 * @param pcbEncoded Where to return the encoded size. Optional.
1620 * @param pErrInfo Where to store extended error information.
1621 * Optional.
1622 */
1623RTDECL(int) RTAsn1EncodePrepare(PRTASN1CORE pRoot, uint32_t fFlags, uint32_t *pcbEncoded, PRTERRINFO pErrInfo);
1624
1625/**
1626 * Encodes and writes the header of an ASN.1 object.
1627 *
1628 * @returns IPRT status code.
1629 * @retval VINF_ASN1_NOT_ENCODED if nothing was written (default value,
1630 * whatever).
1631 * @param pAsn1Core The object in question.
1632 * @param fFlags Valid combination of the RTASN1ENCODE_F_XXX
1633 * flags. Must include the encoding type.
1634 * @param pfnWriter The output writer callback.
1635 * @param pvUser The user argument to pass to @a pfnWriter.
1636 * @param pErrInfo Where to store extended error information.
1637 * Optional.
1638 */
1639RTDECL(int) RTAsn1EncodeWriteHeader(PCRTASN1CORE pAsn1Core, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
1640 PRTERRINFO pErrInfo);
1641
1642/**
1643 * Encodes and writes an ASN.1 object.
1644 *
1645 * @returns IPRT status code
1646 * @param pRoot The root of the ASN.1 object tree to encode.
1647 * @param fFlags Valid combination of the RTASN1ENCODE_F_XXX
1648 * flags. Must include the encoding type.
1649 * @param pfnWriter The output writer callback.
1650 * @param pvUser The user argument to pass to @a pfnWriter.
1651 * @param pErrInfo Where to store extended error information.
1652 * Optional.
1653 */
1654RTDECL(int) RTAsn1EncodeWrite(PCRTASN1CORE pRoot, uint32_t fFlags, FNRTASN1ENCODEWRITER pfnWriter, void *pvUser,
1655 PRTERRINFO pErrInfo);
1656
1657/**
1658 * Encodes and writes an ASN.1 object into a caller allocated memory buffer.
1659 *
1660 * @returns IPRT status code
1661 * @param pRoot The root of the ASN.1 object tree to encode.
1662 * @param fFlags Valid combination of the RTASN1ENCODE_F_XXX
1663 * flags. Must include the encoding type.
1664 * @param pvBuf The output buffer.
1665 * @param cbBuf The buffer size. This should have the size
1666 * returned by RTAsn1EncodePrepare().
1667 * @param pErrInfo Where to store extended error information.
1668 * Optional.
1669 */
1670RTDECL(int) RTAsn1EncodeToBuffer(PCRTASN1CORE pRoot, uint32_t fFlags, void *pvBuf, size_t cbBuf, PRTERRINFO pErrInfo);
1671
1672/**
1673 * Helper for when DER encoded ASN.1 is needed for something.
1674 *
1675 * Handy when interfacing with OpenSSL and the many d2i_Xxxxx OpenSSL functions,
1676 * but also handy when structures needs to be digested or similar during signing
1677 * or verification.
1678 *
1679 * We sometimes can use the data we've decoded directly, but often we have
1680 * encode it into a temporary heap buffer.
1681 *
1682 * @returns IPRT status code, details in @a pErrInfo if present.
1683 * @param pRoot The ASN.1 root of the structure to be passed to OpenSSL.
1684 * @param ppbRaw Where to return the pointer to raw encoded data.
1685 * @param pcbRaw Where to return the size of the raw encoded data.
1686 * @param ppvFree Where to return what to pass to RTMemTmpFree, i.e. NULL
1687 * if we use the previously decoded data directly and
1688 * non-NULL if we had to allocate heap and encode it.
1689 * @param pErrInfo Where to return details about encoding issues. Optional.
1690 */
1691RTDECL(int) RTAsn1EncodeQueryRawBits(PRTASN1CORE pRoot, const uint8_t **ppbRaw, uint32_t *pcbRaw,
1692 void **ppvFree, PRTERRINFO pErrInfo);
1693
1694/** @} */
1695
1696
1697
1698/** @defgroup rp_asn1_cursor RTAsn1Cursor - BER, DER, and CER cursor
1699 * @{ */
1700
1701/**
1702 * ASN.1 decoder byte cursor.
1703 */
1704typedef struct RTASN1CURSOR
1705{
1706 /** Pointer to the current (next) byte. */
1707 uint8_t const *pbCur;
1708 /** Number of bytes left to decode. */
1709 uint32_t cbLeft;
1710 /** RTASN1CURSOR_FLAGS_XXX. */
1711 uint8_t fFlags;
1712 /** The cursor depth. */
1713 uint8_t cDepth;
1714 /** Two bytes reserved for future tricks. */
1715 uint8_t abReserved[2];
1716 /** Pointer to the primary cursor. */
1717 struct RTASN1CURSORPRIMARY *pPrimary;
1718 /** Pointer to the parent cursor. */
1719 struct RTASN1CURSOR *pUp;
1720 /** The error tag for this cursor level. */
1721 const char *pszErrorTag;
1722} RTASN1CURSOR;
1723
1724/** @name RTASN1CURSOR_FLAGS_XXX - Cursor flags.
1725 * @{ */
1726/** Enforce DER rules. */
1727#define RTASN1CURSOR_FLAGS_DER RT_BIT(1)
1728/** Enforce CER rules. */
1729#define RTASN1CURSOR_FLAGS_CER RT_BIT(2)
1730/** Pending indefinite length encoding. */
1731#define RTASN1CURSOR_FLAGS_INDEFINITE_LENGTH RT_BIT(3)
1732/** @} */
1733
1734
1735typedef struct RTASN1CURSORPRIMARY
1736{
1737 /** The normal cursor bits. */
1738 RTASN1CURSOR Cursor;
1739 /** For error reporting. */
1740 PRTERRINFO pErrInfo;
1741 /** The allocator virtual method table. */
1742 PCRTASN1ALLOCATORVTABLE pAllocator;
1743 /** Pointer to the first byte. Useful for calculating offsets. */
1744 uint8_t const *pbFirst;
1745} RTASN1CURSORPRIMARY;
1746typedef RTASN1CURSORPRIMARY *PRTASN1CURSORPRIMARY;
1747
1748
1749/**
1750 * Initializes a primary cursor.
1751 *
1752 * The primary cursor is special in that it stores information shared with the
1753 * sub-cursors created by methods like RTAsn1CursorGetContextTagNCursor and
1754 * RTAsn1CursorGetSequenceCursor. Even if just sharing a few items at present,
1755 * it still important to save every possible byte since stack space is scarce in
1756 * some of the execution environments.
1757 *
1758 * @returns Pointer to pCursor->Cursor.
1759 * @param pPrimaryCursor The primary cursor structure to initialize.
1760 * @param pvFirst The first byte to decode.
1761 * @param cb The number of bytes to decode.
1762 * @param pErrInfo Where to store error information.
1763 * @param pAllocator The allocator to use.
1764 * @param fFlags RTASN1CURSOR_FLAGS_XXX.
1765 * @param pszErrorTag The primary error tag.
1766 */
1767RTDECL(PRTASN1CURSOR) RTAsn1CursorInitPrimary(PRTASN1CURSORPRIMARY pPrimaryCursor, void const *pvFirst, uint32_t cb,
1768 PRTERRINFO pErrInfo, PCRTASN1ALLOCATORVTABLE pAllocator, uint32_t fFlags,
1769 const char *pszErrorTag);
1770
1771RTDECL(int) RTAsn1CursorInitSub(PRTASN1CURSOR pParent, uint32_t cb, PRTASN1CURSOR pChild, const char *pszErrorTag);
1772
1773/**
1774 * Initialize a sub-cursor for traversing the content of an ASN.1 object.
1775 *
1776 * @returns IPRT status code.
1777 * @param pParent The parent cursor.
1778 * @param pAsn1Core The ASN.1 object which content we should
1779 * traverse with the sub-cursor.
1780 * @param pChild The sub-cursor to initialize.
1781 * @param pszErrorTag The error tag of the sub-cursor.
1782 */
1783RTDECL(int) RTAsn1CursorInitSubFromCore(PRTASN1CURSOR pParent, PRTASN1CORE pAsn1Core,
1784 PRTASN1CURSOR pChild, const char *pszErrorTag);
1785
1786/**
1787 * Initalizes the an allocation structure prior to making an allocation.
1788 *
1789 * To try unify and optimize memory managment for decoding and in-memory
1790 * construction of ASN.1 objects, each allocation has an allocation structure
1791 * associated with it. This stores the allocator and keep statistics for
1792 * optimizing resizable allocations.
1793 *
1794 * @returns Pointer to the allocator info (for call in alloc parameter).
1795 * @param pCursor The cursor.
1796 * @param pAllocation The allocation structure to initialize.
1797 */
1798RTDECL(PRTASN1ALLOCATION) RTAsn1CursorInitAllocation(PRTASN1CURSOR pCursor, PRTASN1ALLOCATION pAllocation);
1799
1800/**
1801 * Initalizes the an array allocation structure prior to making an allocation.
1802 *
1803 * This is a special case of RTAsn1CursorInitAllocation. We store a little bit
1804 * more detail here in order to optimize growing and shrinking of arrays.
1805 *
1806 * @returns Pointer to the allocator info (for call in alloc parameter).
1807 * @param pCursor The cursor.
1808 * @param pAllocation The allocation structure to initialize.
1809 * @param cbEntry The array entry size.
1810 */
1811RTDECL(PRTASN1ARRAYALLOCATION) RTAsn1CursorInitArrayAllocation(PRTASN1CURSOR pCursor, PRTASN1ARRAYALLOCATION pAllocation,
1812 size_t cbEntry);
1813
1814/**
1815 * Wrapper around RTErrInfoSetV.
1816 *
1817 * @returns @a rc
1818 * @param pCursor The cursor.
1819 * @param rc The return code to return.
1820 * @param pszMsg Message format string.
1821 * @param ... Format arguments.
1822 */
1823RTDECL(int) RTAsn1CursorSetInfo(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, ...) RT_IPRT_FORMAT_ATTR(3, 4);
1824
1825/**
1826 * Wrapper around RTErrInfoSetV.
1827 *
1828 * @returns @a rc
1829 * @param pCursor The cursor.
1830 * @param rc The return code to return.
1831 * @param pszMsg Message format string.
1832 * @param va Format arguments.
1833 */
1834RTDECL(int) RTAsn1CursorSetInfoV(PRTASN1CURSOR pCursor, int rc, const char *pszMsg, va_list va) RT_IPRT_FORMAT_ATTR(3, 0);
1835
1836/**
1837 * Checks that we've reached the end of the data for the cursor.
1838 *
1839 * This differs from RTAsn1CursorCheckEnd in that it does not consider the end
1840 * an error and therefore leaves the error buffer alone.
1841 *
1842 * @returns True if end, otherwise false.
1843 * @param pCursor The cursor we're decoding from.
1844 */
1845RTDECL(bool) RTAsn1CursorIsEnd(PRTASN1CURSOR pCursor);
1846
1847/**
1848 * Checks that we've reached the end of the data for the cursor.
1849 *
1850 * @returns IPRT status code.
1851 * @param pCursor The cursor we're decoding from.
1852 */
1853RTDECL(int) RTAsn1CursorCheckEnd(PRTASN1CURSOR pCursor);
1854
1855/**
1856 * Specialization of RTAsn1CursorCheckEnd for handling indefinite length sequences.
1857 *
1858 * Makes sure we've reached the end of the data for the cursor, and in case of a
1859 * an indefinite length sequence it may adjust sequence length and the parent
1860 * cursor.
1861 *
1862 * @returns IPRT status code.
1863 * @param pCursor The cursor we're decoding from.
1864 * @param pSeqCore The sequence core record.
1865 * @sa RTAsn1CursorCheckSetEnd, RTAsn1CursorCheckOctStrEnd,
1866 * RTAsn1CursorCheckEnd
1867 */
1868RTDECL(int) RTAsn1CursorCheckSeqEnd(PRTASN1CURSOR pCursor, PRTASN1SEQUENCECORE pSeqCore);
1869
1870/**
1871 * Specialization of RTAsn1CursorCheckEnd for handling indefinite length sets.
1872 *
1873 * Makes sure we've reached the end of the data for the cursor, and in case of a
1874 * an indefinite length sets it may adjust set length and the parent cursor.
1875 *
1876 * @returns IPRT status code.
1877 * @param pCursor The cursor we're decoding from.
1878 * @param pSetCore The set core record.
1879 * @sa RTAsn1CursorCheckSeqEnd, RTAsn1CursorCheckOctStrEnd,
1880 * RTAsn1CursorCheckEnd
1881 */
1882RTDECL(int) RTAsn1CursorCheckSetEnd(PRTASN1CURSOR pCursor, PRTASN1SETCORE pSetCore);
1883
1884/**
1885 * Specialization of RTAsn1CursorCheckEnd for handling indefinite length
1886 * constructed octet strings.
1887 *
1888 * This function must used when parsing the content of an octet string, like
1889 * for example the Content of a PKCS\#7 ContentInfo structure. It makes sure
1890 * we've reached the end of the data for the cursor, and in case of a an
1891 * indefinite length sets it may adjust set length and the parent cursor.
1892 *
1893 * @returns IPRT status code.
1894 * @param pCursor The cursor we're decoding from.
1895 * @param pOctetString The octet string.
1896 * @sa RTAsn1CursorCheckSeqEnd, RTAsn1CursorCheckSetEnd,
1897 * RTAsn1CursorCheckEnd
1898 */
1899RTDECL(int) RTAsn1CursorCheckOctStrEnd(PRTASN1CURSOR pCursor, PRTASN1OCTETSTRING pOctetString);
1900
1901
1902/**
1903 * Skips a given number of bytes.
1904 *
1905 * @returns @a pCursor
1906 * @param pCursor The cursor.
1907 * @param cb The number of bytes to skip.
1908 * @internal
1909 */
1910DECLINLINE(PRTASN1CURSOR) RTAsn1CursorSkip(PRTASN1CURSOR pCursor, uint32_t cb)
1911{
1912 if (cb <= pCursor->cbLeft)
1913 {
1914 pCursor->cbLeft -= cb;
1915 pCursor->pbCur += cb;
1916 }
1917 else
1918 {
1919 pCursor->pbCur += pCursor->cbLeft;
1920 pCursor->cbLeft = 0;
1921 }
1922
1923 return pCursor;
1924}
1925
1926/**
1927 * Low-level function for reading an ASN.1 header.
1928 *
1929 * @returns IPRT status code.
1930 * @param pCursor The cursor we're decoding from.
1931 * @param pAsn1Core The output object core.
1932 * @param pszErrorTag Error tag.
1933 * @internal
1934 */
1935RTDECL(int) RTAsn1CursorReadHdr(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, const char *pszErrorTag);
1936
1937/**
1938 * Common helper for simple tag matching.
1939 *
1940 * @returns IPRT status code.
1941 * @param pCursor The cursor (for error reporting).
1942 * @param pAsn1Core The ASN.1 core structure.
1943 * @param uTag The expected tag.
1944 * @param fClass The expected class.
1945 * @param fString Set if it's a string type that shall follow
1946 * special CER and DER rules wrt to constructed and
1947 * primitive encoding.
1948 * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
1949 * @param pszErrorTag The error tag.
1950 * @param pszWhat The type/whatever name.
1951 */
1952RTDECL(int) RTAsn1CursorMatchTagClassFlagsEx(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
1953 bool fString, uint32_t fFlags, const char *pszErrorTag, const char *pszWhat);
1954
1955/**
1956 * Common helper for simple tag matching.
1957 *
1958 * @returns IPRT status code.
1959 * @param pCursor The cursor (for error reporting).
1960 * @param pAsn1Core The ASN.1 core structure.
1961 * @param uTag The expected tag.
1962 * @param fClass The expected class.
1963 * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
1964 * @param pszErrorTag The error tag.
1965 * @param pszWhat The type/whatever name.
1966 * @internal
1967 */
1968DECLINLINE(int) RTAsn1CursorMatchTagClassFlags(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
1969 uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
1970{
1971 if (pAsn1Core->uTag == uTag && pAsn1Core->fClass == fClass)
1972 return VINF_SUCCESS;
1973 return RTAsn1CursorMatchTagClassFlagsEx(pCursor, pAsn1Core, uTag, fClass, false /*fString*/, fFlags, pszErrorTag, pszWhat);
1974}
1975
1976
1977/**
1978 * Common helper for simple tag matching for strings.
1979 *
1980 * Check string encoding considerations.
1981 *
1982 * @returns IPRT status code.
1983 * @param pCursor The cursor (for error reporting).
1984 * @param pAsn1Core The ASN.1 core structure.
1985 * @param uTag The expected tag.
1986 * @param fClass The expected class.
1987 * @param fFlags The RTASN1CURSOR_GET_F_XXX flags.
1988 * @param pszErrorTag The error tag.
1989 * @param pszWhat The type/whatever name.
1990 * @internal
1991 */
1992DECLINLINE(int) RTAsn1CursorMatchTagClassFlagsString(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core, uint32_t uTag, uint32_t fClass,
1993 uint32_t fFlags, const char *pszErrorTag, const char *pszWhat)
1994{
1995 if (pAsn1Core->uTag == uTag && pAsn1Core->fClass == fClass)
1996 return VINF_SUCCESS;
1997 return RTAsn1CursorMatchTagClassFlagsEx(pCursor, pAsn1Core, uTag, fClass, true /*fString*/, fFlags, pszErrorTag, pszWhat);
1998}
1999
2000
2001
2002/** @name RTASN1CURSOR_GET_F_XXX - Common flags for all the getters.
2003 * @{ */
2004/** Used for decoding objects with implicit tags assigned to them. This only
2005 * works when calling getters with a unambigious types. */
2006#define RTASN1CURSOR_GET_F_IMPLICIT RT_BIT_32(0)
2007/** @} */
2008
2009/**
2010 * Read ANY object.
2011 *
2012 * @returns IPRT status code.
2013 * @param pCursor The cursor we're decoding from.
2014 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2015 * @param pAsn1Core The output object core.
2016 * @param pszErrorTag Error tag.
2017 */
2018RTDECL(int) RTAsn1CursorGetCore(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1CORE pAsn1Core, const char *pszErrorTag);
2019
2020/**
2021 * Read a NULL object.
2022 *
2023 * @returns IPRT status code.
2024 * @param pCursor The cursor we're decoding from.
2025 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2026 * @param pNull The output NULL object.
2027 * @param pszErrorTag Error tag.
2028 */
2029RTDECL(int) RTAsn1CursorGetNull(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1NULL pNull, const char *pszErrorTag);
2030
2031/**
2032 * Read an INTEGER object.
2033 *
2034 * @returns IPRT status code.
2035 * @param pCursor The cursor we're decoding from.
2036 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2037 * @param pInteger The output integer object.
2038 * @param pszErrorTag Error tag.
2039 */
2040RTDECL(int) RTAsn1CursorGetInteger(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1INTEGER pInteger, const char *pszErrorTag);
2041
2042/**
2043 * Read an BOOLEAN object.
2044 *
2045 * @returns IPRT status code.
2046 * @param pCursor The cursor we're decoding from.
2047 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2048 * @param pBoolean The output boolean object.
2049 * @param pszErrorTag Error tag.
2050 */
2051RTDECL(int) RTAsn1CursorGetBoolean(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BOOLEAN pBoolean, const char *pszErrorTag);
2052
2053/**
2054 * Retrives an object identifier (aka ObjId or OID) item from the ASN.1 stream.
2055 *
2056 * @returns IPRT status code.
2057 * @param pCursor The cursor.
2058 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2059 * @param pObjId The output ODI object.
2060 * @param pszErrorTag Error tag.
2061 */
2062RTDECL(int) RTAsn1CursorGetObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId, const char *pszErrorTag);
2063
2064/**
2065 * Retrives and verifies an object identifier.
2066 *
2067 * @returns IPRT status code.
2068 * @param pCursor The cursor.
2069 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2070 * @param pObjId Where to return the parsed object ID, optional.
2071 * @param pszExpectedObjId The expected object identifier (dotted).
2072 * @param pszErrorTag Error tag.
2073 */
2074RTDECL(int) RTAsn1CursorGetAndCheckObjId(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OBJID pObjId,
2075 const char *pszExpectedObjId, const char *pszErrorTag);
2076
2077/**
2078 * Read an UTC TIME or GENERALIZED TIME object.
2079 *
2080 * @returns IPRT status code.
2081 * @param pCursor The cursor we're decoding from.
2082 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2083 * @param pTime The output time object.
2084 * @param pszErrorTag Error tag.
2085 */
2086RTDECL(int) RTAsn1CursorGetTime(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1TIME pTime, const char *pszErrorTag);
2087
2088/**
2089 * Read an BIT STRING object (skips past the content).
2090 *
2091 * @returns IPRT status ocde.
2092 * @param pCursor The cursor.
2093 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2094 * @param pBitString The output bit string object.
2095 * @param pszErrorTag Error tag.
2096 */
2097RTDECL(int) RTAsn1CursorGetBitString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1BITSTRING pBitString,
2098 const char *pszErrorTag);
2099
2100/**
2101 * Read an BIT STRING object (skips past the content), extended version with
2102 * cMaxBits.
2103 *
2104 * @returns IPRT status ocde.
2105 * @param pCursor The cursor.
2106 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2107 * @param cMaxBits The max length of the bit string in bits. Pass
2108 * UINT32_MAX if variable size.
2109 * @param pBitString The output bit string object.
2110 * @param pszErrorTag Error tag.
2111 */
2112RTDECL(int) RTAsn1CursorGetBitStringEx(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t cMaxBits, PRTASN1BITSTRING pBitString,
2113 const char *pszErrorTag);
2114
2115/**
2116 * Read an OCTET STRING object (skips past the content).
2117 *
2118 * @returns IPRT status ocde.
2119 * @param pCursor The cursor.
2120 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2121 * @param pOctetString The output octet string object.
2122 * @param pszErrorTag Error tag.
2123 */
2124RTDECL(int) RTAsn1CursorGetOctetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1OCTETSTRING pOctetString,
2125 const char *pszErrorTag);
2126
2127/**
2128 * Read any kind of string object, except 'character string (29)'.
2129 *
2130 * @returns IPRT status code.
2131 * @param pCursor The cursor we're decoding from.
2132 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2133 * @param pString The output boolean object.
2134 * @param pszErrorTag Error tag.
2135 */
2136RTDECL(int) RTAsn1CursorGetString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag);
2137
2138/**
2139 * Read a IA5 STRING object.
2140 *
2141 * @returns IPRT status code.
2142 * @param pCursor The cursor we're decoding from.
2143 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2144 * @param pString The output boolean object.
2145 * @param pszErrorTag Error tag.
2146 */
2147RTDECL(int) RTAsn1CursorGetIa5String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag);
2148
2149/**
2150 * Read a UTF8 STRING object.
2151 *
2152 * @returns IPRT status code.
2153 * @param pCursor The cursor we're decoding from.
2154 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2155 * @param pString The output boolean object.
2156 * @param pszErrorTag Error tag.
2157 */
2158RTDECL(int) RTAsn1CursorGetUtf8String(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag);
2159
2160/**
2161 * Read a BMP STRING (UCS-2) object.
2162 *
2163 * @returns IPRT status code.
2164 * @param pCursor The cursor we're decoding from.
2165 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2166 * @param pString The output boolean object.
2167 * @param pszErrorTag Error tag.
2168 */
2169RTDECL(int) RTAsn1CursorGetBmpString(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1STRING pString, const char *pszErrorTag);
2170
2171/**
2172 * Read a SEQUENCE object and create a cursor for its content.
2173 *
2174 * @returns IPRT status code.
2175 * @param pCursor The cursor we're decoding from.
2176 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2177 * @param pSeqCore The output sequence core object.
2178 * @param pSeqCursor The output cursor for the sequence content.
2179 * @param pszErrorTag Error tag, this will be associated with the
2180 * returned cursor.
2181 */
2182RTDECL(int) RTAsn1CursorGetSequenceCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
2183 PRTASN1SEQUENCECORE pSeqCore, PRTASN1CURSOR pSeqCursor, const char *pszErrorTag);
2184
2185/**
2186 * Read a SET object and create a cursor for its content.
2187 *
2188 * @returns IPRT status code.
2189 * @param pCursor The cursor we're decoding from.
2190 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2191 * @param pSetCore The output set core object.
2192 * @param pSetCursor The output cursor for the set content.
2193 * @param pszErrorTag Error tag, this will be associated with the
2194 * returned cursor.
2195 */
2196RTDECL(int) RTAsn1CursorGetSetCursor(PRTASN1CURSOR pCursor, uint32_t fFlags,
2197 PRTASN1SETCORE pSetCore, PRTASN1CURSOR pSetCursor, const char *pszErrorTag);
2198
2199/**
2200 * Read a given constructed context tag and create a cursor for its content.
2201 *
2202 * @returns IPRT status code.
2203 * @param pCursor The cursor we're decoding from.
2204 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2205 * @param uExpectedTag The expected tag.
2206 * @param pVtable The vtable for the context tag node (see
2207 * RTASN1TMPL_PASS_XTAG).
2208 * @param pCtxTag The output context tag object.
2209 * @param pCtxTagCursor The output cursor for the context tag content.
2210 * @param pszErrorTag Error tag, this will be associated with the
2211 * returned cursor.
2212 *
2213 * @remarks There are specialized version of this function for each of the
2214 * numbered context tag structures, like for RTASN1CONTEXTTAG0 there is
2215 * RTAsn1CursorGetContextTag0Cursor.
2216 */
2217RTDECL(int) RTAsn1CursorGetContextTagNCursor(PRTASN1CURSOR pCursor, uint32_t fFlags, uint32_t uExpectedTag,
2218 PCRTASN1COREVTABLE pVtable, PRTASN1CONTEXTTAG pCtxTag, PRTASN1CURSOR pCtxTagCursor,
2219 const char *pszErrorTag);
2220
2221/**
2222 * Read a dynamic ASN.1 type.
2223 *
2224 * @returns IPRT status code.
2225 * @param pCursor The cursor we're decoding from.
2226 * @param fFlags RTASN1CURSOR_GET_F_XXX.
2227 * @param pDynType The output context tag object.
2228 * @param pszErrorTag Error tag.
2229 */
2230RTDECL(int) RTAsn1CursorGetDynType(PRTASN1CURSOR pCursor, uint32_t fFlags, PRTASN1DYNTYPE pDynType, const char *pszErrorTag);
2231
2232/**
2233 * Peeks at the next ASN.1 object.
2234 *
2235 * @returns IPRT status code.
2236 * @param pCursor The cursore we're decoding from.
2237 * @param pAsn1Core Where to store the output of the peek.
2238 */
2239RTDECL(int) RTAsn1CursorPeek(PRTASN1CURSOR pCursor, PRTASN1CORE pAsn1Core);
2240
2241/**
2242 * Checks if the next ASN.1 object matches the given tag and class/flags.
2243 *
2244 * @returns @c true on match, @c false on mismatch.
2245 * @param pCursor The cursore we're decoding from.
2246 * @param uTag The tag number to match against.
2247 * @param fClass The tag class and flags to match against.
2248 */
2249RTDECL(bool) RTAsn1CursorIsNextEx(PRTASN1CURSOR pCursor, uint32_t uTag, uint8_t fClass);
2250
2251
2252
2253/** @internal */
2254#define RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(a_uTag) \
2255 DECLINLINE(int) RT_CONCAT3(RTAsn1CursorGetContextTag,a_uTag,Cursor)(PRTASN1CURSOR pCursor, uint32_t fFlags, \
2256 PCRTASN1COREVTABLE pVtable, \
2257 RT_CONCAT(PRTASN1CONTEXTTAG,a_uTag) pCtxTag, \
2258 PRTASN1CURSOR pCtxTagCursor, const char *pszErrorTag) \
2259 { /* Constructed is automatically implied if you need a cursor to it. */ \
2260 return RTAsn1CursorGetContextTagNCursor(pCursor, fFlags, a_uTag, pVtable, (PRTASN1CONTEXTTAG)pCtxTag, pCtxTagCursor, pszErrorTag); \
2261 } \
2262 DECLINLINE(int) RT_CONCAT3(RTAsn1ContextTag,a_uTag,InitDefault)(RT_CONCAT(PRTASN1CONTEXTTAG,a_uTag) pCtxTag) \
2263 { /* Constructed is automatically implied if you need to init it with a default value. */ \
2264 return RTAsn1Core_InitDefault(&pCtxTag->Asn1Core, a_uTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED); \
2265 } \
2266 DECLINLINE(int) RT_CONCAT3(RTAsn1CursorIsConstructedContextTag,a_uTag,Next)(PRTASN1CURSOR pCursor) \
2267 { \
2268 return RTAsn1CursorIsNextEx(pCursor, a_uTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED); \
2269 } \
2270 DECLINLINE(int) RT_CONCAT3(RTAsn1CursorIsPrimitiveContextTag,a_uTag,Next)(PRTASN1CURSOR pCursor) \
2271 { \
2272 return RTAsn1CursorIsNextEx(pCursor, a_uTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_PRIMITIVE); \
2273 } \
2274 DECLINLINE(int) RT_CONCAT3(RTAsn1CursorIsAnyContextTag,a_uTag,Next)(PRTASN1CURSOR pCursor) \
2275 { \
2276 return RTAsn1CursorIsNextEx(pCursor, a_uTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_CONSTRUCTED) \
2277 || RTAsn1CursorIsNextEx(pCursor, a_uTag, ASN1_TAGCLASS_CONTEXT | ASN1_TAGFLAG_PRIMITIVE);\
2278 } \
2279
2280RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(0)
2281RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(1)
2282RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(2)
2283RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(3)
2284RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(4)
2285RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(5)
2286RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(6)
2287RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES(7)
2288#undef RTASN1CONTEXTTAG_IMPL_CURSOR_INLINES
2289
2290
2291/**
2292 * Checks if the next object is a boolean.
2293 *
2294 * @returns true / false
2295 * @param pCursor The cursor we're decoding from.
2296 * @remarks May produce error info output on mismatch.
2297 */
2298DECLINLINE(bool) RTAsn1CursorIsBooleanNext(PRTASN1CURSOR pCursor)
2299{
2300 return RTAsn1CursorIsNextEx(pCursor, ASN1_TAG_BOOLEAN, ASN1_TAGFLAG_PRIMITIVE | ASN1_TAGCLASS_UNIVERSAL);
2301}
2302
2303
2304/**
2305 * Checks if the next object is a set.
2306 *
2307 * @returns true / false
2308 * @param pCursor The cursor we're decoding from.
2309 * @remarks May produce error info output on mismatch.
2310 */
2311DECLINLINE(bool) RTAsn1CursorIsSetNext(PRTASN1CURSOR pCursor)
2312{
2313 return RTAsn1CursorIsNextEx(pCursor, ASN1_TAG_SET, ASN1_TAGFLAG_CONSTRUCTED | ASN1_TAGCLASS_UNIVERSAL);
2314}
2315
2316
2317/** @} */
2318
2319
2320/** @name ASN.1 Utility APIs
2321 * @{ */
2322
2323/**
2324 * Dumps an IPRT representation of a ASN.1 object tree.
2325 *
2326 * @returns IPRT status code.
2327 * @param pAsn1Core The ASN.1 object which members should be dumped.
2328 * @param fFlags RTASN1DUMP_F_XXX.
2329 * @param uLevel The indentation level to start at.
2330 * @param pfnPrintfV The output function.
2331 * @param pvUser Argument to the output function.
2332 */
2333RTDECL(int) RTAsn1Dump(PCRTASN1CORE pAsn1Core, uint32_t fFlags, uint32_t uLevel, PFNRTDUMPPRINTFV pfnPrintfV, void *pvUser);
2334
2335/**
2336 * Queries the name for an object identifier.
2337 *
2338 * This API is very simple due to how we store the data.
2339 *
2340 * @returns IPRT status code.
2341 * @retval VINF_SUCCESS on success.
2342 * @retval VERR_NOT_FOUND if not found.
2343 * @retval VERR_BUFFER_OVERFLOW if more buffer space is required.
2344 *
2345 * @param pObjId The object ID to name.
2346 * @param pszDst Where to store the name if found.
2347 * @param cbDst The size of the destination buffer.
2348 */
2349RTDECL(int) RTAsn1QueryObjIdName(PCRTASN1OBJID pObjId, char *pszDst, size_t cbDst);
2350
2351/** @} */
2352
2353/** @} */
2354
2355RT_C_DECLS_END
2356
2357#endif /* !IPRT_INCLUDED_asn1_h */
2358
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