summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/x509v3/v3_utl.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/lib/libcrypto/x509v3/v3_utl.c')
-rw-r--r--src/lib/libcrypto/x509v3/v3_utl.c134
1 files changed, 116 insertions, 18 deletions
diff --git a/src/lib/libcrypto/x509v3/v3_utl.c b/src/lib/libcrypto/x509v3/v3_utl.c
index 4c2c4a9483..619f161b58 100644
--- a/src/lib/libcrypto/x509v3/v3_utl.c
+++ b/src/lib/libcrypto/x509v3/v3_utl.c
@@ -65,6 +65,10 @@
65#include <openssl/x509v3.h> 65#include <openssl/x509v3.h>
66 66
67static char *strip_spaces(char *name); 67static char *strip_spaces(char *name);
68static int sk_strcmp(const char * const *a, const char * const *b);
69static STACK *get_email(X509_NAME *name, STACK_OF(GENERAL_NAME) *gens);
70static void str_free(void *str);
71static int append_ia5(STACK **sk, ASN1_IA5STRING *email);
68 72
69/* Add a CONF_VALUE name value pair to stack */ 73/* Add a CONF_VALUE name value pair to stack */
70 74
@@ -75,8 +79,8 @@ int X509V3_add_value(const char *name, const char *value,
75 char *tname = NULL, *tvalue = NULL; 79 char *tname = NULL, *tvalue = NULL;
76 if(name && !(tname = BUF_strdup(name))) goto err; 80 if(name && !(tname = BUF_strdup(name))) goto err;
77 if(value && !(tvalue = BUF_strdup(value))) goto err;; 81 if(value && !(tvalue = BUF_strdup(value))) goto err;;
78 if(!(vtmp = (CONF_VALUE *)Malloc(sizeof(CONF_VALUE)))) goto err; 82 if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
79 if(!*extlist && !(*extlist = sk_CONF_VALUE_new(NULL))) goto err; 83 if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
80 vtmp->section = NULL; 84 vtmp->section = NULL;
81 vtmp->name = tname; 85 vtmp->name = tname;
82 vtmp->value = tvalue; 86 vtmp->value = tvalue;
@@ -84,9 +88,9 @@ int X509V3_add_value(const char *name, const char *value,
84 return 1; 88 return 1;
85 err: 89 err:
86 X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE); 90 X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
87 if(vtmp) Free(vtmp); 91 if(vtmp) OPENSSL_free(vtmp);
88 if(tname) Free(tname); 92 if(tname) OPENSSL_free(tname);
89 if(tvalue) Free(tvalue); 93 if(tvalue) OPENSSL_free(tvalue);
90 return 0; 94 return 0;
91} 95}
92 96
@@ -101,10 +105,10 @@ int X509V3_add_value_uchar(const char *name, const unsigned char *value,
101void X509V3_conf_free(CONF_VALUE *conf) 105void X509V3_conf_free(CONF_VALUE *conf)
102{ 106{
103 if(!conf) return; 107 if(!conf) return;
104 if(conf->name) Free(conf->name); 108 if(conf->name) OPENSSL_free(conf->name);
105 if(conf->value) Free(conf->value); 109 if(conf->value) OPENSSL_free(conf->value);
106 if(conf->section) Free(conf->section); 110 if(conf->section) OPENSSL_free(conf->section);
107 Free(conf); 111 OPENSSL_free(conf);
108} 112}
109 113
110int X509V3_add_value_bool(const char *name, int asn1_bool, 114int X509V3_add_value_bool(const char *name, int asn1_bool,
@@ -176,7 +180,7 @@ int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
176 if(!aint) return 1; 180 if(!aint) return 1;
177 if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0; 181 if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
178 ret = X509V3_add_value(name, strtmp, extlist); 182 ret = X509V3_add_value(name, strtmp, extlist);
179 Free(strtmp); 183 OPENSSL_free(strtmp);
180 return ret; 184 return ret;
181} 185}
182 186
@@ -298,11 +302,11 @@ STACK_OF(CONF_VALUE) *X509V3_parse_list(char *line)
298 } 302 }
299 X509V3_add_value(ntmp, NULL, &values); 303 X509V3_add_value(ntmp, NULL, &values);
300 } 304 }
301Free(linebuf); 305OPENSSL_free(linebuf);
302return values; 306return values;
303 307
304err: 308err:
305Free(linebuf); 309OPENSSL_free(linebuf);
306sk_CONF_VALUE_pop_free(values, X509V3_conf_free); 310sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
307return NULL; 311return NULL;
308 312
@@ -325,8 +329,9 @@ static char *strip_spaces(char *name)
325 329
326/* hex string utilities */ 330/* hex string utilities */
327 331
328/* Given a buffer of length 'len' return a Malloc'ed string with its 332/* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
329 * hex representation 333 * hex representation
334 * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
330 */ 335 */
331 336
332char *hex_to_string(unsigned char *buffer, long len) 337char *hex_to_string(unsigned char *buffer, long len)
@@ -336,7 +341,7 @@ char *hex_to_string(unsigned char *buffer, long len)
336 int i; 341 int i;
337 static char hexdig[] = "0123456789ABCDEF"; 342 static char hexdig[] = "0123456789ABCDEF";
338 if(!buffer || !len) return NULL; 343 if(!buffer || !len) return NULL;
339 if(!(tmp = Malloc(len * 3 + 1))) { 344 if(!(tmp = OPENSSL_malloc(len * 3 + 1))) {
340 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE); 345 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
341 return NULL; 346 return NULL;
342 } 347 }
@@ -347,6 +352,10 @@ char *hex_to_string(unsigned char *buffer, long len)
347 *q++ = ':'; 352 *q++ = ':';
348 } 353 }
349 q[-1] = 0; 354 q[-1] = 0;
355#ifdef CHARSET_EBCDIC
356 ebcdic2ascii(tmp, tmp, q - tmp - 1);
357#endif
358
350 return tmp; 359 return tmp;
351} 360}
352 361
@@ -362,14 +371,20 @@ unsigned char *string_to_hex(char *str, long *len)
362 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT); 371 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
363 return NULL; 372 return NULL;
364 } 373 }
365 if(!(hexbuf = Malloc(strlen(str) >> 1))) goto err; 374 if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err;
366 for(p = (unsigned char *)str, q = hexbuf; *p;) { 375 for(p = (unsigned char *)str, q = hexbuf; *p;) {
367 ch = *p++; 376 ch = *p++;
377#ifdef CHARSET_EBCDIC
378 ch = os_toebcdic[ch];
379#endif
368 if(ch == ':') continue; 380 if(ch == ':') continue;
369 cl = *p++; 381 cl = *p++;
382#ifdef CHARSET_EBCDIC
383 cl = os_toebcdic[cl];
384#endif
370 if(!cl) { 385 if(!cl) {
371 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS); 386 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
372 Free(hexbuf); 387 OPENSSL_free(hexbuf);
373 return NULL; 388 return NULL;
374 } 389 }
375 if(isupper(ch)) ch = tolower(ch); 390 if(isupper(ch)) ch = tolower(ch);
@@ -391,12 +406,12 @@ unsigned char *string_to_hex(char *str, long *len)
391 return hexbuf; 406 return hexbuf;
392 407
393 err: 408 err:
394 if(hexbuf) Free(hexbuf); 409 if(hexbuf) OPENSSL_free(hexbuf);
395 X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE); 410 X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
396 return NULL; 411 return NULL;
397 412
398 badhex: 413 badhex:
399 Free(hexbuf); 414 OPENSSL_free(hexbuf);
400 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT); 415 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
401 return NULL; 416 return NULL;
402 417
@@ -416,3 +431,86 @@ int name_cmp(const char *name, const char *cmp)
416 if(!c || (c=='.')) return 0; 431 if(!c || (c=='.')) return 0;
417 return 1; 432 return 1;
418} 433}
434
435static int sk_strcmp(const char * const *a, const char * const *b)
436{
437 return strcmp(*a, *b);
438}
439
440STACK *X509_get1_email(X509 *x)
441{
442 STACK_OF(GENERAL_NAME) *gens;
443 STACK *ret;
444 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
445 ret = get_email(X509_get_subject_name(x), gens);
446 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
447 return ret;
448}
449
450STACK *X509_REQ_get1_email(X509_REQ *x)
451{
452 STACK_OF(GENERAL_NAME) *gens;
453 STACK_OF(X509_EXTENSION) *exts;
454 STACK *ret;
455 exts = X509_REQ_get_extensions(x);
456 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
457 ret = get_email(X509_REQ_get_subject_name(x), gens);
458 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
459 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
460 return ret;
461}
462
463
464static STACK *get_email(X509_NAME *name, STACK_OF(GENERAL_NAME) *gens)
465{
466 STACK *ret = NULL;
467 X509_NAME_ENTRY *ne;
468 ASN1_IA5STRING *email;
469 GENERAL_NAME *gen;
470 int i;
471 /* Now add any email address(es) to STACK */
472 i = -1;
473 /* First supplied X509_NAME */
474 while((i = X509_NAME_get_index_by_NID(name,
475 NID_pkcs9_emailAddress, i)) > 0) {
476 ne = X509_NAME_get_entry(name, i);
477 email = X509_NAME_ENTRY_get_data(ne);
478 if(!append_ia5(&ret, email)) return NULL;
479 }
480 for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
481 {
482 gen = sk_GENERAL_NAME_value(gens, i);
483 if(gen->type != GEN_EMAIL) continue;
484 if(!append_ia5(&ret, gen->d.ia5)) return NULL;
485 }
486 return ret;
487}
488
489static void str_free(void *str)
490{
491 OPENSSL_free(str);
492}
493
494static int append_ia5(STACK **sk, ASN1_IA5STRING *email)
495{
496 char *emtmp;
497 /* First some sanity checks */
498 if(email->type != V_ASN1_IA5STRING) return 1;
499 if(!email->data || !email->length) return 1;
500 if(!*sk) *sk = sk_new(sk_strcmp);
501 if(!*sk) return 0;
502 /* Don't add duplicates */
503 if(sk_find(*sk, (char *)email->data) != -1) return 1;
504 emtmp = BUF_strdup((char *)email->data);
505 if(!emtmp || !sk_push(*sk, emtmp)) {
506 X509_email_free(*sk);
507 *sk = NULL;
508 return 0;
509 }
510 return 1;
511}
512
513void X509_email_free(STACK *sk)
514{
515 sk_pop_free(sk, str_free);
516}