| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include "testlib/s2n_testlib.h" |
| 17 |
#include "tls/s2n_connection.h" |
| 18 |
|
| 19 |
#define S2N_TEST_PSK_VALUE "psk_test" |
| 20 |
|
| 21 |
struct s2n_psk *s2n_test_psk_new(struct s2n_connection *conn) |
| 22 |
{ |
| 23 |
PTR_ENSURE_REF(conn); |
| 24 |
|
| 25 |
|
| 26 |
uint8_t buffer[sizeof(S2N_TEST_PSK_VALUE) + 1] = { 0 }; |
| 27 |
int r = snprintf((char *) buffer, sizeof(buffer), "%s%u", S2N_TEST_PSK_VALUE, conn->psk_params.psk_list.len); |
| 28 |
PTR_ENSURE_GT(r, 0); |
| 29 |
PTR_ENSURE_LT(r, sizeof(buffer)); |
| 30 |
|
| 31 |
DEFER_CLEANUP(struct s2n_psk *psk = s2n_external_psk_new(), s2n_psk_free); |
| 32 |
PTR_GUARD_POSIX(s2n_psk_set_identity(psk, buffer, sizeof(buffer))); |
| 33 |
PTR_GUARD_POSIX(s2n_psk_set_secret(psk, buffer, sizeof(buffer))); |
| 34 |
|
| 35 |
struct s2n_psk *result_psk = psk; |
| 36 |
ZERO_TO_DISABLE_DEFER_CLEANUP(psk); |
| 37 |
return result_psk; |
| 38 |
} |
| 39 |
|
| 40 |
S2N_RESULT s2n_append_test_psk_with_early_data(struct s2n_connection *conn, uint32_t max_early_data, |
| 41 |
const struct s2n_cipher_suite *cipher_suite) |
| 42 |
{ |
| 43 |
RESULT_ENSURE_REF(conn); |
| 44 |
RESULT_ENSURE_REF(cipher_suite); |
| 45 |
|
| 46 |
DEFER_CLEANUP(struct s2n_psk *psk = s2n_test_psk_new(conn), s2n_psk_free); |
| 47 |
psk->hmac_alg = cipher_suite->prf_alg; |
| 48 |
if (max_early_data > 0) { |
| 49 |
RESULT_GUARD_POSIX(s2n_psk_configure_early_data(psk, max_early_data, |
| 50 |
cipher_suite->iana_value[0], cipher_suite->iana_value[1])); |
| 51 |
} |
| 52 |
RESULT_GUARD_POSIX(s2n_connection_append_psk(conn, psk)); |
| 53 |
return S2N_RESULT_OK; |
| 54 |
} |
| 55 |
|
| 56 |
S2N_RESULT s2n_append_test_chosen_psk_with_early_data(struct s2n_connection *conn, uint32_t max_early_data, |
| 57 |
const struct s2n_cipher_suite *cipher_suite) |
| 58 |
{ |
| 59 |
RESULT_ENSURE_REF(conn); |
| 60 |
RESULT_ENSURE_REF(cipher_suite); |
| 61 |
|
| 62 |
RESULT_GUARD(s2n_append_test_psk_with_early_data(conn, max_early_data, cipher_suite)); |
| 63 |
RESULT_ENSURE_GT(conn->psk_params.psk_list.len, 0); |
| 64 |
|
| 65 |
struct s2n_psk *last_psk = NULL; |
| 66 |
RESULT_GUARD(s2n_array_get(&conn->psk_params.psk_list, conn->psk_params.psk_list.len - 1, (void **) &last_psk)); |
| 67 |
conn->psk_params.chosen_psk = last_psk; |
| 68 |
|
| 69 |
return S2N_RESULT_OK; |
| 70 |
} |