forked from quic-go/quic-go
handshake: use new crypto/tls 0-RTT API (#4953)
* handshake: simplify method signature of cryptoSetup.handleEvent * use the new crypto/tls 0-RTT API
This commit is contained in:
@@ -1,5 +1,10 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// EncryptionLevel is the encryption level
|
||||
// Default value is Unencrypted
|
||||
type EncryptionLevel uint8
|
||||
@@ -28,3 +33,33 @@ func (e EncryptionLevel) String() string {
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
func (e EncryptionLevel) ToTLSEncryptionLevel() tls.QUICEncryptionLevel {
|
||||
switch e {
|
||||
case EncryptionInitial:
|
||||
return tls.QUICEncryptionLevelInitial
|
||||
case EncryptionHandshake:
|
||||
return tls.QUICEncryptionLevelHandshake
|
||||
case Encryption1RTT:
|
||||
return tls.QUICEncryptionLevelApplication
|
||||
case Encryption0RTT:
|
||||
return tls.QUICEncryptionLevelEarly
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpected encryption level: %s", e))
|
||||
}
|
||||
}
|
||||
|
||||
func FromTLSEncryptionLevel(e tls.QUICEncryptionLevel) EncryptionLevel {
|
||||
switch e {
|
||||
case tls.QUICEncryptionLevelInitial:
|
||||
return EncryptionInitial
|
||||
case tls.QUICEncryptionLevelHandshake:
|
||||
return EncryptionHandshake
|
||||
case tls.QUICEncryptionLevelApplication:
|
||||
return Encryption1RTT
|
||||
case tls.QUICEncryptionLevelEarly:
|
||||
return Encryption0RTT
|
||||
default:
|
||||
panic(fmt.Sprintf("unexpect encryption level: %s", e))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package protocol
|
||||
|
||||
import (
|
||||
"crypto/tls"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -10,6 +11,27 @@ func TestEncryptionLevelNonZeroValue(t *testing.T) {
|
||||
require.NotZero(t, EncryptionInitial*EncryptionHandshake*Encryption0RTT*Encryption1RTT)
|
||||
}
|
||||
|
||||
func TestEncryptionLevelConversion(t *testing.T) {
|
||||
testCases := []struct {
|
||||
quicLevel EncryptionLevel
|
||||
tlsLevel tls.QUICEncryptionLevel
|
||||
}{
|
||||
{EncryptionInitial, tls.QUICEncryptionLevelInitial},
|
||||
{EncryptionHandshake, tls.QUICEncryptionLevelHandshake},
|
||||
{Encryption1RTT, tls.QUICEncryptionLevelApplication},
|
||||
{Encryption0RTT, tls.QUICEncryptionLevelEarly},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.quicLevel.String(), func(t *testing.T) {
|
||||
// conversion from QUIC to TLS encryption level
|
||||
require.Equal(t, tc.tlsLevel, tc.quicLevel.ToTLSEncryptionLevel())
|
||||
// conversion from TLS to QUIC encryption level
|
||||
require.Equal(t, tc.quicLevel, FromTLSEncryptionLevel(tc.tlsLevel))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestEncryptionLevelStringRepresentation(t *testing.T) {
|
||||
require.Equal(t, "Initial", EncryptionInitial.String())
|
||||
require.Equal(t, "Handshake", EncryptionHandshake.String())
|
||||
|
||||
Reference in New Issue
Block a user