Bug Summary

File:src/lib/libcrypto/asn1/tasn_enc.c
Warning:line 205, column 10
Access to field 'asn1_ex_i2d' results in a dereference of a null pointer (loaded from variable 'ef')

Annotated Source Code

Press '?' to see keyboard shortcuts

clang -cc1 -cc1 -triple amd64-unknown-openbsd7.4 -analyze -disable-free -clear-ast-before-backend -disable-llvm-verifier -discard-value-names -main-file-name tasn_enc.c -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -setup-static-analyzer -mrelocation-model pic -pic-level 1 -pic-is-pie -mframe-pointer=all -relaxed-aliasing -ffp-contract=on -fno-rounding-math -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -target-feature +retpoline-indirect-calls -target-feature +retpoline-indirect-branches -tune-cpu generic -debugger-tuning=gdb -fcoverage-compilation-dir=/usr/src/lib/libcrypto/obj -resource-dir /usr/local/llvm16/lib/clang/16 -D LIBRESSL_INTERNAL -D HAVE_FUNOPEN -I /usr/src/lib/libcrypto -I /usr/src/lib/libcrypto/arch/amd64 -I /usr/src/lib/libcrypto/asn1 -I /usr/src/lib/libcrypto/bio -I /usr/src/lib/libcrypto/bn -I /usr/src/lib/libcrypto/bn/arch/amd64 -I /usr/src/lib/libcrypto/bytestring -I /usr/src/lib/libcrypto/curve25519 -I /usr/src/lib/libcrypto/dh -I /usr/src/lib/libcrypto/dsa -I /usr/src/lib/libcrypto/ec -I /usr/src/lib/libcrypto/ecdsa -I /usr/src/lib/libcrypto/evp -I /usr/src/lib/libcrypto/hidden -I /usr/src/lib/libcrypto/hmac -I /usr/src/lib/libcrypto/kdf -I /usr/src/lib/libcrypto/modes -I /usr/src/lib/libcrypto/ocsp -I /usr/src/lib/libcrypto/pkcs12 -I /usr/src/lib/libcrypto/rsa -I /usr/src/lib/libcrypto/sha -I /usr/src/lib/libcrypto/ts -I /usr/src/lib/libcrypto/x509 -I /usr/src/lib/libcrypto/obj -D AES_ASM -D BSAES_ASM -D VPAES_ASM -D OPENSSL_IA32_SSE2 -D RSA_ASM -D OPENSSL_BN_ASM_MONT -D OPENSSL_BN_ASM_MONT5 -D MD5_ASM -D GHASH_ASM -D RC4_MD5_ASM -D SHA1_ASM -D SHA256_ASM -D SHA512_ASM -D WHIRLPOOL_ASM -D OPENSSL_CPUID_OBJ -internal-isystem /usr/local/llvm16/lib/clang/16/include -internal-externc-isystem /usr/include -O2 -fdebug-compilation-dir=/usr/src/lib/libcrypto/obj -ferror-limit 19 -fwrapv -D_RET_PROTECTOR -ret-protector -fcf-protection=branch -fno-jump-tables -fgnuc-version=4.2.1 -vectorize-loops -vectorize-slp -fno-builtin-malloc -fno-builtin-calloc -fno-builtin-realloc -fno-builtin-valloc -fno-builtin-free -fno-builtin-strdup -fno-builtin-strndup -analyzer-output=html -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /home/ben/Projects/scan/2024-01-11-140451-98009-1 -x c /usr/src/lib/libcrypto/asn1/tasn_enc.c
1/* $OpenBSD: tasn_enc.c,v 1.33 2023/07/28 10:00:10 tb Exp $ */
2/* Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
3 * project 2000.
4 */
5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
58
59#include <stddef.h>
60#include <string.h>
61
62#include <openssl/asn1.h>
63#include <openssl/asn1t.h>
64#include <openssl/err.h>
65#include <openssl/objects.h>
66
67#include "asn1_local.h"
68
69static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
70 const ASN1_ITEM *it, int tag, int aclass);
71static int asn1_set_seq_out(STACK_OF(ASN1_VALUE)struct stack_st_ASN1_VALUE *sk, unsigned char **out,
72 int skcontlen, const ASN1_ITEM *item, int do_sort, int iclass);
73static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
74 const ASN1_TEMPLATE *tt, int tag, int aclass);
75static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
76 const ASN1_ITEM *it, int flags);
77static int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
78 const ASN1_ITEM *it);
79
80/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
81 * to use indefinite length constructed encoding, where appropriate
82 */
83
84int
85ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
86{
87 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF(0x1<<11));
88}
89
90int
91ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
92{
93 return asn1_item_flags_i2d(val, out, it, 0);
1
Calling 'asn1_item_flags_i2d'
94}
95LCRYPTO_ALIAS(ASN1_item_i2d)asm("");
96
97/* Encode an ASN1 item, this is use by the
98 * standard 'i2d' function. 'out' points to
99 * a buffer to output the data to.
100 *
101 * The new i2d has one additional feature. If the output
102 * buffer is NULL (i.e. *out == NULL) then a buffer is
103 * allocated and populated with the encoding.
104 */
105
106static int
107asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it,
108 int flags)
109{
110 unsigned char *p, *buf;
111 int len;
112
113 if (out == NULL((void *)0) || *out != NULL((void *)0))
2
Assuming 'out' is equal to NULL
114 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
3
Calling 'ASN1_item_ex_i2d'
115
116 if ((len = ASN1_item_ex_i2d(&val, NULL((void *)0), it, -1, flags)) <= 0)
117 return len;
118
119 if ((buf = calloc(1, len)) == NULL((void *)0))
120 return -1;
121
122 p = buf;
123 if (ASN1_item_ex_i2d(&val, &p, it, -1, flags) != len) {
124 freezero(buf, len);
125 ASN1error(ASN1_R_LENGTH_ERROR)ERR_put_error(13,(0xfff),(136),"/usr/src/lib/libcrypto/asn1/tasn_enc.c"
,125)
;
126 return -1;
127 }
128
129 *out = buf;
130
131 return len;
132}
133
134/* Encode an item, taking care of IMPLICIT tagging (if any).
135 * This function performs the normal item handling: it can be
136 * used in external types.
137 */
138
139int
140ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
141 int tag, int aclass)
142{
143 const ASN1_TEMPLATE *tt = NULL((void *)0);
144 int i, seqcontlen, seqlen, ndef = 1;
145 const ASN1_EXTERN_FUNCS *ef;
146 const ASN1_AUX *aux = it->funcs;
147 ASN1_aux_cb *asn1_cb = NULL((void *)0);
148
149 if ((it->itype != ASN1_ITYPE_PRIMITIVE0x0) && !*pval)
4
Assuming field 'itype' is not equal to ASN1_ITYPE_PRIMITIVE
5
Assuming the condition is false
150 return 0;
151
152 if (aux && aux->asn1_cb)
6
Assuming 'aux' is null
153 asn1_cb = aux->asn1_cb;
154
155 switch (it->itype) {
7
Control jumps to 'case 4:' at line 202
156
157 case ASN1_ITYPE_PRIMITIVE0x0:
158 if (it->templates)
159 return asn1_template_ex_i2d(pval, out, it->templates,
160 tag, aclass);
161 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
162 break;
163
164 case ASN1_ITYPE_MSTRING0x5:
165 /*
166 * It never makes sense for multi-strings to have implicit
167 * tagging, so if tag != -1, then this looks like an error in
168 * the template.
169 */
170 if (tag != -1) {
171 ASN1error(ASN1_R_BAD_TEMPLATE)ERR_put_error(13,(0xfff),(230),"/usr/src/lib/libcrypto/asn1/tasn_enc.c"
,171)
;
172 return 0;
173 }
174 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass);
175
176 case ASN1_ITYPE_CHOICE0x2:
177 /*
178 * It never makes sense for CHOICE types to have implicit
179 * tagging, so if tag != -1, then this looks like an error in
180 * the template.
181 */
182 if (tag != -1) {
183 ASN1error(ASN1_R_BAD_TEMPLATE)ERR_put_error(13,(0xfff),(230),"/usr/src/lib/libcrypto/asn1/tasn_enc.c"
,183)
;
184 return 0;
185 }
186 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE6, pval, it, NULL((void *)0)))
187 return 0;
188 i = asn1_get_choice_selector(pval, it);
189 if ((i >= 0) && (i < it->tcount)) {
190 ASN1_VALUE **pchval;
191 const ASN1_TEMPLATE *chtt;
192 chtt = it->templates + i;
193 pchval = asn1_get_field_ptr(pval, chtt);
194 return asn1_template_ex_i2d(pchval, out, chtt,
195 -1, aclass);
196 }
197 /* Fixme: error condition if selector out of range */
198 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST7, pval, it, NULL((void *)0)))
199 return 0;
200 break;
201
202 case ASN1_ITYPE_EXTERN0x4:
203 /* If new style i2d it does all the work */
204 ef = it->funcs;
8
Null pointer value stored to 'ef'
205 return ef->asn1_ex_i2d(pval, out, it, tag, aclass);
9
Access to field 'asn1_ex_i2d' results in a dereference of a null pointer (loaded from variable 'ef')
206
207 case ASN1_ITYPE_NDEF_SEQUENCE0x6:
208 /* Use indefinite length constructed if requested */
209 if (aclass & ASN1_TFLG_NDEF(0x1<<11))
210 ndef = 2;
211 /* fall through */
212
213 case ASN1_ITYPE_SEQUENCE0x1:
214 i = asn1_enc_restore(&seqcontlen, out, pval, it);
215 /* An error occurred */
216 if (i < 0)
217 return 0;
218 /* We have a valid cached encoding... */
219 if (i > 0)
220 return seqcontlen;
221 /* Otherwise carry on */
222 seqcontlen = 0;
223 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
224 if (tag == -1) {
225 tag = V_ASN1_SEQUENCE16;
226 /* Retain any other flags in aclass */
227 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS(0x3<<6)) |
228 V_ASN1_UNIVERSAL0x00;
229 }
230 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE6, pval, it, NULL((void *)0)))
231 return 0;
232 /* First work out sequence content length */
233 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
234 const ASN1_TEMPLATE *seqtt;
235 ASN1_VALUE **pseqval;
236 seqtt = asn1_do_adb(pval, tt, 1);
237 if (!seqtt)
238 return 0;
239 pseqval = asn1_get_field_ptr(pval, seqtt);
240 /* FIXME: check for errors in enhanced version */
241 seqcontlen += asn1_template_ex_i2d(pseqval, NULL((void *)0), seqtt,
242 -1, aclass);
243 }
244
245 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
246 if (!out)
247 return seqlen;
248 /* Output SEQUENCE header */
249 ASN1_put_object(out, ndef, seqcontlen, tag, aclass);
250 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
251 const ASN1_TEMPLATE *seqtt;
252 ASN1_VALUE **pseqval;
253 seqtt = asn1_do_adb(pval, tt, 1);
254 if (!seqtt)
255 return 0;
256 pseqval = asn1_get_field_ptr(pval, seqtt);
257 /* FIXME: check for errors in enhanced version */
258 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass);
259 }
260 if (ndef == 2)
261 ASN1_put_eoc(out);
262 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST7, pval, it, NULL((void *)0)))
263 return 0;
264 return seqlen;
265
266 default:
267 return 0;
268
269 }
270 return 0;
271}
272LCRYPTO_ALIAS(ASN1_item_ex_i2d)asm("");
273
274static int
275asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
276 const ASN1_TEMPLATE *tt, int tag, int iclass)
277{
278 int i, ret, flags, ttag, tclass, ndef;
279 flags = tt->flags;
280 /* Work out tag and class to use: tagging may come
281 * either from the template or the arguments, not both
282 * because this would create ambiguity. Additionally
283 * the iclass argument may contain some additional flags
284 * which should be noted and passed down to other levels.
285 */
286 if (flags & ASN1_TFLG_TAG_MASK(0x3 << 3)) {
287 /* Error if argument and template tagging */
288 if (tag != -1)
289 /* FIXME: error code here */
290 return -1;
291 /* Get tagging from template */
292 ttag = tt->tag;
293 tclass = flags & ASN1_TFLG_TAG_CLASS(0x3<<6);
294 } else if (tag != -1) {
295 /* No template tagging, get from arguments */
296 ttag = tag;
297 tclass = iclass & ASN1_TFLG_TAG_CLASS(0x3<<6);
298 } else {
299 ttag = -1;
300 tclass = 0;
301 }
302 /*
303 * Remove any class mask from iflag.
304 */
305 iclass &= ~ASN1_TFLG_TAG_CLASS(0x3<<6);
306
307 /* At this point 'ttag' contains the outer tag to use,
308 * 'tclass' is the class and iclass is any flags passed
309 * to this function.
310 */
311
312 /* if template and arguments require ndef, use it */
313 if ((flags & ASN1_TFLG_NDEF(0x1<<11)) && (iclass & ASN1_TFLG_NDEF(0x1<<11)))
314 ndef = 2;
315 else
316 ndef = 1;
317
318 if (flags & ASN1_TFLG_SK_MASK(0x3 << 1)) {
319 /* SET OF, SEQUENCE OF */
320 STACK_OF(ASN1_VALUE)struct stack_st_ASN1_VALUE *sk = (STACK_OF(ASN1_VALUE)struct stack_st_ASN1_VALUE *)*pval;
321 int isset, sktag, skaclass;
322 int skcontlen, sklen;
323 ASN1_VALUE *skitem;
324
325 if (!*pval)
326 return 0;
327
328 if (flags & ASN1_TFLG_SET_OF(0x1 << 1)) {
329 isset = 1;
330 /* 2 means we reorder */
331 if (flags & ASN1_TFLG_SEQUENCE_OF(0x2 << 1))
332 isset = 2;
333 } else
334 isset = 0;
335
336 /* Work out inner tag value: if EXPLICIT
337 * or no tagging use underlying type.
338 */
339 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG(0x2 << 3))) {
340 sktag = ttag;
341 skaclass = tclass;
342 } else {
343 skaclass = V_ASN1_UNIVERSAL0x00;
344 if (isset)
345 sktag = V_ASN1_SET17;
346 else
347 sktag = V_ASN1_SEQUENCE16;
348 }
349
350 /* Determine total length of items */
351 skcontlen = 0;
352 for (i = 0; i < sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
; i++) {
353 skitem = sk_ASN1_VALUE_value(sk, i)((ASN1_VALUE *)sk_value(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE
*)0)), (i)))
;
354 skcontlen += ASN1_item_ex_i2d(&skitem, NULL((void *)0),
355 tt->item, -1, iclass);
356 }
357 sklen = ASN1_object_size(ndef, skcontlen, sktag);
358 /* If EXPLICIT need length of surrounding tag */
359 if (flags & ASN1_TFLG_EXPTAG(0x2 << 3))
360 ret = ASN1_object_size(ndef, sklen, ttag);
361 else
362 ret = sklen;
363
364 if (!out)
365 return ret;
366
367 /* Now encode this lot... */
368 /* EXPLICIT tag */
369 if (flags & ASN1_TFLG_EXPTAG(0x2 << 3))
370 ASN1_put_object(out, ndef, sklen, ttag, tclass);
371 /* SET or SEQUENCE and IMPLICIT tag */
372 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
373 /* And the stuff itself */
374 asn1_set_seq_out(sk, out, skcontlen, tt->item,
375 isset, iclass);
376 if (ndef == 2) {
377 ASN1_put_eoc(out);
378 if (flags & ASN1_TFLG_EXPTAG(0x2 << 3))
379 ASN1_put_eoc(out);
380 }
381
382 return ret;
383 }
384
385 if (flags & ASN1_TFLG_EXPTAG(0x2 << 3)) {
386 /* EXPLICIT tagging */
387 /* Find length of tagged item */
388 i = ASN1_item_ex_i2d(pval, NULL((void *)0), tt->item,
389 -1, iclass);
390 if (!i)
391 return 0;
392 /* Find length of EXPLICIT tag */
393 ret = ASN1_object_size(ndef, i, ttag);
394 if (out) {
395 /* Output tag and item */
396 ASN1_put_object(out, ndef, i, ttag, tclass);
397 ASN1_item_ex_i2d(pval, out, tt->item,
398 -1, iclass);
399 if (ndef == 2)
400 ASN1_put_eoc(out);
401 }
402 return ret;
403 }
404
405 /* Either normal or IMPLICIT tagging: combine class and flags */
406 return ASN1_item_ex_i2d(pval, out, tt->item,
407 ttag, tclass | iclass);
408}
409
410/* Temporary structure used to hold DER encoding of items for SET OF */
411
412typedef struct {
413 unsigned char *data;
414 int length;
415 ASN1_VALUE *field;
416} DER_ENC;
417
418static int
419der_cmp(const void *a, const void *b)
420{
421 const DER_ENC *d1 = a, *d2 = b;
422 int cmplen, i;
423
424 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
425 i = memcmp(d1->data, d2->data, cmplen);
426 if (i)
427 return i;
428 return d1->length - d2->length;
429}
430
431/* Output the content octets of SET OF or SEQUENCE OF */
432
433static int
434asn1_set_seq_out(STACK_OF(ASN1_VALUE)struct stack_st_ASN1_VALUE *sk, unsigned char **out, int skcontlen,
435 const ASN1_ITEM *item, int do_sort, int iclass)
436{
437 int i;
438 ASN1_VALUE *skitem;
439 unsigned char *tmpdat = NULL((void *)0), *p = NULL((void *)0);
440 DER_ENC *derlst = NULL((void *)0), *tder;
441
442 if (do_sort) {
443 /* Don't need to sort less than 2 items */
444 if (sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
< 2)
445 do_sort = 0;
446 else {
447 derlst = reallocarray(NULL((void *)0), sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
,
448 sizeof(*derlst));
449 tmpdat = malloc(skcontlen);
450 if (!derlst || !tmpdat) {
451 free(derlst);
452 free(tmpdat);
453 return 0;
454 }
455 }
456 }
457 /* If not sorting just output each item */
458 if (!do_sort) {
459 for (i = 0; i < sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
; i++) {
460 skitem = sk_ASN1_VALUE_value(sk, i)((ASN1_VALUE *)sk_value(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE
*)0)), (i)))
;
461 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass);
462 }
463 return 1;
464 }
465 p = tmpdat;
466
467 /* Doing sort: build up a list of each member's DER encoding */
468 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
; i++, tder++) {
469 skitem = sk_ASN1_VALUE_value(sk, i)((ASN1_VALUE *)sk_value(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE
*)0)), (i)))
;
470 tder->data = p;
471 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass);
472 tder->field = skitem;
473 }
474
475 /* Now sort them */
476 qsort(derlst, sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
, sizeof(*derlst), der_cmp);
477 /* Output sorted DER encoding */
478 p = *out;
479 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
; i++, tder++) {
480 memcpy(p, tder->data, tder->length);
481 p += tder->length;
482 }
483 *out = p;
484 /* If do_sort is 2 then reorder the STACK */
485 if (do_sort == 2) {
486 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk)sk_num(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
))
; i++, tder++)
487 (void)sk_ASN1_VALUE_set(sk, i, tder->field)sk_set(((_STACK*) (1 ? (sk) : (struct stack_st_ASN1_VALUE*)0)
), (i), ((void*) (1 ? (tder->field) : (ASN1_VALUE*)0)))
;
488 }
489 free(derlst);
490 free(tmpdat);
491 return 1;
492}
493
494static int
495asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
496 const ASN1_ITEM *it, int tag, int aclass)
497{
498 int olen, len;
499 int utype;
500 int usetag;
501 int ndef = 0;
502
503 utype = it->utype;
504
505 /* Get length of content octets and maybe find
506 * out the underlying type.
507 */
508
509 olen = len = asn1_ex_i2c(pval, NULL((void *)0), &utype, it);
510
511 /* If SEQUENCE, SET or OTHER then header is
512 * included in pseudo content octets so don't
513 * include tag+length. We need to check here
514 * because the call to asn1_ex_i2c() could change
515 * utype.
516 */
517 if ((utype == V_ASN1_SEQUENCE16) || (utype == V_ASN1_SET17) ||
518 (utype == V_ASN1_OTHER-3))
519 usetag = 0;
520 else
521 usetag = 1;
522
523 /* -1 means omit type */
524 if (len == -1)
525 return 0;
526
527 /* -2 return is special meaning use ndef */
528 if (len == -2) {
529 ndef = 2;
530 len = 0;
531 }
532
533 /* Treat any other negative value as an error. */
534 if (len < 0)
535 return -1;
536
537 /* If not implicitly tagged get tag from underlying type */
538 if (tag == -1)
539 tag = utype;
540
541 /* Output tag+length followed by content octets */
542 if (out) {
543 if (usetag)
544 ASN1_put_object(out, ndef, len, tag, aclass);
545 if (asn1_ex_i2c(pval, *out, &utype, it) != olen)
546 return -1;
547 if (ndef)
548 ASN1_put_eoc(out);
549 else
550 *out += len;
551 }
552
553 if (usetag)
554 return ASN1_object_size(ndef, len, tag);
555 return len;
556}
557
558/* Produce content octets from a structure */
559
560static int
561asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
562 const ASN1_ITEM *it)
563{
564 ASN1_BOOLEAN *tbool = NULL((void *)0);
565 ASN1_STRING *strtmp;
566 ASN1_OBJECT *otmp;
567 int utype;
568 const unsigned char *cont;
569 unsigned char c;
570 int len;
571
572 if (it->funcs != NULL((void *)0)) {
573 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
574
575 if (pf->prim_i2c == NULL((void *)0))
576 return -1;
577 return pf->prim_i2c(pval, cout, putype, it);
578 }
579
580 /* Should type be omitted? */
581 if ((it->itype != ASN1_ITYPE_PRIMITIVE0x0) ||
582 (it->utype != V_ASN1_BOOLEAN1)) {
583 if (!*pval)
584 return -1;
585 }
586
587 if (it->itype == ASN1_ITYPE_MSTRING0x5) {
588 /* If MSTRING type set the underlying type */
589 strtmp = (ASN1_STRING *)*pval;
590 utype = strtmp->type;
591 *putype = utype;
592 } else if (it->utype == V_ASN1_ANY-4) {
593 /* If ANY set type and pointer to value */
594 ASN1_TYPE *typ;
595 typ = (ASN1_TYPE *)*pval;
596 utype = typ->type;
597 *putype = utype;
598 pval = &typ->value.asn1_value;
599 } else
600 utype = *putype;
601
602 switch (utype) {
603 case V_ASN1_OBJECT6:
604 otmp = (ASN1_OBJECT *)*pval;
605 cont = otmp->data;
606 len = otmp->length;
607 break;
608
609 case V_ASN1_NULL5:
610 cont = NULL((void *)0);
611 len = 0;
612 break;
613
614 case V_ASN1_BOOLEAN1:
615 tbool = (ASN1_BOOLEAN *)pval;
616 if (*tbool == -1)
617 return -1;
618 if (it->utype != V_ASN1_ANY-4) {
619 /* Default handling if value == size field then omit */
620 if (*tbool && (it->size > 0))
621 return -1;
622 if (!*tbool && !it->size)
623 return -1;
624 }
625 c = (unsigned char)*tbool;
626 cont = &c;
627 len = 1;
628 break;
629
630 case V_ASN1_BIT_STRING3:
631 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
632 cout ? &cout : NULL((void *)0));
633 break;
634
635 case V_ASN1_INTEGER2:
636 case V_ASN1_ENUMERATED10:
637 /* These are all have the same content format
638 * as ASN1_INTEGER
639 */
640 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,
641 cout ? &cout : NULL((void *)0));
642 break;
643
644 case V_ASN1_OCTET_STRING4:
645 case V_ASN1_NUMERICSTRING18:
646 case V_ASN1_PRINTABLESTRING19:
647 case V_ASN1_T61STRING20:
648 case V_ASN1_VIDEOTEXSTRING21:
649 case V_ASN1_IA5STRING22:
650 case V_ASN1_UTCTIME23:
651 case V_ASN1_GENERALIZEDTIME24:
652 case V_ASN1_GRAPHICSTRING25:
653 case V_ASN1_VISIBLESTRING26:
654 case V_ASN1_GENERALSTRING27:
655 case V_ASN1_UNIVERSALSTRING28:
656 case V_ASN1_BMPSTRING30:
657 case V_ASN1_UTF8STRING12:
658 case V_ASN1_SEQUENCE16:
659 case V_ASN1_SET17:
660 default:
661 /* All based on ASN1_STRING and handled the same */
662 strtmp = (ASN1_STRING *)*pval;
663 /* Special handling for NDEF */
664 if ((it->size == ASN1_TFLG_NDEF(0x1<<11)) &&
665 (strtmp->flags & ASN1_STRING_FLAG_NDEF0x010)) {
666 if (cout) {
667 strtmp->data = cout;
668 strtmp->length = 0;
669 }
670 /* Special return code */
671 return -2;
672 }
673 cont = strtmp->data;
674 len = strtmp->length;
675
676 break;
677
678 }
679 if (cout && len)
680 memcpy(cout, cont, len);
681 return len;
682}