| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "api/s2n.h" |
| 17 |
#include "crypto/s2n_certificate.h" |
| 18 |
#include "error/s2n_errno.h" |
| 19 |
#include "stuffer/s2n_stuffer.h" |
| 20 |
#include "tls/s2n_cipher_suites.h" |
| 21 |
#include "tls/s2n_config.h" |
| 22 |
#include "tls/s2n_connection.h" |
| 23 |
#include "tls/s2n_tls.h" |
| 24 |
#include "utils/s2n_blob.h" |
| 25 |
#include "utils/s2n_safety.h" |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
static S2N_RESULT s2n_client_cert_chain_store(struct s2n_connection *conn, |
| 55 |
struct s2n_blob *raw_cert_chain) |
| 56 |
{ |
| 57 |
RESULT_ENSURE_REF(conn); |
| 58 |
RESULT_ENSURE_REF(raw_cert_chain); |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
if (conn->handshake_params.client_cert_chain.size > 0) { |
| 64 |
return S2N_RESULT_OK; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
if (conn->actual_protocol_version < S2N_TLS13) { |
| 69 |
RESULT_GUARD_POSIX(s2n_dup(raw_cert_chain, &conn->handshake_params.client_cert_chain)); |
| 70 |
return S2N_RESULT_OK; |
| 71 |
} |
| 72 |
|
| 73 |
DEFER_CLEANUP(struct s2n_blob output = { 0 }, s2n_free); |
| 74 |
RESULT_GUARD_POSIX(s2n_realloc(&output, raw_cert_chain->size)); |
| 75 |
|
| 76 |
struct s2n_stuffer cert_chain_in = { 0 }; |
| 77 |
RESULT_GUARD_POSIX(s2n_stuffer_init_written(&cert_chain_in, raw_cert_chain)); |
| 78 |
|
| 79 |
struct s2n_stuffer cert_chain_out = { 0 }; |
| 80 |
RESULT_GUARD_POSIX(s2n_stuffer_init(&cert_chain_out, &output)); |
| 81 |
|
| 82 |
uint32_t cert_size = 0; |
| 83 |
uint16_t extensions_size = 0; |
| 84 |
while (s2n_stuffer_data_available(&cert_chain_in)) { |
| 85 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint24(&cert_chain_in, &cert_size)); |
| 86 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint24(&cert_chain_out, cert_size)); |
| 87 |
RESULT_GUARD_POSIX(s2n_stuffer_copy(&cert_chain_in, &cert_chain_out, cert_size)); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint16(&cert_chain_in, &extensions_size)); |
| 93 |
RESULT_GUARD_POSIX(s2n_stuffer_skip_read(&cert_chain_in, extensions_size)); |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
output.size = s2n_stuffer_data_available(&cert_chain_out); |
| 100 |
|
| 101 |
conn->handshake_params.client_cert_chain = output; |
| 102 |
ZERO_TO_DISABLE_DEFER_CLEANUP(output); |
| 103 |
return S2N_RESULT_OK; |
| 104 |
} |
| 105 |
|
| 106 |
int s2n_client_cert_recv(struct s2n_connection *conn) |
| 107 |
{ |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
struct s2n_stuffer in = conn->handshake.io; |
| 112 |
|
| 113 |
if (conn->actual_protocol_version == S2N_TLS13) { |
| 114 |
uint8_t certificate_request_context_len = 0; |
| 115 |
POSIX_GUARD(s2n_stuffer_read_uint8(&in, &certificate_request_context_len)); |
| 116 |
S2N_ERROR_IF(certificate_request_context_len != 0, S2N_ERR_BAD_MESSAGE); |
| 117 |
} |
| 118 |
|
| 119 |
uint32_t cert_chain_size = 0; |
| 120 |
POSIX_GUARD(s2n_stuffer_read_uint24(&in, &cert_chain_size)); |
| 121 |
POSIX_ENSURE(cert_chain_size <= s2n_stuffer_data_available(&in), S2N_ERR_BAD_MESSAGE); |
| 122 |
if (cert_chain_size == 0) { |
| 123 |
POSIX_GUARD(s2n_conn_set_handshake_no_client_cert(conn)); |
| 124 |
return S2N_SUCCESS; |
| 125 |
} |
| 126 |
|
| 127 |
uint8_t *cert_chain_data = s2n_stuffer_raw_read(&in, cert_chain_size); |
| 128 |
POSIX_ENSURE_REF(cert_chain_data); |
| 129 |
|
| 130 |
struct s2n_blob cert_chain = { 0 }; |
| 131 |
POSIX_GUARD(s2n_blob_init(&cert_chain, cert_chain_data, cert_chain_size)); |
| 132 |
POSIX_ENSURE(s2n_result_is_ok(s2n_client_cert_chain_store(conn, &cert_chain)), |
| 133 |
S2N_ERR_BAD_MESSAGE); |
| 134 |
|
| 135 |
DEFER_CLEANUP(s2n_cert_public_key public_key = { 0 }, s2n_pkey_free); |
| 136 |
POSIX_GUARD(s2n_pkey_zero_init(&public_key)); |
| 137 |
|
| 138 |
|
| 139 |
s2n_pkey_type pkey_type = S2N_PKEY_TYPE_UNKNOWN; |
| 140 |
POSIX_GUARD_RESULT(s2n_x509_validator_validate_cert_chain(&conn->x509_validator, conn, cert_chain_data, |
| 141 |
cert_chain_size, &pkey_type, &public_key)); |
| 142 |
|
| 143 |
conn->handshake_params.client_cert_pkey_type = pkey_type; |
| 144 |
POSIX_GUARD_RESULT(s2n_pkey_setup_for_type(&public_key, pkey_type)); |
| 145 |
|
| 146 |
POSIX_GUARD(s2n_pkey_check_key_exists(&public_key)); |
| 147 |
conn->handshake_params.client_public_key = public_key; |
| 148 |
ZERO_TO_DISABLE_DEFER_CLEANUP(public_key); |
| 149 |
|
| 150 |
|
| 151 |
conn->handshake.io = in; |
| 152 |
|
| 153 |
return S2N_SUCCESS; |
| 154 |
} |
| 155 |
|
| 156 |
int s2n_client_cert_send(struct s2n_connection *conn) |
| 157 |
{ |
| 158 |
struct s2n_cert_chain_and_key *chain_and_key = conn->handshake_params.our_chain_and_key; |
| 159 |
|
| 160 |
if (conn->actual_protocol_version >= S2N_TLS13) { |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
uint8_t certificate_request_context_len = 0; |
| 169 |
POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, certificate_request_context_len)); |
| 170 |
} |
| 171 |
|
| 172 |
if (chain_and_key == NULL) { |
| 173 |
POSIX_GUARD(s2n_conn_set_handshake_no_client_cert(conn)); |
| 174 |
POSIX_GUARD(s2n_send_empty_cert_chain(&conn->handshake.io)); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
POSIX_GUARD(s2n_send_cert_chain(conn, &conn->handshake.io, chain_and_key)); |
| 179 |
return S2N_SUCCESS; |
| 180 |
} |