forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_utils.go
73 lines (63 loc) · 2.1 KB
/
tcp_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package ravendb
import (
"crypto/tls"
"crypto/x509"
"encoding/base64"
"fmt"
"net"
"net/url"
)
func newTLSConfig(certificate *tls.Certificate, trustStore *x509.Certificate) (*tls.Config, error) {
if certificate != nil && trustStore == nil {
return nil, newIllegalArgumentError("certificates and trustStoreASN1 can't be both empty")
}
config := &tls.Config{}
if trustStore != nil {
roots := x509.NewCertPool()
roots.AddCert(trustStore)
config.RootCAs = roots
}
// TODO: not sure if this should always (ever?) be set
// see setSSLHostnameVerifier and loadTrustMaterial in java code
config.InsecureSkipVerify = true
config.Certificates = []tls.Certificate{*certificate}
return config, nil
}
func tcpConnect(uri string, serverCertificateBase64 []byte, clientCertificate *tls.Certificate) (net.Conn, error) {
// uri is in the format: tcp://127.0.0.1:14206
parsed, err := url.Parse(uri)
if err != nil {
return nil, err
}
if parsed.Scheme != "tcp" {
return nil, fmt.Errorf("bad url: '%s', expected scheme to be 'ftp', is '%s'", uri, parsed.Scheme)
}
if len(serverCertificateBase64) > 0 || clientCertificate != nil {
// serverCertificateBase64 is base64-encoded ASN1-encoded certificate
// This is a root signing certificate needed for tls.Dial to recognize
// data Send by the server (?) as properly signed.
// If we didn't have this we could set tls.Config.InsecureSkipVerify to true
var trustStore *x509.Certificate
if len(serverCertificateBase64) > 0 {
serverCertificate, err := base64.StdEncoding.DecodeString(string(serverCertificateBase64))
if err != nil {
return nil, err
}
trustStore, err = x509.ParseCertificate(serverCertificate)
if err != nil {
return nil, err
}
}
config, err := newTLSConfig(clientCertificate, trustStore)
if err != nil {
return nil, err
}
// forcing TLS 1.2 as Java code seems to be doing
config.MinVersion = tls.VersionTLS12
config.MaxVersion = tls.VersionTLS12
conn, err := tls.Dial("tcp", parsed.Host, config)
return conn, err
}
// parsed.Host is in the form "127.0.0.1:14206"
return net.Dial("tcp", parsed.Host)
}