-
Notifications
You must be signed in to change notification settings - Fork 718
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: getter for TLS1.2 master secrets #4470
Merged
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7478402
feat: getter for TLS1.2 master secrets
lrstewart 1d0b7ac
Why did we declare s2n_conn_get_current_message_type so many times?
lrstewart 9e5557e
Clarify valid versions
lrstewart c364184
skip sslv3 with fips
lrstewart b516b75
Comment
lrstewart 8c8ae45
Merge branch 'main' into master_secret
lrstewart edca1f8
Merge branch 'main' into master_secret
lrstewart File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
#include "tls/s2n_crypto.h" | ||
|
||
#include "s2n_test.h" | ||
#include "testlib/s2n_testlib.h" | ||
|
||
int main() | ||
{ | ||
BEGIN_TEST(); | ||
|
||
DEFER_CLEANUP(struct s2n_cert_chain_and_key *ecdsa_chain_and_key = NULL, | ||
s2n_cert_chain_and_key_ptr_free); | ||
EXPECT_SUCCESS(s2n_test_cert_chain_and_key_new(&ecdsa_chain_and_key, | ||
S2N_DEFAULT_ECDSA_TEST_CERT_CHAIN, S2N_DEFAULT_ECDSA_TEST_PRIVATE_KEY)); | ||
|
||
/* Test s2n_connection_get_master_secret */ | ||
{ | ||
const uint8_t test_secret[] = { | ||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, | ||
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x20, | ||
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10, | ||
0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFF, | ||
0x88, 0x87, 0x86, 0x85, 0x84, 0x83, 0x82, 0x81 | ||
}; | ||
|
||
/* s2n_connection_get_master_secret takes a constant connection, so our | ||
* tests can share the same connection. | ||
*/ | ||
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), | ||
s2n_connection_ptr_free); | ||
EXPECT_OK(s2n_skip_handshake(conn)); | ||
EXPECT_MEMCPY_SUCCESS(conn->secrets.version.tls12.master_secret, | ||
test_secret, sizeof(test_secret)); | ||
|
||
/* Test safety checks */ | ||
{ | ||
uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, NULL, 0), | ||
S2N_ERR_NULL); | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(NULL, output, 0), | ||
S2N_ERR_NULL); | ||
}; | ||
|
||
/* Test: successfully get master secret */ | ||
{ | ||
uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); | ||
EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); | ||
}; | ||
|
||
/* Test: TLS1.3 not supported */ | ||
{ | ||
uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
|
||
conn->actual_protocol_version = S2N_TLS13; | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, output, sizeof(output)), | ||
S2N_ERR_INVALID_STATE); | ||
|
||
conn->actual_protocol_version = S2N_TLS12; | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); | ||
EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); | ||
}; | ||
|
||
/* Test: at least S2N_TLS_SECRET_LEN of output required */ | ||
{ | ||
uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
|
||
/* Fail if insufficient memory */ | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, output, 0), | ||
S2N_ERR_INSUFFICIENT_MEM_SIZE); | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, output, 1), | ||
S2N_ERR_INSUFFICIENT_MEM_SIZE); | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN - 1), | ||
S2N_ERR_INSUFFICIENT_MEM_SIZE); | ||
|
||
/* Succeed if exactly S2N_TLS_SECRET_LEN bytes */ | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN)); | ||
EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); | ||
|
||
/* Succeed if more than S2N_TLS_SECRET_LEN bytes */ | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, S2N_TLS_SECRET_LEN + 1)); | ||
EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); | ||
}; | ||
|
||
/* Test: handshake must be complete */ | ||
{ | ||
uint8_t output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
|
||
conn->handshake.message_number = 0; | ||
EXPECT_FAILURE_WITH_ERRNO( | ||
s2n_connection_get_master_secret(conn, output, sizeof(output)), | ||
S2N_ERR_HANDSHAKE_NOT_COMPLETE); | ||
|
||
EXPECT_OK(s2n_skip_handshake(conn)); | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(conn, output, sizeof(output))); | ||
EXPECT_BYTEARRAY_EQUAL(test_secret, output, sizeof(output)); | ||
}; | ||
|
||
/* Test: self-talk */ | ||
{ | ||
DEFER_CLEANUP(struct s2n_config *config = s2n_config_new(), s2n_config_ptr_free); | ||
EXPECT_NOT_NULL(config); | ||
EXPECT_SUCCESS(s2n_config_add_cert_chain_and_key_to_store(config, ecdsa_chain_and_key)); | ||
EXPECT_SUCCESS(s2n_config_set_unsafe_for_testing(config)); | ||
EXPECT_SUCCESS(s2n_config_set_cipher_preferences(config, "test_all_tls12")); | ||
|
||
DEFER_CLEANUP(struct s2n_connection *client = s2n_connection_new(S2N_CLIENT), | ||
s2n_connection_ptr_free); | ||
EXPECT_NOT_NULL(client); | ||
EXPECT_SUCCESS(s2n_connection_set_config(client, config)); | ||
|
||
DEFER_CLEANUP(struct s2n_connection *server = s2n_connection_new(S2N_SERVER), | ||
s2n_connection_ptr_free); | ||
EXPECT_NOT_NULL(server); | ||
EXPECT_SUCCESS(s2n_connection_set_config(server, config)); | ||
|
||
struct s2n_test_io_pair io_pair = { 0 }; | ||
EXPECT_SUCCESS(s2n_io_pair_init_non_blocking(&io_pair)); | ||
EXPECT_SUCCESS(s2n_connections_set_io_pair(client, server, &io_pair)); | ||
EXPECT_SUCCESS(s2n_negotiate_test_server_and_client(server, client)); | ||
|
||
uint8_t server_output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(server, | ||
server_output, sizeof(server_output))); | ||
EXPECT_BYTEARRAY_EQUAL(server->secrets.version.tls12.master_secret, | ||
server_output, sizeof(server_output)); | ||
EXPECT_BYTEARRAY_EQUAL(client->secrets.version.tls12.master_secret, | ||
server_output, sizeof(server_output)); | ||
|
||
uint8_t client_output[S2N_TLS_SECRET_LEN] = { 0 }; | ||
EXPECT_SUCCESS(s2n_connection_get_master_secret(client, | ||
client_output, sizeof(client_output))); | ||
EXPECT_BYTEARRAY_EQUAL(server_output, client_output, sizeof(client_output)); | ||
}; | ||
}; | ||
|
||
END_TEST(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yay for less scary rust bindings 🥳
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Eeeeeeeeeh we still pass it a *mut pointer 🙃
I was just looking at this for our Send/Sync safety. No matter what we do with const, the bindings are still scary. You can get a
*mut s2n_connection
from a&Connection
, because you can get a*mut s2n_connection
from a&NonNull<s2n_connection>
. The joys of FFI.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, the
* mut
isn't very pretty but I'm mostly just celebrating the fact that the rust connection parameter&self
is way more obviously accurate for these bindings. Little wins 😄