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