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.
5 // This package partially implements the TLS 1.1 protocol, as specified in RFC 4346.
18 // Server returns a new TLS server side connection
19 // using conn as the underlying transport.
20 // The configuration config must be non-nil and must have
21 // at least one certificate.
22 func Server(conn net.Conn, config *Config) *Conn {
23 return &Conn{conn: conn, config: config}
26 // Client returns a new TLS client side connection
27 // using conn as the underlying transport.
28 // Client interprets a nil configuration as equivalent to
29 // the zero configuration; see the documentation of Config
31 func Client(conn net.Conn, config *Config) *Conn {
32 return &Conn{conn: conn, config: config, isClient: true}
35 // A Listener implements a network listener (net.Listener) for TLS connections.
36 type Listener struct {
41 // Accept waits for and returns the next incoming TLS connection.
42 // The returned connection c is a *tls.Conn.
43 func (l *Listener) Accept() (c net.Conn, err os.Error) {
44 c, err = l.listener.Accept()
48 c = Server(c, l.config)
52 // Close closes the listener.
53 func (l *Listener) Close() os.Error { return l.listener.Close() }
55 // Addr returns the listener's network address.
56 func (l *Listener) Addr() net.Addr { return l.listener.Addr() }
58 // NewListener creates a Listener which accepts connections from an inner
59 // Listener and wraps each connection with Server.
60 // The configuration config must be non-nil and must have
61 // at least one certificate.
62 func NewListener(listener net.Listener, config *Config) (l *Listener) {
69 // Listen creates a TLS listener accepting connections on the
70 // given network address using net.Listen.
71 // The configuration config must be non-nil and must have
72 // at least one certificate.
73 func Listen(network, laddr string, config *Config) (*Listener, os.Error) {
74 if config == nil || len(config.Certificates) == 0 {
75 return nil, os.NewError("tls.Listen: no certificates in configuration")
77 l, err := net.Listen(network, laddr)
81 return NewListener(l, config), nil
84 // Dial connects to the given network address using net.Dial
85 // and then initiates a TLS handshake, returning the resulting
87 // Dial interprets a nil configuration as equivalent to
88 // the zero configuration; see the documentation of Config
90 func Dial(network, laddr, raddr string, config *Config) (*Conn, os.Error) {
91 c, err := net.Dial(network, laddr, raddr)
96 colonPos := strings.LastIndex(raddr, ":")
100 hostname := raddr[:colonPos]
103 config = defaultConfig()
105 if config.ServerName != "" {
106 // Make a copy to avoid polluting argument or default.
108 c.ServerName = hostname
111 conn := Client(c, config)
112 if err = conn.Handshake(); err != nil {
119 // LoadX509KeyPair reads and parses a public/private key pair from a pair of
120 // files. The files must contain PEM encoded data.
121 func LoadX509KeyPair(certFile string, keyFile string) (cert Certificate, err os.Error) {
122 certPEMBlock, err := ioutil.ReadFile(certFile)
127 var certDERBlock *pem.Block
129 certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
130 if certDERBlock == nil {
133 if certDERBlock.Type == "CERTIFICATE" {
134 cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
138 if len(cert.Certificate) == 0 {
139 err = os.ErrorString("crypto/tls: failed to parse certificate PEM data")
143 keyPEMBlock, err := ioutil.ReadFile(keyFile)
148 keyDERBlock, _ := pem.Decode(keyPEMBlock)
149 if keyDERBlock == nil {
150 err = os.ErrorString("crypto/tls: failed to parse key PEM data")
154 key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
156 err = os.ErrorString("crypto/tls: failed to parse key")
160 cert.PrivateKey = key
162 // We don't need to parse the public key for TLS, but we so do anyway
163 // to check that it looks sane and matches the private key.
164 x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
169 if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
170 err = os.ErrorString("crypto/tls: private key does not match public key")