/
common.go
374 lines (328 loc) · 8.93 KB
/
common.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
package tlcp
import (
"bytes"
"context"
"crypto"
"crypto/rand"
"errors"
"fmt"
"github.com/aacfactory/afssl/gmsm/smx509"
"io"
"net"
"sync"
"time"
)
const (
VersionTLCP = 0x0101
)
const (
maxPlaintext = 16384
maxCiphertext = 16384 + 2048
recordHeaderLen = 5
maxHandshake = 65536
maxUselessRecords = 16
)
type recordType uint8
const (
recordTypeChangeCipherSpec recordType = 20
recordTypeAlert recordType = 21
recordTypeHandshake recordType = 22
recordTypeApplicationData recordType = 23
)
const (
typeClientHello uint8 = 1
typeServerHello uint8 = 2
typeCertificate uint8 = 11
typeServerKeyExchange uint8 = 12
typeCertificateRequest uint8 = 13
typeServerHelloDone uint8 = 14
typeCertificateVerify uint8 = 15
typeClientKeyExchange uint8 = 16
typeFinished uint8 = 20
)
func HandshakeMessageTypeName(id uint8) string {
switch id {
case typeClientHello:
return "Client Hello"
case typeServerHello:
return "Server Hello"
case typeCertificate:
return "Certificate"
case typeServerKeyExchange:
return "Server Key Exchange"
case typeCertificateRequest:
return "Certificate Request"
case typeServerHelloDone:
return "Server Hello Done"
case typeCertificateVerify:
return "Certificate Verify"
case typeClientKeyExchange:
return "Client Key Exchange"
case typeFinished:
return "Finished"
}
return fmt.Sprintf("0x%02X", id)
}
const (
compressionNone uint8 = 0
)
type CurveID uint16
const (
CurveSM2 CurveID = 41
)
const (
certTypeRSASign = 1
certTypeECDSASign = 64 // ECDSA or EdDSA keys, see RFC 8422, Section 3.
certTypeIbcParams = 80
)
type ConnectionState struct {
Version uint16
HandshakeComplete bool
DidResume bool
CipherSuite uint16
ServerName string
PeerCertificates []*smx509.Certificate
VerifiedChains [][]*smx509.Certificate
}
type ClientAuthType int
const (
NoClientCert ClientAuthType = iota
RequestClientCert
RequireAnyClientCert
VerifyClientCertIfGiven
RequireAndVerifyClientCert
RequireAndVerifyAnyKeyUsageClientCert
)
func requiresClientCert(c ClientAuthType) bool {
switch c {
case RequireAnyClientCert, RequireAndVerifyClientCert, RequireAndVerifyAnyKeyUsageClientCert:
return true
default:
return false
}
}
type ClientHelloInfo struct {
CipherSuites []uint16
ServerName string
SupportedVersions []uint16
Conn net.Conn
config *Config
ctx context.Context
}
func (c *ClientHelloInfo) Context() context.Context {
return c.ctx
}
type CertificateRequestInfo struct {
AcceptableCAs [][]byte
Version uint16
ctx context.Context
}
func (cri *CertificateRequestInfo) Context() context.Context {
return cri.ctx
}
func (cri *CertificateRequestInfo) SupportsCertificate(c *Certificate) error {
if len(cri.AcceptableCAs) == 0 {
return nil
}
for j, cert := range c.Certificate {
x509Cert := c.Leaf
if j != 0 || x509Cert == nil {
var err error
if x509Cert, err = smx509.ParseCertificate(cert); err != nil {
return fmt.Errorf("failed to parse certificate #%d in the chain: %w", j, err)
}
}
for _, ca := range cri.AcceptableCAs {
if bytes.Equal(x509Cert.RawIssuer, ca) {
return nil
}
}
}
return errors.New("chain is not signed by an acceptable CA")
}
var supportedVersions = []uint16{
VersionTLCP,
}
const (
roleClient = true
roleServer = false
)
var errNoCertificates = errors.New("tlcp: no certificates configured")
type Config struct {
Rand io.Reader
Time func() time.Time
Certificates []Certificate
GetCertificate func(*ClientHelloInfo) (*Certificate, error)
GetKECertificate func(*ClientHelloInfo) (*Certificate, error)
GetClientCertificate func(*CertificateRequestInfo) (*Certificate, error)
GetClientKECertificate func(*CertificateRequestInfo) (*Certificate, error)
GetConfigForClient func(*ClientHelloInfo) (*Config, error)
VerifyPeerCertificate func(rawCerts [][]byte, verifiedChains [][]*smx509.Certificate) error
VerifyConnection func(ConnectionState) error
RootCAs *smx509.CertPool
ServerName string
ClientAuth ClientAuthType
ClientCAs *smx509.CertPool
InsecureSkipVerify bool
CipherSuites []uint16
SessionCache SessionCache
MinVersion uint16
MaxVersion uint16
DynamicRecordSizingDisabled bool
OnAlert func(code uint8, conn *Conn)
mutex sync.RWMutex
ClientECDHEParamsAsVector bool
}
func (c *Config) Clone() *Config {
if c == nil {
return nil
}
c.mutex.RLock()
defer c.mutex.RUnlock()
return &Config{
Rand: c.Rand,
Time: c.Time,
Certificates: c.Certificates,
GetCertificate: c.GetCertificate,
GetKECertificate: c.GetKECertificate,
GetClientCertificate: c.GetClientCertificate,
GetClientKECertificate: c.GetClientKECertificate,
GetConfigForClient: c.GetConfigForClient,
VerifyPeerCertificate: c.VerifyPeerCertificate,
VerifyConnection: c.VerifyConnection,
RootCAs: c.RootCAs,
ServerName: c.ServerName,
ClientECDHEParamsAsVector: c.ClientECDHEParamsAsVector,
ClientAuth: c.ClientAuth,
ClientCAs: c.ClientCAs,
InsecureSkipVerify: c.InsecureSkipVerify,
CipherSuites: c.CipherSuites,
SessionCache: c.SessionCache,
MinVersion: c.MinVersion,
MaxVersion: c.MaxVersion,
DynamicRecordSizingDisabled: c.DynamicRecordSizingDisabled,
OnAlert: c.OnAlert,
}
}
func (c *Config) rand() io.Reader {
r := c.Rand
if r == nil {
return rand.Reader
}
return r
}
func (c *Config) time() time.Time {
t := c.Time
if t == nil {
t = time.Now
}
return t()
}
func (c *Config) cipherSuites() []uint16 {
if c.CipherSuites != nil {
return c.CipherSuites
}
return defaultCipherSuites
}
func (c *Config) supportedVersions(isClient bool) []uint16 {
versions := make([]uint16, 0, len(supportedVersions))
for _, v := range supportedVersions {
if c != nil && c.MinVersion != 0 && v < c.MinVersion {
continue
}
if c != nil && c.MaxVersion != 0 && v > c.MaxVersion {
continue
}
versions = append(versions, v)
}
return versions
}
func (c *Config) maxSupportedVersion(isClient bool) uint16 {
supportedVersions := c.supportedVersions(isClient)
if len(supportedVersions) == 0 {
return 0
}
return supportedVersions[0]
}
func (c *Config) mutualVersion(isClient bool, peerVersions []uint16) (uint16, bool) {
supportedVersions := c.supportedVersions(isClient)
for _, peerVersion := range peerVersions {
for _, v := range supportedVersions {
if v == peerVersion {
return v, true
}
}
}
return 0, false
}
func (c *Config) getCertificate(clientHello *ClientHelloInfo) (*Certificate, error) {
if c.GetCertificate != nil &&
(len(c.Certificates) == 0 || len(clientHello.ServerName) > 0) {
cert, err := c.GetCertificate(clientHello)
if cert != nil || err != nil {
return cert, err
}
}
if len(c.Certificates) == 0 {
return nil, errNoCertificates
}
return &c.Certificates[0], nil
}
func (c *Config) getEKCertificate(clientHello *ClientHelloInfo) (*Certificate, error) {
if c.GetKECertificate != nil && (len(c.Certificates) < 2) {
cert, err := c.GetKECertificate(clientHello)
if cert != nil || err != nil {
return cert, err
}
}
if len(c.Certificates) < 2 {
return nil, errNoCertificates
}
return &c.Certificates[1], nil
}
func supportedVersionsFromMax(maxVersion uint16) []uint16 {
versions := make([]uint16, 0, len(supportedVersions))
if (maxVersion & 0xFF00) == 0x0300 {
return versions
}
for _, v := range supportedVersions {
if v > maxVersion {
continue
}
versions = append(versions, v)
}
return versions
}
type Certificate struct {
Certificate [][]byte
PrivateKey crypto.PrivateKey
Leaf *smx509.Certificate
}
func (c *Certificate) leaf() (*smx509.Certificate, error) {
if c.Leaf != nil {
return c.Leaf, nil
}
return smx509.ParseCertificate(c.Certificate[0])
}
type handshakeMessage interface {
marshal() ([]byte, error)
unmarshal([]byte) bool
messageType() uint8
}
var emptyConfig Config
func defaultConfig() *Config {
return &emptyConfig
}
func unexpectedMessageError(wanted, got interface{}) error {
return fmt.Errorf("tlcp: received unexpected handshake message of type %T when waiting for %T", got, wanted)
}
type CertificateVerificationError struct {
UnverifiedCertificates []*smx509.Certificate
Err error
}
func (e *CertificateVerificationError) Error() string {
return fmt.Sprintf("tlcp: failed to verify certificate: %s", e.Err)
}
func (e *CertificateVerificationError) Unwrap() error {
return e.Err
}