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