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