| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <openssl/asn1.h> |
| 5 |
|
| 6 |
#include <assert.h> |
| 7 |
#include <limits.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
#include <openssl/asn1t.h> |
| 11 |
#include <openssl/mem.h> |
| 12 |
|
| 13 |
#include "../internal.h" |
| 14 |
#include "internal.h" |
| 15 |
|
| 16 |
|
| 17 |
static int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out, |
| 18 |
const ASN1_ITEM *it, int tag, int aclass, |
| 19 |
int optional); |
| 20 |
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 21 |
const ASN1_ITEM *it, int tag, int aclass, |
| 22 |
int optional); |
| 23 |
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cont, int *out_omit, |
| 24 |
int *putype, const ASN1_ITEM *it); |
| 25 |
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, |
| 26 |
int skcontlen, const ASN1_ITEM *item, int do_sort); |
| 27 |
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 28 |
const ASN1_TEMPLATE *tt, int tag, int aclass, |
| 29 |
int optional); |
| 30 |
|
| 31 |
// Top level i2d equivalents |
| 32 |
|
| 33 |
int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) { |
| 34 |
if (out && !*out) { |
| 35 |
unsigned char *p, *buf; |
| 36 |
int len = ASN1_item_ex_i2d(&val, NULL, it, /*tag=*/-1, /*aclass=*/0); |
| 37 |
if (len <= 0) { |
| 38 |
return len; |
| 39 |
} |
| 40 |
buf = OPENSSL_malloc(len); |
| 41 |
if (!buf) { |
| 42 |
return -1; |
| 43 |
} |
| 44 |
p = buf; |
| 45 |
int len2 = ASN1_item_ex_i2d(&val, &p, it, /*tag=*/-1, /*aclass=*/0); |
| 46 |
if (len2 <= 0) { |
| 47 |
OPENSSL_free(buf); |
| 48 |
return len2; |
| 49 |
} |
| 50 |
assert(len == len2); |
| 51 |
*out = buf; |
| 52 |
return len; |
| 53 |
} |
| 54 |
|
| 55 |
return ASN1_item_ex_i2d(&val, out, it, /*tag=*/-1, /*aclass=*/0); |
| 56 |
} |
| 57 |
|
| 58 |
// Encode an item, taking care of IMPLICIT tagging (if any). This function |
| 59 |
// performs the normal item handling: it can be used in external types. |
| 60 |
|
| 61 |
int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 62 |
const ASN1_ITEM *it, int tag, int aclass) { |
| 63 |
int ret = asn1_item_ex_i2d_opt(pval, out, it, tag, aclass, /*optional=*/0); |
| 64 |
assert(ret != 0); |
| 65 |
return ret; |
| 66 |
} |
| 67 |
|
| 68 |
// asn1_item_ex_i2d_opt behaves like |ASN1_item_ex_i2d| but, if |optional| is |
| 69 |
// non-zero and |*pval| is omitted, it returns zero and writes no bytes. |
| 70 |
int asn1_item_ex_i2d_opt(ASN1_VALUE **pval, unsigned char **out, |
| 71 |
const ASN1_ITEM *it, int tag, int aclass, |
| 72 |
int optional) { |
| 73 |
const ASN1_TEMPLATE *tt = NULL; |
| 74 |
int i, seqcontlen, seqlen; |
| 75 |
|
| 76 |
// Historically, |aclass| was repurposed to pass additional flags into the |
| 77 |
// encoding process. |
| 78 |
assert((aclass & ASN1_TFLG_TAG_CLASS) == aclass); |
| 79 |
// If not overridding the tag, |aclass| is ignored and should be zero. |
| 80 |
assert(tag != -1 || aclass == 0); |
| 81 |
|
| 82 |
// All fields are pointers, except for boolean |ASN1_ITYPE_PRIMITIVE|s. |
| 83 |
// Optional primitives are handled later. |
| 84 |
if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) { |
| 85 |
if (optional) { |
| 86 |
return 0; |
| 87 |
} |
| 88 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); |
| 89 |
return -1; |
| 90 |
} |
| 91 |
|
| 92 |
switch (it->itype) { |
| 93 |
case ASN1_ITYPE_PRIMITIVE: |
| 94 |
if (it->templates) { |
| 95 |
// This is an |ASN1_ITEM_TEMPLATE|. |
| 96 |
if (it->templates->flags & ASN1_TFLG_OPTIONAL) { |
| 97 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 98 |
return -1; |
| 99 |
} |
| 100 |
return asn1_template_ex_i2d(pval, out, it->templates, tag, aclass, |
| 101 |
optional); |
| 102 |
} |
| 103 |
return asn1_i2d_ex_primitive(pval, out, it, tag, aclass, optional); |
| 104 |
|
| 105 |
case ASN1_ITYPE_MSTRING: |
| 106 |
// It never makes sense for multi-strings to have implicit tagging, so |
| 107 |
// if tag != -1, then this looks like an error in the template. |
| 108 |
if (tag != -1) { |
| 109 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 110 |
return -1; |
| 111 |
} |
| 112 |
return asn1_i2d_ex_primitive(pval, out, it, -1, 0, optional); |
| 113 |
|
| 114 |
case ASN1_ITYPE_CHOICE: { |
| 115 |
// It never makes sense for CHOICE types to have implicit tagging, so if |
| 116 |
// tag != -1, then this looks like an error in the template. |
| 117 |
if (tag != -1) { |
| 118 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 119 |
return -1; |
| 120 |
} |
| 121 |
i = asn1_get_choice_selector(pval, it); |
| 122 |
if (i < 0 || i >= it->tcount) { |
| 123 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_NO_MATCHING_CHOICE_TYPE); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
const ASN1_TEMPLATE *chtt = it->templates + i; |
| 127 |
if (chtt->flags & ASN1_TFLG_OPTIONAL) { |
| 128 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
ASN1_VALUE **pchval = asn1_get_field_ptr(pval, chtt); |
| 132 |
return asn1_template_ex_i2d(pchval, out, chtt, -1, 0, /*optional=*/0); |
| 133 |
} |
| 134 |
|
| 135 |
case ASN1_ITYPE_EXTERN: { |
| 136 |
// If new style i2d it does all the work |
| 137 |
const ASN1_EXTERN_FUNCS *ef = it->funcs; |
| 138 |
int ret = ef->asn1_ex_i2d(pval, out, it, tag, aclass); |
| 139 |
if (ret == 0) { |
| 140 |
// |asn1_ex_i2d| should never return zero. We have already checked |
| 141 |
// for optional values generically, and |ASN1_ITYPE_EXTERN| fields |
| 142 |
// must be pointers. |
| 143 |
OPENSSL_PUT_ERROR(ASN1, ERR_R_INTERNAL_ERROR); |
| 144 |
return -1; |
| 145 |
} |
| 146 |
return ret; |
| 147 |
} |
| 148 |
|
| 149 |
case ASN1_ITYPE_SEQUENCE: { |
| 150 |
i = asn1_enc_restore(&seqcontlen, out, pval, it); |
| 151 |
// An error occurred |
| 152 |
if (i < 0) { |
| 153 |
return -1; |
| 154 |
} |
| 155 |
// We have a valid cached encoding... |
| 156 |
if (i > 0) { |
| 157 |
return seqcontlen; |
| 158 |
} |
| 159 |
// Otherwise carry on |
| 160 |
seqcontlen = 0; |
| 161 |
// If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL |
| 162 |
if (tag == -1) { |
| 163 |
tag = V_ASN1_SEQUENCE; |
| 164 |
aclass = V_ASN1_UNIVERSAL; |
| 165 |
} |
| 166 |
// First work out sequence content length |
| 167 |
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
| 168 |
const ASN1_TEMPLATE *seqtt; |
| 169 |
ASN1_VALUE **pseqval; |
| 170 |
int tmplen; |
| 171 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 172 |
if (!seqtt) { |
| 173 |
return -1; |
| 174 |
} |
| 175 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 176 |
tmplen = |
| 177 |
asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, 0, /*optional=*/0); |
| 178 |
if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) { |
| 179 |
return -1; |
| 180 |
} |
| 181 |
seqcontlen += tmplen; |
| 182 |
} |
| 183 |
|
| 184 |
seqlen = ASN1_object_size(/*constructed=*/1, seqcontlen, tag); |
| 185 |
if (!out || seqlen == -1) { |
| 186 |
return seqlen; |
| 187 |
} |
| 188 |
// Output SEQUENCE header |
| 189 |
ASN1_put_object(out, /*constructed=*/1, seqcontlen, tag, aclass); |
| 190 |
for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
| 191 |
const ASN1_TEMPLATE *seqtt; |
| 192 |
ASN1_VALUE **pseqval; |
| 193 |
seqtt = asn1_do_adb(pval, tt, 1); |
| 194 |
if (!seqtt) { |
| 195 |
return -1; |
| 196 |
} |
| 197 |
pseqval = asn1_get_field_ptr(pval, seqtt); |
| 198 |
if (asn1_template_ex_i2d(pseqval, out, seqtt, -1, 0, /*optional=*/0) < |
| 199 |
0) { |
| 200 |
return -1; |
| 201 |
} |
| 202 |
} |
| 203 |
return seqlen; |
| 204 |
} |
| 205 |
|
| 206 |
default: |
| 207 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 208 |
return -1; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// asn1_template_ex_i2d behaves like |asn1_item_ex_i2d_opt| but uses an |
| 213 |
// |ASN1_TEMPLATE| instead of an |ASN1_ITEM|. An |ASN1_TEMPLATE| wraps an |
| 214 |
// |ASN1_ITEM| with modifiers such as tagging, SEQUENCE or SET, etc. |
| 215 |
static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, |
| 216 |
const ASN1_TEMPLATE *tt, int tag, int iclass, |
| 217 |
int optional) { |
| 218 |
int i, ret, ttag, tclass; |
| 219 |
size_t j; |
| 220 |
uint32_t flags = tt->flags; |
| 221 |
|
| 222 |
// Historically, |iclass| was repurposed to pass additional flags into the |
| 223 |
// encoding process. |
| 224 |
assert((iclass & ASN1_TFLG_TAG_CLASS) == iclass); |
| 225 |
// If not overridding the tag, |iclass| is ignored and should be zero. |
| 226 |
assert(tag != -1 || iclass == 0); |
| 227 |
|
| 228 |
// Work out tag and class to use: tagging may come either from the |
| 229 |
// template or the arguments, not both because this would create |
| 230 |
// ambiguity. |
| 231 |
if (flags & ASN1_TFLG_TAG_MASK) { |
| 232 |
// Error if argument and template tagging |
| 233 |
if (tag != -1) { |
| 234 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_BAD_TEMPLATE); |
| 235 |
return -1; |
| 236 |
} |
| 237 |
// Get tagging from template |
| 238 |
ttag = tt->tag; |
| 239 |
tclass = flags & ASN1_TFLG_TAG_CLASS; |
| 240 |
} else if (tag != -1) { |
| 241 |
// No template tagging, get from arguments |
| 242 |
ttag = tag; |
| 243 |
tclass = iclass & ASN1_TFLG_TAG_CLASS; |
| 244 |
} else { |
| 245 |
ttag = -1; |
| 246 |
tclass = 0; |
| 247 |
} |
| 248 |
|
| 249 |
// The template may itself by marked as optional, or this may be the template |
| 250 |
// of an |ASN1_ITEM_TEMPLATE| type which was contained inside an outer |
| 251 |
// optional template. (They cannot both be true because the |
| 252 |
// |ASN1_ITEM_TEMPLATE| codepath rejects optional templates.) |
| 253 |
assert(!optional || (flags & ASN1_TFLG_OPTIONAL) == 0); |
| 254 |
optional = optional || (flags & ASN1_TFLG_OPTIONAL) != 0; |
| 255 |
|
| 256 |
// At this point 'ttag' contains the outer tag to use, and 'tclass' is the |
| 257 |
// class. |
| 258 |
|
| 259 |
if (flags & ASN1_TFLG_SK_MASK) { |
| 260 |
// SET OF, SEQUENCE OF |
| 261 |
STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; |
| 262 |
int isset, sktag, skaclass; |
| 263 |
int skcontlen, sklen; |
| 264 |
ASN1_VALUE *skitem; |
| 265 |
|
| 266 |
if (!*pval) { |
| 267 |
if (optional) { |
| 268 |
return 0; |
| 269 |
} |
| 270 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); |
| 271 |
return -1; |
| 272 |
} |
| 273 |
|
| 274 |
if (flags & ASN1_TFLG_SET_OF) { |
| 275 |
isset = 1; |
| 276 |
// Historically, types with both bits set were mutated when |
| 277 |
// serialized to apply the sort. We no longer support this. |
| 278 |
assert((flags & ASN1_TFLG_SEQUENCE_OF) == 0); |
| 279 |
} else { |
| 280 |
isset = 0; |
| 281 |
} |
| 282 |
|
| 283 |
// Work out inner tag value: if EXPLICIT or no tagging use underlying |
| 284 |
// type. |
| 285 |
if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) { |
| 286 |
sktag = ttag; |
| 287 |
skaclass = tclass; |
| 288 |
} else { |
| 289 |
skaclass = V_ASN1_UNIVERSAL; |
| 290 |
if (isset) { |
| 291 |
sktag = V_ASN1_SET; |
| 292 |
} else { |
| 293 |
sktag = V_ASN1_SEQUENCE; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
// Determine total length of items |
| 298 |
skcontlen = 0; |
| 299 |
for (j = 0; j < sk_ASN1_VALUE_num(sk); j++) { |
| 300 |
int tmplen; |
| 301 |
skitem = sk_ASN1_VALUE_value(sk, j); |
| 302 |
tmplen = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item), -1, 0); |
| 303 |
if (tmplen == -1 || (skcontlen > INT_MAX - tmplen)) { |
| 304 |
return -1; |
| 305 |
} |
| 306 |
skcontlen += tmplen; |
| 307 |
} |
| 308 |
sklen = ASN1_object_size(/*constructed=*/1, skcontlen, sktag); |
| 309 |
if (sklen == -1) { |
| 310 |
return -1; |
| 311 |
} |
| 312 |
// If EXPLICIT need length of surrounding tag |
| 313 |
if (flags & ASN1_TFLG_EXPTAG) { |
| 314 |
ret = ASN1_object_size(/*constructed=*/1, sklen, ttag); |
| 315 |
} else { |
| 316 |
ret = sklen; |
| 317 |
} |
| 318 |
|
| 319 |
if (!out || ret == -1) { |
| 320 |
return ret; |
| 321 |
} |
| 322 |
|
| 323 |
// Now encode this lot... |
| 324 |
// EXPLICIT tag |
| 325 |
if (flags & ASN1_TFLG_EXPTAG) { |
| 326 |
ASN1_put_object(out, /*constructed=*/1, sklen, ttag, tclass); |
| 327 |
} |
| 328 |
// SET or SEQUENCE and IMPLICIT tag |
| 329 |
ASN1_put_object(out, /*constructed=*/1, skcontlen, sktag, skaclass); |
| 330 |
// And the stuff itself |
| 331 |
if (!asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), isset)) { |
| 332 |
return -1; |
| 333 |
} |
| 334 |
return ret; |
| 335 |
} |
| 336 |
|
| 337 |
if (flags & ASN1_TFLG_EXPTAG) { |
| 338 |
// EXPLICIT tagging |
| 339 |
// Find length of tagged item |
| 340 |
i = asn1_item_ex_i2d_opt(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, 0, |
| 341 |
optional); |
| 342 |
if (i <= 0) { |
| 343 |
return i; |
| 344 |
} |
| 345 |
// Find length of EXPLICIT tag |
| 346 |
ret = ASN1_object_size(/*constructed=*/1, i, ttag); |
| 347 |
if (out && ret != -1) { |
| 348 |
// Output tag and item |
| 349 |
ASN1_put_object(out, /*constructed=*/1, i, ttag, tclass); |
| 350 |
if (ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0) < 0) { |
| 351 |
return -1; |
| 352 |
} |
| 353 |
} |
| 354 |
return ret; |
| 355 |
} |
| 356 |
|
| 357 |
// Either normal or IMPLICIT tagging |
| 358 |
return asn1_item_ex_i2d_opt(pval, out, ASN1_ITEM_ptr(tt->item), ttag, tclass, |
| 359 |
optional); |
| 360 |
} |
| 361 |
|
| 362 |
// Temporary structure used to hold DER encoding of items for SET OF |
| 363 |
|
| 364 |
typedef struct { |
| 365 |
unsigned char *data; |
| 366 |
int length; |
| 367 |
} DER_ENC; |
| 368 |
|
| 369 |
static int der_cmp(const void *a, const void *b) { |
| 370 |
const DER_ENC *d1 = a, *d2 = b; |
| 371 |
int cmplen, i; |
| 372 |
cmplen = (d1->length < d2->length) ? d1->length : d2->length; |
| 373 |
i = OPENSSL_memcmp(d1->data, d2->data, cmplen); |
| 374 |
if (i) { |
| 375 |
return i; |
| 376 |
} |
| 377 |
return d1->length - d2->length; |
| 378 |
} |
| 379 |
|
| 380 |
// asn1_set_seq_out writes |sk| to |out| under the i2d output convention, |
| 381 |
// excluding the tag and length. It returns one on success and zero on error. |
| 382 |
// |skcontlen| must be the total encoded size. If |do_sort| is non-zero, the |
| 383 |
// elements are sorted for a SET OF type. Each element of |sk| has type |
| 384 |
// |item|. |
| 385 |
static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, |
| 386 |
int skcontlen, const ASN1_ITEM *item, int do_sort) { |
| 387 |
// No need to sort if there are fewer than two items. |
| 388 |
if (!do_sort || sk_ASN1_VALUE_num(sk) < 2) { |
| 389 |
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
| 390 |
ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i); |
| 391 |
if (ASN1_item_ex_i2d(&skitem, out, item, -1, 0) < 0) { |
| 392 |
return 0; |
| 393 |
} |
| 394 |
} |
| 395 |
return 1; |
| 396 |
} |
| 397 |
|
| 398 |
int ret = 0; |
| 399 |
unsigned char *const buf = OPENSSL_malloc(skcontlen); |
| 400 |
DER_ENC *encoded = OPENSSL_calloc(sk_ASN1_VALUE_num(sk), sizeof(*encoded)); |
| 401 |
if (encoded == NULL || buf == NULL) { |
| 402 |
goto err; |
| 403 |
} |
| 404 |
|
| 405 |
// Encode all the elements into |buf| and populate |encoded|. |
| 406 |
unsigned char *p = buf; |
| 407 |
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
| 408 |
ASN1_VALUE *skitem = sk_ASN1_VALUE_value(sk, i); |
| 409 |
encoded[i].data = p; |
| 410 |
encoded[i].length = ASN1_item_ex_i2d(&skitem, &p, item, -1, 0); |
| 411 |
if (encoded[i].length < 0) { |
| 412 |
goto err; |
| 413 |
} |
| 414 |
assert(p - buf <= skcontlen); |
| 415 |
} |
| 416 |
|
| 417 |
qsort(encoded, sk_ASN1_VALUE_num(sk), sizeof(*encoded), der_cmp); |
| 418 |
|
| 419 |
// Output the elements in sorted order. |
| 420 |
p = *out; |
| 421 |
for (size_t i = 0; i < sk_ASN1_VALUE_num(sk); i++) { |
| 422 |
OPENSSL_memcpy(p, encoded[i].data, encoded[i].length); |
| 423 |
p += encoded[i].length; |
| 424 |
} |
| 425 |
*out = p; |
| 426 |
|
| 427 |
ret = 1; |
| 428 |
|
| 429 |
err: |
| 430 |
OPENSSL_free(encoded); |
| 431 |
OPENSSL_free(buf); |
| 432 |
return ret; |
| 433 |
} |
| 434 |
|
| 435 |
// asn1_i2d_ex_primitive behaves like |ASN1_item_ex_i2d| but |item| must be a |
| 436 |
// a PRIMITIVE or MSTRING type that is not an |ASN1_ITEM_TEMPLATE|. |
| 437 |
static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, |
| 438 |
const ASN1_ITEM *it, int tag, int aclass, |
| 439 |
int optional) { |
| 440 |
// Get length of content octets and maybe find out the underlying type. |
| 441 |
int omit; |
| 442 |
int utype = it->utype; |
| 443 |
int len = asn1_ex_i2c(pval, NULL, &omit, &utype, it); |
| 444 |
if (len < 0) { |
| 445 |
return -1; |
| 446 |
} |
| 447 |
if (omit) { |
| 448 |
if (optional) { |
| 449 |
return 0; |
| 450 |
} |
| 451 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_MISSING_VALUE); |
| 452 |
return -1; |
| 453 |
} |
| 454 |
|
| 455 |
// If SEQUENCE, SET or OTHER then header is included in pseudo content |
| 456 |
// octets so don't include tag+length. We need to check here because the |
| 457 |
// call to asn1_ex_i2c() could change utype. |
| 458 |
int usetag = |
| 459 |
utype != V_ASN1_SEQUENCE && utype != V_ASN1_SET && utype != V_ASN1_OTHER; |
| 460 |
|
| 461 |
// If not implicitly tagged get tag from underlying type |
| 462 |
if (tag == -1) { |
| 463 |
tag = utype; |
| 464 |
} |
| 465 |
|
| 466 |
// Output tag+length followed by content octets |
| 467 |
if (out) { |
| 468 |
if (usetag) { |
| 469 |
ASN1_put_object(out, /*constructed=*/0, len, tag, aclass); |
| 470 |
} |
| 471 |
int len2 = asn1_ex_i2c(pval, *out, &omit, &utype, it); |
| 472 |
if (len2 < 0) { |
| 473 |
return -1; |
| 474 |
} |
| 475 |
assert(len == len2); |
| 476 |
assert(!omit); |
| 477 |
*out += len; |
| 478 |
} |
| 479 |
|
| 480 |
if (usetag) { |
| 481 |
return ASN1_object_size(/*constructed=*/0, len, tag); |
| 482 |
} |
| 483 |
return len; |
| 484 |
} |
| 485 |
|
| 486 |
// asn1_ex_i2c writes the |*pval| to |cout| under the i2d output convention, |
| 487 |
// excluding the tag and length. It returns the number of bytes written, |
| 488 |
// possibly zero, on success or -1 on error. If |*pval| should be omitted, it |
| 489 |
// returns zero and sets |*out_omit| to true. |
| 490 |
// |
| 491 |
// If |it| is an MSTRING or ANY type, it gets the underlying type from |*pval|, |
| 492 |
// which must be an |ASN1_STRING| or |ASN1_TYPE|, respectively. It then updates |
| 493 |
// |*putype| with the tag number of type used, or |V_ASN1_OTHER| if it was not a |
| 494 |
// universal type. If |*putype| is set to |V_ASN1_SEQUENCE|, |V_ASN1_SET|, or |
| 495 |
// |V_ASN1_OTHER|, it additionally outputs the tag and length, so the caller |
| 496 |
// must not do so. |
| 497 |
// |
| 498 |
// Otherwise, |*putype| must contain |it->utype|. |
| 499 |
// |
| 500 |
// WARNING: Unlike most functions in this file, |asn1_ex_i2c| can return zero |
| 501 |
// without omitting the element. ASN.1 values may have empty contents. |
| 502 |
static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *out_omit, |
| 503 |
int *putype, const ASN1_ITEM *it) { |
| 504 |
ASN1_BOOLEAN *tbool = NULL; |
| 505 |
ASN1_STRING *strtmp; |
| 506 |
ASN1_OBJECT *otmp; |
| 507 |
int utype; |
| 508 |
const unsigned char *cont; |
| 509 |
unsigned char c; |
| 510 |
int len; |
| 511 |
|
| 512 |
// Historically, |it->funcs| for primitive types contained an |
| 513 |
// |ASN1_PRIMITIVE_FUNCS| table of callbacks. |
| 514 |
assert(it->funcs == NULL); |
| 515 |
|
| 516 |
*out_omit = 0; |
| 517 |
|
| 518 |
// Should type be omitted? |
| 519 |
if ((it->itype != ASN1_ITYPE_PRIMITIVE) || (it->utype != V_ASN1_BOOLEAN)) { |
| 520 |
if (!*pval) { |
| 521 |
*out_omit = 1; |
| 522 |
return 0; |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
if (it->itype == ASN1_ITYPE_MSTRING) { |
| 527 |
// If MSTRING type set the underlying type |
| 528 |
strtmp = (ASN1_STRING *)*pval; |
| 529 |
utype = strtmp->type; |
| 530 |
if (utype < 0 && utype != V_ASN1_OTHER) { |
| 531 |
// MSTRINGs can have type -1 when default-constructed. |
| 532 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); |
| 533 |
return -1; |
| 534 |
} |
| 535 |
// Negative INTEGER and ENUMERATED values use |ASN1_STRING| type values |
| 536 |
// that do not match their corresponding utype values. INTEGERs cannot |
| 537 |
// participate in MSTRING types, but ENUMERATEDs can. |
| 538 |
// |
| 539 |
// TODO(davidben): Is this a bug? Although arguably one of the MSTRING |
| 540 |
// types should contain more values, rather than less. See |
| 541 |
// https://crbug.com/boringssl/412. But it is not possible to fit all |
| 542 |
// possible ANY values into an |ASN1_STRING|, so matching the spec here |
| 543 |
// is somewhat hopeless. |
| 544 |
if (utype == V_ASN1_NEG_INTEGER) { |
| 545 |
utype = V_ASN1_INTEGER; |
| 546 |
} else if (utype == V_ASN1_NEG_ENUMERATED) { |
| 547 |
utype = V_ASN1_ENUMERATED; |
| 548 |
} |
| 549 |
*putype = utype; |
| 550 |
} else if (it->utype == V_ASN1_ANY) { |
| 551 |
// If ANY set type and pointer to value |
| 552 |
ASN1_TYPE *typ; |
| 553 |
typ = (ASN1_TYPE *)*pval; |
| 554 |
utype = typ->type; |
| 555 |
if (utype < 0 && utype != V_ASN1_OTHER) { |
| 556 |
// |ASN1_TYPE|s can have type -1 when default-constructed. |
| 557 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_WRONG_TYPE); |
| 558 |
return -1; |
| 559 |
} |
| 560 |
*putype = utype; |
| 561 |
pval = &typ->value.asn1_value; |
| 562 |
} else { |
| 563 |
utype = *putype; |
| 564 |
} |
| 565 |
|
| 566 |
switch (utype) { |
| 567 |
case V_ASN1_OBJECT: |
| 568 |
otmp = (ASN1_OBJECT *)*pval; |
| 569 |
cont = otmp->data; |
| 570 |
len = otmp->length; |
| 571 |
if (len == 0) { |
| 572 |
// Some |ASN1_OBJECT|s do not have OIDs and cannot be serialized. |
| 573 |
OPENSSL_PUT_ERROR(ASN1, ASN1_R_ILLEGAL_OBJECT); |
| 574 |
return -1; |
| 575 |
} |
| 576 |
break; |
| 577 |
|
| 578 |
case V_ASN1_NULL: |
| 579 |
cont = NULL; |
| 580 |
len = 0; |
| 581 |
break; |
| 582 |
|
| 583 |
case V_ASN1_BOOLEAN: |
| 584 |
tbool = (ASN1_BOOLEAN *)pval; |
| 585 |
if (*tbool == ASN1_BOOLEAN_NONE) { |
| 586 |
*out_omit = 1; |
| 587 |
return 0; |
| 588 |
} |
| 589 |
if (it->utype != V_ASN1_ANY) { |
| 590 |
// Default handling if value == size field then omit |
| 591 |
if ((*tbool && (it->size > 0)) || (!*tbool && !it->size)) { |
| 592 |
*out_omit = 1; |
| 593 |
return 0; |
| 594 |
} |
| 595 |
} |
| 596 |
c = *tbool ? 0xff : 0x00; |
| 597 |
cont = &c; |
| 598 |
len = 1; |
| 599 |
break; |
| 600 |
|
| 601 |
case V_ASN1_BIT_STRING: { |
| 602 |
int ret = |
| 603 |
i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, cout ? &cout : NULL); |
| 604 |
// |i2c_ASN1_BIT_STRING| returns zero on error instead of -1. |
| 605 |
return ret <= 0 ? -1 : ret; |
| 606 |
} |
| 607 |
|
| 608 |
case V_ASN1_INTEGER: |
| 609 |
case V_ASN1_ENUMERATED: { |
| 610 |
// |i2c_ASN1_INTEGER| also handles ENUMERATED. |
| 611 |
int ret = i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL); |
| 612 |
// |i2c_ASN1_INTEGER| returns zero on error instead of -1. |
| 613 |
return ret <= 0 ? -1 : ret; |
| 614 |
} |
| 615 |
|
| 616 |
case V_ASN1_OCTET_STRING: |
| 617 |
case V_ASN1_NUMERICSTRING: |
| 618 |
case V_ASN1_PRINTABLESTRING: |
| 619 |
case V_ASN1_T61STRING: |
| 620 |
case V_ASN1_VIDEOTEXSTRING: |
| 621 |
case V_ASN1_IA5STRING: |
| 622 |
case V_ASN1_UTCTIME: |
| 623 |
case V_ASN1_GENERALIZEDTIME: |
| 624 |
case V_ASN1_GRAPHICSTRING: |
| 625 |
case V_ASN1_VISIBLESTRING: |
| 626 |
case V_ASN1_GENERALSTRING: |
| 627 |
case V_ASN1_UNIVERSALSTRING: |
| 628 |
case V_ASN1_BMPSTRING: |
| 629 |
case V_ASN1_UTF8STRING: |
| 630 |
case V_ASN1_SEQUENCE: |
| 631 |
case V_ASN1_SET: |
| 632 |
default: |
| 633 |
// All based on ASN1_STRING and handled the same |
| 634 |
strtmp = (ASN1_STRING *)*pval; |
| 635 |
cont = strtmp->data; |
| 636 |
len = strtmp->length; |
| 637 |
|
| 638 |
break; |
| 639 |
} |
| 640 |
if (cout && len) { |
| 641 |
OPENSSL_memcpy(cout, cont, len); |
| 642 |
} |
| 643 |
return len; |
| 644 |
} |