/src/openssl30/crypto/asn1/tasn_enc.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2000-2025 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 | 1.32M | { |
45 | 1.32M | return asn1_item_flags_i2d(val, out, it, 0); |
46 | 1.32M | } |
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 | 1.32M | { |
58 | 1.32M | if (out != NULL && *out == NULL) { |
59 | 979k | unsigned char *p, *buf; |
60 | 979k | int len; |
61 | | |
62 | 979k | len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); |
63 | 979k | if (len <= 0) |
64 | 104 | return len; |
65 | 979k | if ((buf = OPENSSL_malloc(len)) == NULL) { |
66 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
67 | 0 | return -1; |
68 | 0 | } |
69 | 979k | p = buf; |
70 | 979k | ASN1_item_ex_i2d(&val, &p, it, -1, flags); |
71 | 979k | *out = buf; |
72 | 979k | return len; |
73 | 979k | } |
74 | | |
75 | 340k | return ASN1_item_ex_i2d(&val, out, it, -1, flags); |
76 | 1.32M | } |
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 | 498M | { |
86 | 498M | const ASN1_TEMPLATE *tt = NULL; |
87 | 498M | int i, seqcontlen, seqlen, ndef = 1; |
88 | 498M | const ASN1_EXTERN_FUNCS *ef; |
89 | 498M | const ASN1_AUX *aux = it->funcs; |
90 | 498M | ASN1_aux_const_cb *asn1_cb = NULL; |
91 | | |
92 | 498M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) && *pval == NULL) |
93 | 6.95M | return 0; |
94 | | |
95 | 491M | if (aux != NULL) { |
96 | 7.16M | asn1_cb = ((aux->flags & ASN1_AFLG_CONST_CB) != 0) ? aux->asn1_const_cb |
97 | 7.16M | : (ASN1_aux_const_cb *)aux->asn1_cb; /* backward compatibility */ |
98 | 7.16M | } |
99 | | |
100 | 491M | switch (it->itype) { |
101 | | |
102 | 294M | case ASN1_ITYPE_PRIMITIVE: |
103 | 294M | if (it->templates) |
104 | 23.1M | return asn1_template_ex_i2d(pval, out, it->templates, |
105 | 23.1M | tag, aclass); |
106 | 271M | return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); |
107 | | |
108 | 100M | 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 | 100M | if (tag != -1) { |
114 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE); |
115 | 0 | return -1; |
116 | 0 | } |
117 | 100M | return asn1_i2d_ex_primitive(pval, out, it, -1, aclass); |
118 | | |
119 | 6.57M | 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 | 6.57M | if (tag != -1) { |
125 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_BAD_TEMPLATE); |
126 | 0 | return -1; |
127 | 0 | } |
128 | 6.57M | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) |
129 | 0 | return 0; |
130 | 6.57M | i = ossl_asn1_get_choice_selector_const(pval, it); |
131 | 6.57M | if ((i >= 0) && (i < it->tcount)) { |
132 | 6.57M | const ASN1_VALUE **pchval; |
133 | 6.57M | const ASN1_TEMPLATE *chtt; |
134 | 6.57M | chtt = it->templates + i; |
135 | 6.57M | pchval = ossl_asn1_get_const_field_ptr(pval, chtt); |
136 | 6.57M | return asn1_template_ex_i2d(pchval, out, chtt, -1, aclass); |
137 | 6.57M | } |
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 | 1.46M | case ASN1_ITYPE_EXTERN: |
144 | | /* If new style i2d it does all the work */ |
145 | 1.46M | ef = it->funcs; |
146 | 1.46M | return ef->asn1_ex_i2d(pval, out, it, tag, aclass); |
147 | | |
148 | 101k | case ASN1_ITYPE_NDEF_SEQUENCE: |
149 | | /* Use indefinite length constructed if requested */ |
150 | 101k | if (aclass & ASN1_TFLG_NDEF) |
151 | 0 | ndef = 2; |
152 | | /* fall through */ |
153 | | |
154 | 89.4M | case ASN1_ITYPE_SEQUENCE: |
155 | 89.4M | i = ossl_asn1_enc_restore(&seqcontlen, out, pval, it); |
156 | | /* An error occurred */ |
157 | 89.4M | if (i < 0) |
158 | 0 | return 0; |
159 | | /* We have a valid cached encoding... */ |
160 | 89.4M | if (i > 0) |
161 | 1.66M | return seqcontlen; |
162 | | /* Otherwise carry on */ |
163 | 87.7M | seqcontlen = 0; |
164 | | /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ |
165 | 87.7M | if (tag == -1) { |
166 | 86.6M | tag = V_ASN1_SEQUENCE; |
167 | | /* Retain any other flags in aclass */ |
168 | 86.6M | aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) |
169 | 86.6M | | V_ASN1_UNIVERSAL; |
170 | 86.6M | } |
171 | 87.7M | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) |
172 | 0 | return 0; |
173 | | /* First work out sequence content length */ |
174 | 282M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
175 | 195M | const ASN1_TEMPLATE *seqtt; |
176 | 195M | const ASN1_VALUE **pseqval; |
177 | 195M | int tmplen; |
178 | 195M | seqtt = ossl_asn1_do_adb(*pval, tt, 1); |
179 | 195M | if (!seqtt) |
180 | 0 | return 0; |
181 | 195M | pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt); |
182 | 195M | tmplen = asn1_template_ex_i2d(pseqval, NULL, seqtt, -1, aclass); |
183 | 195M | if (tmplen == -1 || (tmplen > INT_MAX - seqcontlen)) |
184 | 0 | return -1; |
185 | 195M | seqcontlen += tmplen; |
186 | 195M | } |
187 | | |
188 | 87.7M | seqlen = ASN1_object_size(ndef, seqcontlen, tag); |
189 | 87.7M | if (!out || seqlen == -1) |
190 | 60.8M | return seqlen; |
191 | | /* Output SEQUENCE header */ |
192 | 26.8M | ASN1_put_object(out, ndef, seqcontlen, tag, aclass); |
193 | 84.4M | for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { |
194 | 57.6M | const ASN1_TEMPLATE *seqtt; |
195 | 57.6M | const ASN1_VALUE **pseqval; |
196 | 57.6M | seqtt = ossl_asn1_do_adb(*pval, tt, 1); |
197 | 57.6M | if (!seqtt) |
198 | 0 | return 0; |
199 | 57.6M | pseqval = ossl_asn1_get_const_field_ptr(pval, seqtt); |
200 | | /* FIXME: check for errors in enhanced version */ |
201 | 57.6M | asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass); |
202 | 57.6M | } |
203 | 26.8M | if (ndef == 2) |
204 | 0 | ASN1_put_eoc(out); |
205 | 26.8M | if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) |
206 | 0 | return 0; |
207 | 26.8M | return seqlen; |
208 | | |
209 | 0 | default: |
210 | 0 | return 0; |
211 | 491M | } |
212 | 0 | return 0; |
213 | 491M | } |
214 | | |
215 | | static int asn1_template_ex_i2d(const ASN1_VALUE **pval, unsigned char **out, |
216 | | const ASN1_TEMPLATE *tt, int tag, int iclass) |
217 | 282M | { |
218 | 282M | const int flags = tt->flags; |
219 | 282M | int i, ret, ttag, tclass, ndef, len; |
220 | 282M | const ASN1_VALUE *tval; |
221 | | |
222 | | /* |
223 | | * If field is embedded then val needs fixing so it is a pointer to |
224 | | * a pointer to a field. |
225 | | */ |
226 | 282M | if (flags & ASN1_TFLG_EMBED) { |
227 | 7.21M | tval = (ASN1_VALUE *)pval; |
228 | 7.21M | pval = &tval; |
229 | 7.21M | } |
230 | | /* |
231 | | * Work out tag and class to use: tagging may come either from the |
232 | | * template or the arguments, not both because this would create |
233 | | * ambiguity. Additionally the iclass argument may contain some |
234 | | * additional flags which should be noted and passed down to other |
235 | | * levels. |
236 | | */ |
237 | 282M | if (flags & ASN1_TFLG_TAG_MASK) { |
238 | | /* Error if argument and template tagging */ |
239 | 27.2M | if (tag != -1) |
240 | | /* FIXME: error code here */ |
241 | 0 | return -1; |
242 | | /* Get tagging from template */ |
243 | 27.2M | ttag = tt->tag; |
244 | 27.2M | tclass = flags & ASN1_TFLG_TAG_CLASS; |
245 | 255M | } else if (tag != -1) { |
246 | | /* No template tagging, get from arguments */ |
247 | 0 | ttag = tag; |
248 | 0 | tclass = iclass & ASN1_TFLG_TAG_CLASS; |
249 | 255M | } else { |
250 | 255M | ttag = -1; |
251 | 255M | tclass = 0; |
252 | 255M | } |
253 | | /* |
254 | | * Remove any class mask from iflag. |
255 | | */ |
256 | 282M | iclass &= ~ASN1_TFLG_TAG_CLASS; |
257 | | |
258 | | /* |
259 | | * At this point 'ttag' contains the outer tag to use, 'tclass' is the |
260 | | * class and iclass is any flags passed to this function. |
261 | | */ |
262 | | |
263 | | /* if template and arguments require ndef, use it */ |
264 | 282M | if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF)) |
265 | 0 | ndef = 2; |
266 | 282M | else |
267 | 282M | ndef = 1; |
268 | | |
269 | 282M | if (flags & ASN1_TFLG_SK_MASK) { |
270 | | /* SET OF, SEQUENCE OF */ |
271 | 28.1M | STACK_OF(const_ASN1_VALUE) *sk = (STACK_OF(const_ASN1_VALUE) *)*pval; |
272 | 28.1M | int isset, sktag, skaclass; |
273 | 28.1M | int skcontlen, sklen; |
274 | 28.1M | const ASN1_VALUE *skitem; |
275 | | |
276 | 28.1M | if (*pval == NULL) |
277 | 4.51M | return 0; |
278 | | |
279 | 23.5M | if (flags & ASN1_TFLG_SET_OF) { |
280 | 22.2M | isset = 1; |
281 | | /* 2 means we reorder */ |
282 | 22.2M | if (flags & ASN1_TFLG_SEQUENCE_OF) |
283 | 1.07k | isset = 2; |
284 | 22.2M | } else |
285 | 1.29M | isset = 0; |
286 | | |
287 | | /* |
288 | | * Work out inner tag value: if EXPLICIT or no tagging use underlying |
289 | | * type. |
290 | | */ |
291 | 23.5M | if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG)) { |
292 | 144k | sktag = ttag; |
293 | 144k | skaclass = tclass; |
294 | 23.4M | } else { |
295 | 23.4M | skaclass = V_ASN1_UNIVERSAL; |
296 | 23.4M | if (isset) |
297 | 22.2M | sktag = V_ASN1_SET; |
298 | 1.19M | else |
299 | 1.19M | sktag = V_ASN1_SEQUENCE; |
300 | 23.4M | } |
301 | | |
302 | | /* Determine total length of items */ |
303 | 23.5M | skcontlen = 0; |
304 | 198M | for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) { |
305 | 175M | skitem = sk_const_ASN1_VALUE_value(sk, i); |
306 | 175M | len = ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item), |
307 | 175M | -1, iclass); |
308 | 175M | if (len == -1 || (skcontlen > INT_MAX - len)) |
309 | 0 | return -1; |
310 | 175M | if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) { |
311 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT); |
312 | 0 | return -1; |
313 | 0 | } |
314 | 175M | skcontlen += len; |
315 | 175M | } |
316 | 23.5M | sklen = ASN1_object_size(ndef, skcontlen, sktag); |
317 | 23.5M | if (sklen == -1) |
318 | 0 | return -1; |
319 | | /* If EXPLICIT need length of surrounding tag */ |
320 | 23.5M | if (flags & ASN1_TFLG_EXPTAG) |
321 | 88.4k | ret = ASN1_object_size(ndef, sklen, ttag); |
322 | 23.4M | else |
323 | 23.4M | ret = sklen; |
324 | | |
325 | 23.5M | if (!out || ret == -1) |
326 | 13.2M | return ret; |
327 | | |
328 | | /* Now encode this lot... */ |
329 | | /* EXPLICIT tag */ |
330 | 10.3M | if (flags & ASN1_TFLG_EXPTAG) |
331 | 9.25k | ASN1_put_object(out, ndef, sklen, ttag, tclass); |
332 | | /* SET or SEQUENCE and IMPLICIT tag */ |
333 | 10.3M | ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); |
334 | | /* And the stuff itself */ |
335 | 10.3M | asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), |
336 | 10.3M | isset, iclass); |
337 | 10.3M | if (ndef == 2) { |
338 | 0 | ASN1_put_eoc(out); |
339 | 0 | if (flags & ASN1_TFLG_EXPTAG) |
340 | 0 | ASN1_put_eoc(out); |
341 | 0 | } |
342 | | |
343 | 10.3M | return ret; |
344 | 23.5M | } |
345 | | |
346 | 254M | if (flags & ASN1_TFLG_EXPTAG) { |
347 | | /* EXPLICIT tagging */ |
348 | | /* Find length of tagged item */ |
349 | 8.27M | i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, iclass); |
350 | 8.27M | if (i == 0) { |
351 | 6.79M | if ((tt->flags & ASN1_TFLG_OPTIONAL) == 0) { |
352 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT); |
353 | 0 | return -1; |
354 | 0 | } |
355 | 6.79M | return 0; |
356 | 6.79M | } |
357 | | /* Find length of EXPLICIT tag */ |
358 | 1.47M | ret = ASN1_object_size(ndef, i, ttag); |
359 | 1.47M | if (out && ret != -1) { |
360 | | /* Output tag and item */ |
361 | 279k | ASN1_put_object(out, ndef, i, ttag, tclass); |
362 | 279k | ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, iclass); |
363 | 279k | if (ndef == 2) |
364 | 0 | ASN1_put_eoc(out); |
365 | 279k | } |
366 | 1.47M | return ret; |
367 | 8.27M | } |
368 | | |
369 | | /* Either normal or IMPLICIT tagging: combine class and flags */ |
370 | 245M | len = ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), |
371 | 245M | ttag, tclass | iclass); |
372 | 245M | if (len == 0 && (tt->flags & ASN1_TFLG_OPTIONAL) == 0) { |
373 | 0 | ERR_raise(ERR_LIB_ASN1, ASN1_R_ILLEGAL_ZERO_CONTENT); |
374 | 0 | return -1; |
375 | 0 | } |
376 | 245M | return len; |
377 | 245M | } |
378 | | |
379 | | /* Temporary structure used to hold DER encoding of items for SET OF */ |
380 | | |
381 | | typedef struct { |
382 | | unsigned char *data; |
383 | | int length; |
384 | | const ASN1_VALUE *field; |
385 | | } DER_ENC; |
386 | | |
387 | | static int der_cmp(const void *a, const void *b) |
388 | 350M | { |
389 | 350M | const DER_ENC *d1 = a, *d2 = b; |
390 | 350M | int cmplen, i; |
391 | 350M | cmplen = (d1->length < d2->length) ? d1->length : d2->length; |
392 | 350M | i = memcmp(d1->data, d2->data, cmplen); |
393 | 350M | if (i) |
394 | 153M | return i; |
395 | 197M | return d1->length - d2->length; |
396 | 350M | } |
397 | | |
398 | | /* Output the content octets of SET OF or SEQUENCE OF */ |
399 | | |
400 | | static int asn1_set_seq_out(STACK_OF(const_ASN1_VALUE) *sk, |
401 | | unsigned char **out, |
402 | | int skcontlen, const ASN1_ITEM *item, |
403 | | int do_sort, int iclass) |
404 | 10.3M | { |
405 | 10.3M | int i, ret = 0; |
406 | 10.3M | const ASN1_VALUE *skitem; |
407 | 10.3M | unsigned char *tmpdat = NULL, *p = NULL; |
408 | 10.3M | DER_ENC *derlst = NULL, *tder; |
409 | | |
410 | 10.3M | if (do_sort) { |
411 | | /* Don't need to sort less than 2 items */ |
412 | 10.0M | if (sk_const_ASN1_VALUE_num(sk) < 2) |
413 | 9.54M | do_sort = 0; |
414 | 467k | else { |
415 | 467k | derlst = OPENSSL_malloc(sk_const_ASN1_VALUE_num(sk) |
416 | 467k | * sizeof(*derlst)); |
417 | 467k | if (derlst == NULL) { |
418 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
419 | 0 | return 0; |
420 | 0 | } |
421 | 467k | tmpdat = OPENSSL_malloc(skcontlen); |
422 | 467k | if (tmpdat == NULL) { |
423 | 0 | ERR_raise(ERR_LIB_ASN1, ERR_R_MALLOC_FAILURE); |
424 | 0 | goto err; |
425 | 0 | } |
426 | 467k | } |
427 | 10.0M | } |
428 | | /* If not sorting just output each item */ |
429 | 10.3M | if (!do_sort) { |
430 | 29.3M | for (i = 0; i < sk_const_ASN1_VALUE_num(sk); i++) { |
431 | 19.4M | skitem = sk_const_ASN1_VALUE_value(sk, i); |
432 | 19.4M | ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); |
433 | 19.4M | } |
434 | 9.84M | return 1; |
435 | 9.84M | } |
436 | 467k | p = tmpdat; |
437 | | |
438 | | /* Doing sort: build up a list of each member's DER encoding */ |
439 | 30.5M | for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) { |
440 | 30.0M | skitem = sk_const_ASN1_VALUE_value(sk, i); |
441 | 30.0M | tder->data = p; |
442 | 30.0M | tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass); |
443 | 30.0M | tder->field = skitem; |
444 | 30.0M | } |
445 | | |
446 | | /* Now sort them */ |
447 | 467k | qsort(derlst, sk_const_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); |
448 | | /* Output sorted DER encoding */ |
449 | 467k | p = *out; |
450 | 30.5M | for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) { |
451 | 30.0M | memcpy(p, tder->data, tder->length); |
452 | 30.0M | p += tder->length; |
453 | 30.0M | } |
454 | 467k | *out = p; |
455 | | /* If do_sort is 2 then reorder the STACK */ |
456 | 467k | if (do_sort == 2) { |
457 | 2.39k | for (i = 0, tder = derlst; i < sk_const_ASN1_VALUE_num(sk); i++, tder++) |
458 | 2.27k | (void)sk_const_ASN1_VALUE_set(sk, i, tder->field); |
459 | 124 | } |
460 | 467k | ret = 1; |
461 | 467k | err: |
462 | 467k | OPENSSL_free(derlst); |
463 | 467k | OPENSSL_free(tmpdat); |
464 | 467k | return ret; |
465 | 467k | } |
466 | | |
467 | | static int asn1_i2d_ex_primitive(const ASN1_VALUE **pval, unsigned char **out, |
468 | | const ASN1_ITEM *it, int tag, int aclass) |
469 | 371M | { |
470 | 371M | int len; |
471 | 371M | int utype; |
472 | 371M | int usetag; |
473 | 371M | int ndef = 0; |
474 | | |
475 | 371M | utype = it->utype; |
476 | | |
477 | | /* |
478 | | * Get length of content octets and maybe find out the underlying type. |
479 | | */ |
480 | | |
481 | 371M | len = asn1_ex_i2c(pval, NULL, &utype, it); |
482 | | |
483 | | /* |
484 | | * If SEQUENCE, SET or OTHER then header is included in pseudo content |
485 | | * octets so don't include tag+length. We need to check here because the |
486 | | * call to asn1_ex_i2c() could change utype. |
487 | | */ |
488 | 371M | if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) |
489 | 103M | usetag = 0; |
490 | 268M | else |
491 | 268M | usetag = 1; |
492 | | |
493 | | /* -1 means omit type */ |
494 | | |
495 | 371M | if (len == -1) |
496 | 12.6M | return 0; |
497 | | |
498 | | /* -2 return is special meaning use ndef */ |
499 | 358M | if (len == -2) { |
500 | 0 | ndef = 2; |
501 | 0 | len = 0; |
502 | 0 | } |
503 | | |
504 | | /* If not implicitly tagged get tag from underlying type */ |
505 | 358M | if (tag == -1) |
506 | 353M | tag = utype; |
507 | | |
508 | | /* Output tag+length followed by content octets */ |
509 | 358M | if (out) { |
510 | 74.9M | if (usetag) |
511 | 57.0M | ASN1_put_object(out, ndef, len, tag, aclass); |
512 | 74.9M | asn1_ex_i2c(pval, *out, &utype, it); |
513 | 74.9M | if (ndef) |
514 | 0 | ASN1_put_eoc(out); |
515 | 74.9M | else |
516 | 74.9M | *out += len; |
517 | 74.9M | } |
518 | | |
519 | 358M | if (usetag) |
520 | 255M | return ASN1_object_size(ndef, len, tag); |
521 | 103M | return len; |
522 | 358M | } |
523 | | |
524 | | /* Produce content octets from a structure */ |
525 | | |
526 | | static int asn1_ex_i2c(const ASN1_VALUE **pval, unsigned char *cout, int *putype, |
527 | | const ASN1_ITEM *it) |
528 | 446M | { |
529 | 446M | ASN1_BOOLEAN *tbool = NULL; |
530 | 446M | ASN1_STRING *strtmp; |
531 | 446M | ASN1_OBJECT *otmp; |
532 | 446M | int utype; |
533 | 446M | const unsigned char *cont; |
534 | 446M | unsigned char c; |
535 | 446M | int len; |
536 | 446M | const ASN1_PRIMITIVE_FUNCS *pf; |
537 | 446M | pf = it->funcs; |
538 | 446M | if (pf && pf->prim_i2c) |
539 | 2.43M | return pf->prim_i2c(pval, cout, putype, it); |
540 | | |
541 | | /* Should type be omitted? */ |
542 | 443M | if ((it->itype != ASN1_ITYPE_PRIMITIVE) |
543 | 442M | || (it->utype != V_ASN1_BOOLEAN)) { |
544 | 442M | if (*pval == NULL) |
545 | 11.2M | return -1; |
546 | 442M | } |
547 | | |
548 | 432M | if (it->itype == ASN1_ITYPE_MSTRING) { |
549 | | /* If MSTRING type set the underlying type */ |
550 | 124M | strtmp = (ASN1_STRING *)*pval; |
551 | 124M | utype = strtmp->type; |
552 | 124M | *putype = utype; |
553 | 308M | } else if (it->utype == V_ASN1_ANY) { |
554 | | /* If ANY set type and pointer to value */ |
555 | 143M | ASN1_TYPE *typ; |
556 | 143M | typ = (ASN1_TYPE *)*pval; |
557 | 143M | utype = typ->type; |
558 | 143M | *putype = utype; |
559 | 143M | pval = (const ASN1_VALUE **)&typ->value.asn1_value; /* actually is const */ |
560 | 143M | } else |
561 | 164M | utype = *putype; |
562 | | |
563 | 432M | switch (utype) { |
564 | 133M | case V_ASN1_OBJECT: |
565 | 133M | otmp = (ASN1_OBJECT *)*pval; |
566 | 133M | cont = otmp->data; |
567 | 133M | len = otmp->length; |
568 | 133M | if (cont == NULL || len == 0) |
569 | 0 | return -1; |
570 | 133M | break; |
571 | | |
572 | 133M | case V_ASN1_UNDEF: |
573 | 0 | return -2; |
574 | | |
575 | 1.85M | case V_ASN1_NULL: |
576 | 1.85M | cont = NULL; |
577 | 1.85M | len = 0; |
578 | 1.85M | break; |
579 | | |
580 | 1.55M | case V_ASN1_BOOLEAN: |
581 | 1.55M | tbool = (ASN1_BOOLEAN *)pval; |
582 | 1.55M | if (*tbool == -1) |
583 | 277k | return -1; |
584 | 1.27M | if (it->utype != V_ASN1_ANY) { |
585 | | /* |
586 | | * Default handling if value == size field then omit |
587 | | */ |
588 | 1.13M | if (*tbool && (it->size > 0)) |
589 | 48 | return -1; |
590 | 1.13M | if (!*tbool && !it->size) |
591 | 1.07M | return -1; |
592 | 1.13M | } |
593 | 204k | c = (unsigned char)*tbool; |
594 | 204k | cont = &c; |
595 | 204k | len = 1; |
596 | 204k | break; |
597 | | |
598 | 31.6M | case V_ASN1_BIT_STRING: |
599 | 31.6M | return ossl_i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, |
600 | 31.6M | cout ? &cout : NULL); |
601 | | |
602 | 25.0M | case V_ASN1_INTEGER: |
603 | 27.4M | case V_ASN1_ENUMERATED: |
604 | | /* |
605 | | * These are all have the same content format as ASN1_INTEGER |
606 | | */ |
607 | 27.4M | return ossl_i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL); |
608 | | |
609 | 3.37M | case V_ASN1_OCTET_STRING: |
610 | 15.1M | case V_ASN1_NUMERICSTRING: |
611 | 16.2M | case V_ASN1_PRINTABLESTRING: |
612 | 16.8M | case V_ASN1_T61STRING: |
613 | 16.8M | case V_ASN1_VIDEOTEXSTRING: |
614 | 18.6M | case V_ASN1_IA5STRING: |
615 | 18.6M | case V_ASN1_UTCTIME: |
616 | 18.9M | case V_ASN1_GENERALIZEDTIME: |
617 | 22.0M | case V_ASN1_GRAPHICSTRING: |
618 | 22.1M | case V_ASN1_VISIBLESTRING: |
619 | 22.2M | case V_ASN1_GENERALSTRING: |
620 | 23.7M | case V_ASN1_UNIVERSALSTRING: |
621 | 24.7M | case V_ASN1_BMPSTRING: |
622 | 70.4M | case V_ASN1_UTF8STRING: |
623 | 87.2M | case V_ASN1_SEQUENCE: |
624 | 173M | case V_ASN1_SET: |
625 | 236M | default: |
626 | | /* All based on ASN1_STRING and handled the same */ |
627 | 236M | strtmp = (ASN1_STRING *)*pval; |
628 | | /* Special handling for NDEF */ |
629 | 236M | if ((it->size == ASN1_TFLG_NDEF) |
630 | 1.09k | && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) { |
631 | 0 | if (cout) { |
632 | 0 | strtmp->data = cout; |
633 | 0 | strtmp->length = 0; |
634 | 0 | } |
635 | | /* Special return code */ |
636 | 0 | return -2; |
637 | 0 | } |
638 | 236M | cont = strtmp->data; |
639 | 236M | len = strtmp->length; |
640 | | |
641 | 236M | break; |
642 | 432M | } |
643 | 371M | if (cout && len) |
644 | 46.5M | memcpy(cout, cont, len); |
645 | 371M | return len; |
646 | 432M | } |