home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / KEYMGMT / BER.H < prev    next >
Text File  |  1996-09-25  |  7KB  |  148 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                ASN.1 Basic Encoding Rules Constants and Structures            *
  4. *                        Copyright Peter Gutmann 1992-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. #ifndef _BER_DEFINED
  9.  
  10. #define _BER_DEFINED
  11.  
  12. /* Definitions for the ISO 8825:1990 Basic Encoding Rules */
  13.  
  14. /* Tag class */
  15.  
  16. #define BER_UNIVERSAL            0x00
  17. #define BER_APPLICATION            0x40
  18. #define BER_CONTEXT_SPECIFIC    0x80
  19. #define BER_PRIVATE                0xC0
  20.  
  21. /* Whether the encoding is constructed or primitive */
  22.  
  23. #define BER_CONSTRUCTED            0x20
  24. #define BER_PRIMITIVE            0x00
  25.  
  26. /* The ID's for universal tag numbers 0-31.  Tag number 0 is reserved for
  27.    encoding the end-of-contents value when an indefinite-length encoding
  28.    is used */
  29.  
  30. enum { BER_ID_RESERVED, BER_ID_BOOLEAN, BER_ID_INTEGER, BER_ID_BITSTRING,
  31.        BER_ID_OCTETSTRING, BER_ID_NULL, BER_ID_OBJECT_IDENTIFIER,
  32.        BER_ID_OBJECT_DESCRIPTOR, BER_ID_EXTERNAL, BER_ID_REAL,
  33.        BER_ID_ENUMERATED, BER_EMBEDDED_PDV, BER_ID_12, BER_ID_13, BER_ID_14,
  34.        BER_ID_15, BER_ID_SEQUENCE, BER_ID_SET, BER_ID_STRING_NUMERIC,
  35.        BER_ID_STRING_PRINTABLE, BER_ID_STRING_T61, BER_ID_STRING_VIDEOTEX,
  36.        BER_ID_STRING_IA5, BER_ID_TIME_UTC, BER_ID_TIME_GENERALIZED,
  37.        BER_ID_STRING_GRAPHIC, BER_ID_STRING_ISO646, BER_ID_STRING_GENERAL,
  38.        BER_ID_STRING_UNIVERSAL, BER_ID_29, BER_ID_STRING_BMP };
  39.  
  40. /* The encodings for the universal types */
  41.  
  42. #define BER_RESERVED        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_RESERVED )
  43. #define BER_BOOLEAN            ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BOOLEAN )
  44. #define BER_INTEGER            ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_INTEGER )
  45. #define BER_BITSTRING        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BITSTRING )
  46. #define BER_OCTETSTRING        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OCTETSTRING )
  47. #define BER_NULL            ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_NULL )
  48. #define BER_OBJECT_IDENTIFIER    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OBJECT_IDENTIFIER )
  49. #define BER_OBJECT_DESCRIPTOR    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_OBJECT_DESCRIPTOR )
  50. #define BER_EXTERNAL        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_EXTERNAL )
  51. #define BER_REAL            ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_REAL )
  52. #define BER_ENUMERATED        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_ENUMERATED )
  53. #define BER_EMBEDDED_PDV    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_EMBEDDED_PDV )
  54. #define BER_12                ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_12 )
  55. #define BER_13                ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_13 )
  56. #define BER_14                ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_14 )
  57. #define BER_15                ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_15 )
  58. #define BER_SEQUENCE        ( BER_UNIVERSAL | BER_CONSTRUCTED | BER_ID_SEQUENCE )
  59. #define BER_SET                ( BER_UNIVERSAL | BER_CONSTRUCTED | BER_ID_SET )
  60. #define BER_STRING_NUMERIC    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_NUMERIC )
  61. #define BER_STRING_PRINTABLE    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_PRINTABLE )
  62. #define BER_STRING_T61        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_T61 )
  63. #define BER_STRING_VIDEOTEX    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_VIDEOTEX )
  64. #define BER_STRING_IA5        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_IA5 )
  65. #define BER_TIME_UTC        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_TIME_UTC )
  66. #define BER_TIME_GENERALIZED    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_TIME_GENERALIZED )
  67. #define BER_STRING_GRAPHIC    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_GRAPHIC )
  68. #define BER_STRING_ISO646    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_ISO646 )
  69. #define BER_STRING_GENERAL    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_GENERAL )
  70. #define BER_STRING_UNIVERSAL    ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_UNIVERSAL )
  71. #define BER_29                ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_BER29 )
  72. #define BER_STRING_BMP        ( BER_UNIVERSAL | BER_PRIMITIVE | BER_ID_STRING_BMP )
  73.  
  74. /* ID's for application-specific tags */
  75.  
  76. enum { BER_APPL_ENCRYPTED_KEY, BER_APPL_PKCENCRYPTED_KEY, BER_APPL_SIGNATURE,
  77.        BER_APPL_ENCRYPTED_DATA, BER_APPL_COMPRESSED_DATA,
  78.        BER_APPL_SIGNED_DATA, BER_APPL_RAW_DATA, BER_APPL_NONDATA };
  79.  
  80. /* The encodings for the application-specific types */
  81.  
  82. #define BER_ENCRYPTED_KEY    ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_ENCRYPTED_KEY )
  83. #define BER_PKCENCRYPTED_KEY ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_PKCENCRYPTED_KEY )
  84. #define BER_SIGNATURE        ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_SIGNATURE )
  85. #define BER_ENCRYPTED_DATA    ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_ENCRYPTED_DATA )
  86. #define BER_COMPRESSED_DATA    ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_COMPRESSED_DATA )
  87. #define BER_SIGNED_DATA        ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_SIGNED_DATA )
  88. #define BER_RAW_DATA        ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_RAW_DATA )
  89. #define BER_NONDATA            ( BER_APPLICATION | BER_CONSTRUCTED | BER_APPL_NONDATA )
  90.  
  91. /* Masks to extract information from a tag number */
  92.  
  93. #define BER_CLASS_MASK            0xC0
  94. #define BER_CONSTRUCTED_MASK    0x20
  95. #define BER_SHORT_ID_MASK        0x1F
  96.  
  97. /* The maximum size for the short tag number encoding, and the magic value
  98.    which indicates that a long encoding of the number is being used */
  99.  
  100. #define MAX_SHORT_BER_ID    30
  101. #define LONG_BER_ID            0x1F
  102.  
  103. /* A struct to store information about a tag */
  104.  
  105. typedef struct {
  106.                int class;                /* Tag class */
  107.                BOOLEAN constructed;        /* Constructed or primitive */
  108.                int identifier;            /* Tag type */
  109.                long length;                /* Tag length */
  110.                } BER_TAGINFO;
  111.  
  112. /* Macros to read and write primitive tags.  These translate directly to
  113.    sgetc()/sputc(), but we use these macros instead to make it more obvious
  114.    what's going on */
  115.  
  116. #define writeTag( stream, tag )    sputc( stream, tag )
  117. #define readTag( stream )        sgetc( stream )
  118.  
  119. /* Helper routines needed by code performing low-level ASN.1 en/decoding
  120.    operations.  These should only be used by libraries which handle ASN.1
  121.    primitives, not called from user code */
  122.  
  123. void writeCtag( STREAM *stream, const int identifier,
  124.                const BOOLEAN isConstructed );
  125. int checkReadTag( STREAM *stream, const int tag );
  126. int checkReadCtag( STREAM *stream, const int identifier,
  127.                    const BOOLEAN isConstructed );
  128. int calculateLengthSize( const long length );
  129. int readRawObject( STREAM *stream, BYTE *buffer, int *bufferLength,
  130.                    const int maxLength, const int tag );
  131. int readStaticInteger( STREAM *stream, BYTE *integer, int *integerLength,
  132.                        const int maxLength );
  133. int writeStaticInteger( STREAM *stream, const BYTE *integer,
  134.                         const int integerLength, const int tag );
  135. int _readStaticOctetString( STREAM *stream, BYTE *string, int *stringLength,
  136.                             const int maxLength, const BOOLEAN readIdent );
  137. int readStaticTextString( STREAM *stream, char *string, const int maxLength,
  138.                           const BOOLEAN readIdent );
  139.  
  140. #define readStaticOctetString( stream, string, stringLength, maxLength ) \
  141.         _readStaticOctetString( stream, string, stringLength, maxLength, TRUE )
  142. #define readStaticOctetStringData( stream, string, stringLength, maxLength ) \
  143.         _readStaticOctetString( stream, string, stringLength, maxLength, FALSE )
  144.  
  145. #define writeRawObject( stream, object, size ) \
  146.         swrite( stream, object, size )
  147. #endif /* !_BER_DEFINED */
  148.