mirror of
https://github.com/SagerNet/sing-box.git
synced 2024-11-22 08:31:30 +00:00
Import cloudflare tls
This commit is contained in:
parent
ee7e976084
commit
a3bb9c2877
|
@ -7,6 +7,10 @@ linters:
|
||||||
- staticcheck
|
- staticcheck
|
||||||
- paralleltest
|
- paralleltest
|
||||||
|
|
||||||
|
run:
|
||||||
|
skip-dirs:
|
||||||
|
- transport/cloudflaretls
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
# gci:
|
# gci:
|
||||||
# sections:
|
# sections:
|
||||||
|
|
1
go.mod
1
go.mod
|
@ -4,6 +4,7 @@ go 1.18
|
||||||
|
|
||||||
require (
|
require (
|
||||||
berty.tech/go-libtor v1.0.385
|
berty.tech/go-libtor v1.0.385
|
||||||
|
github.com/cloudflare/circl v1.2.1-0.20220831060716-4cf0150356fc
|
||||||
github.com/cretz/bine v0.2.0
|
github.com/cretz/bine v0.2.0
|
||||||
github.com/database64128/tfo-go v1.1.2
|
github.com/database64128/tfo-go v1.1.2
|
||||||
github.com/dustin/go-humanize v1.0.0
|
github.com/dustin/go-humanize v1.0.0
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -10,6 +10,8 @@ github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLj
|
||||||
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
|
||||||
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
|
||||||
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
|
||||||
|
github.com/cloudflare/circl v1.2.1-0.20220831060716-4cf0150356fc h1:307gdRLiZ08dwOIKwc5lAQ19DRFaQQvdhHalyB4Asx8=
|
||||||
|
github.com/cloudflare/circl v1.2.1-0.20220831060716-4cf0150356fc/go.mod h1:+CauBF6R70Jqcyl8N2hC8pAXYbWkGIezuSbuGLtRhnw=
|
||||||
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
|
||||||
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
github.com/cncf/udpa/go v0.0.0-20201120205902-5459f2c99403/go.mod h1:WmhPx2Nbnhtbo57+VJT5O0JRkEi1Wbu0z5j0R8u5Hbk=
|
||||||
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
github.com/cncf/xds/go v0.0.0-20210312221358-fbca930ec8ed/go.mod h1:eXthEFrGJvWHgFFCl3hGmgk+/aYT6PnTQLykKQRLhEs=
|
||||||
|
|
7
transport/cloudflaretls/README.md
Normal file
7
transport/cloudflaretls/README.md
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
# cloudflare-tls
|
||||||
|
|
||||||
|
kanged from https://github.com/cloudflare/go
|
||||||
|
branch: cf
|
||||||
|
commit: 4d2a840e50d2b4316aa19934271832d080c44f7f
|
||||||
|
go: 1.18.5
|
||||||
|
changes: use github.com/cloudflare/circl 4cf0150356fc62a0ea5c0eec2f64b756cb404145
|
101
transport/cloudflaretls/alert.go
Normal file
101
transport/cloudflaretls/alert.go
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
type alert uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
// alert level
|
||||||
|
alertLevelWarning = 1
|
||||||
|
alertLevelError = 2
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
alertCloseNotify alert = 0
|
||||||
|
alertUnexpectedMessage alert = 10
|
||||||
|
alertBadRecordMAC alert = 20
|
||||||
|
alertDecryptionFailed alert = 21
|
||||||
|
alertRecordOverflow alert = 22
|
||||||
|
alertDecompressionFailure alert = 30
|
||||||
|
alertHandshakeFailure alert = 40
|
||||||
|
alertBadCertificate alert = 42
|
||||||
|
alertUnsupportedCertificate alert = 43
|
||||||
|
alertCertificateRevoked alert = 44
|
||||||
|
alertCertificateExpired alert = 45
|
||||||
|
alertCertificateUnknown alert = 46
|
||||||
|
alertIllegalParameter alert = 47
|
||||||
|
alertUnknownCA alert = 48
|
||||||
|
alertAccessDenied alert = 49
|
||||||
|
alertDecodeError alert = 50
|
||||||
|
alertDecryptError alert = 51
|
||||||
|
alertExportRestriction alert = 60
|
||||||
|
alertProtocolVersion alert = 70
|
||||||
|
alertInsufficientSecurity alert = 71
|
||||||
|
alertInternalError alert = 80
|
||||||
|
alertInappropriateFallback alert = 86
|
||||||
|
alertUserCanceled alert = 90
|
||||||
|
alertNoRenegotiation alert = 100
|
||||||
|
alertMissingExtension alert = 109
|
||||||
|
alertUnsupportedExtension alert = 110
|
||||||
|
alertCertificateUnobtainable alert = 111
|
||||||
|
alertUnrecognizedName alert = 112
|
||||||
|
alertBadCertificateStatusResponse alert = 113
|
||||||
|
alertBadCertificateHashValue alert = 114
|
||||||
|
alertUnknownPSKIdentity alert = 115
|
||||||
|
alertCertificateRequired alert = 116
|
||||||
|
alertNoApplicationProtocol alert = 120
|
||||||
|
alertECHRequired alert = 121
|
||||||
|
)
|
||||||
|
|
||||||
|
var alertText = map[alert]string{
|
||||||
|
alertCloseNotify: "close notify",
|
||||||
|
alertUnexpectedMessage: "unexpected message",
|
||||||
|
alertBadRecordMAC: "bad record MAC",
|
||||||
|
alertDecryptionFailed: "decryption failed",
|
||||||
|
alertRecordOverflow: "record overflow",
|
||||||
|
alertDecompressionFailure: "decompression failure",
|
||||||
|
alertHandshakeFailure: "handshake failure",
|
||||||
|
alertBadCertificate: "bad certificate",
|
||||||
|
alertUnsupportedCertificate: "unsupported certificate",
|
||||||
|
alertCertificateRevoked: "revoked certificate",
|
||||||
|
alertCertificateExpired: "expired certificate",
|
||||||
|
alertCertificateUnknown: "unknown certificate",
|
||||||
|
alertIllegalParameter: "illegal parameter",
|
||||||
|
alertUnknownCA: "unknown certificate authority",
|
||||||
|
alertAccessDenied: "access denied",
|
||||||
|
alertDecodeError: "error decoding message",
|
||||||
|
alertDecryptError: "error decrypting message",
|
||||||
|
alertExportRestriction: "export restriction",
|
||||||
|
alertProtocolVersion: "protocol version not supported",
|
||||||
|
alertInsufficientSecurity: "insufficient security level",
|
||||||
|
alertInternalError: "internal error",
|
||||||
|
alertInappropriateFallback: "inappropriate fallback",
|
||||||
|
alertUserCanceled: "user canceled",
|
||||||
|
alertNoRenegotiation: "no renegotiation",
|
||||||
|
alertMissingExtension: "missing extension",
|
||||||
|
alertUnsupportedExtension: "unsupported extension",
|
||||||
|
alertCertificateUnobtainable: "certificate unobtainable",
|
||||||
|
alertUnrecognizedName: "unrecognized name",
|
||||||
|
alertBadCertificateStatusResponse: "bad certificate status response",
|
||||||
|
alertBadCertificateHashValue: "bad certificate hash value",
|
||||||
|
alertUnknownPSKIdentity: "unknown PSK identity",
|
||||||
|
alertCertificateRequired: "certificate required",
|
||||||
|
alertNoApplicationProtocol: "no application protocol",
|
||||||
|
alertECHRequired: "ECH required",
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e alert) String() string {
|
||||||
|
s, ok := alertText[e]
|
||||||
|
if ok {
|
||||||
|
return "tls: " + s
|
||||||
|
}
|
||||||
|
return "tls: alert(" + strconv.Itoa(int(e)) + ")"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e alert) Error() string {
|
||||||
|
return e.String()
|
||||||
|
}
|
345
transport/cloudflaretls/auth.go
Normal file
345
transport/cloudflaretls/auth.go
Normal file
|
@ -0,0 +1,345 @@
|
||||||
|
// Copyright 2017 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/rsa"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
circlPki "github.com/cloudflare/circl/pki"
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// verifyHandshakeSignature verifies a signature against pre-hashed
|
||||||
|
// (if required) handshake contents.
|
||||||
|
func verifyHandshakeSignature(sigType uint8, pubkey crypto.PublicKey, hashFunc crypto.Hash, signed, sig []byte) error {
|
||||||
|
switch sigType {
|
||||||
|
case signatureECDSA:
|
||||||
|
pubKey, ok := pubkey.(*ecdsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected an ECDSA public key, got %T", pubkey)
|
||||||
|
}
|
||||||
|
if !ecdsa.VerifyASN1(pubKey, signed, sig) {
|
||||||
|
return errors.New("ECDSA verification failure")
|
||||||
|
}
|
||||||
|
case signatureEd25519:
|
||||||
|
pubKey, ok := pubkey.(ed25519.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected an Ed25519 public key, got %T", pubkey)
|
||||||
|
}
|
||||||
|
if !ed25519.Verify(pubKey, signed, sig) {
|
||||||
|
return errors.New("Ed25519 verification failure")
|
||||||
|
}
|
||||||
|
case signaturePKCS1v15:
|
||||||
|
pubKey, ok := pubkey.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected an RSA public key, got %T", pubkey)
|
||||||
|
}
|
||||||
|
if err := rsa.VerifyPKCS1v15(pubKey, hashFunc, signed, sig); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
case signatureRSAPSS:
|
||||||
|
pubKey, ok := pubkey.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected an RSA public key, got %T", pubkey)
|
||||||
|
}
|
||||||
|
signOpts := &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash}
|
||||||
|
if err := rsa.VerifyPSS(pubKey, hashFunc, signed, sig, signOpts); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
scheme := circlSchemeBySigType(sigType)
|
||||||
|
if scheme == nil {
|
||||||
|
return errors.New("internal error: unknown signature type")
|
||||||
|
}
|
||||||
|
pubKey, ok := pubkey.(circlSign.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("expected a %s public key, got %T", scheme.Name(), pubkey)
|
||||||
|
}
|
||||||
|
if !scheme.Verify(pubKey, signed, sig, nil) {
|
||||||
|
return fmt.Errorf("%s verification failure", scheme.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
serverSignatureContext = "TLS 1.3, server CertificateVerify\x00"
|
||||||
|
clientSignatureContext = "TLS 1.3, client CertificateVerify\x00"
|
||||||
|
)
|
||||||
|
|
||||||
|
var signaturePadding = []byte{
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
|
||||||
|
}
|
||||||
|
|
||||||
|
// signedMessage returns the pre-hashed (if necessary) message to be signed by
|
||||||
|
// certificate keys in TLS 1.3. See RFC 8446, Section 4.4.3.
|
||||||
|
func signedMessage(sigHash crypto.Hash, context string, transcript hash.Hash) []byte {
|
||||||
|
if sigHash == directSigning {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
b.Write(signaturePadding)
|
||||||
|
io.WriteString(b, context)
|
||||||
|
b.Write(transcript.Sum(nil))
|
||||||
|
return b.Bytes()
|
||||||
|
}
|
||||||
|
h := sigHash.New()
|
||||||
|
h.Write(signaturePadding)
|
||||||
|
io.WriteString(h, context)
|
||||||
|
h.Write(transcript.Sum(nil))
|
||||||
|
return h.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// typeAndHashFromSignatureScheme returns the corresponding signature type and
|
||||||
|
// crypto.Hash for a given TLS SignatureScheme.
|
||||||
|
func typeAndHashFromSignatureScheme(signatureAlgorithm SignatureScheme) (sigType uint8, hash crypto.Hash, err error) {
|
||||||
|
switch signatureAlgorithm {
|
||||||
|
case PKCS1WithSHA1, PKCS1WithSHA256, PKCS1WithSHA384, PKCS1WithSHA512:
|
||||||
|
sigType = signaturePKCS1v15
|
||||||
|
case PSSWithSHA256, PSSWithSHA384, PSSWithSHA512:
|
||||||
|
sigType = signatureRSAPSS
|
||||||
|
case ECDSAWithSHA1, ECDSAWithP256AndSHA256, ECDSAWithP384AndSHA384, ECDSAWithP521AndSHA512:
|
||||||
|
sigType = signatureECDSA
|
||||||
|
case Ed25519:
|
||||||
|
sigType = signatureEd25519
|
||||||
|
default:
|
||||||
|
scheme := circlPki.SchemeByTLSID(uint(signatureAlgorithm))
|
||||||
|
if scheme == nil {
|
||||||
|
return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
|
||||||
|
}
|
||||||
|
sigType = sigTypeByCirclScheme(scheme)
|
||||||
|
if sigType == 0 {
|
||||||
|
return 0, 0, fmt.Errorf("github.com/cloudflare/circl scheme %s not supported",
|
||||||
|
scheme.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
switch signatureAlgorithm {
|
||||||
|
case PKCS1WithSHA1, ECDSAWithSHA1:
|
||||||
|
hash = crypto.SHA1
|
||||||
|
case PKCS1WithSHA256, PSSWithSHA256, ECDSAWithP256AndSHA256:
|
||||||
|
hash = crypto.SHA256
|
||||||
|
case PKCS1WithSHA384, PSSWithSHA384, ECDSAWithP384AndSHA384:
|
||||||
|
hash = crypto.SHA384
|
||||||
|
case PKCS1WithSHA512, PSSWithSHA512, ECDSAWithP521AndSHA512:
|
||||||
|
hash = crypto.SHA512
|
||||||
|
case Ed25519:
|
||||||
|
hash = directSigning
|
||||||
|
default:
|
||||||
|
scheme := circlPki.SchemeByTLSID(uint(signatureAlgorithm))
|
||||||
|
if scheme == nil {
|
||||||
|
return 0, 0, fmt.Errorf("unsupported signature algorithm: %v", signatureAlgorithm)
|
||||||
|
}
|
||||||
|
hash = directSigning
|
||||||
|
}
|
||||||
|
return sigType, hash, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// legacyTypeAndHashFromPublicKey returns the fixed signature type and crypto.Hash for
|
||||||
|
// a given public key used with TLS 1.0 and 1.1, before the introduction of
|
||||||
|
// signature algorithm negotiation.
|
||||||
|
func legacyTypeAndHashFromPublicKey(pub crypto.PublicKey) (sigType uint8, hash crypto.Hash, err error) {
|
||||||
|
switch pub.(type) {
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
return signaturePKCS1v15, crypto.MD5SHA1, nil
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
return signatureECDSA, crypto.SHA1, nil
|
||||||
|
case ed25519.PublicKey:
|
||||||
|
// RFC 8422 specifies support for Ed25519 in TLS 1.0 and 1.1,
|
||||||
|
// but it requires holding on to a handshake transcript to do a
|
||||||
|
// full signature, and not even OpenSSL bothers with the
|
||||||
|
// complexity, so we can't even test it properly.
|
||||||
|
return 0, 0, fmt.Errorf("tls: Ed25519 public keys are not supported before TLS 1.2")
|
||||||
|
case circlSign.PublicKey:
|
||||||
|
return 0, 0, fmt.Errorf("tls: circl public keys are not supported before TLS 1.2")
|
||||||
|
default:
|
||||||
|
return 0, 0, fmt.Errorf("tls: unsupported public key: %T", pub)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var rsaSignatureSchemes = []struct {
|
||||||
|
scheme SignatureScheme
|
||||||
|
minModulusBytes int
|
||||||
|
maxVersion uint16
|
||||||
|
}{
|
||||||
|
// RSA-PSS is used with PSSSaltLengthEqualsHash, and requires
|
||||||
|
// emLen >= hLen + sLen + 2
|
||||||
|
{PSSWithSHA256, crypto.SHA256.Size()*2 + 2, VersionTLS13},
|
||||||
|
{PSSWithSHA384, crypto.SHA384.Size()*2 + 2, VersionTLS13},
|
||||||
|
{PSSWithSHA512, crypto.SHA512.Size()*2 + 2, VersionTLS13},
|
||||||
|
// PKCS #1 v1.5 uses prefixes from hashPrefixes in crypto/rsa, and requires
|
||||||
|
// emLen >= len(prefix) + hLen + 11
|
||||||
|
// TLS 1.3 dropped support for PKCS #1 v1.5 in favor of RSA-PSS.
|
||||||
|
{PKCS1WithSHA256, 19 + crypto.SHA256.Size() + 11, VersionTLS12},
|
||||||
|
{PKCS1WithSHA384, 19 + crypto.SHA384.Size() + 11, VersionTLS12},
|
||||||
|
{PKCS1WithSHA512, 19 + crypto.SHA512.Size() + 11, VersionTLS12},
|
||||||
|
{PKCS1WithSHA1, 15 + crypto.SHA1.Size() + 11, VersionTLS12},
|
||||||
|
}
|
||||||
|
|
||||||
|
// signatureSchemesForCertificate returns the list of supported SignatureSchemes
|
||||||
|
// for a given certificate, based on the public key and the protocol version,
|
||||||
|
// and optionally filtered by its explicit SupportedSignatureAlgorithms.
|
||||||
|
//
|
||||||
|
// This function must be kept in sync with supportedSignatureAlgorithms.
|
||||||
|
func signatureSchemesForCertificate(version uint16, cert *Certificate) []SignatureScheme {
|
||||||
|
priv, ok := cert.PrivateKey.(crypto.Signer)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var sigAlgs []SignatureScheme
|
||||||
|
switch pub := priv.Public().(type) {
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
if version != VersionTLS13 {
|
||||||
|
// In TLS 1.2 and earlier, ECDSA algorithms are not
|
||||||
|
// constrained to a single curve.
|
||||||
|
sigAlgs = []SignatureScheme{
|
||||||
|
ECDSAWithP256AndSHA256,
|
||||||
|
ECDSAWithP384AndSHA384,
|
||||||
|
ECDSAWithP521AndSHA512,
|
||||||
|
ECDSAWithSHA1,
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
switch pub.Curve {
|
||||||
|
case elliptic.P256():
|
||||||
|
sigAlgs = []SignatureScheme{ECDSAWithP256AndSHA256}
|
||||||
|
case elliptic.P384():
|
||||||
|
sigAlgs = []SignatureScheme{ECDSAWithP384AndSHA384}
|
||||||
|
case elliptic.P521():
|
||||||
|
sigAlgs = []SignatureScheme{ECDSAWithP521AndSHA512}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
size := pub.Size()
|
||||||
|
sigAlgs = make([]SignatureScheme, 0, len(rsaSignatureSchemes))
|
||||||
|
for _, candidate := range rsaSignatureSchemes {
|
||||||
|
if size >= candidate.minModulusBytes && version <= candidate.maxVersion {
|
||||||
|
sigAlgs = append(sigAlgs, candidate.scheme)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
case ed25519.PublicKey:
|
||||||
|
sigAlgs = []SignatureScheme{Ed25519}
|
||||||
|
case circlSign.PublicKey:
|
||||||
|
scheme := pub.Scheme()
|
||||||
|
tlsScheme, ok := scheme.(circlPki.TLSScheme)
|
||||||
|
if !ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
sigAlgs = []SignatureScheme{SignatureScheme(tlsScheme.TLSIdentifier())}
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if cert.SupportedSignatureAlgorithms != nil {
|
||||||
|
var filteredSigAlgs []SignatureScheme
|
||||||
|
for _, sigAlg := range sigAlgs {
|
||||||
|
if isSupportedSignatureAlgorithm(sigAlg, cert.SupportedSignatureAlgorithms) {
|
||||||
|
filteredSigAlgs = append(filteredSigAlgs, sigAlg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredSigAlgs
|
||||||
|
}
|
||||||
|
return sigAlgs
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectSignatureSchemeDC picks a SignatureScheme from the peer's preference list
|
||||||
|
// that works with the selected delegated credential. It's only called for protocol
|
||||||
|
// versions that support delegated credential, so TLS 1.3.
|
||||||
|
func selectSignatureSchemeDC(vers uint16, dc *DelegatedCredential, peerAlgs []SignatureScheme, peerAlgsDC []SignatureScheme) (SignatureScheme, error) {
|
||||||
|
if vers != VersionTLS13 {
|
||||||
|
return 0, errors.New("unsupported TLS version for dc")
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isSupportedSignatureAlgorithm(dc.algorithm, peerAlgs) {
|
||||||
|
return undefinedSignatureScheme, errors.New("tls: peer doesn't support the delegated credential's signature")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pick signature scheme in the peer's preference order, as our
|
||||||
|
// preference order is not configurable.
|
||||||
|
for _, preferredAlg := range peerAlgsDC {
|
||||||
|
if preferredAlg == dc.cred.expCertVerfAlgo {
|
||||||
|
return preferredAlg, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, errors.New("tls: peer doesn't support the delegated credential's signature algorithm")
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectSignatureScheme picks a SignatureScheme from the peer's preference list
|
||||||
|
// that works with the selected certificate. It's only called for protocol
|
||||||
|
// versions that support signature algorithms, so TLS 1.2 and 1.3.
|
||||||
|
func selectSignatureScheme(vers uint16, c *Certificate, peerAlgs []SignatureScheme) (SignatureScheme, error) {
|
||||||
|
supportedAlgs := signatureSchemesForCertificate(vers, c)
|
||||||
|
if len(supportedAlgs) == 0 {
|
||||||
|
return 0, unsupportedCertificateError(c)
|
||||||
|
}
|
||||||
|
if len(peerAlgs) == 0 && vers == VersionTLS12 {
|
||||||
|
// For TLS 1.2, if the client didn't send signature_algorithms then we
|
||||||
|
// can assume that it supports SHA1. See RFC 5246, Section 7.4.1.4.1.
|
||||||
|
peerAlgs = []SignatureScheme{PKCS1WithSHA1, ECDSAWithSHA1}
|
||||||
|
}
|
||||||
|
// Pick signature scheme in the peer's preference order, as our
|
||||||
|
// preference order is not configurable.
|
||||||
|
for _, preferredAlg := range peerAlgs {
|
||||||
|
if isSupportedSignatureAlgorithm(preferredAlg, supportedAlgs) {
|
||||||
|
return preferredAlg, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0, errors.New("tls: peer doesn't support any of the certificate's signature algorithms")
|
||||||
|
}
|
||||||
|
|
||||||
|
// unsupportedCertificateError returns a helpful error for certificates with
|
||||||
|
// an unsupported private key.
|
||||||
|
func unsupportedCertificateError(cert *Certificate) error {
|
||||||
|
switch cert.PrivateKey.(type) {
|
||||||
|
case rsa.PrivateKey, ecdsa.PrivateKey:
|
||||||
|
return fmt.Errorf("tls: unsupported certificate: private key is %T, expected *%T",
|
||||||
|
cert.PrivateKey, cert.PrivateKey)
|
||||||
|
case *ed25519.PrivateKey:
|
||||||
|
return fmt.Errorf("tls: unsupported certificate: private key is *ed25519.PrivateKey, expected ed25519.PrivateKey")
|
||||||
|
}
|
||||||
|
|
||||||
|
signer, ok := cert.PrivateKey.(crypto.Signer)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("tls: certificate private key (%T) does not implement crypto.Signer",
|
||||||
|
cert.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch pub := signer.Public().(type) {
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
switch pub.Curve {
|
||||||
|
case elliptic.P256():
|
||||||
|
case elliptic.P384():
|
||||||
|
case elliptic.P521():
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("tls: unsupported certificate curve (%s)", pub.Curve.Params().Name)
|
||||||
|
}
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
return fmt.Errorf("tls: certificate RSA key size too small for supported signature algorithms")
|
||||||
|
case ed25519.PublicKey:
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("tls: unsupported certificate key (%T)", pub)
|
||||||
|
}
|
||||||
|
|
||||||
|
if cert.SupportedSignatureAlgorithms != nil {
|
||||||
|
return fmt.Errorf("tls: peer doesn't support the certificate custom signature algorithms")
|
||||||
|
}
|
||||||
|
|
||||||
|
return fmt.Errorf("tls: internal error: unsupported key (%T)", cert.PrivateKey)
|
||||||
|
}
|
104
transport/cloudflaretls/cfkem.go
Normal file
104
transport/cloudflaretls/cfkem.go
Normal file
|
@ -0,0 +1,104 @@
|
||||||
|
// Copyright 2022 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
//
|
||||||
|
// Glue to add Circl's (post-quantum) hybrid KEMs.
|
||||||
|
//
|
||||||
|
// To enable set CurvePreferences with the desired scheme as the first element:
|
||||||
|
//
|
||||||
|
// import (
|
||||||
|
// "github.com/cloudflare/circl/kem/tls"
|
||||||
|
// "github.com/cloudflare/circl/kem/hybrid"
|
||||||
|
//
|
||||||
|
// [...]
|
||||||
|
//
|
||||||
|
// config.CurvePreferences = []tls.CurveID{
|
||||||
|
// hybrid.X25519Kyber512Draft00().(tls.TLSScheme).TLSCurveID(),
|
||||||
|
// tls.X25519,
|
||||||
|
// tls.P256,
|
||||||
|
// }
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/cloudflare/circl/kem"
|
||||||
|
"github.com/cloudflare/circl/kem/hybrid"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Either ecdheParameters or kem.PrivateKey
|
||||||
|
type clientKeySharePrivate interface{}
|
||||||
|
|
||||||
|
var (
|
||||||
|
X25519Kyber512Draft00 = CurveID(0xfe30)
|
||||||
|
X25519Kyber768Draft00 = CurveID(0xfe31)
|
||||||
|
invalidCurveID = CurveID(0)
|
||||||
|
)
|
||||||
|
|
||||||
|
func kemSchemeKeyToCurveID(s kem.Scheme) CurveID {
|
||||||
|
switch s.Name() {
|
||||||
|
case "Kyber512-X25519":
|
||||||
|
return X25519Kyber512Draft00
|
||||||
|
case "Kyber768-X25519":
|
||||||
|
return X25519Kyber768Draft00
|
||||||
|
default:
|
||||||
|
return invalidCurveID
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract CurveID from clientKeySharePrivate
|
||||||
|
func clientKeySharePrivateCurveID(ks clientKeySharePrivate) CurveID {
|
||||||
|
switch v := ks.(type) {
|
||||||
|
case kem.PrivateKey:
|
||||||
|
ret := kemSchemeKeyToCurveID(v.Scheme())
|
||||||
|
if ret == invalidCurveID {
|
||||||
|
panic("cfkem: internal error: don't know CurveID for this KEM")
|
||||||
|
}
|
||||||
|
return ret
|
||||||
|
case ecdheParameters:
|
||||||
|
return v.CurveID()
|
||||||
|
default:
|
||||||
|
panic("cfkem: internal error: unknown clientKeySharePrivate")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns scheme by CurveID if supported by Circl
|
||||||
|
func curveIdToCirclScheme(id CurveID) kem.Scheme {
|
||||||
|
switch id {
|
||||||
|
case X25519Kyber512Draft00:
|
||||||
|
return hybrid.Kyber512X25519()
|
||||||
|
case X25519Kyber768Draft00:
|
||||||
|
return hybrid.Kyber768X25519()
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a new shared secret and encapsulates it for the packed
|
||||||
|
// public key in ppk using randomness from rnd.
|
||||||
|
func encapsulateForKem(scheme kem.Scheme, rnd io.Reader, ppk []byte) (
|
||||||
|
ct, ss []byte, alert alert, err error,
|
||||||
|
) {
|
||||||
|
pk, err := scheme.UnmarshalBinaryPublicKey(ppk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, alertIllegalParameter, fmt.Errorf("unpack pk: %w", err)
|
||||||
|
}
|
||||||
|
seed := make([]byte, scheme.EncapsulationSeedSize())
|
||||||
|
if _, err := io.ReadFull(rnd, seed); err != nil {
|
||||||
|
return nil, nil, alertInternalError, fmt.Errorf("random: %w", err)
|
||||||
|
}
|
||||||
|
ct, ss, err = scheme.EncapsulateDeterministically(pk, seed)
|
||||||
|
return ct, ss, alertIllegalParameter, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate a new keypair using randomness from rnd.
|
||||||
|
func generateKemKeyPair(scheme kem.Scheme, rnd io.Reader) (
|
||||||
|
kem.PublicKey, kem.PrivateKey, error,
|
||||||
|
) {
|
||||||
|
seed := make([]byte, scheme.SeedSize())
|
||||||
|
if _, err := io.ReadFull(rnd, seed); err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
pk, sk := scheme.DeriveKeyPair(seed)
|
||||||
|
return pk, sk, nil
|
||||||
|
}
|
688
transport/cloudflaretls/cipher_suites.go
Normal file
688
transport/cloudflaretls/cipher_suites.go
Normal file
|
@ -0,0 +1,688 @@
|
||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/des"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/rc4"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/chacha20poly1305"
|
||||||
|
"golang.org/x/sys/cpu"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CipherSuite is a TLS cipher suite. Note that most functions in this package
|
||||||
|
// accept and expose cipher suite IDs instead of this type.
|
||||||
|
type CipherSuite struct {
|
||||||
|
ID uint16
|
||||||
|
Name string
|
||||||
|
|
||||||
|
// Supported versions is the list of TLS protocol versions that can
|
||||||
|
// negotiate this cipher suite.
|
||||||
|
SupportedVersions []uint16
|
||||||
|
|
||||||
|
// Insecure is true if the cipher suite has known security issues
|
||||||
|
// due to its primitives, design, or implementation.
|
||||||
|
Insecure bool
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
supportedUpToTLS12 = []uint16{VersionTLS10, VersionTLS11, VersionTLS12}
|
||||||
|
supportedOnlyTLS12 = []uint16{VersionTLS12}
|
||||||
|
supportedOnlyTLS13 = []uint16{VersionTLS13}
|
||||||
|
)
|
||||||
|
|
||||||
|
// CipherSuites returns a list of cipher suites currently implemented by this
|
||||||
|
// package, excluding those with security issues, which are returned by
|
||||||
|
// InsecureCipherSuites.
|
||||||
|
//
|
||||||
|
// The list is sorted by ID. Note that the default cipher suites selected by
|
||||||
|
// this package might depend on logic that can't be captured by a static list,
|
||||||
|
// and might not match those returned by this function.
|
||||||
|
func CipherSuites() []*CipherSuite {
|
||||||
|
return []*CipherSuite{
|
||||||
|
{TLS_RSA_WITH_AES_128_CBC_SHA, "TLS_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_RSA_WITH_AES_256_CBC_SHA, "TLS_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_RSA_WITH_AES_128_GCM_SHA256, "TLS_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
|
||||||
|
{TLS_RSA_WITH_AES_256_GCM_SHA384, "TLS_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
|
||||||
|
|
||||||
|
{TLS_AES_128_GCM_SHA256, "TLS_AES_128_GCM_SHA256", supportedOnlyTLS13, false},
|
||||||
|
{TLS_AES_256_GCM_SHA384, "TLS_AES_256_GCM_SHA384", supportedOnlyTLS13, false},
|
||||||
|
{TLS_CHACHA20_POLY1305_SHA256, "TLS_CHACHA20_POLY1305_SHA256", supportedOnlyTLS13, false},
|
||||||
|
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA", supportedUpToTLS12, false},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256", supportedOnlyTLS12, false},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384", supportedOnlyTLS12, false},
|
||||||
|
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, "TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256", supportedOnlyTLS12, false},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// InsecureCipherSuites returns a list of cipher suites currently implemented by
|
||||||
|
// this package and which have security issues.
|
||||||
|
//
|
||||||
|
// Most applications should not use the cipher suites in this list, and should
|
||||||
|
// only use those returned by CipherSuites.
|
||||||
|
func InsecureCipherSuites() []*CipherSuite {
|
||||||
|
// This list includes RC4, CBC_SHA256, and 3DES cipher suites. See
|
||||||
|
// cipherSuitesPreferenceOrder for details.
|
||||||
|
return []*CipherSuite{
|
||||||
|
{TLS_RSA_WITH_RC4_128_SHA, "TLS_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
|
||||||
|
{TLS_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
|
||||||
|
{TLS_RSA_WITH_AES_128_CBC_SHA256, "TLS_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
|
||||||
|
{TLS_ECDHE_RSA_WITH_RC4_128_SHA, "TLS_ECDHE_RSA_WITH_RC4_128_SHA", supportedUpToTLS12, true},
|
||||||
|
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA", supportedUpToTLS12, true},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256", supportedOnlyTLS12, true},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CipherSuiteName returns the standard name for the passed cipher suite ID
|
||||||
|
// (e.g. "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256"), or a fallback representation
|
||||||
|
// of the ID value if the cipher suite is not implemented by this package.
|
||||||
|
func CipherSuiteName(id uint16) string {
|
||||||
|
for _, c := range CipherSuites() {
|
||||||
|
if c.ID == id {
|
||||||
|
return c.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, c := range InsecureCipherSuites() {
|
||||||
|
if c.ID == id {
|
||||||
|
return c.Name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Sprintf("0x%04X", id)
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
// suiteECDHE indicates that the cipher suite involves elliptic curve
|
||||||
|
// Diffie-Hellman. This means that it should only be selected when the
|
||||||
|
// client indicates that it supports ECC with a curve and point format
|
||||||
|
// that we're happy with.
|
||||||
|
suiteECDHE = 1 << iota
|
||||||
|
// suiteECSign indicates that the cipher suite involves an ECDSA or
|
||||||
|
// EdDSA signature and therefore may only be selected when the server's
|
||||||
|
// certificate is ECDSA or EdDSA. If this is not set then the cipher suite
|
||||||
|
// is RSA based.
|
||||||
|
suiteECSign
|
||||||
|
// suiteTLS12 indicates that the cipher suite should only be advertised
|
||||||
|
// and accepted when using TLS 1.2.
|
||||||
|
suiteTLS12
|
||||||
|
// suiteSHA384 indicates that the cipher suite uses SHA384 as the
|
||||||
|
// handshake hash.
|
||||||
|
suiteSHA384
|
||||||
|
)
|
||||||
|
|
||||||
|
// A cipherSuite is a TLS 1.0–1.2 cipher suite, and defines the key exchange
|
||||||
|
// mechanism, as well as the cipher+MAC pair or the AEAD.
|
||||||
|
type cipherSuite struct {
|
||||||
|
id uint16
|
||||||
|
// the lengths, in bytes, of the key material needed for each component.
|
||||||
|
keyLen int
|
||||||
|
macLen int
|
||||||
|
ivLen int
|
||||||
|
ka func(version uint16) keyAgreement
|
||||||
|
// flags is a bitmask of the suite* values, above.
|
||||||
|
flags int
|
||||||
|
cipher func(key, iv []byte, isRead bool) any
|
||||||
|
mac func(key []byte) hash.Hash
|
||||||
|
aead func(key, fixedNonce []byte) aead
|
||||||
|
}
|
||||||
|
|
||||||
|
var cipherSuites = []*cipherSuite{ // TODO: replace with a map, since the order doesn't matter.
|
||||||
|
{TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, 32, 0, 12, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadChaCha20Poly1305},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheRSAKA, suiteECDHE | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheRSAKA, suiteECDHE | suiteTLS12, cipherAES, macSHA256, nil},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, ecdheECDSAKA, suiteECDHE | suiteECSign | suiteTLS12, cipherAES, macSHA256, nil},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, 16, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheRSAKA, suiteECDHE, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, 32, 20, 16, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_RSA_WITH_AES_128_GCM_SHA256, 16, 0, 4, rsaKA, suiteTLS12, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_RSA_WITH_AES_256_GCM_SHA384, 32, 0, 4, rsaKA, suiteTLS12 | suiteSHA384, nil, nil, aeadAESGCM},
|
||||||
|
{TLS_RSA_WITH_AES_128_CBC_SHA256, 16, 32, 16, rsaKA, suiteTLS12, cipherAES, macSHA256, nil},
|
||||||
|
{TLS_RSA_WITH_AES_128_CBC_SHA, 16, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_RSA_WITH_AES_256_CBC_SHA, 32, 20, 16, rsaKA, 0, cipherAES, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, ecdheRSAKA, suiteECDHE, cipher3DES, macSHA1, nil},
|
||||||
|
{TLS_RSA_WITH_3DES_EDE_CBC_SHA, 24, 20, 8, rsaKA, 0, cipher3DES, macSHA1, nil},
|
||||||
|
{TLS_RSA_WITH_RC4_128_SHA, 16, 20, 0, rsaKA, 0, cipherRC4, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_RSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheRSAKA, suiteECDHE, cipherRC4, macSHA1, nil},
|
||||||
|
{TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, 16, 20, 0, ecdheECDSAKA, suiteECDHE | suiteECSign, cipherRC4, macSHA1, nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectCipherSuite returns the first TLS 1.0–1.2 cipher suite from ids which
|
||||||
|
// is also in supportedIDs and passes the ok filter.
|
||||||
|
func selectCipherSuite(ids, supportedIDs []uint16, ok func(*cipherSuite) bool) *cipherSuite {
|
||||||
|
for _, id := range ids {
|
||||||
|
candidate := cipherSuiteByID(id)
|
||||||
|
if candidate == nil || !ok(candidate) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, suppID := range supportedIDs {
|
||||||
|
if id == suppID {
|
||||||
|
return candidate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A cipherSuiteTLS13 defines only the pair of the AEAD algorithm and hash
|
||||||
|
// algorithm to be used with HKDF. See RFC 8446, Appendix B.4.
|
||||||
|
type cipherSuiteTLS13 struct {
|
||||||
|
id uint16
|
||||||
|
keyLen int
|
||||||
|
aead func(key, fixedNonce []byte) aead
|
||||||
|
hash crypto.Hash
|
||||||
|
}
|
||||||
|
|
||||||
|
var cipherSuitesTLS13 = []*cipherSuiteTLS13{ // TODO: replace with a map.
|
||||||
|
{TLS_AES_128_GCM_SHA256, 16, aeadAESGCMTLS13, crypto.SHA256},
|
||||||
|
{TLS_CHACHA20_POLY1305_SHA256, 32, aeadChaCha20Poly1305, crypto.SHA256},
|
||||||
|
{TLS_AES_256_GCM_SHA384, 32, aeadAESGCMTLS13, crypto.SHA384},
|
||||||
|
}
|
||||||
|
|
||||||
|
// cipherSuitesPreferenceOrder is the order in which we'll select (on the
|
||||||
|
// server) or advertise (on the client) TLS 1.0–1.2 cipher suites.
|
||||||
|
//
|
||||||
|
// Cipher suites are filtered but not reordered based on the application and
|
||||||
|
// peer's preferences, meaning we'll never select a suite lower in this list if
|
||||||
|
// any higher one is available. This makes it more defensible to keep weaker
|
||||||
|
// cipher suites enabled, especially on the server side where we get the last
|
||||||
|
// word, since there are no known downgrade attacks on cipher suites selection.
|
||||||
|
//
|
||||||
|
// The list is sorted by applying the following priority rules, stopping at the
|
||||||
|
// first (most important) applicable one:
|
||||||
|
//
|
||||||
|
// - Anything else comes before RC4
|
||||||
|
//
|
||||||
|
// RC4 has practically exploitable biases. See https://www.rc4nomore.com.
|
||||||
|
//
|
||||||
|
// - Anything else comes before CBC_SHA256
|
||||||
|
//
|
||||||
|
// SHA-256 variants of the CBC ciphersuites don't implement any Lucky13
|
||||||
|
// countermeasures. See http://www.isg.rhul.ac.uk/tls/Lucky13.html and
|
||||||
|
// https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
|
||||||
|
//
|
||||||
|
// - Anything else comes before 3DES
|
||||||
|
//
|
||||||
|
// 3DES has 64-bit blocks, which makes it fundamentally susceptible to
|
||||||
|
// birthday attacks. See https://sweet32.info.
|
||||||
|
//
|
||||||
|
// - ECDHE comes before anything else
|
||||||
|
//
|
||||||
|
// Once we got the broken stuff out of the way, the most important
|
||||||
|
// property a cipher suite can have is forward secrecy. We don't
|
||||||
|
// implement FFDHE, so that means ECDHE.
|
||||||
|
//
|
||||||
|
// - AEADs come before CBC ciphers
|
||||||
|
//
|
||||||
|
// Even with Lucky13 countermeasures, MAC-then-Encrypt CBC cipher suites
|
||||||
|
// are fundamentally fragile, and suffered from an endless sequence of
|
||||||
|
// padding oracle attacks. See https://eprint.iacr.org/2015/1129,
|
||||||
|
// https://www.imperialviolet.org/2014/12/08/poodleagain.html, and
|
||||||
|
// https://blog.cloudflare.com/yet-another-padding-oracle-in-openssl-cbc-ciphersuites/.
|
||||||
|
//
|
||||||
|
// - AES comes before ChaCha20
|
||||||
|
//
|
||||||
|
// When AES hardware is available, AES-128-GCM and AES-256-GCM are faster
|
||||||
|
// than ChaCha20Poly1305.
|
||||||
|
//
|
||||||
|
// When AES hardware is not available, AES-128-GCM is one or more of: much
|
||||||
|
// slower, way more complex, and less safe (because not constant time)
|
||||||
|
// than ChaCha20Poly1305.
|
||||||
|
//
|
||||||
|
// We use this list if we think both peers have AES hardware, and
|
||||||
|
// cipherSuitesPreferenceOrderNoAES otherwise.
|
||||||
|
//
|
||||||
|
// - AES-128 comes before AES-256
|
||||||
|
//
|
||||||
|
// The only potential advantages of AES-256 are better multi-target
|
||||||
|
// margins, and hypothetical post-quantum properties. Neither apply to
|
||||||
|
// TLS, and AES-256 is slower due to its four extra rounds (which don't
|
||||||
|
// contribute to the advantages above).
|
||||||
|
//
|
||||||
|
// - ECDSA comes before RSA
|
||||||
|
//
|
||||||
|
// The relative order of ECDSA and RSA cipher suites doesn't matter,
|
||||||
|
// as they depend on the certificate. Pick one to get a stable order.
|
||||||
|
var cipherSuitesPreferenceOrder = []uint16{
|
||||||
|
// AEADs w/ ECDHE
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
||||||
|
|
||||||
|
// CBC w/ ECDHE
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
|
||||||
|
// AEADs w/o ECDHE
|
||||||
|
TLS_RSA_WITH_AES_128_GCM_SHA256,
|
||||||
|
TLS_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
|
||||||
|
// CBC w/o ECDHE
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA,
|
||||||
|
TLS_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
|
||||||
|
// 3DES
|
||||||
|
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||||
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||||
|
|
||||||
|
// CBC_SHA256
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
|
||||||
|
// RC4
|
||||||
|
TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
|
||||||
|
TLS_RSA_WITH_RC4_128_SHA,
|
||||||
|
}
|
||||||
|
|
||||||
|
var cipherSuitesPreferenceOrderNoAES = []uint16{
|
||||||
|
// ChaCha20Poly1305
|
||||||
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,
|
||||||
|
|
||||||
|
// AES-GCM w/ ECDHE
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
|
||||||
|
// The rest of cipherSuitesPreferenceOrder.
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
TLS_RSA_WITH_AES_128_GCM_SHA256,
|
||||||
|
TLS_RSA_WITH_AES_256_GCM_SHA384,
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA,
|
||||||
|
TLS_RSA_WITH_AES_256_CBC_SHA,
|
||||||
|
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||||
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
|
||||||
|
TLS_RSA_WITH_RC4_128_SHA,
|
||||||
|
}
|
||||||
|
|
||||||
|
// disabledCipherSuites are not used unless explicitly listed in
|
||||||
|
// Config.CipherSuites. They MUST be at the end of cipherSuitesPreferenceOrder.
|
||||||
|
var disabledCipherSuites = []uint16{
|
||||||
|
// CBC_SHA256
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA256,
|
||||||
|
|
||||||
|
// RC4
|
||||||
|
TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, TLS_ECDHE_RSA_WITH_RC4_128_SHA,
|
||||||
|
TLS_RSA_WITH_RC4_128_SHA,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultCipherSuitesLen = len(cipherSuitesPreferenceOrder) - len(disabledCipherSuites)
|
||||||
|
defaultCipherSuites = cipherSuitesPreferenceOrder[:defaultCipherSuitesLen]
|
||||||
|
)
|
||||||
|
|
||||||
|
// defaultCipherSuitesTLS13 is also the preference order, since there are no
|
||||||
|
// disabled by default TLS 1.3 cipher suites. The same AES vs ChaCha20 logic as
|
||||||
|
// cipherSuitesPreferenceOrder applies.
|
||||||
|
var defaultCipherSuitesTLS13 = []uint16{
|
||||||
|
TLS_AES_128_GCM_SHA256,
|
||||||
|
TLS_AES_256_GCM_SHA384,
|
||||||
|
TLS_CHACHA20_POLY1305_SHA256,
|
||||||
|
}
|
||||||
|
|
||||||
|
var defaultCipherSuitesTLS13NoAES = []uint16{
|
||||||
|
TLS_CHACHA20_POLY1305_SHA256,
|
||||||
|
TLS_AES_128_GCM_SHA256,
|
||||||
|
TLS_AES_256_GCM_SHA384,
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ
|
||||||
|
hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL
|
||||||
|
// Keep in sync with crypto/aes/cipher_s390x.go.
|
||||||
|
hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR &&
|
||||||
|
(cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM)
|
||||||
|
|
||||||
|
hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 ||
|
||||||
|
runtime.GOARCH == "arm64" && hasGCMAsmARM64 ||
|
||||||
|
runtime.GOARCH == "s390x" && hasGCMAsmS390X
|
||||||
|
)
|
||||||
|
|
||||||
|
var aesgcmCiphers = map[uint16]bool{
|
||||||
|
// TLS 1.2
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256: true,
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384: true,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256: true,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384: true,
|
||||||
|
// TLS 1.3
|
||||||
|
TLS_AES_128_GCM_SHA256: true,
|
||||||
|
TLS_AES_256_GCM_SHA384: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
var nonAESGCMAEADCiphers = map[uint16]bool{
|
||||||
|
// TLS 1.2
|
||||||
|
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305: true,
|
||||||
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305: true,
|
||||||
|
// TLS 1.3
|
||||||
|
TLS_CHACHA20_POLY1305_SHA256: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
// aesgcmPreferred returns whether the first known cipher in the preference list
|
||||||
|
// is an AES-GCM cipher, implying the peer has hardware support for it.
|
||||||
|
func aesgcmPreferred(ciphers []uint16) bool {
|
||||||
|
for _, cID := range ciphers {
|
||||||
|
if c := cipherSuiteByID(cID); c != nil {
|
||||||
|
return aesgcmCiphers[cID]
|
||||||
|
}
|
||||||
|
if c := cipherSuiteTLS13ByID(cID); c != nil {
|
||||||
|
return aesgcmCiphers[cID]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func cipherRC4(key, iv []byte, isRead bool) any {
|
||||||
|
cipher, _ := rc4.NewCipher(key)
|
||||||
|
return cipher
|
||||||
|
}
|
||||||
|
|
||||||
|
func cipher3DES(key, iv []byte, isRead bool) any {
|
||||||
|
block, _ := des.NewTripleDESCipher(key)
|
||||||
|
if isRead {
|
||||||
|
return cipher.NewCBCDecrypter(block, iv)
|
||||||
|
}
|
||||||
|
return cipher.NewCBCEncrypter(block, iv)
|
||||||
|
}
|
||||||
|
|
||||||
|
func cipherAES(key, iv []byte, isRead bool) any {
|
||||||
|
block, _ := aes.NewCipher(key)
|
||||||
|
if isRead {
|
||||||
|
return cipher.NewCBCDecrypter(block, iv)
|
||||||
|
}
|
||||||
|
return cipher.NewCBCEncrypter(block, iv)
|
||||||
|
}
|
||||||
|
|
||||||
|
// macSHA1 returns a SHA-1 based constant time MAC.
|
||||||
|
func macSHA1(key []byte) hash.Hash {
|
||||||
|
return hmac.New(newConstantTimeHash(sha1.New), key)
|
||||||
|
}
|
||||||
|
|
||||||
|
// macSHA256 returns a SHA-256 based MAC. This is only supported in TLS 1.2 and
|
||||||
|
// is currently only used in disabled-by-default cipher suites.
|
||||||
|
func macSHA256(key []byte) hash.Hash {
|
||||||
|
return hmac.New(sha256.New, key)
|
||||||
|
}
|
||||||
|
|
||||||
|
type aead interface {
|
||||||
|
cipher.AEAD
|
||||||
|
|
||||||
|
// explicitNonceLen returns the number of bytes of explicit nonce
|
||||||
|
// included in each record. This is eight for older AEADs and
|
||||||
|
// zero for modern ones.
|
||||||
|
explicitNonceLen() int
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
aeadNonceLength = 12
|
||||||
|
noncePrefixLength = 4
|
||||||
|
)
|
||||||
|
|
||||||
|
// prefixNonceAEAD wraps an AEAD and prefixes a fixed portion of the nonce to
|
||||||
|
// each call.
|
||||||
|
type prefixNonceAEAD struct {
|
||||||
|
// nonce contains the fixed part of the nonce in the first four bytes.
|
||||||
|
nonce [aeadNonceLength]byte
|
||||||
|
aead cipher.AEAD
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *prefixNonceAEAD) NonceSize() int { return aeadNonceLength - noncePrefixLength }
|
||||||
|
func (f *prefixNonceAEAD) Overhead() int { return f.aead.Overhead() }
|
||||||
|
func (f *prefixNonceAEAD) explicitNonceLen() int { return f.NonceSize() }
|
||||||
|
|
||||||
|
func (f *prefixNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
|
||||||
|
copy(f.nonce[4:], nonce)
|
||||||
|
return f.aead.Seal(out, f.nonce[:], plaintext, additionalData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *prefixNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
|
||||||
|
copy(f.nonce[4:], nonce)
|
||||||
|
return f.aead.Open(out, f.nonce[:], ciphertext, additionalData)
|
||||||
|
}
|
||||||
|
|
||||||
|
// xoredNonceAEAD wraps an AEAD by XORing in a fixed pattern to the nonce
|
||||||
|
// before each call.
|
||||||
|
type xorNonceAEAD struct {
|
||||||
|
nonceMask [aeadNonceLength]byte
|
||||||
|
aead cipher.AEAD
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *xorNonceAEAD) NonceSize() int { return 8 } // 64-bit sequence number
|
||||||
|
func (f *xorNonceAEAD) Overhead() int { return f.aead.Overhead() }
|
||||||
|
func (f *xorNonceAEAD) explicitNonceLen() int { return 0 }
|
||||||
|
|
||||||
|
func (f *xorNonceAEAD) Seal(out, nonce, plaintext, additionalData []byte) []byte {
|
||||||
|
for i, b := range nonce {
|
||||||
|
f.nonceMask[4+i] ^= b
|
||||||
|
}
|
||||||
|
result := f.aead.Seal(out, f.nonceMask[:], plaintext, additionalData)
|
||||||
|
for i, b := range nonce {
|
||||||
|
f.nonceMask[4+i] ^= b
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *xorNonceAEAD) Open(out, nonce, ciphertext, additionalData []byte) ([]byte, error) {
|
||||||
|
for i, b := range nonce {
|
||||||
|
f.nonceMask[4+i] ^= b
|
||||||
|
}
|
||||||
|
result, err := f.aead.Open(out, f.nonceMask[:], ciphertext, additionalData)
|
||||||
|
for i, b := range nonce {
|
||||||
|
f.nonceMask[4+i] ^= b
|
||||||
|
}
|
||||||
|
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
func aeadAESGCM(key, noncePrefix []byte) aead {
|
||||||
|
if len(noncePrefix) != noncePrefixLength {
|
||||||
|
panic("tls: internal error: wrong nonce length")
|
||||||
|
}
|
||||||
|
aes, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
aead, err := cipher.NewGCM(aes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &prefixNonceAEAD{aead: aead}
|
||||||
|
copy(ret.nonce[:], noncePrefix)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func aeadAESGCMTLS13(key, nonceMask []byte) aead {
|
||||||
|
if len(nonceMask) != aeadNonceLength {
|
||||||
|
panic("tls: internal error: wrong nonce length")
|
||||||
|
}
|
||||||
|
aes, err := aes.NewCipher(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
aead, err := cipher.NewGCM(aes)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &xorNonceAEAD{aead: aead}
|
||||||
|
copy(ret.nonceMask[:], nonceMask)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
func aeadChaCha20Poly1305(key, nonceMask []byte) aead {
|
||||||
|
if len(nonceMask) != aeadNonceLength {
|
||||||
|
panic("tls: internal error: wrong nonce length")
|
||||||
|
}
|
||||||
|
aead, err := chacha20poly1305.New(key)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ret := &xorNonceAEAD{aead: aead}
|
||||||
|
copy(ret.nonceMask[:], nonceMask)
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
type constantTimeHash interface {
|
||||||
|
hash.Hash
|
||||||
|
ConstantTimeSum(b []byte) []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// cthWrapper wraps any hash.Hash that implements ConstantTimeSum, and replaces
|
||||||
|
// with that all calls to Sum. It's used to obtain a ConstantTimeSum-based HMAC.
|
||||||
|
type cthWrapper struct {
|
||||||
|
h constantTimeHash
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *cthWrapper) Size() int { return c.h.Size() }
|
||||||
|
func (c *cthWrapper) BlockSize() int { return c.h.BlockSize() }
|
||||||
|
func (c *cthWrapper) Reset() { c.h.Reset() }
|
||||||
|
func (c *cthWrapper) Write(p []byte) (int, error) { return c.h.Write(p) }
|
||||||
|
func (c *cthWrapper) Sum(b []byte) []byte { return c.h.ConstantTimeSum(b) }
|
||||||
|
|
||||||
|
func newConstantTimeHash(h func() hash.Hash) func() hash.Hash {
|
||||||
|
return func() hash.Hash {
|
||||||
|
return &cthWrapper{h().(constantTimeHash)}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// tls10MAC implements the TLS 1.0 MAC function. RFC 2246, Section 6.2.3.
|
||||||
|
func tls10MAC(h hash.Hash, out, seq, header, data, extra []byte) []byte {
|
||||||
|
h.Reset()
|
||||||
|
h.Write(seq)
|
||||||
|
h.Write(header)
|
||||||
|
h.Write(data)
|
||||||
|
res := h.Sum(out)
|
||||||
|
if extra != nil {
|
||||||
|
h.Write(extra)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func rsaKA(version uint16) keyAgreement {
|
||||||
|
return rsaKeyAgreement{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ecdheECDSAKA(version uint16) keyAgreement {
|
||||||
|
return &ecdheKeyAgreement{
|
||||||
|
isRSA: false,
|
||||||
|
version: version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ecdheRSAKA(version uint16) keyAgreement {
|
||||||
|
return &ecdheKeyAgreement{
|
||||||
|
isRSA: true,
|
||||||
|
version: version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// mutualCipherSuite returns a cipherSuite given a list of supported
|
||||||
|
// ciphersuites and the id requested by the peer.
|
||||||
|
func mutualCipherSuite(have []uint16, want uint16) *cipherSuite {
|
||||||
|
for _, id := range have {
|
||||||
|
if id == want {
|
||||||
|
return cipherSuiteByID(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cipherSuiteByID(id uint16) *cipherSuite {
|
||||||
|
for _, cipherSuite := range cipherSuites {
|
||||||
|
if cipherSuite.id == id {
|
||||||
|
return cipherSuite
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func mutualCipherSuiteTLS13(have []uint16, want uint16) *cipherSuiteTLS13 {
|
||||||
|
for _, id := range have {
|
||||||
|
if id == want {
|
||||||
|
return cipherSuiteTLS13ByID(id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func cipherSuiteTLS13ByID(id uint16) *cipherSuiteTLS13 {
|
||||||
|
for _, cipherSuite := range cipherSuitesTLS13 {
|
||||||
|
if cipherSuite.id == id {
|
||||||
|
return cipherSuite
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// A list of cipher suite IDs that are, or have been, implemented by this
|
||||||
|
// package.
|
||||||
|
//
|
||||||
|
// See https://www.iana.org/assignments/tls-parameters/tls-parameters.xml
|
||||||
|
const (
|
||||||
|
// TLS 1.0 - 1.2 cipher suites.
|
||||||
|
TLS_RSA_WITH_RC4_128_SHA uint16 = 0x0005
|
||||||
|
TLS_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0x000a
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA uint16 = 0x002f
|
||||||
|
TLS_RSA_WITH_AES_256_CBC_SHA uint16 = 0x0035
|
||||||
|
TLS_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0x003c
|
||||||
|
TLS_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0x009c
|
||||||
|
TLS_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0x009d
|
||||||
|
TLS_ECDHE_ECDSA_WITH_RC4_128_SHA uint16 = 0xc007
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA uint16 = 0xc009
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA uint16 = 0xc00a
|
||||||
|
TLS_ECDHE_RSA_WITH_RC4_128_SHA uint16 = 0xc011
|
||||||
|
TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA uint16 = 0xc012
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA uint16 = 0xc013
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA uint16 = 0xc014
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc023
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 uint16 = 0xc027
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02f
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 uint16 = 0xc02b
|
||||||
|
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc030
|
||||||
|
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 uint16 = 0xc02c
|
||||||
|
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca8
|
||||||
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 uint16 = 0xcca9
|
||||||
|
|
||||||
|
// TLS 1.3 cipher suites.
|
||||||
|
TLS_AES_128_GCM_SHA256 uint16 = 0x1301
|
||||||
|
TLS_AES_256_GCM_SHA384 uint16 = 0x1302
|
||||||
|
TLS_CHACHA20_POLY1305_SHA256 uint16 = 0x1303
|
||||||
|
|
||||||
|
// TLS_FALLBACK_SCSV isn't a standard cipher suite but an indicator
|
||||||
|
// that the client is doing version fallback. See RFC 7507.
|
||||||
|
TLS_FALLBACK_SCSV uint16 = 0x5600
|
||||||
|
|
||||||
|
// Legacy names for the corresponding cipher suites with the correct _SHA256
|
||||||
|
// suffix, retained for backward compatibility.
|
||||||
|
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256
|
||||||
|
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305 = TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256
|
||||||
|
)
|
1666
transport/cloudflaretls/common.go
Normal file
1666
transport/cloudflaretls/common.go
Normal file
File diff suppressed because it is too large
Load diff
116
transport/cloudflaretls/common_string.go
Normal file
116
transport/cloudflaretls/common_string.go
Normal file
|
@ -0,0 +1,116 @@
|
||||||
|
// Code generated by "stringer -type=SignatureScheme,CurveID,ClientAuthType -output=common_string.go"; DO NOT EDIT.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import "strconv"
|
||||||
|
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[PKCS1WithSHA256-1025]
|
||||||
|
_ = x[PKCS1WithSHA384-1281]
|
||||||
|
_ = x[PKCS1WithSHA512-1537]
|
||||||
|
_ = x[PSSWithSHA256-2052]
|
||||||
|
_ = x[PSSWithSHA384-2053]
|
||||||
|
_ = x[PSSWithSHA512-2054]
|
||||||
|
_ = x[ECDSAWithP256AndSHA256-1027]
|
||||||
|
_ = x[ECDSAWithP384AndSHA384-1283]
|
||||||
|
_ = x[ECDSAWithP521AndSHA512-1539]
|
||||||
|
_ = x[Ed25519-2055]
|
||||||
|
_ = x[PKCS1WithSHA1-513]
|
||||||
|
_ = x[ECDSAWithSHA1-515]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
_SignatureScheme_name_0 = "PKCS1WithSHA1"
|
||||||
|
_SignatureScheme_name_1 = "ECDSAWithSHA1"
|
||||||
|
_SignatureScheme_name_2 = "PKCS1WithSHA256"
|
||||||
|
_SignatureScheme_name_3 = "ECDSAWithP256AndSHA256"
|
||||||
|
_SignatureScheme_name_4 = "PKCS1WithSHA384"
|
||||||
|
_SignatureScheme_name_5 = "ECDSAWithP384AndSHA384"
|
||||||
|
_SignatureScheme_name_6 = "PKCS1WithSHA512"
|
||||||
|
_SignatureScheme_name_7 = "ECDSAWithP521AndSHA512"
|
||||||
|
_SignatureScheme_name_8 = "PSSWithSHA256PSSWithSHA384PSSWithSHA512Ed25519"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_SignatureScheme_index_8 = [...]uint8{0, 13, 26, 39, 46}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i SignatureScheme) String() string {
|
||||||
|
switch {
|
||||||
|
case i == 513:
|
||||||
|
return _SignatureScheme_name_0
|
||||||
|
case i == 515:
|
||||||
|
return _SignatureScheme_name_1
|
||||||
|
case i == 1025:
|
||||||
|
return _SignatureScheme_name_2
|
||||||
|
case i == 1027:
|
||||||
|
return _SignatureScheme_name_3
|
||||||
|
case i == 1281:
|
||||||
|
return _SignatureScheme_name_4
|
||||||
|
case i == 1283:
|
||||||
|
return _SignatureScheme_name_5
|
||||||
|
case i == 1537:
|
||||||
|
return _SignatureScheme_name_6
|
||||||
|
case i == 1539:
|
||||||
|
return _SignatureScheme_name_7
|
||||||
|
case 2052 <= i && i <= 2055:
|
||||||
|
i -= 2052
|
||||||
|
return _SignatureScheme_name_8[_SignatureScheme_index_8[i]:_SignatureScheme_index_8[i+1]]
|
||||||
|
default:
|
||||||
|
return "SignatureScheme(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[CurveP256-23]
|
||||||
|
_ = x[CurveP384-24]
|
||||||
|
_ = x[CurveP521-25]
|
||||||
|
_ = x[X25519-29]
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
_CurveID_name_0 = "CurveP256CurveP384CurveP521"
|
||||||
|
_CurveID_name_1 = "X25519"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_CurveID_index_0 = [...]uint8{0, 9, 18, 27}
|
||||||
|
)
|
||||||
|
|
||||||
|
func (i CurveID) String() string {
|
||||||
|
switch {
|
||||||
|
case 23 <= i && i <= 25:
|
||||||
|
i -= 23
|
||||||
|
return _CurveID_name_0[_CurveID_index_0[i]:_CurveID_index_0[i+1]]
|
||||||
|
case i == 29:
|
||||||
|
return _CurveID_name_1
|
||||||
|
default:
|
||||||
|
return "CurveID(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
func _() {
|
||||||
|
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||||
|
// Re-run the stringer command to generate them again.
|
||||||
|
var x [1]struct{}
|
||||||
|
_ = x[NoClientCert-0]
|
||||||
|
_ = x[RequestClientCert-1]
|
||||||
|
_ = x[RequireAnyClientCert-2]
|
||||||
|
_ = x[VerifyClientCertIfGiven-3]
|
||||||
|
_ = x[RequireAndVerifyClientCert-4]
|
||||||
|
}
|
||||||
|
|
||||||
|
const _ClientAuthType_name = "NoClientCertRequestClientCertRequireAnyClientCertVerifyClientCertIfGivenRequireAndVerifyClientCert"
|
||||||
|
|
||||||
|
var _ClientAuthType_index = [...]uint8{0, 12, 29, 49, 72, 98}
|
||||||
|
|
||||||
|
func (i ClientAuthType) String() string {
|
||||||
|
if i < 0 || i >= ClientAuthType(len(_ClientAuthType_index)-1) {
|
||||||
|
return "ClientAuthType(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||||
|
}
|
||||||
|
return _ClientAuthType_name[_ClientAuthType_index[i]:_ClientAuthType_index[i+1]]
|
||||||
|
}
|
1603
transport/cloudflaretls/conn.go
Normal file
1603
transport/cloudflaretls/conn.go
Normal file
File diff suppressed because it is too large
Load diff
550
transport/cloudflaretls/delegated_credentials.go
Normal file
550
transport/cloudflaretls/delegated_credentials.go
Normal file
|
@ -0,0 +1,550 @@
|
||||||
|
// Copyright 2020-2021 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
// Delegated Credentials for TLS
|
||||||
|
// (https://tools.ietf.org/html/draft-ietf-tls-subcerts) is an IETF Internet
|
||||||
|
// draft and proposed TLS extension. If the client or server supports this
|
||||||
|
// extension, then the server or client may use a "delegated credential" as the
|
||||||
|
// signing key in the handshake. A delegated credential is a short lived
|
||||||
|
// public/secret key pair delegated to the peer by an entity trusted by the
|
||||||
|
// corresponding peer. This allows a reverse proxy to terminate a TLS connection
|
||||||
|
// on behalf of the entity. Credentials can't be revoked; in order to
|
||||||
|
// mitigate risk in case the reverse proxy is compromised, the credential is only
|
||||||
|
// valid for a short time (days, hours, or even minutes).
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/binary"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// In the absence of an application profile standard specifying otherwise,
|
||||||
|
// the maximum validity period is set to 7 days.
|
||||||
|
dcMaxTTLSeconds = 60 * 60 * 24 * 7
|
||||||
|
dcMaxTTL = time.Duration(dcMaxTTLSeconds * time.Second)
|
||||||
|
dcMaxPubLen = (1 << 24) - 1 // Bytes
|
||||||
|
dcMaxSignatureLen = (1 << 16) - 1 // Bytes
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
undefinedSignatureScheme SignatureScheme = 0x0000
|
||||||
|
)
|
||||||
|
|
||||||
|
var extensionDelegatedCredential = []int{1, 3, 6, 1, 4, 1, 44363, 44}
|
||||||
|
|
||||||
|
// isValidForDelegation returns true if a certificate can be used for Delegated
|
||||||
|
// Credentials.
|
||||||
|
func isValidForDelegation(cert *x509.Certificate) bool {
|
||||||
|
// Check that the digitalSignature key usage is set.
|
||||||
|
// The certificate must contains the digitalSignature KeyUsage.
|
||||||
|
if (cert.KeyUsage & x509.KeyUsageDigitalSignature) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the certificate has the DelegationUsage extension and that
|
||||||
|
// it's marked as non-critical (See Section 4.2 of RFC5280).
|
||||||
|
for _, extension := range cert.Extensions {
|
||||||
|
if extension.Id.Equal(extensionDelegatedCredential) {
|
||||||
|
if extension.Critical {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// isExpired returns true if the credential has expired. The end of the validity
|
||||||
|
// interval is defined as the delegator certificate's notBefore field ('start')
|
||||||
|
// plus dc.cred.validTime seconds. This function simply checks that the current time
|
||||||
|
// ('now') is before the end of the validity interval.
|
||||||
|
func (dc *DelegatedCredential) isExpired(start, now time.Time) bool {
|
||||||
|
end := start.Add(dc.cred.validTime)
|
||||||
|
return !now.Before(end)
|
||||||
|
}
|
||||||
|
|
||||||
|
// invalidTTL returns true if the credential's validity period is longer than the
|
||||||
|
// maximum permitted. This is defined by the certificate's notBefore field
|
||||||
|
// ('start') plus the dc.validTime, minus the current time ('now').
|
||||||
|
func (dc *DelegatedCredential) invalidTTL(start, now time.Time) bool {
|
||||||
|
return dc.cred.validTime > (now.Sub(start) + dcMaxTTL).Round(time.Second)
|
||||||
|
}
|
||||||
|
|
||||||
|
// credential stores the public components of a Delegated Credential.
|
||||||
|
type credential struct {
|
||||||
|
// The amount of time for which the credential is valid. Specifically, the
|
||||||
|
// the credential expires 'validTime' seconds after the 'notBefore' of the
|
||||||
|
// delegation certificate. The delegator shall not issue Delegated
|
||||||
|
// Credentials that are valid for more than 7 days from the current time.
|
||||||
|
//
|
||||||
|
// When this data structure is serialized, this value is converted to a
|
||||||
|
// uint32 representing the duration in seconds.
|
||||||
|
validTime time.Duration
|
||||||
|
// The signature scheme associated with the credential public key.
|
||||||
|
// This is expected to be the same as the CertificateVerify.algorithm
|
||||||
|
// sent by the client or server.
|
||||||
|
expCertVerfAlgo SignatureScheme
|
||||||
|
// The credential's public key.
|
||||||
|
publicKey crypto.PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// DelegatedCredential stores a Delegated Credential with the credential and its
|
||||||
|
// signature.
|
||||||
|
type DelegatedCredential struct {
|
||||||
|
// The serialized form of the Delegated Credential.
|
||||||
|
raw []byte
|
||||||
|
|
||||||
|
// Cred stores the public components of a Delegated Credential.
|
||||||
|
cred *credential
|
||||||
|
|
||||||
|
// The signature scheme used to sign the Delegated Credential.
|
||||||
|
algorithm SignatureScheme
|
||||||
|
|
||||||
|
// The Credential's delegation: a signature that binds the credential to
|
||||||
|
// the end-entity certificate's public key.
|
||||||
|
signature []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshalPublicKeyInfo returns a DER encoded PublicKeyInfo
|
||||||
|
// from a Delegated Credential (as defined in the X.509 standard).
|
||||||
|
// The following key types are currently supported: *ecdsa.PublicKey
|
||||||
|
// and ed25519.PublicKey. Unsupported key types result in an error.
|
||||||
|
// rsa.PublicKey is not supported as defined by the draft.
|
||||||
|
func (cred *credential) marshalPublicKeyInfo() ([]byte, error) {
|
||||||
|
switch cred.expCertVerfAlgo {
|
||||||
|
case ECDSAWithP256AndSHA256,
|
||||||
|
ECDSAWithP384AndSHA384,
|
||||||
|
ECDSAWithP521AndSHA512,
|
||||||
|
Ed25519:
|
||||||
|
rawPub, err := x509.MarshalPKIXPublicKey(cred.publicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return rawPub, nil
|
||||||
|
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("tls: unsupported signature scheme: 0x%04x", cred.expCertVerfAlgo)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// marshal encodes the credential struct of the Delegated Credential.
|
||||||
|
func (cred *credential) marshal() ([]byte, error) {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
|
||||||
|
b.AddUint32(uint32(cred.validTime / time.Second))
|
||||||
|
b.AddUint16(uint16(cred.expCertVerfAlgo))
|
||||||
|
|
||||||
|
// Encode the public key
|
||||||
|
rawPub, err := cred.marshalPublicKeyInfo()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// Assert that the public key encoding is no longer than 2^24-1 bytes.
|
||||||
|
if len(rawPub) > dcMaxPubLen {
|
||||||
|
return nil, errors.New("tls: public key length exceeds 2^24-1 limit")
|
||||||
|
}
|
||||||
|
|
||||||
|
b.AddUint24(uint32(len(rawPub)))
|
||||||
|
b.AddBytes(rawPub)
|
||||||
|
|
||||||
|
raw := b.BytesOrPanic()
|
||||||
|
return raw, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalCredential decodes serialized bytes and returns a credential, if possible.
|
||||||
|
func unmarshalCredential(raw []byte) (*credential, error) {
|
||||||
|
if len(raw) < 10 {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid: invalid length")
|
||||||
|
}
|
||||||
|
|
||||||
|
s := cryptobyte.String(raw)
|
||||||
|
var t uint32
|
||||||
|
if !s.ReadUint32(&t) {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
validTime := time.Duration(t) * time.Second
|
||||||
|
|
||||||
|
var pubAlgo uint16
|
||||||
|
if !s.ReadUint16(&pubAlgo) {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
algo := SignatureScheme(pubAlgo)
|
||||||
|
|
||||||
|
var pubLen uint32
|
||||||
|
s.ReadUint24(&pubLen)
|
||||||
|
|
||||||
|
pubKey, err := x509.ParsePKIXPublicKey(s)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &credential{validTime, algo, pubKey}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getCredentialLen returns the number of bytes comprising the serialized
|
||||||
|
// credential struct inside the Delegated Credential.
|
||||||
|
func getCredentialLen(raw []byte) (int, error) {
|
||||||
|
if len(raw) < 10 {
|
||||||
|
return 0, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
var read []byte
|
||||||
|
s := cryptobyte.String(raw)
|
||||||
|
s.ReadBytes(&read, 6)
|
||||||
|
|
||||||
|
var pubLen uint32
|
||||||
|
s.ReadUint24(&pubLen)
|
||||||
|
if !(pubLen > 0) {
|
||||||
|
return 0, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
raw = raw[6:]
|
||||||
|
if len(raw) < int(pubLen) {
|
||||||
|
return 0, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return 9 + int(pubLen), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// getHash maps the SignatureScheme to its corresponding hash function.
|
||||||
|
func getHash(scheme SignatureScheme) crypto.Hash {
|
||||||
|
switch scheme {
|
||||||
|
case ECDSAWithP256AndSHA256:
|
||||||
|
return crypto.SHA256
|
||||||
|
case ECDSAWithP384AndSHA384:
|
||||||
|
return crypto.SHA384
|
||||||
|
case ECDSAWithP521AndSHA512:
|
||||||
|
return crypto.SHA512
|
||||||
|
case Ed25519:
|
||||||
|
return directSigning
|
||||||
|
case PKCS1WithSHA256, PSSWithSHA256:
|
||||||
|
return crypto.SHA256
|
||||||
|
case PSSWithSHA384:
|
||||||
|
return crypto.SHA384
|
||||||
|
case PSSWithSHA512:
|
||||||
|
return crypto.SHA512
|
||||||
|
default:
|
||||||
|
return 0 // Unknown hash function
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// getECDSACurve maps the SignatureScheme to its corresponding ecdsa elliptic.Curve.
|
||||||
|
func getECDSACurve(scheme SignatureScheme) elliptic.Curve {
|
||||||
|
switch scheme {
|
||||||
|
case ECDSAWithP256AndSHA256:
|
||||||
|
return elliptic.P256()
|
||||||
|
case ECDSAWithP384AndSHA384:
|
||||||
|
return elliptic.P384()
|
||||||
|
case ECDSAWithP521AndSHA512:
|
||||||
|
return elliptic.P521()
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prepareDelegationSignatureInput returns the message that the delegator is going to sign.
|
||||||
|
func prepareDelegationSignatureInput(hash crypto.Hash, cred *credential, dCert []byte, algo SignatureScheme, isClient bool) ([]byte, error) {
|
||||||
|
header := make([]byte, 64)
|
||||||
|
for i := range header {
|
||||||
|
header[i] = 0x20
|
||||||
|
}
|
||||||
|
|
||||||
|
var context string
|
||||||
|
if !isClient {
|
||||||
|
context = "TLS, server delegated credentials\x00"
|
||||||
|
} else {
|
||||||
|
context = "TLS, client delegated credentials\x00"
|
||||||
|
}
|
||||||
|
|
||||||
|
rawCred, err := cred.marshal()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawAlgo [2]byte
|
||||||
|
binary.BigEndian.PutUint16(rawAlgo[:], uint16(algo))
|
||||||
|
|
||||||
|
if hash == directSigning {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
b.Write(header)
|
||||||
|
io.WriteString(b, context)
|
||||||
|
b.Write(dCert)
|
||||||
|
b.Write(rawCred)
|
||||||
|
b.Write(rawAlgo[:])
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
h := hash.New()
|
||||||
|
h.Write(header)
|
||||||
|
io.WriteString(h, context)
|
||||||
|
h.Write(dCert)
|
||||||
|
h.Write(rawCred)
|
||||||
|
h.Write(rawAlgo[:])
|
||||||
|
return h.Sum(nil), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Extract the algorithm used to sign the Delegated Credential from the
|
||||||
|
// end-entity (leaf) certificate.
|
||||||
|
func getSignatureAlgorithm(cert *Certificate) (SignatureScheme, error) {
|
||||||
|
switch sk := cert.PrivateKey.(type) {
|
||||||
|
case *ecdsa.PrivateKey:
|
||||||
|
pk := sk.Public().(*ecdsa.PublicKey)
|
||||||
|
curveName := pk.Curve.Params().Name
|
||||||
|
certAlg := cert.Leaf.PublicKeyAlgorithm
|
||||||
|
if certAlg == x509.ECDSA && curveName == "P-256" {
|
||||||
|
return ECDSAWithP256AndSHA256, nil
|
||||||
|
} else if certAlg == x509.ECDSA && curveName == "P-384" {
|
||||||
|
return ECDSAWithP384AndSHA384, nil
|
||||||
|
} else if certAlg == x509.ECDSA && curveName == "P-521" {
|
||||||
|
return ECDSAWithP521AndSHA512, nil
|
||||||
|
} else {
|
||||||
|
return undefinedSignatureScheme, fmt.Errorf("using curve %s for %s is not supported", curveName, cert.Leaf.SignatureAlgorithm)
|
||||||
|
}
|
||||||
|
case ed25519.PrivateKey:
|
||||||
|
return Ed25519, nil
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
// If the certificate has the RSAEncryption OID there are a number of valid signature schemes that may sign the DC.
|
||||||
|
// In the absence of better information, we make a reasonable choice.
|
||||||
|
return PSSWithSHA256, nil
|
||||||
|
default:
|
||||||
|
return undefinedSignatureScheme, fmt.Errorf("tls: unsupported algorithm for signing Delegated Credential")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewDelegatedCredential creates a new Delegated Credential using 'cert' for
|
||||||
|
// delegation, depending if the caller is the client or the server (defined by
|
||||||
|
// 'isClient'). It generates a public/private key pair for the provided signature
|
||||||
|
// algorithm ('pubAlgo') and it defines a validity interval (defined
|
||||||
|
// by 'cert.Leaf.notBefore' and 'validTime'). It signs the Delegated Credential
|
||||||
|
// using 'cert.PrivateKey'.
|
||||||
|
func NewDelegatedCredential(cert *Certificate, pubAlgo SignatureScheme, validTime time.Duration, isClient bool) (*DelegatedCredential, crypto.PrivateKey, error) {
|
||||||
|
// The granularity of DC validity is seconds.
|
||||||
|
validTime = validTime.Round(time.Second)
|
||||||
|
|
||||||
|
// Parse the leaf certificate if needed.
|
||||||
|
var err error
|
||||||
|
if cert.Leaf == nil {
|
||||||
|
if len(cert.Certificate[0]) == 0 {
|
||||||
|
return nil, nil, errors.New("tls: missing leaf certificate for Delegated Credential")
|
||||||
|
}
|
||||||
|
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that the leaf certificate can be used for delegation.
|
||||||
|
if !isValidForDelegation(cert.Leaf) {
|
||||||
|
return nil, nil, errors.New("tls: certificate not authorized for delegation")
|
||||||
|
}
|
||||||
|
|
||||||
|
sigAlgo, err := getSignatureAlgorithm(cert)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate the Delegated Credential key pair based on the provided scheme
|
||||||
|
var privK crypto.PrivateKey
|
||||||
|
var pubK crypto.PublicKey
|
||||||
|
switch pubAlgo {
|
||||||
|
case ECDSAWithP256AndSHA256,
|
||||||
|
ECDSAWithP384AndSHA384,
|
||||||
|
ECDSAWithP521AndSHA512:
|
||||||
|
privK, err = ecdsa.GenerateKey(getECDSACurve(pubAlgo), rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
pubK = privK.(*ecdsa.PrivateKey).Public()
|
||||||
|
case Ed25519:
|
||||||
|
pubK, privK, err = ed25519.GenerateKey(rand.Reader)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, nil, fmt.Errorf("tls: unsupported algorithm for Delegated Credential: %s", pubAlgo)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare the credential for signing
|
||||||
|
hash := getHash(sigAlgo)
|
||||||
|
credential := &credential{validTime, pubAlgo, pubK}
|
||||||
|
values, err := prepareDelegationSignatureInput(hash, credential, cert.Leaf.Raw, sigAlgo, isClient)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var sig []byte
|
||||||
|
switch sk := cert.PrivateKey.(type) {
|
||||||
|
case *ecdsa.PrivateKey:
|
||||||
|
opts := crypto.SignerOpts(hash)
|
||||||
|
sig, err = sk.Sign(rand.Reader, values, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
case ed25519.PrivateKey:
|
||||||
|
opts := crypto.SignerOpts(hash)
|
||||||
|
sig, err = sk.Sign(rand.Reader, values, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
opts := &rsa.PSSOptions{
|
||||||
|
SaltLength: rsa.PSSSaltLengthEqualsHash,
|
||||||
|
Hash: hash,
|
||||||
|
}
|
||||||
|
sig, err = rsa.SignPSS(rand.Reader, sk, hash, values, opts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return nil, nil, fmt.Errorf("tls: unsupported key type for Delegated Credential")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(sig) > dcMaxSignatureLen {
|
||||||
|
return nil, nil, errors.New("tls: unable to create a Delegated Credential")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DelegatedCredential{
|
||||||
|
cred: credential,
|
||||||
|
algorithm: sigAlgo,
|
||||||
|
signature: sig,
|
||||||
|
}, privK, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate validates the Delegated Credential by checking that the signature is
|
||||||
|
// valid, that it hasn't expired, and that the TTL is valid. It also checks that
|
||||||
|
// certificate can be used for delegation.
|
||||||
|
func (dc *DelegatedCredential) Validate(cert *x509.Certificate, isClient bool, now time.Time, certVerifyMsg *certificateVerifyMsg) bool {
|
||||||
|
if dc.isExpired(cert.NotBefore, now) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if dc.invalidTTL(cert.NotBefore, now) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if dc.cred.expCertVerfAlgo != certVerifyMsg.signatureAlgorithm {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isValidForDelegation(cert) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
hash := getHash(dc.algorithm)
|
||||||
|
in, err := prepareDelegationSignatureInput(hash, dc.cred, cert.Raw, dc.algorithm, isClient)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
switch dc.algorithm {
|
||||||
|
case ECDSAWithP256AndSHA256,
|
||||||
|
ECDSAWithP384AndSHA384,
|
||||||
|
ECDSAWithP521AndSHA512:
|
||||||
|
pk, ok := cert.PublicKey.(*ecdsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ecdsa.VerifyASN1(pk, in, dc.signature)
|
||||||
|
case Ed25519:
|
||||||
|
pk, ok := cert.PublicKey.(ed25519.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return ed25519.Verify(pk, in, dc.signature)
|
||||||
|
case PSSWithSHA256,
|
||||||
|
PSSWithSHA384,
|
||||||
|
PSSWithSHA512:
|
||||||
|
pk, ok := cert.PublicKey.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
hash := getHash(dc.algorithm)
|
||||||
|
return rsa.VerifyPSS(pk, hash, in, dc.signature, nil) == nil
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal encodes a DelegatedCredential structure. It also sets dc.Raw to that
|
||||||
|
// encoding.
|
||||||
|
func (dc *DelegatedCredential) Marshal() ([]byte, error) {
|
||||||
|
if len(dc.signature) > dcMaxSignatureLen {
|
||||||
|
return nil, errors.New("tls: delegated credential is not valid")
|
||||||
|
}
|
||||||
|
if len(dc.signature) == 0 {
|
||||||
|
return nil, errors.New("tls: delegated credential has no signature")
|
||||||
|
}
|
||||||
|
|
||||||
|
raw, err := dc.cred.marshal()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddBytes(raw)
|
||||||
|
b.AddUint16(uint16(dc.algorithm))
|
||||||
|
b.AddUint16(uint16(len(dc.signature)))
|
||||||
|
b.AddBytes(dc.signature)
|
||||||
|
|
||||||
|
dc.raw = b.BytesOrPanic()
|
||||||
|
return dc.raw, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalDelegatedCredential decodes a DelegatedCredential structure.
|
||||||
|
func UnmarshalDelegatedCredential(raw []byte) (*DelegatedCredential, error) {
|
||||||
|
rawCredentialLen, err := getCredentialLen(raw)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
credential, err := unmarshalCredential(raw[:rawCredentialLen])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
raw = raw[rawCredentialLen:]
|
||||||
|
if len(raw) < 4 {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
s := cryptobyte.String(raw)
|
||||||
|
|
||||||
|
var algo uint16
|
||||||
|
if !s.ReadUint16(&algo) {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawSignatureLen uint16
|
||||||
|
if !s.ReadUint16(&rawSignatureLen) {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
var sig []byte
|
||||||
|
if !s.ReadBytes(&sig, int(rawSignatureLen)) {
|
||||||
|
return nil, errors.New("tls: Delegated Credential is not valid")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &DelegatedCredential{
|
||||||
|
cred: credential,
|
||||||
|
algorithm: SignatureScheme(algo),
|
||||||
|
signature: sig,
|
||||||
|
}, nil
|
||||||
|
}
|
1079
transport/cloudflaretls/ech.go
Normal file
1079
transport/cloudflaretls/ech.go
Normal file
File diff suppressed because it is too large
Load diff
164
transport/cloudflaretls/ech_config.go
Normal file
164
transport/cloudflaretls/ech_config.go
Normal file
|
@ -0,0 +1,164 @@
|
||||||
|
// Copyright 2020 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/cloudflare/circl/hpke"
|
||||||
|
"github.com/cloudflare/circl/kem"
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ECHConfig represents an ECH configuration.
|
||||||
|
type ECHConfig struct {
|
||||||
|
pk kem.PublicKey
|
||||||
|
raw []byte
|
||||||
|
|
||||||
|
// Parsed from raw
|
||||||
|
version uint16
|
||||||
|
configId uint8
|
||||||
|
rawPublicName []byte
|
||||||
|
rawPublicKey []byte
|
||||||
|
kemId uint16
|
||||||
|
suites []hpkeSymmetricCipherSuite
|
||||||
|
maxNameLen uint8
|
||||||
|
ignoredExtensions []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// UnmarshalECHConfigs parses a sequence of ECH configurations.
|
||||||
|
func UnmarshalECHConfigs(raw []byte) ([]ECHConfig, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
config ECHConfig
|
||||||
|
t, contents cryptobyte.String
|
||||||
|
)
|
||||||
|
configs := make([]ECHConfig, 0)
|
||||||
|
s := cryptobyte.String(raw)
|
||||||
|
if !s.ReadUint16LengthPrefixed(&t) || !s.Empty() {
|
||||||
|
return configs, errors.New("error parsing configs")
|
||||||
|
}
|
||||||
|
raw = raw[2:]
|
||||||
|
ConfigsLoop:
|
||||||
|
for !t.Empty() {
|
||||||
|
l := len(t)
|
||||||
|
if !t.ReadUint16(&config.version) ||
|
||||||
|
!t.ReadUint16LengthPrefixed(&contents) {
|
||||||
|
return nil, errors.New("error parsing config")
|
||||||
|
}
|
||||||
|
n := l - len(t)
|
||||||
|
config.raw = raw[:n]
|
||||||
|
raw = raw[n:]
|
||||||
|
|
||||||
|
if config.version != extensionECH {
|
||||||
|
continue ConfigsLoop
|
||||||
|
}
|
||||||
|
if !readConfigContents(&contents, &config) {
|
||||||
|
return nil, errors.New("error parsing config contents")
|
||||||
|
}
|
||||||
|
|
||||||
|
kem := hpke.KEM(config.kemId)
|
||||||
|
if !kem.IsValid() {
|
||||||
|
continue ConfigsLoop
|
||||||
|
}
|
||||||
|
config.pk, err = kem.Scheme().UnmarshalBinaryPublicKey(config.rawPublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error parsing public key: %s", err)
|
||||||
|
}
|
||||||
|
configs = append(configs, config)
|
||||||
|
}
|
||||||
|
return configs, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func echMarshalConfigs(configs []ECHConfig) ([]byte, error) {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
for _, config := range configs {
|
||||||
|
if config.raw == nil {
|
||||||
|
panic("config.raw not set")
|
||||||
|
}
|
||||||
|
b.AddBytes(config.raw)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return b.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
func readConfigContents(contents *cryptobyte.String, config *ECHConfig) bool {
|
||||||
|
var t cryptobyte.String
|
||||||
|
if !contents.ReadUint8(&config.configId) ||
|
||||||
|
!contents.ReadUint16(&config.kemId) ||
|
||||||
|
!contents.ReadUint16LengthPrefixed(&t) ||
|
||||||
|
!t.ReadBytes(&config.rawPublicKey, len(t)) ||
|
||||||
|
!contents.ReadUint16LengthPrefixed(&t) ||
|
||||||
|
len(t)%4 != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
config.suites = nil
|
||||||
|
for !t.Empty() {
|
||||||
|
var kdfId, aeadId uint16
|
||||||
|
if !t.ReadUint16(&kdfId) || !t.ReadUint16(&aeadId) {
|
||||||
|
// This indicates an internal bug.
|
||||||
|
panic("internal error while parsing contents.cipher_suites")
|
||||||
|
}
|
||||||
|
config.suites = append(config.suites, hpkeSymmetricCipherSuite{kdfId, aeadId})
|
||||||
|
}
|
||||||
|
|
||||||
|
if !contents.ReadUint8(&config.maxNameLen) ||
|
||||||
|
!contents.ReadUint8LengthPrefixed(&t) ||
|
||||||
|
!t.ReadBytes(&config.rawPublicName, len(t)) ||
|
||||||
|
!contents.ReadUint16LengthPrefixed(&t) ||
|
||||||
|
!t.ReadBytes(&config.ignoredExtensions, len(t)) ||
|
||||||
|
!contents.Empty() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupSealer generates the client's HPKE context for use with the ECH
|
||||||
|
// extension. It returns the context and corresponding encapsulated key.
|
||||||
|
func (config *ECHConfig) setupSealer(rand io.Reader) (enc []byte, sealer hpke.Sealer, err error) {
|
||||||
|
if config.raw == nil {
|
||||||
|
panic("config.raw not set")
|
||||||
|
}
|
||||||
|
hpkeSuite, err := config.selectSuite()
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
info := append(append([]byte(echHpkeInfoSetup), 0), config.raw...)
|
||||||
|
sender, err := hpkeSuite.NewSender(config.pk, info)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
return sender.Setup(rand)
|
||||||
|
}
|
||||||
|
|
||||||
|
// isPeerCipherSuiteSupported returns true if this configuration indicates
|
||||||
|
// support for the given ciphersuite.
|
||||||
|
func (config *ECHConfig) isPeerCipherSuiteSupported(suite hpkeSymmetricCipherSuite) bool {
|
||||||
|
for _, configSuite := range config.suites {
|
||||||
|
if suite == configSuite {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// selectSuite returns the first ciphersuite indicated by this
|
||||||
|
// configuration that is supported by the caller.
|
||||||
|
func (config *ECHConfig) selectSuite() (hpke.Suite, error) {
|
||||||
|
for _, suite := range config.suites {
|
||||||
|
hpkeSuite, err := hpkeAssembleSuite(
|
||||||
|
config.kemId,
|
||||||
|
suite.kdfId,
|
||||||
|
suite.aeadId,
|
||||||
|
)
|
||||||
|
if err == nil {
|
||||||
|
return hpkeSuite, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return hpke.Suite{}, errors.New("could not negotiate a ciphersuite")
|
||||||
|
}
|
302
transport/cloudflaretls/ech_provider.go
Normal file
302
transport/cloudflaretls/ech_provider.go
Normal file
|
@ -0,0 +1,302 @@
|
||||||
|
// Copyright 2020 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cloudflare/circl/hpke"
|
||||||
|
"github.com/cloudflare/circl/kem"
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ECHProvider specifies the interface of an ECH service provider that decrypts
|
||||||
|
// the ECH payload on behalf of the client-facing server. It also defines the
|
||||||
|
// set of acceptable ECH configurations.
|
||||||
|
type ECHProvider interface {
|
||||||
|
// GetDecryptionContext attempts to construct the HPKE context used by the
|
||||||
|
// client-facing server for decryption. (See draft-irtf-cfrg-hpke-07,
|
||||||
|
// Section 5.2.)
|
||||||
|
//
|
||||||
|
// handle encodes the parameters of the client's "encrypted_client_hello"
|
||||||
|
// extension that are needed to construct the context. Since
|
||||||
|
// draft-ietf-tls-esni-10 these are the ECH cipher suite, the identity of
|
||||||
|
// the ECH configuration, and the encapsulated key.
|
||||||
|
//
|
||||||
|
// version is the version of ECH indicated by the client.
|
||||||
|
//
|
||||||
|
// res.Status == ECHProviderStatusSuccess indicates the call was successful
|
||||||
|
// and the caller may proceed. res.Context is set.
|
||||||
|
//
|
||||||
|
// res.Status == ECHProviderStatusReject indicates the caller must reject
|
||||||
|
// ECH. res.RetryConfigs may be set.
|
||||||
|
//
|
||||||
|
// res.Status == ECHProviderStatusAbort indicates the caller should abort
|
||||||
|
// the handshake. Note that, in some cases, it's appropriate to reject
|
||||||
|
// rather than abort. In particular, aborting with "illegal_parameter" might
|
||||||
|
// "stick out". res.Alert and res.Error are set.
|
||||||
|
GetDecryptionContext(handle []byte, version uint16) (res ECHProviderResult)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ECHProviderStatus is the status of the ECH provider's response.
|
||||||
|
type ECHProviderStatus uint
|
||||||
|
|
||||||
|
const (
|
||||||
|
ECHProviderSuccess ECHProviderStatus = 0
|
||||||
|
ECHProviderReject = 1
|
||||||
|
ECHProviderAbort = 2
|
||||||
|
|
||||||
|
errHPKEInvalidPublicKey = "hpke: invalid KEM public key"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ECHProviderResult represents the result of invoking the ECH provider.
|
||||||
|
type ECHProviderResult struct {
|
||||||
|
Status ECHProviderStatus
|
||||||
|
|
||||||
|
// Alert is the TLS alert sent by the caller when aborting the handshake.
|
||||||
|
Alert uint8
|
||||||
|
|
||||||
|
// Error is the error propagated by the caller when aborting the handshake.
|
||||||
|
Error error
|
||||||
|
|
||||||
|
// RetryConfigs is the sequence of ECH configs to offer to the client for
|
||||||
|
// retrying the handshake. This may be set in case of success or rejection.
|
||||||
|
RetryConfigs []byte
|
||||||
|
|
||||||
|
// Context is the server's HPKE context. This is set if ECH is not rejected
|
||||||
|
// by the provider and no error was reported. The data has the following
|
||||||
|
// format (in TLS syntax):
|
||||||
|
//
|
||||||
|
// enum { sealer(0), opener(1) } HpkeRole;
|
||||||
|
//
|
||||||
|
// struct {
|
||||||
|
// HpkeRole role;
|
||||||
|
// HpkeKemId kem_id; // as defined in draft-irtf-cfrg-hpke-07
|
||||||
|
// HpkeKdfId kdf_id; // as defined in draft-irtf-cfrg-hpke-07
|
||||||
|
// HpkeAeadId aead_id; // as defined in draft-irtf-cfrg-hpke-07
|
||||||
|
// opaque exporter_secret<0..255>;
|
||||||
|
// opaque key<0..255>;
|
||||||
|
// opaque base_nonce<0..255>;
|
||||||
|
// opaque seq<0..255>;
|
||||||
|
// } HpkeContext;
|
||||||
|
Context []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXP_ECHKeySet implements the ECHProvider interface for a sequence of ECH keys.
|
||||||
|
//
|
||||||
|
// NOTE: This API is EXPERIMENTAL and subject to change.
|
||||||
|
type EXP_ECHKeySet struct {
|
||||||
|
// The serialized ECHConfigs, in order of the server's preference.
|
||||||
|
configs []byte
|
||||||
|
|
||||||
|
// Maps a configuration identifier to its secret key.
|
||||||
|
sk map[uint8]EXP_ECHKey
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXP_NewECHKeySet constructs an EXP_ECHKeySet.
|
||||||
|
func EXP_NewECHKeySet(keys []EXP_ECHKey) (*EXP_ECHKeySet, error) {
|
||||||
|
if len(keys) > 255 {
|
||||||
|
return nil, fmt.Errorf("tls: ech provider: unable to support more than 255 ECH configurations at once")
|
||||||
|
}
|
||||||
|
|
||||||
|
keySet := new(EXP_ECHKeySet)
|
||||||
|
keySet.sk = make(map[uint8]EXP_ECHKey)
|
||||||
|
configs := make([]byte, 0)
|
||||||
|
for _, key := range keys {
|
||||||
|
if _, ok := keySet.sk[key.config.configId]; ok {
|
||||||
|
return nil, fmt.Errorf("tls: ech provider: ECH config conflict for configId %d", key.config.configId)
|
||||||
|
}
|
||||||
|
|
||||||
|
keySet.sk[key.config.configId] = key
|
||||||
|
configs = append(configs, key.config.raw...)
|
||||||
|
}
|
||||||
|
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(configs)
|
||||||
|
})
|
||||||
|
keySet.configs = b.BytesOrPanic()
|
||||||
|
|
||||||
|
return keySet, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetDecryptionContext is required by the ECHProvider interface.
|
||||||
|
func (keySet *EXP_ECHKeySet) GetDecryptionContext(rawHandle []byte, version uint16) (res ECHProviderResult) {
|
||||||
|
// Propagate retry configurations regardless of the result. The caller sends
|
||||||
|
// these to the clients only if it rejects.
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
|
||||||
|
// Ensure we know how to proceed, i.e., the caller has indicated a supported
|
||||||
|
// version of ECH. Currently only draft-ietf-tls-esni-13 is supported.
|
||||||
|
if version != extensionECH {
|
||||||
|
res.Status = ECHProviderAbort
|
||||||
|
res.Alert = uint8(alertInternalError)
|
||||||
|
res.Error = errors.New("version not supported")
|
||||||
|
return // Abort
|
||||||
|
}
|
||||||
|
|
||||||
|
// Parse the handle.
|
||||||
|
s := cryptobyte.String(rawHandle)
|
||||||
|
handle := new(echContextHandle)
|
||||||
|
if !echReadContextHandle(&s, handle) || !s.Empty() {
|
||||||
|
// This is the result of a client-side error. However, aborting with
|
||||||
|
// "illegal_parameter" would stick out, so we reject instead.
|
||||||
|
res.Status = ECHProviderReject
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
return // Reject
|
||||||
|
}
|
||||||
|
handle.raw = rawHandle
|
||||||
|
|
||||||
|
// Look up the secret key for the configuration indicated by the client.
|
||||||
|
key, ok := keySet.sk[handle.configId]
|
||||||
|
if !ok {
|
||||||
|
res.Status = ECHProviderReject
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
return // Reject
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure that support for the selected ciphersuite is indicated by the
|
||||||
|
// configuration.
|
||||||
|
suite := handle.suite
|
||||||
|
if !key.config.isPeerCipherSuiteSupported(suite) {
|
||||||
|
// This is the result of a client-side error. However, aborting with
|
||||||
|
// "illegal_parameter" would stick out, so we reject instead.
|
||||||
|
res.Status = ECHProviderReject
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
return // Reject
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure the version indicated by the client matches the version supported
|
||||||
|
// by the configuration.
|
||||||
|
if version != key.config.version {
|
||||||
|
// This is the result of a client-side error. However, aborting with
|
||||||
|
// "illegal_parameter" would stick out, so we reject instead.
|
||||||
|
res.Status = ECHProviderReject
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
return // Reject
|
||||||
|
}
|
||||||
|
|
||||||
|
// Compute the decryption context.
|
||||||
|
opener, err := key.setupOpener(handle.enc, suite)
|
||||||
|
if err != nil {
|
||||||
|
if err.Error() == errHPKEInvalidPublicKey {
|
||||||
|
// This occurs if the KEM algorithm used to generate handle.enc is
|
||||||
|
// not the same as the KEM algorithm of the key. One way this can
|
||||||
|
// happen is if the client sent a GREASE ECH extension with a
|
||||||
|
// config_id that happens to match a known config, but which uses a
|
||||||
|
// different KEM algorithm.
|
||||||
|
res.Status = ECHProviderReject
|
||||||
|
res.RetryConfigs = keySet.configs
|
||||||
|
return // Reject
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Status = ECHProviderAbort
|
||||||
|
res.Alert = uint8(alertInternalError)
|
||||||
|
res.Error = err
|
||||||
|
return // Abort
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialize the decryption context.
|
||||||
|
res.Context, err = opener.MarshalBinary()
|
||||||
|
if err != nil {
|
||||||
|
res.Status = ECHProviderAbort
|
||||||
|
res.Alert = uint8(alertInternalError)
|
||||||
|
res.Error = err
|
||||||
|
return // Abort
|
||||||
|
}
|
||||||
|
|
||||||
|
res.Status = ECHProviderSuccess
|
||||||
|
return // Success
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXP_ECHKey represents an ECH key and its corresponding configuration. The
|
||||||
|
// encoding of an ECH Key has the format defined below (in TLS syntax). Note
|
||||||
|
// that the ECH standard does not specify this format.
|
||||||
|
//
|
||||||
|
// struct {
|
||||||
|
// opaque sk<0..2^16-1>;
|
||||||
|
// ECHConfig config<0..2^16>; // draft-ietf-tls-esni-13
|
||||||
|
// } ECHKey;
|
||||||
|
type EXP_ECHKey struct {
|
||||||
|
sk kem.PrivateKey
|
||||||
|
config ECHConfig
|
||||||
|
}
|
||||||
|
|
||||||
|
// EXP_UnmarshalECHKeys parses a sequence of ECH keys.
|
||||||
|
func EXP_UnmarshalECHKeys(raw []byte) ([]EXP_ECHKey, error) {
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
key EXP_ECHKey
|
||||||
|
sk, config, contents cryptobyte.String
|
||||||
|
)
|
||||||
|
s := cryptobyte.String(raw)
|
||||||
|
keys := make([]EXP_ECHKey, 0)
|
||||||
|
KeysLoop:
|
||||||
|
for !s.Empty() {
|
||||||
|
if !s.ReadUint16LengthPrefixed(&sk) ||
|
||||||
|
!s.ReadUint16LengthPrefixed(&config) {
|
||||||
|
return nil, errors.New("error parsing key")
|
||||||
|
}
|
||||||
|
|
||||||
|
key.config.raw = config
|
||||||
|
if !config.ReadUint16(&key.config.version) ||
|
||||||
|
!config.ReadUint16LengthPrefixed(&contents) ||
|
||||||
|
!config.Empty() {
|
||||||
|
return nil, errors.New("error parsing config")
|
||||||
|
}
|
||||||
|
|
||||||
|
if key.config.version != extensionECH {
|
||||||
|
continue KeysLoop
|
||||||
|
}
|
||||||
|
if !readConfigContents(&contents, &key.config) {
|
||||||
|
return nil, errors.New("error parsing config contents")
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, suite := range key.config.suites {
|
||||||
|
if !hpke.KDF(suite.kdfId).IsValid() ||
|
||||||
|
!hpke.AEAD(suite.aeadId).IsValid() {
|
||||||
|
continue KeysLoop
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
kem := hpke.KEM(key.config.kemId)
|
||||||
|
if !kem.IsValid() {
|
||||||
|
continue KeysLoop
|
||||||
|
}
|
||||||
|
key.config.pk, err = kem.Scheme().UnmarshalBinaryPublicKey(key.config.rawPublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error parsing public key: %s", err)
|
||||||
|
}
|
||||||
|
key.sk, err = kem.Scheme().UnmarshalBinaryPrivateKey(sk)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("error parsing secret key: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
keys = append(keys, key)
|
||||||
|
}
|
||||||
|
return keys, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// setupOpener computes the HPKE context used by the server in the ECH
|
||||||
|
// extension.i
|
||||||
|
func (key *EXP_ECHKey) setupOpener(enc []byte, suite hpkeSymmetricCipherSuite) (hpke.Opener, error) {
|
||||||
|
if key.config.raw == nil {
|
||||||
|
panic("raw config not set")
|
||||||
|
}
|
||||||
|
hpkeSuite, err := hpkeAssembleSuite(
|
||||||
|
key.config.kemId,
|
||||||
|
suite.kdfId,
|
||||||
|
suite.aeadId,
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
info := append(append([]byte(echHpkeInfoSetup), 0), key.config.raw...)
|
||||||
|
receiver, err := hpkeSuite.NewReceiver(key.sk, info)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return receiver.Setup(enc)
|
||||||
|
}
|
194
transport/cloudflaretls/generate_cert.go
Normal file
194
transport/cloudflaretls/generate_cert.go
Normal file
|
@ -0,0 +1,194 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:build ignore
|
||||||
|
|
||||||
|
// Generate a self-signed X.509 certificate for a TLS server. Outputs to
|
||||||
|
// 'cert.pem' and 'key.pem' and will overwrite existing files.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/rand"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"crypto/x509/pkix"
|
||||||
|
"encoding/pem"
|
||||||
|
"flag"
|
||||||
|
"log"
|
||||||
|
"math/big"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
circlSchemes "github.com/cloudflare/circl/sign/schemes"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
host = flag.String("host", "", "Comma-separated hostnames and IPs to generate a certificate for")
|
||||||
|
validFrom = flag.String("start-date", "", "Creation date formatted as Jan 1 15:04:05 2011")
|
||||||
|
validFor = flag.Duration("duration", 365*24*time.Hour, "Duration that certificate is valid for")
|
||||||
|
isCA = flag.Bool("ca", false, "whether this cert should be its own Certificate Authority")
|
||||||
|
allowDC = flag.Bool("allowDC", false, "whether this cert can be used with Delegated Credentials")
|
||||||
|
rsaBits = flag.Int("rsa-bits", 2048, "Size of RSA key to generate. Ignored if --ecdsa-curve is set")
|
||||||
|
ecdsaCurve = flag.String("ecdsa-curve", "", "ECDSA curve to use to generate a key. Valid values are P224, P256 (recommended), P384, P521")
|
||||||
|
ed25519Key = flag.Bool("ed25519", false, "Generate an Ed25519 key")
|
||||||
|
circlKey = flag.String("github.com/cloudflare/circl", "", "Generate a key supported by Circl")
|
||||||
|
)
|
||||||
|
|
||||||
|
func publicKey(priv any) any {
|
||||||
|
switch k := priv.(type) {
|
||||||
|
case *rsa.PrivateKey:
|
||||||
|
return &k.PublicKey
|
||||||
|
case *ecdsa.PrivateKey:
|
||||||
|
return &k.PublicKey
|
||||||
|
case ed25519.PrivateKey:
|
||||||
|
return k.Public().(ed25519.PublicKey)
|
||||||
|
case circlSign.PrivateKey:
|
||||||
|
return k.Public()
|
||||||
|
default:
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
|
||||||
|
if len(*host) == 0 {
|
||||||
|
log.Fatalf("Missing required --host parameter")
|
||||||
|
}
|
||||||
|
|
||||||
|
var priv any
|
||||||
|
var err error
|
||||||
|
switch *ecdsaCurve {
|
||||||
|
case "":
|
||||||
|
if *ed25519Key {
|
||||||
|
_, priv, err = ed25519.GenerateKey(rand.Reader)
|
||||||
|
} else if *circlKey != "" {
|
||||||
|
scheme := circlSchemes.ByName(*circlKey)
|
||||||
|
if scheme == nil {
|
||||||
|
log.Fatalf("No such Circl scheme: %s", *circlKey)
|
||||||
|
}
|
||||||
|
_, priv, err = scheme.GenerateKey()
|
||||||
|
} else {
|
||||||
|
priv, err = rsa.GenerateKey(rand.Reader, *rsaBits)
|
||||||
|
}
|
||||||
|
case "P224":
|
||||||
|
priv, err = ecdsa.GenerateKey(elliptic.P224(), rand.Reader)
|
||||||
|
case "P256":
|
||||||
|
priv, err = ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
||||||
|
case "P384":
|
||||||
|
priv, err = ecdsa.GenerateKey(elliptic.P384(), rand.Reader)
|
||||||
|
case "P521":
|
||||||
|
priv, err = ecdsa.GenerateKey(elliptic.P521(), rand.Reader)
|
||||||
|
default:
|
||||||
|
log.Fatalf("Unrecognized elliptic curve: %q", *ecdsaCurve)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to generate private key: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ECDSA, ED25519 and RSA subject keys should have the DigitalSignature
|
||||||
|
// KeyUsage bits set in the x509.Certificate template
|
||||||
|
keyUsage := x509.KeyUsageDigitalSignature
|
||||||
|
// Only RSA subject keys should have the KeyEncipherment KeyUsage bits set. In
|
||||||
|
// the context of TLS this KeyUsage is particular to RSA key exchange and
|
||||||
|
// authentication.
|
||||||
|
if _, isRSA := priv.(*rsa.PrivateKey); isRSA {
|
||||||
|
keyUsage |= x509.KeyUsageKeyEncipherment
|
||||||
|
}
|
||||||
|
|
||||||
|
var notBefore time.Time
|
||||||
|
if len(*validFrom) == 0 {
|
||||||
|
notBefore = time.Now()
|
||||||
|
} else {
|
||||||
|
notBefore, err = time.Parse("Jan 2 15:04:05 2006", *validFrom)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to parse creation date: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
notAfter := notBefore.Add(*validFor)
|
||||||
|
|
||||||
|
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||||
|
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to generate serial number: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
template := x509.Certificate{
|
||||||
|
SerialNumber: serialNumber,
|
||||||
|
Subject: pkix.Name{
|
||||||
|
Organization: []string{"Acme Co"},
|
||||||
|
},
|
||||||
|
NotBefore: notBefore,
|
||||||
|
NotAfter: notAfter,
|
||||||
|
|
||||||
|
KeyUsage: keyUsage,
|
||||||
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||||
|
BasicConstraintsValid: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
hosts := strings.Split(*host, ",")
|
||||||
|
for _, h := range hosts {
|
||||||
|
if ip := net.ParseIP(h); ip != nil {
|
||||||
|
template.IPAddresses = append(template.IPAddresses, ip)
|
||||||
|
} else {
|
||||||
|
template.DNSNames = append(template.DNSNames, h)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if *isCA {
|
||||||
|
if *allowDC {
|
||||||
|
log.Fatal("Failed to create certificate: ca is not allowed with the dc flag")
|
||||||
|
}
|
||||||
|
|
||||||
|
template.IsCA = true
|
||||||
|
template.KeyUsage |= x509.KeyUsageCertSign
|
||||||
|
}
|
||||||
|
|
||||||
|
if *allowDC {
|
||||||
|
template.AllowDC = true
|
||||||
|
template.KeyUsage |= x509.KeyUsageDigitalSignature
|
||||||
|
}
|
||||||
|
|
||||||
|
derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, publicKey(priv), priv)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create certificate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
certOut, err := os.Create("cert.pem")
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open cert.pem for writing: %v", err)
|
||||||
|
}
|
||||||
|
if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes}); err != nil {
|
||||||
|
log.Fatalf("Failed to write data to cert.pem: %v", err)
|
||||||
|
}
|
||||||
|
if err := certOut.Close(); err != nil {
|
||||||
|
log.Fatalf("Error closing cert.pem: %v", err)
|
||||||
|
}
|
||||||
|
log.Print("wrote cert.pem\n")
|
||||||
|
|
||||||
|
keyOut, err := os.OpenFile("key.pem", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o600)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open key.pem for writing: %v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
privBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Unable to marshal private key: %v", err)
|
||||||
|
}
|
||||||
|
if err := pem.Encode(keyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: privBytes}); err != nil {
|
||||||
|
log.Fatalf("Failed to write data to key.pem: %v", err)
|
||||||
|
}
|
||||||
|
if err := keyOut.Close(); err != nil {
|
||||||
|
log.Fatalf("Error closing key.pem: %v", err)
|
||||||
|
}
|
||||||
|
log.Print("wrote key.pem\n")
|
||||||
|
}
|
126
transport/cloudflaretls/generate_delegated_credential.go
Normal file
126
transport/cloudflaretls/generate_delegated_credential.go
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
// Copyright 2022 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
//go:build ignore
|
||||||
|
|
||||||
|
// Generate a delegated credential with the given signature scheme, signed with
|
||||||
|
// the given x.509 key pair. Outputs to 'dc.cred' and 'dckey.pem' and will
|
||||||
|
// overwrite existing files.
|
||||||
|
|
||||||
|
// Example usage:
|
||||||
|
// generate_delegated_credential -cert-path cert.pem -key-path key.pem -signature-scheme Ed25519 -duration 24h
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/tls"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
validFor = flag.Duration("duration", 5*24*time.Hour, "Duration that credential is valid for")
|
||||||
|
signatureScheme = flag.String("signature-scheme", "", "The signature scheme used by the DC")
|
||||||
|
certPath = flag.String("cert-path", "./cert.pem", "Path to signing cert")
|
||||||
|
keyPath = flag.String("key-path", "./key.pem", "Path to signing key")
|
||||||
|
isClient = flag.Bool("client-dc", false, "Create a client Delegated Credential")
|
||||||
|
outPath = flag.String("out-path", "./", "Path to output directory")
|
||||||
|
)
|
||||||
|
|
||||||
|
var SigStringMap = map[string]tls.SignatureScheme{
|
||||||
|
// ECDSA algorithms. Only constrained to a specific curve in TLS 1.3.
|
||||||
|
"ECDSAWithP256AndSHA256": tls.ECDSAWithP256AndSHA256,
|
||||||
|
"ECDSAWithP384AndSHA384": tls.ECDSAWithP384AndSHA384,
|
||||||
|
"ECDSAWithP521AndSHA512": tls.ECDSAWithP521AndSHA512,
|
||||||
|
|
||||||
|
// EdDSA algorithms.
|
||||||
|
"Ed25519": tls.Ed25519,
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
flag.Parse()
|
||||||
|
sa := SigStringMap[*signatureScheme]
|
||||||
|
|
||||||
|
cert, err := tls.LoadX509KeyPair(*certPath, *keyPath)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to load certificate and key: %v", err)
|
||||||
|
}
|
||||||
|
cert.Leaf, err = x509.ParseCertificate(cert.Certificate[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to parse leaf certificate: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
validTime := time.Since(cert.Leaf.NotBefore) + *validFor
|
||||||
|
dc, priv, err := tls.NewDelegatedCredential(&cert, sa, validTime, *isClient)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create a DC: %v\n", err)
|
||||||
|
}
|
||||||
|
dcBytes, err := dc.Marshal()
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to marshal DC: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DCOut, err := os.Create(filepath.Join(*outPath, "dc.cred"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open dc.cred for writing: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DCOut.Write(dcBytes)
|
||||||
|
if err := DCOut.Close(); err != nil {
|
||||||
|
log.Fatalf("Error closing dc.cred: %v", err)
|
||||||
|
}
|
||||||
|
log.Print("wrote dc.cred\n")
|
||||||
|
|
||||||
|
derBytes, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to marshal DC private key: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
DCKeyOut, err := os.Create(filepath.Join(*outPath, "dckey.pem"))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to open dckey.pem for writing: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := pem.Encode(DCKeyOut, &pem.Block{Type: "PRIVATE KEY", Bytes: derBytes}); err != nil {
|
||||||
|
log.Fatalf("Failed to write data to dckey.pem: %v\n", err)
|
||||||
|
}
|
||||||
|
if err := DCKeyOut.Close(); err != nil {
|
||||||
|
log.Fatalf("Error closing dckey.pem: %v\n", err)
|
||||||
|
}
|
||||||
|
log.Print("wrote dckey.pem\n")
|
||||||
|
|
||||||
|
fmt.Println("Success")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Copied from tls.go, because it's private.
|
||||||
|
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
|
||||||
|
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
|
||||||
|
switch key := key.(type) {
|
||||||
|
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey, circlSign.PrivateKey:
|
||||||
|
return key, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("tls: found unknown private key type in PKCS#8 wrapping")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if key, err := x509.ParseECPrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("tls: failed to parse private key")
|
||||||
|
}
|
1069
transport/cloudflaretls/handshake_client.go
Normal file
1069
transport/cloudflaretls/handshake_client.go
Normal file
File diff suppressed because it is too large
Load diff
1032
transport/cloudflaretls/handshake_client_tls13.go
Normal file
1032
transport/cloudflaretls/handshake_client_tls13.go
Normal file
File diff suppressed because it is too large
Load diff
1927
transport/cloudflaretls/handshake_messages.go
Normal file
1927
transport/cloudflaretls/handshake_messages.go
Normal file
File diff suppressed because it is too large
Load diff
893
transport/cloudflaretls/handshake_server.go
Normal file
893
transport/cloudflaretls/handshake_server.go
Normal file
|
@ -0,0 +1,893 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/subtle"
|
||||||
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
"io"
|
||||||
|
"sync/atomic"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// serverHandshakeState contains details of a server handshake in progress.
|
||||||
|
// It's discarded once the handshake has completed.
|
||||||
|
type serverHandshakeState struct {
|
||||||
|
c *Conn
|
||||||
|
ctx context.Context
|
||||||
|
clientHello *clientHelloMsg
|
||||||
|
hello *serverHelloMsg
|
||||||
|
suite *cipherSuite
|
||||||
|
ecdheOk bool
|
||||||
|
ecSignOk bool
|
||||||
|
rsaDecryptOk bool
|
||||||
|
rsaSignOk bool
|
||||||
|
sessionState *sessionState
|
||||||
|
finishedHash finishedHash
|
||||||
|
masterSecret []byte
|
||||||
|
cert *Certificate
|
||||||
|
}
|
||||||
|
|
||||||
|
// serverHandshake performs a TLS handshake as a server.
|
||||||
|
func (c *Conn) serverHandshake(ctx context.Context) error {
|
||||||
|
clientHello, err := c.readClientHello(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.vers == VersionTLS13 {
|
||||||
|
hs := serverHandshakeStateTLS13{
|
||||||
|
c: c,
|
||||||
|
ctx: ctx,
|
||||||
|
clientHello: clientHello,
|
||||||
|
hsTimings: createTLS13ServerHandshakeTimingInfo(c.config.Time),
|
||||||
|
}
|
||||||
|
return hs.handshake()
|
||||||
|
}
|
||||||
|
|
||||||
|
hs := serverHandshakeState{
|
||||||
|
c: c,
|
||||||
|
ctx: ctx,
|
||||||
|
clientHello: clientHello,
|
||||||
|
}
|
||||||
|
return hs.handshake()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) handshake() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
if err := hs.processClientHello(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// For an overview of TLS handshaking, see RFC 5246, Section 7.3.
|
||||||
|
c.buffering = true
|
||||||
|
if hs.checkForResumption() {
|
||||||
|
// The client has included a session ticket and so we do an abbreviated handshake.
|
||||||
|
c.didResume = true
|
||||||
|
if err := hs.doResumeHandshake(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.establishKeys(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.sendSessionTicket(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.sendFinished(c.serverFinished[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := c.flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.clientFinishedIsFirst = false
|
||||||
|
if err := hs.readFinished(nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// The client didn't include a session ticket, or it wasn't
|
||||||
|
// valid so we do a full handshake.
|
||||||
|
if err := hs.pickCipherSuite(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.doFullHandshake(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.establishKeys(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.readFinished(c.clientFinished[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
c.clientFinishedIsFirst = true
|
||||||
|
c.buffering = true
|
||||||
|
if err := hs.sendSessionTicket(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := hs.sendFinished(nil); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if _, err := c.flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
c.ekm = ekmFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random)
|
||||||
|
atomic.StoreUint32(&c.handshakeStatus, 1)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// readClientHello reads a ClientHello message and selects the protocol version.
|
||||||
|
func (c *Conn) readClientHello(ctx context.Context) (*clientHelloMsg, error) {
|
||||||
|
msg, err := c.readHandshake()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
clientHello, ok := msg.(*clientHelloMsg)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertUnexpectedMessage)
|
||||||
|
return nil, unexpectedMessageError(clientHello, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
// NOTE(cjpatton): ECH usage is resolved before calling GetConfigForClient()
|
||||||
|
// or GetCertifciate(). Hence, it is not currently possible to reject ECH if
|
||||||
|
// we don't recognize the inner SNI. This may or may not be desirable in the
|
||||||
|
// future.
|
||||||
|
clientHello, err = c.echAcceptOrReject(clientHello, false) // afterHRR == false
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("tls: %s", err) // Alert sent.
|
||||||
|
}
|
||||||
|
|
||||||
|
var configForClient *Config
|
||||||
|
originalConfig := c.config
|
||||||
|
if c.config.GetConfigForClient != nil {
|
||||||
|
chi := clientHelloInfo(ctx, c, clientHello)
|
||||||
|
if configForClient, err = c.config.GetConfigForClient(chi); err != nil {
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
return nil, err
|
||||||
|
} else if configForClient != nil {
|
||||||
|
c.config = configForClient
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.ticketKeys = originalConfig.ticketKeys(configForClient)
|
||||||
|
|
||||||
|
clientVersions := clientHello.supportedVersions
|
||||||
|
if len(clientHello.supportedVersions) == 0 {
|
||||||
|
clientVersions = supportedVersionsFromMax(clientHello.vers)
|
||||||
|
}
|
||||||
|
c.vers, ok = c.config.mutualVersion(roleServer, clientVersions)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertProtocolVersion)
|
||||||
|
return nil, fmt.Errorf("tls: client offered only unsupported versions: %x", clientVersions)
|
||||||
|
}
|
||||||
|
c.haveVers = true
|
||||||
|
c.in.version = c.vers
|
||||||
|
c.out.version = c.vers
|
||||||
|
|
||||||
|
return clientHello, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) processClientHello() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
hs.hello = new(serverHelloMsg)
|
||||||
|
hs.hello.vers = c.vers
|
||||||
|
|
||||||
|
foundCompression := false
|
||||||
|
// We only support null compression, so check that the client offered it.
|
||||||
|
for _, compression := range hs.clientHello.compressionMethods {
|
||||||
|
if compression == compressionNone {
|
||||||
|
foundCompression = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !foundCompression {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return errors.New("tls: client does not support uncompressed connections")
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.hello.random = make([]byte, 32)
|
||||||
|
serverRandom := hs.hello.random
|
||||||
|
// Downgrade protection canaries. See RFC 8446, Section 4.1.3.
|
||||||
|
maxVers := c.config.maxSupportedVersion(roleServer)
|
||||||
|
if maxVers >= VersionTLS12 && c.vers < maxVers || testingOnlyForceDowngradeCanary {
|
||||||
|
if c.vers == VersionTLS12 {
|
||||||
|
copy(serverRandom[24:], downgradeCanaryTLS12)
|
||||||
|
} else {
|
||||||
|
copy(serverRandom[24:], downgradeCanaryTLS11)
|
||||||
|
}
|
||||||
|
serverRandom = serverRandom[:24]
|
||||||
|
}
|
||||||
|
_, err := io.ReadFull(c.config.rand(), serverRandom)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(hs.clientHello.secureRenegotiation) != 0 {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return errors.New("tls: initial handshake had non-empty renegotiation extension")
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.hello.secureRenegotiationSupported = hs.clientHello.secureRenegotiationSupported
|
||||||
|
hs.hello.compressionMethod = compressionNone
|
||||||
|
if len(hs.clientHello.serverName) > 0 {
|
||||||
|
c.serverName = hs.clientHello.serverName
|
||||||
|
}
|
||||||
|
|
||||||
|
selectedProto, err := negotiateALPN(c.config.NextProtos, hs.clientHello.alpnProtocols)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertNoApplicationProtocol)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hs.hello.alpnProtocol = selectedProto
|
||||||
|
c.clientProtocol = selectedProto
|
||||||
|
|
||||||
|
hs.cert, err = c.config.getCertificate(clientHelloInfo(hs.ctx, c, hs.clientHello))
|
||||||
|
if err != nil {
|
||||||
|
if err == errNoCertificates {
|
||||||
|
c.sendAlert(alertUnrecognizedName)
|
||||||
|
} else {
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if hs.clientHello.scts {
|
||||||
|
hs.hello.scts = hs.cert.SignedCertificateTimestamps
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.ecdheOk = supportsECDHE(c.config, hs.clientHello.supportedCurves, hs.clientHello.supportedPoints)
|
||||||
|
|
||||||
|
if hs.ecdheOk {
|
||||||
|
// Although omitting the ec_point_formats extension is permitted, some
|
||||||
|
// old OpenSSL version will refuse to handshake if not present.
|
||||||
|
//
|
||||||
|
// Per RFC 4492, section 5.1.2, implementations MUST support the
|
||||||
|
// uncompressed point format. See golang.org/issue/31943.
|
||||||
|
hs.hello.supportedPoints = []uint8{pointFormatUncompressed}
|
||||||
|
}
|
||||||
|
|
||||||
|
if priv, ok := hs.cert.PrivateKey.(crypto.Signer); ok {
|
||||||
|
switch priv.Public().(type) {
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
hs.ecSignOk = true
|
||||||
|
case ed25519.PublicKey:
|
||||||
|
hs.ecSignOk = true
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
hs.rsaSignOk = true
|
||||||
|
default:
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
return fmt.Errorf("tls: unsupported signing key type (%T)", priv.Public())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if priv, ok := hs.cert.PrivateKey.(crypto.Decrypter); ok {
|
||||||
|
switch priv.Public().(type) {
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
hs.rsaDecryptOk = true
|
||||||
|
default:
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
return fmt.Errorf("tls: unsupported decryption key type (%T)", priv.Public())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// negotiateALPN picks a shared ALPN protocol that both sides support in server
|
||||||
|
// preference order. If ALPN is not configured or the peer doesn't support it,
|
||||||
|
// it returns "" and no error.
|
||||||
|
func negotiateALPN(serverProtos, clientProtos []string) (string, error) {
|
||||||
|
if len(serverProtos) == 0 || len(clientProtos) == 0 {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
var http11fallback bool
|
||||||
|
for _, s := range serverProtos {
|
||||||
|
for _, c := range clientProtos {
|
||||||
|
if s == c {
|
||||||
|
return s, nil
|
||||||
|
}
|
||||||
|
if s == "h2" && c == "http/1.1" {
|
||||||
|
http11fallback = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// As a special case, let http/1.1 clients connect to h2 servers as if they
|
||||||
|
// didn't support ALPN. We used not to enforce protocol overlap, so over
|
||||||
|
// time a number of HTTP servers were configured with only "h2", but
|
||||||
|
// expected to accept connections from "http/1.1" clients. See Issue 46310.
|
||||||
|
if http11fallback {
|
||||||
|
return "", nil
|
||||||
|
}
|
||||||
|
return "", fmt.Errorf("tls: client requested unsupported application protocols (%s)", clientProtos)
|
||||||
|
}
|
||||||
|
|
||||||
|
// supportsECDHE returns whether ECDHE key exchanges can be used with this
|
||||||
|
// pre-TLS 1.3 client.
|
||||||
|
func supportsECDHE(c *Config, supportedCurves []CurveID, supportedPoints []uint8) bool {
|
||||||
|
supportsCurve := false
|
||||||
|
for _, curve := range supportedCurves {
|
||||||
|
if c.supportsCurve(curve) {
|
||||||
|
supportsCurve = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
supportsPointFormat := false
|
||||||
|
for _, pointFormat := range supportedPoints {
|
||||||
|
if pointFormat == pointFormatUncompressed {
|
||||||
|
supportsPointFormat = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return supportsCurve && supportsPointFormat
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) pickCipherSuite() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
preferenceOrder := cipherSuitesPreferenceOrder
|
||||||
|
if !hasAESGCMHardwareSupport || !aesgcmPreferred(hs.clientHello.cipherSuites) {
|
||||||
|
preferenceOrder = cipherSuitesPreferenceOrderNoAES
|
||||||
|
}
|
||||||
|
|
||||||
|
configCipherSuites := c.config.cipherSuites()
|
||||||
|
preferenceList := make([]uint16, 0, len(configCipherSuites))
|
||||||
|
for _, suiteID := range preferenceOrder {
|
||||||
|
for _, id := range configCipherSuites {
|
||||||
|
if id == suiteID {
|
||||||
|
preferenceList = append(preferenceList, id)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.suite = selectCipherSuite(preferenceList, hs.clientHello.cipherSuites, hs.cipherSuiteOk)
|
||||||
|
if hs.suite == nil {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return errors.New("tls: no cipher suite supported by both client and server")
|
||||||
|
}
|
||||||
|
c.cipherSuite = hs.suite.id
|
||||||
|
|
||||||
|
for _, id := range hs.clientHello.cipherSuites {
|
||||||
|
if id == TLS_FALLBACK_SCSV {
|
||||||
|
// The client is doing a fallback connection. See RFC 7507.
|
||||||
|
if hs.clientHello.vers < c.config.maxSupportedVersion(roleServer) {
|
||||||
|
c.sendAlert(alertInappropriateFallback)
|
||||||
|
return errors.New("tls: client using inappropriate protocol fallback")
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) cipherSuiteOk(c *cipherSuite) bool {
|
||||||
|
if c.flags&suiteECDHE != 0 {
|
||||||
|
if !hs.ecdheOk {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if c.flags&suiteECSign != 0 {
|
||||||
|
if !hs.ecSignOk {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if !hs.rsaSignOk {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} else if !hs.rsaDecryptOk {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if hs.c.vers < VersionTLS12 && c.flags&suiteTLS12 != 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// checkForResumption reports whether we should perform resumption on this connection.
|
||||||
|
func (hs *serverHandshakeState) checkForResumption() bool {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
if c.config.SessionTicketsDisabled || c.config.ECHEnabled {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
plaintext, usedOldKey := c.decryptTicket(hs.clientHello.sessionTicket)
|
||||||
|
if plaintext == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
hs.sessionState = &sessionState{usedOldKey: usedOldKey}
|
||||||
|
ok := hs.sessionState.unmarshal(plaintext)
|
||||||
|
if !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
createdAt := time.Unix(int64(hs.sessionState.createdAt), 0)
|
||||||
|
if c.config.time().Sub(createdAt) > maxSessionTicketLifetime {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Never resume a session for a different TLS version.
|
||||||
|
if c.vers != hs.sessionState.vers {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
cipherSuiteOk := false
|
||||||
|
// Check that the client is still offering the ciphersuite in the session.
|
||||||
|
for _, id := range hs.clientHello.cipherSuites {
|
||||||
|
if id == hs.sessionState.cipherSuite {
|
||||||
|
cipherSuiteOk = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !cipherSuiteOk {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check that we also support the ciphersuite from the session.
|
||||||
|
hs.suite = selectCipherSuite([]uint16{hs.sessionState.cipherSuite},
|
||||||
|
c.config.cipherSuites(), hs.cipherSuiteOk)
|
||||||
|
if hs.suite == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
sessionHasClientCerts := len(hs.sessionState.certificates) != 0
|
||||||
|
needClientCerts := requiresClientCert(c.config.ClientAuth)
|
||||||
|
if needClientCerts && !sessionHasClientCerts {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if sessionHasClientCerts && c.config.ClientAuth == NoClientCert {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) doResumeHandshake() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
hs.hello.cipherSuite = hs.suite.id
|
||||||
|
c.cipherSuite = hs.suite.id
|
||||||
|
// We echo the client's session ID in the ServerHello to let it know
|
||||||
|
// that we're doing a resumption.
|
||||||
|
hs.hello.sessionId = hs.clientHello.sessionId
|
||||||
|
hs.hello.ticketSupported = hs.sessionState.usedOldKey
|
||||||
|
hs.finishedHash = newFinishedHash(c.vers, hs.suite)
|
||||||
|
hs.finishedHash.discardHandshakeBuffer()
|
||||||
|
hs.finishedHash.Write(hs.clientHello.marshal())
|
||||||
|
hs.finishedHash.Write(hs.hello.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, hs.hello.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := c.processCertsFromClient(Certificate{
|
||||||
|
Certificate: hs.sessionState.certificates,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.config.VerifyConnection != nil {
|
||||||
|
if err := c.config.VerifyConnection(c.connectionStateLocked()); err != nil {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.masterSecret = hs.sessionState.masterSecret
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) doFullHandshake() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
if hs.clientHello.ocspStapling && len(hs.cert.OCSPStaple) > 0 {
|
||||||
|
hs.hello.ocspStapling = true
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.hello.ticketSupported = hs.clientHello.ticketSupported && !c.config.SessionTicketsDisabled && !c.config.ECHEnabled
|
||||||
|
hs.hello.cipherSuite = hs.suite.id
|
||||||
|
|
||||||
|
hs.finishedHash = newFinishedHash(hs.c.vers, hs.suite)
|
||||||
|
if c.config.ClientAuth == NoClientCert {
|
||||||
|
// No need to keep a full record of the handshake if client
|
||||||
|
// certificates won't be used.
|
||||||
|
hs.finishedHash.discardHandshakeBuffer()
|
||||||
|
}
|
||||||
|
hs.finishedHash.Write(hs.clientHello.marshal())
|
||||||
|
hs.finishedHash.Write(hs.hello.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, hs.hello.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
certMsg := new(certificateMsg)
|
||||||
|
certMsg.certificates = hs.cert.Certificate
|
||||||
|
hs.finishedHash.Write(certMsg.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, certMsg.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if hs.hello.ocspStapling {
|
||||||
|
certStatus := new(certificateStatusMsg)
|
||||||
|
certStatus.response = hs.cert.OCSPStaple
|
||||||
|
hs.finishedHash.Write(certStatus.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, certStatus.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
keyAgreement := hs.suite.ka(c.vers)
|
||||||
|
skx, err := keyAgreement.generateServerKeyExchange(c.config, hs.cert, hs.clientHello, hs.hello)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if skx != nil {
|
||||||
|
hs.finishedHash.Write(skx.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, skx.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var certReq *certificateRequestMsg
|
||||||
|
if c.config.ClientAuth >= RequestClientCert {
|
||||||
|
// Request a client certificate
|
||||||
|
certReq = new(certificateRequestMsg)
|
||||||
|
certReq.certificateTypes = []byte{
|
||||||
|
byte(certTypeRSASign),
|
||||||
|
byte(certTypeECDSASign),
|
||||||
|
}
|
||||||
|
if c.vers >= VersionTLS12 {
|
||||||
|
certReq.hasSignatureAlgorithm = true
|
||||||
|
certReq.supportedSignatureAlgorithms = c.config.supportedSignatureAlgorithms()
|
||||||
|
}
|
||||||
|
|
||||||
|
// An empty list of certificateAuthorities signals to
|
||||||
|
// the client that it may send any certificate in response
|
||||||
|
// to our request. When we know the CAs we trust, then
|
||||||
|
// we can send them down, so that the client can choose
|
||||||
|
// an appropriate certificate to give to us.
|
||||||
|
if c.config.ClientCAs != nil {
|
||||||
|
certReq.certificateAuthorities = c.config.ClientCAs.Subjects()
|
||||||
|
}
|
||||||
|
hs.finishedHash.Write(certReq.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, certReq.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
helloDone := new(serverHelloDoneMsg)
|
||||||
|
hs.finishedHash.Write(helloDone.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, helloDone.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := c.flush(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var pub crypto.PublicKey // public key for client auth, if any
|
||||||
|
|
||||||
|
msg, err := c.readHandshake()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we requested a client certificate, then the client must send a
|
||||||
|
// certificate message, even if it's empty.
|
||||||
|
if c.config.ClientAuth >= RequestClientCert {
|
||||||
|
certMsg, ok := msg.(*certificateMsg)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertUnexpectedMessage)
|
||||||
|
return unexpectedMessageError(certMsg, msg)
|
||||||
|
}
|
||||||
|
hs.finishedHash.Write(certMsg.marshal())
|
||||||
|
|
||||||
|
if err := c.processCertsFromClient(Certificate{
|
||||||
|
Certificate: certMsg.certificates,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if len(certMsg.certificates) != 0 {
|
||||||
|
pub = c.peerCertificates[0].PublicKey
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err = c.readHandshake()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.config.VerifyConnection != nil {
|
||||||
|
if err := c.config.VerifyConnection(c.connectionStateLocked()); err != nil {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get client key exchange
|
||||||
|
ckx, ok := msg.(*clientKeyExchangeMsg)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertUnexpectedMessage)
|
||||||
|
return unexpectedMessageError(ckx, msg)
|
||||||
|
}
|
||||||
|
hs.finishedHash.Write(ckx.marshal())
|
||||||
|
|
||||||
|
preMasterSecret, err := keyAgreement.processClientKeyExchange(c.config, hs.cert, ckx, c.vers)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if eccKex, ok := keyAgreement.(*ecdheKeyAgreement); ok {
|
||||||
|
c.handleCFEvent(CFEventTLSNegotiatedNamedKEX{
|
||||||
|
KEX: eccKex.params.CurveID(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
hs.masterSecret = masterFromPreMasterSecret(c.vers, hs.suite, preMasterSecret, hs.clientHello.random, hs.hello.random)
|
||||||
|
if err := c.config.writeKeyLog(keyLogLabelTLS12, hs.clientHello.random, hs.masterSecret); err != nil {
|
||||||
|
c.sendAlert(alertInternalError)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we received a client cert in response to our certificate request message,
|
||||||
|
// the client will send us a certificateVerifyMsg immediately after the
|
||||||
|
// clientKeyExchangeMsg. This message is a digest of all preceding
|
||||||
|
// handshake-layer messages that is signed using the private key corresponding
|
||||||
|
// to the client's certificate. This allows us to verify that the client is in
|
||||||
|
// possession of the private key of the certificate.
|
||||||
|
if len(c.peerCertificates) > 0 {
|
||||||
|
msg, err = c.readHandshake()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
certVerify, ok := msg.(*certificateVerifyMsg)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertUnexpectedMessage)
|
||||||
|
return unexpectedMessageError(certVerify, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
var sigType uint8
|
||||||
|
var sigHash crypto.Hash
|
||||||
|
if c.vers >= VersionTLS12 {
|
||||||
|
if !isSupportedSignatureAlgorithm(certVerify.signatureAlgorithm, certReq.supportedSignatureAlgorithms) {
|
||||||
|
c.sendAlert(alertIllegalParameter)
|
||||||
|
return errors.New("tls: client certificate used with invalid signature algorithm")
|
||||||
|
}
|
||||||
|
sigType, sigHash, err = typeAndHashFromSignatureScheme(certVerify.signatureAlgorithm)
|
||||||
|
if err != nil {
|
||||||
|
return c.sendAlert(alertInternalError)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sigType, sigHash, err = legacyTypeAndHashFromPublicKey(pub)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertIllegalParameter)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
signed := hs.finishedHash.hashForClientCertificate(sigType, sigHash, hs.masterSecret)
|
||||||
|
if err := verifyHandshakeSignature(sigType, pub, sigHash, signed, certVerify.signature); err != nil {
|
||||||
|
c.sendAlert(alertDecryptError)
|
||||||
|
return errors.New("tls: invalid signature by the client certificate: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.finishedHash.Write(certVerify.marshal())
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.finishedHash.discardHandshakeBuffer()
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) establishKeys() error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV := keysFromMasterSecret(c.vers, hs.suite, hs.masterSecret, hs.clientHello.random, hs.hello.random, hs.suite.macLen, hs.suite.keyLen, hs.suite.ivLen)
|
||||||
|
|
||||||
|
var clientCipher, serverCipher any
|
||||||
|
var clientHash, serverHash hash.Hash
|
||||||
|
|
||||||
|
if hs.suite.aead == nil {
|
||||||
|
clientCipher = hs.suite.cipher(clientKey, clientIV, true /* for reading */)
|
||||||
|
clientHash = hs.suite.mac(clientMAC)
|
||||||
|
serverCipher = hs.suite.cipher(serverKey, serverIV, false /* not for reading */)
|
||||||
|
serverHash = hs.suite.mac(serverMAC)
|
||||||
|
} else {
|
||||||
|
clientCipher = hs.suite.aead(clientKey, clientIV)
|
||||||
|
serverCipher = hs.suite.aead(serverKey, serverIV)
|
||||||
|
}
|
||||||
|
|
||||||
|
c.in.prepareCipherSpec(c.vers, clientCipher, clientHash)
|
||||||
|
c.out.prepareCipherSpec(c.vers, serverCipher, serverHash)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) readFinished(out []byte) error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
if err := c.readChangeCipherSpec(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
msg, err := c.readHandshake()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
clientFinished, ok := msg.(*finishedMsg)
|
||||||
|
if !ok {
|
||||||
|
c.sendAlert(alertUnexpectedMessage)
|
||||||
|
return unexpectedMessageError(clientFinished, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
verify := hs.finishedHash.clientSum(hs.masterSecret)
|
||||||
|
if len(verify) != len(clientFinished.verifyData) ||
|
||||||
|
subtle.ConstantTimeCompare(verify, clientFinished.verifyData) != 1 {
|
||||||
|
c.sendAlert(alertHandshakeFailure)
|
||||||
|
return errors.New("tls: client's Finished message is incorrect")
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.finishedHash.Write(clientFinished.marshal())
|
||||||
|
copy(out, verify)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) sendSessionTicket() error {
|
||||||
|
// ticketSupported is set in a resumption handshake if the
|
||||||
|
// ticket from the client was encrypted with an old session
|
||||||
|
// ticket key and thus a refreshed ticket should be sent.
|
||||||
|
if !hs.hello.ticketSupported {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
c := hs.c
|
||||||
|
m := new(newSessionTicketMsg)
|
||||||
|
|
||||||
|
createdAt := uint64(c.config.time().Unix())
|
||||||
|
if hs.sessionState != nil {
|
||||||
|
// If this is re-wrapping an old key, then keep
|
||||||
|
// the original time it was created.
|
||||||
|
createdAt = hs.sessionState.createdAt
|
||||||
|
}
|
||||||
|
|
||||||
|
var certsFromClient [][]byte
|
||||||
|
for _, cert := range c.peerCertificates {
|
||||||
|
certsFromClient = append(certsFromClient, cert.Raw)
|
||||||
|
}
|
||||||
|
state := sessionState{
|
||||||
|
vers: c.vers,
|
||||||
|
cipherSuite: hs.suite.id,
|
||||||
|
createdAt: createdAt,
|
||||||
|
masterSecret: hs.masterSecret,
|
||||||
|
certificates: certsFromClient,
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
m.ticket, err = c.encryptTicket(state.marshal())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hs.finishedHash.Write(m.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, m.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (hs *serverHandshakeState) sendFinished(out []byte) error {
|
||||||
|
c := hs.c
|
||||||
|
|
||||||
|
if _, err := c.writeRecord(recordTypeChangeCipherSpec, []byte{1}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
finished := new(finishedMsg)
|
||||||
|
finished.verifyData = hs.finishedHash.serverSum(hs.masterSecret)
|
||||||
|
hs.finishedHash.Write(finished.marshal())
|
||||||
|
if _, err := c.writeRecord(recordTypeHandshake, finished.marshal()); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(out, finished.verifyData)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processCertsFromClient takes a chain of client certificates either from a
|
||||||
|
// Certificates message or from a sessionState and verifies them. It returns
|
||||||
|
// the public key of the leaf certificate.
|
||||||
|
func (c *Conn) processCertsFromClient(certificate Certificate) error {
|
||||||
|
certificates := certificate.Certificate
|
||||||
|
certs := make([]*x509.Certificate, len(certificates))
|
||||||
|
var err error
|
||||||
|
for i, asn1Data := range certificates {
|
||||||
|
if certs[i], err = x509.ParseCertificate(asn1Data); err != nil {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return errors.New("tls: failed to parse client certificate: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(certs) == 0 && requiresClientCert(c.config.ClientAuth) {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return errors.New("tls: client didn't provide a certificate")
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.config.ClientAuth >= VerifyClientCertIfGiven && len(certs) > 0 {
|
||||||
|
opts := x509.VerifyOptions{
|
||||||
|
Roots: c.config.ClientCAs,
|
||||||
|
CurrentTime: c.config.time(),
|
||||||
|
Intermediates: x509.NewCertPool(),
|
||||||
|
KeyUsages: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, cert := range certs[1:] {
|
||||||
|
opts.Intermediates.AddCert(cert)
|
||||||
|
}
|
||||||
|
|
||||||
|
chains, err := certs[0].Verify(opts)
|
||||||
|
if err != nil {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return errors.New("tls: failed to verify client certificate: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
c.verifiedChains = chains
|
||||||
|
}
|
||||||
|
|
||||||
|
c.peerCertificates = certs
|
||||||
|
c.ocspResponse = certificate.OCSPStaple
|
||||||
|
c.scts = certificate.SignedCertificateTimestamps
|
||||||
|
|
||||||
|
if len(certs) > 0 {
|
||||||
|
switch certs[0].PublicKey.(type) {
|
||||||
|
case *ecdsa.PublicKey, *rsa.PublicKey, ed25519.PublicKey, circlSign.PublicKey:
|
||||||
|
default:
|
||||||
|
c.sendAlert(alertUnsupportedCertificate)
|
||||||
|
return fmt.Errorf("tls: client certificate contains an unsupported public key of type %T", certs[0].PublicKey)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if c.config.VerifyPeerCertificate != nil {
|
||||||
|
if err := c.config.VerifyPeerCertificate(certificates, c.verifiedChains); err != nil {
|
||||||
|
c.sendAlert(alertBadCertificate)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func clientHelloInfo(ctx context.Context, c *Conn, clientHello *clientHelloMsg) *ClientHelloInfo {
|
||||||
|
supportedVersions := clientHello.supportedVersions
|
||||||
|
if len(clientHello.supportedVersions) == 0 {
|
||||||
|
supportedVersions = supportedVersionsFromMax(clientHello.vers)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &ClientHelloInfo{
|
||||||
|
CipherSuites: clientHello.cipherSuites,
|
||||||
|
ServerName: clientHello.serverName,
|
||||||
|
SupportedCurves: clientHello.supportedCurves,
|
||||||
|
SupportedPoints: clientHello.supportedPoints,
|
||||||
|
SignatureSchemes: clientHello.supportedSignatureAlgorithms,
|
||||||
|
SupportedProtos: clientHello.alpnProtocols,
|
||||||
|
SupportedVersions: supportedVersions,
|
||||||
|
SupportsDelegatedCredential: clientHello.delegatedCredentialSupported,
|
||||||
|
SignatureSchemesDC: clientHello.supportedSignatureAlgorithmsDC,
|
||||||
|
Conn: c.conn,
|
||||||
|
config: c.config,
|
||||||
|
ctx: ctx,
|
||||||
|
}
|
||||||
|
}
|
1121
transport/cloudflaretls/handshake_server_tls13.go
Normal file
1121
transport/cloudflaretls/handshake_server_tls13.go
Normal file
File diff suppressed because it is too large
Load diff
42
transport/cloudflaretls/hpke.go
Normal file
42
transport/cloudflaretls/hpke.go
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
// Copyright 2020 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/cloudflare/circl/hpke"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The mandatory-to-implement HPKE cipher suite for use with the ECH extension.
|
||||||
|
var defaultHPKESuite hpke.Suite
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var err error
|
||||||
|
defaultHPKESuite, err = hpkeAssembleSuite(
|
||||||
|
uint16(hpke.KEM_X25519_HKDF_SHA256),
|
||||||
|
uint16(hpke.KDF_HKDF_SHA256),
|
||||||
|
uint16(hpke.AEAD_AES128GCM),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Sprintf("hpke: mandatory-to-implement cipher suite not supported: %s", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func hpkeAssembleSuite(kemId, kdfId, aeadId uint16) (hpke.Suite, error) {
|
||||||
|
kem := hpke.KEM(kemId)
|
||||||
|
if !kem.IsValid() {
|
||||||
|
return hpke.Suite{}, errors.New("KEM is not supported")
|
||||||
|
}
|
||||||
|
kdf := hpke.KDF(kdfId)
|
||||||
|
if !kdf.IsValid() {
|
||||||
|
return hpke.Suite{}, errors.New("KDF is not supported")
|
||||||
|
}
|
||||||
|
aead := hpke.AEAD(aeadId)
|
||||||
|
if !aead.IsValid() {
|
||||||
|
return hpke.Suite{}, errors.New("AEAD is not supported")
|
||||||
|
}
|
||||||
|
return hpke.NewSuite(kem, kdf, aead), nil
|
||||||
|
}
|
359
transport/cloudflaretls/key_agreement.go
Normal file
359
transport/cloudflaretls/key_agreement.go
Normal file
|
@ -0,0 +1,359 @@
|
||||||
|
// Copyright 2010 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/md5"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/x509"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
// a keyAgreement implements the client and server side of a TLS key agreement
|
||||||
|
// protocol by generating and processing key exchange messages.
|
||||||
|
type keyAgreement interface {
|
||||||
|
// On the server side, the first two methods are called in order.
|
||||||
|
|
||||||
|
// In the case that the key agreement protocol doesn't use a
|
||||||
|
// ServerKeyExchange message, generateServerKeyExchange can return nil,
|
||||||
|
// nil.
|
||||||
|
generateServerKeyExchange(*Config, *Certificate, *clientHelloMsg, *serverHelloMsg) (*serverKeyExchangeMsg, error)
|
||||||
|
processClientKeyExchange(*Config, *Certificate, *clientKeyExchangeMsg, uint16) ([]byte, error)
|
||||||
|
|
||||||
|
// On the client side, the next two methods are called in order.
|
||||||
|
|
||||||
|
// This method may not be called if the server doesn't send a
|
||||||
|
// ServerKeyExchange message.
|
||||||
|
processServerKeyExchange(*Config, *clientHelloMsg, *serverHelloMsg, *x509.Certificate, *serverKeyExchangeMsg) error
|
||||||
|
generateClientKeyExchange(*Config, *clientHelloMsg, *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
errClientKeyExchange = errors.New("tls: invalid ClientKeyExchange message")
|
||||||
|
errServerKeyExchange = errors.New("tls: invalid ServerKeyExchange message")
|
||||||
|
)
|
||||||
|
|
||||||
|
// rsaKeyAgreement implements the standard TLS key agreement where the client
|
||||||
|
// encrypts the pre-master secret to the server's public key.
|
||||||
|
type rsaKeyAgreement struct{}
|
||||||
|
|
||||||
|
func (ka rsaKeyAgreement) generateServerKeyExchange(config *Config, cert *Certificate, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka rsaKeyAgreement) processClientKeyExchange(config *Config, cert *Certificate, ckx *clientKeyExchangeMsg, version uint16) ([]byte, error) {
|
||||||
|
if len(ckx.ciphertext) < 2 {
|
||||||
|
return nil, errClientKeyExchange
|
||||||
|
}
|
||||||
|
ciphertextLen := int(ckx.ciphertext[0])<<8 | int(ckx.ciphertext[1])
|
||||||
|
if ciphertextLen != len(ckx.ciphertext)-2 {
|
||||||
|
return nil, errClientKeyExchange
|
||||||
|
}
|
||||||
|
ciphertext := ckx.ciphertext[2:]
|
||||||
|
|
||||||
|
priv, ok := cert.PrivateKey.(crypto.Decrypter)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tls: certificate private key does not implement crypto.Decrypter")
|
||||||
|
}
|
||||||
|
// Perform constant time RSA PKCS #1 v1.5 decryption
|
||||||
|
preMasterSecret, err := priv.Decrypt(config.rand(), ciphertext, &rsa.PKCS1v15DecryptOptions{SessionKeyLen: 48})
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
// We don't check the version number in the premaster secret. For one,
|
||||||
|
// by checking it, we would leak information about the validity of the
|
||||||
|
// encrypted pre-master secret. Secondly, it provides only a small
|
||||||
|
// benefit against a downgrade attack and some implementations send the
|
||||||
|
// wrong version anyway. See the discussion at the end of section
|
||||||
|
// 7.4.7.1 of RFC 4346.
|
||||||
|
return preMasterSecret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka rsaKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) error {
|
||||||
|
return errors.New("tls: unexpected ServerKeyExchange")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka rsaKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) {
|
||||||
|
preMasterSecret := make([]byte, 48)
|
||||||
|
preMasterSecret[0] = byte(clientHello.vers >> 8)
|
||||||
|
preMasterSecret[1] = byte(clientHello.vers)
|
||||||
|
_, err := io.ReadFull(config.rand(), preMasterSecret[2:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
rsaKey, ok := cert.PublicKey.(*rsa.PublicKey)
|
||||||
|
if !ok {
|
||||||
|
return nil, nil, errors.New("tls: server certificate contains incorrect key type for selected ciphersuite")
|
||||||
|
}
|
||||||
|
encrypted, err := rsa.EncryptPKCS1v15(config.rand(), rsaKey, preMasterSecret)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
|
}
|
||||||
|
ckx := new(clientKeyExchangeMsg)
|
||||||
|
ckx.ciphertext = make([]byte, len(encrypted)+2)
|
||||||
|
ckx.ciphertext[0] = byte(len(encrypted) >> 8)
|
||||||
|
ckx.ciphertext[1] = byte(len(encrypted))
|
||||||
|
copy(ckx.ciphertext[2:], encrypted)
|
||||||
|
return preMasterSecret, ckx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// sha1Hash calculates a SHA1 hash over the given byte slices.
|
||||||
|
func sha1Hash(slices [][]byte) []byte {
|
||||||
|
hsha1 := sha1.New()
|
||||||
|
for _, slice := range slices {
|
||||||
|
hsha1.Write(slice)
|
||||||
|
}
|
||||||
|
return hsha1.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// md5SHA1Hash implements TLS 1.0's hybrid hash function which consists of the
|
||||||
|
// concatenation of an MD5 and SHA1 hash.
|
||||||
|
func md5SHA1Hash(slices [][]byte) []byte {
|
||||||
|
md5sha1 := make([]byte, md5.Size+sha1.Size)
|
||||||
|
hmd5 := md5.New()
|
||||||
|
for _, slice := range slices {
|
||||||
|
hmd5.Write(slice)
|
||||||
|
}
|
||||||
|
copy(md5sha1, hmd5.Sum(nil))
|
||||||
|
copy(md5sha1[md5.Size:], sha1Hash(slices))
|
||||||
|
return md5sha1
|
||||||
|
}
|
||||||
|
|
||||||
|
// hashForServerKeyExchange hashes the given slices and returns their digest
|
||||||
|
// using the given hash function (for >= TLS 1.2) or using a default based on
|
||||||
|
// the sigType (for earlier TLS versions). For Ed25519 signatures, which don't
|
||||||
|
// do pre-hashing, it returns the concatenation of the slices.
|
||||||
|
func hashForServerKeyExchange(sigType uint8, hashFunc crypto.Hash, version uint16, slices ...[]byte) []byte {
|
||||||
|
if sigType == signatureEd25519 || circlSchemeBySigType(sigType) != nil {
|
||||||
|
var signed []byte
|
||||||
|
for _, slice := range slices {
|
||||||
|
signed = append(signed, slice...)
|
||||||
|
}
|
||||||
|
return signed
|
||||||
|
}
|
||||||
|
if version >= VersionTLS12 {
|
||||||
|
h := hashFunc.New()
|
||||||
|
for _, slice := range slices {
|
||||||
|
h.Write(slice)
|
||||||
|
}
|
||||||
|
digest := h.Sum(nil)
|
||||||
|
return digest
|
||||||
|
}
|
||||||
|
if sigType == signatureECDSA {
|
||||||
|
return sha1Hash(slices)
|
||||||
|
}
|
||||||
|
return md5SHA1Hash(slices)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ecdheKeyAgreement implements a TLS key agreement where the server
|
||||||
|
// generates an ephemeral EC public/private key pair and signs it. The
|
||||||
|
// pre-master secret is then calculated using ECDH. The signature may
|
||||||
|
// be ECDSA, Ed25519 or RSA.
|
||||||
|
type ecdheKeyAgreement struct {
|
||||||
|
version uint16
|
||||||
|
isRSA bool
|
||||||
|
params ecdheParameters
|
||||||
|
|
||||||
|
// ckx and preMasterSecret are generated in processServerKeyExchange
|
||||||
|
// and returned in generateClientKeyExchange.
|
||||||
|
ckx *clientKeyExchangeMsg
|
||||||
|
preMasterSecret []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka *ecdheKeyAgreement) generateServerKeyExchange(config *Config, cert *Certificate, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
|
||||||
|
var curveID CurveID
|
||||||
|
for _, c := range clientHello.supportedCurves {
|
||||||
|
if config.supportsCurve(c) && curveIdToCirclScheme(c) == nil {
|
||||||
|
curveID = c
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if curveID == 0 {
|
||||||
|
return nil, errors.New("tls: no supported elliptic curves offered")
|
||||||
|
}
|
||||||
|
if _, ok := curveForCurveID(curveID); curveID != X25519 && !ok {
|
||||||
|
return nil, errors.New("tls: CurvePreferences includes unsupported curve")
|
||||||
|
}
|
||||||
|
|
||||||
|
params, err := generateECDHEParameters(config.rand(), curveID)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
ka.params = params
|
||||||
|
|
||||||
|
// See RFC 4492, Section 5.4.
|
||||||
|
ecdhePublic := params.PublicKey()
|
||||||
|
serverECDHEParams := make([]byte, 1+2+1+len(ecdhePublic))
|
||||||
|
serverECDHEParams[0] = 3 // named curve
|
||||||
|
serverECDHEParams[1] = byte(curveID >> 8)
|
||||||
|
serverECDHEParams[2] = byte(curveID)
|
||||||
|
serverECDHEParams[3] = byte(len(ecdhePublic))
|
||||||
|
copy(serverECDHEParams[4:], ecdhePublic)
|
||||||
|
|
||||||
|
priv, ok := cert.PrivateKey.(crypto.Signer)
|
||||||
|
if !ok {
|
||||||
|
return nil, fmt.Errorf("tls: certificate private key of type %T does not implement crypto.Signer", cert.PrivateKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
var signatureAlgorithm SignatureScheme
|
||||||
|
var sigType uint8
|
||||||
|
var sigHash crypto.Hash
|
||||||
|
if ka.version >= VersionTLS12 {
|
||||||
|
signatureAlgorithm, err = selectSignatureScheme(ka.version, cert, clientHello.supportedSignatureAlgorithms)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
sigType, sigHash, err = typeAndHashFromSignatureScheme(signatureAlgorithm)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sigType, sigHash, err = legacyTypeAndHashFromPublicKey(priv.Public())
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sigType == signaturePKCS1v15 || sigType == signatureRSAPSS) != ka.isRSA {
|
||||||
|
return nil, errors.New("tls: certificate cannot be used with the selected cipher suite")
|
||||||
|
}
|
||||||
|
|
||||||
|
signed := hashForServerKeyExchange(sigType, sigHash, ka.version, clientHello.random, hello.random, serverECDHEParams)
|
||||||
|
|
||||||
|
signOpts := crypto.SignerOpts(sigHash)
|
||||||
|
if sigType == signatureRSAPSS {
|
||||||
|
signOpts = &rsa.PSSOptions{SaltLength: rsa.PSSSaltLengthEqualsHash, Hash: sigHash}
|
||||||
|
}
|
||||||
|
sig, err := priv.Sign(config.rand(), signed, signOpts)
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("tls: failed to sign ECDHE parameters: " + err.Error())
|
||||||
|
}
|
||||||
|
|
||||||
|
skx := new(serverKeyExchangeMsg)
|
||||||
|
sigAndHashLen := 0
|
||||||
|
if ka.version >= VersionTLS12 {
|
||||||
|
sigAndHashLen = 2
|
||||||
|
}
|
||||||
|
skx.key = make([]byte, len(serverECDHEParams)+sigAndHashLen+2+len(sig))
|
||||||
|
copy(skx.key, serverECDHEParams)
|
||||||
|
k := skx.key[len(serverECDHEParams):]
|
||||||
|
if ka.version >= VersionTLS12 {
|
||||||
|
k[0] = byte(signatureAlgorithm >> 8)
|
||||||
|
k[1] = byte(signatureAlgorithm)
|
||||||
|
k = k[2:]
|
||||||
|
}
|
||||||
|
k[0] = byte(len(sig) >> 8)
|
||||||
|
k[1] = byte(len(sig))
|
||||||
|
copy(k[2:], sig)
|
||||||
|
|
||||||
|
return skx, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka *ecdheKeyAgreement) processClientKeyExchange(config *Config, cert *Certificate, ckx *clientKeyExchangeMsg, version uint16) ([]byte, error) {
|
||||||
|
if len(ckx.ciphertext) == 0 || int(ckx.ciphertext[0]) != len(ckx.ciphertext)-1 {
|
||||||
|
return nil, errClientKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
preMasterSecret := ka.params.SharedKey(ckx.ciphertext[1:])
|
||||||
|
if preMasterSecret == nil {
|
||||||
|
return nil, errClientKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
return preMasterSecret, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka *ecdheKeyAgreement) processServerKeyExchange(config *Config, clientHello *clientHelloMsg, serverHello *serverHelloMsg, cert *x509.Certificate, skx *serverKeyExchangeMsg) error {
|
||||||
|
if len(skx.key) < 4 {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
if skx.key[0] != 3 { // named curve
|
||||||
|
return errors.New("tls: server selected unsupported curve")
|
||||||
|
}
|
||||||
|
curveID := CurveID(skx.key[1])<<8 | CurveID(skx.key[2])
|
||||||
|
|
||||||
|
publicLen := int(skx.key[3])
|
||||||
|
if publicLen+4 > len(skx.key) {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
serverECDHEParams := skx.key[:4+publicLen]
|
||||||
|
publicKey := serverECDHEParams[4:]
|
||||||
|
|
||||||
|
sig := skx.key[4+publicLen:]
|
||||||
|
if len(sig) < 2 {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := curveForCurveID(curveID); curveID != X25519 && !ok {
|
||||||
|
return errors.New("tls: server selected unsupported curve")
|
||||||
|
}
|
||||||
|
|
||||||
|
params, err := generateECDHEParameters(config.rand(), curveID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ka.params = params
|
||||||
|
|
||||||
|
ka.preMasterSecret = params.SharedKey(publicKey)
|
||||||
|
if ka.preMasterSecret == nil {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
ourPublicKey := params.PublicKey()
|
||||||
|
ka.ckx = new(clientKeyExchangeMsg)
|
||||||
|
ka.ckx.ciphertext = make([]byte, 1+len(ourPublicKey))
|
||||||
|
ka.ckx.ciphertext[0] = byte(len(ourPublicKey))
|
||||||
|
copy(ka.ckx.ciphertext[1:], ourPublicKey)
|
||||||
|
|
||||||
|
var sigType uint8
|
||||||
|
var sigHash crypto.Hash
|
||||||
|
if ka.version >= VersionTLS12 {
|
||||||
|
signatureAlgorithm := SignatureScheme(sig[0])<<8 | SignatureScheme(sig[1])
|
||||||
|
sig = sig[2:]
|
||||||
|
if len(sig) < 2 {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
if !isSupportedSignatureAlgorithm(signatureAlgorithm, clientHello.supportedSignatureAlgorithms) {
|
||||||
|
return errors.New("tls: certificate used with invalid signature algorithm")
|
||||||
|
}
|
||||||
|
sigType, sigHash, err = typeAndHashFromSignatureScheme(signatureAlgorithm)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
sigType, sigHash, err = legacyTypeAndHashFromPublicKey(cert.PublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sigType == signaturePKCS1v15 || sigType == signatureRSAPSS) != ka.isRSA {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
|
||||||
|
sigLen := int(sig[0])<<8 | int(sig[1])
|
||||||
|
if sigLen+2 != len(sig) {
|
||||||
|
return errServerKeyExchange
|
||||||
|
}
|
||||||
|
sig = sig[2:]
|
||||||
|
|
||||||
|
signed := hashForServerKeyExchange(sigType, sigHash, ka.version, clientHello.random, serverHello.random, serverECDHEParams)
|
||||||
|
if err := verifyHandshakeSignature(sigType, cert.PublicKey, sigHash, signed, sig); err != nil {
|
||||||
|
return errors.New("tls: invalid signature by the server certificate: " + err.Error())
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ka *ecdheKeyAgreement) generateClientKeyExchange(config *Config, clientHello *clientHelloMsg, cert *x509.Certificate) ([]byte, *clientKeyExchangeMsg, error) {
|
||||||
|
if ka.ckx == nil {
|
||||||
|
return nil, nil, errors.New("tls: missing ServerKeyExchange message")
|
||||||
|
}
|
||||||
|
|
||||||
|
return ka.preMasterSecret, ka.ckx, nil
|
||||||
|
}
|
199
transport/cloudflaretls/key_schedule.go
Normal file
199
transport/cloudflaretls/key_schedule.go
Normal file
|
@ -0,0 +1,199 @@
|
||||||
|
// Copyright 2018 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/elliptic"
|
||||||
|
"crypto/hmac"
|
||||||
|
"errors"
|
||||||
|
"hash"
|
||||||
|
"io"
|
||||||
|
"math/big"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
"golang.org/x/crypto/curve25519"
|
||||||
|
"golang.org/x/crypto/hkdf"
|
||||||
|
)
|
||||||
|
|
||||||
|
// This file contains the functions necessary to compute the TLS 1.3 key
|
||||||
|
// schedule. See RFC 8446, Section 7.
|
||||||
|
|
||||||
|
const (
|
||||||
|
resumptionBinderLabel = "res binder"
|
||||||
|
clientHandshakeTrafficLabel = "c hs traffic"
|
||||||
|
serverHandshakeTrafficLabel = "s hs traffic"
|
||||||
|
clientApplicationTrafficLabel = "c ap traffic"
|
||||||
|
serverApplicationTrafficLabel = "s ap traffic"
|
||||||
|
exporterLabel = "exp master"
|
||||||
|
resumptionLabel = "res master"
|
||||||
|
trafficUpdateLabel = "traffic upd"
|
||||||
|
)
|
||||||
|
|
||||||
|
// expandLabel implements HKDF-Expand-Label from RFC 8446, Section 7.1.
|
||||||
|
func (c *cipherSuiteTLS13) expandLabel(secret []byte, label string, context []byte, length int) []byte {
|
||||||
|
var hkdfLabel cryptobyte.Builder
|
||||||
|
hkdfLabel.AddUint16(uint16(length))
|
||||||
|
hkdfLabel.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes([]byte("tls13 "))
|
||||||
|
b.AddBytes([]byte(label))
|
||||||
|
})
|
||||||
|
hkdfLabel.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(context)
|
||||||
|
})
|
||||||
|
out := make([]byte, length)
|
||||||
|
n, err := hkdf.Expand(c.hash.New, secret, hkdfLabel.BytesOrPanic()).Read(out)
|
||||||
|
if err != nil || n != length {
|
||||||
|
panic("tls: HKDF-Expand-Label invocation failed unexpectedly")
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// deriveSecret implements Derive-Secret from RFC 8446, Section 7.1.
|
||||||
|
func (c *cipherSuiteTLS13) deriveSecret(secret []byte, label string, transcript hash.Hash) []byte {
|
||||||
|
if transcript == nil {
|
||||||
|
transcript = c.hash.New()
|
||||||
|
}
|
||||||
|
return c.expandLabel(secret, label, transcript.Sum(nil), c.hash.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
// extract implements HKDF-Extract with the cipher suite hash.
|
||||||
|
func (c *cipherSuiteTLS13) extract(newSecret, currentSecret []byte) []byte {
|
||||||
|
if newSecret == nil {
|
||||||
|
newSecret = make([]byte, c.hash.Size())
|
||||||
|
}
|
||||||
|
return hkdf.Extract(c.hash.New, newSecret, currentSecret)
|
||||||
|
}
|
||||||
|
|
||||||
|
// nextTrafficSecret generates the next traffic secret, given the current one,
|
||||||
|
// according to RFC 8446, Section 7.2.
|
||||||
|
func (c *cipherSuiteTLS13) nextTrafficSecret(trafficSecret []byte) []byte {
|
||||||
|
return c.expandLabel(trafficSecret, trafficUpdateLabel, nil, c.hash.Size())
|
||||||
|
}
|
||||||
|
|
||||||
|
// trafficKey generates traffic keys according to RFC 8446, Section 7.3.
|
||||||
|
func (c *cipherSuiteTLS13) trafficKey(trafficSecret []byte) (key, iv []byte) {
|
||||||
|
key = c.expandLabel(trafficSecret, "key", nil, c.keyLen)
|
||||||
|
iv = c.expandLabel(trafficSecret, "iv", nil, aeadNonceLength)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// finishedHash generates the Finished verify_data or PskBinderEntry according
|
||||||
|
// to RFC 8446, Section 4.4.4. See sections 4.4 and 4.2.11.2 for the baseKey
|
||||||
|
// selection.
|
||||||
|
func (c *cipherSuiteTLS13) finishedHash(baseKey []byte, transcript hash.Hash) []byte {
|
||||||
|
finishedKey := c.expandLabel(baseKey, "finished", nil, c.hash.Size())
|
||||||
|
verifyData := hmac.New(c.hash.New, finishedKey)
|
||||||
|
verifyData.Write(transcript.Sum(nil))
|
||||||
|
return verifyData.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// exportKeyingMaterial implements RFC5705 exporters for TLS 1.3 according to
|
||||||
|
// RFC 8446, Section 7.5.
|
||||||
|
func (c *cipherSuiteTLS13) exportKeyingMaterial(masterSecret []byte, transcript hash.Hash) func(string, []byte, int) ([]byte, error) {
|
||||||
|
expMasterSecret := c.deriveSecret(masterSecret, exporterLabel, transcript)
|
||||||
|
return func(label string, context []byte, length int) ([]byte, error) {
|
||||||
|
secret := c.deriveSecret(expMasterSecret, label, nil)
|
||||||
|
h := c.hash.New()
|
||||||
|
h.Write(context)
|
||||||
|
return c.expandLabel(secret, "exporter", h.Sum(nil), length), nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ecdheParameters implements Diffie-Hellman with either NIST curves or X25519,
|
||||||
|
// according to RFC 8446, Section 4.2.8.2.
|
||||||
|
type ecdheParameters interface {
|
||||||
|
CurveID() CurveID
|
||||||
|
PublicKey() []byte
|
||||||
|
SharedKey(peerPublicKey []byte) []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func generateECDHEParameters(rand io.Reader, curveID CurveID) (ecdheParameters, error) {
|
||||||
|
if curveID == X25519 {
|
||||||
|
privateKey := make([]byte, curve25519.ScalarSize)
|
||||||
|
if _, err := io.ReadFull(rand, privateKey); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
publicKey, err := curve25519.X25519(privateKey, curve25519.Basepoint)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &x25519Parameters{privateKey: privateKey, publicKey: publicKey}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
curve, ok := curveForCurveID(curveID)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("tls: internal error: unsupported curve")
|
||||||
|
}
|
||||||
|
|
||||||
|
p := &nistParameters{curveID: curveID}
|
||||||
|
var err error
|
||||||
|
p.privateKey, p.x, p.y, err = elliptic.GenerateKey(curve, rand)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return p, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func curveForCurveID(id CurveID) (elliptic.Curve, bool) {
|
||||||
|
switch id {
|
||||||
|
case CurveP256:
|
||||||
|
return elliptic.P256(), true
|
||||||
|
case CurveP384:
|
||||||
|
return elliptic.P384(), true
|
||||||
|
case CurveP521:
|
||||||
|
return elliptic.P521(), true
|
||||||
|
default:
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type nistParameters struct {
|
||||||
|
privateKey []byte
|
||||||
|
x, y *big.Int // public key
|
||||||
|
curveID CurveID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *nistParameters) CurveID() CurveID {
|
||||||
|
return p.curveID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *nistParameters) PublicKey() []byte {
|
||||||
|
curve, _ := curveForCurveID(p.curveID)
|
||||||
|
return elliptic.Marshal(curve, p.x, p.y)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *nistParameters) SharedKey(peerPublicKey []byte) []byte {
|
||||||
|
curve, _ := curveForCurveID(p.curveID)
|
||||||
|
// Unmarshal also checks whether the given point is on the curve.
|
||||||
|
x, y := elliptic.Unmarshal(curve, peerPublicKey)
|
||||||
|
if x == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
xShared, _ := curve.ScalarMult(x, y, p.privateKey)
|
||||||
|
sharedKey := make([]byte, (curve.Params().BitSize+7)/8)
|
||||||
|
return xShared.FillBytes(sharedKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
type x25519Parameters struct {
|
||||||
|
privateKey []byte
|
||||||
|
publicKey []byte
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *x25519Parameters) CurveID() CurveID {
|
||||||
|
return X25519
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *x25519Parameters) PublicKey() []byte {
|
||||||
|
return p.publicKey[:]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *x25519Parameters) SharedKey(peerPublicKey []byte) []byte {
|
||||||
|
sharedKey, err := curve25519.X25519(p.privateKey, peerPublicKey)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return sharedKey
|
||||||
|
}
|
285
transport/cloudflaretls/prf.go
Normal file
285
transport/cloudflaretls/prf.go
Normal file
|
@ -0,0 +1,285 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/md5"
|
||||||
|
"crypto/sha1"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/sha512"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"hash"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Split a premaster secret in two as specified in RFC 4346, Section 5.
|
||||||
|
func splitPreMasterSecret(secret []byte) (s1, s2 []byte) {
|
||||||
|
s1 = secret[0 : (len(secret)+1)/2]
|
||||||
|
s2 = secret[len(secret)/2:]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// pHash implements the P_hash function, as defined in RFC 4346, Section 5.
|
||||||
|
func pHash(result, secret, seed []byte, hash func() hash.Hash) {
|
||||||
|
h := hmac.New(hash, secret)
|
||||||
|
h.Write(seed)
|
||||||
|
a := h.Sum(nil)
|
||||||
|
|
||||||
|
j := 0
|
||||||
|
for j < len(result) {
|
||||||
|
h.Reset()
|
||||||
|
h.Write(a)
|
||||||
|
h.Write(seed)
|
||||||
|
b := h.Sum(nil)
|
||||||
|
copy(result[j:], b)
|
||||||
|
j += len(b)
|
||||||
|
|
||||||
|
h.Reset()
|
||||||
|
h.Write(a)
|
||||||
|
a = h.Sum(nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prf10 implements the TLS 1.0 pseudo-random function, as defined in RFC 2246, Section 5.
|
||||||
|
func prf10(result, secret, label, seed []byte) {
|
||||||
|
hashSHA1 := sha1.New
|
||||||
|
hashMD5 := md5.New
|
||||||
|
|
||||||
|
labelAndSeed := make([]byte, len(label)+len(seed))
|
||||||
|
copy(labelAndSeed, label)
|
||||||
|
copy(labelAndSeed[len(label):], seed)
|
||||||
|
|
||||||
|
s1, s2 := splitPreMasterSecret(secret)
|
||||||
|
pHash(result, s1, labelAndSeed, hashMD5)
|
||||||
|
result2 := make([]byte, len(result))
|
||||||
|
pHash(result2, s2, labelAndSeed, hashSHA1)
|
||||||
|
|
||||||
|
for i, b := range result2 {
|
||||||
|
result[i] ^= b
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// prf12 implements the TLS 1.2 pseudo-random function, as defined in RFC 5246, Section 5.
|
||||||
|
func prf12(hashFunc func() hash.Hash) func(result, secret, label, seed []byte) {
|
||||||
|
return func(result, secret, label, seed []byte) {
|
||||||
|
labelAndSeed := make([]byte, len(label)+len(seed))
|
||||||
|
copy(labelAndSeed, label)
|
||||||
|
copy(labelAndSeed[len(label):], seed)
|
||||||
|
|
||||||
|
pHash(result, secret, labelAndSeed, hashFunc)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
masterSecretLength = 48 // Length of a master secret in TLS 1.1.
|
||||||
|
finishedVerifyLength = 12 // Length of verify_data in a Finished message.
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
masterSecretLabel = []byte("master secret")
|
||||||
|
keyExpansionLabel = []byte("key expansion")
|
||||||
|
clientFinishedLabel = []byte("client finished")
|
||||||
|
serverFinishedLabel = []byte("server finished")
|
||||||
|
)
|
||||||
|
|
||||||
|
func prfAndHashForVersion(version uint16, suite *cipherSuite) (func(result, secret, label, seed []byte), crypto.Hash) {
|
||||||
|
switch version {
|
||||||
|
case VersionTLS10, VersionTLS11:
|
||||||
|
return prf10, crypto.Hash(0)
|
||||||
|
case VersionTLS12:
|
||||||
|
if suite.flags&suiteSHA384 != 0 {
|
||||||
|
return prf12(sha512.New384), crypto.SHA384
|
||||||
|
}
|
||||||
|
return prf12(sha256.New), crypto.SHA256
|
||||||
|
default:
|
||||||
|
panic("unknown version")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func prfForVersion(version uint16, suite *cipherSuite) func(result, secret, label, seed []byte) {
|
||||||
|
prf, _ := prfAndHashForVersion(version, suite)
|
||||||
|
return prf
|
||||||
|
}
|
||||||
|
|
||||||
|
// masterFromPreMasterSecret generates the master secret from the pre-master
|
||||||
|
// secret. See RFC 5246, Section 8.1.
|
||||||
|
func masterFromPreMasterSecret(version uint16, suite *cipherSuite, preMasterSecret, clientRandom, serverRandom []byte) []byte {
|
||||||
|
seed := make([]byte, 0, len(clientRandom)+len(serverRandom))
|
||||||
|
seed = append(seed, clientRandom...)
|
||||||
|
seed = append(seed, serverRandom...)
|
||||||
|
|
||||||
|
masterSecret := make([]byte, masterSecretLength)
|
||||||
|
prfForVersion(version, suite)(masterSecret, preMasterSecret, masterSecretLabel, seed)
|
||||||
|
return masterSecret
|
||||||
|
}
|
||||||
|
|
||||||
|
// keysFromMasterSecret generates the connection keys from the master
|
||||||
|
// secret, given the lengths of the MAC key, cipher key and IV, as defined in
|
||||||
|
// RFC 2246, Section 6.3.
|
||||||
|
func keysFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte, macLen, keyLen, ivLen int) (clientMAC, serverMAC, clientKey, serverKey, clientIV, serverIV []byte) {
|
||||||
|
seed := make([]byte, 0, len(serverRandom)+len(clientRandom))
|
||||||
|
seed = append(seed, serverRandom...)
|
||||||
|
seed = append(seed, clientRandom...)
|
||||||
|
|
||||||
|
n := 2*macLen + 2*keyLen + 2*ivLen
|
||||||
|
keyMaterial := make([]byte, n)
|
||||||
|
prfForVersion(version, suite)(keyMaterial, masterSecret, keyExpansionLabel, seed)
|
||||||
|
clientMAC = keyMaterial[:macLen]
|
||||||
|
keyMaterial = keyMaterial[macLen:]
|
||||||
|
serverMAC = keyMaterial[:macLen]
|
||||||
|
keyMaterial = keyMaterial[macLen:]
|
||||||
|
clientKey = keyMaterial[:keyLen]
|
||||||
|
keyMaterial = keyMaterial[keyLen:]
|
||||||
|
serverKey = keyMaterial[:keyLen]
|
||||||
|
keyMaterial = keyMaterial[keyLen:]
|
||||||
|
clientIV = keyMaterial[:ivLen]
|
||||||
|
keyMaterial = keyMaterial[ivLen:]
|
||||||
|
serverIV = keyMaterial[:ivLen]
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func newFinishedHash(version uint16, cipherSuite *cipherSuite) finishedHash {
|
||||||
|
var buffer []byte
|
||||||
|
if version >= VersionTLS12 {
|
||||||
|
buffer = []byte{}
|
||||||
|
}
|
||||||
|
|
||||||
|
prf, hash := prfAndHashForVersion(version, cipherSuite)
|
||||||
|
if hash != 0 {
|
||||||
|
return finishedHash{hash.New(), hash.New(), nil, nil, buffer, version, prf}
|
||||||
|
}
|
||||||
|
|
||||||
|
return finishedHash{sha1.New(), sha1.New(), md5.New(), md5.New(), buffer, version, prf}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A finishedHash calculates the hash of a set of handshake messages suitable
|
||||||
|
// for including in a Finished message.
|
||||||
|
type finishedHash struct {
|
||||||
|
client hash.Hash
|
||||||
|
server hash.Hash
|
||||||
|
|
||||||
|
// Prior to TLS 1.2, an additional MD5 hash is required.
|
||||||
|
clientMD5 hash.Hash
|
||||||
|
serverMD5 hash.Hash
|
||||||
|
|
||||||
|
// In TLS 1.2, a full buffer is sadly required.
|
||||||
|
buffer []byte
|
||||||
|
|
||||||
|
version uint16
|
||||||
|
prf func(result, secret, label, seed []byte)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h *finishedHash) Write(msg []byte) (n int, err error) {
|
||||||
|
h.client.Write(msg)
|
||||||
|
h.server.Write(msg)
|
||||||
|
|
||||||
|
if h.version < VersionTLS12 {
|
||||||
|
h.clientMD5.Write(msg)
|
||||||
|
h.serverMD5.Write(msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.buffer != nil {
|
||||||
|
h.buffer = append(h.buffer, msg...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return len(msg), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (h finishedHash) Sum() []byte {
|
||||||
|
if h.version >= VersionTLS12 {
|
||||||
|
return h.client.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make([]byte, 0, md5.Size+sha1.Size)
|
||||||
|
out = h.clientMD5.Sum(out)
|
||||||
|
return h.client.Sum(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
// clientSum returns the contents of the verify_data member of a client's
|
||||||
|
// Finished message.
|
||||||
|
func (h finishedHash) clientSum(masterSecret []byte) []byte {
|
||||||
|
out := make([]byte, finishedVerifyLength)
|
||||||
|
h.prf(out, masterSecret, clientFinishedLabel, h.Sum())
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// serverSum returns the contents of the verify_data member of a server's
|
||||||
|
// Finished message.
|
||||||
|
func (h finishedHash) serverSum(masterSecret []byte) []byte {
|
||||||
|
out := make([]byte, finishedVerifyLength)
|
||||||
|
h.prf(out, masterSecret, serverFinishedLabel, h.Sum())
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// hashForClientCertificate returns the handshake messages so far, pre-hashed if
|
||||||
|
// necessary, suitable for signing by a TLS client certificate.
|
||||||
|
func (h finishedHash) hashForClientCertificate(sigType uint8, hashAlg crypto.Hash, masterSecret []byte) []byte {
|
||||||
|
if (h.version >= VersionTLS12 || sigType == signatureEd25519 || circlSchemeBySigType(sigType) != nil) && h.buffer == nil {
|
||||||
|
panic("tls: handshake hash for a client certificate requested after discarding the handshake buffer")
|
||||||
|
}
|
||||||
|
|
||||||
|
if sigType == signatureEd25519 || circlSchemeBySigType(sigType) != nil {
|
||||||
|
return h.buffer
|
||||||
|
}
|
||||||
|
|
||||||
|
if h.version >= VersionTLS12 {
|
||||||
|
hash := hashAlg.New()
|
||||||
|
hash.Write(h.buffer)
|
||||||
|
return hash.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
if sigType == signatureECDSA {
|
||||||
|
return h.server.Sum(nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
return h.Sum()
|
||||||
|
}
|
||||||
|
|
||||||
|
// discardHandshakeBuffer is called when there is no more need to
|
||||||
|
// buffer the entirety of the handshake messages.
|
||||||
|
func (h *finishedHash) discardHandshakeBuffer() {
|
||||||
|
h.buffer = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// noExportedKeyingMaterial is used as a value of
|
||||||
|
// ConnectionState.ekm when renegotiation is enabled and thus
|
||||||
|
// we wish to fail all key-material export requests.
|
||||||
|
func noExportedKeyingMaterial(label string, context []byte, length int) ([]byte, error) {
|
||||||
|
return nil, errors.New("crypto/tls: ExportKeyingMaterial is unavailable when renegotiation is enabled")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ekmFromMasterSecret generates exported keying material as defined in RFC 5705.
|
||||||
|
func ekmFromMasterSecret(version uint16, suite *cipherSuite, masterSecret, clientRandom, serverRandom []byte) func(string, []byte, int) ([]byte, error) {
|
||||||
|
return func(label string, context []byte, length int) ([]byte, error) {
|
||||||
|
switch label {
|
||||||
|
case "client finished", "server finished", "master secret", "key expansion":
|
||||||
|
// These values are reserved and may not be used.
|
||||||
|
return nil, fmt.Errorf("crypto/tls: reserved ExportKeyingMaterial label: %s", label)
|
||||||
|
}
|
||||||
|
|
||||||
|
seedLen := len(serverRandom) + len(clientRandom)
|
||||||
|
if context != nil {
|
||||||
|
seedLen += 2 + len(context)
|
||||||
|
}
|
||||||
|
seed := make([]byte, 0, seedLen)
|
||||||
|
|
||||||
|
seed = append(seed, clientRandom...)
|
||||||
|
seed = append(seed, serverRandom...)
|
||||||
|
|
||||||
|
if context != nil {
|
||||||
|
if len(context) >= 1<<16 {
|
||||||
|
return nil, fmt.Errorf("crypto/tls: ExportKeyingMaterial context too long")
|
||||||
|
}
|
||||||
|
seed = append(seed, byte(len(context)>>8), byte(len(context)))
|
||||||
|
seed = append(seed, context...)
|
||||||
|
}
|
||||||
|
|
||||||
|
keyMaterial := make([]byte, length)
|
||||||
|
prfForVersion(version, suite)(keyMaterial, masterSecret, []byte(label), seed)
|
||||||
|
return keyMaterial, nil
|
||||||
|
}
|
||||||
|
}
|
185
transport/cloudflaretls/ticket.go
Normal file
185
transport/cloudflaretls/ticket.go
Normal file
|
@ -0,0 +1,185 @@
|
||||||
|
// Copyright 2012 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"crypto/aes"
|
||||||
|
"crypto/cipher"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
|
"crypto/subtle"
|
||||||
|
"errors"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"golang.org/x/crypto/cryptobyte"
|
||||||
|
)
|
||||||
|
|
||||||
|
// sessionState contains the information that is serialized into a session
|
||||||
|
// ticket in order to later resume a connection.
|
||||||
|
type sessionState struct {
|
||||||
|
vers uint16
|
||||||
|
cipherSuite uint16
|
||||||
|
createdAt uint64
|
||||||
|
masterSecret []byte // opaque master_secret<1..2^16-1>;
|
||||||
|
// struct { opaque certificate<1..2^24-1> } Certificate;
|
||||||
|
certificates [][]byte // Certificate certificate_list<0..2^24-1>;
|
||||||
|
|
||||||
|
// usedOldKey is true if the ticket from which this session came from
|
||||||
|
// was encrypted with an older key and thus should be refreshed.
|
||||||
|
usedOldKey bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *sessionState) marshal() []byte {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddUint16(m.vers)
|
||||||
|
b.AddUint16(m.cipherSuite)
|
||||||
|
addUint64(&b, m.createdAt)
|
||||||
|
b.AddUint16LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(m.masterSecret)
|
||||||
|
})
|
||||||
|
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
for _, cert := range m.certificates {
|
||||||
|
b.AddUint24LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(cert)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return b.BytesOrPanic()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *sessionState) unmarshal(data []byte) bool {
|
||||||
|
*m = sessionState{usedOldKey: m.usedOldKey}
|
||||||
|
s := cryptobyte.String(data)
|
||||||
|
if ok := s.ReadUint16(&m.vers) &&
|
||||||
|
s.ReadUint16(&m.cipherSuite) &&
|
||||||
|
readUint64(&s, &m.createdAt) &&
|
||||||
|
readUint16LengthPrefixed(&s, &m.masterSecret) &&
|
||||||
|
len(m.masterSecret) != 0; !ok {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
var certList cryptobyte.String
|
||||||
|
if !s.ReadUint24LengthPrefixed(&certList) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for !certList.Empty() {
|
||||||
|
var cert []byte
|
||||||
|
if !readUint24LengthPrefixed(&certList, &cert) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
m.certificates = append(m.certificates, cert)
|
||||||
|
}
|
||||||
|
return s.Empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
// sessionStateTLS13 is the content of a TLS 1.3 session ticket. Its first
|
||||||
|
// version (revision = 0) doesn't carry any of the information needed for 0-RTT
|
||||||
|
// validation and the nonce is always empty.
|
||||||
|
type sessionStateTLS13 struct {
|
||||||
|
// uint8 version = 0x0304;
|
||||||
|
// uint8 revision = 0;
|
||||||
|
cipherSuite uint16
|
||||||
|
createdAt uint64
|
||||||
|
resumptionSecret []byte // opaque resumption_master_secret<1..2^8-1>;
|
||||||
|
certificate Certificate // CertificateEntry certificate_list<0..2^24-1>;
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *sessionStateTLS13) marshal() []byte {
|
||||||
|
var b cryptobyte.Builder
|
||||||
|
b.AddUint16(VersionTLS13)
|
||||||
|
b.AddUint8(0) // revision
|
||||||
|
b.AddUint16(m.cipherSuite)
|
||||||
|
addUint64(&b, m.createdAt)
|
||||||
|
b.AddUint8LengthPrefixed(func(b *cryptobyte.Builder) {
|
||||||
|
b.AddBytes(m.resumptionSecret)
|
||||||
|
})
|
||||||
|
marshalCertificate(&b, m.certificate)
|
||||||
|
return b.BytesOrPanic()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *sessionStateTLS13) unmarshal(data []byte) bool {
|
||||||
|
*m = sessionStateTLS13{}
|
||||||
|
s := cryptobyte.String(data)
|
||||||
|
var version uint16
|
||||||
|
var revision uint8
|
||||||
|
return s.ReadUint16(&version) &&
|
||||||
|
version == VersionTLS13 &&
|
||||||
|
s.ReadUint8(&revision) &&
|
||||||
|
revision == 0 &&
|
||||||
|
s.ReadUint16(&m.cipherSuite) &&
|
||||||
|
readUint64(&s, &m.createdAt) &&
|
||||||
|
readUint8LengthPrefixed(&s, &m.resumptionSecret) &&
|
||||||
|
len(m.resumptionSecret) != 0 &&
|
||||||
|
unmarshalCertificate(&s, &m.certificate) &&
|
||||||
|
s.Empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) encryptTicket(state []byte) ([]byte, error) {
|
||||||
|
if len(c.ticketKeys) == 0 {
|
||||||
|
return nil, errors.New("tls: internal error: session ticket keys unavailable")
|
||||||
|
}
|
||||||
|
|
||||||
|
encrypted := make([]byte, ticketKeyNameLen+aes.BlockSize+len(state)+sha256.Size)
|
||||||
|
keyName := encrypted[:ticketKeyNameLen]
|
||||||
|
iv := encrypted[ticketKeyNameLen : ticketKeyNameLen+aes.BlockSize]
|
||||||
|
macBytes := encrypted[len(encrypted)-sha256.Size:]
|
||||||
|
|
||||||
|
if _, err := io.ReadFull(c.config.rand(), iv); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
key := c.ticketKeys[0]
|
||||||
|
copy(keyName, key.keyName[:])
|
||||||
|
block, err := aes.NewCipher(key.aesKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("tls: failed to create cipher while encrypting ticket: " + err.Error())
|
||||||
|
}
|
||||||
|
cipher.NewCTR(block, iv).XORKeyStream(encrypted[ticketKeyNameLen+aes.BlockSize:], state)
|
||||||
|
|
||||||
|
mac := hmac.New(sha256.New, key.hmacKey[:])
|
||||||
|
mac.Write(encrypted[:len(encrypted)-sha256.Size])
|
||||||
|
mac.Sum(macBytes[:0])
|
||||||
|
|
||||||
|
return encrypted, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Conn) decryptTicket(encrypted []byte) (plaintext []byte, usedOldKey bool) {
|
||||||
|
if len(encrypted) < ticketKeyNameLen+aes.BlockSize+sha256.Size {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
keyName := encrypted[:ticketKeyNameLen]
|
||||||
|
iv := encrypted[ticketKeyNameLen : ticketKeyNameLen+aes.BlockSize]
|
||||||
|
macBytes := encrypted[len(encrypted)-sha256.Size:]
|
||||||
|
ciphertext := encrypted[ticketKeyNameLen+aes.BlockSize : len(encrypted)-sha256.Size]
|
||||||
|
|
||||||
|
keyIndex := -1
|
||||||
|
for i, candidateKey := range c.ticketKeys {
|
||||||
|
if bytes.Equal(keyName, candidateKey.keyName[:]) {
|
||||||
|
keyIndex = i
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if keyIndex == -1 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
key := &c.ticketKeys[keyIndex]
|
||||||
|
|
||||||
|
mac := hmac.New(sha256.New, key.hmacKey[:])
|
||||||
|
mac.Write(encrypted[:len(encrypted)-sha256.Size])
|
||||||
|
expected := mac.Sum(nil)
|
||||||
|
|
||||||
|
if subtle.ConstantTimeCompare(macBytes, expected) != 1 {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
|
||||||
|
block, err := aes.NewCipher(key.aesKey[:])
|
||||||
|
if err != nil {
|
||||||
|
return nil, false
|
||||||
|
}
|
||||||
|
plaintext = make([]byte, len(ciphertext))
|
||||||
|
cipher.NewCTR(block, iv).XORKeyStream(plaintext, ciphertext)
|
||||||
|
|
||||||
|
return plaintext, keyIndex > 0
|
||||||
|
}
|
410
transport/cloudflaretls/tls.go
Normal file
410
transport/cloudflaretls/tls.go
Normal file
|
@ -0,0 +1,410 @@
|
||||||
|
// Copyright 2009 The Go Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
// Package tls partially implements TLS 1.2, as specified in RFC 5246,
|
||||||
|
// and TLS 1.3, as specified in RFC 8446.
|
||||||
|
//
|
||||||
|
// This package implements the "Encrypted ClientHello (ECH)" extension, as
|
||||||
|
// specified by draft-ietf-tls-esni-13. This extension allows the client to
|
||||||
|
// encrypt its ClientHello to the public key of an ECH-service provider, known
|
||||||
|
// as the client-facing server. If successful, then the client-facing server
|
||||||
|
// forwards the decrypted ClientHello to the intended recipient, known as the
|
||||||
|
// backend server. The goal of this mechanism is to ensure that connections made
|
||||||
|
// to backend servers are indistinguishable from one another.
|
||||||
|
//
|
||||||
|
// This package implements the "Delegated Credentials" extension, as
|
||||||
|
// specified by draft-ietf-tls-subcerts-10. This extension allows the usage
|
||||||
|
// of a limited delegation mechanism that allows a TLS peer to issue its own
|
||||||
|
// credentials within the scope of a certificate issued by an external
|
||||||
|
// CA. These credentials only enable the recipient of the delegation to
|
||||||
|
// speak for names that the CA has authorized. If the client or server supports
|
||||||
|
// this extension, then the server or client may use a "delegated credential"
|
||||||
|
// as the signing key in the handshake. A delegated credential is a short lived
|
||||||
|
// public/secret key pair delegated to the peer by an entity trusted by the
|
||||||
|
// corresponding peer. This allows a reverse proxy to terminate a TLS connection
|
||||||
|
// on behalf of the entity. Credentials can't be revoked; in order to
|
||||||
|
// mitigate risk in case the reverse proxy is compromised, the credential is only
|
||||||
|
// valid for a short time (days, hours, or even minutes).
|
||||||
|
package tls
|
||||||
|
|
||||||
|
// BUG(cjpatton): In order to achieve its security goal, the ECH extension
|
||||||
|
// requires padding in order to ensure that the length of handshake messages
|
||||||
|
// doesn't depend on who terminates the connection. This package does not yet
|
||||||
|
// implement server-side padding: see
|
||||||
|
// https://github.com/tlswg/draft-ietf-tls-esni/issues/264.
|
||||||
|
|
||||||
|
// BUG(cjpatton): The interaction of the ECH extension with PSK has not yet been
|
||||||
|
// fully vetted. For now, the server disables session tickets if ECH is enabled.
|
||||||
|
|
||||||
|
// BUG(cjpatton): Upon ECH rejection, if retry configurations are provided, then
|
||||||
|
// the client is expected to retry the connection. Otherwise, it may regard ECH
|
||||||
|
// as being securely disabled by the client-facing server. The client in this
|
||||||
|
// package does not attempt to retry the handshake.
|
||||||
|
|
||||||
|
// BUG(cjpatton): If the client offers the ECH extension and the client-facing
|
||||||
|
// server rejects it, then only the client-facing server is authenticated. In
|
||||||
|
// particular, the client is expected to respond to a CertificateRequest with an
|
||||||
|
// empty certificate. This package does not yet implement this behavior.
|
||||||
|
|
||||||
|
// BUG(agl): The crypto/tls package only implements some countermeasures
|
||||||
|
// against Lucky13 attacks on CBC-mode encryption, and only on SHA1
|
||||||
|
// variants. See http://www.isg.rhul.ac.uk/tls/TLStiming.pdf and
|
||||||
|
// https://www.imperialviolet.org/2013/02/04/luckythirteen.html.
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"context"
|
||||||
|
"crypto"
|
||||||
|
"crypto/ecdsa"
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/rsa"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"net"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Server returns a new TLS server side connection
|
||||||
|
// using conn as the underlying transport.
|
||||||
|
// The configuration config must be non-nil and must include
|
||||||
|
// at least one certificate or else set GetCertificate.
|
||||||
|
func Server(conn net.Conn, config *Config) *Conn {
|
||||||
|
c := &Conn{
|
||||||
|
conn: conn,
|
||||||
|
config: config,
|
||||||
|
}
|
||||||
|
c.handshakeFn = c.serverHandshake
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// Client returns a new TLS client side connection
|
||||||
|
// using conn as the underlying transport.
|
||||||
|
// The config cannot be nil: users must set either ServerName or
|
||||||
|
// InsecureSkipVerify in the config.
|
||||||
|
func Client(conn net.Conn, config *Config) *Conn {
|
||||||
|
c := &Conn{
|
||||||
|
conn: conn,
|
||||||
|
config: config,
|
||||||
|
isClient: true,
|
||||||
|
}
|
||||||
|
c.handshakeFn = c.clientHandshake
|
||||||
|
return c
|
||||||
|
}
|
||||||
|
|
||||||
|
// A listener implements a network listener (net.Listener) for TLS connections.
|
||||||
|
type listener struct {
|
||||||
|
net.Listener
|
||||||
|
config *Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accept waits for and returns the next incoming TLS connection.
|
||||||
|
// The returned connection is of type *Conn.
|
||||||
|
func (l *listener) Accept() (net.Conn, error) {
|
||||||
|
c, err := l.Listener.Accept()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return Server(c, l.config), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// NewListener creates a Listener which accepts connections from an inner
|
||||||
|
// Listener and wraps each connection with Server.
|
||||||
|
// The configuration config must be non-nil and must include
|
||||||
|
// at least one certificate or else set GetCertificate.
|
||||||
|
func NewListener(inner net.Listener, config *Config) net.Listener {
|
||||||
|
l := new(listener)
|
||||||
|
l.Listener = inner
|
||||||
|
l.config = config
|
||||||
|
return l
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen creates a TLS listener accepting connections on the
|
||||||
|
// given network address using net.Listen.
|
||||||
|
// The configuration config must be non-nil and must include
|
||||||
|
// at least one certificate or else set GetCertificate.
|
||||||
|
func Listen(network, laddr string, config *Config) (net.Listener, error) {
|
||||||
|
if config == nil || len(config.Certificates) == 0 &&
|
||||||
|
config.GetCertificate == nil && config.GetConfigForClient == nil {
|
||||||
|
return nil, errors.New("tls: neither Certificates, GetCertificate, nor GetConfigForClient set in Config")
|
||||||
|
}
|
||||||
|
l, err := net.Listen(network, laddr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return NewListener(l, config), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type timeoutError struct{}
|
||||||
|
|
||||||
|
func (timeoutError) Error() string { return "tls: DialWithDialer timed out" }
|
||||||
|
func (timeoutError) Timeout() bool { return true }
|
||||||
|
func (timeoutError) Temporary() bool { return true }
|
||||||
|
|
||||||
|
// DialWithDialer connects to the given network address using dialer.Dial and
|
||||||
|
// then initiates a TLS handshake, returning the resulting TLS connection. Any
|
||||||
|
// timeout or deadline given in the dialer apply to connection and TLS
|
||||||
|
// handshake as a whole.
|
||||||
|
//
|
||||||
|
// DialWithDialer interprets a nil configuration as equivalent to the zero
|
||||||
|
// configuration; see the documentation of Config for the defaults.
|
||||||
|
//
|
||||||
|
// DialWithDialer uses context.Background internally; to specify the context,
|
||||||
|
// use Dialer.DialContext with NetDialer set to the desired dialer.
|
||||||
|
func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
|
||||||
|
return dial(context.Background(), dialer, network, addr, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
func dial(ctx context.Context, netDialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
|
||||||
|
if netDialer.Timeout != 0 {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithTimeout(ctx, netDialer.Timeout)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
if !netDialer.Deadline.IsZero() {
|
||||||
|
var cancel context.CancelFunc
|
||||||
|
ctx, cancel = context.WithDeadline(ctx, netDialer.Deadline)
|
||||||
|
defer cancel()
|
||||||
|
}
|
||||||
|
|
||||||
|
rawConn, err := netDialer.DialContext(ctx, network, addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
colonPos := strings.LastIndex(addr, ":")
|
||||||
|
if colonPos == -1 {
|
||||||
|
colonPos = len(addr)
|
||||||
|
}
|
||||||
|
hostname := addr[:colonPos]
|
||||||
|
|
||||||
|
if config == nil {
|
||||||
|
config = defaultConfig()
|
||||||
|
}
|
||||||
|
// If no ServerName is set, infer the ServerName
|
||||||
|
// from the hostname we're connecting to.
|
||||||
|
if config.ServerName == "" {
|
||||||
|
// Make a copy to avoid polluting argument or default.
|
||||||
|
c := config.Clone()
|
||||||
|
c.ServerName = hostname
|
||||||
|
config = c
|
||||||
|
}
|
||||||
|
|
||||||
|
conn := Client(rawConn, config)
|
||||||
|
if err := conn.HandshakeContext(ctx); err != nil {
|
||||||
|
rawConn.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return conn, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial connects to the given network address using net.Dial
|
||||||
|
// and then initiates a TLS handshake, returning the resulting
|
||||||
|
// TLS connection.
|
||||||
|
// Dial interprets a nil configuration as equivalent to
|
||||||
|
// the zero configuration; see the documentation of Config
|
||||||
|
// for the defaults.
|
||||||
|
func Dial(network, addr string, config *Config) (*Conn, error) {
|
||||||
|
return DialWithDialer(new(net.Dialer), network, addr, config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dialer dials TLS connections given a configuration and a Dialer for the
|
||||||
|
// underlying connection.
|
||||||
|
type Dialer struct {
|
||||||
|
// NetDialer is the optional dialer to use for the TLS connections'
|
||||||
|
// underlying TCP connections.
|
||||||
|
// A nil NetDialer is equivalent to the net.Dialer zero value.
|
||||||
|
NetDialer *net.Dialer
|
||||||
|
|
||||||
|
// Config is the TLS configuration to use for new connections.
|
||||||
|
// A nil configuration is equivalent to the zero
|
||||||
|
// configuration; see the documentation of Config for the
|
||||||
|
// defaults.
|
||||||
|
Config *Config
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dial connects to the given network address and initiates a TLS
|
||||||
|
// handshake, returning the resulting TLS connection.
|
||||||
|
//
|
||||||
|
// The returned Conn, if any, will always be of type *Conn.
|
||||||
|
//
|
||||||
|
// Dial uses context.Background internally; to specify the context,
|
||||||
|
// use DialContext.
|
||||||
|
func (d *Dialer) Dial(network, addr string) (net.Conn, error) {
|
||||||
|
return d.DialContext(context.Background(), network, addr)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (d *Dialer) netDialer() *net.Dialer {
|
||||||
|
if d.NetDialer != nil {
|
||||||
|
return d.NetDialer
|
||||||
|
}
|
||||||
|
return new(net.Dialer)
|
||||||
|
}
|
||||||
|
|
||||||
|
// DialContext connects to the given network address and initiates a TLS
|
||||||
|
// handshake, returning the resulting TLS connection.
|
||||||
|
//
|
||||||
|
// The provided Context must be non-nil. If the context expires before
|
||||||
|
// the connection is complete, an error is returned. Once successfully
|
||||||
|
// connected, any expiration of the context will not affect the
|
||||||
|
// connection.
|
||||||
|
//
|
||||||
|
// The returned Conn, if any, will always be of type *Conn.
|
||||||
|
func (d *Dialer) DialContext(ctx context.Context, network, addr string) (net.Conn, error) {
|
||||||
|
c, err := dial(ctx, d.netDialer(), network, addr, d.Config)
|
||||||
|
if err != nil {
|
||||||
|
// Don't return c (a typed nil) in an interface.
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return c, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// LoadX509KeyPair reads and parses a public/private key pair from a pair
|
||||||
|
// of files. The files must contain PEM encoded data. The certificate file
|
||||||
|
// may contain intermediate certificates following the leaf certificate to
|
||||||
|
// form a certificate chain. On successful return, Certificate.Leaf will
|
||||||
|
// be nil because the parsed form of the certificate is not retained.
|
||||||
|
func LoadX509KeyPair(certFile, keyFile string) (Certificate, error) {
|
||||||
|
certPEMBlock, err := os.ReadFile(certFile)
|
||||||
|
if err != nil {
|
||||||
|
return Certificate{}, err
|
||||||
|
}
|
||||||
|
keyPEMBlock, err := os.ReadFile(keyFile)
|
||||||
|
if err != nil {
|
||||||
|
return Certificate{}, err
|
||||||
|
}
|
||||||
|
return X509KeyPair(certPEMBlock, keyPEMBlock)
|
||||||
|
}
|
||||||
|
|
||||||
|
// X509KeyPair parses a public/private key pair from a pair of
|
||||||
|
// PEM encoded data. On successful return, Certificate.Leaf will be nil because
|
||||||
|
// the parsed form of the certificate is not retained.
|
||||||
|
func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (Certificate, error) {
|
||||||
|
fail := func(err error) (Certificate, error) { return Certificate{}, err }
|
||||||
|
|
||||||
|
var cert Certificate
|
||||||
|
var skippedBlockTypes []string
|
||||||
|
for {
|
||||||
|
var certDERBlock *pem.Block
|
||||||
|
certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
|
||||||
|
if certDERBlock == nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if certDERBlock.Type == "CERTIFICATE" {
|
||||||
|
cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
|
||||||
|
} else {
|
||||||
|
skippedBlockTypes = append(skippedBlockTypes, certDERBlock.Type)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(cert.Certificate) == 0 {
|
||||||
|
if len(skippedBlockTypes) == 0 {
|
||||||
|
return fail(errors.New("tls: failed to find any PEM data in certificate input"))
|
||||||
|
}
|
||||||
|
if len(skippedBlockTypes) == 1 && strings.HasSuffix(skippedBlockTypes[0], "PRIVATE KEY") {
|
||||||
|
return fail(errors.New("tls: failed to find certificate PEM data in certificate input, but did find a private key; PEM inputs may have been switched"))
|
||||||
|
}
|
||||||
|
return fail(fmt.Errorf("tls: failed to find \"CERTIFICATE\" PEM block in certificate input after skipping PEM blocks of the following types: %v", skippedBlockTypes))
|
||||||
|
}
|
||||||
|
|
||||||
|
skippedBlockTypes = skippedBlockTypes[:0]
|
||||||
|
var keyDERBlock *pem.Block
|
||||||
|
for {
|
||||||
|
keyDERBlock, keyPEMBlock = pem.Decode(keyPEMBlock)
|
||||||
|
if keyDERBlock == nil {
|
||||||
|
if len(skippedBlockTypes) == 0 {
|
||||||
|
return fail(errors.New("tls: failed to find any PEM data in key input"))
|
||||||
|
}
|
||||||
|
if len(skippedBlockTypes) == 1 && skippedBlockTypes[0] == "CERTIFICATE" {
|
||||||
|
return fail(errors.New("tls: found a certificate rather than a key in the PEM for the private key"))
|
||||||
|
}
|
||||||
|
return fail(fmt.Errorf("tls: failed to find PEM block with type ending in \"PRIVATE KEY\" in key input after skipping PEM blocks of the following types: %v", skippedBlockTypes))
|
||||||
|
}
|
||||||
|
if keyDERBlock.Type == "PRIVATE KEY" || strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
skippedBlockTypes = append(skippedBlockTypes, keyDERBlock.Type)
|
||||||
|
}
|
||||||
|
|
||||||
|
// We don't need to parse the public key for TLS, but we so do anyway
|
||||||
|
// to check that it looks sane and matches the private key.
|
||||||
|
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
|
||||||
|
if err != nil {
|
||||||
|
return fail(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes)
|
||||||
|
if err != nil {
|
||||||
|
return fail(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
switch pub := x509Cert.PublicKey.(type) {
|
||||||
|
case *rsa.PublicKey:
|
||||||
|
priv, ok := cert.PrivateKey.(*rsa.PrivateKey)
|
||||||
|
if !ok {
|
||||||
|
return fail(errors.New("tls: private key type does not match public key type"))
|
||||||
|
}
|
||||||
|
if pub.N.Cmp(priv.N) != 0 {
|
||||||
|
return fail(errors.New("tls: private key does not match public key"))
|
||||||
|
}
|
||||||
|
case *ecdsa.PublicKey:
|
||||||
|
priv, ok := cert.PrivateKey.(*ecdsa.PrivateKey)
|
||||||
|
if !ok {
|
||||||
|
return fail(errors.New("tls: private key type does not match public key type"))
|
||||||
|
}
|
||||||
|
if pub.X.Cmp(priv.X) != 0 || pub.Y.Cmp(priv.Y) != 0 {
|
||||||
|
return fail(errors.New("tls: private key does not match public key"))
|
||||||
|
}
|
||||||
|
case ed25519.PublicKey:
|
||||||
|
priv, ok := cert.PrivateKey.(ed25519.PrivateKey)
|
||||||
|
if !ok {
|
||||||
|
return fail(errors.New("tls: private key type does not match public key type"))
|
||||||
|
}
|
||||||
|
if !bytes.Equal(priv.Public().(ed25519.PublicKey), pub) {
|
||||||
|
return fail(errors.New("tls: private key does not match public key"))
|
||||||
|
}
|
||||||
|
case circlSign.PublicKey:
|
||||||
|
priv, ok := cert.PrivateKey.(circlSign.PrivateKey)
|
||||||
|
if !ok {
|
||||||
|
return fail(errors.New("tls: private key type does not match public key type"))
|
||||||
|
}
|
||||||
|
pkBytes, err := priv.Public().(circlSign.PublicKey).MarshalBinary()
|
||||||
|
pkBytes2, err2 := pub.MarshalBinary()
|
||||||
|
|
||||||
|
if err != nil || err2 != nil || !bytes.Equal(pkBytes, pkBytes2) {
|
||||||
|
return fail(errors.New("tls: private key does not match public key"))
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return fail(errors.New("tls: unknown public key algorithm"))
|
||||||
|
}
|
||||||
|
|
||||||
|
return cert, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to parse the given private key DER block. OpenSSL 0.9.8 generates
|
||||||
|
// PKCS #1 private keys by default, while OpenSSL 1.0.0 generates PKCS #8 keys.
|
||||||
|
// OpenSSL ecparam generates SEC1 EC private keys for ECDSA. We try all three.
|
||||||
|
func parsePrivateKey(der []byte) (crypto.PrivateKey, error) {
|
||||||
|
if key, err := x509.ParsePKCS1PrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
if key, err := x509.ParsePKCS8PrivateKey(der); err == nil {
|
||||||
|
switch key := key.(type) {
|
||||||
|
case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey, circlSign.PrivateKey:
|
||||||
|
return key, nil
|
||||||
|
default:
|
||||||
|
return nil, errors.New("tls: found unknown private key type in PKCS#8 wrapping")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if key, err := x509.ParseECPrivateKey(der); err == nil {
|
||||||
|
return key, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, errors.New("tls: failed to parse private key")
|
||||||
|
}
|
241
transport/cloudflaretls/tls_cf.go
Normal file
241
transport/cloudflaretls/tls_cf.go
Normal file
|
@ -0,0 +1,241 @@
|
||||||
|
// Copyright 2021 Cloudflare, Inc. All rights reserved. Use of this source code
|
||||||
|
// is governed by a BSD-style license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package tls
|
||||||
|
|
||||||
|
import (
|
||||||
|
"time"
|
||||||
|
|
||||||
|
circlPki "github.com/cloudflare/circl/pki"
|
||||||
|
circlSign "github.com/cloudflare/circl/sign"
|
||||||
|
"github.com/cloudflare/circl/sign/eddilithium3"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// Constants for ECH status events.
|
||||||
|
echStatusBypassed = 1 + iota
|
||||||
|
echStatusInner
|
||||||
|
echStatusOuter
|
||||||
|
)
|
||||||
|
|
||||||
|
// To add a signature scheme from Circl
|
||||||
|
//
|
||||||
|
// 1. make sure it implements TLSScheme and CertificateScheme,
|
||||||
|
// 2. follow the instructions in crypto/x509/x509_cf.go
|
||||||
|
// 3. add a signature<NameOfAlg> to the iota in common.go
|
||||||
|
// 4. add row in the circlSchemes lists below
|
||||||
|
|
||||||
|
var circlSchemes = [...]struct {
|
||||||
|
sigType uint8
|
||||||
|
scheme circlSign.Scheme
|
||||||
|
}{
|
||||||
|
{signatureEdDilithium3, eddilithium3.Scheme()},
|
||||||
|
}
|
||||||
|
|
||||||
|
func circlSchemeBySigType(sigType uint8) circlSign.Scheme {
|
||||||
|
for _, cs := range circlSchemes {
|
||||||
|
if cs.sigType == sigType {
|
||||||
|
return cs.scheme
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func sigTypeByCirclScheme(scheme circlSign.Scheme) uint8 {
|
||||||
|
for _, cs := range circlSchemes {
|
||||||
|
if cs.scheme == scheme {
|
||||||
|
return cs.sigType
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
var supportedSignatureAlgorithmsWithCircl []SignatureScheme
|
||||||
|
|
||||||
|
// supportedSignatureAlgorithms returns enabled signature schemes. PQ signature
|
||||||
|
// schemes are only included when tls.Config#PQSignatureSchemesEnabled is set.
|
||||||
|
func (c *Config) supportedSignatureAlgorithms() []SignatureScheme {
|
||||||
|
if c != nil && c.PQSignatureSchemesEnabled {
|
||||||
|
return supportedSignatureAlgorithmsWithCircl
|
||||||
|
}
|
||||||
|
return supportedSignatureAlgorithms
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
supportedSignatureAlgorithmsWithCircl = append([]SignatureScheme{}, supportedSignatureAlgorithms...)
|
||||||
|
for _, cs := range circlSchemes {
|
||||||
|
supportedSignatureAlgorithmsWithCircl = append(supportedSignatureAlgorithmsWithCircl,
|
||||||
|
SignatureScheme(cs.scheme.(circlPki.TLSScheme).TLSIdentifier()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEvent is a value emitted at various points in the handshake that is
|
||||||
|
// handled by the callback Config.CFEventHandler.
|
||||||
|
type CFEvent interface {
|
||||||
|
Name() string
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventTLS13ClientHandshakeTimingInfo carries intra-stack time durations for
|
||||||
|
// TLS 1.3 client-state machine changes. It can be used for tracking metrics
|
||||||
|
// during a connection. Some durations may be sensitive, such as the amount of
|
||||||
|
// time to process a particular handshake message, so this event should only be
|
||||||
|
// used for experimental purposes.
|
||||||
|
type CFEventTLS13ClientHandshakeTimingInfo struct {
|
||||||
|
timer func() time.Time
|
||||||
|
start time.Time
|
||||||
|
WriteClientHello time.Duration
|
||||||
|
ProcessServerHello time.Duration
|
||||||
|
ReadEncryptedExtensions time.Duration
|
||||||
|
ReadCertificate time.Duration
|
||||||
|
ReadCertificateVerify time.Duration
|
||||||
|
ReadServerFinished time.Duration
|
||||||
|
WriteCertificate time.Duration
|
||||||
|
WriteCertificateVerify time.Duration
|
||||||
|
WriteClientFinished time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name is required by the CFEvent interface.
|
||||||
|
func (e CFEventTLS13ClientHandshakeTimingInfo) Name() string {
|
||||||
|
return "TLS13ClientHandshakeTimingInfo"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CFEventTLS13ClientHandshakeTimingInfo) elapsedTime() time.Duration {
|
||||||
|
if e.timer == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return e.timer().Sub(e.start)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTLS13ClientHandshakeTimingInfo(timerFunc func() time.Time) CFEventTLS13ClientHandshakeTimingInfo {
|
||||||
|
timer := time.Now
|
||||||
|
if timerFunc != nil {
|
||||||
|
timer = timerFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
return CFEventTLS13ClientHandshakeTimingInfo{
|
||||||
|
timer: timer,
|
||||||
|
start: timer(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventTLS13ServerHandshakeTimingInfo carries intra-stack time durations
|
||||||
|
// for TLS 1.3 state machine changes. It can be used for tracking metrics during a
|
||||||
|
// connection. Some durations may be sensitive, such as the amount of time to
|
||||||
|
// process a particular handshake message, so this event should only be used
|
||||||
|
// for experimental purposes.
|
||||||
|
type CFEventTLS13ServerHandshakeTimingInfo struct {
|
||||||
|
timer func() time.Time
|
||||||
|
start time.Time
|
||||||
|
ProcessClientHello time.Duration
|
||||||
|
WriteServerHello time.Duration
|
||||||
|
WriteEncryptedExtensions time.Duration
|
||||||
|
WriteCertificate time.Duration
|
||||||
|
WriteCertificateVerify time.Duration
|
||||||
|
WriteServerFinished time.Duration
|
||||||
|
ReadCertificate time.Duration
|
||||||
|
ReadCertificateVerify time.Duration
|
||||||
|
ReadClientFinished time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name is required by the CFEvent interface.
|
||||||
|
func (e CFEventTLS13ServerHandshakeTimingInfo) Name() string {
|
||||||
|
return "TLS13ServerHandshakeTimingInfo"
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CFEventTLS13ServerHandshakeTimingInfo) elapsedTime() time.Duration {
|
||||||
|
if e.timer == nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
return e.timer().Sub(e.start)
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTLS13ServerHandshakeTimingInfo(timerFunc func() time.Time) CFEventTLS13ServerHandshakeTimingInfo {
|
||||||
|
timer := time.Now
|
||||||
|
if timerFunc != nil {
|
||||||
|
timer = timerFunc
|
||||||
|
}
|
||||||
|
|
||||||
|
return CFEventTLS13ServerHandshakeTimingInfo{
|
||||||
|
timer: timer,
|
||||||
|
start: timer(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventECHClientStatus is emitted once it is known whether the client
|
||||||
|
// bypassed, offered, or greased ECH.
|
||||||
|
type CFEventECHClientStatus int
|
||||||
|
|
||||||
|
// Bypassed returns true if the client bypassed ECH.
|
||||||
|
func (e CFEventECHClientStatus) Bypassed() bool {
|
||||||
|
return e == echStatusBypassed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Offered returns true if the client offered ECH.
|
||||||
|
func (e CFEventECHClientStatus) Offered() bool {
|
||||||
|
return e == echStatusInner
|
||||||
|
}
|
||||||
|
|
||||||
|
// Greased returns true if the client greased ECH.
|
||||||
|
func (e CFEventECHClientStatus) Greased() bool {
|
||||||
|
return e == echStatusOuter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name is required by the CFEvent interface.
|
||||||
|
func (e CFEventECHClientStatus) Name() string {
|
||||||
|
return "ech client status"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventECHServerStatus is emitted once it is known whether the client
|
||||||
|
// bypassed, offered, or greased ECH.
|
||||||
|
type CFEventECHServerStatus int
|
||||||
|
|
||||||
|
// Bypassed returns true if the client bypassed ECH.
|
||||||
|
func (e CFEventECHServerStatus) Bypassed() bool {
|
||||||
|
return e == echStatusBypassed
|
||||||
|
}
|
||||||
|
|
||||||
|
// Accepted returns true if the client offered ECH.
|
||||||
|
func (e CFEventECHServerStatus) Accepted() bool {
|
||||||
|
return e == echStatusInner
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rejected returns true if the client greased ECH.
|
||||||
|
func (e CFEventECHServerStatus) Rejected() bool {
|
||||||
|
return e == echStatusOuter
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name is required by the CFEvent interface.
|
||||||
|
func (e CFEventECHServerStatus) Name() string {
|
||||||
|
return "ech server status"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventECHPublicNameMismatch is emitted if the outer SNI does not match
|
||||||
|
// match the public name of the ECH configuration. Note that we do not record
|
||||||
|
// the outer SNI in order to avoid collecting this potentially sensitive data.
|
||||||
|
type CFEventECHPublicNameMismatch struct{}
|
||||||
|
|
||||||
|
// Name is required by the CFEvent interface.
|
||||||
|
func (e CFEventECHPublicNameMismatch) Name() string {
|
||||||
|
return "ech public name does not match outer sni"
|
||||||
|
}
|
||||||
|
|
||||||
|
// For backwards compatibility.
|
||||||
|
type CFEventTLS13NegotiatedKEX = CFEventTLSNegotiatedNamedKEX
|
||||||
|
|
||||||
|
// CFEventTLSNegotiatedNamedKEX is emitted when a key agreement mechanism has been
|
||||||
|
// established that uses a named group. This includes all key agreements
|
||||||
|
// in TLSv1.3, but excludes RSA and DH in TLS 1.2 and earlier.
|
||||||
|
type CFEventTLSNegotiatedNamedKEX struct {
|
||||||
|
KEX CurveID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e CFEventTLSNegotiatedNamedKEX) Name() string {
|
||||||
|
return "CFEventTLSNegotiatedNamedKEX"
|
||||||
|
}
|
||||||
|
|
||||||
|
// CFEventTLS13HRR is emitted when a HRR is sent or received
|
||||||
|
type CFEventTLS13HRR struct{}
|
||||||
|
|
||||||
|
func (e CFEventTLS13HRR) Name() string {
|
||||||
|
return "CFEventTLS13HRR"
|
||||||
|
}
|
Loading…
Reference in a new issue