Merge branch 'topic/mm-fixes' of git://208.110.73.182/silc into silc.1.1.branch
[silc.git] / lib / silcasn1 / silcasn1.h
index f9c4c46f79f61797892224758c82c2b30e0878a2..df5bde14dab3319be604476d40de1647c15985f5 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2003 - 2005 Pekka Riikonen
+  Copyright (C) 2003 - 2007 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -35,7 +35,7 @@
  * pre-allocated SilcStack is used as memory.
  *
  * The encoding and decoding interface is simple.  silc_asn1_encode is used
- * encode and silc_asn1_decode to decode.  The actual ASN.1 is defined
+ * to encode and silc_asn1_decode to decode.  The actual ASN.1 is defined
  * as variable argument list to the function.  Various macros can be used
  * to encode and decode different ASN.1 types.  All types may also be used
  * to encode and decode with various options (such as implicit and explicit
@@ -102,6 +102,16 @@ typedef struct SilcAsn1Object SilcAsn1Struct;
  *    with SILC_ASN1_OPTS macro.  Other options can be given with various
  *    SILC_ASN1_*_T macros.
  *
+ * EXAMPLE
+ *
+ *    // Encodes boolean value with explicit tag and private class, and
+ *    // the result is allocated into `dest'.
+ *    silc_asn1_encode(asn1, &dest,
+ *                     SILC_ASN1_OPTS(SILC_ASN1_ALLOC),
+ *                     SILC_ASN1_BOOLEAN_T(SILC_ASN1_PRIVATE |
+ *                                         SILC_ASN1_EXPLICIT, 100, boolval),
+ *                     SILC_ASN1_END);
+ *
  * SOURCE
  */
 typedef enum {
@@ -231,7 +241,7 @@ void silc_asn1_free(SilcAsn1 asn1);
  *
  * SYNOPSIS
  *
- *    bool silc_asn1_init(SilcAsn1 asn1);
+ *    SilcBool silc_asn1_init(SilcAsn1 asn1);
  *
  * DESCRIPTION
  *
@@ -246,7 +256,7 @@ void silc_asn1_free(SilcAsn1 asn1);
  *      error;
  *
  ***/
-bool silc_asn1_init(SilcAsn1 asn1);
+SilcBool silc_asn1_init(SilcAsn1 asn1);
 
 /****f* silcasn1/SilcASN1API/silc_asn1_uninit
  *
@@ -266,7 +276,7 @@ void silc_asn1_uninit(SilcAsn1 asn1);
  *
  * SYNOPSIS
  *
- *    bool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
+ *    SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
  *
  * DESCRIPTION
  *
@@ -313,13 +323,13 @@ void silc_asn1_uninit(SilcAsn1 asn1);
  *      string2  [1]  OCTET-STRING }
  *
  ***/
-bool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
+SilcBool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
 
 /****f* silcasn1/SilcASN1API/silc_asn1_decode
  *
  * SYNOPSIS
  *
- *    bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
+ *    SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  *
  * DESCRIPTION
  *
@@ -355,7 +365,7 @@ bool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
  *
  * EXAMPLE
  *
- *    bool bool_val, foo;
+ *    SilcBool bool_val, foo;
  *    unsigned char *string, string2;
  *    SilcUInt32 string_len, string2_len;
  *
@@ -370,7 +380,7 @@ bool silc_asn1_encode(SilcAsn1 asn1, SilcBuffer dest, ...);
  *                     SILC_ASN1_END, SILC_ASN1_END);
  *
  ***/
-bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
+SilcBool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
 
 /****f* silcasn1/SilcASN1API/SILC_ASN1_OPTS
  *
@@ -413,7 +423,7 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  *
  *    Decoding:
  *    SILC_ASN1_ANY(&buffer)
- *    SILC_ASN1_ANY_T(opts, tag, &buffer)
+ *    SILC_ASN1_ANY_T(opts, tag, buffer)
  *
  * DESCRIPTION
  *
@@ -443,6 +453,52 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
 #define SILC_ASN1_ANY(x) SILC_ASN1_U1(ANY, x)
 #define SILC_ASN1_ANY_T(o, t, x) SILC_ASN1_T1(ANY, o, t, x)
 
+/****f* silcasn1/SilcASN1API/SILC_ASN1_ANY_PRIMITIVE
+ *
+ * SYNOPSIS
+ *
+ *    Encoding:
+ *    SILC_ASN1_ANY_PRIMITIVE(tag, buffer)
+ *    SILC_ASN1_ANY_PRIMITIVE_T(opts, tag, buffer)
+ *
+ *    Decoding:
+ *    SILC_ASN1_ANY_PRIMITIVE(tag, &buffer)
+ *    SILC_ASN1_ANY_PRIMITIVE_T(opts, tag, buffer)
+ *
+ * DESCRIPTION
+ *
+ *    Special macro used to encode pre-encoded primitive data blob.  The data
+ *    can be any primitive type that is already encoded in correct format.
+ *    The caller is responsible of making sure the data is formatted
+ *    correctly.  When decoding this returns the raw data blob and the caller
+ *    must know of what type and format it is.  The buffer type is SilcBuffer.
+ *
+ *    This macro can be used in cases when the data to be encoded is already
+ *    in encoded format, and it only needs to be added to ASN.1 tree.  The
+ *    SILC_ASN1_ANY cannot be used with primitives when tagging implicitly,
+ *    in these cases this macro can be used.
+ *
+ *    The `opts' is SilcAsn1Options.  The `tag' is a tag number.
+ *
+ * EXAMPLE
+ *
+ *    // Get MP integer in encoded format
+ *    mpbuf = mp_get_octet_string(mp);
+ *
+ *    // Encode the MP integer data to the tree
+ *    silc_asn1_encode(asn1, tree,
+ *                     SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_INTEGER, mpbuf),
+ *                     SILC_ASN1_END);
+ *
+ *    // Decode the MP integer data from the tree
+ *    silc_asn1_decode(asn1, tree,
+ *                     SILC_ASN1_ANY_PRIMITIVE(SILC_ASN1_TAG_INTEGER, &buffer),
+ *                     SILC_ASN1_END);
+ *
+ ***/
+#define SILC_ASN1_ANY_PRIMITIVE(t, x) SILC_ASN1_T1(ANY_PRIMITIVE, 0, t, x)
+#define SILC_ASN1_ANY_PRIMITIVE_T(o, t, x) SILC_ASN1_T1(ANY_PRIMITIVE, o, t, x)
+
 /****f* silcasn1/SilcASN1API/SILC_ASN1_SEQUENCE
  *
  * SYNOPSIS
@@ -601,17 +657,17 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  * SYNOPSIS
  *
  *    Encoding:
- *    SILC_ASN1_BOOLEAN(bool)
- *    SILC_ASN1_BOOLEAN_T(opts, tag, bool)
+ *    SILC_ASN1_BOOLEAN(boolval)
+ *    SILC_ASN1_BOOLEAN_T(opts, tag, boolval)
  *
  *    Decoding:
- *    SILC_ASN1_BOOLEAN(&bool)
- *    SILC_ASN1_BOOLEAN_T(opts, tag, &bool)
+ *    SILC_ASN1_BOOLEAN(&boolval)
+ *    SILC_ASN1_BOOLEAN_T(opts, tag, &boolval)
  *
  * DESCRIPTION
  *
- *    Macro used to encode or decode boolean value.  Type boolean type
- *    is bool.
+ *    Macro used to encode or decode boolean value.  The boolean type
+ *    is SilcBool.
  *
  *    The `opts' is SilcAsn1Options.  The `tag' is a tag number.
  *
@@ -625,7 +681,7 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  *
  *    Encoding:
  *    SILC_ASN1_INT(integer)
- *    SILC_ASN1_INT_T(opts, tag, integer)
+ *    SILC_ASN1_INT_T(opts, tag, &integer)
  *
  *    Decoding:
  *    SILC_ASN1_INT(&integer)
@@ -642,13 +698,36 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
 #define SILC_ASN1_INT(x) SILC_ASN1_U1(INTEGER, x)
 #define SILC_ASN1_INT_T(o, t, x) SILC_ASN1_T1(INTEGER, o, t, x)
 
+/****f* silcasn1/SilcASN1API/SILC_ASN1_SHORT_INT
+ *
+ * SYNOPSIS
+ *
+ *    Encoding:
+ *    SILC_ASN1_SHORT_INT(integer)
+ *    SILC_ASN1_SHORT_INT_T(opts, tag, &integer)
+ *
+ *    Decoding:
+ *    SILC_ASN1_SHORT_INT(&integer)
+ *    SILC_ASN1_SHORT_INT_T(opts, tag, &integer);
+ *
+ * DESCRIPTION
+ *
+ *    Macro used to encode or decode short integer (32 bits).  The
+ *    integer type is SilcUInt32.
+ *
+ *    The `opts' is SilcAsn1Options.  The `tag' is a tag number.
+ *
+ ***/
+#define SILC_ASN1_SHORT_INT(x) SILC_ASN1_U1(SHORT_INTEGER, x)
+#define SILC_ASN1_SHORT_INT_T(o, t, x) SILC_ASN1_T1(SHORT_INTEGER, o, t, x)
+
 /****f* silcasn1/SilcASN1API/SILC_ASN1_ENUM
  *
  * SYNOPSIS
  *
  *    Encoding:
  *    SILC_ASN1_ENUM(enum)
- *    SILC_ASN1_ENUM_T(opts, tag, enum)
+ *    SILC_ASN1_ENUM_T(opts, tag, &enum)
  *
  *    Decoding:
  *    SILC_ASN1_ENUM(&enum)
@@ -750,16 +829,11 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  * DESCRIPTION
  *
  *    Macro used to encode or decode octet string.  The string type is
- *    unsigned char and string length SilcUInt32.
+ *    unsigned char and string length SilcUInt32.  Octet string is
+ *    considered to be 8-bit unsigned binary data.
  *
  *    The `opts' is SilcAsn1Options.  The `tag' is a tag number.
  *
- * NOTES
- *
- *    The string must be in UTF-8 encoding when encoding.  The decoded
- *    string will be in UTF-8 encoding.  The actual data is encoded to
- *    or decoded from 8-bit ASCII.
- *
  ***/
 #define SILC_ASN1_OCTET_STRING(x, xl) SILC_ASN1_U2(OCTET_STRING, x, xl)
 #define SILC_ASN1_OCTET_STRING_T(o, t, x, xl) SILC_ASN1_T2(OCTET_STRING, o, t, x, xl)
@@ -1064,7 +1138,7 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  *
  *    Decoding:
  *    SILC_ASN1_UTC_TIME(&str, &timeval)
- *    SILC_ASN1_UTC_TIME_T(opts, tag, &timeval)
+ *    SILC_ASN1_UTC_TIME_T(opts, tag, timeval)
  *
  * DESCRIPTION
  *
@@ -1087,7 +1161,7 @@ bool silc_asn1_decode(SilcAsn1 asn1, SilcBuffer src, ...);
  *
  *    Decoding:
  *    SILC_ASN1_GEN_TIME(&str, &timeval)
- *    SILC_ASN1_GEN_TIME_T(opts, tag, &timeval)
+ *    SILC_ASN1_GEN_TIME_T(opts, tag, timeval)
  *
  * DESCRIPTION
  *