2009-11-03 02:25:20 +00:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2010-04-05 22:38:02 +01:00
|
|
|
"crypto/rand"
|
2009-12-15 23:33:31 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"io"
|
2010-04-05 22:38:02 +01:00
|
|
|
"io/ioutil"
|
2010-08-05 21:14:41 +01:00
|
|
|
"sync"
|
2010-04-05 22:38:02 +01:00
|
|
|
"time"
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2010-04-27 06:19:04 +01:00
|
|
|
maxPlaintext = 16384 // maximum plaintext payload length
|
|
|
|
maxCiphertext = 16384 + 2048 // maximum ciphertext payload length
|
|
|
|
recordHeaderLen = 5 // record header length
|
|
|
|
maxHandshake = 65536 // maximum handshake we support (protocol max is 16 MB)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
minVersion = 0x0301 // minimum supported version - TLS 1.0
|
2010-12-15 16:49:55 +00:00
|
|
|
maxVersion = 0x0301 // maximum supported version - TLS 1.0
|
2010-04-27 06:19:04 +01:00
|
|
|
)
|
2009-11-03 02:25:20 +00:00
|
|
|
|
|
|
|
// TLS record types.
|
|
|
|
type recordType uint8
|
|
|
|
|
|
|
|
const (
|
2009-12-15 23:33:31 +00:00
|
|
|
recordTypeChangeCipherSpec recordType = 20
|
|
|
|
recordTypeAlert recordType = 21
|
|
|
|
recordTypeHandshake recordType = 22
|
|
|
|
recordTypeApplicationData recordType = 23
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TLS handshake message types.
|
|
|
|
const (
|
2010-08-16 16:22:22 +01:00
|
|
|
typeClientHello uint8 = 1
|
|
|
|
typeServerHello uint8 = 2
|
|
|
|
typeCertificate uint8 = 11
|
|
|
|
typeCertificateRequest uint8 = 13
|
|
|
|
typeServerHelloDone uint8 = 14
|
|
|
|
typeCertificateVerify uint8 = 15
|
|
|
|
typeClientKeyExchange uint8 = 16
|
|
|
|
typeFinished uint8 = 20
|
|
|
|
typeCertificateStatus uint8 = 22
|
|
|
|
typeNextProtocol uint8 = 67 // Not IANA assigned
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TLS compression types.
|
2010-07-14 15:40:15 +01:00
|
|
|
const (
|
2009-12-15 23:33:31 +00:00
|
|
|
compressionNone uint8 = 0
|
2009-11-03 02:25:20 +00:00
|
|
|
)
|
|
|
|
|
2009-12-23 19:13:09 +00:00
|
|
|
// TLS extension numbers
|
|
|
|
var (
|
2010-07-14 15:40:15 +01:00
|
|
|
extensionServerName uint16 = 0
|
|
|
|
extensionStatusRequest uint16 = 5
|
|
|
|
extensionNextProtoNeg uint16 = 13172 // not IANA assigned
|
|
|
|
)
|
|
|
|
|
|
|
|
// TLS CertificateStatusType (RFC 3546)
|
|
|
|
const (
|
|
|
|
statusTypeOCSP uint8 = 1
|
2009-12-23 19:13:09 +00:00
|
|
|
)
|
|
|
|
|
2010-08-16 16:22:22 +01:00
|
|
|
// Certificate types (for certificateRequestMsg)
|
|
|
|
const (
|
|
|
|
certTypeRSASign = 1 // A certificate containing an RSA key
|
|
|
|
certTypeDSSSign = 2 // A certificate containing a DSA key
|
|
|
|
certTypeRSAFixedDH = 3 // A certificate containing a static DH key
|
|
|
|
certTypeDSSFixedDH = 4 // A certficiate containing a static DH key
|
|
|
|
// Rest of these are reserved by the TLS spec
|
|
|
|
)
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// ConnectionState records basic TLS details about the connection.
|
2009-11-03 02:25:20 +00:00
|
|
|
type ConnectionState struct {
|
2009-12-23 19:13:09 +00:00
|
|
|
HandshakeComplete bool
|
2010-07-14 15:40:15 +01:00
|
|
|
CipherSuite uint16
|
2009-12-23 19:13:09 +00:00
|
|
|
NegotiatedProtocol string
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A Config structure is used to configure a TLS client or server. After one
|
|
|
|
// has been passed to a TLS function it must not be modified.
|
|
|
|
type Config struct {
|
|
|
|
// Rand provides the source of entropy for nonces and RSA blinding.
|
2010-12-07 21:15:15 +00:00
|
|
|
// If Rand is nil, TLS uses the cryptographic random reader in package
|
|
|
|
// crypto/rand.
|
2009-12-15 23:33:31 +00:00
|
|
|
Rand io.Reader
|
2010-12-07 21:15:15 +00:00
|
|
|
|
2009-11-03 02:25:20 +00:00
|
|
|
// Time returns the current time as the number of seconds since the epoch.
|
2010-12-07 21:15:15 +00:00
|
|
|
// If Time is nil, TLS uses the system time.Seconds.
|
2010-08-16 16:22:22 +01:00
|
|
|
Time func() int64
|
2010-12-07 21:15:15 +00:00
|
|
|
|
|
|
|
// Certificates contains one or more certificate chains
|
|
|
|
// to present to the other side of the connection.
|
|
|
|
// Server configurations must include at least one certificate.
|
2009-12-15 23:33:31 +00:00
|
|
|
Certificates []Certificate
|
2010-12-07 21:15:15 +00:00
|
|
|
|
|
|
|
// RootCAs defines the set of root certificate authorities
|
|
|
|
// that clients use when verifying server certificates.
|
|
|
|
// If RootCAs is nil, TLS uses the host's root CA set.
|
|
|
|
RootCAs *CASet
|
|
|
|
|
2009-12-23 19:13:09 +00:00
|
|
|
// NextProtos is a list of supported, application level protocols.
|
|
|
|
// Currently only server-side handling is supported.
|
|
|
|
NextProtos []string
|
2010-12-07 21:15:15 +00:00
|
|
|
|
2010-07-21 16:36:01 +01:00
|
|
|
// ServerName is included in the client's handshake to support virtual
|
|
|
|
// hosting.
|
|
|
|
ServerName string
|
2010-12-07 21:15:15 +00:00
|
|
|
|
|
|
|
// AuthenticateClient controls whether a server will request a certificate
|
2010-08-16 16:22:22 +01:00
|
|
|
// from the client. It does not require that the client send a
|
2010-12-07 21:15:15 +00:00
|
|
|
// certificate nor does it require that the certificate sent be
|
|
|
|
// anything more than self-signed.
|
2010-08-16 16:22:22 +01:00
|
|
|
AuthenticateClient bool
|
2010-12-15 16:49:55 +00:00
|
|
|
|
|
|
|
// CipherSuites is a list of supported cipher suites. If CipherSuites
|
|
|
|
// is nil, TLS uses a list of suites supported by the implementation.
|
|
|
|
CipherSuites []uint16
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
func (c *Config) rand() io.Reader {
|
|
|
|
r := c.Rand
|
|
|
|
if r == nil {
|
|
|
|
return rand.Reader
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) time() int64 {
|
|
|
|
t := c.Time
|
|
|
|
if t == nil {
|
|
|
|
t = time.Seconds
|
|
|
|
}
|
|
|
|
return t()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) rootCAs() *CASet {
|
|
|
|
s := c.RootCAs
|
|
|
|
if s == nil {
|
|
|
|
s = defaultRoots()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
func (c *Config) cipherSuites() []uint16 {
|
|
|
|
s := c.CipherSuites
|
|
|
|
if len(s) == 0 {
|
|
|
|
s = defaultCipherSuites()
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// A Certificate is a chain of one or more certificates, leaf first.
|
2009-11-03 02:25:20 +00:00
|
|
|
type Certificate struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
Certificate [][]byte
|
|
|
|
PrivateKey *rsa.PrivateKey
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A TLS record.
|
|
|
|
type record struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
contentType recordType
|
|
|
|
major, minor uint8
|
|
|
|
payload []byte
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type handshakeMessage interface {
|
2009-12-15 23:33:31 +00:00
|
|
|
marshal() []byte
|
2010-04-27 06:19:04 +01:00
|
|
|
unmarshal([]byte) bool
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// mutualVersion returns the protocol version to use given the advertised
|
|
|
|
// version of the peer.
|
2010-04-27 06:19:04 +01:00
|
|
|
func mutualVersion(vers uint16) (uint16, bool) {
|
|
|
|
if vers < minVersion {
|
|
|
|
return 0, false
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
if vers > maxVersion {
|
|
|
|
vers = maxVersion
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
return vers, true
|
2009-11-03 02:25:20 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
var emptyConfig Config
|
2010-08-05 21:14:41 +01:00
|
|
|
|
2010-04-05 22:38:02 +01:00
|
|
|
func defaultConfig() *Config {
|
2010-12-07 21:15:15 +00:00
|
|
|
return &emptyConfig
|
2010-04-05 22:38:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Possible certificate files; stop after finding one.
|
|
|
|
// On OS X we should really be using the Directory Services keychain
|
|
|
|
// but that requires a lot of Mach goo to get at. Instead we use
|
|
|
|
// the same root set that curl uses.
|
|
|
|
var certFiles = []string{
|
|
|
|
"/etc/ssl/certs/ca-certificates.crt", // Linux etc
|
|
|
|
"/usr/share/curl/curl-ca-bundle.crt", // OS X
|
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
var once sync.Once
|
|
|
|
|
|
|
|
func defaultRoots() *CASet {
|
2010-12-15 16:49:55 +00:00
|
|
|
once.Do(initDefaults)
|
2010-12-07 21:15:15 +00:00
|
|
|
return varDefaultRoots
|
|
|
|
}
|
|
|
|
|
2010-12-15 16:49:55 +00:00
|
|
|
func defaultCipherSuites() []uint16 {
|
|
|
|
once.Do(initDefaults)
|
|
|
|
return varDefaultCipherSuites
|
|
|
|
}
|
|
|
|
|
|
|
|
func initDefaults() {
|
|
|
|
initDefaultRoots()
|
|
|
|
initDefaultCipherSuites()
|
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
var varDefaultRoots *CASet
|
|
|
|
|
|
|
|
func initDefaultRoots() {
|
2010-04-05 22:38:02 +01:00
|
|
|
roots := NewCASet()
|
|
|
|
for _, file := range certFiles {
|
|
|
|
data, err := ioutil.ReadFile(file)
|
|
|
|
if err == nil {
|
|
|
|
roots.SetFromPEM(data)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2010-12-07 21:15:15 +00:00
|
|
|
varDefaultRoots = roots
|
2010-04-05 22:38:02 +01:00
|
|
|
}
|
2010-12-15 16:49:55 +00:00
|
|
|
|
|
|
|
var varDefaultCipherSuites []uint16
|
|
|
|
|
|
|
|
func initDefaultCipherSuites() {
|
|
|
|
varDefaultCipherSuites = make([]uint16, len(cipherSuites))
|
|
|
|
i := 0
|
|
|
|
for id, _ := range cipherSuites {
|
|
|
|
varDefaultCipherSuites[i] = id
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
}
|