| 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_fips.h" |
| 22 |
#include "crypto/s2n_hmac.h" |
| 23 |
#include "crypto/s2n_libcrypto.h" |
| 24 |
#include "crypto/s2n_openssl.h" |
| 25 |
#include "s2n_test.h" |
| 26 |
#include "stuffer/s2n_stuffer.h" |
| 27 |
#include "testlib/s2n_testlib.h" |
| 28 |
#include "tls/s2n_cipher_suites.h" |
| 29 |
#include "tls/s2n_prf.h" |
| 30 |
#include "tls/s2n_record.h" |
| 31 |
#include "utils/s2n_random.h" |
| 32 |
|
| 33 |
int main(int argc, char **argv) |
| 34 |
{ |
| 35 |
BEGIN_TEST(); |
| 36 |
|
| 37 |
|
| 38 |
if (S2N_OPENSSL_VERSION_AT_LEAST(3, 0, 0)) { |
| 39 |
EXPECT_FALSE(s2n_rc4.is_available()); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
if (s2n_libcrypto_is_awslc()) { |
| 44 |
EXPECT_TRUE(s2n_rc4.is_available()); |
| 45 |
} |
| 46 |
|
| 47 |
struct s2n_connection *conn = NULL; |
| 48 |
uint8_t mac_key[] = "sample mac key"; |
| 49 |
uint8_t rc4_key[] = "123456789012345"; |
| 50 |
struct s2n_blob key_iv = { 0 }; |
| 51 |
EXPECT_SUCCESS(s2n_blob_init(&key_iv, rc4_key, sizeof(rc4_key))); |
| 52 |
uint8_t random_data[S2N_DEFAULT_FRAGMENT_LENGTH + 1]; |
| 53 |
struct s2n_blob r = { 0 }; |
| 54 |
EXPECT_SUCCESS(s2n_blob_init(&r, random_data, sizeof(random_data))); |
| 55 |
|
| 56 |
EXPECT_SUCCESS(s2n_disable_tls13_in_test()); |
| 57 |
|
| 58 |
EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER)); |
| 59 |
EXPECT_OK(s2n_get_public_random_data(&r)); |
| 60 |
|
| 61 |
|
| 62 |
conn->server = conn->secure; |
| 63 |
conn->client = conn->secure; |
| 64 |
|
| 65 |
|
| 66 |
conn->secure->cipher_suite->record_alg = &s2n_record_alg_rc4_sha; |
| 67 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->server_key)); |
| 68 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->init(&conn->secure->client_key)); |
| 69 |
if (conn->secure->cipher_suite->record_alg->cipher->is_available()) { |
| 70 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &key_iv)); |
| 71 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &key_iv)); |
| 72 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->client_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 73 |
EXPECT_SUCCESS(s2n_hmac_init(&conn->secure->server_record_mac, S2N_HMAC_SHA1, mac_key, sizeof(mac_key))); |
| 74 |
conn->actual_protocol_version = S2N_TLS11; |
| 75 |
|
| 76 |
for (size_t i = 0; i <= S2N_DEFAULT_FRAGMENT_LENGTH + 1; i++) { |
| 77 |
struct s2n_blob in = { 0 }; |
| 78 |
EXPECT_SUCCESS(s2n_blob_init(&in, random_data, i)); |
| 79 |
int bytes_written = 0; |
| 80 |
|
| 81 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->out)); |
| 82 |
|
| 83 |
s2n_result result = s2n_record_write(conn, TLS_APPLICATION_DATA, &in); |
| 84 |
if (i <= S2N_DEFAULT_FRAGMENT_LENGTH) { |
| 85 |
EXPECT_OK(result); |
| 86 |
bytes_written = i; |
| 87 |
} else { |
| 88 |
EXPECT_ERROR_WITH_ERRNO(result, S2N_ERR_FRAGMENT_LENGTH_TOO_LARGE); |
| 89 |
bytes_written = S2N_DEFAULT_FRAGMENT_LENGTH; |
| 90 |
} |
| 91 |
|
| 92 |
uint16_t predicted_length = bytes_written + 20; |
| 93 |
EXPECT_EQUAL(conn->out.blob.data[0], TLS_APPLICATION_DATA); |
| 94 |
EXPECT_EQUAL(conn->out.blob.data[1], 3); |
| 95 |
EXPECT_EQUAL(conn->out.blob.data[2], 2); |
| 96 |
EXPECT_EQUAL(conn->out.blob.data[3], (predicted_length >> 8) & 0xff); |
| 97 |
EXPECT_EQUAL(conn->out.blob.data[4], predicted_length & 0xff); |
| 98 |
|
| 99 |
|
| 100 |
if (bytes_written > 10) { |
| 101 |
EXPECT_NOT_EQUAL(memcmp(conn->out.blob.data + 5, random_data, bytes_written), 0); |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 106 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 107 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->header_in, 5)); |
| 108 |
EXPECT_SUCCESS(s2n_stuffer_copy(&conn->out, &conn->in, s2n_stuffer_data_available(&conn->out))); |
| 109 |
|
| 110 |
|
| 111 |
EXPECT_EQUAL(bytes_written + 20, s2n_stuffer_data_available(&conn->in)); |
| 112 |
|
| 113 |
|
| 114 |
uint8_t content_type = 0; |
| 115 |
uint16_t fragment_length = 0; |
| 116 |
EXPECT_SUCCESS(s2n_record_header_parse(conn, &content_type, &fragment_length)); |
| 117 |
EXPECT_SUCCESS(s2n_record_parse(conn)); |
| 118 |
EXPECT_EQUAL(content_type, TLS_APPLICATION_DATA); |
| 119 |
EXPECT_EQUAL(fragment_length, predicted_length); |
| 120 |
|
| 121 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->header_in)); |
| 122 |
EXPECT_SUCCESS(s2n_stuffer_wipe(&conn->in)); |
| 123 |
} |
| 124 |
|
| 125 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->server_key)); |
| 126 |
EXPECT_OK(conn->secure->cipher_suite->record_alg->cipher->destroy_key(&conn->secure->client_key)); |
| 127 |
} else { |
| 128 |
EXPECT_ERROR_WITH_ERRNO(conn->secure->cipher_suite->record_alg->cipher->set_decryption_key(&conn->secure->client_key, &key_iv), S2N_ERR_KEY_INIT); |
| 129 |
EXPECT_ERROR_WITH_ERRNO(conn->secure->cipher_suite->record_alg->cipher->set_encryption_key(&conn->secure->server_key, &key_iv), S2N_ERR_KEY_INIT); |
| 130 |
} |
| 131 |
EXPECT_SUCCESS(s2n_connection_free(conn)); |
| 132 |
END_TEST(); |
| 133 |
} |