VirtualBox

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

Last change on this file since 57579 was 57579, checked in by vboxsync, 9 years ago

iprt/asn1.h: Unused parameter warnings.

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