| 1 |
/* |
| 2 |
* Copyright 1995-2025 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 |
#include <stdio.h> |
| 11 |
#include "internal/cryptlib.h" |
| 12 |
#include <openssl/asn1.h> |
| 13 |
#include <openssl/objects.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
#include <openssl/x509v3.h> |
| 16 |
#include <openssl/core_names.h> |
| 17 |
#include "crypto/x509.h" |
| 18 |
|
| 19 |
int X509_issuer_and_serial_cmp(const X509 *a, const X509 *b) |
| 20 |
{ |
| 21 |
int i; |
| 22 |
const X509_CINF *ai, *bi; |
| 23 |
|
| 24 |
if (b == NULL) |
| 25 |
return a != NULL; |
| 26 |
if (a == NULL) |
| 27 |
return -1; |
| 28 |
ai = &a->cert_info; |
| 29 |
bi = &b->cert_info; |
| 30 |
i = ASN1_INTEGER_cmp(&ai->serialNumber, &bi->serialNumber); |
| 31 |
if (i != 0) |
| 32 |
return i < 0 ? -1 : 1; |
| 33 |
return X509_NAME_cmp(ai->issuer, bi->issuer); |
| 34 |
} |
| 35 |
|
| 36 |
#ifndef OPENSSL_NO_MD5 |
| 37 |
unsigned long X509_issuer_and_serial_hash(X509 *a) |
| 38 |
{ |
| 39 |
unsigned long ret = 0; |
| 40 |
EVP_MD_CTX *ctx = EVP_MD_CTX_new(); |
| 41 |
unsigned char md[16]; |
| 42 |
char *f = NULL; |
| 43 |
EVP_MD *digest = NULL; |
| 44 |
|
| 45 |
if (ctx == NULL) |
| 46 |
goto err; |
| 47 |
f = X509_NAME_oneline(a->cert_info.issuer, NULL, 0); |
| 48 |
if (f == NULL) |
| 49 |
goto err; |
| 50 |
digest = EVP_MD_fetch(a->libctx, SN_md5, a->propq); |
| 51 |
if (digest == NULL) |
| 52 |
goto err; |
| 53 |
|
| 54 |
if (!EVP_DigestInit_ex(ctx, digest, NULL)) |
| 55 |
goto err; |
| 56 |
if (!EVP_DigestUpdate(ctx, (unsigned char *)f, strlen(f))) |
| 57 |
goto err; |
| 58 |
if (!EVP_DigestUpdate(ctx, (unsigned char *)a->cert_info.serialNumber.data, |
| 59 |
(unsigned long)a->cert_info.serialNumber.length)) |
| 60 |
goto err; |
| 61 |
if (!EVP_DigestFinal_ex(ctx, &(md[0]), NULL)) |
| 62 |
goto err; |
| 63 |
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
| 64 |
err: |
| 65 |
OPENSSL_free(f); |
| 66 |
EVP_MD_free(digest); |
| 67 |
EVP_MD_CTX_free(ctx); |
| 68 |
return ret; |
| 69 |
} |
| 70 |
#endif |
| 71 |
|
| 72 |
int X509_issuer_name_cmp(const X509 *a, const X509 *b) |
| 73 |
{ |
| 74 |
return X509_NAME_cmp(a->cert_info.issuer, b->cert_info.issuer); |
| 75 |
} |
| 76 |
|
| 77 |
int X509_subject_name_cmp(const X509 *a, const X509 *b) |
| 78 |
{ |
| 79 |
return X509_NAME_cmp(a->cert_info.subject, b->cert_info.subject); |
| 80 |
} |
| 81 |
|
| 82 |
int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) |
| 83 |
{ |
| 84 |
return X509_NAME_cmp(a->crl.issuer, b->crl.issuer); |
| 85 |
} |
| 86 |
|
| 87 |
int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) |
| 88 |
{ |
| 89 |
int rv; |
| 90 |
|
| 91 |
if ((a->flags & EXFLAG_NO_FINGERPRINT) == 0 |
| 92 |
&& (b->flags & EXFLAG_NO_FINGERPRINT) == 0) |
| 93 |
rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); |
| 94 |
else |
| 95 |
return -2; |
| 96 |
|
| 97 |
return rv < 0 ? -1 : rv > 0; |
| 98 |
} |
| 99 |
|
| 100 |
X509_NAME *X509_get_issuer_name(const X509 *a) |
| 101 |
{ |
| 102 |
return a->cert_info.issuer; |
| 103 |
} |
| 104 |
|
| 105 |
unsigned long X509_issuer_name_hash(X509 *x) |
| 106 |
{ |
| 107 |
return X509_NAME_hash_ex(x->cert_info.issuer, NULL, NULL, NULL); |
| 108 |
} |
| 109 |
|
| 110 |
#ifndef OPENSSL_NO_MD5 |
| 111 |
unsigned long X509_issuer_name_hash_old(X509 *x) |
| 112 |
{ |
| 113 |
return X509_NAME_hash_old(x->cert_info.issuer); |
| 114 |
} |
| 115 |
#endif |
| 116 |
|
| 117 |
X509_NAME *X509_get_subject_name(const X509 *a) |
| 118 |
{ |
| 119 |
return a->cert_info.subject; |
| 120 |
} |
| 121 |
|
| 122 |
ASN1_INTEGER *X509_get_serialNumber(X509 *a) |
| 123 |
{ |
| 124 |
return &a->cert_info.serialNumber; |
| 125 |
} |
| 126 |
|
| 127 |
const ASN1_INTEGER *X509_get0_serialNumber(const X509 *a) |
| 128 |
{ |
| 129 |
return &a->cert_info.serialNumber; |
| 130 |
} |
| 131 |
|
| 132 |
unsigned long X509_subject_name_hash(X509 *x) |
| 133 |
{ |
| 134 |
return X509_NAME_hash_ex(x->cert_info.subject, NULL, NULL, NULL); |
| 135 |
} |
| 136 |
|
| 137 |
#ifndef OPENSSL_NO_MD5 |
| 138 |
unsigned long X509_subject_name_hash_old(X509 *x) |
| 139 |
{ |
| 140 |
return X509_NAME_hash_old(x->cert_info.subject); |
| 141 |
} |
| 142 |
#endif |
| 143 |
|
| 144 |
/* |
| 145 |
* Compare two certificates: they must be identical for this to work. NB: |
| 146 |
* Although "cmp" operations are generally prototyped to take "const" |
| 147 |
* arguments (eg. for use in STACKs), the way X509 handling is - these |
| 148 |
* operations may involve ensuring the hashes are up-to-date and ensuring |
| 149 |
* certain cert information is cached. So this is the point where the |
| 150 |
* "depth-first" constification tree has to halt with an evil cast. |
| 151 |
*/ |
| 152 |
int X509_cmp(const X509 *a, const X509 *b) |
| 153 |
{ |
| 154 |
int rv = 0; |
| 155 |
|
| 156 |
if (a == b) /* for efficiency */ |
| 157 |
return 0; |
| 158 |
|
| 159 |
/* attempt to compute cert hash */ |
| 160 |
(void)X509_check_purpose((X509 *)a, -1, 0); |
| 161 |
(void)X509_check_purpose((X509 *)b, -1, 0); |
| 162 |
|
| 163 |
if ((a->ex_flags & EXFLAG_NO_FINGERPRINT) == 0 |
| 164 |
&& (b->ex_flags & EXFLAG_NO_FINGERPRINT) == 0) |
| 165 |
rv = memcmp(a->sha1_hash, b->sha1_hash, SHA_DIGEST_LENGTH); |
| 166 |
if (rv != 0) |
| 167 |
return rv < 0 ? -1 : 1; |
| 168 |
|
| 169 |
/* Check for match against stored encoding too */ |
| 170 |
if (!a->cert_info.enc.modified && !b->cert_info.enc.modified) { |
| 171 |
if (a->cert_info.enc.len < b->cert_info.enc.len) |
| 172 |
return -1; |
| 173 |
if (a->cert_info.enc.len > b->cert_info.enc.len) |
| 174 |
return 1; |
| 175 |
rv = memcmp(a->cert_info.enc.enc, |
| 176 |
b->cert_info.enc.enc, a->cert_info.enc.len); |
| 177 |
} |
| 178 |
return rv < 0 ? -1 : rv > 0; |
| 179 |
} |
| 180 |
|
| 181 |
int ossl_x509_add_cert_new(STACK_OF(X509) **p_sk, X509 *cert, int flags) |
| 182 |
{ |
| 183 |
if (*p_sk == NULL && (*p_sk = sk_X509_new_null()) == NULL) { |
| 184 |
ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
| 185 |
return 0; |
| 186 |
} |
| 187 |
return X509_add_cert(*p_sk, cert, flags); |
| 188 |
} |
| 189 |
|
| 190 |
int X509_add_cert(STACK_OF(X509) *sk, X509 *cert, int flags) |
| 191 |
{ |
| 192 |
if (sk == NULL) { |
| 193 |
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
| 194 |
return 0; |
| 195 |
} |
| 196 |
if (cert == NULL) |
| 197 |
return 0; |
| 198 |
if ((flags & X509_ADD_FLAG_NO_DUP) != 0) { |
| 199 |
/* |
| 200 |
* not using sk_X509_set_cmp_func() and sk_X509_find() |
| 201 |
* because this re-orders the certs on the stack |
| 202 |
*/ |
| 203 |
int i; |
| 204 |
|
| 205 |
for (i = 0; i < sk_X509_num(sk); i++) { |
| 206 |
if (X509_cmp(sk_X509_value(sk, i), cert) == 0) |
| 207 |
return 1; |
| 208 |
} |
| 209 |
} |
| 210 |
if ((flags & X509_ADD_FLAG_NO_SS) != 0) { |
| 211 |
int ret = X509_self_signed(cert, 0); |
| 212 |
|
| 213 |
if (ret != 0) |
| 214 |
return ret > 0 ? 1 : 0; |
| 215 |
} |
| 216 |
if ((flags & X509_ADD_FLAG_UP_REF) != 0 && !X509_up_ref(cert)) |
| 217 |
return 0; |
| 218 |
if (!sk_X509_insert(sk, cert, |
| 219 |
(flags & X509_ADD_FLAG_PREPEND) != 0 ? 0 : -1)) { |
| 220 |
if ((flags & X509_ADD_FLAG_UP_REF) != 0) |
| 221 |
X509_free(cert); |
| 222 |
ERR_raise(ERR_LIB_X509, ERR_R_CRYPTO_LIB); |
| 223 |
return 0; |
| 224 |
} |
| 225 |
return 1; |
| 226 |
} |
| 227 |
|
| 228 |
int X509_add_certs(STACK_OF(X509) *sk, STACK_OF(X509) *certs, int flags) |
| 229 |
/* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
| 230 |
{ |
| 231 |
if (sk == NULL) { |
| 232 |
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
| 233 |
return 0; |
| 234 |
} |
| 235 |
return ossl_x509_add_certs_new(&sk, certs, flags); |
| 236 |
} |
| 237 |
|
| 238 |
int ossl_x509_add_certs_new(STACK_OF(X509) **p_sk, STACK_OF(X509) *certs, |
| 239 |
int flags) |
| 240 |
/* compiler would allow 'const' for the certs, yet they may get up-ref'ed */ |
| 241 |
{ |
| 242 |
int n = sk_X509_num(certs /* may be NULL */); |
| 243 |
int i; |
| 244 |
|
| 245 |
for (i = 0; i < n; i++) { |
| 246 |
int j = (flags & X509_ADD_FLAG_PREPEND) == 0 ? i : n - 1 - i; |
| 247 |
/* if prepend, add certs in reverse order to keep original order */ |
| 248 |
|
| 249 |
if (!ossl_x509_add_cert_new(p_sk, sk_X509_value(certs, j), flags)) |
| 250 |
return 0; |
| 251 |
} |
| 252 |
return 1; |
| 253 |
} |
| 254 |
|
| 255 |
int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) |
| 256 |
{ |
| 257 |
int ret; |
| 258 |
|
| 259 |
if (b == NULL) |
| 260 |
return a != NULL; |
| 261 |
if (a == NULL) |
| 262 |
return -1; |
| 263 |
|
| 264 |
/* Ensure canonical encoding is present and up to date */ |
| 265 |
if (a->canon_enc == NULL || a->modified) { |
| 266 |
ret = i2d_X509_NAME((X509_NAME *)a, NULL); |
| 267 |
if (ret < 0) |
| 268 |
return -2; |
| 269 |
} |
| 270 |
|
| 271 |
if (b->canon_enc == NULL || b->modified) { |
| 272 |
ret = i2d_X509_NAME((X509_NAME *)b, NULL); |
| 273 |
if (ret < 0) |
| 274 |
return -2; |
| 275 |
} |
| 276 |
|
| 277 |
ret = a->canon_enclen - b->canon_enclen; |
| 278 |
if (ret == 0 && a->canon_enclen == 0) |
| 279 |
return 0; |
| 280 |
|
| 281 |
if (ret == 0) { |
| 282 |
if (a->canon_enc == NULL || b->canon_enc == NULL) |
| 283 |
return -2; |
| 284 |
ret = memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); |
| 285 |
} |
| 286 |
|
| 287 |
return ret < 0 ? -1 : ret > 0; |
| 288 |
} |
| 289 |
|
| 290 |
unsigned long X509_NAME_hash_ex(const X509_NAME *x, OSSL_LIB_CTX *libctx, |
| 291 |
const char *propq, int *ok) |
| 292 |
{ |
| 293 |
unsigned long ret = 0; |
| 294 |
unsigned char md[SHA_DIGEST_LENGTH]; |
| 295 |
EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq); |
| 296 |
int i2d_ret; |
| 297 |
|
| 298 |
/* Make sure X509_NAME structure contains valid cached encoding */ |
| 299 |
i2d_ret = i2d_X509_NAME(x, NULL); |
| 300 |
if (ok != NULL) |
| 301 |
*ok = 0; |
| 302 |
if (i2d_ret >= 0 && sha1 != NULL |
| 303 |
&& EVP_Digest(x->canon_enc, x->canon_enclen, md, NULL, sha1, NULL)) { |
| 304 |
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
| 305 |
if (ok != NULL) |
| 306 |
*ok = 1; |
| 307 |
} |
| 308 |
EVP_MD_free(sha1); |
| 309 |
return ret; |
| 310 |
} |
| 311 |
|
| 312 |
#ifndef OPENSSL_NO_MD5 |
| 313 |
/* |
| 314 |
* I now DER encode the name and hash it. Since I cache the DER encoding, |
| 315 |
* this is reasonably efficient. |
| 316 |
*/ |
| 317 |
unsigned long X509_NAME_hash_old(const X509_NAME *x) |
| 318 |
{ |
| 319 |
EVP_MD *md5 = EVP_MD_fetch(NULL, OSSL_DIGEST_NAME_MD5, "-fips"); |
| 320 |
EVP_MD_CTX *md_ctx = EVP_MD_CTX_new(); |
| 321 |
unsigned long ret = 0; |
| 322 |
unsigned char md[16]; |
| 323 |
|
| 324 |
if (md5 == NULL || md_ctx == NULL) |
| 325 |
goto end; |
| 326 |
|
| 327 |
/* Make sure X509_NAME structure contains valid cached encoding */ |
| 328 |
if (i2d_X509_NAME(x, NULL) < 0) |
| 329 |
goto end; |
| 330 |
|
| 331 |
if (EVP_DigestInit_ex(md_ctx, md5, NULL) |
| 332 |
&& EVP_DigestUpdate(md_ctx, x->bytes->data, x->bytes->length) |
| 333 |
&& EVP_DigestFinal_ex(md_ctx, md, NULL)) |
| 334 |
ret = (((unsigned long)md[0]) | ((unsigned long)md[1] << 8L) | ((unsigned long)md[2] << 16L) | ((unsigned long)md[3] << 24L)) & 0xffffffffL; |
| 335 |
|
| 336 |
end: |
| 337 |
EVP_MD_CTX_free(md_ctx); |
| 338 |
EVP_MD_free(md5); |
| 339 |
|
| 340 |
return ret; |
| 341 |
} |
| 342 |
#endif |
| 343 |
|
| 344 |
/* Search a stack of X509 for a match */ |
| 345 |
X509 *X509_find_by_issuer_and_serial(STACK_OF(X509) *sk, const X509_NAME *name, |
| 346 |
const ASN1_INTEGER *serial) |
| 347 |
{ |
| 348 |
int i; |
| 349 |
X509 x, *x509 = NULL; |
| 350 |
|
| 351 |
if (!sk) |
| 352 |
return NULL; |
| 353 |
|
| 354 |
x.cert_info.serialNumber = *serial; |
| 355 |
x.cert_info.issuer = (X509_NAME *)name; /* won't modify it */ |
| 356 |
|
| 357 |
for (i = 0; i < sk_X509_num(sk); i++) { |
| 358 |
x509 = sk_X509_value(sk, i); |
| 359 |
if (X509_issuer_and_serial_cmp(x509, &x) == 0) |
| 360 |
return x509; |
| 361 |
} |
| 362 |
return NULL; |
| 363 |
} |
| 364 |
|
| 365 |
X509 *X509_find_by_subject(STACK_OF(X509) *sk, const X509_NAME *name) |
| 366 |
{ |
| 367 |
X509 *x509; |
| 368 |
int i; |
| 369 |
|
| 370 |
for (i = 0; i < sk_X509_num(sk); i++) { |
| 371 |
x509 = sk_X509_value(sk, i); |
| 372 |
if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) |
| 373 |
return x509; |
| 374 |
} |
| 375 |
return NULL; |
| 376 |
} |
| 377 |
|
| 378 |
EVP_PKEY *X509_get0_pubkey(const X509 *x) |
| 379 |
{ |
| 380 |
if (x == NULL) |
| 381 |
return NULL; |
| 382 |
return X509_PUBKEY_get0(x->cert_info.key); |
| 383 |
} |
| 384 |
|
| 385 |
EVP_PKEY *X509_get_pubkey(X509 *x) |
| 386 |
{ |
| 387 |
if (x == NULL) |
| 388 |
return NULL; |
| 389 |
return X509_PUBKEY_get(x->cert_info.key); |
| 390 |
} |
| 391 |
|
| 392 |
int X509_check_private_key(const X509 *cert, const EVP_PKEY *pkey) |
| 393 |
{ |
| 394 |
const EVP_PKEY *xk = X509_get0_pubkey(cert); |
| 395 |
|
| 396 |
if (xk == NULL) { |
| 397 |
ERR_raise(ERR_LIB_X509, X509_R_UNABLE_TO_GET_CERTS_PUBLIC_KEY); |
| 398 |
return 0; |
| 399 |
} |
| 400 |
return ossl_x509_check_private_key(xk, pkey); |
| 401 |
} |
| 402 |
|
| 403 |
int ossl_x509_check_private_key(const EVP_PKEY *x, const EVP_PKEY *pkey) |
| 404 |
{ |
| 405 |
if (x == NULL) { |
| 406 |
ERR_raise(ERR_LIB_X509, ERR_R_PASSED_NULL_PARAMETER); |
| 407 |
return 0; |
| 408 |
} |
| 409 |
switch (EVP_PKEY_eq(x, pkey)) { |
| 410 |
case 1: |
| 411 |
return 1; |
| 412 |
case 0: |
| 413 |
ERR_raise(ERR_LIB_X509, X509_R_KEY_VALUES_MISMATCH); |
| 414 |
return 0; |
| 415 |
case -1: |
| 416 |
ERR_raise(ERR_LIB_X509, X509_R_KEY_TYPE_MISMATCH); |
| 417 |
return 0; |
| 418 |
case -2: |
| 419 |
ERR_raise(ERR_LIB_X509, X509_R_UNKNOWN_KEY_TYPE); |
| 420 |
/* fall thru */ |
| 421 |
default: |
| 422 |
return 0; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
/* |
| 427 |
* Check a suite B algorithm is permitted: pass in a public key and the NID |
| 428 |
* of its signature (or 0 if no signature). The pflags is a pointer to a |
| 429 |
* flags field which must contain the suite B verification flags. |
| 430 |
*/ |
| 431 |
|
| 432 |
#ifndef OPENSSL_NO_EC |
| 433 |
|
| 434 |
static int check_suite_b(EVP_PKEY *pkey, int sign_nid, unsigned long *pflags) |
| 435 |
{ |
| 436 |
char curve_name[80]; |
| 437 |
size_t curve_name_len; |
| 438 |
int curve_nid; |
| 439 |
|
| 440 |
if (pkey == NULL || !EVP_PKEY_is_a(pkey, "EC")) |
| 441 |
return X509_V_ERR_SUITE_B_INVALID_ALGORITHM; |
| 442 |
|
| 443 |
if (!EVP_PKEY_get_group_name(pkey, curve_name, sizeof(curve_name), |
| 444 |
&curve_name_len)) |
| 445 |
return X509_V_ERR_SUITE_B_INVALID_CURVE; |
| 446 |
|
| 447 |
curve_nid = OBJ_txt2nid(curve_name); |
| 448 |
/* Check curve is consistent with LOS */ |
| 449 |
if (curve_nid == NID_secp384r1) { /* P-384 */ |
| 450 |
/* |
| 451 |
* Check signature algorithm is consistent with curve. |
| 452 |
*/ |
| 453 |
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA384) |
| 454 |
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
| 455 |
if (!(*pflags & X509_V_FLAG_SUITEB_192_LOS)) |
| 456 |
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
| 457 |
/* If we encounter P-384 we cannot use P-256 later */ |
| 458 |
*pflags &= ~X509_V_FLAG_SUITEB_128_LOS_ONLY; |
| 459 |
} else if (curve_nid == NID_X9_62_prime256v1) { /* P-256 */ |
| 460 |
if (sign_nid != -1 && sign_nid != NID_ecdsa_with_SHA256) |
| 461 |
return X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM; |
| 462 |
if (!(*pflags & X509_V_FLAG_SUITEB_128_LOS_ONLY)) |
| 463 |
return X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED; |
| 464 |
} else { |
| 465 |
return X509_V_ERR_SUITE_B_INVALID_CURVE; |
| 466 |
} |
| 467 |
return X509_V_OK; |
| 468 |
} |
| 469 |
|
| 470 |
int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
| 471 |
unsigned long flags) |
| 472 |
{ |
| 473 |
int rv, i, sign_nid; |
| 474 |
EVP_PKEY *pk; |
| 475 |
unsigned long tflags = flags; |
| 476 |
|
| 477 |
if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
| 478 |
return X509_V_OK; |
| 479 |
|
| 480 |
/* If no EE certificate passed in must be first in chain */ |
| 481 |
if (x == NULL) { |
| 482 |
x = sk_X509_value(chain, 0); |
| 483 |
i = 1; |
| 484 |
} else { |
| 485 |
i = 0; |
| 486 |
} |
| 487 |
pk = X509_get0_pubkey(x); |
| 488 |
|
| 489 |
/* |
| 490 |
* With DANE-EE(3) success, or DANE-EE(3)/PKIX-EE(1) failure we don't build |
| 491 |
* a chain all, just report trust success or failure, but must also report |
| 492 |
* Suite-B errors if applicable. This is indicated via a NULL chain |
| 493 |
* pointer. All we need to do is check the leaf key algorithm. |
| 494 |
*/ |
| 495 |
if (chain == NULL) |
| 496 |
return check_suite_b(pk, -1, &tflags); |
| 497 |
|
| 498 |
if (X509_get_version(x) != X509_VERSION_3) { |
| 499 |
rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
| 500 |
/* Correct error depth */ |
| 501 |
i = 0; |
| 502 |
goto end; |
| 503 |
} |
| 504 |
|
| 505 |
/* Check EE key only */ |
| 506 |
rv = check_suite_b(pk, -1, &tflags); |
| 507 |
if (rv != X509_V_OK) { |
| 508 |
/* Correct error depth */ |
| 509 |
i = 0; |
| 510 |
goto end; |
| 511 |
} |
| 512 |
for (; i < sk_X509_num(chain); i++) { |
| 513 |
sign_nid = X509_get_signature_nid(x); |
| 514 |
x = sk_X509_value(chain, i); |
| 515 |
if (X509_get_version(x) != X509_VERSION_3) { |
| 516 |
rv = X509_V_ERR_SUITE_B_INVALID_VERSION; |
| 517 |
goto end; |
| 518 |
} |
| 519 |
pk = X509_get0_pubkey(x); |
| 520 |
rv = check_suite_b(pk, sign_nid, &tflags); |
| 521 |
if (rv != X509_V_OK) |
| 522 |
goto end; |
| 523 |
} |
| 524 |
|
| 525 |
/* Final check: root CA signature */ |
| 526 |
rv = check_suite_b(pk, X509_get_signature_nid(x), &tflags); |
| 527 |
end: |
| 528 |
if (rv != X509_V_OK) { |
| 529 |
/* Invalid signature or LOS errors are for previous cert */ |
| 530 |
if ((rv == X509_V_ERR_SUITE_B_INVALID_SIGNATURE_ALGORITHM |
| 531 |
|| rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED) |
| 532 |
&& i) |
| 533 |
i--; |
| 534 |
/* |
| 535 |
* If we have LOS error and flags changed then we are signing P-384 |
| 536 |
* with P-256. Use more meaningful error. |
| 537 |
*/ |
| 538 |
if (rv == X509_V_ERR_SUITE_B_LOS_NOT_ALLOWED && flags != tflags) |
| 539 |
rv = X509_V_ERR_SUITE_B_CANNOT_SIGN_P_384_WITH_P_256; |
| 540 |
if (perror_depth) |
| 541 |
*perror_depth = i; |
| 542 |
} |
| 543 |
return rv; |
| 544 |
} |
| 545 |
|
| 546 |
int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
| 547 |
{ |
| 548 |
int sign_nid; |
| 549 |
if (!(flags & X509_V_FLAG_SUITEB_128_LOS)) |
| 550 |
return X509_V_OK; |
| 551 |
sign_nid = OBJ_obj2nid(crl->crl.sig_alg.algorithm); |
| 552 |
return check_suite_b(pk, sign_nid, &flags); |
| 553 |
} |
| 554 |
|
| 555 |
#else |
| 556 |
int X509_chain_check_suiteb(int *perror_depth, X509 *x, STACK_OF(X509) *chain, |
| 557 |
unsigned long flags) |
| 558 |
{ |
| 559 |
return 0; |
| 560 |
} |
| 561 |
|
| 562 |
int X509_CRL_check_suiteb(X509_CRL *crl, EVP_PKEY *pk, unsigned long flags) |
| 563 |
{ |
| 564 |
return 0; |
| 565 |
} |
| 566 |
|
| 567 |
#endif |
| 568 |
|
| 569 |
/* |
| 570 |
* Not strictly speaking an "up_ref" as a STACK doesn't have a reference |
| 571 |
* count but it has the same effect by duping the STACK and upping the ref of |
| 572 |
* each X509 structure. |
| 573 |
*/ |
| 574 |
STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) |
| 575 |
{ |
| 576 |
STACK_OF(X509) *ret = sk_X509_dup(chain); |
| 577 |
int i; |
| 578 |
|
| 579 |
if (ret == NULL) |
| 580 |
return NULL; |
| 581 |
for (i = 0; i < sk_X509_num(ret); i++) { |
| 582 |
X509 *x = sk_X509_value(ret, i); |
| 583 |
|
| 584 |
if (!X509_up_ref(x)) |
| 585 |
goto err; |
| 586 |
} |
| 587 |
return ret; |
| 588 |
|
| 589 |
err: |
| 590 |
while (i-- > 0) |
| 591 |
X509_free(sk_X509_value(ret, i)); |
| 592 |
sk_X509_free(ret); |
| 593 |
return NULL; |
| 594 |
} |