| 6 |
| package runner |
| 7 |
| 8 | import ( |
| 9 | "bytes" |
| 10 | "crypto" |
| 11 | "crypto/ecdsa" |
| 12 | "crypto/ed25519" |
| 13 | "crypto/rsa" |
| 14 | "crypto/x509" |
| 15 | "encoding/pem" |
| 16 | "errors" |
| 17 | "net" |
| 18 | "os" |
| 19 | "strings" |
| 20 | "time" |
| 21 | ) |
| 22 |
| 23 | // Server returns a new TLS server side connection |
| 24 | // using conn as the underlying transport. |
| 25 | // The configuration config must be non-nil and must have |
| 26 | // at least one certificate. |
| 27 | func Server(conn net.Conn, config *Config) *Conn { |
| 28 | c := &Conn{conn: conn, config: config} |
| 29 | c.init() |
| 30 | return c |
| 31 | } |
| 32 |
| 33 | // Client returns a new TLS client side connection |
| 34 | // using conn as the underlying transport. |
| 35 | // The config cannot be nil: users must set either ServerHostname or |
| 36 | // InsecureSkipVerify in the config. |
| 37 | func Client(conn net.Conn, config *Config) *Conn { |
| 38 | c := &Conn{conn: conn, config: config, isClient: true} |
| 39 | c.init() |
| 40 | return c |
| 41 | } |
| 42 |
| 43 | // A listener implements a network listener (net.Listener) for TLS connections. |
| 44 | type listener struct { |
| 45 | net.Listener |
| 46 | config *Config |
| 47 | } |
| 48 |
| 49 | // Accept waits for and returns the next incoming TLS connection. |
| 50 | // The returned connection c is a *tls.Conn. |
| 51 | func (l *listener) Accept() (c net.Conn, err error) { |
| 52 | c, err = l.Listener.Accept() |
| 53 | if err != nil { |
| 54 | return |
| 55 | } |
| 56 | c = Server(c, l.config) |
| 57 | return |
| 58 | } |
| 59 |
| 60 | // NewListener creates a Listener which accepts connections from an inner |
| 61 | // Listener and wraps each connection with Server. |
| 62 | // The configuration config must be non-nil and must have |
| 63 | // at least one certificate. |
| 64 | func NewListener(inner net.Listener, config *Config) net.Listener { |
| 65 | l := new(listener) |
| 66 | l.Listener = inner |
| 67 | l.config = config |
| 68 | return l |
| 69 | } |
| 70 |
| 71 | // Listen creates a TLS listener accepting connections on the |
| 72 | // given network address using net.Listen. |
| 73 | // The configuration config must be non-nil and must have |
| 74 | // at least one certificate. |
| 75 | func Listen(network, laddr string, config *Config) (net.Listener, error) { |
| 76 | if config == nil || config.Credential == nil { |
| 77 | return nil, errors.New("tls.Listen: no certificates in configuration") |
| 78 | } |
| 79 | l, err := net.Listen(network, laddr) |
| 80 | if err != nil { |
| 81 | return nil, err |
| 82 | } |
| 83 | return NewListener(l, config), nil |
| 84 | } |
| 85 |
| 86 | type timeoutError struct{} |
| 87 |
| 88 | func (timeoutError) Error() string { return "tls: DialWithDialer timed out" } |
| 89 | func (timeoutError) Timeout() bool { return true } |
| 90 | func (timeoutError) Temporary() bool { return true } |
| 91 |
| 92 | // DialWithDialer connects to the given network address using dialer.Dial and |
| 93 | // then initiates a TLS handshake, returning the resulting TLS connection. Any |
| 94 | // timeout or deadline given in the dialer apply to connection and TLS |
| 95 | // handshake as a whole. |
| 96 | // |
| 97 | // DialWithDialer interprets a nil configuration as equivalent to the zero |
| 98 | // configuration; see the documentation of Config for the defaults. |
| 99 | func DialWithDialer(dialer *net.Dialer, network, addr string, config *Config) (*Conn, error) { |
| 100 | // We want the Timeout and Deadline values from dialer to cover the |
| 101 | // whole process: TCP connection and TLS handshake. This means that we |
| 102 | // also need to start our own timers now. |
| 103 | timeout := dialer.Timeout |
| 104 |
| 105 | if !dialer.Deadline.IsZero() { |
| 106 | deadlineTimeout := dialer.Deadline.Sub(time.Now()) |
| 107 | if timeout == 0 || deadlineTimeout < timeout { |
| 108 | timeout = deadlineTimeout |
| 109 | } |
| 110 | } |
| 111 |
| 112 | var errChannel chan error |
| 113 |
| 114 | if timeout != 0 { |
| 115 | errChannel = make(chan error, 2) |
| 116 | time.AfterFunc(timeout, func() { |
| 117 | errChannel <- timeoutError{} |
| 118 | }) |
| 119 | } |
| 120 |
| 121 | rawConn, err := dialer.Dial(network, addr) |
| 122 | if err != nil { |
| 123 | return nil, err |
| 124 | } |
| 125 |
| 126 | colonPos := strings.LastIndex(addr, ":") |
| 127 | if colonPos == -1 { |
| 128 | colonPos = len(addr) |
| 129 | } |
| 130 | hostname := addr[:colonPos] |
| 131 |
| 132 | if config == nil { |
| 133 | config = defaultConfig() |
| 134 | } |
| 135 | // If no ServerName is set, infer the ServerName |
| 136 | // from the hostname we're connecting to. |
| 137 | if config.ServerName == "" { |
| 138 | // Make a copy to avoid polluting argument or default. |
| 139 | c := *config |
| 140 | c.ServerName = hostname |
| 141 | config = &c |
| 142 | } |
| 143 |
| 144 | conn := Client(rawConn, config) |
| 145 |
| 146 | if timeout == 0 { |
| 147 | err = conn.Handshake() |
| 148 | } else { |
| 149 | go func() { |
| 150 | errChannel <- conn.Handshake() |
| 151 | }() |
| 152 |
| 153 | err = <-errChannel |
| 154 | } |
| 155 |
| 156 | if err != nil { |
| 157 | rawConn.Close() |
| 158 | return nil, err |
| 159 | } |
| 160 |
| 161 | return conn, nil |
| 162 | } |
| 163 |
| 164 | // Dial connects to the given network address using net.Dial |
| 165 | // and then initiates a TLS handshake, returning the resulting |
| 166 | // TLS connection. |
| 167 | // Dial interprets a nil configuration as equivalent to |
| 168 | // the zero configuration; see the documentation of Config |
| 169 | // for the defaults. |
| 170 | func Dial(network, addr string, config *Config) (*Conn, error) { |
| 171 | return DialWithDialer(new(net.Dialer), network, addr, config) |
| 172 | } |
| 173 |
| 174 | // LoadX509KeyPair reads and parses a public/private key pair from a pair of |
| 175 | // files. The files must contain PEM encoded data. |
| 176 | func LoadX509KeyPair(certFile, keyFile string) (cert Credential, err error) { |
| 177 | certPEMBlock, err := os.ReadFile(certFile) |
| 178 | if err != nil { |
| 179 | return |
| 180 | } |
| 181 | keyPEMBlock, err := os.ReadFile(keyFile) |
| 182 | if err != nil { |
| 183 | return |
| 184 | } |
| 185 | return X509KeyPair(certPEMBlock, keyPEMBlock) |
| 186 | } |
| 187 |
| 188 | // X509KeyPair parses a public/private key pair from a pair of |
| 189 | // PEM encoded data. |
| 190 | func X509KeyPair(certPEMBlock, keyPEMBlock []byte) (cert Credential, err error) { |
| 191 | var certDERBlock *pem.Block |
| 192 | for { |
| 193 | certDERBlock, certPEMBlock = pem.Decode(certPEMBlock) |
| 194 | if certDERBlock == nil { |
| 195 | break |
| 196 | } |
| 197 | if certDERBlock.Type == "CERTIFICATE" { |
| 198 | cert.Certificate = append(cert.Certificate, certDERBlock.Bytes) |
| 199 | } |
| 200 | } |
| 201 |
| 202 | if len(cert.Certificate) == 0 { |
| 203 | err = errors.New("crypto/tls: failed to parse certificate PEM data") |
| 204 | return |
| 205 | } |
| 206 |
| 207 | var keyDERBlock *pem.Block |
| 208 | for { |
| 209 | keyDERBlock, keyPEMBlock = pem.Decode(keyPEMBlock) |
| 210 | if keyDERBlock == nil { |
| 211 | err = errors.New("crypto/tls: failed to parse key PEM data") |
| 212 | return |
| 213 | } |
| 214 | if keyDERBlock.Type == "PRIVATE KEY" || strings.HasSuffix(keyDERBlock.Type, " PRIVATE KEY") { |
| 215 | break |
| 216 | } |
| 217 | } |
| 218 |
| 219 | cert.PrivateKey, err = parsePrivateKey(keyDERBlock.Bytes) |
| 220 | if err != nil { |
| 221 | return |
| 222 | } |
| 223 |
| 224 | // We don't need to parse the public key for TLS, but we so do anyway |
| 225 | // to check that it looks sane and matches the private key. |
| 226 | x509Cert, err := x509.ParseCertificate(cert.Certificate[0]) |
| 227 | if err != nil { |
| 228 | return |
| 229 | } |
| 230 |
| 231 | switch pub := x509Cert.PublicKey.(type) { |
| 232 | case *rsa.PublicKey: |
| 233 | priv, ok := cert.PrivateKey.(*rsa.PrivateKey) |
| 234 | if !ok { |
| 235 | err = errors.New("crypto/tls: private key type does not match public key type") |
| 236 | return |
| 237 | } |
| 238 | if pub.N.Cmp(priv.N) != 0 { |
| 239 | err = errors.New("crypto/tls: private key does not match public key") |
| 240 | return |
| 241 | } |
| 242 | case *ecdsa.PublicKey: |
| 243 | priv, ok := cert.PrivateKey.(*ecdsa.PrivateKey) |
| 244 | if !ok { |
| 245 | err = errors.New("crypto/tls: private key type does not match public key type") |
| 246 | return |
| 247 |
| 248 | } |
| 249 | if pub.X.Cmp(priv.X) != 0 || pub.Y.Cmp(priv.Y) != 0 { |
| 250 | err = errors.New("crypto/tls: private key does not match public key") |
| 251 | return |
| 252 | } |
| 253 | case ed25519.PublicKey: |
| 254 | priv, ok := cert.PrivateKey.(ed25519.PrivateKey) |
| 255 | if !ok { |
| 256 | err = errors.New("crypto/tls: private key type does not match public key type") |
| 257 | return |
| 258 | } |
| 259 | if !bytes.Equal(priv[32:], pub) { |
| 260 | err = errors.New("crypto/tls: private key does not match public key") |
| 261 | return |
| 262 | } |
| 263 | default: |
| 264 | err = errors.New("crypto/tls: unknown public key algorithm") |
| 265 | return |
| 266 | } |
| 267 |
| 268 | return |
| 269 | } |
| 270 |
| 271 | // Attempt to parse the given private key DER block. OpenSSL 0.9.8 generates |
| 272 | // PKCS#1 private keys by default, while OpenSSL 1.0.0 generates PKCS#8 keys. |
| 273 | // OpenSSL ecparam generates SEC1 EC private keys for ECDSA. We try all three. |
| 274 | func parsePrivateKey(der []byte) (crypto.PrivateKey, error) { |
| 275 | if key, err := x509.ParsePKCS1PrivateKey(der); err == nil { |
| 276 | return key, nil |
| 277 | } |
| 278 | if key, err := x509.ParsePKCS8PrivateKey(der); err == nil { |
| 279 | switch key := key.(type) { |
| 280 | case *rsa.PrivateKey, *ecdsa.PrivateKey, ed25519.PrivateKey: |
| 281 | return key, nil |
| 282 | default: |
| 283 | return nil, errors.New("crypto/tls: found unknown private key type in PKCS#8 wrapping") |
| 284 | } |
| 285 | } |
| 286 | if key, err := x509.ParseECPrivateKey(der); err == nil { |
| 287 | return key, nil |
| 288 | } |
| 289 |
| 290 | return nil, errors.New("crypto/tls: failed to parse private key") |
| 291 | } |
| 292 |