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