2009-11-06 00:43:29 +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.
|
|
|
|
|
2012-03-06 17:49:29 +00:00
|
|
|
// Package tls partially implements TLS 1.0, as specified in RFC 2246.
|
2009-11-06 00:43:29 +00:00
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2010-11-05 13:54:56 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2011-11-02 02:04:37 +00:00
|
|
|
"errors"
|
2010-07-02 18:00:18 +01:00
|
|
|
"io/ioutil"
|
2009-12-15 23:33:31 +00:00
|
|
|
"net"
|
2010-11-05 13:54:56 +00:00
|
|
|
"strings"
|
2009-11-06 00:43:29 +00:00
|
|
|
)
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// Server returns a new TLS server side connection
|
|
|
|
// using conn as the underlying transport.
|
|
|
|
// The configuration config must be non-nil and must have
|
|
|
|
// at least one certificate.
|
2009-11-21 23:53:03 +00:00
|
|
|
func Server(conn net.Conn, config *Config) *Conn {
|
2010-04-27 06:19:04 +01:00
|
|
|
return &Conn{conn: conn, config: config}
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// Client returns a new TLS client side connection
|
|
|
|
// using conn as the underlying transport.
|
|
|
|
// Client interprets a nil configuration as equivalent to
|
|
|
|
// the zero configuration; see the documentation of Config
|
|
|
|
// for the defaults.
|
2009-11-21 23:53:03 +00:00
|
|
|
func Client(conn net.Conn, config *Config) *Conn {
|
2010-04-27 06:19:04 +01:00
|
|
|
return &Conn{conn: conn, config: config, isClient: true}
|
2009-11-21 23:53:03 +00:00
|
|
|
}
|
|
|
|
|
2012-02-03 20:08:53 +00:00
|
|
|
// A listener implements a network listener (net.Listener) for TLS connections.
|
|
|
|
type listener struct {
|
|
|
|
net.Listener
|
|
|
|
config *Config
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// Accept waits for and returns the next incoming TLS connection.
|
|
|
|
// The returned connection c is a *tls.Conn.
|
2012-02-03 20:08:53 +00:00
|
|
|
func (l *listener) Accept() (c net.Conn, err error) {
|
|
|
|
c, err = l.Listener.Accept()
|
2009-11-06 00:43:29 +00:00
|
|
|
if err != nil {
|
2009-11-09 20:07:39 +00:00
|
|
|
return
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
2009-12-15 23:33:31 +00:00
|
|
|
c = Server(c, l.config)
|
|
|
|
return
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewListener creates a Listener which accepts connections from an inner
|
|
|
|
// Listener and wraps each connection with Server.
|
2010-04-27 06:19:04 +01:00
|
|
|
// The configuration config must be non-nil and must have
|
|
|
|
// at least one certificate.
|
2012-02-03 20:08:53 +00:00
|
|
|
func NewListener(inner net.Listener, config *Config) net.Listener {
|
|
|
|
l := new(listener)
|
|
|
|
l.Listener = inner
|
2009-12-15 23:33:31 +00:00
|
|
|
l.config = config
|
2012-02-03 20:08:53 +00:00
|
|
|
return l
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
2010-04-05 22:38:02 +01:00
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// Listen creates a TLS listener accepting connections on the
|
|
|
|
// given network address using net.Listen.
|
|
|
|
// The configuration config must be non-nil and must have
|
|
|
|
// at least one certificate.
|
2012-02-03 20:08:53 +00:00
|
|
|
func Listen(network, laddr string, config *Config) (net.Listener, error) {
|
2010-04-27 06:19:04 +01:00
|
|
|
if config == nil || len(config.Certificates) == 0 {
|
2011-11-02 02:04:37 +00:00
|
|
|
return nil, errors.New("tls.Listen: no certificates in configuration")
|
2010-04-27 06:19:04 +01:00
|
|
|
}
|
2010-04-05 22:38:02 +01:00
|
|
|
l, err := net.Listen(network, laddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2010-04-27 06:19:04 +01:00
|
|
|
return NewListener(l, config), nil
|
2010-04-05 22:38:02 +01:00
|
|
|
}
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
// Dial connects to the given network address using net.Dial
|
|
|
|
// and then initiates a TLS handshake, returning the resulting
|
|
|
|
// TLS connection.
|
|
|
|
// Dial interprets a nil configuration as equivalent to
|
|
|
|
// the zero configuration; see the documentation of Config
|
|
|
|
// for the defaults.
|
2011-11-02 02:04:37 +00:00
|
|
|
func Dial(network, addr string, config *Config) (*Conn, error) {
|
2011-03-29 04:28:42 +01:00
|
|
|
raddr := addr
|
|
|
|
c, err := net.Dial(network, raddr)
|
2010-04-05 22:38:02 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2010-11-05 13:54:56 +00:00
|
|
|
|
|
|
|
colonPos := strings.LastIndex(raddr, ":")
|
|
|
|
if colonPos == -1 {
|
|
|
|
colonPos = len(raddr)
|
|
|
|
}
|
|
|
|
hostname := raddr[:colonPos]
|
|
|
|
|
2010-12-07 21:15:15 +00:00
|
|
|
if config == nil {
|
|
|
|
config = defaultConfig()
|
|
|
|
}
|
2012-03-07 18:12:35 +00:00
|
|
|
// If no ServerName is set, infer the ServerName
|
|
|
|
// from the hostname we're connecting to.
|
|
|
|
if config.ServerName == "" {
|
2010-12-07 21:15:15 +00:00
|
|
|
// Make a copy to avoid polluting argument or default.
|
|
|
|
c := *config
|
|
|
|
c.ServerName = hostname
|
|
|
|
config = &c
|
|
|
|
}
|
2010-11-05 13:54:56 +00:00
|
|
|
conn := Client(c, config)
|
2010-12-07 21:15:15 +00:00
|
|
|
if err = conn.Handshake(); err != nil {
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
2010-09-20 15:32:08 +01:00
|
|
|
}
|
2010-12-07 21:15:15 +00:00
|
|
|
return conn, nil
|
2010-04-05 22:38:02 +01:00
|
|
|
}
|
2010-07-02 18:00:18 +01:00
|
|
|
|
2010-10-11 15:39:56 +01:00
|
|
|
// LoadX509KeyPair reads and parses a public/private key pair from a pair of
|
|
|
|
// files. The files must contain PEM encoded data.
|
2012-01-05 17:05:38 +00:00
|
|
|
func LoadX509KeyPair(certFile, keyFile string) (cert Certificate, err error) {
|
2010-07-02 18:00:18 +01:00
|
|
|
certPEMBlock, err := ioutil.ReadFile(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2011-04-04 16:32:59 +01:00
|
|
|
keyPEMBlock, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return X509KeyPair(certPEMBlock, keyPEMBlock)
|
|
|
|
}
|
2010-07-02 18:00:18 +01:00
|
|
|
|
2011-04-04 16:32:59 +01:00
|
|
|
// X509KeyPair parses a public/private key pair from a pair of
|
|
|
|
// PEM encoded data.
|
2011-11-02 02:04:37 +00:00
|
|
|
func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (cert Certificate, err error) {
|
2011-02-05 18:54:25 +00:00
|
|
|
var certDERBlock *pem.Block
|
|
|
|
for {
|
|
|
|
certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
|
|
|
|
if certDERBlock == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if certDERBlock.Type == "CERTIFICATE" {
|
|
|
|
cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cert.Certificate) == 0 {
|
2011-11-02 02:04:37 +00:00
|
|
|
err = errors.New("crypto/tls: failed to parse certificate PEM data")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keyDERBlock, _ := pem.Decode(keyPEMBlock)
|
|
|
|
if keyDERBlock == nil {
|
2011-11-02 02:04:37 +00:00
|
|
|
err = errors.New("crypto/tls: failed to parse key PEM data")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-11-21 19:18:42 +00:00
|
|
|
// OpenSSL 0.9.8 generates PKCS#1 private keys by default, while
|
|
|
|
// OpenSSL 1.0.0 generates PKCS#8 keys. We try both.
|
|
|
|
var key *rsa.PrivateKey
|
|
|
|
if key, err = x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes); err != nil {
|
|
|
|
var privKey interface{}
|
|
|
|
if privKey, err = x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes); err != nil {
|
|
|
|
err = errors.New("crypto/tls: failed to parse key: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var ok bool
|
|
|
|
if key, ok = privKey.(*rsa.PrivateKey); !ok {
|
|
|
|
err = errors.New("crypto/tls: found non-RSA private key in PKCS#8 wrapping")
|
|
|
|
return
|
|
|
|
}
|
2010-07-02 18:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
cert.PrivateKey = key
|
|
|
|
|
|
|
|
// We don't need to parse the public key for TLS, but we so do anyway
|
|
|
|
// to check that it looks sane and matches the private key.
|
2011-02-05 18:54:25 +00:00
|
|
|
x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
|
2010-07-02 18:00:18 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
|
2011-11-02 02:04:37 +00:00
|
|
|
err = errors.New("crypto/tls: private key does not match public key")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|