blob: 1ae4849d7a621f1d89da43306c9d42af52a88ea1 [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,
36 int algorithm_prf) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070037 if (!hs->transcript.InitHash(version, algorithm_prf)) {
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) {
50 if (!init_key_schedule(hs, ssl3_protocol_version(hs->ssl),
51 hs->new_cipher->algorithm_prf)) {
52 return 0;
53 }
54
Robert Sloanb6d070c2017-07-24 08:40:01 -070055 hs->transcript.FreeBuffer();
David Benjaminc895d6b2016-08-11 13:26:41 -040056 return 1;
57}
58
Robert Sloan6d0d00e2017-03-27 07:13:07 -070059int tls13_init_early_key_schedule(SSL_HANDSHAKE *hs) {
60 SSL *const ssl = hs->ssl;
Robert Sloanf6200e72017-07-10 08:09:18 -070061 return init_key_schedule(hs, SSL_SESSION_protocol_version(ssl->session),
62 ssl->session->cipher->algorithm_prf);
Robert Sloan6d0d00e2017-03-27 07:13:07 -070063}
64
David Benjamin1b249672016-12-06 18:25:50 -050065int tls13_advance_key_schedule(SSL_HANDSHAKE *hs, const uint8_t *in,
66 size_t len) {
Robert Sloanb6d070c2017-07-24 08:40:01 -070067 return HKDF_extract(hs->secret, &hs->hash_len, hs->transcript.Digest(), in,
68 len, hs->secret, hs->hash_len);
David Benjaminc895d6b2016-08-11 13:26:41 -040069}
70
71static int hkdf_expand_label(uint8_t *out, const EVP_MD *digest,
72 const uint8_t *secret, size_t secret_len,
73 const uint8_t *label, size_t label_len,
74 const uint8_t *hash, size_t hash_len, size_t len) {
75 static const char kTLS13LabelVersion[] = "TLS 1.3, ";
76
Robert Sloanb6d070c2017-07-24 08:40:01 -070077 ScopedCBB cbb;
78 CBB child;
David Benjaminc895d6b2016-08-11 13:26:41 -040079 uint8_t *hkdf_label;
80 size_t hkdf_label_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -070081 if (!CBB_init(cbb.get(), 2 + 1 + strlen(kTLS13LabelVersion) + label_len + 1 +
82 hash_len) ||
83 !CBB_add_u16(cbb.get(), len) ||
84 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040085 !CBB_add_bytes(&child, (const uint8_t *)kTLS13LabelVersion,
86 strlen(kTLS13LabelVersion)) ||
87 !CBB_add_bytes(&child, label, label_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -070088 !CBB_add_u8_length_prefixed(cbb.get(), &child) ||
David Benjaminc895d6b2016-08-11 13:26:41 -040089 !CBB_add_bytes(&child, hash, hash_len) ||
Robert Sloanb6d070c2017-07-24 08:40:01 -070090 !CBB_finish(cbb.get(), &hkdf_label, &hkdf_label_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -040091 return 0;
92 }
93
94 int ret = HKDF_expand(out, len, digest, secret, secret_len, hkdf_label,
95 hkdf_label_len);
96 OPENSSL_free(hkdf_label);
97 return ret;
98}
99
David Benjaminc895d6b2016-08-11 13:26:41 -0400100/* derive_secret derives a secret of length |len| and writes the result in |out|
101 * with the given label and the current base secret and most recently-saved
102 * handshake context. It returns one on success and zero on error. */
David Benjamin1b249672016-12-06 18:25:50 -0500103static int derive_secret(SSL_HANDSHAKE *hs, uint8_t *out, size_t len,
David Benjaminc895d6b2016-08-11 13:26:41 -0400104 const uint8_t *label, size_t label_len) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500105 uint8_t context_hash[EVP_MAX_MD_SIZE];
106 size_t context_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700107 if (!hs->transcript.GetHash(context_hash, &context_hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400108 return 0;
109 }
110
Robert Sloanb6d070c2017-07-24 08:40:01 -0700111 return hkdf_expand_label(out, hs->transcript.Digest(), hs->secret,
Robert Sloan5d625782017-02-13 09:55:39 -0800112 hs->hash_len, label, label_len, context_hash,
113 context_hash_len, len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400114}
115
Steven Valdez909b19f2016-11-21 15:35:44 -0500116int tls13_set_traffic_key(SSL *ssl, enum evp_aead_direction_t direction,
David Benjaminc895d6b2016-08-11 13:26:41 -0400117 const uint8_t *traffic_secret,
118 size_t traffic_secret_len) {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700119 const SSL_SESSION *session = SSL_get_session(ssl);
Robert Sloanf6200e72017-07-10 08:09:18 -0700120 uint16_t version = SSL_SESSION_protocol_version(session);
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700121
David Benjaminc895d6b2016-08-11 13:26:41 -0400122 if (traffic_secret_len > 0xff) {
123 OPENSSL_PUT_ERROR(SSL, ERR_R_OVERFLOW);
124 return 0;
125 }
126
David Benjaminc895d6b2016-08-11 13:26:41 -0400127 /* Look up cipher suite properties. */
128 const EVP_AEAD *aead;
Steven Valdez909b19f2016-11-21 15:35:44 -0500129 size_t discard;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700130 if (!ssl_cipher_get_evp_aead(&aead, &discard, &discard, session->cipher,
Robert Sloan8ff03552017-06-14 12:40:58 -0700131 version, SSL_is_dtls(ssl))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400132 return 0;
133 }
134
Robert Sloanf6200e72017-07-10 08:09:18 -0700135 const EVP_MD *digest = SSL_SESSION_get_digest(session);
Robert Sloan5d625782017-02-13 09:55:39 -0800136
David Benjaminc895d6b2016-08-11 13:26:41 -0400137 /* Derive the key. */
138 size_t key_len = EVP_AEAD_key_length(aead);
139 uint8_t key[EVP_AEAD_MAX_KEY_LENGTH];
David Benjamin95add822016-10-19 01:09:12 -0400140 if (!hkdf_expand_label(key, digest, traffic_secret, traffic_secret_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500141 (const uint8_t *)"key", 3, NULL, 0, key_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400142 return 0;
143 }
144
David Benjaminc895d6b2016-08-11 13:26:41 -0400145 /* Derive the IV. */
146 size_t iv_len = EVP_AEAD_nonce_length(aead);
147 uint8_t iv[EVP_AEAD_MAX_NONCE_LENGTH];
David Benjamin95add822016-10-19 01:09:12 -0400148 if (!hkdf_expand_label(iv, digest, traffic_secret, traffic_secret_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500149 (const uint8_t *)"iv", 2, NULL, 0, iv_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400150 return 0;
151 }
152
Robert Sloanb6d070c2017-07-24 08:40:01 -0700153 UniquePtr<SSLAEADContext> traffic_aead = SSLAEADContext::Create(
154 direction, version, SSL_is_dtls(ssl), session->cipher, key, key_len, NULL,
155 0, iv, iv_len);
156 if (!traffic_aead) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400157 return 0;
158 }
159
160 if (direction == evp_aead_open) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700161 if (!ssl->method->set_read_state(ssl, std::move(traffic_aead))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400162 return 0;
163 }
164 } else {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700165 if (!ssl->method->set_write_state(ssl, std::move(traffic_aead))) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400166 return 0;
167 }
168 }
169
170 /* Save the traffic secret. */
171 if (direction == evp_aead_open) {
Robert Sloan69939df2017-01-09 10:53:07 -0800172 OPENSSL_memmove(ssl->s3->read_traffic_secret, traffic_secret,
173 traffic_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400174 ssl->s3->read_traffic_secret_len = traffic_secret_len;
175 } else {
Robert Sloan69939df2017-01-09 10:53:07 -0800176 OPENSSL_memmove(ssl->s3->write_traffic_secret, traffic_secret,
177 traffic_secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400178 ssl->s3->write_traffic_secret_len = traffic_secret_len;
179 }
180
181 return 1;
182}
183
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700184static const char kTLS13LabelExporter[] = "exporter master secret";
185static const char kTLS13LabelEarlyExporter[] = "early exporter master secret";
186
187static const char kTLS13LabelClientEarlyTraffic[] =
188 "client early traffic secret";
David Benjamin95add822016-10-19 01:09:12 -0400189static const char kTLS13LabelClientHandshakeTraffic[] =
190 "client handshake traffic secret";
191static const char kTLS13LabelServerHandshakeTraffic[] =
192 "server handshake traffic secret";
193static const char kTLS13LabelClientApplicationTraffic[] =
194 "client application traffic secret";
195static const char kTLS13LabelServerApplicationTraffic[] =
196 "server application traffic secret";
David Benjaminc895d6b2016-08-11 13:26:41 -0400197
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700198int tls13_derive_early_secrets(SSL_HANDSHAKE *hs) {
199 SSL *const ssl = hs->ssl;
200 return derive_secret(hs, hs->early_traffic_secret, hs->hash_len,
201 (const uint8_t *)kTLS13LabelClientEarlyTraffic,
202 strlen(kTLS13LabelClientEarlyTraffic)) &&
203 ssl_log_secret(ssl, "CLIENT_EARLY_TRAFFIC_SECRET",
204 hs->early_traffic_secret, hs->hash_len) &&
205 derive_secret(hs, ssl->s3->early_exporter_secret, hs->hash_len,
206 (const uint8_t *)kTLS13LabelEarlyExporter,
207 strlen(kTLS13LabelEarlyExporter));
208}
209
Robert Sloan69939df2017-01-09 10:53:07 -0800210int tls13_derive_handshake_secrets(SSL_HANDSHAKE *hs) {
David Benjamin1b249672016-12-06 18:25:50 -0500211 SSL *const ssl = hs->ssl;
Robert Sloan69939df2017-01-09 10:53:07 -0800212 return derive_secret(hs, hs->client_handshake_secret, hs->hash_len,
213 (const uint8_t *)kTLS13LabelClientHandshakeTraffic,
214 strlen(kTLS13LabelClientHandshakeTraffic)) &&
215 ssl_log_secret(ssl, "CLIENT_HANDSHAKE_TRAFFIC_SECRET",
216 hs->client_handshake_secret, hs->hash_len) &&
217 derive_secret(hs, hs->server_handshake_secret, hs->hash_len,
218 (const uint8_t *)kTLS13LabelServerHandshakeTraffic,
219 strlen(kTLS13LabelServerHandshakeTraffic)) &&
220 ssl_log_secret(ssl, "SERVER_HANDSHAKE_TRAFFIC_SECRET",
221 hs->server_handshake_secret, hs->hash_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400222}
223
David Benjamin1b249672016-12-06 18:25:50 -0500224int tls13_derive_application_secrets(SSL_HANDSHAKE *hs) {
225 SSL *const ssl = hs->ssl;
Steven Valdez909b19f2016-11-21 15:35:44 -0500226 ssl->s3->exporter_secret_len = hs->hash_len;
David Benjamin1b249672016-12-06 18:25:50 -0500227 return derive_secret(hs, hs->client_traffic_secret_0, hs->hash_len,
David Benjamin95add822016-10-19 01:09:12 -0400228 (const uint8_t *)kTLS13LabelClientApplicationTraffic,
229 strlen(kTLS13LabelClientApplicationTraffic)) &&
230 ssl_log_secret(ssl, "CLIENT_TRAFFIC_SECRET_0",
231 hs->client_traffic_secret_0, hs->hash_len) &&
David Benjamin1b249672016-12-06 18:25:50 -0500232 derive_secret(hs, hs->server_traffic_secret_0, hs->hash_len,
David Benjamin95add822016-10-19 01:09:12 -0400233 (const uint8_t *)kTLS13LabelServerApplicationTraffic,
234 strlen(kTLS13LabelServerApplicationTraffic)) &&
235 ssl_log_secret(ssl, "SERVER_TRAFFIC_SECRET_0",
Steven Valdez909b19f2016-11-21 15:35:44 -0500236 hs->server_traffic_secret_0, hs->hash_len) &&
David Benjamin1b249672016-12-06 18:25:50 -0500237 derive_secret(hs, ssl->s3->exporter_secret, hs->hash_len,
Steven Valdez909b19f2016-11-21 15:35:44 -0500238 (const uint8_t *)kTLS13LabelExporter,
239 strlen(kTLS13LabelExporter));
David Benjaminc895d6b2016-08-11 13:26:41 -0400240}
241
David Benjamin95add822016-10-19 01:09:12 -0400242static const char kTLS13LabelApplicationTraffic[] =
243 "application traffic secret";
244
David Benjaminc895d6b2016-08-11 13:26:41 -0400245int tls13_rotate_traffic_key(SSL *ssl, enum evp_aead_direction_t direction) {
Robert Sloan5d625782017-02-13 09:55:39 -0800246 const EVP_MD *digest = ssl_get_handshake_digest(
247 SSL_get_session(ssl)->cipher->algorithm_prf, ssl3_protocol_version(ssl));
David Benjaminc895d6b2016-08-11 13:26:41 -0400248
249 uint8_t *secret;
250 size_t secret_len;
251 if (direction == evp_aead_open) {
252 secret = ssl->s3->read_traffic_secret;
253 secret_len = ssl->s3->read_traffic_secret_len;
254 } else {
255 secret = ssl->s3->write_traffic_secret;
256 secret_len = ssl->s3->write_traffic_secret_len;
257 }
258
259 if (!hkdf_expand_label(secret, digest, secret, secret_len,
260 (const uint8_t *)kTLS13LabelApplicationTraffic,
261 strlen(kTLS13LabelApplicationTraffic), NULL, 0,
262 secret_len)) {
263 return 0;
264 }
265
Steven Valdez909b19f2016-11-21 15:35:44 -0500266 return tls13_set_traffic_key(ssl, direction, secret, secret_len);
David Benjaminc895d6b2016-08-11 13:26:41 -0400267}
268
David Benjaminc895d6b2016-08-11 13:26:41 -0400269static const char kTLS13LabelResumption[] = "resumption master secret";
270
David Benjamin1b249672016-12-06 18:25:50 -0500271int tls13_derive_resumption_secret(SSL_HANDSHAKE *hs) {
Robert Sloana94fe052017-02-21 08:49:28 -0800272 if (hs->hash_len > SSL_MAX_MASTER_KEY_LENGTH) {
David Benjamin1b249672016-12-06 18:25:50 -0500273 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
274 return 0;
275 }
276
Robert Sloana94fe052017-02-21 08:49:28 -0800277 hs->new_session->master_key_length = hs->hash_len;
278 return derive_secret(
279 hs, hs->new_session->master_key, hs->new_session->master_key_length,
280 (const uint8_t *)kTLS13LabelResumption, strlen(kTLS13LabelResumption));
Steven Valdez909b19f2016-11-21 15:35:44 -0500281}
David Benjaminc895d6b2016-08-11 13:26:41 -0400282
Steven Valdez909b19f2016-11-21 15:35:44 -0500283static const char kTLS13LabelFinished[] = "finished";
284
285/* tls13_verify_data sets |out| to be the HMAC of |context| using a derived
286 * Finished key for both Finished messages and the PSK binder. */
287static int tls13_verify_data(const EVP_MD *digest, uint8_t *out,
288 size_t *out_len, const uint8_t *secret,
289 size_t hash_len, uint8_t *context,
290 size_t context_len) {
291 uint8_t key[EVP_MAX_MD_SIZE];
292 unsigned len;
293 if (!hkdf_expand_label(key, digest, secret, hash_len,
294 (const uint8_t *)kTLS13LabelFinished,
295 strlen(kTLS13LabelFinished), NULL, 0, hash_len) ||
296 HMAC(digest, key, hash_len, context, context_len, out, &len) == NULL) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400297 return 0;
298 }
Steven Valdez909b19f2016-11-21 15:35:44 -0500299 *out_len = len;
David Benjaminc895d6b2016-08-11 13:26:41 -0400300 return 1;
301}
302
David Benjamin1b249672016-12-06 18:25:50 -0500303int tls13_finished_mac(SSL_HANDSHAKE *hs, uint8_t *out, size_t *out_len,
304 int is_server) {
David Benjaminf0c4a6c2016-08-11 13:26:41 -0400305 const uint8_t *traffic_secret;
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700306 if (is_server) {
307 traffic_secret = hs->server_handshake_secret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400308 } else {
Robert Sloan6d0d00e2017-03-27 07:13:07 -0700309 traffic_secret = hs->client_handshake_secret;
David Benjaminc895d6b2016-08-11 13:26:41 -0400310 }
311
Steven Valdez909b19f2016-11-21 15:35:44 -0500312 uint8_t context_hash[EVP_MAX_MD_SIZE];
313 size_t context_hash_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700314 if (!hs->transcript.GetHash(context_hash, &context_hash_len) ||
315 !tls13_verify_data(hs->transcript.Digest(), out, out_len, traffic_secret,
316 hs->hash_len, context_hash, context_hash_len)) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400317 return 0;
318 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400319 return 1;
320}
321
David Benjaminc895d6b2016-08-11 13:26:41 -0400322int tls13_export_keying_material(SSL *ssl, uint8_t *out, size_t out_len,
323 const char *label, size_t label_len,
324 const uint8_t *context, size_t context_len,
325 int use_context) {
Robert Sloan5d625782017-02-13 09:55:39 -0800326 const EVP_MD *digest = ssl_get_handshake_digest(
327 SSL_get_session(ssl)->cipher->algorithm_prf, ssl3_protocol_version(ssl));
David Benjaminc895d6b2016-08-11 13:26:41 -0400328
329 const uint8_t *hash = NULL;
330 size_t hash_len = 0;
331 if (use_context) {
332 hash = context;
333 hash_len = context_len;
334 }
335 return hkdf_expand_label(out, digest, ssl->s3->exporter_secret,
336 ssl->s3->exporter_secret_len, (const uint8_t *)label,
337 label_len, hash, hash_len, out_len);
338}
Steven Valdez909b19f2016-11-21 15:35:44 -0500339
340static const char kTLS13LabelPSKBinder[] = "resumption psk binder key";
341
Robert Sloan5d625782017-02-13 09:55:39 -0800342static int tls13_psk_binder(uint8_t *out, const EVP_MD *digest, uint8_t *psk,
343 size_t psk_len, uint8_t *context,
Steven Valdez909b19f2016-11-21 15:35:44 -0500344 size_t context_len, size_t hash_len) {
345 uint8_t binder_context[EVP_MAX_MD_SIZE];
346 unsigned binder_context_len;
347 if (!EVP_Digest(NULL, 0, binder_context, &binder_context_len, digest, NULL)) {
348 return 0;
349 }
350
351 uint8_t early_secret[EVP_MAX_MD_SIZE] = {0};
352 size_t early_secret_len;
353 if (!HKDF_extract(early_secret, &early_secret_len, digest, psk, hash_len,
354 NULL, 0)) {
355 return 0;
356 }
357
358 uint8_t binder_key[EVP_MAX_MD_SIZE] = {0};
359 size_t len;
360 if (!hkdf_expand_label(binder_key, digest, early_secret, hash_len,
361 (const uint8_t *)kTLS13LabelPSKBinder,
362 strlen(kTLS13LabelPSKBinder), binder_context,
363 binder_context_len, hash_len) ||
364 !tls13_verify_data(digest, out, &len, binder_key, hash_len, context,
365 context_len)) {
366 return 0;
367 }
368
369 return 1;
370}
371
Robert Sloan5d625782017-02-13 09:55:39 -0800372int tls13_write_psk_binder(SSL_HANDSHAKE *hs, uint8_t *msg, size_t len) {
373 SSL *const ssl = hs->ssl;
Robert Sloanf6200e72017-07-10 08:09:18 -0700374 const EVP_MD *digest = SSL_SESSION_get_digest(ssl->session);
Steven Valdez909b19f2016-11-21 15:35:44 -0500375 size_t hash_len = EVP_MD_size(digest);
376
377 if (len < hash_len + 3) {
378 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
379 return 0;
380 }
381
Robert Sloanb6d070c2017-07-24 08:40:01 -0700382 ScopedEVP_MD_CTX ctx;
Steven Valdez909b19f2016-11-21 15:35:44 -0500383 uint8_t context[EVP_MAX_MD_SIZE];
384 unsigned context_len;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700385 if (!EVP_DigestInit_ex(ctx.get(), digest, NULL) ||
386 !EVP_DigestUpdate(ctx.get(), hs->transcript.buffer_data(),
387 hs->transcript.buffer_len()) ||
388 !EVP_DigestUpdate(ctx.get(), msg, len - hash_len - 3) ||
389 !EVP_DigestFinal_ex(ctx.get(), context, &context_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500390 return 0;
391 }
392
Steven Valdez909b19f2016-11-21 15:35:44 -0500393 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
Robert Sloan5d625782017-02-13 09:55:39 -0800394 if (!tls13_psk_binder(verify_data, digest, ssl->session->master_key,
395 ssl->session->master_key_length, context, context_len,
396 hash_len)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500397 return 0;
398 }
399
Robert Sloan69939df2017-01-09 10:53:07 -0800400 OPENSSL_memcpy(msg + len - hash_len, verify_data, hash_len);
Steven Valdez909b19f2016-11-21 15:35:44 -0500401 return 1;
402}
403
Robert Sloan5d625782017-02-13 09:55:39 -0800404int tls13_verify_psk_binder(SSL_HANDSHAKE *hs, SSL_SESSION *session,
Steven Valdez909b19f2016-11-21 15:35:44 -0500405 CBS *binders) {
Robert Sloanb6d070c2017-07-24 08:40:01 -0700406 size_t hash_len = hs->transcript.DigestLen();
Steven Valdez909b19f2016-11-21 15:35:44 -0500407
408 /* Get the full ClientHello, including message header. It must be large enough
409 * to exclude the binders. */
410 CBS message;
Robert Sloan5d625782017-02-13 09:55:39 -0800411 hs->ssl->method->get_current_message(hs->ssl, &message);
Steven Valdez909b19f2016-11-21 15:35:44 -0500412 if (CBS_len(&message) < CBS_len(binders) + 2) {
413 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
414 return 0;
415 }
416
417 /* Hash a ClientHello prefix up to the binders. For now, this assumes we only
418 * ever verify PSK binders on initial ClientHellos. */
419 uint8_t context[EVP_MAX_MD_SIZE];
420 unsigned context_len;
421 if (!EVP_Digest(CBS_data(&message), CBS_len(&message) - CBS_len(binders) - 2,
Robert Sloanb6d070c2017-07-24 08:40:01 -0700422 context, &context_len, hs->transcript.Digest(), NULL)) {
Steven Valdez909b19f2016-11-21 15:35:44 -0500423 return 0;
424 }
425
426 uint8_t verify_data[EVP_MAX_MD_SIZE] = {0};
427 CBS binder;
Robert Sloanb6d070c2017-07-24 08:40:01 -0700428 if (!tls13_psk_binder(verify_data, hs->transcript.Digest(),
Robert Sloan5d625782017-02-13 09:55:39 -0800429 session->master_key, session->master_key_length,
430 context, context_len, hash_len) ||
Steven Valdez909b19f2016-11-21 15:35:44 -0500431 /* We only consider the first PSK, so compare against the first binder. */
432 !CBS_get_u8_length_prefixed(binders, &binder)) {
433 OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
434 return 0;
435 }
436
Robert Sloan5d625782017-02-13 09:55:39 -0800437 int binder_ok =
438 CBS_len(&binder) == hash_len &&
439 CRYPTO_memcmp(CBS_data(&binder), verify_data, hash_len) == 0;
Steven Valdez909b19f2016-11-21 15:35:44 -0500440#if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
441 binder_ok = 1;
442#endif
443 if (!binder_ok) {
444 OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
445 return 0;
446 }
447
448 return 1;
449}
Robert Sloanb6d070c2017-07-24 08:40:01 -0700450
451} // namespace bssl