-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtls.go
163 lines (149 loc) · 3.95 KB
/
tls.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package main
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"log"
"math/big"
"net/http"
"os"
"os/exec"
"path"
"strings"
"time"
)
// see readme for generating a local CA
func (a *App) loadCACert(caKeyFile, caCertFile string) (*tls.Certificate, error) {
if caCertFile == "" {
caCertFile = strings.TrimSuffix(caKeyFile, ".key") + ".crt"
}
ca, err := tls.LoadX509KeyPair(caCertFile, caKeyFile)
if err != nil {
return nil, err
}
return &ca, nil
}
func (a *App) loadTLSConfig(hostNames []string, caCert *tls.Certificate) (*tls.Config, error) {
notBefore := time.Now()
notAfter := notBefore.Add(time.Hour * 24 * 30)
serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
if err != nil {
log.Fatalf("failed to generate serial number: %s", err)
}
// root
leafKey, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
leafTemplate := &x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"lightproxy"},
CommonName: "Proxy Cert",
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
DNSNames: hostNames,
}
rootTemplate := leafTemplate
var rootKey interface{} = leafKey
if caCert != nil {
rootTemplate, err = x509.ParseCertificate(caCert.Certificate[0])
if err != nil {
return nil, err
}
rootKey = caCert.PrivateKey
}
derBytes, err := x509.CreateCertificate(
rand.Reader, leafTemplate, rootTemplate, &leafKey.PublicKey, rootKey)
if err != nil {
log.Fatalf("Failed to create certificate: %s", err)
}
certBuf := &bytes.Buffer{}
err = pem.Encode(certBuf, &pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
if err != nil {
return nil, err
}
keyBuf := &bytes.Buffer{}
pem.Encode(keyBuf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(leafKey)})
cert, err := tls.X509KeyPair(certBuf.Bytes(), keyBuf.Bytes())
if err != nil {
return nil, err
}
tlsConfig := &tls.Config{
MinVersion: tls.VersionTLS12,
NextProtos: []string{
"http/1.1",
"h2", // http/2
},
Certificates: []tls.Certificate{cert},
}
return tlsConfig, nil
}
func (a *App) startTLSProxy() error {
hostnames := []string{}
for _, e := range a.handlers {
hostnames = append(hostnames, e.e.Source)
}
var ca *tls.Certificate
var err error
if a.config.UseMkcert {
cmd := exec.Command("mkcert", "-CAROOT")
mkcertOut, err := cmd.Output()
if err != nil {
return err
}
mkcertRoot := strings.TrimSpace(string(mkcertOut))
if mkcertRoot == "" {
return fmt.Errorf("No directory found for mkcert; have you run mkcert -install?")
}
caKeyFile := path.Join(mkcertRoot, "rootCA-key.pem")
caCertFile := path.Join(mkcertRoot, "rootCA.pem")
if _, err = os.Stat(caKeyFile); os.IsNotExist(err) {
return fmt.Errorf("mkcert key file not found; have you run mkcert -install?")
}
if _, err = os.Stat(caCertFile); os.IsNotExist(err) {
return fmt.Errorf("mkcert cert file not found; have you run mkcert -install?")
}
ca, err = a.loadCACert(caKeyFile, caCertFile)
if err != nil {
return err
}
fmt.Println("tls: using mkcert")
} else if a.config.CAKeyFile != "" {
ca, err = a.loadCACert(a.config.CAKeyFile, a.config.CACertFile)
if err != nil {
return err
}
}
tlsConfig, err := a.loadTLSConfig(hostnames, ca)
tlsAddr := a.config.TLSAddr
if err != nil {
return err
}
if tlsConfig == nil {
return nil
}
l, err := tls.Listen("tcp", tlsAddr, tlsConfig)
if err != nil {
return err
}
tlsServer := &http.Server{
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
Handler: a,
TLSConfig: tlsConfig,
}
fmt.Println("tls: listening on", tlsAddr)
go tlsServer.Serve(l)
return nil
}