@@ -1776,15 +1776,16 @@ private static byte[] calculateFinishedHMAC(SecurityParameters securityParameter
17761776 {
17771777 int prfCryptoHashAlgorithm = securityParameters .getPRFCryptoHashAlgorithm ();
17781778 int prfHashLength = securityParameters .getPRFHashLength ();
1779+ boolean isDTLS = isDTLS (securityParameters );
17791780
1780- return calculateFinishedHMAC (prfCryptoHashAlgorithm , prfHashLength , baseKey , transcriptHash );
1781+ return calculateFinishedHMAC (prfCryptoHashAlgorithm , prfHashLength , baseKey , transcriptHash , isDTLS );
17811782 }
17821783
17831784 private static byte [] calculateFinishedHMAC (int prfCryptoHashAlgorithm , int prfHashLength , TlsSecret baseKey ,
1784- byte [] transcriptHash ) throws IOException
1785+ byte [] transcriptHash , boolean isDTLS ) throws IOException
17851786 {
17861787 TlsSecret finishedKey = TlsCryptoUtils .hkdfExpandLabel (baseKey , prfCryptoHashAlgorithm , "finished" , EMPTY_BYTES ,
1787- prfHashLength );
1788+ prfHashLength , isDTLS );
17881789
17891790 try
17901791 {
@@ -1817,19 +1818,19 @@ static TlsSecret calculateMasterSecret(TlsContext context, TlsSecret preMasterSe
18171818 }
18181819
18191820 static byte [] calculatePSKBinder (TlsCrypto crypto , boolean isExternalPSK , int pskCryptoHashAlgorithm ,
1820- TlsSecret earlySecret , byte [] transcriptHash ) throws IOException
1821+ TlsSecret earlySecret , byte [] transcriptHash , boolean isDTLS ) throws IOException
18211822 {
18221823 int prfHashLength = TlsCryptoUtils .getHashOutputSize (pskCryptoHashAlgorithm );
18231824
18241825 String label = isExternalPSK ? "ext binder" : "res binder" ;
18251826 byte [] emptyTranscriptHash = crypto .createHash (pskCryptoHashAlgorithm ).calculateHash ();
18261827
18271828 TlsSecret binderKey = deriveSecret (pskCryptoHashAlgorithm , prfHashLength , earlySecret , label ,
1828- emptyTranscriptHash );
1829+ emptyTranscriptHash , isDTLS );
18291830
18301831 try
18311832 {
1832- return calculateFinishedHMAC (pskCryptoHashAlgorithm , prfHashLength , binderKey , transcriptHash );
1833+ return calculateFinishedHMAC (pskCryptoHashAlgorithm , prfHashLength , binderKey , transcriptHash , isDTLS );
18331834 }
18341835 finally
18351836 {
@@ -2012,7 +2013,21 @@ private static void update13TrafficSecret(TlsContext context, boolean forServer)
20122013 private static TlsSecret update13TrafficSecret (SecurityParameters securityParameters , TlsSecret secret ) throws IOException
20132014 {
20142015 return TlsCryptoUtils .hkdfExpandLabel (secret , securityParameters .getPRFCryptoHashAlgorithm (), "traffic upd" ,
2015- EMPTY_BYTES , securityParameters .getPRFHashLength ());
2016+ EMPTY_BYTES , securityParameters .getPRFHashLength (), isDTLS (securityParameters ));
2017+ }
2018+
2019+ /**
2020+ * Whether the (D)TLS 1.3 key schedule for these security parameters uses the DTLS 1.3 label prefix (RFC 9147
2021+ * 5.9), i.e. whether the negotiated version is a DTLS version.
2022+ */
2023+ private static boolean isDTLS (SecurityParameters securityParameters )
2024+ {
2025+ ProtocolVersion negotiatedVersion = securityParameters .getNegotiatedVersion ();
2026+ if (null == negotiatedVersion )
2027+ {
2028+ throw new IllegalStateException ("(D)TLS 1.3 key derivation before the version is negotiated" );
2029+ }
2030+ return negotiatedVersion .isDTLS ();
20162031 }
20172032
20182033 public static ASN1ObjectIdentifier getOIDForHashAlgorithm (short hashAlgorithm )
@@ -6117,18 +6132,20 @@ static TlsSecret deriveSecret(SecurityParameters securityParameters, TlsSecret s
61176132 int prfCryptoHashAlgorithm = securityParameters .getPRFCryptoHashAlgorithm ();
61186133 int prfHashLength = securityParameters .getPRFHashLength ();
61196134
6120- return deriveSecret (prfCryptoHashAlgorithm , prfHashLength , secret , label , transcriptHash );
6135+ return deriveSecret (prfCryptoHashAlgorithm , prfHashLength , secret , label , transcriptHash ,
6136+ isDTLS (securityParameters ));
61216137 }
61226138
61236139 static TlsSecret deriveSecret (int prfCryptoHashAlgorithm , int prfHashLength , TlsSecret secret , String label ,
6124- byte [] transcriptHash ) throws IOException
6140+ byte [] transcriptHash , boolean isDTLS ) throws IOException
61256141 {
61266142 if (transcriptHash .length != prfHashLength )
61276143 {
61286144 throw new TlsFatalAlert (AlertDescription .internal_error );
61296145 }
61306146
6131- return TlsCryptoUtils .hkdfExpandLabel (secret , prfCryptoHashAlgorithm , label , transcriptHash , prfHashLength );
6147+ return TlsCryptoUtils .hkdfExpandLabel (secret , prfCryptoHashAlgorithm , label , transcriptHash , prfHashLength ,
6148+ isDTLS );
61326149 }
61336150
61346151 static TlsSecret getSessionMasterSecret (TlsCrypto crypto , TlsSecret masterSecret )
@@ -6449,7 +6466,7 @@ static OfferedPsks.SelectedConfig selectPreSharedKey(TlsServerContext serverCont
64496466 }
64506467
64516468 byte [] calculatedBinder = calculatePSKBinder (crypto , isExternalPSK , pskCryptoHashAlgorithm ,
6452- earlySecret , transcriptHash );
6469+ earlySecret , transcriptHash , serverContext . getServerVersion (). isDTLS () );
64536470
64546471 if (!Arrays .constantTimeAreEqual (calculatedBinder , binder ))
64556472 {
0 commit comments