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 <stdint.h>
17
18 #include "error/s2n_errno.h"
19
20 #include "tls/s2n_connection.h"
21 #include "tls/s2n_tls.h"
22 #include "tls/s2n_tls13_handshake.h"
23
24 #include "stuffer/s2n_stuffer.h"
25
26 #include "utils/s2n_safety.h"
27
s2n_client_finished_recv(struct s2n_connection * conn)28 int s2n_client_finished_recv(struct s2n_connection *conn)
29 {
30 uint8_t *our_version;
31 our_version = conn->handshake.client_finished;
32 uint8_t *their_version = s2n_stuffer_raw_read(&conn->handshake.io, S2N_TLS_FINISHED_LEN);
33 POSIX_ENSURE_REF(their_version);
34
35 S2N_ERROR_IF(!s2n_constant_time_equals(our_version, their_version, S2N_TLS_FINISHED_LEN) || conn->handshake.rsa_failed, S2N_ERR_BAD_MESSAGE);
36
37 return 0;
38 }
39
s2n_client_finished_send(struct s2n_connection * conn)40 int s2n_client_finished_send(struct s2n_connection *conn)
41 {
42 uint8_t *our_version;
43 POSIX_GUARD(s2n_prf_client_finished(conn));
44
45 struct s2n_blob seq = {.data = conn->secure.client_sequence_number,.size = sizeof(conn->secure.client_sequence_number) };
46 POSIX_GUARD(s2n_blob_zero(&seq));
47 our_version = conn->handshake.client_finished;
48
49 /* Update the server to use the cipher suite */
50 conn->client = &conn->secure;
51
52 if (conn->actual_protocol_version == S2N_SSLv3) {
53 POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, our_version, S2N_SSL_FINISHED_LEN));
54 } else {
55 POSIX_GUARD(s2n_stuffer_write_bytes(&conn->handshake.io, our_version, S2N_TLS_FINISHED_LEN));
56 }
57 return 0;
58 }
59
s2n_tls13_client_finished_recv(struct s2n_connection * conn)60 int s2n_tls13_client_finished_recv(struct s2n_connection *conn) {
61 POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
62
63 uint8_t length = s2n_stuffer_data_available(&conn->handshake.io);
64 S2N_ERROR_IF(length == 0, S2N_ERR_BAD_MESSAGE);
65
66 /* read finished mac from handshake */
67 struct s2n_blob wire_finished_mac = {0};
68 s2n_blob_init(&wire_finished_mac, s2n_stuffer_raw_read(&conn->handshake.io, length), length);
69
70 /* get tls13 keys */
71 s2n_tls13_connection_keys(keys, conn);
72
73 /* get transcript hash */
74 struct s2n_hash_state hash_state = {0};
75 POSIX_GUARD(s2n_handshake_get_hash_state(conn, keys.hash_algorithm, &hash_state));
76
77 struct s2n_blob finished_key = {0};
78 POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.client_finished, keys.size));
79
80 s2n_tls13_key_blob(client_finished_mac, keys.size);
81 POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, &hash_state, &client_finished_mac));
82
83 POSIX_GUARD(s2n_tls13_mac_verify(&keys, &client_finished_mac, &wire_finished_mac));
84
85 return 0;
86 }
87
s2n_tls13_client_finished_send(struct s2n_connection * conn)88 int s2n_tls13_client_finished_send(struct s2n_connection *conn) {
89 POSIX_ENSURE_EQ(conn->actual_protocol_version, S2N_TLS13);
90
91 /* get tls13 keys */
92 s2n_tls13_connection_keys(keys, conn);
93
94 /* get transcript hash */
95 struct s2n_hash_state hash_state = {0};
96 POSIX_GUARD(s2n_handshake_get_hash_state(conn, keys.hash_algorithm, &hash_state));
97
98 /* look up finished secret key */
99 struct s2n_blob finished_key = {0};
100 POSIX_GUARD(s2n_blob_init(&finished_key, conn->handshake.client_finished, keys.size));
101
102 /* generate the hashed message authenticated code */
103 s2n_stack_blob(client_finished_mac, keys.size, S2N_TLS13_SECRET_MAX_LEN);
104 POSIX_GUARD(s2n_tls13_calculate_finished_mac(&keys, &finished_key, &hash_state, &client_finished_mac));
105
106 /* write to handshake io */
107 POSIX_GUARD(s2n_stuffer_write(&conn->handshake.io, &client_finished_mac));
108
109 return 0;
110 }
111