Update contrib.
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).
59 © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
63 /* X509 v3 extension utilities */
69 #include <openssl/conf.h>
70 #include <openssl/x509v3.h>
71 #include <openssl/bn.h>
72 #if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
73 #include "libcrypto_wsd_macros.h"
74 #include "libcrypto_wsd.h"
77 static char *strip_spaces(char *name);
78 static int sk_strcmp(const char * const *a, const char * const *b);
79 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens);
80 static void str_free(void *str);
81 static int append_ia5(STACK **sk, ASN1_IA5STRING *email);
83 static int ipv4_from_asc(unsigned char *v4, const char *in);
84 static int ipv6_from_asc(unsigned char *v6, const char *in);
85 static int ipv6_cb(const char *elem, int len, void *usr);
86 static int ipv6_hex(unsigned char *out, const char *in, int inlen);
88 /* Add a CONF_VALUE name value pair to stack */
90 EXPORT_C int X509V3_add_value(const char *name, const char *value,
91 STACK_OF(CONF_VALUE) **extlist)
93 CONF_VALUE *vtmp = NULL;
94 char *tname = NULL, *tvalue = NULL;
95 if(name && !(tname = BUF_strdup(name))) goto err;
96 if(value && !(tvalue = BUF_strdup(value))) goto err;;
97 if(!(vtmp = (CONF_VALUE *)OPENSSL_malloc(sizeof(CONF_VALUE)))) goto err;
98 if(!*extlist && !(*extlist = sk_CONF_VALUE_new_null())) goto err;
101 vtmp->value = tvalue;
102 if(!sk_CONF_VALUE_push(*extlist, vtmp)) goto err;
105 X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
106 if(vtmp) OPENSSL_free(vtmp);
107 if(tname) OPENSSL_free(tname);
108 if(tvalue) OPENSSL_free(tvalue);
112 EXPORT_C 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 EXPORT_C void X509V3_conf_free(CONF_VALUE *conf)
123 if(conf->name) OPENSSL_free(conf->name);
124 if(conf->value) OPENSSL_free(conf->value);
125 if(conf->section) OPENSSL_free(conf->section);
129 EXPORT_C int X509V3_add_value_bool(const char *name, int asn1_bool,
130 STACK_OF(CONF_VALUE) **extlist)
132 if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
133 return X509V3_add_value(name, "FALSE", extlist);
136 EXPORT_C int X509V3_add_value_bool_nf(char *name, int asn1_bool,
137 STACK_OF(CONF_VALUE) **extlist)
139 if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
144 EXPORT_C char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, ASN1_ENUMERATED *a)
146 BIGNUM *bntmp = NULL;
149 if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
150 !(strtmp = BN_bn2dec(bntmp)) )
151 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
156 EXPORT_C char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, ASN1_INTEGER *a)
158 BIGNUM *bntmp = NULL;
161 if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
162 !(strtmp = BN_bn2dec(bntmp)) )
163 X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
168 EXPORT_C ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, char *value)
175 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE);
179 if (value[0] == '-') {
184 if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) {
189 if (ishex) ret = BN_hex2bn(&bn, value);
190 else ret = BN_dec2bn(&bn, value);
192 if (!ret || value[ret]) {
194 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR);
198 if (isneg && BN_is_zero(bn)) isneg = 0;
200 aint = BN_to_ASN1_INTEGER(bn, NULL);
203 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
206 if (isneg) aint->type |= V_ASN1_NEG;
210 EXPORT_C int X509V3_add_value_int(const char *name, ASN1_INTEGER *aint,
211 STACK_OF(CONF_VALUE) **extlist)
216 if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
217 ret = X509V3_add_value(name, strtmp, extlist);
218 OPENSSL_free(strtmp);
222 EXPORT_C int X509V3_get_value_bool(CONF_VALUE *value, int *asn1_bool)
225 if(!(btmp = value->value)) goto err;
226 if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
227 || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
228 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
231 } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
232 || !strcmp(btmp, "N") || !strcmp(btmp, "n")
233 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
238 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
239 X509V3_conf_err(value);
243 EXPORT_C int X509V3_get_value_int(CONF_VALUE *value, ASN1_INTEGER **aint)
246 if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
247 X509V3_conf_err(value);
259 EXPORT_C STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)
263 STACK_OF(CONF_VALUE) *values = NULL;
266 /* We are going to modify the line so copy it first */
267 linebuf = BUF_strdup(line);
270 /* Go through all characters */
271 for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
278 ntmp = strip_spaces(q);
280 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
284 } else if(c == ',') {
286 ntmp = strip_spaces(q);
289 printf("%s\n", ntmp);
292 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
295 X509V3_add_value(ntmp, NULL, &values);
303 vtmp = strip_spaces(q);
305 printf("%s\n", ntmp);
308 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
311 X509V3_add_value(ntmp, vtmp, &values);
319 if(state == HDR_VALUE) {
320 vtmp = strip_spaces(q);
322 printf("%s=%s\n", ntmp, vtmp);
325 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
328 X509V3_add_value(ntmp, vtmp, &values);
330 ntmp = strip_spaces(q);
332 printf("%s\n", ntmp);
335 X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
338 X509V3_add_value(ntmp, NULL, &values);
340 OPENSSL_free(linebuf);
344 OPENSSL_free(linebuf);
345 sk_CONF_VALUE_pop_free(values, X509V3_conf_free);
350 /* Delete leading and trailing spaces from a string */
351 static char *strip_spaces(char *name)
354 /* Skip over leading spaces */
356 while(*p && isspace((unsigned char)*p)) p++;
358 q = p + strlen(p) - 1;
359 while((q != p) && isspace((unsigned char)*q)) q--;
365 /* hex string utilities */
367 /* Given a buffer of length 'len' return a OPENSSL_malloc'ed string with its
369 * @@@ (Contents of buffer are always kept in ASCII, also on EBCDIC machines)
373 EXPORT_C char *hex_to_string(unsigned char *buffer, long len)
379 static char hexdig[] = "0123456789ABCDEF";
381 static const char hexdig[] = "0123456789ABCDEF";
383 if(!buffer || !len) return NULL;
384 if(!(tmp = OPENSSL_malloc(len * 3 + 1))) {
385 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
389 for(i = 0, p = buffer; i < len; i++,p++) {
390 *q++ = hexdig[(*p >> 4) & 0xf];
391 *q++ = hexdig[*p & 0xf];
395 #ifdef CHARSET_EBCDIC
396 ebcdic2ascii(tmp, tmp, q - tmp - 1);
402 /* Give a string of hex digits convert to
406 EXPORT_C unsigned char *string_to_hex(char *str, long *len)
408 unsigned char *hexbuf, *q;
409 unsigned char ch, cl, *p;
411 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
414 if(!(hexbuf = OPENSSL_malloc(strlen(str) >> 1))) goto err;
415 for(p = (unsigned char *)str, q = hexbuf; *p;) {
417 #ifdef CHARSET_EBCDIC
418 ch = os_toebcdic[ch];
420 if(ch == ':') continue;
422 #ifdef CHARSET_EBCDIC
423 cl = os_toebcdic[cl];
426 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
427 OPENSSL_free(hexbuf);
430 if(isupper(ch)) ch = tolower(ch);
431 if(isupper(cl)) cl = tolower(cl);
433 if((ch >= '0') && (ch <= '9')) ch -= '0';
434 else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
437 if((cl >= '0') && (cl <= '9')) cl -= '0';
438 else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
441 *q++ = (ch << 4) | cl;
444 if(len) *len = q - hexbuf;
449 if(hexbuf) OPENSSL_free(hexbuf);
450 X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
454 OPENSSL_free(hexbuf);
455 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
460 /* V2I name comparison function: returns zero if 'name' matches
464 EXPORT_C int name_cmp(const char *name, const char *cmp)
469 if((ret = strncmp(name, cmp, len))) return ret;
471 if(!c || (c=='.')) return 0;
475 static int sk_strcmp(const char * const *a, const char * const *b)
477 return strcmp(*a, *b);
480 EXPORT_C STACK *X509_get1_email(X509 *x)
484 gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);
485 ret = get_email(X509_get_subject_name(x), gens);
486 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
490 EXPORT_C STACK *X509_REQ_get1_email(X509_REQ *x)
493 STACK_OF(X509_EXTENSION) *exts;
495 exts = X509_REQ_get_extensions(x);
496 gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);
497 ret = get_email(X509_REQ_get_subject_name(x), gens);
498 sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);
499 sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
504 static STACK *get_email(X509_NAME *name, GENERAL_NAMES *gens)
508 ASN1_IA5STRING *email;
511 /* Now add any email address(es) to STACK */
513 /* First supplied X509_NAME */
514 while((i = X509_NAME_get_index_by_NID(name,
515 NID_pkcs9_emailAddress, i)) >= 0) {
516 ne = X509_NAME_get_entry(name, i);
517 email = X509_NAME_ENTRY_get_data(ne);
518 if(!append_ia5(&ret, email)) return NULL;
520 for(i = 0; i < sk_GENERAL_NAME_num(gens); i++)
522 gen = sk_GENERAL_NAME_value(gens, i);
523 if(gen->type != GEN_EMAIL) continue;
524 if(!append_ia5(&ret, gen->d.ia5)) return NULL;
529 static void str_free(void *str)
534 static int append_ia5(STACK **sk, ASN1_IA5STRING *email)
537 /* First some sanity checks */
538 if(email->type != V_ASN1_IA5STRING) return 1;
539 if(!email->data || !email->length) return 1;
540 if(!*sk) *sk = sk_new(sk_strcmp);
542 /* Don't add duplicates */
543 if(sk_find(*sk, (char *)email->data) != -1) return 1;
544 emtmp = BUF_strdup((char *)email->data);
545 if(!emtmp || !sk_push(*sk, emtmp)) {
546 X509_email_free(*sk);
553 EXPORT_C void X509_email_free(STACK *sk)
555 sk_pop_free(sk, str_free);
558 /* Convert IP addresses both IPv4 and IPv6 into an
559 * OCTET STRING compatible with RFC3280.
562 EXPORT_C ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)
564 unsigned char ipout[16];
565 ASN1_OCTET_STRING *ret;
568 /* If string contains a ':' assume IPv6 */
570 iplen = a2i_ipadd(ipout, ipasc);
575 ret = ASN1_OCTET_STRING_new();
578 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen))
580 ASN1_OCTET_STRING_free(ret);
586 EXPORT_C ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)
588 ASN1_OCTET_STRING *ret = NULL;
589 unsigned char ipout[32];
590 char *iptmp = NULL, *p;
592 p = strchr(ipasc,'/');
595 iptmp = BUF_strdup(ipasc);
598 p = iptmp + (p - ipasc);
601 iplen1 = a2i_ipadd(ipout, iptmp);
606 iplen2 = a2i_ipadd(ipout + iplen1, p);
611 if (!iplen2 || (iplen1 != iplen2))
614 ret = ASN1_OCTET_STRING_new();
617 if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))
626 ASN1_OCTET_STRING_free(ret);
631 EXPORT_C int a2i_ipadd(unsigned char *ipout, const char *ipasc)
633 /* If string contains a ':' assume IPv6 */
635 if (strchr(ipasc, ':'))
637 if (!ipv6_from_asc(ipout, ipasc))
643 if (!ipv4_from_asc(ipout, ipasc))
649 static int ipv4_from_asc(unsigned char *v4, const char *in)
652 if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4)
654 if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255)
655 || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255))
665 /* Temporary store for IPV6 output */
666 unsigned char tmp[16];
667 /* Total number of bytes in tmp */
669 /* The position of a zero (corresponding to '::') */
671 /* Number of zeroes */
676 static int ipv6_from_asc(unsigned char *v6, const char *in)
680 v6stat.zero_pos = -1;
682 /* Treat the IPv6 representation as a list of values
683 * separated by ':'. The presence of a '::' will parse
684 * as one, two or three zero length elements.
686 if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))
689 /* Now for some sanity checks */
691 if (v6stat.zero_pos == -1)
693 /* If no '::' must have exactly 16 bytes */
694 if (v6stat.total != 16)
699 /* If '::' must have less than 16 bytes */
700 if (v6stat.total == 16)
702 /* More than three zeroes is an error */
703 if (v6stat.zero_cnt > 3)
705 /* Can only have three zeroes if nothing else present */
706 else if (v6stat.zero_cnt == 3)
708 if (v6stat.total > 0)
711 /* Can only have two zeroes if at start or end */
712 else if (v6stat.zero_cnt == 2)
714 if ((v6stat.zero_pos != 0)
715 && (v6stat.zero_pos != v6stat.total))
719 /* Can only have one zero if *not* start or end */
721 if ((v6stat.zero_pos == 0)
722 || (v6stat.zero_pos == v6stat.total))
729 /* Copy initial part */
730 if (v6stat.zero_pos > 0)
731 memcpy(v6, v6stat.tmp, v6stat.zero_pos);
733 if (v6stat.total != 16)
734 memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);
735 /* Copy final part */
736 if (v6stat.total != v6stat.zero_pos)
737 memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,
738 v6stat.tmp + v6stat.zero_pos,
739 v6stat.total - v6stat.zero_pos);
744 static int ipv6_cb(const char *elem, int len, void *usr)
747 /* Error if 16 bytes written */
752 /* Zero length element, corresponds to '::' */
753 if (s->zero_pos == -1)
754 s->zero_pos = s->total;
755 /* If we've already got a :: its an error */
756 else if (s->zero_pos != s->total)
762 /* If more than 4 characters could be final a.b.c.d form */
765 /* Need at least 4 bytes left */
768 /* Must be end of string */
771 if (!ipv4_from_asc(s->tmp + s->total, elem))
777 if (!ipv6_hex(s->tmp + s->total, elem, len))
785 /* Convert a string of up to 4 hex digits into the corresponding
789 static int ipv6_hex(unsigned char *out, const char *in, int inlen)
792 unsigned int num = 0;
799 if ((c >= '0') && (c <= '9'))
801 else if ((c >= 'A') && (c <= 'F'))
803 else if ((c >= 'a') && (c <= 'f'))
814 EXPORT_C int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE)*dn_sk,
815 unsigned long chtype)
823 for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++)
825 v=sk_CONF_VALUE_value(dn_sk,i);
827 /* Skip past any leading X. X: X, etc to allow for
830 for(p = type; *p ; p++)
831 #ifndef CHARSET_EBCDIC
832 if ((*p == ':') || (*p == ',') || (*p == '.'))
834 if ((*p == os_toascii[':']) || (*p == os_toascii[',']) || (*p == os_toascii['.']))
841 #ifndef CHARSET_EBCDIC
844 if (*type == os_toascii['+'])
852 if (!X509_NAME_add_entry_by_txt(nm,type, chtype,
853 (unsigned char *) v->value,-1,-1,mval))