-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
163 lines (136 loc) · 3.8 KB
/
proxy.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
// Copyright (c) 2019 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package proxy can be used with the Gate service gateservice.net/listener
// when it doesn't allow public client connections. The proxy creates client
// certificates using the Ed25519 private key which owns the Gate instance.
package proxy
import (
"crypto"
"crypto/ed25519"
"crypto/tls"
"crypto/x509"
"io"
"math/big"
"net"
"time"
"github.com/tsavola/mu"
)
// Proxy manages client certificates automatically.
type Proxy struct {
key ed25519.PrivateKey
mu mu.Mutex
pending chan struct{}
certs []tls.Certificate // 0 to 1 certificates.
err error
}
// New supports ed25519.PrivateKey.
func New(key crypto.PrivateKey) *Proxy {
return &Proxy{
key: key.(ed25519.PrivateKey),
}
}
// Dial with default dialer. See DialWithDialer for details.
func (p *Proxy) Dial(network, addr string, config *tls.Config) (*tls.Conn, error) {
return p.DialWithDialer(new(net.Dialer), network, addr, config)
}
// DialWithDialer and transparent connection authentication. Either addr or
// config.ServerName should refer to the DNS name of a Gate listener endpoint.
// ServerName is set automatically if it's empty.
func (p *Proxy) DialWithDialer(dialer *net.Dialer, network, addr string, config *tls.Config) (*tls.Conn, error) {
certs, err := p.getCert()
if err != nil {
return nil, err
}
if config == nil {
config = new(tls.Config)
} else {
config = config.Clone()
}
if config.ServerName == "" {
config.ServerName, _, err = net.SplitHostPort(addr)
if err != nil {
return nil, err
}
}
if len(config.Certificates) == 0 {
config.Certificates = certs
} else {
config.Certificates = append(append([]tls.Certificate{}, certs...), config.Certificates...)
}
if config.MinVersion == 0 {
config.MinVersion = tls.VersionTLS13
}
return tls.DialWithDialer(dialer, network, addr, config)
}
func (p *Proxy) getCert() ([]tls.Certificate, error) {
now := time.Now()
p.mu.Lock()
defer p.mu.Unlock()
retry:
if p.err != nil {
return nil, p.err
}
if len(p.certs) == 0 {
p.certs, p.err = newCert(p.key)
} else if p.certs[0].Leaf.NotAfter.Before(now.Add(5 * time.Minute)) {
if p.pending == nil {
p.pending = make(chan struct{})
go p.replaceCert()
} else if p.certs[0].Leaf.NotAfter.Before(now.Add(4 * time.Minute)) {
wait := p.pending
p.mu.UnlockGuard(func() {
<-wait
now = time.Now()
})
goto retry
}
}
return p.certs, p.err
}
func (p *Proxy) replaceCert() {
var (
certs []tls.Certificate
err error
)
defer p.mu.Guard(func() {
close(p.pending)
p.pending = nil
p.certs = certs
p.err = err
})
certs, err = newCert(p.key)
}
func newCert(key ed25519.PrivateKey) ([]tls.Certificate, error) {
c, err := CreateClientCertificate(key, time.Now(), nil)
if err != nil {
return nil, err
}
return []tls.Certificate{c}, nil
}
// CreateClientCertificate valid for a short time. Supports
// ed25519.PrivateKey.
func CreateClientCertificate(key crypto.PrivateKey, now time.Time, rand io.Reader) (tls.Certificate, error) {
template := &x509.Certificate{
SerialNumber: big.NewInt(1),
NotBefore: now.Add(-time.Minute),
NotAfter: now.Add(time.Hour),
KeyUsage: x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth},
BasicConstraintsValid: true,
}
data, err := x509.CreateCertificate(rand, template, rootCert, key.(ed25519.PrivateKey).Public(), rootKey)
if err != nil {
return tls.Certificate{}, err
}
certx, err := x509.ParseCertificate(data)
if err != nil {
return tls.Certificate{}, err
}
cert := tls.Certificate{
Certificate: [][]byte{data},
PrivateKey: key,
Leaf: certx,
}
return cert, nil
}