-
Notifications
You must be signed in to change notification settings - Fork 258
/
exchange.go
282 lines (244 loc) · 9.47 KB
/
exchange.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
* Copyright (c) 2019, Psiphon Inc.
* All rights reserved.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package psiphon
import (
"encoding/base64"
"encoding/json"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/errors"
"github.com/Psiphon-Labs/psiphon-tunnel-core/psiphon/common/protocol"
"golang.org/x/crypto/nacl/secretbox"
)
// ExportExchangePayload creates a payload for client-to-client server
// connection info exchange. The payload includes the most recent successful
// server entry -- the server entry in the affinity position -- and any
// associated dial parameters, for the current network ID.
//
// ExportExchangePayload is intended to be called when the client is
// connected, as the affinity server will be the currently connected server
// and there will be dial parameters for the current network ID.
//
// Only signed server entries will be exchanged. The signature is created by
// the Psiphon Network and may be verified using the
// ServerEntrySignaturePublicKey embedded in clients. This signture defends
// against attacks by rogue clients and man-in-the-middle operatives which
// could otherwise cause the importer to receive phony server entry values.
//
// Only a subset of dial parameters are exchanged. See the comment for
// ExchangedDialParameters for more details. When no dial parameters is
// present the exchange proceeds without dial parameters.
//
// The exchange payload is obfuscated with the ExchangeObfuscationKey embedded
// in clients. The purpose of this obfuscation is to ensure that plaintext
// server entry info cannot be trivially exported and displayed or published;
// or at least require an effort equal to what's required without the export
// feature.
//
// There is no success notice for exchange ExportExchangePayload (or
// ImportExchangePayload) as this would potentially leak a user releationship if
// two users performed and exchange and subseqently submit diagnostic feedback
// containg import and export logs at almost the same point in time, along
// with logs showing connections to the same server, with source "EXCHANGED"
// in the importer case.
//
// Failure notices are logged as, presumably, the event will only appear on
// one end of the exchange and the error is potentially important diagnostics.
//
// There remains some risk of user linkability from Connecting/ConnectedServer
// diagnostics and metrics alone, because the appearance of "EXCHANGED" may
// indicate an exchange event. But there are various degrees of ambiguity in
// this case in terms of determining the server entry was freshly exchanged;
// and with likely many users often connecting to any given server in a short
// time period.
//
// The return value is a payload that may be exchanged with another client;
// when "", the export failed and a diagnostic notice has been logged.
func ExportExchangePayload(config *Config) string {
payload, err := exportExchangePayload(config)
if err != nil {
NoticeWarning("ExportExchangePayload failed: %s", errors.Trace(err))
return ""
}
return payload
}
// ImportExchangePayload imports a payload generated by ExportExchangePayload.
// The server entry in the payload is promoted to the affinity position so it
// will be the first candidate in any establishment that begins after the
// import.
//
// The current network ID. This may not be the same network as the exporter,
// even if the client-to-client exchange occurs in real time. For example, if
// the exchange is performed over NFC between two devices, they may be on
// different mobile or WiFi networks. As mentioned in the comment for
// ExchangedDialParameters, the exchange dial parameters includes only the
// most broadly applicable fields.
//
// The return value indicates a successful import. If the import failed, a
// a diagnostic notice has been logged.
func ImportExchangePayload(config *Config, encodedPayload string) bool {
err := importExchangePayload(config, encodedPayload)
if err != nil {
NoticeWarning("ImportExchangePayload failed: %s", errors.Trace(err))
return false
}
return true
}
type exchangePayload struct {
ServerEntryFields protocol.ServerEntryFields
ExchangedDialParameters *ExchangedDialParameters
}
func exportExchangePayload(config *Config) (string, error) {
networkID := config.GetNetworkID()
key, err := getExchangeObfuscationKey(config)
if err != nil {
return "", errors.Trace(err)
}
serverEntryFields, dialParams, err :=
GetAffinityServerEntryAndDialParameters(networkID)
if err != nil {
return "", errors.Trace(err)
}
// Fail if the server entry has no signature, as the exchange would be
// insecure. Given the mechanism where handshake will return a signed server
// entry to clients without one, this case is not expected to occur.
if !serverEntryFields.HasSignature() {
return "", errors.TraceNew("export server entry not signed")
}
// RemoveUnsignedFields also removes potentially sensitive local fields, so
// explicitly strip these before exchanging.
serverEntryFields.RemoveUnsignedFields()
var exchangedDialParameters *ExchangedDialParameters
if dialParams != nil {
exchangedDialParameters = NewExchangedDialParameters(dialParams)
}
payload := &exchangePayload{
ServerEntryFields: serverEntryFields,
ExchangedDialParameters: exchangedDialParameters,
}
payloadJSON, err := json.Marshal(payload)
if err != nil {
return "", errors.Trace(err)
}
// A unique nonce is generated and included with the payload as the
// obfuscation keys is not single-use.
nonce, err := common.MakeSecureRandomBytes(24)
if err != nil {
return "", errors.Trace(err)
}
var secretboxNonce [24]byte
copy(secretboxNonce[:], nonce)
var secretboxKey [32]byte
copy(secretboxKey[:], key)
boxedPayload := secretbox.Seal(
nil, payloadJSON, &secretboxNonce, &secretboxKey)
boxedPayload = append(secretboxNonce[:], boxedPayload...)
return base64.StdEncoding.EncodeToString(boxedPayload), nil
}
func importExchangePayload(config *Config, encodedPayload string) error {
networkID := config.GetNetworkID()
key, err := getExchangeObfuscationKey(config)
if err != nil {
return errors.Trace(err)
}
boxedPayload, err := base64.StdEncoding.DecodeString(encodedPayload)
if err != nil {
return errors.Trace(err)
}
if len(boxedPayload) <= 24 {
return errors.TraceNew("unexpected box length")
}
var secretboxNonce [24]byte
copy(secretboxNonce[:], boxedPayload[:24])
var secretboxKey [32]byte
copy(secretboxKey[:], key)
payloadJSON, ok := secretbox.Open(
nil, boxedPayload[24:], &secretboxNonce, &secretboxKey)
if !ok {
return errors.TraceNew("unbox failed")
}
var payload *exchangePayload
err = json.Unmarshal(payloadJSON, &payload)
if err != nil {
return errors.Trace(err)
}
// Explicitly strip any unsigned fields that should not be exchanged or
// imported.
payload.ServerEntryFields.RemoveUnsignedFields()
err = payload.ServerEntryFields.VerifySignature(
config.ServerEntrySignaturePublicKey)
if err != nil {
return errors.Trace(err)
}
payload.ServerEntryFields.SetLocalSource(
protocol.SERVER_ENTRY_SOURCE_EXCHANGED)
payload.ServerEntryFields.SetLocalTimestamp(
common.TruncateTimestampToHour(common.GetCurrentTimestamp()))
// The following sequence of datastore calls -- StoreServerEntry,
// PromoteServerEntry, SetDialParameters -- is not an atomic transaction but
// the datastore will end up in a consistent state in case of failure to
// complete the sequence. The existing calls are reused to avoid redundant
// code.
//
// TODO: refactor existing code to allow reuse in a single transaction?
err = StoreServerEntry(payload.ServerEntryFields, true)
if err != nil {
return errors.Trace(err)
}
err = PromoteServerEntry(config, payload.ServerEntryFields.GetIPAddress())
if err != nil {
return errors.Trace(err)
}
if payload.ExchangedDialParameters != nil {
serverEntry, err := payload.ServerEntryFields.GetServerEntry()
if err != nil {
return errors.Trace(err)
}
// Don't abort if Validate fails, as the current client may simply not
// support the exchanged dial parameter values (for example, a new tunnel
// protocol).
//
// No notice is issued in the error case for the give linkage reason, as the
// notice would be a proxy for an import success log.
err = payload.ExchangedDialParameters.Validate(serverEntry)
if err == nil {
dialParams := payload.ExchangedDialParameters.MakeDialParameters(
config,
config.GetParameters().Get(),
serverEntry)
err = SetDialParameters(
payload.ServerEntryFields.GetIPAddress(),
networkID,
dialParams)
if err != nil {
return errors.Trace(err)
}
}
}
return nil
}
func getExchangeObfuscationKey(config *Config) ([]byte, error) {
key, err := base64.StdEncoding.DecodeString(config.ExchangeObfuscationKey)
if err != nil {
return nil, errors.Trace(err)
}
if len(key) != 32 {
return nil, errors.TraceNew("invalid key size")
}
return key, nil
}