-
Notifications
You must be signed in to change notification settings - Fork 256
/
meekConn.go
639 lines (588 loc) · 23 KB
/
meekConn.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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
/*
* Copyright (c) 2015, 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 (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"sync"
"time"
"golang.org/x/crypto/nacl/box"
)
// MeekConn is based on meek-client.go from Tor and Psiphon:
//
// https://gitweb.torproject.org/pluggable-transports/meek.git/blob/HEAD:/meek-client/meek-client.go
// CC0 1.0 Universal
//
// https://bitbucket.org/psiphon/psiphon-circumvention-system/src/default/go/meek-client/meek-client.go
const (
MEEK_PROTOCOL_VERSION = 2
MEEK_COOKIE_MAX_PADDING = 32
MAX_SEND_PAYLOAD_LENGTH = 65536
FULL_RECEIVE_BUFFER_LENGTH = 4194304
READ_PAYLOAD_CHUNK_LENGTH = 65536
MIN_POLL_INTERVAL = 100 * time.Millisecond
MAX_POLL_INTERVAL = 5 * time.Second
POLL_INTERNAL_MULTIPLIER = 1.5
MEEK_ROUND_TRIP_RETRY_DEADLINE = 1 * time.Second
MEEK_ROUND_TRIP_RETRY_DELAY = 50 * time.Millisecond
)
// MeekConn is a network connection that tunnels TCP over HTTP and supports "fronting". Meek sends
// client->server flow in HTTP request bodies and receives server->client flow in HTTP response bodies.
// Polling is used to achieve full duplex TCP.
//
// Fronting is an obfuscation technique in which the connection
// to a web server, typically a CDN, is indistinguishable from any other HTTPS connection to the generic
// "fronting domain" -- the HTTP Host header is used to route the requests to the actual destination.
// See https://trac.torproject.org/projects/tor/wiki/doc/meek for more details.
//
// MeekConn also operates in unfronted mode, in which plain HTTP connections are made without routing
// through a CDN.
type MeekConn struct {
frontingAddress string
url *url.URL
cookie *http.Cookie
pendingConns *Conns
transport *http.Transport
mutex sync.Mutex
isClosed bool
closedSignal chan struct{}
broadcastClosed chan struct{}
relayWaitGroup *sync.WaitGroup
emptyReceiveBuffer chan *bytes.Buffer
partialReceiveBuffer chan *bytes.Buffer
fullReceiveBuffer chan *bytes.Buffer
emptySendBuffer chan *bytes.Buffer
partialSendBuffer chan *bytes.Buffer
fullSendBuffer chan *bytes.Buffer
}
// DialMeek returns an initialized meek connection. A meek connection is
// an HTTP session which does not depend on an underlying socket connection (although
// persistent HTTP connections are used for performance). This function does not
// wait for the connection to be "established" before returning. A goroutine
// is spawned which will eventually start HTTP polling.
// When frontingAddress is not "", fronting is used. This option assumes caller has
// already checked server entry capabilities.
func DialMeek(
serverEntry *ServerEntry, sessionId string,
frontingAddress string, config *DialConfig) (meek *MeekConn, err error) {
// Configure transport
// Note: MeekConn has its own PendingConns to manage the underlying HTTP transport connections,
// which may be interrupted on MeekConn.Close(). This code previously used the establishTunnel
// pendingConns here, but that was a lifecycle mismatch: we don't want to abort HTTP transport
// connections while MeekConn is still in use
pendingConns := new(Conns)
// Use a copy of DialConfig with the meek pendingConns
meekConfig := new(DialConfig)
*meekConfig = *config
meekConfig.PendingConns = pendingConns
var host string
var dialer Dialer
var proxyUrl func(*http.Request) (*url.URL, error)
if frontingAddress != "" {
// In this case, host is not what is dialed but is what ends up in the HTTP Host header
host = serverEntry.MeekFrontingHost
// Custom TLS dialer:
//
// 1. ignores the HTTP request address and uses the fronting domain
// 2. disables SNI -- SNI breaks fronting when used with CDNs that support SNI on the server side.
// 3. skips verifying the server cert.
//
// Reasoning for #3:
//
// With a TLS MiM attack in place, and server certs verified, we'll fail to connect because the client
// will refuse to connect. That's not a successful outcome.
//
// With a MiM attack in place, and server certs not verified, we'll fail to connect if the MiM is actively
// targeting Psiphon and classifying the HTTP traffic by Host header or payload signature.
//
// However, in the case of a passive MiM that's just recording traffic or an active MiM that's targeting
// something other than Psiphon, the client will connect. This is a successful outcome.
//
// What is exposed to the MiM? The Host header does not contain a Psiphon server IP address, just an
// unrelated, randomly generated domain name which cannot be used to block direct connections. The
// Psiphon server IP is sent over meek, but it's in the encrypted cookie.
//
// The payload (user traffic) gets its confidentiality and integrity from the underlying SSH protocol.
// So, nothing is leaked to the MiM apart from signatures which could be used to classify the traffic
// as Psiphon to possibly block it; but note that not revealing that the client is Psiphon is outside
// our threat model; we merely seek to evade mass blocking by taking steps that require progressively
// more effort to block.
//
// There is a subtle attack remaining: an adversary that can MiM some CDNs but not others (and so can
// classify Psiphon traffic on some CDNs but not others) may throttle non-MiM CDNs so that our server
// selection always chooses tunnels to the MiM CDN (without any server cert verification, we won't
// exclusively connect to non-MiM CDNs); then the adversary kills the underlying TCP connection after
// some short period. This is similar to the "unidentified protocol" attack outlined in selectProtocol().
// A similar weighted selection defense may be appropriate.
dialer = NewCustomTLSDialer(
&CustomTLSConfig{
Dial: NewTCPDialer(meekConfig),
Timeout: meekConfig.ConnectTimeout,
FrontingAddr: fmt.Sprintf("%s:%d", frontingAddress, 443),
SendServerName: false,
SkipVerify: true,
})
} else {
// In this case, host is both what is dialed and what ends up in the HTTP Host header
host = fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.MeekServerPort)
if meekConfig.UpstreamHttpProxyAddress != "" {
// For unfronted meek, we let the http.Transport handle proxying, as the
// target server hostname has to be in the HTTP request line. Also, in this
// case, we don't require the proxy to support CONNECT and so we can work
// through HTTP proxies that don't support it.
url, err := url.Parse(fmt.Sprintf("http://%s", meekConfig.UpstreamHttpProxyAddress))
if err != nil {
return nil, ContextError(err)
}
proxyUrl = http.ProxyURL(url)
meekConfig.UpstreamHttpProxyAddress = ""
}
dialer = NewTCPDialer(meekConfig)
}
// Scheme is always "http". Otherwise http.Transport will try to do another TLS
// handshake inside the explicit TLS session (in fronting mode).
url := &url.URL{
Scheme: "http",
Host: host,
Path: "/",
}
cookie, err := makeCookie(serverEntry, sessionId)
if err != nil {
return nil, ContextError(err)
}
transport := &http.Transport{
Proxy: proxyUrl,
Dial: dialer,
ResponseHeaderTimeout: TUNNEL_WRITE_TIMEOUT,
}
// The main loop of a MeekConn is run in the relay() goroutine.
// A MeekConn implements net.Conn concurrency semantics:
// "Multiple goroutines may invoke methods on a Conn simultaneously."
//
// Read() calls and relay() are synchronized by exchanging control of a single
// receiveBuffer (bytes.Buffer). This single buffer may be:
// - in the emptyReceiveBuffer channel when it is available and empty;
// - in the partialReadBuffer channel when it is available and contains data;
// - in the fullReadBuffer channel when it is available and full of data;
// - "checked out" by relay or Read when they are are writing to or reading from the
// buffer, respectively.
// relay() will obtain the buffer from either the empty or partial channel but block when
// the buffer is full. Read will obtain the buffer from the partial or full channel when
// there is data to read but block when the buffer is empty.
// Write() calls and relay() are synchronized in a similar way, using a single
// sendBuffer.
meek = &MeekConn{
frontingAddress: frontingAddress,
url: url,
cookie: cookie,
pendingConns: pendingConns,
transport: transport,
isClosed: false,
broadcastClosed: make(chan struct{}),
relayWaitGroup: new(sync.WaitGroup),
emptyReceiveBuffer: make(chan *bytes.Buffer, 1),
partialReceiveBuffer: make(chan *bytes.Buffer, 1),
fullReceiveBuffer: make(chan *bytes.Buffer, 1),
emptySendBuffer: make(chan *bytes.Buffer, 1),
partialSendBuffer: make(chan *bytes.Buffer, 1),
fullSendBuffer: make(chan *bytes.Buffer, 1),
}
// TODO: benchmark bytes.Buffer vs. built-in append with slices?
meek.emptyReceiveBuffer <- new(bytes.Buffer)
meek.emptySendBuffer <- new(bytes.Buffer)
meek.relayWaitGroup.Add(1)
go meek.relay()
// Enable interruption
config.PendingConns.Add(meek)
return meek, nil
}
// SetClosedSignal implements psiphon.Conn.SetClosedSignal
func (meek *MeekConn) SetClosedSignal(closedSignal chan struct{}) bool {
meek.mutex.Lock()
defer meek.mutex.Unlock()
if meek.isClosed {
return false
}
meek.closedSignal = closedSignal
return true
}
// Close terminates the meek connection. Close waits for the relay processing goroutine
// to stop and releases HTTP transport resources.
// A mutex is required to support psiphon.Conn.SetClosedSignal concurrency semantics.
func (meek *MeekConn) Close() (err error) {
meek.mutex.Lock()
defer meek.mutex.Unlock()
if !meek.isClosed {
close(meek.broadcastClosed)
meek.pendingConns.CloseAll()
meek.relayWaitGroup.Wait()
meek.transport.CloseIdleConnections()
meek.isClosed = true
select {
case meek.closedSignal <- *new(struct{}):
default:
}
}
return nil
}
func (meek *MeekConn) closed() bool {
meek.mutex.Lock()
defer meek.mutex.Unlock()
return meek.isClosed
}
// Read reads data from the connection.
// net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
func (meek *MeekConn) Read(buffer []byte) (n int, err error) {
if meek.closed() {
return 0, ContextError(errors.New("meek connection is closed"))
}
// Block until there is received data to consume
var receiveBuffer *bytes.Buffer
select {
case receiveBuffer = <-meek.partialReceiveBuffer:
case receiveBuffer = <-meek.fullReceiveBuffer:
case <-meek.broadcastClosed:
return 0, ContextError(errors.New("meek connection has closed"))
}
n, err = receiveBuffer.Read(buffer)
meek.replaceReceiveBuffer(receiveBuffer)
return n, err
}
// Write writes data to the connection.
// net.Conn Deadlines are ignored. net.Conn concurrency semantics are supported.
func (meek *MeekConn) Write(buffer []byte) (n int, err error) {
if meek.closed() {
return 0, ContextError(errors.New("meek connection is closed"))
}
// Repeats until all n bytes are written
n = len(buffer)
for len(buffer) > 0 {
// Block until there is capacity in the send buffer
var sendBuffer *bytes.Buffer
select {
case sendBuffer = <-meek.emptySendBuffer:
case sendBuffer = <-meek.partialSendBuffer:
case <-meek.broadcastClosed:
return 0, ContextError(errors.New("meek connection has closed"))
}
writeLen := MAX_SEND_PAYLOAD_LENGTH - sendBuffer.Len()
if writeLen > 0 {
if writeLen > len(buffer) {
writeLen = len(buffer)
}
_, err = sendBuffer.Write(buffer[:writeLen])
buffer = buffer[writeLen:]
}
meek.replaceSendBuffer(sendBuffer)
}
return n, err
}
// Stub implementation of net.Conn.LocalAddr
func (meek *MeekConn) LocalAddr() net.Addr {
return nil
}
// Stub implementation of net.Conn.RemoteAddr
func (meek *MeekConn) RemoteAddr() net.Addr {
return nil
}
// Stub implementation of net.Conn.SetDeadline
func (meek *MeekConn) SetDeadline(t time.Time) error {
return ContextError(errors.New("not supported"))
}
// Stub implementation of net.Conn.SetReadDeadline
func (meek *MeekConn) SetReadDeadline(t time.Time) error {
return ContextError(errors.New("not supported"))
}
// Stub implementation of net.Conn.SetWriteDeadline
func (meek *MeekConn) SetWriteDeadline(t time.Time) error {
return ContextError(errors.New("not supported"))
}
func (meek *MeekConn) replaceReceiveBuffer(receiveBuffer *bytes.Buffer) {
switch {
case receiveBuffer.Len() == 0:
meek.emptyReceiveBuffer <- receiveBuffer
case receiveBuffer.Len() >= FULL_RECEIVE_BUFFER_LENGTH:
meek.fullReceiveBuffer <- receiveBuffer
default:
meek.partialReceiveBuffer <- receiveBuffer
}
}
func (meek *MeekConn) replaceSendBuffer(sendBuffer *bytes.Buffer) {
switch {
case sendBuffer.Len() == 0:
meek.emptySendBuffer <- sendBuffer
case sendBuffer.Len() >= MAX_SEND_PAYLOAD_LENGTH:
meek.fullSendBuffer <- sendBuffer
default:
meek.partialSendBuffer <- sendBuffer
}
}
// relay sends and receives tunneled traffic (payload). An HTTP request is
// triggered when data is in the write queue or at a polling interval.
// There's a geometric increase, up to a maximum, in the polling interval when
// no data is exchanged. Only one HTTP request is in flight at a time.
func (meek *MeekConn) relay() {
// Note: meek.Close() calls here in relay() are made asynchronously
// (using goroutines) since Close() will wait on this WaitGroup.
defer meek.relayWaitGroup.Done()
interval := MIN_POLL_INTERVAL
timeout := time.NewTimer(interval)
sendPayload := make([]byte, MAX_SEND_PAYLOAD_LENGTH)
for {
timeout.Reset(interval)
// Block until there is payload to send or it is time to poll
var sendBuffer *bytes.Buffer
select {
case sendBuffer = <-meek.partialSendBuffer:
case sendBuffer = <-meek.fullSendBuffer:
case <-timeout.C:
// In the polling case, send an empty payload
case <-meek.broadcastClosed:
// TODO: timeout case may be selected when broadcastClosed is set?
return
}
sendPayloadSize := 0
if sendBuffer != nil {
var err error
sendPayloadSize, err = sendBuffer.Read(sendPayload)
meek.replaceSendBuffer(sendBuffer)
if err != nil {
NoticeAlert("%s", ContextError(err))
go meek.Close()
return
}
}
receivedPayload, err := meek.roundTrip(sendPayload[:sendPayloadSize])
if err != nil {
NoticeAlert("%s", ContextError(err))
go meek.Close()
return
}
if receivedPayload == nil {
// In this case, meek.roundTrip encountered broadcastClosed. Exit without error.
return
}
receivedPayloadSize, err := meek.readPayload(receivedPayload)
if err != nil {
NoticeAlert("%s", ContextError(err))
go meek.Close()
return
}
if receivedPayloadSize > 0 || sendPayloadSize > 0 {
interval = 0
} else if interval == 0 {
interval = MIN_POLL_INTERVAL
} else {
interval = time.Duration(float64(interval) * POLL_INTERNAL_MULTIPLIER)
if interval >= MAX_POLL_INTERVAL {
interval = MIN_POLL_INTERVAL
}
}
}
}
// readPayload reads the HTTP response in chunks, making the read buffer available
// to MeekConn.Read() calls after each chunk; the intention is to allow bytes to
// flow back to the reader as soon as possible instead of buffering the entire payload.
func (meek *MeekConn) readPayload(receivedPayload io.ReadCloser) (totalSize int64, err error) {
defer receivedPayload.Close()
totalSize = 0
for {
reader := io.LimitReader(receivedPayload, READ_PAYLOAD_CHUNK_LENGTH)
// Block until there is capacity in the receive buffer
var receiveBuffer *bytes.Buffer
select {
case receiveBuffer = <-meek.emptyReceiveBuffer:
case receiveBuffer = <-meek.partialReceiveBuffer:
case <-meek.broadcastClosed:
return 0, nil
}
// Note: receiveBuffer size may exceed FULL_RECEIVE_BUFFER_LENGTH by up to the size
// of one received payload. The FULL_RECEIVE_BUFFER_LENGTH value is just a threshold.
n, err := receiveBuffer.ReadFrom(reader)
meek.replaceReceiveBuffer(receiveBuffer)
if err != nil {
return 0, ContextError(err)
}
totalSize += n
if n == 0 {
break
}
}
return totalSize, nil
}
// roundTrip configures and makes the actual HTTP POST request
func (meek *MeekConn) roundTrip(sendPayload []byte) (receivedPayload io.ReadCloser, err error) {
request, err := http.NewRequest("POST", meek.url.String(), bytes.NewReader(sendPayload))
if err != nil {
return nil, err
}
if meek.frontingAddress != "" && nil == net.ParseIP(meek.frontingAddress) {
request.Header.Set("X-Psiphon-Fronting-Address", meek.frontingAddress)
}
// Don't use the default user agent ("Go 1.1 package http").
// For now, just omit the header (net/http/request.go: "may be blank to not send the header").
request.Header.Set("User-Agent", "")
request.Header.Set("Content-Type", "application/octet-stream")
request.AddCookie(meek.cookie)
// The retry mitigates intermittent failures between the client and front/server.
//
// Note: Retry will only be effective if entire request failed (underlying transport protocol
// such as SSH will fail if extra bytes are replayed in either direction due to partial relay
// success followed by retry).
// At least one retry is always attempted. We retry when still within a brief deadline and wait
// for a short time before re-dialing.
//
// TODO: in principle, we could retry for min(TUNNEL_WRITE_TIMEOUT, meek-server.MAX_SESSION_STALENESS),
// i.e., as long as the underlying tunnel has not timed out and as long as the server has not
// expired the current meek session. Presently not doing this to avoid excessive connection attempts
// through the first hop. In addition, this will require additional support for timely shutdown.
retries := uint(0)
retryDeadline := time.Now().Add(MEEK_ROUND_TRIP_RETRY_DEADLINE)
var response *http.Response
for {
// The http.Transport.RoundTrip is run in a goroutine to enable cancelling a request in-flight.
type roundTripResponse struct {
response *http.Response
err error
}
roundTripResponseChannel := make(chan *roundTripResponse, 1)
roundTripWaitGroup := new(sync.WaitGroup)
roundTripWaitGroup.Add(1)
go func() {
defer roundTripWaitGroup.Done()
r, err := meek.transport.RoundTrip(request)
roundTripResponseChannel <- &roundTripResponse{r, err}
}()
select {
case roundTripResponse := <-roundTripResponseChannel:
response = roundTripResponse.response
err = roundTripResponse.err
case <-meek.broadcastClosed:
meek.transport.CancelRequest(request)
return nil, nil
}
roundTripWaitGroup.Wait()
if err == nil {
break
}
if retries >= 1 && time.Now().After(retryDeadline) {
break
}
retries += 1
time.Sleep(MEEK_ROUND_TRIP_RETRY_DELAY)
}
if err != nil {
return nil, ContextError(err)
}
if response.StatusCode != http.StatusOK {
return nil, ContextError(fmt.Errorf("http request failed %d", response.StatusCode))
}
// observe response cookies for meek session key token.
// Once found it must be used for all consecutive requests made to the server
for _, c := range response.Cookies() {
if meek.cookie.Name == c.Name {
meek.cookie.Value = c.Value
break
}
}
return response.Body, nil
}
type meekCookieData struct {
ServerAddress string `json:"p"`
SessionID string `json:"s"`
MeekProtocolVersion int `json:"v"`
}
// makeCookie creates the cookie to be sent with initial meek HTTP request.
// The purpose of the cookie is to send the following to the server:
// ServerAddress -- the Psiphon Server address the meek server should relay to
// SessionID -- the Psiphon session ID (used by meek server to relay geolocation
// information obtained from the CDN through to the Psiphon Server)
// MeekProtocolVersion -- tells the meek server that this client understands
// the latest protocol.
// The server will create a session using these values and send the session ID
// back to the client via Set-Cookie header. Client must use that value with
// all consequent HTTP requests
// In unfronted meek mode, the cookie is visible over the adversary network, so the
// cookie is encrypted and obfuscated.
func makeCookie(serverEntry *ServerEntry, sessionId string) (cookie *http.Cookie, err error) {
// Make the JSON data
serverAddress := fmt.Sprintf("%s:%d", serverEntry.IpAddress, serverEntry.SshObfuscatedPort)
cookieData := &meekCookieData{
ServerAddress: serverAddress,
SessionID: sessionId,
MeekProtocolVersion: MEEK_PROTOCOL_VERSION,
}
serializedCookie, err := json.Marshal(cookieData)
if err != nil {
return nil, ContextError(err)
}
// Encrypt the JSON data
// NaCl box is used for encryption. The peer public key comes from the server entry.
// Nonce is always all zeros, and is not sent in the cookie (the server also uses an all-zero nonce).
// http://nacl.cace-project.eu/box.html:
// "There is no harm in having the same nonce for different messages if the {sender, receiver} sets are
// different. This is true even if the sets overlap. For example, a sender can use the same nonce for two
// different messages if the messages are sent to two different public keys."
var nonce [24]byte
var publicKey [32]byte
decodedPublicKey, err := base64.StdEncoding.DecodeString(serverEntry.MeekCookieEncryptionPublicKey)
if err != nil {
return nil, ContextError(err)
}
copy(publicKey[:], decodedPublicKey)
ephemeralPublicKey, ephemeralPrivateKey, err := box.GenerateKey(rand.Reader)
if err != nil {
return nil, ContextError(err)
}
box := box.Seal(nil, serializedCookie, &nonce, &publicKey, ephemeralPrivateKey)
encryptedCookie := make([]byte, 32+len(box))
copy(encryptedCookie[0:32], ephemeralPublicKey[0:32])
copy(encryptedCookie[32:], box)
// Obfuscate the encrypted data
obfuscator, err := NewObfuscator(
&ObfuscatorConfig{Keyword: serverEntry.MeekObfuscatedKey, MaxPadding: MEEK_COOKIE_MAX_PADDING})
if err != nil {
return nil, ContextError(err)
}
obfuscatedCookie := obfuscator.ConsumeSeedMessage()
seedLen := len(obfuscatedCookie)
obfuscatedCookie = append(obfuscatedCookie, encryptedCookie...)
obfuscator.ObfuscateClientToServer(obfuscatedCookie[seedLen:])
// Format the HTTP cookie
// The format is <random letter 'A'-'Z'>=<base64 data>, which is intended to match common cookie formats.
A := int('A')
Z := int('Z')
// letterIndex is integer in range [int('A'), int('Z')]
letterIndex, err := MakeSecureRandomInt(Z - A + 1)
if err != nil {
return nil, ContextError(err)
}
return &http.Cookie{
Name: string(byte(A + letterIndex)),
Value: base64.StdEncoding.EncodeToString(obfuscatedCookie)},
nil
}