Documentation
¶
Index ¶
- Constants
- func CipherSuiteName(id uint16) string
- func ToConfig(c *Config) *tls.Config
- func VersionName(version uint16) string
- type ActiveCert
- type AlertError
- type CertCache
- type CipherSuite
- type ClientSessionState
- type Config
- type Connection
- type ConnectionBuilder
- type ConnectionState
- type ECHRejectionError
- type ExportKeyingMaterial
- type Handshake
- type QUICEncryptionLevel
- type SessionState
Constants ¶
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 )
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 ( QUICEncryptionLevelInitial = QUICEncryptionLevel(iota) QUICEncryptionLevelEarly QUICEncryptionLevelHandshake QUICEncryptionLevelApplication )
const ( VersionTLS10 = 0x0301 VersionTLS11 = 0x0302 VersionTLS12 = 0x0303 VersionTLS13 = 0x0304 // Deprecated: SSLv3 is cryptographically broken, and is no longer // supported by this package. See golang.org/issue/32716. VersionSSL30 = 0x0300 )
Variables ¶
This section is empty.
Functions ¶
func CipherSuiteName ¶
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 VersionName ¶
VersionName returns the name for the provided TLS version number (e.g. "TLS 1.3"), or a fallback representation of the value if the version is not implemented by this package.
Types ¶
type ActiveCert ¶
type ActiveCert struct {
// contains filtered or unexported fields
}
func (*ActiveCert) Cert ¶
func (e *ActiveCert) Cert() *x509.Certificate
type AlertError ¶
type AlertError uint8
func (AlertError) Error ¶
func (e AlertError) Error() string
type CipherSuite ¶
type CipherSuite struct {
// contains filtered or unexported fields
}
func CipherSuites ¶
func CipherSuites() []*CipherSuite
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 InsecureCipherSuites ¶
func InsecureCipherSuites() []*CipherSuite
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 (*CipherSuite) Flags ¶
func (suite *CipherSuite) Flags() int
func (*CipherSuite) Id ¶
func (suite *CipherSuite) Id() uint16
func (*CipherSuite) Insecure ¶
func (suite *CipherSuite) Insecure() bool
func (*CipherSuite) Name ¶
func (suite *CipherSuite) Name() string
func (*CipherSuite) SupportedVersion ¶
func (suite *CipherSuite) SupportedVersion(ver uint16) bool
func (*CipherSuite) SupportedVersions ¶
func (suite *CipherSuite) SupportedVersions() []uint16
type ClientSessionState ¶
type ClientSessionState struct {
// contains filtered or unexported fields
}
ClientSessionState contains the state needed by a client to resume a previous TLS session.
func NewResumptionState ¶
func NewResumptionState(ticket []byte, state *SessionState) (*ClientSessionState, error)
NewResumptionState returns a state value that can be returned by [ClientSessionCache.Get] to resume a previous session.
state needs to be returned by ParseSessionState, and the ticket and session state must have been returned by ClientSessionState.ResumptionState.
func (*ClientSessionState) ResumptionState ¶
func (cs *ClientSessionState) ResumptionState() (ticket []byte, state *SessionState, err error)
ResumptionState returns the session ticket sent by the server (also known as the session's identity) and the state necessary to resume this session.
It can be called by [ClientSessionCache.Put] to serialize (with SessionState.Bytes) and store the session.
type Config ¶
type Config struct { // Rand provides the source of entropy for nonces and RSA blinding. // If Rand is nil, TLS uses the cryptographic random reader in package // crypto/rand. // The Reader must be safe for use by multiple goroutines. Rand io.Reader // Time returns the current time as the number of seconds since the epoch. // If Time is nil, TLS uses time.Now. Time func() time.Time // Certificates contains one or more certificate chains to present to the // other side of the connection. The first certificate compatible with the // peer's requirements is selected automatically. // // Server configurations must set one of Certificates, GetCertificate or // GetConfigForClient. Clients doing client-authentication may set either // Certificates or GetClientCertificate. // // Note: if there are multiple Certificates, and they don't have the // optional field Leaf set, certificate selection will incur a significant // per-handshake performance cost. Certificates []tls.Certificate // NameToCertificate maps from a certificate name to an element of // Certificates. Note that a certificate name can be of the form // '*.example.com' and so doesn't have to be a domain name as such. // // Deprecated: NameToCertificate only allows associating a single // certificate with a given name. Leave this field nil to let the library // select the first compatible chain from Certificates. NameToCertificate map[string]*tls.Certificate // GetCertificate returns a Certificate based on the given // ClientHelloInfo. It will only be called if the client supplies SNI // information or if Certificates is empty. // // If GetCertificate is nil or returns nil, then the certificate is // retrieved from NameToCertificate. If NameToCertificate is nil, the // best element of Certificates will be used. // // Once a Certificate is returned it should not be modified. GetCertificate func(*tls.ClientHelloInfo) (*tls.Certificate, error) // GetClientCertificate, if not nil, is called when a server requests a // certificate from a client. If set, the contents of Certificates will // be ignored. // // If GetClientCertificate returns an error, the handshake will be // aborted and that error will be returned. Otherwise // GetClientCertificate must return a non-nil Certificate. If // Certificate.Certificate is empty then no certificate will be sent to // the server. If this is unacceptable to the server then it may abort // the handshake. // // GetClientCertificate may be called multiple times for the same // connection if renegotiation occurs or if TLS 1.3 is in use. // // Once a Certificate is returned it should not be modified. GetClientCertificate func(*tls.CertificateRequestInfo) (*tls.Certificate, error) // GetConfigForClient, if not nil, is called after a ClientHello is // received from a client. It may return a non-nil Config in order to // change the Config that will be used to handle this connection. If // the returned Config is nil, the original Config will be used. The // Config returned by this callback may not be subsequently modified. // // If GetConfigForClient is nil, the Config passed to Server() will be // used for all connections. // // If SessionTicketKey was explicitly set on the returned Config, or if // SetSessionTicketKeys was called on the returned Config, those keys will // be used. Otherwise, the original Config keys will be used (and possibly // rotated if they are automatically managed). GetConfigForClient func(*tls.ClientHelloInfo) (*Config, error) // VerifyPeerCertificate, if not nil, is called after normal // certificate verification by either a TLS client or server. It // receives the raw ASN.1 certificates provided by the peer and also // any verified chains that normal processing found. If it returns a // non-nil error, the handshake is aborted and that error results. // // If normal verification fails then the handshake will abort before // considering this callback. If normal verification is disabled (on the // client when InsecureSkipVerify is set, or on a server when ClientAuth is // RequestClientCert or RequireAnyClientCert), then this callback will be // considered but the verifiedChains argument will always be nil. When // ClientAuth is NoClientCert, this callback is not called on the server. // rawCerts may be empty on the server if ClientAuth is RequestClientCert or // VerifyClientCertIfGiven. // // This callback is not invoked on resumed connections, as certificates are // not re-verified on resumption. // // verifiedChains and its contents should not be modified. VerifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*x509.Certificate) error // VerifyConnection, if not nil, is called after normal certificate // verification and after VerifyPeerCertificate by either a TLS client // or server. If it returns a non-nil error, the handshake is aborted // and that error results. // // If normal verification fails then the handshake will abort before // considering this callback. This callback will run for all connections, // including resumptions, regardless of InsecureSkipVerify or ClientAuth // settings. VerifyConnection func(ConnectionState) error // RootCAs defines the set of root certificate authorities // that clients use when verifying server certificates. // If RootCAs is nil, TLS uses the host's root CA set. RootCAs *x509.CertPool // NextProtos is a list of supported application level protocols, in // order of preference. If both peers support ALPN, the selected // protocol will be one from this list, and the connection will fail // if there is no mutually supported protocol. If NextProtos is empty // or the peer doesn't support ALPN, the connection will succeed and // ConnectionState.NegotiatedProtocol will be empty. NextProtos []string // ServerName is used to verify the hostname on the returned // certificates unless InsecureSkipVerify is given. It is also included // in the client's handshake to support virtual hosting unless it is // an IP address. ServerName string // ClientAuth determines the server's policy for // TLS Client Authentication. The default is NoClientCert. ClientAuth tls.ClientAuthType // ClientCAs defines the set of root certificate authorities // that servers use if required to verify a client certificate // by the policy in ClientAuth. ClientCAs *x509.CertPool // InsecureSkipVerify controls whether a client verifies the server's // certificate chain and host name. If InsecureSkipVerify is true, crypto/tls // accepts any certificate presented by the server and any host name in that // certificate. In this mode, TLS is susceptible to machine-in-the-middle // attacks unless custom verification is used. This should be used only for // testing or in combination with VerifyConnection or VerifyPeerCertificate. InsecureSkipVerify bool // CipherSuites is a list of enabled TLS 1.0–1.2 cipher suites. The order of // the list is ignored. Note that TLS 1.3 ciphersuites are not configurable. // // If CipherSuites is nil, a safe default list is used. The default cipher // suites might change over time. In Go 1.22 RSA key exchange based cipher // suites were removed from the default list, but can be re-added with the // GODEBUG setting tlsrsakex=1. In Go 1.23 3DES cipher suites were removed // from the default list, but can be re-added with the GODEBUG setting // tls3des=1. CipherSuites []uint16 // PreferServerCipherSuites is a legacy field and has no effect. // // It used to control whether the server would follow the client's or the // server's preference. Servers now select the best mutually supported // cipher suite based on logic that takes into account inferred client // hardware, server hardware, and security. // // Deprecated: PreferServerCipherSuites is ignored. PreferServerCipherSuites bool // SessionTicketsDisabled may be set to true to disable session ticket and // PSK (resumption) support. Note that on clients, session ticket support is // also disabled if ClientSessionCache is nil. SessionTicketsDisabled bool // SessionTicketKey is used by TLS servers to provide session resumption. // See RFC 5077 and the PSK mode of RFC 8446. If zero, it will be filled // with random data before the first server handshake. // // Deprecated: if this field is left at zero, session ticket keys will be // automatically rotated every day and dropped after seven days. For // customizing the rotation schedule or synchronizing servers that are // terminating connections for the same host, use SetSessionTicketKeys. SessionTicketKey [32]byte // ClientSessionCache is a cache of ClientSessionState entries for TLS // session resumption. It is only used by clients. ClientSessionCache tls.ClientSessionCache // UnwrapSession is called on the server to turn a ticket/identity // previously produced by [WrapSession] into a usable session. // // UnwrapSession will usually either decrypt a session state in the ticket // (for example with [Config.EncryptTicket]), or use the ticket as a handle // to recover a previously stored state. It must use [ParseSessionState] to // deserialize the session state. // // If UnwrapSession returns an error, the connection is terminated. If it // returns (nil, nil), the session is ignored. crypto/tls may still choose // not to resume the returned session. UnwrapSession func(identity []byte, cs ConnectionState) (*SessionState, error) // WrapSession is called on the server to produce a session ticket/identity. // // WrapSession must serialize the session state with [SessionState.Bytes]. // It may then encrypt the serialized state (for example with // [Config.DecryptTicket]) and use it as the ticket, or store the state and // return a handle for it. // // If WrapSession returns an error, the connection is terminated. // // Warning: the return value will be exposed on the wire and to clients in // plaintext. The application is in charge of encrypting and authenticating // it (and rotating keys) or returning high-entropy identifiers. Failing to // do so correctly can compromise current, previous, and future connections // depending on the protocol version. WrapSession func(ConnectionState, *SessionState) ([]byte, error) // MinVersion contains the minimum TLS version that is acceptable. // // By default, TLS 1.2 is currently used as the minimum. TLS 1.0 is the // minimum supported by this package. // // The server-side default can be reverted to TLS 1.0 by including the value // "tls10server=1" in the GODEBUG environment variable. MinVersion uint16 // MaxVersion contains the maximum TLS version that is acceptable. // // By default, the maximum version supported by this package is used, // which is currently TLS 1.3. MaxVersion uint16 // CurvePreferences contains the elliptic curves that will be used in // an ECDHE handshake, in preference order. If empty, the default will // be used. The client will use the first preference as the type for // its key share in TLS 1.3. This may change in the future. // // From Go 1.23, the default includes the X25519Kyber768Draft00 hybrid // post-quantum key exchange. To disable it, set CurvePreferences explicitly // or use the GODEBUG=tlskyber=0 environment variable. CurvePreferences []tls.CurveID // DynamicRecordSizingDisabled disables adaptive sizing of TLS records. // When true, the largest possible TLS record size is always used. When // false, the size of TLS records may be adjusted in an attempt to // improve latency. DynamicRecordSizingDisabled bool // Renegotiation controls what types of renegotiation are supported. // The default, none, is correct for the vast majority of applications. Renegotiation tls.RenegotiationSupport // KeyLogWriter optionally specifies a destination for TLS master secrets // in NSS key log format that can be used to allow external programs // such as Wireshark to decrypt TLS connections. // See https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format. // Use of KeyLogWriter compromises security and should only be // used for debugging. KeyLogWriter io.Writer // EncryptedClientHelloConfigList is a serialized ECHConfigList. If // provided, clients will attempt to connect to servers using Encrypted // Client Hello (ECH) using one of the provided ECHConfigs. Servers // currently ignore this field. // // If the list contains no valid ECH configs, the handshake will fail // and return an error. // // If EncryptedClientHelloConfigList is set, MinVersion, if set, must // be VersionTLS13. // // When EncryptedClientHelloConfigList is set, the handshake will only // succeed if ECH is sucessfully negotiated. If the server rejects ECH, // an ECHRejectionError error will be returned, which may contain a new // ECHConfigList that the server suggests using. // // How this field is parsed may change in future Go versions, if the // encoding described in the final Encrypted Client Hello RFC changes. EncryptedClientHelloConfigList []byte // EncryptedClientHelloRejectionVerify, if not nil, is called when ECH is // rejected, in order to verify the ECH provider certificate in the outer // Client Hello. If it returns a non-nil error, the handshake is aborted and // that error results. // // Unlike VerifyPeerCertificate and VerifyConnection, normal certificate // verification will not be performed before calling // EncryptedClientHelloRejectionVerify. // // If EncryptedClientHelloRejectionVerify is nil and ECH is rejected, the // roots in RootCAs will be used to verify the ECH providers public // certificate. VerifyPeerCertificate and VerifyConnection are not called // when ECH is rejected, even if set, and InsecureSkipVerify is ignored. EncryptedClientHelloRejectionVerify func(ConnectionState) error // contains filtered or unexported fields }
A Config structure is used to configure a TLS client or server. After one has been passed to a TLS function it must not be modified. A Config may be reused; the tls package will also not modify it.
func FromConfig ¶
func (*Config) Clone ¶
Clone returns a shallow clone of c or nil if c is nil. It is safe to clone a Config that is being used concurrently by a TLS client or server.
func (*Config) SessionTicketKeys ¶
func (c *Config) SessionTicketKeys() []ticketKey
func (*Config) SetSessionTicketKeys ¶
SetSessionTicketKeys updates the session ticket keys for a server.
The first key will be used when creating new tickets, while all keys can be used for decrypting tickets. It is safe to call this function while the server is running in order to rotate the session ticket keys. The function will panic if keys is empty.
Calling this function will turn off automatic session ticket key rotation.
If multiple servers are terminating connections for the same host they should all have the same session ticket keys. If the session ticket keys leaks, previously recorded and future TLS connections using those keys might be compromised.
type Connection ¶
type ConnectionBuilder ¶
type ConnectionBuilder interface { Client(ts transport.Connection) Connection Server(ts transport.Connection) Connection }
func NewConnectionBuilder ¶
func NewConnectionBuilder(config *tls.Config) ConnectionBuilder
type ConnectionState ¶
type ConnectionState struct { Version uint16 HandshakeComplete bool DidResume bool CipherSuite uint16 NegotiatedProtocol string NegotiatedProtocolIsMutual bool ServerName string PeerCertificates []*x509.Certificate VerifiedChains [][]*x509.Certificate SignedCertificateTimestamps [][]byte OCSPResponse []byte TLSUnique []byte ECHAccepted bool // contains filtered or unexported fields }
func (*ConnectionState) AsTLSConnectionState ¶
func (cs *ConnectionState) AsTLSConnectionState() tls.ConnectionState
func (*ConnectionState) ExportKeyingMaterial ¶
func (*ConnectionState) SetExportKeyingMaterial ¶
func (cs *ConnectionState) SetExportKeyingMaterial(config *tls.Config, version uint16, extMasterSecret bool, defaultEKM ExportKeyingMaterial)
type ECHRejectionError ¶
type ECHRejectionError struct {
RetryConfigList []byte
}
ECHRejectionError is the error type returned when ECH is rejected by a remote server. If the server offered a ECHConfigList to use for retries, the RetryConfigList field will contain this list.
The client may treat an ECHRejectionError with an empty set of RetryConfigs as a secure signal from the server.
func (*ECHRejectionError) Error ¶
func (e *ECHRejectionError) Error() string
type ExportKeyingMaterial ¶
type QUICEncryptionLevel ¶
type QUICEncryptionLevel int
QUICEncryptionLevel represents a QUIC encryption level used to transmit handshake messages.
func (QUICEncryptionLevel) String ¶
func (l QUICEncryptionLevel) String() string
type SessionState ¶
type SessionState struct { // Extra is ignored by crypto/tls, but is encoded by [SessionState.Bytes] // and parsed by [ParseSessionState]. // // This allows [Config.UnwrapSession]/[Config.WrapSession] and // [ClientSessionCache] implementations to store and retrieve additional // data alongside this session. // // To allow different layers in a protocol stack to share this field, // applications must only append to it, not replace it, and must use entries // that can be recognized even if out of order (for example, by starting // with an id and version prefix). Extra [][]byte // EarlyData indicates whether the ticket can be used for 0-RTT in a QUIC // connection. The application may set this to false if it is true to // decline to offer 0-RTT even if supported. EarlyData bool // contains filtered or unexported fields }
A SessionState is a resumable session.
func ParseSessionState ¶
func ParseSessionState(data []byte) (*SessionState, error)
ParseSessionState parses a SessionState encoded by SessionState.Bytes.
func (*SessionState) Bytes ¶
func (s *SessionState) Bytes() ([]byte, error)
Bytes encodes the session, including any private fields, so that it can be parsed by ParseSessionState. The encoding contains secret values critical to the security of future and possibly past sessions.
The specific encoding should be considered opaque and may change incompatibly between Go versions.
Source Files
¶
- alert.go
- auth.go
- cache.go
- certificate.go
- cipher_aead.go
- cipher_cbc.go
- cipher_ecdhe.go
- cipher_ech.go
- cipher_signature.go
- cipher_suite.go
- config.go
- connection.go
- connection_alert.go
- connection_close.go
- connection_half.go
- connection_handshake.go
- connection_read.go
- connection_record.go
- connection_state.go
- connection_write.go
- defaults.go
- extension.go
- handshake.go
- handshake_client.go
- handshake_message.go
- handshake_server.go
- key_agreement.go
- key_schedule.go
- noboring.go
- prf.go
- quic.go
- ticket.go
- version.go