Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | // Copyright 2009 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Adam Langley | dc7e9c4 | 2015-09-29 15:21:04 -0700 | [diff] [blame] | 5 | package runner |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
| 8 | "container/list" |
| 9 | "crypto" |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 10 | "crypto/ecdsa" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 11 | "crypto/rand" |
| 12 | "crypto/x509" |
| 13 | "fmt" |
| 14 | "io" |
| 15 | "math/big" |
| 16 | "strings" |
| 17 | "sync" |
| 18 | "time" |
| 19 | ) |
| 20 | |
| 21 | const ( |
| 22 | VersionSSL30 = 0x0300 |
| 23 | VersionTLS10 = 0x0301 |
| 24 | VersionTLS11 = 0x0302 |
| 25 | VersionTLS12 = 0x0303 |
| 26 | ) |
| 27 | |
| 28 | const ( |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 29 | maxPlaintext = 16384 // maximum plaintext payload length |
| 30 | maxCiphertext = 16384 + 2048 // maximum ciphertext payload length |
| 31 | tlsRecordHeaderLen = 5 // record header length |
| 32 | dtlsRecordHeaderLen = 13 |
| 33 | maxHandshake = 65536 // maximum handshake we support (protocol max is 16 MB) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 34 | |
| 35 | minVersion = VersionSSL30 |
| 36 | maxVersion = VersionTLS12 |
| 37 | ) |
| 38 | |
| 39 | // TLS record types. |
| 40 | type recordType uint8 |
| 41 | |
| 42 | const ( |
| 43 | recordTypeChangeCipherSpec recordType = 20 |
| 44 | recordTypeAlert recordType = 21 |
| 45 | recordTypeHandshake recordType = 22 |
| 46 | recordTypeApplicationData recordType = 23 |
| 47 | ) |
| 48 | |
| 49 | // TLS handshake message types. |
| 50 | const ( |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 51 | typeHelloRequest uint8 = 0 |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 52 | typeClientHello uint8 = 1 |
| 53 | typeServerHello uint8 = 2 |
| 54 | typeHelloVerifyRequest uint8 = 3 |
| 55 | typeNewSessionTicket uint8 = 4 |
| 56 | typeCertificate uint8 = 11 |
| 57 | typeServerKeyExchange uint8 = 12 |
| 58 | typeCertificateRequest uint8 = 13 |
| 59 | typeServerHelloDone uint8 = 14 |
| 60 | typeCertificateVerify uint8 = 15 |
| 61 | typeClientKeyExchange uint8 = 16 |
| 62 | typeFinished uint8 = 20 |
| 63 | typeCertificateStatus uint8 = 22 |
| 64 | typeNextProtocol uint8 = 67 // Not IANA assigned |
| 65 | typeEncryptedExtensions uint8 = 203 // Not IANA assigned |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 66 | ) |
| 67 | |
| 68 | // TLS compression types. |
| 69 | const ( |
| 70 | compressionNone uint8 = 0 |
| 71 | ) |
| 72 | |
| 73 | // TLS extension numbers |
| 74 | const ( |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 75 | extensionServerName uint16 = 0 |
| 76 | extensionStatusRequest uint16 = 5 |
| 77 | extensionSupportedCurves uint16 = 10 |
| 78 | extensionSupportedPoints uint16 = 11 |
| 79 | extensionSignatureAlgorithms uint16 = 13 |
| 80 | extensionUseSRTP uint16 = 14 |
| 81 | extensionALPN uint16 = 16 |
| 82 | extensionSignedCertificateTimestamp uint16 = 18 |
| 83 | extensionExtendedMasterSecret uint16 = 23 |
| 84 | extensionSessionTicket uint16 = 35 |
David Benjamin | 399e7c9 | 2015-07-30 23:01:27 -0400 | [diff] [blame] | 85 | extensionCustom uint16 = 1234 // not IANA assigned |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 86 | extensionNextProtoNeg uint16 = 13172 // not IANA assigned |
| 87 | extensionRenegotiationInfo uint16 = 0xff01 |
| 88 | extensionChannelID uint16 = 30032 // not IANA assigned |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 89 | ) |
| 90 | |
| 91 | // TLS signaling cipher suite values |
| 92 | const ( |
| 93 | scsvRenegotiation uint16 = 0x00ff |
| 94 | ) |
| 95 | |
| 96 | // CurveID is the type of a TLS identifier for an elliptic curve. See |
| 97 | // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-8 |
| 98 | type CurveID uint16 |
| 99 | |
| 100 | const ( |
David Benjamin | cba2b62 | 2015-12-18 22:13:41 -0500 | [diff] [blame] | 101 | CurveP224 CurveID = 21 |
| 102 | CurveP256 CurveID = 23 |
| 103 | CurveP384 CurveID = 24 |
| 104 | CurveP521 CurveID = 25 |
| 105 | CurveX25519 CurveID = 29 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 106 | ) |
| 107 | |
| 108 | // TLS Elliptic Curve Point Formats |
| 109 | // http://www.iana.org/assignments/tls-parameters/tls-parameters.xml#tls-parameters-9 |
| 110 | const ( |
| 111 | pointFormatUncompressed uint8 = 0 |
| 112 | ) |
| 113 | |
| 114 | // TLS CertificateStatusType (RFC 3546) |
| 115 | const ( |
| 116 | statusTypeOCSP uint8 = 1 |
| 117 | ) |
| 118 | |
| 119 | // Certificate types (for certificateRequestMsg) |
| 120 | const ( |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 121 | CertTypeRSASign = 1 // A certificate containing an RSA key |
| 122 | CertTypeDSSSign = 2 // A certificate containing a DSA key |
| 123 | CertTypeRSAFixedDH = 3 // A certificate containing a static DH key |
| 124 | CertTypeDSSFixedDH = 4 // A certificate containing a static DH key |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | |
| 126 | // See RFC4492 sections 3 and 5.5. |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 127 | CertTypeECDSASign = 64 // A certificate containing an ECDSA-capable public key, signed with ECDSA. |
| 128 | CertTypeRSAFixedECDH = 65 // A certificate containing an ECDH-capable public key, signed with RSA. |
| 129 | CertTypeECDSAFixedECDH = 66 // A certificate containing an ECDH-capable public key, signed with ECDSA. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | |
| 131 | // Rest of these are reserved by the TLS spec |
| 132 | ) |
| 133 | |
| 134 | // Hash functions for TLS 1.2 (See RFC 5246, section A.4.1) |
| 135 | const ( |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 136 | hashMD5 uint8 = 1 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 137 | hashSHA1 uint8 = 2 |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 138 | hashSHA224 uint8 = 3 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 139 | hashSHA256 uint8 = 4 |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 140 | hashSHA384 uint8 = 5 |
| 141 | hashSHA512 uint8 = 6 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 142 | ) |
| 143 | |
| 144 | // Signature algorithms for TLS 1.2 (See RFC 5246, section A.4.1) |
| 145 | const ( |
| 146 | signatureRSA uint8 = 1 |
| 147 | signatureECDSA uint8 = 3 |
| 148 | ) |
| 149 | |
| 150 | // signatureAndHash mirrors the TLS 1.2, SignatureAndHashAlgorithm struct. See |
| 151 | // RFC 5246, section A.4.1. |
| 152 | type signatureAndHash struct { |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 153 | signature, hash uint8 |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | // supportedSKXSignatureAlgorithms contains the signature and hash algorithms |
| 157 | // that the code advertises as supported in a TLS 1.2 ClientHello. |
| 158 | var supportedSKXSignatureAlgorithms = []signatureAndHash{ |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 159 | {signatureRSA, hashSHA256}, |
| 160 | {signatureECDSA, hashSHA256}, |
| 161 | {signatureRSA, hashSHA1}, |
| 162 | {signatureECDSA, hashSHA1}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | // supportedClientCertSignatureAlgorithms contains the signature and hash |
| 166 | // algorithms that the code advertises as supported in a TLS 1.2 |
| 167 | // CertificateRequest. |
| 168 | var supportedClientCertSignatureAlgorithms = []signatureAndHash{ |
David Benjamin | e098ec2 | 2014-08-27 23:13:20 -0400 | [diff] [blame] | 169 | {signatureRSA, hashSHA256}, |
| 170 | {signatureECDSA, hashSHA256}, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | } |
| 172 | |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 173 | // SRTP protection profiles (See RFC 5764, section 4.1.2) |
| 174 | const ( |
| 175 | SRTP_AES128_CM_HMAC_SHA1_80 uint16 = 0x0001 |
| 176 | SRTP_AES128_CM_HMAC_SHA1_32 = 0x0002 |
| 177 | ) |
| 178 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 179 | // ConnectionState records basic TLS details about the connection. |
| 180 | type ConnectionState struct { |
| 181 | Version uint16 // TLS version used by the connection (e.g. VersionTLS12) |
| 182 | HandshakeComplete bool // TLS handshake is complete |
| 183 | DidResume bool // connection resumes a previous TLS connection |
| 184 | CipherSuite uint16 // cipher suite in use (TLS_RSA_WITH_RC4_128_SHA, ...) |
| 185 | NegotiatedProtocol string // negotiated next protocol (from Config.NextProtos) |
| 186 | NegotiatedProtocolIsMutual bool // negotiated protocol was advertised by server |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 187 | NegotiatedProtocolFromALPN bool // protocol negotiated with ALPN |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 188 | ServerName string // server name requested by client, if any (server side only) |
| 189 | PeerCertificates []*x509.Certificate // certificate chain presented by remote peer |
| 190 | VerifiedChains [][]*x509.Certificate // verified chains built from PeerCertificates |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 191 | ChannelID *ecdsa.PublicKey // the channel ID for this connection |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 192 | SRTPProtectionProfile uint16 // the negotiated DTLS-SRTP protection profile |
David Benjamin | c057762 | 2015-09-12 18:28:38 -0400 | [diff] [blame] | 193 | TLSUnique []byte // the tls-unique channel binding |
Paul Lietar | 4fac72e | 2015-09-09 13:44:55 +0100 | [diff] [blame] | 194 | SCTList []byte // signed certificate timestamp list |
Steven Valdez | 0d62f26 | 2015-09-04 12:41:04 -0400 | [diff] [blame] | 195 | ClientCertSignatureHash uint8 // TLS id of the hash used by the client to sign the handshake |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | // ClientAuthType declares the policy the server will follow for |
| 199 | // TLS Client Authentication. |
| 200 | type ClientAuthType int |
| 201 | |
| 202 | const ( |
| 203 | NoClientCert ClientAuthType = iota |
| 204 | RequestClientCert |
| 205 | RequireAnyClientCert |
| 206 | VerifyClientCertIfGiven |
| 207 | RequireAndVerifyClientCert |
| 208 | ) |
| 209 | |
| 210 | // ClientSessionState contains the state needed by clients to resume TLS |
| 211 | // sessions. |
| 212 | type ClientSessionState struct { |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 213 | sessionId []uint8 // Session ID supplied by the server. nil if the session has a ticket. |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 214 | sessionTicket []uint8 // Encrypted ticket used for session resumption with server |
| 215 | vers uint16 // SSL/TLS version negotiated for the session |
| 216 | cipherSuite uint16 // Ciphersuite negotiated for the session |
| 217 | masterSecret []byte // MasterSecret generated by client on a full handshake |
| 218 | handshakeHash []byte // Handshake hash for Channel ID purposes. |
| 219 | serverCertificates []*x509.Certificate // Certificate chain presented by the server |
| 220 | extendedMasterSecret bool // Whether an extended master secret was used to generate the session |
Paul Lietar | 62be8ac | 2015-09-16 10:03:30 +0100 | [diff] [blame] | 221 | sctList []byte |
| 222 | ocspResponse []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // ClientSessionCache is a cache of ClientSessionState objects that can be used |
| 226 | // by a client to resume a TLS session with a given server. ClientSessionCache |
| 227 | // implementations should expect to be called concurrently from different |
| 228 | // goroutines. |
| 229 | type ClientSessionCache interface { |
| 230 | // Get searches for a ClientSessionState associated with the given key. |
| 231 | // On return, ok is true if one was found. |
| 232 | Get(sessionKey string) (session *ClientSessionState, ok bool) |
| 233 | |
| 234 | // Put adds the ClientSessionState to the cache with the given key. |
| 235 | Put(sessionKey string, cs *ClientSessionState) |
| 236 | } |
| 237 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 238 | // ServerSessionCache is a cache of sessionState objects that can be used by a |
| 239 | // client to resume a TLS session with a given server. ServerSessionCache |
| 240 | // implementations should expect to be called concurrently from different |
| 241 | // goroutines. |
| 242 | type ServerSessionCache interface { |
| 243 | // Get searches for a sessionState associated with the given session |
| 244 | // ID. On return, ok is true if one was found. |
| 245 | Get(sessionId string) (session *sessionState, ok bool) |
| 246 | |
| 247 | // Put adds the sessionState to the cache with the given session ID. |
| 248 | Put(sessionId string, session *sessionState) |
| 249 | } |
| 250 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 251 | // A Config structure is used to configure a TLS client or server. |
| 252 | // After one has been passed to a TLS function it must not be |
| 253 | // modified. A Config may be reused; the tls package will also not |
| 254 | // modify it. |
| 255 | type Config struct { |
| 256 | // Rand provides the source of entropy for nonces and RSA blinding. |
| 257 | // If Rand is nil, TLS uses the cryptographic random reader in package |
| 258 | // crypto/rand. |
| 259 | // The Reader must be safe for use by multiple goroutines. |
| 260 | Rand io.Reader |
| 261 | |
| 262 | // Time returns the current time as the number of seconds since the epoch. |
| 263 | // If Time is nil, TLS uses time.Now. |
| 264 | Time func() time.Time |
| 265 | |
| 266 | // Certificates contains one or more certificate chains |
| 267 | // to present to the other side of the connection. |
| 268 | // Server configurations must include at least one certificate. |
| 269 | Certificates []Certificate |
| 270 | |
| 271 | // NameToCertificate maps from a certificate name to an element of |
| 272 | // Certificates. Note that a certificate name can be of the form |
| 273 | // '*.example.com' and so doesn't have to be a domain name as such. |
| 274 | // See Config.BuildNameToCertificate |
| 275 | // The nil value causes the first element of Certificates to be used |
| 276 | // for all connections. |
| 277 | NameToCertificate map[string]*Certificate |
| 278 | |
| 279 | // RootCAs defines the set of root certificate authorities |
| 280 | // that clients use when verifying server certificates. |
| 281 | // If RootCAs is nil, TLS uses the host's root CA set. |
| 282 | RootCAs *x509.CertPool |
| 283 | |
| 284 | // NextProtos is a list of supported, application level protocols. |
| 285 | NextProtos []string |
| 286 | |
| 287 | // ServerName is used to verify the hostname on the returned |
| 288 | // certificates unless InsecureSkipVerify is given. It is also included |
| 289 | // in the client's handshake to support virtual hosting. |
| 290 | ServerName string |
| 291 | |
| 292 | // ClientAuth determines the server's policy for |
| 293 | // TLS Client Authentication. The default is NoClientCert. |
| 294 | ClientAuth ClientAuthType |
| 295 | |
| 296 | // ClientCAs defines the set of root certificate authorities |
| 297 | // that servers use if required to verify a client certificate |
| 298 | // by the policy in ClientAuth. |
| 299 | ClientCAs *x509.CertPool |
| 300 | |
David Benjamin | 7b03051 | 2014-07-08 17:30:11 -0400 | [diff] [blame] | 301 | // ClientCertificateTypes defines the set of allowed client certificate |
| 302 | // types. The default is CertTypeRSASign and CertTypeECDSASign. |
| 303 | ClientCertificateTypes []byte |
| 304 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | // InsecureSkipVerify controls whether a client verifies the |
| 306 | // server's certificate chain and host name. |
| 307 | // If InsecureSkipVerify is true, TLS accepts any certificate |
| 308 | // presented by the server and any host name in that certificate. |
| 309 | // In this mode, TLS is susceptible to man-in-the-middle attacks. |
| 310 | // This should be used only for testing. |
| 311 | InsecureSkipVerify bool |
| 312 | |
| 313 | // CipherSuites is a list of supported cipher suites. If CipherSuites |
| 314 | // is nil, TLS uses a list of suites supported by the implementation. |
| 315 | CipherSuites []uint16 |
| 316 | |
| 317 | // PreferServerCipherSuites controls whether the server selects the |
| 318 | // client's most preferred ciphersuite, or the server's most preferred |
| 319 | // ciphersuite. If true then the server's preference, as expressed in |
| 320 | // the order of elements in CipherSuites, is used. |
| 321 | PreferServerCipherSuites bool |
| 322 | |
| 323 | // SessionTicketsDisabled may be set to true to disable session ticket |
| 324 | // (resumption) support. |
| 325 | SessionTicketsDisabled bool |
| 326 | |
| 327 | // SessionTicketKey is used by TLS servers to provide session |
| 328 | // resumption. See RFC 5077. If zero, it will be filled with |
| 329 | // random data before the first server handshake. |
| 330 | // |
| 331 | // If multiple servers are terminating connections for the same host |
| 332 | // they should all have the same SessionTicketKey. If the |
| 333 | // SessionTicketKey leaks, previously recorded and future TLS |
| 334 | // connections using that key are compromised. |
| 335 | SessionTicketKey [32]byte |
| 336 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 337 | // ClientSessionCache is a cache of ClientSessionState entries |
| 338 | // for TLS session resumption. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 339 | ClientSessionCache ClientSessionCache |
| 340 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 341 | // ServerSessionCache is a cache of sessionState entries for TLS session |
| 342 | // resumption. |
| 343 | ServerSessionCache ServerSessionCache |
| 344 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 345 | // MinVersion contains the minimum SSL/TLS version that is acceptable. |
| 346 | // If zero, then SSLv3 is taken as the minimum. |
| 347 | MinVersion uint16 |
| 348 | |
| 349 | // MaxVersion contains the maximum SSL/TLS version that is acceptable. |
| 350 | // If zero, then the maximum version supported by this package is used, |
| 351 | // which is currently TLS 1.2. |
| 352 | MaxVersion uint16 |
| 353 | |
| 354 | // CurvePreferences contains the elliptic curves that will be used in |
| 355 | // an ECDHE handshake, in preference order. If empty, the default will |
| 356 | // be used. |
| 357 | CurvePreferences []CurveID |
| 358 | |
David Benjamin | d30a990 | 2014-08-24 01:44:23 -0400 | [diff] [blame] | 359 | // ChannelID contains the ECDSA key for the client to use as |
| 360 | // its TLS Channel ID. |
| 361 | ChannelID *ecdsa.PrivateKey |
| 362 | |
| 363 | // RequestChannelID controls whether the server requests a TLS |
| 364 | // Channel ID. If negotiated, the client's public key is |
| 365 | // returned in the ConnectionState. |
| 366 | RequestChannelID bool |
| 367 | |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 368 | // PreSharedKey, if not nil, is the pre-shared key to use with |
| 369 | // the PSK cipher suites. |
| 370 | PreSharedKey []byte |
| 371 | |
| 372 | // PreSharedKeyIdentity, if not empty, is the identity to use |
| 373 | // with the PSK cipher suites. |
| 374 | PreSharedKeyIdentity string |
| 375 | |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 376 | // SRTPProtectionProfiles, if not nil, is the list of SRTP |
| 377 | // protection profiles to offer in DTLS-SRTP. |
| 378 | SRTPProtectionProfiles []uint16 |
| 379 | |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 380 | // SignatureAndHashes, if not nil, overrides the default set of |
| 381 | // supported signature and hash algorithms to advertise in |
| 382 | // CertificateRequest. |
| 383 | SignatureAndHashes []signatureAndHash |
| 384 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | // Bugs specifies optional misbehaviour to be used for testing other |
| 386 | // implementations. |
| 387 | Bugs ProtocolBugs |
| 388 | |
| 389 | serverInitOnce sync.Once // guards calling (*Config).serverInit |
| 390 | } |
| 391 | |
| 392 | type BadValue int |
| 393 | |
| 394 | const ( |
| 395 | BadValueNone BadValue = iota |
| 396 | BadValueNegative |
| 397 | BadValueZero |
| 398 | BadValueLimit |
| 399 | BadValueLarge |
| 400 | NumBadValues |
| 401 | ) |
| 402 | |
David Benjamin | b36a395 | 2015-12-01 18:53:13 -0500 | [diff] [blame] | 403 | type RSABadValue int |
| 404 | |
| 405 | const ( |
| 406 | RSABadValueNone RSABadValue = iota |
| 407 | RSABadValueCorrupt |
| 408 | RSABadValueTooLong |
| 409 | RSABadValueTooShort |
| 410 | RSABadValueWrongVersion |
| 411 | NumRSABadValues |
| 412 | ) |
| 413 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 414 | type ProtocolBugs struct { |
| 415 | // InvalidSKXSignature specifies that the signature in a |
| 416 | // ServerKeyExchange message should be invalid. |
| 417 | InvalidSKXSignature bool |
| 418 | |
David Benjamin | 6de0e53 | 2015-07-28 22:43:19 -0400 | [diff] [blame] | 419 | // InvalidCertVerifySignature specifies that the signature in a |
| 420 | // CertificateVerify message should be invalid. |
| 421 | InvalidCertVerifySignature bool |
| 422 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 423 | // InvalidSKXCurve causes the curve ID in the ServerKeyExchange message |
| 424 | // to be wrong. |
| 425 | InvalidSKXCurve bool |
| 426 | |
David Benjamin | 2b07fa4 | 2016-03-02 00:23:57 -0500 | [diff] [blame^] | 427 | // InvalidECDHPoint, if true, causes the ECC points in |
| 428 | // ServerKeyExchange or ClientKeyExchange messages to be invalid. |
| 429 | InvalidECDHPoint bool |
| 430 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 431 | // BadECDSAR controls ways in which the 'r' value of an ECDSA signature |
| 432 | // can be invalid. |
| 433 | BadECDSAR BadValue |
| 434 | BadECDSAS BadValue |
Adam Langley | 80842bd | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
| 436 | // MaxPadding causes CBC records to have the maximum possible padding. |
| 437 | MaxPadding bool |
| 438 | // PaddingFirstByteBad causes the first byte of the padding to be |
| 439 | // incorrect. |
| 440 | PaddingFirstByteBad bool |
| 441 | // PaddingFirstByteBadIf255 causes the first byte of padding to be |
| 442 | // incorrect if there's a maximum amount of padding (i.e. 255 bytes). |
| 443 | PaddingFirstByteBadIf255 bool |
Adam Langley | ac61fa3 | 2014-06-23 12:03:11 -0700 | [diff] [blame] | 444 | |
| 445 | // FailIfNotFallbackSCSV causes a server handshake to fail if the |
| 446 | // client doesn't send the fallback SCSV value. |
| 447 | FailIfNotFallbackSCSV bool |
David Benjamin | 35a7a44 | 2014-07-05 00:23:20 -0400 | [diff] [blame] | 448 | |
| 449 | // DuplicateExtension causes an extra empty extension of bogus type to |
| 450 | // be emitted in either the ClientHello or the ServerHello. |
| 451 | DuplicateExtension bool |
David Benjamin | 1c375dd | 2014-07-12 00:48:23 -0400 | [diff] [blame] | 452 | |
| 453 | // UnauthenticatedECDH causes the server to pretend ECDHE_RSA |
| 454 | // and ECDHE_ECDSA cipher suites are actually ECDH_anon. No |
| 455 | // Certificate message is sent and no signature is added to |
| 456 | // ServerKeyExchange. |
| 457 | UnauthenticatedECDH bool |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 458 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 459 | // SkipHelloVerifyRequest causes a DTLS server to skip the |
| 460 | // HelloVerifyRequest message. |
| 461 | SkipHelloVerifyRequest bool |
| 462 | |
David Benjamin | dcd979f | 2015-04-20 18:26:52 -0400 | [diff] [blame] | 463 | // SkipCertificateStatus, if true, causes the server to skip the |
| 464 | // CertificateStatus message. This is legal because CertificateStatus is |
| 465 | // optional, even with a status_request in ServerHello. |
| 466 | SkipCertificateStatus bool |
| 467 | |
David Benjamin | 9c651c9 | 2014-07-12 13:27:45 -0400 | [diff] [blame] | 468 | // SkipServerKeyExchange causes the server to skip sending |
| 469 | // ServerKeyExchange messages. |
| 470 | SkipServerKeyExchange bool |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 471 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 472 | // SkipNewSessionTicket causes the server to skip sending the |
| 473 | // NewSessionTicket message despite promising to in ServerHello. |
| 474 | SkipNewSessionTicket bool |
| 475 | |
David Benjamin | a0e5223 | 2014-07-19 17:39:58 -0400 | [diff] [blame] | 476 | // SkipChangeCipherSpec causes the implementation to skip |
| 477 | // sending the ChangeCipherSpec message (and adjusting cipher |
| 478 | // state accordingly for the Finished message). |
| 479 | SkipChangeCipherSpec bool |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 480 | |
David Benjamin | b80168e | 2015-02-08 18:30:14 -0500 | [diff] [blame] | 481 | // SkipFinished causes the implementation to skip sending the Finished |
| 482 | // message. |
| 483 | SkipFinished bool |
| 484 | |
David Benjamin | f3ec83d | 2014-07-21 22:42:34 -0400 | [diff] [blame] | 485 | // EarlyChangeCipherSpec causes the client to send an early |
| 486 | // ChangeCipherSpec message before the ClientKeyExchange. A value of |
| 487 | // zero disables this behavior. One and two configure variants for 0.9.8 |
| 488 | // and 1.0.1 modes, respectively. |
| 489 | EarlyChangeCipherSpec int |
David Benjamin | d23f412 | 2014-07-23 15:09:48 -0400 | [diff] [blame] | 490 | |
David Benjamin | 86271ee | 2014-07-21 16:14:03 -0400 | [diff] [blame] | 491 | // FragmentAcrossChangeCipherSpec causes the implementation to fragment |
| 492 | // the Finished (or NextProto) message around the ChangeCipherSpec |
| 493 | // messages. |
| 494 | FragmentAcrossChangeCipherSpec bool |
| 495 | |
David Benjamin | d86c767 | 2014-08-02 04:07:12 -0400 | [diff] [blame] | 496 | // SendV2ClientHello causes the client to send a V2ClientHello |
| 497 | // instead of a normal ClientHello. |
| 498 | SendV2ClientHello bool |
David Benjamin | bef270a | 2014-08-02 04:22:02 -0400 | [diff] [blame] | 499 | |
| 500 | // SendFallbackSCSV causes the client to include |
| 501 | // TLS_FALLBACK_SCSV in the ClientHello. |
| 502 | SendFallbackSCSV bool |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 503 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 504 | // SendRenegotiationSCSV causes the client to include the renegotiation |
| 505 | // SCSV in the ClientHello. |
| 506 | SendRenegotiationSCSV bool |
| 507 | |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 508 | // MaxHandshakeRecordLength, if non-zero, is the maximum size of a |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 509 | // handshake record. Handshake messages will be split into multiple |
| 510 | // records at the specified size, except that the client_version will |
David Benjamin | bd15a8e | 2015-05-29 18:48:16 -0400 | [diff] [blame] | 511 | // never be fragmented. For DTLS, it is the maximum handshake fragment |
| 512 | // size, not record size; DTLS allows multiple handshake fragments in a |
| 513 | // single handshake record. See |PackHandshakeFragments|. |
David Benjamin | 43ec06f | 2014-08-05 02:28:57 -0400 | [diff] [blame] | 514 | MaxHandshakeRecordLength int |
David Benjamin | a8e3e0e | 2014-08-06 22:11:10 -0400 | [diff] [blame] | 515 | |
David Benjamin | 9821454 | 2014-08-07 18:02:39 -0400 | [diff] [blame] | 516 | // FragmentClientVersion will allow MaxHandshakeRecordLength to apply to |
| 517 | // the first 6 bytes of the ClientHello. |
| 518 | FragmentClientVersion bool |
| 519 | |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 520 | // FragmentAlert will cause all alerts to be fragmented across |
| 521 | // two records. |
| 522 | FragmentAlert bool |
| 523 | |
David Benjamin | 3fd1fbd | 2015-02-03 16:07:32 -0500 | [diff] [blame] | 524 | // SendSpuriousAlert, if non-zero, will cause an spurious, unwanted |
| 525 | // alert to be sent. |
| 526 | SendSpuriousAlert alert |
Alex Chernyakhovsky | 4cd8c43 | 2014-11-01 19:39:08 -0400 | [diff] [blame] | 527 | |
David Benjamin | b36a395 | 2015-12-01 18:53:13 -0500 | [diff] [blame] | 528 | // BadRSAClientKeyExchange causes the client to send a corrupted RSA |
| 529 | // ClientKeyExchange which would not pass padding checks. |
| 530 | BadRSAClientKeyExchange RSABadValue |
David Benjamin | bed9aae | 2014-08-07 19:13:38 -0400 | [diff] [blame] | 531 | |
| 532 | // RenewTicketOnResume causes the server to renew the session ticket and |
| 533 | // send a NewSessionTicket message during an abbreviated handshake. |
| 534 | RenewTicketOnResume bool |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 535 | |
| 536 | // SendClientVersion, if non-zero, causes the client to send a different |
| 537 | // TLS version in the ClientHello than the maximum supported version. |
| 538 | SendClientVersion uint16 |
David Benjamin | 83c0bc9 | 2014-08-04 01:23:53 -0400 | [diff] [blame] | 539 | |
David Benjamin | e58c4f5 | 2014-08-24 03:47:07 -0400 | [diff] [blame] | 540 | // ExpectFalseStart causes the server to, on full handshakes, |
| 541 | // expect the peer to False Start; the server Finished message |
| 542 | // isn't sent until we receive an application data record |
| 543 | // from the peer. |
| 544 | ExpectFalseStart bool |
David Benjamin | 5c24a1d | 2014-08-31 00:59:27 -0400 | [diff] [blame] | 545 | |
David Benjamin | 1c63315 | 2015-04-02 20:19:11 -0400 | [diff] [blame] | 546 | // AlertBeforeFalseStartTest, if non-zero, causes the server to, on full |
| 547 | // handshakes, send an alert just before reading the application data |
| 548 | // record to test False Start. This can be used in a negative False |
| 549 | // Start test to determine whether the peer processed the alert (and |
| 550 | // closed the connection) before or after sending app data. |
| 551 | AlertBeforeFalseStartTest alert |
| 552 | |
David Benjamin | 39ebf53 | 2014-08-31 02:23:49 -0400 | [diff] [blame] | 553 | // SkipCipherVersionCheck causes the server to negotiate |
| 554 | // TLS 1.2 ciphers in earlier versions of TLS. |
| 555 | SkipCipherVersionCheck bool |
David Benjamin | e78bfde | 2014-09-06 12:45:15 -0400 | [diff] [blame] | 556 | |
| 557 | // ExpectServerName, if not empty, is the hostname the client |
| 558 | // must specify in the server_name extension. |
| 559 | ExpectServerName string |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 560 | |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 561 | // SwapNPNAndALPN switches the relative order between NPN and ALPN in |
| 562 | // both ClientHello and ServerHello. |
David Benjamin | fc7b086 | 2014-09-06 13:21:53 -0400 | [diff] [blame] | 563 | SwapNPNAndALPN bool |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 564 | |
Adam Langley | efb0e16 | 2015-07-09 11:35:04 -0700 | [diff] [blame] | 565 | // ALPNProtocol, if not nil, sets the ALPN protocol that a server will |
| 566 | // return. |
| 567 | ALPNProtocol *string |
| 568 | |
David Benjamin | 01fe820 | 2014-09-24 15:21:44 -0400 | [diff] [blame] | 569 | // AllowSessionVersionMismatch causes the server to resume sessions |
| 570 | // regardless of the version associated with the session. |
| 571 | AllowSessionVersionMismatch bool |
Adam Langley | 3831173 | 2014-10-16 19:04:35 -0700 | [diff] [blame] | 572 | |
| 573 | // CorruptTicket causes a client to corrupt a session ticket before |
| 574 | // sending it in a resume handshake. |
| 575 | CorruptTicket bool |
| 576 | |
| 577 | // OversizedSessionId causes the session id that is sent with a ticket |
| 578 | // resumption attempt to be too large (33 bytes). |
| 579 | OversizedSessionId bool |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 580 | |
| 581 | // RequireExtendedMasterSecret, if true, requires that the peer support |
| 582 | // the extended master secret option. |
| 583 | RequireExtendedMasterSecret bool |
| 584 | |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 585 | // NoExtendedMasterSecret causes the client and server to behave as if |
Adam Langley | 7571292 | 2014-10-10 16:23:43 -0700 | [diff] [blame] | 586 | // they didn't support an extended master secret. |
| 587 | NoExtendedMasterSecret bool |
Adam Langley | 2ae77d2 | 2014-10-28 17:29:33 -0700 | [diff] [blame] | 588 | |
| 589 | // EmptyRenegotiationInfo causes the renegotiation extension to be |
| 590 | // empty in a renegotiation handshake. |
| 591 | EmptyRenegotiationInfo bool |
| 592 | |
| 593 | // BadRenegotiationInfo causes the renegotiation extension value in a |
| 594 | // renegotiation handshake to be incorrect. |
| 595 | BadRenegotiationInfo bool |
David Benjamin | 5e961c1 | 2014-11-07 01:48:35 -0500 | [diff] [blame] | 596 | |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 597 | // NoRenegotiationInfo disables renegotiation info support in all |
| 598 | // handshakes. |
David Benjamin | ca6554b | 2014-11-08 12:31:52 -0500 | [diff] [blame] | 599 | NoRenegotiationInfo bool |
| 600 | |
David Benjamin | 3e052de | 2015-11-25 20:10:31 -0500 | [diff] [blame] | 601 | // NoRenegotiationInfoInInitial disables renegotiation info support in |
| 602 | // the initial handshake. |
| 603 | NoRenegotiationInfoInInitial bool |
| 604 | |
| 605 | // NoRenegotiationInfoAfterInitial disables renegotiation info support |
| 606 | // in renegotiation handshakes. |
| 607 | NoRenegotiationInfoAfterInitial bool |
| 608 | |
Adam Langley | 5021b22 | 2015-06-12 18:27:58 -0700 | [diff] [blame] | 609 | // RequireRenegotiationInfo, if true, causes the client to return an |
| 610 | // error if the server doesn't reply with the renegotiation extension. |
| 611 | RequireRenegotiationInfo bool |
| 612 | |
David Benjamin | 8e6db49 | 2015-07-25 18:29:23 -0400 | [diff] [blame] | 613 | // SequenceNumberMapping, if non-nil, is the mapping function to apply |
| 614 | // to the sequence number of outgoing packets. For both TLS and DTLS, |
| 615 | // the two most-significant bytes in the resulting sequence number are |
| 616 | // ignored so that the DTLS epoch cannot be changed. |
| 617 | SequenceNumberMapping func(uint64) uint64 |
David Benjamin | 9114fae | 2014-11-08 11:41:14 -0500 | [diff] [blame] | 618 | |
David Benjamin | a3e8949 | 2015-02-26 15:16:22 -0500 | [diff] [blame] | 619 | // RSAEphemeralKey, if true, causes the server to send a |
| 620 | // ServerKeyExchange message containing an ephemeral key (as in |
| 621 | // RSA_EXPORT) in the plain RSA key exchange. |
| 622 | RSAEphemeralKey bool |
David Benjamin | ca6c826 | 2014-11-15 19:06:08 -0500 | [diff] [blame] | 623 | |
| 624 | // SRTPMasterKeyIdentifer, if not empty, is the SRTP MKI value that the |
| 625 | // client offers when negotiating SRTP. MKI support is still missing so |
| 626 | // the peer must still send none. |
| 627 | SRTPMasterKeyIdentifer string |
| 628 | |
| 629 | // SendSRTPProtectionProfile, if non-zero, is the SRTP profile that the |
| 630 | // server sends in the ServerHello instead of the negotiated one. |
| 631 | SendSRTPProtectionProfile uint16 |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 632 | |
| 633 | // NoSignatureAndHashes, if true, causes the client to omit the |
| 634 | // signature and hashes extension. |
| 635 | // |
| 636 | // For a server, it will cause an empty list to be sent in the |
| 637 | // CertificateRequest message. None the less, the configured set will |
| 638 | // still be enforced. |
| 639 | NoSignatureAndHashes bool |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 640 | |
David Benjamin | 55a4364 | 2015-04-20 14:45:55 -0400 | [diff] [blame] | 641 | // NoSupportedCurves, if true, causes the client to omit the |
| 642 | // supported_curves extension. |
| 643 | NoSupportedCurves bool |
| 644 | |
David Benjamin | c44b1df | 2014-11-23 12:11:01 -0500 | [diff] [blame] | 645 | // RequireSameRenegoClientVersion, if true, causes the server |
| 646 | // to require that all ClientHellos match in offered version |
| 647 | // across a renego. |
| 648 | RequireSameRenegoClientVersion bool |
Feng Lu | 41aa325 | 2014-11-21 22:47:56 -0800 | [diff] [blame] | 649 | |
David Benjamin | 1e29a6b | 2014-12-10 02:27:24 -0500 | [diff] [blame] | 650 | // ExpectInitialRecordVersion, if non-zero, is the expected |
| 651 | // version of the records before the version is determined. |
| 652 | ExpectInitialRecordVersion uint16 |
David Benjamin | 13be1de | 2015-01-11 16:29:36 -0500 | [diff] [blame] | 653 | |
| 654 | // MaxPacketLength, if non-zero, is the maximum acceptable size for a |
| 655 | // packet. |
| 656 | MaxPacketLength int |
David Benjamin | 6095de8 | 2014-12-27 01:50:38 -0500 | [diff] [blame] | 657 | |
| 658 | // SendCipherSuite, if non-zero, is the cipher suite value that the |
| 659 | // server will send in the ServerHello. This does not affect the cipher |
| 660 | // the server believes it has actually negotiated. |
| 661 | SendCipherSuite uint16 |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 662 | |
David Benjamin | 4cf369b | 2015-08-22 01:35:43 -0400 | [diff] [blame] | 663 | // AppDataBeforeHandshake, if not nil, causes application data to be |
| 664 | // sent immediately before the first handshake message. |
| 665 | AppDataBeforeHandshake []byte |
| 666 | |
| 667 | // AppDataAfterChangeCipherSpec, if not nil, causes application data to |
David Benjamin | 4189bd9 | 2015-01-25 23:52:39 -0500 | [diff] [blame] | 668 | // be sent immediately after ChangeCipherSpec. |
| 669 | AppDataAfterChangeCipherSpec []byte |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 670 | |
David Benjamin | dc3da93 | 2015-03-12 15:09:02 -0400 | [diff] [blame] | 671 | // AlertAfterChangeCipherSpec, if non-zero, causes an alert to be sent |
| 672 | // immediately after ChangeCipherSpec. |
| 673 | AlertAfterChangeCipherSpec alert |
| 674 | |
David Benjamin | 83f9040 | 2015-01-27 01:09:43 -0500 | [diff] [blame] | 675 | // TimeoutSchedule is the schedule of packet drops and simulated |
| 676 | // timeouts for before each handshake leg from the peer. |
| 677 | TimeoutSchedule []time.Duration |
| 678 | |
| 679 | // PacketAdaptor is the packetAdaptor to use to simulate timeouts. |
| 680 | PacketAdaptor *packetAdaptor |
David Benjamin | b3774b9 | 2015-01-31 17:16:01 -0500 | [diff] [blame] | 681 | |
| 682 | // ReorderHandshakeFragments, if true, causes handshake fragments in |
| 683 | // DTLS to overlap and be sent in the wrong order. It also causes |
| 684 | // pre-CCS flights to be sent twice. (Post-CCS flights consist of |
| 685 | // Finished and will trigger a spurious retransmit.) |
| 686 | ReorderHandshakeFragments bool |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 687 | |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 688 | // MixCompleteMessageWithFragments, if true, causes handshake |
| 689 | // messages in DTLS to redundantly both fragment the message |
| 690 | // and include a copy of the full one. |
| 691 | MixCompleteMessageWithFragments bool |
| 692 | |
David Benjamin | ddb9f15 | 2015-02-03 15:44:39 -0500 | [diff] [blame] | 693 | // SendInvalidRecordType, if true, causes a record with an invalid |
| 694 | // content type to be sent immediately following the handshake. |
| 695 | SendInvalidRecordType bool |
David Benjamin | bcb2d91 | 2015-02-24 23:45:43 -0500 | [diff] [blame] | 696 | |
| 697 | // WrongCertificateMessageType, if true, causes Certificate message to |
| 698 | // be sent with the wrong message type. |
| 699 | WrongCertificateMessageType bool |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 700 | |
| 701 | // FragmentMessageTypeMismatch, if true, causes all non-initial |
| 702 | // handshake fragments in DTLS to have the wrong message type. |
| 703 | FragmentMessageTypeMismatch bool |
| 704 | |
| 705 | // FragmentMessageLengthMismatch, if true, causes all non-initial |
| 706 | // handshake fragments in DTLS to have the wrong message length. |
| 707 | FragmentMessageLengthMismatch bool |
| 708 | |
David Benjamin | 11fc66a | 2015-06-16 11:40:24 -0400 | [diff] [blame] | 709 | // SplitFragments, if non-zero, causes the handshake fragments in DTLS |
| 710 | // to be split across two records. The value of |SplitFragments| is the |
| 711 | // number of bytes in the first fragment. |
| 712 | SplitFragments int |
David Benjamin | 7538122 | 2015-03-02 19:30:30 -0500 | [diff] [blame] | 713 | |
| 714 | // SendEmptyFragments, if true, causes handshakes to include empty |
| 715 | // fragments in DTLS. |
| 716 | SendEmptyFragments bool |
David Benjamin | cdea40c | 2015-03-19 14:09:43 -0400 | [diff] [blame] | 717 | |
David Benjamin | 9a41d1b | 2015-05-16 01:30:09 -0400 | [diff] [blame] | 718 | // SendSplitAlert, if true, causes an alert to be sent with the header |
| 719 | // and record body split across multiple packets. The peer should |
| 720 | // discard these packets rather than process it. |
| 721 | SendSplitAlert bool |
| 722 | |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame] | 723 | // FailIfResumeOnRenego, if true, causes renegotiations to fail if the |
| 724 | // client offers a resumption or the server accepts one. |
| 725 | FailIfResumeOnRenego bool |
David Benjamin | 3c9746a | 2015-03-19 15:00:10 -0400 | [diff] [blame] | 726 | |
David Benjamin | 67d1fb5 | 2015-03-16 15:16:23 -0400 | [diff] [blame] | 727 | // IgnorePeerCipherPreferences, if true, causes the peer's cipher |
| 728 | // preferences to be ignored. |
| 729 | IgnorePeerCipherPreferences bool |
David Benjamin | 72dc783 | 2015-03-16 17:49:43 -0400 | [diff] [blame] | 730 | |
| 731 | // IgnorePeerSignatureAlgorithmPreferences, if true, causes the peer's |
| 732 | // signature algorithm preferences to be ignored. |
| 733 | IgnorePeerSignatureAlgorithmPreferences bool |
David Benjamin | 340d5ed | 2015-03-21 02:21:37 -0400 | [diff] [blame] | 734 | |
David Benjamin | c574f41 | 2015-04-20 11:13:01 -0400 | [diff] [blame] | 735 | // IgnorePeerCurvePreferences, if true, causes the peer's curve |
| 736 | // preferences to be ignored. |
| 737 | IgnorePeerCurvePreferences bool |
| 738 | |
David Benjamin | 513f0ea | 2015-04-02 19:33:31 -0400 | [diff] [blame] | 739 | // BadFinished, if true, causes the Finished hash to be broken. |
| 740 | BadFinished bool |
Adam Langley | a7997f1 | 2015-05-14 17:38:50 -0700 | [diff] [blame] | 741 | |
| 742 | // DHGroupPrime, if not nil, is used to define the (finite field) |
| 743 | // Diffie-Hellman group. The generator used is always two. |
| 744 | DHGroupPrime *big.Int |
David Benjamin | bd15a8e | 2015-05-29 18:48:16 -0400 | [diff] [blame] | 745 | |
| 746 | // PackHandshakeFragments, if true, causes handshake fragments to be |
| 747 | // packed into individual handshake records, up to the specified record |
| 748 | // size. |
| 749 | PackHandshakeFragments int |
| 750 | |
| 751 | // PackHandshakeRecords, if true, causes handshake records to be packed |
| 752 | // into individual packets, up to the specified packet size. |
| 753 | PackHandshakeRecords int |
David Benjamin | 0fa4012 | 2015-05-30 17:13:12 -0400 | [diff] [blame] | 754 | |
| 755 | // EnableAllCiphersInDTLS, if true, causes RC4 to be enabled in DTLS. |
| 756 | EnableAllCiphersInDTLS bool |
David Benjamin | 8923c0b | 2015-06-07 11:42:34 -0400 | [diff] [blame] | 757 | |
| 758 | // EmptyCertificateList, if true, causes the server to send an empty |
| 759 | // certificate list in the Certificate message. |
| 760 | EmptyCertificateList bool |
David Benjamin | d98452d | 2015-06-16 14:16:23 -0400 | [diff] [blame] | 761 | |
| 762 | // ExpectNewTicket, if true, causes the client to abort if it does not |
| 763 | // receive a new ticket. |
| 764 | ExpectNewTicket bool |
Adam Langley | 33ad2b5 | 2015-07-20 17:43:53 -0700 | [diff] [blame] | 765 | |
| 766 | // RequireClientHelloSize, if not zero, is the required length in bytes |
| 767 | // of the ClientHello /record/. This is checked by the server. |
| 768 | RequireClientHelloSize int |
Adam Langley | 0950563 | 2015-07-30 18:10:13 -0700 | [diff] [blame] | 769 | |
| 770 | // CustomExtension, if not empty, contains the contents of an extension |
| 771 | // that will be added to client/server hellos. |
| 772 | CustomExtension string |
| 773 | |
| 774 | // ExpectedCustomExtension, if not nil, contains the expected contents |
| 775 | // of a custom extension. |
| 776 | ExpectedCustomExtension *string |
David Benjamin | 30789da | 2015-08-29 22:56:45 -0400 | [diff] [blame] | 777 | |
| 778 | // NoCloseNotify, if true, causes the close_notify alert to be skipped |
| 779 | // on connection shutdown. |
| 780 | NoCloseNotify bool |
| 781 | |
| 782 | // ExpectCloseNotify, if true, requires a close_notify from the peer on |
| 783 | // shutdown. Records from the peer received after close_notify is sent |
| 784 | // are not discard. |
| 785 | ExpectCloseNotify bool |
David Benjamin | 2c99d28 | 2015-09-01 10:23:00 -0400 | [diff] [blame] | 786 | |
| 787 | // SendLargeRecords, if true, allows outgoing records to be sent |
| 788 | // arbitrarily large. |
| 789 | SendLargeRecords bool |
David Benjamin | 76c2efc | 2015-08-31 14:24:29 -0400 | [diff] [blame] | 790 | |
| 791 | // NegotiateALPNAndNPN, if true, causes the server to negotiate both |
| 792 | // ALPN and NPN in the same connetion. |
| 793 | NegotiateALPNAndNPN bool |
David Benjamin | dd6fed9 | 2015-10-23 17:41:12 -0400 | [diff] [blame] | 794 | |
| 795 | // SendEmptySessionTicket, if true, causes the server to send an empty |
| 796 | // session ticket. |
| 797 | SendEmptySessionTicket bool |
| 798 | |
| 799 | // FailIfSessionOffered, if true, causes the server to fail any |
| 800 | // connections where the client offers a non-empty session ID or session |
| 801 | // ticket. |
| 802 | FailIfSessionOffered bool |
Adam Langley | 27a0d08 | 2015-11-03 13:34:10 -0800 | [diff] [blame] | 803 | |
| 804 | // SendHelloRequestBeforeEveryAppDataRecord, if true, causes a |
| 805 | // HelloRequest handshake message to be sent before each application |
| 806 | // data record. This only makes sense for a server. |
| 807 | SendHelloRequestBeforeEveryAppDataRecord bool |
Adam Langley | c4f25ce | 2015-11-26 16:39:08 -0800 | [diff] [blame] | 808 | |
| 809 | // RequireDHPublicValueLen causes a fatal error if the length (in |
| 810 | // bytes) of the server's Diffie-Hellman public value is not equal to |
| 811 | // this. |
| 812 | RequireDHPublicValueLen int |
David Benjamin | 8411b24 | 2015-11-26 12:07:28 -0500 | [diff] [blame] | 813 | |
| 814 | // BadChangeCipherSpec, if not nil, is the body to be sent in |
| 815 | // ChangeCipherSpec records instead of {1}. |
| 816 | BadChangeCipherSpec []byte |
David Benjamin | ef5dfd2 | 2015-12-06 13:17:07 -0500 | [diff] [blame] | 817 | |
| 818 | // BadHelloRequest, if not nil, is what to send instead of a |
| 819 | // HelloRequest. |
| 820 | BadHelloRequest []byte |
David Benjamin | ef1b009 | 2015-11-21 14:05:44 -0500 | [diff] [blame] | 821 | |
| 822 | // RequireSessionTickets, if true, causes the client to require new |
| 823 | // sessions use session tickets instead of session IDs. |
| 824 | RequireSessionTickets bool |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | func (c *Config) serverInit() { |
| 828 | if c.SessionTicketsDisabled { |
| 829 | return |
| 830 | } |
| 831 | |
| 832 | // If the key has already been set then we have nothing to do. |
| 833 | for _, b := range c.SessionTicketKey { |
| 834 | if b != 0 { |
| 835 | return |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if _, err := io.ReadFull(c.rand(), c.SessionTicketKey[:]); err != nil { |
| 840 | c.SessionTicketsDisabled = true |
| 841 | } |
| 842 | } |
| 843 | |
| 844 | func (c *Config) rand() io.Reader { |
| 845 | r := c.Rand |
| 846 | if r == nil { |
| 847 | return rand.Reader |
| 848 | } |
| 849 | return r |
| 850 | } |
| 851 | |
| 852 | func (c *Config) time() time.Time { |
| 853 | t := c.Time |
| 854 | if t == nil { |
| 855 | t = time.Now |
| 856 | } |
| 857 | return t() |
| 858 | } |
| 859 | |
| 860 | func (c *Config) cipherSuites() []uint16 { |
| 861 | s := c.CipherSuites |
| 862 | if s == nil { |
| 863 | s = defaultCipherSuites() |
| 864 | } |
| 865 | return s |
| 866 | } |
| 867 | |
| 868 | func (c *Config) minVersion() uint16 { |
| 869 | if c == nil || c.MinVersion == 0 { |
| 870 | return minVersion |
| 871 | } |
| 872 | return c.MinVersion |
| 873 | } |
| 874 | |
| 875 | func (c *Config) maxVersion() uint16 { |
| 876 | if c == nil || c.MaxVersion == 0 { |
| 877 | return maxVersion |
| 878 | } |
| 879 | return c.MaxVersion |
| 880 | } |
| 881 | |
David Benjamin | cba2b62 | 2015-12-18 22:13:41 -0500 | [diff] [blame] | 882 | var defaultCurvePreferences = []CurveID{CurveX25519, CurveP256, CurveP384, CurveP521} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 883 | |
| 884 | func (c *Config) curvePreferences() []CurveID { |
| 885 | if c == nil || len(c.CurvePreferences) == 0 { |
| 886 | return defaultCurvePreferences |
| 887 | } |
| 888 | return c.CurvePreferences |
| 889 | } |
| 890 | |
| 891 | // mutualVersion returns the protocol version to use given the advertised |
| 892 | // version of the peer. |
| 893 | func (c *Config) mutualVersion(vers uint16) (uint16, bool) { |
| 894 | minVersion := c.minVersion() |
| 895 | maxVersion := c.maxVersion() |
| 896 | |
| 897 | if vers < minVersion { |
| 898 | return 0, false |
| 899 | } |
| 900 | if vers > maxVersion { |
| 901 | vers = maxVersion |
| 902 | } |
| 903 | return vers, true |
| 904 | } |
| 905 | |
| 906 | // getCertificateForName returns the best certificate for the given name, |
| 907 | // defaulting to the first element of c.Certificates if there are no good |
| 908 | // options. |
| 909 | func (c *Config) getCertificateForName(name string) *Certificate { |
| 910 | if len(c.Certificates) == 1 || c.NameToCertificate == nil { |
| 911 | // There's only one choice, so no point doing any work. |
| 912 | return &c.Certificates[0] |
| 913 | } |
| 914 | |
| 915 | name = strings.ToLower(name) |
| 916 | for len(name) > 0 && name[len(name)-1] == '.' { |
| 917 | name = name[:len(name)-1] |
| 918 | } |
| 919 | |
| 920 | if cert, ok := c.NameToCertificate[name]; ok { |
| 921 | return cert |
| 922 | } |
| 923 | |
| 924 | // try replacing labels in the name with wildcards until we get a |
| 925 | // match. |
| 926 | labels := strings.Split(name, ".") |
| 927 | for i := range labels { |
| 928 | labels[i] = "*" |
| 929 | candidate := strings.Join(labels, ".") |
| 930 | if cert, ok := c.NameToCertificate[candidate]; ok { |
| 931 | return cert |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | // If nothing matches, return the first certificate. |
| 936 | return &c.Certificates[0] |
| 937 | } |
| 938 | |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 939 | func (c *Config) signatureAndHashesForServer() []signatureAndHash { |
| 940 | if c != nil && c.SignatureAndHashes != nil { |
| 941 | return c.SignatureAndHashes |
| 942 | } |
| 943 | return supportedClientCertSignatureAlgorithms |
| 944 | } |
| 945 | |
| 946 | func (c *Config) signatureAndHashesForClient() []signatureAndHash { |
| 947 | if c != nil && c.SignatureAndHashes != nil { |
| 948 | return c.SignatureAndHashes |
| 949 | } |
| 950 | return supportedSKXSignatureAlgorithms |
| 951 | } |
| 952 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 953 | // BuildNameToCertificate parses c.Certificates and builds c.NameToCertificate |
| 954 | // from the CommonName and SubjectAlternateName fields of each of the leaf |
| 955 | // certificates. |
| 956 | func (c *Config) BuildNameToCertificate() { |
| 957 | c.NameToCertificate = make(map[string]*Certificate) |
| 958 | for i := range c.Certificates { |
| 959 | cert := &c.Certificates[i] |
| 960 | x509Cert, err := x509.ParseCertificate(cert.Certificate[0]) |
| 961 | if err != nil { |
| 962 | continue |
| 963 | } |
| 964 | if len(x509Cert.Subject.CommonName) > 0 { |
| 965 | c.NameToCertificate[x509Cert.Subject.CommonName] = cert |
| 966 | } |
| 967 | for _, san := range x509Cert.DNSNames { |
| 968 | c.NameToCertificate[san] = cert |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | // A Certificate is a chain of one or more certificates, leaf first. |
| 974 | type Certificate struct { |
| 975 | Certificate [][]byte |
| 976 | PrivateKey crypto.PrivateKey // supported types: *rsa.PrivateKey, *ecdsa.PrivateKey |
| 977 | // OCSPStaple contains an optional OCSP response which will be served |
| 978 | // to clients that request it. |
| 979 | OCSPStaple []byte |
David Benjamin | 61f9527 | 2014-11-25 01:55:35 -0500 | [diff] [blame] | 980 | // SignedCertificateTimestampList contains an optional encoded |
| 981 | // SignedCertificateTimestampList structure which will be |
| 982 | // served to clients that request it. |
| 983 | SignedCertificateTimestampList []byte |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 984 | // Leaf is the parsed form of the leaf certificate, which may be |
| 985 | // initialized using x509.ParseCertificate to reduce per-handshake |
| 986 | // processing for TLS clients doing client authentication. If nil, the |
| 987 | // leaf certificate will be parsed as needed. |
| 988 | Leaf *x509.Certificate |
| 989 | } |
| 990 | |
| 991 | // A TLS record. |
| 992 | type record struct { |
| 993 | contentType recordType |
| 994 | major, minor uint8 |
| 995 | payload []byte |
| 996 | } |
| 997 | |
| 998 | type handshakeMessage interface { |
| 999 | marshal() []byte |
| 1000 | unmarshal([]byte) bool |
| 1001 | } |
| 1002 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1003 | // lruSessionCache is a client or server session cache implementation |
| 1004 | // that uses an LRU caching strategy. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1005 | type lruSessionCache struct { |
| 1006 | sync.Mutex |
| 1007 | |
| 1008 | m map[string]*list.Element |
| 1009 | q *list.List |
| 1010 | capacity int |
| 1011 | } |
| 1012 | |
| 1013 | type lruSessionCacheEntry struct { |
| 1014 | sessionKey string |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1015 | state interface{} |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | // Put adds the provided (sessionKey, cs) pair to the cache. |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1019 | func (c *lruSessionCache) Put(sessionKey string, cs interface{}) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1020 | c.Lock() |
| 1021 | defer c.Unlock() |
| 1022 | |
| 1023 | if elem, ok := c.m[sessionKey]; ok { |
| 1024 | entry := elem.Value.(*lruSessionCacheEntry) |
| 1025 | entry.state = cs |
| 1026 | c.q.MoveToFront(elem) |
| 1027 | return |
| 1028 | } |
| 1029 | |
| 1030 | if c.q.Len() < c.capacity { |
| 1031 | entry := &lruSessionCacheEntry{sessionKey, cs} |
| 1032 | c.m[sessionKey] = c.q.PushFront(entry) |
| 1033 | return |
| 1034 | } |
| 1035 | |
| 1036 | elem := c.q.Back() |
| 1037 | entry := elem.Value.(*lruSessionCacheEntry) |
| 1038 | delete(c.m, entry.sessionKey) |
| 1039 | entry.sessionKey = sessionKey |
| 1040 | entry.state = cs |
| 1041 | c.q.MoveToFront(elem) |
| 1042 | c.m[sessionKey] = elem |
| 1043 | } |
| 1044 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1045 | // Get returns the value associated with a given key. It returns (nil, |
| 1046 | // false) if no value is found. |
| 1047 | func (c *lruSessionCache) Get(sessionKey string) (interface{}, bool) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1048 | c.Lock() |
| 1049 | defer c.Unlock() |
| 1050 | |
| 1051 | if elem, ok := c.m[sessionKey]; ok { |
| 1052 | c.q.MoveToFront(elem) |
| 1053 | return elem.Value.(*lruSessionCacheEntry).state, true |
| 1054 | } |
| 1055 | return nil, false |
| 1056 | } |
| 1057 | |
David Benjamin | fe8eb9a | 2014-11-17 03:19:02 -0500 | [diff] [blame] | 1058 | // lruClientSessionCache is a ClientSessionCache implementation that |
| 1059 | // uses an LRU caching strategy. |
| 1060 | type lruClientSessionCache struct { |
| 1061 | lruSessionCache |
| 1062 | } |
| 1063 | |
| 1064 | func (c *lruClientSessionCache) Put(sessionKey string, cs *ClientSessionState) { |
| 1065 | c.lruSessionCache.Put(sessionKey, cs) |
| 1066 | } |
| 1067 | |
| 1068 | func (c *lruClientSessionCache) Get(sessionKey string) (*ClientSessionState, bool) { |
| 1069 | cs, ok := c.lruSessionCache.Get(sessionKey) |
| 1070 | if !ok { |
| 1071 | return nil, false |
| 1072 | } |
| 1073 | return cs.(*ClientSessionState), true |
| 1074 | } |
| 1075 | |
| 1076 | // lruServerSessionCache is a ServerSessionCache implementation that |
| 1077 | // uses an LRU caching strategy. |
| 1078 | type lruServerSessionCache struct { |
| 1079 | lruSessionCache |
| 1080 | } |
| 1081 | |
| 1082 | func (c *lruServerSessionCache) Put(sessionId string, session *sessionState) { |
| 1083 | c.lruSessionCache.Put(sessionId, session) |
| 1084 | } |
| 1085 | |
| 1086 | func (c *lruServerSessionCache) Get(sessionId string) (*sessionState, bool) { |
| 1087 | cs, ok := c.lruSessionCache.Get(sessionId) |
| 1088 | if !ok { |
| 1089 | return nil, false |
| 1090 | } |
| 1091 | return cs.(*sessionState), true |
| 1092 | } |
| 1093 | |
| 1094 | // NewLRUClientSessionCache returns a ClientSessionCache with the given |
| 1095 | // capacity that uses an LRU strategy. If capacity is < 1, a default capacity |
| 1096 | // is used instead. |
| 1097 | func NewLRUClientSessionCache(capacity int) ClientSessionCache { |
| 1098 | const defaultSessionCacheCapacity = 64 |
| 1099 | |
| 1100 | if capacity < 1 { |
| 1101 | capacity = defaultSessionCacheCapacity |
| 1102 | } |
| 1103 | return &lruClientSessionCache{ |
| 1104 | lruSessionCache{ |
| 1105 | m: make(map[string]*list.Element), |
| 1106 | q: list.New(), |
| 1107 | capacity: capacity, |
| 1108 | }, |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | // NewLRUServerSessionCache returns a ServerSessionCache with the given |
| 1113 | // capacity that uses an LRU strategy. If capacity is < 1, a default capacity |
| 1114 | // is used instead. |
| 1115 | func NewLRUServerSessionCache(capacity int) ServerSessionCache { |
| 1116 | const defaultSessionCacheCapacity = 64 |
| 1117 | |
| 1118 | if capacity < 1 { |
| 1119 | capacity = defaultSessionCacheCapacity |
| 1120 | } |
| 1121 | return &lruServerSessionCache{ |
| 1122 | lruSessionCache{ |
| 1123 | m: make(map[string]*list.Element), |
| 1124 | q: list.New(), |
| 1125 | capacity: capacity, |
| 1126 | }, |
| 1127 | } |
| 1128 | } |
| 1129 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1130 | // TODO(jsing): Make these available to both crypto/x509 and crypto/tls. |
| 1131 | type dsaSignature struct { |
| 1132 | R, S *big.Int |
| 1133 | } |
| 1134 | |
| 1135 | type ecdsaSignature dsaSignature |
| 1136 | |
| 1137 | var emptyConfig Config |
| 1138 | |
| 1139 | func defaultConfig() *Config { |
| 1140 | return &emptyConfig |
| 1141 | } |
| 1142 | |
| 1143 | var ( |
| 1144 | once sync.Once |
| 1145 | varDefaultCipherSuites []uint16 |
| 1146 | ) |
| 1147 | |
| 1148 | func defaultCipherSuites() []uint16 { |
| 1149 | once.Do(initDefaultCipherSuites) |
| 1150 | return varDefaultCipherSuites |
| 1151 | } |
| 1152 | |
| 1153 | func initDefaultCipherSuites() { |
David Benjamin | 48cae08 | 2014-10-27 01:06:24 -0400 | [diff] [blame] | 1154 | for _, suite := range cipherSuites { |
| 1155 | if suite.flags&suitePSK == 0 { |
| 1156 | varDefaultCipherSuites = append(varDefaultCipherSuites, suite.id) |
| 1157 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1158 | } |
| 1159 | } |
| 1160 | |
| 1161 | func unexpectedMessageError(wanted, got interface{}) error { |
| 1162 | return fmt.Errorf("tls: received unexpected handshake message of type %T when waiting for %T", got, wanted) |
| 1163 | } |
David Benjamin | 000800a | 2014-11-14 01:43:59 -0500 | [diff] [blame] | 1164 | |
| 1165 | func isSupportedSignatureAndHash(sigHash signatureAndHash, sigHashes []signatureAndHash) bool { |
| 1166 | for _, s := range sigHashes { |
| 1167 | if s == sigHash { |
| 1168 | return true |
| 1169 | } |
| 1170 | } |
| 1171 | return false |
| 1172 | } |