3 * Written by Dr Stephen N Henson (steve@openssl.org) for the OpenSSL
6 /* ====================================================================
7 * Copyright (c) 1999-2003 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
59 /* X509 v3 extension utilities */
64 #include <openssl/conf.h>
65 #include <openssl/x509v3.h>
66 #include <openssl/bn.h>
68 static char *strip_spaces(char *name);
69 static int sk_strcmp(const char *const *a, const char *const *b);
70 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
72 static void str_free(OPENSSL_STRING str);
73 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email);
75 static int ipv4_from_asc(unsigned char *v4, const char *in);
76 static int ipv6_from_asc(unsigned char *v6, const char *in);
77 static int ipv6_cb(const char *elem, int len, void *usr);
78 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
80 /* Add a CONF_VALUE name value pair to stack */
82 int X509V3_add_value(const char *name, const char *value,
83 STACK_OF(CONF_VALUE) **extlist)
85 CONF_VALUE *vtmp = NULL;
86 char *tname = NULL, *tvalue = NULL;
87 if (name && !(tname = BUF_strdup(name)))
89 if (value && !(tvalue = BUF_strdup(value)))
91 if (!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE))))
93 if (!*extlist && !(*extlist = sk_CONF_VALUE_new_null()))
98 if (!sk_CONF_VALUE_push(*extlist, vtmp))
102 X509V3err(X509V3_F_X509V3_ADD_VALUE, ERR_R_MALLOC_FAILURE);
108 OPENSSL_free(tvalue);
112 int X509V3_add_value_uchar(const char *name, const unsigned char *value,
113 STACK_OF(CONF_VALUE) **extlist)
115 return X509V3_add_value(name, (const char *)value, extlist);
118 /* Free function for STACK_OF(CONF_VALUE) */
120 void X509V3_conf_free(CONF_VALUE *conf)
125 OPENSSL_free(conf->name);
127 OPENSSL_free(conf->value);
129 OPENSSL_free(conf->section);
133 int X509V3_add_value_bool(const char *name, int asn1_bool,
134 STACK_OF(CONF_VALUE) **extlist)
137 return X509V3_add_value(name, "TRUE", extlist);
138 return X509V3_add_value(name, "FALSE", extlist);
141 int X509V3_add_value_bool_nf(char *name, int asn1_bool,
142 STACK_OF(CONF_VALUE) **extlist)
145 return X509V3_add_value(name, "TRUE", extlist);
149 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
151 BIGNUM *bntmp = NULL;
155 if (!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
156 !(strtmp = BN_bn2dec(bntmp)))
157 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE);
162 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
164 BIGNUM *bntmp = NULL;
168 if (!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
169 !(strtmp = BN_bn2dec(bntmp)))
170 X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE);
175 ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
182 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE);
186 if (value[0] == '-') {
192 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
199 ret = BN_hex2bn(&bn, value);
201 ret = BN_dec2bn(&bn, value);
203 if (!ret || value[ret]) {
205 X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR);
209 if (isneg && BN_is_zero(bn))
212 aint = BN_to_ASN1_INTEGER(bn, NULL);
215 X509V3err(X509V3_F_S2I_ASN1_INTEGER,
216 X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
220 aint->type |= V_ASN1_NEG;
224 int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
225 STACK_OF(CONF_VALUE) **extlist)
231 if (!(strtmp = i2s_ASN1_INTEGER(NULL, aint)))
233 ret = X509V3_add_value(name, strtmp, extlist);
234 OPENSSL_free(strtmp);
238 int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
241 if (!(btmp = value->value))
243 if (!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
244 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
245 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
248 } else if (!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
249 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
250 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
255 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,
256 X509V3_R_INVALID_BOOLEAN_STRING);
257 X509V3_conf_err(value);
261 int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
264 if (!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
265 X509V3_conf_err(value);
279 STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
283 STACK_OF(CONF_VALUE) *values = NULL;
286 /* We are going to modify the line so copy it first */
287 linebuf = BUF_strdup(line);
290 /* Go through all characters */
291 for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');
299 ntmp = strip_spaces(q);
301 X509V3err(X509V3_F_X509V3_PARSE_LIST,
302 X509V3_R_INVALID_NULL_NAME);
306 } else if (c == ',') {
308 ntmp = strip_spaces(q);
311 printf("%s\n", ntmp);
314 X509V3err(X509V3_F_X509V3_PARSE_LIST,
315 X509V3_R_INVALID_NULL_NAME);
318 X509V3_add_value(ntmp, NULL, &values);
326 vtmp = strip_spaces(q);
328 printf("%s\n", ntmp);
331 X509V3err(X509V3_F_X509V3_PARSE_LIST,
332 X509V3_R_INVALID_NULL_VALUE);
335 X509V3_add_value(ntmp, vtmp, &values);
343 if (state == HDR_VALUE) {
344 vtmp = strip_spaces(q);
346 printf("%s=%s\n", ntmp, vtmp);
349 X509V3err(X509V3_F_X509V3_PARSE_LIST,
350 X509V3_R_INVALID_NULL_VALUE);
353 X509V3_add_value(ntmp, vtmp, &values);
355 ntmp = strip_spaces(q);
357 printf("%s\n", ntmp);
360 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
363 X509V3_add_value(ntmp, NULL, &values);
365 OPENSSL_free(linebuf);
369 OPENSSL_free(linebuf);
370 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
375 /* Delete leading and trailing spaces from a string */
376 static char *strip_spaces(char *name)
379 /* Skip over leading spaces */
381 while (*p && isspace((unsigned char)*p))
385 q = p + strlen(p) - 1;
386 while ((q != p) && isspace((unsigned char)*q))
395 /* hex string utilities */
398 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
399 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
400 * on EBCDIC machines)
403 char *hex_to_string(const unsigned char *buffer, long len)
406 const unsigned char *p;
408 const static char hexdig[] = "0123456789ABCDEF";
411 if (!(tmp = OPENSSL_malloc(len * 3 + 1))) {
412 X509V3err(X509V3_F_HEX_TO_STRING, ERR_R_MALLOC_FAILURE);
416 for (i = 0, p = buffer; i < len; i++, p++) {
417 *q++ = hexdig[(*p >> 4) & 0xf];
418 *q++ = hexdig[*p & 0xf];
422 #ifdef CHARSET_EBCDIC
423 ebcdic2ascii(tmp, tmp, q - tmp - 1);
430 * Give a string of hex digits convert to a buffer
433 unsigned char *string_to_hex(const char *str, long *len)
435 unsigned char *hexbuf, *q;
436 unsigned char ch, cl, *p;
438 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_INVALID_NULL_ARGUMENT);
441 if (!(hexbuf = OPENSSL_malloc(strlen(str) >> 1)))
443 for (p = (unsigned char *)str, q = hexbuf; *p;) {
445 #ifdef CHARSET_EBCDIC
446 ch = os_toebcdic[ch];
451 #ifdef CHARSET_EBCDIC
452 cl = os_toebcdic[cl];
455 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ODD_NUMBER_OF_DIGITS);
456 OPENSSL_free(hexbuf);
464 if ((ch >= '0') && (ch <= '9'))
466 else if ((ch >= 'a') && (ch <= 'f'))
471 if ((cl >= '0') && (cl <= '9'))
473 else if ((cl >= 'a') && (cl <= 'f'))
478 *q++ = (ch << 4) | cl;
488 OPENSSL_free(hexbuf);
489 X509V3err(X509V3_F_STRING_TO_HEX, ERR_R_MALLOC_FAILURE);
493 OPENSSL_free(hexbuf);
494 X509V3err(X509V3_F_STRING_TO_HEX, X509V3_R_ILLEGAL_HEX_DIGIT);
500 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
503 int name_cmp(const char *name, const char *cmp)
508 if ((ret = strncmp(name, cmp, len)))
511 if (!c || (c == '.'))
516 static int sk_strcmp(const char *const *a, const char *const *b)
518 return strcmp(*a, *b);
521 STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)
524 STACK_OF(OPENSSL_STRING) *ret;
526 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
527 ret = get_email(X509_get_subject_name(x), gens);
528 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
532 STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)
534 AUTHORITY_INFO_ACCESS *info;
535 STACK_OF(OPENSSL_STRING) *ret = NULL;
538 info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);
541 for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) {
542 ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);
543 if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) {
544 if (ad->location->type == GEN_URI) {
546 (&ret, ad->location->d.uniformResourceIdentifier))
551 AUTHORITY_INFO_ACCESS_free(info);
555 STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)
558 STACK_OF(X509_EXTENSION) *exts;
559 STACK_OF(OPENSSL_STRING) *ret;
561 exts = X509_REQ_get_extensions(x);
562 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
563 ret = get_email(X509_REQ_get_subject_name(x), gens);
564 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
565 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
569 static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name,
572 STACK_OF(OPENSSL_STRING) *ret = NULL;
574 ASN1_IA5STRING *email;
577 /* Now add any email address(es) to STACK */
579 /* First supplied X509_NAME */
580 while ((i = X509_NAME_get_index_by_NID(name,
581 NID_pkcs9_emailAddress, i)) >= 0) {
582 ne = X509_NAME_get_entry(name, i);
583 email = X509_NAME_ENTRY_get_data(ne);
584 if (!append_ia5(&ret, email))
587 for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) {
588 gen = sk_GENERAL_NAME_value(gens, i);
589 if (gen->type != GEN_EMAIL)
591 if (!append_ia5(&ret, gen->d.ia5))
597 static void str_free(OPENSSL_STRING str)
602 static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, ASN1_IA5STRING *email)
605 /* First some sanity checks */
606 if (email->type != V_ASN1_IA5STRING)
608 if (!email->data || !email->length)
611 *sk = sk_OPENSSL_STRING_new(sk_strcmp);
614 /* Don't add duplicates */
615 if (sk_OPENSSL_STRING_find(*sk, (char *)email->data) != -1)
617 emtmp = BUF_strdup((char *)email->data);
618 if (!emtmp || !sk_OPENSSL_STRING_push(*sk, emtmp)) {
619 X509_email_free(*sk);
626 void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)
628 sk_OPENSSL_STRING_pop_free(sk, str_free);
632 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
636 ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
638 unsigned char ipout[16];
639 ASN1_OCTET_STRING *ret;
642 /* If string contains a ':' assume IPv6 */
644 iplen = a2i_ipadd(ipout, ipasc);
649 ret = ASN1_OCTET_STRING_new();
652 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) {
653 ASN1_OCTET_STRING_free(ret);
659 ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
661 ASN1_OCTET_STRING *ret = NULL;
662 unsigned char ipout[32];
663 char *iptmp = NULL, *p;
665 p = strchr(ipasc, '/');
668 iptmp = BUF_strdup(ipasc);
671 p = iptmp + (p - ipasc);
674 iplen1 = a2i_ipadd(ipout, iptmp);
679 iplen2 = a2i_ipadd(ipout + iplen1, p);
684 if (!iplen2 || (iplen1 != iplen2))
687 ret = ASN1_OCTET_STRING_new();
690 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
699 ASN1_OCTET_STRING_free(ret);
703 int a2i_ipadd(unsigned char *ipout, const char *ipasc)
705 /* If string contains a ':' assume IPv6 */
707 if (strchr(ipasc, ':')) {
708 if (!ipv6_from_asc(ipout, ipasc))
712 if (!ipv4_from_asc(ipout, ipasc))
718 static int ipv4_from_asc(unsigned char *v4, const char *in)
721 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
723 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
724 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
734 /* Temporary store for IPV6 output */
735 unsigned char tmp[16];
736 /* Total number of bytes in tmp */
738 /* The position of a zero (corresponding to '::') */
740 /* Number of zeroes */
744 static int ipv6_from_asc(unsigned char *v6, const char *in)
748 v6stat.zero_pos = -1;
751 * Treat the IPv6 representation as a list of values separated by ':'.
752 * The presence of a '::' will parse as one, two or three zero length
755 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
758 /* Now for some sanity checks */
760 if (v6stat.zero_pos == -1) {
761 /* If no '::' must have exactly 16 bytes */
762 if (v6stat.total != 16)
765 /* If '::' must have less than 16 bytes */
766 if (v6stat.total == 16)
768 /* More than three zeroes is an error */
769 if (v6stat.zero_cnt > 3)
771 /* Can only have three zeroes if nothing else present */
772 else if (v6stat.zero_cnt == 3) {
773 if (v6stat.total > 0)
776 /* Can only have two zeroes if at start or end */
777 else if (v6stat.zero_cnt == 2) {
778 if ((v6stat.zero_pos != 0)
779 && (v6stat.zero_pos != v6stat.total))
782 /* Can only have one zero if *not* start or end */
784 if ((v6stat.zero_pos == 0)
785 || (v6stat.zero_pos == v6stat.total))
792 if (v6stat.zero_pos >= 0) {
793 /* Copy initial part */
794 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
796 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
797 /* Copy final part */
798 if (v6stat.total != v6stat.zero_pos)
799 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
800 v6stat.tmp + v6stat.zero_pos,
801 v6stat.total - v6stat.zero_pos);
803 memcpy(v6, v6stat.tmp, 16);
808 static int ipv6_cb(const char *elem, int len, void *usr)
811 /* Error if 16 bytes written */
815 /* Zero length element, corresponds to '::' */
816 if (s->zero_pos == -1)
817 s->zero_pos = s->total;
818 /* If we've already got a :: its an error */
819 else if (s->zero_pos != s->total)
823 /* If more than 4 characters could be final a.b.c.d form */
825 /* Need at least 4 bytes left */
828 /* Must be end of string */
831 if (!ipv4_from_asc(s->tmp + s->total, elem))
835 if (!ipv6_hex(s->tmp + s->total, elem, len))
844 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
847 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
850 unsigned int num = 0;
856 if ((c >= '0') && (c <= '9'))
858 else if ((c >= 'A') && (c <= 'F'))
860 else if ((c >= 'a') && (c <= 'f'))
870 int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,
871 unsigned long chtype)
879 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) {
880 v = sk_CONF_VALUE_value(dn_sk, i);
883 * Skip past any leading X. X: X, etc to allow for multiple instances
885 for (p = type; *p; p++)
886 #ifndef CHARSET_EBCDIC
887 if ((*p == ':') || (*p == ',') || (*p == '.'))
889 if ((*p == os_toascii[':']) || (*p == os_toascii[','])
890 || (*p == os_toascii['.']))
898 #ifndef CHARSET_EBCDIC
901 if (*type == os_toascii['+'])
908 if (!X509_NAME_add_entry_by_txt(nm, type, chtype,
909 (unsigned char *)v->value, -1, -1,