diff options
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_enc.c')
| -rw-r--r-- | src/lib/libcrypto/asn1/tasn_enc.c | 514 |
1 files changed, 352 insertions, 162 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c index c675c3c832..be19b36acd 100644 --- a/src/lib/libcrypto/asn1/tasn_enc.c +++ b/src/lib/libcrypto/asn1/tasn_enc.c | |||
| @@ -3,7 +3,7 @@ | |||
| 3 | * project 2000. | 3 | * project 2000. |
| 4 | */ | 4 | */ |
| 5 | /* ==================================================================== | 5 | /* ==================================================================== |
| 6 | * Copyright (c) 2000 The OpenSSL Project. All rights reserved. | 6 | * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. |
| 7 | * | 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without | 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions | 9 | * modification, are permitted provided that the following conditions |
| @@ -59,88 +59,119 @@ | |||
| 59 | 59 | ||
| 60 | #include <stddef.h> | 60 | #include <stddef.h> |
| 61 | #include <string.h> | 61 | #include <string.h> |
| 62 | #include "cryptlib.h" | ||
| 62 | #include <openssl/asn1.h> | 63 | #include <openssl/asn1.h> |
| 63 | #include <openssl/asn1t.h> | 64 | #include <openssl/asn1t.h> |
| 64 | #include <openssl/objects.h> | 65 | #include <openssl/objects.h> |
| 65 | 66 | ||
| 66 | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass); | 67 | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 67 | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *seq, unsigned char **out, int skcontlen, const ASN1_ITEM *item, int isset); | 68 | const ASN1_ITEM *it, |
| 69 | int tag, int aclass); | ||
| 70 | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, | ||
| 71 | int skcontlen, const ASN1_ITEM *item, | ||
| 72 | int do_sort, int iclass); | ||
| 73 | static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, | ||
| 74 | const ASN1_TEMPLATE *tt, | ||
| 75 | int tag, int aclass); | ||
| 76 | static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, | ||
| 77 | const ASN1_ITEM *it, int flags); | ||
| 78 | |||
| 79 | /* Top level i2d equivalents: the 'ndef' variant instructs the encoder | ||
| 80 | * to use indefinite length constructed encoding, where appropriate | ||
| 81 | */ | ||
| 82 | |||
| 83 | int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, | ||
| 84 | const ASN1_ITEM *it) | ||
| 85 | { | ||
| 86 | return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF); | ||
| 87 | } | ||
| 88 | |||
| 89 | int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) | ||
| 90 | { | ||
| 91 | return asn1_item_flags_i2d(val, out, it, 0); | ||
| 92 | } | ||
| 68 | 93 | ||
| 69 | /* Encode an ASN1 item, this is compatible with the | 94 | /* Encode an ASN1 item, this is use by the |
| 70 | * standard 'i2d' function. 'out' points to | 95 | * standard 'i2d' function. 'out' points to |
| 71 | * a buffer to output the data to, in future we will | 96 | * a buffer to output the data to. |
| 72 | * have more advanced versions that can output data | ||
| 73 | * a piece at a time and this will simply be a special | ||
| 74 | * case. | ||
| 75 | * | 97 | * |
| 76 | * The new i2d has one additional feature. If the output | 98 | * The new i2d has one additional feature. If the output |
| 77 | * buffer is NULL (i.e. *out == NULL) then a buffer is | 99 | * buffer is NULL (i.e. *out == NULL) then a buffer is |
| 78 | * allocated and populated with the encoding. | 100 | * allocated and populated with the encoding. |
| 79 | */ | 101 | */ |
| 80 | 102 | ||
| 81 | 103 | static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, | |
| 82 | int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) | 104 | const ASN1_ITEM *it, int flags) |
| 83 | { | 105 | { |
| 84 | if(out && !*out) { | 106 | if (out && !*out) |
| 107 | { | ||
| 85 | unsigned char *p, *buf; | 108 | unsigned char *p, *buf; |
| 86 | int len; | 109 | int len; |
| 87 | len = ASN1_item_ex_i2d(&val, NULL, it, -1, 0); | 110 | len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); |
| 88 | if(len <= 0) return len; | 111 | if (len <= 0) |
| 112 | return len; | ||
| 89 | buf = OPENSSL_malloc(len); | 113 | buf = OPENSSL_malloc(len); |
| 90 | if(!buf) return -1; | 114 | if (!buf) |
| 115 | return -1; | ||
| 91 | p = buf; | 116 | p = buf; |
| 92 | ASN1_item_ex_i2d(&val, &p, it, -1, 0); | 117 | ASN1_item_ex_i2d(&val, &p, it, -1, flags); |
| 93 | *out = buf; | 118 | *out = buf; |
| 94 | return len; | 119 | return len; |
| 120 | } | ||
| 121 | |||
| 122 | return ASN1_item_ex_i2d(&val, out, it, -1, flags); | ||
| 95 | } | 123 | } |
| 96 | |||
| 97 | return ASN1_item_ex_i2d(&val, out, it, -1, 0); | ||
| 98 | } | ||
| 99 | 124 | ||
| 100 | /* Encode an item, taking care of IMPLICIT tagging (if any). | 125 | /* Encode an item, taking care of IMPLICIT tagging (if any). |
| 101 | * This function performs the normal item handling: it can be | 126 | * This function performs the normal item handling: it can be |
| 102 | * used in external types. | 127 | * used in external types. |
| 103 | */ | 128 | */ |
| 104 | 129 | ||
| 105 | int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass) | 130 | int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 106 | { | 131 | const ASN1_ITEM *it, int tag, int aclass) |
| 132 | { | ||
| 107 | const ASN1_TEMPLATE *tt = NULL; | 133 | const ASN1_TEMPLATE *tt = NULL; |
| 108 | unsigned char *p = NULL; | 134 | unsigned char *p = NULL; |
| 109 | int i, seqcontlen, seqlen; | 135 | int i, seqcontlen, seqlen, ndef = 1; |
| 110 | ASN1_STRING *strtmp; | ||
| 111 | const ASN1_COMPAT_FUNCS *cf; | 136 | const ASN1_COMPAT_FUNCS *cf; |
| 112 | const ASN1_EXTERN_FUNCS *ef; | 137 | const ASN1_EXTERN_FUNCS *ef; |
| 113 | const ASN1_AUX *aux = it->funcs; | 138 | const ASN1_AUX *aux = it->funcs; |
| 114 | ASN1_aux_cb *asn1_cb; | 139 | ASN1_aux_cb *asn1_cb = 0; |
| 115 | if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return 0; | 140 | |
| 116 | if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb; | 141 | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) |
| 117 | else asn1_cb = 0; | 142 | return 0; |
| 118 | 143 | ||
| 119 | switch(it->itype) { | 144 | if (aux && aux->asn1_cb) |
| 145 | asn1_cb = aux->asn1_cb; | ||
| 146 | |||
| 147 | switch(it->itype) | ||
| 148 | { | ||
| 120 | 149 | ||
| 121 | case ASN1_ITYPE_PRIMITIVE: | 150 | case ASN1_ITYPE_PRIMITIVE: |
| 122 | if(it->templates) | 151 | if (it->templates) |
| 123 | return ASN1_template_i2d(pval, out, it->templates); | 152 | return asn1_template_ex_i2d(pval, out, it->templates, |
| 153 | tag, aclass); | ||
| 124 | return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); | 154 | return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); |
| 125 | break; | 155 | break; |
| 126 | 156 | ||
| 127 | case ASN1_ITYPE_MSTRING: | 157 | case ASN1_ITYPE_MSTRING: |
| 128 | strtmp = (ASN1_STRING *)*pval; | 158 | return asn1_i2d_ex_primitive(pval, out, it, -1, aclass); |
| 129 | return asn1_i2d_ex_primitive(pval, out, it, -1, 0); | ||
| 130 | 159 | ||
| 131 | case ASN1_ITYPE_CHOICE: | 160 | case ASN1_ITYPE_CHOICE: |
| 132 | if(asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) | 161 | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) |
| 133 | return 0; | 162 | return 0; |
| 134 | i = asn1_get_choice_selector(pval, it); | 163 | i = asn1_get_choice_selector(pval, it); |
| 135 | if((i >= 0) && (i < it->tcount)) { | 164 | if ((i >= 0) && (i < it->tcount)) |
| 165 | { | ||
| 136 | ASN1_VALUE **pchval; | 166 | ASN1_VALUE **pchval; |
| 137 | const ASN1_TEMPLATE *chtt; | 167 | const ASN1_TEMPLATE *chtt; |
| 138 | chtt = it->templates + i; | 168 | chtt = it->templates + i; |
| 139 | pchval = asn1_get_field_ptr(pval, chtt); | 169 | pchval = asn1_get_field_ptr(pval, chtt); |
| 140 | return ASN1_template_i2d(pchval, out, chtt); | 170 | return asn1_template_ex_i2d(pchval, out, chtt, |
| 141 | } | 171 | -1, aclass); |
| 172 | } | ||
| 142 | /* Fixme: error condition if selector out of range */ | 173 | /* Fixme: error condition if selector out of range */ |
| 143 | if(asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it)) | 174 | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it)) |
| 144 | return 0; | 175 | return 0; |
| 145 | break; | 176 | break; |
| 146 | 177 | ||
| @@ -152,136 +183,236 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it | |||
| 152 | case ASN1_ITYPE_COMPAT: | 183 | case ASN1_ITYPE_COMPAT: |
| 153 | /* old style hackery... */ | 184 | /* old style hackery... */ |
| 154 | cf = it->funcs; | 185 | cf = it->funcs; |
| 155 | if(out) p = *out; | 186 | if (out) |
| 187 | p = *out; | ||
| 156 | i = cf->asn1_i2d(*pval, out); | 188 | i = cf->asn1_i2d(*pval, out); |
| 157 | /* Fixup for IMPLICIT tag: note this messes up for tags > 30, | 189 | /* Fixup for IMPLICIT tag: note this messes up for tags > 30, |
| 158 | * but so did the old code. Tags > 30 are very rare anyway. | 190 | * but so did the old code. Tags > 30 are very rare anyway. |
| 159 | */ | 191 | */ |
| 160 | if(out && (tag != -1)) | 192 | if (out && (tag != -1)) |
| 161 | *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED); | 193 | *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED); |
| 162 | return i; | 194 | return i; |
| 163 | 195 | ||
| 196 | case ASN1_ITYPE_NDEF_SEQUENCE: | ||
| 197 | /* Use indefinite length constructed if requested */ | ||
| 198 | if (aclass & ASN1_TFLG_NDEF) ndef = 2; | ||
| 199 | /* fall through */ | ||
| 200 | |||
| 164 | case ASN1_ITYPE_SEQUENCE: | 201 | case ASN1_ITYPE_SEQUENCE: |
| 165 | i = asn1_enc_restore(&seqcontlen, out, pval, it); | 202 | i = asn1_enc_restore(&seqcontlen, out, pval, it); |
| 166 | /* An error occurred */ | 203 | /* An error occurred */ |
| 167 | if(i < 0) return 0; | 204 | if (i < 0) |
| 205 | return 0; | ||
| 168 | /* We have a valid cached encoding... */ | 206 | /* We have a valid cached encoding... */ |
| 169 | if(i > 0) return seqcontlen; | 207 | if (i > 0) |
| 208 | return seqcontlen; | ||
| 170 | /* Otherwise carry on */ | 209 | /* Otherwise carry on */ |
| 171 | seqcontlen = 0; | 210 | seqcontlen = 0; |
| 172 | /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ | 211 | /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ |
| 173 | if(tag == -1) { | 212 | if (tag == -1) |
| 213 | { | ||
| 174 | tag = V_ASN1_SEQUENCE; | 214 | tag = V_ASN1_SEQUENCE; |
| 175 | aclass = V_ASN1_UNIVERSAL; | 215 | /* Retain any other flags in aclass */ |
| 176 | } | 216 | aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) |
| 177 | if(asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) | 217 | | V_ASN1_UNIVERSAL; |
| 218 | } | ||
| 219 | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) | ||
| 178 | return 0; | 220 | return 0; |
| 179 | /* First work out sequence content length */ | 221 | /* First work out sequence content length */ |
| 180 | for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) { | 222 | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) |
| 223 | { | ||
| 181 | const ASN1_TEMPLATE *seqtt; | 224 | const ASN1_TEMPLATE *seqtt; |
| 182 | ASN1_VALUE **pseqval; | 225 | ASN1_VALUE **pseqval; |
| 183 | seqtt = asn1_do_adb(pval, tt, 1); | 226 | seqtt = asn1_do_adb(pval, tt, 1); |
| 184 | if(!seqtt) return 0; | 227 | if (!seqtt) |
| 228 | return 0; | ||
| 185 | pseqval = asn1_get_field_ptr(pval, seqtt); | 229 | pseqval = asn1_get_field_ptr(pval, seqtt); |
| 186 | /* FIXME: check for errors in enhanced version */ | 230 | /* FIXME: check for errors in enhanced version */ |
| 187 | /* FIXME: special handling of indefinite length encoding */ | 231 | seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt, |
| 188 | seqcontlen += ASN1_template_i2d(pseqval, NULL, seqtt); | 232 | -1, aclass); |
| 189 | } | 233 | } |
| 190 | seqlen = ASN1_object_size(1, seqcontlen, tag); | 234 | |
| 191 | if(!out) return seqlen; | 235 | seqlen = ASN1_object_size(ndef, seqcontlen, tag); |
| 236 | if (!out) | ||
| 237 | return seqlen; | ||
| 192 | /* Output SEQUENCE header */ | 238 | /* Output SEQUENCE header */ |
| 193 | ASN1_put_object(out, 1, seqcontlen, tag, aclass); | 239 | ASN1_put_object(out, ndef, seqcontlen, tag, aclass); |
| 194 | for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) { | 240 | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) |
| 241 | { | ||
| 195 | const ASN1_TEMPLATE *seqtt; | 242 | const ASN1_TEMPLATE *seqtt; |
| 196 | ASN1_VALUE **pseqval; | 243 | ASN1_VALUE **pseqval; |
| 197 | seqtt = asn1_do_adb(pval, tt, 1); | 244 | seqtt = asn1_do_adb(pval, tt, 1); |
| 198 | if(!seqtt) return 0; | 245 | if (!seqtt) |
| 246 | return 0; | ||
| 199 | pseqval = asn1_get_field_ptr(pval, seqtt); | 247 | pseqval = asn1_get_field_ptr(pval, seqtt); |
| 200 | /* FIXME: check for errors in enhanced version */ | 248 | /* FIXME: check for errors in enhanced version */ |
| 201 | ASN1_template_i2d(pseqval, out, seqtt); | 249 | asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass); |
| 202 | } | 250 | } |
| 203 | if(asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it)) | 251 | if (ndef == 2) |
| 252 | ASN1_put_eoc(out); | ||
| 253 | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it)) | ||
| 204 | return 0; | 254 | return 0; |
| 205 | return seqlen; | 255 | return seqlen; |
| 206 | 256 | ||
| 207 | default: | 257 | default: |
| 208 | return 0; | 258 | return 0; |
| 209 | } | 259 | |
| 260 | } | ||
| 210 | return 0; | 261 | return 0; |
| 211 | } | 262 | } |
| 212 | 263 | ||
| 213 | int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt) | 264 | int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 214 | { | 265 | const ASN1_TEMPLATE *tt) |
| 215 | int i, ret, flags, aclass; | 266 | { |
| 267 | return asn1_template_ex_i2d(pval, out, tt, -1, 0); | ||
| 268 | } | ||
| 269 | |||
| 270 | static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, | ||
| 271 | const ASN1_TEMPLATE *tt, int tag, int iclass) | ||
| 272 | { | ||
| 273 | int i, ret, flags, ttag, tclass, ndef; | ||
| 216 | flags = tt->flags; | 274 | flags = tt->flags; |
| 217 | aclass = flags & ASN1_TFLG_TAG_CLASS; | 275 | /* Work out tag and class to use: tagging may come |
| 218 | if(flags & ASN1_TFLG_SK_MASK) { | 276 | * either from the template or the arguments, not both |
| 277 | * because this would create ambiguity. Additionally | ||
| 278 | * the iclass argument may contain some additional flags | ||
| 279 | * which should be noted and passed down to other levels. | ||
| 280 | */ | ||
| 281 | if (flags & ASN1_TFLG_TAG_MASK) | ||
| 282 | { | ||
| 283 | /* Error if argument and template tagging */ | ||
| 284 | if (tag != -1) | ||
| 285 | /* FIXME: error code here */ | ||
| 286 | return -1; | ||
| 287 | /* Get tagging from template */ | ||
| 288 | ttag = tt->tag; | ||
| 289 | tclass = flags & ASN1_TFLG_TAG_CLASS; | ||
| 290 | } | ||
| 291 | else if (tag != -1) | ||
| 292 | { | ||
| 293 | /* No template tagging, get from arguments */ | ||
| 294 | ttag = tag; | ||
| 295 | tclass = iclass & ASN1_TFLG_TAG_CLASS; | ||
| 296 | } | ||
| 297 | else | ||
| 298 | { | ||
| 299 | ttag = -1; | ||
| 300 | tclass = 0; | ||
| 301 | } | ||
| 302 | /* | ||
| 303 | * Remove any class mask from iflag. | ||
| 304 | */ | ||
| 305 | iclass &= ~ASN1_TFLG_TAG_CLASS; | ||
| 306 | |||
| 307 | /* At this point 'ttag' contains the outer tag to use, | ||
| 308 | * 'tclass' is the class and iclass is any flags passed | ||
| 309 | * to this function. | ||
| 310 | */ | ||
| 311 | |||
| 312 | /* if template and arguments require ndef, use it */ | ||
| 313 | if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF)) | ||
| 314 | ndef = 2; | ||
| 315 | else ndef = 1; | ||
| 316 | |||
| 317 | if (flags & ASN1_TFLG_SK_MASK) | ||
| 318 | { | ||
| 219 | /* SET OF, SEQUENCE OF */ | 319 | /* SET OF, SEQUENCE OF */ |
| 220 | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; | 320 | STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
| 221 | int isset, sktag, skaclass; | 321 | int isset, sktag, skaclass; |
| 222 | int skcontlen, sklen; | 322 | int skcontlen, sklen; |
| 223 | ASN1_VALUE *skitem; | 323 | ASN1_VALUE *skitem; |
| 224 | if(!*pval) return 0; | 324 | |
| 225 | if(flags & ASN1_TFLG_SET_OF) { | 325 | if (!*pval) |
| 326 | return 0; | ||
| 327 | |||
| 328 | if (flags & ASN1_TFLG_SET_OF) | ||
| 329 | { | ||
| 226 | isset = 1; | 330 | isset = 1; |
| 227 | /* 2 means we reorder */ | 331 | /* 2 means we reorder */ |
| 228 | if(flags & ASN1_TFLG_SEQUENCE_OF) isset = 2; | 332 | if (flags & ASN1_TFLG_SEQUENCE_OF) |
| 229 | } else isset = 0; | 333 | isset = 2; |
| 230 | /* First work out inner tag value */ | 334 | } |
| 231 | if(flags & ASN1_TFLG_IMPTAG) { | 335 | else isset = 0; |
| 232 | sktag = tt->tag; | 336 | |
| 233 | skaclass = aclass; | 337 | /* Work out inner tag value: if EXPLICIT |
| 234 | } else { | 338 | * or no tagging use underlying type. |
| 339 | */ | ||
| 340 | if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) | ||
| 341 | { | ||
| 342 | sktag = ttag; | ||
| 343 | skaclass = tclass; | ||
| 344 | } | ||
| 345 | else | ||
| 346 | { | ||
| 235 | skaclass = V_ASN1_UNIVERSAL; | 347 | skaclass = V_ASN1_UNIVERSAL; |
| 236 | if(isset) sktag = V_ASN1_SET; | 348 | if (isset) |
| 349 | sktag = V_ASN1_SET; | ||
| 237 | else sktag = V_ASN1_SEQUENCE; | 350 | else sktag = V_ASN1_SEQUENCE; |
| 238 | } | 351 | } |
| 239 | /* Now work out length of items */ | 352 | |
| 353 | /* Determine total length of items */ | ||
| 240 | skcontlen = 0; | 354 | skcontlen = 0; |
| 241 | for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) { | 355 | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) |
| 356 | { | ||
| 242 | skitem = sk_ASN1_VALUE_value(sk, i); | 357 | skitem = sk_ASN1_VALUE_value(sk, i); |
| 243 | skcontlen += ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item), -1, 0); | 358 | skcontlen += ASN1_item_ex_i2d(&skitem, NULL, |
| 244 | } | 359 | ASN1_ITEM_ptr(tt->item), |
| 245 | sklen = ASN1_object_size(1, skcontlen, sktag); | 360 | -1, iclass); |
| 361 | } | ||
| 362 | sklen = ASN1_object_size(ndef, skcontlen, sktag); | ||
| 246 | /* If EXPLICIT need length of surrounding tag */ | 363 | /* If EXPLICIT need length of surrounding tag */ |
| 247 | if(flags & ASN1_TFLG_EXPTAG) | 364 | if (flags & ASN1_TFLG_EXPTAG) |
| 248 | ret = ASN1_object_size(1, sklen, tt->tag); | 365 | ret = ASN1_object_size(ndef, sklen, ttag); |
| 249 | else ret = sklen; | 366 | else ret = sklen; |
| 250 | 367 | ||
| 251 | if(!out) return ret; | 368 | if (!out) |
| 369 | return ret; | ||
| 252 | 370 | ||
| 253 | /* Now encode this lot... */ | 371 | /* Now encode this lot... */ |
| 254 | /* EXPLICIT tag */ | 372 | /* EXPLICIT tag */ |
| 255 | if(flags & ASN1_TFLG_EXPTAG) | 373 | if (flags & ASN1_TFLG_EXPTAG) |
| 256 | ASN1_put_object(out, 1, sklen, tt->tag, aclass); | 374 | ASN1_put_object(out, ndef, sklen, ttag, tclass); |
| 257 | /* SET or SEQUENCE and IMPLICIT tag */ | 375 | /* SET or SEQUENCE and IMPLICIT tag */ |
| 258 | ASN1_put_object(out, 1, skcontlen, sktag, skaclass); | 376 | ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); |
| 259 | /* And finally the stuff itself */ | 377 | /* And the stuff itself */ |
| 260 | asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), isset); | 378 | asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), |
| 379 | isset, iclass); | ||
| 380 | if (ndef == 2) | ||
| 381 | { | ||
| 382 | ASN1_put_eoc(out); | ||
| 383 | if (flags & ASN1_TFLG_EXPTAG) | ||
| 384 | ASN1_put_eoc(out); | ||
| 385 | } | ||
| 261 | 386 | ||
| 262 | return ret; | 387 | return ret; |
| 263 | } | 388 | } |
| 264 | 389 | ||
| 265 | if(flags & ASN1_TFLG_EXPTAG) { | 390 | if (flags & ASN1_TFLG_EXPTAG) |
| 391 | { | ||
| 266 | /* EXPLICIT tagging */ | 392 | /* EXPLICIT tagging */ |
| 267 | /* Find length of tagged item */ | 393 | /* Find length of tagged item */ |
| 268 | i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, 0); | 394 | i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), |
| 269 | if(!i) return 0; | 395 | -1, iclass); |
| 396 | if (!i) | ||
| 397 | return 0; | ||
| 270 | /* Find length of EXPLICIT tag */ | 398 | /* Find length of EXPLICIT tag */ |
| 271 | ret = ASN1_object_size(1, i, tt->tag); | 399 | ret = ASN1_object_size(ndef, i, ttag); |
| 272 | if(out) { | 400 | if (out) |
| 401 | { | ||
| 273 | /* Output tag and item */ | 402 | /* Output tag and item */ |
| 274 | ASN1_put_object(out, 1, i, tt->tag, aclass); | 403 | ASN1_put_object(out, ndef, i, ttag, tclass); |
| 275 | ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0); | 404 | ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), |
| 276 | } | 405 | -1, iclass); |
| 406 | if (ndef == 2) | ||
| 407 | ASN1_put_eoc(out); | ||
| 408 | } | ||
| 277 | return ret; | 409 | return ret; |
| 278 | } | 410 | } |
| 279 | if(flags & ASN1_TFLG_IMPTAG) { | 411 | |
| 280 | /* IMPLICIT tagging */ | 412 | /* Either normal or IMPLICIT tagging: combine class and flags */ |
| 281 | return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), tt->tag, aclass); | 413 | return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), |
| 282 | } | 414 | ttag, tclass | iclass); |
| 283 | /* Nothing special: treat as normal */ | 415 | |
| 284 | return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0); | ||
| 285 | } | 416 | } |
| 286 | 417 | ||
| 287 | /* Temporary structure used to hold DER encoding of items for SET OF */ | 418 | /* Temporary structure used to hold DER encoding of items for SET OF */ |
| @@ -293,72 +424,90 @@ typedef struct { | |||
| 293 | } DER_ENC; | 424 | } DER_ENC; |
| 294 | 425 | ||
| 295 | static int der_cmp(const void *a, const void *b) | 426 | static int der_cmp(const void *a, const void *b) |
| 296 | { | 427 | { |
| 297 | const DER_ENC *d1 = a, *d2 = b; | 428 | const DER_ENC *d1 = a, *d2 = b; |
| 298 | int cmplen, i; | 429 | int cmplen, i; |
| 299 | cmplen = (d1->length < d2->length) ? d1->length : d2->length; | 430 | cmplen = (d1->length < d2->length) ? d1->length : d2->length; |
| 300 | i = memcmp(d1->data, d2->data, cmplen); | 431 | i = memcmp(d1->data, d2->data, cmplen); |
| 301 | if(i) return i; | 432 | if (i) |
| 433 | return i; | ||
| 302 | return d1->length - d2->length; | 434 | return d1->length - d2->length; |
| 303 | } | 435 | } |
| 304 | 436 | ||
| 305 | /* Output the content octets of SET OF or SEQUENCE OF */ | 437 | /* Output the content octets of SET OF or SEQUENCE OF */ |
| 306 | 438 | ||
| 307 | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, int skcontlen, const ASN1_ITEM *item, int do_sort) | 439 | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, |
| 308 | { | 440 | int skcontlen, const ASN1_ITEM *item, |
| 441 | int do_sort, int iclass) | ||
| 442 | { | ||
| 309 | int i; | 443 | int i; |
| 310 | ASN1_VALUE *skitem; | 444 | ASN1_VALUE *skitem; |
| 311 | unsigned char *tmpdat = NULL, *p = NULL; | 445 | unsigned char *tmpdat = NULL, *p = NULL; |
| 312 | DER_ENC *derlst = NULL, *tder; | 446 | DER_ENC *derlst = NULL, *tder; |
| 313 | if(do_sort) { | 447 | if (do_sort) |
| 448 | { | ||
| 314 | /* Don't need to sort less than 2 items */ | 449 | /* Don't need to sort less than 2 items */ |
| 315 | if(sk_ASN1_VALUE_num(sk) < 2) do_sort = 0; | 450 | if (sk_ASN1_VALUE_num(sk) < 2) |
| 316 | else { | 451 | do_sort = 0; |
| 317 | derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) * sizeof(*derlst)); | 452 | else |
| 453 | { | ||
| 454 | derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) | ||
| 455 | * sizeof(*derlst)); | ||
| 318 | tmpdat = OPENSSL_malloc(skcontlen); | 456 | tmpdat = OPENSSL_malloc(skcontlen); |
| 319 | if(!derlst || !tmpdat) return 0; | 457 | if (!derlst || !tmpdat) |
| 458 | return 0; | ||
| 459 | } | ||
| 320 | } | 460 | } |
| 321 | } | ||
| 322 | /* If not sorting just output each item */ | 461 | /* If not sorting just output each item */ |
| 323 | if(!do_sort) { | 462 | if (!do_sort) |
| 324 | for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) { | 463 | { |
| 464 | for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) | ||
| 465 | { | ||
| 325 | skitem = sk_ASN1_VALUE_value(sk, i); | 466 | skitem = sk_ASN1_VALUE_value(sk, i); |
| 326 | ASN1_item_i2d(skitem, out, item); | 467 | ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); |
| 327 | } | 468 | } |
| 328 | return 1; | 469 | return 1; |
| 329 | } | 470 | } |
| 330 | p = tmpdat; | 471 | p = tmpdat; |
| 472 | |||
| 331 | /* Doing sort: build up a list of each member's DER encoding */ | 473 | /* Doing sort: build up a list of each member's DER encoding */ |
| 332 | for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { | 474 | for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) |
| 475 | { | ||
| 333 | skitem = sk_ASN1_VALUE_value(sk, i); | 476 | skitem = sk_ASN1_VALUE_value(sk, i); |
| 334 | tder->data = p; | 477 | tder->data = p; |
| 335 | tder->length = ASN1_item_i2d(skitem, &p, item); | 478 | tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass); |
| 336 | tder->field = skitem; | 479 | tder->field = skitem; |
| 337 | } | 480 | } |
| 481 | |||
| 338 | /* Now sort them */ | 482 | /* Now sort them */ |
| 339 | qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); | 483 | qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); |
| 340 | /* Output sorted DER encoding */ | 484 | /* Output sorted DER encoding */ |
| 341 | p = *out; | 485 | p = *out; |
| 342 | for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { | 486 | for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) |
| 487 | { | ||
| 343 | memcpy(p, tder->data, tder->length); | 488 | memcpy(p, tder->data, tder->length); |
| 344 | p += tder->length; | 489 | p += tder->length; |
| 345 | } | 490 | } |
| 346 | *out = p; | 491 | *out = p; |
| 347 | /* If do_sort is 2 then reorder the STACK */ | 492 | /* If do_sort is 2 then reorder the STACK */ |
| 348 | if(do_sort == 2) { | 493 | if (do_sort == 2) |
| 349 | for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) | 494 | { |
| 350 | sk_ASN1_VALUE_set(sk, i, tder->field); | 495 | for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); |
| 351 | } | 496 | i++, tder++) |
| 497 | (void)sk_ASN1_VALUE_set(sk, i, tder->field); | ||
| 498 | } | ||
| 352 | OPENSSL_free(derlst); | 499 | OPENSSL_free(derlst); |
| 353 | OPENSSL_free(tmpdat); | 500 | OPENSSL_free(tmpdat); |
| 354 | return 1; | 501 | return 1; |
| 355 | } | 502 | } |
| 356 | 503 | ||
| 357 | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass) | 504 | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 358 | { | 505 | const ASN1_ITEM *it, int tag, int aclass) |
| 506 | { | ||
| 359 | int len; | 507 | int len; |
| 360 | int utype; | 508 | int utype; |
| 361 | int usetag; | 509 | int usetag; |
| 510 | int ndef = 0; | ||
| 362 | 511 | ||
| 363 | utype = it->utype; | 512 | utype = it->utype; |
| 364 | 513 | ||
| @@ -374,33 +523,48 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, const A | |||
| 374 | * because the call to asn1_ex_i2c() could change | 523 | * because the call to asn1_ex_i2c() could change |
| 375 | * utype. | 524 | * utype. |
| 376 | */ | 525 | */ |
| 377 | if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || | 526 | if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || |
| 378 | (utype == V_ASN1_OTHER)) | 527 | (utype == V_ASN1_OTHER)) |
| 379 | usetag = 0; | 528 | usetag = 0; |
| 380 | else usetag = 1; | 529 | else usetag = 1; |
| 381 | 530 | ||
| 382 | /* -1 means omit type */ | 531 | /* -1 means omit type */ |
| 383 | 532 | ||
| 384 | if(len == -1) return 0; | 533 | if (len == -1) |
| 534 | return 0; | ||
| 535 | |||
| 536 | /* -2 return is special meaning use ndef */ | ||
| 537 | if (len == -2) | ||
| 538 | { | ||
| 539 | ndef = 2; | ||
| 540 | len = 0; | ||
| 541 | } | ||
| 385 | 542 | ||
| 386 | /* If not implicitly tagged get tag from underlying type */ | 543 | /* If not implicitly tagged get tag from underlying type */ |
| 387 | if(tag == -1) tag = utype; | 544 | if (tag == -1) tag = utype; |
| 388 | 545 | ||
| 389 | /* Output tag+length followed by content octets */ | 546 | /* Output tag+length followed by content octets */ |
| 390 | if(out) { | 547 | if (out) |
| 391 | if(usetag) ASN1_put_object(out, 0, len, tag, aclass); | 548 | { |
| 549 | if (usetag) | ||
| 550 | ASN1_put_object(out, ndef, len, tag, aclass); | ||
| 392 | asn1_ex_i2c(pval, *out, &utype, it); | 551 | asn1_ex_i2c(pval, *out, &utype, it); |
| 393 | *out += len; | 552 | if (ndef) |
| 394 | } | 553 | ASN1_put_eoc(out); |
| 554 | else | ||
| 555 | *out += len; | ||
| 556 | } | ||
| 395 | 557 | ||
| 396 | if(usetag) return ASN1_object_size(0, len, tag); | 558 | if (usetag) |
| 559 | return ASN1_object_size(ndef, len, tag); | ||
| 397 | return len; | 560 | return len; |
| 398 | } | 561 | } |
| 399 | 562 | ||
| 400 | /* Produce content octets from a structure */ | 563 | /* Produce content octets from a structure */ |
| 401 | 564 | ||
| 402 | int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ITEM *it) | 565 | int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, |
| 403 | { | 566 | const ASN1_ITEM *it) |
| 567 | { | ||
| 404 | ASN1_BOOLEAN *tbool = NULL; | 568 | ASN1_BOOLEAN *tbool = NULL; |
| 405 | ASN1_STRING *strtmp; | 569 | ASN1_STRING *strtmp; |
| 406 | ASN1_OBJECT *otmp; | 570 | ASN1_OBJECT *otmp; |
| @@ -409,28 +573,36 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ | |||
| 409 | int len; | 573 | int len; |
| 410 | const ASN1_PRIMITIVE_FUNCS *pf; | 574 | const ASN1_PRIMITIVE_FUNCS *pf; |
| 411 | pf = it->funcs; | 575 | pf = it->funcs; |
| 412 | if(pf && pf->prim_i2c) return pf->prim_i2c(pval, cout, putype, it); | 576 | if (pf && pf->prim_i2c) |
| 577 | return pf->prim_i2c(pval, cout, putype, it); | ||
| 413 | 578 | ||
| 414 | /* Should type be omitted? */ | 579 | /* Should type be omitted? */ |
| 415 | if((it->itype != ASN1_ITYPE_PRIMITIVE) || (it->utype != V_ASN1_BOOLEAN)) { | 580 | if ((it->itype != ASN1_ITYPE_PRIMITIVE) |
| 416 | if(!*pval) return -1; | 581 | || (it->utype != V_ASN1_BOOLEAN)) |
| 417 | } | 582 | { |
| 583 | if (!*pval) return -1; | ||
| 584 | } | ||
| 418 | 585 | ||
| 419 | if(it->itype == ASN1_ITYPE_MSTRING) { | 586 | if (it->itype == ASN1_ITYPE_MSTRING) |
| 587 | { | ||
| 420 | /* If MSTRING type set the underlying type */ | 588 | /* If MSTRING type set the underlying type */ |
| 421 | strtmp = (ASN1_STRING *)*pval; | 589 | strtmp = (ASN1_STRING *)*pval; |
| 422 | utype = strtmp->type; | 590 | utype = strtmp->type; |
| 423 | *putype = utype; | 591 | *putype = utype; |
| 424 | } else if(it->utype == V_ASN1_ANY) { | 592 | } |
| 593 | else if (it->utype == V_ASN1_ANY) | ||
| 594 | { | ||
| 425 | /* If ANY set type and pointer to value */ | 595 | /* If ANY set type and pointer to value */ |
| 426 | ASN1_TYPE *typ; | 596 | ASN1_TYPE *typ; |
| 427 | typ = (ASN1_TYPE *)*pval; | 597 | typ = (ASN1_TYPE *)*pval; |
| 428 | utype = typ->type; | 598 | utype = typ->type; |
| 429 | *putype = utype; | 599 | *putype = utype; |
| 430 | pval = (ASN1_VALUE **)&typ->value.ptr; | 600 | pval = &typ->value.asn1_value; |
| 431 | } else utype = *putype; | 601 | } |
| 602 | else utype = *putype; | ||
| 432 | 603 | ||
| 433 | switch(utype) { | 604 | switch(utype) |
| 605 | { | ||
| 434 | case V_ASN1_OBJECT: | 606 | case V_ASN1_OBJECT: |
| 435 | otmp = (ASN1_OBJECT *)*pval; | 607 | otmp = (ASN1_OBJECT *)*pval; |
| 436 | cont = otmp->data; | 608 | cont = otmp->data; |
| @@ -444,12 +616,15 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ | |||
| 444 | 616 | ||
| 445 | case V_ASN1_BOOLEAN: | 617 | case V_ASN1_BOOLEAN: |
| 446 | tbool = (ASN1_BOOLEAN *)pval; | 618 | tbool = (ASN1_BOOLEAN *)pval; |
| 447 | if(*tbool == -1) return -1; | 619 | if (*tbool == -1) |
| 620 | return -1; | ||
| 448 | if (it->utype != V_ASN1_ANY) | 621 | if (it->utype != V_ASN1_ANY) |
| 449 | { | 622 | { |
| 450 | /* Default handling if value == size field then omit */ | 623 | /* Default handling if value == size field then omit */ |
| 451 | if(*tbool && (it->size > 0)) return -1; | 624 | if (*tbool && (it->size > 0)) |
| 452 | if(!*tbool && !it->size) return -1; | 625 | return -1; |
| 626 | if (!*tbool && !it->size) | ||
| 627 | return -1; | ||
| 453 | } | 628 | } |
| 454 | c = (unsigned char)*tbool; | 629 | c = (unsigned char)*tbool; |
| 455 | cont = &c; | 630 | cont = &c; |
| @@ -457,7 +632,8 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ | |||
| 457 | break; | 632 | break; |
| 458 | 633 | ||
| 459 | case V_ASN1_BIT_STRING: | 634 | case V_ASN1_BIT_STRING: |
| 460 | return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, cout ? &cout : NULL); | 635 | return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, |
| 636 | cout ? &cout : NULL); | ||
| 461 | break; | 637 | break; |
| 462 | 638 | ||
| 463 | case V_ASN1_INTEGER: | 639 | case V_ASN1_INTEGER: |
| @@ -467,7 +643,8 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ | |||
| 467 | /* These are all have the same content format | 643 | /* These are all have the same content format |
| 468 | * as ASN1_INTEGER | 644 | * as ASN1_INTEGER |
| 469 | */ | 645 | */ |
| 470 | return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL); | 646 | return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, |
| 647 | cout ? &cout : NULL); | ||
| 471 | break; | 648 | break; |
| 472 | 649 | ||
| 473 | case V_ASN1_OCTET_STRING: | 650 | case V_ASN1_OCTET_STRING: |
| @@ -489,12 +666,25 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ | |||
| 489 | default: | 666 | default: |
| 490 | /* All based on ASN1_STRING and handled the same */ | 667 | /* All based on ASN1_STRING and handled the same */ |
| 491 | strtmp = (ASN1_STRING *)*pval; | 668 | strtmp = (ASN1_STRING *)*pval; |
| 669 | /* Special handling for NDEF */ | ||
| 670 | if ((it->size == ASN1_TFLG_NDEF) | ||
| 671 | && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) | ||
| 672 | { | ||
| 673 | if (cout) | ||
| 674 | { | ||
| 675 | strtmp->data = cout; | ||
| 676 | strtmp->length = 0; | ||
| 677 | } | ||
| 678 | /* Special return code */ | ||
| 679 | return -2; | ||
| 680 | } | ||
| 492 | cont = strtmp->data; | 681 | cont = strtmp->data; |
| 493 | len = strtmp->length; | 682 | len = strtmp->length; |
| 494 | 683 | ||
| 495 | break; | 684 | break; |
| 496 | 685 | ||
| 497 | } | 686 | } |
| 498 | if(cout && len) memcpy(cout, cont, len); | 687 | if (cout && len) |
| 688 | memcpy(cout, cont, len); | ||
| 499 | return len; | 689 | return len; |
| 500 | } | 690 | } |
