/src/openssl/crypto/x509/v3_utl.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * Copyright 1999-2025 The OpenSSL Project Authors. All Rights Reserved.  | 
3  |  |  *  | 
4  |  |  * Licensed under the Apache License 2.0 (the "License").  You may not use  | 
5  |  |  * this file except in compliance with the License.  You can obtain a copy  | 
6  |  |  * in the file LICENSE in the source distribution or at  | 
7  |  |  * https://www.openssl.org/source/license.html  | 
8  |  |  */  | 
9  |  |  | 
10  |  | /* X509 v3 extension utilities */  | 
11  |  |  | 
12  |  | #include "internal/e_os.h"  | 
13  |  | #include "internal/cryptlib.h"  | 
14  |  | #include <stdio.h>  | 
15  |  | #include <string.h>  | 
16  |  | #include "crypto/ctype.h"  | 
17  |  | #include <openssl/conf.h>  | 
18  |  | #include <openssl/crypto.h>  | 
19  |  | #include <openssl/x509v3.h>  | 
20  |  | #include "crypto/x509.h"  | 
21  |  | #include <openssl/bn.h>  | 
22  |  | #include "ext_dat.h"  | 
23  |  | #include "x509_local.h"  | 
24  |  |  | 
25  |  | static char *strip_spaces(char *name);  | 
26  |  | static int sk_strcmp(const char *const *a, const char *const *b);  | 
27  |  | static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name,  | 
28  |  |                                            GENERAL_NAMES *gens);  | 
29  |  | static void str_free(OPENSSL_STRING str);  | 
30  |  | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk,  | 
31  |  |                       const ASN1_IA5STRING *email);  | 
32  |  |  | 
33  |  | static int ipv4_from_asc(unsigned char *v4, const char *in);  | 
34  |  | static int ipv6_from_asc(unsigned char *v6, const char *in);  | 
35  |  | static int ipv6_cb(const char *elem, int len, void *usr);  | 
36  |  | static int ipv6_hex(unsigned char *out, const char *in, int inlen);  | 
37  |  |  | 
38  |  | /* Add a CONF_VALUE name value pair to stack */  | 
39  |  |  | 
40  |  | static int x509v3_add_len_value(const char *name, const char *value,  | 
41  |  |                                 size_t vallen, STACK_OF(CONF_VALUE) **extlist)  | 
42  | 0  | { | 
43  | 0  |     CONF_VALUE *vtmp = NULL;  | 
44  | 0  |     char *tname = NULL, *tvalue = NULL;  | 
45  | 0  |     int sk_allocated = (*extlist == NULL);  | 
46  |  | 
  | 
47  | 0  |     if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL)  | 
48  | 0  |         goto err;  | 
49  | 0  |     if (value != NULL) { | 
50  |  |         /* We don't allow embedded NUL characters */  | 
51  | 0  |         if (memchr(value, 0, vallen) != NULL)  | 
52  | 0  |             goto err;  | 
53  | 0  |         tvalue = OPENSSL_strndup(value, vallen);  | 
54  | 0  |         if (tvalue == NULL)  | 
55  | 0  |             goto err;  | 
56  | 0  |     }  | 
57  | 0  |     if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL)  | 
58  | 0  |         goto err;  | 
59  | 0  |     if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) { | 
60  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB);  | 
61  | 0  |         goto err;  | 
62  | 0  |     }  | 
63  | 0  |     vtmp->section = NULL;  | 
64  | 0  |     vtmp->name = tname;  | 
65  | 0  |     vtmp->value = tvalue;  | 
66  | 0  |     if (!sk_CONF_VALUE_push(*extlist, vtmp))  | 
67  | 0  |         goto err;  | 
68  | 0  |     return 1;  | 
69  | 0  |  err:  | 
70  | 0  |     if (sk_allocated) { | 
71  | 0  |         sk_CONF_VALUE_free(*extlist);  | 
72  | 0  |         *extlist = NULL;  | 
73  | 0  |     }  | 
74  | 0  |     OPENSSL_free(vtmp);  | 
75  | 0  |     OPENSSL_free(tname);  | 
76  | 0  |     OPENSSL_free(tvalue);  | 
77  | 0  |     return 0;  | 
78  | 0  | }  | 
79  |  |  | 
80  |  | int X509V3_add_value(const char *name, const char *value,  | 
81  |  |                      STACK_OF(CONF_VALUE) **extlist)  | 
82  | 0  | { | 
83  | 0  |     return x509v3_add_len_value(name, value,  | 
84  | 0  |                                 value != NULL ? strlen((const char *)value) : 0,  | 
85  | 0  |                                 extlist);  | 
86  | 0  | }  | 
87  |  |  | 
88  |  | int X509V3_add_value_uchar(const char *name, const unsigned char *value,  | 
89  |  |                            STACK_OF(CONF_VALUE) **extlist)  | 
90  | 0  | { | 
91  | 0  |     return x509v3_add_len_value(name, (const char *)value,  | 
92  | 0  |                                 value != NULL ? strlen((const char *)value) : 0,  | 
93  | 0  |                                 extlist);  | 
94  | 0  | }  | 
95  |  |  | 
96  |  | int x509v3_add_len_value_uchar(const char *name, const unsigned char *value,  | 
97  |  |                                size_t vallen, STACK_OF(CONF_VALUE) **extlist)  | 
98  | 0  | { | 
99  | 0  |     return x509v3_add_len_value(name, (const char *)value, vallen, extlist);  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | /* Free function for STACK_OF(CONF_VALUE) */  | 
103  |  |  | 
104  |  | void X509V3_conf_free(CONF_VALUE *conf)  | 
105  | 0  | { | 
106  | 0  |     if (!conf)  | 
107  | 0  |         return;  | 
108  | 0  |     OPENSSL_free(conf->name);  | 
109  | 0  |     OPENSSL_free(conf->value);  | 
110  | 0  |     OPENSSL_free(conf->section);  | 
111  | 0  |     OPENSSL_free(conf);  | 
112  | 0  | }  | 
113  |  |  | 
114  |  | int X509V3_add_value_bool(const char *name, int asn1_bool,  | 
115  |  |                           STACK_OF(CONF_VALUE) **extlist)  | 
116  | 0  | { | 
117  | 0  |     if (asn1_bool)  | 
118  | 0  |         return X509V3_add_value(name, "TRUE", extlist);  | 
119  | 0  |     return X509V3_add_value(name, "FALSE", extlist);  | 
120  | 0  | }  | 
121  |  |  | 
122  |  | int X509V3_add_value_bool_nf(const char *name, int asn1_bool,  | 
123  |  |                              STACK_OF(CONF_VALUE) **extlist)  | 
124  | 0  | { | 
125  | 0  |     if (asn1_bool)  | 
126  | 0  |         return X509V3_add_value(name, "TRUE", extlist);  | 
127  | 0  |     return 1;  | 
128  | 0  | }  | 
129  |  |  | 
130  |  | static char *bignum_to_string(const BIGNUM *bn)  | 
131  | 0  | { | 
132  | 0  |     char *tmp, *ret;  | 
133  | 0  |     size_t len;  | 
134  |  |  | 
135  |  |     /*  | 
136  |  |      * Display large numbers in hex and small numbers in decimal. Converting to  | 
137  |  |      * decimal takes quadratic time and is no more useful than hex for large  | 
138  |  |      * numbers.  | 
139  |  |      */  | 
140  | 0  |     if (BN_num_bits(bn) < 128)  | 
141  | 0  |         return BN_bn2dec(bn);  | 
142  |  |  | 
143  | 0  |     tmp = BN_bn2hex(bn);  | 
144  | 0  |     if (tmp == NULL)  | 
145  | 0  |         return NULL;  | 
146  |  |  | 
147  | 0  |     len = strlen(tmp) + 3;  | 
148  | 0  |     ret = OPENSSL_malloc(len);  | 
149  | 0  |     if (ret == NULL) { | 
150  | 0  |         OPENSSL_free(tmp);  | 
151  | 0  |         return NULL;  | 
152  | 0  |     }  | 
153  |  |  | 
154  |  |     /* Prepend "0x", but place it after the "-" if negative. */  | 
155  | 0  |     if (tmp[0] == '-') { | 
156  | 0  |         OPENSSL_strlcpy(ret, "-0x", len);  | 
157  | 0  |         OPENSSL_strlcat(ret, tmp + 1, len);  | 
158  | 0  |     } else { | 
159  | 0  |         OPENSSL_strlcpy(ret, "0x", len);  | 
160  | 0  |         OPENSSL_strlcat(ret, tmp, len);  | 
161  | 0  |     }  | 
162  | 0  |     OPENSSL_free(tmp);  | 
163  | 0  |     return ret;  | 
164  | 0  | }  | 
165  |  |  | 
166  |  | char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a)  | 
167  | 0  | { | 
168  | 0  |     BIGNUM *bntmp = NULL;  | 
169  | 0  |     char *strtmp = NULL;  | 
170  |  | 
  | 
171  | 0  |     if (!a)  | 
172  | 0  |         return NULL;  | 
173  | 0  |     if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL)  | 
174  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
175  | 0  |     else if ((strtmp = bignum_to_string(bntmp)) == NULL)  | 
176  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);  | 
177  | 0  |     BN_free(bntmp);  | 
178  | 0  |     return strtmp;  | 
179  | 0  | }  | 
180  |  |  | 
181  |  | char *i2s_ASN1_INTEGER(X509V3_EXT_METHOD *method, const ASN1_INTEGER *a)  | 
182  | 0  | { | 
183  | 0  |     BIGNUM *bntmp = NULL;  | 
184  | 0  |     char *strtmp = NULL;  | 
185  |  | 
  | 
186  | 0  |     if (!a)  | 
187  | 0  |         return NULL;  | 
188  | 0  |     if ((bntmp = ASN1_INTEGER_to_BN(a, NULL)) == NULL)  | 
189  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB);  | 
190  | 0  |     else if ((strtmp = bignum_to_string(bntmp)) == NULL)  | 
191  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB);  | 
192  | 0  |     BN_free(bntmp);  | 
193  | 0  |     return strtmp;  | 
194  | 0  | }  | 
195  |  |  | 
196  |  | ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value)  | 
197  | 0  | { | 
198  | 0  |     BIGNUM *bn = NULL;  | 
199  | 0  |     ASN1_INTEGER *aint;  | 
200  | 0  |     int isneg, ishex;  | 
201  | 0  |     int ret;  | 
202  |  | 
  | 
203  | 0  |     if (value == NULL) { | 
204  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);  | 
205  | 0  |         return NULL;  | 
206  | 0  |     }  | 
207  | 0  |     bn = BN_new();  | 
208  | 0  |     if (bn == NULL) { | 
209  | 0  |         ERR_raise(ERR_LIB_X509V3, ERR_R_BN_LIB);  | 
210  | 0  |         return NULL;  | 
211  | 0  |     }  | 
212  | 0  |     if (value[0] == '-') { | 
213  | 0  |         value++;  | 
214  | 0  |         isneg = 1;  | 
215  | 0  |     } else { | 
216  | 0  |         isneg = 0;  | 
217  | 0  |     }  | 
218  |  | 
  | 
219  | 0  |     if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { | 
220  | 0  |         value += 2;  | 
221  | 0  |         ishex = 1;  | 
222  | 0  |     } else { | 
223  | 0  |         ishex = 0;  | 
224  | 0  |     }  | 
225  |  | 
  | 
226  | 0  |     if (ishex)  | 
227  | 0  |         ret = BN_hex2bn(&bn, value);  | 
228  | 0  |     else  | 
229  | 0  |         ret = BN_dec2bn(&bn, value);  | 
230  |  | 
  | 
231  | 0  |     if (!ret || value[ret]) { | 
232  | 0  |         BN_free(bn);  | 
233  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_DEC2BN_ERROR);  | 
234  | 0  |         return NULL;  | 
235  | 0  |     }  | 
236  |  |  | 
237  | 0  |     if (isneg && BN_is_zero(bn))  | 
238  | 0  |         isneg = 0;  | 
239  |  | 
  | 
240  | 0  |     aint = BN_to_ASN1_INTEGER(bn, NULL);  | 
241  | 0  |     BN_free(bn);  | 
242  | 0  |     if (!aint) { | 
243  | 0  |         ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR);  | 
244  | 0  |         return NULL;  | 
245  | 0  |     }  | 
246  | 0  |     if (isneg)  | 
247  | 0  |         aint->type |= V_ASN1_NEG;  | 
248  | 0  |     return aint;  | 
249  | 0  | }  | 
250  |  |  | 
251  |  | int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint,  | 
252  |  |                          STACK_OF(CONF_VALUE) **extlist)  | 
253  | 0  | { | 
254  | 0  |     char *strtmp;  | 
255  | 0  |     int ret;  | 
256  |  | 
  | 
257  | 0  |     if (!aint)  | 
258  | 0  |         return 1;  | 
259  | 0  |     if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL)  | 
260  | 0  |         return 0;  | 
261  | 0  |     ret = X509V3_add_value(name, strtmp, extlist);  | 
262  | 0  |     OPENSSL_free(strtmp);  | 
263  | 0  |     return ret;  | 
264  | 0  | }  | 
265  |  |  | 
266  |  | int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool)  | 
267  | 0  | { | 
268  | 0  |     const char *btmp;  | 
269  |  | 
  | 
270  | 0  |     if ((btmp = value->value) == NULL)  | 
271  | 0  |         goto err;  | 
272  | 0  |     if (strcmp(btmp, "TRUE") == 0  | 
273  | 0  |         || strcmp(btmp, "true") == 0  | 
274  | 0  |         || strcmp(btmp, "Y") == 0  | 
275  | 0  |         || strcmp(btmp, "y") == 0  | 
276  | 0  |         || strcmp(btmp, "YES") == 0  | 
277  | 0  |         || strcmp(btmp, "yes") == 0) { | 
278  | 0  |         *asn1_bool = 0xff;  | 
279  | 0  |         return 1;  | 
280  | 0  |     }  | 
281  | 0  |     if (strcmp(btmp, "FALSE") == 0  | 
282  | 0  |         || strcmp(btmp, "false") == 0  | 
283  | 0  |         || strcmp(btmp, "N") == 0  | 
284  | 0  |         || strcmp(btmp, "n") == 0  | 
285  | 0  |         || strcmp(btmp, "NO") == 0  | 
286  | 0  |         || strcmp(btmp, "no") == 0) { | 
287  | 0  |         *asn1_bool = 0;  | 
288  | 0  |         return 1;  | 
289  | 0  |     }  | 
290  | 0  |  err:  | 
291  | 0  |     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_BOOLEAN_STRING);  | 
292  | 0  |     X509V3_conf_add_error_name_value(value);  | 
293  | 0  |     return 0;  | 
294  | 0  | }  | 
295  |  |  | 
296  |  | int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint)  | 
297  | 0  | { | 
298  | 0  |     ASN1_INTEGER *itmp;  | 
299  |  | 
  | 
300  | 0  |     if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) { | 
301  | 0  |         X509V3_conf_add_error_name_value(value);  | 
302  | 0  |         return 0;  | 
303  | 0  |     }  | 
304  | 0  |     *aint = itmp;  | 
305  | 0  |     return 1;  | 
306  | 0  | }  | 
307  |  |  | 
308  | 0  | #define HDR_NAME        1  | 
309  | 0  | #define HDR_VALUE       2  | 
310  |  |  | 
311  |  | /*  | 
312  |  |  * #define DEBUG  | 
313  |  |  */  | 
314  |  |  | 
315  |  | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line)  | 
316  | 0  | { | 
317  | 0  |     char *p, *q, c;  | 
318  | 0  |     char *ntmp, *vtmp;  | 
319  | 0  |     STACK_OF(CONF_VALUE) *values = NULL;  | 
320  | 0  |     char *linebuf;  | 
321  | 0  |     int state;  | 
322  |  |  | 
323  |  |     /* We are going to modify the line so copy it first */  | 
324  | 0  |     linebuf = OPENSSL_strdup(line);  | 
325  | 0  |     if (linebuf == NULL)  | 
326  | 0  |         goto err;  | 
327  | 0  |     state = HDR_NAME;  | 
328  | 0  |     ntmp = NULL;  | 
329  |  |     /* Go through all characters */  | 
330  | 0  |     for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n');  | 
331  | 0  |          p++) { | 
332  |  | 
  | 
333  | 0  |         switch (state) { | 
334  | 0  |         case HDR_NAME:  | 
335  | 0  |             if (c == ':') { | 
336  | 0  |                 state = HDR_VALUE;  | 
337  | 0  |                 *p = 0;  | 
338  | 0  |                 ntmp = strip_spaces(q);  | 
339  | 0  |                 if (!ntmp) { | 
340  | 0  |                     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);  | 
341  | 0  |                     goto err;  | 
342  | 0  |                 }  | 
343  | 0  |                 q = p + 1;  | 
344  | 0  |             } else if (c == ',') { | 
345  | 0  |                 *p = 0;  | 
346  | 0  |                 ntmp = strip_spaces(q);  | 
347  | 0  |                 q = p + 1;  | 
348  | 0  |                 if (!ntmp) { | 
349  | 0  |                     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);  | 
350  | 0  |                     goto err;  | 
351  | 0  |                 }  | 
352  | 0  |                 if (!X509V3_add_value(ntmp, NULL, &values)) { | 
353  | 0  |                     goto err;  | 
354  | 0  |                 }  | 
355  | 0  |             }  | 
356  | 0  |             break;  | 
357  |  |  | 
358  | 0  |         case HDR_VALUE:  | 
359  | 0  |             if (c == ',') { | 
360  | 0  |                 state = HDR_NAME;  | 
361  | 0  |                 *p = 0;  | 
362  | 0  |                 vtmp = strip_spaces(q);  | 
363  | 0  |                 if (!vtmp) { | 
364  | 0  |                     ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);  | 
365  | 0  |                     goto err;  | 
366  | 0  |                 }  | 
367  | 0  |                 if (!X509V3_add_value(ntmp, vtmp, &values)) { | 
368  | 0  |                     goto err;  | 
369  | 0  |                 }  | 
370  | 0  |                 ntmp = NULL;  | 
371  | 0  |                 q = p + 1;  | 
372  | 0  |             }  | 
373  |  | 
  | 
374  | 0  |         }  | 
375  | 0  |     }  | 
376  |  |  | 
377  | 0  |     if (state == HDR_VALUE) { | 
378  | 0  |         vtmp = strip_spaces(q);  | 
379  | 0  |         if (!vtmp) { | 
380  | 0  |             ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE);  | 
381  | 0  |             goto err;  | 
382  | 0  |         }  | 
383  | 0  |         if (!X509V3_add_value(ntmp, vtmp, &values)) { | 
384  | 0  |             goto err;  | 
385  | 0  |         }  | 
386  | 0  |     } else { | 
387  | 0  |         ntmp = strip_spaces(q);  | 
388  | 0  |         if (!ntmp) { | 
389  | 0  |             ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME);  | 
390  | 0  |             goto err;  | 
391  | 0  |         }  | 
392  | 0  |         if (!X509V3_add_value(ntmp, NULL, &values)) { | 
393  | 0  |             goto err;  | 
394  | 0  |         }  | 
395  | 0  |     }  | 
396  | 0  |     OPENSSL_free(linebuf);  | 
397  | 0  |     return values;  | 
398  |  |  | 
399  | 0  |  err:  | 
400  | 0  |     OPENSSL_free(linebuf);  | 
401  | 0  |     sk_CONF_VALUE_pop_free(values, X509V3_conf_free);  | 
402  | 0  |     return NULL;  | 
403  |  | 
  | 
404  | 0  | }  | 
405  |  |  | 
406  |  | /* Delete leading and trailing spaces from a string */  | 
407  |  | static char *strip_spaces(char *name)  | 
408  | 0  | { | 
409  | 0  |     char *p, *q;  | 
410  |  |  | 
411  |  |     /* Skip over leading spaces */  | 
412  | 0  |     p = name;  | 
413  | 0  |     while (*p && ossl_isspace(*p))  | 
414  | 0  |         p++;  | 
415  | 0  |     if (*p == '\0')  | 
416  | 0  |         return NULL;  | 
417  | 0  |     q = p + strlen(p) - 1;  | 
418  | 0  |     while ((q != p) && ossl_isspace(*q))  | 
419  | 0  |         q--;  | 
420  | 0  |     if (p != q)  | 
421  | 0  |         q[1] = 0;  | 
422  | 0  |     if (*p == '\0')  | 
423  | 0  |         return NULL;  | 
424  | 0  |     return p;  | 
425  | 0  | }  | 
426  |  |  | 
427  |  |  | 
428  |  | /*  | 
429  |  |  * V2I name comparison function: returns zero if 'name' matches cmp or cmp.*  | 
430  |  |  */  | 
431  |  |  | 
432  |  | int ossl_v3_name_cmp(const char *name, const char *cmp)  | 
433  | 0  | { | 
434  | 0  |     size_t len;  | 
435  | 0  |     int ret;  | 
436  | 0  |     char c;  | 
437  |  | 
  | 
438  | 0  |     len = strlen(cmp);  | 
439  | 0  |     if ((ret = strncmp(name, cmp, len)))  | 
440  | 0  |         return ret;  | 
441  | 0  |     c = name[len];  | 
442  | 0  |     if (!c || (c == '.'))  | 
443  | 0  |         return 0;  | 
444  | 0  |     return 1;  | 
445  | 0  | }  | 
446  |  |  | 
447  |  | static int sk_strcmp(const char *const *a, const char *const *b)  | 
448  | 0  | { | 
449  | 0  |     return strcmp(*a, *b);  | 
450  | 0  | }  | 
451  |  |  | 
452  |  | STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x)  | 
453  | 0  | { | 
454  | 0  |     GENERAL_NAMES *gens;  | 
455  | 0  |     STACK_OF(OPENSSL_STRING) *ret;  | 
456  |  | 
  | 
457  | 0  |     gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);  | 
458  | 0  |     ret = get_email(X509_get_subject_name(x), gens);  | 
459  | 0  |     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);  | 
460  | 0  |     return ret;  | 
461  | 0  | }  | 
462  |  |  | 
463  |  | STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x)  | 
464  | 0  | { | 
465  | 0  |     AUTHORITY_INFO_ACCESS *info;  | 
466  | 0  |     STACK_OF(OPENSSL_STRING) *ret = NULL;  | 
467  | 0  |     int i;  | 
468  |  | 
  | 
469  | 0  |     info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL);  | 
470  | 0  |     if (!info)  | 
471  | 0  |         return NULL;  | 
472  | 0  |     for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { | 
473  | 0  |         ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i);  | 
474  | 0  |         if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { | 
475  | 0  |             if (ad->location->type == GEN_URI) { | 
476  | 0  |                 if (!append_ia5  | 
477  | 0  |                     (&ret, ad->location->d.uniformResourceIdentifier))  | 
478  | 0  |                     break;  | 
479  | 0  |             }  | 
480  | 0  |         }  | 
481  | 0  |     }  | 
482  | 0  |     AUTHORITY_INFO_ACCESS_free(info);  | 
483  | 0  |     return ret;  | 
484  | 0  | }  | 
485  |  |  | 
486  |  | STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x)  | 
487  | 0  | { | 
488  | 0  |     GENERAL_NAMES *gens;  | 
489  | 0  |     STACK_OF(X509_EXTENSION) *exts;  | 
490  | 0  |     STACK_OF(OPENSSL_STRING) *ret;  | 
491  |  | 
  | 
492  | 0  |     exts = X509_REQ_get_extensions(x);  | 
493  | 0  |     gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL);  | 
494  | 0  |     ret = get_email(X509_REQ_get_subject_name(x), gens);  | 
495  | 0  |     sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free);  | 
496  | 0  |     sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);  | 
497  | 0  |     return ret;  | 
498  | 0  | }  | 
499  |  |  | 
500  |  | static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name,  | 
501  |  |                                            GENERAL_NAMES *gens)  | 
502  | 0  | { | 
503  | 0  |     STACK_OF(OPENSSL_STRING) *ret = NULL;  | 
504  | 0  |     X509_NAME_ENTRY *ne;  | 
505  | 0  |     const ASN1_IA5STRING *email;  | 
506  | 0  |     GENERAL_NAME *gen;  | 
507  | 0  |     int i = -1;  | 
508  |  |  | 
509  |  |     /* Now add any email address(es) to STACK */  | 
510  |  |     /* First supplied X509_NAME */  | 
511  | 0  |     while ((i = X509_NAME_get_index_by_NID(name,  | 
512  | 0  |                                            NID_pkcs9_emailAddress, i)) >= 0) { | 
513  | 0  |         ne = X509_NAME_get_entry(name, i);  | 
514  | 0  |         email = X509_NAME_ENTRY_get_data(ne);  | 
515  | 0  |         if (!append_ia5(&ret, email))  | 
516  | 0  |             return NULL;  | 
517  | 0  |     }  | 
518  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
519  | 0  |         gen = sk_GENERAL_NAME_value(gens, i);  | 
520  | 0  |         if (gen->type != GEN_EMAIL)  | 
521  | 0  |             continue;  | 
522  | 0  |         if (!append_ia5(&ret, gen->d.ia5))  | 
523  | 0  |             return NULL;  | 
524  | 0  |     }  | 
525  | 0  |     return ret;  | 
526  | 0  | }  | 
527  |  |  | 
528  |  | static void str_free(OPENSSL_STRING str)  | 
529  | 0  | { | 
530  | 0  |     OPENSSL_free(str);  | 
531  | 0  | }  | 
532  |  |  | 
533  |  | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk,  | 
534  |  |                       const ASN1_IA5STRING *email)  | 
535  | 0  | { | 
536  | 0  |     char *emtmp;  | 
537  |  |  | 
538  |  |     /* First some sanity checks */  | 
539  | 0  |     if (email->type != V_ASN1_IA5STRING)  | 
540  | 0  |         return 1;  | 
541  | 0  |     if (email->data == NULL || email->length == 0)  | 
542  | 0  |         return 1;  | 
543  | 0  |     if (memchr(email->data, 0, email->length) != NULL)  | 
544  | 0  |         return 1;  | 
545  | 0  |     if (*sk == NULL)  | 
546  | 0  |         *sk = sk_OPENSSL_STRING_new(sk_strcmp);  | 
547  | 0  |     if (*sk == NULL)  | 
548  | 0  |         return 0;  | 
549  |  |  | 
550  | 0  |     emtmp = OPENSSL_strndup((char *)email->data, email->length);  | 
551  | 0  |     if (emtmp == NULL) { | 
552  | 0  |         X509_email_free(*sk);  | 
553  | 0  |         *sk = NULL;  | 
554  | 0  |         return 0;  | 
555  | 0  |     }  | 
556  |  |  | 
557  |  |     /* Don't add duplicates */  | 
558  | 0  |     if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { | 
559  | 0  |         OPENSSL_free(emtmp);  | 
560  | 0  |         return 1;  | 
561  | 0  |     }  | 
562  | 0  |     if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { | 
563  | 0  |         OPENSSL_free(emtmp); /* free on push failure */  | 
564  | 0  |         X509_email_free(*sk);  | 
565  | 0  |         *sk = NULL;  | 
566  | 0  |         return 0;  | 
567  | 0  |     }  | 
568  | 0  |     return 1;  | 
569  | 0  | }  | 
570  |  |  | 
571  |  | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk)  | 
572  | 0  | { | 
573  | 0  |     sk_OPENSSL_STRING_pop_free(sk, str_free);  | 
574  | 0  | }  | 
575  |  |  | 
576  |  | typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len,  | 
577  |  |                          const unsigned char *subject, size_t subject_len,  | 
578  |  |                          unsigned int flags);  | 
579  |  |  | 
580  |  | /* Skip pattern prefix to match "wildcard" subject */  | 
581  |  | static void skip_prefix(const unsigned char **p, size_t *plen,  | 
582  |  |                         size_t subject_len,  | 
583  |  |                         unsigned int flags)  | 
584  | 0  | { | 
585  | 0  |     const unsigned char *pattern = *p;  | 
586  | 0  |     size_t pattern_len = *plen;  | 
587  |  |  | 
588  |  |     /*  | 
589  |  |      * If subject starts with a leading '.' followed by more octets, and  | 
590  |  |      * pattern is longer, compare just an equal-length suffix with the  | 
591  |  |      * full subject (starting at the '.'), provided the prefix contains  | 
592  |  |      * no NULs.  | 
593  |  |      */  | 
594  | 0  |     if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0)  | 
595  | 0  |         return;  | 
596  |  |  | 
597  | 0  |     while (pattern_len > subject_len && *pattern) { | 
598  | 0  |         if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) &&  | 
599  | 0  |             *pattern == '.')  | 
600  | 0  |             break;  | 
601  | 0  |         ++pattern;  | 
602  | 0  |         --pattern_len;  | 
603  | 0  |     }  | 
604  |  |  | 
605  |  |     /* Skip if entire prefix acceptable */  | 
606  | 0  |     if (pattern_len == subject_len) { | 
607  | 0  |         *p = pattern;  | 
608  | 0  |         *plen = pattern_len;  | 
609  | 0  |     }  | 
610  | 0  | }  | 
611  |  |  | 
612  |  | /* Compare while ASCII ignoring case. */  | 
613  |  | static int equal_nocase(const unsigned char *pattern, size_t pattern_len,  | 
614  |  |                         const unsigned char *subject, size_t subject_len,  | 
615  |  |                         unsigned int flags)  | 
616  | 0  | { | 
617  | 0  |     skip_prefix(&pattern, &pattern_len, subject_len, flags);  | 
618  | 0  |     if (pattern_len != subject_len)  | 
619  | 0  |         return 0;  | 
620  | 0  |     while (pattern_len != 0) { | 
621  | 0  |         unsigned char l = *pattern;  | 
622  | 0  |         unsigned char r = *subject;  | 
623  |  |  | 
624  |  |         /* The pattern must not contain NUL characters. */  | 
625  | 0  |         if (l == 0)  | 
626  | 0  |             return 0;  | 
627  | 0  |         if (l != r) { | 
628  | 0  |             if ('A' <= l && l <= 'Z') | 
629  | 0  |                 l = (l - 'A') + 'a';  | 
630  | 0  |             if ('A' <= r && r <= 'Z') | 
631  | 0  |                 r = (r - 'A') + 'a';  | 
632  | 0  |             if (l != r)  | 
633  | 0  |                 return 0;  | 
634  | 0  |         }  | 
635  | 0  |         ++pattern;  | 
636  | 0  |         ++subject;  | 
637  | 0  |         --pattern_len;  | 
638  | 0  |     }  | 
639  | 0  |     return 1;  | 
640  | 0  | }  | 
641  |  |  | 
642  |  | /* Compare using memcmp. */  | 
643  |  | static int equal_case(const unsigned char *pattern, size_t pattern_len,  | 
644  |  |                       const unsigned char *subject, size_t subject_len,  | 
645  |  |                       unsigned int flags)  | 
646  | 0  | { | 
647  | 0  |     skip_prefix(&pattern, &pattern_len, subject_len, flags);  | 
648  | 0  |     if (pattern_len != subject_len)  | 
649  | 0  |         return 0;  | 
650  | 0  |     return !memcmp(pattern, subject, pattern_len);  | 
651  | 0  | }  | 
652  |  |  | 
653  |  | /*  | 
654  |  |  * RFC 5280, section 7.5, requires that only the domain is compared in a  | 
655  |  |  * case-insensitive manner.  | 
656  |  |  */  | 
657  |  | static int equal_email(const unsigned char *a, size_t a_len,  | 
658  |  |                        const unsigned char *b, size_t b_len,  | 
659  |  |                        unsigned int unused_flags)  | 
660  | 0  | { | 
661  | 0  |     size_t i = a_len;  | 
662  |  | 
  | 
663  | 0  |     if (a_len != b_len)  | 
664  | 0  |         return 0;  | 
665  |  |     /*  | 
666  |  |      * We search backwards for the '@' character, so that we do not have to  | 
667  |  |      * deal with quoted local-parts.  The domain part is compared in a  | 
668  |  |      * case-insensitive manner.  | 
669  |  |      */  | 
670  | 0  |     while (i > 0) { | 
671  | 0  |         --i;  | 
672  | 0  |         if (a[i] == '@' || b[i] == '@') { | 
673  | 0  |             if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0))  | 
674  | 0  |                 return 0;  | 
675  | 0  |             break;  | 
676  | 0  |         }  | 
677  | 0  |     }  | 
678  | 0  |     if (i == 0)  | 
679  | 0  |         i = a_len;  | 
680  | 0  |     return equal_case(a, i, b, i, 0);  | 
681  | 0  | }  | 
682  |  |  | 
683  |  | /*  | 
684  |  |  * Compare the prefix and suffix with the subject, and check that the  | 
685  |  |  * characters in-between are valid.  | 
686  |  |  */  | 
687  |  | static int wildcard_match(const unsigned char *prefix, size_t prefix_len,  | 
688  |  |                           const unsigned char *suffix, size_t suffix_len,  | 
689  |  |                           const unsigned char *subject, size_t subject_len,  | 
690  |  |                           unsigned int flags)  | 
691  | 0  | { | 
692  | 0  |     const unsigned char *wildcard_start;  | 
693  | 0  |     const unsigned char *wildcard_end;  | 
694  | 0  |     const unsigned char *p;  | 
695  | 0  |     int allow_multi = 0;  | 
696  | 0  |     int allow_idna = 0;  | 
697  |  | 
  | 
698  | 0  |     if (subject_len < prefix_len + suffix_len)  | 
699  | 0  |         return 0;  | 
700  | 0  |     if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags))  | 
701  | 0  |         return 0;  | 
702  | 0  |     wildcard_start = subject + prefix_len;  | 
703  | 0  |     wildcard_end = subject + (subject_len - suffix_len);  | 
704  | 0  |     if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags))  | 
705  | 0  |         return 0;  | 
706  |  |     /*  | 
707  |  |      * If the wildcard makes up the entire first label, it must match at  | 
708  |  |      * least one character.  | 
709  |  |      */  | 
710  | 0  |     if (prefix_len == 0 && *suffix == '.') { | 
711  | 0  |         if (wildcard_start == wildcard_end)  | 
712  | 0  |             return 0;  | 
713  | 0  |         allow_idna = 1;  | 
714  | 0  |         if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS)  | 
715  | 0  |             allow_multi = 1;  | 
716  | 0  |     }  | 
717  |  |     /* IDNA labels cannot match partial wildcards */  | 
718  | 0  |     if (!allow_idna &&  | 
719  | 0  |         subject_len >= 4 && HAS_CASE_PREFIX((const char *)subject, "xn--"))  | 
720  | 0  |         return 0;  | 
721  |  |     /* The wildcard may match a literal '*' */  | 
722  | 0  |     if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*')  | 
723  | 0  |         return 1;  | 
724  |  |     /*  | 
725  |  |      * Check that the part matched by the wildcard contains only  | 
726  |  |      * permitted characters and only matches a single label unless  | 
727  |  |      * allow_multi is set.  | 
728  |  |      */  | 
729  | 0  |     for (p = wildcard_start; p != wildcard_end; ++p)  | 
730  | 0  |         if (!(('0' <= *p && *p <= '9') || | 
731  | 0  |               ('A' <= *p && *p <= 'Z') || | 
732  | 0  |               ('a' <= *p && *p <= 'z') || | 
733  | 0  |               *p == '-' || (allow_multi && *p == '.')))  | 
734  | 0  |             return 0;  | 
735  | 0  |     return 1;  | 
736  | 0  | }  | 
737  |  |  | 
738  | 0  | #define LABEL_START     (1 << 0)  | 
739  |  | #define LABEL_END       (1 << 1)  | 
740  | 0  | #define LABEL_HYPHEN    (1 << 2)  | 
741  | 0  | #define LABEL_IDNA      (1 << 3)  | 
742  |  |  | 
743  |  | static const unsigned char *valid_star(const unsigned char *p, size_t len,  | 
744  |  |                                        unsigned int flags)  | 
745  | 0  | { | 
746  | 0  |     const unsigned char *star = 0;  | 
747  | 0  |     size_t i;  | 
748  | 0  |     int state = LABEL_START;  | 
749  | 0  |     int dots = 0;  | 
750  |  | 
  | 
751  | 0  |     for (i = 0; i < len; ++i) { | 
752  |  |         /*  | 
753  |  |          * Locate first and only legal wildcard, either at the start  | 
754  |  |          * or end of a non-IDNA first and not final label.  | 
755  |  |          */  | 
756  | 0  |         if (p[i] == '*') { | 
757  | 0  |             int atstart = (state & LABEL_START);  | 
758  | 0  |             int atend = (i == len - 1 || p[i + 1] == '.');  | 
759  |  |             /*-  | 
760  |  |              * At most one wildcard per pattern.  | 
761  |  |              * No wildcards in IDNA labels.  | 
762  |  |              * No wildcards after the first label.  | 
763  |  |              */  | 
764  | 0  |             if (star != NULL || (state & LABEL_IDNA) != 0 || dots)  | 
765  | 0  |                 return NULL;  | 
766  |  |             /* Only full-label '*.example.com' wildcards? */  | 
767  | 0  |             if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS)  | 
768  | 0  |                 && (!atstart || !atend))  | 
769  | 0  |                 return NULL;  | 
770  |  |             /* No 'foo*bar' wildcards */  | 
771  | 0  |             if (!atstart && !atend)  | 
772  | 0  |                 return NULL;  | 
773  | 0  |             star = &p[i];  | 
774  | 0  |             state &= ~LABEL_START;  | 
775  | 0  |         } else if (('a' <= p[i] && p[i] <= 'z') | 
776  | 0  |                    || ('A' <= p[i] && p[i] <= 'Z') | 
777  | 0  |                    || ('0' <= p[i] && p[i] <= '9')) { | 
778  | 0  |             if ((state & LABEL_START) != 0  | 
779  | 0  |                 && len - i >= 4 && HAS_CASE_PREFIX((const char *)&p[i], "xn--"))  | 
780  | 0  |                 state |= LABEL_IDNA;  | 
781  | 0  |             state &= ~(LABEL_HYPHEN | LABEL_START);  | 
782  | 0  |         } else if (p[i] == '.') { | 
783  | 0  |             if ((state & (LABEL_HYPHEN | LABEL_START)) != 0)  | 
784  | 0  |                 return NULL;  | 
785  | 0  |             state = LABEL_START;  | 
786  | 0  |             ++dots;  | 
787  | 0  |         } else if (p[i] == '-') { | 
788  |  |             /* no domain/subdomain starts with '-' */  | 
789  | 0  |             if ((state & LABEL_START) != 0)  | 
790  | 0  |                 return NULL;  | 
791  | 0  |             state |= LABEL_HYPHEN;  | 
792  | 0  |         } else { | 
793  | 0  |             return NULL;  | 
794  | 0  |         }  | 
795  | 0  |     }  | 
796  |  |  | 
797  |  |     /*  | 
798  |  |      * The final label must not end in a hyphen or ".", and  | 
799  |  |      * there must be at least two dots after the star.  | 
800  |  |      */  | 
801  | 0  |     if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2)  | 
802  | 0  |         return NULL;  | 
803  | 0  |     return star;  | 
804  | 0  | }  | 
805  |  |  | 
806  |  | /* Compare using wildcards. */  | 
807  |  | static int equal_wildcard(const unsigned char *pattern, size_t pattern_len,  | 
808  |  |                           const unsigned char *subject, size_t subject_len,  | 
809  |  |                           unsigned int flags)  | 
810  | 0  | { | 
811  | 0  |     const unsigned char *star = NULL;  | 
812  |  |  | 
813  |  |     /*  | 
814  |  |      * Subject names starting with '.' can only match a wildcard pattern  | 
815  |  |      * via a subject sub-domain pattern suffix match.  | 
816  |  |      */  | 
817  | 0  |     if (!(subject_len > 1 && subject[0] == '.'))  | 
818  | 0  |         star = valid_star(pattern, pattern_len, flags);  | 
819  | 0  |     if (star == NULL)  | 
820  | 0  |         return equal_nocase(pattern, pattern_len,  | 
821  | 0  |                             subject, subject_len, flags);  | 
822  | 0  |     return wildcard_match(pattern, star - pattern,  | 
823  | 0  |                           star + 1, (pattern + pattern_len) - star - 1,  | 
824  | 0  |                           subject, subject_len, flags);  | 
825  | 0  | }  | 
826  |  |  | 
827  |  | /*  | 
828  |  |  * Compare an ASN1_STRING to a supplied string. If they match return 1. If  | 
829  |  |  * cmp_type > 0 only compare if string matches the type, otherwise convert it  | 
830  |  |  * to UTF8.  | 
831  |  |  */  | 
832  |  |  | 
833  |  | static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal,  | 
834  |  |                            unsigned int flags, const char *b, size_t blen,  | 
835  |  |                            char **peername)  | 
836  | 0  | { | 
837  | 0  |     int rv = 0;  | 
838  |  | 
  | 
839  | 0  |     if (!a->data || !a->length)  | 
840  | 0  |         return 0;  | 
841  | 0  |     if (cmp_type > 0) { | 
842  | 0  |         if (cmp_type != a->type)  | 
843  | 0  |             return 0;  | 
844  | 0  |         if (cmp_type == V_ASN1_IA5STRING)  | 
845  | 0  |             rv = equal(a->data, a->length, (unsigned char *)b, blen, flags);  | 
846  | 0  |         else if (a->length == (int)blen && !memcmp(a->data, b, blen))  | 
847  | 0  |             rv = 1;  | 
848  | 0  |         if (rv > 0 && peername != NULL) { | 
849  | 0  |             *peername = OPENSSL_strndup((char *)a->data, a->length);  | 
850  | 0  |             if (*peername == NULL)  | 
851  | 0  |                 return -1;  | 
852  | 0  |         }  | 
853  | 0  |     } else { | 
854  | 0  |         int astrlen;  | 
855  | 0  |         unsigned char *astr;  | 
856  | 0  |         astrlen = ASN1_STRING_to_UTF8(&astr, a);  | 
857  | 0  |         if (astrlen < 0) { | 
858  |  |             /*  | 
859  |  |              * -1 could be an internal malloc failure or a decoding error from  | 
860  |  |              * malformed input; we can't distinguish.  | 
861  |  |              */  | 
862  | 0  |             return -1;  | 
863  | 0  |         }  | 
864  | 0  |         rv = equal(astr, astrlen, (unsigned char *)b, blen, flags);  | 
865  | 0  |         if (rv > 0 && peername != NULL) { | 
866  | 0  |             *peername = OPENSSL_strndup((char *)astr, astrlen);  | 
867  | 0  |             if (*peername == NULL) { | 
868  | 0  |                 OPENSSL_free(astr);  | 
869  | 0  |                 return -1;  | 
870  | 0  |             }  | 
871  | 0  |         }  | 
872  | 0  |         OPENSSL_free(astr);  | 
873  | 0  |     }  | 
874  | 0  |     return rv;  | 
875  | 0  | }  | 
876  |  |  | 
877  |  | static int do_x509_check(X509 *x, const char *chk, size_t chklen,  | 
878  |  |                          unsigned int flags, int check_type, char **peername)  | 
879  | 0  | { | 
880  | 0  |     GENERAL_NAMES *gens = NULL;  | 
881  | 0  |     const X509_NAME *name = NULL;  | 
882  | 0  |     int i;  | 
883  | 0  |     int cnid = NID_undef;  | 
884  | 0  |     int alt_type;  | 
885  | 0  |     int san_present = 0;  | 
886  | 0  |     int rv = 0;  | 
887  | 0  |     equal_fn equal;  | 
888  |  |  | 
889  |  |     /* See below, this flag is internal-only */  | 
890  | 0  |     flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS;  | 
891  | 0  |     if (check_type == GEN_EMAIL) { | 
892  | 0  |         cnid = NID_pkcs9_emailAddress;  | 
893  | 0  |         alt_type = V_ASN1_IA5STRING;  | 
894  | 0  |         equal = equal_email;  | 
895  | 0  |     } else if (check_type == GEN_DNS) { | 
896  | 0  |         cnid = NID_commonName;  | 
897  |  |         /* Implicit client-side DNS sub-domain pattern */  | 
898  | 0  |         if (chklen > 1 && chk[0] == '.')  | 
899  | 0  |             flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS;  | 
900  | 0  |         alt_type = V_ASN1_IA5STRING;  | 
901  | 0  |         if (flags & X509_CHECK_FLAG_NO_WILDCARDS)  | 
902  | 0  |             equal = equal_nocase;  | 
903  | 0  |         else  | 
904  | 0  |             equal = equal_wildcard;  | 
905  | 0  |     } else { | 
906  | 0  |         alt_type = V_ASN1_OCTET_STRING;  | 
907  | 0  |         equal = equal_case;  | 
908  | 0  |     }  | 
909  |  | 
  | 
910  | 0  |     if (chklen == 0)  | 
911  | 0  |         chklen = strlen(chk);  | 
912  |  | 
  | 
913  | 0  |     gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL);  | 
914  | 0  |     if (gens) { | 
915  | 0  |         for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
916  | 0  |             GENERAL_NAME *gen;  | 
917  | 0  |             ASN1_STRING *cstr;  | 
918  |  | 
  | 
919  | 0  |             gen = sk_GENERAL_NAME_value(gens, i);  | 
920  | 0  |             switch (gen->type) { | 
921  | 0  |             default:  | 
922  | 0  |                 continue;  | 
923  | 0  |             case GEN_OTHERNAME:  | 
924  | 0  |     switch (OBJ_obj2nid(gen->d.otherName->type_id)) { | 
925  | 0  |                 default:  | 
926  | 0  |                     continue;  | 
927  | 0  |                 case NID_id_on_SmtpUTF8Mailbox:  | 
928  |  |                     /*-  | 
929  |  |                      * https://datatracker.ietf.org/doc/html/rfc8398#section-3  | 
930  |  |                      *  | 
931  |  |                      *   Due to name constraint compatibility reasons described  | 
932  |  |                      *   in Section 6, SmtpUTF8Mailbox subjectAltName MUST NOT  | 
933  |  |                      *   be used unless the local-part of the email address  | 
934  |  |                      *   contains non-ASCII characters. When the local-part is  | 
935  |  |                      *   ASCII, rfc822Name subjectAltName MUST be used instead  | 
936  |  |                      *   of SmtpUTF8Mailbox. This is compatible with legacy  | 
937  |  |                      *   software that supports only rfc822Name (and not  | 
938  |  |                      *   SmtpUTF8Mailbox). [...]  | 
939  |  |                      *  | 
940  |  |                      *   SmtpUTF8Mailbox is encoded as UTF8String.  | 
941  |  |                      *  | 
942  |  |                      * If it is not a UTF8String then that is unexpected, and  | 
943  |  |                      * we ignore the invalid SAN (neither set san_present nor  | 
944  |  |                      * consider it a candidate for equality).  This does mean  | 
945  |  |                      * that the subject CN may be considered, as would be the  | 
946  |  |                      * case when the malformed SmtpUtf8Mailbox SAN is instead  | 
947  |  |                      * simply absent.  | 
948  |  |                      *  | 
949  |  |                      * When CN-ID matching is not desirable, applications can  | 
950  |  |                      * choose to turn it off, doing so is at this time a best  | 
951  |  |                      * practice.  | 
952  |  |                      */  | 
953  | 0  |                     if (check_type != GEN_EMAIL  | 
954  | 0  |                         || gen->d.otherName->value->type != V_ASN1_UTF8STRING)  | 
955  | 0  |                         continue;  | 
956  | 0  |                     alt_type = 0;  | 
957  | 0  |                     cstr = gen->d.otherName->value->value.utf8string;  | 
958  | 0  |                     break;  | 
959  | 0  |                 }  | 
960  | 0  |                 break;  | 
961  | 0  |             case GEN_EMAIL:  | 
962  | 0  |                 if (check_type != GEN_EMAIL)  | 
963  | 0  |                     continue;  | 
964  | 0  |                 cstr = gen->d.rfc822Name;  | 
965  | 0  |                 break;  | 
966  | 0  |             case GEN_DNS:  | 
967  | 0  |                 if (check_type != GEN_DNS)  | 
968  | 0  |                     continue;  | 
969  | 0  |                 cstr = gen->d.dNSName;  | 
970  | 0  |                 break;  | 
971  | 0  |             case GEN_IPADD:  | 
972  | 0  |                 if (check_type != GEN_IPADD)  | 
973  | 0  |                     continue;  | 
974  | 0  |                 cstr = gen->d.iPAddress;  | 
975  | 0  |                 break;  | 
976  | 0  |             }  | 
977  | 0  |             san_present = 1;  | 
978  |  |             /* Positive on success, negative on error! */  | 
979  | 0  |             if ((rv = do_check_string(cstr, alt_type, equal, flags,  | 
980  | 0  |                                       chk, chklen, peername)) != 0)  | 
981  | 0  |                 break;  | 
982  | 0  |         }  | 
983  | 0  |         GENERAL_NAMES_free(gens);  | 
984  | 0  |         if (rv != 0)  | 
985  | 0  |             return rv;  | 
986  | 0  |         if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT))  | 
987  | 0  |             return 0;  | 
988  | 0  |     }  | 
989  |  |  | 
990  |  |     /* We're done if CN-ID is not pertinent */  | 
991  | 0  |     if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT))  | 
992  | 0  |         return 0;  | 
993  |  |  | 
994  | 0  |     i = -1;  | 
995  | 0  |     name = X509_get_subject_name(x);  | 
996  | 0  |     while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) { | 
997  | 0  |         const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i);  | 
998  | 0  |         const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne);  | 
999  |  |  | 
1000  |  |         /* Positive on success, negative on error! */  | 
1001  | 0  |         if ((rv = do_check_string(str, -1, equal, flags,  | 
1002  | 0  |                                   chk, chklen, peername)) != 0)  | 
1003  | 0  |             return rv;  | 
1004  | 0  |     }  | 
1005  | 0  |     return 0;  | 
1006  | 0  | }  | 
1007  |  |  | 
1008  |  | int X509_check_host(X509 *x, const char *chk, size_t chklen,  | 
1009  |  |                     unsigned int flags, char **peername)  | 
1010  | 0  | { | 
1011  | 0  |     if (chk == NULL)  | 
1012  | 0  |         return -2;  | 
1013  |  |     /*  | 
1014  |  |      * Embedded NULs are disallowed, except as the last character of a  | 
1015  |  |      * string of length 2 or more (tolerate caller including terminating  | 
1016  |  |      * NUL in string length).  | 
1017  |  |      */  | 
1018  | 0  |     if (chklen == 0)  | 
1019  | 0  |         chklen = strlen(chk);  | 
1020  | 0  |     else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))  | 
1021  | 0  |         return -2;  | 
1022  | 0  |     if (chklen > 1 && chk[chklen - 1] == '\0')  | 
1023  | 0  |         --chklen;  | 
1024  | 0  |     return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername);  | 
1025  | 0  | }  | 
1026  |  |  | 
1027  |  | int X509_check_email(X509 *x, const char *chk, size_t chklen,  | 
1028  |  |                      unsigned int flags)  | 
1029  | 0  | { | 
1030  | 0  |     if (chk == NULL)  | 
1031  | 0  |         return -2;  | 
1032  |  |     /*  | 
1033  |  |      * Embedded NULs are disallowed, except as the last character of a  | 
1034  |  |      * string of length 2 or more (tolerate caller including terminating  | 
1035  |  |      * NUL in string length).  | 
1036  |  |      */  | 
1037  | 0  |     if (chklen == 0)  | 
1038  | 0  |         chklen = strlen((char *)chk);  | 
1039  | 0  |     else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen))  | 
1040  | 0  |         return -2;  | 
1041  | 0  |     if (chklen > 1 && chk[chklen - 1] == '\0')  | 
1042  | 0  |         --chklen;  | 
1043  | 0  |     return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL);  | 
1044  | 0  | }  | 
1045  |  |  | 
1046  |  | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen,  | 
1047  |  |                   unsigned int flags)  | 
1048  | 0  | { | 
1049  | 0  |     if (chk == NULL)  | 
1050  | 0  |         return -2;  | 
1051  | 0  |     return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL);  | 
1052  | 0  | }  | 
1053  |  |  | 
1054  |  | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags)  | 
1055  | 0  | { | 
1056  | 0  |     unsigned char ipout[16];  | 
1057  | 0  |     size_t iplen;  | 
1058  |  | 
  | 
1059  | 0  |     if (ipasc == NULL)  | 
1060  | 0  |         return -2;  | 
1061  | 0  |     iplen = (size_t)ossl_a2i_ipadd(ipout, ipasc);  | 
1062  | 0  |     if (iplen == 0)  | 
1063  | 0  |         return -2;  | 
1064  | 0  |     return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL);  | 
1065  | 0  | }  | 
1066  |  |  | 
1067  |  | char *ossl_ipaddr_to_asc(unsigned char *p, int len)  | 
1068  | 0  | { | 
1069  |  |     /*  | 
1070  |  |      * 40 is enough space for the longest IPv6 address + nul terminator byte  | 
1071  |  |      * XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX\0  | 
1072  |  |      */  | 
1073  | 0  |     char buf[40], *out;  | 
1074  | 0  |     int i = 0, remain = 0, bytes = 0;  | 
1075  |  | 
  | 
1076  | 0  |     switch (len) { | 
1077  | 0  |     case 4: /* IPv4 */  | 
1078  | 0  |         BIO_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);  | 
1079  | 0  |         break;  | 
1080  | 0  |     case 16: /* IPv6 */  | 
1081  | 0  |         for (out = buf, i = 8, remain = sizeof(buf);  | 
1082  | 0  |              i-- > 0 && bytes >= 0;  | 
1083  | 0  |              remain -= bytes, out += bytes) { | 
1084  | 0  |             const char *template = (i > 0 ? "%X:" : "%X");  | 
1085  |  | 
  | 
1086  | 0  |             bytes = BIO_snprintf(out, remain, template, p[0] << 8 | p[1]);  | 
1087  | 0  |             p += 2;  | 
1088  | 0  |         }  | 
1089  | 0  |         break;  | 
1090  | 0  |     default:  | 
1091  | 0  |         BIO_snprintf(buf, sizeof(buf), "<invalid length=%d>", len);  | 
1092  | 0  |         break;  | 
1093  | 0  |     }  | 
1094  | 0  |     return OPENSSL_strdup(buf);  | 
1095  | 0  | }  | 
1096  |  |  | 
1097  |  | /*  | 
1098  |  |  * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible  | 
1099  |  |  * with RFC3280.  | 
1100  |  |  */  | 
1101  |  |  | 
1102  |  | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc)  | 
1103  | 0  | { | 
1104  | 0  |     unsigned char ipout[16];  | 
1105  | 0  |     ASN1_OCTET_STRING *ret;  | 
1106  | 0  |     int iplen;  | 
1107  |  |  | 
1108  |  |     /* If string contains a ':' assume IPv6 */  | 
1109  |  | 
  | 
1110  | 0  |     iplen = ossl_a2i_ipadd(ipout, ipasc);  | 
1111  |  | 
  | 
1112  | 0  |     if (!iplen)  | 
1113  | 0  |         return NULL;  | 
1114  |  |  | 
1115  | 0  |     ret = ASN1_OCTET_STRING_new();  | 
1116  | 0  |     if (ret == NULL)  | 
1117  | 0  |         return NULL;  | 
1118  | 0  |     if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { | 
1119  | 0  |         ASN1_OCTET_STRING_free(ret);  | 
1120  | 0  |         return NULL;  | 
1121  | 0  |     }  | 
1122  | 0  |     return ret;  | 
1123  | 0  | }  | 
1124  |  |  | 
1125  |  | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc)  | 
1126  | 0  | { | 
1127  | 0  |     ASN1_OCTET_STRING *ret = NULL;  | 
1128  | 0  |     unsigned char ipout[32];  | 
1129  | 0  |     char *iptmp = NULL, *p;  | 
1130  | 0  |     int iplen1, iplen2;  | 
1131  |  | 
  | 
1132  | 0  |     p = strchr(ipasc, '/');  | 
1133  | 0  |     if (p == NULL)  | 
1134  | 0  |         return NULL;  | 
1135  | 0  |     iptmp = OPENSSL_strdup(ipasc);  | 
1136  | 0  |     if (iptmp == NULL)  | 
1137  | 0  |         return NULL;  | 
1138  | 0  |     p = iptmp + (p - ipasc);  | 
1139  | 0  |     *p++ = 0;  | 
1140  |  | 
  | 
1141  | 0  |     iplen1 = ossl_a2i_ipadd(ipout, iptmp);  | 
1142  |  | 
  | 
1143  | 0  |     if (!iplen1)  | 
1144  | 0  |         goto err;  | 
1145  |  |  | 
1146  | 0  |     iplen2 = ossl_a2i_ipadd(ipout + iplen1, p);  | 
1147  |  | 
  | 
1148  | 0  |     OPENSSL_free(iptmp);  | 
1149  | 0  |     iptmp = NULL;  | 
1150  |  | 
  | 
1151  | 0  |     if (!iplen2 || (iplen1 != iplen2))  | 
1152  | 0  |         goto err;  | 
1153  |  |  | 
1154  | 0  |     ret = ASN1_OCTET_STRING_new();  | 
1155  | 0  |     if (ret == NULL)  | 
1156  | 0  |         goto err;  | 
1157  | 0  |     if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2))  | 
1158  | 0  |         goto err;  | 
1159  |  |  | 
1160  | 0  |     return ret;  | 
1161  |  |  | 
1162  | 0  |  err:  | 
1163  | 0  |     OPENSSL_free(iptmp);  | 
1164  | 0  |     ASN1_OCTET_STRING_free(ret);  | 
1165  | 0  |     return NULL;  | 
1166  | 0  | }  | 
1167  |  |  | 
1168  |  | int ossl_a2i_ipadd(unsigned char *ipout, const char *ipasc)  | 
1169  | 0  | { | 
1170  |  |     /* If string contains a ':' assume IPv6 */  | 
1171  |  | 
  | 
1172  | 0  |     if (strchr(ipasc, ':')) { | 
1173  | 0  |         if (!ipv6_from_asc(ipout, ipasc))  | 
1174  | 0  |             return 0;  | 
1175  | 0  |         return 16;  | 
1176  | 0  |     } else { | 
1177  | 0  |         if (!ipv4_from_asc(ipout, ipasc))  | 
1178  | 0  |             return 0;  | 
1179  | 0  |         return 4;  | 
1180  | 0  |     }  | 
1181  | 0  | }  | 
1182  |  |  | 
1183  |  | /*  | 
1184  |  |  * get_ipv4_component consumes one IPv4 component, terminated by either '.' or  | 
1185  |  |  * the end of the string, from *str. On success, it returns one, sets *out  | 
1186  |  |  * to the component, and advances *str to the first unconsumed character. On  | 
1187  |  |  * invalid input, it returns zero.  | 
1188  |  |  */  | 
1189  | 0  | static int get_ipv4_component(uint8_t *out_byte, const char **str) { | 
1190  |  |     /* Store a slightly larger intermediary so the overflow check is easier. */  | 
1191  | 0  |     uint32_t out = 0;  | 
1192  |  | 
  | 
1193  | 0  |     for (;;) { | 
1194  | 0  |         if (!ossl_isdigit(**str)) { | 
1195  | 0  |             return 0;  | 
1196  | 0  |         }  | 
1197  | 0  |         out = (out * 10) + (**str - '0');  | 
1198  | 0  |         if (out > 255) { | 
1199  |  |             /* Components must be 8-bit. */  | 
1200  | 0  |             return 0;  | 
1201  | 0  |         }  | 
1202  | 0  |         (*str)++;  | 
1203  | 0  |         if ((**str) == '.' || (**str) == '\0') { | 
1204  | 0  |             *out_byte = (uint8_t)out;  | 
1205  | 0  |             return 1;  | 
1206  | 0  |         }  | 
1207  | 0  |         if (out == 0) { | 
1208  |  |       /* Reject extra leading zeros. Parsers sometimes treat them as  | 
1209  |  |              * octal, so accepting them would misinterpret input.  | 
1210  |  |              */  | 
1211  | 0  |             return 0;  | 
1212  | 0  |         }  | 
1213  | 0  |     }  | 
1214  | 0  | }  | 
1215  |  |  | 
1216  |  | /*  | 
1217  |  |  * get_ipv4_dot consumes a '.' from *str and advances it. It returns one on  | 
1218  |  |  * success and zero if *str does not point to a '.'.  | 
1219  |  |  */  | 
1220  |  | static int get_ipv4_dot(const char **str)  | 
1221  | 0  | { | 
1222  | 0  |     if (**str != '.') { | 
1223  | 0  |         return 0;  | 
1224  | 0  |     }  | 
1225  | 0  |     (*str)++;  | 
1226  | 0  |     return 1;  | 
1227  | 0  | }  | 
1228  |  |  | 
1229  |  | static int ipv4_from_asc(unsigned char *v4, const char *in)  | 
1230  | 0  | { | 
1231  | 0  |     if (!get_ipv4_component(&v4[0], &in) || !get_ipv4_dot(&in)  | 
1232  | 0  |         || !get_ipv4_component(&v4[1], &in) || !get_ipv4_dot(&in)  | 
1233  | 0  |         || !get_ipv4_component(&v4[2], &in) || !get_ipv4_dot(&in)  | 
1234  | 0  |         || !get_ipv4_component(&v4[3], &in) || *in != '\0') { | 
1235  | 0  |          return 0;  | 
1236  | 0  |     }  | 
1237  | 0  |     return 1;  | 
1238  | 0  | }  | 
1239  |  |  | 
1240  |  | typedef struct { | 
1241  |  |     /* Temporary store for IPV6 output */  | 
1242  |  |     unsigned char tmp[16];  | 
1243  |  |     /* Total number of bytes in tmp */  | 
1244  |  |     int total;  | 
1245  |  |     /* The position of a zero (corresponding to '::') */  | 
1246  |  |     int zero_pos;  | 
1247  |  |     /* Number of zeroes */  | 
1248  |  |     int zero_cnt;  | 
1249  |  | } IPV6_STAT;  | 
1250  |  |  | 
1251  |  | static int ipv6_from_asc(unsigned char *v6, const char *in)  | 
1252  | 0  | { | 
1253  | 0  |     IPV6_STAT v6stat;  | 
1254  |  | 
  | 
1255  | 0  |     v6stat.total = 0;  | 
1256  | 0  |     v6stat.zero_pos = -1;  | 
1257  | 0  |     v6stat.zero_cnt = 0;  | 
1258  |  |     /*  | 
1259  |  |      * Treat the IPv6 representation as a list of values separated by ':'.  | 
1260  |  |      * The presence of a '::' will parse as one, two or three zero length  | 
1261  |  |      * elements.  | 
1262  |  |      */  | 
1263  | 0  |     if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat))  | 
1264  | 0  |         return 0;  | 
1265  |  |  | 
1266  |  |     /* Now for some sanity checks */  | 
1267  |  |  | 
1268  | 0  |     if (v6stat.zero_pos == -1) { | 
1269  |  |         /* If no '::' must have exactly 16 bytes */  | 
1270  | 0  |         if (v6stat.total != 16)  | 
1271  | 0  |             return 0;  | 
1272  | 0  |     } else { | 
1273  |  |         /* If '::' must have less than 16 bytes */  | 
1274  | 0  |         if (v6stat.total == 16)  | 
1275  | 0  |             return 0;  | 
1276  |  |         /* More than three zeroes is an error */  | 
1277  | 0  |         if (v6stat.zero_cnt > 3) { | 
1278  | 0  |             return 0;  | 
1279  |  |         /* Can only have three zeroes if nothing else present */  | 
1280  | 0  |         } else if (v6stat.zero_cnt == 3) { | 
1281  | 0  |             if (v6stat.total > 0)  | 
1282  | 0  |                 return 0;  | 
1283  | 0  |         } else if (v6stat.zero_cnt == 2) { | 
1284  |  |             /* Can only have two zeroes if at start or end */  | 
1285  | 0  |             if ((v6stat.zero_pos != 0)  | 
1286  | 0  |                 && (v6stat.zero_pos != v6stat.total))  | 
1287  | 0  |                 return 0;  | 
1288  | 0  |         } else { | 
1289  |  |             /* Can only have one zero if *not* start or end */  | 
1290  | 0  |             if ((v6stat.zero_pos == 0)  | 
1291  | 0  |                 || (v6stat.zero_pos == v6stat.total))  | 
1292  | 0  |                 return 0;  | 
1293  | 0  |         }  | 
1294  | 0  |     }  | 
1295  |  |  | 
1296  |  |     /* Format result */  | 
1297  |  |  | 
1298  | 0  |     if (v6stat.zero_pos >= 0) { | 
1299  |  |         /* Copy initial part */  | 
1300  | 0  |         memcpy(v6, v6stat.tmp, v6stat.zero_pos);  | 
1301  |  |         /* Zero middle */  | 
1302  | 0  |         memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total);  | 
1303  |  |         /* Copy final part */  | 
1304  | 0  |         if (v6stat.total != v6stat.zero_pos)  | 
1305  | 0  |             memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total,  | 
1306  | 0  |                    v6stat.tmp + v6stat.zero_pos,  | 
1307  | 0  |                    v6stat.total - v6stat.zero_pos);  | 
1308  | 0  |     } else { | 
1309  | 0  |         memcpy(v6, v6stat.tmp, 16);  | 
1310  | 0  |     }  | 
1311  |  | 
  | 
1312  | 0  |     return 1;  | 
1313  | 0  | }  | 
1314  |  |  | 
1315  |  | static int ipv6_cb(const char *elem, int len, void *usr)  | 
1316  | 0  | { | 
1317  | 0  |     IPV6_STAT *s = usr;  | 
1318  |  |  | 
1319  |  |     /* Error if 16 bytes written */  | 
1320  | 0  |     if (s->total == 16)  | 
1321  | 0  |         return 0;  | 
1322  | 0  |     if (len == 0) { | 
1323  |  |         /* Zero length element, corresponds to '::' */  | 
1324  | 0  |         if (s->zero_pos == -1)  | 
1325  | 0  |             s->zero_pos = s->total;  | 
1326  |  |         /* If we've already got a :: its an error */  | 
1327  | 0  |         else if (s->zero_pos != s->total)  | 
1328  | 0  |             return 0;  | 
1329  | 0  |         s->zero_cnt++;  | 
1330  | 0  |     } else { | 
1331  |  |         /* If more than 4 characters could be final a.b.c.d form */  | 
1332  | 0  |         if (len > 4) { | 
1333  |  |             /* Need at least 4 bytes left */  | 
1334  | 0  |             if (s->total > 12)  | 
1335  | 0  |                 return 0;  | 
1336  |  |             /* Must be end of string */  | 
1337  | 0  |             if (elem[len])  | 
1338  | 0  |                 return 0;  | 
1339  | 0  |             if (!ipv4_from_asc(s->tmp + s->total, elem))  | 
1340  | 0  |                 return 0;  | 
1341  | 0  |             s->total += 4;  | 
1342  | 0  |         } else { | 
1343  | 0  |             if (!ipv6_hex(s->tmp + s->total, elem, len))  | 
1344  | 0  |                 return 0;  | 
1345  | 0  |             s->total += 2;  | 
1346  | 0  |         }  | 
1347  | 0  |     }  | 
1348  | 0  |     return 1;  | 
1349  | 0  | }  | 
1350  |  |  | 
1351  |  | /*  | 
1352  |  |  * Convert a string of up to 4 hex digits into the corresponding IPv6 form.  | 
1353  |  |  */  | 
1354  |  |  | 
1355  |  | static int ipv6_hex(unsigned char *out, const char *in, int inlen)  | 
1356  | 0  | { | 
1357  | 0  |     unsigned char c;  | 
1358  | 0  |     unsigned int num = 0;  | 
1359  | 0  |     int x;  | 
1360  |  | 
  | 
1361  | 0  |     if (inlen > 4)  | 
1362  | 0  |         return 0;  | 
1363  | 0  |     while (inlen--) { | 
1364  | 0  |         c = *in++;  | 
1365  | 0  |         num <<= 4;  | 
1366  | 0  |         x = OPENSSL_hexchar2int(c);  | 
1367  | 0  |         if (x < 0)  | 
1368  | 0  |             return 0;  | 
1369  | 0  |         num |= (char)x;  | 
1370  | 0  |     }  | 
1371  | 0  |     out[0] = num >> 8;  | 
1372  | 0  |     out[1] = num & 0xff;  | 
1373  | 0  |     return 1;  | 
1374  | 0  | }  | 
1375  |  |  | 
1376  |  | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk,  | 
1377  |  |                              unsigned long chtype)  | 
1378  | 0  | { | 
1379  | 0  |     CONF_VALUE *v;  | 
1380  | 0  |     int i, mval, spec_char, plus_char;  | 
1381  | 0  |     char *p, *type;  | 
1382  |  | 
  | 
1383  | 0  |     if (!nm)  | 
1384  | 0  |         return 0;  | 
1385  |  |  | 
1386  | 0  |     for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { | 
1387  | 0  |         v = sk_CONF_VALUE_value(dn_sk, i);  | 
1388  | 0  |         type = v->name;  | 
1389  |  |         /*  | 
1390  |  |          * Skip past any leading X. X: X, etc to allow for multiple instances  | 
1391  |  |          */  | 
1392  | 0  |         for (p = type; *p; p++) { | 
1393  | 0  | #ifndef CHARSET_EBCDIC  | 
1394  | 0  |             spec_char = ((*p == ':') || (*p == ',') || (*p == '.'));  | 
1395  |  | #else  | 
1396  |  |             spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[','])  | 
1397  |  |                          || (*p == os_toascii['.']));  | 
1398  |  | #endif  | 
1399  | 0  |             if (spec_char) { | 
1400  | 0  |                 p++;  | 
1401  | 0  |                 if (*p)  | 
1402  | 0  |                     type = p;  | 
1403  | 0  |                 break;  | 
1404  | 0  |             }  | 
1405  | 0  |         }  | 
1406  | 0  | #ifndef CHARSET_EBCDIC  | 
1407  | 0  |         plus_char = (*type == '+');  | 
1408  |  | #else  | 
1409  |  |         plus_char = (*type == os_toascii['+']);  | 
1410  |  | #endif  | 
1411  | 0  |         if (plus_char) { | 
1412  | 0  |             mval = -1;  | 
1413  | 0  |             type++;  | 
1414  | 0  |         } else { | 
1415  | 0  |             mval = 0;  | 
1416  | 0  |         }  | 
1417  | 0  |         if (!X509_NAME_add_entry_by_txt(nm, type, chtype,  | 
1418  | 0  |                                         (unsigned char *)v->value, -1, -1,  | 
1419  | 0  |                                         mval))  | 
1420  | 0  |             return 0;  | 
1421  |  | 
  | 
1422  | 0  |     }  | 
1423  | 0  |     return 1;  | 
1424  | 0  | }  | 
1425  |  |  | 
1426  |  | int OSSL_GENERAL_NAMES_print(BIO *out, GENERAL_NAMES *gens, int indent)  | 
1427  | 0  | { | 
1428  | 0  |     int i;  | 
1429  |  | 
  | 
1430  | 0  |     for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { | 
1431  | 0  |         if (i > 0)  | 
1432  | 0  |             BIO_puts(out, "\n");  | 
1433  | 0  |         BIO_printf(out, "%*s", indent + 2, "");  | 
1434  | 0  |         GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i));  | 
1435  | 0  |     }  | 
1436  | 0  |     return 1;  | 
1437  | 0  | }  | 
1438  |  |  | 
1439  |  | int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len)  | 
1440  | 0  | { | 
1441  | 0  |     int result;  | 
1442  | 0  |     char *hexbuf;  | 
1443  |  | 
  | 
1444  | 0  |     if (len == 0)  | 
1445  | 0  |         return 1;  | 
1446  |  |  | 
1447  | 0  |     hexbuf = OPENSSL_buf2hexstr(buf, len);  | 
1448  | 0  |     if (hexbuf == NULL)  | 
1449  | 0  |         return 0;  | 
1450  | 0  |     result = BIO_puts(out, hexbuf) > 0;  | 
1451  |  | 
  | 
1452  | 0  |     OPENSSL_free(hexbuf);  | 
1453  | 0  |     return result;  | 
1454  | 0  | }  |