-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.go
257 lines (209 loc) · 5.79 KB
/
record.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
package toyls
import "hash"
type recordProtocol interface {
readRecord(ContentType) ([]byte, error)
writeRecord(ContentType, []byte) error
establishKeys([48]byte, [32]byte, [32]byte)
changeWriteCipherSpec()
changeReadCipherSpec()
}
type connectionEnd uint8
const (
SERVER connectionEnd = 0
CLIENT = 1
)
type securityParameters struct {
//TODO extract from here
macKeyLength uint8
encKeyLength uint8
fixedIVLength uint8
masterSecret [48]byte
clientRandom [32]byte
serverRandom [32]byte
}
type prfAlgorithm interface{}
type cipherType interface{}
type nullStreamCipher struct{}
func (nullStreamCipher) XORKeyStream(dst, src []byte) {
return
}
type bulkCipherAlgorithm interface{}
type macAlgorithm interface {
Size() int
MAC(digestBuf, seq, header, data []byte) []byte
}
type nullMacAlgorithm struct{}
func (s nullMacAlgorithm) Size() int {
return 0
}
func (s nullMacAlgorithm) MAC(digestBuf, seq, header, data []byte) []byte {
return []byte{}
}
type hmacAlgorithm struct {
h hash.Hash
}
func (s hmacAlgorithm) Size() int {
return s.h.Size()
}
func (s hmacAlgorithm) MAC(digestBuf, seq, header, data []byte) []byte {
s.h.Reset()
s.h.Write(seq)
s.h.Write(header)
s.h.Write(data)
return s.h.Sum(digestBuf[:0])
}
type compressionMethod interface {
compress([]byte) ([]byte, uint16)
decompress([]byte) ([]byte, uint16)
}
type nullCompressionMethod struct{}
func (nullCompressionMethod) compress(data []byte) ([]byte, uint16) {
return data, uint16(len(data))
}
func (nullCompressionMethod) decompress(compressed []byte) ([]byte, uint16) {
return compressed, uint16(len(compressed))
}
type ContentType uint8
var (
CHANGE_CIPHER_SPEC ContentType = 0x14
ALERT ContentType = 0x15
HANDSHAKE ContentType = 0x16
APPLICATION_DATA ContentType = 0x17
// other ContentType = 255
)
type protocolVersion struct {
major uint8
minor uint8
}
var (
VersionSSL30 = protocolVersion{0x03, 0x00}
VersionTLS10 = protocolVersion{0x03, 0x01}
VersionTLS11 = protocolVersion{0x03, 0x02}
VersionTLS12 = protocolVersion{0x03, 0x03}
)
type keyingMaterial struct {
clientMAC,
serverMAC,
clientKey,
serverKey,
clientIV,
serverIV []byte
}
type TLSPlaintext struct {
contentType ContentType
version protocolVersion
length uint16 //is this len(fragment)?
fragment []byte //TLSPlaintext.length MUST NOT exceed 2^14.
}
type TLSCompressed struct {
contentType ContentType
version protocolVersion
length uint16 //is this len(fragment)?
fragment []byte //TLSCompressed.length MUST NOT exceed 2^14 + 1024.
}
type TLSCiphertext struct {
contentType ContentType
version protocolVersion
length uint16 //is this len(fragment)?
// select (SecurityParameters.cipher_type) {
// case stream: GenericStreamCipher;
// case block: GenericBlockCipher;
// case aead: GenericAEADCipher;
// } fragment;
fragment []byte //TLSCiphertext.length MUST NOT exceed 2^14 + 2048.
}
func (t TLSCiphertext) serialize() (ret []byte) {
ret = append(ret, byte(t.contentType))
ret = append(ret, t.version.major)
ret = append(ret, t.version.minor)
ret = append(ret, (byte)(t.length>>8))
ret = append(ret, (byte)(t.length))
ret = append(ret, t.fragment...)
return ret
}
func (t TLSCiphertext) header() (ret []byte) {
return append(ret[:0], byte(t.contentType), t.version.major, t.version.minor, (byte)(t.length>>8), (byte)(t.length))
}
type Ciphered interface {
Marshal() []byte
Content() []byte
Mac() []byte
}
type GenericStreamCipher struct {
content []byte //TLSCompressed.length
MAC []byte //SecurityParameters.mac_length
/*
MAC(MAC_write_key, seq_num + TLSCompressed.type +
TLSCompressed.version +
TLSCompressed.length +
TLSCompressed.fragment);
*/
}
func (c GenericStreamCipher) Marshal() []byte {
ret := []byte{}
ret = append(ret, c.content...)
ret = append(ret, c.MAC...)
return ret
}
func (c GenericStreamCipher) UnMarshal(fragment []byte, macSize int) Ciphered {
c.content = fragment[:len(fragment)-macSize]
c.MAC = fragment[len(fragment)-macSize:]
return c
}
func (c GenericStreamCipher) Content() []byte {
return c.content
}
func (c GenericStreamCipher) Mac() []byte {
return c.MAC
}
type GenericBlockCipher struct {
IV []byte //SecurityParameters.record_iv_length
content []byte //TLSCompressed.length
MAC []byte //SecurityParameters.mac_length
padding []byte //GenericBlockCipher.padding_length
padding_length uint8
}
func (c GenericBlockCipher) Marshal() []byte {
ret := []byte{}
ret = append(ret, c.IV...)
ret = append(ret, c.content...)
ret = append(ret, c.MAC...)
ret = append(ret, c.padding...)
ret = append(ret, c.padding_length)
return ret
}
func (c GenericBlockCipher) UnMarshal(fragment []byte, ivLength, macSize int) Ciphered {
c.IV = fragment[:ivLength]
c.padding_length = fragment[len(fragment)-1]
c.padding = fragment[len(fragment)-1-int(c.padding_length) : len(fragment)-1]
c.MAC = fragment[len(fragment)-1-int(c.padding_length)-macSize : len(fragment)-1-int(c.padding_length)]
c.content = fragment[ivLength : len(fragment)-1-int(c.padding_length)-macSize]
return c
}
func (c GenericBlockCipher) Content() []byte {
return c.content
}
func (c GenericBlockCipher) Mac() []byte {
return c.MAC
}
type GenericAEADCipher struct {
nonce_explicit []byte //SecurityParameters.record_iv_length
content []byte //TLSCompressed.length
}
func (c GenericAEADCipher) Marshal() []byte {
ret := []byte{}
ret = append(ret, c.nonce_explicit...)
ret = append(ret, c.content...)
return ret
}
func (c GenericAEADCipher) UnMarshal(fragment []byte, ivLength int) Ciphered {
c.nonce_explicit = fragment[:ivLength]
c.content = fragment[ivLength:]
return c
}
func (c GenericAEADCipher) Content() []byte {
return c.content
}
func (c GenericAEADCipher) Mac() []byte {
return []byte{}
}