-
Notifications
You must be signed in to change notification settings - Fork 1
/
ssh2.js
2113 lines (1829 loc) · 79.8 KB
/
ssh2.js
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
var Util = require("util"),
Events = require("events"),
Net = require("net"),
Utils = require("./util"),
Strtok = require("./../support/node-strtok/lib/strtok"),
BufferReader = require("./../support/BufferReader").BufferReader;
//Execution Bitmap Masks
exports.NET_SSH2_MASK_CONSTRUCTOR = 0x00000001;
exports.NET_SSH2_MASK_LOGIN = 0x00000002;
/**
* Channel constants
*
* RFC4254 refers not to client and server channels but rather to sender and recipient channels. we don't refer
* to them in that way because RFC4254 toggles the meaning. the client sends a SSH_MSG_CHANNEL_OPEN message with
* a sender channel and the server sends a SSH_MSG_CHANNEL_OPEN_CONFIRMATION in response, with a sender and a
* recepient channel. at first glance, you might conclude that SSH_MSG_CHANNEL_OPEN_CONFIRMATION's sender channel
* would be the same thing as SSH_MSG_CHANNEL_OPEN's sender channel, but it's not, per this snipet:
* The 'recipient channel' is the channel number given in the original
* open request, and 'sender channel' is the channel number allocated by
* the other side.
*
* @see ssh2._send_channel_packet()
* @see ssh2._get_channel_packet()
*/
exports.NET_SSH2_CHANNEL_EXEC = 0; // PuTTy uses 0x100
/**
* Returns the message numbers
*/
exports.NET_SSH2_LOG_SIMPLE = 1;
/**
* Returns the message content
*/
exports.NET_SSH2_LOG_COMPLEX = 2;
// message_numbers
exports.NET_SSH2_MSG_DISCONNECT = 1;
exports.NET_SSH2_MSG_IGNORE = 2;
exports.NET_SSH2_MSG_UNIMPLEMENTED = 3;
exports.NET_SSH2_MSG_DEBUG = 4;
exports.NET_SSH2_MSG_SERVICE_REQUEST = 5;
exports.NET_SSH2_MSG_SERVICE_ACCEPT = 6;
exports.NET_SSH2_MSG_KEXINIT = 20;
exports.NET_SSH2_MSG_NEWKEYS = 21;
exports.NET_SSH2_MSG_KEXDH_INIT = 30;
exports.NET_SSH2_MSG_KEXDH_REPLY = 31;
exports.NET_SSH2_MSG_USERAUTH_REQUEST = 50;
exports.NET_SSH2_MSG_USERAUTH_FAILURE = 51;
exports.NET_SSH2_MSG_USERAUTH_SUCCESS = 52;
exports.NET_SSH2_MSG_USERAUTH_BANNER = 53;
exports.NET_SSH2_MSG_GLOBAL_REQUEST = 80;
exports.NET_SSH2_MSG_REQUEST_SUCCESS = 81;
exports.NET_SSH2_MSG_REQUEST_FAILURE = 82;
exports.NET_SSH2_MSG_CHANNEL_OPEN = 90;
exports.NET_SSH2_MSG_CHANNEL_OPEN_CONFIRMATION = 91;
exports.NET_SSH2_MSG_CHANNEL_OPEN_FAILURE = 92;
exports.NET_SSH2_MSG_CHANNEL_WINDOW_ADJUST = 93;
exports.NET_SSH2_MSG_CHANNEL_DATA = 94;
exports.NET_SSH2_MSG_CHANNEL_EXTENDED_DATA = 95;
exports.NET_SSH2_MSG_CHANNEL_EOF = 96;
exports.NET_SSH2_MSG_CHANNEL_CLOSE = 97;
exports.NET_SSH2_MSG_CHANNEL_REQUEST = 98;
exports.NET_SSH2_MSG_CHANNEL_SUCCESS = 99;
exports.NET_SSH2_MSG_CHANNEL_FAILURE = 100;
// disconnect_reasons
exports.NET_SSH2_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT = 1;
exports.NET_SSH2_DISCONNECT_PROTOCOL_ERROR = 2;
exports.NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED = 3;
exports.NET_SSH2_DISCONNECT_RESERVED = 4;
exports.NET_SSH2_DISCONNECT_MAC_ERROR = 5;
exports.NET_SSH2_DISCONNECT_COMPRESSION_ERROR = 6;
exports.NET_SSH2_DISCONNECT_SERVICE_NOT_AVAILABLE = 7;
exports.NET_SSH2_DISCONNECT_PROTOCOL_VERSION_NOT_SUPPORTED = 8;
exports.NET_SSH2_DISCONNECT_HOST_KEY_NOT_VERIFIABLE = 9;
exports.NET_SSH2_DISCONNECT_CONNECTION_LOST = 10;
exports.NET_SSH2_DISCONNECT_BY_APPLICATION = 11;
exports.NET_SSH2_DISCONNECT_TOO_MANY_CONNECTIONS = 12;
exports.NET_SSH2_DISCONNECT_AUTH_CANCELLED_BY_USER = 13;
exports.NET_SSH2_DISCONNECT_NO_MORE_AUTH_METHODS_AVAILABLE = 14;
exports.NET_SSH2_DISCONNECT_ILLEGAL_USER_NAME = 15;
// channel_open_failure_reasons
exports.NET_SSH2_OPEN_ADMINISTRATIVELY_PROHIBITED = 1;
// terminal_modes
exports.NET_SSH2_TTY_OP_END = 0;
// channel_extended_data_type_codes
exports.NET_SSH2_EXTENDED_DATA_STDERR = 1;
exports.NET_SSH2_MSG_USERAUTH_PASSWD_CHANGEREQ = 60;
exports.NET_SSH2_MSG_USERAUTH_PK_OK = 60;
// logging constants:
exports.NET_SSH2_LOG_NONE = 0;
exports.NET_SSH2_LOG_SIMPLE = 1;
exports.NET_SSH2_LOG_COMPLEX = 2;
exports.NET_SSH2_LOGGING = exports.NET_SSH2_LOG_NONE;
/**
* Default Constructor.
* Connects to an SSHv2 server
*
* @param String host
* @param optional Number port
* @param optional Number timeout
* @return Net_SSH2
*/
function ssh2(host, port, timeout) {
Events.EventEmitter.call(this);
this.host = host;
this.port = port || 22;
this.timeout = timeout || 10000;
}
Util.inherits(ssh2, Events.EventEmitter);
(function() {
/**
* The SSH identifier
*
* @var String
*/
this.identifier = "SSH-2.0-node_ssh_0.1";
/**
* The Socket Object
*
* @var Object
*/
this.fsock;
/**
* Execution Bitmap
*
* The bits that are set reprsent functions that have been called already.
* This is used to determine if a requisite function has been successfully executed.
* If not, an error should be thrown.
*
* @var Number
*/
this.bitmap = 0;
/**
* Error information
*
* @see ssh2.getErrors()
* @see ssh2.getLastError()
* @var String
*/
this.errors = [];
/**
* Server Identifier
*
* @see ssh2.getServerIdentification()
* @var String
*/
this.server_identifier = "";
/**
* Key Exchange Algorithms
*
* @see ssh2.getKexAlgorithims()
* @var Array
*/
this.kex_algorithms;
/**
* Server Host Key Algorithms
*
* @see ssh2.getServerHostKeyAlgorithms()
* @var Array
*/
this.server_host_key_algorithms;
/**
* Encryption Algorithms: Client to Server
*
* @see ssh2.getEncryptionAlgorithmsClient2Server()
* @var Array
*/
this.encryption_algorithms_client_to_server;
/**
* Encryption Algorithms: Server to Client
*
* @see ssh2.getEncryptionAlgorithmsServer2Client()
* @var Array
*/
this.encryption_algorithms_server_to_client;
/**
* MAC Algorithms: Client to Server
*
* @see ssh2.getMACAlgorithmsClient2Server()
* @var Array
*/
this.mac_algorithms_client_to_server;
/**
* MAC Algorithms: Server to Client
*
* @see ssh2.getMACAlgorithmsServer2Client()
* @var Array
*/
this.mac_algorithms_server_to_client;
/**
* Compression Algorithms: Client to Server
*
* @see ssh2.getCompressionAlgorithmsClient2Server()
* @var Array
*/
this.compression_algorithms_client_to_server;
/**
* Compression Algorithms: Server to Client
*
* @see ssh2.getCompressionAlgorithmsServer2Client()
* @var Array
*/
this.compression_algorithms_server_to_client;
/**
* Languages: Server to Client
*
* @see ssh2.getLanguagesServer2Client()
* @var Array
*/
this.languages_server_to_client;
/**
* Languages: Client to Server
*
* @see ssh2.getLanguagesClient2Server()
* @var Array
*/
this.languages_client_to_server;
/**
* Block Size for Server to Client Encryption
*
* "Note that the length of the concatenation of 'packet_length',
* 'padding_length', 'payload', and 'random padding' MUST be a multiple
* of the cipher block size or 8, whichever is larger. This constraint
* MUST be enforced, even when using stream ciphers."
*
* -- http://tools.ietf.org/html/rfc4253#section-6
*
* @see ssh2.Net_SSH2()
* @see ssh2._send_binary_packet()
* @var Number
*/
this.encrypt_block_size = 8;
/**
* Block Size for Client to Server Encryption
*
* @see ssh2.Net_SSH2()
* @see ssh2._get_binary_packet()
* @var Number
*/
this.decrypt_block_size = 8;
/**
* Server to Client Encryption Object
*
* @see ssh2._get_binary_packet()
* @var Object
*/
this.decrypt = null;
/**
* Client to Server Encryption Object
*
* @see ssh2._send_binary_packet()
* @var Object
*/
this.encrypt = null;
/**
* Client to Server HMAC Object
*
* @see ssh2._send_binary_packet()
* @var Object
*/
this.hmac_create = null;
/**
* Server to Client HMAC Object
*
* @see ssh2._get_binary_packet()
* @var Object
*/
this.hmac_check = null;
/**
* Size of server to client HMAC
*
* We need to know how big the HMAC will be for the server to client direction
* so that we know how many bytes to read. For the client to server side, the
* HMAC object will make the HMAC as long as it needs to be. All we need to do is
* append it.
*
* @see ssh2._get_binary_packet()
* @var Number
*/
this.hmac_size = null;
/**
* Server Public Host Key
*
* @see ssh2.getServerPublicHostKey()
* @var String
*/
this.server_public_host_key;
/**
* Session identifer
*
* "The exchange hash H from the first key exchange is additionally
* used as the session identifier, which is a unique identifier for
* this connection."
*
* -- http://tools.ietf.org/html/rfc4253#section-7.2
*
* @see ssh2._key_exchange()
* @var String
*/
this.session_id = false;
/**
* Message Numbers
*
* @var Array
*/
this.message_numbers = [];
/**
* Disconnection Message 'reason codes' defined in RFC4253
*
* @var Array
*/
this.disconnect_reasons = [];
/**
* SSH_MSG_CHANNEL_OPEN_FAILURE 'reason codes', defined in RFC4254
*
* @var Array
*/
this.channel_open_failure_reasons = [];
/**
* Terminal Modes
*
* @link http://tools.ietf.org/html/rfc4254#section-8
* @var Array
*/
this.terminal_modes = [];
/**
* SSH_MSG_CHANNEL_EXTENDED_DATA's data_type_codes
*
* @link http://tools.ietf.org/html/rfc4254#section-5.2
* @var Array
*/
this.channel_extended_data_type_codes = [];
/**
* Send Sequence Number
* See 'Section 6.4. Data Integrity' of rfc4253 for more info.
*
* @see ssh2._send_binary_packet()
* @var Number
*/
this.send_seq_no = 0;
/**
* Get Sequence Number
* See 'Section 6.4. Data Integrity' of rfc4253 for more info.
*
* @see ssh2._get_binary_packet()
* @var Number
*/
this.get_seq_no = 0;
/**
* Server Channels
* Maps client channels to server channels
*
* @see ssh2._get_channel_packet()
* @see ssh2.exec()
* @var Array
*/
this.server_channels = [];
/**
* Channel Buffers
* If a client requests a packet from one channel but receives two packets from
* another those packets should be placed in a buffer
*
* @see ssh2._get_channel_packet()
* @see ssh2.exec()
* @var Array
*/
this.channel_buffers = [];
/**
* Channel Status
* Contains the type of the last sent message
*
* @see ssh2._get_channel_packet()
* @var Array
*/
this.channel_status = [];
/**
* Packet Size
* Maximum packet size indexed by channel
*
* @see ssh2._send_channel_packet()
* @var Array
*/
this.packet_size_client_to_server = [];
/**
* Message Number Log
*
* @see ssh2.getLog()
* @var Array
*/
this.message_number_log = [];
/**
* Message Log
*
* @see ssh2.getLog()
* @var Array
*/
this.message_log = [];
/**
* The Window Size
* Bytes the other party can send before it must wait for the window to be
* adjusted (0x7FFFFFFF = 4GB)
*
* @var Number
* @see ssh2._send_channel_packet()
* @see ssh2.exec()
*/
this.window_size = 0x7FFFFFFF;
/**
* Window size
* Window size indexed by channel
*
* @see ssh2._send_channel_packet()
* @var Array
*/
this.window_size_client_to_server = [];
/**
* Server signature
* Verified against this.session_id
*
* @see ssh2.getServerPublicHostKey()
* @var String
*/
this.signature = "";
/**
* Server signature format
*
* ssh-rsa or ssh-dss.
*
* @see ssh2.getServerPublicHostKey()
* @var String
*/
this.signature_format = "";
this.listen_callbacks = [];
this.connect = function(cbconnect) {
var _self = this;
this.fsock = Net.createConnection(this.port, this.host);
this.fsock.setTimeout(this.timeout);
//this.fsock.setNoDelay();
this.fsock.addListener("connect", function() {
var connected = false,
authenticated = false,
extra = "";
console.log("listening...");
//_self.buffer = new Utils.Buffy();
_self.fsock.addListener("data", function listener(data) {
/* According to the SSH2 specs,
"The server MAY send other lines of data before sending the version
string. Each line SHOULD be terminated by a Carriage Return and Line
Feed. Such lines MUST NOT begin with "SSH-", and SHOULD be encoded
in ISO-10646 UTF-8 [RFC3629] (language is not specified). Clients
MUST be able to process such lines." */
if (!connected) {
var temp = data.toString(),
matches = temp.match(/^SSH-(\d\.\d+)/);
if (!matches)
return extra += temp;
if (exports.NET_SSH2_LOGGING) {
_self.message_number_log.push("<-", "->");
if (exports.NET_SSH2_LOGGING == exports.NET_SSH2_LOG_COMPLEX)
_self.message_log.push(temp, _self.identifier + "\r\n");
}
_self.server_identifier = temp.replace(/[\r\n]+/, "");
if (extra)
_self.errors.push(extra);
if (matches[1] != "1.99" && matches[1] != "2.0")
return cbconnect("Cannot connect to SSH " + $matches[1] +" servers");
_self.fsock.write(_self.identifier + "\r\n");
connected = true;
_self.fsock.removeListener("data", listener);
_self._get_binary_packet(function(response) {
});
_self._listen();
}
else {
if (false){
if (!_self.buffer)
_self.buffer = data;
else
_self.buffer.addChunk(data);
console.log("buffer expanded", _self.buffer.length);
if (!authenticated) {
var response = _self._get_binary_packet();
if (response === false)
return cbconnect("Connection closed by server");
console.log("RES", typeof response, response);
/*return;
if (response[0].charCodeAt(0) != exports.NET_SSH2_MSG_KEXINIT)
return cbconnect("Expected SSH_MSG_KEXINIT");
if (!_self._key_exchange(response))
return cbconnect("No supported authentication method found");*/
_self.bitmap = exports.NET_SSH2_MASK_CONSTRUCTOR;
authenticated = true;
cbconnect(null, _self);
}
}
}
console.log("data: ", data + "--END--");
});
});
this.fsock.addListener("error", function(err) {
cbconnect("Cannot connect to host: " + err);
_self.fsock.destroy();
});
this.fsock.addListener("timeout", function(err) {
_self.fsock.end();
});
};
/**
* Disconnect
*/
this.disconnect = function() {
this._disconnect(exports.NET_SSH2_DISCONNECT_BY_APPLICATION);
};
/**
* Disconnect
*
* @param Integer reason
* @return Boolean
*/
this._disconnect = function(reason) {
if (this.bitmap) {
data = pack("CNNa*Na*", exports.NET_SSH2_MSG_DISCONNECT, reason, 0, "", 0, "");
this._send_binary_packet(data);
this.bitmap = 0;
fclose(this.fsock);
return false;
}
};
/**
* Key Exchange
*
* @param String $kexinit_payload_server
* @access private
*/
function _key_exchange(kexinit_payload_server)
{
var kex_algorithms = [
"diffie-hellman-group1-sha1", // REQUIRED
"diffie-hellman-group14-sha1" // REQUIRED
];
var server_host_key_algorithms = [
"ssh-rsa", // RECOMMENDED sign Raw RSA Key
"ssh-dss" // REQUIRED sign Raw DSS Key
];
var encryption_algorithms = [
// from <http://tools.ietf.org/html/rfc4345#section-4>:
"arcfour256",
"arcfour128",
"arcfour", // OPTIONAL the ARCFOUR stream cipher with a 128-bit key
"aes128-cbc", // RECOMMENDED AES with a 128-bit key
"aes192-cbc", // OPTIONAL AES with a 192-bit key
"aes256-cbc", // OPTIONAL AES in CBC mode, with a 256-bit key
// from <http://tools.ietf.org/html/rfc4344#section-4>:
"aes128-ctr", // RECOMMENDED AES (Rijndael) in SDCTR mode, with 128-bit key
"aes192-ctr", // RECOMMENDED AES with 192-bit key
"aes256-ctr", // RECOMMENDED AES with 256-bit key
"3des-ctr", // RECOMMENDED Three-key 3DES in SDCTR mode
"3des-cbc", // REQUIRED three-key 3DES in CBC mode
"none" // OPTIONAL no encryption; NOT RECOMMENDED
];
var mac_algorithms = [
"hmac-sha1-96", // RECOMMENDED first 96 bits of HMAC-SHA1 (digest length = 12, key length = 20)
"hmac-sha1", // REQUIRED HMAC-SHA1 (digest length = key length = 20)
"hmac-md5-96", // OPTIONAL first 96 bits of HMAC-MD5 (digest length = 12, key length = 16)
"hmac-md5", // OPTIONAL HMAC-MD5 (digest length = key length = 16)
"none" // OPTIONAL no MAC; NOT RECOMMENDED
];
var compression_algorithms = [
"none" // REQUIRED no compression
//"zlib" // OPTIONAL ZLIB (LZ77) compression
];
var str_kex_algorithms, str_server_host_key_algorithms,
encryption_algorithms_server_to_client, mac_algorithms_server_to_client, compression_algorithms_server_to_client,
encryption_algorithms_client_to_server, mac_algorithms_client_to_server, compression_algorithms_client_to_server;
if (empty(str_kex_algorithms)) {
str_kex_algorithms = implode(",", kex_algorithms);
str_server_host_key_algorithms = implode(",", server_host_key_algorithms);
encryption_algorithms_server_to_client = encryption_algorithms_client_to_server = implode(",", encryption_algorithms);
mac_algorithms_server_to_client = mac_algorithms_client_to_server = implode(",", mac_algorithms);
compression_algorithms_server_to_client = compression_algorithms_client_to_server = implode(",", compression_algorithms);
}
client_cookie = "";
for (i = 0; i < 16; i++) {
client_cookie += chr(crypt_random(0, 255));
}
response = kexinit_payload_server;
this._string_shift(response, 1); // skip past the message number (it should be SSH_MSG_KEXINIT)
server_cookie = this._string_shift(response, 16);
temp = unpack("Nlength", this._string_shift(response, 4));
this.kex_algorithms = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.server_host_key_algorithms = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.encryption_algorithms_client_to_server = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.encryption_algorithms_server_to_client = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.mac_algorithms_client_to_server = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.mac_algorithms_server_to_client = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.compression_algorithms_client_to_server = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.compression_algorithms_server_to_client = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.languages_client_to_server = explode(",", this._string_shift(response, temp["length"]));
temp = unpack("Nlength", this._string_shift(response, 4));
this.languages_server_to_client = explode(",", this._string_shift(response, temp["length"]));
extract(unpack("Cfirst_kex_packet_follows", this._string_shift(response, 1)));
first_kex_packet_follows = first_kex_packet_follows != 0;
// the sending of SSH2_MSG_KEXINIT could go in one of two places. this is the second place.
kexinit_payload_client = pack("Ca*Na*Na*Na*Na*Na*Na*Na*Na*Na*Na*CN",
NET_SSH2_MSG_KEXINIT, client_cookie, strlen(str_kex_algorithms), str_kex_algorithms,
strlen(str_server_host_key_algorithms), str_server_host_key_algorithms, strlen(encryption_algorithms_client_to_server),
encryption_algorithms_client_to_server, strlen(encryption_algorithms_server_to_client), encryption_algorithms_server_to_client,
strlen(mac_algorithms_client_to_server), mac_algorithms_client_to_server, strlen(mac_algorithms_server_to_client),
mac_algorithms_server_to_client, strlen(compression_algorithms_client_to_server), compression_algorithms_client_to_server,
strlen(compression_algorithms_server_to_client), compression_algorithms_server_to_client, 0, "", 0, "",
0, 0
);
if (!this._send_binary_packet(kexinit_payload_client)) {
return false;
}
// here ends the second place.
// we need to decide upon the symmetric encryption algorithms before we do the diffie-hellman key exchange
for (i = 0; i < count(encryption_algorithms) && !in_array(encryption_algorithms[i], this.encryption_algorithms_server_to_client); i++);
if (i == count(encryption_algorithms)) {
user_error("No compatible server to client encryption algorithms found", E_USER_NOTICE);
return this._disconnect(NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
// we don"t initialize any crypto-objects, yet - we do that, later. for now, we need the lengths to make the
// diffie-hellman key exchange as fast as possible
decrypt = encryption_algorithms[i];
switch (decrypt) {
case "3des-cbc":
case "3des-ctr":
decryptKeyLength = 24; // eg. 192 / 8
break;
case "aes256-cbc":
case "aes256-ctr":
decryptKeyLength = 32; // eg. 256 / 8
break;
case "aes192-cbc":
case "aes192-ctr":
decryptKeyLength = 24; // eg. 192 / 8
break;
case "aes128-cbc":
case "aes128-ctr":
decryptKeyLength = 16; // eg. 128 / 8
break;
case "arcfour":
case "arcfour128":
decryptKeyLength = 16; // eg. 128 / 8
break;
case "arcfour256":
decryptKeyLength = 32; // eg. 128 / 8
break;
case "none":
decryptKeyLength = 0;
break;
}
for (i = 0; i < count(encryption_algorithms) && !in_array(encryption_algorithms[i], this.encryption_algorithms_client_to_server); i++);
if (i == count(encryption_algorithms)) {
user_error("No compatible client to server encryption algorithms found", E_USER_NOTICE);
return this._disconnect(exports.NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
encrypt = encryption_algorithms[i];
switch (encrypt) {
case "3des-cbc":
case "3des-ctr":
encryptKeyLength = 24;
break;
case "aes256-cbc":
case "aes256-ctr":
encryptKeyLength = 32;
break;
case "aes192-cbc":
case "aes192-ctr":
encryptKeyLength = 24;
break;
case "aes128-cbc":
case "aes128-ctr":
encryptKeyLength = 16;
break;
case "arcfour":
case "arcfour128":
encryptKeyLength = 16;
break;
case "arcfour256":
encryptKeyLength = 32;
break;
case "none":
encryptKeyLength = 0;
break;
}
keyLength = decryptKeyLength > encryptKeyLength ? decryptKeyLength : encryptKeyLength;
// through diffie-hellman key exchange a symmetric key is obtained
for (i = 0; i < count(kex_algorithms) && !in_array(kex_algorithms[i], this.kex_algorithms); i++);
if (i == count(kex_algorithms)) {
user_error("No compatible key exchange algorithms found", E_USER_NOTICE);
return this._disconnect(exports.NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
switch (kex_algorithms[i]) {
// see http://tools.ietf.org/html/rfc2409#section-6.2 and
// http://tools.ietf.org/html/rfc2412, appendex E
case "diffie-hellman-group1-sha1":
p = pack("H256", "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74" +
"020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437" +
"4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF");
keyLength = keyLength < 160 ? keyLength : 160;
hash = "sha1";
break;
// see http://tools.ietf.org/html/rfc3526#section-3
case "diffie-hellman-group14-sha1":
p = pack("H512", "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74" +
"020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437" +
"4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED" +
"EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE45B3DC2007CB8A163BF05" +
"98DA48361C55D39A69163FA8FD24CF5F83655D23DCA3AD961C62F356208552BB" +
"9ED529077096966D670C354E4ABC9804F1746C08CA18217C32905E462E36CE3B" +
"E39E772C180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718" +
"3995497CEA956AE515D2261898FA051015728E5A8AACAA68FFFFFFFFFFFFFFFF");
keyLength = keyLength < 160 ? keyLength : 160;
hash = "sha1";
}
p = new Math_BigInteger(p, 256);
//q = p.bitwise_rightShift(1);
/* To increase the speed of the key exchange, both client and server may
reduce the size of their private exponents. It should be at least
twice as long as the key material that is generated from the shared
secret. For more details, see the paper by van Oorschot and Wiener
[VAN-OORSCHOT].
-- http://tools.ietf.org/html/rfc4419#section-6.2 */
q = new Math_BigInteger(1);
q = q.bitwise_leftShift(2 * keyLength);
q = q.subtract(new Math_BigInteger(1));
g = new Math_BigInteger(2);
x = new Math_BigInteger();
x.setRandomGenerator("crypt_random");
x = x.random(new Math_BigInteger(1), q);
e = g.modPow(x, p);
eBytes = e.toBytes(true);
data = pack("CNa*", exports.NET_SSH2_MSG_KEXDH_INIT, strlen(eBytes), eBytes);
if (!this._send_binary_packet(data)) {
user_error("Connection closed by server", E_USER_NOTICE);
return false;
}
response = this._get_binary_packet();
if (response === false) {
user_error("Connection closed by server", E_USER_NOTICE);
return false;
}
extract(unpack("Ctype", this._string_shift(response, 1)));
if (type != NET_SSH2_MSG_KEXDH_REPLY) {
user_error("Expected SSH_MSG_KEXDH_REPLY", E_USER_NOTICE);
return false;
}
temp = unpack("Nlength", this._string_shift(response, 4));
this.server_public_host_key = server_public_host_key = this._string_shift(response, temp["length"]);
temp = unpack("Nlength", this._string_shift(server_public_host_key, 4));
public_key_format = this._string_shift(server_public_host_key, temp["length"]);
temp = unpack("Nlength", this._string_shift(response, 4));
fBytes = this._string_shift(response, temp["length"]);
f = new Math_BigInteger(fBytes, -256);
temp = unpack("Nlength", this._string_shift(response, 4));
this.signature = this._string_shift(response, temp["length"]);
temp = unpack("Nlength", this._string_shift(this.signature, 4));
this.signature_format = this._string_shift(this.signature, temp["length"]);
key = f.modPow(x, p);
keyBytes = key.toBytes(true);
if (this.session_id === false) {
source = pack("Na*Na*Na*Na*Na*Na*Na*Na*",
strlen(this.identifier), this.identifier, strlen(this.server_identifier), this.server_identifier,
strlen(kexinit_payload_client), kexinit_payload_client, strlen(kexinit_payload_server),
kexinit_payload_server, strlen(this.server_public_host_key), this.server_public_host_key, strlen(eBytes),
eBytes, strlen(fBytes), fBytes, strlen(keyBytes), keyBytes
);
source = pack("H*", hash(source));
this.session_id = source;
}
for (i = 0; i < count(server_host_key_algorithms) && !in_array(server_host_key_algorithms[i], this.server_host_key_algorithms); i++);
if (i == count(server_host_key_algorithms)) {
user_error("No compatible server host key algorithms found", E_USER_NOTICE);
return this._disconnect(exports.NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
if (public_key_format != server_host_key_algorithms[i] || this.signature_format != server_host_key_algorithms[i]) {
user_error("Sever Host Key Algorithm Mismatch", E_USER_NOTICE);
return this._disconnect(exports.NET_SSH2_DISCONNECT_KEY_EXCHANGE_FAILED);
}
packet = pack("C",
exports.NET_SSH2_MSG_NEWKEYS
);
if (!this._send_binary_packet(packet)) {
return false;
}
response = this._get_binary_packet();
if (response === false) {
user_error("Connection closed by server", E_USER_NOTICE);
return false;
}
extract(unpack("Ctype", this._string_shift(response, 1)));
if (type != exports.NET_SSH2_MSG_NEWKEYS) {
user_error("Expected SSH_MSG_NEWKEYS", E_USER_NOTICE);
return false;
}
switch (encrypt) {
case "3des-cbc":
this.encrypt = new Crypt_TripleDES();
// this.encrypt_block_size = 64 / 8 == the default
break;
case "3des-ctr":
this.encrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
// this.encrypt_block_size = 64 / 8 == the default
break;
case "aes256-cbc":
case "aes192-cbc":
case "aes128-cbc":
this.encrypt = new Crypt_AES();
this.encrypt_block_size = 16; // eg. 128 / 8
break;
case "aes256-ctr":
case "aes192-ctr":
case "aes128-ctr":
this.encrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
this.encrypt_block_size = 16; // eg. 128 / 8
break;
case "arcfour":
case "arcfour128":
case "arcfour256":
this.encrypt = new Crypt_RC4();
break;
case "none":
//this.encrypt = new Crypt_Null();
break;
}
switch (decrypt) {
case "3des-cbc":
this.decrypt = new Crypt_TripleDES();
break;
case "3des-ctr":
this.decrypt = new Crypt_TripleDES(CRYPT_DES_MODE_CTR);
break;
case "aes256-cbc":
case "aes192-cbc":
case "aes128-cbc":
this.decrypt = new Crypt_AES();
this.decrypt_block_size = 16;
break;
case "aes256-ctr":
case "aes192-ctr":
case "aes128-ctr":
this.decrypt = new Crypt_AES(CRYPT_AES_MODE_CTR);
this.decrypt_block_size = 16;
break;
case "arcfour":
case "arcfour128":
case "arcfour256":
this.decrypt = new Crypt_RC4();
break;
case "none":
//this.decrypt = new Crypt_Null();
break;
}
keyBytes = pack("Na*", strlen(keyBytes), keyBytes);
if (this.encrypt) {