blob: 39e80be5dc5dcbf5e13e510695813a3da13bf053 [file] [log] [blame]
David Benjaminc895d6b2016-08-11 13:26:41 -04001/* Copyright (c) 2016, Google Inc.
2 *
3 * Permission to use, copy, modify, and/or distribute this software for any
4 * purpose with or without fee is hereby granted, provided that the above
5 * copyright notice and this permission notice appear in all copies.
6 *
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14
15#include <openssl/ssl.h>
16
17#include <assert.h>
18#include <string.h>
19
Robert Sloanb6d070c2017-07-24 08:40:01 -070020#include <utility>
21
David Benjaminc895d6b2016-08-11 13:26:41 -040022#include <openssl/aead.h>
23#include <openssl/bytestring.h>
24#include <openssl/digest.h>
David Benjaminc895d6b2016-08-11 13:26:41 -040025#include <openssl/hkdf.h>
Steven Valdez909b19f2016-11-21 15:35:44 -050026#include <openssl/hmac.h>
David Benjaminc895d6b2016-08-11 13:26:41 -040027#include <openssl/mem.h>
28
Robert Sloan69939df2017-01-09 10:53:07 -080029#include "../crypto/internal.h"
David Benjaminc895d6b2016-08-11 13:26:41 -040030#include "internal.h"
31
32
Robert Sloanb6d070c2017-07-24 08:40:01 -070033namespace bssl {
34
Robert Sloan6d0d00e2017-03-27 07:13:07 -070035static int init_key_schedule(SSL_HANDSHAKE *hs, uint16_t version,
Robert Sloan84377092017-08-14 09:33:19 -070036 const SSL_CIPHER *cipher) {
37 if (!hs->transcript.InitHash(version, cipher)) {
Robert Sloan5d625782017-02-13 09:55:39 -080038 return 0;
39 }
David Benjaminc895d6b2016-08-11 13:26:41 -040040
Robert Sloanb6d070c2017-07-24 08:40:01 -070041 hs->hash_len = hs->transcript.DigestLen();
David Benjaminc895d6b2016-08-11 13:26:41 -040042
David Benjaminc895d6b2016-08-11 13:26:41 -040043 /* Initialize the secret to the zero key. */
Robert Sloan69939df2017-01-09 10:53:07 -080044 OPENSSL_memset(hs->secret, 0, hs->hash_len);
David Benjaminc895d6b2016-08-11 13:26:41 -040045
Robert Sloan6d0d00e2017-03-27 07:13:07 -070046 return 1;
47}
48
49int tls13_init_key_schedule(SSL_HANDSHAKE *hs) {
Robert Sloan84377092017-08-14 09:33:19 -070050 if (!init_key_schedule(hs, ssl3_protocol_version(hs->ssl), hs->new_cipher)) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -070051 return 0;
52 }
53
Robert Sloanb6d070c2017-07-24 08:40:01 -070054 hs->transcript.FreeBuffer();
David Benjaminc895d6b2016-08-11 13:26:41 -040055 return 1;
56}
57
Robert Sloan6d0d00e2017-03-27 07:13:07 -070058int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs) {
59 SSL *const ssl = hs->ssl;
Robert Sloanf6200e72017-07-10 08:09:18 -070060 return init_key_schedule(hs, SSL_SESSION_protocol_version(ssl->session),
Robert Sloan84377092017-08-14 09:33:19 -070061 ssl->session->cipher);
Robert Sloan6d0d00e2017-03-27 07:13:07 -070062}
63
David Benjamin1b249672016-12-06 18:25:50 -050064int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
65 size_t len) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070066 return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), in,
67 len, hs->secret, hs->hash_len);
David Benjaminc895d6b2016-08-11 13:26:41 -040068}
69
70static int hkdf_expand_label(uint8_t *out, const EVP_MD *digest,
71 const uint8_t *secret, size_t secret_len,
72 const uint8_t *label, size_t label_len,
73 const uint8_t *hash, size_t hash_len, size_t len) {
74 static const char kTLS13LabelVersion[] = "TLS 1.3, ";
75
Robert Sloanb6d070c2017-07-24 08:40:01 -070076 ScopedCBB cbb;
77 CBB child;
David Benjaminc895d6b2016-08-11 13:26:41 -040078 uint8_t *hkdf_label;
79 size_t hkdf_label_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -070080 if (!CBB_init(cbb.get(), 2 + 1 + strlen(kTLS13LabelVersion) + label_len + 1 +
81 hash_len) ||
82 !CBB_add_u16(cbb.get(), len) ||
83 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040084 !CBB_add_bytes(&child, (const uint8_t *)kTLS13LabelVersion,
85 strlen(kTLS13LabelVersion)) ||
86 !CBB_add_bytes(&child, label, label_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -070087 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040088 !CBB_add_bytes(&child, hash, hash_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -070089 !CBB_finish(cbb.get(), &hkdf_label, &hkdf_label_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -040090 return 0;
91 }
92
93 int ret = HKDF_expand(out, len, digest, secret, secret_len, hkdf_label,
94 hkdf_label_len);
95 OPENSSL_free(hkdf_label);
96 return ret;
97}
98
David Benjaminc895d6b2016-08-11 13:26:41 -040099/* derive_secret derives a secret of length |len| and writes the result in |out|
100 * with the given label and the current base secret and most recently-saved
101 * handshake context. It returns one on success and zero on error. */
David Benjamin1b249672016-12-06 18:25:50 -0500102static int derive_secret(SSL_HANDSHAKE *hs, uint8_t *out, size_t len,
David Benjaminc895d6b2016-08-11 13:26:41 -0400103 const uint8_t *label, size_t label_len) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500104 uint8_t context_hash[EVP_MAX_MD_SIZE];
105 size_t context_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700106 if (!hs->transcript.GetHash(context_hash, &context_hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400107 return 0;
108 }
109
Robert Sloanb6d070c2017-07-24 08:40:01 -0700110 return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret,
Robert Sloan5d625782017-02-13 09:55:39 -0800111 hs->hash_len, label, label_len, context_hash,
112 context_hash_len, len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400113}
114
Steven Valdez909b19f2016-11-21 15:35:44 -0500115int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
David Benjaminc895d6b2016-08-11 13:26:41 -0400116 const uint8_t *traffic_secret,
117 size_t traffic_secret_len) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700118 const SSL_SESSION *session = SSL_get_session(ssl);
Robert Sloanf6200e72017-07-10 08:09:18 -0700119 uint16_t version = SSL_SESSION_protocol_version(session);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700120
David Benjaminc895d6b2016-08-11 13:26:41 -0400121 if (traffic_secret_len > 0xff) {
122 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
123 return 0;
124 }
125
David Benjaminc895d6b2016-08-11 13:26:41 -0400126 /* Look up cipher suite properties. */
127 const EVP_AEAD *aead;
Steven Valdez909b19f2016-11-21 15:35:44 -0500128 size_t discard;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700129 if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher,
Robert Sloan8ff03552017-06-14 12:40:58 -0700130 version, SSL_is_dtls(ssl))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400131 return 0;
132 }
133
Robert Sloanf6200e72017-07-10 08:09:18 -0700134 const EVP_MD *digest = SSL_SESSION_get_digest(session);
Robert Sloan5d625782017-02-13 09:55:39 -0800135
David Benjaminc895d6b2016-08-11 13:26:41 -0400136 /* Derive the key. */
137 size_t key_len = EVP_AEAD_key_length(aead);
138 uint8_t key[EVP_AEAD_MAX_KEY_LENGTH];
David Benjamin95add822016-10-19 01:09:12 -0400139 if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500140 (const uint8_t *)"key", 3, NULL, 0, key_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400141 return 0;
142 }
143
David Benjaminc895d6b2016-08-11 13:26:41 -0400144 /* Derive the IV. */
145 size_t iv_len = EVP_AEAD_nonce_length(aead);
146 uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH];
David Benjamin95add822016-10-19 01:09:12 -0400147 if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500148 (const uint8_t *)"iv", 2, NULL, 0, iv_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400149 return 0;
150 }
151
Robert Sloanb6d070c2017-07-24 08:40:01 -0700152 UniquePtr<SSLAEADContext> traffic_aead = SSLAEADContext::Create(
153 direction, version, SSL_is_dtls(ssl), session->cipher, key, key_len, NULL,
154 0, iv, iv_len);
155 if (!traffic_aead) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400156 return 0;
157 }
158
159 if (direction == evp_aead_open) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700160 if (!ssl->method->set_read_state(ssl, std::move(traffic_aead))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400161 return 0;
162 }
163 } else {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700164 if (!ssl->method->set_write_state(ssl, std::move(traffic_aead))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400165 return 0;
166 }
167 }
168
169 /* Save the traffic secret. */
170 if (direction == evp_aead_open) {
Robert Sloan69939df2017-01-09 10:53:07 -0800171 OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret,
172 traffic_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400173 ssl->s3->read_traffic_secret_len = traffic_secret_len;
174 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800175 OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret,
176 traffic_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400177 ssl->s3->write_traffic_secret_len = traffic_secret_len;
178 }
179
180 return 1;
181}
182
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700183static const char kTLS13LabelExporter[] = "exporter master secret";
184static const char kTLS13LabelEarlyExporter[] = "early exporter master secret";
185
186static const char kTLS13LabelClientEarlyTraffic[] =
187 "client early traffic secret";
David Benjamin95add822016-10-19 01:09:12 -0400188static const char kTLS13LabelClientHandshakeTraffic[] =
189 "client handshake traffic secret";
190static const char kTLS13LabelServerHandshakeTraffic[] =
191 "server handshake traffic secret";
192static const char kTLS13LabelClientApplicationTraffic[] =
193 "client application traffic secret";
194static const char kTLS13LabelServerApplicationTraffic[] =
195 "server application traffic secret";
David Benjaminc895d6b2016-08-11 13:26:41 -0400196
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700197int tls13_derive_early_secrets(SSL_HANDSHAKE *hs) {
198 SSL *const ssl = hs->ssl;
199 return derive_secret(hs, hs->early_traffic_secret, hs->hash_len,
200 (const uint8_t *)kTLS13LabelClientEarlyTraffic,
201 strlen(kTLS13LabelClientEarlyTraffic)) &&
202 ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET",
203 hs->early_traffic_secret, hs->hash_len) &&
204 derive_secret(hs, ssl->s3->early_exporter_secret, hs->hash_len,
205 (const uint8_t *)kTLS13LabelEarlyExporter,
206 strlen(kTLS13LabelEarlyExporter));
207}
208
Robert Sloan69939df2017-01-09 10:53:07 -0800209int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500210 SSL *const ssl = hs->ssl;
Robert Sloan69939df2017-01-09 10:53:07 -0800211 return derive_secret(hs, hs->client_handshake_secret, hs->hash_len,
212 (const uint8_t *)kTLS13LabelClientHandshakeTraffic,
213 strlen(kTLS13LabelClientHandshakeTraffic)) &&
214 ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
215 hs->client_handshake_secret, hs->hash_len) &&
216 derive_secret(hs, hs->server_handshake_secret, hs->hash_len,
217 (const uint8_t *)kTLS13LabelServerHandshakeTraffic,
218 strlen(kTLS13LabelServerHandshakeTraffic)) &&
219 ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
220 hs->server_handshake_secret, hs->hash_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400221}
222
David Benjamin1b249672016-12-06 18:25:50 -0500223int tls13_derive_application_secrets(SSL_HANDSHAKE *hs) {
224 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -0500225 ssl->s3->exporter_secret_len = hs->hash_len;
David Benjamin1b249672016-12-06 18:25:50 -0500226 return derive_secret(hs, hs->client_traffic_secret_0, hs->hash_len,
David Benjamin95add822016-10-19 01:09:12 -0400227 (const uint8_t *)kTLS13LabelClientApplicationTraffic,
228 strlen(kTLS13LabelClientApplicationTraffic)) &&
229 ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0",
230 hs->client_traffic_secret_0, hs->hash_len) &&
David Benjamin1b249672016-12-06 18:25:50 -0500231 derive_secret(hs, hs->server_traffic_secret_0, hs->hash_len,
David Benjamin95add822016-10-19 01:09:12 -0400232 (const uint8_t *)kTLS13LabelServerApplicationTraffic,
233 strlen(kTLS13LabelServerApplicationTraffic)) &&
234 ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0",
Steven Valdez909b19f2016-11-21 15:35:44 -0500235 hs->server_traffic_secret_0, hs->hash_len) &&
David Benjamin1b249672016-12-06 18:25:50 -0500236 derive_secret(hs, ssl->s3->exporter_secret, hs->hash_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500237 (const uint8_t *)kTLS13LabelExporter,
238 strlen(kTLS13LabelExporter));
David Benjaminc895d6b2016-08-11 13:26:41 -0400239}
240
David Benjamin95add822016-10-19 01:09:12 -0400241static const char kTLS13LabelApplicationTraffic[] =
242 "application traffic secret";
243
David Benjaminc895d6b2016-08-11 13:26:41 -0400244int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400245 uint8_t *secret;
246 size_t secret_len;
247 if (direction == evp_aead_open) {
248 secret = ssl->s3->read_traffic_secret;
249 secret_len = ssl->s3->read_traffic_secret_len;
250 } else {
251 secret = ssl->s3->write_traffic_secret;
252 secret_len = ssl->s3->write_traffic_secret_len;
253 }
254
Robert Sloan84377092017-08-14 09:33:19 -0700255 const EVP_MD *digest = SSL_SESSION_get_digest(SSL_get_session(ssl));
David Benjaminc895d6b2016-08-11 13:26:41 -0400256 if (!hkdf_expand_label(secret, digest, secret, secret_len,
257 (const uint8_t *)kTLS13LabelApplicationTraffic,
258 strlen(kTLS13LabelApplicationTraffic), NULL, 0,
259 secret_len)) {
260 return 0;
261 }
262
Steven Valdez909b19f2016-11-21 15:35:44 -0500263 return tls13_set_traffic_key(ssl, direction, secret, secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400264}
265
David Benjaminc895d6b2016-08-11 13:26:41 -0400266static const char kTLS13LabelResumption[] = "resumption master secret";
267
David Benjamin1b249672016-12-06 18:25:50 -0500268int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) {
Robert Sloana94fe052017-02-21 08:49:28 -0800269 if (hs->hash_len > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin1b249672016-12-06 18:25:50 -0500270 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
271 return 0;
272 }
273
Robert Sloana94fe052017-02-21 08:49:28 -0800274 hs->new_session->master_key_length = hs->hash_len;
275 return derive_secret(
276 hs, hs->new_session->master_key, hs->new_session->master_key_length,
277 (const uint8_t *)kTLS13LabelResumption, strlen(kTLS13LabelResumption));
Steven Valdez909b19f2016-11-21 15:35:44 -0500278}
David Benjaminc895d6b2016-08-11 13:26:41 -0400279
Steven Valdez909b19f2016-11-21 15:35:44 -0500280static const char kTLS13LabelFinished[] = "finished";
281
282/* tls13_verify_data sets |out| to be the HMAC of |context| using a derived
283 * Finished key for both Finished messages and the PSK binder. */
284static int tls13_verify_data(const EVP_MD *digest, uint8_t *out,
285 size_t *out_len, const uint8_t *secret,
286 size_t hash_len, uint8_t *context,
287 size_t context_len) {
288 uint8_t key[EVP_MAX_MD_SIZE];
289 unsigned len;
290 if (!hkdf_expand_label(key, digest, secret, hash_len,
291 (const uint8_t *)kTLS13LabelFinished,
292 strlen(kTLS13LabelFinished), NULL, 0, hash_len) ||
293 HMAC(digest, key, hash_len, context, context_len, out, &len) == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400294 return 0;
295 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500296 *out_len = len;
David Benjaminc895d6b2016-08-11 13:26:41 -0400297 return 1;
298}
299
David Benjamin1b249672016-12-06 18:25:50 -0500300int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
301 int is_server) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400302 const uint8_t *traffic_secret;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700303 if (is_server) {
304 traffic_secret = hs->server_handshake_secret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400305 } else {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700306 traffic_secret = hs->client_handshake_secret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400307 }
308
Steven Valdez909b19f2016-11-21 15:35:44 -0500309 uint8_t context_hash[EVP_MAX_MD_SIZE];
310 size_t context_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700311 if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
312 !tls13_verify_data(hs->transcript.Digest(), out, out_len, traffic_secret,
313 hs->hash_len, context_hash, context_hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400314 return 0;
315 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400316 return 1;
317}
318
David Benjaminc895d6b2016-08-11 13:26:41 -0400319int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
320 const char *label, size_t label_len,
321 const uint8_t *context, size_t context_len,
322 int use_context) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400323 const uint8_t *hash = NULL;
324 size_t hash_len = 0;
325 if (use_context) {
326 hash = context;
327 hash_len = context_len;
328 }
Robert Sloan84377092017-08-14 09:33:19 -0700329
330 const EVP_MD *digest = SSL_SESSION_get_digest(SSL_get_session(ssl));
David Benjaminc895d6b2016-08-11 13:26:41 -0400331 return hkdf_expand_label(out, digest, ssl->s3->exporter_secret,
332 ssl->s3->exporter_secret_len, (const uint8_t *)label,
333 label_len, hash, hash_len, out_len);
334}
Steven Valdez909b19f2016-11-21 15:35:44 -0500335
336static const char kTLS13LabelPSKBinder[] = "resumption psk binder key";
337
Robert Sloan5d625782017-02-13 09:55:39 -0800338static int tls13_psk_binder(uint8_t *out, const EVP_MD *digest, uint8_t *psk,
339 size_t psk_len, uint8_t *context,
Steven Valdez909b19f2016-11-21 15:35:44 -0500340 size_t context_len, size_t hash_len) {
341 uint8_t binder_context[EVP_MAX_MD_SIZE];
342 unsigned binder_context_len;
343 if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) {
344 return 0;
345 }
346
347 uint8_t early_secret[EVP_MAX_MD_SIZE] = {0};
348 size_t early_secret_len;
349 if (!HKDF_extract(early_secret, &early_secret_len, digest, psk, hash_len,
350 NULL, 0)) {
351 return 0;
352 }
353
354 uint8_t binder_key[EVP_MAX_MD_SIZE] = {0};
355 size_t len;
356 if (!hkdf_expand_label(binder_key, digest, early_secret, hash_len,
357 (const uint8_t *)kTLS13LabelPSKBinder,
358 strlen(kTLS13LabelPSKBinder), binder_context,
359 binder_context_len, hash_len) ||
360 !tls13_verify_data(digest, out, &len, binder_key, hash_len, context,
361 context_len)) {
362 return 0;
363 }
364
365 return 1;
366}
367
Robert Sloan5d625782017-02-13 09:55:39 -0800368int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len) {
369 SSL *const ssl = hs->ssl;
Robert Sloanf6200e72017-07-10 08:09:18 -0700370 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session);
Steven Valdez909b19f2016-11-21 15:35:44 -0500371 size_t hash_len = EVP_MD_size(digest);
372
373 if (len < hash_len + 3) {
374 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
375 return 0;
376 }
377
Robert Sloanb6d070c2017-07-24 08:40:01 -0700378 ScopedEVP_MD_CTX ctx;
Steven Valdez909b19f2016-11-21 15:35:44 -0500379 uint8_t context[EVP_MAX_MD_SIZE];
380 unsigned context_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700381 if (!EVP_DigestInit_ex(ctx.get(), digest, NULL) ||
382 !EVP_DigestUpdate(ctx.get(), hs->transcript.buffer_data(),
383 hs->transcript.buffer_len()) ||
384 !EVP_DigestUpdate(ctx.get(), msg, len - hash_len - 3) ||
385 !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500386 return 0;
387 }
388
Steven Valdez909b19f2016-11-21 15:35:44 -0500389 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
Robert Sloan5d625782017-02-13 09:55:39 -0800390 if (!tls13_psk_binder(verify_data, digest, ssl->session->master_key,
391 ssl->session->master_key_length, context, context_len,
392 hash_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500393 return 0;
394 }
395
Robert Sloan69939df2017-01-09 10:53:07 -0800396 OPENSSL_memcpy(msg + len - hash_len, verify_data, hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -0500397 return 1;
398}
399
Robert Sloan5d625782017-02-13 09:55:39 -0800400int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
Robert Sloan84377092017-08-14 09:33:19 -0700401 const SSLMessage &msg, CBS *binders) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700402 size_t hash_len = hs->transcript.DigestLen();
Steven Valdez909b19f2016-11-21 15:35:44 -0500403
Robert Sloan84377092017-08-14 09:33:19 -0700404 /* The message must be large enough to exclude the binders. */
405 if (CBS_len(&msg.raw) < CBS_len(binders) + 2) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500406 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
407 return 0;
408 }
409
Robert Sloan84377092017-08-14 09:33:19 -0700410 /* Hash a ClientHello prefix up to the binders. This includes the header. For
411 * now, this assumes we only ever verify PSK binders on initial
412 * ClientHellos. */
Steven Valdez909b19f2016-11-21 15:35:44 -0500413 uint8_t context[EVP_MAX_MD_SIZE];
414 unsigned context_len;
Robert Sloan84377092017-08-14 09:33:19 -0700415 if (!EVP_Digest(CBS_data(&msg.raw), CBS_len(&msg.raw) - CBS_len(binders) - 2,
Robert Sloanb6d070c2017-07-24 08:40:01 -0700416 context, &context_len, hs->transcript.Digest(), NULL)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500417 return 0;
418 }
419
420 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
421 CBS binder;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700422 if (!tls13_psk_binder(verify_data, hs->transcript.Digest(),
Robert Sloan5d625782017-02-13 09:55:39 -0800423 session->master_key, session->master_key_length,
424 context, context_len, hash_len) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500425 /* We only consider the first PSK, so compare against the first binder. */
426 !CBS_get_u8_length_prefixed(binders, &binder)) {
427 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
428 return 0;
429 }
430
Robert Sloan5d625782017-02-13 09:55:39 -0800431 int binder_ok =
432 CBS_len(&binder) == hash_len &&
433 CRYPTO_memcmp(CBS_data(&binder), verify_data, hash_len) == 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500434#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
435 binder_ok = 1;
436#endif
437 if (!binder_ok) {
438 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
439 return 0;
440 }
441
442 return 1;
443}
Robert Sloanb6d070c2017-07-24 08:40:01 -0700444
445} // namespace bssl