/src/irssi/subprojects/openssl-1.1.1l/crypto/x509v3/v3_utl.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1999-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the OpenSSL license (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 "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 | | |
24 | | static char *strip_spaces(char *name); |
25 | | static int sk_strcmp(const char *const *a, const char *const *b); |
26 | | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, |
27 | | GENERAL_NAMES *gens); |
28 | | static void str_free(OPENSSL_STRING str); |
29 | | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email); |
30 | | |
31 | | static int ipv4_from_asc(unsigned char *v4, const char *in); |
32 | | static int ipv6_from_asc(unsigned char *v6, const char *in); |
33 | | static int ipv6_cb(const char *elem, int len, void *usr); |
34 | | static int ipv6_hex(unsigned char *out, const char *in, int inlen); |
35 | | |
36 | | /* Add a CONF_VALUE name value pair to stack */ |
37 | | |
38 | | static int x509v3_add_len_value(const char *name, const char *value, |
39 | | size_t vallen, STACK_OF(CONF_VALUE) **extlist) |
40 | 0 | { |
41 | 0 | CONF_VALUE *vtmp = NULL; |
42 | 0 | char *tname = NULL, *tvalue = NULL; |
43 | 0 | int sk_allocated = (*extlist == NULL); |
44 | |
|
45 | 0 | if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL) |
46 | 0 | goto err; |
47 | 0 | if (value != NULL && vallen > 0) { |
48 | | /* |
49 | | * We tolerate a single trailing NUL character, but otherwise no |
50 | | * embedded NULs |
51 | | */ |
52 | 0 | if (memchr(value, 0, vallen - 1) != NULL) |
53 | 0 | goto err; |
54 | 0 | tvalue = OPENSSL_strndup(value, vallen); |
55 | 0 | if (tvalue == NULL) |
56 | 0 | goto err; |
57 | 0 | } |
58 | 0 | if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL) |
59 | 0 | goto err; |
60 | 0 | if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) |
61 | 0 | goto err; |
62 | 0 | vtmp->section = NULL; |
63 | 0 | vtmp->name = tname; |
64 | 0 | vtmp->value = tvalue; |
65 | 0 | if (!sk_CONF_VALUE_push(*extlist, vtmp)) |
66 | 0 | goto err; |
67 | 0 | return 1; |
68 | 0 | err: |
69 | 0 | X509V3err(X509V3_F_X509V3_ADD_LEN_VALUE, ERR_R_MALLOC_FAILURE); |
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 | X509V3err(X509V3_F_BIGNUM_TO_STRING, ERR_R_MALLOC_FAILURE); |
151 | 0 | OPENSSL_free(tmp); |
152 | 0 | return NULL; |
153 | 0 | } |
154 | | |
155 | | /* Prepend "0x", but place it after the "-" if negative. */ |
156 | 0 | if (tmp[0] == '-') { |
157 | 0 | OPENSSL_strlcpy(ret, "-0x", len); |
158 | 0 | OPENSSL_strlcat(ret, tmp + 1, len); |
159 | 0 | } else { |
160 | 0 | OPENSSL_strlcpy(ret, "0x", len); |
161 | 0 | OPENSSL_strlcat(ret, tmp, len); |
162 | 0 | } |
163 | 0 | OPENSSL_free(tmp); |
164 | 0 | return ret; |
165 | 0 | } |
166 | | |
167 | | char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a) |
168 | 0 | { |
169 | 0 | BIGNUM *bntmp = NULL; |
170 | 0 | char *strtmp = NULL; |
171 | |
|
172 | 0 | if (!a) |
173 | 0 | return NULL; |
174 | 0 | if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL |
175 | 0 | || (strtmp = bignum_to_string(bntmp)) == NULL) |
176 | 0 | X509V3err(X509V3_F_I2S_ASN1_ENUMERATED, ERR_R_MALLOC_FAILURE); |
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 | || (strtmp = bignum_to_string(bntmp)) == NULL) |
190 | 0 | X509V3err(X509V3_F_I2S_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); |
191 | 0 | BN_free(bntmp); |
192 | 0 | return strtmp; |
193 | 0 | } |
194 | | |
195 | | ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value) |
196 | 0 | { |
197 | 0 | BIGNUM *bn = NULL; |
198 | 0 | ASN1_INTEGER *aint; |
199 | 0 | int isneg, ishex; |
200 | 0 | int ret; |
201 | 0 | if (value == NULL) { |
202 | 0 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_INVALID_NULL_VALUE); |
203 | 0 | return NULL; |
204 | 0 | } |
205 | 0 | bn = BN_new(); |
206 | 0 | if (bn == NULL) { |
207 | 0 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, ERR_R_MALLOC_FAILURE); |
208 | 0 | return NULL; |
209 | 0 | } |
210 | 0 | if (value[0] == '-') { |
211 | 0 | value++; |
212 | 0 | isneg = 1; |
213 | 0 | } else |
214 | 0 | isneg = 0; |
215 | |
|
216 | 0 | if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { |
217 | 0 | value += 2; |
218 | 0 | ishex = 1; |
219 | 0 | } else |
220 | 0 | ishex = 0; |
221 | |
|
222 | 0 | if (ishex) |
223 | 0 | ret = BN_hex2bn(&bn, value); |
224 | 0 | else |
225 | 0 | ret = BN_dec2bn(&bn, value); |
226 | |
|
227 | 0 | if (!ret || value[ret]) { |
228 | 0 | BN_free(bn); |
229 | 0 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, X509V3_R_BN_DEC2BN_ERROR); |
230 | 0 | return NULL; |
231 | 0 | } |
232 | | |
233 | 0 | if (isneg && BN_is_zero(bn)) |
234 | 0 | isneg = 0; |
235 | |
|
236 | 0 | aint = BN_to_ASN1_INTEGER(bn, NULL); |
237 | 0 | BN_free(bn); |
238 | 0 | if (!aint) { |
239 | 0 | X509V3err(X509V3_F_S2I_ASN1_INTEGER, |
240 | 0 | X509V3_R_BN_TO_ASN1_INTEGER_ERROR); |
241 | 0 | return NULL; |
242 | 0 | } |
243 | 0 | if (isneg) |
244 | 0 | aint->type |= V_ASN1_NEG; |
245 | 0 | return aint; |
246 | 0 | } |
247 | | |
248 | | int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, |
249 | | STACK_OF(CONF_VALUE) **extlist) |
250 | 0 | { |
251 | 0 | char *strtmp; |
252 | 0 | int ret; |
253 | |
|
254 | 0 | if (!aint) |
255 | 0 | return 1; |
256 | 0 | if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL) |
257 | 0 | return 0; |
258 | 0 | ret = X509V3_add_value(name, strtmp, extlist); |
259 | 0 | OPENSSL_free(strtmp); |
260 | 0 | return ret; |
261 | 0 | } |
262 | | |
263 | | int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool) |
264 | 0 | { |
265 | 0 | const char *btmp; |
266 | |
|
267 | 0 | if ((btmp = value->value) == NULL) |
268 | 0 | goto err; |
269 | 0 | if (strcmp(btmp, "TRUE") == 0 |
270 | 0 | || strcmp(btmp, "true") == 0 |
271 | 0 | || strcmp(btmp, "Y") == 0 |
272 | 0 | || strcmp(btmp, "y") == 0 |
273 | 0 | || strcmp(btmp, "YES") == 0 |
274 | 0 | || strcmp(btmp, "yes") == 0) { |
275 | 0 | *asn1_bool = 0xff; |
276 | 0 | return 1; |
277 | 0 | } |
278 | 0 | if (strcmp(btmp, "FALSE") == 0 |
279 | 0 | || strcmp(btmp, "false") == 0 |
280 | 0 | || strcmp(btmp, "N") == 0 |
281 | 0 | || strcmp(btmp, "n") == 0 |
282 | 0 | || strcmp(btmp, "NO") == 0 |
283 | 0 | || strcmp(btmp, "no") == 0) { |
284 | 0 | *asn1_bool = 0; |
285 | 0 | return 1; |
286 | 0 | } |
287 | 0 | err: |
288 | 0 | X509V3err(X509V3_F_X509V3_GET_VALUE_BOOL, |
289 | 0 | X509V3_R_INVALID_BOOLEAN_STRING); |
290 | 0 | X509V3_conf_err(value); |
291 | 0 | return 0; |
292 | 0 | } |
293 | | |
294 | | int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint) |
295 | 0 | { |
296 | 0 | ASN1_INTEGER *itmp; |
297 | |
|
298 | 0 | if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) { |
299 | 0 | X509V3_conf_err(value); |
300 | 0 | return 0; |
301 | 0 | } |
302 | 0 | *aint = itmp; |
303 | 0 | return 1; |
304 | 0 | } |
305 | | |
306 | 0 | #define HDR_NAME 1 |
307 | 0 | #define HDR_VALUE 2 |
308 | | |
309 | | /* |
310 | | * #define DEBUG |
311 | | */ |
312 | | |
313 | | STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line) |
314 | 0 | { |
315 | 0 | char *p, *q, c; |
316 | 0 | char *ntmp, *vtmp; |
317 | 0 | STACK_OF(CONF_VALUE) *values = NULL; |
318 | 0 | char *linebuf; |
319 | 0 | int state; |
320 | | /* We are going to modify the line so copy it first */ |
321 | 0 | linebuf = OPENSSL_strdup(line); |
322 | 0 | if (linebuf == NULL) { |
323 | 0 | X509V3err(X509V3_F_X509V3_PARSE_LIST, ERR_R_MALLOC_FAILURE); |
324 | 0 | goto err; |
325 | 0 | } |
326 | 0 | state = HDR_NAME; |
327 | 0 | ntmp = NULL; |
328 | | /* Go through all characters */ |
329 | 0 | for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); |
330 | 0 | p++) { |
331 | |
|
332 | 0 | switch (state) { |
333 | 0 | case HDR_NAME: |
334 | 0 | if (c == ':') { |
335 | 0 | state = HDR_VALUE; |
336 | 0 | *p = 0; |
337 | 0 | ntmp = strip_spaces(q); |
338 | 0 | if (!ntmp) { |
339 | 0 | X509V3err(X509V3_F_X509V3_PARSE_LIST, |
340 | 0 | X509V3_R_INVALID_NULL_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 | X509V3err(X509V3_F_X509V3_PARSE_LIST, |
350 | 0 | X509V3_R_INVALID_NULL_NAME); |
351 | 0 | goto err; |
352 | 0 | } |
353 | 0 | X509V3_add_value(ntmp, NULL, &values); |
354 | 0 | } |
355 | 0 | break; |
356 | | |
357 | 0 | case HDR_VALUE: |
358 | 0 | if (c == ',') { |
359 | 0 | state = HDR_NAME; |
360 | 0 | *p = 0; |
361 | 0 | vtmp = strip_spaces(q); |
362 | 0 | if (!vtmp) { |
363 | 0 | X509V3err(X509V3_F_X509V3_PARSE_LIST, |
364 | 0 | X509V3_R_INVALID_NULL_VALUE); |
365 | 0 | goto err; |
366 | 0 | } |
367 | 0 | X509V3_add_value(ntmp, vtmp, &values); |
368 | 0 | ntmp = NULL; |
369 | 0 | q = p + 1; |
370 | 0 | } |
371 | |
|
372 | 0 | } |
373 | 0 | } |
374 | | |
375 | 0 | if (state == HDR_VALUE) { |
376 | 0 | vtmp = strip_spaces(q); |
377 | 0 | if (!vtmp) { |
378 | 0 | X509V3err(X509V3_F_X509V3_PARSE_LIST, |
379 | 0 | X509V3_R_INVALID_NULL_VALUE); |
380 | 0 | goto err; |
381 | 0 | } |
382 | 0 | X509V3_add_value(ntmp, vtmp, &values); |
383 | 0 | } else { |
384 | 0 | ntmp = strip_spaces(q); |
385 | 0 | if (!ntmp) { |
386 | 0 | X509V3err(X509V3_F_X509V3_PARSE_LIST, X509V3_R_INVALID_NULL_NAME); |
387 | 0 | goto err; |
388 | 0 | } |
389 | 0 | X509V3_add_value(ntmp, NULL, &values); |
390 | 0 | } |
391 | 0 | OPENSSL_free(linebuf); |
392 | 0 | return values; |
393 | | |
394 | 0 | err: |
395 | 0 | OPENSSL_free(linebuf); |
396 | 0 | sk_CONF_VALUE_pop_free(values, X509V3_conf_free); |
397 | 0 | return NULL; |
398 | |
|
399 | 0 | } |
400 | | |
401 | | /* Delete leading and trailing spaces from a string */ |
402 | | static char *strip_spaces(char *name) |
403 | 0 | { |
404 | 0 | char *p, *q; |
405 | | /* Skip over leading spaces */ |
406 | 0 | p = name; |
407 | 0 | while (*p && ossl_isspace(*p)) |
408 | 0 | p++; |
409 | 0 | if (!*p) |
410 | 0 | return NULL; |
411 | 0 | q = p + strlen(p) - 1; |
412 | 0 | while ((q != p) && ossl_isspace(*q)) |
413 | 0 | q--; |
414 | 0 | if (p != q) |
415 | 0 | q[1] = 0; |
416 | 0 | if (!*p) |
417 | 0 | return NULL; |
418 | 0 | return p; |
419 | 0 | } |
420 | | |
421 | | |
422 | | /* |
423 | | * V2I name comparison function: returns zero if 'name' matches cmp or cmp.* |
424 | | */ |
425 | | |
426 | | int name_cmp(const char *name, const char *cmp) |
427 | 0 | { |
428 | 0 | int len, ret; |
429 | 0 | char c; |
430 | 0 | len = strlen(cmp); |
431 | 0 | if ((ret = strncmp(name, cmp, len))) |
432 | 0 | return ret; |
433 | 0 | c = name[len]; |
434 | 0 | if (!c || (c == '.')) |
435 | 0 | return 0; |
436 | 0 | return 1; |
437 | 0 | } |
438 | | |
439 | | static int sk_strcmp(const char *const *a, const char *const *b) |
440 | 0 | { |
441 | 0 | return strcmp(*a, *b); |
442 | 0 | } |
443 | | |
444 | | STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) |
445 | 0 | { |
446 | 0 | GENERAL_NAMES *gens; |
447 | 0 | STACK_OF(OPENSSL_STRING) *ret; |
448 | |
|
449 | 0 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
450 | 0 | ret = get_email(X509_get_subject_name(x), gens); |
451 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
452 | 0 | return ret; |
453 | 0 | } |
454 | | |
455 | | STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) |
456 | 0 | { |
457 | 0 | AUTHORITY_INFO_ACCESS *info; |
458 | 0 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
459 | 0 | int i; |
460 | |
|
461 | 0 | info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL); |
462 | 0 | if (!info) |
463 | 0 | return NULL; |
464 | 0 | for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
465 | 0 | ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
466 | 0 | if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { |
467 | 0 | if (ad->location->type == GEN_URI) { |
468 | 0 | if (!append_ia5 |
469 | 0 | (&ret, ad->location->d.uniformResourceIdentifier)) |
470 | 0 | break; |
471 | 0 | } |
472 | 0 | } |
473 | 0 | } |
474 | 0 | AUTHORITY_INFO_ACCESS_free(info); |
475 | 0 | return ret; |
476 | 0 | } |
477 | | |
478 | | STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) |
479 | 0 | { |
480 | 0 | GENERAL_NAMES *gens; |
481 | 0 | STACK_OF(X509_EXTENSION) *exts; |
482 | 0 | STACK_OF(OPENSSL_STRING) *ret; |
483 | |
|
484 | 0 | exts = X509_REQ_get_extensions(x); |
485 | 0 | gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL); |
486 | 0 | ret = get_email(X509_REQ_get_subject_name(x), gens); |
487 | 0 | sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
488 | 0 | sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
489 | 0 | return ret; |
490 | 0 | } |
491 | | |
492 | | static STACK_OF(OPENSSL_STRING) *get_email(X509_NAME *name, |
493 | | GENERAL_NAMES *gens) |
494 | 0 | { |
495 | 0 | STACK_OF(OPENSSL_STRING) *ret = NULL; |
496 | 0 | X509_NAME_ENTRY *ne; |
497 | 0 | const ASN1_IA5STRING *email; |
498 | 0 | GENERAL_NAME *gen; |
499 | 0 | int i = -1; |
500 | | |
501 | | /* Now add any email address(es) to STACK */ |
502 | | /* First supplied X509_NAME */ |
503 | 0 | while ((i = X509_NAME_get_index_by_NID(name, |
504 | 0 | NID_pkcs9_emailAddress, i)) >= 0) { |
505 | 0 | ne = X509_NAME_get_entry(name, i); |
506 | 0 | email = X509_NAME_ENTRY_get_data(ne); |
507 | 0 | if (!append_ia5(&ret, email)) |
508 | 0 | return NULL; |
509 | 0 | } |
510 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
511 | 0 | gen = sk_GENERAL_NAME_value(gens, i); |
512 | 0 | if (gen->type != GEN_EMAIL) |
513 | 0 | continue; |
514 | 0 | if (!append_ia5(&ret, gen->d.ia5)) |
515 | 0 | return NULL; |
516 | 0 | } |
517 | 0 | return ret; |
518 | 0 | } |
519 | | |
520 | | static void str_free(OPENSSL_STRING str) |
521 | 0 | { |
522 | 0 | OPENSSL_free(str); |
523 | 0 | } |
524 | | |
525 | | static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, const ASN1_IA5STRING *email) |
526 | 0 | { |
527 | 0 | char *emtmp; |
528 | | /* First some sanity checks */ |
529 | 0 | if (email->type != V_ASN1_IA5STRING) |
530 | 0 | return 1; |
531 | 0 | if (email->data == NULL || email->length == 0) |
532 | 0 | return 1; |
533 | 0 | if (memchr(email->data, 0, email->length) != NULL) |
534 | 0 | return 1; |
535 | 0 | if (*sk == NULL) |
536 | 0 | *sk = sk_OPENSSL_STRING_new(sk_strcmp); |
537 | 0 | if (*sk == NULL) |
538 | 0 | return 0; |
539 | | |
540 | 0 | emtmp = OPENSSL_strndup((char *)email->data, email->length); |
541 | 0 | if (emtmp == NULL) |
542 | 0 | return 0; |
543 | | |
544 | | /* Don't add duplicates */ |
545 | 0 | if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { |
546 | 0 | OPENSSL_free(emtmp); |
547 | 0 | return 1; |
548 | 0 | } |
549 | 0 | if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { |
550 | 0 | OPENSSL_free(emtmp); /* free on push failure */ |
551 | 0 | X509_email_free(*sk); |
552 | 0 | *sk = NULL; |
553 | 0 | return 0; |
554 | 0 | } |
555 | 0 | return 1; |
556 | 0 | } |
557 | | |
558 | | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
559 | 0 | { |
560 | 0 | sk_OPENSSL_STRING_pop_free(sk, str_free); |
561 | 0 | } |
562 | | |
563 | | typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len, |
564 | | const unsigned char *subject, size_t subject_len, |
565 | | unsigned int flags); |
566 | | |
567 | | /* Skip pattern prefix to match "wildcard" subject */ |
568 | | static void skip_prefix(const unsigned char **p, size_t *plen, |
569 | | size_t subject_len, |
570 | | unsigned int flags) |
571 | 0 | { |
572 | 0 | const unsigned char *pattern = *p; |
573 | 0 | size_t pattern_len = *plen; |
574 | | |
575 | | /* |
576 | | * If subject starts with a leading '.' followed by more octets, and |
577 | | * pattern is longer, compare just an equal-length suffix with the |
578 | | * full subject (starting at the '.'), provided the prefix contains |
579 | | * no NULs. |
580 | | */ |
581 | 0 | if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0) |
582 | 0 | return; |
583 | | |
584 | 0 | while (pattern_len > subject_len && *pattern) { |
585 | 0 | if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && |
586 | 0 | *pattern == '.') |
587 | 0 | break; |
588 | 0 | ++pattern; |
589 | 0 | --pattern_len; |
590 | 0 | } |
591 | | |
592 | | /* Skip if entire prefix acceptable */ |
593 | 0 | if (pattern_len == subject_len) { |
594 | 0 | *p = pattern; |
595 | 0 | *plen = pattern_len; |
596 | 0 | } |
597 | 0 | } |
598 | | |
599 | | /* Compare while ASCII ignoring case. */ |
600 | | static int equal_nocase(const unsigned char *pattern, size_t pattern_len, |
601 | | const unsigned char *subject, size_t subject_len, |
602 | | unsigned int flags) |
603 | 0 | { |
604 | 0 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
605 | 0 | if (pattern_len != subject_len) |
606 | 0 | return 0; |
607 | 0 | while (pattern_len) { |
608 | 0 | unsigned char l = *pattern; |
609 | 0 | unsigned char r = *subject; |
610 | | /* The pattern must not contain NUL characters. */ |
611 | 0 | if (l == 0) |
612 | 0 | return 0; |
613 | 0 | if (l != r) { |
614 | 0 | if ('A' <= l && l <= 'Z') |
615 | 0 | l = (l - 'A') + 'a'; |
616 | 0 | if ('A' <= r && r <= 'Z') |
617 | 0 | r = (r - 'A') + 'a'; |
618 | 0 | if (l != r) |
619 | 0 | return 0; |
620 | 0 | } |
621 | 0 | ++pattern; |
622 | 0 | ++subject; |
623 | 0 | --pattern_len; |
624 | 0 | } |
625 | 0 | return 1; |
626 | 0 | } |
627 | | |
628 | | /* Compare using memcmp. */ |
629 | | static int equal_case(const unsigned char *pattern, size_t pattern_len, |
630 | | const unsigned char *subject, size_t subject_len, |
631 | | unsigned int flags) |
632 | 0 | { |
633 | 0 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
634 | 0 | if (pattern_len != subject_len) |
635 | 0 | return 0; |
636 | 0 | return !memcmp(pattern, subject, pattern_len); |
637 | 0 | } |
638 | | |
639 | | /* |
640 | | * RFC 5280, section 7.5, requires that only the domain is compared in a |
641 | | * case-insensitive manner. |
642 | | */ |
643 | | static int equal_email(const unsigned char *a, size_t a_len, |
644 | | const unsigned char *b, size_t b_len, |
645 | | unsigned int unused_flags) |
646 | 0 | { |
647 | 0 | size_t i = a_len; |
648 | 0 | if (a_len != b_len) |
649 | 0 | return 0; |
650 | | /* |
651 | | * We search backwards for the '@' character, so that we do not have to |
652 | | * deal with quoted local-parts. The domain part is compared in a |
653 | | * case-insensitive manner. |
654 | | */ |
655 | 0 | while (i > 0) { |
656 | 0 | --i; |
657 | 0 | if (a[i] == '@' || b[i] == '@') { |
658 | 0 | if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) |
659 | 0 | return 0; |
660 | 0 | break; |
661 | 0 | } |
662 | 0 | } |
663 | 0 | if (i == 0) |
664 | 0 | i = a_len; |
665 | 0 | return equal_case(a, i, b, i, 0); |
666 | 0 | } |
667 | | |
668 | | /* |
669 | | * Compare the prefix and suffix with the subject, and check that the |
670 | | * characters in-between are valid. |
671 | | */ |
672 | | static int wildcard_match(const unsigned char *prefix, size_t prefix_len, |
673 | | const unsigned char *suffix, size_t suffix_len, |
674 | | const unsigned char *subject, size_t subject_len, |
675 | | unsigned int flags) |
676 | 0 | { |
677 | 0 | const unsigned char *wildcard_start; |
678 | 0 | const unsigned char *wildcard_end; |
679 | 0 | const unsigned char *p; |
680 | 0 | int allow_multi = 0; |
681 | 0 | int allow_idna = 0; |
682 | |
|
683 | 0 | if (subject_len < prefix_len + suffix_len) |
684 | 0 | return 0; |
685 | 0 | if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) |
686 | 0 | return 0; |
687 | 0 | wildcard_start = subject + prefix_len; |
688 | 0 | wildcard_end = subject + (subject_len - suffix_len); |
689 | 0 | if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) |
690 | 0 | return 0; |
691 | | /* |
692 | | * If the wildcard makes up the entire first label, it must match at |
693 | | * least one character. |
694 | | */ |
695 | 0 | if (prefix_len == 0 && *suffix == '.') { |
696 | 0 | if (wildcard_start == wildcard_end) |
697 | 0 | return 0; |
698 | 0 | allow_idna = 1; |
699 | 0 | if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS) |
700 | 0 | allow_multi = 1; |
701 | 0 | } |
702 | | /* IDNA labels cannot match partial wildcards */ |
703 | 0 | if (!allow_idna && |
704 | 0 | subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0) |
705 | 0 | return 0; |
706 | | /* The wildcard may match a literal '*' */ |
707 | 0 | if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') |
708 | 0 | return 1; |
709 | | /* |
710 | | * Check that the part matched by the wildcard contains only |
711 | | * permitted characters and only matches a single label unless |
712 | | * allow_multi is set. |
713 | | */ |
714 | 0 | for (p = wildcard_start; p != wildcard_end; ++p) |
715 | 0 | if (!(('0' <= *p && *p <= '9') || |
716 | 0 | ('A' <= *p && *p <= 'Z') || |
717 | 0 | ('a' <= *p && *p <= 'z') || |
718 | 0 | *p == '-' || (allow_multi && *p == '.'))) |
719 | 0 | return 0; |
720 | 0 | return 1; |
721 | 0 | } |
722 | | |
723 | 0 | #define LABEL_START (1 << 0) |
724 | | #define LABEL_END (1 << 1) |
725 | 0 | #define LABEL_HYPHEN (1 << 2) |
726 | 0 | #define LABEL_IDNA (1 << 3) |
727 | | |
728 | | static const unsigned char *valid_star(const unsigned char *p, size_t len, |
729 | | unsigned int flags) |
730 | 0 | { |
731 | 0 | const unsigned char *star = 0; |
732 | 0 | size_t i; |
733 | 0 | int state = LABEL_START; |
734 | 0 | int dots = 0; |
735 | 0 | for (i = 0; i < len; ++i) { |
736 | | /* |
737 | | * Locate first and only legal wildcard, either at the start |
738 | | * or end of a non-IDNA first and not final label. |
739 | | */ |
740 | 0 | if (p[i] == '*') { |
741 | 0 | int atstart = (state & LABEL_START); |
742 | 0 | int atend = (i == len - 1 || p[i + 1] == '.'); |
743 | | /*- |
744 | | * At most one wildcard per pattern. |
745 | | * No wildcards in IDNA labels. |
746 | | * No wildcards after the first label. |
747 | | */ |
748 | 0 | if (star != NULL || (state & LABEL_IDNA) != 0 || dots) |
749 | 0 | return NULL; |
750 | | /* Only full-label '*.example.com' wildcards? */ |
751 | 0 | if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS) |
752 | 0 | && (!atstart || !atend)) |
753 | 0 | return NULL; |
754 | | /* No 'foo*bar' wildcards */ |
755 | 0 | if (!atstart && !atend) |
756 | 0 | return NULL; |
757 | 0 | star = &p[i]; |
758 | 0 | state &= ~LABEL_START; |
759 | 0 | } else if (('a' <= p[i] && p[i] <= 'z') |
760 | 0 | || ('A' <= p[i] && p[i] <= 'Z') |
761 | 0 | || ('0' <= p[i] && p[i] <= '9')) { |
762 | 0 | if ((state & LABEL_START) != 0 |
763 | 0 | && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0) |
764 | 0 | state |= LABEL_IDNA; |
765 | 0 | state &= ~(LABEL_HYPHEN | LABEL_START); |
766 | 0 | } else if (p[i] == '.') { |
767 | 0 | if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) |
768 | 0 | return NULL; |
769 | 0 | state = LABEL_START; |
770 | 0 | ++dots; |
771 | 0 | } else if (p[i] == '-') { |
772 | | /* no domain/subdomain starts with '-' */ |
773 | 0 | if ((state & LABEL_START) != 0) |
774 | 0 | return NULL; |
775 | 0 | state |= LABEL_HYPHEN; |
776 | 0 | } else |
777 | 0 | return NULL; |
778 | 0 | } |
779 | | |
780 | | /* |
781 | | * The final label must not end in a hyphen or ".", and |
782 | | * there must be at least two dots after the star. |
783 | | */ |
784 | 0 | if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) |
785 | 0 | return NULL; |
786 | 0 | return star; |
787 | 0 | } |
788 | | |
789 | | /* Compare using wildcards. */ |
790 | | static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, |
791 | | const unsigned char *subject, size_t subject_len, |
792 | | unsigned int flags) |
793 | 0 | { |
794 | 0 | const unsigned char *star = NULL; |
795 | | |
796 | | /* |
797 | | * Subject names starting with '.' can only match a wildcard pattern |
798 | | * via a subject sub-domain pattern suffix match. |
799 | | */ |
800 | 0 | if (!(subject_len > 1 && subject[0] == '.')) |
801 | 0 | star = valid_star(pattern, pattern_len, flags); |
802 | 0 | if (star == NULL) |
803 | 0 | return equal_nocase(pattern, pattern_len, |
804 | 0 | subject, subject_len, flags); |
805 | 0 | return wildcard_match(pattern, star - pattern, |
806 | 0 | star + 1, (pattern + pattern_len) - star - 1, |
807 | 0 | subject, subject_len, flags); |
808 | 0 | } |
809 | | |
810 | | /* |
811 | | * Compare an ASN1_STRING to a supplied string. If they match return 1. If |
812 | | * cmp_type > 0 only compare if string matches the type, otherwise convert it |
813 | | * to UTF8. |
814 | | */ |
815 | | |
816 | | static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal, |
817 | | unsigned int flags, const char *b, size_t blen, |
818 | | char **peername) |
819 | 0 | { |
820 | 0 | int rv = 0; |
821 | |
|
822 | 0 | if (!a->data || !a->length) |
823 | 0 | return 0; |
824 | 0 | if (cmp_type > 0) { |
825 | 0 | if (cmp_type != a->type) |
826 | 0 | return 0; |
827 | 0 | if (cmp_type == V_ASN1_IA5STRING) |
828 | 0 | rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); |
829 | 0 | else if (a->length == (int)blen && !memcmp(a->data, b, blen)) |
830 | 0 | rv = 1; |
831 | 0 | if (rv > 0 && peername) |
832 | 0 | *peername = OPENSSL_strndup((char *)a->data, a->length); |
833 | 0 | } else { |
834 | 0 | int astrlen; |
835 | 0 | unsigned char *astr; |
836 | 0 | astrlen = ASN1_STRING_to_UTF8(&astr, a); |
837 | 0 | if (astrlen < 0) { |
838 | | /* |
839 | | * -1 could be an internal malloc failure or a decoding error from |
840 | | * malformed input; we can't distinguish. |
841 | | */ |
842 | 0 | return -1; |
843 | 0 | } |
844 | 0 | rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); |
845 | 0 | if (rv > 0 && peername) |
846 | 0 | *peername = OPENSSL_strndup((char *)astr, astrlen); |
847 | 0 | OPENSSL_free(astr); |
848 | 0 | } |
849 | 0 | return rv; |
850 | 0 | } |
851 | | |
852 | | static int do_x509_check(X509 *x, const char *chk, size_t chklen, |
853 | | unsigned int flags, int check_type, char **peername) |
854 | 0 | { |
855 | 0 | GENERAL_NAMES *gens = NULL; |
856 | 0 | X509_NAME *name = NULL; |
857 | 0 | int i; |
858 | 0 | int cnid = NID_undef; |
859 | 0 | int alt_type; |
860 | 0 | int san_present = 0; |
861 | 0 | int rv = 0; |
862 | 0 | equal_fn equal; |
863 | | |
864 | | /* See below, this flag is internal-only */ |
865 | 0 | flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS; |
866 | 0 | if (check_type == GEN_EMAIL) { |
867 | 0 | cnid = NID_pkcs9_emailAddress; |
868 | 0 | alt_type = V_ASN1_IA5STRING; |
869 | 0 | equal = equal_email; |
870 | 0 | } else if (check_type == GEN_DNS) { |
871 | 0 | cnid = NID_commonName; |
872 | | /* Implicit client-side DNS sub-domain pattern */ |
873 | 0 | if (chklen > 1 && chk[0] == '.') |
874 | 0 | flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS; |
875 | 0 | alt_type = V_ASN1_IA5STRING; |
876 | 0 | if (flags & X509_CHECK_FLAG_NO_WILDCARDS) |
877 | 0 | equal = equal_nocase; |
878 | 0 | else |
879 | 0 | equal = equal_wildcard; |
880 | 0 | } else { |
881 | 0 | alt_type = V_ASN1_OCTET_STRING; |
882 | 0 | equal = equal_case; |
883 | 0 | } |
884 | |
|
885 | 0 | if (chklen == 0) |
886 | 0 | chklen = strlen(chk); |
887 | |
|
888 | 0 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
889 | 0 | if (gens) { |
890 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
891 | 0 | GENERAL_NAME *gen; |
892 | 0 | ASN1_STRING *cstr; |
893 | 0 | gen = sk_GENERAL_NAME_value(gens, i); |
894 | 0 | if (gen->type != check_type) |
895 | 0 | continue; |
896 | 0 | san_present = 1; |
897 | 0 | if (check_type == GEN_EMAIL) |
898 | 0 | cstr = gen->d.rfc822Name; |
899 | 0 | else if (check_type == GEN_DNS) |
900 | 0 | cstr = gen->d.dNSName; |
901 | 0 | else |
902 | 0 | cstr = gen->d.iPAddress; |
903 | | /* Positive on success, negative on error! */ |
904 | 0 | if ((rv = do_check_string(cstr, alt_type, equal, flags, |
905 | 0 | chk, chklen, peername)) != 0) |
906 | 0 | break; |
907 | 0 | } |
908 | 0 | GENERAL_NAMES_free(gens); |
909 | 0 | if (rv != 0) |
910 | 0 | return rv; |
911 | 0 | if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)) |
912 | 0 | return 0; |
913 | 0 | } |
914 | | |
915 | | /* We're done if CN-ID is not pertinent */ |
916 | 0 | if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) |
917 | 0 | return 0; |
918 | | |
919 | 0 | i = -1; |
920 | 0 | name = X509_get_subject_name(x); |
921 | 0 | while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) { |
922 | 0 | const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); |
923 | 0 | const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne); |
924 | | |
925 | | /* Positive on success, negative on error! */ |
926 | 0 | if ((rv = do_check_string(str, -1, equal, flags, |
927 | 0 | chk, chklen, peername)) != 0) |
928 | 0 | return rv; |
929 | 0 | } |
930 | 0 | return 0; |
931 | 0 | } |
932 | | |
933 | | int X509_check_host(X509 *x, const char *chk, size_t chklen, |
934 | | unsigned int flags, char **peername) |
935 | 0 | { |
936 | 0 | if (chk == NULL) |
937 | 0 | return -2; |
938 | | /* |
939 | | * Embedded NULs are disallowed, except as the last character of a |
940 | | * string of length 2 or more (tolerate caller including terminating |
941 | | * NUL in string length). |
942 | | */ |
943 | 0 | if (chklen == 0) |
944 | 0 | chklen = strlen(chk); |
945 | 0 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
946 | 0 | return -2; |
947 | 0 | if (chklen > 1 && chk[chklen - 1] == '\0') |
948 | 0 | --chklen; |
949 | 0 | return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername); |
950 | 0 | } |
951 | | |
952 | | int X509_check_email(X509 *x, const char *chk, size_t chklen, |
953 | | unsigned int flags) |
954 | 0 | { |
955 | 0 | if (chk == NULL) |
956 | 0 | return -2; |
957 | | /* |
958 | | * Embedded NULs are disallowed, except as the last character of a |
959 | | * string of length 2 or more (tolerate caller including terminating |
960 | | * NUL in string length). |
961 | | */ |
962 | 0 | if (chklen == 0) |
963 | 0 | chklen = strlen((char *)chk); |
964 | 0 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
965 | 0 | return -2; |
966 | 0 | if (chklen > 1 && chk[chklen - 1] == '\0') |
967 | 0 | --chklen; |
968 | 0 | return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL); |
969 | 0 | } |
970 | | |
971 | | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, |
972 | | unsigned int flags) |
973 | 0 | { |
974 | 0 | if (chk == NULL) |
975 | 0 | return -2; |
976 | 0 | return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL); |
977 | 0 | } |
978 | | |
979 | | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) |
980 | 0 | { |
981 | 0 | unsigned char ipout[16]; |
982 | 0 | size_t iplen; |
983 | |
|
984 | 0 | if (ipasc == NULL) |
985 | 0 | return -2; |
986 | 0 | iplen = (size_t)a2i_ipadd(ipout, ipasc); |
987 | 0 | if (iplen == 0) |
988 | 0 | return -2; |
989 | 0 | return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL); |
990 | 0 | } |
991 | | |
992 | | /* |
993 | | * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible |
994 | | * with RFC3280. |
995 | | */ |
996 | | |
997 | | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) |
998 | 0 | { |
999 | 0 | unsigned char ipout[16]; |
1000 | 0 | ASN1_OCTET_STRING *ret; |
1001 | 0 | int iplen; |
1002 | | |
1003 | | /* If string contains a ':' assume IPv6 */ |
1004 | |
|
1005 | 0 | iplen = a2i_ipadd(ipout, ipasc); |
1006 | |
|
1007 | 0 | if (!iplen) |
1008 | 0 | return NULL; |
1009 | | |
1010 | 0 | ret = ASN1_OCTET_STRING_new(); |
1011 | 0 | if (ret == NULL) |
1012 | 0 | return NULL; |
1013 | 0 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { |
1014 | 0 | ASN1_OCTET_STRING_free(ret); |
1015 | 0 | return NULL; |
1016 | 0 | } |
1017 | 0 | return ret; |
1018 | 0 | } |
1019 | | |
1020 | | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
1021 | 0 | { |
1022 | 0 | ASN1_OCTET_STRING *ret = NULL; |
1023 | 0 | unsigned char ipout[32]; |
1024 | 0 | char *iptmp = NULL, *p; |
1025 | 0 | int iplen1, iplen2; |
1026 | 0 | p = strchr(ipasc, '/'); |
1027 | 0 | if (!p) |
1028 | 0 | return NULL; |
1029 | 0 | iptmp = OPENSSL_strdup(ipasc); |
1030 | 0 | if (!iptmp) |
1031 | 0 | return NULL; |
1032 | 0 | p = iptmp + (p - ipasc); |
1033 | 0 | *p++ = 0; |
1034 | |
|
1035 | 0 | iplen1 = a2i_ipadd(ipout, iptmp); |
1036 | |
|
1037 | 0 | if (!iplen1) |
1038 | 0 | goto err; |
1039 | | |
1040 | 0 | iplen2 = a2i_ipadd(ipout + iplen1, p); |
1041 | |
|
1042 | 0 | OPENSSL_free(iptmp); |
1043 | 0 | iptmp = NULL; |
1044 | |
|
1045 | 0 | if (!iplen2 || (iplen1 != iplen2)) |
1046 | 0 | goto err; |
1047 | | |
1048 | 0 | ret = ASN1_OCTET_STRING_new(); |
1049 | 0 | if (ret == NULL) |
1050 | 0 | goto err; |
1051 | 0 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) |
1052 | 0 | goto err; |
1053 | | |
1054 | 0 | return ret; |
1055 | | |
1056 | 0 | err: |
1057 | 0 | OPENSSL_free(iptmp); |
1058 | 0 | ASN1_OCTET_STRING_free(ret); |
1059 | 0 | return NULL; |
1060 | 0 | } |
1061 | | |
1062 | | int a2i_ipadd(unsigned char *ipout, const char *ipasc) |
1063 | 0 | { |
1064 | | /* If string contains a ':' assume IPv6 */ |
1065 | |
|
1066 | 0 | if (strchr(ipasc, ':')) { |
1067 | 0 | if (!ipv6_from_asc(ipout, ipasc)) |
1068 | 0 | return 0; |
1069 | 0 | return 16; |
1070 | 0 | } else { |
1071 | 0 | if (!ipv4_from_asc(ipout, ipasc)) |
1072 | 0 | return 0; |
1073 | 0 | return 4; |
1074 | 0 | } |
1075 | 0 | } |
1076 | | |
1077 | | static int ipv4_from_asc(unsigned char *v4, const char *in) |
1078 | 0 | { |
1079 | 0 | int a0, a1, a2, a3; |
1080 | 0 | if (sscanf(in, "%d.%d.%d.%d", &a0, &a1, &a2, &a3) != 4) |
1081 | 0 | return 0; |
1082 | 0 | if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255) |
1083 | 0 | || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255)) |
1084 | 0 | return 0; |
1085 | 0 | v4[0] = a0; |
1086 | 0 | v4[1] = a1; |
1087 | 0 | v4[2] = a2; |
1088 | 0 | v4[3] = a3; |
1089 | 0 | return 1; |
1090 | 0 | } |
1091 | | |
1092 | | typedef struct { |
1093 | | /* Temporary store for IPV6 output */ |
1094 | | unsigned char tmp[16]; |
1095 | | /* Total number of bytes in tmp */ |
1096 | | int total; |
1097 | | /* The position of a zero (corresponding to '::') */ |
1098 | | int zero_pos; |
1099 | | /* Number of zeroes */ |
1100 | | int zero_cnt; |
1101 | | } IPV6_STAT; |
1102 | | |
1103 | | static int ipv6_from_asc(unsigned char *v6, const char *in) |
1104 | 0 | { |
1105 | 0 | IPV6_STAT v6stat; |
1106 | 0 | v6stat.total = 0; |
1107 | 0 | v6stat.zero_pos = -1; |
1108 | 0 | v6stat.zero_cnt = 0; |
1109 | | /* |
1110 | | * Treat the IPv6 representation as a list of values separated by ':'. |
1111 | | * The presence of a '::' will parse as one, two or three zero length |
1112 | | * elements. |
1113 | | */ |
1114 | 0 | if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) |
1115 | 0 | return 0; |
1116 | | |
1117 | | /* Now for some sanity checks */ |
1118 | | |
1119 | 0 | if (v6stat.zero_pos == -1) { |
1120 | | /* If no '::' must have exactly 16 bytes */ |
1121 | 0 | if (v6stat.total != 16) |
1122 | 0 | return 0; |
1123 | 0 | } else { |
1124 | | /* If '::' must have less than 16 bytes */ |
1125 | 0 | if (v6stat.total == 16) |
1126 | 0 | return 0; |
1127 | | /* More than three zeroes is an error */ |
1128 | 0 | if (v6stat.zero_cnt > 3) |
1129 | 0 | return 0; |
1130 | | /* Can only have three zeroes if nothing else present */ |
1131 | 0 | else if (v6stat.zero_cnt == 3) { |
1132 | 0 | if (v6stat.total > 0) |
1133 | 0 | return 0; |
1134 | 0 | } |
1135 | | /* Can only have two zeroes if at start or end */ |
1136 | 0 | else if (v6stat.zero_cnt == 2) { |
1137 | 0 | if ((v6stat.zero_pos != 0) |
1138 | 0 | && (v6stat.zero_pos != v6stat.total)) |
1139 | 0 | return 0; |
1140 | 0 | } else |
1141 | | /* Can only have one zero if *not* start or end */ |
1142 | 0 | { |
1143 | 0 | if ((v6stat.zero_pos == 0) |
1144 | 0 | || (v6stat.zero_pos == v6stat.total)) |
1145 | 0 | return 0; |
1146 | 0 | } |
1147 | 0 | } |
1148 | | |
1149 | | /* Format result */ |
1150 | | |
1151 | 0 | if (v6stat.zero_pos >= 0) { |
1152 | | /* Copy initial part */ |
1153 | 0 | memcpy(v6, v6stat.tmp, v6stat.zero_pos); |
1154 | | /* Zero middle */ |
1155 | 0 | memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); |
1156 | | /* Copy final part */ |
1157 | 0 | if (v6stat.total != v6stat.zero_pos) |
1158 | 0 | memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, |
1159 | 0 | v6stat.tmp + v6stat.zero_pos, |
1160 | 0 | v6stat.total - v6stat.zero_pos); |
1161 | 0 | } else |
1162 | 0 | memcpy(v6, v6stat.tmp, 16); |
1163 | |
|
1164 | 0 | return 1; |
1165 | 0 | } |
1166 | | |
1167 | | static int ipv6_cb(const char *elem, int len, void *usr) |
1168 | 0 | { |
1169 | 0 | IPV6_STAT *s = usr; |
1170 | | /* Error if 16 bytes written */ |
1171 | 0 | if (s->total == 16) |
1172 | 0 | return 0; |
1173 | 0 | if (len == 0) { |
1174 | | /* Zero length element, corresponds to '::' */ |
1175 | 0 | if (s->zero_pos == -1) |
1176 | 0 | s->zero_pos = s->total; |
1177 | | /* If we've already got a :: its an error */ |
1178 | 0 | else if (s->zero_pos != s->total) |
1179 | 0 | return 0; |
1180 | 0 | s->zero_cnt++; |
1181 | 0 | } else { |
1182 | | /* If more than 4 characters could be final a.b.c.d form */ |
1183 | 0 | if (len > 4) { |
1184 | | /* Need at least 4 bytes left */ |
1185 | 0 | if (s->total > 12) |
1186 | 0 | return 0; |
1187 | | /* Must be end of string */ |
1188 | 0 | if (elem[len]) |
1189 | 0 | return 0; |
1190 | 0 | if (!ipv4_from_asc(s->tmp + s->total, elem)) |
1191 | 0 | return 0; |
1192 | 0 | s->total += 4; |
1193 | 0 | } else { |
1194 | 0 | if (!ipv6_hex(s->tmp + s->total, elem, len)) |
1195 | 0 | return 0; |
1196 | 0 | s->total += 2; |
1197 | 0 | } |
1198 | 0 | } |
1199 | 0 | return 1; |
1200 | 0 | } |
1201 | | |
1202 | | /* |
1203 | | * Convert a string of up to 4 hex digits into the corresponding IPv6 form. |
1204 | | */ |
1205 | | |
1206 | | static int ipv6_hex(unsigned char *out, const char *in, int inlen) |
1207 | 0 | { |
1208 | 0 | unsigned char c; |
1209 | 0 | unsigned int num = 0; |
1210 | 0 | int x; |
1211 | |
|
1212 | 0 | if (inlen > 4) |
1213 | 0 | return 0; |
1214 | 0 | while (inlen--) { |
1215 | 0 | c = *in++; |
1216 | 0 | num <<= 4; |
1217 | 0 | x = OPENSSL_hexchar2int(c); |
1218 | 0 | if (x < 0) |
1219 | 0 | return 0; |
1220 | 0 | num |= (char)x; |
1221 | 0 | } |
1222 | 0 | out[0] = num >> 8; |
1223 | 0 | out[1] = num & 0xff; |
1224 | 0 | return 1; |
1225 | 0 | } |
1226 | | |
1227 | | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk, |
1228 | | unsigned long chtype) |
1229 | 0 | { |
1230 | 0 | CONF_VALUE *v; |
1231 | 0 | int i, mval, spec_char, plus_char; |
1232 | 0 | char *p, *type; |
1233 | 0 | if (!nm) |
1234 | 0 | return 0; |
1235 | | |
1236 | 0 | for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { |
1237 | 0 | v = sk_CONF_VALUE_value(dn_sk, i); |
1238 | 0 | type = v->name; |
1239 | | /* |
1240 | | * Skip past any leading X. X: X, etc to allow for multiple instances |
1241 | | */ |
1242 | 0 | for (p = type; *p; p++) { |
1243 | 0 | #ifndef CHARSET_EBCDIC |
1244 | 0 | spec_char = ((*p == ':') || (*p == ',') || (*p == '.')); |
1245 | | #else |
1246 | | spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[',']) |
1247 | | || (*p == os_toascii['.'])); |
1248 | | #endif |
1249 | 0 | if (spec_char) { |
1250 | 0 | p++; |
1251 | 0 | if (*p) |
1252 | 0 | type = p; |
1253 | 0 | break; |
1254 | 0 | } |
1255 | 0 | } |
1256 | 0 | #ifndef CHARSET_EBCDIC |
1257 | 0 | plus_char = (*type == '+'); |
1258 | | #else |
1259 | | plus_char = (*type == os_toascii['+']); |
1260 | | #endif |
1261 | 0 | if (plus_char) { |
1262 | 0 | mval = -1; |
1263 | 0 | type++; |
1264 | 0 | } else |
1265 | 0 | mval = 0; |
1266 | 0 | if (!X509_NAME_add_entry_by_txt(nm, type, chtype, |
1267 | 0 | (unsigned char *)v->value, -1, -1, |
1268 | 0 | mval)) |
1269 | 0 | return 0; |
1270 | |
|
1271 | 0 | } |
1272 | 0 | return 1; |
1273 | 0 | } |