Eneboo - Documentación para desarrolladores
|
00001 /* asn1t.h */ 00002 /* Written by Dr Stephen N Henson (steve@openssl.org) 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 * licensing@OpenSSL.org. 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 * (eay@cryptsoft.com). This product includes software written by Tim 00055 * Hudson (tjh@cryptsoft.com). 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 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_NDEF_SEQUENCE_cb(tname, cb) \ 00173 ASN1_SEQUENCE_cb(tname, cb) 00174 00175 #define ASN1_SEQUENCE_cb(tname, cb) \ 00176 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ 00177 ASN1_SEQUENCE(tname) 00178 00179 #define ASN1_BROKEN_SEQUENCE(tname) \ 00180 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_BROKEN, 0, 0, 0, 0}; \ 00181 ASN1_SEQUENCE(tname) 00182 00183 #define ASN1_SEQUENCE_ref(tname, cb, lck) \ 00184 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_REFCOUNT, offsetof(tname, references), lck, cb, 0}; \ 00185 ASN1_SEQUENCE(tname) 00186 00187 #define ASN1_SEQUENCE_enc(tname, enc, cb) \ 00188 static const ASN1_AUX tname##_aux = {NULL, ASN1_AFLG_ENCODING, 0, 0, cb, offsetof(tname, enc)}; \ 00189 ASN1_SEQUENCE(tname) 00190 00191 #define ASN1_NDEF_SEQUENCE_END(tname) \ 00192 ;\ 00193 ASN1_ITEM_start(tname) \ 00194 ASN1_ITYPE_NDEF_SEQUENCE,\ 00195 V_ASN1_SEQUENCE,\ 00196 tname##_seq_tt,\ 00197 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 00198 NULL,\ 00199 sizeof(tname),\ 00200 #tname \ 00201 ASN1_ITEM_end(tname) 00202 00203 #define ASN1_BROKEN_SEQUENCE_END(stname) ASN1_SEQUENCE_END_ref(stname, stname) 00204 00205 #define ASN1_SEQUENCE_END_enc(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 00206 00207 #define ASN1_SEQUENCE_END_cb(stname, tname) ASN1_SEQUENCE_END_ref(stname, tname) 00208 00209 #define ASN1_SEQUENCE_END_ref(stname, tname) \ 00210 ;\ 00211 ASN1_ITEM_start(tname) \ 00212 ASN1_ITYPE_SEQUENCE,\ 00213 V_ASN1_SEQUENCE,\ 00214 tname##_seq_tt,\ 00215 sizeof(tname##_seq_tt) / sizeof(ASN1_TEMPLATE),\ 00216 &tname##_aux,\ 00217 sizeof(stname),\ 00218 #stname \ 00219 ASN1_ITEM_end(tname) 00220 00221 00222 /* This pair helps declare a CHOICE type. We can do: 00223 * 00224 * ASN1_CHOICE(chname) = { 00225 * ... CHOICE options ... 00226 * ASN1_CHOICE_END(chname) 00227 * 00228 * This will produce an ASN1_ITEM called chname_it 00229 * for a structure called chname. The structure 00230 * definition must look like this: 00231 * typedef struct { 00232 * int type; 00233 * union { 00234 * ASN1_SOMETHING *opt1; 00235 * ASN1_SOMEOTHER *opt2; 00236 * } value; 00237 * } chname; 00238 * 00239 * the name of the selector must be 'type'. 00240 * to use an alternative selector name use the 00241 * ASN1_CHOICE_END_selector() version. 00242 */ 00243 00244 #define ASN1_CHOICE(tname) \ 00245 static const ASN1_TEMPLATE tname##_ch_tt[] 00246 00247 #define ASN1_CHOICE_cb(tname, cb) \ 00248 static const ASN1_AUX tname##_aux = {NULL, 0, 0, 0, cb, 0}; \ 00249 ASN1_CHOICE(tname) 00250 00251 #define ASN1_CHOICE_END(stname) ASN1_CHOICE_END_name(stname, stname) 00252 00253 #define ASN1_CHOICE_END_name(stname, tname) ASN1_CHOICE_END_selector(stname, tname, type) 00254 00255 #define ASN1_CHOICE_END_selector(stname, tname, selname) \ 00256 ;\ 00257 ASN1_ITEM_start(tname) \ 00258 ASN1_ITYPE_CHOICE,\ 00259 offsetof(stname,selname) ,\ 00260 tname##_ch_tt,\ 00261 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 00262 NULL,\ 00263 sizeof(stname),\ 00264 #stname \ 00265 ASN1_ITEM_end(tname) 00266 00267 #define ASN1_CHOICE_END_cb(stname, tname, selname) \ 00268 ;\ 00269 ASN1_ITEM_start(tname) \ 00270 ASN1_ITYPE_CHOICE,\ 00271 offsetof(stname,selname) ,\ 00272 tname##_ch_tt,\ 00273 sizeof(tname##_ch_tt) / sizeof(ASN1_TEMPLATE),\ 00274 &tname##_aux,\ 00275 sizeof(stname),\ 00276 #stname \ 00277 ASN1_ITEM_end(tname) 00278 00279 /* This helps with the template wrapper form of ASN1_ITEM */ 00280 00281 #define ASN1_EX_TEMPLATE_TYPE(flags, tag, name, type) { \ 00282 (flags), (tag), 0,\ 00283 #name, ASN1_ITEM_ref(type) } 00284 00285 /* These help with SEQUENCE or CHOICE components */ 00286 00287 /* used to declare other types */ 00288 00289 #define ASN1_EX_TYPE(flags, tag, stname, field, type) { \ 00290 (flags), (tag), offsetof(stname, field),\ 00291 #field, ASN1_ITEM_ref(type) } 00292 00293 /* used when the structure is combined with the parent */ 00294 00295 #define ASN1_EX_COMBINE(flags, tag, type) { \ 00296 (flags)|ASN1_TFLG_COMBINE, (tag), 0, NULL, ASN1_ITEM_ref(type) } 00297 00298 /* implicit and explicit helper macros */ 00299 00300 #define ASN1_IMP_EX(stname, field, type, tag, ex) \ 00301 ASN1_EX_TYPE(ASN1_TFLG_IMPLICIT | ex, tag, stname, field, type) 00302 00303 #define ASN1_EXP_EX(stname, field, type, tag, ex) \ 00304 ASN1_EX_TYPE(ASN1_TFLG_EXPLICIT | ex, tag, stname, field, type) 00305 00306 /* Any defined by macros: the field used is in the table itself */ 00307 00308 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION 00309 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 00310 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, (const ASN1_ITEM *)&(tblname##_adb) } 00311 #else 00312 #define ASN1_ADB_OBJECT(tblname) { ASN1_TFLG_ADB_OID, -1, 0, #tblname, tblname##_adb } 00313 #define ASN1_ADB_INTEGER(tblname) { ASN1_TFLG_ADB_INT, -1, 0, #tblname, tblname##_adb } 00314 #endif 00315 /* Plain simple type */ 00316 #define ASN1_SIMPLE(stname, field, type) ASN1_EX_TYPE(0,0, stname, field, type) 00317 00318 /* OPTIONAL simple type */ 00319 #define ASN1_OPT(stname, field, type) ASN1_EX_TYPE(ASN1_TFLG_OPTIONAL, 0, stname, field, type) 00320 00321 /* IMPLICIT tagged simple type */ 00322 #define ASN1_IMP(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, 0) 00323 00324 /* IMPLICIT tagged OPTIONAL simple type */ 00325 #define ASN1_IMP_OPT(stname, field, type, tag) ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 00326 00327 /* Same as above but EXPLICIT */ 00328 00329 #define ASN1_EXP(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, 0) 00330 #define ASN1_EXP_OPT(stname, field, type, tag) ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL) 00331 00332 /* SEQUENCE OF type */ 00333 #define ASN1_SEQUENCE_OF(stname, field, type) \ 00334 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, stname, field, type) 00335 00336 /* OPTIONAL SEQUENCE OF */ 00337 #define ASN1_SEQUENCE_OF_OPT(stname, field, type) \ 00338 ASN1_EX_TYPE(ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 00339 00340 /* Same as above but for SET OF */ 00341 00342 #define ASN1_SET_OF(stname, field, type) \ 00343 ASN1_EX_TYPE(ASN1_TFLG_SET_OF, 0, stname, field, type) 00344 00345 #define ASN1_SET_OF_OPT(stname, field, type) \ 00346 ASN1_EX_TYPE(ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL, 0, stname, field, type) 00347 00348 /* Finally compound types of SEQUENCE, SET, IMPLICIT, EXPLICIT and OPTIONAL */ 00349 00350 #define ASN1_IMP_SET_OF(stname, field, type, tag) \ 00351 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 00352 00353 #define ASN1_EXP_SET_OF(stname, field, type, tag) \ 00354 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF) 00355 00356 #define ASN1_IMP_SET_OF_OPT(stname, field, type, tag) \ 00357 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 00358 00359 #define ASN1_EXP_SET_OF_OPT(stname, field, type, tag) \ 00360 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SET_OF|ASN1_TFLG_OPTIONAL) 00361 00362 #define ASN1_IMP_SEQUENCE_OF(stname, field, type, tag) \ 00363 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 00364 00365 #define ASN1_IMP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 00366 ASN1_IMP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 00367 00368 #define ASN1_EXP_SEQUENCE_OF(stname, field, type, tag) \ 00369 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF) 00370 00371 #define ASN1_EXP_SEQUENCE_OF_OPT(stname, field, type, tag) \ 00372 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_SEQUENCE_OF|ASN1_TFLG_OPTIONAL) 00373 00374 /* EXPLICIT using indefinite length constructed form */ 00375 #define ASN1_NDEF_EXP(stname, field, type, tag) \ 00376 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_NDEF) 00377 00378 /* EXPLICIT OPTIONAL using indefinite length constructed form */ 00379 #define ASN1_NDEF_EXP_OPT(stname, field, type, tag) \ 00380 ASN1_EXP_EX(stname, field, type, tag, ASN1_TFLG_OPTIONAL|ASN1_TFLG_NDEF) 00381 00382 /* Macros for the ASN1_ADB structure */ 00383 00384 #define ASN1_ADB(name) \ 00385 static const ASN1_ADB_TABLE name##_adbtbl[] 00386 00387 #ifndef OPENSSL_EXPORT_VAR_AS_FUNCTION 00388 00389 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \ 00390 ;\ 00391 static const ASN1_ADB name##_adb = {\ 00392 flags,\ 00393 offsetof(name, field),\ 00394 app_table,\ 00395 name##_adbtbl,\ 00396 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ 00397 def,\ 00398 none\ 00399 } 00400 00401 #else 00402 00403 #define ASN1_ADB_END(name, flags, field, app_table, def, none) \ 00404 ;\ 00405 static const ASN1_ITEM *name##_adb(void) \ 00406 { \ 00407 static const ASN1_ADB internal_adb = \ 00408 {\ 00409 flags,\ 00410 offsetof(name, field),\ 00411 app_table,\ 00412 name##_adbtbl,\ 00413 sizeof(name##_adbtbl) / sizeof(ASN1_ADB_TABLE),\ 00414 def,\ 00415 none\ 00416 }; \ 00417 return (const ASN1_ITEM *) &internal_adb; \ 00418 } \ 00419 void dummy_function(void) 00420 00421 #endif 00422 00423 #define ADB_ENTRY(val, template) {val, template} 00424 00425 #define ASN1_ADB_TEMPLATE(name) \ 00426 static const ASN1_TEMPLATE name##_tt 00427 00428 /* This is the ASN1 template structure that defines 00429 * a wrapper round the actual type. It determines the 00430 * actual position of the field in the value structure, 00431 * various flags such as OPTIONAL and the field name. 00432 */ 00433 00434 struct ASN1_TEMPLATE_st { 00435 unsigned long flags; /* Various flags */ 00436 long tag; /* tag, not used if no tagging */ 00437 unsigned long offset; /* Offset of this field in structure */ 00438 #ifndef NO_ASN1_FIELD_NAMES 00439 const char *field_name; /* Field name */ 00440 #endif 00441 ASN1_ITEM_EXP *item; /* Relevant ASN1_ITEM or ASN1_ADB */ 00442 }; 00443 00444 /* Macro to extract ASN1_ITEM and ASN1_ADB pointer from ASN1_TEMPLATE */ 00445 00446 #define ASN1_TEMPLATE_item(t) (t->item_ptr) 00447 #define ASN1_TEMPLATE_adb(t) (t->item_ptr) 00448 00449 typedef struct ASN1_ADB_TABLE_st ASN1_ADB_TABLE; 00450 typedef struct ASN1_ADB_st ASN1_ADB; 00451 00452 struct ASN1_ADB_st { 00453 unsigned long flags; /* Various flags */ 00454 unsigned long offset; /* Offset of selector field */ 00455 STACK_OF(ASN1_ADB_TABLE) **app_items; /* Application defined items */ 00456 const ASN1_ADB_TABLE *tbl; /* Table of possible types */ 00457 long tblcount; /* Number of entries in tbl */ 00458 const ASN1_TEMPLATE *default_tt; /* Type to use if no match */ 00459 const ASN1_TEMPLATE *null_tt; /* Type to use if selector is NULL */ 00460 }; 00461 00462 struct ASN1_ADB_TABLE_st { 00463 long value; /* NID for an object or value for an int */ 00464 const ASN1_TEMPLATE tt; /* item for this value */ 00465 }; 00466 00467 /* template flags */ 00468 00469 /* Field is optional */ 00470 #define ASN1_TFLG_OPTIONAL (0x1) 00471 00472 /* Field is a SET OF */ 00473 #define ASN1_TFLG_SET_OF (0x1 << 1) 00474 00475 /* Field is a SEQUENCE OF */ 00476 #define ASN1_TFLG_SEQUENCE_OF (0x2 << 1) 00477 00478 /* Special case: this refers to a SET OF that 00479 * will be sorted into DER order when encoded *and* 00480 * the corresponding STACK will be modified to match 00481 * the new order. 00482 */ 00483 #define ASN1_TFLG_SET_ORDER (0x3 << 1) 00484 00485 /* Mask for SET OF or SEQUENCE OF */ 00486 #define ASN1_TFLG_SK_MASK (0x3 << 1) 00487 00488 /* These flags mean the tag should be taken from the 00489 * tag field. If EXPLICIT then the underlying type 00490 * is used for the inner tag. 00491 */ 00492 00493 /* IMPLICIT tagging */ 00494 #define ASN1_TFLG_IMPTAG (0x1 << 3) 00495 00496 00497 /* EXPLICIT tagging, inner tag from underlying type */ 00498 #define ASN1_TFLG_EXPTAG (0x2 << 3) 00499 00500 #define ASN1_TFLG_TAG_MASK (0x3 << 3) 00501 00502 /* context specific IMPLICIT */ 00503 #define ASN1_TFLG_IMPLICIT ASN1_TFLG_IMPTAG|ASN1_TFLG_CONTEXT 00504 00505 /* context specific EXPLICIT */ 00506 #define ASN1_TFLG_EXPLICIT ASN1_TFLG_EXPTAG|ASN1_TFLG_CONTEXT 00507 00508 /* If tagging is in force these determine the 00509 * type of tag to use. Otherwise the tag is 00510 * determined by the underlying type. These 00511 * values reflect the actual octet format. 00512 */ 00513 00514 /* Universal tag */ 00515 #define ASN1_TFLG_UNIVERSAL (0x0<<6) 00516 /* Application tag */ 00517 #define ASN1_TFLG_APPLICATION (0x1<<6) 00518 /* Context specific tag */ 00519 #define ASN1_TFLG_CONTEXT (0x2<<6) 00520 /* Private tag */ 00521 #define ASN1_TFLG_PRIVATE (0x3<<6) 00522 00523 #define ASN1_TFLG_TAG_CLASS (0x3<<6) 00524 00525 /* These are for ANY DEFINED BY type. In this case 00526 * the 'item' field points to an ASN1_ADB structure 00527 * which contains a table of values to decode the 00528 * relevant type 00529 */ 00530 00531 #define ASN1_TFLG_ADB_MASK (0x3<<8) 00532 00533 #define ASN1_TFLG_ADB_OID (0x1<<8) 00534 00535 #define ASN1_TFLG_ADB_INT (0x1<<9) 00536 00537 /* This flag means a parent structure is passed 00538 * instead of the field: this is useful is a 00539 * SEQUENCE is being combined with a CHOICE for 00540 * example. Since this means the structure and 00541 * item name will differ we need to use the 00542 * ASN1_CHOICE_END_name() macro for example. 00543 */ 00544 00545 #define ASN1_TFLG_COMBINE (0x1<<10) 00546 00547 /* This flag when present in a SEQUENCE OF, SET OF 00548 * or EXPLICIT causes indefinite length constructed 00549 * encoding to be used if required. 00550 */ 00551 00552 #define ASN1_TFLG_NDEF (0x1<<11) 00553 00554 /* This is the actual ASN1 item itself */ 00555 00556 struct ASN1_ITEM_st { 00557 char itype; /* The item type, primitive, SEQUENCE, CHOICE or extern */ 00558 long utype; /* underlying type */ 00559 const ASN1_TEMPLATE *templates; /* If SEQUENCE or CHOICE this contains the contents */ 00560 long tcount; /* Number of templates if SEQUENCE or CHOICE */ 00561 const void *funcs; /* functions that handle this type */ 00562 long size; /* Structure size (usually)*/ 00563 #ifndef NO_ASN1_FIELD_NAMES 00564 const char *sname; /* Structure name */ 00565 #endif 00566 }; 00567 00568 /* These are values for the itype field and 00569 * determine how the type is interpreted. 00570 * 00571 * For PRIMITIVE types the underlying type 00572 * determines the behaviour if items is NULL. 00573 * 00574 * Otherwise templates must contain a single 00575 * template and the type is treated in the 00576 * same way as the type specified in the template. 00577 * 00578 * For SEQUENCE types the templates field points 00579 * to the members, the size field is the 00580 * structure size. 00581 * 00582 * For CHOICE types the templates field points 00583 * to each possible member (typically a union) 00584 * and the 'size' field is the offset of the 00585 * selector. 00586 * 00587 * The 'funcs' field is used for application 00588 * specific functions. 00589 * 00590 * For COMPAT types the funcs field gives a 00591 * set of functions that handle this type, this 00592 * supports the old d2i, i2d convention. 00593 * 00594 * The EXTERN type uses a new style d2i/i2d. 00595 * The new style should be used where possible 00596 * because it avoids things like the d2i IMPLICIT 00597 * hack. 00598 * 00599 * MSTRING is a multiple string type, it is used 00600 * for a CHOICE of character strings where the 00601 * actual strings all occupy an ASN1_STRING 00602 * structure. In this case the 'utype' field 00603 * has a special meaning, it is used as a mask 00604 * of acceptable types using the B_ASN1 constants. 00605 * 00606 * NDEF_SEQUENCE is the same as SEQUENCE except 00607 * that it will use indefinite length constructed 00608 * encoding if requested. 00609 * 00610 */ 00611 00612 #define ASN1_ITYPE_PRIMITIVE 0x0 00613 00614 #define ASN1_ITYPE_SEQUENCE 0x1 00615 00616 #define ASN1_ITYPE_CHOICE 0x2 00617 00618 #define ASN1_ITYPE_COMPAT 0x3 00619 00620 #define ASN1_ITYPE_EXTERN 0x4 00621 00622 #define ASN1_ITYPE_MSTRING 0x5 00623 00624 #define ASN1_ITYPE_NDEF_SEQUENCE 0x6 00625 00626 /* Cache for ASN1 tag and length, so we 00627 * don't keep re-reading it for things 00628 * like CHOICE 00629 */ 00630 00631 struct ASN1_TLC_st{ 00632 char valid; /* Values below are valid */ 00633 int ret; /* return value */ 00634 long plen; /* length */ 00635 int ptag; /* class value */ 00636 int pclass; /* class value */ 00637 int hdrlen; /* header length */ 00638 }; 00639 00640 /* Typedefs for ASN1 function pointers */ 00641 00642 typedef ASN1_VALUE * ASN1_new_func(void); 00643 typedef void ASN1_free_func(ASN1_VALUE *a); 00644 typedef ASN1_VALUE * ASN1_d2i_func(ASN1_VALUE **a, const unsigned char ** in, long length); 00645 typedef int ASN1_i2d_func(ASN1_VALUE * a, unsigned char **in); 00646 00647 typedef int ASN1_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it, 00648 int tag, int aclass, char opt, ASN1_TLC *ctx); 00649 00650 typedef int ASN1_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass); 00651 typedef int ASN1_ex_new_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 00652 typedef void ASN1_ex_free_func(ASN1_VALUE **pval, const ASN1_ITEM *it); 00653 00654 typedef int ASN1_primitive_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it); 00655 typedef int ASN1_primitive_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it); 00656 00657 typedef struct ASN1_COMPAT_FUNCS_st { 00658 ASN1_new_func *asn1_new; 00659 ASN1_free_func *asn1_free; 00660 ASN1_d2i_func *asn1_d2i; 00661 ASN1_i2d_func *asn1_i2d; 00662 } ASN1_COMPAT_FUNCS; 00663 00664 typedef struct ASN1_EXTERN_FUNCS_st { 00665 void *app_data; 00666 ASN1_ex_new_func *asn1_ex_new; 00667 ASN1_ex_free_func *asn1_ex_free; 00668 ASN1_ex_free_func *asn1_ex_clear; 00669 ASN1_ex_d2i *asn1_ex_d2i; 00670 ASN1_ex_i2d *asn1_ex_i2d; 00671 } ASN1_EXTERN_FUNCS; 00672 00673 typedef struct ASN1_PRIMITIVE_FUNCS_st { 00674 void *app_data; 00675 unsigned long flags; 00676 ASN1_ex_new_func *prim_new; 00677 ASN1_ex_free_func *prim_free; 00678 ASN1_ex_free_func *prim_clear; 00679 ASN1_primitive_c2i *prim_c2i; 00680 ASN1_primitive_i2c *prim_i2c; 00681 } ASN1_PRIMITIVE_FUNCS; 00682 00683 /* This is the ASN1_AUX structure: it handles various 00684 * miscellaneous requirements. For example the use of 00685 * reference counts and an informational callback. 00686 * 00687 * The "informational callback" is called at various 00688 * points during the ASN1 encoding and decoding. It can 00689 * be used to provide minor customisation of the structures 00690 * used. This is most useful where the supplied routines 00691 * *almost* do the right thing but need some extra help 00692 * at a few points. If the callback returns zero then 00693 * it is assumed a fatal error has occurred and the 00694 * main operation should be abandoned. 00695 * 00696 * If major changes in the default behaviour are required 00697 * then an external type is more appropriate. 00698 */ 00699 00700 typedef int ASN1_aux_cb(int operation, ASN1_VALUE **in, const ASN1_ITEM *it); 00701 00702 typedef struct ASN1_AUX_st { 00703 void *app_data; 00704 int flags; 00705 int ref_offset; /* Offset of reference value */ 00706 int ref_lock; /* Lock type to use */ 00707 ASN1_aux_cb *asn1_cb; 00708 int enc_offset; /* Offset of ASN1_ENCODING structure */ 00709 } ASN1_AUX; 00710 00711 /* Flags in ASN1_AUX */ 00712 00713 /* Use a reference count */ 00714 #define ASN1_AFLG_REFCOUNT 1 00715 /* Save the encoding of structure (useful for signatures) */ 00716 #define ASN1_AFLG_ENCODING 2 00717 /* The Sequence length is invalid */ 00718 #define ASN1_AFLG_BROKEN 4 00719 00720 /* operation values for asn1_cb */ 00721 00722 #define ASN1_OP_NEW_PRE 0 00723 #define ASN1_OP_NEW_POST 1 00724 #define ASN1_OP_FREE_PRE 2 00725 #define ASN1_OP_FREE_POST 3 00726 #define ASN1_OP_D2I_PRE 4 00727 #define ASN1_OP_D2I_POST 5 00728 #define ASN1_OP_I2D_PRE 6 00729 #define ASN1_OP_I2D_POST 7 00730 00731 /* Macro to implement a primitive type */ 00732 #define IMPLEMENT_ASN1_TYPE(stname) IMPLEMENT_ASN1_TYPE_ex(stname, stname, 0) 00733 #define IMPLEMENT_ASN1_TYPE_ex(itname, vname, ex) \ 00734 ASN1_ITEM_start(itname) \ 00735 ASN1_ITYPE_PRIMITIVE, V_##vname, NULL, 0, NULL, ex, #itname \ 00736 ASN1_ITEM_end(itname) 00737 00738 /* Macro to implement a multi string type */ 00739 #define IMPLEMENT_ASN1_MSTRING(itname, mask) \ 00740 ASN1_ITEM_start(itname) \ 00741 ASN1_ITYPE_MSTRING, mask, NULL, 0, NULL, sizeof(ASN1_STRING), #itname \ 00742 ASN1_ITEM_end(itname) 00743 00744 /* Macro to implement an ASN1_ITEM in terms of old style funcs */ 00745 00746 #define IMPLEMENT_COMPAT_ASN1(sname) IMPLEMENT_COMPAT_ASN1_type(sname, V_ASN1_SEQUENCE) 00747 00748 #define IMPLEMENT_COMPAT_ASN1_type(sname, tag) \ 00749 static const ASN1_COMPAT_FUNCS sname##_ff = { \ 00750 (ASN1_new_func *)sname##_new, \ 00751 (ASN1_free_func *)sname##_free, \ 00752 (ASN1_d2i_func *)d2i_##sname, \ 00753 (ASN1_i2d_func *)i2d_##sname, \ 00754 }; \ 00755 ASN1_ITEM_start(sname) \ 00756 ASN1_ITYPE_COMPAT, \ 00757 tag, \ 00758 NULL, \ 00759 0, \ 00760 &sname##_ff, \ 00761 0, \ 00762 #sname \ 00763 ASN1_ITEM_end(sname) 00764 00765 #define IMPLEMENT_EXTERN_ASN1(sname, tag, fptrs) \ 00766 ASN1_ITEM_start(sname) \ 00767 ASN1_ITYPE_EXTERN, \ 00768 tag, \ 00769 NULL, \ 00770 0, \ 00771 &fptrs, \ 00772 0, \ 00773 #sname \ 00774 ASN1_ITEM_end(sname) 00775 00776 /* Macro to implement standard functions in terms of ASN1_ITEM structures */ 00777 00778 #define IMPLEMENT_ASN1_FUNCTIONS(stname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, stname, stname) 00779 00780 #define IMPLEMENT_ASN1_FUNCTIONS_name(stname, itname) IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, itname) 00781 00782 #define IMPLEMENT_ASN1_FUNCTIONS_ENCODE_name(stname, itname) \ 00783 IMPLEMENT_ASN1_FUNCTIONS_ENCODE_fname(stname, itname, itname) 00784 00785 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS(stname) \ 00786 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, stname, stname) 00787 00788 #define IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) \ 00789 stname *fname##_new(void) \ 00790 { \ 00791 return (stname *)ASN1_item_new(ASN1_ITEM_rptr(itname)); \ 00792 } \ 00793 void fname##_free(stname *a) \ 00794 { \ 00795 ASN1_item_free((ASN1_VALUE *)a, ASN1_ITEM_rptr(itname)); \ 00796 } 00797 00798 #define IMPLEMENT_ASN1_FUNCTIONS_fname(stname, itname, fname) \ 00799 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 00800 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 00801 00802 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_fname(stname, itname, fname) \ 00803 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 00804 { \ 00805 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 00806 } \ 00807 int i2d_##fname(stname *a, unsigned char **out) \ 00808 { \ 00809 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 00810 } 00811 00812 #define IMPLEMENT_ASN1_NDEF_FUNCTION(stname) \ 00813 int i2d_##stname##_NDEF(stname *a, unsigned char **out) \ 00814 { \ 00815 return ASN1_item_ndef_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(stname));\ 00816 } 00817 00818 /* This includes evil casts to remove const: they will go away when full 00819 * ASN1 constification is done. 00820 */ 00821 #define IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 00822 stname *d2i_##fname(stname **a, const unsigned char **in, long len) \ 00823 { \ 00824 return (stname *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, ASN1_ITEM_rptr(itname));\ 00825 } \ 00826 int i2d_##fname(const stname *a, unsigned char **out) \ 00827 { \ 00828 return ASN1_item_i2d((ASN1_VALUE *)a, out, ASN1_ITEM_rptr(itname));\ 00829 } 00830 00831 #define IMPLEMENT_ASN1_DUP_FUNCTION(stname) \ 00832 stname * stname##_dup(stname *x) \ 00833 { \ 00834 return ASN1_item_dup(ASN1_ITEM_rptr(stname), x); \ 00835 } 00836 00837 #define IMPLEMENT_ASN1_FUNCTIONS_const(name) \ 00838 IMPLEMENT_ASN1_FUNCTIONS_const_fname(name, name, name) 00839 00840 #define IMPLEMENT_ASN1_FUNCTIONS_const_fname(stname, itname, fname) \ 00841 IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(stname, itname, fname) \ 00842 IMPLEMENT_ASN1_ALLOC_FUNCTIONS_fname(stname, itname, fname) 00843 00844 /* external definitions for primitive types */ 00845 00846 DECLARE_ASN1_ITEM(ASN1_BOOLEAN) 00847 DECLARE_ASN1_ITEM(ASN1_TBOOLEAN) 00848 DECLARE_ASN1_ITEM(ASN1_FBOOLEAN) 00849 DECLARE_ASN1_ITEM(ASN1_SEQUENCE) 00850 DECLARE_ASN1_ITEM(CBIGNUM) 00851 DECLARE_ASN1_ITEM(BIGNUM) 00852 DECLARE_ASN1_ITEM(LONG) 00853 DECLARE_ASN1_ITEM(ZLONG) 00854 00855 DECLARE_STACK_OF(ASN1_VALUE) 00856 00857 /* Functions used internally by the ASN1 code */ 00858 00859 int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 00860 void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 00861 int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 00862 int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it); 00863 00864 void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 00865 int ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_TEMPLATE *tt); 00866 int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, const ASN1_ITEM *it, 00867 int tag, int aclass, char opt, ASN1_TLC *ctx); 00868 00869 int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass); 00870 int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt); 00871 void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 00872 00873 int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *putype, const ASN1_ITEM *it); 00874 int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype, char *free_cont, const ASN1_ITEM *it); 00875 00876 int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it); 00877 int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it); 00878 00879 ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 00880 00881 const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr); 00882 00883 int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it); 00884 00885 void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it); 00886 void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it); 00887 int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, const ASN1_ITEM *it); 00888 int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, const ASN1_ITEM *it); 00889 00890 #ifdef __cplusplus 00891 } 00892 #endif 00893 #endif