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 */
67 static char *strip_spaces(char *name
);
69 /* Add a CONF_VALUE name value pair to stack */
71 int X509V3_add_value(const char *name
, const char *value
, STACK
**extlist
)
73 CONF_VALUE
*vtmp
= NULL
;
74 char *tname
= NULL
, *tvalue
= NULL
;
75 if(name
&& !(tname
= BUF_strdup(name
))) goto err
;
76 if(value
&& !(tvalue
= BUF_strdup(value
))) goto err
;;
77 if(!(vtmp
= (CONF_VALUE
*)Malloc(sizeof(CONF_VALUE
)))) goto err
;
78 if(!*extlist
&& !(*extlist
= sk_new(NULL
))) goto err
;
82 if(!sk_push(*extlist
, (char *)vtmp
)) goto err
;
85 X509V3err(X509V3_F_X509V3_ADD_VALUE
,ERR_R_MALLOC_FAILURE
);
87 if(tname
) Free(tname
);
88 if(tvalue
) Free(tvalue
);
92 /* Free function for STACK of CONF_VALUE */
94 void X509V3_conf_free(CONF_VALUE
*conf
)
97 if(conf
->name
) Free(conf
->name
);
98 if(conf
->value
) Free(conf
->value
);
99 if(conf
->section
) Free(conf
->section
);
103 int X509V3_add_value_bool(const char *name
, int asn1_bool
, STACK
**extlist
)
105 if(asn1_bool
) return X509V3_add_value(name
, "TRUE", extlist
);
106 return X509V3_add_value(name
, "FALSE", extlist
);
109 int X509V3_add_value_bool_nf(char *name
, int asn1_bool
, STACK
**extlist
)
111 if(asn1_bool
) return X509V3_add_value(name
, "TRUE", extlist
);
116 char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD
*method
, ASN1_ENUMERATED
*a
)
118 BIGNUM
*bntmp
= NULL
;
121 if(!(bntmp
= ASN1_ENUMERATED_to_BN(a
, NULL
)) ||
122 !(strtmp
= BN_bn2dec(bntmp
)) )
123 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED
,ERR_R_MALLOC_FAILURE
);
128 char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, ASN1_INTEGER
*a
)
130 BIGNUM
*bntmp
= NULL
;
133 if(!(bntmp
= ASN1_INTEGER_to_BN(a
, NULL
)) ||
134 !(strtmp
= BN_bn2dec(bntmp
)) )
135 X509V3err(X509V3_F_I2S_ASN1_INTEGER
,ERR_R_MALLOC_FAILURE
);
140 ASN1_INTEGER
*s2i_ASN1_INTEGER(X509V3_EXT_METHOD
*method
, char *value
)
146 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_INVALID_NULL_VALUE
);
149 if(!BN_dec2bn(&bn
, value
)) {
150 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_BN_DEC2BN_ERROR
);
154 if(!(aint
= BN_to_ASN1_INTEGER(bn
, NULL
))) {
155 X509V3err(X509V3_F_S2I_ASN1_INTEGER
,X509V3_R_BN_TO_ASN1_INTEGER_ERROR
);
162 int X509V3_add_value_int(const char *name
, ASN1_INTEGER
*aint
,
168 if(!(strtmp
= i2s_ASN1_INTEGER(NULL
, aint
))) return 0;
169 ret
= X509V3_add_value(name
, strtmp
, extlist
);
174 int X509V3_get_value_bool(CONF_VALUE
*value
, int *asn1_bool
)
177 if(!(btmp
= value
->value
)) goto err
;
178 if(!strcmp(btmp
, "TRUE") || !strcmp(btmp
, "true")
179 || !strcmp(btmp
, "Y") || !strcmp(btmp
, "y")
180 || !strcmp(btmp
, "YES") || !strcmp(btmp
, "yes")) {
183 } else if(!strcmp(btmp
, "FALSE") || !strcmp(btmp
, "false")
184 || !strcmp(btmp
, "N") || !strcmp(btmp
, "n")
185 || !strcmp(btmp
, "NO") || !strcmp(btmp
, "no")) {
190 X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL
,X509V3_R_INVALID_BOOLEAN_STRING
);
191 X509V3_conf_err(value
);
195 int X509V3_get_value_int(CONF_VALUE
*value
, ASN1_INTEGER
**aint
)
198 if(!(itmp
= s2i_ASN1_INTEGER(NULL
, value
->value
))) {
199 X509V3_conf_err(value
);
211 STACK
*X509V3_parse_list(char *line
)
215 STACK
*values
= NULL
;
218 /* We are going to modify the line so copy it first */
219 linebuf
= BUF_strdup(line
);
222 /* Go through all characters */
223 for(p
= linebuf
, q
= linebuf
; (c
= *p
) && (c
!='\r') && (c
!='\n'); p
++) {
230 ntmp
= strip_spaces(q
);
232 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
236 } else if(c
== ',') {
238 ntmp
= strip_spaces(q
);
241 printf("%s\n", ntmp
);
244 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
247 X509V3_add_value(ntmp
, NULL
, &values
);
255 vtmp
= strip_spaces(q
);
257 printf("%s\n", ntmp
);
260 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_VALUE
);
263 X509V3_add_value(ntmp
, vtmp
, &values
);
271 if(state
== HDR_VALUE
) {
272 vtmp
= strip_spaces(q
);
274 printf("%s=%s\n", ntmp
, vtmp
);
277 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_VALUE
);
280 X509V3_add_value(ntmp
, vtmp
, &values
);
282 ntmp
= strip_spaces(q
);
284 printf("%s\n", ntmp
);
287 X509V3err(X509V3_F_X509V3_PARSE_LIST
, X509V3_R_INVALID_NULL_NAME
);
290 X509V3_add_value(ntmp
, NULL
, &values
);
297 sk_pop_free(values
, X509V3_conf_free
);
302 /* Delete leading and trailing spaces from a string */
303 static char *strip_spaces(char *name
)
306 /* Skip over leading spaces */
308 while(*p
&& isspace(*p
)) p
++;
310 q
= p
+ strlen(p
) - 1;
311 while((q
!= p
) && isspace(*q
)) q
--;
317 /* hex string utilities */
319 /* Given a buffer of length 'len' return a Malloc'ed string with its
323 char *hex_to_string(unsigned char *buffer
, long len
)
328 static char hexdig
[] = "0123456789ABCDEF";
329 if(!buffer
|| !len
) return NULL
;
330 if(!(tmp
= Malloc(len
* 3 + 1))) {
331 X509V3err(X509V3_F_HEX_TO_STRING
,ERR_R_MALLOC_FAILURE
);
335 for(i
= 0, p
= buffer
; i
< len
; i
++,p
++) {
336 *q
++ = hexdig
[(*p
>> 4) & 0xf];
337 *q
++ = hexdig
[*p
& 0xf];
344 /* Give a string of hex digits convert to
348 unsigned char *string_to_hex(char *str
, long *len
)
350 unsigned char *hexbuf
, *q
;
351 unsigned char ch
, cl
, *p
;
353 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_INVALID_NULL_ARGUMENT
);
356 if(!(hexbuf
= Malloc(strlen(str
) >> 1))) goto err
;
357 for(p
= (unsigned char *)str
, q
= hexbuf
; *p
;) {
359 if(ch
== ':') continue;
362 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_ODD_NUMBER_OF_DIGITS
);
366 if(isupper(ch
)) ch
= tolower(ch
);
367 if(isupper(cl
)) cl
= tolower(cl
);
369 if((ch
>= '0') && (ch
<= '9')) ch
-= '0';
370 else if ((ch
>= 'a') && (ch
<= 'f')) ch
-= 'a' - 10;
373 if((cl
>= '0') && (cl
<= '9')) cl
-= '0';
374 else if ((cl
>= 'a') && (cl
<= 'f')) cl
-= 'a' - 10;
377 *q
++ = (ch
<< 4) | cl
;
380 if(len
) *len
= q
- hexbuf
;
385 if(hexbuf
) Free(hexbuf
);
386 X509V3err(X509V3_F_STRING_TO_HEX
,ERR_R_MALLOC_FAILURE
);
391 X509V3err(X509V3_F_STRING_TO_HEX
,X509V3_R_ILLEGAL_HEX_DIGIT
);
396 /* V2I name comparison function: returns zero if 'name' matches
400 int name_cmp(const char *name
, const char *cmp
)
405 if((ret
= strncmp(name
, cmp
, len
))) return ret
;
407 if(!c
|| (c
=='.')) return 0;