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 // Package tls partially implements the TLS 1.1 protocol, as specified in RFC
19 // Server returns a new TLS server side connection
20 // using conn as the underlying transport.
21 // The configuration config must be non-nil and must have
22 // at least one certificate.
23 func Server(conn net.Conn, config *Config) *Conn {
24 return &Conn{conn: conn, config: config}
27 // Client returns a new TLS client side connection
28 // using conn as the underlying transport.
29 // Client interprets a nil configuration as equivalent to
30 // the zero configuration; see the documentation of Config
32 func Client(conn net.Conn, config *Config) *Conn {
33 return &Conn{conn: conn, config: config, isClient: true}
36 // A listener implements a network listener (net.Listener) for TLS connections.
37 type listener struct {
42 // Accept waits for and returns the next incoming TLS connection.
43 // The returned connection c is a *tls.Conn.
44 func (l *listener) Accept() (c net.Conn, err error) {
45 c, err = l.Listener.Accept()
49 c = Server(c, l.config)
53 // NewListener creates a Listener which accepts connections from an inner
54 // Listener and wraps each connection with Server.
55 // The configuration config must be non-nil and must have
56 // at least one certificate.
57 func NewListener(inner net.Listener, config *Config) net.Listener {
64 // Listen creates a TLS listener accepting connections on the
65 // given network address using net.Listen.
66 // The configuration config must be non-nil and must have
67 // at least one certificate.
68 func Listen(network, laddr string, config *Config) (net.Listener, error) {
69 if config == nil || len(config.Certificates) == 0 {
70 return nil, errors.New("tls.Listen: no certificates in configuration")
72 l, err := net.Listen(network, laddr)
76 return NewListener(l, config), nil
79 // Dial connects to the given network address using net.Dial
80 // and then initiates a TLS handshake, returning the resulting
82 // Dial interprets a nil configuration as equivalent to
83 // the zero configuration; see the documentation of Config
85 func Dial(network, addr string, config *Config) (*Conn, error) {
87 c, err := net.Dial(network, raddr)
92 colonPos := strings.LastIndex(raddr, ":")
96 hostname := raddr[:colonPos]
99 config = defaultConfig()
101 if config.ServerName != "" {
102 // Make a copy to avoid polluting argument or default.
104 c.ServerName = hostname
107 conn := Client(c, config)
108 if err = conn.Handshake(); err != nil {
115 // LoadX509KeyPair reads and parses a public/private key pair from a pair of
116 // files. The files must contain PEM encoded data.
117 func LoadX509KeyPair(certFile, keyFile string) (cert Certificate, err error) {
118 certPEMBlock, err := ioutil.ReadFile(certFile)
122 keyPEMBlock, err := ioutil.ReadFile(keyFile)
126 return X509KeyPair(certPEMBlock, keyPEMBlock)
129 // X509KeyPair parses a public/private key pair from a pair of
131 func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (cert Certificate, err error) {
132 var certDERBlock *pem.Block
134 certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
135 if certDERBlock == nil {
138 if certDERBlock.Type == "CERTIFICATE" {
139 cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
143 if len(cert.Certificate) == 0 {
144 err = errors.New("crypto/tls: failed to parse certificate PEM data")
148 keyDERBlock, _ := pem.Decode(keyPEMBlock)
149 if keyDERBlock == nil {
150 err = errors.New("crypto/tls: failed to parse key PEM data")
154 // OpenSSL 0.9.8 generates PKCS#1 private keys by default, while
155 // OpenSSL 1.0.0 generates PKCS#8 keys. We try both.
156 var key *rsa.PrivateKey
157 if key, err = x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes); err != nil {
158 var privKey interface{}
159 if privKey, err = x509.ParsePKCS8PrivateKey(keyDERBlock.Bytes); err != nil {
160 err = errors.New("crypto/tls: failed to parse key: " + err.Error())
165 if key, ok = privKey.(*rsa.PrivateKey); !ok {
166 err = errors.New("crypto/tls: found non-RSA private key in PKCS#8 wrapping")
171 cert.PrivateKey = key
173 // We don't need to parse the public key for TLS, but we so do anyway
174 // to check that it looks sane and matches the private key.
175 x509Cert, err := x509.ParseCertificate(cert.Certificate[0])
180 if x509Cert.PublicKeyAlgorithm != x509.RSA || x509Cert.PublicKey.(*rsa.PublicKey).N.Cmp(key.PublicKey.N) != 0 {
181 err = errors.New("crypto/tls: private key does not match public key")