OSDN Git Service

e8290d728dd80153835ef5f8b178f5917436a00e
[pf3gnuchains/gcc-fork.git] / libgo / go / crypto / tls / tls.go
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.
4
5 // This package partially implements the TLS 1.1 protocol, as specified in RFC 4346.
6 package tls
7
8 import (
9         "crypto/rsa"
10         "crypto/x509"
11         "encoding/pem"
12         "io/ioutil"
13         "net"
14         "os"
15         "strings"
16 )
17
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}
24 }
25
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
30 // for the defaults.
31 func Client(conn net.Conn, config *Config) *Conn {
32         return &Conn{conn: conn, config: config, isClient: true}
33 }
34
35 // A Listener implements a network listener (net.Listener) for TLS connections.
36 type Listener struct {
37         listener net.Listener
38         config   *Config
39 }
40
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()
45         if err != nil {
46                 return
47         }
48         c = Server(c, l.config)
49         return
50 }
51
52 // Close closes the listener.
53 func (l *Listener) Close() os.Error { return l.listener.Close() }
54
55 // Addr returns the listener's network address.
56 func (l *Listener) Addr() net.Addr { return l.listener.Addr() }
57
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) {
63         l = new(Listener)
64         l.listener = listener
65         l.config = config
66         return
67 }
68
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")
76         }
77         l, err := net.Listen(network, laddr)
78         if err != nil {
79                 return nil, err
80         }
81         return NewListener(l, config), nil
82 }
83
84 // Dial connects to the given network address using net.Dial
85 // and then initiates a TLS handshake, returning the resulting
86 // TLS connection.
87 // Dial interprets a nil configuration as equivalent to
88 // the zero configuration; see the documentation of Config
89 // for the defaults.
90 func Dial(network, laddr, raddr string, config *Config) (*Conn, os.Error) {
91         c, err := net.Dial(network, laddr, raddr)
92         if err != nil {
93                 return nil, err
94         }
95
96         colonPos := strings.LastIndex(raddr, ":")
97         if colonPos == -1 {
98                 colonPos = len(raddr)
99         }
100         hostname := raddr[:colonPos]
101
102         if config == nil {
103                 config = defaultConfig()
104         }
105         if config.ServerName != "" {
106                 // Make a copy to avoid polluting argument or default.
107                 c := *config
108                 c.ServerName = hostname
109                 config = &c
110         }
111         conn := Client(c, config)
112         if err = conn.Handshake(); err != nil {
113                 c.Close()
114                 return nil, err
115         }
116         return conn, nil
117 }
118
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)
123         if err != nil {
124                 return
125         }
126
127         var certDERBlock *pem.Block
128         for {
129                 certDERBlock, certPEMBlock = pem.Decode(certPEMBlock)
130                 if certDERBlock == nil {
131                         break
132                 }
133                 if certDERBlock.Type == "CERTIFICATE" {
134                         cert.Certificate = append(cert.Certificate, certDERBlock.Bytes)
135                 }
136         }
137
138         if len(cert.Certificate) == 0 {
139                 err = os.ErrorString("crypto/tls: failed to parse certificate PEM data")
140                 return
141         }
142
143         keyPEMBlock, err := ioutil.ReadFile(keyFile)
144         if err != nil {
145                 return
146         }
147
148         keyDERBlock, _ := pem.Decode(keyPEMBlock)
149         if keyDERBlock == nil {
150                 err = os.ErrorString("crypto/tls: failed to parse key PEM data")
151                 return
152         }
153
154         key, err := x509.ParsePKCS1PrivateKey(keyDERBlock.Bytes)
155         if err != nil {
156                 err = os.ErrorString("crypto/tls: failed to parse key")
157                 return
158         }
159
160         cert.PrivateKey = key
161
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])
165         if err != nil {
166                 return
167         }
168
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")
171                 return
172         }
173
174         return
175 }