| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
#include "tls/extensions/s2n_cert_authorities.h" |
| 16 |
|
| 17 |
#include <openssl/x509.h> |
| 18 |
|
| 19 |
#include "utils/s2n_safety.h" |
| 20 |
|
| 21 |
bool s2n_cert_authorities_supported_from_trust_store() |
| 22 |
{ |
| 23 |
#if S2N_LIBCRYPTO_SUPPORTS_X509_STORE_LIST |
| 24 |
return true; |
| 25 |
#else |
| 26 |
return false; |
| 27 |
#endif |
| 28 |
} |
| 29 |
|
| 30 |
static S2N_RESULT s2n_cert_authorities_set_from_trust_store(struct s2n_config *config) |
| 31 |
{ |
| 32 |
RESULT_ENSURE_REF(config); |
| 33 |
|
| 34 |
if (!config->trust_store.trust_store) { |
| 35 |
return S2N_RESULT_OK; |
| 36 |
} |
| 37 |
|
| 38 |
#if S2N_LIBCRYPTO_SUPPORTS_X509_STORE_LIST |
| 39 |
DEFER_CLEANUP(struct s2n_stuffer output = { 0 }, s2n_stuffer_free); |
| 40 |
RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&output, 256)); |
| 41 |
|
| 42 |
STACK_OF(X509_OBJECT) *objects = X509_STORE_get0_objects(config->trust_store.trust_store); |
| 43 |
RESULT_ENSURE(objects, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 44 |
|
| 45 |
int objects_count = sk_X509_OBJECT_num(objects); |
| 46 |
RESULT_ENSURE(objects_count >= 0, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 47 |
|
| 48 |
for (int i = 0; i < objects_count; i++) { |
| 49 |
X509_OBJECT *x509_object = sk_X509_OBJECT_value(objects, i); |
| 50 |
RESULT_ENSURE(x509_object, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 51 |
|
| 52 |
X509 *cert = X509_OBJECT_get0_X509(x509_object); |
| 53 |
if (cert == NULL) { |
| 54 |
|
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
X509_NAME *name = X509_get_subject_name(cert); |
| 59 |
RESULT_ENSURE(name, S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 60 |
|
| 61 |
const uint8_t *name_bytes = NULL; |
| 62 |
size_t name_size = 0; |
| 63 |
RESULT_GUARD_OSSL(X509_NAME_get0_der(name, &name_bytes, &name_size), |
| 64 |
S2N_ERR_INTERNAL_LIBCRYPTO_ERROR); |
| 65 |
|
| 66 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint16(&output, name_size)); |
| 67 |
RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(&output, name_bytes, name_size)); |
| 68 |
RESULT_ENSURE(s2n_stuffer_data_available(&output) <= S2N_CERT_AUTHORITIES_MAX_SIZE, |
| 69 |
S2N_ERR_TOO_MANY_CAS); |
| 70 |
} |
| 71 |
|
| 72 |
RESULT_GUARD_POSIX(s2n_stuffer_extract_blob(&output, &config->cert_authorities)); |
| 73 |
return S2N_RESULT_OK; |
| 74 |
#else |
| 75 |
RESULT_BAIL(S2N_ERR_API_UNSUPPORTED_BY_LIBCRYPTO); |
| 76 |
#endif |
| 77 |
} |
| 78 |
|
| 79 |
int s2n_config_set_cert_authorities_from_trust_store(struct s2n_config *config) |
| 80 |
{ |
| 81 |
POSIX_ENSURE_REF(config); |
| 82 |
POSIX_ENSURE(!config->trust_store.loaded_system_certs, S2N_ERR_INVALID_STATE); |
| 83 |
POSIX_GUARD_RESULT(s2n_cert_authorities_set_from_trust_store(config)); |
| 84 |
return S2N_SUCCESS; |
| 85 |
} |
| 86 |
|
| 87 |
int s2n_cert_authorities_send(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 88 |
{ |
| 89 |
POSIX_ENSURE_REF(conn); |
| 90 |
POSIX_ENSURE_REF(conn->config); |
| 91 |
POSIX_ENSURE_EQ(conn->mode, S2N_SERVER); |
| 92 |
struct s2n_blob *cert_authorities = &conn->config->cert_authorities; |
| 93 |
POSIX_GUARD(s2n_stuffer_write_uint16(out, cert_authorities->size)); |
| 94 |
POSIX_GUARD(s2n_stuffer_write(out, cert_authorities)); |
| 95 |
return S2N_SUCCESS; |
| 96 |
} |
| 97 |
|
| 98 |
int s2n_cert_authorities_recv(struct s2n_connection *conn, struct s2n_stuffer *in) |
| 99 |
{ |
| 100 |
POSIX_ENSURE_REF(conn); |
| 101 |
POSIX_ENSURE_REF(conn->config); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
if (conn->mode == S2N_CLIENT && conn->config->cert_request_cb) { |
| 110 |
uint16_t length = 0; |
| 111 |
POSIX_GUARD(s2n_stuffer_read_uint16(in, &length)); |
| 112 |
POSIX_GUARD(s2n_stuffer_extract_blob(in, &conn->cert_authorities)); |
| 113 |
POSIX_ENSURE_EQ(conn->cert_authorities.size, length); |
| 114 |
} |
| 115 |
|
| 116 |
return S2N_SUCCESS; |
| 117 |
} |
| 118 |
|
| 119 |
static bool s2n_cert_authorities_should_send(struct s2n_connection *conn) |
| 120 |
{ |
| 121 |
return conn && conn->config && conn->config->cert_authorities.size > 0; |
| 122 |
} |
| 123 |
|
| 124 |
const s2n_extension_type s2n_cert_authorities_extension = { |
| 125 |
.iana_value = TLS_EXTENSION_CERT_AUTHORITIES, |
| 126 |
.minimum_version = S2N_TLS13, |
| 127 |
.is_response = false, |
| 128 |
.send = s2n_cert_authorities_send, |
| 129 |
.should_send = s2n_cert_authorities_should_send, |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
.recv = s2n_cert_authorities_recv, |
| 138 |
.if_missing = s2n_extension_noop_if_missing, |
| 139 |
}; |