/src/openssl111/crypto/x509v3/v3_utl.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1999-2022 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 | X509_email_free(*sk); |
543 | 0 | *sk = NULL; |
544 | 0 | return 0; |
545 | 0 | } |
546 | | |
547 | | /* Don't add duplicates */ |
548 | 0 | if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { |
549 | 0 | OPENSSL_free(emtmp); |
550 | 0 | return 1; |
551 | 0 | } |
552 | 0 | if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { |
553 | 0 | OPENSSL_free(emtmp); /* free on push failure */ |
554 | 0 | X509_email_free(*sk); |
555 | 0 | *sk = NULL; |
556 | 0 | return 0; |
557 | 0 | } |
558 | 0 | return 1; |
559 | 0 | } |
560 | | |
561 | | void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
562 | 0 | { |
563 | 0 | sk_OPENSSL_STRING_pop_free(sk, str_free); |
564 | 0 | } |
565 | | |
566 | | typedef int (*equal_fn) (const unsigned char *pattern, size_t pattern_len, |
567 | | const unsigned char *subject, size_t subject_len, |
568 | | unsigned int flags); |
569 | | |
570 | | /* Skip pattern prefix to match "wildcard" subject */ |
571 | | static void skip_prefix(const unsigned char **p, size_t *plen, |
572 | | size_t subject_len, |
573 | | unsigned int flags) |
574 | 0 | { |
575 | 0 | const unsigned char *pattern = *p; |
576 | 0 | size_t pattern_len = *plen; |
577 | | |
578 | | /* |
579 | | * If subject starts with a leading '.' followed by more octets, and |
580 | | * pattern is longer, compare just an equal-length suffix with the |
581 | | * full subject (starting at the '.'), provided the prefix contains |
582 | | * no NULs. |
583 | | */ |
584 | 0 | if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0) |
585 | 0 | return; |
586 | | |
587 | 0 | while (pattern_len > subject_len && *pattern) { |
588 | 0 | if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && |
589 | 0 | *pattern == '.') |
590 | 0 | break; |
591 | 0 | ++pattern; |
592 | 0 | --pattern_len; |
593 | 0 | } |
594 | | |
595 | | /* Skip if entire prefix acceptable */ |
596 | 0 | if (pattern_len == subject_len) { |
597 | 0 | *p = pattern; |
598 | 0 | *plen = pattern_len; |
599 | 0 | } |
600 | 0 | } |
601 | | |
602 | | /* Compare while ASCII ignoring case. */ |
603 | | static int equal_nocase(const unsigned char *pattern, size_t pattern_len, |
604 | | const unsigned char *subject, size_t subject_len, |
605 | | unsigned int flags) |
606 | 0 | { |
607 | 0 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
608 | 0 | if (pattern_len != subject_len) |
609 | 0 | return 0; |
610 | 0 | while (pattern_len) { |
611 | 0 | unsigned char l = *pattern; |
612 | 0 | unsigned char r = *subject; |
613 | | /* The pattern must not contain NUL characters. */ |
614 | 0 | if (l == 0) |
615 | 0 | return 0; |
616 | 0 | if (l != r) { |
617 | 0 | if ('A' <= l && l <= 'Z') |
618 | 0 | l = (l - 'A') + 'a'; |
619 | 0 | if ('A' <= r && r <= 'Z') |
620 | 0 | r = (r - 'A') + 'a'; |
621 | 0 | if (l != r) |
622 | 0 | return 0; |
623 | 0 | } |
624 | 0 | ++pattern; |
625 | 0 | ++subject; |
626 | 0 | --pattern_len; |
627 | 0 | } |
628 | 0 | return 1; |
629 | 0 | } |
630 | | |
631 | | /* Compare using memcmp. */ |
632 | | static int equal_case(const unsigned char *pattern, size_t pattern_len, |
633 | | const unsigned char *subject, size_t subject_len, |
634 | | unsigned int flags) |
635 | 0 | { |
636 | 0 | skip_prefix(&pattern, &pattern_len, subject_len, flags); |
637 | 0 | if (pattern_len != subject_len) |
638 | 0 | return 0; |
639 | 0 | return !memcmp(pattern, subject, pattern_len); |
640 | 0 | } |
641 | | |
642 | | /* |
643 | | * RFC 5280, section 7.5, requires that only the domain is compared in a |
644 | | * case-insensitive manner. |
645 | | */ |
646 | | static int equal_email(const unsigned char *a, size_t a_len, |
647 | | const unsigned char *b, size_t b_len, |
648 | | unsigned int unused_flags) |
649 | 0 | { |
650 | 0 | size_t i = a_len; |
651 | 0 | if (a_len != b_len) |
652 | 0 | return 0; |
653 | | /* |
654 | | * We search backwards for the '@' character, so that we do not have to |
655 | | * deal with quoted local-parts. The domain part is compared in a |
656 | | * case-insensitive manner. |
657 | | */ |
658 | 0 | while (i > 0) { |
659 | 0 | --i; |
660 | 0 | if (a[i] == '@' || b[i] == '@') { |
661 | 0 | if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) |
662 | 0 | return 0; |
663 | 0 | break; |
664 | 0 | } |
665 | 0 | } |
666 | 0 | if (i == 0) |
667 | 0 | i = a_len; |
668 | 0 | return equal_case(a, i, b, i, 0); |
669 | 0 | } |
670 | | |
671 | | /* |
672 | | * Compare the prefix and suffix with the subject, and check that the |
673 | | * characters in-between are valid. |
674 | | */ |
675 | | static int wildcard_match(const unsigned char *prefix, size_t prefix_len, |
676 | | const unsigned char *suffix, size_t suffix_len, |
677 | | const unsigned char *subject, size_t subject_len, |
678 | | unsigned int flags) |
679 | 0 | { |
680 | 0 | const unsigned char *wildcard_start; |
681 | 0 | const unsigned char *wildcard_end; |
682 | 0 | const unsigned char *p; |
683 | 0 | int allow_multi = 0; |
684 | 0 | int allow_idna = 0; |
685 | |
|
686 | 0 | if (subject_len < prefix_len + suffix_len) |
687 | 0 | return 0; |
688 | 0 | if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) |
689 | 0 | return 0; |
690 | 0 | wildcard_start = subject + prefix_len; |
691 | 0 | wildcard_end = subject + (subject_len - suffix_len); |
692 | 0 | if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) |
693 | 0 | return 0; |
694 | | /* |
695 | | * If the wildcard makes up the entire first label, it must match at |
696 | | * least one character. |
697 | | */ |
698 | 0 | if (prefix_len == 0 && *suffix == '.') { |
699 | 0 | if (wildcard_start == wildcard_end) |
700 | 0 | return 0; |
701 | 0 | allow_idna = 1; |
702 | 0 | if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS) |
703 | 0 | allow_multi = 1; |
704 | 0 | } |
705 | | /* IDNA labels cannot match partial wildcards */ |
706 | 0 | if (!allow_idna && |
707 | 0 | subject_len >= 4 && strncasecmp((char *)subject, "xn--", 4) == 0) |
708 | 0 | return 0; |
709 | | /* The wildcard may match a literal '*' */ |
710 | 0 | if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') |
711 | 0 | return 1; |
712 | | /* |
713 | | * Check that the part matched by the wildcard contains only |
714 | | * permitted characters and only matches a single label unless |
715 | | * allow_multi is set. |
716 | | */ |
717 | 0 | for (p = wildcard_start; p != wildcard_end; ++p) |
718 | 0 | if (!(('0' <= *p && *p <= '9') || |
719 | 0 | ('A' <= *p && *p <= 'Z') || |
720 | 0 | ('a' <= *p && *p <= 'z') || |
721 | 0 | *p == '-' || (allow_multi && *p == '.'))) |
722 | 0 | return 0; |
723 | 0 | return 1; |
724 | 0 | } |
725 | | |
726 | 0 | #define LABEL_START (1 << 0) |
727 | | #define LABEL_END (1 << 1) |
728 | 0 | #define LABEL_HYPHEN (1 << 2) |
729 | 0 | #define LABEL_IDNA (1 << 3) |
730 | | |
731 | | static const unsigned char *valid_star(const unsigned char *p, size_t len, |
732 | | unsigned int flags) |
733 | 0 | { |
734 | 0 | const unsigned char *star = 0; |
735 | 0 | size_t i; |
736 | 0 | int state = LABEL_START; |
737 | 0 | int dots = 0; |
738 | 0 | for (i = 0; i < len; ++i) { |
739 | | /* |
740 | | * Locate first and only legal wildcard, either at the start |
741 | | * or end of a non-IDNA first and not final label. |
742 | | */ |
743 | 0 | if (p[i] == '*') { |
744 | 0 | int atstart = (state & LABEL_START); |
745 | 0 | int atend = (i == len - 1 || p[i + 1] == '.'); |
746 | | /*- |
747 | | * At most one wildcard per pattern. |
748 | | * No wildcards in IDNA labels. |
749 | | * No wildcards after the first label. |
750 | | */ |
751 | 0 | if (star != NULL || (state & LABEL_IDNA) != 0 || dots) |
752 | 0 | return NULL; |
753 | | /* Only full-label '*.example.com' wildcards? */ |
754 | 0 | if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS) |
755 | 0 | && (!atstart || !atend)) |
756 | 0 | return NULL; |
757 | | /* No 'foo*bar' wildcards */ |
758 | 0 | if (!atstart && !atend) |
759 | 0 | return NULL; |
760 | 0 | star = &p[i]; |
761 | 0 | state &= ~LABEL_START; |
762 | 0 | } else if (('a' <= p[i] && p[i] <= 'z') |
763 | 0 | || ('A' <= p[i] && p[i] <= 'Z') |
764 | 0 | || ('0' <= p[i] && p[i] <= '9')) { |
765 | 0 | if ((state & LABEL_START) != 0 |
766 | 0 | && len - i >= 4 && strncasecmp((char *)&p[i], "xn--", 4) == 0) |
767 | 0 | state |= LABEL_IDNA; |
768 | 0 | state &= ~(LABEL_HYPHEN | LABEL_START); |
769 | 0 | } else if (p[i] == '.') { |
770 | 0 | if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) |
771 | 0 | return NULL; |
772 | 0 | state = LABEL_START; |
773 | 0 | ++dots; |
774 | 0 | } else if (p[i] == '-') { |
775 | | /* no domain/subdomain starts with '-' */ |
776 | 0 | if ((state & LABEL_START) != 0) |
777 | 0 | return NULL; |
778 | 0 | state |= LABEL_HYPHEN; |
779 | 0 | } else |
780 | 0 | return NULL; |
781 | 0 | } |
782 | | |
783 | | /* |
784 | | * The final label must not end in a hyphen or ".", and |
785 | | * there must be at least two dots after the star. |
786 | | */ |
787 | 0 | if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) |
788 | 0 | return NULL; |
789 | 0 | return star; |
790 | 0 | } |
791 | | |
792 | | /* Compare using wildcards. */ |
793 | | static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, |
794 | | const unsigned char *subject, size_t subject_len, |
795 | | unsigned int flags) |
796 | 0 | { |
797 | 0 | const unsigned char *star = NULL; |
798 | | |
799 | | /* |
800 | | * Subject names starting with '.' can only match a wildcard pattern |
801 | | * via a subject sub-domain pattern suffix match. |
802 | | */ |
803 | 0 | if (!(subject_len > 1 && subject[0] == '.')) |
804 | 0 | star = valid_star(pattern, pattern_len, flags); |
805 | 0 | if (star == NULL) |
806 | 0 | return equal_nocase(pattern, pattern_len, |
807 | 0 | subject, subject_len, flags); |
808 | 0 | return wildcard_match(pattern, star - pattern, |
809 | 0 | star + 1, (pattern + pattern_len) - star - 1, |
810 | 0 | subject, subject_len, flags); |
811 | 0 | } |
812 | | |
813 | | /* |
814 | | * Compare an ASN1_STRING to a supplied string. If they match return 1. If |
815 | | * cmp_type > 0 only compare if string matches the type, otherwise convert it |
816 | | * to UTF8. |
817 | | */ |
818 | | |
819 | | static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal, |
820 | | unsigned int flags, const char *b, size_t blen, |
821 | | char **peername) |
822 | 0 | { |
823 | 0 | int rv = 0; |
824 | |
|
825 | 0 | if (!a->data || !a->length) |
826 | 0 | return 0; |
827 | 0 | if (cmp_type > 0) { |
828 | 0 | if (cmp_type != a->type) |
829 | 0 | return 0; |
830 | 0 | if (cmp_type == V_ASN1_IA5STRING) |
831 | 0 | rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); |
832 | 0 | else if (a->length == (int)blen && !memcmp(a->data, b, blen)) |
833 | 0 | rv = 1; |
834 | 0 | if (rv > 0 && peername != NULL) { |
835 | 0 | *peername = OPENSSL_strndup((char *)a->data, a->length); |
836 | 0 | if (*peername == NULL) |
837 | 0 | return -1; |
838 | 0 | } |
839 | 0 | } else { |
840 | 0 | int astrlen; |
841 | 0 | unsigned char *astr; |
842 | 0 | astrlen = ASN1_STRING_to_UTF8(&astr, a); |
843 | 0 | if (astrlen < 0) { |
844 | | /* |
845 | | * -1 could be an internal malloc failure or a decoding error from |
846 | | * malformed input; we can't distinguish. |
847 | | */ |
848 | 0 | return -1; |
849 | 0 | } |
850 | 0 | rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); |
851 | 0 | if (rv > 0 && peername != NULL) { |
852 | 0 | *peername = OPENSSL_strndup((char *)astr, astrlen); |
853 | 0 | if (*peername == NULL) { |
854 | 0 | OPENSSL_free(astr); |
855 | 0 | return -1; |
856 | 0 | } |
857 | 0 | } |
858 | 0 | OPENSSL_free(astr); |
859 | 0 | } |
860 | 0 | return rv; |
861 | 0 | } |
862 | | |
863 | | static int do_x509_check(X509 *x, const char *chk, size_t chklen, |
864 | | unsigned int flags, int check_type, char **peername) |
865 | 0 | { |
866 | 0 | GENERAL_NAMES *gens = NULL; |
867 | 0 | X509_NAME *name = NULL; |
868 | 0 | int i; |
869 | 0 | int cnid = NID_undef; |
870 | 0 | int alt_type; |
871 | 0 | int san_present = 0; |
872 | 0 | int rv = 0; |
873 | 0 | equal_fn equal; |
874 | | |
875 | | /* See below, this flag is internal-only */ |
876 | 0 | flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS; |
877 | 0 | if (check_type == GEN_EMAIL) { |
878 | 0 | cnid = NID_pkcs9_emailAddress; |
879 | 0 | alt_type = V_ASN1_IA5STRING; |
880 | 0 | equal = equal_email; |
881 | 0 | } else if (check_type == GEN_DNS) { |
882 | 0 | cnid = NID_commonName; |
883 | | /* Implicit client-side DNS sub-domain pattern */ |
884 | 0 | if (chklen > 1 && chk[0] == '.') |
885 | 0 | flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS; |
886 | 0 | alt_type = V_ASN1_IA5STRING; |
887 | 0 | if (flags & X509_CHECK_FLAG_NO_WILDCARDS) |
888 | 0 | equal = equal_nocase; |
889 | 0 | else |
890 | 0 | equal = equal_wildcard; |
891 | 0 | } else { |
892 | 0 | alt_type = V_ASN1_OCTET_STRING; |
893 | 0 | equal = equal_case; |
894 | 0 | } |
895 | |
|
896 | 0 | if (chklen == 0) |
897 | 0 | chklen = strlen(chk); |
898 | |
|
899 | 0 | gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
900 | 0 | if (gens) { |
901 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
902 | 0 | GENERAL_NAME *gen; |
903 | 0 | ASN1_STRING *cstr; |
904 | 0 | gen = sk_GENERAL_NAME_value(gens, i); |
905 | 0 | if (gen->type != check_type) |
906 | 0 | continue; |
907 | 0 | san_present = 1; |
908 | 0 | if (check_type == GEN_EMAIL) |
909 | 0 | cstr = gen->d.rfc822Name; |
910 | 0 | else if (check_type == GEN_DNS) |
911 | 0 | cstr = gen->d.dNSName; |
912 | 0 | else |
913 | 0 | cstr = gen->d.iPAddress; |
914 | | /* Positive on success, negative on error! */ |
915 | 0 | if ((rv = do_check_string(cstr, alt_type, equal, flags, |
916 | 0 | chk, chklen, peername)) != 0) |
917 | 0 | break; |
918 | 0 | } |
919 | 0 | GENERAL_NAMES_free(gens); |
920 | 0 | if (rv != 0) |
921 | 0 | return rv; |
922 | 0 | if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)) |
923 | 0 | return 0; |
924 | 0 | } |
925 | | |
926 | | /* We're done if CN-ID is not pertinent */ |
927 | 0 | if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) |
928 | 0 | return 0; |
929 | | |
930 | 0 | i = -1; |
931 | 0 | name = X509_get_subject_name(x); |
932 | 0 | while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) { |
933 | 0 | const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); |
934 | 0 | const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne); |
935 | | |
936 | | /* Positive on success, negative on error! */ |
937 | 0 | if ((rv = do_check_string(str, -1, equal, flags, |
938 | 0 | chk, chklen, peername)) != 0) |
939 | 0 | return rv; |
940 | 0 | } |
941 | 0 | return 0; |
942 | 0 | } |
943 | | |
944 | | int X509_check_host(X509 *x, const char *chk, size_t chklen, |
945 | | unsigned int flags, char **peername) |
946 | 0 | { |
947 | 0 | if (chk == NULL) |
948 | 0 | return -2; |
949 | | /* |
950 | | * Embedded NULs are disallowed, except as the last character of a |
951 | | * string of length 2 or more (tolerate caller including terminating |
952 | | * NUL in string length). |
953 | | */ |
954 | 0 | if (chklen == 0) |
955 | 0 | chklen = strlen(chk); |
956 | 0 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
957 | 0 | return -2; |
958 | 0 | if (chklen > 1 && chk[chklen - 1] == '\0') |
959 | 0 | --chklen; |
960 | 0 | return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername); |
961 | 0 | } |
962 | | |
963 | | int X509_check_email(X509 *x, const char *chk, size_t chklen, |
964 | | unsigned int flags) |
965 | 0 | { |
966 | 0 | if (chk == NULL) |
967 | 0 | return -2; |
968 | | /* |
969 | | * Embedded NULs are disallowed, except as the last character of a |
970 | | * string of length 2 or more (tolerate caller including terminating |
971 | | * NUL in string length). |
972 | | */ |
973 | 0 | if (chklen == 0) |
974 | 0 | chklen = strlen((char *)chk); |
975 | 0 | else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
976 | 0 | return -2; |
977 | 0 | if (chklen > 1 && chk[chklen - 1] == '\0') |
978 | 0 | --chklen; |
979 | 0 | return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL); |
980 | 0 | } |
981 | | |
982 | | int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, |
983 | | unsigned int flags) |
984 | 0 | { |
985 | 0 | if (chk == NULL) |
986 | 0 | return -2; |
987 | 0 | return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL); |
988 | 0 | } |
989 | | |
990 | | int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) |
991 | 0 | { |
992 | 0 | unsigned char ipout[16]; |
993 | 0 | size_t iplen; |
994 | |
|
995 | 0 | if (ipasc == NULL) |
996 | 0 | return -2; |
997 | 0 | iplen = (size_t)a2i_ipadd(ipout, ipasc); |
998 | 0 | if (iplen == 0) |
999 | 0 | return -2; |
1000 | 0 | return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL); |
1001 | 0 | } |
1002 | | |
1003 | | /* |
1004 | | * Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible |
1005 | | * with RFC3280. |
1006 | | */ |
1007 | | |
1008 | | ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) |
1009 | 0 | { |
1010 | 0 | unsigned char ipout[16]; |
1011 | 0 | ASN1_OCTET_STRING *ret; |
1012 | 0 | int iplen; |
1013 | | |
1014 | | /* If string contains a ':' assume IPv6 */ |
1015 | |
|
1016 | 0 | iplen = a2i_ipadd(ipout, ipasc); |
1017 | |
|
1018 | 0 | if (!iplen) |
1019 | 0 | return NULL; |
1020 | | |
1021 | 0 | ret = ASN1_OCTET_STRING_new(); |
1022 | 0 | if (ret == NULL) |
1023 | 0 | return NULL; |
1024 | 0 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { |
1025 | 0 | ASN1_OCTET_STRING_free(ret); |
1026 | 0 | return NULL; |
1027 | 0 | } |
1028 | 0 | return ret; |
1029 | 0 | } |
1030 | | |
1031 | | ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
1032 | 0 | { |
1033 | 0 | ASN1_OCTET_STRING *ret = NULL; |
1034 | 0 | unsigned char ipout[32]; |
1035 | 0 | char *iptmp = NULL, *p; |
1036 | 0 | int iplen1, iplen2; |
1037 | 0 | p = strchr(ipasc, '/'); |
1038 | 0 | if (!p) |
1039 | 0 | return NULL; |
1040 | 0 | iptmp = OPENSSL_strdup(ipasc); |
1041 | 0 | if (!iptmp) |
1042 | 0 | return NULL; |
1043 | 0 | p = iptmp + (p - ipasc); |
1044 | 0 | *p++ = 0; |
1045 | |
|
1046 | 0 | iplen1 = a2i_ipadd(ipout, iptmp); |
1047 | |
|
1048 | 0 | if (!iplen1) |
1049 | 0 | goto err; |
1050 | | |
1051 | 0 | iplen2 = a2i_ipadd(ipout + iplen1, p); |
1052 | |
|
1053 | 0 | OPENSSL_free(iptmp); |
1054 | 0 | iptmp = NULL; |
1055 | |
|
1056 | 0 | if (!iplen2 || (iplen1 != iplen2)) |
1057 | 0 | goto err; |
1058 | | |
1059 | 0 | ret = ASN1_OCTET_STRING_new(); |
1060 | 0 | if (ret == NULL) |
1061 | 0 | goto err; |
1062 | 0 | if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) |
1063 | 0 | goto err; |
1064 | | |
1065 | 0 | return ret; |
1066 | | |
1067 | 0 | err: |
1068 | 0 | OPENSSL_free(iptmp); |
1069 | 0 | ASN1_OCTET_STRING_free(ret); |
1070 | 0 | return NULL; |
1071 | 0 | } |
1072 | | |
1073 | | int a2i_ipadd(unsigned char *ipout, const char *ipasc) |
1074 | 0 | { |
1075 | | /* If string contains a ':' assume IPv6 */ |
1076 | |
|
1077 | 0 | if (strchr(ipasc, ':')) { |
1078 | 0 | if (!ipv6_from_asc(ipout, ipasc)) |
1079 | 0 | return 0; |
1080 | 0 | return 16; |
1081 | 0 | } else { |
1082 | 0 | if (!ipv4_from_asc(ipout, ipasc)) |
1083 | 0 | return 0; |
1084 | 0 | return 4; |
1085 | 0 | } |
1086 | 0 | } |
1087 | | |
1088 | | static int ipv4_from_asc(unsigned char *v4, const char *in) |
1089 | 0 | { |
1090 | 0 | const char *p; |
1091 | 0 | int a0, a1, a2, a3, n; |
1092 | |
|
1093 | 0 | if (sscanf(in, "%d.%d.%d.%d%n", &a0, &a1, &a2, &a3, &n) != 4) |
1094 | 0 | return 0; |
1095 | 0 | if ((a0 < 0) || (a0 > 255) || (a1 < 0) || (a1 > 255) |
1096 | 0 | || (a2 < 0) || (a2 > 255) || (a3 < 0) || (a3 > 255)) |
1097 | 0 | return 0; |
1098 | 0 | p = in + n; |
1099 | 0 | if (!(*p == '\0' || ossl_isspace(*p))) |
1100 | 0 | return 0; |
1101 | 0 | v4[0] = a0; |
1102 | 0 | v4[1] = a1; |
1103 | 0 | v4[2] = a2; |
1104 | 0 | v4[3] = a3; |
1105 | 0 | return 1; |
1106 | 0 | } |
1107 | | |
1108 | | typedef struct { |
1109 | | /* Temporary store for IPV6 output */ |
1110 | | unsigned char tmp[16]; |
1111 | | /* Total number of bytes in tmp */ |
1112 | | int total; |
1113 | | /* The position of a zero (corresponding to '::') */ |
1114 | | int zero_pos; |
1115 | | /* Number of zeroes */ |
1116 | | int zero_cnt; |
1117 | | } IPV6_STAT; |
1118 | | |
1119 | | static int ipv6_from_asc(unsigned char *v6, const char *in) |
1120 | 0 | { |
1121 | 0 | IPV6_STAT v6stat; |
1122 | 0 | v6stat.total = 0; |
1123 | 0 | v6stat.zero_pos = -1; |
1124 | 0 | v6stat.zero_cnt = 0; |
1125 | | /* |
1126 | | * Treat the IPv6 representation as a list of values separated by ':'. |
1127 | | * The presence of a '::' will parse as one, two or three zero length |
1128 | | * elements. |
1129 | | */ |
1130 | 0 | if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) |
1131 | 0 | return 0; |
1132 | | |
1133 | | /* Now for some sanity checks */ |
1134 | | |
1135 | 0 | if (v6stat.zero_pos == -1) { |
1136 | | /* If no '::' must have exactly 16 bytes */ |
1137 | 0 | if (v6stat.total != 16) |
1138 | 0 | return 0; |
1139 | 0 | } else { |
1140 | | /* If '::' must have less than 16 bytes */ |
1141 | 0 | if (v6stat.total == 16) |
1142 | 0 | return 0; |
1143 | | /* More than three zeroes is an error */ |
1144 | 0 | if (v6stat.zero_cnt > 3) |
1145 | 0 | return 0; |
1146 | | /* Can only have three zeroes if nothing else present */ |
1147 | 0 | else if (v6stat.zero_cnt == 3) { |
1148 | 0 | if (v6stat.total > 0) |
1149 | 0 | return 0; |
1150 | 0 | } |
1151 | | /* Can only have two zeroes if at start or end */ |
1152 | 0 | else if (v6stat.zero_cnt == 2) { |
1153 | 0 | if ((v6stat.zero_pos != 0) |
1154 | 0 | && (v6stat.zero_pos != v6stat.total)) |
1155 | 0 | return 0; |
1156 | 0 | } else |
1157 | | /* Can only have one zero if *not* start or end */ |
1158 | 0 | { |
1159 | 0 | if ((v6stat.zero_pos == 0) |
1160 | 0 | || (v6stat.zero_pos == v6stat.total)) |
1161 | 0 | return 0; |
1162 | 0 | } |
1163 | 0 | } |
1164 | | |
1165 | | /* Format result */ |
1166 | | |
1167 | 0 | if (v6stat.zero_pos >= 0) { |
1168 | | /* Copy initial part */ |
1169 | 0 | memcpy(v6, v6stat.tmp, v6stat.zero_pos); |
1170 | | /* Zero middle */ |
1171 | 0 | memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); |
1172 | | /* Copy final part */ |
1173 | 0 | if (v6stat.total != v6stat.zero_pos) |
1174 | 0 | memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, |
1175 | 0 | v6stat.tmp + v6stat.zero_pos, |
1176 | 0 | v6stat.total - v6stat.zero_pos); |
1177 | 0 | } else |
1178 | 0 | memcpy(v6, v6stat.tmp, 16); |
1179 | |
|
1180 | 0 | return 1; |
1181 | 0 | } |
1182 | | |
1183 | | static int ipv6_cb(const char *elem, int len, void *usr) |
1184 | 0 | { |
1185 | 0 | IPV6_STAT *s = usr; |
1186 | | /* Error if 16 bytes written */ |
1187 | 0 | if (s->total == 16) |
1188 | 0 | return 0; |
1189 | 0 | if (len == 0) { |
1190 | | /* Zero length element, corresponds to '::' */ |
1191 | 0 | if (s->zero_pos == -1) |
1192 | 0 | s->zero_pos = s->total; |
1193 | | /* If we've already got a :: its an error */ |
1194 | 0 | else if (s->zero_pos != s->total) |
1195 | 0 | return 0; |
1196 | 0 | s->zero_cnt++; |
1197 | 0 | } else { |
1198 | | /* If more than 4 characters could be final a.b.c.d form */ |
1199 | 0 | if (len > 4) { |
1200 | | /* Need at least 4 bytes left */ |
1201 | 0 | if (s->total > 12) |
1202 | 0 | return 0; |
1203 | | /* Must be end of string */ |
1204 | 0 | if (elem[len]) |
1205 | 0 | return 0; |
1206 | 0 | if (!ipv4_from_asc(s->tmp + s->total, elem)) |
1207 | 0 | return 0; |
1208 | 0 | s->total += 4; |
1209 | 0 | } else { |
1210 | 0 | if (!ipv6_hex(s->tmp + s->total, elem, len)) |
1211 | 0 | return 0; |
1212 | 0 | s->total += 2; |
1213 | 0 | } |
1214 | 0 | } |
1215 | 0 | return 1; |
1216 | 0 | } |
1217 | | |
1218 | | /* |
1219 | | * Convert a string of up to 4 hex digits into the corresponding IPv6 form. |
1220 | | */ |
1221 | | |
1222 | | static int ipv6_hex(unsigned char *out, const char *in, int inlen) |
1223 | 0 | { |
1224 | 0 | unsigned char c; |
1225 | 0 | unsigned int num = 0; |
1226 | 0 | int x; |
1227 | |
|
1228 | 0 | if (inlen > 4) |
1229 | 0 | return 0; |
1230 | 0 | while (inlen--) { |
1231 | 0 | c = *in++; |
1232 | 0 | num <<= 4; |
1233 | 0 | x = OPENSSL_hexchar2int(c); |
1234 | 0 | if (x < 0) |
1235 | 0 | return 0; |
1236 | 0 | num |= (char)x; |
1237 | 0 | } |
1238 | 0 | out[0] = num >> 8; |
1239 | 0 | out[1] = num & 0xff; |
1240 | 0 | return 1; |
1241 | 0 | } |
1242 | | |
1243 | | int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk, |
1244 | | unsigned long chtype) |
1245 | 0 | { |
1246 | 0 | CONF_VALUE *v; |
1247 | 0 | int i, mval, spec_char, plus_char; |
1248 | 0 | char *p, *type; |
1249 | 0 | if (!nm) |
1250 | 0 | return 0; |
1251 | | |
1252 | 0 | for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { |
1253 | 0 | v = sk_CONF_VALUE_value(dn_sk, i); |
1254 | 0 | type = v->name; |
1255 | | /* |
1256 | | * Skip past any leading X. X: X, etc to allow for multiple instances |
1257 | | */ |
1258 | 0 | for (p = type; *p; p++) { |
1259 | 0 | #ifndef CHARSET_EBCDIC |
1260 | 0 | spec_char = ((*p == ':') || (*p == ',') || (*p == '.')); |
1261 | | #else |
1262 | | spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[',']) |
1263 | | || (*p == os_toascii['.'])); |
1264 | | #endif |
1265 | 0 | if (spec_char) { |
1266 | 0 | p++; |
1267 | 0 | if (*p) |
1268 | 0 | type = p; |
1269 | 0 | break; |
1270 | 0 | } |
1271 | 0 | } |
1272 | 0 | #ifndef CHARSET_EBCDIC |
1273 | 0 | plus_char = (*type == '+'); |
1274 | | #else |
1275 | | plus_char = (*type == os_toascii['+']); |
1276 | | #endif |
1277 | 0 | if (plus_char) { |
1278 | 0 | mval = -1; |
1279 | 0 | type++; |
1280 | 0 | } else |
1281 | 0 | mval = 0; |
1282 | 0 | if (!X509_NAME_add_entry_by_txt(nm, type, chtype, |
1283 | 0 | (unsigned char *)v->value, -1, -1, |
1284 | 0 | mval)) |
1285 | 0 | return 0; |
1286 | |
|
1287 | 0 | } |
1288 | 0 | return 1; |
1289 | 0 | } |