summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/asn1/tasn_enc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/asn1/tasn_enc.c')
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c514
1 files changed, 162 insertions, 352 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_enc.c b/src/lib/libcrypto/asn1/tasn_enc.c
index be19b36acd..c675c3c832 100644
--- a/src/lib/libcrypto/asn1/tasn_enc.c
+++ b/src/lib/libcrypto/asn1/tasn_enc.c
@@ -3,7 +3,7 @@
3 * project 2000. 3 * project 2000.
4 */ 4 */
5/* ==================================================================== 5/* ====================================================================
6 * Copyright (c) 2000-2004 The OpenSSL Project. All rights reserved. 6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 * 7 *
8 * Redistribution and use in source and binary forms, with or without 8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions 9 * modification, are permitted provided that the following conditions
@@ -59,119 +59,88 @@
59 59
60#include <stddef.h> 60#include <stddef.h>
61#include <string.h> 61#include <string.h>
62#include "cryptlib.h"
63#include <openssl/asn1.h> 62#include <openssl/asn1.h>
64#include <openssl/asn1t.h> 63#include <openssl/asn1t.h>
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, const ASN1_ITEM *it, int tag, int aclass);
68 const ASN1_ITEM *it, 67static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *seq, unsigned char **out, int skcontlen, const ASN1_ITEM *item, int isset);
69 int tag, int aclass);
70static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out,
71 int skcontlen, const ASN1_ITEM *item,
72 int do_sort, int iclass);
73static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
74 const ASN1_TEMPLATE *tt,
75 int tag, int aclass);
76static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out,
77 const ASN1_ITEM *it, int flags);
78
79/* Top level i2d equivalents: the 'ndef' variant instructs the encoder
80 * to use indefinite length constructed encoding, where appropriate
81 */
82
83int ASN1_item_ndef_i2d(ASN1_VALUE *val, unsigned char **out,
84 const ASN1_ITEM *it)
85 {
86 return asn1_item_flags_i2d(val, out, it, ASN1_TFLG_NDEF);
87 }
88
89int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
90 {
91 return asn1_item_flags_i2d(val, out, it, 0);
92 }
93 68
94/* Encode an ASN1 item, this is use by the 69/* Encode an ASN1 item, this is compatible with the
95 * standard 'i2d' function. 'out' points to 70 * standard 'i2d' function. 'out' points to
96 * a buffer to output the data to. 71 * a buffer to output the data to, in future we will
72 * have more advanced versions that can output data
73 * a piece at a time and this will simply be a special
74 * case.
97 * 75 *
98 * The new i2d has one additional feature. If the output 76 * The new i2d has one additional feature. If the output
99 * buffer is NULL (i.e. *out == NULL) then a buffer is 77 * buffer is NULL (i.e. *out == NULL) then a buffer is
100 * allocated and populated with the encoding. 78 * allocated and populated with the encoding.
101 */ 79 */
102 80
103static int asn1_item_flags_i2d(ASN1_VALUE *val, unsigned char **out, 81
104 const ASN1_ITEM *it, int flags) 82int ASN1_item_i2d(ASN1_VALUE *val, unsigned char **out, const ASN1_ITEM *it)
105 { 83{
106 if (out && !*out) 84 if(out && !*out) {
107 {
108 unsigned char *p, *buf; 85 unsigned char *p, *buf;
109 int len; 86 int len;
110 len = ASN1_item_ex_i2d(&val, NULL, it, -1, flags); 87 len = ASN1_item_ex_i2d(&val, NULL, it, -1, 0);
111 if (len <= 0) 88 if(len <= 0) return len;
112 return len;
113 buf = OPENSSL_malloc(len); 89 buf = OPENSSL_malloc(len);
114 if (!buf) 90 if(!buf) return -1;
115 return -1;
116 p = buf; 91 p = buf;
117 ASN1_item_ex_i2d(&val, &p, it, -1, flags); 92 ASN1_item_ex_i2d(&val, &p, it, -1, 0);
118 *out = buf; 93 *out = buf;
119 return len; 94 return len;
120 }
121
122 return ASN1_item_ex_i2d(&val, out, it, -1, flags);
123 } 95 }
96
97 return ASN1_item_ex_i2d(&val, out, it, -1, 0);
98}
124 99
125/* Encode an item, taking care of IMPLICIT tagging (if any). 100/* Encode an item, taking care of IMPLICIT tagging (if any).
126 * This function performs the normal item handling: it can be 101 * This function performs the normal item handling: it can be
127 * used in external types. 102 * used in external types.
128 */ 103 */
129 104
130int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 105int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
131 const ASN1_ITEM *it, int tag, int aclass) 106{
132 {
133 const ASN1_TEMPLATE *tt = NULL; 107 const ASN1_TEMPLATE *tt = NULL;
134 unsigned char *p = NULL; 108 unsigned char *p = NULL;
135 int i, seqcontlen, seqlen, ndef = 1; 109 int i, seqcontlen, seqlen;
110 ASN1_STRING *strtmp;
136 const ASN1_COMPAT_FUNCS *cf; 111 const ASN1_COMPAT_FUNCS *cf;
137 const ASN1_EXTERN_FUNCS *ef; 112 const ASN1_EXTERN_FUNCS *ef;
138 const ASN1_AUX *aux = it->funcs; 113 const ASN1_AUX *aux = it->funcs;
139 ASN1_aux_cb *asn1_cb = 0; 114 ASN1_aux_cb *asn1_cb;
140 115 if((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) return 0;
141 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) 116 if(aux && aux->asn1_cb) asn1_cb = aux->asn1_cb;
142 return 0; 117 else asn1_cb = 0;
143 118
144 if (aux && aux->asn1_cb) 119 switch(it->itype) {
145 asn1_cb = aux->asn1_cb;
146
147 switch(it->itype)
148 {
149 120
150 case ASN1_ITYPE_PRIMITIVE: 121 case ASN1_ITYPE_PRIMITIVE:
151 if (it->templates) 122 if(it->templates)
152 return asn1_template_ex_i2d(pval, out, it->templates, 123 return ASN1_template_i2d(pval, out, it->templates);
153 tag, aclass);
154 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass); 124 return asn1_i2d_ex_primitive(pval, out, it, tag, aclass);
155 break; 125 break;
156 126
157 case ASN1_ITYPE_MSTRING: 127 case ASN1_ITYPE_MSTRING:
158 return asn1_i2d_ex_primitive(pval, out, it, -1, aclass); 128 strtmp = (ASN1_STRING *)*pval;
129 return asn1_i2d_ex_primitive(pval, out, it, -1, 0);
159 130
160 case ASN1_ITYPE_CHOICE: 131 case ASN1_ITYPE_CHOICE:
161 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it)) 132 if(asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
162 return 0; 133 return 0;
163 i = asn1_get_choice_selector(pval, it); 134 i = asn1_get_choice_selector(pval, it);
164 if ((i >= 0) && (i < it->tcount)) 135 if((i >= 0) && (i < it->tcount)) {
165 {
166 ASN1_VALUE **pchval; 136 ASN1_VALUE **pchval;
167 const ASN1_TEMPLATE *chtt; 137 const ASN1_TEMPLATE *chtt;
168 chtt = it->templates + i; 138 chtt = it->templates + i;
169 pchval = asn1_get_field_ptr(pval, chtt); 139 pchval = asn1_get_field_ptr(pval, chtt);
170 return asn1_template_ex_i2d(pchval, out, chtt, 140 return ASN1_template_i2d(pchval, out, chtt);
171 -1, aclass); 141 }
172 }
173 /* Fixme: error condition if selector out of range */ 142 /* Fixme: error condition if selector out of range */
174 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it)) 143 if(asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
175 return 0; 144 return 0;
176 break; 145 break;
177 146
@@ -183,236 +152,136 @@ int ASN1_item_ex_i2d(ASN1_VALUE **pval, unsigned char **out,
183 case ASN1_ITYPE_COMPAT: 152 case ASN1_ITYPE_COMPAT:
184 /* old style hackery... */ 153 /* old style hackery... */
185 cf = it->funcs; 154 cf = it->funcs;
186 if (out) 155 if(out) p = *out;
187 p = *out;
188 i = cf->asn1_i2d(*pval, out); 156 i = cf->asn1_i2d(*pval, out);
189 /* Fixup for IMPLICIT tag: note this messes up for tags > 30, 157 /* Fixup for IMPLICIT tag: note this messes up for tags > 30,
190 * but so did the old code. Tags > 30 are very rare anyway. 158 * but so did the old code. Tags > 30 are very rare anyway.
191 */ 159 */
192 if (out && (tag != -1)) 160 if(out && (tag != -1))
193 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED); 161 *p = aclass | tag | (*p & V_ASN1_CONSTRUCTED);
194 return i; 162 return i;
195 163
196 case ASN1_ITYPE_NDEF_SEQUENCE:
197 /* Use indefinite length constructed if requested */
198 if (aclass & ASN1_TFLG_NDEF) ndef = 2;
199 /* fall through */
200
201 case ASN1_ITYPE_SEQUENCE: 164 case ASN1_ITYPE_SEQUENCE:
202 i = asn1_enc_restore(&seqcontlen, out, pval, it); 165 i = asn1_enc_restore(&seqcontlen, out, pval, it);
203 /* An error occurred */ 166 /* An error occurred */
204 if (i < 0) 167 if(i < 0) return 0;
205 return 0;
206 /* We have a valid cached encoding... */ 168 /* We have a valid cached encoding... */
207 if (i > 0) 169 if(i > 0) return seqcontlen;
208 return seqcontlen;
209 /* Otherwise carry on */ 170 /* Otherwise carry on */
210 seqcontlen = 0; 171 seqcontlen = 0;
211 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */ 172 /* If no IMPLICIT tagging set to SEQUENCE, UNIVERSAL */
212 if (tag == -1) 173 if(tag == -1) {
213 {
214 tag = V_ASN1_SEQUENCE; 174 tag = V_ASN1_SEQUENCE;
215 /* Retain any other flags in aclass */ 175 aclass = V_ASN1_UNIVERSAL;
216 aclass = (aclass & ~ASN1_TFLG_TAG_CLASS) 176 }
217 | V_ASN1_UNIVERSAL; 177 if(asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
218 }
219 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_PRE, pval, it))
220 return 0; 178 return 0;
221 /* First work out sequence content length */ 179 /* First work out sequence content length */
222 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) 180 for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
223 {
224 const ASN1_TEMPLATE *seqtt; 181 const ASN1_TEMPLATE *seqtt;
225 ASN1_VALUE **pseqval; 182 ASN1_VALUE **pseqval;
226 seqtt = asn1_do_adb(pval, tt, 1); 183 seqtt = asn1_do_adb(pval, tt, 1);
227 if (!seqtt) 184 if(!seqtt) return 0;
228 return 0;
229 pseqval = asn1_get_field_ptr(pval, seqtt); 185 pseqval = asn1_get_field_ptr(pval, seqtt);
230 /* FIXME: check for errors in enhanced version */ 186 /* FIXME: check for errors in enhanced version */
231 seqcontlen += asn1_template_ex_i2d(pseqval, NULL, seqtt, 187 /* FIXME: special handling of indefinite length encoding */
232 -1, aclass); 188 seqcontlen += ASN1_template_i2d(pseqval, NULL, seqtt);
233 } 189 }
234 190 seqlen = ASN1_object_size(1, seqcontlen, tag);
235 seqlen = ASN1_object_size(ndef, seqcontlen, tag); 191 if(!out) return seqlen;
236 if (!out)
237 return seqlen;
238 /* Output SEQUENCE header */ 192 /* Output SEQUENCE header */
239 ASN1_put_object(out, ndef, seqcontlen, tag, aclass); 193 ASN1_put_object(out, 1, seqcontlen, tag, aclass);
240 for (i = 0, tt = it->templates; i < it->tcount; tt++, i++) 194 for(i = 0, tt = it->templates; i < it->tcount; tt++, i++) {
241 {
242 const ASN1_TEMPLATE *seqtt; 195 const ASN1_TEMPLATE *seqtt;
243 ASN1_VALUE **pseqval; 196 ASN1_VALUE **pseqval;
244 seqtt = asn1_do_adb(pval, tt, 1); 197 seqtt = asn1_do_adb(pval, tt, 1);
245 if (!seqtt) 198 if(!seqtt) return 0;
246 return 0;
247 pseqval = asn1_get_field_ptr(pval, seqtt); 199 pseqval = asn1_get_field_ptr(pval, seqtt);
248 /* FIXME: check for errors in enhanced version */ 200 /* FIXME: check for errors in enhanced version */
249 asn1_template_ex_i2d(pseqval, out, seqtt, -1, aclass); 201 ASN1_template_i2d(pseqval, out, seqtt);
250 } 202 }
251 if (ndef == 2) 203 if(asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
252 ASN1_put_eoc(out);
253 if (asn1_cb && !asn1_cb(ASN1_OP_I2D_POST, pval, it))
254 return 0; 204 return 0;
255 return seqlen; 205 return seqlen;
256 206
257 default: 207 default:
258 return 0; 208 return 0;
259
260 }
261 return 0;
262 }
263
264int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out,
265 const ASN1_TEMPLATE *tt)
266 {
267 return asn1_template_ex_i2d(pval, out, tt, -1, 0);
268 } 209 }
210 return 0;
211}
269 212
270static int asn1_template_ex_i2d(ASN1_VALUE **pval, unsigned char **out, 213int ASN1_template_i2d(ASN1_VALUE **pval, unsigned char **out, const ASN1_TEMPLATE *tt)
271 const ASN1_TEMPLATE *tt, int tag, int iclass) 214{
272 { 215 int i, ret, flags, aclass;
273 int i, ret, flags, ttag, tclass, ndef;
274 flags = tt->flags; 216 flags = tt->flags;
275 /* Work out tag and class to use: tagging may come 217 aclass = flags & ASN1_TFLG_TAG_CLASS;
276 * either from the template or the arguments, not both 218 if(flags & ASN1_TFLG_SK_MASK) {
277 * because this would create ambiguity. Additionally
278 * the iclass argument may contain some additional flags
279 * which should be noted and passed down to other levels.
280 */
281 if (flags & ASN1_TFLG_TAG_MASK)
282 {
283 /* Error if argument and template tagging */
284 if (tag != -1)
285 /* FIXME: error code here */
286 return -1;
287 /* Get tagging from template */
288 ttag = tt->tag;
289 tclass = flags & ASN1_TFLG_TAG_CLASS;
290 }
291 else if (tag != -1)
292 {
293 /* No template tagging, get from arguments */
294 ttag = tag;
295 tclass = iclass & ASN1_TFLG_TAG_CLASS;
296 }
297 else
298 {
299 ttag = -1;
300 tclass = 0;
301 }
302 /*
303 * Remove any class mask from iflag.
304 */
305 iclass &= ~ASN1_TFLG_TAG_CLASS;
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) && (iclass & ASN1_TFLG_NDEF))
314 ndef = 2;
315 else ndef = 1;
316
317 if (flags & ASN1_TFLG_SK_MASK)
318 {
319 /* SET OF, SEQUENCE OF */ 219 /* SET OF, SEQUENCE OF */
320 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval; 220 STACK_OF(ASN1_VALUE) *sk = (STACK_OF(ASN1_VALUE) *)*pval;
321 int isset, sktag, skaclass; 221 int isset, sktag, skaclass;
322 int skcontlen, sklen; 222 int skcontlen, sklen;
323 ASN1_VALUE *skitem; 223 ASN1_VALUE *skitem;
324 224 if(!*pval) return 0;
325 if (!*pval) 225 if(flags & ASN1_TFLG_SET_OF) {
326 return 0;
327
328 if (flags & ASN1_TFLG_SET_OF)
329 {
330 isset = 1; 226 isset = 1;
331 /* 2 means we reorder */ 227 /* 2 means we reorder */
332 if (flags & ASN1_TFLG_SEQUENCE_OF) 228 if(flags & ASN1_TFLG_SEQUENCE_OF) isset = 2;
333 isset = 2; 229 } else isset = 0;
334 } 230 /* First work out inner tag value */
335 else isset = 0; 231 if(flags & ASN1_TFLG_IMPTAG) {
336 232 sktag = tt->tag;
337 /* Work out inner tag value: if EXPLICIT 233 skaclass = aclass;
338 * or no tagging use underlying type. 234 } else {
339 */
340 if ((ttag != -1) && !(flags & ASN1_TFLG_EXPTAG))
341 {
342 sktag = ttag;
343 skaclass = tclass;
344 }
345 else
346 {
347 skaclass = V_ASN1_UNIVERSAL; 235 skaclass = V_ASN1_UNIVERSAL;
348 if (isset) 236 if(isset) sktag = V_ASN1_SET;
349 sktag = V_ASN1_SET;
350 else sktag = V_ASN1_SEQUENCE; 237 else sktag = V_ASN1_SEQUENCE;
351 } 238 }
352 239 /* Now work out length of items */
353 /* Determine total length of items */
354 skcontlen = 0; 240 skcontlen = 0;
355 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++) 241 for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
356 {
357 skitem = sk_ASN1_VALUE_value(sk, i); 242 skitem = sk_ASN1_VALUE_value(sk, i);
358 skcontlen += ASN1_item_ex_i2d(&skitem, NULL, 243 skcontlen += ASN1_item_ex_i2d(&skitem, NULL, ASN1_ITEM_ptr(tt->item), -1, 0);
359 ASN1_ITEM_ptr(tt->item), 244 }
360 -1, iclass); 245 sklen = ASN1_object_size(1, skcontlen, sktag);
361 }
362 sklen = ASN1_object_size(ndef, skcontlen, sktag);
363 /* If EXPLICIT need length of surrounding tag */ 246 /* If EXPLICIT need length of surrounding tag */
364 if (flags & ASN1_TFLG_EXPTAG) 247 if(flags & ASN1_TFLG_EXPTAG)
365 ret = ASN1_object_size(ndef, sklen, ttag); 248 ret = ASN1_object_size(1, sklen, tt->tag);
366 else ret = sklen; 249 else ret = sklen;
367 250
368 if (!out) 251 if(!out) return ret;
369 return ret;
370 252
371 /* Now encode this lot... */ 253 /* Now encode this lot... */
372 /* EXPLICIT tag */ 254 /* EXPLICIT tag */
373 if (flags & ASN1_TFLG_EXPTAG) 255 if(flags & ASN1_TFLG_EXPTAG)
374 ASN1_put_object(out, ndef, sklen, ttag, tclass); 256 ASN1_put_object(out, 1, sklen, tt->tag, aclass);
375 /* SET or SEQUENCE and IMPLICIT tag */ 257 /* SET or SEQUENCE and IMPLICIT tag */
376 ASN1_put_object(out, ndef, skcontlen, sktag, skaclass); 258 ASN1_put_object(out, 1, skcontlen, sktag, skaclass);
377 /* And the stuff itself */ 259 /* And finally the stuff itself */
378 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), 260 asn1_set_seq_out(sk, out, skcontlen, ASN1_ITEM_ptr(tt->item), isset);
379 isset, iclass);
380 if (ndef == 2)
381 {
382 ASN1_put_eoc(out);
383 if (flags & ASN1_TFLG_EXPTAG)
384 ASN1_put_eoc(out);
385 }
386 261
387 return ret; 262 return ret;
388 } 263 }
389 264
390 if (flags & ASN1_TFLG_EXPTAG) 265 if(flags & ASN1_TFLG_EXPTAG) {
391 {
392 /* EXPLICIT tagging */ 266 /* EXPLICIT tagging */
393 /* Find length of tagged item */ 267 /* Find length of tagged item */
394 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), 268 i = ASN1_item_ex_i2d(pval, NULL, ASN1_ITEM_ptr(tt->item), -1, 0);
395 -1, iclass); 269 if(!i) return 0;
396 if (!i)
397 return 0;
398 /* Find length of EXPLICIT tag */ 270 /* Find length of EXPLICIT tag */
399 ret = ASN1_object_size(ndef, i, ttag); 271 ret = ASN1_object_size(1, i, tt->tag);
400 if (out) 272 if(out) {
401 {
402 /* Output tag and item */ 273 /* Output tag and item */
403 ASN1_put_object(out, ndef, i, ttag, tclass); 274 ASN1_put_object(out, 1, i, tt->tag, aclass);
404 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), 275 ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0);
405 -1, iclass);
406 if (ndef == 2)
407 ASN1_put_eoc(out);
408 }
409 return ret;
410 } 276 }
411 277 return ret;
412 /* Either normal or IMPLICIT tagging: combine class and flags */ 278 }
413 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), 279 if(flags & ASN1_TFLG_IMPTAG) {
414 ttag, tclass | iclass); 280 /* IMPLICIT tagging */
415 281 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), tt->tag, aclass);
282 }
283 /* Nothing special: treat as normal */
284 return ASN1_item_ex_i2d(pval, out, ASN1_ITEM_ptr(tt->item), -1, 0);
416} 285}
417 286
418/* Temporary structure used to hold DER encoding of items for SET OF */ 287/* Temporary structure used to hold DER encoding of items for SET OF */
@@ -424,90 +293,72 @@ typedef struct {
424} DER_ENC; 293} DER_ENC;
425 294
426static int der_cmp(const void *a, const void *b) 295static int der_cmp(const void *a, const void *b)
427 { 296{
428 const DER_ENC *d1 = a, *d2 = b; 297 const DER_ENC *d1 = a, *d2 = b;
429 int cmplen, i; 298 int cmplen, i;
430 cmplen = (d1->length < d2->length) ? d1->length : d2->length; 299 cmplen = (d1->length < d2->length) ? d1->length : d2->length;
431 i = memcmp(d1->data, d2->data, cmplen); 300 i = memcmp(d1->data, d2->data, cmplen);
432 if (i) 301 if(i) return i;
433 return i;
434 return d1->length - d2->length; 302 return d1->length - d2->length;
435 } 303}
436 304
437/* Output the content octets of SET OF or SEQUENCE OF */ 305/* Output the content octets of SET OF or SEQUENCE OF */
438 306
439static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, 307static int asn1_set_seq_out(STACK_OF(ASN1_VALUE) *sk, unsigned char **out, int skcontlen, const ASN1_ITEM *item, int do_sort)
440 int skcontlen, const ASN1_ITEM *item, 308{
441 int do_sort, int iclass)
442 {
443 int i; 309 int i;
444 ASN1_VALUE *skitem; 310 ASN1_VALUE *skitem;
445 unsigned char *tmpdat = NULL, *p = NULL; 311 unsigned char *tmpdat = NULL, *p = NULL;
446 DER_ENC *derlst = NULL, *tder; 312 DER_ENC *derlst = NULL, *tder;
447 if (do_sort) 313 if(do_sort) {
448 {
449 /* Don't need to sort less than 2 items */ 314 /* Don't need to sort less than 2 items */
450 if (sk_ASN1_VALUE_num(sk) < 2) 315 if(sk_ASN1_VALUE_num(sk) < 2) do_sort = 0;
451 do_sort = 0; 316 else {
452 else 317 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk) * sizeof(*derlst));
453 {
454 derlst = OPENSSL_malloc(sk_ASN1_VALUE_num(sk)
455 * sizeof(*derlst));
456 tmpdat = OPENSSL_malloc(skcontlen); 318 tmpdat = OPENSSL_malloc(skcontlen);
457 if (!derlst || !tmpdat) 319 if(!derlst || !tmpdat) return 0;
458 return 0;
459 }
460 } 320 }
321 }
461 /* If not sorting just output each item */ 322 /* If not sorting just output each item */
462 if (!do_sort) 323 if(!do_sort) {
463 { 324 for(i = 0; i < sk_ASN1_VALUE_num(sk); i++) {
464 for (i = 0; i < sk_ASN1_VALUE_num(sk); i++)
465 {
466 skitem = sk_ASN1_VALUE_value(sk, i); 325 skitem = sk_ASN1_VALUE_value(sk, i);
467 ASN1_item_ex_i2d(&skitem, out, item, -1, iclass); 326 ASN1_item_i2d(skitem, out, item);
468 }
469 return 1;
470 } 327 }
328 return 1;
329 }
471 p = tmpdat; 330 p = tmpdat;
472
473 /* Doing sort: build up a list of each member's DER encoding */ 331 /* Doing sort: build up a list of each member's DER encoding */
474 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) 332 for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
475 {
476 skitem = sk_ASN1_VALUE_value(sk, i); 333 skitem = sk_ASN1_VALUE_value(sk, i);
477 tder->data = p; 334 tder->data = p;
478 tder->length = ASN1_item_ex_i2d(&skitem, &p, item, -1, iclass); 335 tder->length = ASN1_item_i2d(skitem, &p, item);
479 tder->field = skitem; 336 tder->field = skitem;
480 } 337 }
481
482 /* Now sort them */ 338 /* Now sort them */
483 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp); 339 qsort(derlst, sk_ASN1_VALUE_num(sk), sizeof(*derlst), der_cmp);
484 /* Output sorted DER encoding */ 340 /* Output sorted DER encoding */
485 p = *out; 341 p = *out;
486 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) 342 for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++) {
487 {
488 memcpy(p, tder->data, tder->length); 343 memcpy(p, tder->data, tder->length);
489 p += tder->length; 344 p += tder->length;
490 } 345 }
491 *out = p; 346 *out = p;
492 /* If do_sort is 2 then reorder the STACK */ 347 /* If do_sort is 2 then reorder the STACK */
493 if (do_sort == 2) 348 if(do_sort == 2) {
494 { 349 for(i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); i++, tder++)
495 for (i = 0, tder = derlst; i < sk_ASN1_VALUE_num(sk); 350 sk_ASN1_VALUE_set(sk, i, tder->field);
496 i++, tder++) 351 }
497 (void)sk_ASN1_VALUE_set(sk, i, tder->field);
498 }
499 OPENSSL_free(derlst); 352 OPENSSL_free(derlst);
500 OPENSSL_free(tmpdat); 353 OPENSSL_free(tmpdat);
501 return 1; 354 return 1;
502 } 355}
503 356
504static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, 357static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out, const ASN1_ITEM *it, int tag, int aclass)
505 const ASN1_ITEM *it, int tag, int aclass) 358{
506 {
507 int len; 359 int len;
508 int utype; 360 int utype;
509 int usetag; 361 int usetag;
510 int ndef = 0;
511 362
512 utype = it->utype; 363 utype = it->utype;
513 364
@@ -523,48 +374,33 @@ static int asn1_i2d_ex_primitive(ASN1_VALUE **pval, unsigned char **out,
523 * because the call to asn1_ex_i2c() could change 374 * because the call to asn1_ex_i2c() could change
524 * utype. 375 * utype.
525 */ 376 */
526 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) || 377 if((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
527 (utype == V_ASN1_OTHER)) 378 (utype == V_ASN1_OTHER))
528 usetag = 0; 379 usetag = 0;
529 else usetag = 1; 380 else usetag = 1;
530 381
531 /* -1 means omit type */ 382 /* -1 means omit type */
532 383
533 if (len == -1) 384 if(len == -1) return 0;
534 return 0;
535
536 /* -2 return is special meaning use ndef */
537 if (len == -2)
538 {
539 ndef = 2;
540 len = 0;
541 }
542 385
543 /* If not implicitly tagged get tag from underlying type */ 386 /* If not implicitly tagged get tag from underlying type */
544 if (tag == -1) tag = utype; 387 if(tag == -1) tag = utype;
545 388
546 /* Output tag+length followed by content octets */ 389 /* Output tag+length followed by content octets */
547 if (out) 390 if(out) {
548 { 391 if(usetag) ASN1_put_object(out, 0, len, tag, aclass);
549 if (usetag)
550 ASN1_put_object(out, ndef, len, tag, aclass);
551 asn1_ex_i2c(pval, *out, &utype, it); 392 asn1_ex_i2c(pval, *out, &utype, it);
552 if (ndef) 393 *out += len;
553 ASN1_put_eoc(out); 394 }
554 else
555 *out += len;
556 }
557 395
558 if (usetag) 396 if(usetag) return ASN1_object_size(0, len, tag);
559 return ASN1_object_size(ndef, len, tag);
560 return len; 397 return len;
561 } 398}
562 399
563/* Produce content octets from a structure */ 400/* Produce content octets from a structure */
564 401
565int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, 402int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype, const ASN1_ITEM *it)
566 const ASN1_ITEM *it) 403{
567 {
568 ASN1_BOOLEAN *tbool = NULL; 404 ASN1_BOOLEAN *tbool = NULL;
569 ASN1_STRING *strtmp; 405 ASN1_STRING *strtmp;
570 ASN1_OBJECT *otmp; 406 ASN1_OBJECT *otmp;
@@ -573,36 +409,28 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
573 int len; 409 int len;
574 const ASN1_PRIMITIVE_FUNCS *pf; 410 const ASN1_PRIMITIVE_FUNCS *pf;
575 pf = it->funcs; 411 pf = it->funcs;
576 if (pf && pf->prim_i2c) 412 if(pf && pf->prim_i2c) return pf->prim_i2c(pval, cout, putype, it);
577 return pf->prim_i2c(pval, cout, putype, it);
578 413
579 /* Should type be omitted? */ 414 /* Should type be omitted? */
580 if ((it->itype != ASN1_ITYPE_PRIMITIVE) 415 if((it->itype != ASN1_ITYPE_PRIMITIVE) || (it->utype != V_ASN1_BOOLEAN)) {
581 || (it->utype != V_ASN1_BOOLEAN)) 416 if(!*pval) return -1;
582 { 417 }
583 if (!*pval) return -1;
584 }
585 418
586 if (it->itype == ASN1_ITYPE_MSTRING) 419 if(it->itype == ASN1_ITYPE_MSTRING) {
587 {
588 /* If MSTRING type set the underlying type */ 420 /* If MSTRING type set the underlying type */
589 strtmp = (ASN1_STRING *)*pval; 421 strtmp = (ASN1_STRING *)*pval;
590 utype = strtmp->type; 422 utype = strtmp->type;
591 *putype = utype; 423 *putype = utype;
592 } 424 } else if(it->utype == V_ASN1_ANY) {
593 else if (it->utype == V_ASN1_ANY)
594 {
595 /* If ANY set type and pointer to value */ 425 /* If ANY set type and pointer to value */
596 ASN1_TYPE *typ; 426 ASN1_TYPE *typ;
597 typ = (ASN1_TYPE *)*pval; 427 typ = (ASN1_TYPE *)*pval;
598 utype = typ->type; 428 utype = typ->type;
599 *putype = utype; 429 *putype = utype;
600 pval = &typ->value.asn1_value; 430 pval = (ASN1_VALUE **)&typ->value.ptr;
601 } 431 } else utype = *putype;
602 else utype = *putype;
603 432
604 switch(utype) 433 switch(utype) {
605 {
606 case V_ASN1_OBJECT: 434 case V_ASN1_OBJECT:
607 otmp = (ASN1_OBJECT *)*pval; 435 otmp = (ASN1_OBJECT *)*pval;
608 cont = otmp->data; 436 cont = otmp->data;
@@ -616,15 +444,12 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
616 444
617 case V_ASN1_BOOLEAN: 445 case V_ASN1_BOOLEAN:
618 tbool = (ASN1_BOOLEAN *)pval; 446 tbool = (ASN1_BOOLEAN *)pval;
619 if (*tbool == -1) 447 if(*tbool == -1) return -1;
620 return -1;
621 if (it->utype != V_ASN1_ANY) 448 if (it->utype != V_ASN1_ANY)
622 { 449 {
623 /* Default handling if value == size field then omit */ 450 /* Default handling if value == size field then omit */
624 if (*tbool && (it->size > 0)) 451 if(*tbool && (it->size > 0)) return -1;
625 return -1; 452 if(!*tbool && !it->size) return -1;
626 if (!*tbool && !it->size)
627 return -1;
628 } 453 }
629 c = (unsigned char)*tbool; 454 c = (unsigned char)*tbool;
630 cont = &c; 455 cont = &c;
@@ -632,8 +457,7 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
632 break; 457 break;
633 458
634 case V_ASN1_BIT_STRING: 459 case V_ASN1_BIT_STRING:
635 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, 460 return i2c_ASN1_BIT_STRING((ASN1_BIT_STRING *)*pval, cout ? &cout : NULL);
636 cout ? &cout : NULL);
637 break; 461 break;
638 462
639 case V_ASN1_INTEGER: 463 case V_ASN1_INTEGER:
@@ -643,8 +467,7 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
643 /* These are all have the same content format 467 /* These are all have the same content format
644 * as ASN1_INTEGER 468 * as ASN1_INTEGER
645 */ 469 */
646 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, 470 return i2c_ASN1_INTEGER((ASN1_INTEGER *)*pval, cout ? &cout : NULL);
647 cout ? &cout : NULL);
648 break; 471 break;
649 472
650 case V_ASN1_OCTET_STRING: 473 case V_ASN1_OCTET_STRING:
@@ -666,25 +489,12 @@ int asn1_ex_i2c(ASN1_VALUE **pval, unsigned char *cout, int *putype,
666 default: 489 default:
667 /* All based on ASN1_STRING and handled the same */ 490 /* All based on ASN1_STRING and handled the same */
668 strtmp = (ASN1_STRING *)*pval; 491 strtmp = (ASN1_STRING *)*pval;
669 /* Special handling for NDEF */
670 if ((it->size == ASN1_TFLG_NDEF)
671 && (strtmp->flags & ASN1_STRING_FLAG_NDEF))
672 {
673 if (cout)
674 {
675 strtmp->data = cout;
676 strtmp->length = 0;
677 }
678 /* Special return code */
679 return -2;
680 }
681 cont = strtmp->data; 492 cont = strtmp->data;
682 len = strtmp->length; 493 len = strtmp->length;
683 494
684 break; 495 break;
685 496
686 }
687 if (cout && len)
688 memcpy(cout, cont, len);
689 return len;
690 } 497 }
498 if(cout && len) memcpy(cout, cont, len);
499 return len;
500}