| 1 |
// Copyright (C) 1995-1998 Eric Young ([email protected]) All rights reserved. |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include <openssl/asn1.h> |
| 7 |
#include <openssl/digest.h> |
| 8 |
#include <openssl/err.h> |
| 9 |
#include <openssl/mem.h> |
| 10 |
#include <openssl/md5.h> |
| 11 |
#include <openssl/obj.h> |
| 12 |
#include <openssl/sha.h> |
| 13 |
#include <openssl/stack.h> |
| 14 |
#include <openssl/x509.h> |
| 15 |
|
| 16 |
#include "../internal.h" |
| 17 |
#include "internal.h" |
| 18 |
|
| 19 |
|
| 20 |
int X509_issuer_name_cmp(const X509 *a, const X509 *b) { |
| 21 |
return (X509_NAME_cmp(a->cert_info->issuer, b->cert_info->issuer)); |
| 22 |
} |
| 23 |
|
| 24 |
int X509_subject_name_cmp(const X509 *a, const X509 *b) { |
| 25 |
return (X509_NAME_cmp(a->cert_info->subject, b->cert_info->subject)); |
| 26 |
} |
| 27 |
|
| 28 |
int X509_CRL_cmp(const X509_CRL *a, const X509_CRL *b) { |
| 29 |
return (X509_NAME_cmp(a->crl->issuer, b->crl->issuer)); |
| 30 |
} |
| 31 |
|
| 32 |
int X509_CRL_match(const X509_CRL *a, const X509_CRL *b) { |
| 33 |
return OPENSSL_memcmp(a->crl_hash, b->crl_hash, SHA256_DIGEST_LENGTH); |
| 34 |
} |
| 35 |
|
| 36 |
X509_NAME *X509_get_issuer_name(const X509 *a) { |
| 37 |
return a->cert_info->issuer; |
| 38 |
} |
| 39 |
|
| 40 |
uint32_t X509_issuer_name_hash(X509 *x) { |
| 41 |
return X509_NAME_hash(x->cert_info->issuer); |
| 42 |
} |
| 43 |
|
| 44 |
uint32_t X509_issuer_name_hash_old(X509 *x) { |
| 45 |
return (X509_NAME_hash_old(x->cert_info->issuer)); |
| 46 |
} |
| 47 |
|
| 48 |
X509_NAME *X509_get_subject_name(const X509 *a) { |
| 49 |
return a->cert_info->subject; |
| 50 |
} |
| 51 |
|
| 52 |
ASN1_INTEGER *X509_get_serialNumber(X509 *a) { |
| 53 |
return a->cert_info->serialNumber; |
| 54 |
} |
| 55 |
|
| 56 |
const ASN1_INTEGER *X509_get0_serialNumber(const X509 *x509) { |
| 57 |
return x509->cert_info->serialNumber; |
| 58 |
} |
| 59 |
|
| 60 |
uint32_t X509_subject_name_hash(X509 *x) { |
| 61 |
return X509_NAME_hash(x->cert_info->subject); |
| 62 |
} |
| 63 |
|
| 64 |
uint32_t X509_subject_name_hash_old(X509 *x) { |
| 65 |
return X509_NAME_hash_old(x->cert_info->subject); |
| 66 |
} |
| 67 |
|
| 68 |
// Compare two certificates: they must be identical for this to work. NB: |
| 69 |
// Although "cmp" operations are generally prototyped to take "const" |
| 70 |
// arguments (eg. for use in STACKs), the way X509 handling is - these |
| 71 |
// operations may involve ensuring the hashes are up-to-date and ensuring |
| 72 |
// certain cert information is cached. So this is the point where the |
| 73 |
// "depth-first" constification tree has to halt with an evil cast. |
| 74 |
int X509_cmp(const X509 *a, const X509 *b) { |
| 75 |
// Fill in the |cert_hash| fields. |
| 76 |
// |
| 77 |
// TODO(davidben): This may fail, in which case the the hash will be all |
| 78 |
// zeros. This produces a consistent comparison (failures are sticky), but |
| 79 |
// not a good one. OpenSSL now returns -2, but this is not a consistent |
| 80 |
// comparison and may cause misbehaving sorts by transitivity. For now, we |
| 81 |
// retain the old OpenSSL behavior, which was to ignore the error. See |
| 82 |
// https://crbug.com/boringssl/355. |
| 83 |
x509v3_cache_extensions((X509 *)a); |
| 84 |
x509v3_cache_extensions((X509 *)b); |
| 85 |
|
| 86 |
return OPENSSL_memcmp(a->cert_hash, b->cert_hash, SHA256_DIGEST_LENGTH); |
| 87 |
} |
| 88 |
|
| 89 |
int X509_NAME_cmp(const X509_NAME *a, const X509_NAME *b) { |
| 90 |
int ret; |
| 91 |
|
| 92 |
// Ensure canonical encoding is present and up to date |
| 93 |
|
| 94 |
if (!a->canon_enc || a->modified) { |
| 95 |
ret = i2d_X509_NAME((X509_NAME *)a, NULL); |
| 96 |
if (ret < 0) { |
| 97 |
return -2; |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
if (!b->canon_enc || b->modified) { |
| 102 |
ret = i2d_X509_NAME((X509_NAME *)b, NULL); |
| 103 |
if (ret < 0) { |
| 104 |
return -2; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
ret = a->canon_enclen - b->canon_enclen; |
| 109 |
|
| 110 |
if (ret) { |
| 111 |
return ret; |
| 112 |
} |
| 113 |
|
| 114 |
return OPENSSL_memcmp(a->canon_enc, b->canon_enc, a->canon_enclen); |
| 115 |
} |
| 116 |
|
| 117 |
uint32_t X509_NAME_hash(X509_NAME *x) { |
| 118 |
// Make sure the X509_NAME structure contains a valid cached encoding. |
| 119 |
if (i2d_X509_NAME(x, NULL) < 0) { |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
uint8_t md[SHA_DIGEST_LENGTH]; |
| 124 |
SHA1(x->canon_enc, x->canon_enclen, md); |
| 125 |
return CRYPTO_load_u32_le(md); |
| 126 |
} |
| 127 |
|
| 128 |
// I now DER encode the name and hash it. Since I cache the DER encoding, |
| 129 |
// this is reasonably efficient. |
| 130 |
|
| 131 |
uint32_t X509_NAME_hash_old(X509_NAME *x) { |
| 132 |
// Make sure the X509_NAME structure contains a valid cached encoding. |
| 133 |
if (i2d_X509_NAME(x, NULL) < 0) { |
| 134 |
return 0; |
| 135 |
} |
| 136 |
|
| 137 |
uint8_t md[SHA_DIGEST_LENGTH]; |
| 138 |
MD5((const uint8_t *)x->bytes->data, x->bytes->length, md); |
| 139 |
return CRYPTO_load_u32_le(md); |
| 140 |
} |
| 141 |
|
| 142 |
X509 *X509_find_by_issuer_and_serial(const STACK_OF(X509) *sk, X509_NAME *name, |
| 143 |
const ASN1_INTEGER *serial) { |
| 144 |
if (serial->type != V_ASN1_INTEGER && serial->type != V_ASN1_NEG_INTEGER) { |
| 145 |
return NULL; |
| 146 |
} |
| 147 |
|
| 148 |
for (size_t i = 0; i < sk_X509_num(sk); i++) { |
| 149 |
X509 *x509 = sk_X509_value(sk, i); |
| 150 |
if (ASN1_INTEGER_cmp(X509_get0_serialNumber(x509), serial) == 0 && |
| 151 |
X509_NAME_cmp(X509_get_issuer_name(x509), name) == 0) { |
| 152 |
return x509; |
| 153 |
} |
| 154 |
} |
| 155 |
return NULL; |
| 156 |
} |
| 157 |
|
| 158 |
X509 *X509_find_by_subject(const STACK_OF(X509) *sk, X509_NAME *name) { |
| 159 |
for (size_t i = 0; i < sk_X509_num(sk); i++) { |
| 160 |
X509 *x509 = sk_X509_value(sk, i); |
| 161 |
if (X509_NAME_cmp(X509_get_subject_name(x509), name) == 0) { |
| 162 |
return x509; |
| 163 |
} |
| 164 |
} |
| 165 |
return NULL; |
| 166 |
} |
| 167 |
|
| 168 |
EVP_PKEY *X509_get0_pubkey(const X509 *x) { |
| 169 |
if (x == NULL) { |
| 170 |
return NULL; |
| 171 |
} |
| 172 |
return X509_PUBKEY_get0(x->cert_info->key); |
| 173 |
} |
| 174 |
|
| 175 |
EVP_PKEY *X509_get_pubkey(const X509 *x) { |
| 176 |
if (x == NULL) { |
| 177 |
return NULL; |
| 178 |
} |
| 179 |
return X509_PUBKEY_get(x->cert_info->key); |
| 180 |
} |
| 181 |
|
| 182 |
ASN1_BIT_STRING *X509_get0_pubkey_bitstr(const X509 *x) { |
| 183 |
if (!x) { |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
return x->cert_info->key->public_key; |
| 187 |
} |
| 188 |
|
| 189 |
int X509_check_private_key(const X509 *x, const EVP_PKEY *k) { |
| 190 |
const EVP_PKEY *xk = X509_get0_pubkey(x); |
| 191 |
if (xk == NULL) { |
| 192 |
return 0; |
| 193 |
} |
| 194 |
|
| 195 |
int ret = EVP_PKEY_cmp(xk, k); |
| 196 |
if (ret > 0) { |
| 197 |
return 1; |
| 198 |
} |
| 199 |
|
| 200 |
switch (ret) { |
| 201 |
case 0: |
| 202 |
OPENSSL_PUT_ERROR(X509, X509_R_KEY_VALUES_MISMATCH); |
| 203 |
return 0; |
| 204 |
case -1: |
| 205 |
OPENSSL_PUT_ERROR(X509, X509_R_KEY_TYPE_MISMATCH); |
| 206 |
return 0; |
| 207 |
case -2: |
| 208 |
OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_KEY_TYPE); |
| 209 |
return 0; |
| 210 |
} |
| 211 |
|
| 212 |
return 0; |
| 213 |
} |
| 214 |
|
| 215 |
// Not strictly speaking an "up_ref" as a STACK doesn't have a reference |
| 216 |
// count but it has the same effect by duping the STACK and upping the ref of |
| 217 |
// each X509 structure. |
| 218 |
STACK_OF(X509) *X509_chain_up_ref(STACK_OF(X509) *chain) { |
| 219 |
STACK_OF(X509) *ret = sk_X509_dup(chain); |
| 220 |
if (ret == NULL) { |
| 221 |
return NULL; |
| 222 |
} |
| 223 |
for (size_t i = 0; i < sk_X509_num(ret); i++) { |
| 224 |
X509_up_ref(sk_X509_value(ret, i)); |
| 225 |
} |
| 226 |
return ret; |
| 227 |
} |