cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
asn1.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * ASN.1 Constants and Structures *
4 * Copyright Peter Gutmann 1992-2007 *
5 * *
6 ****************************************************************************/
7 
8 #ifndef _ASN1_DEFINED
9 
10 #define _ASN1_DEFINED
11 
12 #include <time.h>
13 #if defined( INC_ALL )
14  #include "stream.h"
15 #else
16  #include "io/stream.h"
17 #endif /* Compiler-specific includes */
18 
19 /****************************************************************************
20 * *
21 * BER/DER Constants and Macros *
22 * *
23 ****************************************************************************/
24 
25 /* Definitions for the ISO 8825:1990 Basic Encoding Rules */
26 
27 /* Tag class */
28 
29 #define BER_UNIVERSAL 0x00
30 #define BER_APPLICATION 0x40
31 #define BER_CONTEXT_SPECIFIC 0x80
32 #define BER_PRIVATE 0xC0
33 
34 /* Whether the encoding is constructed or primitive */
35 
36 #define BER_CONSTRUCTED 0x20
37 #define BER_PRIMITIVE 0x00
38 
39 /* The ID's for universal tag numbers 0-31. Tag number 0 is reserved for
40  encoding the end-of-contents value when an indefinite-length encoding
41  is used */
42 
53 
54 /* The encodings for the universal types */
55 
56 #define BER_EOC 0 /* Pseudo-type for first EOC octet */
57 #define BER_RESERVED ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_RESERVED )
58 #define BER_BOOLEAN ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BOOLEAN )
59 #define BER_INTEGER ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_INTEGER )
60 #define BER_BITSTRING ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BITSTRING )
61 #define BER_OCTETSTRING ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OCTETSTRING )
62 #define BER_NULL ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_NULL )
63 #define BER_OBJECT_IDENTIFIER ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OBJECT_IDENTIFIER )
64 #define BER_OBJECT_DESCRIPTOR ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OBJECT_DESCRIPTOR )
65 #define BER_EXTERNAL ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_EXTERNAL )
66 #define BER_REAL ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_REAL )
67 #define BER_ENUMERATED ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_ENUMERATED )
68 #define BER_EMBEDDED_PDV ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_EMBEDDED_PDV )
69 #define BER_STRING_UTF8 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_UTF8 )
70 #define BER_13 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_13 )
71 #define BER_14 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_14 )
72 #define BER_15 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_15 )
73 #define BER_SEQUENCE ( BER_UNIVERSAL | BER_CONSTRUCTED | BER_ID_SEQUENCE )
74 #define BER_SET ( BER_UNIVERSAL | BER_CONSTRUCTED | BER_ID_SET )
75 #define BER_STRING_NUMERIC ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_NUMERIC )
76 #define BER_STRING_PRINTABLE ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_PRINTABLE )
77 #define BER_STRING_T61 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_T61 )
78 #define BER_STRING_VIDEOTEX ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_VIDEOTEX )
79 #define BER_STRING_IA5 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_IA5 )
80 #define BER_TIME_UTC ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_TIME_UTC )
81 #define BER_TIME_GENERALIZED ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_TIME_GENERALIZED )
82 #define BER_STRING_GRAPHIC ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_GRAPHIC )
83 #define BER_STRING_ISO646 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_ISO646 )
84 #define BER_STRING_GENERAL ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_GENERAL )
85 #define BER_STRING_UNIVERSAL ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_UNIVERSAL )
86 #define BER_29 ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BER29 )
87 #define BER_STRING_BMP ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_BMP )
88 
89 /* The encodings for constructed, indefinite-length tags and lengths */
90 
91 #define BER_OCTETSTRING_INDEF MKDATA( "\x24\x80" )
92 #define BER_SEQUENCE_INDEF MKDATA( "\x30\x80" )
93 #define BER_SET_INDEF MKDATA( "\x31\x80" )
94 #define BER_CTAG0_INDEF MKDATA( "\xA0\x80" )
95 #define BER_END_INDEF MKDATA( "\x00\x00" )
96 
97 /* Masks to extract information from a tag number */
98 
99 #define BER_CLASS_MASK 0xC0
100 #define BER_CONSTRUCTED_MASK 0x20
101 #define BER_SHORT_ID_MASK 0x1F
102 
103 /* The maximum value for the short tag encoding, and the magic value which
104  indicates that a long encoding of the number is being used */
105 
106 #define MAX_SHORT_BER_ID ( BER_STRING_BMP + 1 )
107 #define LONG_BER_ID 0x1F
108 
109 /* Turn an identifier into a context-specific tag, and extract the value from
110  a tag. Normally these are constructed, but in a few special cases they
111  are primitive */
112 
113 #define MAKE_CTAG( identifier ) \
114  ( BER_CONTEXT_SPECIFIC | BER_CONSTRUCTED | ( identifier ) )
115 #define MAKE_CTAG_PRIMITIVE( identifier ) \
116  ( BER_CONTEXT_SPECIFIC | ( identifier ) )
117 #define EXTRACT_CTAG( tag ) \
118  ( ( tag ) & ~( BER_CONTEXT_SPECIFIC | BER_CONSTRUCTED ) )
119 
120 /****************************************************************************
121 * *
122 * ASN.1 Constants and Macros *
123 * *
124 ****************************************************************************/
125 
126 /* Special-case tags. If DEFAULT_TAG is given the basic type (e.g. INTEGER,
127  ENUMERATED) is used, otherwise the value is used as a context-specific
128  tag. If NO_TAG is given, processing of the tag is skipped. If ANY_TAG
129  is given, the tag is ignored. The parentheses are to catch potential
130  erroneous use in an expression */
131 
132 #define DEFAULT_TAG ( -1 )
133 #define NO_TAG ( -2 )
134 #define ANY_TAG ( -3 )
135 
136 /* The highest encoded tag value */
137 
138 #define MAX_TAG ( BER_CONTEXT_SPECIFIC | BER_CONSTRUCTED | \
139  MAX_SHORT_BER_ID )
140 
141 /* The highest allowed raw tag value before encoding as a primitive or
142  constructed tag or before encoding as a content-specific tag. In
143  addition to the standard MAX_TAG_VALUE we also have a value for universal
144  tags whose basic form is constructed (SETs and SEQUENCES), which would
145  fall outside the normal MAX_TAG_VALUE range.
146 
147  Due to CMP's braindamaged use of tag values to communicate message type
148  information we have to be fairly permissive with the context-specific
149  tag range because CMP burns up tag values up to the mid-20s, however we
150  can restrict the range if CMP isn't being used */
151 
152 #define MAX_TAG_VALUE MAX_SHORT_BER_ID
153 #define MAX_CONSTR_TAG_VALUE BER_SET
154 #ifdef USE_CMP
155  #define MAX_CTAG_VALUE 30
156 #else
157  #define MAX_CTAG_VALUE 10
158 #endif /* USE_CMP */
159 
160 /* The minimum and maximum allowed size for an (encoded) object identifier */
161 
162 #define MIN_OID_SIZE 5
163 #define MAX_OID_SIZE 32
164 
165 /* When reading an OID selection with readOID() we sometimes need to allow
166  a catch-all default value that's used when nothing else matches. This is
167  typically used for type-and-value data where we want to ignore anything
168  that we don't recognise. The following value is used as a match-all
169  wildcard. It's longer than any normal OID to make it possible to do a
170  quick-reject match based only on the length. The second byte is set to
171  0x0E (= 14) to make the standard sizeofOID() macro work, since this
172  examines the length field of the encoded OID */
173 
174 #define WILDCARD_OID ( const BYTE * ) \
175  "\xFF\x0E\xFF\x00\xFF\x00\xFF\x00\xFF\x00\xFF\x00\xFF\x00\xFF\x00"
176 #define WILDCARD_OID_SIZE 16
177 
178 /* A macro to make make declaring OIDs simpler */
179 
180 #define MKOID( value ) ( ( const BYTE * ) value )
181 
182 /* Macros and functions to work with indefinite-length tags. The only ones
183  used are SEQUENCE and [0] (for the outer encapsulation) and OCTET STRING
184  (for the data itself) */
185 
186 #define writeOctetStringIndef( stream ) swrite( stream, BER_OCTETSTRING_INDEF, 2 )
187 #define writeSequenceIndef( stream ) swrite( stream, BER_SEQUENCE_INDEF, 2 )
188 #define writeSetIndef( stream ) swrite( stream, BER_SET_INDEF, 2 )
189 #define writeCtag0Indef( stream ) swrite( stream, BER_CTAG0_INDEF, 2 )
190 #define writeEndIndef( stream ) swrite( stream, BER_END_INDEF, 2 )
191 
192 #define sizeofEOC() 2
194 int checkEOC( INOUT STREAM *stream );
195 
196 /****************************************************************************
197 * *
198 * ASN.1 Function Prototypes *
199 * *
200 ****************************************************************************/
201 
202 /* Read/peek at a tag and make sure that it's (approximately) valid, and
203  write a tag. The latter translates directly to sputc(), but we use a
204  macro to make explicit what's going on */
205 
206 RETVAL_RANGE( MAX_ERROR, 0xFF ) STDC_NONNULL_ARG( ( 1 ) ) \
207 int readTag( INOUT STREAM *stream );
208 RETVAL_RANGE( MAX_ERROR, 0xFF ) STDC_NONNULL_ARG( ( 1 ) ) \
209 int peekTag( INOUT STREAM *stream );
210 #define writeTag( stream, tag ) sputc( stream, tag )
211 
212 /* Determine the size of an object once it's wrapped up with a tag and
213  length */
214 
215 RETVAL_RANGE( MAX_ERROR, MAX_INTLENGTH ) \
216 long sizeofObject( IN_LENGTH const long length );
217 
218 /* Generalized ASN.1 type manipulation routines. readRawObject() reads a
219  complete object (including tag and length data) while readUniversal()
220  just skips it. Since readRawObject() always requires a tag, we don't
221  have the xxx/xxxData() variants that exist for other functions */
222 
223 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
224 int readUniversalData( INOUT STREAM *stream );
225 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
226 int readUniversal( INOUT STREAM *stream );
227 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
228 int readRawObject( INOUT STREAM *stream,
229  OUT_BUFFER( bufferMaxLength, *bufferLength ) BYTE *buffer,
230  IN_LENGTH_SHORT_MIN( 3 ) const int bufferMaxLength,
232  IN_TAG_ENCODED const int tag );
233 
234 #define writeRawObject( stream, object, size ) \
235  swrite( stream, object, size )
236 
237 /* Routines for handling OBJECT IDENTIFIERS. The sizeof() macro determines
238  the length of an encoded object identifier as tag + length + value.
239  Write OID routines equivalent to the ones for other ASN.1 types don't
240  exist since OIDs are always read and written as a blob with sread()/
241  swrite(). OIDs are never tagged so we don't need any special-case
242  handling for tags.
243 
244  When there's a choice of possible OIDs, the list of OID values and
245  corresponding selection IDs is provided in an OID_INFO structure (we also
246  provide a shortcut readFixedOID() function when there's only a single OID
247  that's valid at that point). The read OID value is checked against each
248  OID in the OID_INFO list, if a match is found the selectionID is returned.
249 
250  The OID_INFO includes a pointer to further user-supplied information
251  related to this OID that may be used by the user, set when the OID list
252  is initialised. For example it could point to OID-specific handlers for
253  the data. When the caller needs to work with the extraInfo field, it's
254  necessary to return the complete OID_INFO entry rather than just the
255  selection ID, which is done by the ..Ex() form of the function */
256 
257 typedef struct {
258  const BYTE FAR_BSS *oid;/* OID */
259  const int selectionID; /* Value to return for this OID */
260  const void *extraInfo; /* Additional info for this selection */
261  } OID_INFO;
262 
263 #define sizeofOID( oid ) ( 1 + 1 + ( int ) oid[ 1 ] )
264 RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
265 int readOID( INOUT STREAM *stream,
266  IN_ARRAY( noOidSelectionEntries ) \
267  const OID_INFO *oidSelection,
268  IN_RANGE( 1, 50 ) const int noOidSelectionEntries,
269  OUT_RANGE( CRYPT_ERROR, noOidSelectionEntries ) \
270  int *selectionID );
271 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
272 int readOIDEx( INOUT STREAM *stream,
273  IN_ARRAY( noOidSelectionEntries ) \
274  const OID_INFO *oidSelection,
275  IN_RANGE( 1, 50 ) const int noOidSelectionEntries,
277 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
278 int readFixedOID( INOUT STREAM *stream,
279  IN_BUFFER( oidLength ) \
280  const BYTE *oid, IN_LENGTH_OID const int oidLength );
281 RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
282 int readEncodedOID( INOUT STREAM *stream,
283  OUT_BUFFER( oidMaxLength, *oidLength ) BYTE *oid,
284  IN_LENGTH_SHORT_MIN( 5 ) const int oidMaxLength,
286  IN_TAG_ENCODED const int tag );
287 #define writeOID( stream, oid ) \
288  swrite( ( stream ), ( oid ), sizeofOID( oid ) )
289 
290 /* Routines for handling large integers. When we're writing these we can't
291  use sizeofObject() directly because the internal representation is
292  unsigned whereas the encoded form is signed. The following macro performs
293  the appropriate conversion on the data length before passing it on to
294  sizeofObject() */
295 
296 #define sizeofInteger( value, valueLength ) \
297  ( int ) sizeofObject( ( valueLength ) + \
298  ( ( *( BYTE * )( value ) & 0x80 ) ? 1 : 0 ) )
299 
300 RETVAL STDC_NONNULL_ARG( ( 1, 4 ) ) \
301 int readIntegerTag( INOUT STREAM *stream,
303  BYTE *integer,
306  IN_TAG_EXT const int tag );
307 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
308 int writeInteger( INOUT STREAM *stream,
310  const BYTE *integer,
311  IN_LENGTH_SHORT const int integerLength,
312  IN_TAG const int tag );
313 
314 #define readIntegerData( stream, integer, integerLength, maxLength ) \
315  readIntegerTag( stream, integer, integerLength, maxLength, NO_TAG )
316 #define readInteger( stream, integer, integerLength, maxLength ) \
317  readIntegerTag( stream, integer, integerLength, maxLength, DEFAULT_TAG )
318 
319 /* Routines for handling bignums. We use void * rather than BIGNUM * to save
320  having to include the bignum header everywhere where ASN.1 is used */
321 
322 #define sizeofBignum( bignum ) \
323  ( ( int ) sizeofObject( signedBignumSize( bignum ) ) )
324 
325 RETVAL_RANGE( MAX_ERROR, MAX_INTLENGTH_SHORT ) STDC_NONNULL_ARG( ( 1 ) ) \
326 int signedBignumSize( IN TYPECAST( BIGNUM * ) const void *bignum );
327 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
328 int readBignumTag( INOUT STREAM *stream, INOUT void *bignum,
331  IN_OPT const void *maxRange,
332  IN_TAG_EXT const int tag );
333 int writeBignumTag( INOUT STREAM *stream, const void *bignum,
334  const int tag ) \
335  STDC_NONNULL_ARG( ( 1, 2 ) );
336 
337 #define readBignum( stream, bignum, minLen, maxLen, maxRange ) \
338  readBignumTag( stream, bignum, minLen, maxLen, maxRange, DEFAULT_TAG )
339 #define writeBignum( stream, bignum ) \
340  writeBignumTag( stream, bignum, DEFAULT_TAG )
341 
342 /* Special-case bignum read routine that explicitly checks for a too-short
343  key and returns CRYPT_ERROR_NOSECURE rather than the CRYPT_ERROR_BADDATA
344  that'd otherwise be returned */
345 
346 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
347 int readBignumChecked( INOUT STREAM *stream,
348  INOUT TYPECAST( BIGNUM * ) void *bignum,
349  IN_LENGTH_PKC const int minLength,
350  IN_LENGTH_PKC const int maxLength,
351  IN_OPT const void *maxRange );
352 
353 /* Generally most integers will be non-bignum values, so we also define
354  routines to handle values that will fit into a machine word */
355 
356 #define sizeofShortInteger( value ) \
357  ( ( ( value ) < 0x80 ) ? 3 : \
358  ( ( ( long ) value ) < 0x8000L ) ? 4 : \
359  ( ( ( long ) value ) < 0x800000L ) ? 5 : \
360  ( ( ( long ) value ) < 0x80000000UL ) ? 6 : 7 )
361 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
362 int writeShortInteger( INOUT STREAM *stream,
363  IN_INT const long integer,
364  IN_TAG const int tag );
365 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
366 int readShortIntegerTag( INOUT STREAM *stream,
368  IN_TAG_EXT const int tag );
369 
370 #define readShortIntegerData( stream, integer ) \
371  readShortIntegerTag( stream, integer, NO_TAG )
372 #define readShortInteger( stream, integer ) \
373  readShortIntegerTag( stream, integer, DEFAULT_TAG )
374 
375 /* Routines for handling enumerations */
376 
377 #define sizeofEnumerated( value ) ( ( ( value ) < 128 ) ? 3 : 4 )
378 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
379 int writeEnumerated( INOUT STREAM *stream,
380  IN_RANGE( 0, 999 ) const int enumerated,
381  IN_TAG const int tag );
382 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
383 int readEnumeratedTag( INOUT STREAM *stream,
385  IN_TAG_EXT const int tag );
386 
387 #define readEnumeratedData( stream, enumeration ) \
388  readEnumeratedTag( stream, enumeration, NO_TAG )
389 #define readEnumerated( stream, enumeration ) \
390  readEnumeratedTag( stream, enumeration, DEFAULT_TAG )
391 
392 /* Routines for handling booleans */
393 
394 #define sizeofBoolean() ( sizeof( BYTE ) + sizeof( BYTE ) + sizeof( BYTE ) )
395 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
396 int writeBoolean( INOUT STREAM *stream, const BOOLEAN boolean,
397  IN_TAG const int tag );
398 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
399 int readBooleanTag( INOUT STREAM *stream,
400  OUT_OPT_BOOL BOOLEAN *boolean,
401  IN_TAG_EXT const int tag );
402 
403 #define readBooleanData( stream, boolean ) \
404  readBooleanTag( stream, boolean, NO_TAG )
405 #define readBoolean( stream, boolean ) \
406  readBooleanTag( stream, boolean, DEFAULT_TAG )
407 
408 /* Routines for handling null values */
409 
410 #define sizeofNull() ( sizeof( BYTE ) + sizeof( BYTE ) )
411 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
412 int writeNull( INOUT STREAM *stream, IN_TAG const int tag );
413 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
414 int readNullTag( INOUT STREAM *stream, IN_TAG_EXT const int tag );
415 
416 #define readNullData( stream ) readNullTag( stream, NO_TAG )
417 #define readNull( stream ) readNullTag( stream, DEFAULT_TAG )
418 
419 /* Routines for handling octet strings */
420 
421 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
422 int writeOctetString( INOUT STREAM *stream,
423  IN_BUFFER( length ) \
424  const BYTE *string,
426  IN_TAG const int tag );
427 RETVAL STDC_NONNULL_ARG( ( 1, 2, 3 ) ) \
428 int readOctetStringTag( INOUT STREAM *stream,
429  OUT_BUFFER( maxLength, *stringLength ) \
431  IN_LENGTH_SHORT const int minLength,
432  IN_LENGTH_SHORT const int maxLength,
433  IN_TAG_EXT const int tag );
434 
435 #define readOctetStringData( stream, string, stringLength, minLength, maxLength ) \
436  readOctetStringTag( stream, string, stringLength, minLength, maxLength, NO_TAG )
437 #define readOctetString( stream, string, stringLength, minLength, maxLength ) \
438  readOctetStringTag( stream, string, stringLength, minLength, maxLength, DEFAULT_TAG )
439 
440 /* Routines for handling character strings. There are a number of oddball
441  character string types that are all handled through the same functions -
442  it's not worth having a seperate function to handle each of the half-dozen
443  types */
444 
445 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
446 int writeCharacterString( INOUT STREAM *stream,
447  IN_BUFFER( length ) const void *string,
448  IN_LENGTH_SHORT const int length,
449  IN_TAG_ENCODED const int tag );
450 RETVAL STDC_NONNULL_ARG( ( 1, 2, 4 ) ) \
451 int readCharacterString( INOUT STREAM *stream,
452  OUT_BUFFER( stringMaxLength, *stringLength ) void *string,
455  IN_TAG_EXT const int tag );
456 
457 /* Routines for handling bit strings. The sizeof() values are 3 bytes for
458  the tag, length, and surplus-bits value, and the data itself */
459 
460 #define sizeofBitString( value ) \
461  ( 3 + ( ( ( ( long ) value ) > 0xFFFFFFL ) ? 4 : \
462  ( ( ( long ) value ) > 0xFFFFL ) ? 3 : \
463  ( ( value ) > 0xFF ) ? 2 : ( value ) ? 1 : 0 ) )
464 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
465 int writeBitString( INOUT STREAM *stream,
466  IN_INT const int bitString,
467  IN_TAG const int tag );
468 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
469 int readBitStringTag( INOUT STREAM *stream,
470  OUT_OPT_INT_Z int *bitString,
471  IN_TAG_EXT const int tag );
472 
473 #define readBitStringData( stream, bitString ) \
474  readBitStringTag( stream, bitString, NO_TAG )
475 #define readBitString( stream, bitString ) \
476  readBitStringTag( stream, bitString, DEFAULT_TAG )
477 
478 /* Routines for handling UTC and Generalized time */
479 
480 #define sizeofUTCTime() ( 1 + 1 + 13 )
481 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
482 int writeUTCTime( INOUT STREAM *stream, const time_t timeVal,
483  IN_TAG const int tag );
484 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
485 int readUTCTimeTag( INOUT STREAM *stream, OUT time_t *timeVal,
486  IN_TAG_EXT const int tag );
487 
488 #define readUTCTimeData( stream, time ) readUTCTimeTag( stream, time, NO_TAG )
489 #define readUTCTime( stream, time ) readUTCTimeTag( stream, time, DEFAULT_TAG )
490 
491 #define sizeofGeneralizedTime() ( 1 + 1 + 15 )
492 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
493 int writeGeneralizedTime( INOUT STREAM *stream, const time_t timeVal,
494  IN_TAG const int tag );
495 RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
496 int readGeneralizedTimeTag( INOUT STREAM *stream, OUT time_t *timeVal,
497  IN_TAG_EXT const int tag );
498 
499 #define readGeneralizedTimeData( stream, time ) \
500  readGeneralizedTimeTag( stream, time, NO_TAG )
501 #define readGeneralizedTime( stream, time ) \
502  readGeneralizedTimeTag( stream, time, DEFAULT_TAG )
503 
504 /* Utilitity routines for reading and writing constructed objects and
505  equivalent holes. The difference between writeOctet/BitStringHole() and
506  writeGenericHole() is that the octet/bit-string versions create a normal
507  or context-specific-tagged string while the generic version creates a
508  pure hole with no processing of tags */
509 
510 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
511 int readSequence( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length );
512 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
513 int readSet( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length );
514 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
515 int readConstructed( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length,
516  IN_TAG const int tag );
517 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
518 int readOctetStringHole( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length,
519  IN_LENGTH_SHORT const int minLength,
520  IN_TAG const int tag );
521 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
522 int readBitStringHole( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length,
523  IN_LENGTH_SHORT const int minLength,
524  IN_TAG const int tag );
525 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
526 int readGenericHole( INOUT STREAM *stream, OUT_OPT_LENGTH_Z int *length,
527  IN_LENGTH_SHORT const int minLength,
528  IN_TAG const int tag );
529 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
530 int writeSequence( INOUT STREAM *stream, IN_LENGTH_Z const int length );
531 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
532 int writeSet( INOUT STREAM *stream, IN_LENGTH_Z const int length );
533 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
534 int writeConstructed( INOUT STREAM *stream, IN_LENGTH_Z const int length,
535  IN_TAG const int tag );
536 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
537 int writeOctetStringHole( INOUT STREAM *stream,
538  IN_LENGTH_Z const int length,
539  IN_TAG const int tag );
540 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
541 int writeBitStringHole( INOUT STREAM *stream, IN_LENGTH_Z const int length,
542  IN_TAG const int tag );
543 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
544 int writeGenericHole( INOUT STREAM *stream, IN_LENGTH_Z const int length,
545  IN_TAG const int tag );
546 
547 /* Alternative versions of the above that allow indefinite lengths. This
548  (non-DER) behaviour is the exception rather than the rule, so we have to
549  enable it explicitly */
550 
551 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
552 int readSequenceI( INOUT STREAM *stream,
554 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
555 int readSetI( INOUT STREAM *stream, OUT_OPT_LENGTH_INDEF int *length );
556 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
557 int readConstructedI( INOUT STREAM *stream, OUT_OPT_LENGTH_INDEF int *length,
558  IN_TAG const int tag );
559 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
560 int readGenericHoleI( INOUT STREAM *stream,
562  IN_LENGTH_SHORT const int minLength,
563  IN_TAG const int tag );
564 
565 /* Read a generic object header, used to find the length of an object being
566  read as a blob */
567 
568 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
569 int readGenericObjectHeader( INOUT STREAM *stream,
571  const BOOLEAN isLongObject );
572 
573 /* Read an arbitrary-length constructed object's data into a memory buffer.
574  This is the arbitrary-length form of readRawObject() */
575 
576 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 3 ) ) \
577 int readRawObjectAlloc( INOUT STREAM *stream,
580  IN_LENGTH_SHORT_MIN( 16 ) const int minLength,
581  IN_LENGTH_SHORT const int maxLength );
582 
583 /* Determine the length of an ASN.1-encoded object (this just reads the
584  outer length if present, but will burrow down into the object if necessary
585  if the length is indefinite) and check that an object has valid encoding */
586 
587 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
588 int getStreamObjectLength( INOUT STREAM *stream, OUT_LENGTH_Z int *length );
589 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
590 int getObjectLength( IN_BUFFER( objectLength ) \
591  const void *objectPtr,
593  OUT_LENGTH_Z int *length );
595 int checkObjectEncoding( IN_BUFFER( objectLength ) const void *objectPtr,
596  IN_LENGTH const int objectLength );
597 
598 /* Full-length equivalents of length/encapsulating-object read routines.
599  These are used explicitly in the rare situations where long lengths are
600  valid, all other ASN.1 code only works with short lengths. Because these
601  can be quite long, they allow definite or indefinite lengths */
602 
603 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
604 int readLongSequence( INOUT STREAM *stream,
606 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
607 int readLongSet( INOUT STREAM *stream, OUT_OPT_LENGTH_INDEF long *length );
608 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
609 int readLongConstructed( INOUT STREAM *stream,
611  IN_TAG const int tag );
612 RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
613 int readLongGenericHole( INOUT STREAM *stream,
615  IN_TAG const int tag );
616 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
617 int getLongStreamObjectLength( INOUT STREAM *stream,
618  OUT_LENGTH_Z long *length );
619 CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
620 int getLongObjectLength( IN_BUFFER( objectLength ) const void *objectPtr,
621  IN_LENGTH const long objectLength,
622  OUT_LENGTH_Z long *length );
623 #endif /* !_ASN1_DEFINED */