-
Notifications
You must be signed in to change notification settings - Fork 58
/
casigner.go
147 lines (138 loc) · 3.26 KB
/
casigner.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
package httpproxy
import (
"crypto/rsa"
"crypto/sha1"
"crypto/tls"
"crypto/x509"
"math/big"
mrand "math/rand"
"net"
"sort"
"sync"
"time"
)
// CaSigner is a certificate signer by CA certificate. It supports caching.
type CaSigner struct {
// Ca specifies CA certificate. You must set before using.
Ca *tls.Certificate
mu sync.RWMutex
certMap map[string]*tls.Certificate
certList []string
certIndex int
certMax int
}
// NewCaSigner returns a new CaSigner without caching.
func NewCaSigner() *CaSigner {
return NewCaSignerCache(0)
}
// NewCaSignerCache returns a new CaSigner with caching given max.
func NewCaSignerCache(max int) *CaSigner {
if max < 0 {
max = 0
}
return &CaSigner{
certMap: make(map[string]*tls.Certificate),
certList: make([]string, max),
certIndex: 0,
certMax: max,
}
}
// SignHost generates TLS certificate given single host, signed by CA certificate.
func (c *CaSigner) SignHost(host string) (cert *tls.Certificate) {
if host == "" {
return
}
if c.certMax <= 0 {
crt, err := SignHosts(*c.Ca, []string{host})
if err != nil {
return nil
}
cert = crt
return
}
func() {
c.mu.RLock()
defer c.mu.RUnlock()
cert = c.certMap[host]
}()
if cert != nil {
return
}
c.mu.Lock()
defer c.mu.Unlock()
cert = c.certMap[host]
if cert != nil {
return
}
crt, err := SignHosts(*c.Ca, []string{host})
if err != nil {
return nil
}
cert = crt
if len(c.certMap) >= c.certMax {
delete(c.certMap, c.certList[c.certIndex])
}
c.certMap[host] = cert
c.certList[c.certIndex] = host
c.certIndex++
if c.certIndex >= c.certMax {
c.certIndex = 0
}
return
}
// SignHosts generates TLS certificate given hosts, signed by CA certificate.
func SignHosts(ca tls.Certificate, hosts []string) (*tls.Certificate, error) {
x509ca, err := x509.ParseCertificate(ca.Certificate[0])
if err != nil {
return nil, err
}
start := time.Unix(0, 0)
end, _ := time.Parse("2006-01-02", "2038-01-19")
serial := hashSortedBigInt(append(hosts, "1"))
template := x509.Certificate{
SerialNumber: serial,
Issuer: x509ca.Subject,
Subject: x509ca.Subject,
NotBefore: start,
NotAfter: end,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
BasicConstraintsValid: true,
}
for _, h := range hosts {
h = stripPort(h)
if ip := net.ParseIP(h); ip != nil {
template.IPAddresses = append(template.IPAddresses, ip)
} else {
template.DNSNames = append(template.DNSNames, h)
}
}
rnd := mrand.New(mrand.NewSource(serial.Int64()))
certPriv, err := rsa.GenerateKey(rnd, 1024)
if err != nil {
return nil, err
}
derBytes, err := x509.CreateCertificate(rnd, &template, x509ca, &certPriv.PublicKey, ca.PrivateKey)
if err != nil {
return nil, err
}
return &tls.Certificate{
Certificate: [][]byte{derBytes, ca.Certificate[0]},
PrivateKey: certPriv,
}, nil
}
func hashSorted(lst []string) []byte {
c := make([]string, len(lst))
copy(c, lst)
sort.Strings(c)
h := sha1.New()
for _, s := range c {
h.Write([]byte(s + ","))
}
return h.Sum(nil)
}
func hashSortedBigInt(lst []string) *big.Int {
rv := new(big.Int)
rv.SetBytes(hashSorted(lst))
return rv
}