/
authPackage.go
524 lines (436 loc) · 14.2 KB
/
authPackage.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
/*
* Copyright (c) 2016, 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 common
import (
"bufio"
"bytes"
"compress/zlib"
"crypto"
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/json"
"io"
"io/ioutil"
"sync"
"github.com/astaguna/popon-core/psiphon/common/errors"
)
// AuthenticatedDataPackage is a JSON record containing some Psiphon data
// payload, such as list of Psiphon server entries. As it may be downloaded
// from various sources, it is digitally signed so that the data may be
// authenticated.
type AuthenticatedDataPackage struct {
Data string `json:"data"`
SigningPublicKeyDigest []byte `json:"signingPublicKeyDigest"`
Signature []byte `json:"signature"`
}
// GenerateAuthenticatedDataPackageKeys generates a key pair
// be used to sign and verify AuthenticatedDataPackages.
func GenerateAuthenticatedDataPackageKeys() (string, string, error) {
rsaKey, err := rsa.GenerateKey(rand.Reader, 4096)
if err != nil {
return "", "", errors.Trace(err)
}
publicKeyBytes, err := x509.MarshalPKIXPublicKey(rsaKey.Public())
if err != nil {
return "", "", errors.Trace(err)
}
privateKeyBytes := x509.MarshalPKCS1PrivateKey(rsaKey)
return base64.StdEncoding.EncodeToString(publicKeyBytes),
base64.StdEncoding.EncodeToString(privateKeyBytes),
nil
}
func sha256sum(data string) []byte {
digest := sha256.Sum256([]byte(data))
return digest[:]
}
// WriteAuthenticatedDataPackage creates an AuthenticatedDataPackage
// containing the specified data and signed by the given key. The output
// conforms with the legacy format here:
// https://bitbucket.org/psiphon/psiphon-circumvention-system/src/c25d080f6827b141fe637050ce0d5bd0ae2e9db5/Automation/psi_ops_crypto_tools.py
func WriteAuthenticatedDataPackage(
data string, signingPublicKey, signingPrivateKey string) ([]byte, error) {
derEncodedPrivateKey, err := base64.StdEncoding.DecodeString(signingPrivateKey)
if err != nil {
return nil, errors.Trace(err)
}
rsaPrivateKey, err := x509.ParsePKCS1PrivateKey(derEncodedPrivateKey)
if err != nil {
return nil, errors.Trace(err)
}
signature, err := rsa.SignPKCS1v15(
rand.Reader,
rsaPrivateKey,
crypto.SHA256,
sha256sum(data))
if err != nil {
return nil, errors.Trace(err)
}
packageJSON, err := json.Marshal(
&AuthenticatedDataPackage{
Data: data,
SigningPublicKeyDigest: sha256sum(signingPublicKey),
Signature: signature,
})
if err != nil {
return nil, errors.Trace(err)
}
return Compress(packageJSON), nil
}
// ReadAuthenticatedDataPackage extracts and verifies authenticated
// data from an AuthenticatedDataPackage. The package must have been
// signed with the given key.
//
// Set isCompressed to false to read packages that are not compressed.
func ReadAuthenticatedDataPackage(
dataPackage []byte, isCompressed bool, signingPublicKey string) (string, error) {
var packageJSON []byte
var err error
if isCompressed {
packageJSON, err = Decompress(dataPackage)
if err != nil {
return "", errors.Trace(err)
}
} else {
packageJSON = dataPackage
}
var authenticatedDataPackage *AuthenticatedDataPackage
err = json.Unmarshal(packageJSON, &authenticatedDataPackage)
if err != nil {
return "", errors.Trace(err)
}
derEncodedPublicKey, err := base64.StdEncoding.DecodeString(signingPublicKey)
if err != nil {
return "", errors.Trace(err)
}
publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
if err != nil {
return "", errors.Trace(err)
}
rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
if !ok {
return "", errors.TraceNew("unexpected signing public key type")
}
if !bytes.Equal(
authenticatedDataPackage.SigningPublicKeyDigest,
sha256sum(signingPublicKey)) {
return "", errors.TraceNew("unexpected signing public key digest")
}
err = rsa.VerifyPKCS1v15(
rsaPublicKey,
crypto.SHA256,
sha256sum(authenticatedDataPackage.Data),
authenticatedDataPackage.Signature)
if err != nil {
return "", errors.Trace(err)
}
return authenticatedDataPackage.Data, nil
}
// NewAuthenticatedDataPackageReader extracts and verifies authenticated
// data from an AuthenticatedDataPackage stored in the specified file. The
// package must have been signed with the given key.
// NewAuthenticatedDataPackageReader does not load the entire package nor
// the entire data into memory. It streams the package while verifying, and
// returns an io.Reader that the caller may use to stream the authenticated
// data payload.
func NewAuthenticatedDataPackageReader(
dataPackage io.ReadSeeker, signingPublicKey string) (io.Reader, error) {
// The file is streamed in 2 passes. The first pass verifies the package
// signature. No payload data should be accepted/processed until the signature
// check is complete. The second pass repositions to the data payload and returns
// a reader the caller will use to stream the authenticated payload.
//
// Note: No exclusive file lock is held between passes, so it's possible to
// verify the data in one pass, and read different data in the second pass.
// For Psiphon's use cases, this will not happen in practise -- the packageFileName
// will not change while the returned io.Reader is used -- unless the client host
// is compromised; a compromised client host is outside of our threat model.
var payload io.Reader
for pass := 0; pass < 2; pass++ {
_, err := dataPackage.Seek(0, io.SeekStart)
if err != nil {
return nil, errors.Trace(err)
}
decompressor, err := zlib.NewReader(dataPackage)
if err != nil {
return nil, errors.Trace(err)
}
// TODO: need to Close decompressor to ensure zlib checksum is verified?
hash := sha256.New()
var jsonData io.Reader
var jsonSigningPublicKey []byte
var jsonSignature []byte
jsonReadBase64Value := func(value io.Reader) ([]byte, error) {
base64Value, err := ioutil.ReadAll(value)
if err != nil {
return nil, errors.Trace(err)
}
decodedValue, err := base64.StdEncoding.DecodeString(string(base64Value))
if err != nil {
return nil, errors.Trace(err)
}
return decodedValue, nil
}
jsonHandler := func(key string, value io.Reader) (bool, error) {
switch key {
case "data":
if pass == 0 {
_, err := io.Copy(hash, value)
if err != nil {
return false, errors.Trace(err)
}
return true, nil
} else { // pass == 1
jsonData = value
// The JSON stream parser must halt at this position,
// leaving the reader to be returned to the caller positioned
// at the start of the data payload.
return false, nil
}
case "signingPublicKeyDigest":
jsonSigningPublicKey, err = jsonReadBase64Value(value)
if err != nil {
return false, errors.Trace(err)
}
return true, nil
case "signature":
jsonSignature, err = jsonReadBase64Value(value)
if err != nil {
return false, errors.Trace(err)
}
return true, nil
}
return false, errors.Tracef("unexpected key '%s'", key)
}
// Using a buffered reader to consume zlib output in batches
// yields a significant speed up in the BenchmarkAuthenticatedPackage.
jsonStreamer := &limitedJSONStreamer{
reader: bufio.NewReader(decompressor),
handler: jsonHandler,
}
err = jsonStreamer.Stream()
if err != nil {
return nil, errors.Trace(err)
}
if pass == 0 {
if jsonSigningPublicKey == nil || jsonSignature == nil {
return nil, errors.TraceNew("missing expected field")
}
derEncodedPublicKey, err := base64.StdEncoding.DecodeString(signingPublicKey)
if err != nil {
return nil, errors.Trace(err)
}
publicKey, err := x509.ParsePKIXPublicKey(derEncodedPublicKey)
if err != nil {
return nil, errors.Trace(err)
}
rsaPublicKey, ok := publicKey.(*rsa.PublicKey)
if !ok {
return nil, errors.TraceNew("unexpected signing public key type")
}
if !bytes.Equal(jsonSigningPublicKey, sha256sum(signingPublicKey)) {
return nil, errors.TraceNew("unexpected signing public key digest")
}
err = rsa.VerifyPKCS1v15(
rsaPublicKey,
crypto.SHA256,
hash.Sum(nil),
jsonSignature)
if err != nil {
return nil, errors.Trace(err)
}
} else { // pass == 1
if jsonData == nil {
return nil, errors.TraceNew("missing expected field")
}
payload = jsonData
}
}
return payload, nil
}
// limitedJSONStreamer is a streaming JSON parser that supports just the
// JSON required for the AuthenticatedDataPackage format and expected data payloads.
//
// Unlike other common streaming JSON parsers, limitedJSONStreamer streams the JSON
// _values_, as the AuthenticatedDataPackage "data" value may be too large to fit into
// memory.
//
// limitedJSONStreamer is not intended for use outside of AuthenticatedDataPackage
// and supports only a small subset of JSON: one object with string values only,
// no escaped characters, no nested objects, no arrays, no numbers, etc.
//
// limitedJSONStreamer does support any JSON spec (http://www.json.org/) format
// for its limited subset. So, for example, any whitespace/formatting should be
// supported and the creator of AuthenticatedDataPackage should be able to use
// any valid JSON that results in a AuthenticatedDataPackage object.
//
// For each key/value pair, handler is invoked with the key name and a reader
// to stream the value. The handler _must_ read value to EOF (or return an error).
type limitedJSONStreamer struct {
reader io.Reader
handler func(key string, value io.Reader) (bool, error)
}
const (
stateJSONSeekingObjectStart = iota
stateJSONSeekingKeyStart
stateJSONSeekingKeyEnd
stateJSONSeekingColon
stateJSONSeekingStringValueStart
stateJSONSeekingStringValueEnd
stateJSONSeekingNextPair
stateJSONObjectEnd
)
func (streamer *limitedJSONStreamer) Stream() error {
// TODO: validate that strings are valid Unicode?
isWhitespace := func(b byte) bool {
return b == ' ' || b == '\t' || b == '\r' || b == '\n'
}
nextByte := make([]byte, 1)
keyBuffer := new(bytes.Buffer)
state := stateJSONSeekingObjectStart
for {
n, readErr := streamer.reader.Read(nextByte)
if n > 0 {
b := nextByte[0]
switch state {
case stateJSONSeekingObjectStart:
if b == '{' {
state = stateJSONSeekingKeyStart
} else if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U while seeking object start", b)
}
case stateJSONSeekingKeyStart:
if b == '"' {
state = stateJSONSeekingKeyEnd
keyBuffer.Reset()
} else if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U while seeking key start", b)
}
case stateJSONSeekingKeyEnd:
if b == '\\' {
return errors.TraceNew("unsupported escaped character")
} else if b == '"' {
state = stateJSONSeekingColon
} else {
keyBuffer.WriteByte(b)
}
case stateJSONSeekingColon:
if b == ':' {
state = stateJSONSeekingStringValueStart
} else if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U while seeking colon", b)
}
case stateJSONSeekingStringValueStart:
if b == '"' {
state = stateJSONSeekingStringValueEnd
key := keyBuffer.String()
// Wrap the main reader in a reader that will read up to the end
// of the value and then EOF. The handler is expected to consume
// the full value, and then stream parsing will resume after the
// end of the value.
valueStreamer := &limitedJSONValueStreamer{
reader: streamer.reader,
}
continueStreaming, err := streamer.handler(key, valueStreamer)
if err != nil {
return errors.Trace(err)
}
// The handler may request that streaming halt at this point; no
// further changes are made to streamer.reader, leaving the value
// exactly where the hander leaves it.
if !continueStreaming {
return nil
}
state = stateJSONSeekingNextPair
} else if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U while seeking value start", b)
}
case stateJSONSeekingNextPair:
if b == ',' {
state = stateJSONSeekingKeyStart
} else if b == '}' {
state = stateJSONObjectEnd
} else if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U while seeking next name/value pair", b)
}
case stateJSONObjectEnd:
if !isWhitespace(b) {
return errors.Tracef("unexpected character %#U after object end", b)
}
default:
return errors.TraceNew("unexpected state")
}
}
if readErr != nil {
if readErr == io.EOF {
if state != stateJSONObjectEnd {
return errors.TraceNew("unexpected EOF before object end")
}
return nil
}
return errors.Trace(readErr)
}
}
}
// limitedJSONValueStreamer wraps the limitedJSONStreamer reader
// with a reader that reads to the end of a string value and then
// terminates with EOF.
type limitedJSONValueStreamer struct {
mutex sync.Mutex
eof bool
reader io.Reader
}
// Read implements the io.Reader interface.
func (streamer *limitedJSONValueStreamer) Read(p []byte) (int, error) {
streamer.mutex.Lock()
defer streamer.mutex.Unlock()
if streamer.eof {
return 0, io.EOF
}
var i int
var err error
for i = 0; i < len(p); i++ {
var n int
n, err = streamer.reader.Read(p[i : i+1])
// process n == 1 before handling err, in case err is io.EOF
if n == 1 {
if p[i] == '"' {
streamer.eof = true
err = io.EOF
} else if p[i] == '\\' {
if err == nil {
// Psiphon server list string values contain '\n', so support
// that required case.
n, err = streamer.reader.Read(p[i : i+1])
if n == 1 && p[i] == 'n' {
p[i] = '\n'
} else {
err = errors.TraceNew("unsupported escaped character")
}
}
}
}
if err != nil {
break
}
}
return i, err
}