More extension code. Incomplete support for subject and issuer alt
[openssl.git] / crypto / x509v3 / v3_utl.c
1 /* v3_utl.c */
2 /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
3  * project 1999.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58 /* X509 v3 extension utilities */
59
60 #include <stdlib.h>
61 #include <ctype.h>
62 #include <pem.h>
63 #include <conf.h>
64 #include <err.h>
65 #include "x509v3.h"
66
67 static char * str_dup(char *str);
68 static char *strip_spaces(char *name);
69
70 static char *str_dup(str)
71 char *str;
72 {
73         char *tmp;
74         if(!(tmp = Malloc(strlen(str) + 1))) return NULL;
75         strcpy(tmp, str);
76         return tmp;
77 }
78
79 /* Add a CONF_VALUE name value pair to stack */
80
81 int X509V3_add_value(name, value, extlist)
82 char *name;
83 char *value;
84 STACK **extlist;
85 {
86         CONF_VALUE *vtmp = NULL;
87         char *tname = NULL, *tvalue = NULL;
88         if(name && !(tname = str_dup(name))) goto err;
89         if(value && !(tvalue = str_dup(value))) goto err;;
90         if(!(vtmp = (CONF_VALUE *)Malloc(sizeof(CONF_VALUE)))) goto err;
91         if(!*extlist && !(*extlist = sk_new(NULL))) goto err;
92         vtmp->section = NULL;
93         vtmp->name = tname;
94         vtmp->value = tvalue;
95         if(!sk_push(*extlist, (char *)vtmp)) goto err;
96         return 1;
97         err:
98         X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
99         if(vtmp) Free(vtmp);
100         if(tname) Free(tname);
101         if(tvalue) Free(tvalue);
102         return 0;
103 }
104
105 /* Free function for STACK of CONF_VALUE */
106
107 void X509V3_conf_free(conf)
108 CONF_VALUE *conf;
109 {
110         if(!conf) return;
111         if(conf->name) Free(conf->name);
112         if(conf->value) Free(conf->value);
113         if(conf->section) Free(conf->section);
114         Free((char *)conf);
115 }
116
117 int X509V3_add_value_bool(name, asn1_bool, extlist)
118 char *name;
119 int asn1_bool;
120 STACK **extlist;
121 {
122         if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
123         return X509V3_add_value(name, "FALSE", extlist);
124 }
125
126 int X509V3_add_value_bool_nf(name, asn1_bool, extlist)
127 char *name;
128 int asn1_bool;
129 STACK **extlist;
130 {
131         if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
132         return 1;
133 }
134
135 int X509V3_add_value_int(name, aint, extlist)
136 char *name;
137 ASN1_INTEGER *aint;
138 STACK **extlist;
139 {
140         BIGNUM *bntmp;
141         char *strtmp;
142         int ret;
143         if(!aint) return 1;
144         bntmp = ASN1_INTEGER_to_BN(aint, NULL);
145         strtmp = BN_bn2dec(bntmp);
146         ret = X509V3_add_value(name, strtmp, extlist);
147         BN_free(bntmp);
148         Free(strtmp);
149         return ret;
150 }
151
152 int X509V3_get_value_bool(value, asn1_bool)
153 CONF_VALUE *value;
154 int *asn1_bool;
155 {
156         char *btmp;
157         if(!(btmp = value->value)) goto err;
158         if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
159                  || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
160                 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
161                 *asn1_bool = 0xff;
162                 return 1;
163         } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
164                  || !strcmp(btmp, "N") || !strcmp(btmp, "n")
165                 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
166                 *asn1_bool = 0;
167                 return 1;
168         }
169         err:
170         X509V3err(X509V3_F_X509V3_VALUE_GET_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
171         X509V3_conf_err(value);
172         return 0;
173 }
174
175 int X509V3_get_value_int(value, aint)
176 CONF_VALUE *value;
177 ASN1_INTEGER **aint;
178 {
179         BIGNUM *bn = NULL;
180         bn = BN_new();
181         if(!value->value) {
182                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_INVALID_NULL_VALUE);
183                 X509V3_conf_err(value);
184                 return 0;
185         }
186         if(!BN_dec2bn(&bn, value->value)) {
187                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_BN_DEC2BN_ERROR);
188                 X509V3_conf_err(value);
189                 return 0;
190         }
191
192         if(!(*aint = BN_to_ASN1_INTEGER(bn, NULL))) {
193                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
194                 X509V3_conf_err(value);
195                 return 0;
196         }
197         BN_free(bn);
198         return 1;
199 }
200
201 #define HDR_NAME        1
202 #define HDR_VALUE       2
203
204 /*#define DEBUG*/
205
206 STACK *X509V3_parse_list(line)
207 char *line;
208 {
209         char *p, *q, c;
210         char *ntmp, *vtmp;
211         STACK *values = NULL;
212         char *linebuf;
213         int state;
214         /* We are going to modify the line so copy it first */
215         linebuf = str_dup(line);
216         state = HDR_NAME;
217         ntmp = NULL;
218         /* Go through all characters */
219         for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
220
221                 switch(state) {
222                         case HDR_NAME:
223                         if(c == ':') {
224                                 state = HDR_VALUE;
225                                 *p = 0;
226                                 ntmp = strip_spaces(q);
227                                 if(!ntmp) {
228                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
229                                         goto err;
230                                 }
231                                 q = p + 1;
232                         } else if(c == ',') {
233                                 *p = 0;
234                                 ntmp = strip_spaces(q);
235                                 q = p + 1;
236 #ifdef DEBUG
237                                 printf("%s\n", ntmp);
238 #endif
239                                 if(!ntmp) {
240                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
241                                         goto err;
242                                 }
243                                 X509V3_add_value(ntmp, NULL, &values);
244                         }
245                         break ;
246
247                         case HDR_VALUE:
248                         if(c == ',') {
249                                 state = HDR_NAME;
250                                 *p = 0;
251                                 vtmp = strip_spaces(q);
252 #ifdef DEBUG
253                                 printf("%s\n", ntmp);
254 #endif
255                                 if(!vtmp) {
256                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
257                                         goto err;
258                                 }
259                                 X509V3_add_value(ntmp, vtmp, &values);
260                                 ntmp = NULL;
261                                 q = p + 1;
262                         }
263
264                 }
265         }
266
267         if(state == HDR_VALUE) {
268                 vtmp = strip_spaces(q);
269 #ifdef DEBUG
270                 printf("%s=%s\n", ntmp, vtmp);
271 #endif
272                 if(!vtmp) {
273                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
274                         goto err;
275                 }
276                 X509V3_add_value(ntmp, vtmp, &values);
277         } else {
278                 ntmp = strip_spaces(q);
279 #ifdef DEBUG
280                 printf("%s\n", ntmp);
281 #endif
282                 if(!ntmp) {
283                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
284                         goto err;
285                 }
286                 X509V3_add_value(ntmp, NULL, &values);
287         }
288 Free(linebuf);
289 return values;
290
291 err:
292 Free(linebuf);
293 sk_pop_free(values, X509V3_conf_free);
294 return NULL;
295
296 }
297
298 /* Delete leading and trailing spaces from a string */
299 static char *strip_spaces(name)
300 char *name;
301 {
302         char *p, *q;
303         /* Skip over leading spaces */
304         p = name;
305         while(*p && isspace(*p)) p++;
306         if(!*p) return NULL;
307         q = p + strlen(p) - 1;
308         while((q != p) && isspace(*q)) q--;
309         if(p != q) q[1] = 0;
310         if(!*p) return NULL;
311         return p;
312 }
313
314 /* hex string utilities */
315
316 /* Given a buffer of length 'len' return a Malloc'ed string with its
317  * hex representation
318  */
319
320 char *hex_to_string(buffer, len)
321 unsigned char *buffer;
322 long len;
323 {
324         char *tmp, *q;
325         unsigned char *p;
326         int i;
327         static char hexdig[] = "0123456789ABCDEF";
328         if(!buffer || !len) return NULL;
329         if(!(tmp = Malloc(len * 3 + 1))) {
330                 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
331                 return NULL;
332         }
333         q = tmp;
334         for(i = 0, p = buffer; i < len; i++,p++) {
335                 *q++ = hexdig[(*p >> 4) & 0xf];
336                 *q++ = hexdig[*p & 0xf];
337                 *q++ = ':';
338         }
339         q[-1] = 0;
340         return tmp;
341 }
342
343 /* Give a string of hex digits convert to
344  * a buffer
345  */
346
347 unsigned char *string_to_hex(str, len)
348 char *str;
349 long *len;
350 {
351         unsigned char *hexbuf, *q;
352         unsigned char ch, cl, *p;
353         if(!str) {
354                 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
355                 return NULL;
356         }
357         if(!(hexbuf = Malloc(strlen(str) >> 1))) goto err;
358         for(p = (unsigned char *)str, q = hexbuf; *p;) {
359                 ch = *p++;
360                 if(ch == ':') continue;
361                 cl = *p++;
362                 if(!cl) {
363                         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
364                         Free(hexbuf);
365                         return NULL;
366                 }
367                 if(isupper(ch)) ch = tolower(ch);
368                 if(isupper(cl)) cl = tolower(cl);
369
370                 if((ch >= '0') && (ch <= '9')) ch -= '0';
371                 else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
372                 else goto badhex;
373
374                 if((cl >= '0') && (cl <= '9')) cl -= '0';
375                 else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
376                 else goto badhex;
377
378                 *q++ = (ch << 4) | cl;
379         }
380
381         if(len) *len = q - hexbuf;
382
383         return hexbuf;
384
385         err:
386         if(hexbuf) Free(hexbuf);
387         X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
388         return NULL;
389
390         badhex:
391         Free(hexbuf);
392         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
393         return NULL;
394
395 }