File: s2n_handshake_misc_test.c

package info (click to toggle)
aws-crt-python 0.28.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 78,428 kB
  • sloc: ansic: 437,955; python: 27,657; makefile: 5,855; sh: 4,289; ruby: 208; java: 82; perl: 73; cpp: 25; xml: 11
file content (74 lines) | stat: -rw-r--r-- 2,967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
 * 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 "s2n_test.h"
#include "tls/s2n_handshake.h"
#include "tls/s2n_tls.h"

static S2N_RESULT s2n_test_handshake_finish_header(uint32_t handshake_message_length)
{
    DEFER_CLEANUP(struct s2n_stuffer stuffer = { 0 }, s2n_stuffer_free);
    RESULT_GUARD_POSIX(s2n_stuffer_growable_alloc(&stuffer, 0));

    uint32_t stuffer_size = TLS_HANDSHAKE_HEADER_LENGTH + handshake_message_length;
    RESULT_GUARD_POSIX(s2n_stuffer_skip_write(&stuffer, stuffer_size));
    RESULT_GUARD_POSIX(s2n_handshake_finish_header(&stuffer));

    return S2N_RESULT_OK;
}

int main(int argc, char **argv)
{
    BEGIN_TEST();

    /* Test s2n_handshake_set_finished_len */
    {
        DEFER_CLEANUP(struct s2n_connection *conn = s2n_connection_new(S2N_SERVER), s2n_connection_ptr_free);
        EXPECT_NOT_NULL(conn);
        const uint8_t max_len = sizeof(conn->handshake.client_finished);

        /* Safety */
        EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(NULL, 0), S2N_ERR_NULL);

        /* Length must be less than available memory */
        EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, UINT8_MAX), S2N_ERR_SAFETY);
        EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, max_len + 1), S2N_ERR_SAFETY);

        /* Length must be greater than zero */
        EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, 0), S2N_ERR_SAFETY);

        /* Length can change from zero to a valid length */
        EXPECT_EQUAL(conn->handshake.finished_len, 0);
        EXPECT_OK(s2n_handshake_set_finished_len(conn, max_len));
        EXPECT_EQUAL(conn->handshake.finished_len, max_len);

        /* Length can't change if already set.
         * This method will be called when calculating both the client and server finished / verify_data.
         * Both client and server should have the same length, or something has gone wrong in our implementation.
         */
        EXPECT_ERROR_WITH_ERRNO(s2n_handshake_set_finished_len(conn, max_len - 1), S2N_ERR_SAFETY);
    };

    /* Test: s2n_handshake_finish_header */
    {
        /* When handshake message size is larger than uint16_max */
        EXPECT_OK(s2n_test_handshake_finish_header(UINT16_MAX + 1));

        /* When handshake message size is larger than uint24_max */
        EXPECT_ERROR_WITH_ERRNO(s2n_test_handshake_finish_header(1 << 24), S2N_ERR_INTEGER_OVERFLOW);
    }

    END_TEST();
}