summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjsing <>2014-04-19 07:20:59 +0000
committerjsing <>2014-04-19 07:20:59 +0000
commit8ffabaf4eba42faf2565cf746f06a82adf194000 (patch)
tree700547d363cb502688982d0c0f83f28e94857640 /src
parent524832e77ced4928195feaa76ecd8d699007445d (diff)
downloadopenbsd-8ffabaf4eba42faf2565cf746f06a82adf194000.tar.gz
openbsd-8ffabaf4eba42faf2565cf746f06a82adf194000.tar.bz2
openbsd-8ffabaf4eba42faf2565cf746f06a82adf194000.zip
More KNF.
Diffstat (limited to 'src')
-rw-r--r--src/lib/libcrypto/asn1/asn_mime.c597
-rw-r--r--src/lib/libcrypto/asn1/asn_moid.c33
-rw-r--r--src/lib/libcrypto/asn1/asn_pack.c28
-rw-r--r--src/lib/libcrypto/asn1/bio_asn1.c152
-rw-r--r--src/lib/libcrypto/asn1/bio_ndef.c24
-rw-r--r--src/lib/libcrypto/asn1/charmap.h17
-rw-r--r--src/lib/libssl/src/crypto/asn1/asn_mime.c597
-rw-r--r--src/lib/libssl/src/crypto/asn1/asn_moid.c33
-rw-r--r--src/lib/libssl/src/crypto/asn1/asn_pack.c28
-rw-r--r--src/lib/libssl/src/crypto/asn1/bio_asn1.c152
-rw-r--r--src/lib/libssl/src/crypto/asn1/bio_ndef.c24
-rw-r--r--src/lib/libssl/src/crypto/asn1/charmap.h17
12 files changed, 942 insertions, 760 deletions
diff --git a/src/lib/libcrypto/asn1/asn_mime.c b/src/lib/libcrypto/asn1/asn_mime.c
index 6cde8fd44e..890557578d 100644
--- a/src/lib/libcrypto/asn1/asn_mime.c
+++ b/src/lib/libcrypto/asn1/asn_mime.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
@@ -71,24 +71,24 @@
71 */ 71 */
72 72
73typedef struct { 73typedef struct {
74char *param_name; /* Param name e.g. "micalg" */ 74 char *param_name; /* Param name e.g. "micalg" */
75char *param_value; /* Param value e.g. "sha1" */ 75 char *param_value; /* Param value e.g. "sha1" */
76} MIME_PARAM; 76} MIME_PARAM;
77 77
78DECLARE_STACK_OF(MIME_PARAM) 78DECLARE_STACK_OF(MIME_PARAM)
79IMPLEMENT_STACK_OF(MIME_PARAM) 79IMPLEMENT_STACK_OF(MIME_PARAM)
80 80
81typedef struct { 81typedef struct {
82char *name; /* Name of line e.g. "content-type" */ 82 char *name; /* Name of line e.g. "content-type" */
83char *value; /* Value of line e.g. "text/plain" */ 83 char *value; /* Value of line e.g. "text/plain" */
84STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */ 84 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
85} MIME_HEADER; 85} MIME_HEADER;
86 86
87DECLARE_STACK_OF(MIME_HEADER) 87DECLARE_STACK_OF(MIME_HEADER)
88IMPLEMENT_STACK_OF(MIME_HEADER) 88IMPLEMENT_STACK_OF(MIME_HEADER)
89 89
90static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, 90static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
91 const ASN1_ITEM *it); 91 const ASN1_ITEM *it);
92static char * strip_ends(char *name); 92static char * strip_ends(char *name);
93static char * strip_start(char *name); 93static char * strip_start(char *name);
94static char * strip_end(char *name); 94static char * strip_end(char *name);
@@ -96,9 +96,9 @@ static MIME_HEADER *mime_hdr_new(char *name, char *value);
96static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value); 96static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
97static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); 97static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
98static int mime_hdr_cmp(const MIME_HEADER * const *a, 98static int mime_hdr_cmp(const MIME_HEADER * const *a,
99 const MIME_HEADER * const *b); 99 const MIME_HEADER * const *b);
100static int mime_param_cmp(const MIME_PARAM * const *a, 100static int mime_param_cmp(const MIME_PARAM * const *a,
101 const MIME_PARAM * const *b); 101 const MIME_PARAM * const *b);
102static void mime_param_free(MIME_PARAM *param); 102static void mime_param_free(MIME_PARAM *param);
103static int mime_bound_check(char *line, int linelen, char *bound, int blen); 103static int mime_bound_check(char *line, int linelen, char *bound, int blen);
104static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret); 104static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
@@ -112,15 +112,17 @@ static void mime_hdr_free(MIME_HEADER *hdr);
112 112
113/* Output an ASN1 structure in BER format streaming if necessary */ 113/* Output an ASN1 structure in BER format streaming if necessary */
114 114
115int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 115int
116 const ASN1_ITEM *it) 116i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
117 const ASN1_ITEM *it)
117{ 118{
118 /* If streaming create stream BIO and copy all content through it */ 119 /* If streaming create stream BIO and copy all content through it */
119 if (flags & SMIME_STREAM) { 120 if (flags & SMIME_STREAM) {
120 BIO *bio, *tbio; 121 BIO *bio, *tbio;
121 bio = BIO_new_NDEF(out, val, it); 122 bio = BIO_new_NDEF(out, val, it);
122 if (!bio) { 123 if (!bio) {
123 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,ERR_R_MALLOC_FAILURE); 124 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,
125 ERR_R_MALLOC_FAILURE);
124 return 0; 126 return 0;
125 } 127 }
126 SMIME_crlf_copy(in, bio, flags); 128 SMIME_crlf_copy(in, bio, flags);
@@ -142,14 +144,16 @@ int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
142 144
143/* Base 64 read and write of ASN1 structure */ 145/* Base 64 read and write of ASN1 structure */
144 146
145static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 147static int
146 const ASN1_ITEM *it) 148B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
149 const ASN1_ITEM *it)
147{ 150{
148 BIO *b64; 151 BIO *b64;
149 int r; 152 int r;
153
150 b64 = BIO_new(BIO_f_base64()); 154 b64 = BIO_new(BIO_f_base64());
151 if(!b64) { 155 if (!b64) {
152 ASN1err(ASN1_F_B64_WRITE_ASN1,ERR_R_MALLOC_FAILURE); 156 ASN1err(ASN1_F_B64_WRITE_ASN1, ERR_R_MALLOC_FAILURE);
153 return 0; 157 return 0;
154 } 158 }
155 /* prepend the b64 BIO so all data is base64 encoded. 159 /* prepend the b64 BIO so all data is base64 encoded.
@@ -164,29 +168,31 @@ static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
164 168
165/* Streaming ASN1 PEM write */ 169/* Streaming ASN1 PEM write */
166 170
167int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 171int
168 const char *hdr, 172PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
169 const ASN1_ITEM *it) 173 const char *hdr, const ASN1_ITEM *it)
170{ 174{
171 int r; 175 int r;
176
172 BIO_printf(out, "-----BEGIN %s-----\n", hdr); 177 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
173 r = B64_write_ASN1(out, val, in, flags, it); 178 r = B64_write_ASN1(out, val, in, flags, it);
174 BIO_printf(out, "-----END %s-----\n", hdr); 179 BIO_printf(out, "-----END %s-----\n", hdr);
175 return r; 180 return r;
176} 181}
177 182
178static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it) 183static ASN1_VALUE *
184b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
179{ 185{
180 BIO *b64; 186 BIO *b64;
181 ASN1_VALUE *val; 187 ASN1_VALUE *val;
182 if(!(b64 = BIO_new(BIO_f_base64()))) { 188 if (!(b64 = BIO_new(BIO_f_base64()))) {
183 ASN1err(ASN1_F_B64_READ_ASN1,ERR_R_MALLOC_FAILURE); 189 ASN1err(ASN1_F_B64_READ_ASN1, ERR_R_MALLOC_FAILURE);
184 return 0; 190 return 0;
185} 191 }
186 bio = BIO_push(b64, bio); 192 bio = BIO_push(b64, bio);
187 val = ASN1_item_d2i_bio(it, bio, NULL); 193 val = ASN1_item_d2i_bio(it, bio, NULL);
188 if(!val) 194 if (!val)
189 ASN1err(ASN1_F_B64_READ_ASN1,ASN1_R_DECODE_ERROR); 195 ASN1err(ASN1_F_B64_READ_ASN1, ASN1_R_DECODE_ERROR);
190 (void)BIO_flush(bio); 196 (void)BIO_flush(bio);
191 bio = BIO_pop(bio); 197 bio = BIO_pop(bio);
192 BIO_free(b64); 198 BIO_free(b64);
@@ -195,10 +201,12 @@ static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
195 201
196/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */ 202/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
197 203
198static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs) 204static int
205asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
199{ 206{
200 const EVP_MD *md; 207 const EVP_MD *md;
201 int i, have_unknown = 0, write_comma, ret = 0, md_nid; 208 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
209
202 have_unknown = 0; 210 have_unknown = 0;
203 write_comma = 0; 211 write_comma = 0;
204 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) { 212 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
@@ -219,33 +227,33 @@ static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
219 if (rv != -2) 227 if (rv != -2)
220 goto err; 228 goto err;
221 } 229 }
222 switch(md_nid) { 230 switch (md_nid) {
223 case NID_sha1: 231 case NID_sha1:
224 BIO_puts(out, "sha1"); 232 BIO_puts(out, "sha1");
225 break; 233 break;
226 234
227 case NID_md5: 235 case NID_md5:
228 BIO_puts(out, "md5"); 236 BIO_puts(out, "md5");
229 break; 237 break;
230 238
231 case NID_sha256: 239 case NID_sha256:
232 BIO_puts(out, "sha-256"); 240 BIO_puts(out, "sha-256");
233 break; 241 break;
234 242
235 case NID_sha384: 243 case NID_sha384:
236 BIO_puts(out, "sha-384"); 244 BIO_puts(out, "sha-384");
237 break; 245 break;
238 246
239 case NID_sha512: 247 case NID_sha512:
240 BIO_puts(out, "sha-512"); 248 BIO_puts(out, "sha-512");
241 break; 249 break;
242 250
243 case NID_id_GostR3411_94: 251 case NID_id_GostR3411_94:
244 BIO_puts(out, "gostr3411-94"); 252 BIO_puts(out, "gostr3411-94");
245 goto err; 253 goto err;
246 break; 254 break;
247 255
248 default: 256 default:
249 if (have_unknown) 257 if (have_unknown)
250 write_comma = 0; 258 write_comma = 0;
251 else { 259 else {
@@ -258,23 +266,23 @@ static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
258 } 266 }
259 267
260 ret = 1; 268 ret = 1;
261 err:
262 269
270err:
263 return ret; 271 return ret;
264
265} 272}
266 273
267/* SMIME sender */ 274/* SMIME sender */
268 275
269int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, 276int
270 int ctype_nid, int econt_nid, 277SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
271 STACK_OF(X509_ALGOR) *mdalgs, 278 int ctype_nid, int econt_nid, STACK_OF(X509_ALGOR) *mdalgs,
272 const ASN1_ITEM *it) 279 const ASN1_ITEM *it)
273{ 280{
274 char bound[33], c; 281 char bound[33], c;
275 int i; 282 int i;
276 const char *mime_prefix, *mime_eol, *cname = "smime.p7m"; 283 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
277 const char *msg_type=NULL; 284 const char *msg_type = NULL;
285
278 if (flags & SMIME_OLDMIME) 286 if (flags & SMIME_OLDMIME)
279 mime_prefix = "application/x-pkcs7-"; 287 mime_prefix = "application/x-pkcs7-";
280 else 288 else
@@ -284,16 +292,18 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
284 mime_eol = "\r\n"; 292 mime_eol = "\r\n";
285 else 293 else
286 mime_eol = "\n"; 294 mime_eol = "\n";
287 if((flags & SMIME_DETACHED) && data) { 295 if ((flags & SMIME_DETACHED) && data) {
288 /* We want multipart/signed */ 296 /* We want multipart/signed */
289 /* Generate a random boundary */ 297 /* Generate a random boundary */
290 RAND_pseudo_bytes((unsigned char *)bound, 32); 298 RAND_pseudo_bytes((unsigned char *)bound, 32);
291 for(i = 0; i < 32; i++) { 299 for (i = 0; i < 32; i++) {
292 c = bound[i] & 0xf; 300 c = bound[i] & 0xf;
293 if(c < 10) c += '0'; 301 if (c < 10)
294 else c += 'A' - 10; 302 c += '0';
303 else
304 c += 'A' - 10;
295 bound[i] = c; 305 bound[i] = c;
296 } 306 }
297 bound[32] = 0; 307 bound[32] = 0;
298 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); 308 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
299 BIO_printf(bio, "Content-Type: multipart/signed;"); 309 BIO_printf(bio, "Content-Type: multipart/signed;");
@@ -301,9 +311,9 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
301 BIO_puts(bio, " micalg=\""); 311 BIO_puts(bio, " micalg=\"");
302 asn1_write_micalg(bio, mdalgs); 312 asn1_write_micalg(bio, mdalgs);
303 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s", 313 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
304 bound, mime_eol, mime_eol); 314 bound, mime_eol, mime_eol);
305 BIO_printf(bio, "This is an S/MIME signed message%s%s", 315 BIO_printf(bio, "This is an S/MIME signed message%s%s",
306 mime_eol, mime_eol); 316 mime_eol, mime_eol);
307 /* Now write out the first part */ 317 /* Now write out the first part */
308 BIO_printf(bio, "------%s%s", bound, mime_eol); 318 BIO_printf(bio, "------%s%s", bound, mime_eol);
309 if (!asn1_output_data(bio, data, val, flags, it)) 319 if (!asn1_output_data(bio, data, val, flags, it))
@@ -312,18 +322,18 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
312 322
313 /* Headers for signature */ 323 /* Headers for signature */
314 324
315 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); 325 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
316 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); 326 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
317 BIO_printf(bio, "Content-Transfer-Encoding: base64%s", 327 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
318 mime_eol); 328 mime_eol);
319 BIO_printf(bio, "Content-Disposition: attachment;"); 329 BIO_printf(bio, "Content-Disposition: attachment;");
320 BIO_printf(bio, " filename=\"smime.p7s\"%s%s", 330 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
321 mime_eol, mime_eol); 331 mime_eol, mime_eol);
322 B64_write_ASN1(bio, val, NULL, 0, it); 332 B64_write_ASN1(bio, val, NULL, 0, it);
323 BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound, 333 BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
324 mime_eol, mime_eol); 334 mime_eol, mime_eol);
325 return 1; 335 return 1;
326} 336 }
327 337
328 /* Determine smime-type header */ 338 /* Determine smime-type header */
329 339
@@ -349,7 +359,7 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
349 BIO_printf(bio, " smime-type=%s;", msg_type); 359 BIO_printf(bio, " smime-type=%s;", msg_type);
350 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol); 360 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
351 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", 361 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
352 mime_eol, mime_eol); 362 mime_eol, mime_eol);
353 if (!B64_write_ASN1(bio, val, data, flags, it)) 363 if (!B64_write_ASN1(bio, val, data, flags, it))
354 return 0; 364 return 0;
355 BIO_printf(bio, "%s", mime_eol); 365 BIO_printf(bio, "%s", mime_eol);
@@ -359,8 +369,9 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
359/* Handle output of ASN1 data */ 369/* Handle output of ASN1 data */
360 370
361 371
362static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, 372static int
363 const ASN1_ITEM *it) 373asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
374 const ASN1_ITEM *it)
364{ 375{
365 BIO *tmpbio; 376 BIO *tmpbio;
366 const ASN1_AUX *aux = it->funcs; 377 const ASN1_AUX *aux = it->funcs;
@@ -377,7 +388,7 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
377 388
378 if (!aux || !aux->asn1_cb) { 389 if (!aux || !aux->asn1_cb) {
379 ASN1err(ASN1_F_ASN1_OUTPUT_DATA, 390 ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
380 ASN1_R_STREAMING_NOT_SUPPORTED); 391 ASN1_R_STREAMING_NOT_SUPPORTED);
381 return 0; 392 return 0;
382 } 393 }
383 394
@@ -406,7 +417,6 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
406 } 417 }
407 418
408 return rv; 419 return rv;
409
410} 420}
411 421
412/* SMIME reader: handle multipart/signed and opaque signing. 422/* SMIME reader: handle multipart/signed and opaque signing.
@@ -414,7 +424,8 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
414 * pointed to by "bcont". In opaque this is set to NULL 424 * pointed to by "bcont". In opaque this is set to NULL
415 */ 425 */
416 426
417ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it) 427ASN1_VALUE *
428SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
418{ 429{
419 BIO *asnin; 430 BIO *asnin;
420 STACK_OF(MIME_HEADER) *headers = NULL; 431 STACK_OF(MIME_HEADER) *headers = NULL;
@@ -424,106 +435,114 @@ ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
424 ASN1_VALUE *val; 435 ASN1_VALUE *val;
425 int ret; 436 int ret;
426 437
427 if(bcont) *bcont = NULL; 438 if (bcont)
439 *bcont = NULL;
428 440
429 if (!(headers = mime_parse_hdr(bio))) { 441 if (!(headers = mime_parse_hdr(bio))) {
430 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_PARSE_ERROR); 442 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_PARSE_ERROR);
431 return NULL; 443 return NULL;
432} 444 }
433 445
434 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { 446 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
435 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 447 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
436 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE); 448 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
437 return NULL; 449 return NULL;
438} 450 }
439 451
440 /* Handle multipart/signed */ 452 /* Handle multipart/signed */
441 453
442 if(!strcmp(hdr->value, "multipart/signed")) { 454 if (!strcmp(hdr->value, "multipart/signed")) {
443 /* Split into two parts */ 455 /* Split into two parts */
444 prm = mime_param_find(hdr, "boundary"); 456 prm = mime_param_find(hdr, "boundary");
445 if(!prm || !prm->param_value) { 457 if (!prm || !prm->param_value) {
446 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 458 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
447 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY); 459 ASN1err(ASN1_F_SMIME_READ_ASN1,
460 ASN1_R_NO_MULTIPART_BOUNDARY);
448 return NULL; 461 return NULL;
449 } 462 }
450 ret = multi_split(bio, prm->param_value, &parts); 463 ret = multi_split(bio, prm->param_value, &parts);
451 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 464 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
452 if(!ret || (sk_BIO_num(parts) != 2) ) { 465 if (!ret || (sk_BIO_num(parts) != 2) ) {
453 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE); 466 ASN1err(ASN1_F_SMIME_READ_ASN1,
467 ASN1_R_NO_MULTIPART_BODY_FAILURE);
454 sk_BIO_pop_free(parts, BIO_vfree); 468 sk_BIO_pop_free(parts, BIO_vfree);
455 return NULL; 469 return NULL;
456 } 470 }
457 471
458 /* Parse the signature piece */ 472 /* Parse the signature piece */
459 asnin = sk_BIO_value(parts, 1); 473 asnin = sk_BIO_value(parts, 1);
460 474
461 if (!(headers = mime_parse_hdr(asnin))) { 475 if (!(headers = mime_parse_hdr(asnin))) {
462 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_SIG_PARSE_ERROR); 476 ASN1err(ASN1_F_SMIME_READ_ASN1,
477 ASN1_R_MIME_SIG_PARSE_ERROR);
463 sk_BIO_pop_free(parts, BIO_vfree); 478 sk_BIO_pop_free(parts, BIO_vfree);
464 return NULL; 479 return NULL;
465 } 480 }
466 481
467 /* Get content type */ 482 /* Get content type */
468 483
469 if(!(hdr = mime_hdr_find(headers, "content-type")) || 484 if (!(hdr = mime_hdr_find(headers, "content-type")) ||
470 !hdr->value) { 485 !hdr->value) {
471 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 486 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
472 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE); 487 ASN1err(ASN1_F_SMIME_READ_ASN1,
488 ASN1_R_NO_SIG_CONTENT_TYPE);
473 return NULL; 489 return NULL;
474 } 490 }
475 491
476 if(strcmp(hdr->value, "application/x-pkcs7-signature") && 492 if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
477 strcmp(hdr->value, "application/pkcs7-signature")) { 493 strcmp(hdr->value, "application/pkcs7-signature")) {
478 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_SIG_INVALID_MIME_TYPE); 494 ASN1err(ASN1_F_SMIME_READ_ASN1,
495 ASN1_R_SIG_INVALID_MIME_TYPE);
479 ERR_add_error_data(2, "type: ", hdr->value); 496 ERR_add_error_data(2, "type: ", hdr->value);
480 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 497 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
481 sk_BIO_pop_free(parts, BIO_vfree); 498 sk_BIO_pop_free(parts, BIO_vfree);
482 return NULL; 499 return NULL;
483 } 500 }
484 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 501 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
485 /* Read in ASN1 */ 502 /* Read in ASN1 */
486 if(!(val = b64_read_asn1(asnin, it))) { 503 if (!(val = b64_read_asn1(asnin, it))) {
487 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_ASN1_SIG_PARSE_ERROR); 504 ASN1err(ASN1_F_SMIME_READ_ASN1,
505 ASN1_R_ASN1_SIG_PARSE_ERROR);
488 sk_BIO_pop_free(parts, BIO_vfree); 506 sk_BIO_pop_free(parts, BIO_vfree);
489 return NULL; 507 return NULL;
490 } 508 }
491 509
492 if(bcont) { 510 if (bcont) {
493 *bcont = sk_BIO_value(parts, 0); 511 *bcont = sk_BIO_value(parts, 0);
494 BIO_free(asnin); 512 BIO_free(asnin);
495 sk_BIO_free(parts); 513 sk_BIO_free(parts);
496 } else sk_BIO_pop_free(parts, BIO_vfree); 514 } else sk_BIO_pop_free(parts, BIO_vfree);
497 return val; 515 return val;
498} 516 }
499 517
500 /* OK, if not multipart/signed try opaque signature */ 518 /* OK, if not multipart/signed try opaque signature */
501 519
502 if (strcmp (hdr->value, "application/x-pkcs7-mime") && 520 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
503 strcmp (hdr->value, "application/pkcs7-mime")) { 521 strcmp (hdr->value, "application/pkcs7-mime")) {
504 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_INVALID_MIME_TYPE); 522 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_INVALID_MIME_TYPE);
505 ERR_add_error_data(2, "type: ", hdr->value); 523 ERR_add_error_data(2, "type: ", hdr->value);
506 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 524 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
507 return NULL; 525 return NULL;
508} 526 }
509 527
510 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 528 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
511 529
512 if(!(val = b64_read_asn1(bio, it))) { 530 if (!(val = b64_read_asn1(bio, it))) {
513 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR); 531 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
514 return NULL; 532 return NULL;
515} 533 }
516 return val; 534 return val;
517
518} 535}
519 536
520/* Copy text from one BIO to another making the output CRLF at EOL */ 537/* Copy text from one BIO to another making the output CRLF at EOL */
521int SMIME_crlf_copy(BIO *in, BIO *out, int flags) 538int
539SMIME_crlf_copy(BIO *in, BIO *out, int flags)
522{ 540{
523 BIO *bf; 541 BIO *bf;
524 char eol; 542 char eol;
525 int len; 543 int len;
526 char linebuf[MAX_SMLEN]; 544 char linebuf[MAX_SMLEN];
545
527 /* Buffer output so we don't write one line at a time. This is 546 /* Buffer output so we don't write one line at a time. This is
528 * useful when streaming as we don't end up with one OCTET STRING 547 * useful when streaming as we don't end up with one OCTET STRING
529 * per line. 548 * per line.
@@ -532,17 +551,18 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
532 if (!bf) 551 if (!bf)
533 return 0; 552 return 0;
534 out = BIO_push(bf, out); 553 out = BIO_push(bf, out);
535 if(flags & SMIME_BINARY) { 554 if (flags & SMIME_BINARY) {
536 while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) 555 while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
537 BIO_write(out, linebuf, len); 556 BIO_write(out, linebuf, len);
538 } else { 557 } else {
539 if(flags & SMIME_TEXT) 558 if (flags & SMIME_TEXT)
540 BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); 559 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
541 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { 560 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
542 eol = strip_eol(linebuf, &len); 561 eol = strip_eol(linebuf, &len);
543 if (len) 562 if (len)
544 BIO_write(out, linebuf, len); 563 BIO_write(out, linebuf, len);
545 if(eol) BIO_write(out, "\r\n", 2); 564 if (eol)
565 BIO_write(out, "\r\n", 2);
546 } 566 }
547 } 567 }
548 (void)BIO_flush(out); 568 (void)BIO_flush(out);
@@ -552,7 +572,8 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
552} 572}
553 573
554/* Strip off headers if they are text/plain */ 574/* Strip off headers if they are text/plain */
555int SMIME_text(BIO *in, BIO *out) 575int
576SMIME_text(BIO *in, BIO *out)
556{ 577{
557 char iobuf[4096]; 578 char iobuf[4096];
558 int len; 579 int len;
@@ -560,23 +581,23 @@ int SMIME_text(BIO *in, BIO *out)
560 MIME_HEADER *hdr; 581 MIME_HEADER *hdr;
561 582
562 if (!(headers = mime_parse_hdr(in))) { 583 if (!(headers = mime_parse_hdr(in))) {
563 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_PARSE_ERROR); 584 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_PARSE_ERROR);
564 return 0; 585 return 0;
565} 586 }
566 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { 587 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
567 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_NO_CONTENT_TYPE); 588 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_NO_CONTENT_TYPE);
568 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 589 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
569 return 0; 590 return 0;
570} 591 }
571 if (strcmp (hdr->value, "text/plain")) { 592 if (strcmp (hdr->value, "text/plain")) {
572 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_INVALID_MIME_TYPE); 593 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_INVALID_MIME_TYPE);
573 ERR_add_error_data(2, "type: ", hdr->value); 594 ERR_add_error_data(2, "type: ", hdr->value);
574 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 595 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
575 return 0; 596 return 0;
576} 597 }
577 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 598 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
578 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) 599 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
579 BIO_write(out, iobuf, len); 600 BIO_write(out, iobuf, len);
580 if (len < 0) 601 if (len < 0)
581 return 0; 602 return 0;
582 return 1; 603 return 1;
@@ -586,7 +607,8 @@ int SMIME_text(BIO *in, BIO *out)
586 * canonical parts in a STACK of bios 607 * canonical parts in a STACK of bios
587 */ 608 */
588 609
589static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) 610static int
611multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
590{ 612{
591 char linebuf[MAX_SMLEN]; 613 char linebuf[MAX_SMLEN];
592 int len, blen; 614 int len, blen;
@@ -603,27 +625,28 @@ static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
603 *ret = parts; 625 *ret = parts;
604 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { 626 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
605 state = mime_bound_check(linebuf, len, bound, blen); 627 state = mime_bound_check(linebuf, len, bound, blen);
606 if(state == 1) { 628 if (state == 1) {
607 first = 1; 629 first = 1;
608 part++; 630 part++;
609 } else if(state == 2) { 631 } else if (state == 2) {
610 sk_BIO_push(parts, bpart); 632 sk_BIO_push(parts, bpart);
611 return 1; 633 return 1;
612 } else if(part) { 634 } else if (part) {
613 /* Strip CR+LF from linebuf */ 635 /* Strip CR+LF from linebuf */
614 next_eol = strip_eol(linebuf, &len); 636 next_eol = strip_eol(linebuf, &len);
615 if(first) { 637 if (first) {
616 first = 0; 638 first = 0;
617 if(bpart) sk_BIO_push(parts, bpart); 639 if (bpart)
640 sk_BIO_push(parts, bpart);
618 bpart = BIO_new(BIO_s_mem()); 641 bpart = BIO_new(BIO_s_mem());
619 BIO_set_mem_eof_return(bpart, 0); 642 BIO_set_mem_eof_return(bpart, 0);
620 } else if (eol) 643 } else if (eol)
621 BIO_write(bpart, "\r\n", 2); 644 BIO_write(bpart, "\r\n", 2);
622 eol = next_eol; 645 eol = next_eol;
623 if (len) 646 if (len)
624 BIO_write(bpart, linebuf, len); 647 BIO_write(bpart, linebuf, len);
648 }
625 } 649 }
626}
627 return 0; 650 return 0;
628} 651}
629 652
@@ -638,7 +661,8 @@ static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
638#define MIME_COMMENT 6 661#define MIME_COMMENT 6
639 662
640 663
641static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) 664static
665STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
642{ 666{
643 char *p, *q, c; 667 char *p, *q, c;
644 char *ntmp; 668 char *ntmp;
@@ -649,254 +673,294 @@ static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
649 673
650 headers = sk_MIME_HEADER_new(mime_hdr_cmp); 674 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
651 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { 675 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
652 /* If whitespace at line start then continuation line */ 676 /* If whitespace at line start then continuation line */
653 if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; 677 if (mhdr && isspace((unsigned char)linebuf[0]))
654 else state = MIME_START; 678 state = MIME_NAME;
655 ntmp = NULL; 679 else
656 /* Go through all characters */ 680 state = MIME_START;
657 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { 681 ntmp = NULL;
658 682
659 /* State machine to handle MIME headers 683 /* Go through all characters */
660 * if this looks horrible that's because it *is* 684 for (p = linebuf, q = linebuf;
661 */ 685 (c = *p) && (c != '\r') && (c != '\n'); p++) {
662 686
663 switch(state) { 687 /* State machine to handle MIME headers
688 * if this looks horrible that's because it *is*
689 */
690
691 switch (state) {
664 case MIME_START: 692 case MIME_START:
665 if(c == ':') { 693 if (c == ':') {
666 state = MIME_TYPE; 694 state = MIME_TYPE;
667 *p = 0; 695 *p = 0;
668 ntmp = strip_ends(q); 696 ntmp = strip_ends(q);
669 q = p + 1; 697 q = p + 1;
670 } 698 }
671 break; 699 break;
672 700
673 case MIME_TYPE: 701 case MIME_TYPE:
674 if(c == ';') { 702 if (c == ';') {
675 mime_debug("Found End Value\n"); 703 mime_debug("Found End Value\n");
676 *p = 0; 704 *p = 0;
677 mhdr = mime_hdr_new(ntmp, strip_ends(q)); 705 mhdr = mime_hdr_new(ntmp,
678 sk_MIME_HEADER_push(headers, mhdr); 706 strip_ends(q));
679 ntmp = NULL; 707 sk_MIME_HEADER_push(headers, mhdr);
680 q = p + 1; 708 ntmp = NULL;
681 state = MIME_NAME; 709 q = p + 1;
682 } else if(c == '(') { 710 state = MIME_NAME;
683 save_state = state; 711 } else if (c == '(') {
684 state = MIME_COMMENT; 712 save_state = state;
685 } 713 state = MIME_COMMENT;
686 break; 714 }
715 break;
687 716
688 case MIME_COMMENT: 717 case MIME_COMMENT:
689 if(c == ')') { 718 if (c == ')') {
690 state = save_state; 719 state = save_state;
691 } 720 }
692 break; 721 break;
693 722
694 case MIME_NAME: 723 case MIME_NAME:
695 if(c == '=') { 724 if (c == '=') {
696 state = MIME_VALUE; 725 state = MIME_VALUE;
697 *p = 0; 726 *p = 0;
698 ntmp = strip_ends(q); 727 ntmp = strip_ends(q);
699 q = p + 1; 728 q = p + 1;
700 } 729 }
701 break ; 730 break;
702 731
703 case MIME_VALUE: 732 case MIME_VALUE:
704 if(c == ';') { 733 if (c == ';') {
705 state = MIME_NAME; 734 state = MIME_NAME;
706 *p = 0; 735 *p = 0;
707 mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); 736 mime_hdr_addparam(mhdr, ntmp,
708 ntmp = NULL; 737 strip_ends(q));
709 q = p + 1; 738 ntmp = NULL;
710 } else if (c == '"') { 739 q = p + 1;
711 mime_debug("Found Quote\n"); 740 } else if (c == '"') {
712 state = MIME_QUOTE; 741 mime_debug("Found Quote\n");
713 } else if(c == '(') { 742 state = MIME_QUOTE;
714 save_state = state; 743 } else if (c == '(') {
715 state = MIME_COMMENT; 744 save_state = state;
716 } 745 state = MIME_COMMENT;
717 break; 746 }
747 break;
718 748
719 case MIME_QUOTE: 749 case MIME_QUOTE:
720 if(c == '"') { 750 if (c == '"') {
721 mime_debug("Found Match Quote\n"); 751 mime_debug("Found Match Quote\n");
722 state = MIME_VALUE; 752 state = MIME_VALUE;
753 }
754 break;
755 }
723 } 756 }
724 break;
725 }
726}
727 757
728 if(state == MIME_TYPE) { 758 if (state == MIME_TYPE) {
729 mhdr = mime_hdr_new(ntmp, strip_ends(q)); 759 mhdr = mime_hdr_new(ntmp, strip_ends(q));
730 sk_MIME_HEADER_push(headers, mhdr); 760 sk_MIME_HEADER_push(headers, mhdr);
731} else if(state == MIME_VALUE) 761 } else if (state == MIME_VALUE)
732 mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); 762 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
733 if(p == linebuf) break; /* Blank line means end of headers */
734}
735 763
736return headers; 764 if (p == linebuf)
765 break; /* Blank line means end of headers */
766 }
737 767
768 return headers;
738} 769}
739 770
740static char *strip_ends(char *name) 771static char *
772strip_ends(char *name)
741{ 773{
742 return strip_end(strip_start(name)); 774 return strip_end(strip_start(name));
743} 775}
744 776
745/* Strip a parameter of whitespace from start of param */ 777/* Strip a parameter of whitespace from start of param */
746static char *strip_start(char *name) 778static char *
779strip_start(char *name)
747{ 780{
748 char *p, c; 781 char *p, c;
782
749 /* Look for first non white space or quote */ 783 /* Look for first non white space or quote */
750 for(p = name; (c = *p) ;p++) { 784 for (p = name; (c = *p); p++) {
751 if(c == '"') { 785 if (c == '"') {
752 /* Next char is start of string if non null */ 786 /* Next char is start of string if non null */
753 if(p[1]) return p + 1; 787 if (p[1])
788 return p + 1;
754 /* Else null string */ 789 /* Else null string */
755 return NULL; 790 return NULL;
791 }
792 if (!isspace((unsigned char)c))
793 return p;
756 } 794 }
757 if(!isspace((unsigned char)c)) return p;
758}
759 return NULL; 795 return NULL;
760} 796}
761 797
762/* As above but strip from end of string : maybe should handle brackets? */ 798/* As above but strip from end of string : maybe should handle brackets? */
763static char *strip_end(char *name) 799static char *
800strip_end(char *name)
764{ 801{
765 char *p, c; 802 char *p, c;
766 if(!name) return NULL; 803
804 if (!name)
805 return NULL;
806
767 /* Look for first non white space or quote */ 807 /* Look for first non white space or quote */
768 for(p = name + strlen(name) - 1; p >= name ;p--) { 808 for (p = name + strlen(name) - 1; p >= name; p--) {
769 c = *p; 809 c = *p;
770 if(c == '"') { 810 if (c == '"') {
771 if(p - 1 == name) return NULL; 811 if (p - 1 == name)
812 return NULL;
772 *p = 0; 813 *p = 0;
773 return name; 814 return name;
815 }
816 if (isspace((unsigned char)c))
817 *p = 0;
818 else
819 return name;
774 } 820 }
775 if(isspace((unsigned char)c)) *p = 0;
776 else return name;
777}
778 return NULL; 821 return NULL;
779} 822}
780 823
781static MIME_HEADER *mime_hdr_new(char *name, char *value) 824static MIME_HEADER *
825mime_hdr_new(char *name, char *value)
782{ 826{
783 MIME_HEADER *mhdr; 827 MIME_HEADER *mhdr;
784 char *tmpname, *tmpval, *p; 828 char *tmpname, *tmpval, *p;
785 int c; 829 int c;
786 if(name) { 830 if (name) {
787 if(!(tmpname = BUF_strdup(name))) return NULL; 831 if (!(tmpname = BUF_strdup(name)))
788 for(p = tmpname ; *p; p++) { 832 return NULL;
833 for (p = tmpname; *p; p++) {
789 c = (unsigned char)*p; 834 c = (unsigned char)*p;
790 if(isupper(c)) { 835 if (isupper(c)) {
791 c = tolower(c); 836 c = tolower(c);
792 *p = c; 837 *p = c;
838 }
793 } 839 }
794 } 840 } else
795} else tmpname = NULL; 841 tmpname = NULL;
796 if(value) { 842 if (value) {
797 if(!(tmpval = BUF_strdup(value))) return NULL; 843 if (!(tmpval = BUF_strdup(value)))
798 for(p = tmpval ; *p; p++) { 844 return NULL;
845 for (p = tmpval; *p; p++) {
799 c = (unsigned char)*p; 846 c = (unsigned char)*p;
800 if(isupper(c)) { 847 if (isupper(c)) {
801 c = tolower(c); 848 c = tolower(c);
802 *p = c; 849 *p = c;
850 }
803 } 851 }
804 } 852 } else tmpval = NULL;
805} else tmpval = NULL; 853 mhdr = (MIME_HEADER *)malloc(sizeof(MIME_HEADER));
806 mhdr = (MIME_HEADER *) malloc(sizeof(MIME_HEADER));
807 if (!mhdr) { 854 if (!mhdr) {
808 OPENSSL_free(tmpname); 855 OPENSSL_free(tmpname);
809 return NULL; 856 return NULL;
810} 857 }
811 mhdr->name = tmpname; 858 mhdr->name = tmpname;
812 mhdr->value = tmpval; 859 mhdr->value = tmpval;
813 if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) { 860 if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) {
814 free(mhdr); 861 free(mhdr);
815 return NULL; 862 return NULL;
816} 863 }
817 return mhdr; 864 return mhdr;
818} 865}
819 866
820static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) 867static int
868mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
821{ 869{
822 char *tmpname, *tmpval, *p; 870 char *tmpname, *tmpval, *p;
823 int c; 871 int c;
824 MIME_PARAM *mparam; 872 MIME_PARAM *mparam;
825 if(name) { 873
874 if (name) {
826 tmpname = BUF_strdup(name); 875 tmpname = BUF_strdup(name);
827 if(!tmpname) return 0; 876 if (!tmpname)
828 for(p = tmpname ; *p; p++) { 877 return 0;
878 for (p = tmpname; *p; p++) {
829 c = (unsigned char)*p; 879 c = (unsigned char)*p;
830 if(isupper(c)) { 880 if (isupper(c)) {
831 c = tolower(c); 881 c = tolower(c);
832 *p = c; 882 *p = c;
883 }
833 } 884 }
834 } 885 } else
835} else tmpname = NULL; 886 tmpname = NULL;
836 if(value) { 887 if (value) {
837 tmpval = BUF_strdup(value); 888 tmpval = BUF_strdup(value);
838 if(!tmpval) return 0; 889 if (!tmpval)
839} else tmpval = NULL; 890 return 0;
891 } else
892 tmpval = NULL;
840 /* Parameter values are case sensitive so leave as is */ 893 /* Parameter values are case sensitive so leave as is */
841 mparam = (MIME_PARAM *) malloc(sizeof(MIME_PARAM)); 894 mparam = (MIME_PARAM *) malloc(sizeof(MIME_PARAM));
842 if(!mparam) return 0; 895 if (!mparam)
896 return 0;
843 mparam->param_name = tmpname; 897 mparam->param_name = tmpname;
844 mparam->param_value = tmpval; 898 mparam->param_value = tmpval;
845 sk_MIME_PARAM_push(mhdr->params, mparam); 899 sk_MIME_PARAM_push(mhdr->params, mparam);
846 return 1; 900 return 1;
847} 901}
848 902
849static int mime_hdr_cmp(const MIME_HEADER * const *a, 903static int
850 const MIME_HEADER * const *b) 904mime_hdr_cmp(const MIME_HEADER * const *a, const MIME_HEADER * const *b)
851{ 905{
852 if (!(*a)->name || !(*b)->name) 906 if (!(*a)->name || !(*b)->name)
853 return !!(*a)->name - !!(*b)->name; 907 return !!(*a)->name - !!(*b)->name;
854 908 return (strcmp((*a)->name, (*b)->name));
855 return(strcmp((*a)->name, (*b)->name));
856} 909}
857 910
858static int mime_param_cmp(const MIME_PARAM * const *a, 911static int
859 const MIME_PARAM * const *b) 912mime_param_cmp(const MIME_PARAM * const *a, const MIME_PARAM * const *b)
860{ 913{
861 if (!(*a)->param_name || !(*b)->param_name) 914 if (!(*a)->param_name || !(*b)->param_name)
862 return !!(*a)->param_name - !!(*b)->param_name; 915 return !!(*a)->param_name - !!(*b)->param_name;
863 return(strcmp((*a)->param_name, (*b)->param_name)); 916 return (strcmp((*a)->param_name, (*b)->param_name));
864} 917}
865 918
866/* Find a header with a given name (if possible) */ 919/* Find a header with a given name (if possible) */
867 920
868static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) 921static MIME_HEADER *
922mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
869{ 923{
870 MIME_HEADER htmp; 924 MIME_HEADER htmp;
871 int idx; 925 int idx;
872 htmp.name = name; 926 htmp.name = name;
873 idx = sk_MIME_HEADER_find(hdrs, &htmp); 927 idx = sk_MIME_HEADER_find(hdrs, &htmp);
874 if(idx < 0) return NULL; 928 if (idx < 0)
929 return NULL;
875 return sk_MIME_HEADER_value(hdrs, idx); 930 return sk_MIME_HEADER_value(hdrs, idx);
876} 931}
877 932
878static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) 933static MIME_PARAM *
934mime_param_find(MIME_HEADER *hdr, char *name)
879{ 935{
880 MIME_PARAM param; 936 MIME_PARAM param;
881 int idx; 937 int idx;
882 param.param_name = name; 938 param.param_name = name;
883 idx = sk_MIME_PARAM_find(hdr->params, &param); 939 idx = sk_MIME_PARAM_find(hdr->params, &param);
884 if(idx < 0) return NULL; 940 if (idx < 0)
941 return NULL;
885 return sk_MIME_PARAM_value(hdr->params, idx); 942 return sk_MIME_PARAM_value(hdr->params, idx);
886} 943}
887 944
888static void mime_hdr_free(MIME_HEADER *hdr) 945static void
946mime_hdr_free(MIME_HEADER *hdr)
889{ 947{
890 if(hdr->name) free(hdr->name); 948 if (hdr->name)
891 if(hdr->value) free(hdr->value); 949 free(hdr->name);
892 if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); 950 if (hdr->value)
951 free(hdr->value);
952 if (hdr->params)
953 sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
893 free(hdr); 954 free(hdr);
894} 955}
895 956
896static void mime_param_free(MIME_PARAM *param) 957static void
958mime_param_free(MIME_PARAM *param)
897{ 959{
898 if(param->param_name) free(param->param_name); 960 if (param->param_name)
899 if(param->param_value) free(param->param_value); 961 free(param->param_name);
962 if (param->param_value)
963 free(param->param_value);
900 free(param); 964 free(param);
901} 965}
902 966
@@ -905,21 +969,28 @@ static void mime_param_free(MIME_PARAM *param)
905 * 1 : part boundary 969 * 1 : part boundary
906 * 2 : final boundary 970 * 2 : final boundary
907 */ 971 */
908static int mime_bound_check(char *line, int linelen, char *bound, int blen) 972static int
973mime_bound_check(char *line, int linelen, char *bound, int blen)
909{ 974{
910 if(linelen == -1) linelen = strlen(line); 975 if (linelen == -1)
911 if(blen == -1) blen = strlen(bound); 976 linelen = strlen(line);
977 if (blen == -1)
978 blen = strlen(bound);
912 /* Quickly eliminate if line length too short */ 979 /* Quickly eliminate if line length too short */
913 if(blen + 2 > linelen) return 0; 980 if (blen + 2 > linelen)
981 return 0;
914 /* Check for part boundary */ 982 /* Check for part boundary */
915 if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { 983 if (!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
916 if(!strncmp(line + blen + 2, "--", 2)) return 2; 984 if (!strncmp(line + blen + 2, "--", 2))
917 else return 1; 985 return 2;
918} 986 else
987 return 1;
988 }
919 return 0; 989 return 0;
920} 990}
921 991
922static int strip_eol(char *linebuf, int *plen) 992static int
993strip_eol(char *linebuf, int *plen)
923{ 994{
924 int len = *plen; 995 int len = *plen;
925 char *p, c; 996 char *p, c;
diff --git a/src/lib/libcrypto/asn1/asn_moid.c b/src/lib/libcrypto/asn1/asn_moid.c
index 0c09a8e934..7ddf212e9a 100644
--- a/src/lib/libcrypto/asn1/asn_moid.c
+++ b/src/lib/libcrypto/asn1/asn_moid.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
@@ -68,20 +68,22 @@
68 68
69static int do_create(char *value, char *name); 69static int do_create(char *value, char *name);
70 70
71static int oid_module_init(CONF_IMODULE *md, const CONF *cnf) 71static int
72oid_module_init(CONF_IMODULE *md, const CONF *cnf)
72{ 73{
73 int i; 74 int i;
74 const char *oid_section; 75 const char *oid_section;
75 STACK_OF(CONF_VALUE) *sktmp; 76 STACK_OF(CONF_VALUE) *sktmp;
76 CONF_VALUE *oval; 77 CONF_VALUE *oval;
78
77 oid_section = CONF_imodule_get_value(md); 79 oid_section = CONF_imodule_get_value(md);
78 if(!(sktmp = NCONF_get_section(cnf, oid_section))) { 80 if (!(sktmp = NCONF_get_section(cnf, oid_section))) {
79 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION); 81 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
80 return 0; 82 return 0;
81 } 83 }
82 for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { 84 for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
83 oval = sk_CONF_VALUE_value(sktmp, i); 85 oval = sk_CONF_VALUE_value(sktmp, i);
84 if(!do_create(oval->value, oval->name)) { 86 if (!do_create(oval->value, oval->name)) {
85 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT); 87 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
86 return 0; 88 return 0;
87 } 89 }
@@ -89,12 +91,14 @@ static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
89 return 1; 91 return 1;
90} 92}
91 93
92static void oid_module_finish(CONF_IMODULE *md) 94static void
95oid_module_finish(CONF_IMODULE *md)
93{ 96{
94 OBJ_cleanup(); 97 OBJ_cleanup();
95} 98}
96 99
97void ASN1_add_oid_module(void) 100void
101ASN1_add_oid_module(void)
98{ 102{
99 CONF_module_add("oid_section", oid_module_init, oid_module_finish); 103 CONF_module_add("oid_section", oid_module_init, oid_module_finish);
100} 104}
@@ -104,12 +108,13 @@ void ASN1_add_oid_module(void)
104 * shortname = some long name, 1.2.3.4 108 * shortname = some long name, 1.2.3.4
105 */ 109 */
106 110
107 111static int
108static int do_create(char *value, char *name) 112do_create(char *value, char *name)
109{ 113{
110 int nid; 114 int nid;
111 ASN1_OBJECT *oid; 115 ASN1_OBJECT *oid;
112 char *ln, *ostr, *p, *lntmp; 116 char *ln, *ostr, *p, *lntmp;
117
113 p = strrchr(value, ','); 118 p = strrchr(value, ',');
114 if (!p) { 119 if (!p) {
115 ln = name; 120 ln = name;
@@ -119,7 +124,8 @@ static int do_create(char *value, char *name)
119 ostr = p + 1; 124 ostr = p + 1;
120 if (!*ostr) 125 if (!*ostr)
121 return 0; 126 return 0;
122 while(isspace((unsigned char)*ostr)) ostr++; 127 while (isspace((unsigned char)*ostr))
128 ostr++;
123 } 129 }
124 130
125 nid = OBJ_create(ostr, name, ln); 131 nid = OBJ_create(ostr, name, ln);
@@ -129,9 +135,10 @@ static int do_create(char *value, char *name)
129 135
130 if (p) { 136 if (p) {
131 ln = value; 137 ln = value;
132 while(isspace((unsigned char)*ln)) ln++; 138 while (isspace((unsigned char)*ln))
139 ln++;
133 p--; 140 p--;
134 while(isspace((unsigned char)*p)) { 141 while (isspace((unsigned char)*p)) {
135 if (p == ln) 142 if (p == ln)
136 return 0; 143 return 0;
137 p--; 144 p--;
@@ -148,5 +155,3 @@ static int do_create(char *value, char *name)
148 155
149 return 1; 156 return 1;
150} 157}
151
152
diff --git a/src/lib/libcrypto/asn1/asn_pack.c b/src/lib/libcrypto/asn1/asn_pack.c
index 13dc5d4665..9752a68206 100644
--- a/src/lib/libcrypto/asn1/asn_pack.c
+++ b/src/lib/libcrypto/asn1/asn_pack.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,29 +62,32 @@
62 62
63/* ASN1_ITEM versions of the above */ 63/* ASN1_ITEM versions of the above */
64 64
65ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) 65ASN1_STRING *
66ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
66{ 67{
67 ASN1_STRING *octmp; 68 ASN1_STRING *octmp;
68 69
69 if (!oct || !*oct) { 70 if (!oct || !*oct) {
70 if (!(octmp = ASN1_STRING_new ())) { 71 if (!(octmp = ASN1_STRING_new ())) {
71 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 72 ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
72 return NULL; 73 return NULL;
73 } 74 }
74 if (oct) *oct = octmp; 75 if (oct)
75 } else octmp = *oct; 76 *oct = octmp;
77 } else
78 octmp = *oct;
76 79
77 if(octmp->data) { 80 if (octmp->data) {
78 free(octmp->data); 81 free(octmp->data);
79 octmp->data = NULL; 82 octmp->data = NULL;
80 } 83 }
81 84
82 if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) { 85 if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
83 ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR); 86 ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
84 return NULL; 87 return NULL;
85 } 88 }
86 if (!octmp->data) { 89 if (!octmp->data) {
87 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 90 ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
88 return NULL; 91 return NULL;
89 } 92 }
90 return octmp; 93 return octmp;
@@ -92,13 +95,14 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
92 95
93/* Extract an ASN1 object from an ASN1_STRING */ 96/* Extract an ASN1 object from an ASN1_STRING */
94 97
95void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it) 98void *
99ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
96{ 100{
97 const unsigned char *p; 101 const unsigned char *p;
98 void *ret; 102 void *ret;
99 103
100 p = oct->data; 104 p = oct->data;
101 if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it))) 105 if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
102 ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR); 106 ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR);
103 return ret; 107 return ret;
104} 108}
diff --git a/src/lib/libcrypto/asn1/bio_asn1.c b/src/lib/libcrypto/asn1/bio_asn1.c
index 8ebac02a00..36b82758ed 100644
--- a/src/lib/libcrypto/asn1/bio_asn1.c
+++ b/src/lib/libcrypto/asn1/bio_asn1.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
@@ -68,8 +68,7 @@
68/* Must be large enough for biggest tag+length */ 68/* Must be large enough for biggest tag+length */
69#define DEFAULT_ASN1_BUF_SIZE 20 69#define DEFAULT_ASN1_BUF_SIZE 20
70 70
71typedef enum 71typedef enum {
72{
73 ASN1_STATE_START, 72 ASN1_STATE_START,
74 ASN1_STATE_PRE_COPY, 73 ASN1_STATE_PRE_COPY,
75 ASN1_STATE_HEADER, 74 ASN1_STATE_HEADER,
@@ -79,14 +78,12 @@ typedef enum
79 ASN1_STATE_DONE 78 ASN1_STATE_DONE
80} asn1_bio_state_t; 79} asn1_bio_state_t;
81 80
82typedef struct BIO_ASN1_EX_FUNCS_st 81typedef struct BIO_ASN1_EX_FUNCS_st {
83{
84 asn1_ps_func *ex_func; 82 asn1_ps_func *ex_func;
85 asn1_ps_func *ex_free_func; 83 asn1_ps_func *ex_free_func;
86} BIO_ASN1_EX_FUNCS; 84} BIO_ASN1_EX_FUNCS;
87 85
88typedef struct BIO_ASN1_BUF_CTX_t 86typedef struct BIO_ASN1_BUF_CTX_t {
89{
90 /* Internal state */ 87 /* Internal state */
91 asn1_bio_state_t state; 88 asn1_bio_state_t state;
92 /* Internal buffer */ 89 /* Internal buffer */
@@ -110,7 +107,7 @@ typedef struct BIO_ASN1_BUF_CTX_t
110} BIO_ASN1_BUF_CTX; 107} BIO_ASN1_BUF_CTX;
111 108
112 109
113static int asn1_bio_write(BIO *h, const char *buf,int num); 110static int asn1_bio_write(BIO *h, const char *buf, int num);
114static int asn1_bio_read(BIO *h, char *buf, int size); 111static int asn1_bio_read(BIO *h, char *buf, int size);
115static int asn1_bio_puts(BIO *h, const char *str); 112static int asn1_bio_puts(BIO *h, const char *str);
116static int asn1_bio_gets(BIO *h, char *str, int size); 113static int asn1_bio_gets(BIO *h, char *str, int size);
@@ -121,14 +118,12 @@ static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
121 118
122static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size); 119static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
123static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 120static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
124 asn1_ps_func *cleanup, asn1_bio_state_t next); 121 asn1_ps_func *cleanup, asn1_bio_state_t next);
125static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 122static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
126 asn1_ps_func *setup, 123 asn1_ps_func *setup, asn1_bio_state_t ex_state,
127 asn1_bio_state_t ex_state, 124 asn1_bio_state_t other_state);
128 asn1_bio_state_t other_state);
129 125
130static BIO_METHOD methods_asn1= 126static BIO_METHOD methods_asn1 = {
131{
132 BIO_TYPE_ASN1, 127 BIO_TYPE_ASN1,
133 "asn1", 128 "asn1",
134 asn1_bio_write, 129 asn1_bio_write,
@@ -141,13 +136,14 @@ static BIO_METHOD methods_asn1=
141 asn1_bio_callback_ctrl, 136 asn1_bio_callback_ctrl,
142}; 137};
143 138
144BIO_METHOD *BIO_f_asn1(void) 139BIO_METHOD *
140BIO_f_asn1(void)
145{ 141{
146 return(&methods_asn1); 142 return (&methods_asn1);
147} 143}
148 144
149 145static int
150static int asn1_bio_new(BIO *b) 146asn1_bio_new(BIO *b)
151{ 147{
152 BIO_ASN1_BUF_CTX *ctx; 148 BIO_ASN1_BUF_CTX *ctx;
153 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX)); 149 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX));
@@ -156,14 +152,15 @@ static int asn1_bio_new(BIO *b)
156 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { 152 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
157 free(ctx); 153 free(ctx);
158 return 0; 154 return 0;
159} 155 }
160 b->init = 1; 156 b->init = 1;
161 b->ptr = (char *)ctx; 157 b->ptr = (char *)ctx;
162 b->flags = 0; 158 b->flags = 0;
163 return 1; 159 return 1;
164} 160}
165 161
166static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) 162static int
163asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
167{ 164{
168 ctx->buf = malloc(size); 165 ctx->buf = malloc(size);
169 if (!ctx->buf) 166 if (!ctx->buf)
@@ -181,9 +178,11 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
181 return 1; 178 return 1;
182} 179}
183 180
184static int asn1_bio_free(BIO *b) 181static int
182asn1_bio_free(BIO *b)
185{ 183{
186 BIO_ASN1_BUF_CTX *ctx; 184 BIO_ASN1_BUF_CTX *ctx;
185
187 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 186 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
188 if (ctx == NULL) 187 if (ctx == NULL)
189 return 0; 188 return 0;
@@ -196,11 +195,13 @@ static int asn1_bio_free(BIO *b)
196 return 1; 195 return 1;
197} 196}
198 197
199static int asn1_bio_write(BIO *b, const char *in , int inl) 198static int
199asn1_bio_write(BIO *b, const char *in , int inl)
200{ 200{
201 BIO_ASN1_BUF_CTX *ctx; 201 BIO_ASN1_BUF_CTX *ctx;
202 int wrmax, wrlen, ret; 202 int wrmax, wrlen, ret;
203 unsigned char *p; 203 unsigned char *p;
204
204 if (!in || (inl < 0) || (b->next_bio == NULL)) 205 if (!in || (inl < 0) || (b->next_bio == NULL))
205 return 0; 206 return 0;
206 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 207 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
@@ -210,42 +211,38 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
210 wrlen = 0; 211 wrlen = 0;
211 ret = -1; 212 ret = -1;
212 213
213 for(;;) { 214 for (;;) {
214 switch (ctx->state) { 215 switch (ctx->state) {
215 216
216 /* Setup prefix data, call it */ 217 /* Setup prefix data, call it */
217 case ASN1_STATE_START: 218 case ASN1_STATE_START:
218 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix, 219 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
219 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER)) 220 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
220 return 0; 221 return 0;
221 break; 222 break;
222 223
223 /* Copy any pre data first */ 224 /* Copy any pre data first */
224 case ASN1_STATE_PRE_COPY: 225 case ASN1_STATE_PRE_COPY:
225
226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free, 226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
227 ASN1_STATE_HEADER); 227 ASN1_STATE_HEADER);
228
229 if (ret <= 0) 228 if (ret <= 0)
230 goto done; 229 goto done;
231
232 break; 230 break;
233 231
234 case ASN1_STATE_HEADER: 232 case ASN1_STATE_HEADER:
235 ctx->buflen = 233 ctx->buflen =
236 ASN1_object_size(0, inl, ctx->asn1_tag) - inl; 234 ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
237 OPENSSL_assert(ctx->buflen <= ctx->bufsize); 235 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
238 p = ctx->buf; 236 p = ctx->buf;
239 ASN1_put_object(&p, 0, inl, 237 ASN1_put_object(&p, 0, inl,
240 ctx->asn1_tag, ctx->asn1_class); 238 ctx->asn1_tag, ctx->asn1_class);
241 ctx->copylen = inl; 239 ctx->copylen = inl;
242 ctx->state = ASN1_STATE_HEADER_COPY; 240 ctx->state = ASN1_STATE_HEADER_COPY;
243
244 break; 241 break;
245 242
246 case ASN1_STATE_HEADER_COPY: 243 case ASN1_STATE_HEADER_COPY:
247 ret = BIO_write(b->next_bio, 244 ret = BIO_write(b->next_bio,
248 ctx->buf + ctx->bufpos, ctx->buflen); 245 ctx->buf + ctx->bufpos, ctx->buflen);
249 if (ret <= 0) 246 if (ret <= 0)
250 goto done; 247 goto done;
251 248
@@ -256,7 +253,6 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
256 ctx->bufpos = 0; 253 ctx->bufpos = 0;
257 ctx->state = ASN1_STATE_DATA_COPY; 254 ctx->state = ASN1_STATE_DATA_COPY;
258 } 255 }
259
260 break; 256 break;
261 257
262 case ASN1_STATE_DATA_COPY: 258 case ASN1_STATE_DATA_COPY:
@@ -275,46 +271,44 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
275 271
276 if (ctx->copylen == 0) 272 if (ctx->copylen == 0)
277 ctx->state = ASN1_STATE_HEADER; 273 ctx->state = ASN1_STATE_HEADER;
278
279 if (inl == 0) 274 if (inl == 0)
280 goto done; 275 goto done;
281
282 break; 276 break;
283 277
284 default: 278 default:
285 BIO_clear_retry_flags(b); 279 BIO_clear_retry_flags(b);
286 return 0; 280 return 0;
287
288 } 281 }
289 282
290 } 283 }
291 284
292 done: 285done:
293 BIO_clear_retry_flags(b); 286 BIO_clear_retry_flags(b);
294 BIO_copy_next_retry(b); 287 BIO_copy_next_retry(b);
295 288
296 return (wrlen > 0) ? wrlen : ret; 289 return (wrlen > 0) ? wrlen : ret;
297
298} 290}
299 291
300static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 292static int
301 asn1_ps_func *cleanup, asn1_bio_state_t next) 293asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *cleanup,
294 asn1_bio_state_t next)
302{ 295{
303 int ret; 296 int ret;
297
304 if (ctx->ex_len <= 0) 298 if (ctx->ex_len <= 0)
305 return 1; 299 return 1;
306 for(;;) { 300 for (;;) {
307 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, 301 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
308 ctx->ex_len); 302 ctx->ex_len);
309 if (ret <= 0) 303 if (ret <= 0)
310 break; 304 break;
311 ctx->ex_len -= ret; 305 ctx->ex_len -= ret;
312 if (ctx->ex_len > 0) 306 if (ctx->ex_len > 0)
313 ctx->ex_pos += ret; 307 ctx->ex_pos += ret;
314 else { 308 else {
315 if(cleanup) 309 if (cleanup)
316 cleanup(b, &ctx->ex_buf, &ctx->ex_len, 310 cleanup(b, &ctx->ex_buf, &ctx->ex_len,
317 &ctx->ex_arg); 311 &ctx->ex_arg);
318 ctx->state = next; 312 ctx->state = next;
319 ctx->ex_pos = 0; 313 ctx->ex_pos = 0;
320 break; 314 break;
@@ -323,10 +317,9 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
323 return ret; 317 return ret;
324} 318}
325 319
326static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 320static int
327 asn1_ps_func *setup, 321asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *setup,
328 asn1_bio_state_t ex_state, 322 asn1_bio_state_t ex_state, asn1_bio_state_t other_state)
329 asn1_bio_state_t other_state)
330{ 323{
331 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) { 324 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
332 BIO_clear_retry_flags(b); 325 BIO_clear_retry_flags(b);
@@ -339,45 +332,52 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
339 return 1; 332 return 1;
340} 333}
341 334
342static int asn1_bio_read(BIO *b, char *in , int inl) 335static int
336asn1_bio_read(BIO *b, char *in , int inl)
343{ 337{
344 if (!b->next_bio) 338 if (!b->next_bio)
345 return 0; 339 return 0;
346 return BIO_read(b->next_bio, in , inl); 340 return BIO_read(b->next_bio, in , inl);
347} 341}
348 342
349static int asn1_bio_puts(BIO *b, const char *str) 343static int
344asn1_bio_puts(BIO *b, const char *str)
350{ 345{
351 return asn1_bio_write(b, str, strlen(str)); 346 return asn1_bio_write(b, str, strlen(str));
352} 347}
353 348
354static int asn1_bio_gets(BIO *b, char *str, int size) 349static int
350asn1_bio_gets(BIO *b, char *str, int size)
355{ 351{
356 if (!b->next_bio) 352 if (!b->next_bio)
357 return 0; 353 return 0;
358 return BIO_gets(b->next_bio, str , size); 354 return BIO_gets(b->next_bio, str , size);
359} 355}
360 356
361static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 357static long
358asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
362{ 359{
363 if (b->next_bio == NULL) return(0); 360 if (b->next_bio == NULL)
364 return BIO_callback_ctrl(b->next_bio,cmd,fp); 361 return (0);
362 return BIO_callback_ctrl(b->next_bio, cmd, fp);
365} 363}
366 364
367static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) 365static long
366asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
368{ 367{
369 BIO_ASN1_BUF_CTX *ctx; 368 BIO_ASN1_BUF_CTX *ctx;
370 BIO_ASN1_EX_FUNCS *ex_func; 369 BIO_ASN1_EX_FUNCS *ex_func;
371 long ret = 1; 370 long ret = 1;
371
372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
373 if (ctx == NULL) 373 if (ctx == NULL)
374 return 0; 374 return 0;
375 switch(cmd) { 375 switch (cmd) {
376 376
377 case BIO_C_SET_PREFIX: 377 case BIO_C_SET_PREFIX:
378 ex_func = arg2; 378 ex_func = arg2;
379 ctx->prefix = ex_func->ex_func; 379 ctx->prefix = ex_func->ex_func;
380 ctx->prefix_free = ex_func->ex_free_func; 380 ctx->prefix_free = ex_func->ex_free_func;
381 break; 381 break;
382 382
383 case BIO_C_GET_PREFIX: 383 case BIO_C_GET_PREFIX:
@@ -388,8 +388,8 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
388 388
389 case BIO_C_SET_SUFFIX: 389 case BIO_C_SET_SUFFIX:
390 ex_func = arg2; 390 ex_func = arg2;
391 ctx->suffix = ex_func->ex_func; 391 ctx->suffix = ex_func->ex_func;
392 ctx->suffix_free = ex_func->ex_free_func; 392 ctx->suffix_free = ex_func->ex_free_func;
393 break; 393 break;
394 394
395 case BIO_C_GET_SUFFIX: 395 case BIO_C_GET_SUFFIX:
@@ -413,13 +413,13 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
413 /* Call post function if possible */ 413 /* Call post function if possible */
414 if (ctx->state == ASN1_STATE_HEADER) { 414 if (ctx->state == ASN1_STATE_HEADER) {
415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, 415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) 416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
417 return 0; 417 return 0;
418 } 418 }
419 419
420 if (ctx->state == ASN1_STATE_POST_COPY) { 420 if (ctx->state == ASN1_STATE_POST_COPY) {
421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, 421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
422 ASN1_STATE_DONE); 422 ASN1_STATE_DONE);
423 if (ret <= 0) 423 if (ret <= 0)
424 return ret; 424 return ret;
425 } 425 }
@@ -443,20 +443,24 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
443 return ret; 443 return ret;
444} 444}
445 445
446static int asn1_bio_set_ex(BIO *b, int cmd, 446static int
447 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) 447asn1_bio_set_ex(BIO *b, int cmd, asn1_ps_func *ex_func, asn1_ps_func
448 *ex_free_func)
448{ 449{
449 BIO_ASN1_EX_FUNCS extmp; 450 BIO_ASN1_EX_FUNCS extmp;
451
450 extmp.ex_func = ex_func; 452 extmp.ex_func = ex_func;
451 extmp.ex_free_func = ex_free_func; 453 extmp.ex_free_func = ex_free_func;
452 return BIO_ctrl(b, cmd, 0, &extmp); 454 return BIO_ctrl(b, cmd, 0, &extmp);
453} 455}
454 456
455static int asn1_bio_get_ex(BIO *b, int cmd, 457static int
456 asn1_ps_func **ex_func, asn1_ps_func **ex_free_func) 458asn1_bio_get_ex(BIO *b, int cmd, asn1_ps_func **ex_func,
459 asn1_ps_func **ex_free_func)
457{ 460{
458 BIO_ASN1_EX_FUNCS extmp; 461 BIO_ASN1_EX_FUNCS extmp;
459 int ret; 462 int ret;
463
460 ret = BIO_ctrl(b, cmd, 0, &extmp); 464 ret = BIO_ctrl(b, cmd, 0, &extmp);
461 if (ret > 0) { 465 if (ret > 0) {
462 *ex_func = extmp.ex_func; 466 *ex_func = extmp.ex_func;
@@ -465,22 +469,26 @@ static int asn1_bio_get_ex(BIO *b, int cmd,
465 return ret; 469 return ret;
466} 470}
467 471
468int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free) 472int
473BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
469{ 474{
470 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); 475 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
471} 476}
472 477
473int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free) 478int
479BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
474{ 480{
475 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); 481 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
476} 482}
477 483
478int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free) 484int
485BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
479{ 486{
480 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); 487 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
481} 488}
482 489
483int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free) 490int
491BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
484{ 492{
485 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); 493 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
486} 494}
diff --git a/src/lib/libcrypto/asn1/bio_ndef.c b/src/lib/libcrypto/asn1/bio_ndef.c
index 595e6471c2..f00d38e5e4 100644
--- a/src/lib/libcrypto/asn1/bio_ndef.c
+++ b/src/lib/libcrypto/asn1/bio_ndef.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
@@ -78,8 +78,7 @@
78 78
79/* BIO support data stored in the ASN1 BIO ex_arg */ 79/* BIO support data stored in the ASN1 BIO ex_arg */
80 80
81typedef struct ndef_aux_st 81typedef struct ndef_aux_st {
82{
83 /* ASN1 structure this BIO refers to */ 82 /* ASN1 structure this BIO refers to */
84 ASN1_VALUE *val; 83 ASN1_VALUE *val;
85 const ASN1_ITEM *it; 84 const ASN1_ITEM *it;
@@ -98,7 +97,8 @@ static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
98static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg); 97static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
99static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); 98static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
100 99
101BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) 100BIO *
101BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
102{ 102{
103 NDEF_SUPPORT *ndef_aux = NULL; 103 NDEF_SUPPORT *ndef_aux = NULL;
104 BIO *asn_bio = NULL; 104 BIO *asn_bio = NULL;
@@ -143,7 +143,7 @@ BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
143 143
144 return sarg.ndef_bio; 144 return sarg.ndef_bio;
145 145
146 err: 146err:
147 if (asn_bio) 147 if (asn_bio)
148 BIO_free(asn_bio); 148 BIO_free(asn_bio);
149 if (ndef_aux) 149 if (ndef_aux)
@@ -151,7 +151,8 @@ BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
151 return NULL; 151 return NULL;
152} 152}
153 153
154static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 154static int
155ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
155{ 156{
156 NDEF_SUPPORT *ndef_aux; 157 NDEF_SUPPORT *ndef_aux;
157 unsigned char *p; 158 unsigned char *p;
@@ -176,7 +177,8 @@ static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
176 return 1; 177 return 1;
177} 178}
178 179
179static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 180static int
181ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
180{ 182{
181 NDEF_SUPPORT *ndef_aux; 183 NDEF_SUPPORT *ndef_aux;
182 184
@@ -194,7 +196,8 @@ static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
194 return 1; 196 return 1;
195} 197}
196 198
197static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 199static int
200ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
198{ 201{
199 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg; 202 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
200 if (!ndef_prefix_free(b, pbuf, plen, parg)) 203 if (!ndef_prefix_free(b, pbuf, plen, parg))
@@ -204,7 +207,8 @@ static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
204 return 1; 207 return 1;
205} 208}
206 209
207static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 210static int
211ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
208{ 212{
209 NDEF_SUPPORT *ndef_aux; 213 NDEF_SUPPORT *ndef_aux;
210 unsigned char *p; 214 unsigned char *p;
@@ -224,7 +228,7 @@ static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
224 sarg.out = ndef_aux->out; 228 sarg.out = ndef_aux->out;
225 sarg.boundary = ndef_aux->boundary; 229 sarg.boundary = ndef_aux->boundary;
226 if (aux->asn1_cb(ASN1_OP_STREAM_POST, 230 if (aux->asn1_cb(ASN1_OP_STREAM_POST,
227 &ndef_aux->val, ndef_aux->it, &sarg) <= 0) 231 &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
228 return 0; 232 return 0;
229 233
230 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); 234 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
diff --git a/src/lib/libcrypto/asn1/charmap.h b/src/lib/libcrypto/asn1/charmap.h
index b55e638725..acab65b77f 100644
--- a/src/lib/libcrypto/asn1/charmap.h
+++ b/src/lib/libcrypto/asn1/charmap.h
@@ -3,13 +3,12 @@
3 */ 3 */
4 4
5static const unsigned char char_type[] = { 5static const unsigned char char_type[] = {
6 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
7 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 7 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
8120, 0, 1,40, 0, 0, 0,16,16,16, 0,25,25,16,16,16, 8 120, 0, 1, 40, 0, 0, 0, 16, 16, 16, 0, 25, 25, 16, 16, 16,
916,16,16,16,16,16,16,16,16,16,16, 9, 9,16, 9,16, 9 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 9, 9, 16, 9, 16,
10 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, 10 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1116,16,16,16,16,16,16,16,16,16,16, 0, 1, 0, 0, 0, 11 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1, 0, 0, 0,
12 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, 12 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1316,16,16,16,16,16,16,16,16,16,16, 0, 0, 0, 0, 2 13 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 2
14}; 14};
15
diff --git a/src/lib/libssl/src/crypto/asn1/asn_mime.c b/src/lib/libssl/src/crypto/asn1/asn_mime.c
index 6cde8fd44e..890557578d 100644
--- a/src/lib/libssl/src/crypto/asn1/asn_mime.c
+++ b/src/lib/libssl/src/crypto/asn1/asn_mime.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
@@ -71,24 +71,24 @@
71 */ 71 */
72 72
73typedef struct { 73typedef struct {
74char *param_name; /* Param name e.g. "micalg" */ 74 char *param_name; /* Param name e.g. "micalg" */
75char *param_value; /* Param value e.g. "sha1" */ 75 char *param_value; /* Param value e.g. "sha1" */
76} MIME_PARAM; 76} MIME_PARAM;
77 77
78DECLARE_STACK_OF(MIME_PARAM) 78DECLARE_STACK_OF(MIME_PARAM)
79IMPLEMENT_STACK_OF(MIME_PARAM) 79IMPLEMENT_STACK_OF(MIME_PARAM)
80 80
81typedef struct { 81typedef struct {
82char *name; /* Name of line e.g. "content-type" */ 82 char *name; /* Name of line e.g. "content-type" */
83char *value; /* Value of line e.g. "text/plain" */ 83 char *value; /* Value of line e.g. "text/plain" */
84STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */ 84 STACK_OF(MIME_PARAM) *params; /* Zero or more parameters */
85} MIME_HEADER; 85} MIME_HEADER;
86 86
87DECLARE_STACK_OF(MIME_HEADER) 87DECLARE_STACK_OF(MIME_HEADER)
88IMPLEMENT_STACK_OF(MIME_HEADER) 88IMPLEMENT_STACK_OF(MIME_HEADER)
89 89
90static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, 90static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
91 const ASN1_ITEM *it); 91 const ASN1_ITEM *it);
92static char * strip_ends(char *name); 92static char * strip_ends(char *name);
93static char * strip_start(char *name); 93static char * strip_start(char *name);
94static char * strip_end(char *name); 94static char * strip_end(char *name);
@@ -96,9 +96,9 @@ static MIME_HEADER *mime_hdr_new(char *name, char *value);
96static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value); 96static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value);
97static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio); 97static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio);
98static int mime_hdr_cmp(const MIME_HEADER * const *a, 98static int mime_hdr_cmp(const MIME_HEADER * const *a,
99 const MIME_HEADER * const *b); 99 const MIME_HEADER * const *b);
100static int mime_param_cmp(const MIME_PARAM * const *a, 100static int mime_param_cmp(const MIME_PARAM * const *a,
101 const MIME_PARAM * const *b); 101 const MIME_PARAM * const *b);
102static void mime_param_free(MIME_PARAM *param); 102static void mime_param_free(MIME_PARAM *param);
103static int mime_bound_check(char *line, int linelen, char *bound, int blen); 103static int mime_bound_check(char *line, int linelen, char *bound, int blen);
104static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret); 104static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret);
@@ -112,15 +112,17 @@ static void mime_hdr_free(MIME_HEADER *hdr);
112 112
113/* Output an ASN1 structure in BER format streaming if necessary */ 113/* Output an ASN1 structure in BER format streaming if necessary */
114 114
115int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 115int
116 const ASN1_ITEM *it) 116i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
117 const ASN1_ITEM *it)
117{ 118{
118 /* If streaming create stream BIO and copy all content through it */ 119 /* If streaming create stream BIO and copy all content through it */
119 if (flags & SMIME_STREAM) { 120 if (flags & SMIME_STREAM) {
120 BIO *bio, *tbio; 121 BIO *bio, *tbio;
121 bio = BIO_new_NDEF(out, val, it); 122 bio = BIO_new_NDEF(out, val, it);
122 if (!bio) { 123 if (!bio) {
123 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,ERR_R_MALLOC_FAILURE); 124 ASN1err(ASN1_F_I2D_ASN1_BIO_STREAM,
125 ERR_R_MALLOC_FAILURE);
124 return 0; 126 return 0;
125 } 127 }
126 SMIME_crlf_copy(in, bio, flags); 128 SMIME_crlf_copy(in, bio, flags);
@@ -142,14 +144,16 @@ int i2d_ASN1_bio_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
142 144
143/* Base 64 read and write of ASN1 structure */ 145/* Base 64 read and write of ASN1 structure */
144 146
145static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 147static int
146 const ASN1_ITEM *it) 148B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
149 const ASN1_ITEM *it)
147{ 150{
148 BIO *b64; 151 BIO *b64;
149 int r; 152 int r;
153
150 b64 = BIO_new(BIO_f_base64()); 154 b64 = BIO_new(BIO_f_base64());
151 if(!b64) { 155 if (!b64) {
152 ASN1err(ASN1_F_B64_WRITE_ASN1,ERR_R_MALLOC_FAILURE); 156 ASN1err(ASN1_F_B64_WRITE_ASN1, ERR_R_MALLOC_FAILURE);
153 return 0; 157 return 0;
154 } 158 }
155 /* prepend the b64 BIO so all data is base64 encoded. 159 /* prepend the b64 BIO so all data is base64 encoded.
@@ -164,29 +168,31 @@ static int B64_write_ASN1(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
164 168
165/* Streaming ASN1 PEM write */ 169/* Streaming ASN1 PEM write */
166 170
167int PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags, 171int
168 const char *hdr, 172PEM_write_bio_ASN1_stream(BIO *out, ASN1_VALUE *val, BIO *in, int flags,
169 const ASN1_ITEM *it) 173 const char *hdr, const ASN1_ITEM *it)
170{ 174{
171 int r; 175 int r;
176
172 BIO_printf(out, "-----BEGIN %s-----\n", hdr); 177 BIO_printf(out, "-----BEGIN %s-----\n", hdr);
173 r = B64_write_ASN1(out, val, in, flags, it); 178 r = B64_write_ASN1(out, val, in, flags, it);
174 BIO_printf(out, "-----END %s-----\n", hdr); 179 BIO_printf(out, "-----END %s-----\n", hdr);
175 return r; 180 return r;
176} 181}
177 182
178static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it) 183static ASN1_VALUE *
184b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
179{ 185{
180 BIO *b64; 186 BIO *b64;
181 ASN1_VALUE *val; 187 ASN1_VALUE *val;
182 if(!(b64 = BIO_new(BIO_f_base64()))) { 188 if (!(b64 = BIO_new(BIO_f_base64()))) {
183 ASN1err(ASN1_F_B64_READ_ASN1,ERR_R_MALLOC_FAILURE); 189 ASN1err(ASN1_F_B64_READ_ASN1, ERR_R_MALLOC_FAILURE);
184 return 0; 190 return 0;
185} 191 }
186 bio = BIO_push(b64, bio); 192 bio = BIO_push(b64, bio);
187 val = ASN1_item_d2i_bio(it, bio, NULL); 193 val = ASN1_item_d2i_bio(it, bio, NULL);
188 if(!val) 194 if (!val)
189 ASN1err(ASN1_F_B64_READ_ASN1,ASN1_R_DECODE_ERROR); 195 ASN1err(ASN1_F_B64_READ_ASN1, ASN1_R_DECODE_ERROR);
190 (void)BIO_flush(bio); 196 (void)BIO_flush(bio);
191 bio = BIO_pop(bio); 197 bio = BIO_pop(bio);
192 BIO_free(b64); 198 BIO_free(b64);
@@ -195,10 +201,12 @@ static ASN1_VALUE *b64_read_asn1(BIO *bio, const ASN1_ITEM *it)
195 201
196/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */ 202/* Generate the MIME "micalg" parameter from RFC3851, RFC4490 */
197 203
198static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs) 204static int
205asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
199{ 206{
200 const EVP_MD *md; 207 const EVP_MD *md;
201 int i, have_unknown = 0, write_comma, ret = 0, md_nid; 208 int i, have_unknown = 0, write_comma, ret = 0, md_nid;
209
202 have_unknown = 0; 210 have_unknown = 0;
203 write_comma = 0; 211 write_comma = 0;
204 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) { 212 for (i = 0; i < sk_X509_ALGOR_num(mdalgs); i++) {
@@ -219,33 +227,33 @@ static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
219 if (rv != -2) 227 if (rv != -2)
220 goto err; 228 goto err;
221 } 229 }
222 switch(md_nid) { 230 switch (md_nid) {
223 case NID_sha1: 231 case NID_sha1:
224 BIO_puts(out, "sha1"); 232 BIO_puts(out, "sha1");
225 break; 233 break;
226 234
227 case NID_md5: 235 case NID_md5:
228 BIO_puts(out, "md5"); 236 BIO_puts(out, "md5");
229 break; 237 break;
230 238
231 case NID_sha256: 239 case NID_sha256:
232 BIO_puts(out, "sha-256"); 240 BIO_puts(out, "sha-256");
233 break; 241 break;
234 242
235 case NID_sha384: 243 case NID_sha384:
236 BIO_puts(out, "sha-384"); 244 BIO_puts(out, "sha-384");
237 break; 245 break;
238 246
239 case NID_sha512: 247 case NID_sha512:
240 BIO_puts(out, "sha-512"); 248 BIO_puts(out, "sha-512");
241 break; 249 break;
242 250
243 case NID_id_GostR3411_94: 251 case NID_id_GostR3411_94:
244 BIO_puts(out, "gostr3411-94"); 252 BIO_puts(out, "gostr3411-94");
245 goto err; 253 goto err;
246 break; 254 break;
247 255
248 default: 256 default:
249 if (have_unknown) 257 if (have_unknown)
250 write_comma = 0; 258 write_comma = 0;
251 else { 259 else {
@@ -258,23 +266,23 @@ static int asn1_write_micalg(BIO *out, STACK_OF(X509_ALGOR) *mdalgs)
258 } 266 }
259 267
260 ret = 1; 268 ret = 1;
261 err:
262 269
270err:
263 return ret; 271 return ret;
264
265} 272}
266 273
267/* SMIME sender */ 274/* SMIME sender */
268 275
269int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags, 276int
270 int ctype_nid, int econt_nid, 277SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
271 STACK_OF(X509_ALGOR) *mdalgs, 278 int ctype_nid, int econt_nid, STACK_OF(X509_ALGOR) *mdalgs,
272 const ASN1_ITEM *it) 279 const ASN1_ITEM *it)
273{ 280{
274 char bound[33], c; 281 char bound[33], c;
275 int i; 282 int i;
276 const char *mime_prefix, *mime_eol, *cname = "smime.p7m"; 283 const char *mime_prefix, *mime_eol, *cname = "smime.p7m";
277 const char *msg_type=NULL; 284 const char *msg_type = NULL;
285
278 if (flags & SMIME_OLDMIME) 286 if (flags & SMIME_OLDMIME)
279 mime_prefix = "application/x-pkcs7-"; 287 mime_prefix = "application/x-pkcs7-";
280 else 288 else
@@ -284,16 +292,18 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
284 mime_eol = "\r\n"; 292 mime_eol = "\r\n";
285 else 293 else
286 mime_eol = "\n"; 294 mime_eol = "\n";
287 if((flags & SMIME_DETACHED) && data) { 295 if ((flags & SMIME_DETACHED) && data) {
288 /* We want multipart/signed */ 296 /* We want multipart/signed */
289 /* Generate a random boundary */ 297 /* Generate a random boundary */
290 RAND_pseudo_bytes((unsigned char *)bound, 32); 298 RAND_pseudo_bytes((unsigned char *)bound, 32);
291 for(i = 0; i < 32; i++) { 299 for (i = 0; i < 32; i++) {
292 c = bound[i] & 0xf; 300 c = bound[i] & 0xf;
293 if(c < 10) c += '0'; 301 if (c < 10)
294 else c += 'A' - 10; 302 c += '0';
303 else
304 c += 'A' - 10;
295 bound[i] = c; 305 bound[i] = c;
296 } 306 }
297 bound[32] = 0; 307 bound[32] = 0;
298 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol); 308 BIO_printf(bio, "MIME-Version: 1.0%s", mime_eol);
299 BIO_printf(bio, "Content-Type: multipart/signed;"); 309 BIO_printf(bio, "Content-Type: multipart/signed;");
@@ -301,9 +311,9 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
301 BIO_puts(bio, " micalg=\""); 311 BIO_puts(bio, " micalg=\"");
302 asn1_write_micalg(bio, mdalgs); 312 asn1_write_micalg(bio, mdalgs);
303 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s", 313 BIO_printf(bio, "\"; boundary=\"----%s\"%s%s",
304 bound, mime_eol, mime_eol); 314 bound, mime_eol, mime_eol);
305 BIO_printf(bio, "This is an S/MIME signed message%s%s", 315 BIO_printf(bio, "This is an S/MIME signed message%s%s",
306 mime_eol, mime_eol); 316 mime_eol, mime_eol);
307 /* Now write out the first part */ 317 /* Now write out the first part */
308 BIO_printf(bio, "------%s%s", bound, mime_eol); 318 BIO_printf(bio, "------%s%s", bound, mime_eol);
309 if (!asn1_output_data(bio, data, val, flags, it)) 319 if (!asn1_output_data(bio, data, val, flags, it))
@@ -312,18 +322,18 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
312 322
313 /* Headers for signature */ 323 /* Headers for signature */
314 324
315 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix); 325 BIO_printf(bio, "Content-Type: %ssignature;", mime_prefix);
316 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol); 326 BIO_printf(bio, " name=\"smime.p7s\"%s", mime_eol);
317 BIO_printf(bio, "Content-Transfer-Encoding: base64%s", 327 BIO_printf(bio, "Content-Transfer-Encoding: base64%s",
318 mime_eol); 328 mime_eol);
319 BIO_printf(bio, "Content-Disposition: attachment;"); 329 BIO_printf(bio, "Content-Disposition: attachment;");
320 BIO_printf(bio, " filename=\"smime.p7s\"%s%s", 330 BIO_printf(bio, " filename=\"smime.p7s\"%s%s",
321 mime_eol, mime_eol); 331 mime_eol, mime_eol);
322 B64_write_ASN1(bio, val, NULL, 0, it); 332 B64_write_ASN1(bio, val, NULL, 0, it);
323 BIO_printf(bio,"%s------%s--%s%s", mime_eol, bound, 333 BIO_printf(bio, "%s------%s--%s%s", mime_eol, bound,
324 mime_eol, mime_eol); 334 mime_eol, mime_eol);
325 return 1; 335 return 1;
326} 336 }
327 337
328 /* Determine smime-type header */ 338 /* Determine smime-type header */
329 339
@@ -349,7 +359,7 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
349 BIO_printf(bio, " smime-type=%s;", msg_type); 359 BIO_printf(bio, " smime-type=%s;", msg_type);
350 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol); 360 BIO_printf(bio, " name=\"%s\"%s", cname, mime_eol);
351 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s", 361 BIO_printf(bio, "Content-Transfer-Encoding: base64%s%s",
352 mime_eol, mime_eol); 362 mime_eol, mime_eol);
353 if (!B64_write_ASN1(bio, val, data, flags, it)) 363 if (!B64_write_ASN1(bio, val, data, flags, it))
354 return 0; 364 return 0;
355 BIO_printf(bio, "%s", mime_eol); 365 BIO_printf(bio, "%s", mime_eol);
@@ -359,8 +369,9 @@ int SMIME_write_ASN1(BIO *bio, ASN1_VALUE *val, BIO *data, int flags,
359/* Handle output of ASN1 data */ 369/* Handle output of ASN1 data */
360 370
361 371
362static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags, 372static int
363 const ASN1_ITEM *it) 373asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
374 const ASN1_ITEM *it)
364{ 375{
365 BIO *tmpbio; 376 BIO *tmpbio;
366 const ASN1_AUX *aux = it->funcs; 377 const ASN1_AUX *aux = it->funcs;
@@ -377,7 +388,7 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
377 388
378 if (!aux || !aux->asn1_cb) { 389 if (!aux || !aux->asn1_cb) {
379 ASN1err(ASN1_F_ASN1_OUTPUT_DATA, 390 ASN1err(ASN1_F_ASN1_OUTPUT_DATA,
380 ASN1_R_STREAMING_NOT_SUPPORTED); 391 ASN1_R_STREAMING_NOT_SUPPORTED);
381 return 0; 392 return 0;
382 } 393 }
383 394
@@ -406,7 +417,6 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
406 } 417 }
407 418
408 return rv; 419 return rv;
409
410} 420}
411 421
412/* SMIME reader: handle multipart/signed and opaque signing. 422/* SMIME reader: handle multipart/signed and opaque signing.
@@ -414,7 +424,8 @@ static int asn1_output_data(BIO *out, BIO *data, ASN1_VALUE *val, int flags,
414 * pointed to by "bcont". In opaque this is set to NULL 424 * pointed to by "bcont". In opaque this is set to NULL
415 */ 425 */
416 426
417ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it) 427ASN1_VALUE *
428SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
418{ 429{
419 BIO *asnin; 430 BIO *asnin;
420 STACK_OF(MIME_HEADER) *headers = NULL; 431 STACK_OF(MIME_HEADER) *headers = NULL;
@@ -424,106 +435,114 @@ ASN1_VALUE *SMIME_read_ASN1(BIO *bio, BIO **bcont, const ASN1_ITEM *it)
424 ASN1_VALUE *val; 435 ASN1_VALUE *val;
425 int ret; 436 int ret;
426 437
427 if(bcont) *bcont = NULL; 438 if (bcont)
439 *bcont = NULL;
428 440
429 if (!(headers = mime_parse_hdr(bio))) { 441 if (!(headers = mime_parse_hdr(bio))) {
430 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_PARSE_ERROR); 442 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_MIME_PARSE_ERROR);
431 return NULL; 443 return NULL;
432} 444 }
433 445
434 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { 446 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
435 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 447 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
436 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE); 448 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_CONTENT_TYPE);
437 return NULL; 449 return NULL;
438} 450 }
439 451
440 /* Handle multipart/signed */ 452 /* Handle multipart/signed */
441 453
442 if(!strcmp(hdr->value, "multipart/signed")) { 454 if (!strcmp(hdr->value, "multipart/signed")) {
443 /* Split into two parts */ 455 /* Split into two parts */
444 prm = mime_param_find(hdr, "boundary"); 456 prm = mime_param_find(hdr, "boundary");
445 if(!prm || !prm->param_value) { 457 if (!prm || !prm->param_value) {
446 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 458 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
447 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BOUNDARY); 459 ASN1err(ASN1_F_SMIME_READ_ASN1,
460 ASN1_R_NO_MULTIPART_BOUNDARY);
448 return NULL; 461 return NULL;
449 } 462 }
450 ret = multi_split(bio, prm->param_value, &parts); 463 ret = multi_split(bio, prm->param_value, &parts);
451 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 464 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
452 if(!ret || (sk_BIO_num(parts) != 2) ) { 465 if (!ret || (sk_BIO_num(parts) != 2) ) {
453 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_MULTIPART_BODY_FAILURE); 466 ASN1err(ASN1_F_SMIME_READ_ASN1,
467 ASN1_R_NO_MULTIPART_BODY_FAILURE);
454 sk_BIO_pop_free(parts, BIO_vfree); 468 sk_BIO_pop_free(parts, BIO_vfree);
455 return NULL; 469 return NULL;
456 } 470 }
457 471
458 /* Parse the signature piece */ 472 /* Parse the signature piece */
459 asnin = sk_BIO_value(parts, 1); 473 asnin = sk_BIO_value(parts, 1);
460 474
461 if (!(headers = mime_parse_hdr(asnin))) { 475 if (!(headers = mime_parse_hdr(asnin))) {
462 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_MIME_SIG_PARSE_ERROR); 476 ASN1err(ASN1_F_SMIME_READ_ASN1,
477 ASN1_R_MIME_SIG_PARSE_ERROR);
463 sk_BIO_pop_free(parts, BIO_vfree); 478 sk_BIO_pop_free(parts, BIO_vfree);
464 return NULL; 479 return NULL;
465 } 480 }
466 481
467 /* Get content type */ 482 /* Get content type */
468 483
469 if(!(hdr = mime_hdr_find(headers, "content-type")) || 484 if (!(hdr = mime_hdr_find(headers, "content-type")) ||
470 !hdr->value) { 485 !hdr->value) {
471 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 486 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
472 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_NO_SIG_CONTENT_TYPE); 487 ASN1err(ASN1_F_SMIME_READ_ASN1,
488 ASN1_R_NO_SIG_CONTENT_TYPE);
473 return NULL; 489 return NULL;
474 } 490 }
475 491
476 if(strcmp(hdr->value, "application/x-pkcs7-signature") && 492 if (strcmp(hdr->value, "application/x-pkcs7-signature") &&
477 strcmp(hdr->value, "application/pkcs7-signature")) { 493 strcmp(hdr->value, "application/pkcs7-signature")) {
478 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_SIG_INVALID_MIME_TYPE); 494 ASN1err(ASN1_F_SMIME_READ_ASN1,
495 ASN1_R_SIG_INVALID_MIME_TYPE);
479 ERR_add_error_data(2, "type: ", hdr->value); 496 ERR_add_error_data(2, "type: ", hdr->value);
480 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 497 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
481 sk_BIO_pop_free(parts, BIO_vfree); 498 sk_BIO_pop_free(parts, BIO_vfree);
482 return NULL; 499 return NULL;
483 } 500 }
484 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 501 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
485 /* Read in ASN1 */ 502 /* Read in ASN1 */
486 if(!(val = b64_read_asn1(asnin, it))) { 503 if (!(val = b64_read_asn1(asnin, it))) {
487 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_ASN1_SIG_PARSE_ERROR); 504 ASN1err(ASN1_F_SMIME_READ_ASN1,
505 ASN1_R_ASN1_SIG_PARSE_ERROR);
488 sk_BIO_pop_free(parts, BIO_vfree); 506 sk_BIO_pop_free(parts, BIO_vfree);
489 return NULL; 507 return NULL;
490 } 508 }
491 509
492 if(bcont) { 510 if (bcont) {
493 *bcont = sk_BIO_value(parts, 0); 511 *bcont = sk_BIO_value(parts, 0);
494 BIO_free(asnin); 512 BIO_free(asnin);
495 sk_BIO_free(parts); 513 sk_BIO_free(parts);
496 } else sk_BIO_pop_free(parts, BIO_vfree); 514 } else sk_BIO_pop_free(parts, BIO_vfree);
497 return val; 515 return val;
498} 516 }
499 517
500 /* OK, if not multipart/signed try opaque signature */ 518 /* OK, if not multipart/signed try opaque signature */
501 519
502 if (strcmp (hdr->value, "application/x-pkcs7-mime") && 520 if (strcmp (hdr->value, "application/x-pkcs7-mime") &&
503 strcmp (hdr->value, "application/pkcs7-mime")) { 521 strcmp (hdr->value, "application/pkcs7-mime")) {
504 ASN1err(ASN1_F_SMIME_READ_ASN1,ASN1_R_INVALID_MIME_TYPE); 522 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_INVALID_MIME_TYPE);
505 ERR_add_error_data(2, "type: ", hdr->value); 523 ERR_add_error_data(2, "type: ", hdr->value);
506 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 524 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
507 return NULL; 525 return NULL;
508} 526 }
509 527
510 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 528 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
511 529
512 if(!(val = b64_read_asn1(bio, it))) { 530 if (!(val = b64_read_asn1(bio, it))) {
513 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR); 531 ASN1err(ASN1_F_SMIME_READ_ASN1, ASN1_R_ASN1_PARSE_ERROR);
514 return NULL; 532 return NULL;
515} 533 }
516 return val; 534 return val;
517
518} 535}
519 536
520/* Copy text from one BIO to another making the output CRLF at EOL */ 537/* Copy text from one BIO to another making the output CRLF at EOL */
521int SMIME_crlf_copy(BIO *in, BIO *out, int flags) 538int
539SMIME_crlf_copy(BIO *in, BIO *out, int flags)
522{ 540{
523 BIO *bf; 541 BIO *bf;
524 char eol; 542 char eol;
525 int len; 543 int len;
526 char linebuf[MAX_SMLEN]; 544 char linebuf[MAX_SMLEN];
545
527 /* Buffer output so we don't write one line at a time. This is 546 /* Buffer output so we don't write one line at a time. This is
528 * useful when streaming as we don't end up with one OCTET STRING 547 * useful when streaming as we don't end up with one OCTET STRING
529 * per line. 548 * per line.
@@ -532,17 +551,18 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
532 if (!bf) 551 if (!bf)
533 return 0; 552 return 0;
534 out = BIO_push(bf, out); 553 out = BIO_push(bf, out);
535 if(flags & SMIME_BINARY) { 554 if (flags & SMIME_BINARY) {
536 while((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0) 555 while ((len = BIO_read(in, linebuf, MAX_SMLEN)) > 0)
537 BIO_write(out, linebuf, len); 556 BIO_write(out, linebuf, len);
538 } else { 557 } else {
539 if(flags & SMIME_TEXT) 558 if (flags & SMIME_TEXT)
540 BIO_printf(out, "Content-Type: text/plain\r\n\r\n"); 559 BIO_printf(out, "Content-Type: text/plain\r\n\r\n");
541 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) { 560 while ((len = BIO_gets(in, linebuf, MAX_SMLEN)) > 0) {
542 eol = strip_eol(linebuf, &len); 561 eol = strip_eol(linebuf, &len);
543 if (len) 562 if (len)
544 BIO_write(out, linebuf, len); 563 BIO_write(out, linebuf, len);
545 if(eol) BIO_write(out, "\r\n", 2); 564 if (eol)
565 BIO_write(out, "\r\n", 2);
546 } 566 }
547 } 567 }
548 (void)BIO_flush(out); 568 (void)BIO_flush(out);
@@ -552,7 +572,8 @@ int SMIME_crlf_copy(BIO *in, BIO *out, int flags)
552} 572}
553 573
554/* Strip off headers if they are text/plain */ 574/* Strip off headers if they are text/plain */
555int SMIME_text(BIO *in, BIO *out) 575int
576SMIME_text(BIO *in, BIO *out)
556{ 577{
557 char iobuf[4096]; 578 char iobuf[4096];
558 int len; 579 int len;
@@ -560,23 +581,23 @@ int SMIME_text(BIO *in, BIO *out)
560 MIME_HEADER *hdr; 581 MIME_HEADER *hdr;
561 582
562 if (!(headers = mime_parse_hdr(in))) { 583 if (!(headers = mime_parse_hdr(in))) {
563 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_PARSE_ERROR); 584 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_PARSE_ERROR);
564 return 0; 585 return 0;
565} 586 }
566 if(!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) { 587 if (!(hdr = mime_hdr_find(headers, "content-type")) || !hdr->value) {
567 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_MIME_NO_CONTENT_TYPE); 588 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_MIME_NO_CONTENT_TYPE);
568 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 589 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
569 return 0; 590 return 0;
570} 591 }
571 if (strcmp (hdr->value, "text/plain")) { 592 if (strcmp (hdr->value, "text/plain")) {
572 ASN1err(ASN1_F_SMIME_TEXT,ASN1_R_INVALID_MIME_TYPE); 593 ASN1err(ASN1_F_SMIME_TEXT, ASN1_R_INVALID_MIME_TYPE);
573 ERR_add_error_data(2, "type: ", hdr->value); 594 ERR_add_error_data(2, "type: ", hdr->value);
574 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 595 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
575 return 0; 596 return 0;
576} 597 }
577 sk_MIME_HEADER_pop_free(headers, mime_hdr_free); 598 sk_MIME_HEADER_pop_free(headers, mime_hdr_free);
578 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0) 599 while ((len = BIO_read(in, iobuf, sizeof(iobuf))) > 0)
579 BIO_write(out, iobuf, len); 600 BIO_write(out, iobuf, len);
580 if (len < 0) 601 if (len < 0)
581 return 0; 602 return 0;
582 return 1; 603 return 1;
@@ -586,7 +607,8 @@ int SMIME_text(BIO *in, BIO *out)
586 * canonical parts in a STACK of bios 607 * canonical parts in a STACK of bios
587 */ 608 */
588 609
589static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret) 610static int
611multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
590{ 612{
591 char linebuf[MAX_SMLEN]; 613 char linebuf[MAX_SMLEN];
592 int len, blen; 614 int len, blen;
@@ -603,27 +625,28 @@ static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
603 *ret = parts; 625 *ret = parts;
604 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { 626 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
605 state = mime_bound_check(linebuf, len, bound, blen); 627 state = mime_bound_check(linebuf, len, bound, blen);
606 if(state == 1) { 628 if (state == 1) {
607 first = 1; 629 first = 1;
608 part++; 630 part++;
609 } else if(state == 2) { 631 } else if (state == 2) {
610 sk_BIO_push(parts, bpart); 632 sk_BIO_push(parts, bpart);
611 return 1; 633 return 1;
612 } else if(part) { 634 } else if (part) {
613 /* Strip CR+LF from linebuf */ 635 /* Strip CR+LF from linebuf */
614 next_eol = strip_eol(linebuf, &len); 636 next_eol = strip_eol(linebuf, &len);
615 if(first) { 637 if (first) {
616 first = 0; 638 first = 0;
617 if(bpart) sk_BIO_push(parts, bpart); 639 if (bpart)
640 sk_BIO_push(parts, bpart);
618 bpart = BIO_new(BIO_s_mem()); 641 bpart = BIO_new(BIO_s_mem());
619 BIO_set_mem_eof_return(bpart, 0); 642 BIO_set_mem_eof_return(bpart, 0);
620 } else if (eol) 643 } else if (eol)
621 BIO_write(bpart, "\r\n", 2); 644 BIO_write(bpart, "\r\n", 2);
622 eol = next_eol; 645 eol = next_eol;
623 if (len) 646 if (len)
624 BIO_write(bpart, linebuf, len); 647 BIO_write(bpart, linebuf, len);
648 }
625 } 649 }
626}
627 return 0; 650 return 0;
628} 651}
629 652
@@ -638,7 +661,8 @@ static int multi_split(BIO *bio, char *bound, STACK_OF(BIO) **ret)
638#define MIME_COMMENT 6 661#define MIME_COMMENT 6
639 662
640 663
641static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio) 664static
665STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
642{ 666{
643 char *p, *q, c; 667 char *p, *q, c;
644 char *ntmp; 668 char *ntmp;
@@ -649,254 +673,294 @@ static STACK_OF(MIME_HEADER) *mime_parse_hdr(BIO *bio)
649 673
650 headers = sk_MIME_HEADER_new(mime_hdr_cmp); 674 headers = sk_MIME_HEADER_new(mime_hdr_cmp);
651 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) { 675 while ((len = BIO_gets(bio, linebuf, MAX_SMLEN)) > 0) {
652 /* If whitespace at line start then continuation line */ 676 /* If whitespace at line start then continuation line */
653 if(mhdr && isspace((unsigned char)linebuf[0])) state = MIME_NAME; 677 if (mhdr && isspace((unsigned char)linebuf[0]))
654 else state = MIME_START; 678 state = MIME_NAME;
655 ntmp = NULL; 679 else
656 /* Go through all characters */ 680 state = MIME_START;
657 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) { 681 ntmp = NULL;
658 682
659 /* State machine to handle MIME headers 683 /* Go through all characters */
660 * if this looks horrible that's because it *is* 684 for (p = linebuf, q = linebuf;
661 */ 685 (c = *p) && (c != '\r') && (c != '\n'); p++) {
662 686
663 switch(state) { 687 /* State machine to handle MIME headers
688 * if this looks horrible that's because it *is*
689 */
690
691 switch (state) {
664 case MIME_START: 692 case MIME_START:
665 if(c == ':') { 693 if (c == ':') {
666 state = MIME_TYPE; 694 state = MIME_TYPE;
667 *p = 0; 695 *p = 0;
668 ntmp = strip_ends(q); 696 ntmp = strip_ends(q);
669 q = p + 1; 697 q = p + 1;
670 } 698 }
671 break; 699 break;
672 700
673 case MIME_TYPE: 701 case MIME_TYPE:
674 if(c == ';') { 702 if (c == ';') {
675 mime_debug("Found End Value\n"); 703 mime_debug("Found End Value\n");
676 *p = 0; 704 *p = 0;
677 mhdr = mime_hdr_new(ntmp, strip_ends(q)); 705 mhdr = mime_hdr_new(ntmp,
678 sk_MIME_HEADER_push(headers, mhdr); 706 strip_ends(q));
679 ntmp = NULL; 707 sk_MIME_HEADER_push(headers, mhdr);
680 q = p + 1; 708 ntmp = NULL;
681 state = MIME_NAME; 709 q = p + 1;
682 } else if(c == '(') { 710 state = MIME_NAME;
683 save_state = state; 711 } else if (c == '(') {
684 state = MIME_COMMENT; 712 save_state = state;
685 } 713 state = MIME_COMMENT;
686 break; 714 }
715 break;
687 716
688 case MIME_COMMENT: 717 case MIME_COMMENT:
689 if(c == ')') { 718 if (c == ')') {
690 state = save_state; 719 state = save_state;
691 } 720 }
692 break; 721 break;
693 722
694 case MIME_NAME: 723 case MIME_NAME:
695 if(c == '=') { 724 if (c == '=') {
696 state = MIME_VALUE; 725 state = MIME_VALUE;
697 *p = 0; 726 *p = 0;
698 ntmp = strip_ends(q); 727 ntmp = strip_ends(q);
699 q = p + 1; 728 q = p + 1;
700 } 729 }
701 break ; 730 break;
702 731
703 case MIME_VALUE: 732 case MIME_VALUE:
704 if(c == ';') { 733 if (c == ';') {
705 state = MIME_NAME; 734 state = MIME_NAME;
706 *p = 0; 735 *p = 0;
707 mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); 736 mime_hdr_addparam(mhdr, ntmp,
708 ntmp = NULL; 737 strip_ends(q));
709 q = p + 1; 738 ntmp = NULL;
710 } else if (c == '"') { 739 q = p + 1;
711 mime_debug("Found Quote\n"); 740 } else if (c == '"') {
712 state = MIME_QUOTE; 741 mime_debug("Found Quote\n");
713 } else if(c == '(') { 742 state = MIME_QUOTE;
714 save_state = state; 743 } else if (c == '(') {
715 state = MIME_COMMENT; 744 save_state = state;
716 } 745 state = MIME_COMMENT;
717 break; 746 }
747 break;
718 748
719 case MIME_QUOTE: 749 case MIME_QUOTE:
720 if(c == '"') { 750 if (c == '"') {
721 mime_debug("Found Match Quote\n"); 751 mime_debug("Found Match Quote\n");
722 state = MIME_VALUE; 752 state = MIME_VALUE;
753 }
754 break;
755 }
723 } 756 }
724 break;
725 }
726}
727 757
728 if(state == MIME_TYPE) { 758 if (state == MIME_TYPE) {
729 mhdr = mime_hdr_new(ntmp, strip_ends(q)); 759 mhdr = mime_hdr_new(ntmp, strip_ends(q));
730 sk_MIME_HEADER_push(headers, mhdr); 760 sk_MIME_HEADER_push(headers, mhdr);
731} else if(state == MIME_VALUE) 761 } else if (state == MIME_VALUE)
732 mime_hdr_addparam(mhdr, ntmp, strip_ends(q)); 762 mime_hdr_addparam(mhdr, ntmp, strip_ends(q));
733 if(p == linebuf) break; /* Blank line means end of headers */
734}
735 763
736return headers; 764 if (p == linebuf)
765 break; /* Blank line means end of headers */
766 }
737 767
768 return headers;
738} 769}
739 770
740static char *strip_ends(char *name) 771static char *
772strip_ends(char *name)
741{ 773{
742 return strip_end(strip_start(name)); 774 return strip_end(strip_start(name));
743} 775}
744 776
745/* Strip a parameter of whitespace from start of param */ 777/* Strip a parameter of whitespace from start of param */
746static char *strip_start(char *name) 778static char *
779strip_start(char *name)
747{ 780{
748 char *p, c; 781 char *p, c;
782
749 /* Look for first non white space or quote */ 783 /* Look for first non white space or quote */
750 for(p = name; (c = *p) ;p++) { 784 for (p = name; (c = *p); p++) {
751 if(c == '"') { 785 if (c == '"') {
752 /* Next char is start of string if non null */ 786 /* Next char is start of string if non null */
753 if(p[1]) return p + 1; 787 if (p[1])
788 return p + 1;
754 /* Else null string */ 789 /* Else null string */
755 return NULL; 790 return NULL;
791 }
792 if (!isspace((unsigned char)c))
793 return p;
756 } 794 }
757 if(!isspace((unsigned char)c)) return p;
758}
759 return NULL; 795 return NULL;
760} 796}
761 797
762/* As above but strip from end of string : maybe should handle brackets? */ 798/* As above but strip from end of string : maybe should handle brackets? */
763static char *strip_end(char *name) 799static char *
800strip_end(char *name)
764{ 801{
765 char *p, c; 802 char *p, c;
766 if(!name) return NULL; 803
804 if (!name)
805 return NULL;
806
767 /* Look for first non white space or quote */ 807 /* Look for first non white space or quote */
768 for(p = name + strlen(name) - 1; p >= name ;p--) { 808 for (p = name + strlen(name) - 1; p >= name; p--) {
769 c = *p; 809 c = *p;
770 if(c == '"') { 810 if (c == '"') {
771 if(p - 1 == name) return NULL; 811 if (p - 1 == name)
812 return NULL;
772 *p = 0; 813 *p = 0;
773 return name; 814 return name;
815 }
816 if (isspace((unsigned char)c))
817 *p = 0;
818 else
819 return name;
774 } 820 }
775 if(isspace((unsigned char)c)) *p = 0;
776 else return name;
777}
778 return NULL; 821 return NULL;
779} 822}
780 823
781static MIME_HEADER *mime_hdr_new(char *name, char *value) 824static MIME_HEADER *
825mime_hdr_new(char *name, char *value)
782{ 826{
783 MIME_HEADER *mhdr; 827 MIME_HEADER *mhdr;
784 char *tmpname, *tmpval, *p; 828 char *tmpname, *tmpval, *p;
785 int c; 829 int c;
786 if(name) { 830 if (name) {
787 if(!(tmpname = BUF_strdup(name))) return NULL; 831 if (!(tmpname = BUF_strdup(name)))
788 for(p = tmpname ; *p; p++) { 832 return NULL;
833 for (p = tmpname; *p; p++) {
789 c = (unsigned char)*p; 834 c = (unsigned char)*p;
790 if(isupper(c)) { 835 if (isupper(c)) {
791 c = tolower(c); 836 c = tolower(c);
792 *p = c; 837 *p = c;
838 }
793 } 839 }
794 } 840 } else
795} else tmpname = NULL; 841 tmpname = NULL;
796 if(value) { 842 if (value) {
797 if(!(tmpval = BUF_strdup(value))) return NULL; 843 if (!(tmpval = BUF_strdup(value)))
798 for(p = tmpval ; *p; p++) { 844 return NULL;
845 for (p = tmpval; *p; p++) {
799 c = (unsigned char)*p; 846 c = (unsigned char)*p;
800 if(isupper(c)) { 847 if (isupper(c)) {
801 c = tolower(c); 848 c = tolower(c);
802 *p = c; 849 *p = c;
850 }
803 } 851 }
804 } 852 } else tmpval = NULL;
805} else tmpval = NULL; 853 mhdr = (MIME_HEADER *)malloc(sizeof(MIME_HEADER));
806 mhdr = (MIME_HEADER *) malloc(sizeof(MIME_HEADER));
807 if (!mhdr) { 854 if (!mhdr) {
808 OPENSSL_free(tmpname); 855 OPENSSL_free(tmpname);
809 return NULL; 856 return NULL;
810} 857 }
811 mhdr->name = tmpname; 858 mhdr->name = tmpname;
812 mhdr->value = tmpval; 859 mhdr->value = tmpval;
813 if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) { 860 if (!(mhdr->params = sk_MIME_PARAM_new(mime_param_cmp))) {
814 free(mhdr); 861 free(mhdr);
815 return NULL; 862 return NULL;
816} 863 }
817 return mhdr; 864 return mhdr;
818} 865}
819 866
820static int mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value) 867static int
868mime_hdr_addparam(MIME_HEADER *mhdr, char *name, char *value)
821{ 869{
822 char *tmpname, *tmpval, *p; 870 char *tmpname, *tmpval, *p;
823 int c; 871 int c;
824 MIME_PARAM *mparam; 872 MIME_PARAM *mparam;
825 if(name) { 873
874 if (name) {
826 tmpname = BUF_strdup(name); 875 tmpname = BUF_strdup(name);
827 if(!tmpname) return 0; 876 if (!tmpname)
828 for(p = tmpname ; *p; p++) { 877 return 0;
878 for (p = tmpname; *p; p++) {
829 c = (unsigned char)*p; 879 c = (unsigned char)*p;
830 if(isupper(c)) { 880 if (isupper(c)) {
831 c = tolower(c); 881 c = tolower(c);
832 *p = c; 882 *p = c;
883 }
833 } 884 }
834 } 885 } else
835} else tmpname = NULL; 886 tmpname = NULL;
836 if(value) { 887 if (value) {
837 tmpval = BUF_strdup(value); 888 tmpval = BUF_strdup(value);
838 if(!tmpval) return 0; 889 if (!tmpval)
839} else tmpval = NULL; 890 return 0;
891 } else
892 tmpval = NULL;
840 /* Parameter values are case sensitive so leave as is */ 893 /* Parameter values are case sensitive so leave as is */
841 mparam = (MIME_PARAM *) malloc(sizeof(MIME_PARAM)); 894 mparam = (MIME_PARAM *) malloc(sizeof(MIME_PARAM));
842 if(!mparam) return 0; 895 if (!mparam)
896 return 0;
843 mparam->param_name = tmpname; 897 mparam->param_name = tmpname;
844 mparam->param_value = tmpval; 898 mparam->param_value = tmpval;
845 sk_MIME_PARAM_push(mhdr->params, mparam); 899 sk_MIME_PARAM_push(mhdr->params, mparam);
846 return 1; 900 return 1;
847} 901}
848 902
849static int mime_hdr_cmp(const MIME_HEADER * const *a, 903static int
850 const MIME_HEADER * const *b) 904mime_hdr_cmp(const MIME_HEADER * const *a, const MIME_HEADER * const *b)
851{ 905{
852 if (!(*a)->name || !(*b)->name) 906 if (!(*a)->name || !(*b)->name)
853 return !!(*a)->name - !!(*b)->name; 907 return !!(*a)->name - !!(*b)->name;
854 908 return (strcmp((*a)->name, (*b)->name));
855 return(strcmp((*a)->name, (*b)->name));
856} 909}
857 910
858static int mime_param_cmp(const MIME_PARAM * const *a, 911static int
859 const MIME_PARAM * const *b) 912mime_param_cmp(const MIME_PARAM * const *a, const MIME_PARAM * const *b)
860{ 913{
861 if (!(*a)->param_name || !(*b)->param_name) 914 if (!(*a)->param_name || !(*b)->param_name)
862 return !!(*a)->param_name - !!(*b)->param_name; 915 return !!(*a)->param_name - !!(*b)->param_name;
863 return(strcmp((*a)->param_name, (*b)->param_name)); 916 return (strcmp((*a)->param_name, (*b)->param_name));
864} 917}
865 918
866/* Find a header with a given name (if possible) */ 919/* Find a header with a given name (if possible) */
867 920
868static MIME_HEADER *mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name) 921static MIME_HEADER *
922mime_hdr_find(STACK_OF(MIME_HEADER) *hdrs, char *name)
869{ 923{
870 MIME_HEADER htmp; 924 MIME_HEADER htmp;
871 int idx; 925 int idx;
872 htmp.name = name; 926 htmp.name = name;
873 idx = sk_MIME_HEADER_find(hdrs, &htmp); 927 idx = sk_MIME_HEADER_find(hdrs, &htmp);
874 if(idx < 0) return NULL; 928 if (idx < 0)
929 return NULL;
875 return sk_MIME_HEADER_value(hdrs, idx); 930 return sk_MIME_HEADER_value(hdrs, idx);
876} 931}
877 932
878static MIME_PARAM *mime_param_find(MIME_HEADER *hdr, char *name) 933static MIME_PARAM *
934mime_param_find(MIME_HEADER *hdr, char *name)
879{ 935{
880 MIME_PARAM param; 936 MIME_PARAM param;
881 int idx; 937 int idx;
882 param.param_name = name; 938 param.param_name = name;
883 idx = sk_MIME_PARAM_find(hdr->params, &param); 939 idx = sk_MIME_PARAM_find(hdr->params, &param);
884 if(idx < 0) return NULL; 940 if (idx < 0)
941 return NULL;
885 return sk_MIME_PARAM_value(hdr->params, idx); 942 return sk_MIME_PARAM_value(hdr->params, idx);
886} 943}
887 944
888static void mime_hdr_free(MIME_HEADER *hdr) 945static void
946mime_hdr_free(MIME_HEADER *hdr)
889{ 947{
890 if(hdr->name) free(hdr->name); 948 if (hdr->name)
891 if(hdr->value) free(hdr->value); 949 free(hdr->name);
892 if(hdr->params) sk_MIME_PARAM_pop_free(hdr->params, mime_param_free); 950 if (hdr->value)
951 free(hdr->value);
952 if (hdr->params)
953 sk_MIME_PARAM_pop_free(hdr->params, mime_param_free);
893 free(hdr); 954 free(hdr);
894} 955}
895 956
896static void mime_param_free(MIME_PARAM *param) 957static void
958mime_param_free(MIME_PARAM *param)
897{ 959{
898 if(param->param_name) free(param->param_name); 960 if (param->param_name)
899 if(param->param_value) free(param->param_value); 961 free(param->param_name);
962 if (param->param_value)
963 free(param->param_value);
900 free(param); 964 free(param);
901} 965}
902 966
@@ -905,21 +969,28 @@ static void mime_param_free(MIME_PARAM *param)
905 * 1 : part boundary 969 * 1 : part boundary
906 * 2 : final boundary 970 * 2 : final boundary
907 */ 971 */
908static int mime_bound_check(char *line, int linelen, char *bound, int blen) 972static int
973mime_bound_check(char *line, int linelen, char *bound, int blen)
909{ 974{
910 if(linelen == -1) linelen = strlen(line); 975 if (linelen == -1)
911 if(blen == -1) blen = strlen(bound); 976 linelen = strlen(line);
977 if (blen == -1)
978 blen = strlen(bound);
912 /* Quickly eliminate if line length too short */ 979 /* Quickly eliminate if line length too short */
913 if(blen + 2 > linelen) return 0; 980 if (blen + 2 > linelen)
981 return 0;
914 /* Check for part boundary */ 982 /* Check for part boundary */
915 if(!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) { 983 if (!strncmp(line, "--", 2) && !strncmp(line + 2, bound, blen)) {
916 if(!strncmp(line + blen + 2, "--", 2)) return 2; 984 if (!strncmp(line + blen + 2, "--", 2))
917 else return 1; 985 return 2;
918} 986 else
987 return 1;
988 }
919 return 0; 989 return 0;
920} 990}
921 991
922static int strip_eol(char *linebuf, int *plen) 992static int
993strip_eol(char *linebuf, int *plen)
923{ 994{
924 int len = *plen; 995 int len = *plen;
925 char *p, c; 996 char *p, c;
diff --git a/src/lib/libssl/src/crypto/asn1/asn_moid.c b/src/lib/libssl/src/crypto/asn1/asn_moid.c
index 0c09a8e934..7ddf212e9a 100644
--- a/src/lib/libssl/src/crypto/asn1/asn_moid.c
+++ b/src/lib/libssl/src/crypto/asn1/asn_moid.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
@@ -68,20 +68,22 @@
68 68
69static int do_create(char *value, char *name); 69static int do_create(char *value, char *name);
70 70
71static int oid_module_init(CONF_IMODULE *md, const CONF *cnf) 71static int
72oid_module_init(CONF_IMODULE *md, const CONF *cnf)
72{ 73{
73 int i; 74 int i;
74 const char *oid_section; 75 const char *oid_section;
75 STACK_OF(CONF_VALUE) *sktmp; 76 STACK_OF(CONF_VALUE) *sktmp;
76 CONF_VALUE *oval; 77 CONF_VALUE *oval;
78
77 oid_section = CONF_imodule_get_value(md); 79 oid_section = CONF_imodule_get_value(md);
78 if(!(sktmp = NCONF_get_section(cnf, oid_section))) { 80 if (!(sktmp = NCONF_get_section(cnf, oid_section))) {
79 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION); 81 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ERROR_LOADING_SECTION);
80 return 0; 82 return 0;
81 } 83 }
82 for(i = 0; i < sk_CONF_VALUE_num(sktmp); i++) { 84 for (i = 0; i < sk_CONF_VALUE_num(sktmp); i++) {
83 oval = sk_CONF_VALUE_value(sktmp, i); 85 oval = sk_CONF_VALUE_value(sktmp, i);
84 if(!do_create(oval->value, oval->name)) { 86 if (!do_create(oval->value, oval->name)) {
85 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT); 87 ASN1err(ASN1_F_OID_MODULE_INIT, ASN1_R_ADDING_OBJECT);
86 return 0; 88 return 0;
87 } 89 }
@@ -89,12 +91,14 @@ static int oid_module_init(CONF_IMODULE *md, const CONF *cnf)
89 return 1; 91 return 1;
90} 92}
91 93
92static void oid_module_finish(CONF_IMODULE *md) 94static void
95oid_module_finish(CONF_IMODULE *md)
93{ 96{
94 OBJ_cleanup(); 97 OBJ_cleanup();
95} 98}
96 99
97void ASN1_add_oid_module(void) 100void
101ASN1_add_oid_module(void)
98{ 102{
99 CONF_module_add("oid_section", oid_module_init, oid_module_finish); 103 CONF_module_add("oid_section", oid_module_init, oid_module_finish);
100} 104}
@@ -104,12 +108,13 @@ void ASN1_add_oid_module(void)
104 * shortname = some long name, 1.2.3.4 108 * shortname = some long name, 1.2.3.4
105 */ 109 */
106 110
107 111static int
108static int do_create(char *value, char *name) 112do_create(char *value, char *name)
109{ 113{
110 int nid; 114 int nid;
111 ASN1_OBJECT *oid; 115 ASN1_OBJECT *oid;
112 char *ln, *ostr, *p, *lntmp; 116 char *ln, *ostr, *p, *lntmp;
117
113 p = strrchr(value, ','); 118 p = strrchr(value, ',');
114 if (!p) { 119 if (!p) {
115 ln = name; 120 ln = name;
@@ -119,7 +124,8 @@ static int do_create(char *value, char *name)
119 ostr = p + 1; 124 ostr = p + 1;
120 if (!*ostr) 125 if (!*ostr)
121 return 0; 126 return 0;
122 while(isspace((unsigned char)*ostr)) ostr++; 127 while (isspace((unsigned char)*ostr))
128 ostr++;
123 } 129 }
124 130
125 nid = OBJ_create(ostr, name, ln); 131 nid = OBJ_create(ostr, name, ln);
@@ -129,9 +135,10 @@ static int do_create(char *value, char *name)
129 135
130 if (p) { 136 if (p) {
131 ln = value; 137 ln = value;
132 while(isspace((unsigned char)*ln)) ln++; 138 while (isspace((unsigned char)*ln))
139 ln++;
133 p--; 140 p--;
134 while(isspace((unsigned char)*p)) { 141 while (isspace((unsigned char)*p)) {
135 if (p == ln) 142 if (p == ln)
136 return 0; 143 return 0;
137 p--; 144 p--;
@@ -148,5 +155,3 @@ static int do_create(char *value, char *name)
148 155
149 return 1; 156 return 1;
150} 157}
151
152
diff --git a/src/lib/libssl/src/crypto/asn1/asn_pack.c b/src/lib/libssl/src/crypto/asn1/asn_pack.c
index 13dc5d4665..9752a68206 100644
--- a/src/lib/libssl/src/crypto/asn1/asn_pack.c
+++ b/src/lib/libssl/src/crypto/asn1/asn_pack.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,29 +62,32 @@
62 62
63/* ASN1_ITEM versions of the above */ 63/* ASN1_ITEM versions of the above */
64 64
65ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct) 65ASN1_STRING *
66ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
66{ 67{
67 ASN1_STRING *octmp; 68 ASN1_STRING *octmp;
68 69
69 if (!oct || !*oct) { 70 if (!oct || !*oct) {
70 if (!(octmp = ASN1_STRING_new ())) { 71 if (!(octmp = ASN1_STRING_new ())) {
71 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 72 ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
72 return NULL; 73 return NULL;
73 } 74 }
74 if (oct) *oct = octmp; 75 if (oct)
75 } else octmp = *oct; 76 *oct = octmp;
77 } else
78 octmp = *oct;
76 79
77 if(octmp->data) { 80 if (octmp->data) {
78 free(octmp->data); 81 free(octmp->data);
79 octmp->data = NULL; 82 octmp->data = NULL;
80 } 83 }
81 84
82 if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) { 85 if (!(octmp->length = ASN1_item_i2d(obj, &octmp->data, it))) {
83 ASN1err(ASN1_F_ASN1_ITEM_PACK,ASN1_R_ENCODE_ERROR); 86 ASN1err(ASN1_F_ASN1_ITEM_PACK, ASN1_R_ENCODE_ERROR);
84 return NULL; 87 return NULL;
85 } 88 }
86 if (!octmp->data) { 89 if (!octmp->data) {
87 ASN1err(ASN1_F_ASN1_ITEM_PACK,ERR_R_MALLOC_FAILURE); 90 ASN1err(ASN1_F_ASN1_ITEM_PACK, ERR_R_MALLOC_FAILURE);
88 return NULL; 91 return NULL;
89 } 92 }
90 return octmp; 93 return octmp;
@@ -92,13 +95,14 @@ ASN1_STRING *ASN1_item_pack(void *obj, const ASN1_ITEM *it, ASN1_STRING **oct)
92 95
93/* Extract an ASN1 object from an ASN1_STRING */ 96/* Extract an ASN1 object from an ASN1_STRING */
94 97
95void *ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it) 98void *
99ASN1_item_unpack(ASN1_STRING *oct, const ASN1_ITEM *it)
96{ 100{
97 const unsigned char *p; 101 const unsigned char *p;
98 void *ret; 102 void *ret;
99 103
100 p = oct->data; 104 p = oct->data;
101 if(!(ret = ASN1_item_d2i(NULL, &p, oct->length, it))) 105 if (!(ret = ASN1_item_d2i(NULL, &p, oct->length, it)))
102 ASN1err(ASN1_F_ASN1_ITEM_UNPACK,ASN1_R_DECODE_ERROR); 106 ASN1err(ASN1_F_ASN1_ITEM_UNPACK, ASN1_R_DECODE_ERROR);
103 return ret; 107 return ret;
104} 108}
diff --git a/src/lib/libssl/src/crypto/asn1/bio_asn1.c b/src/lib/libssl/src/crypto/asn1/bio_asn1.c
index 8ebac02a00..36b82758ed 100644
--- a/src/lib/libssl/src/crypto/asn1/bio_asn1.c
+++ b/src/lib/libssl/src/crypto/asn1/bio_asn1.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
@@ -68,8 +68,7 @@
68/* Must be large enough for biggest tag+length */ 68/* Must be large enough for biggest tag+length */
69#define DEFAULT_ASN1_BUF_SIZE 20 69#define DEFAULT_ASN1_BUF_SIZE 20
70 70
71typedef enum 71typedef enum {
72{
73 ASN1_STATE_START, 72 ASN1_STATE_START,
74 ASN1_STATE_PRE_COPY, 73 ASN1_STATE_PRE_COPY,
75 ASN1_STATE_HEADER, 74 ASN1_STATE_HEADER,
@@ -79,14 +78,12 @@ typedef enum
79 ASN1_STATE_DONE 78 ASN1_STATE_DONE
80} asn1_bio_state_t; 79} asn1_bio_state_t;
81 80
82typedef struct BIO_ASN1_EX_FUNCS_st 81typedef struct BIO_ASN1_EX_FUNCS_st {
83{
84 asn1_ps_func *ex_func; 82 asn1_ps_func *ex_func;
85 asn1_ps_func *ex_free_func; 83 asn1_ps_func *ex_free_func;
86} BIO_ASN1_EX_FUNCS; 84} BIO_ASN1_EX_FUNCS;
87 85
88typedef struct BIO_ASN1_BUF_CTX_t 86typedef struct BIO_ASN1_BUF_CTX_t {
89{
90 /* Internal state */ 87 /* Internal state */
91 asn1_bio_state_t state; 88 asn1_bio_state_t state;
92 /* Internal buffer */ 89 /* Internal buffer */
@@ -110,7 +107,7 @@ typedef struct BIO_ASN1_BUF_CTX_t
110} BIO_ASN1_BUF_CTX; 107} BIO_ASN1_BUF_CTX;
111 108
112 109
113static int asn1_bio_write(BIO *h, const char *buf,int num); 110static int asn1_bio_write(BIO *h, const char *buf, int num);
114static int asn1_bio_read(BIO *h, char *buf, int size); 111static int asn1_bio_read(BIO *h, char *buf, int size);
115static int asn1_bio_puts(BIO *h, const char *str); 112static int asn1_bio_puts(BIO *h, const char *str);
116static int asn1_bio_gets(BIO *h, char *str, int size); 113static int asn1_bio_gets(BIO *h, char *str, int size);
@@ -121,14 +118,12 @@ static long asn1_bio_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
121 118
122static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size); 119static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size);
123static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 120static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
124 asn1_ps_func *cleanup, asn1_bio_state_t next); 121 asn1_ps_func *cleanup, asn1_bio_state_t next);
125static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 122static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
126 asn1_ps_func *setup, 123 asn1_ps_func *setup, asn1_bio_state_t ex_state,
127 asn1_bio_state_t ex_state, 124 asn1_bio_state_t other_state);
128 asn1_bio_state_t other_state);
129 125
130static BIO_METHOD methods_asn1= 126static BIO_METHOD methods_asn1 = {
131{
132 BIO_TYPE_ASN1, 127 BIO_TYPE_ASN1,
133 "asn1", 128 "asn1",
134 asn1_bio_write, 129 asn1_bio_write,
@@ -141,13 +136,14 @@ static BIO_METHOD methods_asn1=
141 asn1_bio_callback_ctrl, 136 asn1_bio_callback_ctrl,
142}; 137};
143 138
144BIO_METHOD *BIO_f_asn1(void) 139BIO_METHOD *
140BIO_f_asn1(void)
145{ 141{
146 return(&methods_asn1); 142 return (&methods_asn1);
147} 143}
148 144
149 145static int
150static int asn1_bio_new(BIO *b) 146asn1_bio_new(BIO *b)
151{ 147{
152 BIO_ASN1_BUF_CTX *ctx; 148 BIO_ASN1_BUF_CTX *ctx;
153 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX)); 149 ctx = malloc(sizeof(BIO_ASN1_BUF_CTX));
@@ -156,14 +152,15 @@ static int asn1_bio_new(BIO *b)
156 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) { 152 if (!asn1_bio_init(ctx, DEFAULT_ASN1_BUF_SIZE)) {
157 free(ctx); 153 free(ctx);
158 return 0; 154 return 0;
159} 155 }
160 b->init = 1; 156 b->init = 1;
161 b->ptr = (char *)ctx; 157 b->ptr = (char *)ctx;
162 b->flags = 0; 158 b->flags = 0;
163 return 1; 159 return 1;
164} 160}
165 161
166static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size) 162static int
163asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
167{ 164{
168 ctx->buf = malloc(size); 165 ctx->buf = malloc(size);
169 if (!ctx->buf) 166 if (!ctx->buf)
@@ -181,9 +178,11 @@ static int asn1_bio_init(BIO_ASN1_BUF_CTX *ctx, int size)
181 return 1; 178 return 1;
182} 179}
183 180
184static int asn1_bio_free(BIO *b) 181static int
182asn1_bio_free(BIO *b)
185{ 183{
186 BIO_ASN1_BUF_CTX *ctx; 184 BIO_ASN1_BUF_CTX *ctx;
185
187 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 186 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
188 if (ctx == NULL) 187 if (ctx == NULL)
189 return 0; 188 return 0;
@@ -196,11 +195,13 @@ static int asn1_bio_free(BIO *b)
196 return 1; 195 return 1;
197} 196}
198 197
199static int asn1_bio_write(BIO *b, const char *in , int inl) 198static int
199asn1_bio_write(BIO *b, const char *in , int inl)
200{ 200{
201 BIO_ASN1_BUF_CTX *ctx; 201 BIO_ASN1_BUF_CTX *ctx;
202 int wrmax, wrlen, ret; 202 int wrmax, wrlen, ret;
203 unsigned char *p; 203 unsigned char *p;
204
204 if (!in || (inl < 0) || (b->next_bio == NULL)) 205 if (!in || (inl < 0) || (b->next_bio == NULL))
205 return 0; 206 return 0;
206 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 207 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
@@ -210,42 +211,38 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
210 wrlen = 0; 211 wrlen = 0;
211 ret = -1; 212 ret = -1;
212 213
213 for(;;) { 214 for (;;) {
214 switch (ctx->state) { 215 switch (ctx->state) {
215 216
216 /* Setup prefix data, call it */ 217 /* Setup prefix data, call it */
217 case ASN1_STATE_START: 218 case ASN1_STATE_START:
218 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix, 219 if (!asn1_bio_setup_ex(b, ctx, ctx->prefix,
219 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER)) 220 ASN1_STATE_PRE_COPY, ASN1_STATE_HEADER))
220 return 0; 221 return 0;
221 break; 222 break;
222 223
223 /* Copy any pre data first */ 224 /* Copy any pre data first */
224 case ASN1_STATE_PRE_COPY: 225 case ASN1_STATE_PRE_COPY:
225
226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free, 226 ret = asn1_bio_flush_ex(b, ctx, ctx->prefix_free,
227 ASN1_STATE_HEADER); 227 ASN1_STATE_HEADER);
228
229 if (ret <= 0) 228 if (ret <= 0)
230 goto done; 229 goto done;
231
232 break; 230 break;
233 231
234 case ASN1_STATE_HEADER: 232 case ASN1_STATE_HEADER:
235 ctx->buflen = 233 ctx->buflen =
236 ASN1_object_size(0, inl, ctx->asn1_tag) - inl; 234 ASN1_object_size(0, inl, ctx->asn1_tag) - inl;
237 OPENSSL_assert(ctx->buflen <= ctx->bufsize); 235 OPENSSL_assert(ctx->buflen <= ctx->bufsize);
238 p = ctx->buf; 236 p = ctx->buf;
239 ASN1_put_object(&p, 0, inl, 237 ASN1_put_object(&p, 0, inl,
240 ctx->asn1_tag, ctx->asn1_class); 238 ctx->asn1_tag, ctx->asn1_class);
241 ctx->copylen = inl; 239 ctx->copylen = inl;
242 ctx->state = ASN1_STATE_HEADER_COPY; 240 ctx->state = ASN1_STATE_HEADER_COPY;
243
244 break; 241 break;
245 242
246 case ASN1_STATE_HEADER_COPY: 243 case ASN1_STATE_HEADER_COPY:
247 ret = BIO_write(b->next_bio, 244 ret = BIO_write(b->next_bio,
248 ctx->buf + ctx->bufpos, ctx->buflen); 245 ctx->buf + ctx->bufpos, ctx->buflen);
249 if (ret <= 0) 246 if (ret <= 0)
250 goto done; 247 goto done;
251 248
@@ -256,7 +253,6 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
256 ctx->bufpos = 0; 253 ctx->bufpos = 0;
257 ctx->state = ASN1_STATE_DATA_COPY; 254 ctx->state = ASN1_STATE_DATA_COPY;
258 } 255 }
259
260 break; 256 break;
261 257
262 case ASN1_STATE_DATA_COPY: 258 case ASN1_STATE_DATA_COPY:
@@ -275,46 +271,44 @@ static int asn1_bio_write(BIO *b, const char *in , int inl)
275 271
276 if (ctx->copylen == 0) 272 if (ctx->copylen == 0)
277 ctx->state = ASN1_STATE_HEADER; 273 ctx->state = ASN1_STATE_HEADER;
278
279 if (inl == 0) 274 if (inl == 0)
280 goto done; 275 goto done;
281
282 break; 276 break;
283 277
284 default: 278 default:
285 BIO_clear_retry_flags(b); 279 BIO_clear_retry_flags(b);
286 return 0; 280 return 0;
287
288 } 281 }
289 282
290 } 283 }
291 284
292 done: 285done:
293 BIO_clear_retry_flags(b); 286 BIO_clear_retry_flags(b);
294 BIO_copy_next_retry(b); 287 BIO_copy_next_retry(b);
295 288
296 return (wrlen > 0) ? wrlen : ret; 289 return (wrlen > 0) ? wrlen : ret;
297
298} 290}
299 291
300static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 292static int
301 asn1_ps_func *cleanup, asn1_bio_state_t next) 293asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *cleanup,
294 asn1_bio_state_t next)
302{ 295{
303 int ret; 296 int ret;
297
304 if (ctx->ex_len <= 0) 298 if (ctx->ex_len <= 0)
305 return 1; 299 return 1;
306 for(;;) { 300 for (;;) {
307 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos, 301 ret = BIO_write(b->next_bio, ctx->ex_buf + ctx->ex_pos,
308 ctx->ex_len); 302 ctx->ex_len);
309 if (ret <= 0) 303 if (ret <= 0)
310 break; 304 break;
311 ctx->ex_len -= ret; 305 ctx->ex_len -= ret;
312 if (ctx->ex_len > 0) 306 if (ctx->ex_len > 0)
313 ctx->ex_pos += ret; 307 ctx->ex_pos += ret;
314 else { 308 else {
315 if(cleanup) 309 if (cleanup)
316 cleanup(b, &ctx->ex_buf, &ctx->ex_len, 310 cleanup(b, &ctx->ex_buf, &ctx->ex_len,
317 &ctx->ex_arg); 311 &ctx->ex_arg);
318 ctx->state = next; 312 ctx->state = next;
319 ctx->ex_pos = 0; 313 ctx->ex_pos = 0;
320 break; 314 break;
@@ -323,10 +317,9 @@ static int asn1_bio_flush_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
323 return ret; 317 return ret;
324} 318}
325 319
326static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, 320static int
327 asn1_ps_func *setup, 321asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx, asn1_ps_func *setup,
328 asn1_bio_state_t ex_state, 322 asn1_bio_state_t ex_state, asn1_bio_state_t other_state)
329 asn1_bio_state_t other_state)
330{ 323{
331 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) { 324 if (setup && !setup(b, &ctx->ex_buf, &ctx->ex_len, &ctx->ex_arg)) {
332 BIO_clear_retry_flags(b); 325 BIO_clear_retry_flags(b);
@@ -339,45 +332,52 @@ static int asn1_bio_setup_ex(BIO *b, BIO_ASN1_BUF_CTX *ctx,
339 return 1; 332 return 1;
340} 333}
341 334
342static int asn1_bio_read(BIO *b, char *in , int inl) 335static int
336asn1_bio_read(BIO *b, char *in , int inl)
343{ 337{
344 if (!b->next_bio) 338 if (!b->next_bio)
345 return 0; 339 return 0;
346 return BIO_read(b->next_bio, in , inl); 340 return BIO_read(b->next_bio, in , inl);
347} 341}
348 342
349static int asn1_bio_puts(BIO *b, const char *str) 343static int
344asn1_bio_puts(BIO *b, const char *str)
350{ 345{
351 return asn1_bio_write(b, str, strlen(str)); 346 return asn1_bio_write(b, str, strlen(str));
352} 347}
353 348
354static int asn1_bio_gets(BIO *b, char *str, int size) 349static int
350asn1_bio_gets(BIO *b, char *str, int size)
355{ 351{
356 if (!b->next_bio) 352 if (!b->next_bio)
357 return 0; 353 return 0;
358 return BIO_gets(b->next_bio, str , size); 354 return BIO_gets(b->next_bio, str , size);
359} 355}
360 356
361static long asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp) 357static long
358asn1_bio_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
362{ 359{
363 if (b->next_bio == NULL) return(0); 360 if (b->next_bio == NULL)
364 return BIO_callback_ctrl(b->next_bio,cmd,fp); 361 return (0);
362 return BIO_callback_ctrl(b->next_bio, cmd, fp);
365} 363}
366 364
367static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2) 365static long
366asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
368{ 367{
369 BIO_ASN1_BUF_CTX *ctx; 368 BIO_ASN1_BUF_CTX *ctx;
370 BIO_ASN1_EX_FUNCS *ex_func; 369 BIO_ASN1_EX_FUNCS *ex_func;
371 long ret = 1; 370 long ret = 1;
371
372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr; 372 ctx = (BIO_ASN1_BUF_CTX *) b->ptr;
373 if (ctx == NULL) 373 if (ctx == NULL)
374 return 0; 374 return 0;
375 switch(cmd) { 375 switch (cmd) {
376 376
377 case BIO_C_SET_PREFIX: 377 case BIO_C_SET_PREFIX:
378 ex_func = arg2; 378 ex_func = arg2;
379 ctx->prefix = ex_func->ex_func; 379 ctx->prefix = ex_func->ex_func;
380 ctx->prefix_free = ex_func->ex_free_func; 380 ctx->prefix_free = ex_func->ex_free_func;
381 break; 381 break;
382 382
383 case BIO_C_GET_PREFIX: 383 case BIO_C_GET_PREFIX:
@@ -388,8 +388,8 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
388 388
389 case BIO_C_SET_SUFFIX: 389 case BIO_C_SET_SUFFIX:
390 ex_func = arg2; 390 ex_func = arg2;
391 ctx->suffix = ex_func->ex_func; 391 ctx->suffix = ex_func->ex_func;
392 ctx->suffix_free = ex_func->ex_free_func; 392 ctx->suffix_free = ex_func->ex_free_func;
393 break; 393 break;
394 394
395 case BIO_C_GET_SUFFIX: 395 case BIO_C_GET_SUFFIX:
@@ -413,13 +413,13 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
413 /* Call post function if possible */ 413 /* Call post function if possible */
414 if (ctx->state == ASN1_STATE_HEADER) { 414 if (ctx->state == ASN1_STATE_HEADER) {
415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix, 415 if (!asn1_bio_setup_ex(b, ctx, ctx->suffix,
416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE)) 416 ASN1_STATE_POST_COPY, ASN1_STATE_DONE))
417 return 0; 417 return 0;
418 } 418 }
419 419
420 if (ctx->state == ASN1_STATE_POST_COPY) { 420 if (ctx->state == ASN1_STATE_POST_COPY) {
421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free, 421 ret = asn1_bio_flush_ex(b, ctx, ctx->suffix_free,
422 ASN1_STATE_DONE); 422 ASN1_STATE_DONE);
423 if (ret <= 0) 423 if (ret <= 0)
424 return ret; 424 return ret;
425 } 425 }
@@ -443,20 +443,24 @@ static long asn1_bio_ctrl(BIO *b, int cmd, long arg1, void *arg2)
443 return ret; 443 return ret;
444} 444}
445 445
446static int asn1_bio_set_ex(BIO *b, int cmd, 446static int
447 asn1_ps_func *ex_func, asn1_ps_func *ex_free_func) 447asn1_bio_set_ex(BIO *b, int cmd, asn1_ps_func *ex_func, asn1_ps_func
448 *ex_free_func)
448{ 449{
449 BIO_ASN1_EX_FUNCS extmp; 450 BIO_ASN1_EX_FUNCS extmp;
451
450 extmp.ex_func = ex_func; 452 extmp.ex_func = ex_func;
451 extmp.ex_free_func = ex_free_func; 453 extmp.ex_free_func = ex_free_func;
452 return BIO_ctrl(b, cmd, 0, &extmp); 454 return BIO_ctrl(b, cmd, 0, &extmp);
453} 455}
454 456
455static int asn1_bio_get_ex(BIO *b, int cmd, 457static int
456 asn1_ps_func **ex_func, asn1_ps_func **ex_free_func) 458asn1_bio_get_ex(BIO *b, int cmd, asn1_ps_func **ex_func,
459 asn1_ps_func **ex_free_func)
457{ 460{
458 BIO_ASN1_EX_FUNCS extmp; 461 BIO_ASN1_EX_FUNCS extmp;
459 int ret; 462 int ret;
463
460 ret = BIO_ctrl(b, cmd, 0, &extmp); 464 ret = BIO_ctrl(b, cmd, 0, &extmp);
461 if (ret > 0) { 465 if (ret > 0) {
462 *ex_func = extmp.ex_func; 466 *ex_func = extmp.ex_func;
@@ -465,22 +469,26 @@ static int asn1_bio_get_ex(BIO *b, int cmd,
465 return ret; 469 return ret;
466} 470}
467 471
468int BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free) 472int
473BIO_asn1_set_prefix(BIO *b, asn1_ps_func *prefix, asn1_ps_func *prefix_free)
469{ 474{
470 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free); 475 return asn1_bio_set_ex(b, BIO_C_SET_PREFIX, prefix, prefix_free);
471} 476}
472 477
473int BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free) 478int
479BIO_asn1_get_prefix(BIO *b, asn1_ps_func **pprefix, asn1_ps_func **pprefix_free)
474{ 480{
475 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free); 481 return asn1_bio_get_ex(b, BIO_C_GET_PREFIX, pprefix, pprefix_free);
476} 482}
477 483
478int BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free) 484int
485BIO_asn1_set_suffix(BIO *b, asn1_ps_func *suffix, asn1_ps_func *suffix_free)
479{ 486{
480 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free); 487 return asn1_bio_set_ex(b, BIO_C_SET_SUFFIX, suffix, suffix_free);
481} 488}
482 489
483int BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free) 490int
491BIO_asn1_get_suffix(BIO *b, asn1_ps_func **psuffix, asn1_ps_func **psuffix_free)
484{ 492{
485 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free); 493 return asn1_bio_get_ex(b, BIO_C_GET_SUFFIX, psuffix, psuffix_free);
486} 494}
diff --git a/src/lib/libssl/src/crypto/asn1/bio_ndef.c b/src/lib/libssl/src/crypto/asn1/bio_ndef.c
index 595e6471c2..f00d38e5e4 100644
--- a/src/lib/libssl/src/crypto/asn1/bio_ndef.c
+++ b/src/lib/libssl/src/crypto/asn1/bio_ndef.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
@@ -78,8 +78,7 @@
78 78
79/* BIO support data stored in the ASN1 BIO ex_arg */ 79/* BIO support data stored in the ASN1 BIO ex_arg */
80 80
81typedef struct ndef_aux_st 81typedef struct ndef_aux_st {
82{
83 /* ASN1 structure this BIO refers to */ 82 /* ASN1 structure this BIO refers to */
84 ASN1_VALUE *val; 83 ASN1_VALUE *val;
85 const ASN1_ITEM *it; 84 const ASN1_ITEM *it;
@@ -98,7 +97,8 @@ static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
98static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg); 97static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg);
99static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg); 98static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg);
100 99
101BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it) 100BIO *
101BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
102{ 102{
103 NDEF_SUPPORT *ndef_aux = NULL; 103 NDEF_SUPPORT *ndef_aux = NULL;
104 BIO *asn_bio = NULL; 104 BIO *asn_bio = NULL;
@@ -143,7 +143,7 @@ BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
143 143
144 return sarg.ndef_bio; 144 return sarg.ndef_bio;
145 145
146 err: 146err:
147 if (asn_bio) 147 if (asn_bio)
148 BIO_free(asn_bio); 148 BIO_free(asn_bio);
149 if (ndef_aux) 149 if (ndef_aux)
@@ -151,7 +151,8 @@ BIO *BIO_new_NDEF(BIO *out, ASN1_VALUE *val, const ASN1_ITEM *it)
151 return NULL; 151 return NULL;
152} 152}
153 153
154static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 154static int
155ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
155{ 156{
156 NDEF_SUPPORT *ndef_aux; 157 NDEF_SUPPORT *ndef_aux;
157 unsigned char *p; 158 unsigned char *p;
@@ -176,7 +177,8 @@ static int ndef_prefix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
176 return 1; 177 return 1;
177} 178}
178 179
179static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 180static int
181ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
180{ 182{
181 NDEF_SUPPORT *ndef_aux; 183 NDEF_SUPPORT *ndef_aux;
182 184
@@ -194,7 +196,8 @@ static int ndef_prefix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
194 return 1; 196 return 1;
195} 197}
196 198
197static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg) 199static int
200ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
198{ 201{
199 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg; 202 NDEF_SUPPORT **pndef_aux = (NDEF_SUPPORT **)parg;
200 if (!ndef_prefix_free(b, pbuf, plen, parg)) 203 if (!ndef_prefix_free(b, pbuf, plen, parg))
@@ -204,7 +207,8 @@ static int ndef_suffix_free(BIO *b, unsigned char **pbuf, int *plen, void *parg)
204 return 1; 207 return 1;
205} 208}
206 209
207static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg) 210static int
211ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
208{ 212{
209 NDEF_SUPPORT *ndef_aux; 213 NDEF_SUPPORT *ndef_aux;
210 unsigned char *p; 214 unsigned char *p;
@@ -224,7 +228,7 @@ static int ndef_suffix(BIO *b, unsigned char **pbuf, int *plen, void *parg)
224 sarg.out = ndef_aux->out; 228 sarg.out = ndef_aux->out;
225 sarg.boundary = ndef_aux->boundary; 229 sarg.boundary = ndef_aux->boundary;
226 if (aux->asn1_cb(ASN1_OP_STREAM_POST, 230 if (aux->asn1_cb(ASN1_OP_STREAM_POST,
227 &ndef_aux->val, ndef_aux->it, &sarg) <= 0) 231 &ndef_aux->val, ndef_aux->it, &sarg) <= 0)
228 return 0; 232 return 0;
229 233
230 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it); 234 derlen = ASN1_item_ndef_i2d(ndef_aux->val, NULL, ndef_aux->it);
diff --git a/src/lib/libssl/src/crypto/asn1/charmap.h b/src/lib/libssl/src/crypto/asn1/charmap.h
index b55e638725..acab65b77f 100644
--- a/src/lib/libssl/src/crypto/asn1/charmap.h
+++ b/src/lib/libssl/src/crypto/asn1/charmap.h
@@ -3,13 +3,12 @@
3 */ 3 */
4 4
5static const unsigned char char_type[] = { 5static const unsigned char char_type[] = {
6 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 6 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
7 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 7 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
8120, 0, 1,40, 0, 0, 0,16,16,16, 0,25,25,16,16,16, 8 120, 0, 1, 40, 0, 0, 0, 16, 16, 16, 0, 25, 25, 16, 16, 16,
916,16,16,16,16,16,16,16,16,16,16, 9, 9,16, 9,16, 9 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 9, 9, 16, 9, 16,
10 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, 10 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1116,16,16,16,16,16,16,16,16,16,16, 0, 1, 0, 0, 0, 11 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 1, 0, 0, 0,
12 0,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16, 12 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
1316,16,16,16,16,16,16,16,16,16,16, 0, 0, 0, 0, 2 13 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 0, 0, 0, 0, 2
14}; 14};
15