| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
#include <stdio.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#include "api/s2n.h" |
| 20 |
#include "crypto/s2n_cipher.h" |
| 21 |
#include "crypto/s2n_hmac.h" |
| 22 |
#include "s2n_test.h" |
| 23 |
#include "stuffer/s2n_stuffer.h" |
| 24 |
#include "testlib/s2n_testlib.h" |
| 25 |
#include "tls/s2n_cipher_suites.h" |
| 26 |
#include "tls/s2n_prf.h" |
| 27 |
#include "tls/s2n_record.h" |
| 28 |
#include "utils/s2n_random.h" |
| 29 |
|
| 30 |
int main(int argc, char **argv) |
| 31 |
{ |
| 32 |
struct s2n_connection *conn = NULL; |
| 33 |
uint8_t mac_key[] = "sample mac key"; |
| 34 |
uint8_t aes128_key[] = "123456789012345"; |
| 35 |
uint8_t aes256_key[] = "1234567890123456789012345678901"; |
| 36 |
struct s2n_blob aes128 = { 0 }; |
| 37 |
EXPECT_SUCCESS(s2n_blob_init(&aes128, aes128_key, sizeof(aes128_key))); |
| 38 |
struct s2n_blob aes256 = { 0 }; |
| 39 |
EXPECT_SUCCESS(s2n_blob_init(&aes256, aes256_key, sizeof(aes256_key))); |
| 40 |
uint8_t random_data[S2N_DEFAULT_FRAGMENT_LENGTH + 1]; |
| 41 |
struct s2n_blob r = { 0 }; |
| 42 |
EXPECT_SUCCESS(s2n_blob_init(&r, random_data, sizeof(random_data))); |
| 43 |
|
| 44 |
BEGIN_TEST(); |
| 45 |
EXPECT_SUCCESS(s2n_disable_tls13_in_test()); |
| 46 |
|
| 47 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 48 |
EXPECT_OK(s2n_get_public_random_data(&r)); |
| 49 |
|
| 50 |
|
| 51 |
conn->server = conn->secure; |
| 52 |
conn->client = conn->secure; |
| 53 |
|
| 54 |
|
| 55 |
conn->secure->cipher_suite->record_alg = &s2n_record_alg_aes128_sha; |
| 56 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->server_key)); |
| 57 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->client_key)); |
| 58 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &aes128)); |
| 59 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &aes128)); |
| 60 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->client_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 61 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->server_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 62 |
conn->actual_protocol_version = S2N_TLS11; |
| 63 |
|
| 64 |
for (size_t i = 0; i <= S2N_DEFAULT_FRAGMENT_LENGTH + 1; i++) { |
| 65 |
struct s2n_blob in = { 0 }; |
| 66 |
EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i)); |
| 67 |
int bytes_written = 0; |
| 68 |
|
| 69 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out)); |
| 70 |
|
| 71 |
s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in); |
| 72 |
if (i <= S2N_DEFAULT_FRAGMENT_LENGTH) { |
| 73 |
EXPECT_OK(result); |
| 74 |
bytes_written = i; |
| 75 |
} else { |
| 76 |
EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE); |
| 77 |
bytes_written = S2N_DEFAULT_FRAGMENT_LENGTH; |
| 78 |
} |
| 79 |
|
| 80 |
uint16_t predicted_length = bytes_written + 1 + 20 + 16; |
| 81 |
if (predicted_length % 16) { |
| 82 |
predicted_length += (16 - (predicted_length % 16)); |
| 83 |
} |
| 84 |
EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA); |
| 85 |
EXPECT_EQUAL(conn->out.blob.data[1], 3); |
| 86 |
EXPECT_EQUAL(conn->out.blob.data[2], 2); |
| 87 |
EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff); |
| 88 |
EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff); |
| 89 |
|
| 90 |
|
| 91 |
if (bytes_written > 10) { |
| 92 |
EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + 5, random_data, bytes_written), 0); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 97 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 98 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5)); |
| 99 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out))); |
| 100 |
|
| 101 |
|
| 102 |
uint8_t content_type = 0; |
| 103 |
uint16_t fragment_length = 0; |
| 104 |
EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length)); |
| 105 |
EXPECT_SUCCESS(s2n_record_parse(conn)); |
| 106 |
EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA); |
| 107 |
EXPECT_EQUAL(fragment_length, predicted_length); |
| 108 |
|
| 109 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 110 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 111 |
} |
| 112 |
|
| 113 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->server_key)); |
| 114 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->client_key)); |
| 115 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 116 |
|
| 117 |
|
| 118 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 119 |
conn->server = conn->secure; |
| 120 |
conn->client = conn->secure; |
| 121 |
conn->secure->cipher_suite->record_alg = &s2n_record_alg_aes256_sha; |
| 122 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->server_key)); |
| 123 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->client_key)); |
| 124 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &aes256)); |
| 125 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &aes256)); |
| 126 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->client_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 127 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->server_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 128 |
conn->actual_protocol_version = S2N_TLS11; |
| 129 |
|
| 130 |
for (size_t i = 0; i <= S2N_DEFAULT_FRAGMENT_LENGTH + 1; i++) { |
| 131 |
struct s2n_blob in = { 0 }; |
| 132 |
EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i)); |
| 133 |
int bytes_written = 0; |
| 134 |
|
| 135 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out)); |
| 136 |
|
| 137 |
s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in); |
| 138 |
if (i <= S2N_DEFAULT_FRAGMENT_LENGTH) { |
| 139 |
EXPECT_OK(result); |
| 140 |
bytes_written = i; |
| 141 |
} else { |
| 142 |
EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE); |
| 143 |
bytes_written = S2N_DEFAULT_FRAGMENT_LENGTH; |
| 144 |
} |
| 145 |
|
| 146 |
uint16_t predicted_length = bytes_written + 1 + 20 + 16; |
| 147 |
if (predicted_length % 16) { |
| 148 |
predicted_length += (16 - (predicted_length % 16)); |
| 149 |
} |
| 150 |
EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA); |
| 151 |
EXPECT_EQUAL(conn->out.blob.data[1], 3); |
| 152 |
EXPECT_EQUAL(conn->out.blob.data[2], 2); |
| 153 |
EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff); |
| 154 |
EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff); |
| 155 |
|
| 156 |
|
| 157 |
if (bytes_written > 10) { |
| 158 |
EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + 5, random_data, bytes_written), 0); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 163 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 164 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5)); |
| 165 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out))); |
| 166 |
|
| 167 |
|
| 168 |
uint8_t content_type = 0; |
| 169 |
uint16_t fragment_length = 0; |
| 170 |
EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length)); |
| 171 |
EXPECT_SUCCESS(s2n_record_parse(conn)); |
| 172 |
EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA); |
| 173 |
EXPECT_EQUAL(fragment_length, predicted_length); |
| 174 |
|
| 175 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 176 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 177 |
} |
| 178 |
|
| 179 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->server_key)); |
| 180 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->client_key)); |
| 181 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 182 |
|
| 183 |
END_TEST(); |
| 184 |
} |