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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
/*
* 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 "tls/s2n_signature_algorithms.h"
#include "crypto/s2n_fips.h"
#include "crypto/s2n_rsa_pss.h"
#include "crypto/s2n_rsa_signing.h"
#include "error/s2n_errno.h"
#include "tls/s2n_auth_selection.h"
#include "tls/s2n_cipher_suites.h"
#include "tls/s2n_kex.h"
#include "tls/s2n_security_policies.h"
#include "tls/s2n_signature_scheme.h"
#include "utils/s2n_safety.h"
static int s2n_signature_scheme_valid_to_offer(struct s2n_connection *conn, const struct s2n_signature_scheme *scheme)
{
POSIX_ENSURE_REF(conn);
/* We don't know what protocol version we will eventually negotiate, but we know that it won't be any higher. */
POSIX_ENSURE_GTE(conn->actual_protocol_version, scheme->minimum_protocol_version);
/* QUIC only supports TLS1.3 */
if (s2n_connection_is_quic_enabled(conn) && scheme->maximum_protocol_version) {
POSIX_ENSURE_GTE(scheme->maximum_protocol_version, S2N_TLS13);
}
if (!s2n_is_rsa_pss_signing_supported()) {
POSIX_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_RSAE);
}
if (!s2n_is_rsa_pss_certs_supported()) {
POSIX_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_PSS);
}
return 0;
}
static int s2n_signature_scheme_valid_to_accept(struct s2n_connection *conn, const struct s2n_signature_scheme *scheme)
{
POSIX_ENSURE_REF(scheme);
POSIX_ENSURE_REF(conn);
POSIX_GUARD(s2n_signature_scheme_valid_to_offer(conn, scheme));
if (scheme->maximum_protocol_version != S2N_UNKNOWN_PROTOCOL_VERSION) {
POSIX_ENSURE_LTE(conn->actual_protocol_version, scheme->maximum_protocol_version);
}
POSIX_ENSURE_NE(conn->actual_protocol_version, S2N_UNKNOWN_PROTOCOL_VERSION);
if (conn->actual_protocol_version >= S2N_TLS13) {
POSIX_ENSURE_NE(scheme->hash_alg, S2N_HASH_SHA1);
POSIX_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA);
} else {
POSIX_ENSURE_NE(scheme->sig_alg, S2N_SIGNATURE_RSA_PSS_PSS);
}
return 0;
}
static int s2n_is_signature_scheme_usable(struct s2n_connection *conn, const struct s2n_signature_scheme *candidate)
{
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(candidate);
POSIX_GUARD(s2n_signature_scheme_valid_to_accept(conn, candidate));
POSIX_GUARD(s2n_is_sig_scheme_valid_for_auth(conn, candidate));
return S2N_SUCCESS;
}
static int s2n_choose_sig_scheme(struct s2n_connection *conn, struct s2n_sig_scheme_list *peer_wire_prefs,
struct s2n_signature_scheme *chosen_scheme_out)
{
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(conn->secure);
const struct s2n_signature_preferences *signature_preferences = NULL;
POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
POSIX_ENSURE_REF(signature_preferences);
struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
POSIX_ENSURE_REF(cipher_suite);
for (size_t i = 0; i < signature_preferences->count; i++) {
const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
if (s2n_is_signature_scheme_usable(conn, candidate) != S2N_SUCCESS) {
continue;
}
for (size_t j = 0; j < peer_wire_prefs->len; j++) {
uint16_t their_iana_val = peer_wire_prefs->iana_list[j];
if (candidate->iana_value == their_iana_val) {
*chosen_scheme_out = *candidate;
return S2N_SUCCESS;
}
}
}
/* do not error even if there's no match */
return S2N_SUCCESS;
}
/* similar to s2n_choose_sig_scheme() without matching client's preference */
int s2n_tls13_default_sig_scheme(struct s2n_connection *conn, struct s2n_signature_scheme *chosen_scheme_out)
{
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(conn->secure);
const struct s2n_signature_preferences *signature_preferences = NULL;
POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
POSIX_ENSURE_REF(signature_preferences);
struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
POSIX_ENSURE_REF(cipher_suite);
for (size_t i = 0; i < signature_preferences->count; i++) {
const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
if (s2n_is_signature_scheme_usable(conn, candidate) != S2N_SUCCESS) {
continue;
}
*chosen_scheme_out = *candidate;
return S2N_SUCCESS;
}
POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_SCHEME);
}
int s2n_get_and_validate_negotiated_signature_scheme(struct s2n_connection *conn, struct s2n_stuffer *in,
struct s2n_signature_scheme *chosen_sig_scheme)
{
uint16_t actual_iana_val;
POSIX_GUARD(s2n_stuffer_read_uint16(in, &actual_iana_val));
const struct s2n_signature_preferences *signature_preferences = NULL;
POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
POSIX_ENSURE_REF(signature_preferences);
for (size_t i = 0; i < signature_preferences->count; i++) {
const struct s2n_signature_scheme *candidate = signature_preferences->signature_schemes[i];
if (0 != s2n_signature_scheme_valid_to_accept(conn, candidate)) {
continue;
}
if (candidate->iana_value == actual_iana_val) {
*chosen_sig_scheme = *candidate;
return S2N_SUCCESS;
}
}
/* We require an exact match in TLS 1.3, but all previous versions can fall back to the default SignatureScheme.
* This means that an s2n client will accept the default SignatureScheme from a TLS server, even if the client did
* not send it in it's ClientHello. This pre-TLS1.3 behavior is an intentional choice to maximize support. */
struct s2n_signature_scheme default_scheme = { 0 };
POSIX_GUARD(s2n_choose_default_sig_scheme(conn, &default_scheme, S2N_PEER_MODE(conn->mode)));
if ((conn->actual_protocol_version <= S2N_TLS12)
&& (s2n_signature_scheme_valid_to_accept(conn, &default_scheme) == S2N_SUCCESS)
&& (actual_iana_val == default_scheme.iana_value)) {
*chosen_sig_scheme = default_scheme;
return S2N_SUCCESS;
}
POSIX_BAIL(S2N_ERR_INVALID_SIGNATURE_SCHEME);
}
int s2n_choose_default_sig_scheme(struct s2n_connection *conn, struct s2n_signature_scheme *sig_scheme_out, s2n_mode signer)
{
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(conn->secure);
POSIX_ENSURE_REF(sig_scheme_out);
s2n_authentication_method auth_method = 0;
if (signer == S2N_CLIENT) {
POSIX_GUARD(s2n_get_auth_method_for_cert_type(conn->handshake_params.client_cert_pkey_type, &auth_method));
} else {
POSIX_ENSURE_REF(conn->secure->cipher_suite);
auth_method = conn->secure->cipher_suite->auth_method;
}
/* Default our signature digest algorithms.
* For >=TLS 1.2 this default may be overridden by the signature_algorithms extension.
*/
if (auth_method == S2N_AUTHENTICATION_ECDSA) {
*sig_scheme_out = s2n_ecdsa_sha1;
} else if (conn->actual_protocol_version >= S2N_TLS12) {
*sig_scheme_out = s2n_rsa_pkcs1_sha1;
} else {
*sig_scheme_out = s2n_rsa_pkcs1_md5_sha1;
}
return S2N_SUCCESS;
}
int s2n_choose_sig_scheme_from_peer_preference_list(struct s2n_connection *conn, struct s2n_sig_scheme_list *peer_wire_prefs,
struct s2n_signature_scheme *sig_scheme_out)
{
POSIX_ENSURE_REF(conn);
POSIX_ENSURE_REF(sig_scheme_out);
struct s2n_signature_scheme chosen_scheme = { 0 };
if (conn->actual_protocol_version < S2N_TLS13) {
POSIX_GUARD(s2n_choose_default_sig_scheme(conn, &chosen_scheme, conn->mode));
} else {
/* Pick a default signature algorithm in TLS 1.3 https://tools.ietf.org/html/rfc8446#section-4.4.2.2 */
POSIX_GUARD(s2n_tls13_default_sig_scheme(conn, &chosen_scheme));
}
/* SignatureScheme preference list was first added in TLS 1.2. It will be empty in older TLS versions. */
if (conn->actual_protocol_version >= S2N_TLS12 && peer_wire_prefs != NULL && peer_wire_prefs->len > 0) {
/* Use a best effort approach to selecting a signature scheme matching client's preferences */
POSIX_GUARD(s2n_choose_sig_scheme(conn, peer_wire_prefs, &chosen_scheme));
}
*sig_scheme_out = chosen_scheme;
return S2N_SUCCESS;
}
int s2n_send_supported_sig_scheme_list(struct s2n_connection *conn, struct s2n_stuffer *out)
{
const struct s2n_signature_preferences *signature_preferences = NULL;
POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
POSIX_ENSURE_REF(signature_preferences);
POSIX_GUARD(s2n_stuffer_write_uint16(out, s2n_supported_sig_scheme_list_size(conn)));
for (size_t i = 0; i < signature_preferences->count; i++) {
const struct s2n_signature_scheme *const scheme = signature_preferences->signature_schemes[i];
if (0 == s2n_signature_scheme_valid_to_offer(conn, scheme)) {
POSIX_GUARD(s2n_stuffer_write_uint16(out, scheme->iana_value));
}
}
return 0;
}
int s2n_supported_sig_scheme_list_size(struct s2n_connection *conn)
{
return s2n_supported_sig_schemes_count(conn) * TLS_SIGNATURE_SCHEME_LEN;
}
int s2n_supported_sig_schemes_count(struct s2n_connection *conn)
{
const struct s2n_signature_preferences *signature_preferences = NULL;
POSIX_GUARD(s2n_connection_get_signature_preferences(conn, &signature_preferences));
POSIX_ENSURE_REF(signature_preferences);
uint8_t count = 0;
for (size_t i = 0; i < signature_preferences->count; i++) {
if (0 == s2n_signature_scheme_valid_to_offer(conn, signature_preferences->signature_schemes[i])) {
count++;
}
}
return count;
}
int s2n_recv_supported_sig_scheme_list(struct s2n_stuffer *in, struct s2n_sig_scheme_list *sig_hash_algs)
{
uint16_t length_of_all_pairs;
POSIX_GUARD(s2n_stuffer_read_uint16(in, &length_of_all_pairs));
if (length_of_all_pairs > s2n_stuffer_data_available(in)) {
/* Malformed length, ignore the extension */
return 0;
}
if (length_of_all_pairs % 2) {
/* Pairs occur in two byte lengths. Malformed length, ignore the extension and skip ahead */
POSIX_GUARD(s2n_stuffer_skip_read(in, length_of_all_pairs));
return 0;
}
int pairs_available = length_of_all_pairs / 2;
if (pairs_available > TLS_SIGNATURE_SCHEME_LIST_MAX_LEN) {
POSIX_BAIL(S2N_ERR_TOO_MANY_SIGNATURE_SCHEMES);
}
sig_hash_algs->len = 0;
for (size_t i = 0; i < (size_t) pairs_available; i++) {
uint16_t sig_scheme = 0;
POSIX_GUARD(s2n_stuffer_read_uint16(in, &sig_scheme));
sig_hash_algs->iana_list[sig_hash_algs->len] = sig_scheme;
sig_hash_algs->len += 1;
}
return 0;
}
|