Debian Codegraph @

tls.go

raw symbols SCIP
1 package mint
2
3 // XXX(rlb): This file is borrowed pretty much wholesale from crypto/tls
4
5 import (
6 "errors"
7 "net"
8 "strings"
9 "time"
10 )
11
12 // Server returns a new TLS server side connection
13 // using conn as the underlying transport.
14 // The configuration config must be non-nil and must include
15 // at least one certificate or else set GetCertificate.
16 func Server(conn net.Conn, config *Config) *Conn {
17 return NewConn(conn, config, false)
18 }
19
20 // Client returns a new TLS client side connection
21 // using conn as the underlying transport.
22 // The config cannot be nil: users must set either ServerName or
23 // InsecureSkipVerify in the config.
24 func Client(conn net.Conn, config *Config) *Conn {
25 return NewConn(conn, config, true)
26 }
27
28 // A listener implements a network listener (net.Listener) for TLS connections.
29 type Listener struct {
30 net.Listener
31 config *Config
32 }
33
34 // Accept waits for and returns the next incoming TLS connection.
35 // The returned connection c is a *tls.Conn.
36 func (l *Listener) Accept() (c net.Conn, err error) {
37 c, err = l.Listener.Accept()
38 if err != nil {
39 return
40 }
41 server := Server(c, l.config)
42 err = server.Handshake()
43 if err == AlertNoAlert {
44 err = nil
45 }
46 c = server
47 return
48 }
49
50 // NewListener creates a Listener which accepts connections from an inner
51 // Listener and wraps each connection with Server.
52 // The configuration config must be non-nil and must include
53 // at least one certificate or else set GetCertificate.
54 func NewListener(inner net.Listener, config *Config) (net.Listener, error) {
55 if config != nil && config.NonBlocking {
56 return nil, errors.New("listening not possible in non-blocking mode")
57 }
58 l := new(Listener)
59 l.Listener = inner
60 l.config = config
61 return l, nil
62 }
63
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 include
67 // at least one certificate or else set GetCertificate.
68 func Listen(network, laddr string, config *Config) (net.Listener, error) {
69 if config == nil || !config.ValidForServer() {
70 return nil, errors.New("tls: neither Certificates nor GetCertificate set in Config")
71 }
72 l, err := net.Listen(network, laddr)
73 if err != nil {
74 return nil, err
75 }
76 return NewListener(l, config)
77 }
78
79 type TimeoutError struct{}
80
81 func (TimeoutError) Error() string { return "tls: DialWithDialer timed out" }
82 func (TimeoutError) Timeout() bool { return true }
83 func (TimeoutError) Temporary() bool { return true }
84
85 // DialWithDialer connects to the given network address using dialer.Dial and
86 // then initiates a TLS handshake, returning the resulting TLS connection. Any
87 // timeout or deadline given in the dialer apply to connection and TLS
88 // handshake as a whole.
89 //
90 // DialWithDialer interprets a nil configuration as equivalent to the zero
91 // configuration; see the documentation of Config for the defaults.
92 func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) {
93 if config != nil && config.NonBlocking {
94 return nil, errors.New("dialing not possible in non-blocking mode")
95 }
96
97 // We want the Timeout and Deadline values from dialer to cover the
98 // whole process: TCP connection and TLS handshake. This means that we
99 // also need to start our own timers now.
100 timeout := dialer.Timeout
101
102 if !dialer.Deadline.IsZero() {
103 deadlineTimeout := dialer.Deadline.Sub(time.Now())
104 if timeout == 0 || deadlineTimeout < timeout {
105 timeout = deadlineTimeout
106 }
107 }
108
109 var errChannel chan error
110
111 if timeout != 0 {
112 errChannel = make(chan error, 2)
113 time.AfterFunc(timeout, func() {
114 errChannel <- TimeoutError{}
115 })
116 }
117
118 rawConn, err := dialer.Dial(network, addr)
119 if err != nil {
120 return nil, err
121 }
122
123 colonPos := strings.LastIndex(addr, ":")
124 if colonPos == -1 {
125 colonPos = len(addr)
126 }
127 hostname := addr[:colonPos]
128
129 if config == nil {
130 config = &Config{}
131 } else {
132 config = config.Clone()
133 }
134
135 // If no ServerName is set, infer the ServerName
136 // from the hostname we're connecting to.
137 if config.ServerName == "" {
138 config.ServerName = hostname
139
140 }
141
142 // Set up DTLS as needed.
143 config.UseDTLS = (network == "udp")
144
145 conn := Client(rawConn, config)
146
147 if timeout == 0 {
148 err = conn.Handshake()
149 if err == AlertNoAlert {
150 err = nil
151 }
152 } else {
153 go func() {
154 errChannel <- conn.Handshake()
155 }()
156
157 err = <-errChannel
158 if err == AlertNoAlert {
159 err = nil
160 }
161 }
162
163 if err != nil {
164 rawConn.Close()
165 return nil, err
166 }
167
168 return conn, nil
169 }
170
171 // Dial connects to the given network address using net.Dial
172 // and then initiates a TLS handshake, returning the resulting
173 // TLS connection.
174 // Dial interprets a nil configuration as equivalent to
175 // the zero configuration; see the documentation of Config
176 // for the defaults.
177 func Dial(network, addr string, config *Config) (*Conn, error) {
178 return DialWithDialer(new(net.Dialer), network, addr, config)
179 }