VirtualBox

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

Last change on this file since 73662 was 73662, checked in by vboxsync, 6 years ago

IPRT/ASN.1: Added g_RTAsn1EFenceAllocator and RTAsn1CursorIsEnd.

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