| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "error/s2n_errno.h" |
| 17 |
#include "stuffer/s2n_stuffer.h" |
| 18 |
#include "tls/s2n_tls.h" |
| 19 |
#include "utils/s2n_safety.h" |
| 20 |
|
| 21 |
S2N_RESULT s2n_calculate_padding(uint8_t protocol_len, uint8_t *padding_len) |
| 22 |
{ |
| 23 |
RESULT_ENSURE_REF(padding_len); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
*padding_len = 32 - (((uint16_t) protocol_len + 2) % 32); |
| 30 |
return S2N_RESULT_OK; |
| 31 |
} |
| 32 |
|
| 33 |
S2N_RESULT s2n_write_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *out) |
| 34 |
{ |
| 35 |
RESULT_ENSURE_REF(conn); |
| 36 |
|
| 37 |
uint8_t protocol_len = strlen(conn->application_protocol); |
| 38 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(out, protocol_len)); |
| 39 |
RESULT_GUARD_POSIX(s2n_stuffer_write_bytes(out, (uint8_t *) conn->application_protocol, protocol_len)); |
| 40 |
|
| 41 |
uint8_t padding_len = 0; |
| 42 |
RESULT_GUARD(s2n_calculate_padding(protocol_len, &padding_len)); |
| 43 |
RESULT_GUARD_POSIX(s2n_stuffer_write_uint8(out, padding_len)); |
| 44 |
uint8_t *data_ptr = s2n_stuffer_raw_write(out, padding_len); |
| 45 |
RESULT_ENSURE_REF(data_ptr); |
| 46 |
RESULT_CHECKED_MEMSET(data_ptr, 0, padding_len); |
| 47 |
|
| 48 |
return S2N_RESULT_OK; |
| 49 |
} |
| 50 |
|
| 51 |
S2N_RESULT s2n_read_npn_protocol(struct s2n_connection *conn, struct s2n_stuffer *in) |
| 52 |
{ |
| 53 |
RESULT_ENSURE_REF(conn); |
| 54 |
|
| 55 |
uint8_t protocol_len = 0; |
| 56 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &protocol_len)); |
| 57 |
|
| 58 |
uint8_t *protocol = s2n_stuffer_raw_read(in, protocol_len); |
| 59 |
RESULT_ENSURE_REF(protocol); |
| 60 |
RESULT_CHECKED_MEMCPY(conn->application_protocol, protocol, protocol_len); |
| 61 |
conn->application_protocol[protocol_len] = '\0'; |
| 62 |
|
| 63 |
uint8_t expected_padding_len = 0; |
| 64 |
RESULT_GUARD(s2n_calculate_padding(protocol_len, &expected_padding_len)); |
| 65 |
uint8_t padding_len = 0; |
| 66 |
RESULT_GUARD_POSIX(s2n_stuffer_read_uint8(in, &padding_len)); |
| 67 |
RESULT_ENSURE_EQ(padding_len, expected_padding_len); |
| 68 |
|
| 69 |
uint8_t *data_ptr = s2n_stuffer_raw_read(in, padding_len); |
| 70 |
RESULT_ENSURE_REF(data_ptr); |
| 71 |
uint8_t empty_array[UINT8_MAX] = { 0 }; |
| 72 |
RESULT_ENSURE_EQ(s2n_constant_time_equals(data_ptr, empty_array, padding_len), 1); |
| 73 |
|
| 74 |
RESULT_ENSURE_EQ(s2n_stuffer_data_available(in), 0); |
| 75 |
|
| 76 |
return S2N_RESULT_OK; |
| 77 |
} |
| 78 |
|
| 79 |
int s2n_next_protocol_send(struct s2n_connection *conn) |
| 80 |
{ |
| 81 |
POSIX_ENSURE_REF(conn); |
| 82 |
POSIX_ENSURE(conn->actual_protocol_version < S2N_TLS13, S2N_ERR_BAD_MESSAGE); |
| 83 |
|
| 84 |
struct s2n_stuffer *out = &conn->handshake.io; |
| 85 |
POSIX_GUARD_RESULT(s2n_write_npn_protocol(conn, out)); |
| 86 |
|
| 87 |
POSIX_GUARD_RESULT(s2n_crypto_parameters_switch(conn)); |
| 88 |
|
| 89 |
return S2N_SUCCESS; |
| 90 |
} |
| 91 |
|
| 92 |
int s2n_next_protocol_recv(struct s2n_connection *conn) |
| 93 |
{ |
| 94 |
POSIX_ENSURE_REF(conn); |
| 95 |
POSIX_ENSURE(conn->actual_protocol_version < S2N_TLS13, S2N_ERR_BAD_MESSAGE); |
| 96 |
|
| 97 |
struct s2n_stuffer *in = &conn->handshake.io; |
| 98 |
POSIX_GUARD_RESULT(s2n_read_npn_protocol(conn, in)); |
| 99 |
|
| 100 |
return S2N_SUCCESS; |
| 101 |
} |