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.
|
|
|
|
|
|
|
|
// This package partially implements the TLS 1.1 protocol, as specified in RFC 4346.
|
|
|
|
package tls
|
|
|
|
|
|
|
|
import (
|
2010-11-05 13:54:56 +00:00
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/pem"
|
2010-07-02 18:00:18 +01:00
|
|
|
"io/ioutil"
|
2009-12-15 23:33:31 +00:00
|
|
|
"net"
|
2010-07-02 18:00:18 +01:00
|
|
|
"os"
|
2010-11-05 13:54:56 +00:00
|
|
|
"strings"
|
2009-11-06 00:43:29 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-11-06 00:43:29 +00:00
|
|
|
type Listener struct {
|
2009-12-15 23:33:31 +00:00
|
|
|
listener net.Listener
|
|
|
|
config *Config
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
|
|
|
|
2009-12-28 19:40:01 +00:00
|
|
|
func (l *Listener) Accept() (c net.Conn, err os.Error) {
|
2009-12-15 23:33:31 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2009-12-28 19:40:01 +00:00
|
|
|
func (l *Listener) Close() os.Error { return l.listener.Close() }
|
2009-11-06 00:43:29 +00:00
|
|
|
|
2009-12-28 19:40:01 +00:00
|
|
|
func (l *Listener) Addr() net.Addr { return l.listener.Addr() }
|
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.
|
2009-12-28 19:40:01 +00:00
|
|
|
func NewListener(listener net.Listener, config *Config) (l *Listener) {
|
|
|
|
l = new(Listener)
|
2009-12-15 23:33:31 +00:00
|
|
|
l.listener = listener
|
|
|
|
l.config = config
|
|
|
|
return
|
2009-11-06 00:43:29 +00:00
|
|
|
}
|
2010-04-05 22:38:02 +01:00
|
|
|
|
2010-04-27 06:19:04 +01:00
|
|
|
func Listen(network, laddr string, config *Config) (net.Listener, os.Error) {
|
|
|
|
if config == nil || len(config.Certificates) == 0 {
|
|
|
|
return nil, os.NewError("tls.Listen: no certificates in configuration")
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
|
|
|
func Dial(network, laddr, raddr string) (net.Conn, os.Error) {
|
|
|
|
c, err := net.Dial(network, laddr, raddr)
|
|
|
|
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]
|
|
|
|
|
|
|
|
config := defaultConfig()
|
|
|
|
config.ServerName = hostname
|
|
|
|
conn := Client(c, config)
|
2010-09-20 15:32:08 +01:00
|
|
|
err = conn.Handshake()
|
|
|
|
if err == nil {
|
|
|
|
return conn, nil
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
return nil, err
|
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.
|
2010-07-02 18:00:18 +01:00
|
|
|
func LoadX509KeyPair(certFile string, keyFile string) (cert Certificate, err os.Error) {
|
|
|
|
certPEMBlock, err := ioutil.ReadFile(certFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
certDERBlock, _ := pem.Decode(certPEMBlock)
|
|
|
|
if certDERBlock == nil {
|
2010-07-02 21:43:48 +01:00
|
|
|
err = os.ErrorString("crypto/tls: failed to parse certificate PEM data")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cert.Certificate = [][]byte{certDERBlock.Bytes}
|
|
|
|
|
|
|
|
keyPEMBlock, err := ioutil.ReadFile(keyFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
keyDERBlock, _ := pem.Decode(keyPEMBlock)
|
|
|
|
if keyDERBlock == nil {
|
2010-07-02 21:43:48 +01:00
|
|
|
err = os.ErrorString("crypto/tls: failed to parse key PEM data")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
|
|
|
|
if err != nil {
|
2010-07-02 21:43:48 +01:00
|
|
|
err = os.ErrorString("crypto/tls: failed to parse key")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
|
|
|
x509Cert, err := x509.ParseCertificate(certDERBlock.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
|
2010-07-02 21:43:48 +01:00
|
|
|
err = os.ErrorString("crypto/tls: private key does not match public key")
|
2010-07-02 18:00:18 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|