Debian Codegraph @

crt/s2n/tests/testlib/s2n_key_schedule_testlib.c

raw symbols SCIP
1 /*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License").
5 * You may not use this file except in compliance with the License.
6 * A copy of the License is located at
7 *
8 * http://aws.amazon.com/apache2.0
9 *
10 * or in the "license" file accompanying this file. This file is distributed
11 * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12 * express or implied. See the License for the specific language governing
13 * permissions and limitations under the License.
14 */
15
16 #include "testlib/s2n_testlib.h"
17
18 S2N_RESULT s2n_connection_set_test_transcript_hash(struct s2n_connection *conn,
19 message_type_t message_type, const struct s2n_blob *digest)
20 {
21 RESULT_GUARD(s2n_connection_set_test_message_type(conn, message_type));
22 RESULT_CHECKED_MEMCPY(conn->handshake.hashes->transcript_hash_digest,
23 digest->data, digest->size);
24 return S2N_RESULT_OK;
25 }
26
27 S2N_RESULT s2n_connection_set_test_early_secret(struct s2n_connection *conn,
28 const struct s2n_blob *early_secret)
29 {
30 RESULT_ENSURE_REF(conn);
31 RESULT_ENSURE_REF(early_secret);
32 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.extract_secret,
33 early_secret->data, early_secret->size);
34 conn->secrets.extract_secret_type = S2N_EARLY_SECRET;
35 return S2N_RESULT_OK;
36 }
37
38 S2N_RESULT s2n_connection_set_test_handshake_secret(struct s2n_connection *conn,
39 const struct s2n_blob *handshake_secret)
40 {
41 RESULT_ENSURE_REF(conn);
42 RESULT_ENSURE_REF(handshake_secret);
43 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.extract_secret,
44 handshake_secret->data, handshake_secret->size);
45 conn->secrets.extract_secret_type = S2N_HANDSHAKE_SECRET;
46 return S2N_RESULT_OK;
47 }
48
49 S2N_RESULT s2n_connection_set_test_master_secret(struct s2n_connection *conn,
50 const struct s2n_blob *master_secret)
51 {
52 RESULT_ENSURE_REF(conn);
53 RESULT_ENSURE_REF(master_secret);
54 RESULT_CHECKED_MEMCPY(conn->secrets.version.tls13.extract_secret,
55 master_secret->data, master_secret->size);
56 conn->secrets.extract_secret_type = S2N_MASTER_SECRET;
57 return S2N_RESULT_OK;
58 }
59
60 /* This function will iterate over all rows and columns of the handshake state
61 * machine until it finds a valid (handshake_type, handshake_number) such that
62 * the active message is `expected_message_type`. If callers need to depend on a
63 * specific `message_number` or `handshake_type` this function should not be
64 * used.
65 */
66 S2N_RESULT s2n_connection_set_test_message_type(struct s2n_connection *conn, message_type_t expected_message_type)
67 {
68 for (uint32_t handshake = 0; handshake < S2N_HANDSHAKES_COUNT; handshake++) {
69 for (int message = 0; message < S2N_MAX_HANDSHAKE_LENGTH; message++) {
70 conn->handshake.handshake_type = handshake;
71 conn->handshake.message_number = message;
72 if (s2n_conn_get_current_message_type(conn) == expected_message_type) {
73 return S2N_RESULT_OK;
74 }
75 }
76 }
77 RESULT_BAIL(S2N_ERR_HANDSHAKE_UNREACHABLE);
78 }