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

  1. /****************************************************************************
  2. *                                                                            *
  3. *                          ASN.1 Constants and Structures                    *
  4. *                        Copyright Peter Gutmann 1992-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. #ifndef _ASN1_DEFINED
  9.  
  10. #define _ASN1_DEFINED
  11.  
  12. #include <time.h>
  13. #if defined( INC_CHILD )|| defined( INC_ALL )
  14.   #include "stream.h"
  15.   #include "ber.h"
  16. #else
  17.   #include "keymgmt/stream.h"
  18.   #include "keymgmt/ber.h"
  19. #endif /* Compiler-specific includes */
  20.  
  21. /* Data structures and routines for handling ISO 8824:1990 ASN.1 data types.
  22.    We use the types BOOLEAN (defined in "defs.h"), INTEGER, OCTET STRING,
  23.    BITSTRING, ENUMERATION, NULL, and various constructed types such as OBJECT
  24.    IDENTIFIER and SEQUENCE which are handled by the ASN.1 I/O routines.
  25.  
  26.    Each of the structures below act somewhat like a C++ object - their
  27.    constructors must be called before use, and their destructors should
  28.    be called after use.  In addition they can be queried as to their encoded
  29.    size, and written to and read from disk.  The methods are:
  30.  
  31.    new<objectName>();        // Constructor
  32.    delete<objectName>();    // Destructor
  33.    assign<objectName>();    // Assign value to object
  34.    sizeof<objectName>();    // Find encoded size of object
  35.    read<objectName>();        // Read object
  36.    read<objectName>Data();    // Read object's data
  37.    write<objectName>();        // Write object
  38.  
  39.    The read() calls actually have two types, the read() form which reads
  40.    the basic ASN.1 object type and checks the tag, and the more general
  41.    readData() form which only reads the data contained in the ASN.1 object
  42.    and doesn't read the tag.
  43.  
  44.    Finally, many of the data types are represented only indirectly, or don't
  45.    have new/delete/assign methods.  This is because they are too general to
  46.    be useful (for example the OCTET STRING code is never used, but there are
  47.    two or three functions to read and write size-limited byte strings which
  48.    are much more practical).
  49.  
  50.    Some ASN.1 structures are unused, either because they aren't needed or
  51.    because they're impractical.  These are made conditional by the following
  52.    define.  Uncommenting it will make these routines available for use (as
  53.    well as increasing the total code size somewhat) */
  54.  
  55. /* #define STRICT_ASN1 */
  56.  
  57. /****************************************************************************
  58. *                                                                            *
  59. *                            Universal ASN.1 Structures                        *
  60. *                                                                            *
  61. ****************************************************************************/
  62.  
  63. /* ASN.1 types.  These include dummy values for currently undefined types */
  64.  
  65. typedef enum {
  66.     ASN1_TYPE_RESERVED, ASN1_TYPE_BOOLEAN, ASN1_TYPE_INTEGER,
  67.     ASN1_TYPE_BITSTRING, ASN1_TYPE_OCTETSTRING, ASN1_TYPE_NULL,
  68.     ASN1_TYPE_OBJECT_IDENTIFIER, ASN1_TYPE_OBJECT_DESCRIPTOR,
  69.     ASN1_TYPE_EXTERNAL, ASN1_TYPE_REAL, ASN1_TYPE_ENUMERATED, ASN1_TYPE_11,
  70.     ASN1_TYPE_12, ASN1_TYPE_13, ASN1_TYPE_14, ASN1_TYPE_15,
  71.     ASN1_TYPE_SEQUENCE, ASN1_TYPE_SET, ASN1_TYPE_STRING_NUMERIC,
  72.     ASN1_TYPE_STRING_PRINTABLE, ASN1_TYPE_STRING_T61,
  73.     ASN1_TYPE_STRING_VIDEOTEX, ASN1_TYPE_STRING_IA5, ASN1_TYPE_TIME_UTC,
  74.     ASN1_TYPE_TIME_GENERALIZED, ASN1_TYPE_STRING_GRAPHIC,
  75.     ASN1_TYPE_STRING_ISO646, ASN1_TYPE_STRING_GENERAL, ASN1_TYPE_28,
  76.     ASN1_TYPE_29, ASN1_TYPE_30
  77.     } ASN1_TYPE;
  78.  
  79. /* The default value for tagged types.  If this value is given the basic
  80.    type is used, otherwise the value is used as a context-specific tag */
  81.  
  82. #define DEFAULT_TAG            -1
  83.  
  84. /* The ASN.1 INTEGER type.  In most cases this will fit into a machine
  85.    word, so we use a variant record which contains the integer in this
  86.    form, but which can also store the value as an array of bytes, in little-
  87.    endian format */
  88.  
  89. typedef struct {
  90.     /* An integer value which will fit into a machine word */
  91.     long shortInteger;
  92.  
  93.     /* A worst-case true ASN.1 integer value.  Precision is set to zero if
  94.        it'll fit into a longint */
  95.     int precision;
  96.     BYTE *longInteger;
  97.     } INTEGER;
  98.  
  99. #ifdef STRICT_ASN1
  100.  
  101. /* The ASN.1 OCTET STRING type */
  102.  
  103. typedef struct {
  104.     BYTE *string;                /* The octet string itself */
  105.     int length;                    /* The length of the string */
  106.     } OCTETSTRING;
  107.  
  108. /* The ASN.1 OBJECT IDENTIFIER type */
  109.  
  110. typedef struct {
  111.     int noFields;                /* Number of fields in object ID */
  112.     int root;                    /* ID of root node */
  113.     int type;                    /* ID of type */
  114.     long ident;                    /* Main identifier */
  115.     long subIdent1, subIdent2, subIdent3;    /* Subidentifiers */
  116.     } OBJECT_IDENTIFIER;
  117.  
  118. /* Values for object identifier root node and type */
  119.  
  120. enum { OBJECT_ID_CCITT, OBJECT_ID_ISO, OBJECT_ID_JOINT_ISO_CCITT };
  121. enum { OBJECT_ID_STANDARD, OBJECT_ID_REGISTRATION_AUTHORITY,
  122.        OBJECT_ID_MEMBER_BODY, OBJECT_ID_IDENTIFIED_ORGANISATION };
  123.  
  124. /* The ASN.1 GENERALIZED TIME type */
  125.  
  126. typedef struct {
  127.     int years, months, days;    /* Years, months, days */
  128.     int hours, minutes, seconds;/* Hours, minutes, seconds */
  129.     int fractional;                /* Fractional seconds */
  130.     int timeDiff;                /* Offset from UTC time in hours */
  131.     } GENERALIZED_TIME;
  132.  
  133. #endif /* STRICT_ASN1 */
  134.  
  135. /****************************************************************************
  136. *                                                                            *
  137. *            Constants and Structures for Useful ASN.1 Data Types            *
  138. *                                                                            *
  139. ****************************************************************************/
  140.  
  141. /* The types of strings we may use */
  142.  
  143. typedef enum {
  144.     /* String non-type (generally used for unknown string types */
  145.     STRINGTYPE_NONE,        /* String non-type */
  146.  
  147.     /* Standard ASN.1 string types */
  148.     STRINGTYPE_ISO646,        /* ISO 646/ASCII */
  149.     STRINGTYPE_ISO10646,    /* ISO 10646/Unicode Level 5 compression */
  150.  
  151.     /* Single-byte ISO string types */
  152.     STRINGTYPE_ISO8859_1,    /* ISO 8859-1/latin1, West European languages */
  153.     STRINGTYPE_ISO8859_2,    /* ISO 8859-2/latin2, East European languages */
  154.     STRINGTYPE_ISO8859_3,    /* ISO 8859-3/latin3, Other latin languages */
  155.     STRINGTYPE_ISO8859_4,    /* ISO 8859-4/latin4, North European languages */
  156.     STRINGTYPE_ISO8859_5,    /* ISO 8859-5 Cyrillic */
  157.     STRINGTYPE_ISO8859_6,    /* ISO 8859-6 Arabic */
  158.     STRINGTYPE_ISO8859_7,    /* ISO 8859-7 Greek */
  159.     STRINGTYPE_ISO8859_8,    /* ISO 8859-8 Hebrew */
  160.     STRINGTYPE_ISO8859_9,    /* ISO 8859-9/latin5, Latin 1 modification for Turkey */
  161.     STRINGTYPE_ISO8859_10,    /* ISO 8859-10/latin6, Scandinvian languages */
  162.  
  163.     /* Single-byte non-ISO string types */
  164.     STRINGTYPE_KOI8,        /* Russian KOI8 (Unix and derivatives) */
  165.     STRINGTYPE_ALTKOI8,        /* Alternative KOI8/MSDOS CP866 */
  166.     STRINGTYPE_MAZOVIA,        /* Polish Mazovia (preferred to 8859-2 in Poland */
  167.     STRINGTYPE_EBCDIC,        /* IBM 037 EBCDIC (NB: Several EBCDIC types) */
  168.  
  169.     /* Multibyte nonstandard string types */
  170.     STRINGTYPE_JIS,            /* Japanese JIS (NB: Several types. Use EUC-J??) */
  171.     STRINGTYPE_SHIFT_JIS,    /* Japanese (Unix and PC) */
  172.     STRINGTYPE_EUC_C,        /* Chinese (PRC). Guo-Biao (GB2312-80) is EUC-C codeset 1 */
  173.     STRINGTYPE_EUC_H,        /* Han (Taiwanese). Big5 is EUC-H codeset 1 */
  174.     STRINGTYPE_EUC_J,        /* Japanese (Unix) */
  175.     STRINGTYPE_EUC_K,        /* Korean */
  176.  
  177.     /* Unicode/ISO10646 subtypes */
  178.     STRINGTYPE_UTF_1,        /* UCS Translation Format 1: 1, 2, 3, 5 bytes/char */
  179.     STRINGTYPE_UTF_2        /* UCS Translation Format 2: 1-6 bytes/char */
  180.     } STRINGTYPE;
  181.  
  182. /* The types of currencies we may use */
  183.  
  184. typedef enum {
  185.     /* Currency non-type (generally used for unknown currency types */
  186.     CURRENCYTYPE_NONE,        /* Currency non-type */
  187.  
  188.     /* Common currencies */
  189.     CURRENCYTYPE_USD        /* US dollars */
  190.     } CURRENCYTYPE;
  191.  
  192. /* The ASN.1 GENERALIZED_STRING type.  This is much more useful than the
  193.    strange and bizarre standard ASN.1 string types */
  194.  
  195. typedef struct {
  196.     STRINGTYPE type;        /* The string type */
  197.     int length;                /* Length of string */
  198.     BYTE *string;            /* The string itself */
  199.     } GENERALIZED_STRING;
  200.  
  201. /* The ASN.1 TIME type.  Again, this is preferred to the ISO 8601-compliant
  202.    standard ASN.1 time type */
  203.  
  204. typedef struct {
  205.     time_t seconds;            /* Seconds since 1970 */
  206.     time_t fractional;        /* Fractional seconds */
  207.     } TIME;
  208.  
  209. /* The ASN.1 MONETARY_AMOUNT type.  There is no ASN.1 type that this is
  210.    better than */
  211.  
  212. typedef struct {
  213.     CURRENCYTYPE currency;    /* Currency type */
  214.     long units;                /* Currency units */
  215.     long fractional;        /* Fractional currency seconds */
  216.     } MONETARY_AMOUNT;
  217.  
  218. /****************************************************************************
  219. *                                                                            *
  220. *                            ASN.1 Function Prototypes                        *
  221. *                                                                            *
  222. ****************************************************************************/
  223.  
  224. /* Routines to read and write the identifier information for an ASN.1 value.
  225.    These are occasionally needed by higher-level routines to handle a stream
  226.    of complex ASN.1 structures involving constructed and choice types */
  227.  
  228. int readLength( STREAM *stream, long *length );
  229. void writeLength( STREAM *stream, long length );
  230.  
  231. /* Generalized ASN.1 type manipulation routines */
  232.  
  233. int readUniversalData( STREAM *stream );
  234. int readUniversal( STREAM *stream );
  235. long sizeofObject( const long length );
  236.  
  237. /* Routines for handling integers */
  238.  
  239. int newInteger( INTEGER *integer, const long value );
  240. int deleteInteger( INTEGER *integer );
  241. int assignInteger( INTEGER *integer, const long value );
  242. int sizeofInteger( const INTEGER *integer );
  243. int writeInteger( STREAM *stream, const INTEGER *integer, const int tag );
  244. int _readInteger( STREAM *stream, INTEGER *integer, const BOOLEAN readIdent );
  245.  
  246. #define readInteger( stream,integer ) \
  247.         _readInteger( stream, integer, TRUE )
  248. #define readIntegerData( stream,integer )    \
  249.         _readInteger( stream, integer, FALSE )
  250.  
  251. /* Generally most integers will be non-bignum values, so we also define
  252.    routines to handle values which will fit into a machine word */
  253.  
  254. int sizeofShortInteger( const long integer );
  255. int writeShortInteger( STREAM *stream, const long value, const int tag );
  256. int _readShortInteger( STREAM *stream, long *value, const BOOLEAN readIdent );
  257.  
  258. #define readShortInteger( stream, integer ) \
  259.         _readShortInteger( stream, integer, TRUE )
  260. #define readShortIntegerData( stream, integer )    \
  261.         _readShortInteger( stream, integer, FALSE )
  262.  
  263. /* Routines for handling enumerations */
  264.  
  265. int sizeofEnumerated( const int enumerated );
  266. int writeEnumerated( STREAM *stream, const int enumerated, const int tag );
  267. int _readEnumerated( STREAM *stream, int *enumeration, const BOOLEAN readIdent );
  268.  
  269. #define readEnumerated( stream, enumeration ) \
  270.         _readEnumerated( stream, enumeration, TRUE )
  271. #define readEnumeratedData( stream, enumeration ) \
  272.         _readEnumerated( stream, enumeration, FALSE )
  273.  
  274. /* Routines for handling booleans */
  275.  
  276. #define sizeofBoolean()    ( sizeof( BYTE ) + sizeof( BYTE ) )
  277. int writeBoolean( STREAM *stream, const BOOLEAN boolean, const int tag );
  278. int _readBoolean( STREAM *stream, BOOLEAN *boolean, const BOOLEAN readIdent );
  279.  
  280. #define readBoolean( stream, boolean ) \
  281.         _readBoolean( stream, boolean, TRUE )
  282. #define readBooleanData( stream, boolean ) \
  283.         _readBoolean( stream, boolean, FALSE )
  284.  
  285. /* Routines for handling null values */
  286.  
  287. #define sizeofNull()    ( sizeof( BYTE ) + sizeof( BYTE ) + sizeof( BYTE ) )
  288. int writeNull( STREAM *stream, const int tag );
  289. int _readNull( STREAM *stream, BOOLEAN const readIdent );
  290.  
  291. #define readNull( stream )        _readNull( stream, TRUE )
  292. #define readNullData( stream )    _readNull( stream, FALSE )
  293.  
  294. #ifdef STICT_ASN1
  295.  
  296. /* Routines for handling octet strings */
  297.  
  298. int newOctetString( OCTETSTRING *octetString, const BYTE *value, const int length );
  299. int deleteOctetString( OCTETSTRING *octetString );
  300. int assignOctetString( OCTETSTRING *octetString, const BYTE *value, const int length );
  301. int sizeofOctetString( const OCTETSTRING *octetString );
  302. int writeOctetString( STREAM *stream, const OCTETSTRING *octetString, const int tag );
  303. int _readOctetString( STREAM *stream, OCTETSTRING *octetString, const BOOLEAN readIdent );
  304.  
  305. #define readOctetString( stream, octetString ) \
  306.         _readOctetString( stream, octetString, TRUE )
  307. #define readOctetStringData( stream, octetString ) \
  308.         _readOctetString( stream, octetString, FALSE )
  309.  
  310. #endif /* STRICT_ASN1 */
  311.  
  312. /* Octet strings are basically just byte strings, and it's generally easier
  313.    to write them as such rather than creating an octet string only to destroy
  314.    it a few moments later */
  315.  
  316. int writeByteString( STREAM *stream, const BYTE *string, const int length, \
  317.                      const int tag );
  318.  
  319. /* Routines for handling bit strings */
  320.  
  321. int sizeofBitString( const long bitString );
  322. int writeBitString( STREAM *stream, const long bitString, const int tag );
  323. int _readBitString( STREAM *stream, long *bitString, const BOOLEAN readIdent );
  324.  
  325. #define readBitString( stream, bitString ) \
  326.         _readBitString( stream, bitString, TRUE )
  327. #define readBitStringData( stream, bitString ) \
  328.         _readBitString( stream, bitString, FALSE )
  329.  
  330. #ifdef STRICT_ASN1
  331.  
  332. /* Routines for handling ISO 646 strings */
  333.  
  334. int sizeofISO646string( const char *iso646string );
  335. int writeISO646string( STREAM *stream, const char *iso646string, const int tag );
  336. int _readISO646string( STREAM *stream, char *iso646string, const BOOLEAN readIdent );
  337.  
  338. #define readISO646string( stream, iso646string ) \
  339.         _readISO747String( stream, iso646string, TRUE )
  340. #define readISO646stringData( stream, iso646string ) \
  341.         _readISO747String( stream, iso646string, FALSE )
  342.  
  343. /* Routines for handling object identifiers */
  344.  
  345. int sizeofObjectIdentifier( const OBJECT_IDENTIFIER *objectIdentifier );
  346. int writeObjectIdentifier( STREAM *stream, const OBJECT_IDENTIFIER *objectIdentifier, int tag );
  347. int _readObjectIdentifier( STREAM *stream, OBJECT_IDENTIFIER *objectIdentifier );
  348.  
  349. #define readObjectIdentifier( stream, objectIdentifier ) \
  350.         _readObjectIdentifier( stream, objectIdentifier, TRUE )
  351. #define readObjectIdentifierData( stream, objectIdentifier ) \
  352.         _readObjectIdentifier( stream, objectIdentifier, FALSE )
  353.  
  354. /* Routines for handling generalized time */
  355.  
  356. int newGeneralizedTime( GENERALIZED_TIME *generalizedTime,
  357.                          const int years, const int months, const int days,
  358.                          const int hours, const int minutes, const int seconds );
  359. int deleteGeneralizedTime( GENERALIZED_TIME *generalizedTime );
  360. int assignGeneralizedTime( GENERALIZED_TIME *generalizedTime,
  361.                             const int years, const int months, const int days,
  362.                             const int hours, const int minutes, const int seconds );
  363. int sizeofGeneralizedTime( const GENERALIZED_TIME *generalizedTime );
  364. int writeGeneralizedTime( STREAM *stream, const GENERALIZED_TIME *generalizedTime, int tag );
  365. int _readGeneralizedTime( STREAM *stream, GENERALIZED_TIME *generalizedTime, BOOLEAN readIdent );
  366.  
  367. #define readGeneralizedTime( stream, generalizedTime ) \
  368.         _readGeneralizedTime( stream, generalizedTime, TRUE )
  369. #define readGeneralizedTimeData( stream, generalizedTime ) \
  370.         _readGeneralizedTime( stream, generalizedTime, FALSE )
  371.  
  372. #endif /* STRICT_ASN1 */
  373.  
  374. /* Routines for handling UTC time */
  375.  
  376. int writeUTCTime( STREAM *stream, const time_t time, const int tag );
  377. int _readUTCTime( STREAM *stream, time_t *time, const BOOLEAN readIdent );
  378.  
  379. #define readUTCTime( stream, time )        _readUTCTime( stream, time, TRUE )
  380. #define readUTCTimeData( stream, time )    _readUTCTime( stream, time, FALSE )
  381.  
  382. /* Routines for handling generalized strings */
  383.  
  384. int newGeneralizedString( GENERALIZED_STRING *generalizedString,
  385.                           const STRINGTYPE type, const BYTE *string,
  386.                           const int length );
  387. int deleteGeneralizedString( GENERALIZED_STRING *generalizedString );
  388. int assignGeneralizedString( GENERALIZED_STRING *generalizedString,
  389.                              const STRINGTYPE type, const BYTE *string,
  390.                              const int length );
  391. int sizeofGeneralizedString( const GENERALIZED_STRING *generalizedString );
  392. int writeGeneralizedString( STREAM *stream, const GENERALIZED_STRING *generalizedString,
  393.                             const int tag );
  394. int _readGeneralizedString( STREAM *stream, GENERALIZED_STRING *generalizedString,
  395.                             const BOOLEAN readIdent );
  396.  
  397. #define readGeneralizedString( stream, generalizedString ) \
  398.         _readGeneralizedString( stream, generalizedString, TRUE )
  399. #define readGeneralizedStringData( stream, generalizedString ) \
  400.         _readGeneralizedString( stream, generalizedString, FALSE )
  401.  
  402. /* Generally most strings will be ISO 646/ASCII, so we also define routines
  403.    to handle ASCII strings */
  404.  
  405. int sizeofTextString( const char *string );
  406. int writeTextString( STREAM *stream, const char *string, const int tag );
  407.  
  408. /* Macros to simplify things when we're working with ISO 646 generalized
  409.    strings */
  410.  
  411. #define newString( string, value ) \
  412.         newGeneralizedString( string, STRINGTYPE_ISO646, ( BYTE * ) value, strlen( value ) )
  413. #define deleteString( string )    deleteGeneralizedString( string )
  414. #define assignString( string, value ) \
  415.         assignGeneralizedString( string, STRINGTYPE_ISO646, ( BYTE * ) value, strlen( value ) )
  416. #define sizeofString( string )    sizeofGeneralizedString( string )
  417. #define writeString( stream, string, tag ) \
  418.         writeGeneralizedString( stream, string, tag )
  419. #define readString( stream, string ) \
  420.         readGeneralizedString( stream, string )
  421. #define readStringData( stream, string ) \
  422.         readGeneralizedStringData( stream, string )
  423.  
  424. /* Routines for handling time */
  425.  
  426. int newTime( TIME *time, const time_t seconds, const time_t fractional );
  427. int deleteTime( TIME *time );
  428. int assignTime( TIME *time, const time_t seconds, const time_t fractional );
  429. int sizeofTime( const TIME *time );
  430. int writeTime( STREAM *stream, const TIME *time, const int tag );
  431. int _readTime( STREAM *stream, TIME *time, const BOOLEAN readIdent );
  432.  
  433. #define readTime( stream, time )        _readTime( stream, time, TRUE )
  434. #define readTimeData( stream, time )    _readTime( stream, time, FALSE )
  435.  
  436. /* Routines for handling monetary amounts */
  437.  
  438. int newMonetaryAmount( MONETARY_AMOUNT *monetaryAmount,
  439.                        const CURRENCYTYPE currency, const long units,
  440.                        const long fractional );
  441. int deleteMonetaryAmount( MONETARY_AMOUNT *monetaryAmount );
  442. int assignMonetaryAmount( MONETARY_AMOUNT *monetaryAmount,
  443.                           const CURRENCYTYPE currency, const long units,
  444.                           const long fractional );
  445. int sizeofMonetaryAmount( const MONETARY_AMOUNT *monetaryAmount );
  446. int writeMonetaryAmount( STREAM *stream, const MONETARY_AMOUNT *monetaryAmount,
  447.                          const int tag );
  448. int _readMonetaryAmount( STREAM *stream, MONETARY_AMOUNT *monetaryAmount,
  449.                          const BOOLEAN readIdent );
  450.  
  451. #define readMonetaryAmount( stream, monetaryAmount ) \
  452.         _readMonetaryAmount( stream, monetaryAmount, TRUE )
  453. #define readMonetaryAmountData( stream, monetaryAmount ) \
  454.         _readMonetaryAmount( stream, monetaryAmount, FALSE )
  455.  
  456. #endif /* !_ASN1_DEFINED */
  457.