summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_enc.c
diff options
context:
space:
mode:
authorjsing <>2014-04-18 12:15:48 +0000
committerjsing <>2014-04-18 12:15:48 +0000
commitc28759e489ff78d60b33248f7658689125bd0492 (patch)
treef92dece9fbc28772667b90178bfb5162b1cb1ad4 /src/lib/libcrypto/asn1/tasn_enc.c
parenta93a406b5997f92acc07a066c2bacb9194b5c737 (diff)
downloadopenbsd-c28759e489ff78d60b33248f7658689125bd0492.tar.gz
openbsd-c28759e489ff78d60b33248f7658689125bd0492.tar.bz2
openbsd-c28759e489ff78d60b33248f7658689125bd0492.zip
More KNF.
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_enc.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c159
1 files changed, 85 insertions, 74 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
index c9131cd9ca..f5fc8820f6 100644
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ b/src/lib/libcrypto/asn1/tasn_enc.c
@@ -10,7 +10,7 @@
10 * are met: 10 * are met:
11 * 11 *
12 * 1. Redistributions of source code must retain the above copyright 12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer. 13 * notice, this list of conditions and the following disclaimer.
14 * 14 *
15 * 2. Redistributions in binary form must reproduce the above copyright 15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in 16 * notice, this list of conditions and the following disclaimer in
@@ -56,7 +56,6 @@
56 * 56 *
57 */ 57 */
58 58
59
60#include <stddef.h> 59#include <stddef.h>
61#include <string.h> 60#include <string.h>
62#include "cryptlib.h" 61#include "cryptlib.h"
@@ -65,34 +64,32 @@
65#include <openssl/objects.h> 64#include <openssl/objects.h>
66 65
67static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, 66static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
68 const ASN1_ITEM *it, 67 const ASN1_ITEM *it, int tag, int aclass);
69 int tag, int aclass);
70static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, 68static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
71 int skcontlen, const ASN1_ITEM *item, 69 int skcontlen, const ASN1_ITEM *item, int do_sort, int iclass);
72 int do_sort, int iclass);
73static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 70static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
74 const ASN1_TEMPLATE *tt, 71 const ASN1_TEMPLATE *tt, int tag, int aclass);
75 int tag, int aclass);
76static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, 72static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
77 const ASN1_ITEM *it, int flags); 73 const ASN1_ITEM *it, int flags);
78 74
79/* Top level i2d equivalents: the 'ndef' variant instructs the encoder 75/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
80 * to use indefinite length constructed encoding, where appropriate 76 * to use indefinite length constructed encoding, where appropriate
81 */ 77 */
82 78
83int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, 79int
84 const ASN1_ITEM *it) 80ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
85{ 81{
86 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF); 82 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
87} 83}
88 84
89int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it) 85int
86ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
90{ 87{
91 return asn1_item_flags_i2d(val, out, it, 0); 88 return asn1_item_flags_i2d(val, out, it, 0);
92} 89}
93 90
94/* Encode an ASN1 item, this is use by the 91/* Encode an ASN1 item, this is use by the
95 * standard 'i2d' function. 'out' points to 92 * standard 'i2d' function. 'out' points to
96 * a buffer to output the data to. 93 * a buffer to output the data to.
97 * 94 *
98 * The new i2d has one additional feature. If the output 95 * The new i2d has one additional feature. If the output
@@ -100,8 +97,9 @@ int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
100 * allocated and populated with the encoding. 97 * allocated and populated with the encoding.
101 */ 98 */
102 99
103static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, 100static int
104 const ASN1_ITEM *it, int flags) 101asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it,
102 int flags)
105{ 103{
106 if (out && !*out) { 104 if (out && !*out) {
107 unsigned char *p, *buf; 105 unsigned char *p, *buf;
@@ -126,8 +124,9 @@ static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
126 * used in external types. 124 * used in external types.
127 */ 125 */
128 126
129int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 127int
130 const ASN1_ITEM *it, int tag, int aclass) 128ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it,
129 int tag, int aclass)
131{ 130{
132 const ASN1_TEMPLATE *tt = NULL; 131 const ASN1_TEMPLATE *tt = NULL;
133 unsigned char *p = NULL; 132 unsigned char *p = NULL;
@@ -141,14 +140,14 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
141 return 0; 140 return 0;
142 141
143 if (aux && aux->asn1_cb) 142 if (aux && aux->asn1_cb)
144 asn1_cb = aux->asn1_cb; 143 asn1_cb = aux->asn1_cb;
145 144
146 switch(it->itype) { 145 switch (it->itype) {
147 146
148 case ASN1_ITYPE_PRIMITIVE: 147 case ASN1_ITYPE_PRIMITIVE:
149 if (it->templates) 148 if (it->templates)
150 return asn1_template_ex_i2d(pval, out, it->templates, 149 return asn1_template_ex_i2d(pval, out, it->templates,
151 tag, aclass); 150 tag, aclass);
152 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); 151 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
153 break; 152 break;
154 153
@@ -157,7 +156,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
157 156
158 case ASN1_ITYPE_CHOICE: 157 case ASN1_ITYPE_CHOICE:
159 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) 158 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
160 return 0; 159 return 0;
161 i = asn1_get_choice_selector(pval, it); 160 i = asn1_get_choice_selector(pval, it);
162 if ((i >= 0) && (i < it->tcount)) { 161 if ((i >= 0) && (i < it->tcount)) {
163 ASN1_VALUE **pchval; 162 ASN1_VALUE **pchval;
@@ -165,11 +164,11 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
165 chtt = it->templates + i; 164 chtt = it->templates + i;
166 pchval = asn1_get_field_ptr(pval, chtt); 165 pchval = asn1_get_field_ptr(pval, chtt);
167 return asn1_template_ex_i2d(pchval, out, chtt, 166 return asn1_template_ex_i2d(pchval, out, chtt,
168 -1, aclass); 167 -1, aclass);
169 } 168 }
170 /* Fixme: error condition if selector out of range */ 169 /* Fixme: error condition if selector out of range */
171 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) 170 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
172 return 0; 171 return 0;
173 break; 172 break;
174 173
175 case ASN1_ITYPE_EXTERN: 174 case ASN1_ITYPE_EXTERN:
@@ -189,10 +188,11 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
189 if (out && (tag != -1)) 188 if (out && (tag != -1))
190 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED); 189 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
191 return i; 190 return i;
192 191
193 case ASN1_ITYPE_NDEF_SEQUENCE: 192 case ASN1_ITYPE_NDEF_SEQUENCE:
194 /* Use indefinite length constructed if requested */ 193 /* Use indefinite length constructed if requested */
195 if (aclass & ASN1_TFLG_NDEF) ndef = 2; 194 if (aclass & ASN1_TFLG_NDEF)
195 ndef = 2;
196 /* fall through */ 196 /* fall through */
197 197
198 case ASN1_ITYPE_SEQUENCE: 198 case ASN1_ITYPE_SEQUENCE:
@@ -209,11 +209,11 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
209 if (tag == -1) { 209 if (tag == -1) {
210 tag = V_ASN1_SEQUENCE; 210 tag = V_ASN1_SEQUENCE;
211 /* Retain any other flags in aclass */ 211 /* Retain any other flags in aclass */
212 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) 212 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) |
213 | V_ASN1_UNIVERSAL; 213 V_ASN1_UNIVERSAL;
214 } 214 }
215 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL)) 215 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it, NULL))
216 return 0; 216 return 0;
217 /* First work out sequence content length */ 217 /* First work out sequence content length */
218 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) { 218 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
219 const ASN1_TEMPLATE *seqtt; 219 const ASN1_TEMPLATE *seqtt;
@@ -224,7 +224,7 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
224 pseqval = asn1_get_field_ptr(pval, seqtt); 224 pseqval = asn1_get_field_ptr(pval, seqtt);
225 /* FIXME: check for errors in enhanced version */ 225 /* FIXME: check for errors in enhanced version */
226 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt, 226 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt,
227 -1, aclass); 227 -1, aclass);
228 } 228 }
229 229
230 seqlen = ASN1_object_size(ndef, seqcontlen, tag); 230 seqlen = ASN1_object_size(ndef, seqcontlen, tag);
@@ -245,24 +245,26 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
245 if (ndef == 2) 245 if (ndef == 2)
246 ASN1_put_eoc(out); 246 ASN1_put_eoc(out);
247 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL)) 247 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it, NULL))
248 return 0; 248 return 0;
249 return seqlen; 249 return seqlen;
250 250
251 default: 251 default:
252 return 0; 252 return 0;
253 253
254 } 254 }
255 return 0; 255 return 0;
256} 256}
257 257
258int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, 258int
259 const ASN1_TEMPLATE *tt) 259ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
260 const ASN1_TEMPLATE *tt)
260{ 261{
261 return asn1_template_ex_i2d(pval, out, tt, -1, 0); 262 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
262} 263}
263 264
264static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 265static int
265 const ASN1_TEMPLATE *tt, int tag, int iclass) 266asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
267 const ASN1_TEMPLATE *tt, int tag, int iclass)
266{ 268{
267 int i, ret, flags, ttag, tclass, ndef; 269 int i, ret, flags, ttag, tclass, ndef;
268 flags = tt->flags; 270 flags = tt->flags;
@@ -288,7 +290,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
288 ttag = -1; 290 ttag = -1;
289 tclass = 0; 291 tclass = 0;
290 } 292 }
291 /* 293 /*
292 * Remove any class mask from iflag. 294 * Remove any class mask from iflag.
293 */ 295 */
294 iclass &= ~ASN1_TFLG_TAG_CLASS; 296 iclass &= ~ASN1_TFLG_TAG_CLASS;
@@ -301,7 +303,8 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
301 /* if template and arguments require ndef, use it */ 303 /* if template and arguments require ndef, use it */
302 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF)) 304 if ((flags & ASN1_TFLG_NDEF) && (iclass & ASN1_TFLG_NDEF))
303 ndef = 2; 305 ndef = 2;
304 else ndef = 1; 306 else
307 ndef = 1;
305 308
306 if (flags & ASN1_TFLG_SK_MASK) { 309 if (flags & ASN1_TFLG_SK_MASK) {
307 /* SET OF, SEQUENCE OF */ 310 /* SET OF, SEQUENCE OF */
@@ -318,8 +321,8 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
318 /* 2 means we reorder */ 321 /* 2 means we reorder */
319 if (flags & ASN1_TFLG_SEQUENCE_OF) 322 if (flags & ASN1_TFLG_SEQUENCE_OF)
320 isset = 2; 323 isset = 2;
321 } 324 } else
322 else isset = 0; 325 isset = 0;
323 326
324 /* Work out inner tag value: if EXPLICIT 327 /* Work out inner tag value: if EXPLICIT
325 * or no tagging use underlying type. 328 * or no tagging use underlying type.
@@ -331,7 +334,8 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
331 skaclass = V_ASN1_UNIVERSAL; 334 skaclass = V_ASN1_UNIVERSAL;
332 if (isset) 335 if (isset)
333 sktag = V_ASN1_SET; 336 sktag = V_ASN1_SET;
334 else sktag = V_ASN1_SEQUENCE; 337 else
338 sktag = V_ASN1_SEQUENCE;
335 } 339 }
336 340
337 /* Determine total length of items */ 341 /* Determine total length of items */
@@ -339,14 +343,14 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
339 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) { 343 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
340 skitem = sk_ASN1_VALUE_value(sk, i); 344 skitem = sk_ASN1_VALUE_value(sk, i);
341 skcontlen += ASN1_item_ex_i2d(&skitem, NULL, 345 skcontlen += ASN1_item_ex_i2d(&skitem, NULL,
342 ASN1_ITEM_ptr(tt->item), 346 ASN1_ITEM_ptr(tt->item), -1, iclass);
343 -1, iclass);
344 } 347 }
345 sklen = ASN1_object_size(ndef, skcontlen, sktag); 348 sklen = ASN1_object_size(ndef, skcontlen, sktag);
346 /* If EXPLICIT need length of surrounding tag */ 349 /* If EXPLICIT need length of surrounding tag */
347 if (flags & ASN1_TFLG_EXPTAG) 350 if (flags & ASN1_TFLG_EXPTAG)
348 ret = ASN1_object_size(ndef, sklen, ttag); 351 ret = ASN1_object_size(ndef, sklen, ttag);
349 else ret = sklen; 352 else
353 ret = sklen;
350 354
351 if (!out) 355 if (!out)
352 return ret; 356 return ret;
@@ -359,7 +363,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
359 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); 363 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass);
360 /* And the stuff itself */ 364 /* And the stuff itself */
361 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), 365 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item),
362 isset, iclass); 366 isset, iclass);
363 if (ndef == 2) { 367 if (ndef == 2) {
364 ASN1_put_eoc(out); 368 ASN1_put_eoc(out);
365 if (flags & ASN1_TFLG_EXPTAG) 369 if (flags & ASN1_TFLG_EXPTAG)
@@ -373,7 +377,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
373 /* EXPLICIT tagging */ 377 /* EXPLICIT tagging */
374 /* Find length of tagged item */ 378 /* Find length of tagged item */
375 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), 379 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item),
376 -1, iclass); 380 -1, iclass);
377 if (!i) 381 if (!i)
378 return 0; 382 return 0;
379 /* Find length of EXPLICIT tag */ 383 /* Find length of EXPLICIT tag */
@@ -382,7 +386,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
382 /* Output tag and item */ 386 /* Output tag and item */
383 ASN1_put_object(out, ndef, i, ttag, tclass); 387 ASN1_put_object(out, ndef, i, ttag, tclass);
384 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), 388 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
385 -1, iclass); 389 -1, iclass);
386 if (ndef == 2) 390 if (ndef == 2)
387 ASN1_put_eoc(out); 391 ASN1_put_eoc(out);
388 } 392 }
@@ -391,8 +395,7 @@ static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
391 395
392 /* Either normal or IMPLICIT tagging: combine class and flags */ 396 /* Either normal or IMPLICIT tagging: combine class and flags */
393 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), 397 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item),
394 ttag, tclass | iclass); 398 ttag, tclass | iclass);
395
396} 399}
397 400
398/* Temporary structure used to hold DER encoding of items for SET OF */ 401/* Temporary structure used to hold DER encoding of items for SET OF */
@@ -403,10 +406,12 @@ typedef struct {
403 ASN1_VALUE *field; 406 ASN1_VALUE *field;
404} DER_ENC; 407} DER_ENC;
405 408
406static int der_cmp(const void *a, const void *b) 409static int
410der_cmp(const void *a, const void *b)
407{ 411{
408 const DER_ENC *d1 = a, *d2 = b; 412 const DER_ENC *d1 = a, *d2 = b;
409 int cmplen, i; 413 int cmplen, i;
414
410 cmplen = (d1->length < d2->length) ? d1->length : d2->length; 415 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
411 i = memcmp(d1->data, d2->data, cmplen); 416 i = memcmp(d1->data, d2->data, cmplen);
412 if (i) 417 if (i)
@@ -416,22 +421,22 @@ static int der_cmp(const void *a, const void *b)
416 421
417/* Output the content octets of SET OF or SEQUENCE OF */ 422/* Output the content octets of SET OF or SEQUENCE OF */
418 423
419static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, 424static int
420 int skcontlen, const ASN1_ITEM *item, 425asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, int skcontlen,
421 int do_sort, int iclass) 426 const ASN1_ITEM *item, int do_sort, int iclass)
422{ 427{
423 int i; 428 int i;
424 ASN1_VALUE *skitem; 429 ASN1_VALUE *skitem;
425 unsigned char *tmpdat = NULL, *p = NULL; 430 unsigned char *tmpdat = NULL, *p = NULL;
426 DER_ENC *derlst = NULL, *tder; 431 DER_ENC *derlst = NULL, *tder;
427 if (do_sort) 432
428 { 433 if (do_sort) {
429 /* Don't need to sort less than 2 items */ 434 /* Don't need to sort less than 2 items */
430 if (sk_ASN1_VALUE_num(sk) < 2) 435 if (sk_ASN1_VALUE_num(sk) < 2)
431 do_sort = 0; 436 do_sort = 0;
432 else { 437 else {
433 derlst = malloc(sk_ASN1_VALUE_num(sk) 438 derlst = malloc(sk_ASN1_VALUE_num(sk) *
434 * sizeof(*derlst)); 439 sizeof(*derlst));
435 tmpdat = malloc(skcontlen); 440 tmpdat = malloc(skcontlen);
436 if (!derlst || !tmpdat) { 441 if (!derlst || !tmpdat) {
437 free(derlst); 442 free(derlst);
@@ -460,7 +465,7 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
460 465
461 /* Now sort them */ 466 /* Now sort them */
462 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); 467 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
463 /* Output sorted DER encoding */ 468 /* Output sorted DER encoding */
464 p = *out; 469 p = *out;
465 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) { 470 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
466 memcpy(p, tder->data, tder->length); 471 memcpy(p, tder->data, tder->length);
@@ -477,8 +482,9 @@ static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
477 return 1; 482 return 1;
478} 483}
479 484
480static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, 485static int
481 const ASN1_ITEM *it, int tag, int aclass) 486asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
487 const ASN1_ITEM *it, int tag, int aclass)
482{ 488{
483 int len; 489 int len;
484 int utype; 490 int utype;
@@ -500,12 +506,12 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
500 * utype. 506 * utype.
501 */ 507 */
502 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || 508 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
503 (utype == V_ASN1_OTHER)) 509 (utype == V_ASN1_OTHER))
504 usetag = 0; 510 usetag = 0;
505 else usetag = 1; 511 else
512 usetag = 1;
506 513
507 /* -1 means omit type */ 514 /* -1 means omit type */
508
509 if (len == -1) 515 if (len == -1)
510 return 0; 516 return 0;
511 517
@@ -516,7 +522,8 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
516 } 522 }
517 523
518 /* If not implicitly tagged get tag from underlying type */ 524 /* If not implicitly tagged get tag from underlying type */
519 if (tag == -1) tag = utype; 525 if (tag == -1)
526 tag = utype;
520 527
521 /* Output tag+length followed by content octets */ 528 /* Output tag+length followed by content octets */
522 if (out) { 529 if (out) {
@@ -536,8 +543,9 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
536 543
537/* Produce content octets from a structure */ 544/* Produce content octets from a structure */
538 545
539int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, 546int
540 const ASN1_ITEM *it) 547asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
548 const ASN1_ITEM *it)
541{ 549{
542 ASN1_BOOLEAN *tbool = NULL; 550 ASN1_BOOLEAN *tbool = NULL;
543 ASN1_STRING *strtmp; 551 ASN1_STRING *strtmp;
@@ -547,13 +555,16 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
547 unsigned char c; 555 unsigned char c;
548 int len; 556 int len;
549 const ASN1_PRIMITIVE_FUNCS *pf; 557 const ASN1_PRIMITIVE_FUNCS *pf;
558
550 pf = it->funcs; 559 pf = it->funcs;
551 if (pf && pf->prim_i2c) 560 if (pf && pf->prim_i2c)
552 return pf->prim_i2c(pval, cout, putype, it); 561 return pf->prim_i2c(pval, cout, putype, it);
553 562
554 /* Should type be omitted? */ 563 /* Should type be omitted? */
555 if ((it->itype != ASN1_ITYPE_PRIMITIVE) || (it->utype != V_ASN1_BOOLEAN)) { 564 if ((it->itype != ASN1_ITYPE_PRIMITIVE) ||
556 if (!*pval) return -1; 565 (it->utype != V_ASN1_BOOLEAN)) {
566 if (!*pval)
567 return -1;
557 } 568 }
558 569
559 if (it->itype == ASN1_ITYPE_MSTRING) { 570 if (it->itype == ASN1_ITYPE_MSTRING) {
@@ -568,10 +579,10 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
568 utype = typ->type; 579 utype = typ->type;
569 *putype = utype; 580 *putype = utype;
570 pval = &typ->value.asn1_value; 581 pval = &typ->value.asn1_value;
571 } 582 } else
572 else utype = *putype; 583 utype = *putype;
573 584
574 switch(utype) { 585 switch (utype) {
575 case V_ASN1_OBJECT: 586 case V_ASN1_OBJECT:
576 otmp = (ASN1_OBJECT *)*pval; 587 otmp = (ASN1_OBJECT *)*pval;
577 cont = otmp->data; 588 cont = otmp->data;
@@ -601,7 +612,7 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
601 612
602 case V_ASN1_BIT_STRING: 613 case V_ASN1_BIT_STRING:
603 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, 614 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval,
604 cout ? &cout : NULL); 615 cout ? &cout : NULL);
605 break; 616 break;
606 617
607 case V_ASN1_INTEGER: 618 case V_ASN1_INTEGER:
@@ -612,7 +623,7 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
612 * as ASN1_INTEGER 623 * as ASN1_INTEGER
613 */ 624 */
614 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, 625 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval,
615 cout ? &cout : NULL); 626 cout ? &cout : NULL);
616 break; 627 break;
617 628
618 case V_ASN1_OCTET_STRING: 629 case V_ASN1_OCTET_STRING:
@@ -635,8 +646,8 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
635 /* All based on ASN1_STRING and handled the same */ 646 /* All based on ASN1_STRING and handled the same */
636 strtmp = (ASN1_STRING *)*pval; 647 strtmp = (ASN1_STRING *)*pval;
637 /* Special handling for NDEF */ 648 /* Special handling for NDEF */
638 if ((it->size == ASN1_TFLG_NDEF) 649 if ((it->size == ASN1_TFLG_NDEF) &&
639 && (strtmp->flags & ASN1_STRING_FLAG_NDEF)) { 650 (strtmp->flags & ASN1_STRING_FLAG_NDEF)) {
640 if (cout) { 651 if (cout) {
641 strtmp->data = cout; 652 strtmp->data = cout;
642 strtmp->length = 0; 653 strtmp->length = 0;