-
Notifications
You must be signed in to change notification settings - Fork 16
/
record-layer.go
118 lines (101 loc) · 2.59 KB
/
record-layer.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
package minq
import (
"github.com/bifurcation/mint"
"io"
"sync"
)
type RecordLayerImpl struct {
sync.Mutex
conn *Connection
epoch mint.Epoch
dir mint.Direction
buffer []byte
}
func (r *RecordLayerImpl) SetVersion(v uint16) {
// Do nothing
}
func (r *RecordLayerImpl) SetLabel(s string) {
// Do nothing
}
func (r *RecordLayerImpl) Rekey(epoch mint.Epoch, factory mint.AeadFactory, keys *mint.KeySet) error {
logf(logTypeTls, "Rekey epoch=%v", epoch)
// TODO([email protected]): Check to see if it's GCM.
aead, err := newWrappedAESGCM(keys.Key, keys.Iv)
if err != nil {
return mint.AlertInternalError
}
st := cryptoState{
aead: aead,
pne: newPneCipherFactoryAES(keys.Pn),
}
if r.dir == mint.DirectionRead {
r.conn.encryptionLevels[epoch].recvCipher = &st
} else {
r.conn.encryptionLevels[epoch].sendCipher = &st
}
r.epoch = epoch
return nil
}
func (r *RecordLayerImpl) ResetClear(seq uint64) {
panic("UNIMPLEMENTED")
}
func (r *RecordLayerImpl) DiscardReadKey(epoch mint.Epoch) {
// Do nothing
}
func (r *RecordLayerImpl) readBytes() ([]byte, error) {
str := &(r.conn.encryptionLevels[r.epoch].recvCryptoStream.(*recvStream).recvStreamBase)
b := make([]byte, 16384)
n, err := str.read(b)
logf(logTypeStream, "EKR: n=%d err=%v\n", n, err)
if err == ErrorWouldBlock {
return nil, mint.AlertWouldBlock
}
if err != nil {
return nil, mint.AlertInternalError
}
return b[:n], nil
}
func (r *RecordLayerImpl) PeekRecordType(block bool) (mint.RecordType, error) {
assert(r.buffer == nil)
var err error
r.buffer, err = r.readBytes()
if err != nil {
return 0, err
}
return mint.RecordTypeHandshake, nil
}
func (r *RecordLayerImpl) ReadRecord() (*mint.TLSPlaintext, error) {
var b []byte
var err error
if r.buffer != nil {
b = r.buffer
r.buffer = nil
} else {
b, err = r.readBytes()
if err != nil {
return nil, err
}
}
return mint.NewTLSPlaintext(mint.RecordTypeHandshake, r.epoch, b), nil
}
func (r *RecordLayerImpl) WriteRecord(pt *mint.TLSPlaintext) error {
logf(logTypeTls, "WriteRecord(epoch=%v, len=%v)", r.epoch, len(pt.Fragment()))
_, err := r.conn.encryptionLevels[r.epoch].sendCryptoStream.(*sendStream).write(pt.Fragment(), nil)
return err
}
func (r *RecordLayerImpl) Epoch() mint.Epoch {
return r.epoch
}
type RecordLayerFactoryImpl struct {
conn *Connection
}
func newRecordLayerFactory(conn *Connection) mint.RecordLayerFactory {
return &RecordLayerFactoryImpl{conn: conn}
}
func (f *RecordLayerFactoryImpl) NewLayer(conn io.ReadWriter, dir mint.Direction) mint.RecordLayer {
return &RecordLayerImpl{
dir: dir,
conn: f.conn,
buffer: nil,
}
}