Various changes to make this stuff compile under Win32 and VC++ with and
[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
61 #include <stdio.h>
62 #include <ctype.h>
63 #include "cryptlib.h"
64 #include "conf.h"
65 #include "x509v3.h"
66
67 static char *strip_spaces(char *name);
68
69 char *str_dup(str)
70 char *str;
71 {
72         char *tmp;
73         if(!(tmp = Malloc(strlen(str) + 1))) return NULL;
74         strcpy(tmp, str);
75         return tmp;
76 }
77
78 /* Add a CONF_VALUE name value pair to stack */
79
80 int X509V3_add_value(name, value, extlist)
81 char *name;
82 char *value;
83 STACK **extlist;
84 {
85         CONF_VALUE *vtmp = NULL;
86         char *tname = NULL, *tvalue = NULL;
87         if(name && !(tname = str_dup(name))) goto err;
88         if(value && !(tvalue = str_dup(value))) goto err;;
89         if(!(vtmp = (CONF_VALUE *)Malloc(sizeof(CONF_VALUE)))) goto err;
90         if(!*extlist && !(*extlist = sk_new(NULL))) goto err;
91         vtmp->section = NULL;
92         vtmp->name = tname;
93         vtmp->value = tvalue;
94         if(!sk_push(*extlist, (char *)vtmp)) goto err;
95         return 1;
96         err:
97         X509V3err(X509V3_F_X509V3_ADD_VALUE,ERR_R_MALLOC_FAILURE);
98         if(vtmp) Free(vtmp);
99         if(tname) Free(tname);
100         if(tvalue) Free(tvalue);
101         return 0;
102 }
103
104 /* Free function for STACK of CONF_VALUE */
105
106 void X509V3_conf_free(conf)
107 CONF_VALUE *conf;
108 {
109         if(!conf) return;
110         if(conf->name) Free(conf->name);
111         if(conf->value) Free(conf->value);
112         if(conf->section) Free(conf->section);
113         Free((char *)conf);
114 }
115
116 int X509V3_add_value_bool(name, asn1_bool, extlist)
117 char *name;
118 int asn1_bool;
119 STACK **extlist;
120 {
121         if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
122         return X509V3_add_value(name, "FALSE", extlist);
123 }
124
125 int X509V3_add_value_bool_nf(name, asn1_bool, extlist)
126 char *name;
127 int asn1_bool;
128 STACK **extlist;
129 {
130         if(asn1_bool) return X509V3_add_value(name, "TRUE", extlist);
131         return 1;
132 }
133
134
135 char *i2s_ASN1_ENUMERATED(method, a)
136 X509V3_EXT_METHOD *method;
137 ASN1_ENUMERATED *a;
138 {
139         BIGNUM *bntmp = NULL;
140         char *strtmp = NULL;
141         if(!a) return NULL;
142         if(!(bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) ||
143             !(strtmp = BN_bn2dec(bntmp)) )
144                 X509V3err(X509V3_F_I2S_ASN1_ENUMERATED,ERR_R_MALLOC_FAILURE);
145         BN_free(bntmp);
146         return strtmp;
147 }
148
149 char *i2s_ASN1_INTEGER(method, a)
150 X509V3_EXT_METHOD *method;
151 ASN1_INTEGER *a;
152 {
153         BIGNUM *bntmp = NULL;
154         char *strtmp = NULL;
155         if(!a) return NULL;
156         if(!(bntmp = ASN1_INTEGER_to_BN(a, NULL)) ||
157             !(strtmp = BN_bn2dec(bntmp)) )
158                 X509V3err(X509V3_F_I2S_ASN1_INTEGER,ERR_R_MALLOC_FAILURE);
159         BN_free(bntmp);
160         return strtmp;
161 }
162
163 int X509V3_add_value_int(name, aint, extlist)
164 char *name;
165 ASN1_INTEGER *aint;
166 STACK **extlist;
167 {
168         char *strtmp;
169         int ret;
170         if(!aint) return 1;
171         if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
172         ret = X509V3_add_value(name, strtmp, extlist);
173         Free(strtmp);
174         return ret;
175 }
176
177 int X509V3_get_value_bool(value, asn1_bool)
178 CONF_VALUE *value;
179 int *asn1_bool;
180 {
181         char *btmp;
182         if(!(btmp = value->value)) goto err;
183         if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
184                  || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
185                 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
186                 *asn1_bool = 0xff;
187                 return 1;
188         } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
189                  || !strcmp(btmp, "N") || !strcmp(btmp, "n")
190                 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
191                 *asn1_bool = 0;
192                 return 1;
193         }
194         err:
195         X509V3err(X509V3_F_X509V3_VALUE_GET_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
196         X509V3_conf_err(value);
197         return 0;
198 }
199
200 int X509V3_get_value_int(value, aint)
201 CONF_VALUE *value;
202 ASN1_INTEGER **aint;
203 {
204         BIGNUM *bn = NULL;
205         bn = BN_new();
206         if(!value->value) {
207                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_INVALID_NULL_VALUE);
208                 X509V3_conf_err(value);
209                 return 0;
210         }
211         if(!BN_dec2bn(&bn, value->value)) {
212                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_BN_DEC2BN_ERROR);
213                 X509V3_conf_err(value);
214                 return 0;
215         }
216
217         if(!(*aint = BN_to_ASN1_INTEGER(bn, NULL))) {
218                 X509V3err(X509V3_F_X509V3_GET_VALUE_INT,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
219                 X509V3_conf_err(value);
220                 return 0;
221         }
222         BN_free(bn);
223         return 1;
224 }
225
226 #define HDR_NAME        1
227 #define HDR_VALUE       2
228
229 /*#define DEBUG*/
230
231 STACK *X509V3_parse_list(line)
232 char *line;
233 {
234         char *p, *q, c;
235         char *ntmp, *vtmp;
236         STACK *values = NULL;
237         char *linebuf;
238         int state;
239         /* We are going to modify the line so copy it first */
240         linebuf = str_dup(line);
241         state = HDR_NAME;
242         ntmp = NULL;
243         /* Go through all characters */
244         for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
245
246                 switch(state) {
247                         case HDR_NAME:
248                         if(c == ':') {
249                                 state = HDR_VALUE;
250                                 *p = 0;
251                                 ntmp = strip_spaces(q);
252                                 if(!ntmp) {
253                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
254                                         goto err;
255                                 }
256                                 q = p + 1;
257                         } else if(c == ',') {
258                                 *p = 0;
259                                 ntmp = strip_spaces(q);
260                                 q = p + 1;
261 #ifdef DEBUG
262                                 printf("%s\n", ntmp);
263 #endif
264                                 if(!ntmp) {
265                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
266                                         goto err;
267                                 }
268                                 X509V3_add_value(ntmp, NULL, &values);
269                         }
270                         break ;
271
272                         case HDR_VALUE:
273                         if(c == ',') {
274                                 state = HDR_NAME;
275                                 *p = 0;
276                                 vtmp = strip_spaces(q);
277 #ifdef DEBUG
278                                 printf("%s\n", ntmp);
279 #endif
280                                 if(!vtmp) {
281                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
282                                         goto err;
283                                 }
284                                 X509V3_add_value(ntmp, vtmp, &values);
285                                 ntmp = NULL;
286                                 q = p + 1;
287                         }
288
289                 }
290         }
291
292         if(state == HDR_VALUE) {
293                 vtmp = strip_spaces(q);
294 #ifdef DEBUG
295                 printf("%s=%s\n", ntmp, vtmp);
296 #endif
297                 if(!vtmp) {
298                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
299                         goto err;
300                 }
301                 X509V3_add_value(ntmp, vtmp, &values);
302         } else {
303                 ntmp = strip_spaces(q);
304 #ifdef DEBUG
305                 printf("%s\n", ntmp);
306 #endif
307                 if(!ntmp) {
308                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
309                         goto err;
310                 }
311                 X509V3_add_value(ntmp, NULL, &values);
312         }
313 Free(linebuf);
314 return values;
315
316 err:
317 Free(linebuf);
318 sk_pop_free(values, X509V3_conf_free);
319 return NULL;
320
321 }
322
323 /* Delete leading and trailing spaces from a string */
324 static char *strip_spaces(name)
325 char *name;
326 {
327         char *p, *q;
328         /* Skip over leading spaces */
329         p = name;
330         while(*p && isspace(*p)) p++;
331         if(!*p) return NULL;
332         q = p + strlen(p) - 1;
333         while((q != p) && isspace(*q)) q--;
334         if(p != q) q[1] = 0;
335         if(!*p) return NULL;
336         return p;
337 }
338
339 /* hex string utilities */
340
341 /* Given a buffer of length 'len' return a Malloc'ed string with its
342  * hex representation
343  */
344
345 char *hex_to_string(buffer, len)
346 unsigned char *buffer;
347 long len;
348 {
349         char *tmp, *q;
350         unsigned char *p;
351         int i;
352         static char hexdig[] = "0123456789ABCDEF";
353         if(!buffer || !len) return NULL;
354         if(!(tmp = Malloc(len * 3 + 1))) {
355                 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
356                 return NULL;
357         }
358         q = tmp;
359         for(i = 0, p = buffer; i < len; i++,p++) {
360                 *q++ = hexdig[(*p >> 4) & 0xf];
361                 *q++ = hexdig[*p & 0xf];
362                 *q++ = ':';
363         }
364         q[-1] = 0;
365         return tmp;
366 }
367
368 /* Give a string of hex digits convert to
369  * a buffer
370  */
371
372 unsigned char *string_to_hex(str, len)
373 char *str;
374 long *len;
375 {
376         unsigned char *hexbuf, *q;
377         unsigned char ch, cl, *p;
378         if(!str) {
379                 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
380                 return NULL;
381         }
382         if(!(hexbuf = Malloc(strlen(str) >> 1))) goto err;
383         for(p = (unsigned char *)str, q = hexbuf; *p;) {
384                 ch = *p++;
385                 if(ch == ':') continue;
386                 cl = *p++;
387                 if(!cl) {
388                         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
389                         Free(hexbuf);
390                         return NULL;
391                 }
392                 if(isupper(ch)) ch = tolower(ch);
393                 if(isupper(cl)) cl = tolower(cl);
394
395                 if((ch >= '0') && (ch <= '9')) ch -= '0';
396                 else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
397                 else goto badhex;
398
399                 if((cl >= '0') && (cl <= '9')) cl -= '0';
400                 else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
401                 else goto badhex;
402
403                 *q++ = (ch << 4) | cl;
404         }
405
406         if(len) *len = q - hexbuf;
407
408         return hexbuf;
409
410         err:
411         if(hexbuf) Free(hexbuf);
412         X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
413         return NULL;
414
415         badhex:
416         Free(hexbuf);
417         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
418         return NULL;
419
420 }
421
422 /* V2I name comparison function: returns zero if 'name' matches
423  * cmp or cmp.*
424  */
425
426 int name_cmp(name, cmp)
427 char *name;
428 char *cmp;
429 {
430         int len, ret;
431         char c;
432         len = strlen(cmp);
433         if((ret = strncmp(name, cmp, len))) return ret;
434         c = name[len];
435         if(!c || (c=='.')) return 0;
436         return 1;
437 }