Massive constification.
[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 const char *name;
73 const 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 const 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 ASN1_INTEGER *s2i_ASN1_INTEGER(method, value)
155 X509V3_EXT_METHOD *method;
156 char *value;
157 {
158         BIGNUM *bn = NULL;
159         ASN1_INTEGER *aint;
160         bn = BN_new();
161         if(!value) {
162                 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_INVALID_NULL_VALUE);
163                 return 0;
164         }
165         if(!BN_dec2bn(&bn, value)) {
166                 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_DEC2BN_ERROR);
167                 return 0;
168         }
169
170         if(!(aint = BN_to_ASN1_INTEGER(bn, NULL))) {
171                 X509V3err(X509V3_F_S2I_ASN1_INTEGER,X509V3_R_BN_TO_ASN1_INTEGER_ERROR);
172                 return 0;
173         }
174         BN_free(bn);
175         return aint;
176 }
177
178 int X509V3_add_value_int(name, aint, extlist)
179 const char *name;
180 ASN1_INTEGER *aint;
181 STACK **extlist;
182 {
183         char *strtmp;
184         int ret;
185         if(!aint) return 1;
186         if(!(strtmp = i2s_ASN1_INTEGER(NULL, aint))) return 0;
187         ret = X509V3_add_value(name, strtmp, extlist);
188         Free(strtmp);
189         return ret;
190 }
191
192 int X509V3_get_value_bool(value, asn1_bool)
193 CONF_VALUE *value;
194 int *asn1_bool;
195 {
196         char *btmp;
197         if(!(btmp = value->value)) goto err;
198         if(!strcmp(btmp, "TRUE") || !strcmp(btmp, "true")
199                  || !strcmp(btmp, "Y") || !strcmp(btmp, "y")
200                 || !strcmp(btmp, "YES") || !strcmp(btmp, "yes")) {
201                 *asn1_bool = 0xff;
202                 return 1;
203         } else if(!strcmp(btmp, "FALSE") || !strcmp(btmp, "false")
204                  || !strcmp(btmp, "N") || !strcmp(btmp, "n")
205                 || !strcmp(btmp, "NO") || !strcmp(btmp, "no")) {
206                 *asn1_bool = 0;
207                 return 1;
208         }
209         err:
210         X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL,X509V3_R_INVALID_BOOLEAN_STRING);
211         X509V3_conf_err(value);
212         return 0;
213 }
214
215 int X509V3_get_value_int(value, aint)
216 CONF_VALUE *value;
217 ASN1_INTEGER **aint;
218 {
219         ASN1_INTEGER *itmp;
220         if(!(itmp = s2i_ASN1_INTEGER(NULL, value->value))) {
221                 X509V3_conf_err(value);
222                 return 0;
223         }
224         *aint = itmp;
225         return 1;
226 }
227
228 #define HDR_NAME        1
229 #define HDR_VALUE       2
230
231 /*#define DEBUG*/
232
233 STACK *X509V3_parse_list(line)
234 char *line;
235 {
236         char *p, *q, c;
237         char *ntmp, *vtmp;
238         STACK *values = NULL;
239         char *linebuf;
240         int state;
241         /* We are going to modify the line so copy it first */
242         linebuf = BUF_strdup(line);
243         state = HDR_NAME;
244         ntmp = NULL;
245         /* Go through all characters */
246         for(p = linebuf, q = linebuf; (c = *p) && (c!='\r') && (c!='\n'); p++) {
247
248                 switch(state) {
249                         case HDR_NAME:
250                         if(c == ':') {
251                                 state = HDR_VALUE;
252                                 *p = 0;
253                                 ntmp = strip_spaces(q);
254                                 if(!ntmp) {
255                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
256                                         goto err;
257                                 }
258                                 q = p + 1;
259                         } else if(c == ',') {
260                                 *p = 0;
261                                 ntmp = strip_spaces(q);
262                                 q = p + 1;
263 #ifdef DEBUG
264                                 printf("%s\n", ntmp);
265 #endif
266                                 if(!ntmp) {
267                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
268                                         goto err;
269                                 }
270                                 X509V3_add_value(ntmp, NULL, &values);
271                         }
272                         break ;
273
274                         case HDR_VALUE:
275                         if(c == ',') {
276                                 state = HDR_NAME;
277                                 *p = 0;
278                                 vtmp = strip_spaces(q);
279 #ifdef DEBUG
280                                 printf("%s\n", ntmp);
281 #endif
282                                 if(!vtmp) {
283                                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
284                                         goto err;
285                                 }
286                                 X509V3_add_value(ntmp, vtmp, &values);
287                                 ntmp = NULL;
288                                 q = p + 1;
289                         }
290
291                 }
292         }
293
294         if(state == HDR_VALUE) {
295                 vtmp = strip_spaces(q);
296 #ifdef DEBUG
297                 printf("%s=%s\n", ntmp, vtmp);
298 #endif
299                 if(!vtmp) {
300                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_VALUE);
301                         goto err;
302                 }
303                 X509V3_add_value(ntmp, vtmp, &values);
304         } else {
305                 ntmp = strip_spaces(q);
306 #ifdef DEBUG
307                 printf("%s\n", ntmp);
308 #endif
309                 if(!ntmp) {
310                         X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME);
311                         goto err;
312                 }
313                 X509V3_add_value(ntmp, NULL, &values);
314         }
315 Free(linebuf);
316 return values;
317
318 err:
319 Free(linebuf);
320 sk_pop_free(values, X509V3_conf_free);
321 return NULL;
322
323 }
324
325 /* Delete leading and trailing spaces from a string */
326 static char *strip_spaces(name)
327 char *name;
328 {
329         char *p, *q;
330         /* Skip over leading spaces */
331         p = name;
332         while(*p && isspace(*p)) p++;
333         if(!*p) return NULL;
334         q = p + strlen(p) - 1;
335         while((q != p) && isspace(*q)) q--;
336         if(p != q) q[1] = 0;
337         if(!*p) return NULL;
338         return p;
339 }
340
341 /* hex string utilities */
342
343 /* Given a buffer of length 'len' return a Malloc'ed string with its
344  * hex representation
345  */
346
347 char *hex_to_string(buffer, len)
348 unsigned char *buffer;
349 long len;
350 {
351         char *tmp, *q;
352         unsigned char *p;
353         int i;
354         static char hexdig[] = "0123456789ABCDEF";
355         if(!buffer || !len) return NULL;
356         if(!(tmp = Malloc(len * 3 + 1))) {
357                 X509V3err(X509V3_F_HEX_TO_STRING,ERR_R_MALLOC_FAILURE);
358                 return NULL;
359         }
360         q = tmp;
361         for(i = 0, p = buffer; i < len; i++,p++) {
362                 *q++ = hexdig[(*p >> 4) & 0xf];
363                 *q++ = hexdig[*p & 0xf];
364                 *q++ = ':';
365         }
366         q[-1] = 0;
367         return tmp;
368 }
369
370 /* Give a string of hex digits convert to
371  * a buffer
372  */
373
374 unsigned char *string_to_hex(str, len)
375 char *str;
376 long *len;
377 {
378         unsigned char *hexbuf, *q;
379         unsigned char ch, cl, *p;
380         if(!str) {
381                 X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_INVALID_NULL_ARGUMENT);
382                 return NULL;
383         }
384         if(!(hexbuf = Malloc(strlen(str) >> 1))) goto err;
385         for(p = (unsigned char *)str, q = hexbuf; *p;) {
386                 ch = *p++;
387                 if(ch == ':') continue;
388                 cl = *p++;
389                 if(!cl) {
390                         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ODD_NUMBER_OF_DIGITS);
391                         Free(hexbuf);
392                         return NULL;
393                 }
394                 if(isupper(ch)) ch = tolower(ch);
395                 if(isupper(cl)) cl = tolower(cl);
396
397                 if((ch >= '0') && (ch <= '9')) ch -= '0';
398                 else if ((ch >= 'a') && (ch <= 'f')) ch -= 'a' - 10;
399                 else goto badhex;
400
401                 if((cl >= '0') && (cl <= '9')) cl -= '0';
402                 else if ((cl >= 'a') && (cl <= 'f')) cl -= 'a' - 10;
403                 else goto badhex;
404
405                 *q++ = (ch << 4) | cl;
406         }
407
408         if(len) *len = q - hexbuf;
409
410         return hexbuf;
411
412         err:
413         if(hexbuf) Free(hexbuf);
414         X509V3err(X509V3_F_STRING_TO_HEX,ERR_R_MALLOC_FAILURE);
415         return NULL;
416
417         badhex:
418         Free(hexbuf);
419         X509V3err(X509V3_F_STRING_TO_HEX,X509V3_R_ILLEGAL_HEX_DIGIT);
420         return NULL;
421
422 }
423
424 /* V2I name comparison function: returns zero if 'name' matches
425  * cmp or cmp.*
426  */
427
428 int name_cmp(name, cmp)
429 const char *name;
430 const char *cmp;
431 {
432         int len, ret;
433         char c;
434         len = strlen(cmp);
435         if((ret = strncmp(name, cmp, len))) return ret;
436         c = name[len];
437         if(!c || (c=='.')) return 0;
438         return 1;
439 }