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