This repository was archived by the owner on Jan 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcell.go
227 lines (213 loc) · 5.3 KB
/
cell.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
package torch
import (
"bytes"
"crypto/x509"
"encoding/binary"
"fmt"
"io"
"net"
"time"
)
type cell struct {
circid []byte
command byte
payload []byte
}
func packCell(c cell) (ret []byte) {
variableLength := c.command == 7 || c.command >= 128
if !variableLength {
if len(c.payload) > PAYLOAD_LEN {
panic("packCell: len(payload) > PAYLOAD_LEN")
}
ret = make([]byte, 0, len(c.circid)+1+PAYLOAD_LEN)
} else {
ret = make([]byte, 0, len(c.circid)+1+2+len(c.payload))
}
ret = append(ret, c.circid...)
ret = append(ret, c.command)
if variableLength {
ret = ret[:len(ret)+2]
binary.BigEndian.PutUint16(ret[len(ret)-2:], uint16(len(c.payload)))
}
ret = append(ret, c.payload...)
return ret[:cap(ret)]
}
func readCell(conn io.Reader, linkProtoVersion uint16) (c cell, err error) {
for {
c, err = readCellMaybePadding(conn, linkProtoVersion)
if err != nil || c.command != CELL_PADDING && c.command != CELL_VPADDING {
return
}
}
}
func readCellMaybePadding(conn io.Reader, linkProtoVersion uint16) (c cell, err error) {
if linkProtoVersion < 4 {
c.circid = make([]byte, 2)
} else {
c.circid = make([]byte, 4)
}
if _, err = io.ReadFull(conn, c.circid); err != nil {
return
}
if err = binary.Read(conn, binary.BigEndian, &c.command); err != nil {
return
}
payloadLength := uint16(PAYLOAD_LEN)
if c.command == 7 || c.command >= 128 {
if err = binary.Read(conn, binary.BigEndian, &payloadLength); err != nil {
return
}
}
c.payload = make([]byte, payloadLength)
if _, err = io.ReadFull(conn, c.payload); err != nil {
return
}
return
}
func (tc *TorConn) readVersionsCell() (canonicalVersion uint16, err error) {
c, err := readCell(tc.tlsConn, 2)
if err != nil {
return
}
nVersions := len(c.payload) / 2
for i := 0; i < nVersions; i++ {
version := binary.BigEndian.Uint16(c.payload[2*i:])
switch version {
case 4:
canonicalVersion = 4
}
}
return
}
func (tc *TorConn) readCertsCell() (
linkKeyCert, identityCert, authenticateCellCert *x509.Certificate,
err error,
) {
c, err := readCell(tc.tlsConn, tc.linkProtoVersion)
if err != nil {
return
}
payload := bytes.NewReader(c.payload)
var nCerts byte
if err = binary.Read(payload, binary.BigEndian, &nCerts); err != nil {
return
}
for i := byte(0); i < nCerts; i++ {
var certType byte
if err = binary.Read(payload, binary.BigEndian, &certType); err != nil {
return
}
var certLen int16
if err = binary.Read(payload, binary.BigEndian, &certLen); err != nil {
return
}
certCER := make([]byte, int(certLen))
if _, err = io.ReadFull(payload, certCER); err != nil {
return
}
if !(certType == 1 || certType == 2 || certType == 3) {
continue
}
cert, err := x509.ParseCertificate(certCER)
if err != nil {
fmt.Printf("%x\n%x\n%s\n", c.payload, certCER, err)
return nil, nil, nil, err
}
switch certType {
case 1: // Link key certificate certified by identity
if linkKeyCert != nil {
return nil, nil, nil, fmt.Errorf("multiple link key certificates")
}
linkKeyCert = cert
case 2: // Identity certificate
if identityCert != nil {
return nil, nil, nil, fmt.Errorf("multiple identity certificates")
}
identityCert = cert
case 3: // AUTHENTICATE cell link certificate
if authenticateCellCert != nil {
return nil, nil, nil, fmt.Errorf("multiple AUTHENTICATE cell certificates")
}
authenticateCellCert = cert
}
}
return
}
func (tc *TorConn) readAuthChallengeCell() (
challenge []byte,
supportsRSASignSha256TLSSercret bool,
err error,
) {
c, err := readCell(tc.tlsConn, tc.linkProtoVersion)
if err != nil {
return
}
payload := bytes.NewReader(c.payload)
challenge = make([]byte, 32)
if _, err = io.ReadFull(payload, challenge); err != nil {
return
}
var nMethods int16
if err = binary.Read(payload, binary.BigEndian, &nMethods); err != nil {
return
}
for i := int16(0); i < nMethods; i++ {
var method int16
if err = binary.Read(payload, binary.BigEndian, &method); err != nil {
return
}
switch method {
case 1:
supportsRSASignSha256TLSSercret = true
}
}
return
}
func netInfoCell(now time.Time, ourAddress net.Addr, theirAddress net.Addr, linkProtoVersion uint16) []byte {
var circid []byte
if linkProtoVersion < 4 {
circid = make([]byte, 2)
} else {
circid = make([]byte, 4)
}
buf := make([]byte, 4)
binary.BigEndian.PutUint32(buf, uint32(now.Unix()))
if ourAddress != nil {
panic("not implemented")
} else {
buf = append(buf, 0, 0)
}
if theirAddress != nil {
panic("not implemented")
} else {
buf = append(buf, 0, 0)
}
return packCell(cell{circid, CELL_NETINFO, buf})
}
// XXX not implemented, just ignores the cell
func (tc *TorConn) readNetInfoCell() error {
_, err := readCell(tc.tlsConn, tc.linkProtoVersion)
if err != nil {
return err
}
return nil
}
/*
func (tc *torConn) readCreated2Cell() ([]byte, error) {
circid, command, payload, err := readCell(tc.tlsConn, tc.linkProtoVersion)
if command != CELL_CREATED2 {
return nil, fmt.Errorf("got circid %v command %v (expected CREATED2=%d)", circid, command, CELL_CREATED2)
}
if err != nil {
return nil, err
}
defer ioutil.ReadAll(payload)
var length uint16
if err = binary.Read(payload, binary.BigEndian, &length); err != nil {
return nil, err
}
handshake := make([]byte, length)
_, err = io.ReadFull(payload, handshake)
return handshake, nil
}
*/