2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2003 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>
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
))) goto err
;
87 if(value
&& !(tvalue
= BUF_strdup(value
))) goto err
;;
88 if(!(vtmp
= (CONF_VALUE
*)OPENSSL_malloc(sizeof(CONF_VALUE
)))) goto err
;
89 if(!*extlist
&& !(*extlist
= sk_CONF_VALUE_new_null())) goto err
;
93 if(!sk_CONF_VALUE_push(*extlist
, vtmp
)) goto err
;
96 X509V3err(X509V3_F_X509V3_ADD_VALUE
,ERR_R_MALLOC_FAILURE
);
97 if(vtmp
) OPENSSL_free(vtmp
);
98 if(tname
) OPENSSL_free(tname
);
99 if(tvalue
) OPENSSL_free(tvalue
);
103 int X509V3_add_value_uchar(const char *name
, const unsigned char *value
,
104 STACK_OF(CONF_VALUE
) **extlist
)
106 return X509V3_add_value(name
,(const char *)value
,extlist
);
109 /* Free function for STACK_OF(CONF_VALUE) */
111 void X509V3_conf_free(CONF_VALUE
*conf
)
114 if(conf
->name
) OPENSSL_free(conf
->name
);
115 if(conf
->value
) OPENSSL_free(conf
->value
);
116 if(conf
->section
) OPENSSL_free(conf
->section
);
120 int X509V3_add_value_bool(const char *name
, int asn1_bool
,
121 STACK_OF(CONF_VALUE
) **extlist
)
123 if(asn1_bool
) return X509V3_add_value(name
, "TRUE", extlist
);
124 return X509V3_add_value(name
, "FALSE", extlist
);
127 int X509V3_add_value_bool_nf(char *name
, int asn1_bool
,
128 STACK_OF(CONF_VALUE
) **extlist
)
130 if(asn1_bool
) return X509V3_add_value(name
, "TRUE", extlist
);
135 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, ASN1_ENUMERATED
*a
)
137 BIGNUM
*bntmp
= NULL
;
140 if(!(bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) ||
141 !(strtmp
= BN_bn2dec(bntmp
)) )
142 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED
,ERR_R_MALLOC_FAILURE
);
147 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, ASN1_INTEGER
*a
)
149 BIGNUM
*bntmp
= NULL
;
152 if(!(bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) ||
153 !(strtmp
= BN_bn2dec(bntmp
)) )
154 X509V3err(X509V3_F_I2S_ASN1_INTEGER
,ERR_R_MALLOC_FAILURE
);
159 ASN1_INTEGER
*s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, char *value
)
166 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_INVALID_NULL_VALUE
);
170 if (value
[0] == '-') {
175 if (value
[0] == '0' && ((value
[1] == 'x') || (value
[1] == 'X'))) {
180 if (ishex
) ret
= BN_hex2bn(&bn
, value
);
181 else ret
= BN_dec2bn(&bn
, value
);
183 if (!ret
|| value
[ret
]) {
185 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_BN_DEC2BN_ERROR
);
189 if (isneg
&& BN_is_zero(bn
)) isneg
= 0;
191 aint
= BN_to_ASN1_INTEGER(bn
, NULL
);
194 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
197 if (isneg
) aint
->type
|= V_ASN1_NEG
;
201 int X509V3_add_value_int(const char *name
, ASN1_INTEGER
*aint
,
202 STACK_OF(CONF_VALUE
) **extlist
)
207 if(!(strtmp
= i2s_ASN1_INTEGER(NULL
, aint
))) return 0;
208 ret
= X509V3_add_value(name
, strtmp
, extlist
);
209 OPENSSL_free(strtmp
);
213 int X509V3_get_value_bool(CONF_VALUE
*value
, int *asn1_bool
)
216 if(!(btmp
= value
->value
)) goto err
;
217 if(!strcmp(btmp
, "TRUE") || !strcmp(btmp
, "true")
218 || !strcmp(btmp
, "Y") || !strcmp(btmp
, "y")
219 || !strcmp(btmp
, "YES") || !strcmp(btmp
, "yes")) {
222 } else if(!strcmp(btmp
, "FALSE") || !strcmp(btmp
, "false")
223 || !strcmp(btmp
, "N") || !strcmp(btmp
, "n")
224 || !strcmp(btmp
, "NO") || !strcmp(btmp
, "no")) {
229 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL
,X509V3_R_INVALID_BOOLEAN_STRING
);
230 X509V3_conf_err(value
);
234 int X509V3_get_value_int(CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
237 if(!(itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
))) {
238 X509V3_conf_err(value
);
250 STACK_OF(CONF_VALUE
) *X509V3_parse_list(const char *line
)
254 STACK_OF(CONF_VALUE
) *values
= NULL
;
257 /* We are going to modify the line so copy it first */
258 linebuf
= BUF_strdup(line
);
261 /* Go through all characters */
262 for(p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!='\r') && (c
!='\n'); p
++) {
269 ntmp
= strip_spaces(q
);
271 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
275 } else if(c
== ',') {
277 ntmp
= strip_spaces(q
);
280 printf("%s\n", ntmp
);
283 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
286 X509V3_add_value(ntmp
, NULL
, &values
);
294 vtmp
= strip_spaces(q
);
296 printf("%s\n", ntmp
);
299 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_VALUE
);
302 X509V3_add_value(ntmp
, vtmp
, &values
);
310 if(state
== HDR_VALUE
) {
311 vtmp
= strip_spaces(q
);
313 printf("%s=%s\n", ntmp
, vtmp
);
316 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_VALUE
);
319 X509V3_add_value(ntmp
, vtmp
, &values
);
321 ntmp
= strip_spaces(q
);
323 printf("%s\n", ntmp
);
326 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
329 X509V3_add_value(ntmp
, NULL
, &values
);
331 OPENSSL_free(linebuf
);
335 OPENSSL_free(linebuf
);
336 sk_CONF_VALUE_pop_free(values
, X509V3_conf_free
);
341 /* Delete leading and trailing spaces from a string */
342 static char *strip_spaces(char *name
)
345 /* Skip over leading spaces */
347 while(*p
&& isspace((unsigned char)*p
)) p
++;
349 q
= p
+ strlen(p
) - 1;
350 while((q
!= p
) && isspace((unsigned char)*q
)) q
--;
356 /* hex string utilities */
358 /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
360 * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
363 char *hex_to_string(unsigned char *buffer
, long len
)
368 const static char hexdig
[] = "0123456789ABCDEF";
369 if(!buffer
|| !len
) return NULL
;
370 if(!(tmp
= OPENSSL_malloc(len
* 3 + 1))) {
371 X509V3err(X509V3_F_HEX_TO_STRING
,ERR_R_MALLOC_FAILURE
);
375 for(i
= 0, p
= buffer
; i
< len
; i
++,p
++) {
376 *q
++ = hexdig
[(*p
>> 4) & 0xf];
377 *q
++ = hexdig
[*p
& 0xf];
381 #ifdef CHARSET_EBCDIC
382 ebcdic2ascii(tmp
, tmp
, q
- tmp
- 1);
388 /* Give a string of hex digits convert to
392 unsigned char *string_to_hex(char *str
, long *len
)
394 unsigned char *hexbuf
, *q
;
395 unsigned char ch
, cl
, *p
;
397 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_INVALID_NULL_ARGUMENT
);
400 if(!(hexbuf
= OPENSSL_malloc(strlen(str
) >> 1))) goto err
;
401 for(p
= (unsigned char *)str
, q
= hexbuf
; *p
;) {
403 #ifdef CHARSET_EBCDIC
404 ch
= os_toebcdic
[ch
];
406 if(ch
== ':') continue;
408 #ifdef CHARSET_EBCDIC
409 cl
= os_toebcdic
[cl
];
412 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_ODD_NUMBER_OF_DIGITS
);
413 OPENSSL_free(hexbuf
);
416 if(isupper(ch
)) ch
= tolower(ch
);
417 if(isupper(cl
)) cl
= tolower(cl
);
419 if((ch
>= '0') && (ch
<= '9')) ch
-= '0';
420 else if ((ch
>= 'a') && (ch
<= 'f')) ch
-= 'a' - 10;
423 if((cl
>= '0') && (cl
<= '9')) cl
-= '0';
424 else if ((cl
>= 'a') && (cl
<= 'f')) cl
-= 'a' - 10;
427 *q
++ = (ch
<< 4) | cl
;
430 if(len
) *len
= q
- hexbuf
;
435 if(hexbuf
) OPENSSL_free(hexbuf
);
436 X509V3err(X509V3_F_STRING_TO_HEX
,ERR_R_MALLOC_FAILURE
);
440 OPENSSL_free(hexbuf
);
441 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_ILLEGAL_HEX_DIGIT
);
446 /* V2I name comparison function: returns zero if 'name' matches
450 int name_cmp(const char *name
, const char *cmp
)
455 if((ret
= strncmp(name
, cmp
, len
))) return ret
;
457 if(!c
|| (c
=='.')) return 0;
461 static int sk_strcmp(const char * const *a
, const char * const *b
)
463 return strcmp(*a
, *b
);
466 STACK
*X509_get1_email(X509
*x
)
470 gens
= X509_get_ext_d2i(x
, NID_subject_alt_name
, NULL
, NULL
);
471 ret
= get_email(X509_get_subject_name(x
), gens
);
472 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
476 STACK
*X509_REQ_get1_email(X509_REQ
*x
)
479 STACK_OF(X509_EXTENSION
) *exts
;
481 exts
= X509_REQ_get_extensions(x
);
482 gens
= X509V3_get_d2i(exts
, NID_subject_alt_name
, NULL
, NULL
);
483 ret
= get_email(X509_REQ_get_subject_name(x
), gens
);
484 sk_GENERAL_NAME_pop_free(gens
, GENERAL_NAME_free
);
485 sk_X509_EXTENSION_pop_free(exts
, X509_EXTENSION_free
);
490 static STACK
*get_email(X509_NAME
*name
, GENERAL_NAMES
*gens
)
494 ASN1_IA5STRING
*email
;
497 /* Now add any email address(es) to STACK */
499 /* First supplied X509_NAME */
500 while((i
= X509_NAME_get_index_by_NID(name
,
501 NID_pkcs9_emailAddress
, i
)) >= 0) {
502 ne
= X509_NAME_get_entry(name
, i
);
503 email
= X509_NAME_ENTRY_get_data(ne
);
504 if(!append_ia5(&ret
, email
)) return NULL
;
506 for(i
= 0; i
< sk_GENERAL_NAME_num(gens
); i
++)
508 gen
= sk_GENERAL_NAME_value(gens
, i
);
509 if(gen
->type
!= GEN_EMAIL
) continue;
510 if(!append_ia5(&ret
, gen
->d
.ia5
)) return NULL
;
515 static void str_free(void *str
)
520 static int append_ia5(STACK
**sk
, ASN1_IA5STRING
*email
)
523 /* First some sanity checks */
524 if(email
->type
!= V_ASN1_IA5STRING
) return 1;
525 if(!email
->data
|| !email
->length
) return 1;
526 if(!*sk
) *sk
= sk_new(sk_strcmp
);
528 /* Don't add duplicates */
529 if(sk_find(*sk
, (char *)email
->data
) != -1) return 1;
530 emtmp
= BUF_strdup((char *)email
->data
);
531 if(!emtmp
|| !sk_push(*sk
, emtmp
)) {
532 X509_email_free(*sk
);
539 void X509_email_free(STACK
*sk
)
541 sk_pop_free(sk
, str_free
);
544 /* Convert IP addresses both IPv4 and IPv6 into an
545 * OCTET STRING compatible with RFC3280.
548 ASN1_OCTET_STRING
*a2i_IPADDRESS(const char *ipasc
)
550 unsigned char ipout
[16];
551 ASN1_OCTET_STRING
*ret
;
554 /* If string contains a ':' assume IPv6 */
556 iplen
= a2i_ipadd(ipout
, ipasc
);
561 ret
= ASN1_OCTET_STRING_new();
564 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen
))
566 ASN1_OCTET_STRING_free(ret
);
572 ASN1_OCTET_STRING
*a2i_IPADDRESS_NC(const char *ipasc
)
574 ASN1_OCTET_STRING
*ret
= NULL
;
575 unsigned char ipout
[32];
576 char *iptmp
= NULL
, *p
;
578 p
= strchr(ipasc
,'/');
581 iptmp
= BUF_strdup(ipasc
);
584 p
= iptmp
+ (p
- ipasc
);
587 iplen1
= a2i_ipadd(ipout
, iptmp
);
592 iplen2
= a2i_ipadd(ipout
+ iplen1
, p
);
597 if (!iplen2
|| (iplen1
!= iplen2
))
600 ret
= ASN1_OCTET_STRING_new();
603 if (!ASN1_OCTET_STRING_set(ret
, ipout
, iplen1
+ iplen2
))
612 ASN1_OCTET_STRING_free(ret
);
617 int a2i_ipadd(unsigned char *ipout
, const char *ipasc
)
619 /* If string contains a ':' assume IPv6 */
621 if (strchr(ipasc
, ':'))
623 if (!ipv6_from_asc(ipout
, ipasc
))
629 if (!ipv4_from_asc(ipout
, ipasc
))
635 static int ipv4_from_asc(unsigned char *v4
, const char *in
)
638 if (sscanf(in
, "%d.%d.%d.%d", &a0
, &a1
, &a2
, &a3
) != 4)
640 if ((a0
< 0) || (a0
> 255) || (a1
< 0) || (a1
> 255)
641 || (a2
< 0) || (a2
> 255) || (a3
< 0) || (a3
> 255))
651 /* Temporary store for IPV6 output */
652 unsigned char tmp
[16];
653 /* Total number of bytes in tmp */
655 /* The position of a zero (corresponding to '::') */
657 /* Number of zeroes */
662 static int ipv6_from_asc(unsigned char *v6
, const char *in
)
666 v6stat
.zero_pos
= -1;
668 /* Treat the IPv6 representation as a list of values
669 * separated by ':'. The presence of a '::' will parse
670 * as one, two or three zero length elements.
672 if (!CONF_parse_list(in
, ':', 0, ipv6_cb
, &v6stat
))
675 /* Now for some sanity checks */
677 if (v6stat
.zero_pos
== -1)
679 /* If no '::' must have exactly 16 bytes */
680 if (v6stat
.total
!= 16)
685 /* If '::' must have less than 16 bytes */
686 if (v6stat
.total
== 16)
688 /* More than three zeroes is an error */
689 if (v6stat
.zero_cnt
> 3)
691 /* Can only have three zeroes if nothing else present */
692 else if (v6stat
.zero_cnt
== 3)
694 if (v6stat
.total
> 0)
697 /* Can only have two zeroes if at start or end */
698 else if (v6stat
.zero_cnt
== 2)
700 if ((v6stat
.zero_pos
!= 0)
701 && (v6stat
.zero_pos
!= v6stat
.total
))
705 /* Can only have one zero if *not* start or end */
707 if ((v6stat
.zero_pos
== 0)
708 || (v6stat
.zero_pos
== v6stat
.total
))
715 /* Copy initial part */
716 if (v6stat
.zero_pos
> 0)
717 memcpy(v6
, v6stat
.tmp
, v6stat
.zero_pos
);
719 if (v6stat
.total
!= 16)
720 memset(v6
+ v6stat
.zero_pos
, 0, 16 - v6stat
.total
);
721 /* Copy final part */
722 if (v6stat
.total
!= v6stat
.zero_pos
)
723 memcpy(v6
+ v6stat
.zero_pos
+ 16 - v6stat
.total
,
724 v6stat
.tmp
+ v6stat
.zero_pos
,
725 v6stat
.total
- v6stat
.zero_pos
);
730 static int ipv6_cb(const char *elem
, int len
, void *usr
)
733 /* Error if 16 bytes written */
738 /* Zero length element, corresponds to '::' */
739 if (s
->zero_pos
== -1)
740 s
->zero_pos
= s
->total
;
741 /* If we've already got a :: its an error */
742 else if (s
->zero_pos
!= s
->total
)
748 /* If more than 4 characters could be final a.b.c.d form */
751 /* Need at least 4 bytes left */
754 /* Must be end of string */
757 if (!ipv4_from_asc(s
->tmp
+ s
->total
, elem
))
763 if (!ipv6_hex(s
->tmp
+ s
->total
, elem
, len
))
771 /* Convert a string of up to 4 hex digits into the corresponding
775 static int ipv6_hex(unsigned char *out
, const char *in
, int inlen
)
778 unsigned int num
= 0;
785 if ((c
>= '0') && (c
<= '9'))
787 else if ((c
>= 'A') && (c
<= 'F'))
789 else if ((c
>= 'a') && (c
<= 'f'))
800 int X509V3_NAME_from_section(X509_NAME
*nm
, STACK_OF(CONF_VALUE
)*dn_sk
,
801 unsigned long chtype
)
809 for (i
= 0; i
< sk_CONF_VALUE_num(dn_sk
); i
++)
811 v
=sk_CONF_VALUE_value(dn_sk
,i
);
813 /* Skip past any leading X. X: X, etc to allow for
816 for(p
= type
; *p
; p
++)
817 #ifndef CHARSET_EBCDIC
818 if ((*p
== ':') || (*p
== ',') || (*p
== '.'))
820 if ((*p
== os_toascii
[':']) || (*p
== os_toascii
[',']) || (*p
== os_toascii
['.']))
827 #ifndef CHARSET_EBCDIC
830 if (*type
== os_toascii
['+'])
838 if (!X509_NAME_add_entry_by_txt(nm
,type
, chtype
,
839 (unsigned char *) v
->value
,-1,-1,mval
))