ASN1C C/C++ Common Runtime  ASN1C v7.3.x
asn1type.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 1997-2019 Objective Systems, Inc.
3  *
4  * This software is furnished under a license and may be used and copied
5  * only in accordance with the terms of such license and with the
6  * inclusion of the above copyright notice. This software or any other
7  * copies thereof may not be provided or otherwise made available to any
8  * other person. No title to and ownership of the software is hereby
9  * transferred.
10  *
11  * The information in this software is subject to change without notice
12  * and should not be construed as a commitment by Objective Systems, Inc.
13  *
14  * PROPRIETARY NOTICE
15  *
16  * This software is an unpublished work subject to a confidentiality agreement
17  * and is protected by copyright and trade secret law. Unauthorized copying,
18  * redistribution or other use of this work is prohibited.
19  *
20  * The above notice of copyright on this source code product does not indicate
21  * any actual or intended publication of such source code.
22  *
23  *****************************************************************************/
24  /*The following is the text for the main C/C++ Runtime index page.*/
100 #ifndef _ASN1TYPE_H_
101 #define _ASN1TYPE_H_
102 
103 #include <limits.h>
104 #include <stdio.h>
105 #include <string.h>
106 #include <stddef.h>
107 #include <setjmp.h>
108 #include <stdlib.h> /*so that borland compilers don't choke on extern linkage*/
109 #ifndef _WIN32_WCE
110 #include <time.h>
111 #endif
112 
113 #ifdef _NUCLEUS
114 #ifndef NUCLEUS
115 #include <nucleus.h>
116 #endif /* NUCLEUS */
117 #else
118 
119 #if !defined(_MSC_VER) && !defined(__GNUC__) && !defined(_16BIT) && !defined(__SYMBIAN32__)
120 #include <wchar.h>
121 #endif
122 
123 #endif /* _NUCLEUS */
124 
125 #include "rtxsrc/rtxExternDefs.h"
126 #include "rtxsrc/rtxSList.h"
127 #include "rtxsrc/rtxStack.h"
128 #include "rtxsrc/rtxUTF8.h"
129 
130 
169 #include "rtsrc/asn1tag.h"
170 #include "rtsrc/asn1ErrCodes.h"
171 #include "rtsrc/rtExternDefs.h"
172 
173 /*
174  * Flags used to determine or change the behavior of encoding and
175  * decoding.
176  */
178 #define XM_SEEK 0x01
179 
180 #define XM_ADVANCE 0x02
181 
182 #define XM_DYNAMIC 0x04
183 
184 #define XM_SKIP 0x08
185 
186 #define XM_OPTIONAL 0x10
187 
188 /* Sizing Constants */
189 
191 #define ASN_K_MAXDEPTH 32
192 
193 #define ASN_K_MAXENUM 100
194 
195 #define ASN_K_MAXERRP 5
196 
197 #define ASN_K_MAXERRSTK 8
198 
199 #ifndef ASN_K_ENCBUFSIZ
200 
201 #define ASN_K_ENCBUFSIZ 16*1024
202 #endif
203 
204 #ifndef ASN_K_MEMBUFSEG
205 
206 #define ASN_K_MEMBUFSEG 1024
207 #endif
208 
210 #define OSRTINDENTSPACES 3
211 
212 /* Constants for encoding/decoding real values */
213 
215 #define ASN1_K_PLUS_INFINITY 0x40
216 
218 #define ASN1_K_MINUS_INFINITY 0x41
219 
221 #define ASN1_K_NOT_A_NUMBER 0x42
222 
224 #define ASN1_K_MINUS_ZERO 0x43
225 
226 #define REAL_BINARY 0x80
227 #define REAL_SIGN 0x40
228 #define REAL_EXPLEN_MASK 0x03
229 #define REAL_EXPLEN_1 0x00
230 #define REAL_EXPLEN_2 0x01
231 #define REAL_EXPLEN_3 0x02
232 #define REAL_EXPLEN_LONG 0x03
233 #define REAL_FACTOR_MASK 0x0c
234 #define REAL_BASE_MASK 0x30
235 #define REAL_BASE_2 0x00
236 #define REAL_BASE_8 0x10
237 #define REAL_BASE_16 0x20
238 #define REAL_ISO6093_MASK 0x3F
239 
240 #ifndef __SYMBIAN32__
241 #include <float.h>
242 #define ASN1REALMAX (OSREAL)DBL_MAX
243 #define ASN1REALMIN (OSREAL)-DBL_MAX
244 #endif
245 
246 typedef void* ASN1ANY;
247 
252 typedef enum { ASN1HEX, ASN1BIN, ASN1CHR } ASN1StrType;
253 
257 typedef enum { ASN1ENCODE, ASN1DECODE } ASN1ActionType;
258 
259 /*
260  * OBJECT IDENTIFIER
261  */
270 #define ASN_K_MAXSUBIDS 128 /* maximum sub-id's in an object ID */
271 
275 typedef struct { /* object identifier */
277  OSUINT32 numids;
279  OSUINT32 subid[ASN_K_MAXSUBIDS];
280 } ASN1OBJID;
281 
285 typedef struct { /* object identifier with 64-bit arcs */
287  OSUINT32 numids;
289  OSUINT64 subid[ASN_K_MAXSUBIDS];
290 } ASN1OID64;
291 
293 #ifdef __cplusplus
294 #define EXTERN_C extern "C"
295 extern "C" {
296 #else
297 #define EXTERN_C extern
298 #endif
299 
315 EXTERNRT void rtSetOID (ASN1OBJID* ptarget, ASN1OBJID* psource);
316 
332 EXTERNRT void rtAddOID (ASN1OBJID* ptarget, ASN1OBJID* psource);
333 
341 EXTERNRT OSBOOL rtOIDsEqual (const ASN1OBJID* pOID1, const ASN1OBJID* pOID2);
342 
355 EXTERNRT int rtOIDParseString
356 (const char* oidstr, OSSIZE oidstrlen, ASN1OBJID* pvalue);
357 
358 
370 EXTERNRT int rtRelOIDParseString
371 (const char* oidstr, OSSIZE oidstrlen, ASN1OBJID* pvalue);
372 
386 EXTERNRT int rtOIDParseDottedNumberString
387 (const char* oidstr, OSSIZE oidstrlen, ASN1OBJID* pvalue);
388 
399 EXTERNRT OSBOOL rtOIDIsValid (const ASN1OBJID* pvalue);
400 
405 /*
406  * OCTET STRING
407  */
412 typedef struct {
413  OSUINT32 numocts;
414  OSOCTET data[1];
415 } ASN1OctStr;
416 
418 #define ASN1DynOctStr OSDynOctStr
419 #define ASN1DynOctStr64 OSDynOctStr64
420 
421 /*
422  * BIT STRING
423  */
425 typedef struct {
426  OSUINT32 numbits;
427  const OSOCTET* data;
428 } ASN1DynBitStr;
429 
432 typedef struct {
433  OSSIZE numbits;
434  const OSOCTET* data;
436 
438 typedef struct {
439  OSUINT32 numbits;
440  OSOCTET data[4];
441 } ASN1BitStr32;
442 
443 #include "rtxsrc/rtxBitString.h"
444 
451 #define OSSETBIT(bitStr,bitIndex) \
452 rtxSetBit (bitStr.data, bitStr.numbits, bitIndex)
453 
460 #define OSSETBITP(pBitStr,bitIndex) \
461 rtxSetBit ((pBitStr)->data, (pBitStr)->numbits, bitIndex)
462 
469 #define OSCLEARBIT(bitStr,bitIndex) \
470 rtxClearBit (bitStr.data, bitStr.numbits, bitIndex)
471 
478 #define OSCLEARBITP(pBitStr,bitIndex) \
479 rtxClearBit ((pBitStr)->data, (pBitStr)->numbits, bitIndex)
480 
489 #define OSTESTBIT(bitStr,bitIndex) \
490 rtxTestBit (bitStr.data, bitStr.numbits, bitIndex)
491 
498 #define OSTESTBITP(pBitStr,bitIndex) \
499 rtxTestBit ((pBitStr)->data, (pBitStr)->numbits, bitIndex)
500 
534 EXTERNRT int rtMakeGeneralizedTime (OSCTXT* pctxt,
535  const OSNumDateTime* dateTime,
536  char** outdata,
537  size_t outdataSize);
538 
563 EXTERNRT int rtMakeUTCTime (OSCTXT* pctxt,
564  const OSNumDateTime* dateTime,
565  char** outdata,
566  size_t outdataSize);
582 EXTERNRT int rtParseGeneralizedTime (OSCTXT *pctxt,
583  const char* value,
584  OSNumDateTime* dateTime);
585 
601 EXTERNRT int rtParseUTCTime (OSCTXT *pctxt,
602  const char* value,
603  OSNumDateTime* dateTime);
604 
610 EXTERNRT void normalizeTimeZone(OSNumDateTime* pvalue);
611 
616 /*
617  * SEQUENCE OF
618  */
623 typedef struct { /* generic sequence of structure */
625  OSUINT32 n;
626 
628  void* elem;
629 } ASN1SeqOf;
630 
636 typedef struct { /* sequence of OCTET STRING */
638  OSUINT32 n;
639 
641  OSDynOctStr* elem;
643 
648 typedef struct { /* generic open type data structure */
649  OSSIZE numocts;
650  const OSOCTET* data;
651 } ASN1OpenType;
652 
658 typedef struct { /* generic table constraint value holder */
659  ASN1OpenType encoded;
660  void* decoded;
661  OSINT32 index; /* table index */
662 } Asn1Object;
663 
664 typedef Asn1Object ASN1Object;
665 
670 typedef enum { OSXSDAny_binary, OSXSDAny_xmlText } OSXSDAnyAlt;
671 typedef struct OSXSDAny {
672  OSXSDAnyAlt t;
673  union {
674  ASN1OpenType* binary;
675  const OSUTF8CHAR* xmlText;
676  } u;
677 } OSXSDAny;
678 
679 /* ASN.1 useful type definitions */
680 
686 typedef struct {
687  OSSIZE nchars;
688  OSUNICHAR* data;
690 
691 typedef OSUNICHAR ASN116BITCHAR;
692 
698 typedef struct {
699  OSSIZE nchars;
700  OS32BITCHAR* data;
702 
703 typedef const char* ASN1GeneralizedTime;
704 typedef const char* ASN1GeneralString;
705 typedef const char* ASN1GraphicString;
706 typedef const char* ASN1IA5String;
707 typedef const char* ASN1ISO646String;
708 typedef const char* ASN1NumericString;
709 typedef const char* ASN1ObjectDescriptor;
710 typedef const char* ASN1PrintableString;
711 typedef const char* ASN1TeletexString;
712 typedef const char* ASN1T61String;
713 typedef const char* ASN1UTCTime;
714 typedef const char* ASN1VideotexString;
715 typedef const char* ASN1VisibleString;
716 
717 typedef const OSUTF8CHAR* ASN1UTF8String;
720 
721 /* ASN.1 constrained string structures */
722 
727 typedef struct {
728  int nchars;
729  char data[255];
730 } Asn1CharArray;
731 
737 typedef struct {
743 
747  const char* canonicalSet;
748 
753 
758 
764 
770 } Asn1CharSet;
771 
776 typedef struct {
779 
781  OSUINT16 firstChar, lastChar;
782 
784  unsigned unalignedBits;
785 
787  unsigned alignedBits;
789 
794 typedef struct {
797 
799  OSUINT32 firstChar, lastChar;
800 
802  unsigned unalignedBits;
803 
805  unsigned alignedBits;
807 
816 /* Helper functions. */
817 
830 EXTERNRT int rtValidateStr (ASN1TAG tag, const char *pdata);
831 
845 EXTERNRT int rtValidateChars (ASN1TAG tag, const char* pdata, size_t nchars);
846 
847 /* 16-bit character functions */
848 
859 EXTERNRT const char* rtBMPToCString (ASN1BMPString* pBMPString,
860  char* cstring, OSSIZE cstrsize);
861 
872 EXTERNRT const char* rtBMPToNewCString (ASN1BMPString* pBMPString);
873 
887 EXTERNRT const char* rtBMPToNewCStringEx
888  (OSCTXT *pctxt, ASN1BMPString* pBMPString);
889 
905 EXTERNRT ASN1BMPString* rtCToBMPString (OSCTXT* pctxt,
906  const char* cstring,
907  ASN1BMPString* pBMPString,
908  Asn116BitCharSet* pCharSet);
909 
920 EXTERNRT OSBOOL rtIsIn16BitCharSet (OSUNICHAR ch,
921  Asn116BitCharSet* pCharSet);
922 
923 /* 32-bit character (UCS-4) functions */
924 
925 
937 EXTERNRT const char* rtUCSToCString (ASN1UniversalString* pUCSString,
938  char* cstring, OSSIZE cstrsize);
939 
952 EXTERNRT const char* rtUCSToNewCString (ASN1UniversalString* pUCSString);
953 
968 EXTERNRT const char* rtUCSToNewCStringEx
969 (OSCTXT* pctxt, ASN1UniversalString* pUCSString);
970 
986 EXTERNRT ASN1UniversalString* rtCToUCSString
987 (OSCTXT* pctxt, const char* cstring, ASN1UniversalString* pUCSString,
988  Asn132BitCharSet* pCharSet);
989 
990 
1001 EXTERNRT OSBOOL rtIsIn32BitCharSet (OS32BITCHAR ch,
1002  Asn132BitCharSet* pCharSet);
1003 
1016 EXTERNRT wchar_t* rtUCSToWCSString (ASN1UniversalString* pUCSString,
1017  wchar_t* wcstring, OSUINT32 wcstrsize);
1018 
1019 
1037 EXTERNRT ASN1UniversalString* rtWCSToUCSString
1038 (OSCTXT* pctxt, wchar_t* wcstring,
1039  ASN1UniversalString* pUCSString, Asn132BitCharSet* pCharSet);
1040 
1052 EXTERNRT int rtUnivStrToUTF8
1053 (OSCTXT* pctxt, const ASN1UniversalString* pUnivStr, OSOCTET* outbuf,
1054  size_t outbufsiz);
1055 
1070 EXTERNRT int rtUTF8StrToASN1DynBitStr
1071  (OSCTXT* pctxt, const OSUTF8CHAR* utf8str, ASN1DynBitStr* pvalue);
1072 
1089 EXTERNRT int rtUTF8StrnToASN1DynBitStr
1090  (OSCTXT* pctxt, const OSUTF8CHAR* utf8str, size_t nbytes,
1091  ASN1DynBitStr* pvalue);
1092 
1103 typedef struct ASN1BigInt {
1105  size_t numocts;
1106 
1108  OSOCTET* mag;
1109 
1111  int sign;
1112 
1114  size_t allocated;
1115 
1117  OSBOOL dynamic;
1118 } ASN1BigInt;
1119 
1121 #define ASN1_K_CCBMaskSize 32
1122 
1124 #define ASN1_K_NumBitsPerMask 16
1125 
1127 #define ASN1_K_MaxSetElements (ASN1_K_CCBMaskSize*ASN1_K_NumBitsPerMask)
1128 
1136 typedef struct {
1138  OSOCTET* ptr;
1139 
1141  long len;
1142 
1144  int seqx;
1145 
1147  OSUINT16 mask[ASN1_K_CCBMaskSize];
1148 
1150  OSSIZE bytes;
1151 
1153  int stat;
1154 } ASN1CCB;
1155 
1157 typedef int (*ASN1DumpCbFunc) (const char* text_p, void* cbArg_p);
1158 
1159 
1178 #define ALLOC_ASN1ARRAY(pctxt,pseqof,type) do {\
1179 if (sizeof(type)*(pseqof)->n < (pseqof)->n) return RTERR_NOMEM; \
1180 if (((pseqof)->elem = (type*) rtxMemHeapAllocZ \
1181 (&(pctxt)->pMemHeap, sizeof(type)*(pseqof)->n)) == 0) return RTERR_NOMEM; \
1182 } while (0)
1183 
1196 #define ALLOC_ASN1ARRAY1(pctxt,pseqof,type) do {\
1197 if (sizeof(type)*(pseqof)->n < (pseqof)->n) (pseqof)->elem = 0; \
1198 else (pseqof)->elem = (type*) rtxMemHeapAllocZ \
1199 (&(pctxt)->pMemHeap, sizeof(type)*(pseqof)->n); \
1200 } while (0)
1201 
1206 #define ASN1NUMOCTS(nbits) ((nbits>0)?(((nbits-1)/8)+1):0)
1207 
1208 #ifdef __cplusplus
1209 }
1210 #endif
1211 
1212 #include "rtsrc/rtContext.h"
1213 #include "rtxsrc/rtxCommonDefs.h"
1214 #include "rtxsrc/rtxError.h"
1215 #include "rtxsrc/rtxMemory.h"
1216 
1221 #endif
OSOCTET * mag
Definition: asn1type.h:1108
Definition: asn1type.h:1103
OSOCTET * ptr
Definition: asn1type.h:1138
const char * canonicalSet
Definition: asn1type.h:747
OSUINT32 numids
Definition: asn1type.h:277
int rtMakeGeneralizedTime(OSCTXT *pctxt, const OSNumDateTime *dateTime, char **outdata, size_t outdataSize)
Definition: asn1type.h:686
OSUINT32 numids
Definition: asn1type.h:287
int rtUnivStrToUTF8(OSCTXT *pctxt, const ASN1UniversalString *pUnivStr, OSOCTET *outbuf, size_t outbufsiz)
int rtUTF8StrnToASN1DynBitStr(OSCTXT *pctxt, const OSUTF8CHAR *utf8str, size_t nbytes, ASN1DynBitStr *pvalue)
OSBOOL rtIsIn16BitCharSet(OSUNICHAR ch, Asn116BitCharSet *pCharSet)
void normalizeTimeZone(OSNumDateTime *pvalue)
Definition: asn1type.h:438
OSSIZE bytes
Definition: asn1type.h:1150
unsigned alignedBits
Definition: asn1type.h:787
Definition: asn1type.h:648
Definition: asn1type.h:636
Definition: asn1type.h:727
Definition: asn1type.h:275
OSBOOL dynamic
Definition: asn1type.h:1117
ASN1StrType
Definition: asn1type.h:252
OSUINT32 firstChar
Definition: asn1type.h:799
int rtParseUTCTime(OSCTXT *pctxt, const char *value, OSNumDateTime *dateTime)
struct ASN1BigInt ASN1BigInt
const char * rtUCSToNewCStringEx(OSCTXT *pctxt, ASN1UniversalString *pUCSString)
ASN1UniversalString * rtWCSToUCSString(OSCTXT *pctxt, wchar_t *wcstring, ASN1UniversalString *pUCSString, Asn132BitCharSet *pCharSet)
unsigned unalignedBits
Definition: asn1type.h:802
OSUINT32 n
Definition: asn1type.h:638
OSDynOctStr * elem
Definition: asn1type.h:641
Definition: asn1type.h:737
ASN1UniversalString * rtCToUCSString(OSCTXT *pctxt, const char *cstring, ASN1UniversalString *pUCSString, Asn132BitCharSet *pCharSet)
Definition: asn1type.h:285
int rtUTF8StrToASN1DynBitStr(OSCTXT *pctxt, const OSUTF8CHAR *utf8str, ASN1DynBitStr *pvalue)
Asn132BitCharString charSet
Definition: asn1type.h:796
void rtSetOID(ASN1OBJID *ptarget, ASN1OBJID *psource)
OSUINT32 n
Definition: asn1type.h:625
ASN1BMPString * rtCToBMPString(OSCTXT *pctxt, const char *cstring, ASN1BMPString *pBMPString, Asn116BitCharSet *pCharSet)
unsigned charSetAlignedBits
Definition: asn1type.h:769
int seqx
Definition: asn1type.h:1144
Definition: asn1type.h:776
int sign
Definition: asn1type.h:1111
int rtValidateStr(ASN1TAG tag, const char *pdata)
int canonicalSetSize
Definition: asn1type.h:752
OSUINT16 firstChar
Definition: asn1type.h:781
Definition: asn1type.h:794
size_t allocated
Definition: asn1type.h:1114
unsigned alignedBits
Definition: asn1type.h:805
int rtOIDParseString(const char *oidstr, OSSIZE oidstrlen, ASN1OBJID *pvalue)
const char * rtUCSToNewCString(ASN1UniversalString *pUCSString)
unsigned unalignedBits
Definition: asn1type.h:784
Asn116BitCharString charSet
Definition: asn1type.h:778
OSBOOL rtIsIn32BitCharSet(OS32BITCHAR ch, Asn132BitCharSet *pCharSet)
OSBOOL rtOIDIsValid(const ASN1OBJID *pvalue)
Definition: asn1type.h:623
size_t numocts
Definition: asn1type.h:1105
int rtRelOIDParseString(const char *oidstr, OSSIZE oidstrlen, ASN1OBJID *pvalue)
Definition: asn1type.h:698
#define ASN1_K_CCBMaskSize
Definition: asn1type.h:1121
Definition: asn1type.h:432
int rtParseGeneralizedTime(OSCTXT *pctxt, const char *value, OSNumDateTime *dateTime)
void rtAddOID(ASN1OBJID *ptarget, ASN1OBJID *psource)
unsigned charSetUnalignedBits
Definition: asn1type.h:763
const char * rtBMPToCString(ASN1BMPString *pBMPString, char *cstring, OSSIZE cstrsize)
const char * rtBMPToNewCStringEx(OSCTXT *pctxt, ASN1BMPString *pBMPString)
Definition: asn1type.h:425
unsigned canonicalSetBits
Definition: asn1type.h:757
Definition: asn1type.h:671
const char * rtBMPToNewCString(ASN1BMPString *pBMPString)
long len
Definition: asn1type.h:1141
wchar_t * rtUCSToWCSString(ASN1UniversalString *pUCSString, wchar_t *wcstring, OSUINT32 wcstrsize)
int stat
Definition: asn1type.h:1153
int rtValidateChars(ASN1TAG tag, const char *pdata, size_t nchars)
Definition: rtxContext.h:185
Definition: asn1type.h:1136
OSBOOL rtOIDsEqual(const ASN1OBJID *pOID1, const ASN1OBJID *pOID2)
Definition: asn1type.h:658
void * elem
Definition: asn1type.h:628
const char * rtUCSToCString(ASN1UniversalString *pUCSString, char *cstring, OSSIZE cstrsize)
int(* ASN1DumpCbFunc)(const char *text_p, void *cbArg_p)
Definition: asn1type.h:1157
ASN1ActionType
Definition: asn1type.h:257
int rtMakeUTCTime(OSCTXT *pctxt, const OSNumDateTime *dateTime, char **outdata, size_t outdataSize)
Definition: asn1type.h:412
Asn1CharArray charSet
Definition: asn1type.h:742
int rtOIDParseDottedNumberString(const char *oidstr, OSSIZE oidstrlen, ASN1OBJID *pvalue)