2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
58 /* X509 v3 extension utilities */
64 #include <openssl/conf.h>
65 #include <openssl/x509v3.h>
67 static char *strip_spaces(char *name);
68 static int sk_strcmp(const char * const *a, const char * const *b);
69 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens);
70 static void str_free(void *str);
71 static int append_ia5(STACK **sk, ASN1_IA5STRING *email);
73 /* Add a CONF_VALUE name value pair to stack */
75 int X509V3_add_value(const char *name, const char *value,
76 STACK_OF(CONF_VALUE) **extlist)
78 CONF_VALUE *vtmp = NULL;
79 char *tname = NULL, *tvalue = NULL;
80 if(name && !(tname = BUF_strdup(name))) goto err;
81 if(value && !(tvalue = BUF_strdup(value))) goto err;;
82 if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
83 if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
87 if(!sk_CONF_VALUE_push(*extlist, vtmp)) goto err;
90 X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
91 if(vtmp) OPENSSL_free(vtmp);
92 if(tname) OPENSSL_free(tname);
93 if(tvalue) OPENSSL_free(tvalue);
97 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
98 STACK_OF(CONF_VALUE) **extlist)
100 return X509V3_add_value(name,(const char *)value,extlist);
103 /* Free function for STACK_OF(CONF_VALUE) */
105 void X509V3_conf_free(CONF_VALUE *conf)
108 if(conf->name) OPENSSL_free(conf->name);
109 if(conf->value) OPENSSL_free(conf->value);
110 if(conf->section) OPENSSL_free(conf->section);
114 int X509V3_add_value_bool(const char *name, int asn1_bool,
115 STACK_OF(CONF_VALUE) **extlist)
117 if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
118 return X509V3_add_value(name, "FALSE", extlist);
121 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
122 STACK_OF(CONF_VALUE) **extlist)
124 if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
129 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
131 BIGNUM *bntmp = NULL;
134 if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
135 !(strtmp = BN_bn2dec(bntmp)) )
136 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
141 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
143 BIGNUM *bntmp = NULL;
146 if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
147 !(strtmp = BN_bn2dec(bntmp)) )
148 X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
153 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
160 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE);
164 if (value[0] == '-') {
169 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
174 if (ishex) ret = BN_hex2bn(&bn, value);
175 else ret = BN_dec2bn(&bn, value);
177 if (!ret || value[ret]) {
179 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR);
183 if (isneg && BN_is_zero(bn)) isneg = 0;
185 aint = BN_to_ASN1_INTEGER(bn, NULL);
188 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
191 if (isneg) aint->type |= V_ASN1_NEG;
195 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
196 STACK_OF(CONF_VALUE) **extlist)
201 if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
202 ret = X509V3_add_value(name, strtmp, extlist);
203 OPENSSL_free(strtmp);
207 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
210 if(!(btmp = value->value)) goto err;
211 if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
212 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
213 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
216 } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
217 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
218 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
223 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
224 X509V3_conf_err(value);
228 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
231 if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
232 X509V3_conf_err(value);
244 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
248 STACK_OF(CONF_VALUE) *values = NULL;
251 /* We are going to modify the line so copy it first */
252 linebuf = BUF_strdup(line);
255 /* Go through all characters */
256 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
263 ntmp = strip_spaces(q);
265 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
269 } else if(c == ',') {
271 ntmp = strip_spaces(q);
274 printf("%s\n", ntmp);
277 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
280 X509V3_add_value(ntmp, NULL, &values);
288 vtmp = strip_spaces(q);
290 printf("%s\n", ntmp);
293 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
296 X509V3_add_value(ntmp, vtmp, &values);
304 if(state == HDR_VALUE) {
305 vtmp = strip_spaces(q);
307 printf("%s=%s\n", ntmp, vtmp);
310 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
313 X509V3_add_value(ntmp, vtmp, &values);
315 ntmp = strip_spaces(q);
317 printf("%s\n", ntmp);
320 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
323 X509V3_add_value(ntmp, NULL, &values);
325 OPENSSL_free(linebuf);
329 OPENSSL_free(linebuf);
330 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
335 /* Delete leading and trailing spaces from a string */
336 static char *strip_spaces(char *name)
339 /* Skip over leading spaces */
341 while(*p && isspace((unsigned char)*p)) p++;
343 q = p + strlen(p) - 1;
344 while((q != p) && isspace((unsigned char)*q)) q--;
350 /* hex string utilities */
352 /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
354 * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
357 char *hex_to_string(unsigned char *buffer, long len)
362 static char hexdig[] = "0123456789ABCDEF";
363 if(!buffer || !len) return NULL;
364 if(!(tmp = OPENSSL_malloc(len * 3 + 1))) {
365 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
369 for(i = 0, p = buffer; i < len; i++,p++) {
370 *q++ = hexdig[(*p >> 4) & 0xf];
371 *q++ = hexdig[*p & 0xf];
375 #ifdef CHARSET_EBCDIC
376 ebcdic2ascii(tmp, tmp, q - tmp - 1);
382 /* Give a string of hex digits convert to
386 unsigned char *string_to_hex(char *str, long *len)
388 unsigned char *hexbuf, *q;
389 unsigned char ch, cl, *p;
391 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
394 if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err;
395 for(p = (unsigned char *)str, q = hexbuf; *p;) {
397 #ifdef CHARSET_EBCDIC
398 ch = os_toebcdic[ch];
400 if(ch == ':') continue;
402 #ifdef CHARSET_EBCDIC
403 cl = os_toebcdic[cl];
406 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
407 OPENSSL_free(hexbuf);
410 if(isupper(ch)) ch = tolower(ch);
411 if(isupper(cl)) cl = tolower(cl);
413 if((ch >= '0') && (ch <= '9')) ch -= '0';
414 else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
417 if((cl >= '0') && (cl <= '9')) cl -= '0';
418 else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
421 *q++ = (ch << 4) | cl;
424 if(len) *len = q - hexbuf;
429 if(hexbuf) OPENSSL_free(hexbuf);
430 X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
434 OPENSSL_free(hexbuf);
435 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
440 /* V2I name comparison function: returns zero if 'name' matches
444 int name_cmp(const char *name, const char *cmp)
449 if((ret = strncmp(name, cmp, len))) return ret;
451 if(!c || (c=='.')) return 0;
455 static int sk_strcmp(const char * const *a, const char * const *b)
457 return strcmp(*a, *b);
460 STACK *X509_get1_email(X509 *x)
464 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
465 ret = get_email(X509_get_subject_name(x), gens);
466 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
470 STACK *X509_REQ_get1_email(X509_REQ *x)
473 STACK_OF(X509_EXTENSION) *exts;
475 exts = X509_REQ_get_extensions(x);
476 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
477 ret = get_email(X509_REQ_get_subject_name(x), gens);
478 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
479 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
484 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)
488 ASN1_IA5STRING *email;
491 /* Now add any email address(es) to STACK */
493 /* First supplied X509_NAME */
494 while((i = X509_NAME_get_index_by_NID(name,
495 NID_pkcs9_emailAddress, i)) > 0) {
496 ne = X509_NAME_get_entry(name, i);
497 email = X509_NAME_ENTRY_get_data(ne);
498 if(!append_ia5(&ret, email)) return NULL;
500 for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
502 gen = sk_GENERAL_NAME_value(gens, i);
503 if(gen->type != GEN_EMAIL) continue;
504 if(!append_ia5(&ret, gen->d.ia5)) return NULL;
509 static void str_free(void *str)
514 static int append_ia5(STACK **sk, ASN1_IA5STRING *email)
517 /* First some sanity checks */
518 if(email->type != V_ASN1_IA5STRING) return 1;
519 if(!email->data || !email->length) return 1;
520 if(!*sk) *sk = sk_new(sk_strcmp);
522 /* Don't add duplicates */
523 if(sk_find(*sk, (char *)email->data) != -1) return 1;
524 emtmp = BUF_strdup((char *)email->data);
525 if(!emtmp || !sk_push(*sk, emtmp)) {
526 X509_email_free(*sk);
533 void X509_email_free(STACK *sk)
535 sk_pop_free(sk, str_free);