-
Notifications
You must be signed in to change notification settings - Fork 6
/
common.ts
1128 lines (1013 loc) · 40.6 KB
/
common.ts
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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (C) 2016-2022 Threema GmbH
*
* This software may be modified and distributed under the terms
* of the MIT license. See the `LICENSE.md` file for details.
*/
import * as msgpack from 'msgpack-lite';
import * as nacl from 'tweetnacl';
import { CloseCode, explainCloseCode } from '../closecode';
import { Cookie } from '../cookie';
import { ConnectionError, ProtocolError, SignalingError, ValidationError } from '../exceptions';
import { Box } from '../keystore';
import { Nonce } from '../nonce';
import { Initiator, Peer, Responder, Server } from '../peers';
import {
arraysAreEqual,
arrayToBuffer,
byteToHex,
concat,
u8aToHex,
} from '../utils';
import { HandoverState } from './handoverstate';
import { isResponderId } from './helpers';
/**
* Signaling base class.
*/
export abstract class Signaling implements saltyrtc.Signaling {
// Constants
protected static SALTYRTC_SUBPROTOCOL = 'v1.saltyrtc.org';
protected static SALTYRTC_ADDR_UNKNOWN = 0x00;
protected static SALTYRTC_ADDR_SERVER = 0x00;
protected static SALTYRTC_ADDR_INITIATOR = 0x01;
// Logging
protected log: saltyrtc.Log;
// WebSocket
protected host: string;
protected port: number;
protected protocol: string = 'wss';
protected ws: WebSocket = null;
protected pingInterval: number;
// Msgpack
protected msgpackEncodeOptions: msgpack.EncoderOptions = {
codec: msgpack.createCodec({binarraybuffer: true}),
};
protected msgpackDecodeOptions: msgpack.DecoderOptions = {
codec: msgpack.createCodec({binarraybuffer: true}),
};
// Connection state
protected state: saltyrtc.SignalingState = 'new';
public handoverState = new HandoverState();
private neverConnected: boolean = true;
// Main class
protected client: saltyrtc.SaltyRTC;
// Tasks
protected tasks: saltyrtc.Task[];
public task: saltyrtc.Task = null;
// Server information
protected server = new Server();
// Our permanent key
protected permanentKey: saltyrtc.KeyStore;
// Peer trusted key or auth token
protected peerTrustedKey: Uint8Array = null;
protected authToken: saltyrtc.AuthToken = null;
// Server trusted key
protected serverPublicKey: Uint8Array = null;
// Signaling
public role: saltyrtc.SignalingRole = null;
protected logTag: string = '[SaltyRTC.Signaling]';
protected address: number = Signaling.SALTYRTC_ADDR_UNKNOWN;
/**
* Create a new signaling instance.
*/
protected constructor(
client: saltyrtc.SaltyRTC, host: string, port: number, serverKey: Uint8Array,
tasks: saltyrtc.Task[], pingInterval: number,
permanentKey: saltyrtc.KeyStore, peerTrustedKey?: Uint8Array,
) {
this.log = client.log;
this.client = client;
this.permanentKey = permanentKey;
this.host = host;
this.port = port;
this.tasks = tasks;
this.pingInterval = pingInterval;
if (peerTrustedKey !== undefined) {
this.peerTrustedKey = peerTrustedKey;
}
if (serverKey !== undefined) {
this.serverPublicKey = serverKey;
}
this.handoverState.onBoth = () => {
this.client.emit({type: 'handover'});
this.closeWebsocket(CloseCode.Handover);
};
}
/**
* Register a signaling state change.
*/
public setState(newState: saltyrtc.SignalingState): void {
this.state = newState;
// Notify listeners
this.client.emit({type: 'state-change', data: newState});
this.client.emit({type: 'state-change:' + newState});
}
/**
* Return current state.
*/
public getState(): saltyrtc.SignalingState {
return this.state;
}
/**
* Return the public permanent key as Uint8Array.
*/
public get permanentKeyBytes(): Uint8Array {
return this.permanentKey.publicKeyBytes;
}
/**
* Return the auth token as Uint8Array, or null if no auth token is initialized.
*/
public get authTokenBytes(): Uint8Array {
if (this.authToken !== null) {
return this.authToken.keyBytes;
}
return null;
}
/**
* Return the peer permanent key as Uint8Array.
*/
public get peerPermanentKeyBytes(): Uint8Array {
return this.getPeer().permanentSharedKey.remotePublicKeyBytes;
}
/**
* Encode msgpack data.
*/
protected msgpackEncode(data: object) {
return msgpack.encode(data, this.msgpackEncodeOptions);
}
/**
* Decode msgpack data.
*/
protected msgpackDecode(data: Uint8Array) {
return msgpack.decode(data, this.msgpackDecodeOptions);
}
/**
* Open a connection to the signaling server and do the handshake.
*/
public connect(): void {
if (this.neverConnected !== true) {
throw new ConnectionError('Signaling instance cannot be reused. Please create a new client instance.');
}
this.neverConnected = false;
this.resetConnection();
this.initWebsocket();
}
/**
* Disconnect from the signaling server.
*
* @param unbind Whether to unbind all WebSocket events.
*/
public disconnect(unbind = false): void {
const reason = CloseCode.ClosingNormal;
// Update state
this.setState('closing');
// Send close message if necessary
if (this.state === 'task') {
this.sendClose(reason);
}
// Close WebSocket instance and unbind all events
this.closeWebsocket(reason, undefined, unbind);
// Close task connections
if (this.task !== null) {
this.log.debug(this.logTag, 'Closing task connections');
this.task.close(reason);
}
// Update state
this.setState('closed');
}
/**
* Close the WebSocket connection.
*
* @param code The close code.
* @param reason The close reason.
* @param unbind Whether to unbind all events. This will move the Signaling
* instance into the `closed` state.
*/
private closeWebsocket(code?: number, reason?: string, unbind = false): void {
if (this.ws !== null) {
// Drop internal close codes
// see: https://github.com/saltyrtc/saltyrtc-meta/issues/110
if (code === undefined || code <= 3000) {
code = CloseCode.ClosingNormal;
}
// Disconnect
this.log.debug(this.logTag, `Disconnecting WebSocket, close code: ${code}`);
this.ws.close(code, reason);
// Unbind events?
if (unbind) {
this.ws.removeEventListener('open', this.onOpen.bind(this));
this.ws.removeEventListener('error', this.onError.bind(this));
this.ws.removeEventListener('close', this.onClose.bind(this));
this.ws.removeEventListener('message', this.onMessage.bind(this));
}
// Forget instance
this.ws = null;
// Move into closed state (if necessary)
if (unbind) {
this.setState('closed');
}
}
}
/**
* Return connection path for WebSocket.
*/
protected abstract getWebsocketPath(): string;
/**
* Open a new WebSocket connection to the signaling server.
*/
protected initWebsocket() {
const url = this.protocol + '://' + this.host + ':' + this.port + '/';
const path = this.getWebsocketPath();
this.ws = new WebSocket(url + path, Signaling.SALTYRTC_SUBPROTOCOL);
// Set binary type
this.ws.binaryType = 'arraybuffer';
// Set event handlers
this.ws.addEventListener('open', this.onOpen.bind(this));
this.ws.addEventListener('error', this.onError.bind(this));
this.ws.addEventListener('close', this.onClose.bind(this));
this.ws.addEventListener('message', this.onMessage.bind(this));
// Store connection on instance
this.setState('ws-connecting');
this.log.debug(this.logTag, 'Opening WebSocket connection to', url + path);
}
/**
* WebSocket onopen handler.
*/
protected onOpen(): void {
this.log.info(this.logTag, 'Opened connection');
this.setState('server-handshake');
}
/**
* WebSocket onerror handler.
*/
protected onError(ev: ErrorEvent): void {
this.log.error(this.logTag, 'General WebSocket error', ev);
this.client.emit({type: 'connection-error'});
// Note: We don't update the state here, because an error event will be followed
// by a close event, which is already handled in `onClose`.
}
/**
* WebSocket onclose handler.
*/
protected onClose(ev: CloseEvent): void {
if (ev.code === CloseCode.Handover) {
this.log.info(this.logTag, 'Closed WebSocket connection due to handover');
} else {
this.log.info(this.logTag, 'Closed WebSocket connection with close code ' + ev.code +
' (' + explainCloseCode(ev.code) + ')');
this.setState('closed');
this.client.emit({type: 'connection-closed', data: ev.code});
}
}
protected onMessage(ev: MessageEvent): void {
this.log.debug(this.logTag, 'New ws message (' + (ev.data as ArrayBuffer).byteLength + ' bytes)');
if (this.handoverState.peer) {
this.log.error(this.logTag, 'Protocol error: Received WebSocket message from peer ' +
'even though it has already handed over to task.');
this.resetConnection(CloseCode.ProtocolError);
return;
}
let nonce: Nonce;
try {
// Parse buffer
const box: saltyrtc.Box = Box.fromUint8Array(new Uint8Array(ev.data), Nonce.TOTAL_LENGTH);
// Parse and validate nonce
nonce = Nonce.fromUint8Array(box.nonce);
const peer = this.getPeerWithId(nonce.source);
if (peer === null) {
// Note: This can happen when a responder has been dropped
// but a message was still in flight.
this.log.debug(this.logTag, 'Ignoring message from unknown id: ' + nonce.source);
return;
}
try {
this.validateNonce(nonce);
} catch (e) {
if (e.name === 'ValidationError') {
if (e.critical === true) {
// noinspection ExceptionCaughtLocallyJS
throw new ProtocolError('Invalid nonce: ' + e);
} else {
this.log.warn(this.logTag, 'Dropping message with invalid nonce: ' + e);
return;
}
} else {
// noinspection ExceptionCaughtLocallyJS
throw e;
}
}
// Dispatch message
switch (this.getState()) {
case 'server-handshake':
this.onServerHandshakeMessage(box, nonce);
break;
case 'peer-handshake':
this.onPeerHandshakeMessage(box, nonce);
break;
case 'task':
this.onSignalingMessage(box, nonce);
break;
default:
this.log.warn(this.logTag, 'Received message in', this.getState(), 'signaling state. Ignoring.');
}
} catch (e) {
if (e.name === 'SignalingError' || e.name === 'ProtocolError') {
let errmsg = 'Signaling error: ' + explainCloseCode(e.closeCode);
if (e.message) {
errmsg += ' (' + e.message + ')';
}
this.log.error(this.logTag, errmsg);
switch (this.state) {
case 'new':
case 'ws-connecting':
case 'server-handshake':
this.resetConnection(e.closeCode);
break;
case 'peer-handshake':
this.handlePeerHandshakeSignalingError(e, nonce === undefined ? null : nonce.source);
break;
case 'task':
this.sendClose(e.closeCode);
this.resetConnection(CloseCode.ClosingNormal);
break;
case 'closing':
case 'closed':
// Ignore
break;
}
} else if (e.name === 'ConnectionError') {
this.log.warn(this.logTag, 'Connection error. Resetting connection.');
this.resetConnection(CloseCode.InternalError);
} else {
if (e.hasOwnProperty('stack')) {
this.log.error(this.logTag, 'An unknown error occurred:');
this.log.error(e.stack);
}
throw e;
}
}
}
protected abstract handlePeerHandshakeSignalingError(e: SignalingError, source: number | null): void;
/**
* Handle messages received during server handshake.
*
* @throws SignalingError
*/
protected onServerHandshakeMessage(box: saltyrtc.Box, nonce: Nonce): void {
// Decrypt if necessary
let payload: Uint8Array;
if (this.server.handshakeState === 'new') {
// The very first message is unencrypted
payload = box.data;
} else {
// Later, they're encrypted with our permanent key and the server key
payload = this.server.sessionSharedKey.decrypt(box);
}
// Handle message
const msg: saltyrtc.Message = this.decodeMessage(payload, 'server handshake');
switch (this.server.handshakeState) {
case 'new':
// Expect server-hello
if (msg.type !== 'server-hello') {
throw new ProtocolError('Expected server-hello message, but got ' + msg.type);
}
this.log.debug(this.logTag, 'Received server-hello');
this.handleServerHello(msg as saltyrtc.messages.ServerHello, nonce);
this.sendClientHello();
this.sendClientAuth();
break;
case 'hello-sent':
throw new ProtocolError('Received ' + msg.type + ' message before sending client-auth');
case 'auth-sent':
// Expect server-auth
if (msg.type !== 'server-auth') {
throw new ProtocolError('Expected server-auth message, but got ' + msg.type);
}
this.log.debug(this.logTag, 'Received server-auth');
this.handleServerAuth(msg as saltyrtc.messages.ServerAuth, nonce);
break;
case 'done':
throw new SignalingError(CloseCode.InternalError,
'Received server handshake message even though server handshake state is set to \'done\'');
default:
throw new SignalingError(CloseCode.InternalError,
'Unknown server handshake state: ' + this.server.handshakeState);
}
// Check if we're done yet
if (this.server.handshakeState as string === 'done') {
this.setState('peer-handshake');
this.log.debug(this.logTag, 'Server handshake done');
this.initPeerHandshake();
}
}
/**
* Handle messages received during peer handshake.
*/
protected abstract onPeerHandshakeMessage(box: saltyrtc.Box, nonce: Nonce): void;
/**
* Handle messages received from peer *after* the handshake is done.
*/
protected onSignalingMessage(box: saltyrtc.Box, nonce: Nonce): void {
this.log.debug(this.logTag, 'Message received');
if (nonce.source === Signaling.SALTYRTC_ADDR_SERVER) {
this.onSignalingServerMessage(box);
} else {
const decrypted: Uint8Array = this.getPeer().sessionSharedKey.decrypt(box);
this.onSignalingPeerMessage(decrypted);
}
}
protected onSignalingServerMessage(box: saltyrtc.Box): void {
const msg: saltyrtc.Message = this.decryptServerMessage(box);
switch (msg.type) {
case 'send-error':
this.log.debug(this.logTag, 'Received send-error message');
this.handleSendError(msg as saltyrtc.messages.SendError);
break;
case 'disconnected':
this.log.debug(this.logTag, 'Received disconnected message');
this.handleDisconnected(msg as saltyrtc.messages.Disconnected);
break;
default:
this.onUnhandledSignalingServerMessage(msg);
}
}
/**
* Handle messages received from the server *after* the handshake is done.
*/
protected abstract onUnhandledSignalingServerMessage(msg: saltyrtc.Message): void;
/**
* Signaling message received from peer *after* the handshake is done.
*
* @param decrypted Decrypted bytes from the peer.
* @throws SignalingError if the message is invalid.
*/
public onSignalingPeerMessage(decrypted: Uint8Array): void {
const msg: saltyrtc.Message = this.decodeMessage(decrypted);
if (msg.type === 'close') {
this.log.debug(this.logTag, 'Received close');
this.handleClose(msg as saltyrtc.messages.Close);
} else if (msg.type === 'application') {
this.log.debug(this.logTag, 'Received application message');
this.handleApplication(msg as saltyrtc.messages.Application);
} else if (this.task !== null) {
const messageSupportedByTask = this.task.getSupportedMessageTypes().indexOf(msg.type) !== -1;
if (messageSupportedByTask) {
this.log.debug(this.logTag, 'Received', msg.type, '[' + this.task.getName() + ']');
this.task.onTaskMessage(msg as saltyrtc.messages.TaskMessage);
} else {
this.log.error(this.logTag, 'Received', msg.type, 'message which is not supported by the',
this.task.getName(), 'task');
this.resetConnection(CloseCode.ProtocolError);
}
} else {
this.log.warn(this.logTag, 'Received message with invalid type from peer:', msg.type);
}
}
/**
* Handle an incoming server-hello message.
*/
protected handleServerHello(msg: saltyrtc.messages.ServerHello, nonce: Nonce): void {
// Update server instance with the established session key and cookie
this.server.setSessionSharedKey(new Uint8Array(msg.key), this.permanentKey);
this.server.cookiePair.theirs = nonce.cookie;
}
/**
* Send a client-hello message to the server.
*/
protected abstract sendClientHello(): void;
/**
* Send a client-auth message to the server.
*/
protected sendClientAuth(): void {
const message: saltyrtc.messages.ClientAuth = {
type: 'client-auth',
your_cookie: arrayToBuffer(this.server.cookiePair.theirs.bytes),
subprotocols: [Signaling.SALTYRTC_SUBPROTOCOL],
ping_interval: this.pingInterval,
};
if (this.serverPublicKey !== null) {
message.your_key = arrayToBuffer(this.serverPublicKey);
}
const packet: Uint8Array = this.buildPacket(message, this.server);
this.log.debug(this.logTag, 'Sending client-auth');
this.ws.send(packet);
this.server.handshakeState = 'auth-sent';
}
/**
* Handle an incoming server-auth message.
*/
protected abstract handleServerAuth(msg: saltyrtc.messages.ServerAuth, nonce: Nonce): void;
/**
* Initialize the peer handshake.
*/
protected abstract initPeerHandshake(): void;
/**
* Handle an incoming send-error message.
*/
protected handleSendError(msg: saltyrtc.messages.SendError): void {
// Get the message id from the send-error message
const id: DataView = new DataView(msg.id);
const idString: string = u8aToHex(new Uint8Array(msg.id));
// Determine the sender and receiver of the message
const source = id.getUint8(0);
const destination = id.getUint8(1);
// Validate source
if (source !== this.address) {
throw new ProtocolError('Received send-error message for a message not sent by us!');
}
// TODO: Log info about actual message (#62)
this.log.warn(this.logTag, 'SendError: Could not send unknown message:', idString);
this._handleSendError(destination);
}
protected abstract _handleSendError(receiver: number): void;
/**
* Handle an incoming application message.
*/
protected handleApplication(msg: saltyrtc.messages.Application): void {
this.client.emit({type: 'application', data: msg.data});
}
/**
* Send a close message to the peer.
*/
public sendClose(reason: number): void {
const message: saltyrtc.messages.Close = {
type: 'close',
reason: reason,
};
this.log.debug(this.logTag, 'Sending close');
if (this.handoverState.local === true) {
this.task.sendSignalingMessage(this.msgpackEncode(message));
} else {
const packet: Uint8Array = this.buildPacket(message, this.getPeer());
this.ws.send(packet);
}
}
/**
* Handle an incoming close message.
*/
protected handleClose(msg: saltyrtc.messages.Close): void {
this.log.warn(this.logTag, 'Received close message. Reason:',
msg.reason, '(' + explainCloseCode(msg.reason) + ')');
// Notify the task
this.task.close(msg.reason);
// Reset signaling
this.resetConnection(CloseCode.GoingAway);
}
/**
* Handle an incoming disconnected message.
*/
protected handleDisconnected(msg: saltyrtc.messages.Disconnected): void {
this.client.emit({type: 'peer-disconnected', data: msg.id});
}
/**
* Validate the nonce.
*
* Throw ValidationError if validation fails.
* Throw ProtocolError if something goes wrong.
*/
protected validateNonce(nonce: Nonce): void {
this.validateNonceSource(nonce);
this.validateNonceDestination(nonce);
this.validateNonceCsn(nonce);
this.validateNonceCookie(nonce);
}
/**
* Validate the nonce source.
*/
private validateNonceSource(nonce: Nonce): void {
// An initiator SHALL ONLY process messages from the server (0x00). As
// soon as the initiator has been assigned an identity, it MAY ALSO accept
// messages from other responders (0x02..0xff). Other messages SHALL be
// discarded and SHOULD trigger a warning.
//
// A responder SHALL ONLY process messages from the server (0x00). As soon
// as the responder has been assigned an identity, it MAY ALSO accept
// messages from the initiator (0x01). Other messages SHALL be discarded
// and SHOULD trigger a warning.
switch (this.state) {
case 'server-handshake':
// Messages during server handshake must come from the server.
if (nonce.source !== Signaling.SALTYRTC_ADDR_SERVER) {
throw new ValidationError('Received message during server handshake ' +
'with invalid sender address (' + nonce.source + ' != ' + Signaling.SALTYRTC_ADDR_SERVER + ')',
false);
}
break;
case 'peer-handshake':
case 'task':
// Messages after server handshake may come from server or peer.
if (nonce.source !== Signaling.SALTYRTC_ADDR_SERVER) {
if (this.role === 'initiator' && !isResponderId(nonce.source)) {
throw new ValidationError('Initiator peer message does not come from ' +
'a valid responder address: ' + nonce.source, false);
} else if (this.role === 'responder' && nonce.source !== Signaling.SALTYRTC_ADDR_INITIATOR) {
throw new ValidationError('Responder peer message does not come from ' +
'intitiator (' + Signaling.SALTYRTC_ADDR_INITIATOR + '), ' +
'but from ' + nonce.source, false);
}
}
break;
default:
throw new ProtocolError('Cannot validate message nonce in signaling state ' + this.state);
}
}
/**
* Validate the nonce destination.
*/
private validateNonceDestination(nonce: Nonce): void {
let expected: number = null;
if (this.state === 'server-handshake') {
switch (this.server.handshakeState) {
// Before receiving the server auth message, the destination is 0x00
case 'new':
case 'hello-sent':
expected = Signaling.SALTYRTC_ADDR_UNKNOWN;
break;
// The server auth message contains the assigned receiver byte for the first time
case 'auth-sent':
if (this.role === 'initiator') {
expected = Signaling.SALTYRTC_ADDR_INITIATOR;
} else { // Responder
if (!isResponderId(nonce.destination)) {
throw new ValidationError('Received message during server handshake with invalid ' +
'receiver address (' + nonce.destination + ' is not a valid responder id)');
}
}
break;
// Afterwards, the receiver byte is the assigned address
case 'done':
expected = this.address;
break;
}
} else if (this.state === 'peer-handshake' || this.state === 'task') {
expected = this.address;
} else {
throw new ValidationError('Cannot validate message nonce in signaling state ' + this.state);
}
if (expected !== null && nonce.destination !== expected) {
throw new ValidationError('Received message with invalid destination (' +
nonce.destination + ' != ' + expected + ')');
}
}
/**
* Return the peer instance with the specified id.
*
* In the case of the initiator, the peer can be one of multiple responders!
*/
protected abstract getPeerWithId(id: number): Peer | null;
/**
* Validate the nonce CSN.
*/
protected validateNonceCsn(nonce: Nonce): void {
const peer = this.getPeerWithId(nonce.source);
if (peer === null) {
// This can happen e.g. when a responder was dropped between validating the source
// and the csn.
throw new ProtocolError('Could not find peer ' + nonce.source);
}
// If this is the first message from that sender, validate the overflow number and store the CSN.
if (peer.csnPair.theirs === null) {
if (nonce.overflow !== 0) {
throw new ValidationError('First message from ' + peer.name
+ ' must have set the overflow number to 0');
}
peer.csnPair.theirs = nonce.combinedSequenceNumber;
// Otherwise, make sure that the CSN has been increased.
} else {
const previous = peer.csnPair.theirs;
const current = nonce.combinedSequenceNumber;
if (current < previous) {
throw new ValidationError(peer.name + ' CSN is lower than last time');
} else if (current === previous) {
throw new ValidationError(peer.name + " CSN hasn't been incremented");
} else {
peer.csnPair.theirs = current;
}
}
}
/**
* Validate the nonce cookie.
*/
private validateNonceCookie(nonce: Nonce): void {
const peer = this.getPeerWithId(nonce.source);
if (peer !== null && peer.cookiePair.theirs !== null) {
if (!nonce.cookie.equals(peer.cookiePair.theirs)) {
throw new ValidationError(peer.name + ' cookie changed');
}
}
}
/**
* Validate a repeated cookie in an incoming Auth / Server-Auth message.
*
* If it does not equal our own cookie, throw a ProtocolError.
*/
protected validateRepeatedCookie(peer: Peer, repeatedCookieBytes: Uint8Array): void {
const repeatedCookie = new Cookie(repeatedCookieBytes);
if (!repeatedCookie.equals(peer.cookiePair.ours)) {
this.log.debug(this.logTag, 'Their cookie:', repeatedCookie.bytes);
this.log.debug(this.logTag, 'Our cookie:', peer.cookiePair.ours.bytes);
throw new ProtocolError('Peer repeated cookie does not match our cookie');
}
}
/**
* Validate the signed keys sent by the server in the server-auth message.
*
* @param signedKeys The `signed_keys` field from the server-auth message.
* @param nonce The incoming message nonce.
* @param serverPublicKey The expected server public permanent key.
* @throws ValidationError if the signed keys are not valid.
*/
protected validateSignedKeys(signedKeys: Uint8Array, nonce: Nonce, serverPublicKey: Uint8Array): void {
if (signedKeys === null || signedKeys === undefined) {
throw new ValidationError('Server did not send signed_keys in server-auth message');
}
const box = new Box(nonce.toUint8Array(), new Uint8Array(signedKeys), nacl.box.nonceLength);
this.log.debug(this.logTag, 'Expected server public permanent key is', u8aToHex(serverPublicKey));
let decrypted: Uint8Array;
try {
// Note: We will not create a SharedKeyStore here since this will be done only once
decrypted = this.permanentKey.decrypt(box, serverPublicKey);
} catch (e) {
if (e.name === 'CryptoError' && e.code === 'decryption-failed') {
throw new ValidationError('Could not decrypt signed_keys in server_auth message');
}
throw e;
}
const expected = concat(
this.server.sessionSharedKey.remotePublicKeyBytes,
this.permanentKey.publicKeyBytes,
);
if (!arraysAreEqual(decrypted, expected)) {
throw new ValidationError('Decrypted signed_keys in server-auth message is invalid');
}
}
/**
* Decode the decrypted message and validate type.
*
* If decoding fails, throw a `ProtocolError`.
*
* If `enforce` is set to true and the actual type does not match the
* expected type, throw a `ProtocolError`.
*
* @throws ProtocolError
*/
protected decodeMessage(
data: Uint8Array,
expectedType?: saltyrtc.messages.MessageType | string,
enforce = false,
): saltyrtc.Message {
// Decode
const msg = this.msgpackDecode(data) as saltyrtc.Message;
if (msg.type === undefined) {
throw new ProtocolError('Malformed ' + expectedType + ' message: Failed to decode msgpack data.');
}
// Validate type
if (enforce && expectedType !== undefined && msg.type !== expectedType) {
throw new ProtocolError('Invalid ' + expectedType + ' message, bad type: ' + msg);
}
return msg;
}
/**
* Build and return a packet containing the specified message for the
* specified receiver.
*
* Returns encrypted msgpacked bytes, ready to send.
*
* May throw a `ProtocolError`.
*/
protected buildPacket(message: saltyrtc.Message, receiver: Peer, encrypt = true): Uint8Array {
// Choose proper sequence number
let csn: saltyrtc.NextCombinedSequence;
try {
csn = receiver.csnPair.ours.next();
} catch (e) {
throw new ProtocolError('CSN overflow: ' + (e as Error).message);
}
// Create nonce
const nonce = new Nonce(receiver.cookiePair.ours,
csn.overflow, csn.sequenceNumber, this.address, receiver.id);
const nonceBytes = nonce.toUint8Array();
// Encode message
const data: Uint8Array = this.msgpackEncode(message);
// Non encrypted messages can be created by concatenation
if (encrypt === false) {
return concat(nonceBytes, data);
}
// Otherwise, encrypt packet
// TODO: Use polymorphism using peer object (#65)
let box;
if (receiver.id === Signaling.SALTYRTC_ADDR_SERVER) {
box = this.encryptHandshakeDataForServer(data, nonceBytes);
} else if (receiver.id === Signaling.SALTYRTC_ADDR_INITIATOR || isResponderId(receiver.id)) {
box = this.encryptHandshakeDataForPeer(receiver.id, message.type, data, nonceBytes);
} else {
throw new ProtocolError('Bad receiver byte: ' + receiver);
}
return box.toUint8Array();
}
/**
* Encrypt data for the server.
*/
protected encryptHandshakeDataForServer(payload: Uint8Array, nonceBytes: Uint8Array): saltyrtc.Box {
return this.server.sessionSharedKey.encrypt(payload, nonceBytes);
}
/**
* Encrypt data for the specified peer.
*/
protected abstract encryptHandshakeDataForPeer(receiver: number, messageType: string,
payload: Uint8Array, nonceBytes: Uint8Array): saltyrtc.Box;
/**
* Get the peer instance (initiator or responder).
*
* May return null if peer is not yet set.
*/
protected abstract getPeer(): Initiator | Responder;
/**
* If the peer handshake is complete, this will return the incoming and
* outgoing CSN for the authenticated peer. Otherwise, null will be returned.
*/
public getCurrentPeerCsn(): {incoming: number, outgoing: number} {
if (this.getState() !== 'task') {
return null;
}
return {
incoming: this.getPeer().csnPair.theirs,
outgoing: this.getPeer().csnPair.ours.asNumber(),
};
}
/**
* Decrypt data from the peer using the session keys.
*/
public decryptData(box: saltyrtc.Box): Uint8Array {
return this.getPeer().sessionSharedKey.decrypt(box);
}
/**
* Reset/close the connection.
*
* - Close WebSocket if still open
* - Set `this.ws` to `null`
* - Set `this.status` to `new`
* - Reset the server combined sequence
* - Unbind all events
*/
public resetConnection(reason?: number): void {
// Close WebSocket instance
this.closeWebsocket(reason, undefined, true);
// Reset
this.server = new Server();
this.handoverState.reset();
this.setState('new');
if (reason !== undefined) {
this.log.debug(this.logTag, 'Connection reset');
}
// TODO: Close task? (#64)
}
/**
* Initialize the task with the task data sent by the peer.
* Set it as the current task.
*
* @param task The task instance.
* @param data The task data provided by the peer.
* @throws SignalingError
*/
protected initTask(task: saltyrtc.Task, data: saltyrtc.TaskData): void {
try {
task.init(this, data);
} catch (e) {
if (e.name === 'ValidationError') {
throw new ProtocolError('Peer sent invalid task data');
}
throw e;
}
this.task = task;
}
/**
* Decrypt and decode a P2P message, encrypted with the session key.
*
* When `convertErrors` is set to `true`, decryption errors will be
* converted to a `ProtocolError`.
*/
public decryptPeerMessage(box: saltyrtc.Box, convertErrors = true): saltyrtc.Message {
try {
const decrypted = this.getPeer().sessionSharedKey.decrypt(box);
return this.decodeMessage(decrypted, 'peer');
} catch (e) {