Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_invariant_test.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 <errno.h>
17 #include <fcntl.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <unistd.h>
21
22 #include "api/s2n.h"
23 #include "crypto/s2n_fips.h"
24 #include "crypto/s2n_rsa_pss.h"
25 #include "s2n_test.h"
26 #include "testlib/s2n_testlib.h"
27 #include "tls/s2n_cipher_preferences.h"
28 #include "tls/s2n_cipher_suites.h"
29 #include "tls/s2n_connection.h"
30 #include "tls/s2n_handshake.h"
31 #include "tls/s2n_handshake_io.c"
32 #include "tls/s2n_tls13.h"
33 #include "utils/s2n_safety.h"
34
35 /* A full record that has an "APPLICATION_DATA" inside according to s2n's
36 * handshake message_types.
37 */
38 uint8_t record[] = {
39 /* Record type HANDSHAKE */
40 0x16,
41 /* Protocol version TLS 1.2 */
42 0x03, 0x03,
43 /* record len */
44 0x00, 0x05,
45 /* Type(s2n has this as expected message type for APPLICATION_DATA handler.
46 * This is not a standardized value, just something s2n has hardcoded as a placeholder
47 * For the APPLICATON_DATA state in the state machine.
48 */
49 0x00,
50 /* Len */
51 0x00, 0x00, 0x01,
52 /* Data */
53 0x00
54 };
55 static int amt_written = 0;
56
57 int s2n_app_data_in_handshake_record_recv_fn(void *io_context, uint8_t *buf, uint32_t len)
58 {
59 int amt_left = sizeof(record) - amt_written;
60 int to_write = S2N_MIN(len, amt_left);
61 POSIX_CHECKED_MEMCPY(buf, record + amt_written, to_write);
62 amt_written += to_write;
63 return to_write;
64 }
65
66 int main(int argc, char **argv)
67 {
68 BEGIN_TEST();
69 EXPECT_SUCCESS(s2n_disable_tls13_in_test());
70
71 struct s2n_connection *conn = NULL;
72 EXPECT_NOT_NULL(conn = s2n_connection_new(S2N_SERVER));
73
74 /* Initialize *some* handshake type. Not terribly relevant for this test. */
75 conn->handshake.handshake_type = NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY;
76 /* Fast forward the handshake state machine to the end of this "handshake_type".
77 * APPLICATION_DATA is the 11th state for "NEGOTIATED | FULL_HANDSHAKE | TLS12_PERFECT_FORWARD_SECRECY".
78 */
79 conn->handshake.message_number = 10;
80 conn->actual_protocol_version = S2N_TLS12;
81 /* Provide the crafted record to s2n's I/O */
82 s2n_connection_set_recv_cb(conn, s2n_app_data_in_handshake_record_recv_fn);
83
84 EXPECT_FAILURE_WITH_ERRNO(s2n_handshake_read_io(conn), S2N_ERR_BAD_MESSAGE);
85
86 s2n_connection_free(conn);
87 END_TEST();
88 }