NAME
ASN.1/DER runtime basic C types
SYNOPSIS
#include <asn_types.h>

DESCRIPTION
asn_types.h contains the basic ASN.1 types expressed in C.
String Types
String types are standard, '\0'-terminated character arrays. The length of such strings should be obtained using strlen().

BMPString
asn__bmp_string contains UNICODE (2-byte) characters.
IA5String
asn__ia5_string contains ASCII characters. Note, there is no cannonical form for indicating end-of-line. Some systems may require "\n", others "\r\n", while still others "\r".
NumericString
asn__numeric_string contains only the ASCII numeric characters ('0' ... '9') and space (0x20).
PrintableString
asn__printable_string contains the ASCII alpha-numeric characters ('A' ... 'Z', 'a' ... 'z', '0' ... '9'), space (0x20), and several punctuation characters (apostrophe ['\''], left parenthesis ['('], right parenthesis [')'], plus ['+'], comma [','], hyphen ['-'], period ['.'], slash ['/'], colon [':'], equal ['='], and question mark ['?']).
VisibleString
asn__visible_string contains all of the ASCII "visible" characters (exclamation ['!'] (0x21) through tilde '~' (0x7e)) and space (0x20).
Fixed-Length Types
BIT STRING
BIT STRINGs with named bit lists are encoded as fixed-length types. These bits should be accessed using the BIT_SET(), BIT_UNSET(), and BIT_IS_SET() macros, which only work on fixed-length bit strings. BIT_SET() turns on a specific bit within a fixed-length bit string. BIT_UNSET() turns off a specific bit within a fixed-length bit string. BIT_IS_SET() tests whether a specific bit it turned on within a fixed-length bit string. The named bits are enumerated in the header file along with the type declaration.
BOOLEAN
asn__boolean can be set to any non-zero value to represent TRUE.
ENUMERATED
The constants are enumerated in the header file along with the type declaration.
GeneralizedTime
asn__generalized_time takes a time_t. See documenation on time() for more information. This form of generalized time does not support fractional seconds.
INTEGER
asn__integer is a C integer type.
NULL
asn__null never appears in native C form and is only specified for completeness.
REAL
asn__real is a C floating-point type.
UTCTime
asn__utc_time takes a time_t. See documenation on time() for more information.
Variable-Length Types
ANY and ANY DEFINED BY
asn__any contains a DER-encoded ASN.1 object, including the tag and length octets.
BIT STRING
BIT STRINGs without named bit lists are encoded as variable-length types. In this case, the length field specifies the length of the bit string, measured in bits.
OCTET STRING
asn__octet_string contains binary data.
OBJECT IDENTIFIER
asn__object_identifier contains the value portion of a DER-encoded object of type OBJECT IDENTIFIER.
Constructed Types
SEQUENCE and SET
These types are representing using struct.
SEQUENCE OF and SET OF
SEQUENCE OF and SET OF are specified using a listed-list data structure.
Other
CHOICE
CHOICE is represented using a tagged union type. The field used to specify the chosen type is always named _chosen. The tag named is formed by concatenating the module name, the type name, and the choice element name.
OPTIONAL
After setting an OPTIONAL object, it is necessary to use the SET_PRESENT() macro on that object's parent. If the OPTIONAL object is subsquently removed, it is necessary to use the SET_ABSENT() macro. To test whether an OPTIONAL object is present, use the IS_PRESENT() macro. Conversely, the IS_ABSENT() macro can be used to test whether an OPTIONAL object is not present.
EXAMPLE
The example below shows an ASN.1 definition, the native C types generated from the ASN.1 definition, and a C demo program which uses these types.

ASN.1 Definition
Demo DEFINITIONS ::= 
BEGIN

terisaSystems OBJECT IDENTIFIER ::=
  { joint-iso-ccitt(2) countries(16) us(840) us-company(1) terisa(113756) }

AsnTypes ::= SEQUENCE {
    string        StringType,
    fixed         FixedLenthType,
    variable      VariableLengthType,
    constructed   ConstructedType,
    other         OtherType
}

-- string types
StringType ::= SEQUENCE {
    ascii         IA5String,
    numeric       NumericString,
    printable     PrintableString
}

-- fixed-length types
FixedLenthType ::= SEQUENCE {
    named-bits    NamedBits,
    bool          BOOLEAN,
    enumeration   Enumeration,
    gtime         GeneralizedTime,
    integer       INTEGER,
    null          NULL,
    real          REAL,
    utime         UTCTime
}
NamedBits ::= BIT STRING { bit-1(1), bit-10(10) }
Enumeration ::= ENUMERATED { one(1), two(2), fifteen(15) }

-- variable-length types
VariableLengthType ::= SEQUENCE {
    anything      ANY,
    unnamed-bits  BIT STRING,
    binary        OCTET STRING,
    oid           OBJECT IDENTIFIER
}

-- constructed types
ConstructedType ::= SEQUENCE {
    ordered       OrderedList,
    unordered     UnorderedList
    
}
OrderedList ::= SEQUENCE OF INTEGER
UnorderedList ::= SET OF INTEGER 

-- other types
OtherType ::= SEQUENCE {
    choice        IntegerOrReal,
    optional      PrintableString  OPTIONAL
}
IntegerOrReal ::= CHOICE {
    integer  INTEGER,
    real     REAL
}

END
C Native Types
typedef
    struct                         demo__StringType_             
    {                                                                 
        asn__ia5_string                ascii; 
        asn__numeric_string            numeric; 
        asn__printable_string          printable; 
    }                              demo__StringType; 

typedef
# define                           demo__NamedBits__bit_1      1 
# define                           demo__NamedBits__bit_10    10 
    UCHAR                          demo__NamedBits[2]; 

typedef
    asn__enumerated                demo__Enumeration; 
# define                           demo__Enumeration__one        1 
# define                           demo__Enumeration__two        2 
# define                           demo__Enumeration__fifteen    15 

typedef
    struct                         demo__FixedLenthType_         
    {                                                                 
        demo__NamedBits                named_bits; 
        asn__boolean                   bool; 
        demo__Enumeration              enumeration; 
        asn__generalized_time          gtime; 
        asn__integer                   integer; 
        asn__real                      real; 
        asn__utc_time                  utime; 
    }                              demo__FixedLenthType; 

typedef
    struct                         demo__VariableLengthType_     
    {                                                                 
        asn__any                       anything; 
        struct                                                            
        {                                                                 
            UINT4                          length; /* number of bits */
            UCHAR                          *value; 
        }                              unnamed_bits; 
        asn__octet_string              binary; 
        asn__object_identifier         oid; 
    }                              demo__VariableLengthType; 

typedef
    struct                         demo__OrderedList_            
    {                                                                 
        struct demo__OrderedList_      *next; 
        asn__integer                   integer; 
    }                              demo__OrderedList; 

typedef
    struct                         demo__UnorderedList_          
    {                                                                 
        struct demo__UnorderedList_    *next; 
        asn__integer                   integer; 
    }                              demo__UnorderedList; 

typedef
    struct                         demo__ConstructedType_        
    {                                                                 
        demo__OrderedList              *ordered; 
        demo__UnorderedList            *unordered; 
    }                              demo__ConstructedType; 

typedef
    struct                         demo__IntegerOrReal_          
    {                                                                 
# define                               demo__IntegerOrReal__integer 0x25 
# define                               demo__IntegerOrReal__real 0x26 
        UINT4                          _choice; 
        union                                                             
        {                                                                 
            asn__integer                   integer; 
            asn__real                      real; 
        } u; 
    }                              demo__IntegerOrReal; 

typedef
    struct                         demo__OtherType_              
    {                                                                 
# define                               demo__OtherType__optional   0 
        UCHAR                          _present[1]; 
        demo__IntegerOrReal            choice; 
        asn__printable_string          optional; 
    }                              demo__OtherType; 

typedef
    struct                         demo__AsnTypes_               
    {                                                                 
        demo__StringType               string; 
        demo__FixedLenthType           fixed; 
        demo__VariableLengthType       variable; 
        demo__ConstructedType          constructed; 
        demo__OtherType                other; 
    }                              demo__AsnTypes; 
C Demo Program
{
    demo__AsnTypes             example;
    asn_types__OrderedList   **tmp;
    int                        i;

    memset(&example, 0, sizeof(example));      /* always zero it first */

    example.string.ascii         = "an ASCII String\n";
    example.string.numeric       = "0102 0304";
    example.string.printable     = "a subset of ASCII";

    BIT_SET(example.fixed.named_bits, demo__NamedBits__bit_10);
    example.fixed.bool           = 0;   /* false */
    example.fixed.enumeration    = demo__Enumeration__two;  
    example.fixed.gtime          = time(0);
    example.fixed.integer        = -230834;
    example.fixed.real           = 123.456789;
    example.fixed.utime          = time(0);

    example.variable.anything.length = 2;
    example.variable.anything.object = "\005\000";  /* DER encoding of NULL */

    example.variable.unnamed_bits.length = 15;  /* fifteen bits long */
    example.variable.unnamed_bits.value  = "\243\002"; /* 10100011 0000001 */

    example.variable.binary.length = 6;
    example.variable.binary.value  = "\142\162\151\141\156\153";

    example.variable.oid.length = 7;
    example.variable.oid.value  = (UCHAR*)"\140\206\110\001\206\370\134";
                                  /* 2 16 840 1 113756 */

    /* list containing 0, 1, ... 9 */
    tmp = &example.constructed.ordered;
    for (i = 0; i < 10; ++i) {
        demo__OrderedList  *el;
        el = *tmp = calloc(1, sizeof(demo__OrderedList));
        el->integer = i;
        tmp = (void**)&el->next;
    }

    example.constructed.unordered = 0;   /* empty set */

    example.other.choice.u.real   = -548.09435;
    example.other.choice._choice  = demo__IntegerOrReal__real;

    example.other.optional = "string";
    SET_PRESENT(example.other, demo__OtherType__optional);
}
BUGS
This document describes a beta implementation. The information contained in this document may be incomplete and is subject to change.


Copyright © 1996, 1997, Visa International Service Association and MasterCard International Incorporated
All Rights Reserved.