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
|
/*
* Copyright (c) 2014, Yawning Angel <yawning at torproject dot org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package obfs4
import (
"bytes"
"testing"
"github.com/yawning/obfs4/ntor"
)
func TestHandshakeNtor(t *testing.T) {
// Generate the server node id and id keypair.
nodeID, _ := ntor.NewNodeID([]byte("\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13"))
idKeypair, _ := ntor.NewKeypair(false)
serverFilter, _ := newReplayFilter()
// Test client handshake padding.
for l := clientMinPadLength; l <= clientMaxPadLength; l++ {
// Generate the client state and override the pad length.
clientKeypair, err := ntor.NewKeypair(true)
if err != nil {
t.Fatalf("[%d:0] ntor.NewKeypair failed: %s", l, err)
}
clientHs, err := newClientHandshake(nodeID, idKeypair.Public(), clientKeypair)
if err != nil {
t.Fatalf("[%d:0] newClientHandshake failed: %s", l, err)
}
clientHs.padLen = l
// Generate what the client will send to the server.
clientBlob, err := clientHs.generateHandshake()
if err != nil {
t.Fatalf("[%d:0] clientHandshake.generateHandshake() failed: %s", l, err)
}
if len(clientBlob) > maxHandshakeLength {
t.Fatalf("[%d:0] Generated client body is oversized: %d", l, len(clientBlob))
}
if len(clientBlob) < clientMinHandshakeLength {
t.Fatalf("[%d:0] Generated client body is undersized: %d", l, len(clientBlob))
}
if len(clientBlob) != clientMinHandshakeLength+l {
t.Fatalf("[%d:0] Generated client body incorrect size: %d", l, len(clientBlob))
}
// Generate the server state and override the pad length.
serverHs := newServerHandshake(nodeID, idKeypair)
serverHs.padLen = serverMinPadLength
// Parse the client handshake message.
serverSeed, err := serverHs.parseClientHandshake(serverFilter, clientBlob)
if err != nil {
t.Fatalf("[%d:0] serverHandshake.parseClientHandshake() failed: %s", l, err)
}
// Genrate what the server will send to the client.
serverBlob, err := serverHs.generateHandshake()
if err != nil {
t.Fatal("[%d:0]: serverHandshake.generateHandshake() failed: %s", l, err)
}
// Parse the server handshake message.
clientHs.serverRepresentative = nil
n, clientSeed, err := clientHs.parseServerHandshake(serverBlob)
if err != nil {
t.Fatalf("[%d:0] clientHandshake.parseServerHandshake() failed: %s", l, err)
}
if n != len(serverBlob) {
t.Fatalf("[%d:0] clientHandshake.parseServerHandshake() has bytes remaining: %d", l, n)
}
// Ensure the derived shared secret is the same.
if 0 != bytes.Compare(clientSeed, serverSeed) {
t.Fatalf("[%d:0] client/server seed mismatch", l)
}
}
// Test server handshake padding.
for l := serverMinPadLength; l <= serverMaxPadLength+inlineSeedFrameLength; l++ {
// Generate the client state and override the pad length.
clientKeypair, err := ntor.NewKeypair(true)
if err != nil {
t.Fatalf("[%d:0] ntor.NewKeypair failed: %s", l, err)
}
clientHs, err := newClientHandshake(nodeID, idKeypair.Public(), clientKeypair)
if err != nil {
t.Fatalf("[%d:0] newClientHandshake failed: %s", l, err)
}
clientHs.padLen = clientMinPadLength
// Generate what the client will send to the server.
clientBlob, err := clientHs.generateHandshake()
if err != nil {
t.Fatalf("[%d:1] clientHandshake.generateHandshake() failed: %s", l, err)
}
if len(clientBlob) > maxHandshakeLength {
t.Fatalf("[%d:1] Generated client body is oversized: %d", l, len(clientBlob))
}
// Generate the server state and override the pad length.
serverHs := newServerHandshake(nodeID, idKeypair)
serverHs.padLen = l
// Parse the client handshake message.
serverSeed, err := serverHs.parseClientHandshake(serverFilter, clientBlob)
if err != nil {
t.Fatalf("[%d:1] serverHandshake.parseClientHandshake() failed: %s", l, err)
}
// Genrate what the server will send to the client.
serverBlob, err := serverHs.generateHandshake()
if err != nil {
t.Fatal("[%d:1]: serverHandshake.generateHandshake() failed: %s", l, err)
}
// Parse the server handshake message.
n, clientSeed, err := clientHs.parseServerHandshake(serverBlob)
if err != nil {
t.Fatalf("[%d:1] clientHandshake.parseServerHandshake() failed: %s", l, err)
}
if n != len(serverBlob) {
t.Fatalf("[%d:1] clientHandshake.parseServerHandshake() has bytes remaining: %d", l, n)
}
// Ensure the derived shared secret is the same.
if 0 != bytes.Compare(clientSeed, serverSeed) {
t.Fatalf("[%d:1] client/server seed mismatch", l)
}
}
// Test oversized client padding.
clientKeypair, err := ntor.NewKeypair(true)
if err != nil {
t.Fatalf("ntor.NewKeypair failed: %s", err)
}
clientHs, err := newClientHandshake(nodeID, idKeypair.Public(), clientKeypair)
if err != nil {
t.Fatalf("newClientHandshake failed: %s", err)
}
clientHs.padLen = clientMaxPadLength + 1
clientBlob, err := clientHs.generateHandshake()
if err != nil {
t.Fatalf("clientHandshake.generateHandshake() (forced oversize) failed: %s", err)
}
serverHs := newServerHandshake(nodeID, idKeypair)
_, err = serverHs.parseClientHandshake(serverFilter, clientBlob)
if err == nil {
t.Fatalf("serverHandshake.parseClientHandshake() succeded (oversized)")
}
// Test undersized client padding.
clientHs.padLen = clientMinPadLength - 1
clientBlob, err = clientHs.generateHandshake()
if err != nil {
t.Fatalf("clientHandshake.generateHandshake() (forced undersize) failed: %s", err)
}
serverHs = newServerHandshake(nodeID, idKeypair)
_, err = serverHs.parseClientHandshake(serverFilter, clientBlob)
if err == nil {
t.Fatalf("serverHandshake.parseClientHandshake() succeded (undersized)")
}
// Test oversized server padding.
//
// NB: serverMaxPadLength isn't the real maxPadLength that triggers client
// rejection, because the implementation is written with the asusmption
// that/ the PRNG_SEED is also inlined with the response. Thus the client
// actually accepts longer padding. The server handshake test and this
// test adjust around that.
clientHs.padLen = clientMinPadLength
clientBlob, err = clientHs.generateHandshake()
if err != nil {
t.Fatalf("clientHandshake.generateHandshake() failed: %s", err)
}
serverHs = newServerHandshake(nodeID, idKeypair)
serverHs.padLen = serverMaxPadLength + inlineSeedFrameLength + 1
_, err = serverHs.parseClientHandshake(serverFilter, clientBlob)
if err != nil {
t.Fatalf("serverHandshake.parseClientHandshake() failed: %s", err)
}
serverBlob, err := serverHs.generateHandshake()
if err != nil {
t.Fatal("serverHandshake.generateHandshake() (forced oversize) failed: %s", err)
}
_, _, err = clientHs.parseServerHandshake(serverBlob)
if err == nil {
t.Fatalf("clientHandshake.parseServerHandshake() succeded (oversized)")
}
}
|