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