1 /* 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 
20 #include <openssl/bytestring.h>
21 #include <openssl/err.h>
22 #include <openssl/hkdf.h>
23 #include <openssl/mem.h>
24 #include <openssl/stack.h>
25 #include <openssl/x509.h>
26 
27 #include "../crypto/internal.h"
28 #include "internal.h"
29 
30 
31 /* kMaxKeyUpdates is the number of consecutive KeyUpdates that will be
32  * processed. Without this limit an attacker could force unbounded processing
33  * without being able to return application data. */
34 static const uint8_t kMaxKeyUpdates = 32;
35 
tls13_handshake(SSL_HANDSHAKE * hs,int * out_early_return)36 int tls13_handshake(SSL_HANDSHAKE *hs, int *out_early_return) {
37   SSL *const ssl = hs->ssl;
38   for (;;) {
39     /* Resolve the operation the handshake was waiting on. */
40     switch (hs->wait) {
41       case ssl_hs_error:
42         OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE);
43         return -1;
44 
45       case ssl_hs_flush:
46       case ssl_hs_flush_and_read_message: {
47         int ret = ssl->method->flush_flight(ssl);
48         if (ret <= 0) {
49           return ret;
50         }
51         if (hs->wait != ssl_hs_flush_and_read_message) {
52           break;
53         }
54         ssl->method->expect_flight(ssl);
55         hs->wait = ssl_hs_read_message;
56         /* Fall-through. */
57       }
58 
59       case ssl_hs_read_message: {
60         int ret = ssl->method->ssl_get_message(ssl);
61         if (ret <= 0) {
62           return ret;
63         }
64         break;
65       }
66 
67       case ssl_hs_read_end_of_early_data: {
68         if (ssl->s3->hs->can_early_read) {
69           /* While we are processing early data, the handshake returns early. */
70           *out_early_return = 1;
71           return 1;
72         }
73         hs->wait = ssl_hs_ok;
74         break;
75       }
76 
77       case ssl_hs_x509_lookup:
78         ssl->rwstate = SSL_X509_LOOKUP;
79         hs->wait = ssl_hs_ok;
80         return -1;
81 
82       case ssl_hs_channel_id_lookup:
83         ssl->rwstate = SSL_CHANNEL_ID_LOOKUP;
84         hs->wait = ssl_hs_ok;
85         return -1;
86 
87       case ssl_hs_private_key_operation:
88         ssl->rwstate = SSL_PRIVATE_KEY_OPERATION;
89         hs->wait = ssl_hs_ok;
90         return -1;
91 
92       case ssl_hs_pending_ticket:
93         ssl->rwstate = SSL_PENDING_TICKET;
94         hs->wait = ssl_hs_ok;
95         return -1;
96 
97       case ssl_hs_ok:
98         break;
99     }
100 
101     /* Run the state machine again. */
102     hs->wait = hs->do_tls13_handshake(hs);
103     if (hs->wait == ssl_hs_error) {
104       /* Don't loop around to avoid a stray |SSL_R_SSL_HANDSHAKE_FAILURE| the
105        * first time around. */
106       return -1;
107     }
108     if (hs->wait == ssl_hs_ok) {
109       /* The handshake has completed. */
110       return 1;
111     }
112 
113     /* Otherwise, loop to the beginning and resolve what was blocking the
114      * handshake. */
115   }
116 }
117 
tls13_get_cert_verify_signature_input(SSL_HANDSHAKE * hs,uint8_t ** out,size_t * out_len,enum ssl_cert_verify_context_t cert_verify_context)118 int tls13_get_cert_verify_signature_input(
119     SSL_HANDSHAKE *hs, uint8_t **out, size_t *out_len,
120     enum ssl_cert_verify_context_t cert_verify_context) {
121   CBB cbb;
122   if (!CBB_init(&cbb, 64 + 33 + 1 + 2 * EVP_MAX_MD_SIZE)) {
123     goto err;
124   }
125 
126   for (size_t i = 0; i < 64; i++) {
127     if (!CBB_add_u8(&cbb, 0x20)) {
128       goto err;
129     }
130   }
131 
132   const uint8_t *context;
133   size_t context_len;
134   if (cert_verify_context == ssl_cert_verify_server) {
135     /* Include the NUL byte. */
136     static const char kContext[] = "TLS 1.3, server CertificateVerify";
137     context = (const uint8_t *)kContext;
138     context_len = sizeof(kContext);
139   } else if (cert_verify_context == ssl_cert_verify_client) {
140     static const char kContext[] = "TLS 1.3, client CertificateVerify";
141     context = (const uint8_t *)kContext;
142     context_len = sizeof(kContext);
143   } else if (cert_verify_context == ssl_cert_verify_channel_id) {
144     static const char kContext[] = "TLS 1.3, Channel ID";
145     context = (const uint8_t *)kContext;
146     context_len = sizeof(kContext);
147   } else {
148     goto err;
149   }
150 
151   if (!CBB_add_bytes(&cbb, context, context_len)) {
152     goto err;
153   }
154 
155   uint8_t context_hash[EVP_MAX_MD_SIZE];
156   size_t context_hash_len;
157   if (!SSL_TRANSCRIPT_get_hash(&hs->transcript, context_hash,
158                                &context_hash_len) ||
159       !CBB_add_bytes(&cbb, context_hash, context_hash_len) ||
160       !CBB_finish(&cbb, out, out_len)) {
161     goto err;
162   }
163 
164   return 1;
165 
166 err:
167   OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
168   CBB_cleanup(&cbb);
169   return 0;
170 }
171 
tls13_process_certificate(SSL_HANDSHAKE * hs,int allow_anonymous)172 int tls13_process_certificate(SSL_HANDSHAKE *hs, int allow_anonymous) {
173   SSL *const ssl = hs->ssl;
174   CBS cbs, context, certificate_list;
175   CBS_init(&cbs, ssl->init_msg, ssl->init_num);
176   if (!CBS_get_u8_length_prefixed(&cbs, &context) ||
177       CBS_len(&context) != 0) {
178     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
179     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
180     return 0;
181   }
182 
183   const int retain_sha256 =
184       ssl->server && ssl->retain_only_sha256_of_client_certs;
185   int ret = 0;
186 
187   EVP_PKEY *pkey = NULL;
188   STACK_OF(CRYPTO_BUFFER) *certs = sk_CRYPTO_BUFFER_new_null();
189   if (certs == NULL) {
190     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
191     OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
192     goto err;
193   }
194 
195   if (!CBS_get_u24_length_prefixed(&cbs, &certificate_list)) {
196     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
197     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
198     goto err;
199   }
200 
201   while (CBS_len(&certificate_list) > 0) {
202     CBS certificate, extensions;
203     if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate) ||
204         !CBS_get_u16_length_prefixed(&certificate_list, &extensions) ||
205         CBS_len(&certificate) == 0) {
206       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
207       OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH);
208       goto err;
209     }
210 
211     if (sk_CRYPTO_BUFFER_num(certs) == 0) {
212       pkey = ssl_cert_parse_pubkey(&certificate);
213       if (pkey == NULL) {
214         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
215         OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
216         goto err;
217       }
218       /* TLS 1.3 always uses certificate keys for signing thus the correct
219        * keyUsage is enforced. */
220       if (!ssl_cert_check_digital_signature_key_usage(&certificate)) {
221         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER);
222         goto err;
223       }
224 
225       if (retain_sha256) {
226         /* Retain the hash of the leaf certificate if requested. */
227         SHA256(CBS_data(&certificate), CBS_len(&certificate),
228                hs->new_session->peer_sha256);
229       }
230     }
231 
232     CRYPTO_BUFFER *buf =
233         CRYPTO_BUFFER_new_from_CBS(&certificate, ssl->ctx->pool);
234     if (buf == NULL ||
235         !sk_CRYPTO_BUFFER_push(certs, buf)) {
236       CRYPTO_BUFFER_free(buf);
237       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
238       OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE);
239       goto err;
240     }
241 
242     /* Parse out the extensions. */
243     int have_status_request = 0, have_sct = 0;
244     CBS status_request, sct;
245     const SSL_EXTENSION_TYPE ext_types[] = {
246         {TLSEXT_TYPE_status_request, &have_status_request, &status_request},
247         {TLSEXT_TYPE_certificate_timestamp, &have_sct, &sct},
248     };
249 
250     uint8_t alert = SSL_AD_DECODE_ERROR;
251     if (!ssl_parse_extensions(&extensions, &alert, ext_types,
252                               OPENSSL_ARRAY_SIZE(ext_types),
253                               0 /* reject unknown */)) {
254       ssl3_send_alert(ssl, SSL3_AL_FATAL, alert);
255       goto err;
256     }
257 
258     /* All Certificate extensions are parsed, but only the leaf extensions are
259      * stored. */
260     if (have_status_request) {
261       if (ssl->server || !ssl->ocsp_stapling_enabled) {
262         OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
263         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
264         goto err;
265       }
266 
267       uint8_t status_type;
268       CBS ocsp_response;
269       if (!CBS_get_u8(&status_request, &status_type) ||
270           status_type != TLSEXT_STATUSTYPE_ocsp ||
271           !CBS_get_u24_length_prefixed(&status_request, &ocsp_response) ||
272           CBS_len(&ocsp_response) == 0 ||
273           CBS_len(&status_request) != 0) {
274         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
275         goto err;
276       }
277 
278       if (sk_CRYPTO_BUFFER_num(certs) == 1 &&
279           !CBS_stow(&ocsp_response, &hs->new_session->ocsp_response,
280                     &hs->new_session->ocsp_response_length)) {
281         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
282         goto err;
283       }
284     }
285 
286     if (have_sct) {
287       if (ssl->server || !ssl->signed_cert_timestamps_enabled) {
288         OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION);
289         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_EXTENSION);
290         goto err;
291       }
292 
293       if (!ssl_is_sct_list_valid(&sct)) {
294         OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_PARSING_EXTENSION);
295         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
296         goto err;
297       }
298 
299       if (sk_CRYPTO_BUFFER_num(certs) == 1 &&
300           !CBS_stow(
301               &sct, &hs->new_session->tlsext_signed_cert_timestamp_list,
302               &hs->new_session->tlsext_signed_cert_timestamp_list_length)) {
303         ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
304         goto err;
305       }
306     }
307   }
308 
309   if (CBS_len(&cbs) != 0) {
310     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
311     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
312     goto err;
313   }
314 
315   EVP_PKEY_free(hs->peer_pubkey);
316   hs->peer_pubkey = pkey;
317   pkey = NULL;
318 
319   sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free);
320   hs->new_session->certs = certs;
321   certs = NULL;
322 
323   if (!ssl->ctx->x509_method->session_cache_objects(hs->new_session)) {
324     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
325     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
326     goto err;
327   }
328 
329   if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) {
330     if (!allow_anonymous) {
331       OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE);
332       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_CERTIFICATE_REQUIRED);
333       goto err;
334     }
335 
336     /* OpenSSL returns X509_V_OK when no certificates are requested. This is
337      * classed by them as a bug, but it's assumed by at least NGINX. */
338     hs->new_session->verify_result = X509_V_OK;
339 
340     /* No certificate, so nothing more to do. */
341     ret = 1;
342     goto err;
343   }
344 
345   hs->new_session->peer_sha256_valid = retain_sha256;
346 
347   if (!ssl->ctx->x509_method->session_verify_cert_chain(hs->new_session,
348                                                         ssl)) {
349     goto err;
350   }
351 
352   ret = 1;
353 
354 err:
355   sk_CRYPTO_BUFFER_pop_free(certs, CRYPTO_BUFFER_free);
356   EVP_PKEY_free(pkey);
357   return ret;
358 }
359 
tls13_process_certificate_verify(SSL_HANDSHAKE * hs)360 int tls13_process_certificate_verify(SSL_HANDSHAKE *hs) {
361   SSL *const ssl = hs->ssl;
362   int ret = 0;
363   uint8_t *msg = NULL;
364   size_t msg_len;
365 
366   if (hs->peer_pubkey == NULL) {
367     goto err;
368   }
369 
370   CBS cbs, signature;
371   uint16_t signature_algorithm;
372   CBS_init(&cbs, ssl->init_msg, ssl->init_num);
373   if (!CBS_get_u16(&cbs, &signature_algorithm) ||
374       !CBS_get_u16_length_prefixed(&cbs, &signature) ||
375       CBS_len(&cbs) != 0) {
376     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
377     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
378     goto err;
379   }
380 
381   int al;
382   if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) {
383     ssl3_send_alert(ssl, SSL3_AL_FATAL, al);
384     goto err;
385   }
386   hs->new_session->peer_signature_algorithm = signature_algorithm;
387 
388   if (!tls13_get_cert_verify_signature_input(
389           hs, &msg, &msg_len,
390           ssl->server ? ssl_cert_verify_client : ssl_cert_verify_server)) {
391     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
392     goto err;
393   }
394 
395   int sig_ok =
396       ssl_public_key_verify(ssl, CBS_data(&signature), CBS_len(&signature),
397                             signature_algorithm, hs->peer_pubkey, msg, msg_len);
398 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
399   sig_ok = 1;
400   ERR_clear_error();
401 #endif
402   if (!sig_ok) {
403     OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE);
404     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
405     goto err;
406   }
407 
408   ret = 1;
409 
410 err:
411   OPENSSL_free(msg);
412   return ret;
413 }
414 
tls13_process_finished(SSL_HANDSHAKE * hs,int use_saved_value)415 int tls13_process_finished(SSL_HANDSHAKE *hs, int use_saved_value) {
416   SSL *const ssl = hs->ssl;
417   uint8_t verify_data_buf[EVP_MAX_MD_SIZE];
418   const uint8_t *verify_data;
419   size_t verify_data_len;
420   if (use_saved_value) {
421     assert(ssl->server);
422     verify_data = hs->expected_client_finished;
423     verify_data_len = hs->hash_len;
424   } else {
425     if (!tls13_finished_mac(hs, verify_data_buf, &verify_data_len,
426                             !ssl->server)) {
427       return 0;
428     }
429     verify_data = verify_data_buf;
430   }
431 
432   int finished_ok =
433       ssl->init_num == verify_data_len &&
434       CRYPTO_memcmp(verify_data, ssl->init_msg, verify_data_len) == 0;
435 #if defined(BORINGSSL_UNSAFE_FUZZER_MODE)
436   finished_ok = 1;
437 #endif
438   if (!finished_ok) {
439     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR);
440     OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
441     return 0;
442   }
443 
444   return 1;
445 }
446 
tls13_add_certificate(SSL_HANDSHAKE * hs)447 int tls13_add_certificate(SSL_HANDSHAKE *hs) {
448   SSL *const ssl = hs->ssl;
449   CBB cbb, body, certificate_list;
450   if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_CERTIFICATE) ||
451       /* The request context is always empty in the handshake. */
452       !CBB_add_u8(&body, 0) ||
453       !CBB_add_u24_length_prefixed(&body, &certificate_list)) {
454     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
455     goto err;
456   }
457 
458   if (!ssl_has_certificate(ssl)) {
459     if (!ssl_add_message_cbb(ssl, &cbb)) {
460       goto err;
461     }
462 
463     return 1;
464   }
465 
466   CERT *cert = ssl->cert;
467   CRYPTO_BUFFER *leaf_buf = sk_CRYPTO_BUFFER_value(cert->chain, 0);
468   CBB leaf, extensions;
469   if (!CBB_add_u24_length_prefixed(&certificate_list, &leaf) ||
470       !CBB_add_bytes(&leaf, CRYPTO_BUFFER_data(leaf_buf),
471                      CRYPTO_BUFFER_len(leaf_buf)) ||
472       !CBB_add_u16_length_prefixed(&certificate_list, &extensions)) {
473     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
474     goto err;
475   }
476 
477   if (hs->scts_requested && ssl->cert->signed_cert_timestamp_list != NULL) {
478     CBB contents;
479     if (!CBB_add_u16(&extensions, TLSEXT_TYPE_certificate_timestamp) ||
480         !CBB_add_u16_length_prefixed(&extensions, &contents) ||
481         !CBB_add_bytes(
482             &contents,
483             CRYPTO_BUFFER_data(ssl->cert->signed_cert_timestamp_list),
484             CRYPTO_BUFFER_len(ssl->cert->signed_cert_timestamp_list)) ||
485         !CBB_flush(&extensions)) {
486       OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
487       goto err;
488     }
489   }
490 
491   if (hs->ocsp_stapling_requested &&
492       ssl->cert->ocsp_response != NULL) {
493     CBB contents, ocsp_response;
494     if (!CBB_add_u16(&extensions, TLSEXT_TYPE_status_request) ||
495         !CBB_add_u16_length_prefixed(&extensions, &contents) ||
496         !CBB_add_u8(&contents, TLSEXT_STATUSTYPE_ocsp) ||
497         !CBB_add_u24_length_prefixed(&contents, &ocsp_response) ||
498         !CBB_add_bytes(&ocsp_response,
499                        CRYPTO_BUFFER_data(ssl->cert->ocsp_response),
500                        CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) ||
501         !CBB_flush(&extensions)) {
502       OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
503       goto err;
504     }
505   }
506 
507   for (size_t i = 1; i < sk_CRYPTO_BUFFER_num(cert->chain); i++) {
508     CRYPTO_BUFFER *cert_buf = sk_CRYPTO_BUFFER_value(cert->chain, i);
509     CBB child;
510     if (!CBB_add_u24_length_prefixed(&certificate_list, &child) ||
511         !CBB_add_bytes(&child, CRYPTO_BUFFER_data(cert_buf),
512                        CRYPTO_BUFFER_len(cert_buf)) ||
513         !CBB_add_u16(&certificate_list, 0 /* no extensions */)) {
514       OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
515       goto err;
516     }
517   }
518 
519   if (!ssl_add_message_cbb(ssl, &cbb)) {
520     goto err;
521   }
522 
523   return 1;
524 
525 err:
526   CBB_cleanup(&cbb);
527   return 0;
528 }
529 
tls13_add_certificate_verify(SSL_HANDSHAKE * hs,int is_first_run)530 enum ssl_private_key_result_t tls13_add_certificate_verify(SSL_HANDSHAKE *hs,
531                                                            int is_first_run) {
532   SSL *const ssl = hs->ssl;
533   enum ssl_private_key_result_t ret = ssl_private_key_failure;
534   uint8_t *msg = NULL;
535   size_t msg_len;
536   CBB cbb, body;
537   CBB_zero(&cbb);
538 
539   uint16_t signature_algorithm;
540   if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) {
541     goto err;
542   }
543   if (!ssl->method->init_message(ssl, &cbb, &body,
544                                  SSL3_MT_CERTIFICATE_VERIFY) ||
545       !CBB_add_u16(&body, signature_algorithm)) {
546     OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR);
547     goto err;
548   }
549 
550   /* Sign the digest. */
551   CBB child;
552   const size_t max_sig_len = ssl_private_key_max_signature_len(ssl);
553   uint8_t *sig;
554   size_t sig_len;
555   if (!CBB_add_u16_length_prefixed(&body, &child) ||
556       !CBB_reserve(&child, &sig, max_sig_len)) {
557     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
558     goto err;
559   }
560 
561   enum ssl_private_key_result_t sign_result;
562   if (is_first_run) {
563     if (!tls13_get_cert_verify_signature_input(
564             hs, &msg, &msg_len,
565             ssl->server ? ssl_cert_verify_server : ssl_cert_verify_client)) {
566       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
567       goto err;
568     }
569     sign_result = ssl_private_key_sign(ssl, sig, &sig_len, max_sig_len,
570                                        signature_algorithm, msg, msg_len);
571   } else {
572     sign_result = ssl_private_key_complete(ssl, sig, &sig_len, max_sig_len);
573   }
574 
575   if (sign_result != ssl_private_key_success) {
576     ret = sign_result;
577     goto err;
578   }
579 
580   if (!CBB_did_write(&child, sig_len) ||
581       !ssl_add_message_cbb(ssl, &cbb)) {
582     goto err;
583   }
584 
585   ret = ssl_private_key_success;
586 
587 err:
588   CBB_cleanup(&cbb);
589   OPENSSL_free(msg);
590   return ret;
591 }
592 
tls13_add_finished(SSL_HANDSHAKE * hs)593 int tls13_add_finished(SSL_HANDSHAKE *hs) {
594   SSL *const ssl = hs->ssl;
595   size_t verify_data_len;
596   uint8_t verify_data[EVP_MAX_MD_SIZE];
597 
598   if (!tls13_finished_mac(hs, verify_data, &verify_data_len, ssl->server)) {
599     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR);
600     OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED);
601     return 0;
602   }
603 
604   CBB cbb, body;
605   if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_FINISHED) ||
606       !CBB_add_bytes(&body, verify_data, verify_data_len) ||
607       !ssl_add_message_cbb(ssl, &cbb)) {
608     CBB_cleanup(&cbb);
609     return 0;
610   }
611 
612   return 1;
613 }
614 
tls13_receive_key_update(SSL * ssl)615 static int tls13_receive_key_update(SSL *ssl) {
616   CBS cbs;
617   uint8_t key_update_request;
618   CBS_init(&cbs, ssl->init_msg, ssl->init_num);
619   if (!CBS_get_u8(&cbs, &key_update_request) ||
620       CBS_len(&cbs) != 0 ||
621       (key_update_request != SSL_KEY_UPDATE_NOT_REQUESTED &&
622        key_update_request != SSL_KEY_UPDATE_REQUESTED)) {
623     OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR);
624     ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR);
625     return 0;
626   }
627 
628   /* TODO(svaldez): Send KeyUpdate if |key_update_request| is
629    * |SSL_KEY_UPDATE_REQUESTED|. */
630   return tls13_rotate_traffic_key(ssl, evp_aead_open);
631 }
632 
tls13_post_handshake(SSL * ssl)633 int tls13_post_handshake(SSL *ssl) {
634   if (ssl->s3->tmp.message_type == SSL3_MT_KEY_UPDATE) {
635     ssl->s3->key_update_count++;
636     if (ssl->s3->key_update_count > kMaxKeyUpdates) {
637       OPENSSL_PUT_ERROR(SSL, SSL_R_TOO_MANY_KEY_UPDATES);
638       ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
639       return 0;
640     }
641 
642     return tls13_receive_key_update(ssl);
643   }
644 
645   ssl->s3->key_update_count = 0;
646 
647   if (ssl->s3->tmp.message_type == SSL3_MT_NEW_SESSION_TICKET &&
648       !ssl->server) {
649     return tls13_process_new_session_ticket(ssl);
650   }
651 
652   // TODO(svaldez): Handle post-handshake authentication.
653 
654   ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE);
655   OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE);
656   return 0;
657 }
658