-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfull_handshake_test.go
90 lines (69 loc) · 1.86 KB
/
full_handshake_test.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
package toyls
import (
"crypto/tls"
"crypto/x509"
"errors"
. "gopkg.in/check.v1"
)
type record struct {
contentType ContentType
body []byte
}
func pipeHandshakers(c, s handshaker) {
client, server := newPipe()
c.setRecordProtocol(client)
s.setRecordProtocol(server)
}
func newPipe() (recordProtocol, recordProtocol) {
left := make(chan record, 1)
right := make(chan record, 1)
return &dummyRecordProtocol{left, right}, &dummyRecordProtocol{right, left}
}
type dummyRecordProtocol struct {
read <-chan record
write chan<- record
}
func (r *dummyRecordProtocol) readRecord(c ContentType) ([]byte, error) {
record := <-r.read
if record.contentType != c {
return record.body, errors.New("wrong content type")
}
return record.body, nil
}
func (r *dummyRecordProtocol) writeRecord(c ContentType, b []byte) error {
r.write <- record{c, b}
return nil
}
func (r *dummyRecordProtocol) establishKeys([48]byte, [32]byte, [32]byte) {
//We dont care about security
}
func (r *dummyRecordProtocol) changeWriteCipherSpec() {
//We dont care about security
}
func (r *dummyRecordProtocol) changeReadCipherSpec() {
//We dont care about security
}
func (s *ToySuite) TestFullHandshakeNew(c *C) {
var err error
client := newClient()
server := newServer()
cert, err := tls.X509KeyPair([]byte(rsaCertPEM), []byte(rsaKeyPEM))
c.Assert(err, IsNil)
server.handshaker.(*handshakeServer).Certificate = cert
roots := x509.NewCertPool()
ok := roots.AppendCertsFromPEM([]byte(rsaCertPEM))
c.Assert(ok, Equals, true)
client.handshaker.(*handshakeClient).Config.RootCAs = roots
pipeHandshakers(client.handshaker, server.handshaker)
done := make(chan bool, 0)
go func() {
err := server.doHandshake()
c.Assert(err, IsNil)
done <- true
}()
err = client.doHandshake()
c.Assert(err, IsNil)
c.Assert(err, IsNil)
<-done
//You can start to exchange encrypted data
}