summaryrefslogtreecommitdiff
path: root/src
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
parenta93a406b5997f92acc07a066c2bacb9194b5c737 (diff)
downloadopenbsd-c28759e489ff78d60b33248f7658689125bd0492.tar.gz
openbsd-c28759e489ff78d60b33248f7658689125bd0492.tar.bz2
openbsd-c28759e489ff78d60b33248f7658689125bd0492.zip
More KNF.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/asn1/tasn_dec.c392
-rw-r--r--src/lib/libcrypto/asn1/tasn_enc.c159
-rw-r--r--src/lib/libcrypto/asn1/tasn_fre.c37
-rw-r--r--src/lib/libcrypto/asn1/tasn_new.c90
-rw-r--r--src/lib/libcrypto/asn1/tasn_prn.c222
-rw-r--r--src/lib/libcrypto/asn1/tasn_typ.c10
-rw-r--r--src/lib/libcrypto/asn1/tasn_utl.c68
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_dec.c392
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_enc.c159
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_fre.c37
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_new.c90
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_prn.c222
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_typ.c10
-rw-r--r--src/lib/libssl/src/crypto/asn1/tasn_utl.c68
14 files changed, 1028 insertions, 928 deletions
diff --git a/src/lib/libcrypto/asn1/tasn_dec.c b/src/lib/libcrypto/asn1/tasn_dec.c
index 1a06ddc185..e9059df982 100644
--- a/src/lib/libcrypto/asn1/tasn_dec.c
+++ b/src/lib/libcrypto/asn1/tasn_dec.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
@@ -69,45 +69,40 @@ static int asn1_check_eoc(const unsigned char **in, long len);
69static int asn1_find_end(const unsigned char **in, long len, char inf); 69static int asn1_find_end(const unsigned char **in, long len, char inf);
70 70
71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth); 72 char inf, int tag, int aclass, int depth);
73 73
74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); 74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75 75
76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 char *inf, char *cst, 77 char *inf, char *cst, const unsigned char **in, long len, int exptag,
78 const unsigned char **in, long len, 78 int expclass, char opt, ASN1_TLC *ctx);
79 int exptag, int expclass, char opt, 79
80 ASN1_TLC *ctx); 80static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
81 81 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
82static int asn1_template_ex_d2i(ASN1_VALUE **pval, 82static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
83 const unsigned char **in, long len, 83 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
84 const ASN1_TEMPLATE *tt, char opt, 84static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
85 ASN1_TLC *ctx); 85 long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
86static int asn1_template_noexp_d2i(ASN1_VALUE **val, 86 ASN1_TLC *ctx);
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
89 ASN1_TLC *ctx);
90static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
91 const unsigned char **in, long len,
92 const ASN1_ITEM *it,
93 int tag, int aclass, char opt, ASN1_TLC *ctx);
94 87
95/* Table to convert tags to bit values, used for MSTRING type */ 88/* Table to convert tags to bit values, used for MSTRING type */
96static const unsigned long tag2bit[32] = { 89static const unsigned long tag2bit[32] = {
970, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ 90 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
98B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ 91 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
99B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ 92 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
100B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */ 93 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
101B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */ 94 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
102B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */ 95 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ 96 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ 97 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */ 98 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
106}; 99};
107 100
108unsigned long ASN1_tag2bit(int tag) 101unsigned long
102ASN1_tag2bit(int tag)
109{ 103{
110 if ((tag < 0) || (tag > 30)) return 0; 104 if ((tag < 0) || (tag > 30))
105 return 0;
111 return tag2bit[tag]; 106 return tag2bit[tag];
112} 107}
113 108
@@ -117,29 +112,31 @@ unsigned long ASN1_tag2bit(int tag)
117/* Version to avoid compiler warning about 'c' always non-NULL */ 112/* Version to avoid compiler warning about 'c' always non-NULL */
118#define asn1_tlc_clear_nc(c) (c)->valid = 0 113#define asn1_tlc_clear_nc(c) (c)->valid = 0
119 114
120/* Decode an ASN1 item, this currently behaves just 115/* Decode an ASN1 item, this currently behaves just
121 * like a standard 'd2i' function. 'in' points to 116 * like a standard 'd2i' function. 'in' points to
122 * a buffer to read the data from, in future we will 117 * a buffer to read the data from, in future we will
123 * have more advanced versions that can input data 118 * have more advanced versions that can input data
124 * a piece at a time and this will simply be a special 119 * a piece at a time and this will simply be a special
125 * case. 120 * case.
126 */ 121 */
127 122
128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, 123ASN1_VALUE *
129 const unsigned char **in, long len, const ASN1_ITEM *it) 124ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
125 const ASN1_ITEM *it)
130{ 126{
131 ASN1_TLC c; 127 ASN1_TLC c;
132 ASN1_VALUE *ptmpval = NULL; 128 ASN1_VALUE *ptmpval = NULL;
133 if (!pval) 129 if (!pval)
134 pval = &ptmpval; 130 pval = &ptmpval;
135 asn1_tlc_clear_nc(&c); 131 asn1_tlc_clear_nc(&c);
136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 132 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
137 return *pval; 133 return *pval;
138 return NULL; 134 return NULL;
139} 135}
140 136
141int ASN1_template_d2i(ASN1_VALUE **pval, 137int
142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt) 138ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
139 const ASN1_TEMPLATE *tt)
143{ 140{
144 ASN1_TLC c; 141 ASN1_TLC c;
145 asn1_tlc_clear_nc(&c); 142 asn1_tlc_clear_nc(&c);
@@ -151,9 +148,9 @@ int ASN1_template_d2i(ASN1_VALUE **pval,
151 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL 148 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
152 */ 149 */
153 150
154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 151int
155 const ASN1_ITEM *it, 152ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
156 int tag, int aclass, char opt, ASN1_TLC *ctx) 153 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
157{ 154{
158 const ASN1_TEMPLATE *tt, *errtt = NULL; 155 const ASN1_TEMPLATE *tt, *errtt = NULL;
159 const ASN1_COMPAT_FUNCS *cf; 156 const ASN1_COMPAT_FUNCS *cf;
@@ -161,7 +158,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
161 const ASN1_AUX *aux = it->funcs; 158 const ASN1_AUX *aux = it->funcs;
162 ASN1_aux_cb *asn1_cb; 159 ASN1_aux_cb *asn1_cb;
163 const unsigned char *p = NULL, *q; 160 const unsigned char *p = NULL, *q;
164 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */ 161 unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
165 unsigned char imphack = 0, oclass; 162 unsigned char imphack = 0, oclass;
166 char seq_eoc, seq_nolen, cst, isopt; 163 char seq_eoc, seq_nolen, cst, isopt;
167 long tmplen; 164 long tmplen;
@@ -173,9 +170,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
173 return 0; 170 return 0;
174 if (aux && aux->asn1_cb) 171 if (aux && aux->asn1_cb)
175 asn1_cb = aux->asn1_cb; 172 asn1_cb = aux->asn1_cb;
176 else asn1_cb = 0; 173 else
174 asn1_cb = 0;
177 175
178 switch(it->itype) { 176 switch (it->itype) {
179 case ASN1_ITYPE_PRIMITIVE: 177 case ASN1_ITYPE_PRIMITIVE:
180 if (it->templates) { 178 if (it->templates) {
181 /* tagging or OPTIONAL is currently illegal on an item 179 /* tagging or OPTIONAL is currently illegal on an item
@@ -186,33 +184,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
186 */ 184 */
187 if ((tag != -1) || opt) { 185 if ((tag != -1) || opt) {
188 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 186 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
189 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 187 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
190 goto err; 188 goto err;
191 } 189 }
192 return asn1_template_ex_d2i(pval, in, len, 190 return asn1_template_ex_d2i(pval, in, len,
193 it->templates, opt, ctx); 191 it->templates, opt, ctx);
194 } 192 }
195 return asn1_d2i_ex_primitive(pval, in, len, it, 193 return asn1_d2i_ex_primitive(pval, in, len, it,
196 tag, aclass, opt, ctx); 194 tag, aclass, opt, ctx);
197 break; 195 break;
198 196
199 case ASN1_ITYPE_MSTRING: 197 case ASN1_ITYPE_MSTRING:
200 p = *in; 198 p = *in;
201 /* Just read in tag and class */ 199 /* Just read in tag and class */
202 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, 200 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
203 &p, len, -1, 0, 1, ctx); 201 &p, len, -1, 0, 1, ctx);
204 if (!ret) { 202 if (!ret) {
205 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 203 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
206 ERR_R_NESTED_ASN1_ERROR); 204 ERR_R_NESTED_ASN1_ERROR);
207 goto err; 205 goto err;
208 } 206 }
209 207
210 /* Must be UNIVERSAL class */ 208 /* Must be UNIVERSAL class */
211 if (oclass != V_ASN1_UNIVERSAL) { 209 if (oclass != V_ASN1_UNIVERSAL) {
212 /* If OPTIONAL, assume this is OK */ 210 /* If OPTIONAL, assume this is OK */
213 if (opt) return -1; 211 if (opt)
212 return -1;
214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 213 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
215 ASN1_R_MSTRING_NOT_UNIVERSAL); 214 ASN1_R_MSTRING_NOT_UNIVERSAL);
216 goto err; 215 goto err;
217 } 216 }
218 /* Check tag matches bit map */ 217 /* Check tag matches bit map */
@@ -221,17 +220,17 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
221 if (opt) 220 if (opt)
222 return -1; 221 return -1;
223 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 222 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
224 ASN1_R_MSTRING_WRONG_TAG); 223 ASN1_R_MSTRING_WRONG_TAG);
225 goto err; 224 goto err;
226 } 225 }
227 return asn1_d2i_ex_primitive(pval, in, len, 226 return asn1_d2i_ex_primitive(pval, in, len,
228 it, otag, 0, 0, ctx); 227 it, otag, 0, 0, ctx);
229 228
230 case ASN1_ITYPE_EXTERN: 229 case ASN1_ITYPE_EXTERN:
231 /* Use new style d2i */ 230 /* Use new style d2i */
232 ef = it->funcs; 231 ef = it->funcs;
233 return ef->asn1_ex_d2i(pval, in, len, 232 return ef->asn1_ex_d2i(pval, in, len,
234 it, tag, aclass, opt, ctx); 233 it, tag, aclass, opt, ctx);
235 234
236 case ASN1_ITYPE_COMPAT: 235 case ASN1_ITYPE_COMPAT:
237 /* we must resort to old style evil hackery */ 236 /* we must resort to old style evil hackery */
@@ -243,15 +242,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
243 p = *in; 242 p = *in;
244 if (tag == -1) 243 if (tag == -1)
245 exptag = it->utype; 244 exptag = it->utype;
246 else exptag = tag; 245 else
246 exptag = tag;
247 /* Don't care about anything other than presence 247 /* Don't care about anything other than presence
248 * of expected tag */ 248 * of expected tag */
249 249
250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, 250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
251 &p, len, exptag, aclass, 1, ctx); 251 &p, len, exptag, aclass, 1, ctx);
252 if (!ret) { 252 if (!ret) {
253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
254 ERR_R_NESTED_ASN1_ERROR); 254 ERR_R_NESTED_ASN1_ERROR);
255 goto err; 255 goto err;
256 } 256 }
257 if (ret == -1) 257 if (ret == -1)
@@ -278,11 +278,11 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
278 imphack = *wp; 278 imphack = *wp;
279 if (p == NULL) { 279 if (p == NULL) {
280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
281 ERR_R_NESTED_ASN1_ERROR); 281 ERR_R_NESTED_ASN1_ERROR);
282 goto err; 282 goto err;
283 } 283 }
284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) 284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) |
285 | it->utype); 285 it->utype);
286 } 286 }
287 287
288 ptmpval = cf->asn1_d2i(pval, in, len); 288 ptmpval = cf->asn1_d2i(pval, in, len);
@@ -296,20 +296,19 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
297 goto err; 297 goto err;
298 298
299
300 case ASN1_ITYPE_CHOICE: 299 case ASN1_ITYPE_CHOICE:
301 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 300 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
302 goto auxerr; 301 goto auxerr;
303 302
304 /* Allocate structure */ 303 /* Allocate structure */
305 if (!*pval && !ASN1_item_ex_new(pval, it)) { 304 if (!*pval && !ASN1_item_ex_new(pval, it)) {
306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 305 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
307 ERR_R_NESTED_ASN1_ERROR); 306 ERR_R_NESTED_ASN1_ERROR);
308 goto err; 307 goto err;
309 } 308 }
310 /* CHOICE type, try each possibility in turn */ 309 /* CHOICE type, try each possibility in turn */
311 p = *in; 310 p = *in;
312 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) { 311 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
313 pchptr = asn1_get_field_ptr(pval, tt); 312 pchptr = asn1_get_field_ptr(pval, tt);
314 /* We mark field as OPTIONAL so its absence 313 /* We mark field as OPTIONAL so its absence
315 * can be recognised. 314 * can be recognised.
@@ -324,7 +323,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
324 /* Otherwise must be an ASN1 parsing error */ 323 /* Otherwise must be an ASN1 parsing error */
325 errtt = tt; 324 errtt = tt;
326 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 325 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
327 ERR_R_NESTED_ASN1_ERROR); 326 ERR_R_NESTED_ASN1_ERROR);
328 goto err; 327 goto err;
329 } 328 }
330 329
@@ -337,14 +336,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
337 return -1; 336 return -1;
338 } 337 }
339 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
340 ASN1_R_NO_MATCHING_CHOICE_TYPE); 339 ASN1_R_NO_MATCHING_CHOICE_TYPE);
341 goto err; 340 goto err;
342 } 341 }
343 342
344 asn1_set_choice_selector(pval, i, it); 343 asn1_set_choice_selector(pval, i, it);
345 *in = p; 344 *in = p;
346 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 345 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
347 goto auxerr; 346 goto auxerr;
348 return 1; 347 return 1;
349 348
350 case ASN1_ITYPE_NDEF_SEQUENCE: 349 case ASN1_ITYPE_NDEF_SEQUENCE:
@@ -359,34 +358,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
359 } 358 }
360 /* Get SEQUENCE length and update len, p */ 359 /* Get SEQUENCE length and update len, p */
361 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, 360 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
362 &p, len, tag, aclass, opt, ctx); 361 &p, len, tag, aclass, opt, ctx);
363 if (!ret) { 362 if (!ret) {
364 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 363 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
365 ERR_R_NESTED_ASN1_ERROR); 364 ERR_R_NESTED_ASN1_ERROR);
366 goto err; 365 goto err;
367 } 366 } else if (ret == -1)
368 else if (ret == -1)
369 return -1; 367 return -1;
370 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { 368 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
371 len = tmplen - (p - *in); 369 len = tmplen - (p - *in);
372 seq_nolen = 1; 370 seq_nolen = 1;
373 } 371 }
374 /* If indefinite we don't do a length check */ 372 /* If indefinite we don't do a length check */
375 else seq_nolen = seq_eoc; 373 else
374 seq_nolen = seq_eoc;
376 if (!cst) { 375 if (!cst) {
377 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 376 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
378 ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 377 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
379 goto err; 378 goto err;
380 } 379 }
381 380
382 if (!*pval && !ASN1_item_ex_new(pval, it)) { 381 if (!*pval && !ASN1_item_ex_new(pval, it)) {
383 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 382 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
384 ERR_R_NESTED_ASN1_ERROR); 383 ERR_R_NESTED_ASN1_ERROR);
385 goto err; 384 goto err;
386 } 385 }
387 386
388 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 387 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
389 goto auxerr; 388 goto auxerr;
390 389
391 /* Get each field entry */ 390 /* Get each field entry */
392 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 391 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
@@ -403,7 +402,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
403 if (asn1_check_eoc(&p, len)) { 402 if (asn1_check_eoc(&p, len)) {
404 if (!seq_eoc) { 403 if (!seq_eoc) {
405 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 404 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
406 ASN1_R_UNEXPECTED_EOC); 405 ASN1_R_UNEXPECTED_EOC);
407 goto err; 406 goto err;
408 } 407 }
409 len -= p - q; 408 len -= p - q;
@@ -419,12 +418,13 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
419 */ 418 */
420 if (i == (it->tcount - 1)) 419 if (i == (it->tcount - 1))
421 isopt = 0; 420 isopt = 0;
422 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); 421 else
422 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
423 /* attempt to read in field, allowing each to be 423 /* attempt to read in field, allowing each to be
424 * OPTIONAL */ 424 * OPTIONAL */
425 425
426 ret = asn1_template_ex_d2i(pseqval, &p, len, 426 ret = asn1_template_ex_d2i(pseqval, &p, len,
427 seqtt, isopt, ctx); 427 seqtt, isopt, ctx);
428 if (!ret) { 428 if (!ret) {
429 errtt = seqtt; 429 errtt = seqtt;
430 goto err; 430 goto err;
@@ -447,7 +447,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
447 /* Check all data read */ 447 /* Check all data read */
448 if (!seq_nolen && len) { 448 if (!seq_nolen && len) {
449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
450 ASN1_R_SEQUENCE_LENGTH_MISMATCH); 450 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
451 goto err; 451 goto err;
452 } 452 }
453 453
@@ -467,7 +467,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
467 } else { 467 } else {
468 errtt = seqtt; 468 errtt = seqtt;
469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
470 ASN1_R_FIELD_MISSING); 470 ASN1_R_FIELD_MISSING);
471 goto err; 471 goto err;
472 } 472 }
473 } 473 }
@@ -476,19 +476,20 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
476 goto auxerr; 476 goto auxerr;
477 *in = p; 477 *in = p;
478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
479 goto auxerr; 479 goto auxerr;
480 return 1; 480 return 1;
481 481
482 default: 482 default:
483 return 0; 483 return 0;
484 } 484 }
485 auxerr: 485
486auxerr:
486 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); 487 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
487 err: 488err:
488 ASN1_item_ex_free(pval, it); 489 ASN1_item_ex_free(pval, it);
489 if (errtt) 490 if (errtt)
490 ERR_add_error_data(4, "Field=", errtt->field_name, 491 ERR_add_error_data(4, "Field=", errtt->field_name,
491 ", Type=", it->sname); 492 ", Type=", it->sname);
492 else 493 else
493 ERR_add_error_data(2, "Type=", it->sname); 494 ERR_add_error_data(2, "Type=", it->sname);
494 return 0; 495 return 0;
@@ -498,16 +499,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
498 * One handles any EXPLICIT tag and the other handles the rest. 499 * One handles any EXPLICIT tag and the other handles the rest.
499 */ 500 */
500 501
501static int asn1_template_ex_d2i(ASN1_VALUE **val, 502static int
502 const unsigned char **in, long inlen, 503asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
503 const ASN1_TEMPLATE *tt, char opt, 504 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
504 ASN1_TLC *ctx)
505{ 505{
506 int flags, aclass; 506 int flags, aclass;
507 int ret; 507 int ret;
508 long len; 508 long len;
509 const unsigned char *p, *q; 509 const unsigned char *p, *q;
510 char exp_eoc; 510 char exp_eoc;
511
511 if (!val) 512 if (!val)
512 return 0; 513 return 0;
513 flags = tt->flags; 514 flags = tt->flags;
@@ -523,24 +524,24 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
523 * get the info. 524 * get the info.
524 */ 525 */
525 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, 526 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
526 &p, inlen, tt->tag, aclass, opt, ctx); 527 &p, inlen, tt->tag, aclass, opt, ctx);
527 q = p; 528 q = p;
528 if (!ret) { 529 if (!ret) {
529 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 530 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
530 ERR_R_NESTED_ASN1_ERROR); 531 ERR_R_NESTED_ASN1_ERROR);
531 return 0; 532 return 0;
532 } else if (ret == -1) 533 } else if (ret == -1)
533 return -1; 534 return -1;
534 if (!cst) { 535 if (!cst) {
535 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 536 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
536 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 537 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
537 return 0; 538 return 0;
538 } 539 }
539 /* We've found the field so it can't be OPTIONAL now */ 540 /* We've found the field so it can't be OPTIONAL now */
540 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 541 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
541 if (!ret) { 542 if (!ret) {
542 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 543 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
543 ERR_R_NESTED_ASN1_ERROR); 544 ERR_R_NESTED_ASN1_ERROR);
544 return 0; 545 return 0;
545 } 546 }
546 /* We read the field in OK so update length */ 547 /* We read the field in OK so update length */
@@ -549,7 +550,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
549 /* If NDEF we must have an EOC here */ 550 /* If NDEF we must have an EOC here */
550 if (!asn1_check_eoc(&p, len)) { 551 if (!asn1_check_eoc(&p, len)) {
551 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 552 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
552 ASN1_R_MISSING_EOC); 553 ASN1_R_MISSING_EOC);
553 goto err; 554 goto err;
554 } 555 }
555 } else { 556 } else {
@@ -557,31 +558,29 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
557 * an error */ 558 * an error */
558 if (len) { 559 if (len) {
559 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 560 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
560 ASN1_R_EXPLICIT_LENGTH_MISMATCH); 561 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
561 goto err; 562 goto err;
562 } 563 }
563 } 564 }
564 } 565 } else
565 else 566 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
566 return asn1_template_noexp_d2i(val, in, inlen,
567 tt, opt, ctx);
568 567
569 *in = p; 568 *in = p;
570 return 1; 569 return 1;
571 570
572 err: 571err:
573 ASN1_template_free(val, tt); 572 ASN1_template_free(val, tt);
574 return 0; 573 return 0;
575} 574}
576 575
577static int asn1_template_noexp_d2i(ASN1_VALUE **val, 576static int
578 const unsigned char **in, long len, 577asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
579 const ASN1_TEMPLATE *tt, char opt, 578 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
580 ASN1_TLC *ctx)
581{ 579{
582 int flags, aclass; 580 int flags, aclass;
583 int ret; 581 int ret;
584 const unsigned char *p, *q; 582 const unsigned char *p, *q;
583
585 if (!val) 584 if (!val)
586 return 0; 585 return 0;
587 flags = tt->flags; 586 flags = tt->flags;
@@ -607,43 +606,42 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
607 } 606 }
608 /* Get the tag */ 607 /* Get the tag */
609 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, 608 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
610 &p, len, sktag, skaclass, opt, ctx); 609 &p, len, sktag, skaclass, opt, ctx);
611 if (!ret) { 610 if (!ret) {
612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 611 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
613 ERR_R_NESTED_ASN1_ERROR); 612 ERR_R_NESTED_ASN1_ERROR);
614 return 0; 613 return 0;
615 } 614 } else if (ret == -1)
616 else if (ret == -1)
617 return -1; 615 return -1;
618 if (!*val) 616 if (!*val)
619 *val = (ASN1_VALUE *)sk_new_null(); 617 *val = (ASN1_VALUE *)sk_new_null();
620 else { 618 else {
621 /* We've got a valid STACK: free up any items present */ 619 /* We've got a valid STACK: free up any items present */
622 STACK_OF(ASN1_VALUE) *sktmp 620 STACK_OF(ASN1_VALUE) *sktmp =
623 = (STACK_OF(ASN1_VALUE) *)*val; 621 (STACK_OF(ASN1_VALUE) *)*val;
624 ASN1_VALUE *vtmp; 622 ASN1_VALUE *vtmp;
625 while(sk_ASN1_VALUE_num(sktmp) > 0) { 623 while (sk_ASN1_VALUE_num(sktmp) > 0) {
626 vtmp = sk_ASN1_VALUE_pop(sktmp); 624 vtmp = sk_ASN1_VALUE_pop(sktmp);
627 ASN1_item_ex_free(&vtmp, 625 ASN1_item_ex_free(&vtmp,
628 ASN1_ITEM_ptr(tt->item)); 626 ASN1_ITEM_ptr(tt->item));
629 } 627 }
630 } 628 }
631 629
632 if (!*val) { 630 if (!*val) {
633 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 631 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
634 ERR_R_MALLOC_FAILURE); 632 ERR_R_MALLOC_FAILURE);
635 goto err; 633 goto err;
636 } 634 }
637 635
638 /* Read as many items as we can */ 636 /* Read as many items as we can */
639 while(len > 0) { 637 while (len > 0) {
640 ASN1_VALUE *skfield; 638 ASN1_VALUE *skfield;
641 q = p; 639 q = p;
642 /* See if EOC found */ 640 /* See if EOC found */
643 if (asn1_check_eoc(&p, len)) { 641 if (asn1_check_eoc(&p, len)) {
644 if (!sk_eoc) { 642 if (!sk_eoc) {
645 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 643 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
646 ASN1_R_UNEXPECTED_EOC); 644 ASN1_R_UNEXPECTED_EOC);
647 goto err; 645 goto err;
648 } 646 }
649 len -= p - q; 647 len -= p - q;
@@ -652,60 +650,57 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
652 } 650 }
653 skfield = NULL; 651 skfield = NULL;
654 if (!ASN1_item_ex_d2i(&skfield, &p, len, 652 if (!ASN1_item_ex_d2i(&skfield, &p, len,
655 ASN1_ITEM_ptr(tt->item), 653 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
656 -1, 0, 0, ctx)) {
657 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 654 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
658 ERR_R_NESTED_ASN1_ERROR); 655 ERR_R_NESTED_ASN1_ERROR);
659 goto err; 656 goto err;
660 } 657 }
661 len -= p - q; 658 len -= p - q;
662 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 659 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
663 skfield)) { 660 skfield)) {
664 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 661 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
665 ERR_R_MALLOC_FAILURE); 662 ERR_R_MALLOC_FAILURE);
666 goto err; 663 goto err;
667 } 664 }
668 } 665 }
669 if (sk_eoc) { 666 if (sk_eoc) {
670 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); 667 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
668 ASN1_R_MISSING_EOC);
671 goto err; 669 goto err;
672 } 670 }
673 } else if (flags & ASN1_TFLG_IMPTAG) { 671 } else if (flags & ASN1_TFLG_IMPTAG) {
674 /* IMPLICIT tagging */ 672 /* IMPLICIT tagging */
675 ret = ASN1_item_ex_d2i(val, &p, len, 673 ret = ASN1_item_ex_d2i(val, &p, len,
676 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 674 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
677 if (!ret) { 675 if (!ret) {
678 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 676 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
679 ERR_R_NESTED_ASN1_ERROR); 677 ERR_R_NESTED_ASN1_ERROR);
680 goto err; 678 goto err;
681 } 679 } else if (ret == -1)
682 else if (ret == -1)
683 return -1; 680 return -1;
684 } else { 681 } else {
685 /* Nothing special */ 682 /* Nothing special */
686 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), 683 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
687 -1, 0, opt, ctx); 684 -1, 0, opt, ctx);
688 if (!ret) { 685 if (!ret) {
689 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 686 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
690 ERR_R_NESTED_ASN1_ERROR); 687 ERR_R_NESTED_ASN1_ERROR);
691 goto err; 688 goto err;
692 } 689 } else if (ret == -1)
693 else if (ret == -1)
694 return -1; 690 return -1;
695 } 691 }
696 692
697 *in = p; 693 *in = p;
698 return 1; 694 return 1;
699 695
700 err: 696err:
701 ASN1_template_free(val, tt); 697 ASN1_template_free(val, tt);
702 return 0; 698 return 0;
703} 699}
704 700
705static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, 701static int
706 const unsigned char **in, long inlen, 702asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
707 const ASN1_ITEM *it, 703 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
708 int tag, int aclass, char opt, ASN1_TLC *ctx)
709{ 704{
710 int ret = 0, utype; 705 int ret = 0, utype;
711 long plen; 706 long plen;
@@ -713,7 +708,8 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
713 const unsigned char *p; 708 const unsigned char *p;
714 BUF_MEM buf; 709 BUF_MEM buf;
715 const unsigned char *cont = NULL; 710 const unsigned char *cont = NULL;
716 long len; 711 long len;
712
717 if (!pval) { 713 if (!pval) {
718 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 714 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
719 return 0; /* Should never happen */ 715 return 0; /* Should never happen */
@@ -722,8 +718,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
722 if (it->itype == ASN1_ITYPE_MSTRING) { 718 if (it->itype == ASN1_ITYPE_MSTRING) {
723 utype = tag; 719 utype = tag;
724 tag = -1; 720 tag = -1;
725 } 721 } else
726 else
727 utype = it->utype; 722 utype = it->utype;
728 723
729 if (utype == V_ASN1_ANY) { 724 if (utype == V_ASN1_ANY) {
@@ -731,20 +726,20 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
731 unsigned char oclass; 726 unsigned char oclass;
732 if (tag >= 0) { 727 if (tag >= 0) {
733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 728 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
734 ASN1_R_ILLEGAL_TAGGED_ANY); 729 ASN1_R_ILLEGAL_TAGGED_ANY);
735 return 0; 730 return 0;
736 } 731 }
737 if (opt) { 732 if (opt) {
738 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
739 ASN1_R_ILLEGAL_OPTIONAL_ANY); 734 ASN1_R_ILLEGAL_OPTIONAL_ANY);
740 return 0; 735 return 0;
741 } 736 }
742 p = *in; 737 p = *in;
743 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, 738 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
744 &p, inlen, -1, 0, 0, ctx); 739 &p, inlen, -1, 0, 0, ctx);
745 if (!ret) { 740 if (!ret) {
746 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 741 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
747 ERR_R_NESTED_ASN1_ERROR); 742 ERR_R_NESTED_ASN1_ERROR);
748 return 0; 743 return 0;
749 } 744 }
750 if (oclass != V_ASN1_UNIVERSAL) 745 if (oclass != V_ASN1_UNIVERSAL)
@@ -757,17 +752,16 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
757 p = *in; 752 p = *in;
758 /* Check header */ 753 /* Check header */
759 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, 754 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
760 &p, inlen, tag, aclass, opt, ctx); 755 &p, inlen, tag, aclass, opt, ctx);
761 if (!ret) { 756 if (!ret) {
762 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 757 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
763 return 0; 758 return 0;
764 } 759 } else if (ret == -1)
765 else if (ret == -1)
766 return -1; 760 return -1;
767 ret = 0; 761 ret = 0;
768 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 762 /* SEQUENCE, SET and "OTHER" are left in encoded form */
769 if ((utype == V_ASN1_SEQUENCE) 763 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
770 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { 764 (utype == V_ASN1_OTHER)) {
771 /* Clear context cache for type OTHER because the auto clear 765 /* Clear context cache for type OTHER because the auto clear
772 * when we have a exact match wont work 766 * when we have a exact match wont work
773 */ 767 */
@@ -777,7 +771,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
777 /* SEQUENCE and SET must be constructed */ 771 /* SEQUENCE and SET must be constructed */
778 else if (!cst) { 772 else if (!cst) {
779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 773 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
780 ASN1_R_TYPE_NOT_CONSTRUCTED); 774 ASN1_R_TYPE_NOT_CONSTRUCTED);
781 return 0; 775 return 0;
782 } 776 }
783 777
@@ -785,7 +779,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
785 /* If indefinite length constructed find the real end */ 779 /* If indefinite length constructed find the real end */
786 if (inf) { 780 if (inf) {
787 if (!asn1_find_end(&p, plen, inf)) 781 if (!asn1_find_end(&p, plen, inf))
788 goto err; 782 goto err;
789 len = p - cont; 783 len = p - cont;
790 } else { 784 } else {
791 len = p - cont + plen; 785 len = p - cont + plen;
@@ -810,7 +804,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
810 /* Append a final null to string */ 804 /* Append a final null to string */
811 if (!BUF_MEM_grow_clean(&buf, len + 1)) { 805 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 806 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
813 ERR_R_MALLOC_FAILURE); 807 ERR_R_MALLOC_FAILURE);
814 return 0; 808 return 0;
815 } 809 }
816 buf.data[len] = 0; 810 buf.data[len] = 0;
@@ -828,15 +822,18 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
828 822
829 *in = p; 823 *in = p;
830 ret = 1; 824 ret = 1;
831 err: 825
832 if (free_cont && buf.data) free(buf.data); 826err:
827 if (free_cont && buf.data)
828 free(buf.data);
833 return ret; 829 return ret;
834} 830}
835 831
836/* Translate ASN1 content octets into a structure */ 832/* Translate ASN1 content octets into a structure */
837 833
838int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 834int
839 int utype, char *free_cont, const ASN1_ITEM *it) 835asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
836 char *free_cont, const ASN1_ITEM *it)
840{ 837{
841 ASN1_VALUE **opval = NULL; 838 ASN1_VALUE **opval = NULL;
842 ASN1_STRING *stmp; 839 ASN1_STRING *stmp;
@@ -855,8 +852,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
855 if (typ == NULL) 852 if (typ == NULL)
856 goto err; 853 goto err;
857 *pval = (ASN1_VALUE *)typ; 854 *pval = (ASN1_VALUE *)typ;
858 } 855 } else
859 else
860 typ = (ASN1_TYPE *)*pval; 856 typ = (ASN1_TYPE *)*pval;
861 857
862 if (utype != typ->type) 858 if (utype != typ->type)
@@ -864,7 +860,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
864 opval = pval; 860 opval = pval;
865 pval = &typ->value.asn1_value; 861 pval = &typ->value.asn1_value;
866 } 862 }
867 switch(utype) { 863 switch (utype) {
868 case V_ASN1_OBJECT: 864 case V_ASN1_OBJECT:
869 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) 865 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
870 goto err; 866 goto err;
@@ -873,7 +869,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
873 case V_ASN1_NULL: 869 case V_ASN1_NULL:
874 if (len) { 870 if (len) {
875 ASN1err(ASN1_F_ASN1_EX_C2I, 871 ASN1err(ASN1_F_ASN1_EX_C2I,
876 ASN1_R_NULL_IS_WRONG_LENGTH); 872 ASN1_R_NULL_IS_WRONG_LENGTH);
877 goto err; 873 goto err;
878 } 874 }
879 *pval = (ASN1_VALUE *)1; 875 *pval = (ASN1_VALUE *)1;
@@ -882,7 +878,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
882 case V_ASN1_BOOLEAN: 878 case V_ASN1_BOOLEAN:
883 if (len != 1) { 879 if (len != 1) {
884 ASN1err(ASN1_F_ASN1_EX_C2I, 880 ASN1err(ASN1_F_ASN1_EX_C2I,
885 ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 881 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
886 goto err; 882 goto err;
887 } else { 883 } else {
888 ASN1_BOOLEAN *tbool; 884 ASN1_BOOLEAN *tbool;
@@ -927,12 +923,12 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
927 default: 923 default:
928 if (utype == V_ASN1_BMPSTRING && (len & 1)) { 924 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
929 ASN1err(ASN1_F_ASN1_EX_C2I, 925 ASN1err(ASN1_F_ASN1_EX_C2I,
930 ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 926 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
931 goto err; 927 goto err;
932 } 928 }
933 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) { 929 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
934 ASN1err(ASN1_F_ASN1_EX_C2I, 930 ASN1err(ASN1_F_ASN1_EX_C2I,
935 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 931 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
936 goto err; 932 goto err;
937 } 933 }
938 /* All based on ASN1_STRING and handled the same */ 934 /* All based on ASN1_STRING and handled the same */
@@ -940,7 +936,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
940 stmp = ASN1_STRING_type_new(utype); 936 stmp = ASN1_STRING_type_new(utype);
941 if (!stmp) { 937 if (!stmp) {
942 ASN1err(ASN1_F_ASN1_EX_C2I, 938 ASN1err(ASN1_F_ASN1_EX_C2I,
943 ERR_R_MALLOC_FAILURE); 939 ERR_R_MALLOC_FAILURE);
944 goto err; 940 goto err;
945 } 941 }
946 *pval = (ASN1_VALUE *)stmp; 942 *pval = (ASN1_VALUE *)stmp;
@@ -958,8 +954,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
958 } else { 954 } else {
959 if (!ASN1_STRING_set(stmp, cont, len)) { 955 if (!ASN1_STRING_set(stmp, cont, len)) {
960 ASN1err(ASN1_F_ASN1_EX_C2I, 956 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ERR_R_MALLOC_FAILURE); 957 ERR_R_MALLOC_FAILURE);
962 ASN1_STRING_free(stmp); 958 ASN1_STRING_free(stmp);
963 *pval = NULL; 959 *pval = NULL;
964 goto err; 960 goto err;
965 } 961 }
@@ -968,10 +964,11 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
968 } 964 }
969 /* If ASN1_ANY and NULL type fix up value */ 965 /* If ASN1_ANY and NULL type fix up value */
970 if (typ && (utype == V_ASN1_NULL)) 966 if (typ && (utype == V_ASN1_NULL))
971 typ->value.ptr = NULL; 967 typ->value.ptr = NULL;
972 968
973 ret = 1; 969 ret = 1;
974 err: 970
971err:
975 if (!ret) { 972 if (!ret) {
976 ASN1_TYPE_free(typ); 973 ASN1_TYPE_free(typ);
977 if (opval) 974 if (opval)
@@ -987,11 +984,13 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
987 * recurse on each indefinite length header. 984 * recurse on each indefinite length header.
988 */ 985 */
989 986
990static int asn1_find_end(const unsigned char **in, long len, char inf) 987static int
988asn1_find_end(const unsigned char **in, long len, char inf)
991{ 989{
992 int expected_eoc; 990 int expected_eoc;
993 long plen; 991 long plen;
994 const unsigned char *p = *in, *q; 992 const unsigned char *p = *in, *q;
993
995 /* If not indefinite length constructed just add length */ 994 /* If not indefinite length constructed just add length */
996 if (inf == 0) { 995 if (inf == 0) {
997 *in += len; 996 *in += len;
@@ -1004,7 +1003,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1004 * skip to the end of the data. 1003 * skip to the end of the data.
1005 */ 1004 */
1006 while (len > 0) { 1005 while (len > 0) {
1007 if(asn1_check_eoc(&p, len)) { 1006 if (asn1_check_eoc(&p, len)) {
1008 expected_eoc--; 1007 expected_eoc--;
1009 if (expected_eoc == 0) 1008 if (expected_eoc == 0)
1010 break; 1009 break;
@@ -1013,8 +1012,8 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1013 } 1012 }
1014 q = p; 1013 q = p;
1015 /* Just read in a header: only care about the length */ 1014 /* Just read in a header: only care about the length */
1016 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, 1015 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1017 -1, 0, 0, NULL)) { 1016 -1, 0, 0, NULL)) {
1018 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); 1017 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1019 return 0; 1018 return 0;
1020 } 1019 }
@@ -1046,13 +1045,14 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1046#define ASN1_MAX_STRING_NEST 5 1045#define ASN1_MAX_STRING_NEST 5
1047#endif 1046#endif
1048 1047
1049 1048static int
1050static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 1049asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, char inf,
1051 char inf, int tag, int aclass, int depth) 1050 int tag, int aclass, int depth)
1052{ 1051{
1053 const unsigned char *p, *q; 1052 const unsigned char *p, *q;
1054 long plen; 1053 long plen;
1055 char cst, ininf; 1054 char cst, ininf;
1055
1056 p = *in; 1056 p = *in;
1057 inf &= 1; 1057 inf &= 1;
1058 /* If no buffer and not indefinite length constructed just pass over 1058 /* If no buffer and not indefinite length constructed just pass over
@@ -1061,7 +1061,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1061 *in += len; 1061 *in += len;
1062 return 1; 1062 return 1;
1063 } 1063 }
1064 while(len > 0) { 1064 while (len > 0) {
1065 q = p; 1065 q = p;
1066 /* Check for EOC */ 1066 /* Check for EOC */
1067 if (asn1_check_eoc(&p, len)) { 1067 if (asn1_check_eoc(&p, len)) {
@@ -1069,7 +1069,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1069 * constructed form */ 1069 * constructed form */
1070 if (!inf) { 1070 if (!inf) {
1071 ASN1err(ASN1_F_ASN1_COLLECT, 1071 ASN1err(ASN1_F_ASN1_COLLECT,
1072 ASN1_R_UNEXPECTED_EOC); 1072 ASN1_R_UNEXPECTED_EOC);
1073 return 0; 1073 return 0;
1074 } 1074 }
1075 inf = 0; 1075 inf = 0;
@@ -1077,7 +1077,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1077 } 1077 }
1078 1078
1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, 1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1080 len, tag, aclass, 0, NULL)) { 1080 len, tag, aclass, 0, NULL)) {
1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1082 return 0; 1082 return 0;
1083 } 1083 }
@@ -1086,14 +1086,13 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1086 if (cst) { 1086 if (cst) {
1087 if (depth >= ASN1_MAX_STRING_NEST) { 1087 if (depth >= ASN1_MAX_STRING_NEST) {
1088 ASN1err(ASN1_F_ASN1_COLLECT, 1088 ASN1err(ASN1_F_ASN1_COLLECT,
1089 ASN1_R_NESTED_ASN1_STRING); 1089 ASN1_R_NESTED_ASN1_STRING);
1090 return 0; 1090 return 0;
1091 } 1091 }
1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, 1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1093 depth + 1)) 1093 depth + 1))
1094 return 0; 1094 return 0;
1095 } 1095 } else if (plen && !collect_data(buf, &p, plen))
1096 else if (plen && !collect_data(buf, &p, plen))
1097 return 0; 1096 return 0;
1098 len -= p - q; 1097 len -= p - q;
1099 } 1098 }
@@ -1105,7 +1104,8 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1105 return 1; 1104 return 1;
1106} 1105}
1107 1106
1108static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) 1107static int
1108collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1109{ 1109{
1110 int len; 1110 int len;
1111 if (buf) { 1111 if (buf) {
@@ -1122,10 +1122,13 @@ static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1122 1122
1123/* Check for ASN1 EOC and swallow it if found */ 1123/* Check for ASN1 EOC and swallow it if found */
1124 1124
1125static int asn1_check_eoc(const unsigned char **in, long len) 1125static int
1126asn1_check_eoc(const unsigned char **in, long len)
1126{ 1127{
1127 const unsigned char *p; 1128 const unsigned char *p;
1128 if (len < 2) return 0; 1129
1130 if (len < 2)
1131 return 0;
1129 p = *in; 1132 p = *in;
1130 if (!p[0] && !p[1]) { 1133 if (!p[0] && !p[1]) {
1131 *in += 2; 1134 *in += 2;
@@ -1141,11 +1144,10 @@ static int asn1_check_eoc(const unsigned char **in, long len)
1141 * header length just read. 1144 * header length just read.
1142 */ 1145 */
1143 1146
1144static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 1147static int
1145 char *inf, char *cst, 1148asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
1146 const unsigned char **in, long len, 1149 char *cst, const unsigned char **in, long len, int exptag, int expclass,
1147 int exptag, int expclass, char opt, 1150 char opt, ASN1_TLC *ctx)
1148 ASN1_TLC *ctx)
1149{ 1151{
1150 int i; 1152 int i;
1151 int ptag, pclass; 1153 int ptag, pclass;
@@ -1170,11 +1172,11 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1170 ctx->hdrlen = p - q; 1172 ctx->hdrlen = p - q;
1171 ctx->valid = 1; 1173 ctx->valid = 1;
1172 /* If definite length, and no error, length + 1174 /* If definite length, and no error, length +
1173 * header can't exceed total amount of data available. 1175 * header can't exceed total amount of data available.
1174 */ 1176 */
1175 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) { 1177 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1176 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 1178 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1177 ASN1_R_TOO_LONG); 1179 ASN1_R_TOO_LONG);
1178 asn1_tlc_clear(ctx); 1180 asn1_tlc_clear(ctx);
1179 return 0; 1181 return 0;
1180 } 1182 }
@@ -1191,7 +1193,8 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1191 /* If type is OPTIONAL, not an error: 1193 /* If type is OPTIONAL, not an error:
1192 * indicate missing type. 1194 * indicate missing type.
1193 */ 1195 */
1194 if (opt) return -1; 1196 if (opt)
1197 return -1;
1195 asn1_tlc_clear(ctx); 1198 asn1_tlc_clear(ctx);
1196 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); 1199 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1197 return 0; 1200 return 0;
@@ -1203,19 +1206,14 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1203 1206
1204 if (i & 1) 1207 if (i & 1)
1205 plen = len - (p - q); 1208 plen = len - (p - q);
1206
1207 if (inf) 1209 if (inf)
1208 *inf = i & 1; 1210 *inf = i & 1;
1209
1210 if (cst) 1211 if (cst)
1211 *cst = i & V_ASN1_CONSTRUCTED; 1212 *cst = i & V_ASN1_CONSTRUCTED;
1212
1213 if (olen) 1213 if (olen)
1214 *olen = plen; 1214 *olen = plen;
1215
1216 if (oclass) 1215 if (oclass)
1217 *oclass = pclass; 1216 *oclass = pclass;
1218
1219 if (otag) 1217 if (otag)
1220 *otag = ptag; 1218 *otag = ptag;
1221 1219
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;
diff --git a/src/lib/libcrypto/asn1/tasn_fre.c b/src/lib/libcrypto/asn1/tasn_fre.c
index 5211276efe..9e6e7579f1 100644
--- a/src/lib/libcrypto/asn1/tasn_fre.c
+++ b/src/lib/libcrypto/asn1/tasn_fre.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
@@ -62,21 +62,25 @@
62#include <openssl/asn1t.h> 62#include <openssl/asn1t.h>
63#include <openssl/objects.h> 63#include <openssl/objects.h>
64 64
65static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine); 65static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
66 int combine);
66 67
67/* Free up an ASN1 structure */ 68/* Free up an ASN1 structure */
68 69
69void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) 70void
71ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
70{ 72{
71 asn1_item_combine_free(&val, it, 0); 73 asn1_item_combine_free(&val, it, 0);
72} 74}
73 75
74void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 76void
77ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
75{ 78{
76 asn1_item_combine_free(pval, it, 0); 79 asn1_item_combine_free(pval, it, 0);
77} 80}
78 81
79static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine) 82static void
83asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
80{ 84{
81 const ASN1_TEMPLATE *tt = NULL, *seqtt; 85 const ASN1_TEMPLATE *tt = NULL, *seqtt;
82 const ASN1_EXTERN_FUNCS *ef; 86 const ASN1_EXTERN_FUNCS *ef;
@@ -84,6 +88,7 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
84 const ASN1_AUX *aux = it->funcs; 88 const ASN1_AUX *aux = it->funcs;
85 ASN1_aux_cb *asn1_cb; 89 ASN1_aux_cb *asn1_cb;
86 int i; 90 int i;
91
87 if (!pval) 92 if (!pval)
88 return; 93 return;
89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) 94 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
@@ -93,8 +98,7 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
93 else 98 else
94 asn1_cb = 0; 99 asn1_cb = 0;
95 100
96 switch(it->itype) { 101 switch (it->itype) {
97
98 case ASN1_ITYPE_PRIMITIVE: 102 case ASN1_ITYPE_PRIMITIVE:
99 if (it->templates) 103 if (it->templates)
100 ASN1_template_free(pval, it->templates); 104 ASN1_template_free(pval, it->templates);
@@ -147,10 +151,10 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
147 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); 151 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
148 if (i == 2) 152 if (i == 2)
149 return; 153 return;
150 } 154 }
151 asn1_enc_free(pval, it); 155 asn1_enc_free(pval, it);
152 /* If we free up as normal we will invalidate any 156 /* If we free up as normal we will invalidate any
153 * ANY DEFINED BY field and we wont be able to 157 * ANY DEFINED BY field and we wont be able to
154 * determine the type of the field it defines. So 158 * determine the type of the field it defines. So
155 * free up in reverse order. 159 * free up in reverse order.
156 */ 160 */
@@ -173,7 +177,8 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
173 } 177 }
174} 178}
175 179
176void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 180void
181ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
177{ 182{
178 int i; 183 int i;
179 if (tt->flags & ASN1_TFLG_SK_MASK) { 184 if (tt->flags & ASN1_TFLG_SK_MASK) {
@@ -182,17 +187,17 @@ void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
182 ASN1_VALUE *vtmp; 187 ASN1_VALUE *vtmp;
183 vtmp = sk_ASN1_VALUE_value(sk, i); 188 vtmp = sk_ASN1_VALUE_value(sk, i);
184 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 189 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
185 0); 190 0);
186 } 191 }
187 sk_ASN1_VALUE_free(sk); 192 sk_ASN1_VALUE_free(sk);
188 *pval = NULL; 193 *pval = NULL;
189 } 194 } else
190 else
191 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item), 195 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
192 tt->flags & ASN1_TFLG_COMBINE); 196 tt->flags & ASN1_TFLG_COMBINE);
193} 197}
194 198
195void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 199void
200ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
196{ 201{
197 int utype; 202 int utype;
198 if (it) { 203 if (it) {
@@ -220,7 +225,7 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
220 return; 225 return;
221 } 226 }
222 227
223 switch(utype) { 228 switch (utype) {
224 case V_ASN1_OBJECT: 229 case V_ASN1_OBJECT:
225 ASN1_OBJECT_free((ASN1_OBJECT *)*pval); 230 ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
226 break; 231 break;
diff --git a/src/lib/libcrypto/asn1/tasn_new.c b/src/lib/libcrypto/asn1/tasn_new.c
index 00aa31dd63..dc9ddc413a 100644
--- a/src/lib/libcrypto/asn1/tasn_new.c
+++ b/src/lib/libcrypto/asn1/tasn_new.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
@@ -65,12 +65,13 @@
65#include <string.h> 65#include <string.h>
66 66
67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68 int combine); 68 int combine);
69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72 72
73ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) 73ASN1_VALUE *
74ASN1_item_new(const ASN1_ITEM *it)
74{ 75{
75 ASN1_VALUE *ret = NULL; 76 ASN1_VALUE *ret = NULL;
76 if (ASN1_item_ex_new(&ret, it) > 0) 77 if (ASN1_item_ex_new(&ret, it) > 0)
@@ -80,13 +81,14 @@ ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
80 81
81/* Allocate an ASN1 structure */ 82/* Allocate an ASN1 structure */
82 83
83int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 84int
85ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84{ 86{
85 return asn1_item_ex_combine_new(pval, it, 0); 87 return asn1_item_ex_combine_new(pval, it, 0);
86} 88}
87 89
88static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 90static int
89 int combine) 91asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
90{ 92{
91 const ASN1_TEMPLATE *tt = NULL; 93 const ASN1_TEMPLATE *tt = NULL;
92 const ASN1_COMPAT_FUNCS *cf; 94 const ASN1_COMPAT_FUNCS *cf;
@@ -95,20 +97,21 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
95 ASN1_aux_cb *asn1_cb; 97 ASN1_aux_cb *asn1_cb;
96 ASN1_VALUE **pseqval; 98 ASN1_VALUE **pseqval;
97 int i; 99 int i;
100
98 if (aux && aux->asn1_cb) 101 if (aux && aux->asn1_cb)
99 asn1_cb = aux->asn1_cb; 102 asn1_cb = aux->asn1_cb;
100 else 103 else
101 asn1_cb = 0; 104 asn1_cb = 0;
102 105
103 if (!combine) *pval = NULL; 106 if (!combine)
107 *pval = NULL;
104 108
105#ifdef CRYPTO_MDEBUG 109#ifdef CRYPTO_MDEBUG
106 if (it->sname) 110 if (it->sname)
107 CRYPTO_push_info(it->sname); 111 CRYPTO_push_info(it->sname);
108#endif 112#endif
109 113
110 switch(it->itype) { 114 switch (it->itype) {
111
112 case ASN1_ITYPE_EXTERN: 115 case ASN1_ITYPE_EXTERN:
113 ef = it->funcs; 116 ef = it->funcs;
114 if (ef && ef->asn1_ex_new) { 117 if (ef && ef->asn1_ex_new) {
@@ -130,14 +133,13 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
130 if (it->templates) { 133 if (it->templates) {
131 if (!ASN1_template_new(pval, it->templates)) 134 if (!ASN1_template_new(pval, it->templates))
132 goto memerr; 135 goto memerr;
133 } 136 } else if (!ASN1_primitive_new(pval, it))
134 else if (!ASN1_primitive_new(pval, it)) 137 goto memerr;
135 goto memerr;
136 break; 138 break;
137 139
138 case ASN1_ITYPE_MSTRING: 140 case ASN1_ITYPE_MSTRING:
139 if (!ASN1_primitive_new(pval, it)) 141 if (!ASN1_primitive_new(pval, it))
140 goto memerr; 142 goto memerr;
141 break; 143 break;
142 144
143 case ASN1_ITYPE_CHOICE: 145 case ASN1_ITYPE_CHOICE:
@@ -145,7 +147,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
145 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); 147 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
146 if (!i) 148 if (!i)
147 goto auxerr; 149 goto auxerr;
148 if (i==2) { 150 if (i == 2) {
149#ifdef CRYPTO_MDEBUG 151#ifdef CRYPTO_MDEBUG
150 if (it->sname) 152 if (it->sname)
151 CRYPTO_pop_info(); 153 CRYPTO_pop_info();
@@ -161,7 +163,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
161 } 163 }
162 asn1_set_choice_selector(pval, -1, it); 164 asn1_set_choice_selector(pval, -1, it);
163 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) 165 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
164 goto auxerr; 166 goto auxerr;
165 break; 167 break;
166 168
167 case ASN1_ITYPE_NDEF_SEQUENCE: 169 case ASN1_ITYPE_NDEF_SEQUENCE:
@@ -170,7 +172,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
170 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); 172 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
171 if (!i) 173 if (!i)
172 goto auxerr; 174 goto auxerr;
173 if (i==2) { 175 if (i == 2) {
174#ifdef CRYPTO_MDEBUG 176#ifdef CRYPTO_MDEBUG
175 if (it->sname) 177 if (it->sname)
176 CRYPTO_pop_info(); 178 CRYPTO_pop_info();
@@ -192,47 +194,50 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
192 goto memerr; 194 goto memerr;
193 } 195 }
194 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) 196 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
195 goto auxerr; 197 goto auxerr;
196 break; 198 break;
197} 199 }
198#ifdef CRYPTO_MDEBUG 200#ifdef CRYPTO_MDEBUG
199 if (it->sname) CRYPTO_pop_info(); 201 if (it->sname)
202 CRYPTO_pop_info();
200#endif 203#endif
201 return 1; 204 return 1;
202 205
203 memerr: 206memerr:
204 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE); 207 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
205#ifdef CRYPTO_MDEBUG 208#ifdef CRYPTO_MDEBUG
206 if (it->sname) CRYPTO_pop_info(); 209 if (it->sname)
210 CRYPTO_pop_info();
207#endif 211#endif
208 return 0; 212 return 0;
209 213
210 auxerr: 214auxerr:
211 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR); 215 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
212 ASN1_item_ex_free(pval, it); 216 ASN1_item_ex_free(pval, it);
213#ifdef CRYPTO_MDEBUG 217#ifdef CRYPTO_MDEBUG
214 if (it->sname) CRYPTO_pop_info(); 218 if (it->sname)
219 CRYPTO_pop_info();
215#endif 220#endif
216 return 0; 221 return 0;
217 222
218} 223}
219 224
220static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 225static void
226asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
221{ 227{
222 const ASN1_EXTERN_FUNCS *ef; 228 const ASN1_EXTERN_FUNCS *ef;
223 229
224 switch(it->itype) { 230 switch (it->itype) {
225
226 case ASN1_ITYPE_EXTERN: 231 case ASN1_ITYPE_EXTERN:
227 ef = it->funcs; 232 ef = it->funcs;
228 if (ef && ef->asn1_ex_clear) 233 if (ef && ef->asn1_ex_clear)
229 ef->asn1_ex_clear(pval, it); 234 ef->asn1_ex_clear(pval, it);
230 else *pval = NULL; 235 else
236 *pval = NULL;
231 break; 237 break;
232 238
233
234 case ASN1_ITYPE_PRIMITIVE: 239 case ASN1_ITYPE_PRIMITIVE:
235 if (it->templates) 240 if (it->templates)
236 asn1_template_clear(pval, it->templates); 241 asn1_template_clear(pval, it->templates);
237 else 242 else
238 asn1_primitive_clear(pval, it); 243 asn1_primitive_clear(pval, it);
@@ -251,11 +256,12 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
251 } 256 }
252} 257}
253 258
254 259int
255int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 260ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
256{ 261{
257 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); 262 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
258 int ret; 263 int ret;
264
259 if (tt->flags & ASN1_TFLG_OPTIONAL) { 265 if (tt->flags & ASN1_TFLG_OPTIONAL) {
260 asn1_template_clear(pval, tt); 266 asn1_template_clear(pval, tt);
261 return 1; 267 return 1;
@@ -285,7 +291,7 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
285 } 291 }
286 /* Otherwise pass it back to the item routine */ 292 /* Otherwise pass it back to the item routine */
287 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE); 293 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
288 done: 294done:
289#ifdef CRYPTO_MDEBUG 295#ifdef CRYPTO_MDEBUG
290 if (it->sname) 296 if (it->sname)
291 CRYPTO_pop_info(); 297 CRYPTO_pop_info();
@@ -293,10 +299,11 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
293 return ret; 299 return ret;
294} 300}
295 301
296static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 302static void
303asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
297{ 304{
298 /* If ADB or STACK just NULL the field */ 305 /* If ADB or STACK just NULL the field */
299 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 306 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
300 *pval = NULL; 307 *pval = NULL;
301 else 308 else
302 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); 309 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
@@ -307,7 +314,8 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
307 * all the old functions. 314 * all the old functions.
308 */ 315 */
309 316
310int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 317int
318ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
311{ 319{
312 ASN1_TYPE *typ; 320 ASN1_TYPE *typ;
313 ASN1_STRING *str; 321 ASN1_STRING *str;
@@ -323,7 +331,7 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
323 utype = -1; 331 utype = -1;
324 else 332 else
325 utype = it->utype; 333 utype = it->utype;
326 switch(utype) { 334 switch (utype) {
327 case V_ASN1_OBJECT: 335 case V_ASN1_OBJECT:
328 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); 336 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
329 return 1; 337 return 1;
@@ -357,14 +365,15 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
357 return 0; 365 return 0;
358} 366}
359 367
360static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 368static void
369asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
361{ 370{
362 int utype; 371 int utype;
363 if (it && it->funcs) { 372 if (it && it->funcs) {
364 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 373 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
365 if (pf->prim_clear) 374 if (pf->prim_clear)
366 pf->prim_clear(pval, it); 375 pf->prim_clear(pval, it);
367 else 376 else
368 *pval = NULL; 377 *pval = NULL;
369 return; 378 return;
370 } 379 }
@@ -374,5 +383,6 @@ static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
374 utype = it->utype; 383 utype = it->utype;
375 if (utype == V_ASN1_BOOLEAN) 384 if (utype == V_ASN1_BOOLEAN)
376 *(ASN1_BOOLEAN *)pval = it->size; 385 *(ASN1_BOOLEAN *)pval = it->size;
377 else *pval = NULL; 386 else
387 *pval = NULL;
378} 388}
diff --git a/src/lib/libcrypto/asn1/tasn_prn.c b/src/lib/libcrypto/asn1/tasn_prn.c
index 345daeb0f3..13624991c4 100644
--- a/src/lib/libcrypto/asn1/tasn_prn.c
+++ b/src/lib/libcrypto/asn1/tasn_prn.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 "cryptlib.h" 60#include "cryptlib.h"
62#include <openssl/asn1.h> 61#include <openssl/asn1.h>
@@ -72,17 +71,17 @@
72 71
73/* ASN1_PCTX routines */ 72/* ASN1_PCTX routines */
74 73
75ASN1_PCTX default_pctx = 74ASN1_PCTX default_pctx = {
76{
77 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */ 75 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
78 0, /* nm_flags */ 76 0, /* nm_flags */
79 0, /* cert_flags */ 77 0, /* cert_flags */
80 0, /* oid_flags */ 78 0, /* oid_flags */
81 0 /* str_flags */ 79 0 /* str_flags */
82}; 80};
83
84 81
85ASN1_PCTX *ASN1_PCTX_new(void) 82
83ASN1_PCTX *
84ASN1_PCTX_new(void)
86{ 85{
87 ASN1_PCTX *ret; 86 ASN1_PCTX *ret;
88 ret = malloc(sizeof(ASN1_PCTX)); 87 ret = malloc(sizeof(ASN1_PCTX));
@@ -98,57 +97,68 @@ ASN1_PCTX *ASN1_PCTX_new(void)
98 return ret; 97 return ret;
99} 98}
100 99
101void ASN1_PCTX_free(ASN1_PCTX *p) 100void
101ASN1_PCTX_free(ASN1_PCTX *p)
102{ 102{
103 free(p); 103 free(p);
104} 104}
105 105
106unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p) 106unsigned long
107ASN1_PCTX_get_flags(ASN1_PCTX *p)
107{ 108{
108 return p->flags; 109 return p->flags;
109} 110}
110 111
111void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags) 112void
113ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
112{ 114{
113 p->flags = flags; 115 p->flags = flags;
114} 116}
115 117
116unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p) 118unsigned long
119ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
117{ 120{
118 return p->nm_flags; 121 return p->nm_flags;
119} 122}
120 123
121void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags) 124void
125ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
122{ 126{
123 p->nm_flags = flags; 127 p->nm_flags = flags;
124} 128}
125 129
126unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p) 130unsigned long
131ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
127{ 132{
128 return p->cert_flags; 133 return p->cert_flags;
129} 134}
130 135
131void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags) 136void
137ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
132{ 138{
133 p->cert_flags = flags; 139 p->cert_flags = flags;
134} 140}
135 141
136unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p) 142unsigned long
143ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
137{ 144{
138 return p->oid_flags; 145 return p->oid_flags;
139} 146}
140 147
141void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags) 148void
149ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
142{ 150{
143 p->oid_flags = flags; 151 p->oid_flags = flags;
144} 152}
145 153
146unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p) 154unsigned long
155ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
147{ 156{
148 return p->str_flags; 157 return p->str_flags;
149} 158}
150 159
151void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags) 160void
161ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
152{ 162{
153 p->str_flags = flags; 163 p->str_flags = flags;
154} 164}
@@ -156,40 +166,38 @@ void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
156/* Main print routines */ 166/* Main print routines */
157 167
158static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 168static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
159 const ASN1_ITEM *it, 169 const ASN1_ITEM *it, const char *fname, const char *sname, int nohdr,
160 const char *fname, const char *sname, 170 const ASN1_PCTX *pctx);
161 int nohdr, const ASN1_PCTX *pctx);
162 171
163int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 172int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
164 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx); 173 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
165 174
166static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld, 175static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
167 const ASN1_ITEM *it, int indent, 176 const ASN1_ITEM *it, int indent, const char *fname, const char *sname,
168 const char *fname, const char *sname, 177 const ASN1_PCTX *pctx);
169 const ASN1_PCTX *pctx);
170 178
171static int asn1_print_fsname(BIO *out, int indent, 179static int asn1_print_fsname(BIO *out, int indent, const char *fname,
172 const char *fname, const char *sname, 180 const char *sname, const ASN1_PCTX *pctx);
173 const ASN1_PCTX *pctx);
174 181
175int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, 182int
176 const ASN1_ITEM *it, const ASN1_PCTX *pctx) 183ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, const ASN1_ITEM *it,
184 const ASN1_PCTX *pctx)
177{ 185{
178 const char *sname; 186 const char *sname;
187
179 if (pctx == NULL) 188 if (pctx == NULL)
180 pctx = &default_pctx; 189 pctx = &default_pctx;
181 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME) 190 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
182 sname = NULL; 191 sname = NULL;
183 else 192 else
184 sname = it->sname; 193 sname = it->sname;
185 return asn1_item_print_ctx(out, &ifld, indent, it, 194 return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname,
186 NULL, sname, 0, pctx); 195 0, pctx);
187} 196}
188 197
189static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 198static int
190 const ASN1_ITEM *it, 199asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, const ASN1_ITEM *it,
191 const char *fname, const char *sname, 200 const char *fname, const char *sname, int nohdr, const ASN1_PCTX *pctx)
192 int nohdr, const ASN1_PCTX *pctx)
193{ 201{
194 const ASN1_TEMPLATE *tt; 202 const ASN1_TEMPLATE *tt;
195 const ASN1_EXTERN_FUNCS *ef; 203 const ASN1_EXTERN_FUNCS *ef;
@@ -198,18 +206,19 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
198 ASN1_aux_cb *asn1_cb; 206 ASN1_aux_cb *asn1_cb;
199 ASN1_PRINT_ARG parg; 207 ASN1_PRINT_ARG parg;
200 int i; 208 int i;
209
201 if (aux && aux->asn1_cb) { 210 if (aux && aux->asn1_cb) {
202 parg.out = out; 211 parg.out = out;
203 parg.indent = indent; 212 parg.indent = indent;
204 parg.pctx = pctx; 213 parg.pctx = pctx;
205 asn1_cb = aux->asn1_cb; 214 asn1_cb = aux->asn1_cb;
206 } 215 } else
207 else asn1_cb = 0; 216 asn1_cb = 0;
208 217
209 if(*fld == NULL) { 218 if (*fld == NULL) {
210 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) { 219 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
211 if (!nohdr && !asn1_print_fsname(out, indent, 220 if (!nohdr &&
212 fname, sname, pctx)) 221 !asn1_print_fsname(out, indent, fname, sname, pctx))
213 return 0; 222 return 0;
214 if (BIO_puts(out, "<ABSENT>\n") <= 0) 223 if (BIO_puts(out, "<ABSENT>\n") <= 0)
215 return 0; 224 return 0;
@@ -217,22 +226,23 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
217 return 1; 226 return 1;
218 } 227 }
219 228
220 switch(it->itype) { 229 switch (it->itype) {
221 case ASN1_ITYPE_PRIMITIVE: 230 case ASN1_ITYPE_PRIMITIVE:
222 if(it->templates) { 231 if (it->templates) {
223 if (!asn1_template_print_ctx(out, fld, indent, 232 if (!asn1_template_print_ctx(out, fld, indent,
224 it->templates, pctx)) 233 it->templates, pctx))
225 return 0; 234 return 0;
226 } 235 }
227 /* fall thru */ 236 /* fall thru */
228 case ASN1_ITYPE_MSTRING: 237 case ASN1_ITYPE_MSTRING:
229 if (!asn1_primitive_print(out, fld, it, 238 if (!asn1_primitive_print(out, fld, it,
230 indent, fname, sname,pctx)) 239 indent, fname, sname, pctx))
231 return 0; 240 return 0;
232 break; 241 break;
233 242
234 case ASN1_ITYPE_EXTERN: 243 case ASN1_ITYPE_EXTERN:
235 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 244 if (!nohdr &&
245 !asn1_print_fsname(out, indent, fname, sname, pctx))
236 return 0; 246 return 0;
237 /* Use new style print routine if possible */ 247 /* Use new style print routine if possible */
238 ef = it->funcs; 248 ef = it->funcs;
@@ -243,23 +253,23 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
243 if ((i == 2) && (BIO_puts(out, "\n") <= 0)) 253 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
244 return 0; 254 return 0;
245 return 1; 255 return 1;
246 } 256 } else if (sname &&
247 else if (sname && 257 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
248 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
249 return 0; 258 return 0;
250 break; 259 break;
251 260
252 case ASN1_ITYPE_CHOICE: 261 case ASN1_ITYPE_CHOICE:
253#if 0 262#if 0
254 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 263 if (!nohdr &&
264 !asn1_print_fsname(out, indent, fname, sname, pctx))
255 return 0; 265 return 0;
256#endif 266#endif
257 /* CHOICE type, get selector */ 267 /* CHOICE type, get selector */
258 i = asn1_get_choice_selector(fld, it); 268 i = asn1_get_choice_selector(fld, it);
259 /* This should never happen... */ 269 /* This should never happen... */
260 if((i < 0) || (i >= it->tcount)) { 270 if ((i < 0) || (i >= it->tcount)) {
261 if (BIO_printf(out, 271 if (BIO_printf(out,
262 "ERROR: selector [%d] invalid\n", i) <= 0) 272 "ERROR: selector [%d] invalid\n", i) <= 0)
263 return 0; 273 return 0;
264 return 1; 274 return 1;
265 } 275 }
@@ -271,7 +281,8 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
271 281
272 case ASN1_ITYPE_SEQUENCE: 282 case ASN1_ITYPE_SEQUENCE:
273 case ASN1_ITYPE_NDEF_SEQUENCE: 283 case ASN1_ITYPE_NDEF_SEQUENCE:
274 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 284 if (!nohdr &&
285 !asn1_print_fsname(out, indent, fname, sname, pctx))
275 return 0; 286 return 0;
276 if (fname || sname) { 287 if (fname || sname) {
277 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 288 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
@@ -292,12 +303,12 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
292 } 303 }
293 304
294 /* Print each field entry */ 305 /* Print each field entry */
295 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 306 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
296 const ASN1_TEMPLATE *seqtt; 307 const ASN1_TEMPLATE *seqtt;
297 seqtt = asn1_do_adb(fld, tt, 1); 308 seqtt = asn1_do_adb(fld, tt, 1);
298 tmpfld = asn1_get_field_ptr(fld, seqtt); 309 tmpfld = asn1_get_field_ptr(fld, seqtt);
299 if (!asn1_template_print_ctx(out, tmpfld, 310 if (!asn1_template_print_ctx(out, tmpfld, indent + 2,
300 indent + 2, seqtt, pctx)) 311 seqtt, pctx))
301 return 0; 312 return 0;
302 } 313 }
303 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 314 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
@@ -312,7 +323,7 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
312 } 323 }
313 break; 324 break;
314 325
315 default: 326 default:
316 BIO_printf(out, "Unprocessed type %d\n", it->itype); 327 BIO_printf(out, "Unprocessed type %d\n", it->itype);
317 return 0; 328 return 0;
318 } 329 }
@@ -320,65 +331,65 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
320 return 1; 331 return 1;
321} 332}
322 333
323int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 334int
324 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx) 335asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
336 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
325{ 337{
326 int i, flags; 338 int i, flags;
327 const char *sname, *fname; 339 const char *sname, *fname;
340
328 flags = tt->flags; 341 flags = tt->flags;
329 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME) 342 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
330 sname = ASN1_ITEM_ptr(tt->item)->sname; 343 sname = ASN1_ITEM_ptr(tt->item)->sname;
331 else 344 else
332 sname = NULL; 345 sname = NULL;
333 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME) 346 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
334 fname = NULL; 347 fname = NULL;
335 else 348 else
336 fname = tt->field_name; 349 fname = tt->field_name;
337 if(flags & ASN1_TFLG_SK_MASK) { 350 if (flags & ASN1_TFLG_SK_MASK) {
338 char *tname; 351 char *tname;
339 ASN1_VALUE *skitem; 352 ASN1_VALUE *skitem;
340 STACK_OF(ASN1_VALUE) *stack; 353 STACK_OF(ASN1_VALUE) *stack;
341 354
342 /* SET OF, SEQUENCE OF */ 355 /* SET OF, SEQUENCE OF */
343 if (fname) { 356 if (fname) {
344 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) { 357 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
345 if(flags & ASN1_TFLG_SET_OF) 358 if (flags & ASN1_TFLG_SET_OF)
346 tname = "SET"; 359 tname = "SET";
347 else 360 else
348 tname = "SEQUENCE"; 361 tname = "SEQUENCE";
349 if (BIO_printf(out, "%*s%s OF %s {\n", 362 if (BIO_printf(out, "%*s%s OF %s {\n",
350 indent, "", tname, tt->field_name) <= 0) 363 indent, "", tname, tt->field_name) <= 0)
351 return 0; 364 return 0;
352 } 365 } else if (BIO_printf(out, "%*s%s:\n", indent, "",
353 else if (BIO_printf(out, "%*s%s:\n", indent, "", 366 fname) <= 0)
354 fname) <= 0)
355 return 0; 367 return 0;
356 } 368 }
357 stack = (STACK_OF(ASN1_VALUE) *)*fld; 369 stack = (STACK_OF(ASN1_VALUE) *)*fld;
358 for(i = 0; i < sk_ASN1_VALUE_num(stack); i++) { 370 for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
359 if ((i > 0) && (BIO_puts(out, "\n") <= 0)) 371 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
360 return 0; 372 return 0;
361
362 skitem = sk_ASN1_VALUE_value(stack, i); 373 skitem = sk_ASN1_VALUE_value(stack, i);
363 if (!asn1_item_print_ctx(out, &skitem, indent + 2, 374 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
364 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx)) 375 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
365 return 0; 376 return 0;
366 } 377 }
367 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0) 378 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
368 return 0; 379 return 0;
369 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 380 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
370 if (BIO_printf(out, "%*s}\n", indent, "") <= 0) 381 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
371 return 0; 382 return 0;
372 } 383 }
373 return 1; 384 return 1;
374 } 385 }
375 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item), 386 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
376 fname, sname, 0, pctx); 387 fname, sname, 0, pctx);
377} 388}
378 389
379static int asn1_print_fsname(BIO *out, int indent, 390static int
380 const char *fname, const char *sname, 391asn1_print_fsname(BIO *out, int indent, const char *fname, const char *sname,
381 const ASN1_PCTX *pctx) 392 const ASN1_PCTX *pctx)
382{ 393{
383 static char spaces[] = " "; 394 static char spaces[] = " ";
384 const int nspaces = sizeof(spaces) - 1; 395 const int nspaces = sizeof(spaces) - 1;
@@ -419,8 +430,8 @@ static int asn1_print_fsname(BIO *out, int indent,
419 return 1; 430 return 1;
420} 431}
421 432
422static int asn1_print_boolean_ctx(BIO *out, int boolval, 433static int
423 const ASN1_PCTX *pctx) 434asn1_print_boolean_ctx(BIO *out, int boolval, const ASN1_PCTX *pctx)
424{ 435{
425 const char *str; 436 const char *str;
426 switch (boolval) { 437 switch (boolval) {
@@ -432,7 +443,7 @@ static int asn1_print_boolean_ctx(BIO *out, int boolval,
432 str = "FALSE"; 443 str = "FALSE";
433 break; 444 break;
434 445
435 default: 446 default:
436 str = "TRUE"; 447 str = "TRUE";
437 break; 448 break;
438 449
@@ -444,8 +455,8 @@ static int asn1_print_boolean_ctx(BIO *out, int boolval,
444 455
445} 456}
446 457
447static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, 458static int
448 const ASN1_PCTX *pctx) 459asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, const ASN1_PCTX *pctx)
449{ 460{
450 char *s; 461 char *s;
451 int ret = 1; 462 int ret = 1;
@@ -456,13 +467,13 @@ static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
456 return ret; 467 return ret;
457} 468}
458 469
459static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, 470static int
460 const ASN1_PCTX *pctx) 471asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, const ASN1_PCTX *pctx)
461{ 472{
462 char objbuf[80]; 473 char objbuf[80];
463 const char *ln; 474 const char *ln;
464 ln = OBJ_nid2ln(OBJ_obj2nid(oid)); 475 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
465 if(!ln) 476 if (!ln)
466 ln = ""; 477 ln = "";
467 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1); 478 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
468 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0) 479 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
@@ -470,36 +481,36 @@ static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
470 return 1; 481 return 1;
471} 482}
472 483
473static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent, 484static int
474 const ASN1_PCTX *pctx) 485asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
486 const ASN1_PCTX *pctx)
475{ 487{
476 if (str->type == V_ASN1_BIT_STRING) { 488 if (str->type == V_ASN1_BIT_STRING) {
477 if (BIO_printf(out, " (%ld unused bits)\n", 489 if (BIO_printf(out, " (%ld unused bits)\n",
478 str->flags & 0x7) <= 0) 490 str->flags & 0x7) <= 0)
479 return 0; 491 return 0;
480 } 492 } else if (BIO_puts(out, "\n") <= 0)
481 else if (BIO_puts(out, "\n") <= 0)
482 return 0; 493 return 0;
483 if ((str->length > 0) 494 if ((str->length > 0) &&
484 && BIO_dump_indent(out, (char *)str->data, str->length, 495 BIO_dump_indent(out, (char *)str->data, str->length,
485 indent + 2) <= 0) 496 indent + 2) <= 0)
486 return 0; 497 return 0;
487 return 1; 498 return 1;
488} 499}
489 500
490static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld, 501static int
491 const ASN1_ITEM *it, int indent, 502asn1_primitive_print(BIO *out, ASN1_VALUE **fld, const ASN1_ITEM *it,
492 const char *fname, const char *sname, 503 int indent, const char *fname, const char *sname, const ASN1_PCTX *pctx)
493 const ASN1_PCTX *pctx)
494{ 504{
495 long utype; 505 long utype;
496 ASN1_STRING *str; 506 ASN1_STRING *str;
497 int ret = 1, needlf = 1; 507 int ret = 1, needlf = 1;
498 const char *pname; 508 const char *pname;
499 const ASN1_PRIMITIVE_FUNCS *pf; 509 const ASN1_PRIMITIVE_FUNCS *pf;
510
500 pf = it->funcs; 511 pf = it->funcs;
501 if (!asn1_print_fsname(out, indent, fname, sname, pctx)) 512 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
502 return 0; 513 return 0;
503 if (pf && pf->prim_print) 514 if (pf && pf->prim_print)
504 return pf->prim_print(out, fld, it, indent, pctx); 515 return pf->prim_print(out, fld, it, indent, pctx);
505 str = (ASN1_STRING *)*fld; 516 str = (ASN1_STRING *)*fld;
@@ -514,12 +525,12 @@ static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
514 str = (ASN1_STRING *)*fld; 525 str = (ASN1_STRING *)*fld;
515 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE) 526 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
516 pname = NULL; 527 pname = NULL;
517 else 528 else
518 pname = ASN1_tag2str(utype); 529 pname = ASN1_tag2str(utype);
519 } else { 530 } else {
520 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE) 531 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
521 pname = ASN1_tag2str(utype); 532 pname = ASN1_tag2str(utype);
522 else 533 else
523 pname = NULL; 534 pname = NULL;
524 } 535 }
525 536
@@ -575,14 +586,13 @@ static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
575 if (BIO_puts(out, "\n") <= 0) 586 if (BIO_puts(out, "\n") <= 0)
576 return 0; 587 return 0;
577 if (ASN1_parse_dump(out, str->data, str->length, 588 if (ASN1_parse_dump(out, str->data, str->length,
578 indent, 0) <= 0) 589 indent, 0) <= 0)
579 ret = 0; 590 ret = 0;
580 needlf = 0; 591 needlf = 0;
581 break; 592 break;
582 593
583 default: 594 default:
584 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags); 595 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
585
586 } 596 }
587 if (!ret) 597 if (!ret)
588 return 0; 598 return 0;
diff --git a/src/lib/libcrypto/asn1/tasn_typ.c b/src/lib/libcrypto/asn1/tasn_typ.c
index 6fb1c372da..3399e74d84 100644
--- a/src/lib/libcrypto/asn1/tasn_typ.c
+++ b/src/lib/libcrypto/asn1/tasn_typ.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
@@ -136,12 +136,12 @@ IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
136 136
137IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF) 137IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF)
138 138
139ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) = 139ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
140 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY) 140 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
141ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY) 141ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
142 142
143ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) = 143ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) =
144 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY) 144 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY)
145ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY) 145ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY)
146 146
147IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY) 147IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
diff --git a/src/lib/libcrypto/asn1/tasn_utl.c b/src/lib/libcrypto/asn1/tasn_utl.c
index 9715dac931..e1051c2c3e 100644
--- a/src/lib/libcrypto/asn1/tasn_utl.c
+++ b/src/lib/libcrypto/asn1/tasn_utl.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 <openssl/asn1.h> 61#include <openssl/asn1.h>
@@ -73,7 +72,8 @@
73 * the selector value 72 * the selector value
74 */ 73 */
75 74
76int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) 75int
76asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
77{ 77{
78 int *sel = offset2ptr(*pval, it->utype); 78 int *sel = offset2ptr(*pval, it->utype);
79 return *sel; 79 return *sel;
@@ -83,8 +83,9 @@ int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
83 * the selector value, return old value. 83 * the selector value, return old value.
84 */ 84 */
85 85
86int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) 86int
87{ 87asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
88{
88 int *sel, ret; 89 int *sel, ret;
89 sel = offset2ptr(*pval, it->utype); 90 sel = offset2ptr(*pval, it->utype);
90 ret = *sel; 91 ret = *sel;
@@ -92,18 +93,20 @@ int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
92 return ret; 93 return ret;
93} 94}
94 95
95/* Do reference counting. The value 'op' decides what to do. 96/* Do reference counting. The value 'op' decides what to do.
96 * if it is +1 then the count is incremented. If op is 0 count is 97 * if it is +1 then the count is incremented. If op is 0 count is
97 * set to 1. If op is -1 count is decremented and the return value 98 * set to 1. If op is -1 count is decremented and the return value
98 * is the current refrence count or 0 if no reference count exists. 99 * is the current refrence count or 0 if no reference count exists.
99 */ 100 */
100 101
101int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 102int
103asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
102{ 104{
103 const ASN1_AUX *aux; 105 const ASN1_AUX *aux;
104 int *lck, ret; 106 int *lck, ret;
105 if ((it->itype != ASN1_ITYPE_SEQUENCE) 107
106 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) 108 if ((it->itype != ASN1_ITYPE_SEQUENCE) &&
109 (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
107 return 0; 110 return 0;
108 aux = it->funcs; 111 aux = it->funcs;
109 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) 112 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
@@ -117,9 +120,11 @@ int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
117 return ret; 120 return ret;
118} 121}
119 122
120static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) 123static ASN1_ENCODING *
124asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
121{ 125{
122 const ASN1_AUX *aux; 126 const ASN1_AUX *aux;
127
123 if (!pval || !*pval) 128 if (!pval || !*pval)
124 return NULL; 129 return NULL;
125 aux = it->funcs; 130 aux = it->funcs;
@@ -128,9 +133,11 @@ static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
128 return offset2ptr(*pval, aux->enc_offset); 133 return offset2ptr(*pval, aux->enc_offset);
129} 134}
130 135
131void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) 136void
137asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
132{ 138{
133 ASN1_ENCODING *enc; 139 ASN1_ENCODING *enc;
140
134 enc = asn1_get_enc_ptr(pval, it); 141 enc = asn1_get_enc_ptr(pval, it);
135 if (enc) { 142 if (enc) {
136 enc->enc = NULL; 143 enc->enc = NULL;
@@ -139,9 +146,11 @@ void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
139 } 146 }
140} 147}
141 148
142void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 149void
150asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
143{ 151{
144 ASN1_ENCODING *enc; 152 ASN1_ENCODING *enc;
153
145 enc = asn1_get_enc_ptr(pval, it); 154 enc = asn1_get_enc_ptr(pval, it);
146 if (enc) { 155 if (enc) {
147 if (enc->enc) 156 if (enc->enc)
@@ -152,10 +161,12 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
152 } 161 }
153} 162}
154 163
155int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 164int
156 const ASN1_ITEM *it) 165asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
166 const ASN1_ITEM *it)
157{ 167{
158 ASN1_ENCODING *enc; 168 ASN1_ENCODING *enc;
169
159 enc = asn1_get_enc_ptr(pval, it); 170 enc = asn1_get_enc_ptr(pval, it);
160 if (!enc) 171 if (!enc)
161 return 1; 172 return 1;
@@ -171,11 +182,13 @@ int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
171 182
172 return 1; 183 return 1;
173} 184}
174 185
175int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 186int
176 const ASN1_ITEM *it) 187asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
188 const ASN1_ITEM *it)
177{ 189{
178 ASN1_ENCODING *enc; 190 ASN1_ENCODING *enc;
191
179 enc = asn1_get_enc_ptr(pval, it); 192 enc = asn1_get_enc_ptr(pval, it);
180 if (!enc || enc->modified) 193 if (!enc || enc->modified)
181 return 0; 194 return 0;
@@ -189,9 +202,11 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
189} 202}
190 203
191/* Given an ASN1_TEMPLATE get a pointer to a field */ 204/* Given an ASN1_TEMPLATE get a pointer to a field */
192ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 205ASN1_VALUE **
206asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
193{ 207{
194 ASN1_VALUE **pvaltmp; 208 ASN1_VALUE **pvaltmp;
209
195 if (tt->flags & ASN1_TFLG_COMBINE) 210 if (tt->flags & ASN1_TFLG_COMBINE)
196 return pval; 211 return pval;
197 pvaltmp = offset2ptr(*pval, tt->offset); 212 pvaltmp = offset2ptr(*pval, tt->offset);
@@ -206,14 +221,15 @@ ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
206 * the relevant ASN1_TEMPLATE in the table and return it. 221 * the relevant ASN1_TEMPLATE in the table and return it.
207 */ 222 */
208 223
209const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 224const ASN1_TEMPLATE *
210 int nullerr) 225asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr)
211{ 226{
212 const ASN1_ADB *adb; 227 const ASN1_ADB *adb;
213 const ASN1_ADB_TABLE *atbl; 228 const ASN1_ADB_TABLE *atbl;
214 long selector; 229 long selector;
215 ASN1_VALUE **sfld; 230 ASN1_VALUE **sfld;
216 int i; 231 int i;
232
217 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) 233 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
218 return tt; 234 return tt;
219 235
@@ -234,9 +250,9 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
234 * NB: don't check for NID_undef here because it 250 * NB: don't check for NID_undef here because it
235 * might be a legitimate value in the table 251 * might be a legitimate value in the table
236 */ 252 */
237 if (tt->flags & ASN1_TFLG_ADB_OID) 253 if (tt->flags & ASN1_TFLG_ADB_OID)
238 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 254 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
239 else 255 else
240 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 256 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
241 257
242 /* Try to find matching entry in table 258 /* Try to find matching entry in table
@@ -255,13 +271,13 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
255 271
256 /* No match, return default type */ 272 /* No match, return default type */
257 if (!adb->default_tt) 273 if (!adb->default_tt)
258 goto err; 274 goto err;
259 return adb->default_tt; 275 return adb->default_tt;
260 276
261 err: 277err:
262 /* FIXME: should log the value or OID of unsupported type */ 278 /* FIXME: should log the value or OID of unsupported type */
263 if (nullerr) 279 if (nullerr)
264 ASN1err(ASN1_F_ASN1_DO_ADB, 280 ASN1err(ASN1_F_ASN1_DO_ADB,
265 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 281 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
266 return NULL; 282 return NULL;
267} 283}
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_dec.c b/src/lib/libssl/src/crypto/asn1/tasn_dec.c
index 1a06ddc185..e9059df982 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_dec.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_dec.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
@@ -69,45 +69,40 @@ static int asn1_check_eoc(const unsigned char **in, long len);
69static int asn1_find_end(const unsigned char **in, long len, char inf); 69static int asn1_find_end(const unsigned char **in, long len, char inf);
70 70
71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 71static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
72 char inf, int tag, int aclass, int depth); 72 char inf, int tag, int aclass, int depth);
73 73
74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen); 74static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen);
75 75
76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 76static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
77 char *inf, char *cst, 77 char *inf, char *cst, const unsigned char **in, long len, int exptag,
78 const unsigned char **in, long len, 78 int expclass, char opt, ASN1_TLC *ctx);
79 int exptag, int expclass, char opt, 79
80 ASN1_TLC *ctx); 80static int asn1_template_ex_d2i(ASN1_VALUE **pval, const unsigned char **in,
81 81 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
82static int asn1_template_ex_d2i(ASN1_VALUE **pval, 82static int asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in,
83 const unsigned char **in, long len, 83 long len, const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx);
84 const ASN1_TEMPLATE *tt, char opt, 84static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in,
85 ASN1_TLC *ctx); 85 long len, const ASN1_ITEM *it, int tag, int aclass, char opt,
86static int asn1_template_noexp_d2i(ASN1_VALUE **val, 86 ASN1_TLC *ctx);
87 const unsigned char **in, long len,
88 const ASN1_TEMPLATE *tt, char opt,
89 ASN1_TLC *ctx);
90static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
91 const unsigned char **in, long len,
92 const ASN1_ITEM *it,
93 int tag, int aclass, char opt, ASN1_TLC *ctx);
94 87
95/* Table to convert tags to bit values, used for MSTRING type */ 88/* Table to convert tags to bit values, used for MSTRING type */
96static const unsigned long tag2bit[32] = { 89static const unsigned long tag2bit[32] = {
970, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */ 90 0, 0, 0, B_ASN1_BIT_STRING, /* tags 0 - 3 */
98B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */ 91 B_ASN1_OCTET_STRING, 0, 0, B_ASN1_UNKNOWN,/* tags 4- 7 */
99B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */ 92 B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN, B_ASN1_UNKNOWN,/* tags 8-11 */
100B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */ 93 B_ASN1_UTF8STRING,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,B_ASN1_UNKNOWN,/* tags 12-15 */
101B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */ 94 B_ASN1_SEQUENCE,0,B_ASN1_NUMERICSTRING,B_ASN1_PRINTABLESTRING, /* tags 16-19 */
102B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */ 95 B_ASN1_T61STRING,B_ASN1_VIDEOTEXSTRING,B_ASN1_IA5STRING, /* tags 20-22 */
103B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */ 96 B_ASN1_UTCTIME, B_ASN1_GENERALIZEDTIME, /* tags 23-24 */
104B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */ 97 B_ASN1_GRAPHICSTRING,B_ASN1_ISO64STRING,B_ASN1_GENERALSTRING, /* tags 25-27 */
105B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */ 98 B_ASN1_UNIVERSALSTRING,B_ASN1_UNKNOWN,B_ASN1_BMPSTRING,B_ASN1_UNKNOWN, /* tags 28-31 */
106}; 99};
107 100
108unsigned long ASN1_tag2bit(int tag) 101unsigned long
102ASN1_tag2bit(int tag)
109{ 103{
110 if ((tag < 0) || (tag > 30)) return 0; 104 if ((tag < 0) || (tag > 30))
105 return 0;
111 return tag2bit[tag]; 106 return tag2bit[tag];
112} 107}
113 108
@@ -117,29 +112,31 @@ unsigned long ASN1_tag2bit(int tag)
117/* Version to avoid compiler warning about 'c' always non-NULL */ 112/* Version to avoid compiler warning about 'c' always non-NULL */
118#define asn1_tlc_clear_nc(c) (c)->valid = 0 113#define asn1_tlc_clear_nc(c) (c)->valid = 0
119 114
120/* Decode an ASN1 item, this currently behaves just 115/* Decode an ASN1 item, this currently behaves just
121 * like a standard 'd2i' function. 'in' points to 116 * like a standard 'd2i' function. 'in' points to
122 * a buffer to read the data from, in future we will 117 * a buffer to read the data from, in future we will
123 * have more advanced versions that can input data 118 * have more advanced versions that can input data
124 * a piece at a time and this will simply be a special 119 * a piece at a time and this will simply be a special
125 * case. 120 * case.
126 */ 121 */
127 122
128ASN1_VALUE *ASN1_item_d2i(ASN1_VALUE **pval, 123ASN1_VALUE *
129 const unsigned char **in, long len, const ASN1_ITEM *it) 124ASN1_item_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
125 const ASN1_ITEM *it)
130{ 126{
131 ASN1_TLC c; 127 ASN1_TLC c;
132 ASN1_VALUE *ptmpval = NULL; 128 ASN1_VALUE *ptmpval = NULL;
133 if (!pval) 129 if (!pval)
134 pval = &ptmpval; 130 pval = &ptmpval;
135 asn1_tlc_clear_nc(&c); 131 asn1_tlc_clear_nc(&c);
136 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0) 132 if (ASN1_item_ex_d2i(pval, in, len, it, -1, 0, 0, &c) > 0)
137 return *pval; 133 return *pval;
138 return NULL; 134 return NULL;
139} 135}
140 136
141int ASN1_template_d2i(ASN1_VALUE **pval, 137int
142 const unsigned char **in, long len, const ASN1_TEMPLATE *tt) 138ASN1_template_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
139 const ASN1_TEMPLATE *tt)
143{ 140{
144 ASN1_TLC c; 141 ASN1_TLC c;
145 asn1_tlc_clear_nc(&c); 142 asn1_tlc_clear_nc(&c);
@@ -151,9 +148,9 @@ int ASN1_template_d2i(ASN1_VALUE **pval,
151 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL 148 * If 'opt' set and tag mismatch return -1 to handle OPTIONAL
152 */ 149 */
153 150
154int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len, 151int
155 const ASN1_ITEM *it, 152ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
156 int tag, int aclass, char opt, ASN1_TLC *ctx) 153 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
157{ 154{
158 const ASN1_TEMPLATE *tt, *errtt = NULL; 155 const ASN1_TEMPLATE *tt, *errtt = NULL;
159 const ASN1_COMPAT_FUNCS *cf; 156 const ASN1_COMPAT_FUNCS *cf;
@@ -161,7 +158,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
161 const ASN1_AUX *aux = it->funcs; 158 const ASN1_AUX *aux = it->funcs;
162 ASN1_aux_cb *asn1_cb; 159 ASN1_aux_cb *asn1_cb;
163 const unsigned char *p = NULL, *q; 160 const unsigned char *p = NULL, *q;
164 unsigned char *wp=NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */ 161 unsigned char *wp = NULL; /* BIG FAT WARNING! BREAKS CONST WHERE USED */
165 unsigned char imphack = 0, oclass; 162 unsigned char imphack = 0, oclass;
166 char seq_eoc, seq_nolen, cst, isopt; 163 char seq_eoc, seq_nolen, cst, isopt;
167 long tmplen; 164 long tmplen;
@@ -173,9 +170,10 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
173 return 0; 170 return 0;
174 if (aux && aux->asn1_cb) 171 if (aux && aux->asn1_cb)
175 asn1_cb = aux->asn1_cb; 172 asn1_cb = aux->asn1_cb;
176 else asn1_cb = 0; 173 else
174 asn1_cb = 0;
177 175
178 switch(it->itype) { 176 switch (it->itype) {
179 case ASN1_ITYPE_PRIMITIVE: 177 case ASN1_ITYPE_PRIMITIVE:
180 if (it->templates) { 178 if (it->templates) {
181 /* tagging or OPTIONAL is currently illegal on an item 179 /* tagging or OPTIONAL is currently illegal on an item
@@ -186,33 +184,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
186 */ 184 */
187 if ((tag != -1) || opt) { 185 if ((tag != -1) || opt) {
188 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 186 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
189 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE); 187 ASN1_R_ILLEGAL_OPTIONS_ON_ITEM_TEMPLATE);
190 goto err; 188 goto err;
191 } 189 }
192 return asn1_template_ex_d2i(pval, in, len, 190 return asn1_template_ex_d2i(pval, in, len,
193 it->templates, opt, ctx); 191 it->templates, opt, ctx);
194 } 192 }
195 return asn1_d2i_ex_primitive(pval, in, len, it, 193 return asn1_d2i_ex_primitive(pval, in, len, it,
196 tag, aclass, opt, ctx); 194 tag, aclass, opt, ctx);
197 break; 195 break;
198 196
199 case ASN1_ITYPE_MSTRING: 197 case ASN1_ITYPE_MSTRING:
200 p = *in; 198 p = *in;
201 /* Just read in tag and class */ 199 /* Just read in tag and class */
202 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL, 200 ret = asn1_check_tlen(NULL, &otag, &oclass, NULL, NULL,
203 &p, len, -1, 0, 1, ctx); 201 &p, len, -1, 0, 1, ctx);
204 if (!ret) { 202 if (!ret) {
205 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 203 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
206 ERR_R_NESTED_ASN1_ERROR); 204 ERR_R_NESTED_ASN1_ERROR);
207 goto err; 205 goto err;
208 } 206 }
209 207
210 /* Must be UNIVERSAL class */ 208 /* Must be UNIVERSAL class */
211 if (oclass != V_ASN1_UNIVERSAL) { 209 if (oclass != V_ASN1_UNIVERSAL) {
212 /* If OPTIONAL, assume this is OK */ 210 /* If OPTIONAL, assume this is OK */
213 if (opt) return -1; 211 if (opt)
212 return -1;
214 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 213 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
215 ASN1_R_MSTRING_NOT_UNIVERSAL); 214 ASN1_R_MSTRING_NOT_UNIVERSAL);
216 goto err; 215 goto err;
217 } 216 }
218 /* Check tag matches bit map */ 217 /* Check tag matches bit map */
@@ -221,17 +220,17 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
221 if (opt) 220 if (opt)
222 return -1; 221 return -1;
223 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 222 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
224 ASN1_R_MSTRING_WRONG_TAG); 223 ASN1_R_MSTRING_WRONG_TAG);
225 goto err; 224 goto err;
226 } 225 }
227 return asn1_d2i_ex_primitive(pval, in, len, 226 return asn1_d2i_ex_primitive(pval, in, len,
228 it, otag, 0, 0, ctx); 227 it, otag, 0, 0, ctx);
229 228
230 case ASN1_ITYPE_EXTERN: 229 case ASN1_ITYPE_EXTERN:
231 /* Use new style d2i */ 230 /* Use new style d2i */
232 ef = it->funcs; 231 ef = it->funcs;
233 return ef->asn1_ex_d2i(pval, in, len, 232 return ef->asn1_ex_d2i(pval, in, len,
234 it, tag, aclass, opt, ctx); 233 it, tag, aclass, opt, ctx);
235 234
236 case ASN1_ITYPE_COMPAT: 235 case ASN1_ITYPE_COMPAT:
237 /* we must resort to old style evil hackery */ 236 /* we must resort to old style evil hackery */
@@ -243,15 +242,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
243 p = *in; 242 p = *in;
244 if (tag == -1) 243 if (tag == -1)
245 exptag = it->utype; 244 exptag = it->utype;
246 else exptag = tag; 245 else
246 exptag = tag;
247 /* Don't care about anything other than presence 247 /* Don't care about anything other than presence
248 * of expected tag */ 248 * of expected tag */
249 249
250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL, 250 ret = asn1_check_tlen(NULL, NULL, NULL, NULL, NULL,
251 &p, len, exptag, aclass, 1, ctx); 251 &p, len, exptag, aclass, 1, ctx);
252 if (!ret) { 252 if (!ret) {
253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 253 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
254 ERR_R_NESTED_ASN1_ERROR); 254 ERR_R_NESTED_ASN1_ERROR);
255 goto err; 255 goto err;
256 } 256 }
257 if (ret == -1) 257 if (ret == -1)
@@ -278,11 +278,11 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
278 imphack = *wp; 278 imphack = *wp;
279 if (p == NULL) { 279 if (p == NULL) {
280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 280 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
281 ERR_R_NESTED_ASN1_ERROR); 281 ERR_R_NESTED_ASN1_ERROR);
282 goto err; 282 goto err;
283 } 283 }
284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) 284 *wp = (unsigned char)((*p & V_ASN1_CONSTRUCTED) |
285 | it->utype); 285 it->utype);
286 } 286 }
287 287
288 ptmpval = cf->asn1_d2i(pval, in, len); 288 ptmpval = cf->asn1_d2i(pval, in, len);
@@ -296,20 +296,19 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR); 296 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ERR_R_NESTED_ASN1_ERROR);
297 goto err; 297 goto err;
298 298
299
300 case ASN1_ITYPE_CHOICE: 299 case ASN1_ITYPE_CHOICE:
301 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 300 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
302 goto auxerr; 301 goto auxerr;
303 302
304 /* Allocate structure */ 303 /* Allocate structure */
305 if (!*pval && !ASN1_item_ex_new(pval, it)) { 304 if (!*pval && !ASN1_item_ex_new(pval, it)) {
306 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 305 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
307 ERR_R_NESTED_ASN1_ERROR); 306 ERR_R_NESTED_ASN1_ERROR);
308 goto err; 307 goto err;
309 } 308 }
310 /* CHOICE type, try each possibility in turn */ 309 /* CHOICE type, try each possibility in turn */
311 p = *in; 310 p = *in;
312 for (i = 0, tt=it->templates; i < it->tcount; i++, tt++) { 311 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
313 pchptr = asn1_get_field_ptr(pval, tt); 312 pchptr = asn1_get_field_ptr(pval, tt);
314 /* We mark field as OPTIONAL so its absence 313 /* We mark field as OPTIONAL so its absence
315 * can be recognised. 314 * can be recognised.
@@ -324,7 +323,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
324 /* Otherwise must be an ASN1 parsing error */ 323 /* Otherwise must be an ASN1 parsing error */
325 errtt = tt; 324 errtt = tt;
326 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 325 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
327 ERR_R_NESTED_ASN1_ERROR); 326 ERR_R_NESTED_ASN1_ERROR);
328 goto err; 327 goto err;
329 } 328 }
330 329
@@ -337,14 +336,14 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
337 return -1; 336 return -1;
338 } 337 }
339 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 338 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
340 ASN1_R_NO_MATCHING_CHOICE_TYPE); 339 ASN1_R_NO_MATCHING_CHOICE_TYPE);
341 goto err; 340 goto err;
342 } 341 }
343 342
344 asn1_set_choice_selector(pval, i, it); 343 asn1_set_choice_selector(pval, i, it);
345 *in = p; 344 *in = p;
346 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 345 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
347 goto auxerr; 346 goto auxerr;
348 return 1; 347 return 1;
349 348
350 case ASN1_ITYPE_NDEF_SEQUENCE: 349 case ASN1_ITYPE_NDEF_SEQUENCE:
@@ -359,34 +358,34 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
359 } 358 }
360 /* Get SEQUENCE length and update len, p */ 359 /* Get SEQUENCE length and update len, p */
361 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst, 360 ret = asn1_check_tlen(&len, NULL, NULL, &seq_eoc, &cst,
362 &p, len, tag, aclass, opt, ctx); 361 &p, len, tag, aclass, opt, ctx);
363 if (!ret) { 362 if (!ret) {
364 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 363 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
365 ERR_R_NESTED_ASN1_ERROR); 364 ERR_R_NESTED_ASN1_ERROR);
366 goto err; 365 goto err;
367 } 366 } else if (ret == -1)
368 else if (ret == -1)
369 return -1; 367 return -1;
370 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) { 368 if (aux && (aux->flags & ASN1_AFLG_BROKEN)) {
371 len = tmplen - (p - *in); 369 len = tmplen - (p - *in);
372 seq_nolen = 1; 370 seq_nolen = 1;
373 } 371 }
374 /* If indefinite we don't do a length check */ 372 /* If indefinite we don't do a length check */
375 else seq_nolen = seq_eoc; 373 else
374 seq_nolen = seq_eoc;
376 if (!cst) { 375 if (!cst) {
377 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 376 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
378 ASN1_R_SEQUENCE_NOT_CONSTRUCTED); 377 ASN1_R_SEQUENCE_NOT_CONSTRUCTED);
379 goto err; 378 goto err;
380 } 379 }
381 380
382 if (!*pval && !ASN1_item_ex_new(pval, it)) { 381 if (!*pval && !ASN1_item_ex_new(pval, it)) {
383 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 382 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
384 ERR_R_NESTED_ASN1_ERROR); 383 ERR_R_NESTED_ASN1_ERROR);
385 goto err; 384 goto err;
386 } 385 }
387 386
388 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL)) 387 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_PRE, pval, it, NULL))
389 goto auxerr; 388 goto auxerr;
390 389
391 /* Get each field entry */ 390 /* Get each field entry */
392 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 391 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
@@ -403,7 +402,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
403 if (asn1_check_eoc(&p, len)) { 402 if (asn1_check_eoc(&p, len)) {
404 if (!seq_eoc) { 403 if (!seq_eoc) {
405 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 404 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
406 ASN1_R_UNEXPECTED_EOC); 405 ASN1_R_UNEXPECTED_EOC);
407 goto err; 406 goto err;
408 } 407 }
409 len -= p - q; 408 len -= p - q;
@@ -419,12 +418,13 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
419 */ 418 */
420 if (i == (it->tcount - 1)) 419 if (i == (it->tcount - 1))
421 isopt = 0; 420 isopt = 0;
422 else isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL); 421 else
422 isopt = (char)(seqtt->flags & ASN1_TFLG_OPTIONAL);
423 /* attempt to read in field, allowing each to be 423 /* attempt to read in field, allowing each to be
424 * OPTIONAL */ 424 * OPTIONAL */
425 425
426 ret = asn1_template_ex_d2i(pseqval, &p, len, 426 ret = asn1_template_ex_d2i(pseqval, &p, len,
427 seqtt, isopt, ctx); 427 seqtt, isopt, ctx);
428 if (!ret) { 428 if (!ret) {
429 errtt = seqtt; 429 errtt = seqtt;
430 goto err; 430 goto err;
@@ -447,7 +447,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
447 /* Check all data read */ 447 /* Check all data read */
448 if (!seq_nolen && len) { 448 if (!seq_nolen && len) {
449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 449 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
450 ASN1_R_SEQUENCE_LENGTH_MISMATCH); 450 ASN1_R_SEQUENCE_LENGTH_MISMATCH);
451 goto err; 451 goto err;
452 } 452 }
453 453
@@ -467,7 +467,7 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
467 } else { 467 } else {
468 errtt = seqtt; 468 errtt = seqtt;
469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, 469 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I,
470 ASN1_R_FIELD_MISSING); 470 ASN1_R_FIELD_MISSING);
471 goto err; 471 goto err;
472 } 472 }
473 } 473 }
@@ -476,19 +476,20 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
476 goto auxerr; 476 goto auxerr;
477 *in = p; 477 *in = p;
478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL)) 478 if (asn1_cb && !asn1_cb(ASN1_OP_D2I_POST, pval, it, NULL))
479 goto auxerr; 479 goto auxerr;
480 return 1; 480 return 1;
481 481
482 default: 482 default:
483 return 0; 483 return 0;
484 } 484 }
485 auxerr: 485
486auxerr:
486 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR); 487 ASN1err(ASN1_F_ASN1_ITEM_EX_D2I, ASN1_R_AUX_ERROR);
487 err: 488err:
488 ASN1_item_ex_free(pval, it); 489 ASN1_item_ex_free(pval, it);
489 if (errtt) 490 if (errtt)
490 ERR_add_error_data(4, "Field=", errtt->field_name, 491 ERR_add_error_data(4, "Field=", errtt->field_name,
491 ", Type=", it->sname); 492 ", Type=", it->sname);
492 else 493 else
493 ERR_add_error_data(2, "Type=", it->sname); 494 ERR_add_error_data(2, "Type=", it->sname);
494 return 0; 495 return 0;
@@ -498,16 +499,16 @@ int ASN1_item_ex_d2i(ASN1_VALUE **pval, const unsigned char **in, long len,
498 * One handles any EXPLICIT tag and the other handles the rest. 499 * One handles any EXPLICIT tag and the other handles the rest.
499 */ 500 */
500 501
501static int asn1_template_ex_d2i(ASN1_VALUE **val, 502static int
502 const unsigned char **in, long inlen, 503asn1_template_ex_d2i(ASN1_VALUE **val, const unsigned char **in, long inlen,
503 const ASN1_TEMPLATE *tt, char opt, 504 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
504 ASN1_TLC *ctx)
505{ 505{
506 int flags, aclass; 506 int flags, aclass;
507 int ret; 507 int ret;
508 long len; 508 long len;
509 const unsigned char *p, *q; 509 const unsigned char *p, *q;
510 char exp_eoc; 510 char exp_eoc;
511
511 if (!val) 512 if (!val)
512 return 0; 513 return 0;
513 flags = tt->flags; 514 flags = tt->flags;
@@ -523,24 +524,24 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
523 * get the info. 524 * get the info.
524 */ 525 */
525 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst, 526 ret = asn1_check_tlen(&len, NULL, NULL, &exp_eoc, &cst,
526 &p, inlen, tt->tag, aclass, opt, ctx); 527 &p, inlen, tt->tag, aclass, opt, ctx);
527 q = p; 528 q = p;
528 if (!ret) { 529 if (!ret) {
529 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 530 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
530 ERR_R_NESTED_ASN1_ERROR); 531 ERR_R_NESTED_ASN1_ERROR);
531 return 0; 532 return 0;
532 } else if (ret == -1) 533 } else if (ret == -1)
533 return -1; 534 return -1;
534 if (!cst) { 535 if (!cst) {
535 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 536 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
536 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED); 537 ASN1_R_EXPLICIT_TAG_NOT_CONSTRUCTED);
537 return 0; 538 return 0;
538 } 539 }
539 /* We've found the field so it can't be OPTIONAL now */ 540 /* We've found the field so it can't be OPTIONAL now */
540 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx); 541 ret = asn1_template_noexp_d2i(val, &p, len, tt, 0, ctx);
541 if (!ret) { 542 if (!ret) {
542 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 543 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
543 ERR_R_NESTED_ASN1_ERROR); 544 ERR_R_NESTED_ASN1_ERROR);
544 return 0; 545 return 0;
545 } 546 }
546 /* We read the field in OK so update length */ 547 /* We read the field in OK so update length */
@@ -549,7 +550,7 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
549 /* If NDEF we must have an EOC here */ 550 /* If NDEF we must have an EOC here */
550 if (!asn1_check_eoc(&p, len)) { 551 if (!asn1_check_eoc(&p, len)) {
551 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 552 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
552 ASN1_R_MISSING_EOC); 553 ASN1_R_MISSING_EOC);
553 goto err; 554 goto err;
554 } 555 }
555 } else { 556 } else {
@@ -557,31 +558,29 @@ static int asn1_template_ex_d2i(ASN1_VALUE **val,
557 * an error */ 558 * an error */
558 if (len) { 559 if (len) {
559 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I, 560 ASN1err(ASN1_F_ASN1_TEMPLATE_EX_D2I,
560 ASN1_R_EXPLICIT_LENGTH_MISMATCH); 561 ASN1_R_EXPLICIT_LENGTH_MISMATCH);
561 goto err; 562 goto err;
562 } 563 }
563 } 564 }
564 } 565 } else
565 else 566 return asn1_template_noexp_d2i(val, in, inlen, tt, opt, ctx);
566 return asn1_template_noexp_d2i(val, in, inlen,
567 tt, opt, ctx);
568 567
569 *in = p; 568 *in = p;
570 return 1; 569 return 1;
571 570
572 err: 571err:
573 ASN1_template_free(val, tt); 572 ASN1_template_free(val, tt);
574 return 0; 573 return 0;
575} 574}
576 575
577static int asn1_template_noexp_d2i(ASN1_VALUE **val, 576static int
578 const unsigned char **in, long len, 577asn1_template_noexp_d2i(ASN1_VALUE **val, const unsigned char **in, long len,
579 const ASN1_TEMPLATE *tt, char opt, 578 const ASN1_TEMPLATE *tt, char opt, ASN1_TLC *ctx)
580 ASN1_TLC *ctx)
581{ 579{
582 int flags, aclass; 580 int flags, aclass;
583 int ret; 581 int ret;
584 const unsigned char *p, *q; 582 const unsigned char *p, *q;
583
585 if (!val) 584 if (!val)
586 return 0; 585 return 0;
587 flags = tt->flags; 586 flags = tt->flags;
@@ -607,43 +606,42 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
607 } 606 }
608 /* Get the tag */ 607 /* Get the tag */
609 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL, 608 ret = asn1_check_tlen(&len, NULL, NULL, &sk_eoc, NULL,
610 &p, len, sktag, skaclass, opt, ctx); 609 &p, len, sktag, skaclass, opt, ctx);
611 if (!ret) { 610 if (!ret) {
612 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 611 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
613 ERR_R_NESTED_ASN1_ERROR); 612 ERR_R_NESTED_ASN1_ERROR);
614 return 0; 613 return 0;
615 } 614 } else if (ret == -1)
616 else if (ret == -1)
617 return -1; 615 return -1;
618 if (!*val) 616 if (!*val)
619 *val = (ASN1_VALUE *)sk_new_null(); 617 *val = (ASN1_VALUE *)sk_new_null();
620 else { 618 else {
621 /* We've got a valid STACK: free up any items present */ 619 /* We've got a valid STACK: free up any items present */
622 STACK_OF(ASN1_VALUE) *sktmp 620 STACK_OF(ASN1_VALUE) *sktmp =
623 = (STACK_OF(ASN1_VALUE) *)*val; 621 (STACK_OF(ASN1_VALUE) *)*val;
624 ASN1_VALUE *vtmp; 622 ASN1_VALUE *vtmp;
625 while(sk_ASN1_VALUE_num(sktmp) > 0) { 623 while (sk_ASN1_VALUE_num(sktmp) > 0) {
626 vtmp = sk_ASN1_VALUE_pop(sktmp); 624 vtmp = sk_ASN1_VALUE_pop(sktmp);
627 ASN1_item_ex_free(&vtmp, 625 ASN1_item_ex_free(&vtmp,
628 ASN1_ITEM_ptr(tt->item)); 626 ASN1_ITEM_ptr(tt->item));
629 } 627 }
630 } 628 }
631 629
632 if (!*val) { 630 if (!*val) {
633 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 631 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
634 ERR_R_MALLOC_FAILURE); 632 ERR_R_MALLOC_FAILURE);
635 goto err; 633 goto err;
636 } 634 }
637 635
638 /* Read as many items as we can */ 636 /* Read as many items as we can */
639 while(len > 0) { 637 while (len > 0) {
640 ASN1_VALUE *skfield; 638 ASN1_VALUE *skfield;
641 q = p; 639 q = p;
642 /* See if EOC found */ 640 /* See if EOC found */
643 if (asn1_check_eoc(&p, len)) { 641 if (asn1_check_eoc(&p, len)) {
644 if (!sk_eoc) { 642 if (!sk_eoc) {
645 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 643 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
646 ASN1_R_UNEXPECTED_EOC); 644 ASN1_R_UNEXPECTED_EOC);
647 goto err; 645 goto err;
648 } 646 }
649 len -= p - q; 647 len -= p - q;
@@ -652,60 +650,57 @@ static int asn1_template_noexp_d2i(ASN1_VALUE **val,
652 } 650 }
653 skfield = NULL; 651 skfield = NULL;
654 if (!ASN1_item_ex_d2i(&skfield, &p, len, 652 if (!ASN1_item_ex_d2i(&skfield, &p, len,
655 ASN1_ITEM_ptr(tt->item), 653 ASN1_ITEM_ptr(tt->item), -1, 0, 0, ctx)) {
656 -1, 0, 0, ctx)) {
657 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 654 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
658 ERR_R_NESTED_ASN1_ERROR); 655 ERR_R_NESTED_ASN1_ERROR);
659 goto err; 656 goto err;
660 } 657 }
661 len -= p - q; 658 len -= p - q;
662 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val, 659 if (!sk_ASN1_VALUE_push((STACK_OF(ASN1_VALUE) *)*val,
663 skfield)) { 660 skfield)) {
664 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 661 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
665 ERR_R_MALLOC_FAILURE); 662 ERR_R_MALLOC_FAILURE);
666 goto err; 663 goto err;
667 } 664 }
668 } 665 }
669 if (sk_eoc) { 666 if (sk_eoc) {
670 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, ASN1_R_MISSING_EOC); 667 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
668 ASN1_R_MISSING_EOC);
671 goto err; 669 goto err;
672 } 670 }
673 } else if (flags & ASN1_TFLG_IMPTAG) { 671 } else if (flags & ASN1_TFLG_IMPTAG) {
674 /* IMPLICIT tagging */ 672 /* IMPLICIT tagging */
675 ret = ASN1_item_ex_d2i(val, &p, len, 673 ret = ASN1_item_ex_d2i(val, &p, len,
676 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx); 674 ASN1_ITEM_ptr(tt->item), tt->tag, aclass, opt, ctx);
677 if (!ret) { 675 if (!ret) {
678 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 676 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
679 ERR_R_NESTED_ASN1_ERROR); 677 ERR_R_NESTED_ASN1_ERROR);
680 goto err; 678 goto err;
681 } 679 } else if (ret == -1)
682 else if (ret == -1)
683 return -1; 680 return -1;
684 } else { 681 } else {
685 /* Nothing special */ 682 /* Nothing special */
686 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item), 683 ret = ASN1_item_ex_d2i(val, &p, len, ASN1_ITEM_ptr(tt->item),
687 -1, 0, opt, ctx); 684 -1, 0, opt, ctx);
688 if (!ret) { 685 if (!ret) {
689 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I, 686 ASN1err(ASN1_F_ASN1_TEMPLATE_NOEXP_D2I,
690 ERR_R_NESTED_ASN1_ERROR); 687 ERR_R_NESTED_ASN1_ERROR);
691 goto err; 688 goto err;
692 } 689 } else if (ret == -1)
693 else if (ret == -1)
694 return -1; 690 return -1;
695 } 691 }
696 692
697 *in = p; 693 *in = p;
698 return 1; 694 return 1;
699 695
700 err: 696err:
701 ASN1_template_free(val, tt); 697 ASN1_template_free(val, tt);
702 return 0; 698 return 0;
703} 699}
704 700
705static int asn1_d2i_ex_primitive(ASN1_VALUE **pval, 701static int
706 const unsigned char **in, long inlen, 702asn1_d2i_ex_primitive(ASN1_VALUE **pval, const unsigned char **in, long inlen,
707 const ASN1_ITEM *it, 703 const ASN1_ITEM *it, int tag, int aclass, char opt, ASN1_TLC *ctx)
708 int tag, int aclass, char opt, ASN1_TLC *ctx)
709{ 704{
710 int ret = 0, utype; 705 int ret = 0, utype;
711 long plen; 706 long plen;
@@ -713,7 +708,8 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
713 const unsigned char *p; 708 const unsigned char *p;
714 BUF_MEM buf; 709 BUF_MEM buf;
715 const unsigned char *cont = NULL; 710 const unsigned char *cont = NULL;
716 long len; 711 long len;
712
717 if (!pval) { 713 if (!pval) {
718 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL); 714 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ASN1_R_ILLEGAL_NULL);
719 return 0; /* Should never happen */ 715 return 0; /* Should never happen */
@@ -722,8 +718,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
722 if (it->itype == ASN1_ITYPE_MSTRING) { 718 if (it->itype == ASN1_ITYPE_MSTRING) {
723 utype = tag; 719 utype = tag;
724 tag = -1; 720 tag = -1;
725 } 721 } else
726 else
727 utype = it->utype; 722 utype = it->utype;
728 723
729 if (utype == V_ASN1_ANY) { 724 if (utype == V_ASN1_ANY) {
@@ -731,20 +726,20 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
731 unsigned char oclass; 726 unsigned char oclass;
732 if (tag >= 0) { 727 if (tag >= 0) {
733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 728 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
734 ASN1_R_ILLEGAL_TAGGED_ANY); 729 ASN1_R_ILLEGAL_TAGGED_ANY);
735 return 0; 730 return 0;
736 } 731 }
737 if (opt) { 732 if (opt) {
738 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 733 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
739 ASN1_R_ILLEGAL_OPTIONAL_ANY); 734 ASN1_R_ILLEGAL_OPTIONAL_ANY);
740 return 0; 735 return 0;
741 } 736 }
742 p = *in; 737 p = *in;
743 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL, 738 ret = asn1_check_tlen(NULL, &utype, &oclass, NULL, NULL,
744 &p, inlen, -1, 0, 0, ctx); 739 &p, inlen, -1, 0, 0, ctx);
745 if (!ret) { 740 if (!ret) {
746 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 741 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
747 ERR_R_NESTED_ASN1_ERROR); 742 ERR_R_NESTED_ASN1_ERROR);
748 return 0; 743 return 0;
749 } 744 }
750 if (oclass != V_ASN1_UNIVERSAL) 745 if (oclass != V_ASN1_UNIVERSAL)
@@ -757,17 +752,16 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
757 p = *in; 752 p = *in;
758 /* Check header */ 753 /* Check header */
759 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst, 754 ret = asn1_check_tlen(&plen, NULL, NULL, &inf, &cst,
760 &p, inlen, tag, aclass, opt, ctx); 755 &p, inlen, tag, aclass, opt, ctx);
761 if (!ret) { 756 if (!ret) {
762 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR); 757 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, ERR_R_NESTED_ASN1_ERROR);
763 return 0; 758 return 0;
764 } 759 } else if (ret == -1)
765 else if (ret == -1)
766 return -1; 760 return -1;
767 ret = 0; 761 ret = 0;
768 /* SEQUENCE, SET and "OTHER" are left in encoded form */ 762 /* SEQUENCE, SET and "OTHER" are left in encoded form */
769 if ((utype == V_ASN1_SEQUENCE) 763 if ((utype == V_ASN1_SEQUENCE) || (utype == V_ASN1_SET) ||
770 || (utype == V_ASN1_SET) || (utype == V_ASN1_OTHER)) { 764 (utype == V_ASN1_OTHER)) {
771 /* Clear context cache for type OTHER because the auto clear 765 /* Clear context cache for type OTHER because the auto clear
772 * when we have a exact match wont work 766 * when we have a exact match wont work
773 */ 767 */
@@ -777,7 +771,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
777 /* SEQUENCE and SET must be constructed */ 771 /* SEQUENCE and SET must be constructed */
778 else if (!cst) { 772 else if (!cst) {
779 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 773 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
780 ASN1_R_TYPE_NOT_CONSTRUCTED); 774 ASN1_R_TYPE_NOT_CONSTRUCTED);
781 return 0; 775 return 0;
782 } 776 }
783 777
@@ -785,7 +779,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
785 /* If indefinite length constructed find the real end */ 779 /* If indefinite length constructed find the real end */
786 if (inf) { 780 if (inf) {
787 if (!asn1_find_end(&p, plen, inf)) 781 if (!asn1_find_end(&p, plen, inf))
788 goto err; 782 goto err;
789 len = p - cont; 783 len = p - cont;
790 } else { 784 } else {
791 len = p - cont + plen; 785 len = p - cont + plen;
@@ -810,7 +804,7 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
810 /* Append a final null to string */ 804 /* Append a final null to string */
811 if (!BUF_MEM_grow_clean(&buf, len + 1)) { 805 if (!BUF_MEM_grow_clean(&buf, len + 1)) {
812 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE, 806 ASN1err(ASN1_F_ASN1_D2I_EX_PRIMITIVE,
813 ERR_R_MALLOC_FAILURE); 807 ERR_R_MALLOC_FAILURE);
814 return 0; 808 return 0;
815 } 809 }
816 buf.data[len] = 0; 810 buf.data[len] = 0;
@@ -828,15 +822,18 @@ static int asn1_d2i_ex_primitive(ASN1_VALUE **pval,
828 822
829 *in = p; 823 *in = p;
830 ret = 1; 824 ret = 1;
831 err: 825
832 if (free_cont && buf.data) free(buf.data); 826err:
827 if (free_cont && buf.data)
828 free(buf.data);
833 return ret; 829 return ret;
834} 830}
835 831
836/* Translate ASN1 content octets into a structure */ 832/* Translate ASN1 content octets into a structure */
837 833
838int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, 834int
839 int utype, char *free_cont, const ASN1_ITEM *it) 835asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len, int utype,
836 char *free_cont, const ASN1_ITEM *it)
840{ 837{
841 ASN1_VALUE **opval = NULL; 838 ASN1_VALUE **opval = NULL;
842 ASN1_STRING *stmp; 839 ASN1_STRING *stmp;
@@ -855,8 +852,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
855 if (typ == NULL) 852 if (typ == NULL)
856 goto err; 853 goto err;
857 *pval = (ASN1_VALUE *)typ; 854 *pval = (ASN1_VALUE *)typ;
858 } 855 } else
859 else
860 typ = (ASN1_TYPE *)*pval; 856 typ = (ASN1_TYPE *)*pval;
861 857
862 if (utype != typ->type) 858 if (utype != typ->type)
@@ -864,7 +860,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
864 opval = pval; 860 opval = pval;
865 pval = &typ->value.asn1_value; 861 pval = &typ->value.asn1_value;
866 } 862 }
867 switch(utype) { 863 switch (utype) {
868 case V_ASN1_OBJECT: 864 case V_ASN1_OBJECT:
869 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len)) 865 if (!c2i_ASN1_OBJECT((ASN1_OBJECT **)pval, &cont, len))
870 goto err; 866 goto err;
@@ -873,7 +869,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
873 case V_ASN1_NULL: 869 case V_ASN1_NULL:
874 if (len) { 870 if (len) {
875 ASN1err(ASN1_F_ASN1_EX_C2I, 871 ASN1err(ASN1_F_ASN1_EX_C2I,
876 ASN1_R_NULL_IS_WRONG_LENGTH); 872 ASN1_R_NULL_IS_WRONG_LENGTH);
877 goto err; 873 goto err;
878 } 874 }
879 *pval = (ASN1_VALUE *)1; 875 *pval = (ASN1_VALUE *)1;
@@ -882,7 +878,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
882 case V_ASN1_BOOLEAN: 878 case V_ASN1_BOOLEAN:
883 if (len != 1) { 879 if (len != 1) {
884 ASN1err(ASN1_F_ASN1_EX_C2I, 880 ASN1err(ASN1_F_ASN1_EX_C2I,
885 ASN1_R_BOOLEAN_IS_WRONG_LENGTH); 881 ASN1_R_BOOLEAN_IS_WRONG_LENGTH);
886 goto err; 882 goto err;
887 } else { 883 } else {
888 ASN1_BOOLEAN *tbool; 884 ASN1_BOOLEAN *tbool;
@@ -927,12 +923,12 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
927 default: 923 default:
928 if (utype == V_ASN1_BMPSTRING && (len & 1)) { 924 if (utype == V_ASN1_BMPSTRING && (len & 1)) {
929 ASN1err(ASN1_F_ASN1_EX_C2I, 925 ASN1err(ASN1_F_ASN1_EX_C2I,
930 ASN1_R_BMPSTRING_IS_WRONG_LENGTH); 926 ASN1_R_BMPSTRING_IS_WRONG_LENGTH);
931 goto err; 927 goto err;
932 } 928 }
933 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) { 929 if (utype == V_ASN1_UNIVERSALSTRING && (len & 3)) {
934 ASN1err(ASN1_F_ASN1_EX_C2I, 930 ASN1err(ASN1_F_ASN1_EX_C2I,
935 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH); 931 ASN1_R_UNIVERSALSTRING_IS_WRONG_LENGTH);
936 goto err; 932 goto err;
937 } 933 }
938 /* All based on ASN1_STRING and handled the same */ 934 /* All based on ASN1_STRING and handled the same */
@@ -940,7 +936,7 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
940 stmp = ASN1_STRING_type_new(utype); 936 stmp = ASN1_STRING_type_new(utype);
941 if (!stmp) { 937 if (!stmp) {
942 ASN1err(ASN1_F_ASN1_EX_C2I, 938 ASN1err(ASN1_F_ASN1_EX_C2I,
943 ERR_R_MALLOC_FAILURE); 939 ERR_R_MALLOC_FAILURE);
944 goto err; 940 goto err;
945 } 941 }
946 *pval = (ASN1_VALUE *)stmp; 942 *pval = (ASN1_VALUE *)stmp;
@@ -958,8 +954,8 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
958 } else { 954 } else {
959 if (!ASN1_STRING_set(stmp, cont, len)) { 955 if (!ASN1_STRING_set(stmp, cont, len)) {
960 ASN1err(ASN1_F_ASN1_EX_C2I, 956 ASN1err(ASN1_F_ASN1_EX_C2I,
961 ERR_R_MALLOC_FAILURE); 957 ERR_R_MALLOC_FAILURE);
962 ASN1_STRING_free(stmp); 958 ASN1_STRING_free(stmp);
963 *pval = NULL; 959 *pval = NULL;
964 goto err; 960 goto err;
965 } 961 }
@@ -968,10 +964,11 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
968 } 964 }
969 /* If ASN1_ANY and NULL type fix up value */ 965 /* If ASN1_ANY and NULL type fix up value */
970 if (typ && (utype == V_ASN1_NULL)) 966 if (typ && (utype == V_ASN1_NULL))
971 typ->value.ptr = NULL; 967 typ->value.ptr = NULL;
972 968
973 ret = 1; 969 ret = 1;
974 err: 970
971err:
975 if (!ret) { 972 if (!ret) {
976 ASN1_TYPE_free(typ); 973 ASN1_TYPE_free(typ);
977 if (opval) 974 if (opval)
@@ -987,11 +984,13 @@ int asn1_ex_c2i(ASN1_VALUE **pval, const unsigned char *cont, int len,
987 * recurse on each indefinite length header. 984 * recurse on each indefinite length header.
988 */ 985 */
989 986
990static int asn1_find_end(const unsigned char **in, long len, char inf) 987static int
988asn1_find_end(const unsigned char **in, long len, char inf)
991{ 989{
992 int expected_eoc; 990 int expected_eoc;
993 long plen; 991 long plen;
994 const unsigned char *p = *in, *q; 992 const unsigned char *p = *in, *q;
993
995 /* If not indefinite length constructed just add length */ 994 /* If not indefinite length constructed just add length */
996 if (inf == 0) { 995 if (inf == 0) {
997 *in += len; 996 *in += len;
@@ -1004,7 +1003,7 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1004 * skip to the end of the data. 1003 * skip to the end of the data.
1005 */ 1004 */
1006 while (len > 0) { 1005 while (len > 0) {
1007 if(asn1_check_eoc(&p, len)) { 1006 if (asn1_check_eoc(&p, len)) {
1008 expected_eoc--; 1007 expected_eoc--;
1009 if (expected_eoc == 0) 1008 if (expected_eoc == 0)
1010 break; 1009 break;
@@ -1013,8 +1012,8 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1013 } 1012 }
1014 q = p; 1013 q = p;
1015 /* Just read in a header: only care about the length */ 1014 /* Just read in a header: only care about the length */
1016 if(!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, 1015 if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len,
1017 -1, 0, 0, NULL)) { 1016 -1, 0, 0, NULL)) {
1018 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR); 1017 ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
1019 return 0; 1018 return 0;
1020 } 1019 }
@@ -1046,13 +1045,14 @@ static int asn1_find_end(const unsigned char **in, long len, char inf)
1046#define ASN1_MAX_STRING_NEST 5 1045#define ASN1_MAX_STRING_NEST 5
1047#endif 1046#endif
1048 1047
1049 1048static int
1050static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, 1049asn1_collect(BUF_MEM *buf, const unsigned char **in, long len, char inf,
1051 char inf, int tag, int aclass, int depth) 1050 int tag, int aclass, int depth)
1052{ 1051{
1053 const unsigned char *p, *q; 1052 const unsigned char *p, *q;
1054 long plen; 1053 long plen;
1055 char cst, ininf; 1054 char cst, ininf;
1055
1056 p = *in; 1056 p = *in;
1057 inf &= 1; 1057 inf &= 1;
1058 /* If no buffer and not indefinite length constructed just pass over 1058 /* If no buffer and not indefinite length constructed just pass over
@@ -1061,7 +1061,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1061 *in += len; 1061 *in += len;
1062 return 1; 1062 return 1;
1063 } 1063 }
1064 while(len > 0) { 1064 while (len > 0) {
1065 q = p; 1065 q = p;
1066 /* Check for EOC */ 1066 /* Check for EOC */
1067 if (asn1_check_eoc(&p, len)) { 1067 if (asn1_check_eoc(&p, len)) {
@@ -1069,7 +1069,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1069 * constructed form */ 1069 * constructed form */
1070 if (!inf) { 1070 if (!inf) {
1071 ASN1err(ASN1_F_ASN1_COLLECT, 1071 ASN1err(ASN1_F_ASN1_COLLECT,
1072 ASN1_R_UNEXPECTED_EOC); 1072 ASN1_R_UNEXPECTED_EOC);
1073 return 0; 1073 return 0;
1074 } 1074 }
1075 inf = 0; 1075 inf = 0;
@@ -1077,7 +1077,7 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1077 } 1077 }
1078 1078
1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p, 1079 if (!asn1_check_tlen(&plen, NULL, NULL, &ininf, &cst, &p,
1080 len, tag, aclass, 0, NULL)) { 1080 len, tag, aclass, 0, NULL)) {
1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR); 1081 ASN1err(ASN1_F_ASN1_COLLECT, ERR_R_NESTED_ASN1_ERROR);
1082 return 0; 1082 return 0;
1083 } 1083 }
@@ -1086,14 +1086,13 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1086 if (cst) { 1086 if (cst) {
1087 if (depth >= ASN1_MAX_STRING_NEST) { 1087 if (depth >= ASN1_MAX_STRING_NEST) {
1088 ASN1err(ASN1_F_ASN1_COLLECT, 1088 ASN1err(ASN1_F_ASN1_COLLECT,
1089 ASN1_R_NESTED_ASN1_STRING); 1089 ASN1_R_NESTED_ASN1_STRING);
1090 return 0; 1090 return 0;
1091 } 1091 }
1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass, 1092 if (!asn1_collect(buf, &p, plen, ininf, tag, aclass,
1093 depth + 1)) 1093 depth + 1))
1094 return 0; 1094 return 0;
1095 } 1095 } else if (plen && !collect_data(buf, &p, plen))
1096 else if (plen && !collect_data(buf, &p, plen))
1097 return 0; 1096 return 0;
1098 len -= p - q; 1097 len -= p - q;
1099 } 1098 }
@@ -1105,7 +1104,8 @@ static int asn1_collect(BUF_MEM *buf, const unsigned char **in, long len,
1105 return 1; 1104 return 1;
1106} 1105}
1107 1106
1108static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen) 1107static int
1108collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1109{ 1109{
1110 int len; 1110 int len;
1111 if (buf) { 1111 if (buf) {
@@ -1122,10 +1122,13 @@ static int collect_data(BUF_MEM *buf, const unsigned char **p, long plen)
1122 1122
1123/* Check for ASN1 EOC and swallow it if found */ 1123/* Check for ASN1 EOC and swallow it if found */
1124 1124
1125static int asn1_check_eoc(const unsigned char **in, long len) 1125static int
1126asn1_check_eoc(const unsigned char **in, long len)
1126{ 1127{
1127 const unsigned char *p; 1128 const unsigned char *p;
1128 if (len < 2) return 0; 1129
1130 if (len < 2)
1131 return 0;
1129 p = *in; 1132 p = *in;
1130 if (!p[0] && !p[1]) { 1133 if (!p[0] && !p[1]) {
1131 *in += 2; 1134 *in += 2;
@@ -1141,11 +1144,10 @@ static int asn1_check_eoc(const unsigned char **in, long len)
1141 * header length just read. 1144 * header length just read.
1142 */ 1145 */
1143 1146
1144static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, 1147static int
1145 char *inf, char *cst, 1148asn1_check_tlen(long *olen, int *otag, unsigned char *oclass, char *inf,
1146 const unsigned char **in, long len, 1149 char *cst, const unsigned char **in, long len, int exptag, int expclass,
1147 int exptag, int expclass, char opt, 1150 char opt, ASN1_TLC *ctx)
1148 ASN1_TLC *ctx)
1149{ 1151{
1150 int i; 1152 int i;
1151 int ptag, pclass; 1153 int ptag, pclass;
@@ -1170,11 +1172,11 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1170 ctx->hdrlen = p - q; 1172 ctx->hdrlen = p - q;
1171 ctx->valid = 1; 1173 ctx->valid = 1;
1172 /* If definite length, and no error, length + 1174 /* If definite length, and no error, length +
1173 * header can't exceed total amount of data available. 1175 * header can't exceed total amount of data available.
1174 */ 1176 */
1175 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) { 1177 if (!(i & 0x81) && ((plen + ctx->hdrlen) > len)) {
1176 ASN1err(ASN1_F_ASN1_CHECK_TLEN, 1178 ASN1err(ASN1_F_ASN1_CHECK_TLEN,
1177 ASN1_R_TOO_LONG); 1179 ASN1_R_TOO_LONG);
1178 asn1_tlc_clear(ctx); 1180 asn1_tlc_clear(ctx);
1179 return 0; 1181 return 0;
1180 } 1182 }
@@ -1191,7 +1193,8 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1191 /* If type is OPTIONAL, not an error: 1193 /* If type is OPTIONAL, not an error:
1192 * indicate missing type. 1194 * indicate missing type.
1193 */ 1195 */
1194 if (opt) return -1; 1196 if (opt)
1197 return -1;
1195 asn1_tlc_clear(ctx); 1198 asn1_tlc_clear(ctx);
1196 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG); 1199 ASN1err(ASN1_F_ASN1_CHECK_TLEN, ASN1_R_WRONG_TAG);
1197 return 0; 1200 return 0;
@@ -1203,19 +1206,14 @@ static int asn1_check_tlen(long *olen, int *otag, unsigned char *oclass,
1203 1206
1204 if (i & 1) 1207 if (i & 1)
1205 plen = len - (p - q); 1208 plen = len - (p - q);
1206
1207 if (inf) 1209 if (inf)
1208 *inf = i & 1; 1210 *inf = i & 1;
1209
1210 if (cst) 1211 if (cst)
1211 *cst = i & V_ASN1_CONSTRUCTED; 1212 *cst = i & V_ASN1_CONSTRUCTED;
1212
1213 if (olen) 1213 if (olen)
1214 *olen = plen; 1214 *olen = plen;
1215
1216 if (oclass) 1215 if (oclass)
1217 *oclass = pclass; 1216 *oclass = pclass;
1218
1219 if (otag) 1217 if (otag)
1220 *otag = ptag; 1218 *otag = ptag;
1221 1219
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_enc.c b/src/lib/libssl/src/crypto/asn1/tasn_enc.c
index c9131cd9ca..f5fc8820f6 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_enc.c
+++ b/src/lib/libssl/src/crypto/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;
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_fre.c b/src/lib/libssl/src/crypto/asn1/tasn_fre.c
index 5211276efe..9e6e7579f1 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_fre.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_fre.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
@@ -62,21 +62,25 @@
62#include <openssl/asn1t.h> 62#include <openssl/asn1t.h>
63#include <openssl/objects.h> 63#include <openssl/objects.h>
64 64
65static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine); 65static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it,
66 int combine);
66 67
67/* Free up an ASN1 structure */ 68/* Free up an ASN1 structure */
68 69
69void ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it) 70void
71ASN1_item_free(ASN1_VALUE *val, const ASN1_ITEM *it)
70{ 72{
71 asn1_item_combine_free(&val, it, 0); 73 asn1_item_combine_free(&val, it, 0);
72} 74}
73 75
74void ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 76void
77ASN1_item_ex_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
75{ 78{
76 asn1_item_combine_free(pval, it, 0); 79 asn1_item_combine_free(pval, it, 0);
77} 80}
78 81
79static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine) 82static void
83asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
80{ 84{
81 const ASN1_TEMPLATE *tt = NULL, *seqtt; 85 const ASN1_TEMPLATE *tt = NULL, *seqtt;
82 const ASN1_EXTERN_FUNCS *ef; 86 const ASN1_EXTERN_FUNCS *ef;
@@ -84,6 +88,7 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
84 const ASN1_AUX *aux = it->funcs; 88 const ASN1_AUX *aux = it->funcs;
85 ASN1_aux_cb *asn1_cb; 89 ASN1_aux_cb *asn1_cb;
86 int i; 90 int i;
91
87 if (!pval) 92 if (!pval)
88 return; 93 return;
89 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval) 94 if ((it->itype != ASN1_ITYPE_PRIMITIVE) && !*pval)
@@ -93,8 +98,7 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
93 else 98 else
94 asn1_cb = 0; 99 asn1_cb = 0;
95 100
96 switch(it->itype) { 101 switch (it->itype) {
97
98 case ASN1_ITYPE_PRIMITIVE: 102 case ASN1_ITYPE_PRIMITIVE:
99 if (it->templates) 103 if (it->templates)
100 ASN1_template_free(pval, it->templates); 104 ASN1_template_free(pval, it->templates);
@@ -147,10 +151,10 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
147 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL); 151 i = asn1_cb(ASN1_OP_FREE_PRE, pval, it, NULL);
148 if (i == 2) 152 if (i == 2)
149 return; 153 return;
150 } 154 }
151 asn1_enc_free(pval, it); 155 asn1_enc_free(pval, it);
152 /* If we free up as normal we will invalidate any 156 /* If we free up as normal we will invalidate any
153 * ANY DEFINED BY field and we wont be able to 157 * ANY DEFINED BY field and we wont be able to
154 * determine the type of the field it defines. So 158 * determine the type of the field it defines. So
155 * free up in reverse order. 159 * free up in reverse order.
156 */ 160 */
@@ -173,7 +177,8 @@ static void asn1_item_combine_free(ASN1_VALUE **pval, const ASN1_ITEM *it, int c
173 } 177 }
174} 178}
175 179
176void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 180void
181ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
177{ 182{
178 int i; 183 int i;
179 if (tt->flags & ASN1_TFLG_SK_MASK) { 184 if (tt->flags & ASN1_TFLG_SK_MASK) {
@@ -182,17 +187,17 @@ void ASN1_template_free(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
182 ASN1_VALUE *vtmp; 187 ASN1_VALUE *vtmp;
183 vtmp = sk_ASN1_VALUE_value(sk, i); 188 vtmp = sk_ASN1_VALUE_value(sk, i);
184 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item), 189 asn1_item_combine_free(&vtmp, ASN1_ITEM_ptr(tt->item),
185 0); 190 0);
186 } 191 }
187 sk_ASN1_VALUE_free(sk); 192 sk_ASN1_VALUE_free(sk);
188 *pval = NULL; 193 *pval = NULL;
189 } 194 } else
190 else
191 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item), 195 asn1_item_combine_free(pval, ASN1_ITEM_ptr(tt->item),
192 tt->flags & ASN1_TFLG_COMBINE); 196 tt->flags & ASN1_TFLG_COMBINE);
193} 197}
194 198
195void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 199void
200ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
196{ 201{
197 int utype; 202 int utype;
198 if (it) { 203 if (it) {
@@ -220,7 +225,7 @@ void ASN1_primitive_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
220 return; 225 return;
221 } 226 }
222 227
223 switch(utype) { 228 switch (utype) {
224 case V_ASN1_OBJECT: 229 case V_ASN1_OBJECT:
225 ASN1_OBJECT_free((ASN1_OBJECT *)*pval); 230 ASN1_OBJECT_free((ASN1_OBJECT *)*pval);
226 break; 231 break;
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_new.c b/src/lib/libssl/src/crypto/asn1/tasn_new.c
index 00aa31dd63..dc9ddc413a 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_new.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_new.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
@@ -65,12 +65,13 @@
65#include <string.h> 65#include <string.h>
66 66
67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 67static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
68 int combine); 68 int combine);
69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 69static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt); 70static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt);
71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it); 71static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it);
72 72
73ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it) 73ASN1_VALUE *
74ASN1_item_new(const ASN1_ITEM *it)
74{ 75{
75 ASN1_VALUE *ret = NULL; 76 ASN1_VALUE *ret = NULL;
76 if (ASN1_item_ex_new(&ret, it) > 0) 77 if (ASN1_item_ex_new(&ret, it) > 0)
@@ -80,13 +81,14 @@ ASN1_VALUE *ASN1_item_new(const ASN1_ITEM *it)
80 81
81/* Allocate an ASN1 structure */ 82/* Allocate an ASN1 structure */
82 83
83int ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 84int
85ASN1_item_ex_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
84{ 86{
85 return asn1_item_ex_combine_new(pval, it, 0); 87 return asn1_item_ex_combine_new(pval, it, 0);
86} 88}
87 89
88static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, 90static int
89 int combine) 91asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it, int combine)
90{ 92{
91 const ASN1_TEMPLATE *tt = NULL; 93 const ASN1_TEMPLATE *tt = NULL;
92 const ASN1_COMPAT_FUNCS *cf; 94 const ASN1_COMPAT_FUNCS *cf;
@@ -95,20 +97,21 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
95 ASN1_aux_cb *asn1_cb; 97 ASN1_aux_cb *asn1_cb;
96 ASN1_VALUE **pseqval; 98 ASN1_VALUE **pseqval;
97 int i; 99 int i;
100
98 if (aux && aux->asn1_cb) 101 if (aux && aux->asn1_cb)
99 asn1_cb = aux->asn1_cb; 102 asn1_cb = aux->asn1_cb;
100 else 103 else
101 asn1_cb = 0; 104 asn1_cb = 0;
102 105
103 if (!combine) *pval = NULL; 106 if (!combine)
107 *pval = NULL;
104 108
105#ifdef CRYPTO_MDEBUG 109#ifdef CRYPTO_MDEBUG
106 if (it->sname) 110 if (it->sname)
107 CRYPTO_push_info(it->sname); 111 CRYPTO_push_info(it->sname);
108#endif 112#endif
109 113
110 switch(it->itype) { 114 switch (it->itype) {
111
112 case ASN1_ITYPE_EXTERN: 115 case ASN1_ITYPE_EXTERN:
113 ef = it->funcs; 116 ef = it->funcs;
114 if (ef && ef->asn1_ex_new) { 117 if (ef && ef->asn1_ex_new) {
@@ -130,14 +133,13 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
130 if (it->templates) { 133 if (it->templates) {
131 if (!ASN1_template_new(pval, it->templates)) 134 if (!ASN1_template_new(pval, it->templates))
132 goto memerr; 135 goto memerr;
133 } 136 } else if (!ASN1_primitive_new(pval, it))
134 else if (!ASN1_primitive_new(pval, it)) 137 goto memerr;
135 goto memerr;
136 break; 138 break;
137 139
138 case ASN1_ITYPE_MSTRING: 140 case ASN1_ITYPE_MSTRING:
139 if (!ASN1_primitive_new(pval, it)) 141 if (!ASN1_primitive_new(pval, it))
140 goto memerr; 142 goto memerr;
141 break; 143 break;
142 144
143 case ASN1_ITYPE_CHOICE: 145 case ASN1_ITYPE_CHOICE:
@@ -145,7 +147,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
145 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); 147 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
146 if (!i) 148 if (!i)
147 goto auxerr; 149 goto auxerr;
148 if (i==2) { 150 if (i == 2) {
149#ifdef CRYPTO_MDEBUG 151#ifdef CRYPTO_MDEBUG
150 if (it->sname) 152 if (it->sname)
151 CRYPTO_pop_info(); 153 CRYPTO_pop_info();
@@ -161,7 +163,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
161 } 163 }
162 asn1_set_choice_selector(pval, -1, it); 164 asn1_set_choice_selector(pval, -1, it);
163 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) 165 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
164 goto auxerr; 166 goto auxerr;
165 break; 167 break;
166 168
167 case ASN1_ITYPE_NDEF_SEQUENCE: 169 case ASN1_ITYPE_NDEF_SEQUENCE:
@@ -170,7 +172,7 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
170 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL); 172 i = asn1_cb(ASN1_OP_NEW_PRE, pval, it, NULL);
171 if (!i) 173 if (!i)
172 goto auxerr; 174 goto auxerr;
173 if (i==2) { 175 if (i == 2) {
174#ifdef CRYPTO_MDEBUG 176#ifdef CRYPTO_MDEBUG
175 if (it->sname) 177 if (it->sname)
176 CRYPTO_pop_info(); 178 CRYPTO_pop_info();
@@ -192,47 +194,50 @@ static int asn1_item_ex_combine_new(ASN1_VALUE **pval, const ASN1_ITEM *it,
192 goto memerr; 194 goto memerr;
193 } 195 }
194 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL)) 196 if (asn1_cb && !asn1_cb(ASN1_OP_NEW_POST, pval, it, NULL))
195 goto auxerr; 197 goto auxerr;
196 break; 198 break;
197} 199 }
198#ifdef CRYPTO_MDEBUG 200#ifdef CRYPTO_MDEBUG
199 if (it->sname) CRYPTO_pop_info(); 201 if (it->sname)
202 CRYPTO_pop_info();
200#endif 203#endif
201 return 1; 204 return 1;
202 205
203 memerr: 206memerr:
204 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE); 207 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ERR_R_MALLOC_FAILURE);
205#ifdef CRYPTO_MDEBUG 208#ifdef CRYPTO_MDEBUG
206 if (it->sname) CRYPTO_pop_info(); 209 if (it->sname)
210 CRYPTO_pop_info();
207#endif 211#endif
208 return 0; 212 return 0;
209 213
210 auxerr: 214auxerr:
211 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR); 215 ASN1err(ASN1_F_ASN1_ITEM_EX_COMBINE_NEW, ASN1_R_AUX_ERROR);
212 ASN1_item_ex_free(pval, it); 216 ASN1_item_ex_free(pval, it);
213#ifdef CRYPTO_MDEBUG 217#ifdef CRYPTO_MDEBUG
214 if (it->sname) CRYPTO_pop_info(); 218 if (it->sname)
219 CRYPTO_pop_info();
215#endif 220#endif
216 return 0; 221 return 0;
217 222
218} 223}
219 224
220static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 225static void
226asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
221{ 227{
222 const ASN1_EXTERN_FUNCS *ef; 228 const ASN1_EXTERN_FUNCS *ef;
223 229
224 switch(it->itype) { 230 switch (it->itype) {
225
226 case ASN1_ITYPE_EXTERN: 231 case ASN1_ITYPE_EXTERN:
227 ef = it->funcs; 232 ef = it->funcs;
228 if (ef && ef->asn1_ex_clear) 233 if (ef && ef->asn1_ex_clear)
229 ef->asn1_ex_clear(pval, it); 234 ef->asn1_ex_clear(pval, it);
230 else *pval = NULL; 235 else
236 *pval = NULL;
231 break; 237 break;
232 238
233
234 case ASN1_ITYPE_PRIMITIVE: 239 case ASN1_ITYPE_PRIMITIVE:
235 if (it->templates) 240 if (it->templates)
236 asn1_template_clear(pval, it->templates); 241 asn1_template_clear(pval, it->templates);
237 else 242 else
238 asn1_primitive_clear(pval, it); 243 asn1_primitive_clear(pval, it);
@@ -251,11 +256,12 @@ static void asn1_item_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
251 } 256 }
252} 257}
253 258
254 259int
255int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 260ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
256{ 261{
257 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item); 262 const ASN1_ITEM *it = ASN1_ITEM_ptr(tt->item);
258 int ret; 263 int ret;
264
259 if (tt->flags & ASN1_TFLG_OPTIONAL) { 265 if (tt->flags & ASN1_TFLG_OPTIONAL) {
260 asn1_template_clear(pval, tt); 266 asn1_template_clear(pval, tt);
261 return 1; 267 return 1;
@@ -285,7 +291,7 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
285 } 291 }
286 /* Otherwise pass it back to the item routine */ 292 /* Otherwise pass it back to the item routine */
287 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE); 293 ret = asn1_item_ex_combine_new(pval, it, tt->flags & ASN1_TFLG_COMBINE);
288 done: 294done:
289#ifdef CRYPTO_MDEBUG 295#ifdef CRYPTO_MDEBUG
290 if (it->sname) 296 if (it->sname)
291 CRYPTO_pop_info(); 297 CRYPTO_pop_info();
@@ -293,10 +299,11 @@ int ASN1_template_new(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
293 return ret; 299 return ret;
294} 300}
295 301
296static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 302static void
303asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
297{ 304{
298 /* If ADB or STACK just NULL the field */ 305 /* If ADB or STACK just NULL the field */
299 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK)) 306 if (tt->flags & (ASN1_TFLG_ADB_MASK|ASN1_TFLG_SK_MASK))
300 *pval = NULL; 307 *pval = NULL;
301 else 308 else
302 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item)); 309 asn1_item_clear(pval, ASN1_ITEM_ptr(tt->item));
@@ -307,7 +314,8 @@ static void asn1_template_clear(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
307 * all the old functions. 314 * all the old functions.
308 */ 315 */
309 316
310int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it) 317int
318ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
311{ 319{
312 ASN1_TYPE *typ; 320 ASN1_TYPE *typ;
313 ASN1_STRING *str; 321 ASN1_STRING *str;
@@ -323,7 +331,7 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
323 utype = -1; 331 utype = -1;
324 else 332 else
325 utype = it->utype; 333 utype = it->utype;
326 switch(utype) { 334 switch (utype) {
327 case V_ASN1_OBJECT: 335 case V_ASN1_OBJECT:
328 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef); 336 *pval = (ASN1_VALUE *)OBJ_nid2obj(NID_undef);
329 return 1; 337 return 1;
@@ -357,14 +365,15 @@ int ASN1_primitive_new(ASN1_VALUE **pval, const ASN1_ITEM *it)
357 return 0; 365 return 0;
358} 366}
359 367
360static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it) 368static void
369asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
361{ 370{
362 int utype; 371 int utype;
363 if (it && it->funcs) { 372 if (it && it->funcs) {
364 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs; 373 const ASN1_PRIMITIVE_FUNCS *pf = it->funcs;
365 if (pf->prim_clear) 374 if (pf->prim_clear)
366 pf->prim_clear(pval, it); 375 pf->prim_clear(pval, it);
367 else 376 else
368 *pval = NULL; 377 *pval = NULL;
369 return; 378 return;
370 } 379 }
@@ -374,5 +383,6 @@ static void asn1_primitive_clear(ASN1_VALUE **pval, const ASN1_ITEM *it)
374 utype = it->utype; 383 utype = it->utype;
375 if (utype == V_ASN1_BOOLEAN) 384 if (utype == V_ASN1_BOOLEAN)
376 *(ASN1_BOOLEAN *)pval = it->size; 385 *(ASN1_BOOLEAN *)pval = it->size;
377 else *pval = NULL; 386 else
387 *pval = NULL;
378} 388}
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_prn.c b/src/lib/libssl/src/crypto/asn1/tasn_prn.c
index 345daeb0f3..13624991c4 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_prn.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_prn.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 "cryptlib.h" 60#include "cryptlib.h"
62#include <openssl/asn1.h> 61#include <openssl/asn1.h>
@@ -72,17 +71,17 @@
72 71
73/* ASN1_PCTX routines */ 72/* ASN1_PCTX routines */
74 73
75ASN1_PCTX default_pctx = 74ASN1_PCTX default_pctx = {
76{
77 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */ 75 ASN1_PCTX_FLAGS_SHOW_ABSENT, /* flags */
78 0, /* nm_flags */ 76 0, /* nm_flags */
79 0, /* cert_flags */ 77 0, /* cert_flags */
80 0, /* oid_flags */ 78 0, /* oid_flags */
81 0 /* str_flags */ 79 0 /* str_flags */
82}; 80};
83
84 81
85ASN1_PCTX *ASN1_PCTX_new(void) 82
83ASN1_PCTX *
84ASN1_PCTX_new(void)
86{ 85{
87 ASN1_PCTX *ret; 86 ASN1_PCTX *ret;
88 ret = malloc(sizeof(ASN1_PCTX)); 87 ret = malloc(sizeof(ASN1_PCTX));
@@ -98,57 +97,68 @@ ASN1_PCTX *ASN1_PCTX_new(void)
98 return ret; 97 return ret;
99} 98}
100 99
101void ASN1_PCTX_free(ASN1_PCTX *p) 100void
101ASN1_PCTX_free(ASN1_PCTX *p)
102{ 102{
103 free(p); 103 free(p);
104} 104}
105 105
106unsigned long ASN1_PCTX_get_flags(ASN1_PCTX *p) 106unsigned long
107ASN1_PCTX_get_flags(ASN1_PCTX *p)
107{ 108{
108 return p->flags; 109 return p->flags;
109} 110}
110 111
111void ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags) 112void
113ASN1_PCTX_set_flags(ASN1_PCTX *p, unsigned long flags)
112{ 114{
113 p->flags = flags; 115 p->flags = flags;
114} 116}
115 117
116unsigned long ASN1_PCTX_get_nm_flags(ASN1_PCTX *p) 118unsigned long
119ASN1_PCTX_get_nm_flags(ASN1_PCTX *p)
117{ 120{
118 return p->nm_flags; 121 return p->nm_flags;
119} 122}
120 123
121void ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags) 124void
125ASN1_PCTX_set_nm_flags(ASN1_PCTX *p, unsigned long flags)
122{ 126{
123 p->nm_flags = flags; 127 p->nm_flags = flags;
124} 128}
125 129
126unsigned long ASN1_PCTX_get_cert_flags(ASN1_PCTX *p) 130unsigned long
131ASN1_PCTX_get_cert_flags(ASN1_PCTX *p)
127{ 132{
128 return p->cert_flags; 133 return p->cert_flags;
129} 134}
130 135
131void ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags) 136void
137ASN1_PCTX_set_cert_flags(ASN1_PCTX *p, unsigned long flags)
132{ 138{
133 p->cert_flags = flags; 139 p->cert_flags = flags;
134} 140}
135 141
136unsigned long ASN1_PCTX_get_oid_flags(ASN1_PCTX *p) 142unsigned long
143ASN1_PCTX_get_oid_flags(ASN1_PCTX *p)
137{ 144{
138 return p->oid_flags; 145 return p->oid_flags;
139} 146}
140 147
141void ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags) 148void
149ASN1_PCTX_set_oid_flags(ASN1_PCTX *p, unsigned long flags)
142{ 150{
143 p->oid_flags = flags; 151 p->oid_flags = flags;
144} 152}
145 153
146unsigned long ASN1_PCTX_get_str_flags(ASN1_PCTX *p) 154unsigned long
155ASN1_PCTX_get_str_flags(ASN1_PCTX *p)
147{ 156{
148 return p->str_flags; 157 return p->str_flags;
149} 158}
150 159
151void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags) 160void
161ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
152{ 162{
153 p->str_flags = flags; 163 p->str_flags = flags;
154} 164}
@@ -156,40 +166,38 @@ void ASN1_PCTX_set_str_flags(ASN1_PCTX *p, unsigned long flags)
156/* Main print routines */ 166/* Main print routines */
157 167
158static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 168static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
159 const ASN1_ITEM *it, 169 const ASN1_ITEM *it, const char *fname, const char *sname, int nohdr,
160 const char *fname, const char *sname, 170 const ASN1_PCTX *pctx);
161 int nohdr, const ASN1_PCTX *pctx);
162 171
163int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 172int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
164 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx); 173 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx);
165 174
166static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld, 175static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
167 const ASN1_ITEM *it, int indent, 176 const ASN1_ITEM *it, int indent, const char *fname, const char *sname,
168 const char *fname, const char *sname, 177 const ASN1_PCTX *pctx);
169 const ASN1_PCTX *pctx);
170 178
171static int asn1_print_fsname(BIO *out, int indent, 179static int asn1_print_fsname(BIO *out, int indent, const char *fname,
172 const char *fname, const char *sname, 180 const char *sname, const ASN1_PCTX *pctx);
173 const ASN1_PCTX *pctx);
174 181
175int ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, 182int
176 const ASN1_ITEM *it, const ASN1_PCTX *pctx) 183ASN1_item_print(BIO *out, ASN1_VALUE *ifld, int indent, const ASN1_ITEM *it,
184 const ASN1_PCTX *pctx)
177{ 185{
178 const char *sname; 186 const char *sname;
187
179 if (pctx == NULL) 188 if (pctx == NULL)
180 pctx = &default_pctx; 189 pctx = &default_pctx;
181 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME) 190 if (pctx->flags & ASN1_PCTX_FLAGS_NO_STRUCT_NAME)
182 sname = NULL; 191 sname = NULL;
183 else 192 else
184 sname = it->sname; 193 sname = it->sname;
185 return asn1_item_print_ctx(out, &ifld, indent, it, 194 return asn1_item_print_ctx(out, &ifld, indent, it, NULL, sname,
186 NULL, sname, 0, pctx); 195 0, pctx);
187} 196}
188 197
189static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 198static int
190 const ASN1_ITEM *it, 199asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, const ASN1_ITEM *it,
191 const char *fname, const char *sname, 200 const char *fname, const char *sname, int nohdr, const ASN1_PCTX *pctx)
192 int nohdr, const ASN1_PCTX *pctx)
193{ 201{
194 const ASN1_TEMPLATE *tt; 202 const ASN1_TEMPLATE *tt;
195 const ASN1_EXTERN_FUNCS *ef; 203 const ASN1_EXTERN_FUNCS *ef;
@@ -198,18 +206,19 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
198 ASN1_aux_cb *asn1_cb; 206 ASN1_aux_cb *asn1_cb;
199 ASN1_PRINT_ARG parg; 207 ASN1_PRINT_ARG parg;
200 int i; 208 int i;
209
201 if (aux && aux->asn1_cb) { 210 if (aux && aux->asn1_cb) {
202 parg.out = out; 211 parg.out = out;
203 parg.indent = indent; 212 parg.indent = indent;
204 parg.pctx = pctx; 213 parg.pctx = pctx;
205 asn1_cb = aux->asn1_cb; 214 asn1_cb = aux->asn1_cb;
206 } 215 } else
207 else asn1_cb = 0; 216 asn1_cb = 0;
208 217
209 if(*fld == NULL) { 218 if (*fld == NULL) {
210 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) { 219 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_ABSENT) {
211 if (!nohdr && !asn1_print_fsname(out, indent, 220 if (!nohdr &&
212 fname, sname, pctx)) 221 !asn1_print_fsname(out, indent, fname, sname, pctx))
213 return 0; 222 return 0;
214 if (BIO_puts(out, "<ABSENT>\n") <= 0) 223 if (BIO_puts(out, "<ABSENT>\n") <= 0)
215 return 0; 224 return 0;
@@ -217,22 +226,23 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
217 return 1; 226 return 1;
218 } 227 }
219 228
220 switch(it->itype) { 229 switch (it->itype) {
221 case ASN1_ITYPE_PRIMITIVE: 230 case ASN1_ITYPE_PRIMITIVE:
222 if(it->templates) { 231 if (it->templates) {
223 if (!asn1_template_print_ctx(out, fld, indent, 232 if (!asn1_template_print_ctx(out, fld, indent,
224 it->templates, pctx)) 233 it->templates, pctx))
225 return 0; 234 return 0;
226 } 235 }
227 /* fall thru */ 236 /* fall thru */
228 case ASN1_ITYPE_MSTRING: 237 case ASN1_ITYPE_MSTRING:
229 if (!asn1_primitive_print(out, fld, it, 238 if (!asn1_primitive_print(out, fld, it,
230 indent, fname, sname,pctx)) 239 indent, fname, sname, pctx))
231 return 0; 240 return 0;
232 break; 241 break;
233 242
234 case ASN1_ITYPE_EXTERN: 243 case ASN1_ITYPE_EXTERN:
235 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 244 if (!nohdr &&
245 !asn1_print_fsname(out, indent, fname, sname, pctx))
236 return 0; 246 return 0;
237 /* Use new style print routine if possible */ 247 /* Use new style print routine if possible */
238 ef = it->funcs; 248 ef = it->funcs;
@@ -243,23 +253,23 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
243 if ((i == 2) && (BIO_puts(out, "\n") <= 0)) 253 if ((i == 2) && (BIO_puts(out, "\n") <= 0))
244 return 0; 254 return 0;
245 return 1; 255 return 1;
246 } 256 } else if (sname &&
247 else if (sname && 257 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
248 BIO_printf(out, ":EXTERNAL TYPE %s\n", sname) <= 0)
249 return 0; 258 return 0;
250 break; 259 break;
251 260
252 case ASN1_ITYPE_CHOICE: 261 case ASN1_ITYPE_CHOICE:
253#if 0 262#if 0
254 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 263 if (!nohdr &&
264 !asn1_print_fsname(out, indent, fname, sname, pctx))
255 return 0; 265 return 0;
256#endif 266#endif
257 /* CHOICE type, get selector */ 267 /* CHOICE type, get selector */
258 i = asn1_get_choice_selector(fld, it); 268 i = asn1_get_choice_selector(fld, it);
259 /* This should never happen... */ 269 /* This should never happen... */
260 if((i < 0) || (i >= it->tcount)) { 270 if ((i < 0) || (i >= it->tcount)) {
261 if (BIO_printf(out, 271 if (BIO_printf(out,
262 "ERROR: selector [%d] invalid\n", i) <= 0) 272 "ERROR: selector [%d] invalid\n", i) <= 0)
263 return 0; 273 return 0;
264 return 1; 274 return 1;
265 } 275 }
@@ -271,7 +281,8 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
271 281
272 case ASN1_ITYPE_SEQUENCE: 282 case ASN1_ITYPE_SEQUENCE:
273 case ASN1_ITYPE_NDEF_SEQUENCE: 283 case ASN1_ITYPE_NDEF_SEQUENCE:
274 if (!nohdr && !asn1_print_fsname(out, indent, fname, sname, pctx)) 284 if (!nohdr &&
285 !asn1_print_fsname(out, indent, fname, sname, pctx))
275 return 0; 286 return 0;
276 if (fname || sname) { 287 if (fname || sname) {
277 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 288 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
@@ -292,12 +303,12 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
292 } 303 }
293 304
294 /* Print each field entry */ 305 /* Print each field entry */
295 for(i = 0, tt = it->templates; i < it->tcount; i++, tt++) { 306 for (i = 0, tt = it->templates; i < it->tcount; i++, tt++) {
296 const ASN1_TEMPLATE *seqtt; 307 const ASN1_TEMPLATE *seqtt;
297 seqtt = asn1_do_adb(fld, tt, 1); 308 seqtt = asn1_do_adb(fld, tt, 1);
298 tmpfld = asn1_get_field_ptr(fld, seqtt); 309 tmpfld = asn1_get_field_ptr(fld, seqtt);
299 if (!asn1_template_print_ctx(out, tmpfld, 310 if (!asn1_template_print_ctx(out, tmpfld, indent + 2,
300 indent + 2, seqtt, pctx)) 311 seqtt, pctx))
301 return 0; 312 return 0;
302 } 313 }
303 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 314 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
@@ -312,7 +323,7 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
312 } 323 }
313 break; 324 break;
314 325
315 default: 326 default:
316 BIO_printf(out, "Unprocessed type %d\n", it->itype); 327 BIO_printf(out, "Unprocessed type %d\n", it->itype);
317 return 0; 328 return 0;
318 } 329 }
@@ -320,65 +331,65 @@ static int asn1_item_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
320 return 1; 331 return 1;
321} 332}
322 333
323int asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent, 334int
324 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx) 335asn1_template_print_ctx(BIO *out, ASN1_VALUE **fld, int indent,
336 const ASN1_TEMPLATE *tt, const ASN1_PCTX *pctx)
325{ 337{
326 int i, flags; 338 int i, flags;
327 const char *sname, *fname; 339 const char *sname, *fname;
340
328 flags = tt->flags; 341 flags = tt->flags;
329 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME) 342 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME)
330 sname = ASN1_ITEM_ptr(tt->item)->sname; 343 sname = ASN1_ITEM_ptr(tt->item)->sname;
331 else 344 else
332 sname = NULL; 345 sname = NULL;
333 if(pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME) 346 if (pctx->flags & ASN1_PCTX_FLAGS_NO_FIELD_NAME)
334 fname = NULL; 347 fname = NULL;
335 else 348 else
336 fname = tt->field_name; 349 fname = tt->field_name;
337 if(flags & ASN1_TFLG_SK_MASK) { 350 if (flags & ASN1_TFLG_SK_MASK) {
338 char *tname; 351 char *tname;
339 ASN1_VALUE *skitem; 352 ASN1_VALUE *skitem;
340 STACK_OF(ASN1_VALUE) *stack; 353 STACK_OF(ASN1_VALUE) *stack;
341 354
342 /* SET OF, SEQUENCE OF */ 355 /* SET OF, SEQUENCE OF */
343 if (fname) { 356 if (fname) {
344 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) { 357 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SSOF) {
345 if(flags & ASN1_TFLG_SET_OF) 358 if (flags & ASN1_TFLG_SET_OF)
346 tname = "SET"; 359 tname = "SET";
347 else 360 else
348 tname = "SEQUENCE"; 361 tname = "SEQUENCE";
349 if (BIO_printf(out, "%*s%s OF %s {\n", 362 if (BIO_printf(out, "%*s%s OF %s {\n",
350 indent, "", tname, tt->field_name) <= 0) 363 indent, "", tname, tt->field_name) <= 0)
351 return 0; 364 return 0;
352 } 365 } else if (BIO_printf(out, "%*s%s:\n", indent, "",
353 else if (BIO_printf(out, "%*s%s:\n", indent, "", 366 fname) <= 0)
354 fname) <= 0)
355 return 0; 367 return 0;
356 } 368 }
357 stack = (STACK_OF(ASN1_VALUE) *)*fld; 369 stack = (STACK_OF(ASN1_VALUE) *)*fld;
358 for(i = 0; i < sk_ASN1_VALUE_num(stack); i++) { 370 for (i = 0; i < sk_ASN1_VALUE_num(stack); i++) {
359 if ((i > 0) && (BIO_puts(out, "\n") <= 0)) 371 if ((i > 0) && (BIO_puts(out, "\n") <= 0))
360 return 0; 372 return 0;
361
362 skitem = sk_ASN1_VALUE_value(stack, i); 373 skitem = sk_ASN1_VALUE_value(stack, i);
363 if (!asn1_item_print_ctx(out, &skitem, indent + 2, 374 if (!asn1_item_print_ctx(out, &skitem, indent + 2,
364 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx)) 375 ASN1_ITEM_ptr(tt->item), NULL, NULL, 1, pctx))
365 return 0; 376 return 0;
366 } 377 }
367 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0) 378 if (!i && BIO_printf(out, "%*s<EMPTY>\n", indent + 2, "") <= 0)
368 return 0; 379 return 0;
369 if(pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) { 380 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_SEQUENCE) {
370 if (BIO_printf(out, "%*s}\n", indent, "") <= 0) 381 if (BIO_printf(out, "%*s}\n", indent, "") <= 0)
371 return 0; 382 return 0;
372 } 383 }
373 return 1; 384 return 1;
374 } 385 }
375 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item), 386 return asn1_item_print_ctx(out, fld, indent, ASN1_ITEM_ptr(tt->item),
376 fname, sname, 0, pctx); 387 fname, sname, 0, pctx);
377} 388}
378 389
379static int asn1_print_fsname(BIO *out, int indent, 390static int
380 const char *fname, const char *sname, 391asn1_print_fsname(BIO *out, int indent, const char *fname, const char *sname,
381 const ASN1_PCTX *pctx) 392 const ASN1_PCTX *pctx)
382{ 393{
383 static char spaces[] = " "; 394 static char spaces[] = " ";
384 const int nspaces = sizeof(spaces) - 1; 395 const int nspaces = sizeof(spaces) - 1;
@@ -419,8 +430,8 @@ static int asn1_print_fsname(BIO *out, int indent,
419 return 1; 430 return 1;
420} 431}
421 432
422static int asn1_print_boolean_ctx(BIO *out, int boolval, 433static int
423 const ASN1_PCTX *pctx) 434asn1_print_boolean_ctx(BIO *out, int boolval, const ASN1_PCTX *pctx)
424{ 435{
425 const char *str; 436 const char *str;
426 switch (boolval) { 437 switch (boolval) {
@@ -432,7 +443,7 @@ static int asn1_print_boolean_ctx(BIO *out, int boolval,
432 str = "FALSE"; 443 str = "FALSE";
433 break; 444 break;
434 445
435 default: 446 default:
436 str = "TRUE"; 447 str = "TRUE";
437 break; 448 break;
438 449
@@ -444,8 +455,8 @@ static int asn1_print_boolean_ctx(BIO *out, int boolval,
444 455
445} 456}
446 457
447static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, 458static int
448 const ASN1_PCTX *pctx) 459asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str, const ASN1_PCTX *pctx)
449{ 460{
450 char *s; 461 char *s;
451 int ret = 1; 462 int ret = 1;
@@ -456,13 +467,13 @@ static int asn1_print_integer_ctx(BIO *out, ASN1_INTEGER *str,
456 return ret; 467 return ret;
457} 468}
458 469
459static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, 470static int
460 const ASN1_PCTX *pctx) 471asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid, const ASN1_PCTX *pctx)
461{ 472{
462 char objbuf[80]; 473 char objbuf[80];
463 const char *ln; 474 const char *ln;
464 ln = OBJ_nid2ln(OBJ_obj2nid(oid)); 475 ln = OBJ_nid2ln(OBJ_obj2nid(oid));
465 if(!ln) 476 if (!ln)
466 ln = ""; 477 ln = "";
467 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1); 478 OBJ_obj2txt(objbuf, sizeof objbuf, oid, 1);
468 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0) 479 if (BIO_printf(out, "%s (%s)", ln, objbuf) <= 0)
@@ -470,36 +481,36 @@ static int asn1_print_oid_ctx(BIO *out, const ASN1_OBJECT *oid,
470 return 1; 481 return 1;
471} 482}
472 483
473static int asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent, 484static int
474 const ASN1_PCTX *pctx) 485asn1_print_obstring_ctx(BIO *out, ASN1_STRING *str, int indent,
486 const ASN1_PCTX *pctx)
475{ 487{
476 if (str->type == V_ASN1_BIT_STRING) { 488 if (str->type == V_ASN1_BIT_STRING) {
477 if (BIO_printf(out, " (%ld unused bits)\n", 489 if (BIO_printf(out, " (%ld unused bits)\n",
478 str->flags & 0x7) <= 0) 490 str->flags & 0x7) <= 0)
479 return 0; 491 return 0;
480 } 492 } else if (BIO_puts(out, "\n") <= 0)
481 else if (BIO_puts(out, "\n") <= 0)
482 return 0; 493 return 0;
483 if ((str->length > 0) 494 if ((str->length > 0) &&
484 && BIO_dump_indent(out, (char *)str->data, str->length, 495 BIO_dump_indent(out, (char *)str->data, str->length,
485 indent + 2) <= 0) 496 indent + 2) <= 0)
486 return 0; 497 return 0;
487 return 1; 498 return 1;
488} 499}
489 500
490static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld, 501static int
491 const ASN1_ITEM *it, int indent, 502asn1_primitive_print(BIO *out, ASN1_VALUE **fld, const ASN1_ITEM *it,
492 const char *fname, const char *sname, 503 int indent, const char *fname, const char *sname, const ASN1_PCTX *pctx)
493 const ASN1_PCTX *pctx)
494{ 504{
495 long utype; 505 long utype;
496 ASN1_STRING *str; 506 ASN1_STRING *str;
497 int ret = 1, needlf = 1; 507 int ret = 1, needlf = 1;
498 const char *pname; 508 const char *pname;
499 const ASN1_PRIMITIVE_FUNCS *pf; 509 const ASN1_PRIMITIVE_FUNCS *pf;
510
500 pf = it->funcs; 511 pf = it->funcs;
501 if (!asn1_print_fsname(out, indent, fname, sname, pctx)) 512 if (!asn1_print_fsname(out, indent, fname, sname, pctx))
502 return 0; 513 return 0;
503 if (pf && pf->prim_print) 514 if (pf && pf->prim_print)
504 return pf->prim_print(out, fld, it, indent, pctx); 515 return pf->prim_print(out, fld, it, indent, pctx);
505 str = (ASN1_STRING *)*fld; 516 str = (ASN1_STRING *)*fld;
@@ -514,12 +525,12 @@ static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
514 str = (ASN1_STRING *)*fld; 525 str = (ASN1_STRING *)*fld;
515 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE) 526 if (pctx->flags & ASN1_PCTX_FLAGS_NO_ANY_TYPE)
516 pname = NULL; 527 pname = NULL;
517 else 528 else
518 pname = ASN1_tag2str(utype); 529 pname = ASN1_tag2str(utype);
519 } else { 530 } else {
520 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE) 531 if (pctx->flags & ASN1_PCTX_FLAGS_SHOW_TYPE)
521 pname = ASN1_tag2str(utype); 532 pname = ASN1_tag2str(utype);
522 else 533 else
523 pname = NULL; 534 pname = NULL;
524 } 535 }
525 536
@@ -575,14 +586,13 @@ static int asn1_primitive_print(BIO *out, ASN1_VALUE **fld,
575 if (BIO_puts(out, "\n") <= 0) 586 if (BIO_puts(out, "\n") <= 0)
576 return 0; 587 return 0;
577 if (ASN1_parse_dump(out, str->data, str->length, 588 if (ASN1_parse_dump(out, str->data, str->length,
578 indent, 0) <= 0) 589 indent, 0) <= 0)
579 ret = 0; 590 ret = 0;
580 needlf = 0; 591 needlf = 0;
581 break; 592 break;
582 593
583 default: 594 default:
584 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags); 595 ret = ASN1_STRING_print_ex(out, str, pctx->str_flags);
585
586 } 596 }
587 if (!ret) 597 if (!ret)
588 return 0; 598 return 0;
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_typ.c b/src/lib/libssl/src/crypto/asn1/tasn_typ.c
index 6fb1c372da..3399e74d84 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_typ.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_typ.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
@@ -136,12 +136,12 @@ IMPLEMENT_ASN1_TYPE_ex(ASN1_FBOOLEAN, ASN1_BOOLEAN, 0)
136 136
137IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF) 137IMPLEMENT_ASN1_TYPE_ex(ASN1_OCTET_STRING_NDEF, ASN1_OCTET_STRING, ASN1_TFLG_NDEF)
138 138
139ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) = 139ASN1_ITEM_TEMPLATE(ASN1_SEQUENCE_ANY) =
140 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY) 140 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SEQUENCE_OF, 0, ASN1_SEQUENCE_ANY, ASN1_ANY)
141ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY) 141ASN1_ITEM_TEMPLATE_END(ASN1_SEQUENCE_ANY)
142 142
143ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) = 143ASN1_ITEM_TEMPLATE(ASN1_SET_ANY) =
144 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY) 144 ASN1_EX_TEMPLATE_TYPE(ASN1_TFLG_SET_OF, 0, ASN1_SET_ANY, ASN1_ANY)
145ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY) 145ASN1_ITEM_TEMPLATE_END(ASN1_SET_ANY)
146 146
147IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY) 147IMPLEMENT_ASN1_ENCODE_FUNCTIONS_const_fname(ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY, ASN1_SEQUENCE_ANY)
diff --git a/src/lib/libssl/src/crypto/asn1/tasn_utl.c b/src/lib/libssl/src/crypto/asn1/tasn_utl.c
index 9715dac931..e1051c2c3e 100644
--- a/src/lib/libssl/src/crypto/asn1/tasn_utl.c
+++ b/src/lib/libssl/src/crypto/asn1/tasn_utl.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 <openssl/asn1.h> 61#include <openssl/asn1.h>
@@ -73,7 +72,8 @@
73 * the selector value 72 * the selector value
74 */ 73 */
75 74
76int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it) 75int
76asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
77{ 77{
78 int *sel = offset2ptr(*pval, it->utype); 78 int *sel = offset2ptr(*pval, it->utype);
79 return *sel; 79 return *sel;
@@ -83,8 +83,9 @@ int asn1_get_choice_selector(ASN1_VALUE **pval, const ASN1_ITEM *it)
83 * the selector value, return old value. 83 * the selector value, return old value.
84 */ 84 */
85 85
86int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it) 86int
87{ 87asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
88{
88 int *sel, ret; 89 int *sel, ret;
89 sel = offset2ptr(*pval, it->utype); 90 sel = offset2ptr(*pval, it->utype);
90 ret = *sel; 91 ret = *sel;
@@ -92,18 +93,20 @@ int asn1_set_choice_selector(ASN1_VALUE **pval, int value, const ASN1_ITEM *it)
92 return ret; 93 return ret;
93} 94}
94 95
95/* Do reference counting. The value 'op' decides what to do. 96/* Do reference counting. The value 'op' decides what to do.
96 * if it is +1 then the count is incremented. If op is 0 count is 97 * if it is +1 then the count is incremented. If op is 0 count is
97 * set to 1. If op is -1 count is decremented and the return value 98 * set to 1. If op is -1 count is decremented and the return value
98 * is the current refrence count or 0 if no reference count exists. 99 * is the current refrence count or 0 if no reference count exists.
99 */ 100 */
100 101
101int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it) 102int
103asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
102{ 104{
103 const ASN1_AUX *aux; 105 const ASN1_AUX *aux;
104 int *lck, ret; 106 int *lck, ret;
105 if ((it->itype != ASN1_ITYPE_SEQUENCE) 107
106 && (it->itype != ASN1_ITYPE_NDEF_SEQUENCE)) 108 if ((it->itype != ASN1_ITYPE_SEQUENCE) &&
109 (it->itype != ASN1_ITYPE_NDEF_SEQUENCE))
107 return 0; 110 return 0;
108 aux = it->funcs; 111 aux = it->funcs;
109 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT)) 112 if (!aux || !(aux->flags & ASN1_AFLG_REFCOUNT))
@@ -117,9 +120,11 @@ int asn1_do_lock(ASN1_VALUE **pval, int op, const ASN1_ITEM *it)
117 return ret; 120 return ret;
118} 121}
119 122
120static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it) 123static ASN1_ENCODING *
124asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
121{ 125{
122 const ASN1_AUX *aux; 126 const ASN1_AUX *aux;
127
123 if (!pval || !*pval) 128 if (!pval || !*pval)
124 return NULL; 129 return NULL;
125 aux = it->funcs; 130 aux = it->funcs;
@@ -128,9 +133,11 @@ static ASN1_ENCODING *asn1_get_enc_ptr(ASN1_VALUE **pval, const ASN1_ITEM *it)
128 return offset2ptr(*pval, aux->enc_offset); 133 return offset2ptr(*pval, aux->enc_offset);
129} 134}
130 135
131void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it) 136void
137asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
132{ 138{
133 ASN1_ENCODING *enc; 139 ASN1_ENCODING *enc;
140
134 enc = asn1_get_enc_ptr(pval, it); 141 enc = asn1_get_enc_ptr(pval, it);
135 if (enc) { 142 if (enc) {
136 enc->enc = NULL; 143 enc->enc = NULL;
@@ -139,9 +146,11 @@ void asn1_enc_init(ASN1_VALUE **pval, const ASN1_ITEM *it)
139 } 146 }
140} 147}
141 148
142void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it) 149void
150asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
143{ 151{
144 ASN1_ENCODING *enc; 152 ASN1_ENCODING *enc;
153
145 enc = asn1_get_enc_ptr(pval, it); 154 enc = asn1_get_enc_ptr(pval, it);
146 if (enc) { 155 if (enc) {
147 if (enc->enc) 156 if (enc->enc)
@@ -152,10 +161,12 @@ void asn1_enc_free(ASN1_VALUE **pval, const ASN1_ITEM *it)
152 } 161 }
153} 162}
154 163
155int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen, 164int
156 const ASN1_ITEM *it) 165asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
166 const ASN1_ITEM *it)
157{ 167{
158 ASN1_ENCODING *enc; 168 ASN1_ENCODING *enc;
169
159 enc = asn1_get_enc_ptr(pval, it); 170 enc = asn1_get_enc_ptr(pval, it);
160 if (!enc) 171 if (!enc)
161 return 1; 172 return 1;
@@ -171,11 +182,13 @@ int asn1_enc_save(ASN1_VALUE **pval, const unsigned char *in, int inlen,
171 182
172 return 1; 183 return 1;
173} 184}
174 185
175int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval, 186int
176 const ASN1_ITEM *it) 187asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
188 const ASN1_ITEM *it)
177{ 189{
178 ASN1_ENCODING *enc; 190 ASN1_ENCODING *enc;
191
179 enc = asn1_get_enc_ptr(pval, it); 192 enc = asn1_get_enc_ptr(pval, it);
180 if (!enc || enc->modified) 193 if (!enc || enc->modified)
181 return 0; 194 return 0;
@@ -189,9 +202,11 @@ int asn1_enc_restore(int *len, unsigned char **out, ASN1_VALUE **pval,
189} 202}
190 203
191/* Given an ASN1_TEMPLATE get a pointer to a field */ 204/* Given an ASN1_TEMPLATE get a pointer to a field */
192ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt) 205ASN1_VALUE **
206asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
193{ 207{
194 ASN1_VALUE **pvaltmp; 208 ASN1_VALUE **pvaltmp;
209
195 if (tt->flags & ASN1_TFLG_COMBINE) 210 if (tt->flags & ASN1_TFLG_COMBINE)
196 return pval; 211 return pval;
197 pvaltmp = offset2ptr(*pval, tt->offset); 212 pvaltmp = offset2ptr(*pval, tt->offset);
@@ -206,14 +221,15 @@ ASN1_VALUE ** asn1_get_field_ptr(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt)
206 * the relevant ASN1_TEMPLATE in the table and return it. 221 * the relevant ASN1_TEMPLATE in the table and return it.
207 */ 222 */
208 223
209const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, 224const ASN1_TEMPLATE *
210 int nullerr) 225asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt, int nullerr)
211{ 226{
212 const ASN1_ADB *adb; 227 const ASN1_ADB *adb;
213 const ASN1_ADB_TABLE *atbl; 228 const ASN1_ADB_TABLE *atbl;
214 long selector; 229 long selector;
215 ASN1_VALUE **sfld; 230 ASN1_VALUE **sfld;
216 int i; 231 int i;
232
217 if (!(tt->flags & ASN1_TFLG_ADB_MASK)) 233 if (!(tt->flags & ASN1_TFLG_ADB_MASK))
218 return tt; 234 return tt;
219 235
@@ -234,9 +250,9 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
234 * NB: don't check for NID_undef here because it 250 * NB: don't check for NID_undef here because it
235 * might be a legitimate value in the table 251 * might be a legitimate value in the table
236 */ 252 */
237 if (tt->flags & ASN1_TFLG_ADB_OID) 253 if (tt->flags & ASN1_TFLG_ADB_OID)
238 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld); 254 selector = OBJ_obj2nid((ASN1_OBJECT *)*sfld);
239 else 255 else
240 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld); 256 selector = ASN1_INTEGER_get((ASN1_INTEGER *)*sfld);
241 257
242 /* Try to find matching entry in table 258 /* Try to find matching entry in table
@@ -255,13 +271,13 @@ const ASN1_TEMPLATE *asn1_do_adb(ASN1_VALUE **pval, const ASN1_TEMPLATE *tt,
255 271
256 /* No match, return default type */ 272 /* No match, return default type */
257 if (!adb->default_tt) 273 if (!adb->default_tt)
258 goto err; 274 goto err;
259 return adb->default_tt; 275 return adb->default_tt;
260 276
261 err: 277err:
262 /* FIXME: should log the value or OID of unsupported type */ 278 /* FIXME: should log the value or OID of unsupported type */
263 if (nullerr) 279 if (nullerr)
264 ASN1err(ASN1_F_ASN1_DO_ADB, 280 ASN1err(ASN1_F_ASN1_DO_ADB,
265 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE); 281 ASN1_R_UNSUPPORTED_ANY_DEFINED_BY_TYPE);
266 return NULL; 282 return NULL;
267} 283}