| 1 |
/* Copyright (C) 1995-1998 Eric Young ([email protected]) |
| 2 |
* All rights reserved. |
| 3 |
* |
| 4 |
* This package is an SSL implementation written |
| 5 |
* by Eric Young ([email protected]). |
| 6 |
* The implementation was written so as to conform with Netscapes SSL. |
| 7 |
* |
| 8 |
* This library is free for commercial and non-commercial use as long as |
| 9 |
* the following conditions are aheared to. The following conditions |
| 10 |
* apply to all code found in this distribution, be it the RC4, RSA, |
| 11 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 |
* included with this distribution is covered by the same copyright terms |
| 13 |
* except that the holder is Tim Hudson ([email protected]). |
| 14 |
* |
| 15 |
* Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 |
* the code are not to be removed. |
| 17 |
* If this package is used in a product, Eric Young should be given attribution |
| 18 |
* as the author of the parts of the library used. |
| 19 |
* This can be in the form of a textual message at program startup or |
| 20 |
* in documentation (online or textual) provided with the package. |
| 21 |
* |
| 22 |
* Redistribution and use in source and binary forms, with or without |
| 23 |
* modification, are permitted provided that the following conditions |
| 24 |
* are met: |
| 25 |
* 1. Redistributions of source code must retain the copyright |
| 26 |
* notice, this list of conditions and the following disclaimer. |
| 27 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 28 |
* notice, this list of conditions and the following disclaimer in the |
| 29 |
* documentation and/or other materials provided with the distribution. |
| 30 |
* 3. All advertising materials mentioning features or use of this software |
| 31 |
* must display the following acknowledgement: |
| 32 |
* "This product includes cryptographic software written by |
| 33 |
* Eric Young ([email protected])" |
| 34 |
* The word 'cryptographic' can be left out if the rouines from the library |
| 35 |
* being used are not cryptographic related :-). |
| 36 |
* 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 |
* the apps directory (application code) you must include an acknowledgement: |
| 38 |
* "This product includes software written by Tim Hudson ([email protected])" |
| 39 |
* |
| 40 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 |
* SUCH DAMAGE. |
| 51 |
* |
| 52 |
* The licence and distribution terms for any publically available version or |
| 53 |
* derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 |
* copied and put under another distribution licence |
| 55 |
* [including the GNU Public Licence.] */ |
| 56 |
|
| 57 |
#include <openssl/asn1.h> |
| 58 |
|
| 59 |
#include <string.h> |
| 60 |
|
| 61 |
#include <openssl/asn1t.h> |
| 62 |
#include <openssl/mem.h> |
| 63 |
|
| 64 |
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 65 |
const ASN1_ITEM *it, int tag, int aclass); |
| 66 |
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, |
| 67 |
int skcontlen, const ASN1_ITEM *item, |
| 68 |
int do_sort, int iclass); |
| 69 |
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 70 |
const ASN1_TEMPLATE *tt, int tag, int aclass); |
| 71 |
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, |
| 72 |
const ASN1_ITEM *it, int flags); |
| 73 |
|
| 74 |
/* |
| 75 |
* Top level i2d equivalents: the 'ndef' variant instructs the encoder to use |
| 76 |
* indefinite length constructed encoding, where appropriate |
| 77 |
*/ |
| 78 |
|
| 79 |
int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, |
| 80 |
const ASN1_ITEM *it) |
| 81 |
{ |
| 82 |
return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF); |
| 83 |
} |
| 84 |
|
| 85 |
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) |
| 86 |
{ |
| 87 |
return asn1_item_flags_i2d(val, out, it, 0); |
| 88 |
} |
| 89 |
|
| 90 |
/* |
| 91 |
* Encode an ASN1 item, this is use by the standard 'i2d' function. 'out' |
| 92 |
* points to a buffer to output the data to. The new i2d has one additional |
| 93 |
* feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is |
| 94 |
* allocated and populated with the encoding. |
| 95 |
*/ |
| 96 |
|
| 97 |
static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, |
| 98 |
const ASN1_ITEM *it, int flags) |
| 99 |
{ |
| 100 |
if (out && !*out) { |
| 101 |
unsigned char *p, *buf; |
| 102 |
int len; |
| 103 |
len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); |
| 104 |
if (len <= 0) |
| 105 |
return len; |
| 106 |
buf = OPENSSL_malloc(len); |
| 107 |
if (!buf) |
| 108 |
return -1; |
| 109 |
p = buf; |
| 110 |
ASN1_item_ex_i2d(&val, &p, it, -1, flags); |
| 111 |
*out = buf; |
| 112 |
return len; |
| 113 |
} |
| 114 |
|
| 115 |
return ASN1_item_ex_i2d(&val, out, it, -1, flags); |
| 116 |
} |
| 117 |
|
| 118 |
/* |
| 119 |
* Encode an item, taking care of IMPLICIT tagging (if any). This function |
| 120 |
* performs the normal item handling: it can be used in external types. |
| 121 |
*/ |
| 122 |
|
| 123 |
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 124 |
const ASN1_ITEM *it, int tag, int aclass) |
| 125 |
{ |
| 126 |
const ASN1_TEMPLATE *tt = NULL; |
| 127 |
unsigned char *p = NULL; |
| 128 |
int i, seqcontlen, seqlen, ndef = 1; |
| 129 |
const ASN1_COMPAT_FUNCS *cf; |
| 130 |
const ASN1_EXTERN_FUNCS *ef; |
| 131 |
const ASN1_AUX *aux = it->funcs; |
| 132 |
ASN1_aux_cb *asn1_cb = 0; |
| 133 |
|
| 134 |
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) |
| 135 |
return 0; |
| 136 |
|
| 137 |
if (aux && aux->asn1_cb) |
| 138 |
asn1_cb = aux->asn1_cb; |
| 139 |
|
| 140 |
switch (it->itype) { |
| 141 |
|
| 142 |
case ASN1_ITYPE_PRIMITIVE: |
| 143 |
if (it->templates) |
| 144 |
return asn1_template_ex_i2d(pval, out, it->templates, |
| 145 |
tag, aclass); |
| 146 |
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); |
| 147 |
break; |
| 148 |
|
| 149 |
case ASN1_ITYPE_MSTRING: |
| 150 |
return asn1_i2d_ex_primitive(pval, out, it, -1, aclass); |
| 151 |
|
| 152 |
case ASN1_ITYPE_CHOICE: |
| 153 |
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) |
| 154 |
return 0; |
| 155 |
i = asn1_get_choice_selector(pval, it); |
| 156 |
if ((i >= 0) && (i < it->tcount)) { |
| 157 |
ASN1_VALUE **pchval; |
| 158 |
const ASN1_TEMPLATE *chtt; |
| 159 |
chtt = it->templates + i; |
| 160 |
pchval = asn1_get_field_ptr(pval, chtt); |
| 161 |
return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass); |
| 162 |
} |
| 163 |
/* Fixme: error condition if selector out of range */ |
| 164 |
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) |
| 165 |
return 0; |
| 166 |
break; |
| 167 |
|
| 168 |
case ASN1_ITYPE_EXTERN: |
| 169 |
/* If new style i2d it does all the work */ |
| 170 |
ef = it->funcs; |
| 171 |
return ef->asn1_ex_i2d(pval, out, it, tag, aclass); |
| 172 |
|
| 173 |
case ASN1_ITYPE_COMPAT: |
| 174 |
/* old style hackery... */ |
| 175 |
cf = it->funcs; |
| 176 |
if (out) |
| 177 |
p = *out; |
| 178 |
i = cf->asn1_i2d(*pval, out); |
| 179 |
/* |
| 180 |
* Fixup for IMPLICIT tag: note this messes up for tags > 30, but so |
| 181 |
* did the old code. Tags > 30 are very rare anyway. |
| 182 |
*/ |
| 183 |
if (out && (tag != -1)) |
| 184 |
*p = aclass | tag | (*p & V_ASN1_CONSTRUCTED); |
| 185 |
return i; |
| 186 |
|
| 187 |
case ASN1_ITYPE_NDEF_SEQUENCE: |
| 188 |
/* Use indefinite length constructed if requested */ |
| 189 |
if (aclass & ASN1_TFLG_NDEF) |
| 190 |
ndef = 2; |
| 191 |
/* fall through */ |
| 192 |
|
| 193 |
case ASN1_ITYPE_SEQUENCE: |
| 194 |
i = asn1_enc_restore(&seqcontlen, out, pval, it); |
| 195 |
/* An error occurred */ |
| 196 |
if (i < 0) |
| 197 |
return 0; |
| 198 |
/* We have a valid cached encoding... */ |
| 199 |
if (i > 0) |
| 200 |
return seqcontlen; |
| 201 |
/* Otherwise carry on */ |
| 202 |
seqcontlen = 0; |
| 203 |
/* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ |
| 204 |
if (tag == -1) { |
| 205 |
tag = V_ASN1_SEQUENCE; |
| 206 |
/* Retain any other flags in aclass */ |
| 207 |
aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) |
| 208 |
| V_ASN1_UNIVERSAL; |
| 209 |
} |
| 210 |
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) |
| 211 |
return 0; |
| 212 |
/* First work out sequence content length */ |
| 213 |
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
| 214 |
const ASN1_TEMPLATE *seqtt; |
| 215 |
ASN1_VALUE **pseqval; |
| 216 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 217 |
if (!seqtt) |
| 218 |
return 0; |
| 219 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 220 |
/* FIXME: check for errors in enhanced version */ |
| 221 |
seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt, |
| 222 |
-1, aclass); |
| 223 |
} |
| 224 |
|
| 225 |
seqlen = ASN1_object_size(ndef, seqcontlen, tag); |
| 226 |
if (!out) |
| 227 |
return seqlen; |
| 228 |
/* Output SEQUENCE header */ |
| 229 |
ASN1_put_object(out, ndef, seqcontlen, tag, aclass); |
| 230 |
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
| 231 |
const ASN1_TEMPLATE *seqtt; |
| 232 |
ASN1_VALUE **pseqval; |
| 233 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 234 |
if (!seqtt) |
| 235 |
return 0; |
| 236 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 237 |
/* FIXME: check for errors in enhanced version */ |
| 238 |
asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass); |
| 239 |
} |
| 240 |
if (ndef == 2) |
| 241 |
ASN1_put_eoc(out); |
| 242 |
if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) |
| 243 |
return 0; |
| 244 |
return seqlen; |
| 245 |
|
| 246 |
default: |
| 247 |
return 0; |
| 248 |
|
| 249 |
} |
| 250 |
return 0; |
| 251 |
} |
| 252 |
|
| 253 |
int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 254 |
const ASN1_TEMPLATE *tt) |
| 255 |
{ |
| 256 |
return asn1_template_ex_i2d(pval, out, tt, -1, 0); |
| 257 |
} |
| 258 |
|
| 259 |
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 260 |
const ASN1_TEMPLATE *tt, int tag, int iclass) |
| 261 |
{ |
| 262 |
int i, ret, flags, ttag, tclass, ndef; |
| 263 |
size_t j; |
| 264 |
flags = tt->flags; |
| 265 |
/* |
| 266 |
* Work out tag and class to use: tagging may come either from the |
| 267 |
* template or the arguments, not both because this would create |
| 268 |
* ambiguity. Additionally the iclass argument may contain some |
| 269 |
* additional flags which should be noted and passed down to other |
| 270 |
* levels. |
| 271 |
*/ |
| 272 |
if (flags & ASN1_TFLG_TAG_MASK) { |
| 273 |
/* Error if argument and template tagging */ |
| 274 |
if (tag != -1) |
| 275 |
/* FIXME: error code here */ |
| 276 |
return -1; |
| 277 |
/* Get tagging from template */ |
| 278 |
ttag = tt->tag; |
| 279 |
tclass = flags & ASN1_TFLG_TAG_CLASS; |
| 280 |
} else if (tag != -1) { |
| 281 |
/* No template tagging, get from arguments */ |
| 282 |
ttag = tag; |
| 283 |
tclass = iclass & ASN1_TFLG_TAG_CLASS; |
| 284 |
} else { |
| 285 |
ttag = -1; |
| 286 |
tclass = 0; |
| 287 |
} |
| 288 |
/* |
| 289 |
* Remove any class mask from iflag. |
| 290 |
*/ |
| 291 |
iclass &= ~ASN1_TFLG_TAG_CLASS; |
| 292 |
|
| 293 |
/* |
| 294 |
* At this point 'ttag' contains the outer tag to use, 'tclass' is the |
| 295 |
* class and iclass is any flags passed to this function. |
| 296 |
*/ |
| 297 |
|
| 298 |
/* if template and arguments require ndef, use it */ |
| 299 |
if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF)) |
| 300 |
ndef = 2; |
| 301 |
else |
| 302 |
ndef = 1; |
| 303 |
|
| 304 |
if (flags & ASN1_TFLG_SK_MASK) { |
| 305 |
/* SET OF, SEQUENCE OF */ |
| 306 |
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
| 307 |
int isset, sktag, skaclass; |
| 308 |
int skcontlen, sklen; |
| 309 |
ASN1_VALUE *skitem; |
| 310 |
|
| 311 |
if (!*pval) |
| 312 |
return 0; |
| 313 |
|
| 314 |
if (flags & ASN1_TFLG_SET_OF) { |
| 315 |
isset = 1; |
| 316 |
/* 2 means we reorder */ |
| 317 |
if (flags & ASN1_TFLG_SEQUENCE_OF) |
| 318 |
isset = 2; |
| 319 |
} else |
| 320 |
isset = 0; |
| 321 |
|
| 322 |
/* |
| 323 |
* Work out inner tag value: if EXPLICIT or no tagging use underlying |
| 324 |
* type. |
| 325 |
*/ |
| 326 |
if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) { |
| 327 |
sktag = ttag; |
| 328 |
skaclass = tclass; |
| 329 |
} else { |
| 330 |
skaclass = V_ASN1_UNIVERSAL; |
| 331 |
if (isset) |
| 332 |
sktag = V_ASN1_SET; |
| 333 |
else |
| 334 |
sktag = V_ASN1_SEQUENCE; |
| 335 |
} |
| 336 |
|
| 337 |
/* Determine total length of items */ |
| 338 |
skcontlen = 0; |
| 339 |
for (j = 0; j < sk_ASN1_VALUE_num(sk); j++) { |
| 340 |
skitem = sk_ASN1_VALUE_value(sk, j); |
| 341 |
skcontlen += ASN1_item_ex_i2d(&skitem, NULL, |
| 342 |
ASN1_ITEM_ptr(tt->item), |
| 343 |
-1, iclass); |
| 344 |
} |
| 345 |
sklen = ASN1_object_size(ndef, skcontlen, sktag); |
| 346 |
/* If EXPLICIT need length of surrounding tag */ |
| 347 |
if (flags & ASN1_TFLG_EXPTAG) |
| 348 |
ret = ASN1_object_size(ndef, sklen, ttag); |
| 349 |
else |
| 350 |
ret = sklen; |
| 351 |
|
| 352 |
if (!out) |
| 353 |
return ret; |
| 354 |
|
| 355 |
/* Now encode this lot... */ |
| 356 |
/* EXPLICIT tag */ |
| 357 |
if (flags & ASN1_TFLG_EXPTAG) |
| 358 |
ASN1_put_object(out, ndef, sklen, ttag, tclass); |
| 359 |
/* SET or SEQUENCE and IMPLICIT tag */ |
| 360 |
ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); |
| 361 |
/* And the stuff itself */ |
| 362 |
asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), |
| 363 |
isset, iclass); |
| 364 |
if (ndef == 2) { |
| 365 |
ASN1_put_eoc(out); |
| 366 |
if (flags & ASN1_TFLG_EXPTAG) |
| 367 |
ASN1_put_eoc(out); |
| 368 |
} |
| 369 |
|
| 370 |
return ret; |
| 371 |
} |
| 372 |
|
| 373 |
if (flags & ASN1_TFLG_EXPTAG) { |
| 374 |
/* EXPLICIT tagging */ |
| 375 |
/* Find length of tagged item */ |
| 376 |
i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass); |
| 377 |
if (!i) |
| 378 |
return 0; |
| 379 |
/* Find length of EXPLICIT tag */ |
| 380 |
ret = ASN1_object_size(ndef, i, ttag); |
| 381 |
if (out) { |
| 382 |
/* Output tag and item */ |
| 383 |
ASN1_put_object(out, ndef, i, ttag, tclass); |
| 384 |
ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass); |
| 385 |
if (ndef == 2) |
| 386 |
ASN1_put_eoc(out); |
| 387 |
} |
| 388 |
return ret; |
| 389 |
} |
| 390 |
|
| 391 |
/* Either normal or IMPLICIT tagging: combine class and flags */ |
| 392 |
return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), |
| 393 |
ttag, tclass | iclass); |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
/* Temporary structure used to hold DER encoding of items for SET OF */ |
| 398 |
|
| 399 |
typedef struct { |
| 400 |
unsigned char *data; |
| 401 |
int length; |
| 402 |
ASN1_VALUE *field; |
| 403 |
} DER_ENC; |
| 404 |
|
| 405 |
static int der_cmp(const void *a, const void *b) |
| 406 |
{ |
| 407 |
const DER_ENC *d1 = a, *d2 = b; |
| 408 |
int cmplen, i; |
| 409 |
cmplen = (d1->length < d2->length) ? d1->length : d2->length; |
| 410 |
i = memcmp(d1->data, d2->data, cmplen); |
| 411 |
if (i) |
| 412 |
return i; |
| 413 |
return d1->length - d2->length; |
| 414 |
} |
| 415 |
|
| 416 |
/* Output the content octets of SET OF or SEQUENCE OF */ |
| 417 |
|
| 418 |
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, |
| 419 |
int skcontlen, const ASN1_ITEM *item, |
| 420 |
int do_sort, int iclass) |
| 421 |
{ |
| 422 |
size_t i; |
| 423 |
ASN1_VALUE *skitem; |
| 424 |
unsigned char *tmpdat = NULL, *p = NULL; |
| 425 |
DER_ENC *derlst = NULL, *tder; |
| 426 |
if (do_sort) { |
| 427 |
/* Don't need to sort less than 2 items */ |
| 428 |
if (sk_ASN1_VALUE_num(sk) < 2) |
| 429 |
do_sort = 0; |
| 430 |
else { |
| 431 |
derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) |
| 432 |
* sizeof(*derlst)); |
| 433 |
if (!derlst) |
| 434 |
return 0; |
| 435 |
tmpdat = OPENSSL_malloc(skcontlen); |
| 436 |
if (!tmpdat) { |
| 437 |
OPENSSL_free(derlst); |
| 438 |
return 0; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
/* If not sorting just output each item */ |
| 443 |
if (!do_sort) { |
| 444 |
for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
| 445 |
skitem = sk_ASN1_VALUE_value(sk, i); |
| 446 |
ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); |
| 447 |
} |
| 448 |
return 1; |
| 449 |
} |
| 450 |
p = tmpdat; |
| 451 |
|
| 452 |
/* Doing sort: build up a list of each member's DER encoding */ |
| 453 |
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { |
| 454 |
skitem = sk_ASN1_VALUE_value(sk, i); |
| 455 |
tder->data = p; |
| 456 |
tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass); |
| 457 |
tder->field = skitem; |
| 458 |
} |
| 459 |
|
| 460 |
/* Now sort them */ |
| 461 |
qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); |
| 462 |
/* Output sorted DER encoding */ |
| 463 |
p = *out; |
| 464 |
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { |
| 465 |
memcpy(p, tder->data, tder->length); |
| 466 |
p += tder->length; |
| 467 |
} |
| 468 |
*out = p; |
| 469 |
/* If do_sort is 2 then reorder the STACK */ |
| 470 |
if (do_sort == 2) { |
| 471 |
for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) |
| 472 |
(void)sk_ASN1_VALUE_set(sk, i, tder->field); |
| 473 |
} |
| 474 |
OPENSSL_free(derlst); |
| 475 |
OPENSSL_free(tmpdat); |
| 476 |
return 1; |
| 477 |
} |
| 478 |
|
| 479 |
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 480 |
const ASN1_ITEM *it, int tag, int aclass) |
| 481 |
{ |
| 482 |
int len; |
| 483 |
int utype; |
| 484 |
int usetag; |
| 485 |
int ndef = 0; |
| 486 |
|
| 487 |
utype = it->utype; |
| 488 |
|
| 489 |
/* |
| 490 |
* Get length of content octets and maybe find out the underlying type. |
| 491 |
*/ |
| 492 |
|
| 493 |
len = asn1_ex_i2c(pval, NULL, &utype, it); |
| 494 |
|
| 495 |
/* |
| 496 |
* If SEQUENCE, SET or OTHER then header is included in pseudo content |
| 497 |
* octets so don't include tag+length. We need to check here because the |
| 498 |
* call to asn1_ex_i2c() could change utype. |
| 499 |
*/ |
| 500 |
if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || |
| 501 |
(utype == V_ASN1_OTHER)) |
| 502 |
usetag = 0; |
| 503 |
else |
| 504 |
usetag = 1; |
| 505 |
|
| 506 |
/* -1 means omit type */ |
| 507 |
|
| 508 |
if (len == -1) |
| 509 |
return 0; |
| 510 |
|
| 511 |
/* -2 return is special meaning use ndef */ |
| 512 |
if (len == -2) { |
| 513 |
ndef = 2; |
| 514 |
len = 0; |
| 515 |
} |
| 516 |
|
| 517 |
/* If not implicitly tagged get tag from underlying type */ |
| 518 |
if (tag == -1) |
| 519 |
tag = utype; |
| 520 |
|
| 521 |
/* Output tag+length followed by content octets */ |
| 522 |
if (out) { |
| 523 |
if (usetag) |
| 524 |
ASN1_put_object(out, ndef, len, tag, aclass); |
| 525 |
asn1_ex_i2c(pval, *out, &utype, it); |
| 526 |
if (ndef) |
| 527 |
ASN1_put_eoc(out); |
| 528 |
else |
| 529 |
*out += len; |
| 530 |
} |
| 531 |
|
| 532 |
if (usetag) |
| 533 |
return ASN1_object_size(ndef, len, tag); |
| 534 |
return len; |
| 535 |
} |
| 536 |
|
| 537 |
/* Produce content octets from a structure */ |
| 538 |
|
| 539 |
int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, |
| 540 |
const ASN1_ITEM *it) |
| 541 |
{ |
| 542 |
ASN1_BOOLEAN *tbool = NULL; |
| 543 |
ASN1_STRING *strtmp; |
| 544 |
ASN1_OBJECT *otmp; |
| 545 |
int utype; |
| 546 |
const unsigned char *cont; |
| 547 |
unsigned char c; |
| 548 |
int len; |
| 549 |
const ASN1_PRIMITIVE_FUNCS *pf; |
| 550 |
pf = it->funcs; |
| 551 |
if (pf && pf->prim_i2c) |
| 552 |
return pf->prim_i2c(pval, cout, putype, it); |
| 553 |
|
| 554 |
/* Should type be omitted? */ |
| 555 |
if ((it->itype != ASN1_ITYPE_PRIMITIVE) |
| 556 |
|| (it->utype != V_ASN1_BOOLEAN)) { |
| 557 |
if (!*pval) |
| 558 |
return -1; |
| 559 |
} |
| 560 |
|
| 561 |
if (it->itype == ASN1_ITYPE_MSTRING) { |
| 562 |
/* If MSTRING type set the underlying type */ |
| 563 |
strtmp = (ASN1_STRING *)*pval; |
| 564 |
utype = strtmp->type; |
| 565 |
*putype = utype; |
| 566 |
} else if (it->utype == V_ASN1_ANY) { |
| 567 |
/* If ANY set type and pointer to value */ |
| 568 |
ASN1_TYPE *typ; |
| 569 |
typ = (ASN1_TYPE *)*pval; |
| 570 |
utype = typ->type; |
| 571 |
*putype = utype; |
| 572 |
pval = &typ->value.asn1_value; |
| 573 |
} else |
| 574 |
utype = *putype; |
| 575 |
|
| 576 |
switch (utype) { |
| 577 |
case V_ASN1_OBJECT: |
| 578 |
otmp = (ASN1_OBJECT *)*pval; |
| 579 |
cont = otmp->data; |
| 580 |
len = otmp->length; |
| 581 |
break; |
| 582 |
|
| 583 |
case V_ASN1_NULL: |
| 584 |
cont = NULL; |
| 585 |
len = 0; |
| 586 |
break; |
| 587 |
|
| 588 |
case V_ASN1_BOOLEAN: |
| 589 |
tbool = (ASN1_BOOLEAN *)pval; |
| 590 |
if (*tbool == -1) |
| 591 |
return -1; |
| 592 |
if (it->utype != V_ASN1_ANY) { |
| 593 |
/* |
| 594 |
* Default handling if value == size field then omit |
| 595 |
*/ |
| 596 |
if (*tbool && (it->size > 0)) |
| 597 |
return -1; |
| 598 |
if (!*tbool && !it->size) |
| 599 |
return -1; |
| 600 |
} |
| 601 |
c = (unsigned char)*tbool; |
| 602 |
cont = &c; |
| 603 |
len = 1; |
| 604 |
break; |
| 605 |
|
| 606 |
case V_ASN1_BIT_STRING: |
| 607 |
return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, |
| 608 |
cout ? &cout : NULL); |
| 609 |
break; |
| 610 |
|
| 611 |
case V_ASN1_INTEGER: |
| 612 |
case V_ASN1_ENUMERATED: |
| 613 |
/* |
| 614 |
* These are all have the same content format as ASN1_INTEGER |
| 615 |
*/ |
| 616 |
return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL); |
| 617 |
break; |
| 618 |
|
| 619 |
case V_ASN1_OCTET_STRING: |
| 620 |
case V_ASN1_NUMERICSTRING: |
| 621 |
case V_ASN1_PRINTABLESTRING: |
| 622 |
case V_ASN1_T61STRING: |
| 623 |
case V_ASN1_VIDEOTEXSTRING: |
| 624 |
case V_ASN1_IA5STRING: |
| 625 |
case V_ASN1_UTCTIME: |
| 626 |
case V_ASN1_GENERALIZEDTIME: |
| 627 |
case V_ASN1_GRAPHICSTRING: |
| 628 |
case V_ASN1_VISIBLESTRING: |
| 629 |
case V_ASN1_GENERALSTRING: |
| 630 |
case V_ASN1_UNIVERSALSTRING: |
| 631 |
case V_ASN1_BMPSTRING: |
| 632 |
case V_ASN1_UTF8STRING: |
| 633 |
case V_ASN1_SEQUENCE: |
| 634 |
case V_ASN1_SET: |
| 635 |
default: |
| 636 |
/* All based on ASN1_STRING and handled the same */ |
| 637 |
strtmp = (ASN1_STRING *)*pval; |
| 638 |
/* Special handling for NDEF */ |
| 639 |
if ((it->size == ASN1_TFLG_NDEF) |
| 640 |
&& (strtmp->flags & ASN1_STRING_FLAG_NDEF)) { |
| 641 |
if (cout) { |
| 642 |
strtmp->data = cout; |
| 643 |
strtmp->length = 0; |
| 644 |
} |
| 645 |
/* Special return code */ |
| 646 |
return -2; |
| 647 |
} |
| 648 |
cont = strtmp->data; |
| 649 |
len = strtmp->length; |
| 650 |
|
| 651 |
break; |
| 652 |
|
| 653 |
} |
| 654 |
if (cout && len) |
| 655 |
memcpy(cout, cont, len); |
| 656 |
return len; |
| 657 |
} |