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
*get_email(X509_NAME
*name
, GENERAL_NAMES
*gens
);
71 static void str_free(void *str
);
72 static int append_ia5(STACK
** sk
, ASN1_IA5STRING
*email
);
74 static int ipv4_from_asc(unsigned char *v4
, const char *in
);
75 static int ipv6_from_asc(unsigned char *v6
, const char *in
);
76 static int ipv6_cb(const char *elem
, int len
, void *usr
);
77 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
);
79 /* Add a CONF_VALUE name value pair to stack */
81 int X509V3_add_value(const char *name
, const char *value
,
82 STACK_OF(CONF_VALUE
) **extlist
)
84 CONF_VALUE
*vtmp
= NULL
;
85 char *tname
= NULL
, *tvalue
= NULL
;
86 if (name
&& !(tname
= BUF_strdup(name
)))
88 if (value
&& !(tvalue
= BUF_strdup(value
)))
90 if (!(vtmp
= (CONF_VALUE
*)OPENSSL_malloc(sizeof(CONF_VALUE
))))
92 if (!*extlist
&& !(*extlist
= sk_CONF_VALUE_new_null()))
97 if (!sk_CONF_VALUE_push(*extlist
, vtmp
))
101 X509V3err(X509V3_F_X509V3_ADD_VALUE
, ERR_R_MALLOC_FAILURE
);
107 OPENSSL_free(tvalue
);
111 int X509V3_add_value_uchar(const char *name
, const unsigned char *value
,
112 STACK_OF(CONF_VALUE
) **extlist
)
114 return X509V3_add_value(name
, (const char *)value
, extlist
);
117 /* Free function for STACK_OF(CONF_VALUE) */
119 void X509V3_conf_free(CONF_VALUE
*conf
)
124 OPENSSL_free(conf
->name
);
126 OPENSSL_free(conf
->value
);
128 OPENSSL_free(conf
->section
);
132 int X509V3_add_value_bool(const char *name
, int asn1_bool
,
133 STACK_OF(CONF_VALUE
) **extlist
)
136 return X509V3_add_value(name
, "TRUE", extlist
);
137 return X509V3_add_value(name
, "FALSE", extlist
);
140 int X509V3_add_value_bool_nf(char *name
, int asn1_bool
,
141 STACK_OF(CONF_VALUE
) **extlist
)
144 return X509V3_add_value(name
, "TRUE", extlist
);
148 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, ASN1_ENUMERATED
*a
)
150 BIGNUM
*bntmp
= NULL
;
154 if (!(bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) ||
155 !(strtmp
= BN_bn2dec(bntmp
)))
156 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED
, ERR_R_MALLOC_FAILURE
);
161 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, ASN1_INTEGER
*a
)
163 BIGNUM
*bntmp
= NULL
;
167 if (!(bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) ||
168 !(strtmp
= BN_bn2dec(bntmp
)))
169 X509V3err(X509V3_F_I2S_ASN1_INTEGER
, ERR_R_MALLOC_FAILURE
);
174 ASN1_INTEGER
*s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, char *value
)
181 X509V3err(X509V3_F_S2I_ASN1_INTEGER
, X509V3_R_INVALID_NULL_VALUE
);
185 if (value
[0] == '-') {
191 if (value
[0] == '0' && ((value
[1] == 'x') || (value
[1] == 'X'))) {
198 ret
= BN_hex2bn(&bn
, value
);
200 ret
= BN_dec2bn(&bn
, value
);
202 if (!ret
|| value
[ret
]) {
204 X509V3err(X509V3_F_S2I_ASN1_INTEGER
, X509V3_R_BN_DEC2BN_ERROR
);
208 if (isneg
&& BN_is_zero(bn
))
211 aint
= BN_to_ASN1_INTEGER(bn
, NULL
);
214 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,
215 X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
219 aint
->type
|= V_ASN1_NEG
;
223 int X509V3_add_value_int(const char *name
, ASN1_INTEGER
*aint
,
224 STACK_OF(CONF_VALUE
) **extlist
)
230 if (!(strtmp
= i2s_ASN1_INTEGER(NULL
, aint
)))
232 ret
= X509V3_add_value(name
, strtmp
, extlist
);
233 OPENSSL_free(strtmp
);
237 int X509V3_get_value_bool(CONF_VALUE
*value
, int *asn1_bool
)
240 if (!(btmp
= value
->value
))
242 if (!strcmp(btmp
, "TRUE") || !strcmp(btmp
, "true")
243 || !strcmp(btmp
, "Y") || !strcmp(btmp
, "y")
244 || !strcmp(btmp
, "YES") || !strcmp(btmp
, "yes")) {
247 } else if (!strcmp(btmp
, "FALSE") || !strcmp(btmp
, "false")
248 || !strcmp(btmp
, "N") || !strcmp(btmp
, "n")
249 || !strcmp(btmp
, "NO") || !strcmp(btmp
, "no")) {
254 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL
,
255 X509V3_R_INVALID_BOOLEAN_STRING
);
256 X509V3_conf_err(value
);
260 int X509V3_get_value_int(CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
263 if (!(itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
))) {
264 X509V3_conf_err(value
);
278 STACK_OF(CONF_VALUE
) *X509V3_parse_list(const char *line
)
282 STACK_OF(CONF_VALUE
) *values
= NULL
;
285 /* We are going to modify the line so copy it first */
286 linebuf
= BUF_strdup(line
);
289 /* Go through all characters */
290 for (p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!= '\r') && (c
!= '\n');
298 ntmp
= strip_spaces(q
);
300 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
301 X509V3_R_INVALID_NULL_NAME
);
305 } else if (c
== ',') {
307 ntmp
= strip_spaces(q
);
310 printf("%s\n", ntmp
);
313 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
314 X509V3_R_INVALID_NULL_NAME
);
317 X509V3_add_value(ntmp
, NULL
, &values
);
325 vtmp
= strip_spaces(q
);
327 printf("%s\n", ntmp
);
330 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
331 X509V3_R_INVALID_NULL_VALUE
);
334 X509V3_add_value(ntmp
, vtmp
, &values
);
342 if (state
== HDR_VALUE
) {
343 vtmp
= strip_spaces(q
);
345 printf("%s=%s\n", ntmp
, vtmp
);
348 X509V3err(X509V3_F_X509V3_PARSE_LIST
,
349 X509V3_R_INVALID_NULL_VALUE
);
352 X509V3_add_value(ntmp
, vtmp
, &values
);
354 ntmp
= strip_spaces(q
);
356 printf("%s\n", ntmp
);
359 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
362 X509V3_add_value(ntmp
, NULL
, &values
);
364 OPENSSL_free(linebuf
);
368 OPENSSL_free(linebuf
);
369 sk_CONF_VALUE_pop_free(values
, X509V3_conf_free
);
374 /* Delete leading and trailing spaces from a string */
375 static char *strip_spaces(char *name
)
378 /* Skip over leading spaces */
380 while (*p
&& isspace((unsigned char)*p
))
384 q
= p
+ strlen(p
) - 1;
385 while ((q
!= p
) && isspace((unsigned char)*q
))
394 /* hex string utilities */
397 * Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
398 * hex representation @@@ (Contents of buffer are always kept in ASCII, also
399 * on EBCDIC machines)
402 char *hex_to_string(unsigned char *buffer
, long len
)
407 const static char hexdig
[] = "0123456789ABCDEF";
410 if (!(tmp
= OPENSSL_malloc(len
* 3 + 1))) {
411 X509V3err(X509V3_F_HEX_TO_STRING
, ERR_R_MALLOC_FAILURE
);
415 for (i
= 0, p
= buffer
; i
< len
; i
++, p
++) {
416 *q
++ = hexdig
[(*p
>> 4) & 0xf];
417 *q
++ = hexdig
[*p
& 0xf];
421 #ifdef CHARSET_EBCDIC
422 ebcdic2ascii(tmp
, tmp
, q
- tmp
- 1);
429 * Give a string of hex digits convert to a buffer
432 unsigned char *string_to_hex(char *str
, long *len
)
434 unsigned char *hexbuf
, *q
;
435 unsigned char ch
, cl
, *p
;
437 X509V3err(X509V3_F_STRING_TO_HEX
, X509V3_R_INVALID_NULL_ARGUMENT
);
440 if (!(hexbuf
= OPENSSL_malloc(strlen(str
) >> 1)))
442 for (p
= (unsigned char *)str
, q
= hexbuf
; *p
;) {
444 #ifdef CHARSET_EBCDIC
445 ch
= os_toebcdic
[ch
];
450 #ifdef CHARSET_EBCDIC
451 cl
= os_toebcdic
[cl
];
454 X509V3err(X509V3_F_STRING_TO_HEX
, X509V3_R_ODD_NUMBER_OF_DIGITS
);
455 OPENSSL_free(hexbuf
);
463 if ((ch
>= '0') && (ch
<= '9'))
465 else if ((ch
>= 'a') && (ch
<= 'f'))
470 if ((cl
>= '0') && (cl
<= '9'))
472 else if ((cl
>= 'a') && (cl
<= 'f'))
477 *q
++ = (ch
<< 4) | cl
;
487 OPENSSL_free(hexbuf
);
488 X509V3err(X509V3_F_STRING_TO_HEX
, ERR_R_MALLOC_FAILURE
);
492 OPENSSL_free(hexbuf
);
493 X509V3err(X509V3_F_STRING_TO_HEX
, X509V3_R_ILLEGAL_HEX_DIGIT
);
499 * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*
502 int name_cmp(const char *name
, const char *cmp
)
507 if ((ret
= strncmp(name
, cmp
, len
)))
510 if (!c
|| (c
== '.'))
515 static int sk_strcmp(const char *const *a
, const char *const *b
)
517 return strcmp(*a
, *b
);
520 STACK
*X509_get1_email(X509
*x
)
524 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
525 ret
= get_email(X509_get_subject_name(x
), gens
);
526 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
530 STACK
*X509_get1_ocsp(X509
*x
)
532 AUTHORITY_INFO_ACCESS
*info
;
535 info
= X509_get_ext_d2i(x
, NID_info_access
, NULL
, NULL
);
538 for (i
= 0; i
< sk_ACCESS_DESCRIPTION_num(info
); i
++) {
539 ACCESS_DESCRIPTION
*ad
= sk_ACCESS_DESCRIPTION_value(info
, i
);
540 if (OBJ_obj2nid(ad
->method
) == NID_ad_OCSP
) {
541 if (ad
->location
->type
== GEN_URI
) {
543 (&ret
, ad
->location
->d
.uniformResourceIdentifier
))
548 AUTHORITY_INFO_ACCESS_free(info
);
552 STACK
*X509_REQ_get1_email(X509_REQ
*x
)
555 STACK_OF(X509_EXTENSION
) *exts
;
557 exts
= X509_REQ_get_extensions(x
);
558 gens
= X509V3_get_d2i(exts
, NID_subject_alt_name
, NULL
, NULL
);
559 ret
= get_email(X509_REQ_get_subject_name(x
), gens
);
560 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
561 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
565 static STACK
*get_email(X509_NAME
*name
, GENERAL_NAMES
*gens
)
569 ASN1_IA5STRING
*email
;
572 /* Now add any email address(es) to STACK */
574 /* First supplied X509_NAME */
575 while ((i
= X509_NAME_get_index_by_NID(name
,
576 NID_pkcs9_emailAddress
, i
)) >= 0) {
577 ne
= X509_NAME_get_entry(name
, i
);
578 email
= X509_NAME_ENTRY_get_data(ne
);
579 if (!append_ia5(&ret
, email
))
582 for (i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++) {
583 gen
= sk_GENERAL_NAME_value(gens
, i
);
584 if (gen
->type
!= GEN_EMAIL
)
586 if (!append_ia5(&ret
, gen
->d
.ia5
))
592 static void str_free(void *str
)
597 static int append_ia5(STACK
** sk
, ASN1_IA5STRING
*email
)
600 /* First some sanity checks */
601 if (email
->type
!= V_ASN1_IA5STRING
)
603 if (!email
->data
|| !email
->length
)
606 *sk
= sk_new(sk_strcmp
);
609 /* Don't add duplicates */
610 if (sk_find(*sk
, (char *)email
->data
) != -1)
612 emtmp
= BUF_strdup((char *)email
->data
);
613 if (!emtmp
|| !sk_push(*sk
, emtmp
)) {
614 X509_email_free(*sk
);
621 void X509_email_free(STACK
* sk
)
623 sk_pop_free(sk
, str_free
);
627 * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible
631 ASN1_OCTET_STRING
*a2i_IPADDRESS(const char *ipasc
)
633 unsigned char ipout
[16];
634 ASN1_OCTET_STRING
*ret
;
637 /* If string contains a ':' assume IPv6 */
639 iplen
= a2i_ipadd(ipout
, ipasc
);
644 ret
= ASN1_OCTET_STRING_new();
647 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen
)) {
648 ASN1_OCTET_STRING_free(ret
);
654 ASN1_OCTET_STRING
*a2i_IPADDRESS_NC(const char *ipasc
)
656 ASN1_OCTET_STRING
*ret
= NULL
;
657 unsigned char ipout
[32];
658 char *iptmp
= NULL
, *p
;
660 p
= strchr(ipasc
, '/');
663 iptmp
= BUF_strdup(ipasc
);
666 p
= iptmp
+ (p
- ipasc
);
669 iplen1
= a2i_ipadd(ipout
, iptmp
);
674 iplen2
= a2i_ipadd(ipout
+ iplen1
, p
);
679 if (!iplen2
|| (iplen1
!= iplen2
))
682 ret
= ASN1_OCTET_STRING_new();
685 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen1
+ iplen2
))
694 ASN1_OCTET_STRING_free(ret
);
698 int a2i_ipadd(unsigned char *ipout
, const char *ipasc
)
700 /* If string contains a ':' assume IPv6 */
702 if (strchr(ipasc
, ':')) {
703 if (!ipv6_from_asc(ipout
, ipasc
))
707 if (!ipv4_from_asc(ipout
, ipasc
))
713 static int ipv4_from_asc(unsigned char *v4
, const char *in
)
716 if (sscanf(in
, "%d.%d.%d.%d", &a0
, &a1
, &a2
, &a3
) != 4)
718 if ((a0
< 0) || (a0
> 255) || (a1
< 0) || (a1
> 255)
719 || (a2
< 0) || (a2
> 255) || (a3
< 0) || (a3
> 255))
729 /* Temporary store for IPV6 output */
730 unsigned char tmp
[16];
731 /* Total number of bytes in tmp */
733 /* The position of a zero (corresponding to '::') */
735 /* Number of zeroes */
739 static int ipv6_from_asc(unsigned char *v6
, const char *in
)
743 v6stat
.zero_pos
= -1;
746 * Treat the IPv6 representation as a list of values separated by ':'.
747 * The presence of a '::' will parse as one, two or three zero length
750 if (!CONF_parse_list(in
, ':', 0, ipv6_cb
, &v6stat
))
753 /* Now for some sanity checks */
755 if (v6stat
.zero_pos
== -1) {
756 /* If no '::' must have exactly 16 bytes */
757 if (v6stat
.total
!= 16)
760 /* If '::' must have less than 16 bytes */
761 if (v6stat
.total
== 16)
763 /* More than three zeroes is an error */
764 if (v6stat
.zero_cnt
> 3)
766 /* Can only have three zeroes if nothing else present */
767 else if (v6stat
.zero_cnt
== 3) {
768 if (v6stat
.total
> 0)
771 /* Can only have two zeroes if at start or end */
772 else if (v6stat
.zero_cnt
== 2) {
773 if ((v6stat
.zero_pos
!= 0)
774 && (v6stat
.zero_pos
!= v6stat
.total
))
777 /* Can only have one zero if *not* start or end */
779 if ((v6stat
.zero_pos
== 0)
780 || (v6stat
.zero_pos
== v6stat
.total
))
787 if (v6stat
.zero_pos
>= 0) {
788 /* Copy initial part */
789 memcpy(v6
, v6stat
.tmp
, v6stat
.zero_pos
);
791 memset(v6
+ v6stat
.zero_pos
, 0, 16 - v6stat
.total
);
792 /* Copy final part */
793 if (v6stat
.total
!= v6stat
.zero_pos
)
794 memcpy(v6
+ v6stat
.zero_pos
+ 16 - v6stat
.total
,
795 v6stat
.tmp
+ v6stat
.zero_pos
,
796 v6stat
.total
- v6stat
.zero_pos
);
798 memcpy(v6
, v6stat
.tmp
, 16);
803 static int ipv6_cb(const char *elem
, int len
, void *usr
)
806 /* Error if 16 bytes written */
810 /* Zero length element, corresponds to '::' */
811 if (s
->zero_pos
== -1)
812 s
->zero_pos
= s
->total
;
813 /* If we've already got a :: its an error */
814 else if (s
->zero_pos
!= s
->total
)
818 /* If more than 4 characters could be final a.b.c.d form */
820 /* Need at least 4 bytes left */
823 /* Must be end of string */
826 if (!ipv4_from_asc(s
->tmp
+ s
->total
, elem
))
830 if (!ipv6_hex(s
->tmp
+ s
->total
, elem
, len
))
839 * Convert a string of up to 4 hex digits into the corresponding IPv6 form.
842 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
)
845 unsigned int num
= 0;
851 if ((c
>= '0') && (c
<= '9'))
853 else if ((c
>= 'A') && (c
<= 'F'))
855 else if ((c
>= 'a') && (c
<= 'f'))
865 int X509V3_NAME_from_section(X509_NAME
*nm
, STACK_OF(CONF_VALUE
) *dn_sk
,
866 unsigned long chtype
)
874 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++) {
875 v
= sk_CONF_VALUE_value(dn_sk
, i
);
878 * Skip past any leading X. X: X, etc to allow for multiple instances
880 for (p
= type
; *p
; p
++)
881 #ifndef CHARSET_EBCDIC
882 if ((*p
== ':') || (*p
== ',') || (*p
== '.'))
884 if ((*p
== os_toascii
[':']) || (*p
== os_toascii
[','])
885 || (*p
== os_toascii
['.']))
893 #ifndef CHARSET_EBCDIC
896 if (*type
== os_toascii
['+'])
903 if (!X509_NAME_add_entry_by_txt(nm
, type
, chtype
,
904 (unsigned char *)v
->value
, -1, -1,