Debian Codegraph @

crt/s2n/tests/unit/s2n_handshake_misc_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 "s2n_test.h"
17 #include "tls/s2n_handshake.h"
18 #include "tls/s2n_tls.h"
19
20 static S2N_RESULT s2n_test_handshake_finish_header(uint32_t handshake_message_length)
21 {
22 DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
23 RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&stuffer, 0));
24
25 uint32_t stuffer_size = TLS_HANDSHAKE_HEADER_LENGTH + handshake_message_length;
26 RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&stuffer, stuffer_size));
27 RESULT_GUARD_POSIX(s2n_handshake_finish_header(&stuffer));
28
29 return S2N_RESULT_OK;
30 }
31
32 int main(int argc, char **argv)
33 {
34 BEGIN_TEST();
35
36 /* Test s2n_handshake_set_finished_len */
37 {
38 DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
39 EXPECT_NOT_NULL(conn);
40 const uint8_t max_len = sizeof(conn->handshake.client_finished);
41
42 /* Safety */
43 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(NULL, 0), S2N_ERR_NULL);
44
45 /* Length must be less than available memory */
46 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, UINT8_MAX), S2N_ERR_SAFETY);
47 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, max_len + 1), S2N_ERR_SAFETY);
48
49 /* Length must be greater than zero */
50 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, 0), S2N_ERR_SAFETY);
51
52 /* Length can change from zero to a valid length */
53 EXPECT_EQUAL(conn->handshake.finished_len, 0);
54 EXPECT_OK(s2n_handshake_set_finished_len(conn, max_len));
55 EXPECT_EQUAL(conn->handshake.finished_len, max_len);
56
57 /* Length can't change if already set.
58 * This method will be called when calculating both the client and server finished / verify_data.
59 * Both client and server should have the same length, or something has gone wrong in our implementation.
60 */
61 EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, max_len - 1), S2N_ERR_SAFETY);
62 };
63
64 /* Test: s2n_handshake_finish_header */
65 {
66 /* When handshake message size is larger than uint16_max */
67 EXPECT_OK(s2n_test_handshake_finish_header(UINT16_MAX + 1));
68
69 /* When handshake message size is larger than uint24_max */
70 EXPECT_ERROR_WITH_ERRNO(s2n_test_handshake_finish_header(1 << 24), S2N_ERR_INTEGER_OVERFLOW);
71 }
72
73 END_TEST();
74 }