@@ -426,27 +426,145 @@ static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
426426 return 0 ;
427427}
428428
429- int tls13_change_cipher_state (SSL * s , int which )
430- {
431429#ifdef CHARSET_EBCDIC
432- static const unsigned char client_early_traffic [] = {0x63 , 0x20 , 0x65 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
433- static const unsigned char client_handshake_traffic [] = {0x63 , 0x20 , 0x68 , 0x73 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
434- static const unsigned char client_application_traffic [] = {0x63 , 0x20 , 0x61 , 0x70 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
435- static const unsigned char server_handshake_traffic [] = {0x73 , 0x20 , 0x68 , 0x73 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
436- static const unsigned char server_application_traffic [] = {0x73 , 0x20 , 0x61 , 0x70 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
437- static const unsigned char exporter_master_secret [] = {0x65 , 0x78 , 0x70 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
438- static const unsigned char resumption_master_secret [] = {0x72 , 0x65 , 0x73 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
439- static const unsigned char early_exporter_master_secret [] = {0x65 , 0x20 , 0x65 , 0x78 , 0x70 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
430+ static const unsigned char client_early_traffic [] = {0x63 , 0x20 , 0x65 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
431+ static const unsigned char client_handshake_traffic [] = {0x63 , 0x20 , 0x68 , 0x73 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
432+ static const unsigned char client_application_traffic [] = {0x63 , 0x20 , 0x61 , 0x70 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
433+ static const unsigned char server_handshake_traffic [] = {0x73 , 0x20 , 0x68 , 0x73 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
434+ static const unsigned char server_application_traffic [] = {0x73 , 0x20 , 0x61 , 0x70 , 0x20 , /*traffic*/ 0x74 , 0x72 , 0x61 , 0x66 , 0x66 , 0x69 , 0x63 , 0x00 };
435+ static const unsigned char exporter_master_secret [] = {0x65 , 0x78 , 0x70 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
436+ static const unsigned char resumption_master_secret [] = {0x72 , 0x65 , 0x73 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
437+ static const unsigned char early_exporter_master_secret [] = {0x65 , 0x20 , 0x65 , 0x78 , 0x70 , 0x20 , /* master*/ 0x6D , 0x61 , 0x73 , 0x74 , 0x65 , 0x72 , 0x00 };
440438#else
441- static const unsigned char client_early_traffic [] = "c e traffic" ;
442- static const unsigned char client_handshake_traffic [] = "c hs traffic" ;
443- static const unsigned char client_application_traffic [] = "c ap traffic" ;
444- static const unsigned char server_handshake_traffic [] = "s hs traffic" ;
445- static const unsigned char server_application_traffic [] = "s ap traffic" ;
446- static const unsigned char exporter_master_secret [] = "exp master" ;
447- static const unsigned char resumption_master_secret [] = "res master" ;
448- static const unsigned char early_exporter_master_secret [] = "e exp master" ;
439+ static const unsigned char client_early_traffic [] = "c e traffic" ;
440+ static const unsigned char client_handshake_traffic [] = "c hs traffic" ;
441+ static const unsigned char client_application_traffic [] = "c ap traffic" ;
442+ static const unsigned char server_handshake_traffic [] = "s hs traffic" ;
443+ static const unsigned char server_application_traffic [] = "s ap traffic" ;
444+ static const unsigned char exporter_master_secret [] = "exp master" ;
445+ static const unsigned char resumption_master_secret [] = "res master" ;
446+ static const unsigned char early_exporter_master_secret [] = "e exp master" ;
449447#endif
448+ #ifndef OPENSSL_NO_QUIC
449+ static int quic_change_cipher_state (SSL * s , int which )
450+ {
451+ unsigned char hash [EVP_MAX_MD_SIZE ];
452+ size_t hashlen = 0 ;
453+ int hashleni ;
454+ int ret = 0 ;
455+ const EVP_MD * md = NULL ;
456+ OSSL_ENCRYPTION_LEVEL level = ssl_encryption_initial ;
457+ int is_handshake = ((which & SSL3_CC_HANDSHAKE ) == SSL3_CC_HANDSHAKE );
458+ int is_client_read = ((which & SSL3_CHANGE_CIPHER_CLIENT_READ ) == SSL3_CHANGE_CIPHER_CLIENT_READ );
459+ int is_server_write = ((which & SSL3_CHANGE_CIPHER_SERVER_WRITE ) == SSL3_CHANGE_CIPHER_SERVER_WRITE );
460+ int is_early = (which & SSL3_CC_EARLY );
461+
462+ md = ssl_handshake_md (s );
463+ if (!ssl3_digest_cached_records (s , 1 )
464+ || !ssl_handshake_hash (s , hash , sizeof (hash ), & hashlen )) {
465+ /* SSLfatal() already called */ ;
466+ goto err ;
467+ }
468+
469+ /* Ensure cast to size_t is safe */
470+ hashleni = EVP_MD_size (md );
471+ if (!ossl_assert (hashleni >= 0 )) {
472+ SSLfatal (s , SSL_AD_INTERNAL_ERROR , SSL_F_QUIC_CHANGE_CIPHER_STATE ,
473+ ERR_R_EVP_LIB );
474+ goto err ;
475+ }
476+ hashlen = (size_t )hashleni ;
477+
478+ if (is_handshake )
479+ level = ssl_encryption_handshake ;
480+ else
481+ level = ssl_encryption_application ;
482+
483+ if (is_client_read || is_server_write ) {
484+ if (is_handshake ) {
485+ level = ssl_encryption_handshake ;
486+
487+ if (!tls13_hkdf_expand (s , md , s -> handshake_secret , client_handshake_traffic ,
488+ sizeof (client_handshake_traffic )- 1 , hash , hashlen ,
489+ s -> client_hand_traffic_secret , hashlen , 1 )) {
490+ /* SSLfatal() already called */
491+ goto err ;
492+ }
493+ if (!ssl_log_secret (s , CLIENT_HANDSHAKE_LABEL , s -> client_hand_traffic_secret , hashlen )) {
494+ /* SSLfatal() already called */
495+ goto err ;
496+ }
497+
498+ if (!tls13_hkdf_expand (s , md , s -> handshake_secret , server_handshake_traffic ,
499+ sizeof (server_handshake_traffic )- 1 , hash , hashlen ,
500+ s -> server_hand_traffic_secret , hashlen , 1 )) {
501+ /* SSLfatal() already called */
502+ goto err ;
503+ }
504+ if (!ssl_log_secret (s , SERVER_HANDSHAKE_LABEL , s -> server_hand_traffic_secret , hashlen )) {
505+ /* SSLfatal() already called */
506+ goto err ;
507+ }
508+ } else {
509+ level = ssl_encryption_application ;
510+
511+ if (!tls13_hkdf_expand (s , md , s -> master_secret , client_application_traffic ,
512+ sizeof (client_application_traffic )- 1 , hash , hashlen ,
513+ s -> client_app_traffic_secret , hashlen , 1 )) {
514+ /* SSLfatal() already called */
515+ goto err ;
516+ }
517+ if (!ssl_log_secret (s , CLIENT_APPLICATION_LABEL , s -> client_app_traffic_secret , hashlen )) {
518+ /* SSLfatal() already called */
519+ goto err ;
520+ }
521+
522+ if (!tls13_hkdf_expand (s , md , s -> master_secret , server_application_traffic ,
523+ sizeof (server_application_traffic )- 1 , hash , hashlen ,
524+ s -> server_app_traffic_secret , hashlen , 1 )) {
525+ /* SSLfatal() already called */
526+ goto err ;
527+ }
528+ if (!ssl_log_secret (s , SERVER_APPLICATION_LABEL , s -> server_app_traffic_secret , hashlen )) {
529+ /* SSLfatal() already called */
530+ goto err ;
531+ }
532+ }
533+ if (s -> server )
534+ s -> quic_write_level = level ;
535+ else
536+ s -> quic_read_level = level ;
537+ } else {
538+ if (is_early ) {
539+ level = ssl_encryption_early_data ;
540+
541+ if (!tls13_hkdf_expand (s , md , s -> early_secret , client_early_traffic ,
542+ sizeof (client_early_traffic )- 1 , hash , hashlen ,
543+ s -> client_early_traffic_secret , hashlen , 1 )) {
544+ /* SSLfatal() already called */
545+ goto err ;
546+ }
547+ if (!ssl_log_secret (s , CLIENT_EARLY_LABEL , s -> client_early_traffic_secret , hashlen )) {
548+ /* SSLfatal() already called */
549+ goto err ;
550+ }
551+ }
552+ if (s -> server )
553+ s -> quic_read_level = level ;
554+ else
555+ s -> quic_write_level = level ;
556+ }
557+
558+ if (level != ssl_encryption_initial && !quic_set_encryption_secrets (s , level ))
559+ goto err ;
560+
561+ ret = 1 ;
562+ err :
563+ return ret ;
564+ }
565+ #endif /* OPENSSL_NO_QUIC */
566+ int tls13_change_cipher_state (SSL * s , int which )
567+ {
450568 unsigned char * iv ;
451569 unsigned char secret [EVP_MAX_MD_SIZE ];
452570 unsigned char hashval [EVP_MAX_MD_SIZE ];
@@ -461,8 +579,10 @@ int tls13_change_cipher_state(SSL *s, int which)
461579 int ret = 0 ;
462580 const EVP_MD * md = NULL ;
463581 const EVP_CIPHER * cipher = NULL ;
582+
464583#ifndef OPENSSL_NO_QUIC
465- OSSL_ENCRYPTION_LEVEL level = ssl_encryption_initial ;
584+ if (SSL_IS_QUIC (s ))
585+ return quic_change_cipher_state (s , which );
466586#endif
467587
468588 if (which & SSL3_CC_READ ) {
@@ -511,9 +631,6 @@ int tls13_change_cipher_state(SSL *s, int which)
511631 label = client_early_traffic ;
512632 labellen = sizeof (client_early_traffic ) - 1 ;
513633 log_label = CLIENT_EARLY_LABEL ;
514- #ifndef OPENSSL_NO_QUIC
515- level = ssl_encryption_early_data ;
516- #endif
517634
518635 handlen = BIO_get_mem_data (s -> s3 .handshake_buffer , & hdata );
519636 if (handlen <= 0 ) {
@@ -587,30 +704,13 @@ int tls13_change_cipher_state(SSL *s, int which)
587704 /* SSLfatal() already called */
588705 goto err ;
589706 }
590- #ifndef OPENSSL_NO_QUIC
591- if (SSL_IS_QUIC (s )) {
592- if (s -> server )
593- s -> quic_read_level = ssl_encryption_early_data ;
594- else
595- s -> quic_write_level = ssl_encryption_early_data ;
596- }
597- #endif
598707 } else if (which & SSL3_CC_HANDSHAKE ) {
599708 insecret = s -> handshake_secret ;
600709 finsecret = s -> client_finished_secret ;
601710 finsecretlen = EVP_MD_size (ssl_handshake_md (s ));
602711 label = client_handshake_traffic ;
603712 labellen = sizeof (client_handshake_traffic ) - 1 ;
604713 log_label = CLIENT_HANDSHAKE_LABEL ;
605- #ifndef OPENSSL_NO_QUIC
606- if (SSL_IS_QUIC (s )) {
607- level = ssl_encryption_handshake ;
608- if (s -> server )
609- s -> quic_read_level = ssl_encryption_handshake ;
610- else
611- s -> quic_write_level = ssl_encryption_handshake ;
612- }
613- #endif
614714 /*
615715 * The handshake hash used for the server read/client write handshake
616716 * traffic secret is the same as the hash for the server
@@ -633,15 +733,6 @@ int tls13_change_cipher_state(SSL *s, int which)
633733 * previously saved value.
634734 */
635735 hash = s -> server_finished_hash ;
636- #ifndef OPENSSL_NO_QUIC
637- if (SSL_IS_QUIC (s )) {
638- level = ssl_encryption_application ; /* ??? */
639- if (s -> server )
640- s -> quic_read_level = ssl_encryption_application ;
641- else
642- s -> quic_write_level = ssl_encryption_application ;
643- }
644- #endif
645736 }
646737 } else {
647738 /* Early data never applies to client-read/server-write */
@@ -652,29 +743,11 @@ int tls13_change_cipher_state(SSL *s, int which)
652743 label = server_handshake_traffic ;
653744 labellen = sizeof (server_handshake_traffic ) - 1 ;
654745 log_label = SERVER_HANDSHAKE_LABEL ;
655- #ifndef OPENSSL_NO_QUIC
656- if (SSL_IS_QUIC (s )) {
657- level = ssl_encryption_handshake ;
658- if (s -> server )
659- s -> quic_write_level = ssl_encryption_handshake ;
660- else
661- s -> quic_read_level = ssl_encryption_handshake ;
662- }
663- #endif
664746 } else {
665747 insecret = s -> master_secret ;
666748 label = server_application_traffic ;
667749 labellen = sizeof (server_application_traffic ) - 1 ;
668750 log_label = SERVER_APPLICATION_LABEL ;
669- #ifndef OPENSSL_NO_QUIC
670- if (SSL_IS_QUIC (s )) {
671- level = ssl_encryption_application ;
672- if (s -> server )
673- s -> quic_write_level = ssl_encryption_application ;
674- else
675- s -> quic_read_level = ssl_encryption_application ;
676- }
677- #endif
678751 }
679752 }
680753
@@ -739,14 +812,6 @@ int tls13_change_cipher_state(SSL *s, int which)
739812 }
740813 } else if (label == client_application_traffic )
741814 memcpy (s -> client_app_traffic_secret , secret , hashlen );
742- #ifndef OPENSSL_NO_QUIC
743- else if (label == client_handshake_traffic )
744- memcpy (s - > client_hand_traffic_secret , secret , hashlen );
745- else if (label == server_handshake_traffic )
746- memcpy (s - > server_hand_traffic_secret , secret , hashlen );
747- else if (label == client_early_traffic )
748- memcpy (s - > client_early_traffic_secret , secret , hashlen );
749- #endif
750815
751816 if (!ssl_log_secret (s , log_label , secret , hashlen )) {
752817 /* SSLfatal() already called */
@@ -765,11 +830,6 @@ int tls13_change_cipher_state(SSL *s, int which)
765830 else
766831 s -> statem .enc_write_state = ENC_WRITE_STATE_VALID ;
767832
768- #ifndef OPENSSL_NO_QUIC
769- if (!quic_set_encryption_secrets (s , level ))
770- goto err ;
771- #endif
772-
773833 ret = 1 ;
774834 err :
775835 OPENSSL_cleanse (secret , sizeof (secret ));
0 commit comments