aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/aws/s2n/tls/s2n_change_cipher_spec.c
blob: 99dfdfb22a63236753b8feec0efc48b3525b7010 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * 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 <stdint.h>

#include "error/s2n_errno.h"

#include "tls/s2n_cipher_suites.h"
#include "tls/s2n_connection.h"
#include "tls/s2n_tls.h"

#include "stuffer/s2n_stuffer.h"

#include "utils/s2n_safety.h"

/* From RFC5246 7.1: https://tools.ietf.org/html/rfc5246#section-7.1 */
#define CHANGE_CIPHER_SPEC_TYPE  1

int s2n_basic_ccs_recv(struct s2n_connection *conn)
{
    uint8_t type;

    POSIX_GUARD(s2n_stuffer_read_uint8(&conn->handshake.io, &type));
    S2N_ERROR_IF(type != CHANGE_CIPHER_SPEC_TYPE, S2N_ERR_BAD_MESSAGE);

    return 0;
}

int s2n_client_ccs_recv(struct s2n_connection *conn)
{
    POSIX_GUARD(s2n_basic_ccs_recv(conn));

    /* Zero the sequence number */
    struct s2n_blob seq = {.data = conn->secure.client_sequence_number,.size = sizeof(conn->secure.client_sequence_number) };
    POSIX_GUARD(s2n_blob_zero(&seq));

    /* Compute the finished message */
    POSIX_GUARD(s2n_prf_client_finished(conn));

    /* Update the client to use the cipher-suite */
    conn->client = &conn->secure;

    /* Flush any partial alert messages that were pending.
     * If we don't do this, an attacker can inject a 1-byte alert message into the handshake
     * and cause later, valid alerts to be processed incorrectly. */
    POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));

    return 0;
}

int s2n_server_ccs_recv(struct s2n_connection *conn)
{
    POSIX_GUARD(s2n_basic_ccs_recv(conn));

    /* Zero the sequence number */
    struct s2n_blob seq = {.data = conn->secure.server_sequence_number,.size = sizeof(conn->secure.server_sequence_number) };
    POSIX_GUARD(s2n_blob_zero(&seq));

    /* Compute the finished message */
    POSIX_GUARD(s2n_prf_server_finished(conn));

    /* Update the secure state to active, and point the client at the active state */
    conn->server = &conn->secure;

    /* Flush any partial alert messages that were pending.
     * If we don't do this, an attacker can inject a 1-byte alert message into the handshake
     * and cause later, valid alerts to be processed incorrectly. */
    POSIX_GUARD(s2n_stuffer_wipe(&conn->alert_in));

    return 0;
}

int s2n_ccs_send(struct s2n_connection *conn)
{
    POSIX_GUARD(s2n_stuffer_write_uint8(&conn->handshake.io, CHANGE_CIPHER_SPEC_TYPE));

    return 0;
}