| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "crypto/s2n_openssl_x509.h" |
| 17 |
|
| 18 |
#include "api/s2n.h" |
| 19 |
|
| 20 |
DEFINE_POINTER_CLEANUP_FUNC(EVP_PKEY *, EVP_PKEY_free); |
| 21 |
DEFINE_POINTER_CLEANUP_FUNC(EC_KEY *, EC_KEY_free); |
| 22 |
|
| 23 |
S2N_CLEANUP_RESULT s2n_openssl_x509_stack_pop_free(STACK_OF(X509) **cert_chain) |
| 24 |
{ |
| 25 |
RESULT_ENSURE_REF(*cert_chain); |
| 26 |
sk_X509_pop_free(*cert_chain, X509_free); |
| 27 |
*cert_chain = NULL; |
| 28 |
return S2N_RESULT_OK; |
| 29 |
} |
| 30 |
|
| 31 |
S2N_CLEANUP_RESULT s2n_openssl_asn1_time_free_pointer(ASN1_GENERALIZEDTIME **time_ptr) |
| 32 |
{ |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
RESULT_ENSURE_REF(*time_ptr); |
| 41 |
ASN1_STRING_free((ASN1_STRING *) *time_ptr); |
| 42 |
*time_ptr = NULL; |
| 43 |
return S2N_RESULT_OK; |
| 44 |
} |
| 45 |
|
| 46 |
S2N_RESULT s2n_openssl_x509_parse_impl(struct s2n_blob *asn1der, X509 **cert_out, uint32_t *parsed_length) |
| 47 |
{ |
| 48 |
RESULT_ENSURE_REF(asn1der); |
| 49 |
RESULT_ENSURE_REF(asn1der->data); |
| 50 |
RESULT_ENSURE_REF(cert_out); |
| 51 |
RESULT_ENSURE_REF(parsed_length); |
| 52 |
|
| 53 |
uint8_t *cert_to_parse = asn1der->data; |
| 54 |
*cert_out = d2i_X509(NULL, (const unsigned char **) (void *) &cert_to_parse, asn1der->size); |
| 55 |
RESULT_ENSURE(*cert_out != NULL, S2N_ERR_DECODE_CERTIFICATE); |
| 56 |
|
| 57 |
|
| 58 |
*parsed_length = cert_to_parse - asn1der->data; |
| 59 |
|
| 60 |
return S2N_RESULT_OK; |
| 61 |
} |
| 62 |
|
| 63 |
S2N_RESULT s2n_openssl_x509_parse_without_length_validation(struct s2n_blob *asn1der, X509 **cert_out) |
| 64 |
{ |
| 65 |
RESULT_ENSURE_REF(asn1der); |
| 66 |
RESULT_ENSURE_REF(cert_out); |
| 67 |
|
| 68 |
uint32_t parsed_len = 0; |
| 69 |
RESULT_GUARD(s2n_openssl_x509_parse_impl(asn1der, cert_out, &parsed_len)); |
| 70 |
|
| 71 |
return S2N_RESULT_OK; |
| 72 |
} |
| 73 |
|
| 74 |
S2N_RESULT s2n_openssl_x509_parse(struct s2n_blob *asn1der, X509 **cert_out) |
| 75 |
{ |
| 76 |
RESULT_ENSURE_REF(asn1der); |
| 77 |
RESULT_ENSURE_REF(cert_out); |
| 78 |
|
| 79 |
uint32_t parsed_len = 0; |
| 80 |
RESULT_GUARD(s2n_openssl_x509_parse_impl(asn1der, cert_out, &parsed_len)); |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
uint32_t trailing_bytes = asn1der->size - parsed_len; |
| 85 |
RESULT_ENSURE(trailing_bytes <= S2N_MAX_ALLOWED_CERT_TRAILING_BYTES, S2N_ERR_DECODE_CERTIFICATE); |
| 86 |
|
| 87 |
return S2N_RESULT_OK; |
| 88 |
} |
| 89 |
|
| 90 |
S2N_RESULT s2n_openssl_x509_get_cert_info(X509 *cert, struct s2n_cert_info *info) |
| 91 |
{ |
| 92 |
RESULT_ENSURE_REF(cert); |
| 93 |
RESULT_ENSURE_REF(info); |
| 94 |
|
| 95 |
X509_NAME *issuer_name = X509_get_issuer_name(cert); |
| 96 |
RESULT_ENSURE_REF(issuer_name); |
| 97 |
|
| 98 |
X509_NAME *subject_name = X509_get_subject_name(cert); |
| 99 |
RESULT_ENSURE_REF(subject_name); |
| 100 |
|
| 101 |
if (X509_NAME_cmp(issuer_name, subject_name) == 0) { |
| 102 |
info->self_signed = true; |
| 103 |
} else { |
| 104 |
info->self_signed = false; |
| 105 |
} |
| 106 |
|
| 107 |
#if defined(LIBRESSL_VERSION_NUMBER) && (LIBRESSL_VERSION_NUMBER < 0x02070000f) |
| 108 |
RESULT_ENSURE_REF(cert->sig_alg); |
| 109 |
info->signature_nid = OBJ_obj2nid(cert->sig_alg->algorithm); |
| 110 |
#else |
| 111 |
info->signature_nid = X509_get_signature_nid(cert); |
| 112 |
#endif |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
int find_result = OBJ_find_sigid_algs(info->signature_nid, &info->signature_digest_nid, NULL); |
| 121 |
if (find_result != 1) { |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
info->signature_digest_nid = 0; |
| 129 |
} |
| 130 |
|
| 131 |
DEFER_CLEANUP(EVP_PKEY *pubkey = X509_get_pubkey(cert), EVP_PKEY_free_pointer); |
| 132 |
RESULT_ENSURE(pubkey != NULL, S2N_ERR_DECODE_CERTIFICATE); |
| 133 |
|
| 134 |
info->public_key_bits = EVP_PKEY_bits(pubkey); |
| 135 |
RESULT_ENSURE(info->public_key_bits > 0, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 136 |
|
| 137 |
if (EVP_PKEY_base_id(pubkey) == EVP_PKEY_EC) { |
| 138 |
DEFER_CLEANUP(EC_KEY *ec_key = EVP_PKEY_get1_EC_KEY(pubkey), EC_KEY_free_pointer); |
| 139 |
RESULT_ENSURE_REF(ec_key); |
| 140 |
const EC_GROUP *ec_group = EC_KEY_get0_group(ec_key); |
| 141 |
RESULT_ENSURE_REF(ec_group); |
| 142 |
info->public_key_nid = EC_GROUP_get_curve_name(ec_group); |
| 143 |
} else { |
| 144 |
info->public_key_nid = EVP_PKEY_id(pubkey); |
| 145 |
} |
| 146 |
RESULT_ENSURE(info->public_key_nid != NID_undef, S2N_ERR_CERT_TYPE_UNSUPPORTED); |
| 147 |
|
| 148 |
return S2N_RESULT_OK; |
| 149 |
} |