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