Skip to content

Commit ccb8370

Browse files
claucecejhoyla
authored andcommitted
crypto/tls: implement draft-ietf-tls-subcerts-10
* Define API for delegated credentials so they are fetched using the same mechanisms used to fetch certificates * Allow the usage of other keyUsage when checking for the dc extension. Fixes issues in earlier patch, addressing #127, #128, #129, #130, and #131. Add tool for generating delegated credentials. Co-authored-by: Jonathan Hoyland <jhoyland@cloudflare.com>
1 parent dcd22e8 commit ccb8370

19 files changed

+1874
-34
lines changed

src/crypto/tls/auth.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,28 @@ func signatureSchemesForCertificate(version uint16, cert *Certificate) []Signatu
260260
return sigAlgs
261261
}
262262

263+
// selectSignatureSchemeDC picks a SignatureScheme from the peer's preference list
264+
// that works with the selected delegated credential. It's only called for protocol
265+
// versions that support delegated credential, so TLS 1.3.
266+
func selectSignatureSchemeDC(vers uint16, dc *DelegatedCredential, peerAlgs []SignatureScheme, peerAlgsDC []SignatureScheme) (SignatureScheme, error) {
267+
if vers != VersionTLS13 {
268+
return 0, errors.New("unsupported TLS version for dc")
269+
}
270+
271+
if !isSupportedSignatureAlgorithm(dc.algorithm, peerAlgs) {
272+
return undefinedSignatureScheme, errors.New("tls: peer doesn't support the delegated credential's signature")
273+
}
274+
275+
// Pick signature scheme in the peer's preference order, as our
276+
// preference order is not configurable.
277+
for _, preferredAlg := range peerAlgsDC {
278+
if preferredAlg == dc.cred.expCertVerfAlgo {
279+
return preferredAlg, nil
280+
}
281+
}
282+
return 0, errors.New("tls: peer doesn't support the delegated credential's signature algorithm")
283+
}
284+
263285
// selectSignatureScheme picks a SignatureScheme from the peer's preference list
264286
// that works with the selected certificate. It's only called for protocol
265287
// versions that support signature algorithms, so TLS 1.2 and 1.3.

src/crypto/tls/common.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ const (
9090
extensionSignatureAlgorithms uint16 = 13
9191
extensionALPN uint16 = 16
9292
extensionSCT uint16 = 18
93+
extensionDelegatedCredentials uint16 = 34
9394
extensionSessionTicket uint16 = 35
9495
extensionPreSharedKey uint16 = 41
9596
extensionEarlyData uint16 = 42
@@ -191,6 +192,16 @@ var defaultSupportedSignatureAlgorithms = []SignatureScheme{
191192
ECDSAWithSHA1,
192193
}
193194

195+
// supportedSignatureAlgorithmsDC contains the signature and hash algorithms that
196+
// the code advertises as supported in a TLS 1.3 ClientHello and in a TLS 1.3
197+
// CertificateRequest. This excludes 'rsa_pss_rsae_' algorithms.
198+
var supportedSignatureAlgorithmsDC = []SignatureScheme{
199+
ECDSAWithP256AndSHA256,
200+
Ed25519,
201+
ECDSAWithP384AndSHA384,
202+
ECDSAWithP521AndSHA512,
203+
}
204+
194205
// helloRetryRequestRandom is set as the Random value of a ServerHello
195206
// to signal that the message is actually a HelloRetryRequest.
196207
var helloRetryRequestRandom = []byte{ // See RFC 8446, Section 4.1.3.
@@ -258,6 +269,11 @@ type ConnectionState struct {
258269
// (and the peer provided a certificate) or RequireAndVerifyClientCert.
259270
VerifiedChains [][]*x509.Certificate
260271

272+
// VerifiedDC indicates that the Delegated Credential sent by the peer (if advertised
273+
// and correctly processed), which has been verified against the leaf certificate,
274+
// has been used.
275+
VerifiedDC bool
276+
261277
// SignedCertificateTimestamps is a list of SCTs provided by the peer
262278
// through the TLS handshake for the leaf certificate, if any.
263279
SignedCertificateTimestamps [][]byte
@@ -427,6 +443,13 @@ type ClientHelloInfo struct {
427443
// Algorithms Extension is being used (see RFC 5246, Section 7.4.1.4.1).
428444
SignatureSchemes []SignatureScheme
429445

446+
// SignatureSchemesDC lists the signature schemes that the client
447+
// is willing to verify when using Delegated Credentials.
448+
// This is and can be different from SignatureSchemes. SignatureSchemesDC
449+
// is set only if the DelegatedCredentials Extension is being used.
450+
// If Delegated Credentials are supported, this list should not be nil.
451+
SignatureSchemesDC []SignatureScheme
452+
430453
// SupportedProtos lists the application protocols supported by the client.
431454
// SupportedProtos is set only if the Application-Layer Protocol
432455
// Negotiation Extension is being used (see RFC 7301, Section 3.1).
@@ -441,6 +464,10 @@ type ClientHelloInfo struct {
441464
// might be rejected if used.
442465
SupportedVersions []uint16
443466

467+
// SupportDelegatedCredential is true if the client indicated willingness
468+
// to negotiate the Delegated Credential extension.
469+
SupportsDelegatedCredential bool
470+
444471
// Conn is the underlying net.Conn for the connection. Do not read
445472
// from, or write to, this connection; that will cause the TLS
446473
// connection to fail.
@@ -471,10 +498,21 @@ type CertificateRequestInfo struct {
471498
// empty slice indicates that the server has no preference.
472499
AcceptableCAs [][]byte
473500

501+
// SupportDelegatedCredential is true if the server indicated willingness
502+
// to negotiate the Delegated Credential extension.
503+
SupportsDelegatedCredential bool
504+
474505
// SignatureSchemes lists the signature schemes that the server is
475506
// willing to verify.
476507
SignatureSchemes []SignatureScheme
477508

509+
// SignatureSchemesDC lists the signature schemes that the server
510+
// is willing to verify when using Delegated Credentials.
511+
// This is and can be different from SignatureSchemes. SignatureSchemesDC
512+
// is set only if the DelegatedCredentials Extension is being used.
513+
// If Delegated Credentials are supported, this list should not be nil.
514+
SignatureSchemesDC []SignatureScheme
515+
478516
// Version is the TLS version that was negotiated for this connection.
479517
Version uint16
480518

@@ -751,6 +789,13 @@ type Config struct {
751789
// This feature is unstable and applications MUST NOT depend on it.
752790
CFControl interface{}
753791

792+
// SupportDelegatedCredential is true if the client or server is willing
793+
// to negotiate the delegated credential extension.
794+
// This can only be used with TLS 1.3.
795+
//
796+
// See https://tools.ietf.org/html/draft-ietf-tls-subcerts.
797+
SupportDelegatedCredential bool
798+
754799
// mutex protects sessionTicketKeys and autoSessionTicketKeys.
755800
mutex sync.RWMutex
756801
// sessionTicketKeys contains zero or more ticket keys. If set, it means the
@@ -841,6 +886,7 @@ func (c *Config) Clone() *Config {
841886
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
842887
Renegotiation: c.Renegotiation,
843888
KeyLogWriter: c.KeyLogWriter,
889+
SupportDelegatedCredential: c.SupportDelegatedCredential,
844890
CFEventHandler: c.CFEventHandler,
845891
CFControl: c.CFControl,
846892
sessionTicketKeys: c.sessionTicketKeys,
@@ -1382,6 +1428,16 @@ func (c *Config) writeKeyLog(label string, clientRandom, secret []byte) error {
13821428
// and is only for debugging, so a global mutex saves space.
13831429
var writerMutex sync.Mutex
13841430

1431+
// A DelegatedCredentialPair contains a Delegated Credential and its
1432+
// associated private key.
1433+
type DelegatedCredentialPair struct {
1434+
// DC is the delegated credential.
1435+
DC *DelegatedCredential
1436+
// PrivateKey is the private key used to derive the public key of
1437+
// contained in DC. PrivateKey must implement crypto.Signer.
1438+
PrivateKey crypto.PrivateKey
1439+
}
1440+
13851441
// A Certificate is a chain of one or more certificates, leaf first.
13861442
type Certificate struct {
13871443
Certificate [][]byte
@@ -1399,6 +1455,16 @@ type Certificate struct {
13991455
// SignedCertificateTimestamps contains an optional list of Signed
14001456
// Certificate Timestamps which will be served to clients that request it.
14011457
SignedCertificateTimestamps [][]byte
1458+
// DelegatedCredentials are a list of Delegated Credentials with their
1459+
// corresponding private keys, signed by the leaf certificate.
1460+
// If there are no delegated credentials, this field is nil.
1461+
DelegatedCredentials []DelegatedCredentialPair
1462+
// DelegatedCredential is the delegated credential to be used in the
1463+
// handshake.
1464+
// If there are no delegated credentials, this field is nil.
1465+
// NOTE: Do not fill this field, as it will be filled depending on
1466+
// the provided list of delegated credentials.
1467+
DelegatedCredential []byte
14021468
// Leaf is the parsed form of the leaf certificate, which may be initialized
14031469
// using x509.ParseCertificate to reduce per-handshake processing. If nil,
14041470
// the leaf certificate will be parsed as needed.

src/crypto/tls/conn.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ type Conn struct {
5353
// verifiedChains contains the certificate chains that we built, as
5454
// opposed to the ones presented by the server.
5555
verifiedChains [][]*x509.Certificate
56+
// verifiedDC contains the Delegated Credential sent by the peer (if advertised
57+
// and correctly processed), which has been verified against the leaf certificate.
58+
verifiedDC *DelegatedCredential
5659
// serverName contains the server name indicated by the client, if any.
5760
serverName string
5861
// secureRenegotiation is true if the server echoed the secure
@@ -1496,6 +1499,9 @@ func (c *Conn) connectionStateLocked() ConnectionState {
14961499
state.CipherSuite = c.cipherSuite
14971500
state.PeerCertificates = c.peerCertificates
14981501
state.VerifiedChains = c.verifiedChains
1502+
if c.verifiedDC != nil {
1503+
state.VerifiedDC = true
1504+
}
14991505
state.SignedCertificateTimestamps = c.scts
15001506
state.OCSPResponse = c.ocspResponse
15011507
state.CFControl = c.config.CFControl

0 commit comments

Comments
 (0)