asn1t.h

Go to the documentation of this file.
00001 /* asn1t.h */
00002 /* Written by Dr Stephen N Henson ([email protected]) for the OpenSSL
00003  * project 2000.
00004  */
00005 /* ====================================================================
00006  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions
00010  * are met:
00011  *
00012  * 1. Redistributions of source code must retain the above copyright
00013  *    notice, this list of conditions and the following disclaimer. 
00014  *
00015  * 2. Redistributions in binary form must reproduce the above copyright
00016  *    notice, this list of conditions and the following disclaimer in
00017  *    the documentation and/or other materials provided with the
00018  *    distribution.
00019  *
00020  * 3. All advertising materials mentioning features or use of this
00021  *    software must display the following acknowledgment:
00022  *    "This product includes software developed by the OpenSSL Project
00023  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
00024  *
00025  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
00026  *    endorse or promote products derived from this software without
00027  *    prior written permission. For written permission, please contact
00028  *    [email protected]
00029  *
00030  * 5. Products derived from this software may not be called "OpenSSL"
00031  *    nor may "OpenSSL" appear in their names without prior written
00032  *    permission of the OpenSSL Project.
00033  *
00034  * 6. Redistributions of any form whatsoever must retain the following
00035  *    acknowledgment:
00036  *    "This product includes software developed by the OpenSSL Project
00037  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
00038  *
00039  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
00040  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00041  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00042  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
00043  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00044  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00045  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00046  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00047  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
00048  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00049  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
00050  * OF THE POSSIBILITY OF SUCH DAMAGE.
00051  * ====================================================================
00052  *
00053  * This product includes cryptographic software written by Eric Young
00054  * ([email protected]).  This product includes software written by Tim
00055  * Hudson ([email protected]).
00056  *
00057  */
00058 #ifndef HEADER_ASN1T_H
00059 #define HEADER_ASN1T_H
00060 
00061 #include <stddef.h>
00062 #include <openssl/e_os2.h>
00063 #include <openssl/asn1.h>
00064 
00065 #ifdef OPENSSL_BUILD_SHLIBCRYPTO
00066 # undef OPENSSL_EXTERN
00067 # define OPENSSL_EXTERN OPENSSL_EXPORT
00068 #endif
00069 
00070 /* ASN1 template defines, structures and functions */
00071 
00072 #ifdef  __cplusplus
00073 extern "C" {
00074 #endif
00075 
00076 
00077 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
00078 
00079 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
00080 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr))
00081 
00082 
00083 /* Macros for start and end of ASN1_ITEM definition */
00084 
00085 #define ASN1_ITEM_start(itname) \
00086         OPENSSL_GLOBAL const ASN1_ITEM itname##_it = {
00087 
00088 #define ASN1_ITEM_end(itname) \
00089                 };
00090 
00091 #else
00092 
00093 /* Macro to obtain ASN1_ADB pointer from a type (only used internally) */
00094 #define ASN1_ADB_ptr(iptr) ((const ASN1_ADB *)(iptr()))
00095 
00096 
00097 /* Macros for start and end of ASN1_ITEM definition */
00098 
00099 #define ASN1_ITEM_start(itname) \
00100         EXPORT_C const ASN1_ITEM * itname##_it(void) \
00101         { \
00102                 static const ASN1_ITEM local_it = { \
00103 
00104 #define ASN1_ITEM_end(itname) \
00105                 }; \
00106         return &local_it; \
00107         }
00108 
00109 #endif
00110 
00111 
00112 /* Macros to aid ASN1 template writing */
00113 
00114 #define ASN1_ITEM_TEMPLATE(tname) \
00115         static const ASN1_TEMPLATE tname##_item_tt 
00116 
00117 #define ASN1_ITEM_TEMPLATE_END(tname) \
00118         ;\
00119         ASN1_ITEM_start(tname) \
00120                 ASN1_ITYPE_PRIMITIVE,\
00121                 -1,\
00122                 &tname##_item_tt,\
00123                 0,\
00124                 NULL,\
00125                 0,\
00126                 #tname \
00127         ASN1_ITEM_end(tname)
00128 
00129 
00130 /* This is a ASN1 type which just embeds a template */
00131  
00132 /* This pair helps declare a SEQUENCE. We can do:
00133  *
00134  *      ASN1_SEQUENCE(stname) = {
00135  *              ... SEQUENCE components ...
00136  *      } ASN1_SEQUENCE_END(stname)
00137  *
00138  *      This will produce an ASN1_ITEM called stname_it
00139  *      for a structure called stname.
00140  *
00141  *      If you want the same structure but a different
00142  *      name then use:
00143  *
00144  *      ASN1_SEQUENCE(itname) = {
00145  *              ... SEQUENCE components ...
00146  *      } ASN1_SEQUENCE_END_name(stname, itname)
00147  *
00148  *      This will create an item called itname_it using
00149  *      a structure called stname.
00150  */
00151 
00152 #define ASN1_SEQUENCE(tname) \
00153         static const ASN1_TEMPLATE tname##_seq_tt[] 
00154 
00155 #define ASN1_SEQUENCE_END(stname) ASN1_SEQUENCE_END_name(stname, stname)
00156 
00157 #define ASN1_SEQUENCE_END_name(stname, tname) \
00158         ;\
00159         ASN1_ITEM_start(tname) \
00160                 ASN1_ITYPE_SEQUENCE,\
00161                 V_ASN1_SEQUENCE,\
00162                 tname##_seq_tt,\
00163                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
00164                 NULL,\
00165                 sizeof(stname),\
00166                 #stname \
00167         ASN1_ITEM_end(tname)
00168 
00169 #define ASN1_NDEF_SEQUENCE(tname) \
00170         ASN1_SEQUENCE(tname)
00171 
00172 #define ASN1_SEQUENCE_cb(tname, cb) \
00173         static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
00174         ASN1_SEQUENCE(tname)
00175 
00176 #define ASN1_BROKEN_SEQUENCE(tname) \
00177         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \
00178         ASN1_SEQUENCE(tname)
00179 
00180 #define ASN1_SEQUENCE_ref(tname, cb, lck) \
00181         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \
00182         ASN1_SEQUENCE(tname)
00183 
00184 #define ASN1_SEQUENCE_enc(tname, enc, cb) \
00185         static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \
00186         ASN1_SEQUENCE(tname)
00187 
00188 #define ASN1_NDEF_SEQUENCE_END(tname) \
00189         ;\
00190         ASN1_ITEM_start(tname) \
00191                 ASN1_ITYPE_NDEF_SEQUENCE,\
00192                 V_ASN1_SEQUENCE,\
00193                 tname##_seq_tt,\
00194                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
00195                 NULL,\
00196                 sizeof(tname),\
00197                 #tname \
00198         ASN1_ITEM_end(tname)
00199 
00200 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname)
00201 
00202 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
00203 
00204 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname)
00205 
00206 #define ASN1_SEQUENCE_END_ref(stname, tname) \
00207         ;\
00208         ASN1_ITEM_start(tname) \
00209                 ASN1_ITYPE_SEQUENCE,\
00210                 V_ASN1_SEQUENCE,\
00211                 tname##_seq_tt,\
00212                 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\
00213                 &tname##_aux,\
00214                 sizeof(stname),\
00215                 #stname \
00216         ASN1_ITEM_end(tname)
00217 
00218 
00219 /* This pair helps declare a CHOICE type. We can do:
00220  *
00221  *      ASN1_CHOICE(chname) = {
00222  *              ... CHOICE options ...
00223  *      ASN1_CHOICE_END(chname)
00224  *
00225  *      This will produce an ASN1_ITEM called chname_it
00226  *      for a structure called chname. The structure
00227  *      definition must look like this:
00228  *      typedef struct {
00229  *              int type;
00230  *              union {
00231  *                      ASN1_SOMETHING *opt1;
00232  *                      ASN1_SOMEOTHER *opt2;
00233  *              } value;
00234  *      } chname;
00235  *      
00236  *      the name of the selector must be 'type'.
00237  *      to use an alternative selector name use the
00238  *      ASN1_CHOICE_END_selector() version.
00239  */
00240 
00241 #define ASN1_CHOICE(tname) \
00242         static const ASN1_TEMPLATE tname##_ch_tt[] 
00243 
00244 #define ASN1_CHOICE_cb(tname, cb) \
00245         static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \
00246         ASN1_CHOICE(tname)
00247 
00248 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname)
00249 
00250 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type)
00251 
00252 #define ASN1_CHOICE_END_selector(stname, tname, selname) \
00253         ;\
00254         ASN1_ITEM_start(tname) \
00255                 ASN1_ITYPE_CHOICE,\
00256                 offsetof(stname,selname) ,\
00257                 tname##_ch_tt,\
00258                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
00259                 NULL,\
00260                 sizeof(stname),\
00261                 #stname \
00262         ASN1_ITEM_end(tname)
00263 
00264 #define ASN1_CHOICE_END_cb(stname, tname, selname) \
00265         ;\
00266         ASN1_ITEM_start(tname) \
00267                 ASN1_ITYPE_CHOICE,\
00268                 offsetof(stname,selname) ,\
00269                 tname##_ch_tt,\
00270                 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\
00271                 &tname##_aux,\
00272                 sizeof(stname),\
00273                 #stname \
00274         ASN1_ITEM_end(tname)
00275 
00276 /* This helps with the template wrapper form of ASN1_ITEM */
00277 
00278 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \
00279         (flags), (tag), 0,\
00280         #name, ASN1_ITEM_ref(type) }
00281 
00282 /* These help with SEQUENCE or CHOICE components */
00283 
00284 /* used to declare other types */
00285 
00286 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \
00287         (flags), (tag), offsetof(stname, field),\
00288         #field, ASN1_ITEM_ref(type) }
00289 
00290 /* used when the structure is combined with the parent */
00291 
00292 #define ASN1_EX_COMBINE(flags, tag, type) { \
00293         (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) }
00294 
00295 /* implicit and explicit helper macros */
00296 
00297 #define ASN1_IMP_EX(stname, field, type, tag, ex) \
00298                 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type)
00299 
00300 #define ASN1_EXP_EX(stname, field, type, tag, ex) \
00301                 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type)
00302 
00303 /* Any defined by macros: the field used is in the table itself */
00304 
00305 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
00306 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
00307 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) }
00308 #else
00309 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb }
00310 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb }
00311 #endif
00312 /* Plain simple type */
00313 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type)
00314 
00315 /* OPTIONAL simple type */
00316 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type)
00317 
00318 /* IMPLICIT tagged simple type */
00319 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0)
00320 
00321 /* IMPLICIT tagged OPTIONAL simple type */
00322 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
00323 
00324 /* Same as above but EXPLICIT */
00325 
00326 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0)
00327 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL)
00328 
00329 /* SEQUENCE OF type */
00330 #define ASN1_SEQUENCE_OF(stname, field, type) \
00331                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type)
00332 
00333 /* OPTIONAL SEQUENCE OF */
00334 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \
00335                 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
00336 
00337 /* Same as above but for SET OF */
00338 
00339 #define ASN1_SET_OF(stname, field, type) \
00340                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type)
00341 
00342 #define ASN1_SET_OF_OPT(stname, field, type) \
00343                 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type)
00344 
00345 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */
00346 
00347 #define ASN1_IMP_SET_OF(stname, field, type, tag) \
00348                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
00349 
00350 #define ASN1_EXP_SET_OF(stname, field, type, tag) \
00351                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF)
00352 
00353 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \
00354                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
00355 
00356 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \
00357                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL)
00358 
00359 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \
00360                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
00361 
00362 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \
00363                         ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
00364 
00365 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \
00366                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF)
00367 
00368 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \
00369                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL)
00370 
00371 /* EXPLICIT OPTIONAL using indefinite length constructed form */
00372 #define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \
00373                         ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF)
00374 
00375 /* Macros for the ASN1_ADB structure */
00376 
00377 #define ASN1_ADB(name) \
00378         static const ASN1_ADB_TABLE name##_adbtbl[] 
00379 
00380 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION
00381 
00382 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
00383         ;\
00384         static const ASN1_ADB name##_adb = {\
00385                 flags,\
00386                 offsetof(name, field),\
00387                 app_table,\
00388                 name##_adbtbl,\
00389                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
00390                 def,\
00391                 none\
00392         }
00393 
00394 #else
00395 
00396 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \
00397         ;\
00398         EXPORT_C static const ASN1_ITEM *name##_adb(void) \
00399         { \
00400         static const ASN1_ADB internal_adb = \
00401                 {\
00402                 flags,\
00403                 offsetof(name, field),\
00404                 app_table,\
00405                 name##_adbtbl,\
00406                 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\
00407                 def,\
00408                 none\
00409                 }; \
00410                 return (const ASN1_ITEM *) &internal_adb; \
00411         } \
00412         void dummy_function(void)
00413 
00414 #endif
00415 
00416 #define ADB_ENTRY(val, template) {val, template}
00417 
00418 #define ASN1_ADB_TEMPLATE(name) \
00419         static const ASN1_TEMPLATE name##_tt 
00420 
00421 /* This is the ASN1 template structure that defines
00422  * a wrapper round the actual type. It determines the
00423  * actual position of the field in the value structure,
00424  * various flags such as OPTIONAL and the field name.
00425  */
00426 
00427 struct ASN1_TEMPLATE_st {
00428 unsigned long flags;            /* Various flags */
00429 long tag;                       /* tag, not used if no tagging */
00430 unsigned long offset;           /* Offset of this field in structure */
00431 #ifndef NO_ASN1_FIELD_NAMES
00432 const char *field_name;         /* Field name */
00433 #endif
00434 ASN1_ITEM_EXP *item;            /* Relevant ASN1_ITEM or ASN1_ADB */
00435 };
00436 
00437 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */
00438 
00439 #define ASN1_TEMPLATE_item(t) (t->item_ptr)
00440 #define ASN1_TEMPLATE_adb(t) (t->item_ptr)
00441 
00442 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE;
00443 typedef struct ASN1_ADB_st ASN1_ADB;
00444 
00445 struct ASN1_ADB_st {
00446         unsigned long flags;    /* Various flags */
00447         unsigned long offset;   /* Offset of selector field */
00448         STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */
00449         const ASN1_ADB_TABLE *tbl;      /* Table of possible types */
00450         long tblcount;          /* Number of entries in tbl */
00451         const ASN1_TEMPLATE *default_tt;  /* Type to use if no match */
00452         const ASN1_TEMPLATE *null_tt;  /* Type to use if selector is NULL */
00453 };
00454 
00455 struct ASN1_ADB_TABLE_st {
00456         long value;             /* NID for an object or value for an int */
00457         const ASN1_TEMPLATE tt;         /* item for this value */
00458 };
00459 
00460 /* template flags */
00461 
00462 /* Field is optional */
00463 #define ASN1_TFLG_OPTIONAL      (0x1)
00464 
00465 /* Field is a SET OF */
00466 #define ASN1_TFLG_SET_OF        (0x1 << 1)
00467 
00468 /* Field is a SEQUENCE OF */
00469 #define ASN1_TFLG_SEQUENCE_OF   (0x2 << 1)
00470 
00471 /* Special case: this refers to a SET OF that
00472  * will be sorted into DER order when encoded *and*
00473  * the corresponding STACK will be modified to match
00474  * the new order.
00475  */
00476 #define ASN1_TFLG_SET_ORDER     (0x3 << 1)
00477 
00478 /* Mask for SET OF or SEQUENCE OF */
00479 #define ASN1_TFLG_SK_MASK       (0x3 << 1)
00480 
00481 /* These flags mean the tag should be taken from the
00482  * tag field. If EXPLICIT then the underlying type
00483  * is used for the inner tag.
00484  */
00485 
00486 /* IMPLICIT tagging */
00487 #define ASN1_TFLG_IMPTAG        (0x1 << 3)
00488 
00489 
00490 /* EXPLICIT tagging, inner tag from underlying type */
00491 #define ASN1_TFLG_EXPTAG        (0x2 << 3)
00492 
00493 #define ASN1_TFLG_TAG_MASK      (0x3 << 3)
00494 
00495 /* context specific IMPLICIT */
00496 #define ASN1_TFLG_IMPLICIT      ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT
00497 
00498 /* context specific EXPLICIT */
00499 #define ASN1_TFLG_EXPLICIT      ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT
00500 
00501 /* If tagging is in force these determine the
00502  * type of tag to use. Otherwise the tag is
00503  * determined by the underlying type. These 
00504  * values reflect the actual octet format.
00505  */
00506 
00507 /* Universal tag */ 
00508 #define ASN1_TFLG_UNIVERSAL     (0x0<<6)
00509 /* Application tag */ 
00510 #define ASN1_TFLG_APPLICATION   (0x1<<6)
00511 /* Context specific tag */ 
00512 #define ASN1_TFLG_CONTEXT       (0x2<<6)
00513 /* Private tag */ 
00514 #define ASN1_TFLG_PRIVATE       (0x3<<6)
00515 
00516 #define ASN1_TFLG_TAG_CLASS     (0x3<<6)
00517 
00518 /* These are for ANY DEFINED BY type. In this case
00519  * the 'item' field points to an ASN1_ADB structure
00520  * which contains a table of values to decode the
00521  * relevant type
00522  */
00523 
00524 #define ASN1_TFLG_ADB_MASK      (0x3<<8)
00525 
00526 #define ASN1_TFLG_ADB_OID       (0x1<<8)
00527 
00528 #define ASN1_TFLG_ADB_INT       (0x1<<9)
00529 
00530 /* This flag means a parent structure is passed
00531  * instead of the field: this is useful is a
00532  * SEQUENCE is being combined with a CHOICE for
00533  * example. Since this means the structure and
00534  * item name will differ we need to use the
00535  * ASN1_CHOICE_END_name() macro for example.
00536  */
00537 
00538 #define ASN1_TFLG_COMBINE       (0x1<<10)
00539 
00540 /* This flag when present in a SEQUENCE OF, SET OF
00541  * or EXPLICIT causes indefinite length constructed
00542  * encoding to be used if required.
00543  */
00544 
00545 #define ASN1_TFLG_NDEF          (0x1<<11)
00546 
00547 /* This is the actual ASN1 item itself */
00548 
00549 struct ASN1_ITEM_st {
00550 char itype;                     /* The item type, primitive, SEQUENCE, CHOICE or extern */
00551 long utype;                     /* underlying type */
00552 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */
00553 long tcount;                    /* Number of templates if SEQUENCE or CHOICE */
00554 const void *funcs;              /* functions that handle this type */
00555 long size;                      /* Structure size (usually)*/
00556 #ifndef NO_ASN1_FIELD_NAMES
00557 const char *sname;              /* Structure name */
00558 #endif
00559 };
00560 
00561 /* These are values for the itype field and
00562  * determine how the type is interpreted.
00563  *
00564  * For PRIMITIVE types the underlying type
00565  * determines the behaviour if items is NULL.
00566  *
00567  * Otherwise templates must contain a single 
00568  * template and the type is treated in the
00569  * same way as the type specified in the template.
00570  *
00571  * For SEQUENCE types the templates field points
00572  * to the members, the size field is the
00573  * structure size.
00574  *
00575  * For CHOICE types the templates field points
00576  * to each possible member (typically a union)
00577  * and the 'size' field is the offset of the
00578  * selector.
00579  *
00580  * The 'funcs' field is used for application
00581  * specific functions. 
00582  *
00583  * For COMPAT types the funcs field gives a
00584  * set of functions that handle this type, this
00585  * supports the old d2i, i2d convention.
00586  *
00587  * The EXTERN type uses a new style d2i/i2d.
00588  * The new style should be used where possible
00589  * because it avoids things like the d2i IMPLICIT
00590  * hack.
00591  *
00592  * MSTRING is a multiple string type, it is used
00593  * for a CHOICE of character strings where the
00594  * actual strings all occupy an ASN1_STRING
00595  * structure. In this case the 'utype' field
00596  * has a special meaning, it is used as a mask
00597  * of acceptable types using the B_ASN1 constants.
00598  *
00599  * NDEF_SEQUENCE is the same as SEQUENCE except
00600  * that it will use indefinite length constructed
00601  * encoding if requested.
00602  *
00603  */
00604 
00605 #define ASN1_ITYPE_PRIMITIVE            0x0
00606 
00607 #define ASN1_ITYPE_SEQUENCE             0x1
00608 
00609 #define ASN1_ITYPE_CHOICE               0x2
00610 
00611 #define ASN1_ITYPE_COMPAT               0x3
00612 
00613 #define ASN1_ITYPE_EXTERN               0x4
00614 
00615 #define ASN1_ITYPE_MSTRING              0x5
00616 
00617 #define ASN1_ITYPE_NDEF_SEQUENCE        0x6
00618 
00619 /* Cache for ASN1 tag and length, so we
00620  * don't keep re-reading it for things
00621  * like CHOICE
00622  */
00623 
00624 struct ASN1_TLC_st{
00625         char valid;     /* Values below are valid */
00626         int ret;        /* return value */
00627         long plen;      /* length */
00628         int ptag;       /* class value */
00629         int pclass;     /* class value */
00630         int hdrlen;     /* header length */
00631 };
00632 
00633 /* Typedefs for ASN1 function pointers */
00634 
00635 typedef ASN1_VALUE * ASN1_new_func(void);
00636 typedef void ASN1_free_func(ASN1_VALUE *a);
00637 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length);
00638 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in);
00639 
00640 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
00641                                         int tag, int aclass, char opt, ASN1_TLC *ctx);
00642 
00643 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
00644 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
00645 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it);
00646 
00647 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
00648 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
00649 
00650 typedef struct ASN1_COMPAT_FUNCS_st {
00651         ASN1_new_func *asn1_new;
00652         ASN1_free_func *asn1_free;
00653         ASN1_d2i_func *asn1_d2i;
00654         ASN1_i2d_func *asn1_i2d;
00655 } ASN1_COMPAT_FUNCS;
00656 
00657 typedef struct ASN1_EXTERN_FUNCS_st {
00658         void *app_data;
00659         ASN1_ex_new_func *asn1_ex_new;
00660         ASN1_ex_free_func *asn1_ex_free;
00661         ASN1_ex_free_func *asn1_ex_clear;
00662         ASN1_ex_d2i *asn1_ex_d2i;
00663         ASN1_ex_i2d *asn1_ex_i2d;
00664 } ASN1_EXTERN_FUNCS;
00665 
00666 typedef struct ASN1_PRIMITIVE_FUNCS_st {
00667         void *app_data;
00668         unsigned long flags;
00669         ASN1_ex_new_func *prim_new;
00670         ASN1_ex_free_func *prim_free;
00671         ASN1_ex_free_func *prim_clear;
00672         ASN1_primitive_c2i *prim_c2i;
00673         ASN1_primitive_i2c *prim_i2c;
00674 } ASN1_PRIMITIVE_FUNCS;
00675 
00676 /* This is the ASN1_AUX structure: it handles various
00677  * miscellaneous requirements. For example the use of
00678  * reference counts and an informational callback.
00679  *
00680  * The "informational callback" is called at various
00681  * points during the ASN1 encoding and decoding. It can
00682  * be used to provide minor customisation of the structures
00683  * used. This is most useful where the supplied routines
00684  * *almost* do the right thing but need some extra help
00685  * at a few points. If the callback returns zero then
00686  * it is assumed a fatal error has occurred and the 
00687  * main operation should be abandoned.
00688  *
00689  * If major changes in the default behaviour are required
00690  * then an external type is more appropriate.
00691  */
00692 
00693 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it);
00694 
00695 typedef struct ASN1_AUX_st {
00696         void *app_data;
00697         int flags;
00698         int ref_offset;         /* Offset of reference value */
00699         int ref_lock;           /* Lock type to use */
00700         ASN1_aux_cb *asn1_cb;
00701         int enc_offset;         /* Offset of ASN1_ENCODING structure */
00702 } ASN1_AUX;
00703 
00704 /* Flags in ASN1_AUX */
00705 
00706 /* Use a reference count */
00707 #define ASN1_AFLG_REFCOUNT      1
00708 /* Save the encoding of structure (useful for signatures) */
00709 #define ASN1_AFLG_ENCODING      2
00710 /* The Sequence length is invalid */
00711 #define ASN1_AFLG_BROKEN        4
00712 
00713 /* operation values for asn1_cb */
00714 
00715 #define ASN1_OP_NEW_PRE         0
00716 #define ASN1_OP_NEW_POST        1
00717 #define ASN1_OP_FREE_PRE        2
00718 #define ASN1_OP_FREE_POST       3
00719 #define ASN1_OP_D2I_PRE         4
00720 #define ASN1_OP_D2I_POST        5
00721 #define ASN1_OP_I2D_PRE         6
00722 #define ASN1_OP_I2D_POST        7
00723 
00724 /* Macro to implement a primitive type */
00725 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0)
00726 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \
00727                                 ASN1_ITEM_start(itname) \
00728                                         ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \
00729                                 ASN1_ITEM_end(itname)
00730 
00731 /* Macro to implement a multi string type */
00732 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \
00733                                 ASN1_ITEM_start(itname) \
00734                                         ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \
00735                                 ASN1_ITEM_end(itname)
00736 
00737 /* Macro to implement an ASN1_ITEM in terms of old style funcs */
00738 
00739 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE)
00740 
00741 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \
00742         static const ASN1_COMPAT_FUNCS sname##_ff = { \
00743                 (ASN1_new_func *)sname##_new, \
00744                 (ASN1_free_func *)sname##_free, \
00745                 (ASN1_d2i_func *)d2i_##sname, \
00746                 (ASN1_i2d_func *)i2d_##sname, \
00747         }; \
00748         ASN1_ITEM_start(sname) \
00749                 ASN1_ITYPE_COMPAT, \
00750                 tag, \
00751                 NULL, \
00752                 0, \
00753                 &sname##_ff, \
00754                 0, \
00755                 #sname \
00756         ASN1_ITEM_end(sname)
00757 
00758 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \
00759         ASN1_ITEM_start(sname) \
00760                 ASN1_ITYPE_EXTERN, \
00761                 tag, \
00762                 NULL, \
00763                 0, \
00764                 &fptrs, \
00765                 0, \
00766                 #sname \
00767         ASN1_ITEM_end(sname)
00768 
00769 /* Macro to implement standard functions in terms of ASN1_ITEM structures */
00770 
00771 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname)
00772 
00773 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname)
00774 
00775 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \
00776                         IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname)
00777 
00778 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \
00779                 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname)
00780 
00781 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \
00782         EXPORT_C stname *fname##_new(void) \
00783         { \
00784                 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \
00785         } \
00786         EXPORT_C void fname##_free(stname *a) \
00787         { \
00788                 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \
00789         }
00790 
00791 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \
00792         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
00793         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
00794 
00795 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \
00796         EXPORT_C stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
00797         { \
00798                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
00799         } \
00800         EXPORT_C int i2d_##fname(stname *a, unsigned char **out) \
00801         { \
00802                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
00803         } 
00804 
00805 #define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \
00806         EXPORT_C int i2d_##stname##_NDEF(stname *a, unsigned char **out) \
00807         { \
00808                 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\
00809         } 
00810 
00811 /* This includes evil casts to remove const: they will go away when full
00812  * ASN1 constification is done.
00813  */
00814 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
00815         EXPORT_C stname *d2i_##fname(stname **a, const unsigned char **in, long len) \
00816         { \
00817                 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\
00818         } \
00819         EXPORT_C int i2d_##fname(const stname *a, unsigned char **out) \
00820         { \
00821                 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\
00822         } 
00823 
00824 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \
00825         EXPORT_C stname * stname##_dup(stname *x) \
00826         { \
00827         return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \
00828         }
00829 
00830 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \
00831                 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name)
00832 
00833 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \
00834         IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \
00835         IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname)
00836 
00837 /* external definitions for primitive types */
00838 
00839 DECLARE_ASN1_ITEM(ASN1_BOOLEAN)
00840 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN)
00841 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN)
00842 DECLARE_ASN1_ITEM(ASN1_SEQUENCE)
00843 DECLARE_ASN1_ITEM(CBIGNUM)
00844 DECLARE_ASN1_ITEM(BIGNUM)
00845 DECLARE_ASN1_ITEM(LONG)
00846 DECLARE_ASN1_ITEM(ZLONG)
00847 
00848 DECLARE_STACK_OF(ASN1_VALUE)
00849 
00850 /* Functions used internally by the ASN1 code */
00851 
00852 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
00853 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
00854 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
00855 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it);
00856 
00857 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
00858 int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt);
00859 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it,
00860                                 int tag, int aclass, char opt, ASN1_TLC *ctx);
00861 
00862 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass);
00863 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt);
00864 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
00865 
00866 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it);
00867 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it);
00868 
00869 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it);
00870 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it);
00871 
00872 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
00873 
00874 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr);
00875 
00876 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it);
00877 
00878 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it);
00879 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it);
00880 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it);
00881 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it);
00882 
00883 #ifdef  __cplusplus
00884 }
00885 #endif
00886 #endif

Copyright © Nokia Corporation 2001-2008
Back to top