forked from abourget/goproxy
/
signer.go
165 lines (141 loc) · 3.95 KB
/
signer.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
164
165
package goproxy
import (
"bytes"
"crypto/rand"
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"crypto/x509/pkix"
"math/big"
"net"
"strings"
"sync"
"time"
)
// OrganizationName is the name your CA cert will be signed with. It
// will show in your different UIs. Change it globally here to show
// meaningful things to your users.
var OrganizationName = "GoProxy untrusted MITM proxy Inc"
// MaxSerialNumber is the upper boundary that is used to create unique serial
// numbers for the certificate. This can be any unsigned integer up to 20
// bytes (2^(8*20)-1).
var MaxSerialNumber = big.NewInt(0).SetBytes(bytes.Repeat([]byte{255}, 20))
func getWildcardHost(host string) string {
first := strings.Index(host, ".")
if first <= 0 {
return host
}
last := strings.LastIndex(host, ".")
if last == first {
// root domain, no wildcard
return host
}
return "*" + host[first:]
}
// Config is a set of configuration values that are used to build TLS configs
// capable of MITM.
type GoproxyConfig struct {
Root *x509.Certificate
capriv interface{}
priv *rsa.PrivateKey
keyID []byte
validity time.Duration
certmu sync.RWMutex
*tls.Config
}
// NewConfig creates a MITM config using the CA certificate and
// private key to generate on-the-fly certificates.
func NewConfig(ca *x509.Certificate, privateKey interface{}) (*GoproxyConfig, error) {
priv, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
return nil, err
}
pub := priv.Public()
// Subject Key Identifier support for end entity certificate.
// https://www.ietf.org/rfc/rfc3280.txt (section 4.2.1.2)
pkixpub, err := x509.MarshalPKIXPublicKey(pub)
if err != nil {
return nil, err
}
h := sha1.New()
h.Write(pkixpub)
keyID := h.Sum(nil)
tlsConfig := &GoproxyConfig{
Root: ca,
capriv: privateKey,
priv: priv,
keyID: keyID,
validity: time.Hour * 24 * 3650,
Config: rootCAs(nil),
}
if tlsConfig.Config.RootCAs == nil {
tlsConfig.Config.RootCAs = x509.NewCertPool()
}
tlsConfig.Config.RootCAs.AddCert(ca)
tlsConfig.Config.Certificates = make([]tls.Certificate, 0)
tlsConfig.Config.NameToCertificate = make(map[string]*tls.Certificate)
return tlsConfig, nil
}
func (c *GoproxyConfig) cert(hostname string) error {
// Remove the port if it exists.
host, _, err := net.SplitHostPort(getWildcardHost(hostname))
if err == nil {
hostname = host
}
// Remove the port if it exists.
c.certmu.RLock()
tlsc, ok := c.NameToCertificate[hostname]
c.certmu.RUnlock()
if ok {
// Check validity of the certificate for hostname match, expiry, etc. In
// particular, if the cached certificate has expired, create a new one.
if _, err := tlsc.Leaf.Verify(x509.VerifyOptions{
DNSName: hostname,
Roots: c.RootCAs,
}); err == nil {
return nil
}
}
serial, err := rand.Int(rand.Reader, MaxSerialNumber)
if err != nil {
return err
}
tmpl := &x509.Certificate{
SerialNumber: serial,
Subject: pkix.Name{
CommonName: hostname,
Organization: []string{OrganizationName},
},
SubjectKeyId: c.keyID,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
NotBefore: time.Now().Add(-c.validity),
NotAfter: time.Now().Add(c.validity),
}
if ip := net.ParseIP(hostname); ip != nil {
tmpl.IPAddresses = []net.IP{ip}
} else {
tmpl.DNSNames = []string{hostname}
}
raw, err := x509.CreateCertificate(rand.Reader, tmpl, c.Root, c.priv.Public(), c.capriv)
if err != nil {
return err
}
// Parse certificate bytes so that we have a leaf certificate.
x509c, err := x509.ParseCertificate(raw)
if err != nil {
return err
}
tlsc = &tls.Certificate{
Certificate: [][]byte{raw, c.Root.Raw},
PrivateKey: c.priv,
Leaf: x509c,
}
c.certmu.Lock()
c.NameToCertificate[hostname] = tlsc
c.Certificates = append(c.Certificates, *tlsc)
c.certmu.Unlock()
return nil
}