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