/src/openssl111/crypto/asn1/tasn_enc.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the OpenSSL license (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | #include <stddef.h> | 
| 11 |  | #include <string.h> | 
| 12 |  | #include "internal/cryptlib.h" | 
| 13 |  | #include <openssl/asn1.h> | 
| 14 |  | #include <openssl/asn1t.h> | 
| 15 |  | #include <openssl/objects.h> | 
| 16 |  | #include "crypto/asn1.h" | 
| 17 |  | #include "asn1_local.h" | 
| 18 |  |  | 
| 19 |  | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, | 
| 20 |  |                                  const ASN1_ITEM *it, int tag, int aclass); | 
| 21 |  | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, | 
| 22 |  |                             int skcontlen, const ASN1_ITEM *item, | 
| 23 |  |                             int do_sort, int iclass); | 
| 24 |  | static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, | 
| 25 |  |                                 const ASN1_TEMPLATE *tt, int tag, int aclass); | 
| 26 |  | static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, | 
| 27 |  |                                const ASN1_ITEM *it, int flags); | 
| 28 |  | static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, | 
| 29 |  |                        const ASN1_ITEM *it); | 
| 30 |  |  | 
| 31 |  | /* | 
| 32 |  |  * Top level i2d equivalents: the 'ndef' variant instructs the encoder to use | 
| 33 |  |  * indefinite length constructed encoding, where appropriate | 
| 34 |  |  */ | 
| 35 |  |  | 
| 36 |  | int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, | 
| 37 |  |                        const ASN1_ITEM *it) | 
| 38 | 0 | { | 
| 39 | 0 |     return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF); | 
| 40 | 0 | } | 
| 41 |  |  | 
| 42 |  | int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) | 
| 43 | 0 | { | 
| 44 | 0 |     return asn1_item_flags_i2d(val, out, it, 0); | 
| 45 | 0 | } | 
| 46 |  |  | 
| 47 |  | /* | 
| 48 |  |  * Encode an ASN1 item, this is use by the standard 'i2d' function. 'out' | 
| 49 |  |  * points to a buffer to output the data to. The new i2d has one additional | 
| 50 |  |  * feature. If the output buffer is NULL (i.e. *out == NULL) then a buffer is | 
| 51 |  |  * allocated and populated with the encoding. | 
| 52 |  |  */ | 
| 53 |  |  | 
| 54 |  | static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, | 
| 55 |  |                                const ASN1_ITEM *it, int flags) | 
| 56 | 0 | { | 
| 57 | 0 |     if (out && !*out) { | 
| 58 | 0 |         unsigned char *p, *buf; | 
| 59 | 0 |         int len; | 
| 60 |  | 
 | 
| 61 | 0 |         len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); | 
| 62 | 0 |         if (len <= 0) | 
| 63 | 0 |             return len; | 
| 64 | 0 |         if ((buf = OPENSSL_malloc(len)) == NULL) { | 
| 65 | 0 |             ASN1err(ASN1_F_ASN1_ITEM_FLAGS_I2D, ERR_R_MALLOC_FAILURE); | 
| 66 | 0 |             return -1; | 
| 67 | 0 |         } | 
| 68 | 0 |         p = buf; | 
| 69 | 0 |         ASN1_item_ex_i2d(&val, &p, it, -1, flags); | 
| 70 | 0 |         *out = buf; | 
| 71 | 0 |         return len; | 
| 72 | 0 |     } | 
| 73 |  |  | 
| 74 | 0 |     return ASN1_item_ex_i2d(&val, out, it, -1, flags); | 
| 75 | 0 | } | 
| 76 |  |  | 
| 77 |  | /* | 
| 78 |  |  * Encode an item, taking care of IMPLICIT tagging (if any). This function | 
| 79 |  |  * performs the normal item handling: it can be used in external types. | 
| 80 |  |  */ | 
| 81 |  |  | 
| 82 |  | int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, | 
| 83 |  |                      const ASN1_ITEM *it, int tag, int aclass) | 
| 84 | 0 | { | 
| 85 | 0 |     const ASN1_TEMPLATE *tt = NULL; | 
| 86 | 0 |     int i, seqcontlen, seqlen, ndef = 1; | 
| 87 | 0 |     const ASN1_EXTERN_FUNCS *ef; | 
| 88 | 0 |     const ASN1_AUX *aux = it->funcs; | 
| 89 | 0 |     ASN1_aux_cb *asn1_cb = 0; | 
| 90 |  | 
 | 
| 91 | 0 |     if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) | 
| 92 | 0 |         return 0; | 
| 93 |  |  | 
| 94 | 0 |     if (aux && aux->asn1_cb) | 
| 95 | 0 |         asn1_cb = aux->asn1_cb; | 
| 96 |  | 
 | 
| 97 | 0 |     switch (it->itype) { | 
| 98 |  |  | 
| 99 | 0 |     case ASN1_ITYPE_PRIMITIVE: | 
| 100 | 0 |         if (it->templates) | 
| 101 | 0 |             return asn1_template_ex_i2d(pval, out, it->templates, | 
| 102 | 0 |                                         tag, aclass); | 
| 103 | 0 |         return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); | 
| 104 |  |  | 
| 105 | 0 |     case ASN1_ITYPE_MSTRING: | 
| 106 |  |         /* | 
| 107 |  |          * It never makes sense for multi-strings to have implicit tagging, so | 
| 108 |  |          * if tag != -1, then this looks like an error in the template. | 
| 109 |  |          */ | 
| 110 | 0 |         if (tag != -1) { | 
| 111 | 0 |             ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE); | 
| 112 | 0 |             return -1; | 
| 113 | 0 |         } | 
| 114 | 0 |         return asn1_i2d_ex_primitive(pval, out, it, -1, aclass); | 
| 115 |  |  | 
| 116 | 0 |     case ASN1_ITYPE_CHOICE: | 
| 117 |  |         /* | 
| 118 |  |          * It never makes sense for CHOICE types to have implicit tagging, so | 
| 119 |  |          * if tag != -1, then this looks like an error in the template. | 
| 120 |  |          */ | 
| 121 | 0 |         if (tag != -1) { | 
| 122 | 0 |             ASN1err(ASN1_F_ASN1_ITEM_EX_I2D, ASN1_R_BAD_TEMPLATE); | 
| 123 | 0 |             return -1; | 
| 124 | 0 |         } | 
| 125 | 0 |         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) | 
| 126 | 0 |             return 0; | 
| 127 | 0 |         i = asn1_get_choice_selector(pval, it); | 
| 128 | 0 |         if ((i >= 0) && (i < it->tcount)) { | 
| 129 | 0 |             ASN1_VALUE **pchval; | 
| 130 | 0 |             const ASN1_TEMPLATE *chtt; | 
| 131 | 0 |             chtt = it->templates + i; | 
| 132 | 0 |             pchval = asn1_get_field_ptr(pval, chtt); | 
| 133 | 0 |             return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass); | 
| 134 | 0 |         } | 
| 135 |  |         /* Fixme: error condition if selector out of range */ | 
| 136 | 0 |         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) | 
| 137 | 0 |             return 0; | 
| 138 | 0 |         break; | 
| 139 |  |  | 
| 140 | 0 |     case ASN1_ITYPE_EXTERN: | 
| 141 |  |         /* If new style i2d it does all the work */ | 
| 142 | 0 |         ef = it->funcs; | 
| 143 | 0 |         return ef->asn1_ex_i2d(pval, out, it, tag, aclass); | 
| 144 |  |  | 
| 145 | 0 |     case ASN1_ITYPE_NDEF_SEQUENCE: | 
| 146 |  |         /* Use indefinite length constructed if requested */ | 
| 147 | 0 |         if (aclass & ASN1_TFLG_NDEF) | 
| 148 | 0 |             ndef = 2; | 
| 149 |  |         /* fall through */ | 
| 150 |  | 
 | 
| 151 | 0 |     case ASN1_ITYPE_SEQUENCE: | 
| 152 | 0 |         i = asn1_enc_restore(&seqcontlen, out, pval, it); | 
| 153 |  |         /* An error occurred */ | 
| 154 | 0 |         if (i < 0) | 
| 155 | 0 |             return 0; | 
| 156 |  |         /* We have a valid cached encoding... */ | 
| 157 | 0 |         if (i > 0) | 
| 158 | 0 |             return seqcontlen; | 
| 159 |  |         /* Otherwise carry on */ | 
| 160 | 0 |         seqcontlen = 0; | 
| 161 |  |         /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ | 
| 162 | 0 |         if (tag == -1) { | 
| 163 | 0 |             tag = V_ASN1_SEQUENCE; | 
| 164 |  |             /* Retain any other flags in aclass */ | 
| 165 | 0 |             aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) | 
| 166 | 0 |                 | V_ASN1_UNIVERSAL; | 
| 167 | 0 |         } | 
| 168 | 0 |         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) | 
| 169 | 0 |             return 0; | 
| 170 |  |         /* First work out sequence content length */ | 
| 171 | 0 |         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { | 
| 172 | 0 |             const ASN1_TEMPLATE *seqtt; | 
| 173 | 0 |             ASN1_VALUE **pseqval; | 
| 174 | 0 |             int tmplen; | 
| 175 | 0 |             seqtt = asn1_do_adb(pval, tt, 1); | 
| 176 | 0 |             if (!seqtt) | 
| 177 | 0 |                 return 0; | 
| 178 | 0 |             pseqval = asn1_get_field_ptr(pval, seqtt); | 
| 179 | 0 |             tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass); | 
| 180 | 0 |             if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) | 
| 181 | 0 |                 return -1; | 
| 182 | 0 |             seqcontlen += tmplen; | 
| 183 | 0 |         } | 
| 184 |  |  | 
| 185 | 0 |         seqlen = ASN1_object_size(ndef, seqcontlen, tag); | 
| 186 | 0 |         if (!out || seqlen == -1) | 
| 187 | 0 |             return seqlen; | 
| 188 |  |         /* Output SEQUENCE header */ | 
| 189 | 0 |         ASN1_put_object(out, ndef, seqcontlen, tag, aclass); | 
| 190 | 0 |         for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { | 
| 191 | 0 |             const ASN1_TEMPLATE *seqtt; | 
| 192 | 0 |             ASN1_VALUE **pseqval; | 
| 193 | 0 |             seqtt = asn1_do_adb(pval, tt, 1); | 
| 194 | 0 |             if (!seqtt) | 
| 195 | 0 |                 return 0; | 
| 196 | 0 |             pseqval = asn1_get_field_ptr(pval, seqtt); | 
| 197 |  |             /* FIXME: check for errors in enhanced version */ | 
| 198 | 0 |             asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass); | 
| 199 | 0 |         } | 
| 200 | 0 |         if (ndef == 2) | 
| 201 | 0 |             ASN1_put_eoc(out); | 
| 202 | 0 |         if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) | 
| 203 | 0 |             return 0; | 
| 204 | 0 |         return seqlen; | 
| 205 |  |  | 
| 206 | 0 |     default: | 
| 207 | 0 |         return 0; | 
| 208 |  | 
 | 
| 209 | 0 |     } | 
| 210 | 0 |     return 0; | 
| 211 | 0 | } | 
| 212 |  |  | 
| 213 |  | static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, | 
| 214 |  |                                 const ASN1_TEMPLATE *tt, int tag, int iclass) | 
| 215 | 0 | { | 
| 216 | 0 |     int i, ret, flags, ttag, tclass, ndef; | 
| 217 | 0 |     ASN1_VALUE *tval; | 
| 218 | 0 |     flags = tt->flags; | 
| 219 |  |  | 
| 220 |  |     /* | 
| 221 |  |      * If field is embedded then val needs fixing so it is a pointer to | 
| 222 |  |      * a pointer to a field. | 
| 223 |  |      */ | 
| 224 | 0 |     if (flags & ASN1_TFLG_EMBED) { | 
| 225 | 0 |         tval = (ASN1_VALUE *)pval; | 
| 226 | 0 |         pval = &tval; | 
| 227 | 0 |     } | 
| 228 |  |     /* | 
| 229 |  |      * Work out tag and class to use: tagging may come either from the | 
| 230 |  |      * template or the arguments, not both because this would create | 
| 231 |  |      * ambiguity. Additionally the iclass argument may contain some | 
| 232 |  |      * additional flags which should be noted and passed down to other | 
| 233 |  |      * levels. | 
| 234 |  |      */ | 
| 235 | 0 |     if (flags & ASN1_TFLG_TAG_MASK) { | 
| 236 |  |         /* Error if argument and template tagging */ | 
| 237 | 0 |         if (tag != -1) | 
| 238 |  |             /* FIXME: error code here */ | 
| 239 | 0 |             return -1; | 
| 240 |  |         /* Get tagging from template */ | 
| 241 | 0 |         ttag = tt->tag; | 
| 242 | 0 |         tclass = flags & ASN1_TFLG_TAG_CLASS; | 
| 243 | 0 |     } else if (tag != -1) { | 
| 244 |  |         /* No template tagging, get from arguments */ | 
| 245 | 0 |         ttag = tag; | 
| 246 | 0 |         tclass = iclass & ASN1_TFLG_TAG_CLASS; | 
| 247 | 0 |     } else { | 
| 248 | 0 |         ttag = -1; | 
| 249 | 0 |         tclass = 0; | 
| 250 | 0 |     } | 
| 251 |  |     /* | 
| 252 |  |      * Remove any class mask from iflag. | 
| 253 |  |      */ | 
| 254 | 0 |     iclass &= ~ASN1_TFLG_TAG_CLASS; | 
| 255 |  |  | 
| 256 |  |     /* | 
| 257 |  |      * At this point 'ttag' contains the outer tag to use, 'tclass' is the | 
| 258 |  |      * class and iclass is any flags passed to this function. | 
| 259 |  |      */ | 
| 260 |  |  | 
| 261 |  |     /* if template and arguments require ndef, use it */ | 
| 262 | 0 |     if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF)) | 
| 263 | 0 |         ndef = 2; | 
| 264 | 0 |     else | 
| 265 | 0 |         ndef = 1; | 
| 266 |  | 
 | 
| 267 | 0 |     if (flags & ASN1_TFLG_SK_MASK) { | 
| 268 |  |         /* SET OF, SEQUENCE OF */ | 
| 269 | 0 |         STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; | 
| 270 | 0 |         int isset, sktag, skaclass; | 
| 271 | 0 |         int skcontlen, sklen; | 
| 272 | 0 |         ASN1_VALUE *skitem; | 
| 273 |  | 
 | 
| 274 | 0 |         if (!*pval) | 
| 275 | 0 |             return 0; | 
| 276 |  |  | 
| 277 | 0 |         if (flags & ASN1_TFLG_SET_OF) { | 
| 278 | 0 |             isset = 1; | 
| 279 |  |             /* 2 means we reorder */ | 
| 280 | 0 |             if (flags & ASN1_TFLG_SEQUENCE_OF) | 
| 281 | 0 |                 isset = 2; | 
| 282 | 0 |         } else | 
| 283 | 0 |             isset = 0; | 
| 284 |  |  | 
| 285 |  |         /* | 
| 286 |  |          * Work out inner tag value: if EXPLICIT or no tagging use underlying | 
| 287 |  |          * type. | 
| 288 |  |          */ | 
| 289 | 0 |         if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) { | 
| 290 | 0 |             sktag = ttag; | 
| 291 | 0 |             skaclass = tclass; | 
| 292 | 0 |         } else { | 
| 293 | 0 |             skaclass = V_ASN1_UNIVERSAL; | 
| 294 | 0 |             if (isset) | 
| 295 | 0 |                 sktag = V_ASN1_SET; | 
| 296 | 0 |             else | 
| 297 | 0 |                 sktag = V_ASN1_SEQUENCE; | 
| 298 | 0 |         } | 
| 299 |  |  | 
| 300 |  |         /* Determine total length of items */ | 
| 301 | 0 |         skcontlen = 0; | 
| 302 | 0 |         for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { | 
| 303 | 0 |             int tmplen; | 
| 304 | 0 |             skitem = sk_ASN1_VALUE_value(sk, i); | 
| 305 | 0 |             tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item), | 
| 306 | 0 |                                       -1, iclass); | 
| 307 | 0 |             if (tmplen == -1 || (skcontlen > INT_MAX - tmplen)) | 
| 308 | 0 |                 return -1; | 
| 309 | 0 |             skcontlen += tmplen; | 
| 310 | 0 |         } | 
| 311 | 0 |         sklen = ASN1_object_size(ndef, skcontlen, sktag); | 
| 312 | 0 |         if (sklen == -1) | 
| 313 | 0 |             return -1; | 
| 314 |  |         /* If EXPLICIT need length of surrounding tag */ | 
| 315 | 0 |         if (flags & ASN1_TFLG_EXPTAG) | 
| 316 | 0 |             ret = ASN1_object_size(ndef, sklen, ttag); | 
| 317 | 0 |         else | 
| 318 | 0 |             ret = sklen; | 
| 319 |  | 
 | 
| 320 | 0 |         if (!out || ret == -1) | 
| 321 | 0 |             return ret; | 
| 322 |  |  | 
| 323 |  |         /* Now encode this lot... */ | 
| 324 |  |         /* EXPLICIT tag */ | 
| 325 | 0 |         if (flags & ASN1_TFLG_EXPTAG) | 
| 326 | 0 |             ASN1_put_object(out, ndef, sklen, ttag, tclass); | 
| 327 |  |         /* SET or SEQUENCE and IMPLICIT tag */ | 
| 328 | 0 |         ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); | 
| 329 |  |         /* And the stuff itself */ | 
| 330 | 0 |         asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), | 
| 331 | 0 |                          isset, iclass); | 
| 332 | 0 |         if (ndef == 2) { | 
| 333 | 0 |             ASN1_put_eoc(out); | 
| 334 | 0 |             if (flags & ASN1_TFLG_EXPTAG) | 
| 335 | 0 |                 ASN1_put_eoc(out); | 
| 336 | 0 |         } | 
| 337 |  | 
 | 
| 338 | 0 |         return ret; | 
| 339 | 0 |     } | 
| 340 |  |  | 
| 341 | 0 |     if (flags & ASN1_TFLG_EXPTAG) { | 
| 342 |  |         /* EXPLICIT tagging */ | 
| 343 |  |         /* Find length of tagged item */ | 
| 344 | 0 |         i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass); | 
| 345 | 0 |         if (!i) | 
| 346 | 0 |             return 0; | 
| 347 |  |         /* Find length of EXPLICIT tag */ | 
| 348 | 0 |         ret = ASN1_object_size(ndef, i, ttag); | 
| 349 | 0 |         if (out && ret != -1) { | 
| 350 |  |             /* Output tag and item */ | 
| 351 | 0 |             ASN1_put_object(out, ndef, i, ttag, tclass); | 
| 352 | 0 |             ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass); | 
| 353 | 0 |             if (ndef == 2) | 
| 354 | 0 |                 ASN1_put_eoc(out); | 
| 355 | 0 |         } | 
| 356 | 0 |         return ret; | 
| 357 | 0 |     } | 
| 358 |  |  | 
| 359 |  |     /* Either normal or IMPLICIT tagging: combine class and flags */ | 
| 360 | 0 |     return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), | 
| 361 | 0 |                             ttag, tclass | iclass); | 
| 362 |  | 
 | 
| 363 | 0 | } | 
| 364 |  |  | 
| 365 |  | /* Temporary structure used to hold DER encoding of items for SET OF */ | 
| 366 |  |  | 
| 367 |  | typedef struct { | 
| 368 |  |     unsigned char *data; | 
| 369 |  |     int length; | 
| 370 |  |     ASN1_VALUE *field; | 
| 371 |  | } DER_ENC; | 
| 372 |  |  | 
| 373 |  | static int der_cmp(const void *a, const void *b) | 
| 374 | 0 | { | 
| 375 | 0 |     const DER_ENC *d1 = a, *d2 = b; | 
| 376 | 0 |     int cmplen, i; | 
| 377 | 0 |     cmplen = (d1->length < d2->length) ? d1->length : d2->length; | 
| 378 | 0 |     i = memcmp(d1->data, d2->data, cmplen); | 
| 379 | 0 |     if (i) | 
| 380 | 0 |         return i; | 
| 381 | 0 |     return d1->length - d2->length; | 
| 382 | 0 | } | 
| 383 |  |  | 
| 384 |  | /* Output the content octets of SET OF or SEQUENCE OF */ | 
| 385 |  |  | 
| 386 |  | static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, | 
| 387 |  |                             int skcontlen, const ASN1_ITEM *item, | 
| 388 |  |                             int do_sort, int iclass) | 
| 389 | 0 | { | 
| 390 | 0 |     int i; | 
| 391 | 0 |     ASN1_VALUE *skitem; | 
| 392 | 0 |     unsigned char *tmpdat = NULL, *p = NULL; | 
| 393 | 0 |     DER_ENC *derlst = NULL, *tder; | 
| 394 | 0 |     if (do_sort) { | 
| 395 |  |         /* Don't need to sort less than 2 items */ | 
| 396 | 0 |         if (sk_ASN1_VALUE_num(sk) < 2) | 
| 397 | 0 |             do_sort = 0; | 
| 398 | 0 |         else { | 
| 399 | 0 |             derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) | 
| 400 | 0 |                                     * sizeof(*derlst)); | 
| 401 | 0 |             if (derlst == NULL) | 
| 402 | 0 |                 return 0; | 
| 403 | 0 |             tmpdat = OPENSSL_malloc(skcontlen); | 
| 404 | 0 |             if (tmpdat == NULL) { | 
| 405 | 0 |                 OPENSSL_free(derlst); | 
| 406 | 0 |                 return 0; | 
| 407 | 0 |             } | 
| 408 | 0 |         } | 
| 409 | 0 |     } | 
| 410 |  |     /* If not sorting just output each item */ | 
| 411 | 0 |     if (!do_sort) { | 
| 412 | 0 |         for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { | 
| 413 | 0 |             skitem = sk_ASN1_VALUE_value(sk, i); | 
| 414 | 0 |             ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); | 
| 415 | 0 |         } | 
| 416 | 0 |         return 1; | 
| 417 | 0 |     } | 
| 418 | 0 |     p = tmpdat; | 
| 419 |  |  | 
| 420 |  |     /* Doing sort: build up a list of each member's DER encoding */ | 
| 421 | 0 |     for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { | 
| 422 | 0 |         skitem = sk_ASN1_VALUE_value(sk, i); | 
| 423 | 0 |         tder->data = p; | 
| 424 | 0 |         tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass); | 
| 425 | 0 |         tder->field = skitem; | 
| 426 | 0 |     } | 
| 427 |  |  | 
| 428 |  |     /* Now sort them */ | 
| 429 | 0 |     qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); | 
| 430 |  |     /* Output sorted DER encoding */ | 
| 431 | 0 |     p = *out; | 
| 432 | 0 |     for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { | 
| 433 | 0 |         memcpy(p, tder->data, tder->length); | 
| 434 | 0 |         p += tder->length; | 
| 435 | 0 |     } | 
| 436 | 0 |     *out = p; | 
| 437 |  |     /* If do_sort is 2 then reorder the STACK */ | 
| 438 | 0 |     if (do_sort == 2) { | 
| 439 | 0 |         for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) | 
| 440 | 0 |             (void)sk_ASN1_VALUE_set(sk, i, tder->field); | 
| 441 | 0 |     } | 
| 442 | 0 |     OPENSSL_free(derlst); | 
| 443 | 0 |     OPENSSL_free(tmpdat); | 
| 444 | 0 |     return 1; | 
| 445 | 0 | } | 
| 446 |  |  | 
| 447 |  | static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, | 
| 448 |  |                                  const ASN1_ITEM *it, int tag, int aclass) | 
| 449 | 0 | { | 
| 450 | 0 |     int len; | 
| 451 | 0 |     int utype; | 
| 452 | 0 |     int usetag; | 
| 453 | 0 |     int ndef = 0; | 
| 454 |  | 
 | 
| 455 | 0 |     utype = it->utype; | 
| 456 |  |  | 
| 457 |  |     /* | 
| 458 |  |      * Get length of content octets and maybe find out the underlying type. | 
| 459 |  |      */ | 
| 460 |  | 
 | 
| 461 | 0 |     len = asn1_ex_i2c(pval, NULL, &utype, it); | 
| 462 |  |  | 
| 463 |  |     /* | 
| 464 |  |      * If SEQUENCE, SET or OTHER then header is included in pseudo content | 
| 465 |  |      * octets so don't include tag+length. We need to check here because the | 
| 466 |  |      * call to asn1_ex_i2c() could change utype. | 
| 467 |  |      */ | 
| 468 | 0 |     if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || | 
| 469 | 0 |         (utype == V_ASN1_OTHER)) | 
| 470 | 0 |         usetag = 0; | 
| 471 | 0 |     else | 
| 472 | 0 |         usetag = 1; | 
| 473 |  |  | 
| 474 |  |     /* -1 means omit type */ | 
| 475 |  | 
 | 
| 476 | 0 |     if (len == -1) | 
| 477 | 0 |         return 0; | 
| 478 |  |  | 
| 479 |  |     /* -2 return is special meaning use ndef */ | 
| 480 | 0 |     if (len == -2) { | 
| 481 | 0 |         ndef = 2; | 
| 482 | 0 |         len = 0; | 
| 483 | 0 |     } | 
| 484 |  |  | 
| 485 |  |     /* If not implicitly tagged get tag from underlying type */ | 
| 486 | 0 |     if (tag == -1) | 
| 487 | 0 |         tag = utype; | 
| 488 |  |  | 
| 489 |  |     /* Output tag+length followed by content octets */ | 
| 490 | 0 |     if (out) { | 
| 491 | 0 |         if (usetag) | 
| 492 | 0 |             ASN1_put_object(out, ndef, len, tag, aclass); | 
| 493 | 0 |         asn1_ex_i2c(pval, *out, &utype, it); | 
| 494 | 0 |         if (ndef) | 
| 495 | 0 |             ASN1_put_eoc(out); | 
| 496 | 0 |         else | 
| 497 | 0 |             *out += len; | 
| 498 | 0 |     } | 
| 499 |  | 
 | 
| 500 | 0 |     if (usetag) | 
| 501 | 0 |         return ASN1_object_size(ndef, len, tag); | 
| 502 | 0 |     return len; | 
| 503 | 0 | } | 
| 504 |  |  | 
| 505 |  | /* Produce content octets from a structure */ | 
| 506 |  |  | 
| 507 |  | static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, | 
| 508 |  |                        const ASN1_ITEM *it) | 
| 509 | 0 | { | 
| 510 | 0 |     ASN1_BOOLEAN *tbool = NULL; | 
| 511 | 0 |     ASN1_STRING *strtmp; | 
| 512 | 0 |     ASN1_OBJECT *otmp; | 
| 513 | 0 |     int utype; | 
| 514 | 0 |     const unsigned char *cont; | 
| 515 | 0 |     unsigned char c; | 
| 516 | 0 |     int len; | 
| 517 | 0 |     const ASN1_PRIMITIVE_FUNCS *pf; | 
| 518 | 0 |     pf = it->funcs; | 
| 519 | 0 |     if (pf && pf->prim_i2c) | 
| 520 | 0 |         return pf->prim_i2c(pval, cout, putype, it); | 
| 521 |  |  | 
| 522 |  |     /* Should type be omitted? */ | 
| 523 | 0 |     if ((it->itype != ASN1_ITYPE_PRIMITIVE) | 
| 524 | 0 |         || (it->utype != V_ASN1_BOOLEAN)) { | 
| 525 | 0 |         if (!*pval) | 
| 526 | 0 |             return -1; | 
| 527 | 0 |     } | 
| 528 |  |  | 
| 529 | 0 |     if (it->itype == ASN1_ITYPE_MSTRING) { | 
| 530 |  |         /* If MSTRING type set the underlying type */ | 
| 531 | 0 |         strtmp = (ASN1_STRING *)*pval; | 
| 532 | 0 |         utype = strtmp->type; | 
| 533 | 0 |         *putype = utype; | 
| 534 | 0 |     } else if (it->utype == V_ASN1_ANY) { | 
| 535 |  |         /* If ANY set type and pointer to value */ | 
| 536 | 0 |         ASN1_TYPE *typ; | 
| 537 | 0 |         typ = (ASN1_TYPE *)*pval; | 
| 538 | 0 |         utype = typ->type; | 
| 539 | 0 |         *putype = utype; | 
| 540 | 0 |         pval = &typ->value.asn1_value; | 
| 541 | 0 |     } else | 
| 542 | 0 |         utype = *putype; | 
| 543 |  | 
 | 
| 544 | 0 |     switch (utype) { | 
| 545 | 0 |     case V_ASN1_OBJECT: | 
| 546 | 0 |         otmp = (ASN1_OBJECT *)*pval; | 
| 547 | 0 |         cont = otmp->data; | 
| 548 | 0 |         len = otmp->length; | 
| 549 | 0 |         if (cont == NULL || len == 0) | 
| 550 | 0 |             return -1; | 
| 551 | 0 |         break; | 
| 552 |  |  | 
| 553 | 0 |     case V_ASN1_NULL: | 
| 554 | 0 |         cont = NULL; | 
| 555 | 0 |         len = 0; | 
| 556 | 0 |         break; | 
| 557 |  |  | 
| 558 | 0 |     case V_ASN1_BOOLEAN: | 
| 559 | 0 |         tbool = (ASN1_BOOLEAN *)pval; | 
| 560 | 0 |         if (*tbool == -1) | 
| 561 | 0 |             return -1; | 
| 562 | 0 |         if (it->utype != V_ASN1_ANY) { | 
| 563 |  |             /* | 
| 564 |  |              * Default handling if value == size field then omit | 
| 565 |  |              */ | 
| 566 | 0 |             if (*tbool && (it->size > 0)) | 
| 567 | 0 |                 return -1; | 
| 568 | 0 |             if (!*tbool && !it->size) | 
| 569 | 0 |                 return -1; | 
| 570 | 0 |         } | 
| 571 | 0 |         c = (unsigned char)*tbool; | 
| 572 | 0 |         cont = &c; | 
| 573 | 0 |         len = 1; | 
| 574 | 0 |         break; | 
| 575 |  |  | 
| 576 | 0 |     case V_ASN1_BIT_STRING: | 
| 577 | 0 |         return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, | 
| 578 | 0 |                                    cout ? &cout : NULL); | 
| 579 |  |  | 
| 580 | 0 |     case V_ASN1_INTEGER: | 
| 581 | 0 |     case V_ASN1_ENUMERATED: | 
| 582 |  |         /* | 
| 583 |  |          * These are all have the same content format as ASN1_INTEGER | 
| 584 |  |          */ | 
| 585 | 0 |         return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL); | 
| 586 |  |  | 
| 587 | 0 |     case V_ASN1_OCTET_STRING: | 
| 588 | 0 |     case V_ASN1_NUMERICSTRING: | 
| 589 | 0 |     case V_ASN1_PRINTABLESTRING: | 
| 590 | 0 |     case V_ASN1_T61STRING: | 
| 591 | 0 |     case V_ASN1_VIDEOTEXSTRING: | 
| 592 | 0 |     case V_ASN1_IA5STRING: | 
| 593 | 0 |     case V_ASN1_UTCTIME: | 
| 594 | 0 |     case V_ASN1_GENERALIZEDTIME: | 
| 595 | 0 |     case V_ASN1_GRAPHICSTRING: | 
| 596 | 0 |     case V_ASN1_VISIBLESTRING: | 
| 597 | 0 |     case V_ASN1_GENERALSTRING: | 
| 598 | 0 |     case V_ASN1_UNIVERSALSTRING: | 
| 599 | 0 |     case V_ASN1_BMPSTRING: | 
| 600 | 0 |     case V_ASN1_UTF8STRING: | 
| 601 | 0 |     case V_ASN1_SEQUENCE: | 
| 602 | 0 |     case V_ASN1_SET: | 
| 603 | 0 |     default: | 
| 604 |  |         /* All based on ASN1_STRING and handled the same */ | 
| 605 | 0 |         strtmp = (ASN1_STRING *)*pval; | 
| 606 |  |         /* Special handling for NDEF */ | 
| 607 | 0 |         if ((it->size == ASN1_TFLG_NDEF) | 
| 608 | 0 |             && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) { | 
| 609 | 0 |             if (cout) { | 
| 610 | 0 |                 strtmp->data = cout; | 
| 611 | 0 |                 strtmp->length = 0; | 
| 612 | 0 |             } | 
| 613 |  |             /* Special return code */ | 
| 614 | 0 |             return -2; | 
| 615 | 0 |         } | 
| 616 | 0 |         cont = strtmp->data; | 
| 617 | 0 |         len = strtmp->length; | 
| 618 |  | 
 | 
| 619 | 0 |         break; | 
| 620 |  | 
 | 
| 621 | 0 |     } | 
| 622 | 0 |     if (cout && len) | 
| 623 | 0 |         memcpy(cout, cont, len); | 
| 624 | 0 |     return len; | 
| 625 | 0 | } |