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