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