Skip to content

Commit

Permalink
Merge branch 'main' into fix3
Browse files Browse the repository at this point in the history
  • Loading branch information
lrstewart authored Mar 12, 2024
2 parents 55ddb4e + dea5534 commit cc4a058
Show file tree
Hide file tree
Showing 9 changed files with 91 additions and 32 deletions.
24 changes: 24 additions & 0 deletions api/s2n.h
Original file line number Diff line number Diff line change
Expand Up @@ -1882,6 +1882,30 @@ S2N_API extern uint64_t s2n_connection_get_delay(struct s2n_connection *conn);
*/
S2N_API extern int s2n_connection_set_cipher_preferences(struct s2n_connection *conn, const char *version);

/**
* Used to indicate the type of key update that is being requested. For further
* information refer to `s2n_connection_request_key_update`.
*/
typedef enum {
S2N_KEY_UPDATE_NOT_REQUESTED = 0,
S2N_KEY_UPDATE_REQUESTED
} s2n_peer_key_update;

/**
* Signals the connection to do a key_update at the next possible opportunity. Note that the resulting key update message
* will not be sent until `s2n_send` is called.
*
* @param conn The connection object to trigger the key update on.
* @param peer_request Indicates if a key update should also be requested
* of the peer. When set to `S2N_KEY_UPDATE_NOT_REQUESTED`, then only the sending
* key of `conn` will be updated. If set to `S2N_KEY_UPDATE_REQUESTED`, then
* the sending key of conn will be updated AND the peer will be requested to
* update their sending key. Note that s2n-tls currently only supports
* `peer_request` being set to `S2N_KEY_UPDATE_NOT_REQUESTED` and will return
* S2N_FAILURE if any other value is used.
* @returns S2N_SUCCESS on success. S2N_FAILURE on failure
*/
S2N_API extern int s2n_connection_request_key_update(struct s2n_connection *conn, s2n_peer_key_update peer_request);
/**
* Appends the provided application protocol to the preference list
*
Expand Down
7 changes: 6 additions & 1 deletion bindings/rust/s2n-tls/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ impl Drop for Config {
}
}

#[derive(Default)]
pub struct Builder {
config: Config,
load_system_certs: bool,
Expand Down Expand Up @@ -743,6 +742,12 @@ impl Builder {
}
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}

pub(crate) struct Context {
refcount: AtomicUsize,
pub(crate) client_hello_callback: Option<Box<dyn ClientHelloCallback>>,
Expand Down
44 changes: 24 additions & 20 deletions bindings/rust/s2n-tls/src/testing/s2n_tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,32 +756,36 @@ mod tests {
// Load the server certificate into the trust store by overriding the OpenSSL default
// certificate location.
temp_env::with_var("SSL_CERT_FILE", Some(keypair.cert_path()), || {
let mut builder = Builder::new();
builder
.load_pem(keypair.cert(), keypair.key())
.unwrap()
.set_security_policy(&security::DEFAULT_TLS13)
.unwrap()
.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})
.unwrap();
// Test the Builder itself, and also the Builder produced by the Config builder() API.
for mut builder in [Builder::new(), Config::builder()] {
builder
.load_pem(keypair.cert(), keypair.key())
.unwrap()
.set_security_policy(&security::DEFAULT_TLS13)
.unwrap()
.set_verify_host_callback(InsecureAcceptAllCertificatesHandler {})
.unwrap();

// Disable loading system certificates
builder.with_system_certs(false).unwrap();
// Disable loading system certificates
builder.with_system_certs(false).unwrap();

let config = builder.build().unwrap();
let mut config_with_system_certs = config.clone();
let config = builder.build().unwrap();
let mut config_with_system_certs = config.clone();

let mut pair = tls_pair(config);
let mut pair = tls_pair(config);

// System certificates should not be loaded into the trust store. The handshake
// should fail since the certificate should not be trusted.
assert!(poll_tls_pair_result(&mut pair).is_err());
// System certificates should not be loaded into the trust store. The handshake
// should fail since the certificate should not be trusted.
assert!(poll_tls_pair_result(&mut pair).is_err());

// The handshake should succeed after trusting the certificate.
unsafe {
s2n_tls_sys::s2n_config_load_system_certs(config_with_system_certs.as_mut_ptr());
// The handshake should succeed after trusting the certificate.
unsafe {
s2n_tls_sys::s2n_config_load_system_certs(
config_with_system_certs.as_mut_ptr(),
);
}
establish_connection(config_with_system_certs);
}
establish_connection(config_with_system_certs);
});
}

Expand Down
2 changes: 1 addition & 1 deletion tests/sidetrail/working/patches/cbc.patch
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ index 401ab760..f39cc7e2 100644
*/
int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted)
{
- uint8_t mac_digest_size;
- uint8_t mac_digest_size = 0;
- POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));
+ uint8_t mac_digest_size = DIGEST_SIZE;
+ //POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));
Expand Down
24 changes: 23 additions & 1 deletion tests/unit/s2n_key_update_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ int main(int argc, char **argv)
EXPECT_SUCCESS(s2n_stuffer_growable_alloc(&stuffer, 0));
EXPECT_SUCCESS(s2n_connection_set_io_stuffers(&stuffer, &stuffer, client_conn));

s2n_atomic_flag_set(&client_conn->key_update_pending);
EXPECT_SUCCESS(s2n_connection_request_key_update(client_conn, S2N_KEY_UPDATE_NOT_REQUESTED));

s2n_blocked_status blocked = 0;
EXPECT_SUCCESS(s2n_key_update_send(client_conn, &blocked));
Expand Down Expand Up @@ -677,5 +677,27 @@ int main(int argc, char **argv)
}
}

/* s2n_connection_key_update_requested */
{
/* null safety */
{
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_request_key_update(NULL, S2N_KEY_UPDATE_NOT_REQUESTED), S2N_ERR_NULL);
};

/* usage */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_FAILURE_WITH_ERRNO(s2n_connection_request_key_update(conn, S2N_KEY_UPDATE_REQUESTED), S2N_ERR_INVALID_ARGUMENT);
};

/* happy path */
{
DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
EXPECT_FALSE(s2n_atomic_flag_test(&conn->key_update_pending));
EXPECT_SUCCESS(s2n_connection_request_key_update(conn, S2N_KEY_UPDATE_NOT_REQUESTED));
EXPECT_TRUE(s2n_atomic_flag_test(&conn->key_update_pending));
};
};

END_TEST();
}
2 changes: 1 addition & 1 deletion tests/unit/s2n_key_update_threads_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
_##name##_record_alg.encryption_limit = limit; \
name.record_alg = &_##name##_record_alg;

S2N_RESULT s2n_set_key_update_request_for_testing(keyupdate_request request);
S2N_RESULT s2n_set_key_update_request_for_testing(s2n_peer_key_update request);

static void *s2n_send_random_data(void *arg)
{
Expand Down
2 changes: 1 addition & 1 deletion tls/s2n_cbc.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
*/
int s2n_verify_cbc(struct s2n_connection *conn, struct s2n_hmac_state *hmac, struct s2n_blob *decrypted)
{
uint8_t mac_digest_size;
uint8_t mac_digest_size = 0;
POSIX_GUARD(s2n_hmac_digest_size(hmac->alg, &mac_digest_size));

/* The record has to be at least big enough to contain the MAC,
Expand Down
13 changes: 11 additions & 2 deletions tls/s2n_key_update.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@
#include "utils/s2n_atomic.h"
#include "utils/s2n_safety.h"

static keyupdate_request key_update_request_val = S2N_KEY_UPDATE_NOT_REQUESTED;
static s2n_peer_key_update key_update_request_val = S2N_KEY_UPDATE_NOT_REQUESTED;

int s2n_key_update_write(struct s2n_blob *out);
int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequence_number);

S2N_RESULT s2n_set_key_update_request_for_testing(keyupdate_request request)
S2N_RESULT s2n_set_key_update_request_for_testing(s2n_peer_key_update request)
{
RESULT_ENSURE(s2n_in_unit_test(), S2N_ERR_NOT_IN_UNIT_TEST);
key_update_request_val = request;
Expand Down Expand Up @@ -146,3 +146,12 @@ int s2n_check_record_limit(struct s2n_connection *conn, struct s2n_blob *sequenc

return S2N_SUCCESS;
}

int s2n_connection_request_key_update(struct s2n_connection *conn, s2n_peer_key_update peer_request)
{
POSIX_ENSURE_REF(conn);
/* s2n-tls does not currently support requesting key updates from peers */
POSIX_ENSURE(peer_request == S2N_KEY_UPDATE_NOT_REQUESTED, S2N_ERR_INVALID_ARGUMENT);
s2n_atomic_flag_set(&conn->key_update_pending);
return S2N_SUCCESS;
}
5 changes: 0 additions & 5 deletions tls/s2n_key_update.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,5 @@ typedef enum {
RECEIVING
} keyupdate_status;

typedef enum {
S2N_KEY_UPDATE_NOT_REQUESTED = 0,
S2N_KEY_UPDATE_REQUESTED
} keyupdate_request;

int s2n_key_update_recv(struct s2n_connection *conn, struct s2n_stuffer *request);
int s2n_key_update_send(struct s2n_connection *conn, s2n_blocked_status *blocked);

0 comments on commit cc4a058

Please sign in to comment.