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