Debian Codegraph @

crt/s2n/tls/s2n_prf.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 "tls/s2n_prf.h"
17
18 #include <openssl/hmac.h>
19 #include <openssl/md5.h>
20 #include <openssl/sha.h>
21 #include <string.h>
22
23 #include "crypto/s2n_fips.h"
24 #include "crypto/s2n_hash.h"
25 #include "crypto/s2n_hmac.h"
26 #include "crypto/s2n_prf_libcrypto.h"
27 #include "error/s2n_errno.h"
28 #include "stuffer/s2n_stuffer.h"
29 #include "tls/s2n_cipher_suites.h"
30 #include "tls/s2n_connection.h"
31 #include "tls/s2n_crypto_constants.h"
32 #include "tls/s2n_tls.h"
33 #include "utils/s2n_blob.h"
34 #include "utils/s2n_mem.h"
35 #include "utils/s2n_safety.h"
36
37 /* The s2n p_hash implementation is abstracted to allow for separate implementations.
38 * Currently the only implementation uses s2n-tls's custom HMAC implementation.
39 */
40 struct s2n_p_hash_hmac {
41 int (*alloc)(struct s2n_prf_working_space *ws);
42 int (*init)(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret);
43 int (*update)(struct s2n_prf_working_space *ws, const void *data, uint32_t size);
44 int (*final)(struct s2n_prf_working_space *ws, void *digest, uint32_t size);
45 int (*reset)(struct s2n_prf_working_space *ws);
46 int (*cleanup)(struct s2n_prf_working_space *ws);
47 int (*free)(struct s2n_prf_working_space *ws);
48 };
49
50 S2N_RESULT s2n_prf_get_digest_for_ems(struct s2n_connection *conn, struct s2n_blob *message,
51 s2n_hash_algorithm hash_alg, struct s2n_blob *output);
52 S2N_RESULT s2n_prf_tls_extended_master_secret(struct s2n_connection *conn,
53 struct s2n_blob *premaster_secret, struct s2n_blob *session_hash, struct s2n_blob *sha1_hash);
54
55 S2N_RESULT s2n_key_material_init(struct s2n_key_material *key_material, struct s2n_connection *conn)
56 {
57 RESULT_ENSURE_REF(key_material);
58 RESULT_ENSURE_REF(conn);
59 RESULT_ENSURE_REF(conn->secure);
60 RESULT_ENSURE_REF(conn->secure->cipher_suite);
61 RESULT_ENSURE_REF(conn->secure->cipher_suite->record_alg);
62 const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
63 RESULT_ENSURE_REF(cipher);
64
65 uint8_t mac_size = 0;
66 uint32_t key_size = 0;
67 uint32_t iv_size = 0;
68
69 /* MAC size */
70 if (cipher->type == S2N_COMPOSITE) {
71 mac_size = cipher->io.comp.mac_key_size;
72 } else {
73 RESULT_GUARD_POSIX(s2n_hmac_digest_size(conn->secure->cipher_suite->record_alg->hmac_alg, &mac_size));
74 }
75
76 /* KEY size */
77 key_size = cipher->key_material_size;
78
79 /* Only AEAD ciphers have implicit IVs for TLS >= 1.1 */
80 if (conn->actual_protocol_version <= S2N_TLS10 || cipher->type == S2N_AEAD) {
81 /* IV size */
82 switch (cipher->type) {
83 case S2N_AEAD:
84 iv_size = cipher->io.aead.fixed_iv_size;
85 break;
86 case S2N_CBC:
87 iv_size = cipher->io.cbc.block_size;
88 break;
89 case S2N_COMPOSITE:
90 iv_size = cipher->io.comp.block_size;
91 break;
92 /* No-op for stream ciphers */
93 default:
94 break;
95 }
96 }
97
98 struct s2n_stuffer key_material_stuffer = { 0 };
99 struct s2n_blob key_material_blob = { 0 };
100 RESULT_GUARD_POSIX(s2n_blob_init(&key_material_blob, key_material->key_block, sizeof(key_material->key_block)));
101 RESULT_GUARD_POSIX(s2n_stuffer_init_written(&key_material_stuffer, &key_material_blob));
102
103 /* initialize key_material blobs; incrementing ptr to point to the next slice of memory */
104 uint8_t *ptr = NULL;
105 /* MAC */
106 ptr = s2n_stuffer_raw_read(&key_material_stuffer, mac_size);
107 RESULT_ENSURE_REF(ptr);
108 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_mac, ptr, mac_size));
109
110 ptr = s2n_stuffer_raw_read(&key_material_stuffer, mac_size);
111 RESULT_ENSURE_REF(ptr);
112 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_mac, ptr, mac_size));
113
114 /* KEY */
115 ptr = s2n_stuffer_raw_read(&key_material_stuffer, key_size);
116 RESULT_ENSURE_REF(ptr);
117 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_key, ptr, key_size));
118
119 ptr = s2n_stuffer_raw_read(&key_material_stuffer, key_size);
120 RESULT_ENSURE_REF(ptr);
121 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_key, ptr, key_size));
122
123 /* IV */
124 ptr = s2n_stuffer_raw_read(&key_material_stuffer, iv_size);
125 RESULT_ENSURE_REF(ptr);
126 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->client_iv, ptr, iv_size));
127
128 ptr = s2n_stuffer_raw_read(&key_material_stuffer, iv_size);
129 RESULT_ENSURE_REF(ptr);
130 RESULT_GUARD_POSIX(s2n_blob_init(&key_material->server_iv, ptr, iv_size));
131
132 return S2N_RESULT_OK;
133 }
134
135 /* SSLv3 PRF uses MD5 and SHA-1 in a custom hash-based construction (not
136 * HMAC). The use of weak hash algorithms is inherent to the SSLv3 protocol
137 * specification. SSLv3 is disabled by default and not recommended.
138 */
139 static int s2n_prf_sslv3(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *seed_a,
140 struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
141 {
142 POSIX_ENSURE_REF(conn);
143 POSIX_ENSURE_REF(conn->handshake.hashes);
144 struct s2n_hash_state *workspace = &conn->handshake.hashes->hash_workspace;
145
146 uint32_t outputlen = out->size;
147 uint8_t *output = out->data;
148 uint8_t iteration = 1;
149
150 uint8_t md5_digest[MD5_DIGEST_LENGTH] = { 0 }, sha_digest[SHA_DIGEST_LENGTH] = { 0 };
151
152 uint8_t A = 'A';
153 while (outputlen) {
154 struct s2n_hash_state *sha1 = workspace;
155 POSIX_GUARD(s2n_hash_reset(sha1));
156 POSIX_GUARD(s2n_hash_init(sha1, S2N_HASH_SHA1));
157
158 for (int i = 0; i < iteration; i++) {
159 POSIX_GUARD(s2n_hash_update(sha1, &A, 1));
160 }
161
162 POSIX_GUARD(s2n_hash_update(sha1, secret->data, secret->size));
163 POSIX_GUARD(s2n_hash_update(sha1, seed_a->data, seed_a->size));
164
165 if (seed_b) {
166 POSIX_GUARD(s2n_hash_update(sha1, seed_b->data, seed_b->size));
167 if (seed_c) {
168 POSIX_GUARD(s2n_hash_update(sha1, seed_c->data, seed_c->size));
169 }
170 }
171
172 POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, sizeof(sha_digest)));
173
174 struct s2n_hash_state *md5 = workspace;
175 POSIX_GUARD(s2n_hash_reset(md5));
176 POSIX_GUARD(s2n_hash_init(md5, S2N_HASH_MD5));
177 POSIX_GUARD(s2n_hash_update(md5, secret->data, secret->size));
178 POSIX_GUARD(s2n_hash_update(md5, sha_digest, sizeof(sha_digest)));
179 POSIX_GUARD(s2n_hash_digest(md5, md5_digest, sizeof(md5_digest)));
180
181 uint32_t bytes_to_copy = S2N_MIN(outputlen, sizeof(md5_digest));
182
183 POSIX_CHECKED_MEMCPY(output, md5_digest, bytes_to_copy);
184
185 outputlen -= bytes_to_copy;
186 output += bytes_to_copy;
187
188 /* Increment the letter */
189 A++;
190 iteration++;
191 }
192
193 return 0;
194 }
195
196 static int s2n_hmac_p_hash_new(struct s2n_prf_working_space *ws)
197 {
198 POSIX_GUARD(s2n_hmac_new(&ws->p_hash.s2n_hmac));
199 return s2n_hmac_init(&ws->p_hash.s2n_hmac, S2N_HMAC_NONE, NULL, 0);
200 }
201
202 static int s2n_hmac_p_hash_init(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret)
203 {
204 return s2n_hmac_init(&ws->p_hash.s2n_hmac, alg, secret->data, secret->size);
205 }
206
207 static int s2n_hmac_p_hash_update(struct s2n_prf_working_space *ws, const void *data, uint32_t size)
208 {
209 return s2n_hmac_update(&ws->p_hash.s2n_hmac, data, size);
210 }
211
212 static int s2n_hmac_p_hash_digest(struct s2n_prf_working_space *ws, void *digest, uint32_t size)
213 {
214 return s2n_hmac_digest(&ws->p_hash.s2n_hmac, digest, size);
215 }
216
217 static int s2n_hmac_p_hash_reset(struct s2n_prf_working_space *ws)
218 {
219 /* If we actually initialized s2n_hmac, wipe it.
220 * A valid, initialized s2n_hmac_state will have a valid block size.
221 */
222 if (ws->p_hash.s2n_hmac.hash_block_size != 0) {
223 return s2n_hmac_reset(&ws->p_hash.s2n_hmac);
224 }
225 return S2N_SUCCESS;
226 }
227
228 static int s2n_hmac_p_hash_cleanup(struct s2n_prf_working_space *ws)
229 {
230 return s2n_hmac_p_hash_reset(ws);
231 }
232
233 static int s2n_hmac_p_hash_free(struct s2n_prf_working_space *ws)
234 {
235 return s2n_hmac_free(&ws->p_hash.s2n_hmac);
236 }
237
238 static const struct s2n_p_hash_hmac s2n_internal_p_hash_hmac = {
239 .alloc = &s2n_hmac_p_hash_new,
240 .init = &s2n_hmac_p_hash_init,
241 .update = &s2n_hmac_p_hash_update,
242 .final = &s2n_hmac_p_hash_digest,
243 .reset = &s2n_hmac_p_hash_reset,
244 .cleanup = &s2n_hmac_p_hash_cleanup,
245 .free = &s2n_hmac_p_hash_free,
246 };
247
248 /*
249 * For now, use the internal s2n-tls hmac abstraction.
250 * However, that is a custom implementation of hmac built on hashes.
251 * Ideally we should stop using our custom implementation here and switch
252 * to using a libcrypto implementation. Unfortunately, what each libcrypto
253 * can support varies a lot for HMACs.
254 *
255 * For historical reference, there used to be two other hmac implementations:
256 * https://github.com/aws/s2n-tls/blob/711ee0df658cd7c44088cf7a1b20a9f3cf5296d6/tls/s2n_prf.c#L174-L337
257 * Both implementations have compatibility issues with one or more libcryptos.
258 */
259 const struct s2n_p_hash_hmac *s2n_get_hmac_implementation()
260 {
261 return &s2n_internal_p_hash_hmac;
262 }
263
264 static int s2n_p_hash(struct s2n_prf_working_space *ws, s2n_hmac_algorithm alg, struct s2n_blob *secret, struct s2n_blob *label,
265 struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
266 {
267 uint8_t digest_size = 0;
268 POSIX_GUARD(s2n_hmac_digest_size(alg, &digest_size));
269
270 const struct s2n_p_hash_hmac *hmac = s2n_get_hmac_implementation();
271 POSIX_ENSURE_REF(hmac);
272
273 /* First compute hmac(secret + A(0)) */
274 POSIX_GUARD(hmac->init(ws, alg, secret));
275 POSIX_GUARD(hmac->update(ws, label->data, label->size));
276 POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size));
277
278 if (seed_b) {
279 POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size));
280 if (seed_c) {
281 POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size));
282 }
283 }
284 POSIX_GUARD(hmac->final(ws, ws->digest0, digest_size));
285
286 uint32_t outputlen = out->size;
287 uint8_t *output = out->data;
288
289 while (outputlen) {
290 /* Now compute hmac(secret + A(N - 1) + seed) */
291 POSIX_GUARD(hmac->reset(ws));
292 POSIX_GUARD(hmac->update(ws, ws->digest0, digest_size));
293
294 /* Add the label + seed and compute this round's A */
295 POSIX_GUARD(hmac->update(ws, label->data, label->size));
296 POSIX_GUARD(hmac->update(ws, seed_a->data, seed_a->size));
297 if (seed_b) {
298 POSIX_GUARD(hmac->update(ws, seed_b->data, seed_b->size));
299 if (seed_c) {
300 POSIX_GUARD(hmac->update(ws, seed_c->data, seed_c->size));
301 }
302 }
303
304 POSIX_GUARD(hmac->final(ws, ws->digest1, digest_size));
305
306 uint32_t bytes_to_xor = S2N_MIN(outputlen, digest_size);
307
308 for (size_t i = 0; i < bytes_to_xor; i++) {
309 *output ^= ws->digest1[i];
310 output++;
311 outputlen--;
312 }
313
314 /* Stash a digest of A(N), in A(N), for the next round */
315 POSIX_GUARD(hmac->reset(ws));
316 POSIX_GUARD(hmac->update(ws, ws->digest0, digest_size));
317 POSIX_GUARD(hmac->final(ws, ws->digest0, digest_size));
318 }
319
320 POSIX_GUARD(hmac->cleanup(ws));
321
322 return 0;
323 }
324
325 S2N_RESULT s2n_prf_new(struct s2n_connection *conn)
326 {
327 RESULT_ENSURE_REF(conn);
328 RESULT_ENSURE_EQ(conn->prf_space, NULL);
329
330 DEFER_CLEANUP(struct s2n_blob mem = { 0 }, s2n_free);
331 RESULT_GUARD_POSIX(s2n_realloc(&mem, sizeof(struct s2n_prf_working_space)));
332 RESULT_GUARD_POSIX(s2n_blob_zero(&mem));
333 conn->prf_space = (struct s2n_prf_working_space *) (void *) mem.data;
334 ZERO_TO_DISABLE_DEFER_CLEANUP(mem);
335
336 /* Allocate the hmac state */
337 const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
338 RESULT_ENSURE_REF(hmac_impl);
339 RESULT_GUARD_POSIX(hmac_impl->alloc(conn->prf_space));
340 return S2N_RESULT_OK;
341 }
342
343 S2N_RESULT s2n_prf_wipe(struct s2n_connection *conn)
344 {
345 RESULT_ENSURE_REF(conn);
346 RESULT_ENSURE_REF(conn->prf_space);
347
348 const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
349 RESULT_ENSURE_REF(hmac_impl);
350 RESULT_GUARD_POSIX(hmac_impl->reset(conn->prf_space));
351
352 return S2N_RESULT_OK;
353 }
354
355 S2N_RESULT s2n_prf_free(struct s2n_connection *conn)
356 {
357 RESULT_ENSURE_REF(conn);
358 if (conn->prf_space == NULL) {
359 return S2N_RESULT_OK;
360 }
361
362 const struct s2n_p_hash_hmac *hmac_impl = s2n_get_hmac_implementation();
363 RESULT_ENSURE_REF(hmac_impl);
364 RESULT_GUARD_POSIX(hmac_impl->free(conn->prf_space));
365
366 RESULT_GUARD_POSIX(s2n_free_object((uint8_t **) &conn->prf_space, sizeof(struct s2n_prf_working_space)));
367 return S2N_RESULT_OK;
368 }
369
370 S2N_RESULT s2n_prf_custom(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label,
371 struct s2n_blob *seed_a, struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
372 {
373 /* We zero the out blob because p_hash works by XOR'ing with the existing
374 * buffer. This is a little convoluted but means we can avoid dynamic memory
375 * allocation. When we call p_hash once (in the TLS1.2 case) it will produce
376 * the right values. When we call it twice in the regular case, the two
377 * outputs will be XORd just ass the TLS 1.0 and 1.1 RFCs require.
378 */
379 RESULT_GUARD_POSIX(s2n_blob_zero(out));
380
381 if (conn->actual_protocol_version == S2N_TLS12) {
382 RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, conn->secure->cipher_suite->prf_alg, secret, label, seed_a,
383 seed_b, seed_c, out));
384 return S2N_RESULT_OK;
385 }
386
387 struct s2n_blob half_secret = { 0 };
388 RESULT_GUARD_POSIX(s2n_blob_init(&half_secret, secret->data, (secret->size + 1) / 2));
389
390 RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, S2N_HMAC_MD5, &half_secret, label, seed_a, seed_b, seed_c, out));
391 half_secret.data += secret->size - half_secret.size;
392 RESULT_GUARD_POSIX(s2n_p_hash(conn->prf_space, S2N_HMAC_SHA1, &half_secret, label, seed_a, seed_b, seed_c, out));
393
394 return S2N_RESULT_OK;
395 }
396
397 int s2n_prf(struct s2n_connection *conn, struct s2n_blob *secret, struct s2n_blob *label, struct s2n_blob *seed_a,
398 struct s2n_blob *seed_b, struct s2n_blob *seed_c, struct s2n_blob *out)
399 {
400 POSIX_ENSURE_REF(conn);
401 POSIX_ENSURE_REF(conn->secure);
402 POSIX_ENSURE_REF(conn->secure->cipher_suite);
403 POSIX_ENSURE_REF(conn->prf_space);
404 POSIX_ENSURE_REF(secret);
405 POSIX_ENSURE_REF(label);
406 POSIX_ENSURE_REF(out);
407
408 /* seed_a is always required, seed_b is optional, if seed_c is provided seed_b must also be provided */
409 POSIX_ENSURE(seed_a != NULL, S2N_ERR_PRF_INVALID_SEED);
410 POSIX_ENSURE(S2N_IMPLIES(seed_c != NULL, seed_b != NULL), S2N_ERR_PRF_INVALID_SEED);
411
412 if (conn->actual_protocol_version == S2N_SSLv3) {
413 POSIX_GUARD(s2n_prf_sslv3(conn, secret, seed_a, seed_b, seed_c, out));
414 return S2N_SUCCESS;
415 }
416
417 /* By default, s2n-tls uses a custom PRF implementation. When operating in FIPS mode, the
418 * FIPS-validated libcrypto implementation is used instead, if an implementation is provided.
419 */
420 if (s2n_is_in_fips_mode()) {
421 POSIX_GUARD_RESULT(s2n_prf_libcrypto(conn, secret, label, seed_a, seed_b, seed_c, out));
422 return S2N_SUCCESS;
423 }
424
425 POSIX_GUARD_RESULT(s2n_prf_custom(conn, secret, label, seed_a, seed_b, seed_c, out));
426
427 return S2N_SUCCESS;
428 }
429
430 int s2n_prf_tls_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
431 {
432 POSIX_ENSURE_REF(conn);
433
434 struct s2n_blob client_random = { 0 };
435 POSIX_GUARD(s2n_blob_init(&client_random, conn->client_hello.random, sizeof(conn->client_hello.random)));
436 struct s2n_blob server_random = { 0 };
437 POSIX_GUARD(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
438 struct s2n_blob master_secret = { 0 };
439 POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
440
441 uint8_t master_secret_label[] = "master secret";
442 struct s2n_blob label = { 0 };
443 POSIX_GUARD(s2n_blob_init(&label, master_secret_label, sizeof(master_secret_label) - 1));
444
445 return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, NULL, &master_secret);
446 }
447
448 int s2n_prf_hybrid_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
449 {
450 POSIX_ENSURE_REF(conn);
451
452 struct s2n_blob client_random = { 0 };
453 POSIX_GUARD(s2n_blob_init(&client_random, conn->client_hello.random, sizeof(conn->client_hello.random)));
454 struct s2n_blob server_random = { 0 };
455 POSIX_GUARD(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
456 struct s2n_blob master_secret = { 0 };
457 POSIX_GUARD(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
458
459 uint8_t master_secret_label[] = "hybrid master secret";
460 struct s2n_blob label = { 0 };
461 POSIX_GUARD(s2n_blob_init(&label, master_secret_label, sizeof(master_secret_label) - 1));
462
463 return s2n_prf(conn, premaster_secret, &label, &client_random, &server_random, &conn->kex_params.client_key_exchange_message, &master_secret);
464 }
465
466 int s2n_prf_calculate_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret)
467 {
468 POSIX_ENSURE_REF(conn);
469 POSIX_ENSURE_REF(conn->secure);
470
471 POSIX_ENSURE_EQ(s2n_conn_get_current_message_type(conn), CLIENT_KEY);
472
473 if (!conn->ems_negotiated) {
474 POSIX_GUARD(s2n_prf_tls_master_secret(conn, premaster_secret));
475 return S2N_SUCCESS;
476 }
477
478 /* Only the client writes the Client Key Exchange message */
479 if (conn->mode == S2N_CLIENT) {
480 POSIX_GUARD(s2n_handshake_finish_header(&conn->handshake.io));
481 }
482 struct s2n_stuffer client_key_message = conn->handshake.io;
483 POSIX_GUARD(s2n_stuffer_reread(&client_key_message));
484 uint32_t client_key_message_size = s2n_stuffer_data_available(&client_key_message);
485 struct s2n_blob client_key_blob = { 0 };
486 POSIX_GUARD(s2n_blob_init(&client_key_blob, client_key_message.blob.data, client_key_message_size));
487
488 uint8_t data[S2N_MAX_DIGEST_LEN] = { 0 };
489 struct s2n_blob digest = { 0 };
490 POSIX_GUARD(s2n_blob_init(&digest, data, sizeof(data)));
491 if (conn->actual_protocol_version < S2N_TLS12) {
492 uint8_t sha1_data[S2N_MAX_DIGEST_LEN] = { 0 };
493 struct s2n_blob sha1_digest = { 0 };
494 POSIX_GUARD(s2n_blob_init(&sha1_digest, sha1_data, sizeof(sha1_data)));
495 POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, S2N_HASH_MD5, &digest));
496 POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, S2N_HASH_SHA1, &sha1_digest));
497 POSIX_GUARD_RESULT(s2n_prf_tls_extended_master_secret(conn, premaster_secret, &digest, &sha1_digest));
498 } else {
499 s2n_hmac_algorithm prf_alg = conn->secure->cipher_suite->prf_alg;
500 s2n_hash_algorithm hash_alg = 0;
501 POSIX_GUARD(s2n_hmac_hash_alg(prf_alg, &hash_alg));
502 POSIX_GUARD_RESULT(s2n_prf_get_digest_for_ems(conn, &client_key_blob, hash_alg, &digest));
503 POSIX_GUARD_RESULT(s2n_prf_tls_extended_master_secret(conn, premaster_secret, &digest, NULL));
504 }
505 return S2N_SUCCESS;
506 }
507
508 /**
509 *= https://www.rfc-editor.org/rfc/rfc7627#section-4
510 *# When the extended master secret extension is negotiated in a full
511 *# handshake, the "master_secret" is computed as
512 *#
513 *# master_secret = PRF(pre_master_secret, "extended master secret",
514 *# session_hash)
515 *# [0..47];
516 */
517 S2N_RESULT s2n_prf_tls_extended_master_secret(struct s2n_connection *conn, struct s2n_blob *premaster_secret, struct s2n_blob *session_hash, struct s2n_blob *sha1_hash)
518 {
519 RESULT_ENSURE_REF(conn);
520
521 struct s2n_blob extended_master_secret = { 0 };
522 RESULT_GUARD_POSIX(s2n_blob_init(&extended_master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
523
524 uint8_t extended_master_secret_label[] = "extended master secret";
525 /* Subtract one from the label size to remove the "\0" */
526 struct s2n_blob label = { 0 };
527 RESULT_GUARD_POSIX(s2n_blob_init(&label, extended_master_secret_label, sizeof(extended_master_secret_label) - 1));
528
529 RESULT_GUARD_POSIX(s2n_prf(conn, premaster_secret, &label, session_hash, sha1_hash, NULL, &extended_master_secret));
530
531 return S2N_RESULT_OK;
532 }
533
534 S2N_RESULT s2n_prf_get_digest_for_ems(struct s2n_connection *conn, struct s2n_blob *message, s2n_hash_algorithm hash_alg, struct s2n_blob *output)
535 {
536 RESULT_ENSURE_REF(conn);
537 RESULT_ENSURE_REF(conn->handshake.hashes);
538 RESULT_ENSURE_REF(message);
539 RESULT_ENSURE_REF(output);
540
541 struct s2n_hash_state *hash_state = &conn->handshake.hashes->hash_workspace;
542 RESULT_GUARD(s2n_handshake_copy_hash_state(conn, hash_alg, hash_state));
543 RESULT_GUARD_POSIX(s2n_hash_update(hash_state, message->data, message->size));
544
545 uint8_t digest_size = 0;
546 RESULT_GUARD_POSIX(s2n_hash_digest_size(hash_alg, &digest_size));
547 RESULT_ENSURE_GTE(output->size, digest_size);
548 RESULT_GUARD_POSIX(s2n_hash_digest(hash_state, output->data, digest_size));
549 output->size = digest_size;
550
551 return S2N_RESULT_OK;
552 }
553
554 static int s2n_prf_sslv3_finished(struct s2n_connection *conn, uint8_t prefix[4], struct s2n_hash_state *hash_workspace, uint8_t *out)
555 {
556 POSIX_ENSURE_REF(conn);
557 POSIX_ENSURE_REF(conn->handshake.hashes);
558
559 uint8_t xorpad1[48] = { 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
560 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36 };
561 uint8_t xorpad2[48] = { 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
562 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c };
563 uint8_t *md5_digest = out;
564 uint8_t *sha_digest = out + MD5_DIGEST_LENGTH;
565
566 POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, MD5_DIGEST_LENGTH + SHA_DIGEST_LENGTH));
567
568 struct s2n_hash_state *md5 = hash_workspace;
569 POSIX_GUARD(s2n_hash_copy(md5, &conn->handshake.hashes->md5));
570 POSIX_GUARD(s2n_hash_update(md5, prefix, 4));
571 POSIX_GUARD(s2n_hash_update(md5, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
572 POSIX_GUARD(s2n_hash_update(md5, xorpad1, 48));
573 POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH));
574 POSIX_GUARD(s2n_hash_reset(md5));
575 POSIX_GUARD(s2n_hash_update(md5, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
576 POSIX_GUARD(s2n_hash_update(md5, xorpad2, 48));
577 POSIX_GUARD(s2n_hash_update(md5, md5_digest, MD5_DIGEST_LENGTH));
578 POSIX_GUARD(s2n_hash_digest(md5, md5_digest, MD5_DIGEST_LENGTH));
579 POSIX_GUARD(s2n_hash_reset(md5));
580
581 struct s2n_hash_state *sha1 = hash_workspace;
582 POSIX_GUARD(s2n_hash_copy(sha1, &conn->handshake.hashes->sha1));
583 POSIX_GUARD(s2n_hash_update(sha1, prefix, 4));
584 POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
585 POSIX_GUARD(s2n_hash_update(sha1, xorpad1, 40));
586 POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH));
587 POSIX_GUARD(s2n_hash_reset(sha1));
588 POSIX_GUARD(s2n_hash_update(sha1, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
589 POSIX_GUARD(s2n_hash_update(sha1, xorpad2, 40));
590 POSIX_GUARD(s2n_hash_update(sha1, sha_digest, SHA_DIGEST_LENGTH));
591 POSIX_GUARD(s2n_hash_digest(sha1, sha_digest, SHA_DIGEST_LENGTH));
592 POSIX_GUARD(s2n_hash_reset(sha1));
593
594 return 0;
595 }
596
597 static int s2n_prf_sslv3_client_finished(struct s2n_connection *conn)
598 {
599 POSIX_ENSURE_REF(conn);
600 POSIX_ENSURE_REF(conn->handshake.hashes);
601
602 uint8_t prefix[4] = { 0x43, 0x4c, 0x4e, 0x54 };
603
604 return s2n_prf_sslv3_finished(conn, prefix, &conn->handshake.hashes->hash_workspace, conn->handshake.client_finished);
605 }
606
607 static int s2n_prf_sslv3_server_finished(struct s2n_connection *conn)
608 {
609 POSIX_ENSURE_REF(conn);
610 POSIX_ENSURE_REF(conn->handshake.hashes);
611
612 uint8_t prefix[4] = { 0x53, 0x52, 0x56, 0x52 };
613
614 return s2n_prf_sslv3_finished(conn, prefix, &conn->handshake.hashes->hash_workspace, conn->handshake.server_finished);
615 }
616
617 int s2n_prf_client_finished(struct s2n_connection *conn)
618 {
619 POSIX_ENSURE_REF(conn);
620 POSIX_ENSURE_REF(conn->secure);
621 POSIX_ENSURE_REF(conn->handshake.hashes);
622
623 struct s2n_blob master_secret, md5, sha;
624 uint8_t md5_digest[MD5_DIGEST_LENGTH];
625 uint8_t sha_digest[SHA384_DIGEST_LENGTH];
626 uint8_t client_finished_label[] = "client finished";
627 struct s2n_blob client_finished = { 0 };
628 struct s2n_blob label = { 0 };
629
630 if (conn->actual_protocol_version == S2N_SSLv3) {
631 return s2n_prf_sslv3_client_finished(conn);
632 }
633
634 client_finished.data = conn->handshake.client_finished;
635 client_finished.size = S2N_TLS_FINISHED_LEN;
636 POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, client_finished.size));
637 label.data = client_finished_label;
638 label.size = sizeof(client_finished_label) - 1;
639
640 master_secret.data = conn->secrets.version.tls12.master_secret;
641 master_secret.size = sizeof(conn->secrets.version.tls12.master_secret);
642 if (conn->actual_protocol_version == S2N_TLS12) {
643 switch (conn->secure->cipher_suite->prf_alg) {
644 case S2N_HMAC_SHA256:
645 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha256));
646 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH));
647 sha.size = SHA256_DIGEST_LENGTH;
648 break;
649 case S2N_HMAC_SHA384:
650 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha384));
651 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH));
652 sha.size = SHA384_DIGEST_LENGTH;
653 break;
654 default:
655 POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM);
656 }
657
658 sha.data = sha_digest;
659 return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &client_finished);
660 }
661
662 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->md5));
663 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, md5_digest, MD5_DIGEST_LENGTH));
664 md5.data = md5_digest;
665 md5.size = MD5_DIGEST_LENGTH;
666
667 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha1));
668 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA_DIGEST_LENGTH));
669 sha.data = sha_digest;
670 sha.size = SHA_DIGEST_LENGTH;
671
672 return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &client_finished);
673 }
674
675 int s2n_prf_server_finished(struct s2n_connection *conn)
676 {
677 POSIX_ENSURE_REF(conn);
678 POSIX_ENSURE_REF(conn->secure);
679 POSIX_ENSURE_REF(conn->handshake.hashes);
680
681 struct s2n_blob master_secret, md5, sha;
682 uint8_t md5_digest[MD5_DIGEST_LENGTH];
683 uint8_t sha_digest[SHA384_DIGEST_LENGTH];
684 uint8_t server_finished_label[] = "server finished";
685 struct s2n_blob server_finished = { 0 };
686 struct s2n_blob label = { 0 };
687
688 if (conn->actual_protocol_version == S2N_SSLv3) {
689 return s2n_prf_sslv3_server_finished(conn);
690 }
691
692 server_finished.data = conn->handshake.server_finished;
693 server_finished.size = S2N_TLS_FINISHED_LEN;
694 POSIX_GUARD_RESULT(s2n_handshake_set_finished_len(conn, server_finished.size));
695 label.data = server_finished_label;
696 label.size = sizeof(server_finished_label) - 1;
697
698 master_secret.data = conn->secrets.version.tls12.master_secret;
699 master_secret.size = sizeof(conn->secrets.version.tls12.master_secret);
700 if (conn->actual_protocol_version == S2N_TLS12) {
701 switch (conn->secure->cipher_suite->prf_alg) {
702 case S2N_HMAC_SHA256:
703 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha256));
704 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA256_DIGEST_LENGTH));
705 sha.size = SHA256_DIGEST_LENGTH;
706 break;
707 case S2N_HMAC_SHA384:
708 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha384));
709 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA384_DIGEST_LENGTH));
710 sha.size = SHA384_DIGEST_LENGTH;
711 break;
712 default:
713 POSIX_BAIL(S2N_ERR_PRF_INVALID_ALGORITHM);
714 }
715
716 sha.data = sha_digest;
717 return s2n_prf(conn, &master_secret, &label, &sha, NULL, NULL, &server_finished);
718 }
719
720 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->md5));
721 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, md5_digest, MD5_DIGEST_LENGTH));
722 md5.data = md5_digest;
723 md5.size = MD5_DIGEST_LENGTH;
724
725 POSIX_GUARD(s2n_hash_copy(&conn->handshake.hashes->hash_workspace, &conn->handshake.hashes->sha1));
726 POSIX_GUARD(s2n_hash_digest(&conn->handshake.hashes->hash_workspace, sha_digest, SHA_DIGEST_LENGTH));
727 sha.data = sha_digest;
728 sha.size = SHA_DIGEST_LENGTH;
729
730 return s2n_prf(conn, &master_secret, &label, &md5, &sha, NULL, &server_finished);
731 }
732
733 static int s2n_prf_make_client_key(struct s2n_connection *conn, struct s2n_key_material *key_material)
734 {
735 POSIX_ENSURE_REF(conn);
736 POSIX_ENSURE_REF(conn->secure);
737 POSIX_ENSURE_REF(conn->secure->cipher_suite);
738 POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
739 const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
740 POSIX_ENSURE_REF(cipher);
741 POSIX_ENSURE_REF(cipher->set_encryption_key);
742 POSIX_ENSURE_REF(cipher->set_decryption_key);
743
744 if (conn->mode == S2N_CLIENT) {
745 POSIX_GUARD_RESULT(cipher->set_encryption_key(&conn->secure->client_key, &key_material->client_key));
746 } else {
747 POSIX_GUARD_RESULT(cipher->set_decryption_key(&conn->secure->client_key, &key_material->client_key));
748 }
749
750 return 0;
751 }
752
753 static int s2n_prf_make_server_key(struct s2n_connection *conn, struct s2n_key_material *key_material)
754 {
755 POSIX_ENSURE_REF(conn);
756 POSIX_ENSURE_REF(conn->secure);
757 POSIX_ENSURE_REF(conn->secure->cipher_suite);
758 POSIX_ENSURE_REF(conn->secure->cipher_suite->record_alg);
759 const struct s2n_cipher *cipher = conn->secure->cipher_suite->record_alg->cipher;
760 POSIX_ENSURE_REF(cipher);
761 POSIX_ENSURE_REF(cipher->set_encryption_key);
762 POSIX_ENSURE_REF(cipher->set_decryption_key);
763
764 if (conn->mode == S2N_SERVER) {
765 POSIX_GUARD_RESULT(cipher->set_encryption_key(&conn->secure->server_key, &key_material->server_key));
766 } else {
767 POSIX_GUARD_RESULT(cipher->set_decryption_key(&conn->secure->server_key, &key_material->server_key));
768 }
769
770 return 0;
771 }
772
773 S2N_RESULT s2n_prf_generate_key_material(struct s2n_connection *conn, struct s2n_key_material *key_material)
774 {
775 RESULT_ENSURE_REF(conn);
776 RESULT_ENSURE_REF(key_material);
777
778 struct s2n_blob client_random = { 0 };
779 RESULT_GUARD_POSIX(s2n_blob_init(&client_random, conn->client_hello.random, sizeof(conn->client_hello.random)));
780 struct s2n_blob server_random = { 0 };
781 RESULT_GUARD_POSIX(s2n_blob_init(&server_random, conn->handshake_params.server_random, sizeof(conn->handshake_params.server_random)));
782 struct s2n_blob master_secret = { 0 };
783 RESULT_GUARD_POSIX(s2n_blob_init(&master_secret, conn->secrets.version.tls12.master_secret, sizeof(conn->secrets.version.tls12.master_secret)));
784
785 struct s2n_blob label = { 0 };
786 uint8_t key_expansion_label[] = "key expansion";
787 RESULT_GUARD_POSIX(s2n_blob_init(&label, key_expansion_label, sizeof(key_expansion_label) - 1));
788
789 RESULT_GUARD(s2n_key_material_init(key_material, conn));
790 struct s2n_blob prf_out = { 0 };
791 RESULT_GUARD_POSIX(s2n_blob_init(&prf_out, key_material->key_block, sizeof(key_material->key_block)));
792 RESULT_GUARD_POSIX(s2n_prf(conn, &master_secret, &label, &server_random, &client_random, NULL, &prf_out));
793
794 return S2N_RESULT_OK;
795 }
796
797 int s2n_prf_key_expansion(struct s2n_connection *conn)
798 {
799 POSIX_ENSURE_REF(conn);
800 POSIX_ENSURE_REF(conn->secure);
801 struct s2n_cipher_suite *cipher_suite = conn->secure->cipher_suite;
802 POSIX_ENSURE_REF(cipher_suite);
803 POSIX_ENSURE_REF(cipher_suite->record_alg);
804 const struct s2n_cipher *cipher = cipher_suite->record_alg->cipher;
805 POSIX_ENSURE_REF(cipher);
806
807 struct s2n_key_material key_material = { 0 };
808 POSIX_GUARD_RESULT(s2n_prf_generate_key_material(conn, &key_material));
809
810 POSIX_ENSURE(cipher_suite->available, S2N_ERR_PRF_INVALID_ALGORITHM);
811 POSIX_GUARD_RESULT(cipher->init(&conn->secure->client_key));
812 POSIX_GUARD_RESULT(cipher->init(&conn->secure->server_key));
813
814 /* Seed the client MAC */
815 POSIX_GUARD(s2n_hmac_reset(&conn->secure->client_record_mac));
816 POSIX_GUARD(s2n_hmac_init(
817 &conn->secure->client_record_mac,
818 cipher_suite->record_alg->hmac_alg,
819 key_material.client_mac.data,
820 key_material.client_mac.size));
821
822 /* Seed the server MAC */
823 POSIX_GUARD(s2n_hmac_reset(&conn->secure->server_record_mac));
824 POSIX_GUARD(s2n_hmac_init(
825 &conn->secure->server_record_mac,
826 conn->secure->cipher_suite->record_alg->hmac_alg,
827 key_material.server_mac.data,
828 key_material.server_mac.size));
829
830 /* Make the client key */
831 POSIX_GUARD(s2n_prf_make_client_key(conn, &key_material));
832
833 /* Make the server key */
834 POSIX_GUARD(s2n_prf_make_server_key(conn, &key_material));
835
836 /* Composite CBC does MAC inside the cipher, pass it the MAC key.
837 * Must happen after setting encryption/decryption keys.
838 */
839 if (cipher->type == S2N_COMPOSITE) {
840 POSIX_GUARD(cipher->io.comp.set_mac_write_key(&conn->secure->client_key, key_material.client_mac.data, key_material.client_mac.size));
841 POSIX_GUARD(cipher->io.comp.set_mac_write_key(&conn->secure->server_key, key_material.server_mac.data, key_material.server_mac.size));
842 }
843
844 /* set IV */
845 POSIX_ENSURE_EQ(key_material.client_iv.size, key_material.server_iv.size);
846 POSIX_ENSURE_LTE(key_material.client_iv.size, S2N_TLS_MAX_IV_LEN);
847 POSIX_CHECKED_MEMCPY(conn->secure->client_implicit_iv, key_material.client_iv.data, key_material.client_iv.size);
848 POSIX_CHECKED_MEMCPY(conn->secure->server_implicit_iv, key_material.server_iv.data, key_material.server_iv.size);
849
850 return 0;
851 }