-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
107 lines (90 loc) · 2.59 KB
/
get.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package internal
import (
"bytes"
"crypto/tls"
"fmt"
"net"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"time"
)
// GetCertificate returns a certificate from a given URL.
// Certificate can be sourced from a file (e.g. `file:///path/to/the/cert.pem`),
// a TCP/UDP connection (e.g. `tcp://1.2.3.4:443`, `https://google.com`)
// or downloaded (e.g. `https://letsencrypt.org/certs/isrgrootx1.pem`).
// If no scheme is provided, it defaults to TCP.
func GetCertificate(u *url.URL) (*Certificate, error) {
switch {
case u.Scheme == "file" || (u.Hostname() == "" && u.Path != ""):
return getCertFromFile(u.Path)
case u.Scheme == "http" || u.Scheme == "https":
if strings.TrimLeft(u.Path, "/") != "" {
return getCertFromHTTP(u)
}
if u.Port() == "" {
u.Host = fmt.Sprintf("%s:%s", u.Hostname(), "443")
}
fallthrough
case u.Scheme == "":
u.Scheme = "tcp"
fallthrough
case u.Scheme == "tcp" || u.Scheme == "udp":
if u.Hostname() == "" {
return nil, fmt.Errorf("hostname is not specified")
}
if u.Port() == "" {
return nil, fmt.Errorf("port is not specified")
}
return getCertFromTLS(u)
default:
return nil, fmt.Errorf("unsupported scheme %q", u.Scheme)
}
}
func getCertFromFile(path string) (*Certificate, error) {
content, err := os.ReadFile(path)
if err != nil {
return nil, err
}
return ParseCertificate(content, strings.TrimLeft(filepath.Ext(path), "."))
}
func getCertFromTLS(u *url.URL) (*Certificate, error) {
netConn, err := net.DialTimeout(u.Scheme, u.Host, 5*time.Second)
if err != nil {
return nil, err
}
defer netConn.Close()
cfg := &tls.Config{
ServerName: u.Hostname(),
InsecureSkipVerify: true,
}
tlsConn := tls.Client(netConn, cfg)
defer tlsConn.Close()
if err := tlsConn.Handshake(); err != nil {
return nil, err
}
certs := tlsConn.ConnectionState().PeerCertificates
cert := NewCertificate(certs[0])
cert.hostname = u.Hostname()
for _, c := range certs[1:] {
cert.AddCertificateToChain(NewCertificate(c))
}
return cert, nil
}
func getCertFromHTTP(u *url.URL) (*Certificate, error) {
client := &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}}
resp, err := client.Get(u.String())
if err != nil {
return nil, fmt.Errorf("failed to get certificate from %q: %v", u.String(), err)
}
if resp.StatusCode != 200 {
return nil, fmt.Errorf("failed to get certificate from %q: got status code %d", u.String(), resp.StatusCode)
}
buf := bytes.NewBuffer([]byte{})
buf.ReadFrom(resp.Body)
return ParseCertificate(buf.Bytes(), strings.TrimLeft(filepath.Ext(u.Path), "."))
}