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