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