| 1 |
/* |
| 2 |
* Copyright 1999-2026 The OpenSSL Project Authors. All Rights Reserved. |
| 3 |
* |
| 4 |
* Licensed under the Apache License 2.0 (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 "internal/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 |
#include "x509_local.h" |
| 24 |
|
| 25 |
static char *strip_spaces(char *name); |
| 26 |
static int sk_strcmp(const char *const *a, const char *const *b); |
| 27 |
static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name, |
| 28 |
GENERAL_NAMES *gens); |
| 29 |
static void str_free(OPENSSL_STRING str); |
| 30 |
static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, |
| 31 |
const ASN1_IA5STRING *email); |
| 32 |
|
| 33 |
static int ipv4_from_asc(unsigned char *v4, const char *in); |
| 34 |
static int ipv6_from_asc(unsigned char *v6, const char *in); |
| 35 |
static int ipv6_cb(const char *elem, int len, void *usr); |
| 36 |
static int ipv6_hex(unsigned char *out, const char *in, int inlen); |
| 37 |
|
| 38 |
/* Add a CONF_VALUE name value pair to stack */ |
| 39 |
|
| 40 |
static int x509v3_add_len_value(const char *name, const char *value, |
| 41 |
size_t vallen, STACK_OF(CONF_VALUE) **extlist) |
| 42 |
{ |
| 43 |
CONF_VALUE *vtmp = NULL; |
| 44 |
char *tname = NULL, *tvalue = NULL; |
| 45 |
int sk_allocated = (*extlist == NULL); |
| 46 |
|
| 47 |
if (name != NULL && (tname = OPENSSL_strdup(name)) == NULL) |
| 48 |
goto err; |
| 49 |
if (value != NULL) { |
| 50 |
/* We don't allow embedded NUL characters */ |
| 51 |
if (memchr(value, 0, vallen) != NULL) |
| 52 |
goto err; |
| 53 |
tvalue = OPENSSL_strndup(value, vallen); |
| 54 |
if (tvalue == NULL) |
| 55 |
goto err; |
| 56 |
} |
| 57 |
if ((vtmp = OPENSSL_malloc(sizeof(*vtmp))) == NULL) |
| 58 |
goto err; |
| 59 |
if (sk_allocated && (*extlist = sk_CONF_VALUE_new_null()) == NULL) { |
| 60 |
ERR_raise(ERR_LIB_X509V3, ERR_R_CRYPTO_LIB); |
| 61 |
goto err; |
| 62 |
} |
| 63 |
vtmp->section = NULL; |
| 64 |
vtmp->name = tname; |
| 65 |
vtmp->value = tvalue; |
| 66 |
if (!sk_CONF_VALUE_push(*extlist, vtmp)) |
| 67 |
goto err; |
| 68 |
return 1; |
| 69 |
err: |
| 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 |
OPENSSL_free(tmp); |
| 151 |
return NULL; |
| 152 |
} |
| 153 |
|
| 154 |
/* Prepend "0x", but place it after the "-" if negative. */ |
| 155 |
if (tmp[0] == '-') { |
| 156 |
OPENSSL_strlcpy(ret, "-0x", len); |
| 157 |
OPENSSL_strlcat(ret, tmp + 1, len); |
| 158 |
} else { |
| 159 |
OPENSSL_strlcpy(ret, "0x", len); |
| 160 |
OPENSSL_strlcat(ret, tmp, len); |
| 161 |
} |
| 162 |
OPENSSL_free(tmp); |
| 163 |
return ret; |
| 164 |
} |
| 165 |
|
| 166 |
char *i2s_ASN1_ENUMERATED(X509V3_EXT_METHOD *method, const ASN1_ENUMERATED *a) |
| 167 |
{ |
| 168 |
BIGNUM *bntmp = NULL; |
| 169 |
char *strtmp = NULL; |
| 170 |
|
| 171 |
if (!a) |
| 172 |
return NULL; |
| 173 |
if ((bntmp = ASN1_ENUMERATED_to_BN(a, NULL)) == NULL) |
| 174 |
ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
| 175 |
else if ((strtmp = bignum_to_string(bntmp)) == NULL) |
| 176 |
ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB); |
| 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 |
ERR_raise(ERR_LIB_X509V3, ERR_R_ASN1_LIB); |
| 190 |
else if ((strtmp = bignum_to_string(bntmp)) == NULL) |
| 191 |
ERR_raise(ERR_LIB_X509V3, ERR_R_X509V3_LIB); |
| 192 |
BN_free(bntmp); |
| 193 |
return strtmp; |
| 194 |
} |
| 195 |
|
| 196 |
ASN1_INTEGER *s2i_ASN1_INTEGER(X509V3_EXT_METHOD *method, const char *value) |
| 197 |
{ |
| 198 |
BIGNUM *bn = NULL; |
| 199 |
ASN1_INTEGER *aint; |
| 200 |
int isneg, ishex; |
| 201 |
int ret; |
| 202 |
|
| 203 |
if (value == NULL) { |
| 204 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 205 |
return NULL; |
| 206 |
} |
| 207 |
bn = BN_new(); |
| 208 |
if (bn == NULL) { |
| 209 |
ERR_raise(ERR_LIB_X509V3, ERR_R_BN_LIB); |
| 210 |
return NULL; |
| 211 |
} |
| 212 |
if (value[0] == '-') { |
| 213 |
value++; |
| 214 |
isneg = 1; |
| 215 |
} else { |
| 216 |
isneg = 0; |
| 217 |
} |
| 218 |
|
| 219 |
if (value[0] == '0' && ((value[1] == 'x') || (value[1] == 'X'))) { |
| 220 |
value += 2; |
| 221 |
ishex = 1; |
| 222 |
} else { |
| 223 |
ishex = 0; |
| 224 |
} |
| 225 |
|
| 226 |
if (ishex) |
| 227 |
ret = BN_hex2bn(&bn, value); |
| 228 |
else |
| 229 |
ret = BN_dec2bn(&bn, value); |
| 230 |
|
| 231 |
if (!ret || value[ret]) { |
| 232 |
BN_free(bn); |
| 233 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_DEC2BN_ERROR); |
| 234 |
return NULL; |
| 235 |
} |
| 236 |
|
| 237 |
if (isneg && BN_is_zero(bn)) |
| 238 |
isneg = 0; |
| 239 |
|
| 240 |
aint = BN_to_ASN1_INTEGER(bn, NULL); |
| 241 |
BN_free(bn); |
| 242 |
if (!aint) { |
| 243 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_BN_TO_ASN1_INTEGER_ERROR); |
| 244 |
return NULL; |
| 245 |
} |
| 246 |
if (isneg) |
| 247 |
aint->type |= V_ASN1_NEG; |
| 248 |
return aint; |
| 249 |
} |
| 250 |
|
| 251 |
int X509V3_add_value_int(const char *name, const ASN1_INTEGER *aint, |
| 252 |
STACK_OF(CONF_VALUE) **extlist) |
| 253 |
{ |
| 254 |
char *strtmp; |
| 255 |
int ret; |
| 256 |
|
| 257 |
if (!aint) |
| 258 |
return 1; |
| 259 |
if ((strtmp = i2s_ASN1_INTEGER(NULL, aint)) == NULL) |
| 260 |
return 0; |
| 261 |
ret = X509V3_add_value(name, strtmp, extlist); |
| 262 |
OPENSSL_free(strtmp); |
| 263 |
return ret; |
| 264 |
} |
| 265 |
|
| 266 |
int X509V3_get_value_bool(const CONF_VALUE *value, int *asn1_bool) |
| 267 |
{ |
| 268 |
const char *btmp; |
| 269 |
|
| 270 |
if ((btmp = value->value) == NULL) |
| 271 |
goto err; |
| 272 |
if (strcmp(btmp, "TRUE") == 0 |
| 273 |
|| strcmp(btmp, "true") == 0 |
| 274 |
|| strcmp(btmp, "Y") == 0 |
| 275 |
|| strcmp(btmp, "y") == 0 |
| 276 |
|| strcmp(btmp, "YES") == 0 |
| 277 |
|| strcmp(btmp, "yes") == 0) { |
| 278 |
*asn1_bool = 0xff; |
| 279 |
return 1; |
| 280 |
} |
| 281 |
if (strcmp(btmp, "FALSE") == 0 |
| 282 |
|| strcmp(btmp, "false") == 0 |
| 283 |
|| strcmp(btmp, "N") == 0 |
| 284 |
|| strcmp(btmp, "n") == 0 |
| 285 |
|| strcmp(btmp, "NO") == 0 |
| 286 |
|| strcmp(btmp, "no") == 0) { |
| 287 |
*asn1_bool = 0; |
| 288 |
return 1; |
| 289 |
} |
| 290 |
err: |
| 291 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_BOOLEAN_STRING); |
| 292 |
X509V3_conf_add_error_name_value(value); |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
int X509V3_get_value_int(const CONF_VALUE *value, ASN1_INTEGER **aint) |
| 297 |
{ |
| 298 |
ASN1_INTEGER *itmp; |
| 299 |
|
| 300 |
if ((itmp = s2i_ASN1_INTEGER(NULL, value->value)) == NULL) { |
| 301 |
X509V3_conf_add_error_name_value(value); |
| 302 |
return 0; |
| 303 |
} |
| 304 |
*aint = itmp; |
| 305 |
return 1; |
| 306 |
} |
| 307 |
|
| 308 |
#define HDR_NAME 1 |
| 309 |
#define HDR_VALUE 2 |
| 310 |
|
| 311 |
/* |
| 312 |
* #define DEBUG |
| 313 |
*/ |
| 314 |
|
| 315 |
STACK_OF(CONF_VALUE) *X509V3_parse_list(const char *line) |
| 316 |
{ |
| 317 |
char *p, *q, c; |
| 318 |
char *ntmp, *vtmp; |
| 319 |
STACK_OF(CONF_VALUE) *values = NULL; |
| 320 |
char *linebuf; |
| 321 |
int state; |
| 322 |
|
| 323 |
/* We are going to modify the line so copy it first */ |
| 324 |
linebuf = OPENSSL_strdup(line); |
| 325 |
if (linebuf == NULL) |
| 326 |
goto err; |
| 327 |
state = HDR_NAME; |
| 328 |
ntmp = NULL; |
| 329 |
/* Go through all characters */ |
| 330 |
for (p = linebuf, q = linebuf; (c = *p) && (c != '\r') && (c != '\n'); |
| 331 |
p++) { |
| 332 |
|
| 333 |
switch (state) { |
| 334 |
case HDR_NAME: |
| 335 |
if (c == ':') { |
| 336 |
state = HDR_VALUE; |
| 337 |
*p = 0; |
| 338 |
ntmp = strip_spaces(q); |
| 339 |
if (!ntmp) { |
| 340 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_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 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME); |
| 350 |
goto err; |
| 351 |
} |
| 352 |
if (!X509V3_add_value(ntmp, NULL, &values)) { |
| 353 |
goto err; |
| 354 |
} |
| 355 |
} |
| 356 |
break; |
| 357 |
|
| 358 |
case HDR_VALUE: |
| 359 |
if (c == ',') { |
| 360 |
state = HDR_NAME; |
| 361 |
*p = 0; |
| 362 |
vtmp = strip_spaces(q); |
| 363 |
if (!vtmp) { |
| 364 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 365 |
goto err; |
| 366 |
} |
| 367 |
if (!X509V3_add_value(ntmp, vtmp, &values)) { |
| 368 |
goto err; |
| 369 |
} |
| 370 |
ntmp = NULL; |
| 371 |
q = p + 1; |
| 372 |
} |
| 373 |
} |
| 374 |
} |
| 375 |
|
| 376 |
if (state == HDR_VALUE) { |
| 377 |
vtmp = strip_spaces(q); |
| 378 |
if (!vtmp) { |
| 379 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_NULL_VALUE); |
| 380 |
goto err; |
| 381 |
} |
| 382 |
if (!X509V3_add_value(ntmp, vtmp, &values)) { |
| 383 |
goto err; |
| 384 |
} |
| 385 |
} else { |
| 386 |
ntmp = strip_spaces(q); |
| 387 |
if (!ntmp) { |
| 388 |
ERR_raise(ERR_LIB_X509V3, X509V3_R_INVALID_EMPTY_NAME); |
| 389 |
goto err; |
| 390 |
} |
| 391 |
if (!X509V3_add_value(ntmp, NULL, &values)) { |
| 392 |
goto err; |
| 393 |
} |
| 394 |
} |
| 395 |
OPENSSL_free(linebuf); |
| 396 |
return values; |
| 397 |
|
| 398 |
err: |
| 399 |
OPENSSL_free(linebuf); |
| 400 |
sk_CONF_VALUE_pop_free(values, X509V3_conf_free); |
| 401 |
return NULL; |
| 402 |
} |
| 403 |
|
| 404 |
/* Delete leading and trailing spaces from a string */ |
| 405 |
static char *strip_spaces(char *name) |
| 406 |
{ |
| 407 |
char *p, *q; |
| 408 |
|
| 409 |
/* Skip over leading spaces */ |
| 410 |
p = name; |
| 411 |
while (*p && ossl_isspace(*p)) |
| 412 |
p++; |
| 413 |
if (*p == '\0') |
| 414 |
return NULL; |
| 415 |
q = p + strlen(p) - 1; |
| 416 |
while ((q != p) && ossl_isspace(*q)) |
| 417 |
q--; |
| 418 |
if (p != q) |
| 419 |
q[1] = 0; |
| 420 |
if (*p == '\0') |
| 421 |
return NULL; |
| 422 |
return p; |
| 423 |
} |
| 424 |
|
| 425 |
/* |
| 426 |
* V2I name comparison function: returns zero if 'name' matches cmp or cmp.* |
| 427 |
*/ |
| 428 |
|
| 429 |
int ossl_v3_name_cmp(const char *name, const char *cmp) |
| 430 |
{ |
| 431 |
size_t len; |
| 432 |
int ret; |
| 433 |
char c; |
| 434 |
|
| 435 |
len = strlen(cmp); |
| 436 |
if ((ret = strncmp(name, cmp, len))) |
| 437 |
return ret; |
| 438 |
c = name[len]; |
| 439 |
if (!c || (c == '.')) |
| 440 |
return 0; |
| 441 |
return 1; |
| 442 |
} |
| 443 |
|
| 444 |
static int sk_strcmp(const char *const *a, const char *const *b) |
| 445 |
{ |
| 446 |
return strcmp(*a, *b); |
| 447 |
} |
| 448 |
|
| 449 |
STACK_OF(OPENSSL_STRING) *X509_get1_email(X509 *x) |
| 450 |
{ |
| 451 |
GENERAL_NAMES *gens; |
| 452 |
STACK_OF(OPENSSL_STRING) *ret; |
| 453 |
|
| 454 |
gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 455 |
ret = get_email(X509_get_subject_name(x), gens); |
| 456 |
sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 457 |
return ret; |
| 458 |
} |
| 459 |
|
| 460 |
STACK_OF(OPENSSL_STRING) *X509_get1_ocsp(X509 *x) |
| 461 |
{ |
| 462 |
AUTHORITY_INFO_ACCESS *info; |
| 463 |
STACK_OF(OPENSSL_STRING) *ret = NULL; |
| 464 |
int i; |
| 465 |
|
| 466 |
info = X509_get_ext_d2i(x, NID_info_access, NULL, NULL); |
| 467 |
if (!info) |
| 468 |
return NULL; |
| 469 |
for (i = 0; i < sk_ACCESS_DESCRIPTION_num(info); i++) { |
| 470 |
ACCESS_DESCRIPTION *ad = sk_ACCESS_DESCRIPTION_value(info, i); |
| 471 |
if (OBJ_obj2nid(ad->method) == NID_ad_OCSP) { |
| 472 |
if (ad->location->type == GEN_URI) { |
| 473 |
if (!append_ia5(&ret, ad->location->d.uniformResourceIdentifier)) |
| 474 |
break; |
| 475 |
} |
| 476 |
} |
| 477 |
} |
| 478 |
AUTHORITY_INFO_ACCESS_free(info); |
| 479 |
return ret; |
| 480 |
} |
| 481 |
|
| 482 |
STACK_OF(OPENSSL_STRING) *X509_REQ_get1_email(X509_REQ *x) |
| 483 |
{ |
| 484 |
GENERAL_NAMES *gens; |
| 485 |
STACK_OF(X509_EXTENSION) *exts; |
| 486 |
STACK_OF(OPENSSL_STRING) *ret; |
| 487 |
|
| 488 |
exts = X509_REQ_get_extensions(x); |
| 489 |
gens = X509V3_get_d2i(exts, NID_subject_alt_name, NULL, NULL); |
| 490 |
ret = get_email(X509_REQ_get_subject_name(x), gens); |
| 491 |
sk_GENERAL_NAME_pop_free(gens, GENERAL_NAME_free); |
| 492 |
sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free); |
| 493 |
return ret; |
| 494 |
} |
| 495 |
|
| 496 |
static STACK_OF(OPENSSL_STRING) *get_email(const X509_NAME *name, |
| 497 |
GENERAL_NAMES *gens) |
| 498 |
{ |
| 499 |
STACK_OF(OPENSSL_STRING) *ret = NULL; |
| 500 |
X509_NAME_ENTRY *ne; |
| 501 |
const ASN1_IA5STRING *email; |
| 502 |
GENERAL_NAME *gen; |
| 503 |
int i = -1; |
| 504 |
|
| 505 |
/* Now add any email address(es) to STACK */ |
| 506 |
/* First supplied X509_NAME */ |
| 507 |
while ((i = X509_NAME_get_index_by_NID(name, |
| 508 |
NID_pkcs9_emailAddress, i)) |
| 509 |
>= 0) { |
| 510 |
ne = X509_NAME_get_entry(name, i); |
| 511 |
email = X509_NAME_ENTRY_get_data(ne); |
| 512 |
if (!append_ia5(&ret, email)) |
| 513 |
return NULL; |
| 514 |
} |
| 515 |
for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 516 |
gen = sk_GENERAL_NAME_value(gens, i); |
| 517 |
if (gen->type != GEN_EMAIL) |
| 518 |
continue; |
| 519 |
if (!append_ia5(&ret, gen->d.ia5)) |
| 520 |
return NULL; |
| 521 |
} |
| 522 |
return ret; |
| 523 |
} |
| 524 |
|
| 525 |
static void str_free(OPENSSL_STRING str) |
| 526 |
{ |
| 527 |
OPENSSL_free(str); |
| 528 |
} |
| 529 |
|
| 530 |
static int append_ia5(STACK_OF(OPENSSL_STRING) **sk, |
| 531 |
const ASN1_IA5STRING *email) |
| 532 |
{ |
| 533 |
char *emtmp; |
| 534 |
|
| 535 |
/* First some sanity checks */ |
| 536 |
if (email->type != V_ASN1_IA5STRING) |
| 537 |
return 1; |
| 538 |
if (email->data == NULL || email->length == 0) |
| 539 |
return 1; |
| 540 |
if (memchr(email->data, 0, email->length) != NULL) |
| 541 |
return 1; |
| 542 |
if (*sk == NULL) |
| 543 |
*sk = sk_OPENSSL_STRING_new(sk_strcmp); |
| 544 |
if (*sk == NULL) |
| 545 |
return 0; |
| 546 |
|
| 547 |
emtmp = OPENSSL_strndup((char *)email->data, email->length); |
| 548 |
if (emtmp == NULL) { |
| 549 |
X509_email_free(*sk); |
| 550 |
*sk = NULL; |
| 551 |
return 0; |
| 552 |
} |
| 553 |
|
| 554 |
/* Don't add duplicates */ |
| 555 |
if (sk_OPENSSL_STRING_find(*sk, emtmp) != -1) { |
| 556 |
OPENSSL_free(emtmp); |
| 557 |
return 1; |
| 558 |
} |
| 559 |
if (!sk_OPENSSL_STRING_push(*sk, emtmp)) { |
| 560 |
OPENSSL_free(emtmp); /* free on push failure */ |
| 561 |
X509_email_free(*sk); |
| 562 |
*sk = NULL; |
| 563 |
return 0; |
| 564 |
} |
| 565 |
return 1; |
| 566 |
} |
| 567 |
|
| 568 |
void X509_email_free(STACK_OF(OPENSSL_STRING) *sk) |
| 569 |
{ |
| 570 |
sk_OPENSSL_STRING_pop_free(sk, str_free); |
| 571 |
} |
| 572 |
|
| 573 |
typedef int (*equal_fn)(const unsigned char *pattern, size_t pattern_len, |
| 574 |
const unsigned char *subject, size_t subject_len, |
| 575 |
unsigned int flags); |
| 576 |
|
| 577 |
/* Skip pattern prefix to match "wildcard" subject */ |
| 578 |
static void skip_prefix(const unsigned char **p, size_t *plen, |
| 579 |
size_t subject_len, |
| 580 |
unsigned int flags) |
| 581 |
{ |
| 582 |
const unsigned char *pattern = *p; |
| 583 |
size_t pattern_len = *plen; |
| 584 |
|
| 585 |
/* |
| 586 |
* If subject starts with a leading '.' followed by more octets, and |
| 587 |
* pattern is longer, compare just an equal-length suffix with the |
| 588 |
* full subject (starting at the '.'), provided the prefix contains |
| 589 |
* no NULs. |
| 590 |
*/ |
| 591 |
if ((flags & _X509_CHECK_FLAG_DOT_SUBDOMAINS) == 0) |
| 592 |
return; |
| 593 |
|
| 594 |
while (pattern_len > subject_len && *pattern) { |
| 595 |
if ((flags & X509_CHECK_FLAG_SINGLE_LABEL_SUBDOMAINS) && *pattern == '.') |
| 596 |
break; |
| 597 |
++pattern; |
| 598 |
--pattern_len; |
| 599 |
} |
| 600 |
|
| 601 |
/* Skip if entire prefix acceptable */ |
| 602 |
if (pattern_len == subject_len) { |
| 603 |
*p = pattern; |
| 604 |
*plen = pattern_len; |
| 605 |
} |
| 606 |
} |
| 607 |
|
| 608 |
/* Compare while ASCII ignoring case. */ |
| 609 |
static int equal_nocase(const unsigned char *pattern, size_t pattern_len, |
| 610 |
const unsigned char *subject, size_t subject_len, |
| 611 |
unsigned int flags) |
| 612 |
{ |
| 613 |
skip_prefix(&pattern, &pattern_len, subject_len, flags); |
| 614 |
if (pattern_len != subject_len) |
| 615 |
return 0; |
| 616 |
while (pattern_len != 0) { |
| 617 |
unsigned char l = *pattern; |
| 618 |
unsigned char r = *subject; |
| 619 |
|
| 620 |
/* The pattern must not contain NUL characters. */ |
| 621 |
if (l == 0) |
| 622 |
return 0; |
| 623 |
if (l != r) { |
| 624 |
if ('A' <= l && l <= 'Z') |
| 625 |
l = (l - 'A') + 'a'; |
| 626 |
if ('A' <= r && r <= 'Z') |
| 627 |
r = (r - 'A') + 'a'; |
| 628 |
if (l != r) |
| 629 |
return 0; |
| 630 |
} |
| 631 |
++pattern; |
| 632 |
++subject; |
| 633 |
--pattern_len; |
| 634 |
} |
| 635 |
return 1; |
| 636 |
} |
| 637 |
|
| 638 |
/* Compare using memcmp. */ |
| 639 |
static int equal_case(const unsigned char *pattern, size_t pattern_len, |
| 640 |
const unsigned char *subject, size_t subject_len, |
| 641 |
unsigned int flags) |
| 642 |
{ |
| 643 |
skip_prefix(&pattern, &pattern_len, subject_len, flags); |
| 644 |
if (pattern_len != subject_len) |
| 645 |
return 0; |
| 646 |
return !memcmp(pattern, subject, pattern_len); |
| 647 |
} |
| 648 |
|
| 649 |
/* |
| 650 |
* RFC 5280, section 7.5, requires that only the domain is compared in a |
| 651 |
* case-insensitive manner. |
| 652 |
*/ |
| 653 |
static int equal_email(const unsigned char *a, size_t a_len, |
| 654 |
const unsigned char *b, size_t b_len, |
| 655 |
unsigned int unused_flags) |
| 656 |
{ |
| 657 |
size_t i = a_len; |
| 658 |
|
| 659 |
if (a_len != b_len) |
| 660 |
return 0; |
| 661 |
/* |
| 662 |
* We search backwards for the '@' character, so that we do not have to |
| 663 |
* deal with quoted local-parts. The domain part is compared in a |
| 664 |
* case-insensitive manner. |
| 665 |
*/ |
| 666 |
while (i > 0) { |
| 667 |
--i; |
| 668 |
if (a[i] == '@' || b[i] == '@') { |
| 669 |
if (!equal_nocase(a + i, a_len - i, b + i, a_len - i, 0)) |
| 670 |
return 0; |
| 671 |
break; |
| 672 |
} |
| 673 |
} |
| 674 |
if (i == 0) |
| 675 |
i = a_len; |
| 676 |
return equal_case(a, i, b, i, 0); |
| 677 |
} |
| 678 |
|
| 679 |
/* |
| 680 |
* Compare the prefix and suffix with the subject, and check that the |
| 681 |
* characters in-between are valid. |
| 682 |
*/ |
| 683 |
static int wildcard_match(const unsigned char *prefix, size_t prefix_len, |
| 684 |
const unsigned char *suffix, size_t suffix_len, |
| 685 |
const unsigned char *subject, size_t subject_len, |
| 686 |
unsigned int flags) |
| 687 |
{ |
| 688 |
const unsigned char *wildcard_start; |
| 689 |
const unsigned char *wildcard_end; |
| 690 |
const unsigned char *p; |
| 691 |
int allow_multi = 0; |
| 692 |
int allow_idna = 0; |
| 693 |
|
| 694 |
if (subject_len < prefix_len + suffix_len) |
| 695 |
return 0; |
| 696 |
if (!equal_nocase(prefix, prefix_len, subject, prefix_len, flags)) |
| 697 |
return 0; |
| 698 |
wildcard_start = subject + prefix_len; |
| 699 |
wildcard_end = subject + (subject_len - suffix_len); |
| 700 |
if (!equal_nocase(wildcard_end, suffix_len, suffix, suffix_len, flags)) |
| 701 |
return 0; |
| 702 |
/* |
| 703 |
* If the wildcard makes up the entire first label, it must match at |
| 704 |
* least one character. |
| 705 |
*/ |
| 706 |
if (prefix_len == 0 && *suffix == '.') { |
| 707 |
if (wildcard_start == wildcard_end) |
| 708 |
return 0; |
| 709 |
allow_idna = 1; |
| 710 |
if (flags & X509_CHECK_FLAG_MULTI_LABEL_WILDCARDS) |
| 711 |
allow_multi = 1; |
| 712 |
} |
| 713 |
/* IDNA labels cannot match partial wildcards */ |
| 714 |
if (!allow_idna && subject_len >= 4 && HAS_CASE_PREFIX((const char *)subject, "xn--")) |
| 715 |
return 0; |
| 716 |
/* The wildcard may match a literal '*' */ |
| 717 |
if (wildcard_end == wildcard_start + 1 && *wildcard_start == '*') |
| 718 |
return 1; |
| 719 |
/* |
| 720 |
* Check that the part matched by the wildcard contains only |
| 721 |
* permitted characters and only matches a single label unless |
| 722 |
* allow_multi is set. |
| 723 |
*/ |
| 724 |
for (p = wildcard_start; p != wildcard_end; ++p) |
| 725 |
if (!(('0' <= *p && *p <= '9') || ('A' <= *p && *p <= 'Z') || ('a' <= *p && *p <= 'z') || *p == '-' || (allow_multi && *p == '.'))) |
| 726 |
return 0; |
| 727 |
return 1; |
| 728 |
} |
| 729 |
|
| 730 |
#define LABEL_START (1 << 0) |
| 731 |
#define LABEL_END (1 << 1) |
| 732 |
#define LABEL_HYPHEN (1 << 2) |
| 733 |
#define LABEL_IDNA (1 << 3) |
| 734 |
|
| 735 |
static const unsigned char *valid_star(const unsigned char *p, size_t len, |
| 736 |
unsigned int flags) |
| 737 |
{ |
| 738 |
const unsigned char *star = 0; |
| 739 |
size_t i; |
| 740 |
int state = LABEL_START; |
| 741 |
int dots = 0; |
| 742 |
|
| 743 |
for (i = 0; i < len; ++i) { |
| 744 |
/* |
| 745 |
* Locate first and only legal wildcard, either at the start |
| 746 |
* or end of a non-IDNA first and not final label. |
| 747 |
*/ |
| 748 |
if (p[i] == '*') { |
| 749 |
int atstart = (state & LABEL_START); |
| 750 |
int atend = (i == len - 1 || p[i + 1] == '.'); |
| 751 |
/*- |
| 752 |
* At most one wildcard per pattern. |
| 753 |
* No wildcards in IDNA labels. |
| 754 |
* No wildcards after the first label. |
| 755 |
*/ |
| 756 |
if (star != NULL || (state & LABEL_IDNA) != 0 || dots) |
| 757 |
return NULL; |
| 758 |
/* Only full-label '*.example.com' wildcards? */ |
| 759 |
if ((flags & X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS) |
| 760 |
&& (!atstart || !atend)) |
| 761 |
return NULL; |
| 762 |
/* No 'foo*bar' wildcards */ |
| 763 |
if (!atstart && !atend) |
| 764 |
return NULL; |
| 765 |
star = &p[i]; |
| 766 |
state &= ~LABEL_START; |
| 767 |
} else if (('a' <= p[i] && p[i] <= 'z') |
| 768 |
|| ('A' <= p[i] && p[i] <= 'Z') |
| 769 |
|| ('0' <= p[i] && p[i] <= '9')) { |
| 770 |
if ((state & LABEL_START) != 0 |
| 771 |
&& len - i >= 4 && HAS_CASE_PREFIX((const char *)&p[i], "xn--")) |
| 772 |
state |= LABEL_IDNA; |
| 773 |
state &= ~(LABEL_HYPHEN | LABEL_START); |
| 774 |
} else if (p[i] == '.') { |
| 775 |
if ((state & (LABEL_HYPHEN | LABEL_START)) != 0) |
| 776 |
return NULL; |
| 777 |
state = LABEL_START; |
| 778 |
++dots; |
| 779 |
} else if (p[i] == '-') { |
| 780 |
/* no domain/subdomain starts with '-' */ |
| 781 |
if ((state & LABEL_START) != 0) |
| 782 |
return NULL; |
| 783 |
state |= LABEL_HYPHEN; |
| 784 |
} else { |
| 785 |
return NULL; |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
/* |
| 790 |
* The final label must not end in a hyphen or ".", and |
| 791 |
* there must be at least two dots after the star. |
| 792 |
*/ |
| 793 |
if ((state & (LABEL_START | LABEL_HYPHEN)) != 0 || dots < 2) |
| 794 |
return NULL; |
| 795 |
return star; |
| 796 |
} |
| 797 |
|
| 798 |
/* Compare using wildcards. */ |
| 799 |
static int equal_wildcard(const unsigned char *pattern, size_t pattern_len, |
| 800 |
const unsigned char *subject, size_t subject_len, |
| 801 |
unsigned int flags) |
| 802 |
{ |
| 803 |
const unsigned char *star = NULL; |
| 804 |
|
| 805 |
/* |
| 806 |
* Subject names starting with '.' can only match a wildcard pattern |
| 807 |
* via a subject sub-domain pattern suffix match. |
| 808 |
*/ |
| 809 |
if (!(subject_len > 1 && subject[0] == '.')) |
| 810 |
star = valid_star(pattern, pattern_len, flags); |
| 811 |
if (star == NULL) |
| 812 |
return equal_nocase(pattern, pattern_len, |
| 813 |
subject, subject_len, flags); |
| 814 |
return wildcard_match(pattern, star - pattern, |
| 815 |
star + 1, (pattern + pattern_len) - star - 1, |
| 816 |
subject, subject_len, flags); |
| 817 |
} |
| 818 |
|
| 819 |
/* |
| 820 |
* Compare an ASN1_STRING to a supplied string. If they match return 1. If |
| 821 |
* cmp_type > 0 only compare if string matches the type, otherwise convert it |
| 822 |
* to UTF8. |
| 823 |
*/ |
| 824 |
|
| 825 |
static int do_check_string(const ASN1_STRING *a, int cmp_type, equal_fn equal, |
| 826 |
unsigned int flags, const char *b, size_t blen, |
| 827 |
char **peername) |
| 828 |
{ |
| 829 |
int rv = 0; |
| 830 |
|
| 831 |
if (!a->data || !a->length) |
| 832 |
return 0; |
| 833 |
if (cmp_type > 0) { |
| 834 |
if (cmp_type != a->type) |
| 835 |
return 0; |
| 836 |
if (cmp_type == V_ASN1_IA5STRING) |
| 837 |
rv = equal(a->data, a->length, (unsigned char *)b, blen, flags); |
| 838 |
else if (a->length == (int)blen && !memcmp(a->data, b, blen)) |
| 839 |
rv = 1; |
| 840 |
if (rv > 0 && peername != NULL) { |
| 841 |
*peername = OPENSSL_strndup((char *)a->data, a->length); |
| 842 |
if (*peername == NULL) |
| 843 |
return -1; |
| 844 |
} |
| 845 |
} else { |
| 846 |
int astrlen; |
| 847 |
unsigned char *astr; |
| 848 |
astrlen = ASN1_STRING_to_UTF8(&astr, a); |
| 849 |
if (astrlen < 0) { |
| 850 |
/* |
| 851 |
* -1 could be an internal malloc failure or a decoding error from |
| 852 |
* malformed input; we can't distinguish. |
| 853 |
*/ |
| 854 |
return -1; |
| 855 |
} |
| 856 |
rv = equal(astr, astrlen, (unsigned char *)b, blen, flags); |
| 857 |
if (rv > 0 && peername != NULL) { |
| 858 |
*peername = OPENSSL_strndup((char *)astr, astrlen); |
| 859 |
if (*peername == NULL) { |
| 860 |
OPENSSL_free(astr); |
| 861 |
return -1; |
| 862 |
} |
| 863 |
} |
| 864 |
OPENSSL_free(astr); |
| 865 |
} |
| 866 |
return rv; |
| 867 |
} |
| 868 |
|
| 869 |
static int do_x509_check(X509 *x, const char *chk, size_t chklen, |
| 870 |
unsigned int flags, int check_type, char **peername) |
| 871 |
{ |
| 872 |
GENERAL_NAMES *gens = NULL; |
| 873 |
const X509_NAME *name = NULL; |
| 874 |
int i; |
| 875 |
int cnid = NID_undef; |
| 876 |
int alt_type; |
| 877 |
int san_present = 0; |
| 878 |
int rv = 0; |
| 879 |
equal_fn equal; |
| 880 |
|
| 881 |
/* See below, this flag is internal-only */ |
| 882 |
flags &= ~_X509_CHECK_FLAG_DOT_SUBDOMAINS; |
| 883 |
if (check_type == GEN_EMAIL) { |
| 884 |
cnid = NID_pkcs9_emailAddress; |
| 885 |
alt_type = V_ASN1_IA5STRING; |
| 886 |
equal = equal_email; |
| 887 |
} else if (check_type == GEN_DNS) { |
| 888 |
cnid = NID_commonName; |
| 889 |
/* Implicit client-side DNS sub-domain pattern */ |
| 890 |
if (chklen > 1 && chk[0] == '.') |
| 891 |
flags |= _X509_CHECK_FLAG_DOT_SUBDOMAINS; |
| 892 |
alt_type = V_ASN1_IA5STRING; |
| 893 |
if (flags & X509_CHECK_FLAG_NO_WILDCARDS) |
| 894 |
equal = equal_nocase; |
| 895 |
else |
| 896 |
equal = equal_wildcard; |
| 897 |
} else { |
| 898 |
alt_type = V_ASN1_OCTET_STRING; |
| 899 |
equal = equal_case; |
| 900 |
} |
| 901 |
|
| 902 |
if (chklen == 0) |
| 903 |
chklen = strlen(chk); |
| 904 |
|
| 905 |
gens = X509_get_ext_d2i(x, NID_subject_alt_name, NULL, NULL); |
| 906 |
if (gens) { |
| 907 |
for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 908 |
GENERAL_NAME *gen; |
| 909 |
ASN1_STRING *cstr; |
| 910 |
|
| 911 |
gen = sk_GENERAL_NAME_value(gens, i); |
| 912 |
switch (gen->type) { |
| 913 |
default: |
| 914 |
continue; |
| 915 |
case GEN_OTHERNAME: |
| 916 |
switch (OBJ_obj2nid(gen->d.otherName->type_id)) { |
| 917 |
default: |
| 918 |
continue; |
| 919 |
case NID_id_on_SmtpUTF8Mailbox: |
| 920 |
/*- |
| 921 |
* https://datatracker.ietf.org/doc/html/rfc8398#section-3 |
| 922 |
* |
| 923 |
* Due to name constraint compatibility reasons described |
| 924 |
* in Section 6, SmtpUTF8Mailbox subjectAltName MUST NOT |
| 925 |
* be used unless the local-part of the email address |
| 926 |
* contains non-ASCII characters. When the local-part is |
| 927 |
* ASCII, rfc822Name subjectAltName MUST be used instead |
| 928 |
* of SmtpUTF8Mailbox. This is compatible with legacy |
| 929 |
* software that supports only rfc822Name (and not |
| 930 |
* SmtpUTF8Mailbox). [...] |
| 931 |
* |
| 932 |
* SmtpUTF8Mailbox is encoded as UTF8String. |
| 933 |
* |
| 934 |
* If it is not a UTF8String then that is unexpected, and |
| 935 |
* we ignore the invalid SAN (neither set san_present nor |
| 936 |
* consider it a candidate for equality). This does mean |
| 937 |
* that the subject CN may be considered, as would be the |
| 938 |
* case when the malformed SmtpUtf8Mailbox SAN is instead |
| 939 |
* simply absent. |
| 940 |
* |
| 941 |
* When CN-ID matching is not desirable, applications can |
| 942 |
* choose to turn it off, doing so is at this time a best |
| 943 |
* practice. |
| 944 |
*/ |
| 945 |
if (check_type != GEN_EMAIL |
| 946 |
|| gen->d.otherName->value->type != V_ASN1_UTF8STRING) |
| 947 |
continue; |
| 948 |
alt_type = 0; |
| 949 |
cstr = gen->d.otherName->value->value.utf8string; |
| 950 |
break; |
| 951 |
} |
| 952 |
break; |
| 953 |
case GEN_EMAIL: |
| 954 |
if (check_type != GEN_EMAIL) |
| 955 |
continue; |
| 956 |
cstr = gen->d.rfc822Name; |
| 957 |
break; |
| 958 |
case GEN_DNS: |
| 959 |
if (check_type != GEN_DNS) |
| 960 |
continue; |
| 961 |
cstr = gen->d.dNSName; |
| 962 |
break; |
| 963 |
case GEN_IPADD: |
| 964 |
if (check_type != GEN_IPADD) |
| 965 |
continue; |
| 966 |
cstr = gen->d.iPAddress; |
| 967 |
break; |
| 968 |
} |
| 969 |
san_present = 1; |
| 970 |
/* Positive on success, negative on error! */ |
| 971 |
if ((rv = do_check_string(cstr, alt_type, equal, flags, |
| 972 |
chk, chklen, peername)) |
| 973 |
!= 0) |
| 974 |
break; |
| 975 |
} |
| 976 |
GENERAL_NAMES_free(gens); |
| 977 |
if (rv != 0) |
| 978 |
return rv; |
| 979 |
if (san_present && !(flags & X509_CHECK_FLAG_ALWAYS_CHECK_SUBJECT)) |
| 980 |
return 0; |
| 981 |
} |
| 982 |
|
| 983 |
/* We're done if CN-ID is not pertinent */ |
| 984 |
if (cnid == NID_undef || (flags & X509_CHECK_FLAG_NEVER_CHECK_SUBJECT)) |
| 985 |
return 0; |
| 986 |
|
| 987 |
i = -1; |
| 988 |
name = X509_get_subject_name(x); |
| 989 |
while ((i = X509_NAME_get_index_by_NID(name, cnid, i)) >= 0) { |
| 990 |
const X509_NAME_ENTRY *ne = X509_NAME_get_entry(name, i); |
| 991 |
const ASN1_STRING *str = X509_NAME_ENTRY_get_data(ne); |
| 992 |
|
| 993 |
/* Positive on success, negative on error! */ |
| 994 |
if ((rv = do_check_string(str, -1, equal, flags, |
| 995 |
chk, chklen, peername)) |
| 996 |
!= 0) |
| 997 |
return rv; |
| 998 |
} |
| 999 |
return 0; |
| 1000 |
} |
| 1001 |
|
| 1002 |
int X509_check_host(X509 *x, const char *chk, size_t chklen, |
| 1003 |
unsigned int flags, char **peername) |
| 1004 |
{ |
| 1005 |
if (chk == NULL) |
| 1006 |
return -2; |
| 1007 |
/* |
| 1008 |
* Embedded NULs are disallowed, except as the last character of a |
| 1009 |
* string of length 2 or more (tolerate caller including terminating |
| 1010 |
* NUL in string length). |
| 1011 |
*/ |
| 1012 |
if (chklen == 0) |
| 1013 |
chklen = strlen(chk); |
| 1014 |
else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
| 1015 |
return -2; |
| 1016 |
if (chklen > 1 && chk[chklen - 1] == '\0') |
| 1017 |
--chklen; |
| 1018 |
return do_x509_check(x, chk, chklen, flags, GEN_DNS, peername); |
| 1019 |
} |
| 1020 |
|
| 1021 |
int X509_check_email(X509 *x, const char *chk, size_t chklen, |
| 1022 |
unsigned int flags) |
| 1023 |
{ |
| 1024 |
if (chk == NULL) |
| 1025 |
return -2; |
| 1026 |
/* |
| 1027 |
* Embedded NULs are disallowed, except as the last character of a |
| 1028 |
* string of length 2 or more (tolerate caller including terminating |
| 1029 |
* NUL in string length). |
| 1030 |
*/ |
| 1031 |
if (chklen == 0) |
| 1032 |
chklen = strlen((char *)chk); |
| 1033 |
else if (memchr(chk, '\0', chklen > 1 ? chklen - 1 : chklen)) |
| 1034 |
return -2; |
| 1035 |
if (chklen > 1 && chk[chklen - 1] == '\0') |
| 1036 |
--chklen; |
| 1037 |
return do_x509_check(x, chk, chklen, flags, GEN_EMAIL, NULL); |
| 1038 |
} |
| 1039 |
|
| 1040 |
int X509_check_ip(X509 *x, const unsigned char *chk, size_t chklen, |
| 1041 |
unsigned int flags) |
| 1042 |
{ |
| 1043 |
if (chk == NULL) |
| 1044 |
return -2; |
| 1045 |
return do_x509_check(x, (char *)chk, chklen, flags, GEN_IPADD, NULL); |
| 1046 |
} |
| 1047 |
|
| 1048 |
int X509_check_ip_asc(X509 *x, const char *ipasc, unsigned int flags) |
| 1049 |
{ |
| 1050 |
unsigned char ipout[16]; |
| 1051 |
size_t iplen; |
| 1052 |
|
| 1053 |
if (ipasc == NULL) |
| 1054 |
return -2; |
| 1055 |
iplen = (size_t)ossl_a2i_ipadd(ipout, ipasc); |
| 1056 |
if (iplen == 0) |
| 1057 |
return -2; |
| 1058 |
return do_x509_check(x, (char *)ipout, iplen, flags, GEN_IPADD, NULL); |
| 1059 |
} |
| 1060 |
|
| 1061 |
char *ossl_ipaddr_to_asc(unsigned char *p, int len) |
| 1062 |
{ |
| 1063 |
/* |
| 1064 |
* 40 is enough space for the longest IPv6 address + nul terminator byte |
| 1065 |
* XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX:XXXX\0 |
| 1066 |
*/ |
| 1067 |
char buf[40], *out; |
| 1068 |
int i = 0, remain = 0, bytes = 0; |
| 1069 |
|
| 1070 |
switch (len) { |
| 1071 |
case 4: /* IPv4 */ |
| 1072 |
BIO_snprintf(buf, sizeof(buf), "%d.%d.%d.%d", p[0], p[1], p[2], p[3]); |
| 1073 |
break; |
| 1074 |
case 16: /* IPv6 */ |
| 1075 |
for (out = buf, i = 8, remain = sizeof(buf); |
| 1076 |
i-- > 0 && bytes >= 0; |
| 1077 |
remain -= bytes, out += bytes) { |
| 1078 |
const char *template = (i > 0 ? "%X:" : "%X"); |
| 1079 |
|
| 1080 |
bytes = BIO_snprintf(out, remain, template, p[0] << 8 | p[1]); |
| 1081 |
p += 2; |
| 1082 |
} |
| 1083 |
break; |
| 1084 |
default: |
| 1085 |
BIO_snprintf(buf, sizeof(buf), "<invalid length=%d>", len); |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
return OPENSSL_strdup(buf); |
| 1089 |
} |
| 1090 |
|
| 1091 |
/* |
| 1092 |
* Convert IP addresses both IPv4 and IPv6 into an OCTET STRING compatible |
| 1093 |
* with RFC3280. |
| 1094 |
*/ |
| 1095 |
|
| 1096 |
ASN1_OCTET_STRING *a2i_IPADDRESS(const char *ipasc) |
| 1097 |
{ |
| 1098 |
unsigned char ipout[16]; |
| 1099 |
ASN1_OCTET_STRING *ret; |
| 1100 |
int iplen; |
| 1101 |
|
| 1102 |
/* If string contains a ':' assume IPv6 */ |
| 1103 |
|
| 1104 |
iplen = ossl_a2i_ipadd(ipout, ipasc); |
| 1105 |
|
| 1106 |
if (!iplen) |
| 1107 |
return NULL; |
| 1108 |
|
| 1109 |
ret = ASN1_OCTET_STRING_new(); |
| 1110 |
if (ret == NULL) |
| 1111 |
return NULL; |
| 1112 |
if (!ASN1_OCTET_STRING_set(ret, ipout, iplen)) { |
| 1113 |
ASN1_OCTET_STRING_free(ret); |
| 1114 |
return NULL; |
| 1115 |
} |
| 1116 |
return ret; |
| 1117 |
} |
| 1118 |
|
| 1119 |
ASN1_OCTET_STRING *a2i_IPADDRESS_NC(const char *ipasc) |
| 1120 |
{ |
| 1121 |
ASN1_OCTET_STRING *ret = NULL; |
| 1122 |
unsigned char ipout[32]; |
| 1123 |
char *iptmp = NULL, *p; |
| 1124 |
const char *slash; |
| 1125 |
int iplen1, iplen2; |
| 1126 |
|
| 1127 |
slash = strchr(ipasc, '/'); |
| 1128 |
if (slash == NULL) |
| 1129 |
return NULL; |
| 1130 |
iptmp = OPENSSL_strdup(ipasc); |
| 1131 |
if (iptmp == NULL) |
| 1132 |
return NULL; |
| 1133 |
p = iptmp + (slash - ipasc); |
| 1134 |
*p++ = 0; |
| 1135 |
|
| 1136 |
iplen1 = ossl_a2i_ipadd(ipout, iptmp); |
| 1137 |
|
| 1138 |
if (!iplen1) |
| 1139 |
goto err; |
| 1140 |
|
| 1141 |
iplen2 = ossl_a2i_ipadd(ipout + iplen1, p); |
| 1142 |
|
| 1143 |
OPENSSL_free(iptmp); |
| 1144 |
iptmp = NULL; |
| 1145 |
|
| 1146 |
if (!iplen2 || (iplen1 != iplen2)) |
| 1147 |
goto err; |
| 1148 |
|
| 1149 |
ret = ASN1_OCTET_STRING_new(); |
| 1150 |
if (ret == NULL) |
| 1151 |
goto err; |
| 1152 |
if (!ASN1_OCTET_STRING_set(ret, ipout, iplen1 + iplen2)) |
| 1153 |
goto err; |
| 1154 |
|
| 1155 |
return ret; |
| 1156 |
|
| 1157 |
err: |
| 1158 |
OPENSSL_free(iptmp); |
| 1159 |
ASN1_OCTET_STRING_free(ret); |
| 1160 |
return NULL; |
| 1161 |
} |
| 1162 |
|
| 1163 |
int ossl_a2i_ipadd(unsigned char *ipout, const char *ipasc) |
| 1164 |
{ |
| 1165 |
/* If string contains a ':' assume IPv6 */ |
| 1166 |
|
| 1167 |
if (strchr(ipasc, ':')) { |
| 1168 |
if (!ipv6_from_asc(ipout, ipasc)) |
| 1169 |
return 0; |
| 1170 |
return 16; |
| 1171 |
} else { |
| 1172 |
if (!ipv4_from_asc(ipout, ipasc)) |
| 1173 |
return 0; |
| 1174 |
return 4; |
| 1175 |
} |
| 1176 |
} |
| 1177 |
|
| 1178 |
/* |
| 1179 |
* get_ipv4_component consumes one IPv4 component, terminated by either '.' or |
| 1180 |
* the end of the string, from *str. On success, it returns one, sets *out |
| 1181 |
* to the component, and advances *str to the first unconsumed character. On |
| 1182 |
* invalid input, it returns zero. |
| 1183 |
*/ |
| 1184 |
static int get_ipv4_component(uint8_t *out_byte, const char **str) |
| 1185 |
{ |
| 1186 |
/* Store a slightly larger intermediary so the overflow check is easier. */ |
| 1187 |
uint32_t out = 0; |
| 1188 |
|
| 1189 |
for (;;) { |
| 1190 |
if (!ossl_isdigit(**str)) { |
| 1191 |
return 0; |
| 1192 |
} |
| 1193 |
out = (out * 10) + (**str - '0'); |
| 1194 |
if (out > 255) { |
| 1195 |
/* Components must be 8-bit. */ |
| 1196 |
return 0; |
| 1197 |
} |
| 1198 |
(*str)++; |
| 1199 |
if ((**str) == '.' || (**str) == '\0') { |
| 1200 |
*out_byte = (uint8_t)out; |
| 1201 |
return 1; |
| 1202 |
} |
| 1203 |
if (out == 0) { |
| 1204 |
/* Reject extra leading zeros. Parsers sometimes treat them as |
| 1205 |
* octal, so accepting them would misinterpret input. |
| 1206 |
*/ |
| 1207 |
return 0; |
| 1208 |
} |
| 1209 |
} |
| 1210 |
} |
| 1211 |
|
| 1212 |
/* |
| 1213 |
* get_ipv4_dot consumes a '.' from *str and advances it. It returns one on |
| 1214 |
* success and zero if *str does not point to a '.'. |
| 1215 |
*/ |
| 1216 |
static int get_ipv4_dot(const char **str) |
| 1217 |
{ |
| 1218 |
if (**str != '.') { |
| 1219 |
return 0; |
| 1220 |
} |
| 1221 |
(*str)++; |
| 1222 |
return 1; |
| 1223 |
} |
| 1224 |
|
| 1225 |
static int ipv4_from_asc(unsigned char *v4, const char *in) |
| 1226 |
{ |
| 1227 |
if (!get_ipv4_component(&v4[0], &in) || !get_ipv4_dot(&in) |
| 1228 |
|| !get_ipv4_component(&v4[1], &in) || !get_ipv4_dot(&in) |
| 1229 |
|| !get_ipv4_component(&v4[2], &in) || !get_ipv4_dot(&in) |
| 1230 |
|| !get_ipv4_component(&v4[3], &in) || *in != '\0') { |
| 1231 |
return 0; |
| 1232 |
} |
| 1233 |
return 1; |
| 1234 |
} |
| 1235 |
|
| 1236 |
typedef struct { |
| 1237 |
/* Temporary store for IPV6 output */ |
| 1238 |
unsigned char tmp[16]; |
| 1239 |
/* Total number of bytes in tmp */ |
| 1240 |
int total; |
| 1241 |
/* The position of a zero (corresponding to '::') */ |
| 1242 |
int zero_pos; |
| 1243 |
/* Number of zeroes */ |
| 1244 |
int zero_cnt; |
| 1245 |
} IPV6_STAT; |
| 1246 |
|
| 1247 |
static int ipv6_from_asc(unsigned char *v6, const char *in) |
| 1248 |
{ |
| 1249 |
IPV6_STAT v6stat; |
| 1250 |
|
| 1251 |
v6stat.total = 0; |
| 1252 |
v6stat.zero_pos = -1; |
| 1253 |
v6stat.zero_cnt = 0; |
| 1254 |
/* |
| 1255 |
* Treat the IPv6 representation as a list of values separated by ':'. |
| 1256 |
* The presence of a '::' will parse as one, two or three zero length |
| 1257 |
* elements. |
| 1258 |
*/ |
| 1259 |
if (!CONF_parse_list(in, ':', 0, ipv6_cb, &v6stat)) |
| 1260 |
return 0; |
| 1261 |
|
| 1262 |
/* Now for some sanity checks */ |
| 1263 |
|
| 1264 |
if (v6stat.zero_pos == -1) { |
| 1265 |
/* If no '::' must have exactly 16 bytes */ |
| 1266 |
if (v6stat.total != 16) |
| 1267 |
return 0; |
| 1268 |
} else { |
| 1269 |
/* If '::' must have less than 16 bytes */ |
| 1270 |
if (v6stat.total == 16) |
| 1271 |
return 0; |
| 1272 |
/* More than three zeroes is an error */ |
| 1273 |
if (v6stat.zero_cnt > 3) { |
| 1274 |
return 0; |
| 1275 |
/* Can only have three zeroes if nothing else present */ |
| 1276 |
} else if (v6stat.zero_cnt == 3) { |
| 1277 |
if (v6stat.total > 0) |
| 1278 |
return 0; |
| 1279 |
} else if (v6stat.zero_cnt == 2) { |
| 1280 |
/* Can only have two zeroes if at start or end */ |
| 1281 |
if ((v6stat.zero_pos != 0) |
| 1282 |
&& (v6stat.zero_pos != v6stat.total)) |
| 1283 |
return 0; |
| 1284 |
} else { |
| 1285 |
/* Can only have one zero if *not* start or end */ |
| 1286 |
if ((v6stat.zero_pos == 0) |
| 1287 |
|| (v6stat.zero_pos == v6stat.total)) |
| 1288 |
return 0; |
| 1289 |
} |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* Format result */ |
| 1293 |
|
| 1294 |
if (v6stat.zero_pos >= 0) { |
| 1295 |
/* Copy initial part */ |
| 1296 |
memcpy(v6, v6stat.tmp, v6stat.zero_pos); |
| 1297 |
/* Zero middle */ |
| 1298 |
memset(v6 + v6stat.zero_pos, 0, 16 - v6stat.total); |
| 1299 |
/* Copy final part */ |
| 1300 |
if (v6stat.total != v6stat.zero_pos) |
| 1301 |
memcpy(v6 + v6stat.zero_pos + 16 - v6stat.total, |
| 1302 |
v6stat.tmp + v6stat.zero_pos, |
| 1303 |
v6stat.total - v6stat.zero_pos); |
| 1304 |
} else { |
| 1305 |
memcpy(v6, v6stat.tmp, 16); |
| 1306 |
} |
| 1307 |
|
| 1308 |
return 1; |
| 1309 |
} |
| 1310 |
|
| 1311 |
static int ipv6_cb(const char *elem, int len, void *usr) |
| 1312 |
{ |
| 1313 |
IPV6_STAT *s = usr; |
| 1314 |
|
| 1315 |
/* Error if 16 bytes written */ |
| 1316 |
if (s->total == 16) |
| 1317 |
return 0; |
| 1318 |
if (len == 0) { |
| 1319 |
/* Zero length element, corresponds to '::' */ |
| 1320 |
if (s->zero_pos == -1) |
| 1321 |
s->zero_pos = s->total; |
| 1322 |
/* If we've already got a :: its an error */ |
| 1323 |
else if (s->zero_pos != s->total) |
| 1324 |
return 0; |
| 1325 |
s->zero_cnt++; |
| 1326 |
} else { |
| 1327 |
/* If more than 4 characters could be final a.b.c.d form */ |
| 1328 |
if (len > 4) { |
| 1329 |
/* Need at least 4 bytes left */ |
| 1330 |
if (s->total > 12) |
| 1331 |
return 0; |
| 1332 |
/* Must be end of string */ |
| 1333 |
if (elem[len]) |
| 1334 |
return 0; |
| 1335 |
if (!ipv4_from_asc(s->tmp + s->total, elem)) |
| 1336 |
return 0; |
| 1337 |
s->total += 4; |
| 1338 |
} else { |
| 1339 |
if (!ipv6_hex(s->tmp + s->total, elem, len)) |
| 1340 |
return 0; |
| 1341 |
s->total += 2; |
| 1342 |
} |
| 1343 |
} |
| 1344 |
return 1; |
| 1345 |
} |
| 1346 |
|
| 1347 |
/* |
| 1348 |
* Convert a string of up to 4 hex digits into the corresponding IPv6 form. |
| 1349 |
*/ |
| 1350 |
|
| 1351 |
static int ipv6_hex(unsigned char *out, const char *in, int inlen) |
| 1352 |
{ |
| 1353 |
unsigned char c; |
| 1354 |
unsigned int num = 0; |
| 1355 |
int x; |
| 1356 |
|
| 1357 |
if (inlen > 4) |
| 1358 |
return 0; |
| 1359 |
while (inlen--) { |
| 1360 |
c = *in++; |
| 1361 |
num <<= 4; |
| 1362 |
x = OPENSSL_hexchar2int(c); |
| 1363 |
if (x < 0) |
| 1364 |
return 0; |
| 1365 |
num |= (char)x; |
| 1366 |
} |
| 1367 |
out[0] = num >> 8; |
| 1368 |
out[1] = num & 0xff; |
| 1369 |
return 1; |
| 1370 |
} |
| 1371 |
|
| 1372 |
int X509V3_NAME_from_section(X509_NAME *nm, STACK_OF(CONF_VALUE) *dn_sk, |
| 1373 |
unsigned long chtype) |
| 1374 |
{ |
| 1375 |
CONF_VALUE *v; |
| 1376 |
int i, mval, spec_char, plus_char; |
| 1377 |
char *p, *type; |
| 1378 |
|
| 1379 |
if (!nm) |
| 1380 |
return 0; |
| 1381 |
|
| 1382 |
for (i = 0; i < sk_CONF_VALUE_num(dn_sk); i++) { |
| 1383 |
v = sk_CONF_VALUE_value(dn_sk, i); |
| 1384 |
type = v->name; |
| 1385 |
/* |
| 1386 |
* Skip past any leading X. X: X, etc to allow for multiple instances |
| 1387 |
*/ |
| 1388 |
for (p = type; *p; p++) { |
| 1389 |
#ifndef CHARSET_EBCDIC |
| 1390 |
spec_char = ((*p == ':') || (*p == ',') || (*p == '.')); |
| 1391 |
#else |
| 1392 |
spec_char = ((*p == os_toascii[':']) || (*p == os_toascii[',']) |
| 1393 |
|| (*p == os_toascii['.'])); |
| 1394 |
#endif |
| 1395 |
if (spec_char) { |
| 1396 |
p++; |
| 1397 |
if (*p) |
| 1398 |
type = p; |
| 1399 |
break; |
| 1400 |
} |
| 1401 |
} |
| 1402 |
#ifndef CHARSET_EBCDIC |
| 1403 |
plus_char = (*type == '+'); |
| 1404 |
#else |
| 1405 |
plus_char = (*type == os_toascii['+']); |
| 1406 |
#endif |
| 1407 |
if (plus_char) { |
| 1408 |
mval = -1; |
| 1409 |
type++; |
| 1410 |
} else { |
| 1411 |
mval = 0; |
| 1412 |
} |
| 1413 |
if (!X509_NAME_add_entry_by_txt(nm, type, chtype, |
| 1414 |
(unsigned char *)v->value, -1, -1, |
| 1415 |
mval)) |
| 1416 |
return 0; |
| 1417 |
} |
| 1418 |
return 1; |
| 1419 |
} |
| 1420 |
|
| 1421 |
int OSSL_GENERAL_NAMES_print(BIO *out, GENERAL_NAMES *gens, int indent) |
| 1422 |
{ |
| 1423 |
int i; |
| 1424 |
|
| 1425 |
for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
| 1426 |
if (i > 0) |
| 1427 |
BIO_puts(out, "\n"); |
| 1428 |
BIO_printf(out, "%*s", indent + 2, ""); |
| 1429 |
GENERAL_NAME_print(out, sk_GENERAL_NAME_value(gens, i)); |
| 1430 |
} |
| 1431 |
return 1; |
| 1432 |
} |
| 1433 |
|
| 1434 |
int ossl_bio_print_hex(BIO *out, unsigned char *buf, int len) |
| 1435 |
{ |
| 1436 |
int result; |
| 1437 |
char *hexbuf; |
| 1438 |
|
| 1439 |
if (len == 0) |
| 1440 |
return 1; |
| 1441 |
|
| 1442 |
hexbuf = OPENSSL_buf2hexstr(buf, len); |
| 1443 |
if (hexbuf == NULL) |
| 1444 |
return 0; |
| 1445 |
result = BIO_puts(out, hexbuf) > 0; |
| 1446 |
|
| 1447 |
OPENSSL_free(hexbuf); |
| 1448 |
return result; |
| 1449 |
} |