Subversion Repositories Kolibri OS

Rev

Rev 6419 | Rev 6922 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6419 hidnplayr 1
;    ssh.asm - SSH client for KolibriOS
2
;
3
;    Copyright (C) 2015-2016 Jeffrey Amelynck
4
;
5
;    This program is free software: you can redistribute it and/or modify
6
;    it under the terms of the GNU General Public License as published by
7
;    the Free Software Foundation, either version 3 of the License, or
8
;    (at your option) any later version.
9
;
10
;    This program is distributed in the hope that it will be useful,
11
;    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
;    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
;    GNU General Public License for more details.
14
;
15
;    You should have received a copy of the GNU General Public License
16
;    along with this program.  If not, see .
17
 
18
format binary as ""
19
 
20
__DEBUG__       = 1
6469 hidnplayr 21
__DEBUG_LEVEL__ = 2             ; 1: Extreme debugging, 2: Debugging, 3: Errors only
6419 hidnplayr 22
 
23
BUFFERSIZE      = 4096
24
MAX_BITS        = 8192
25
 
26
DH_PRIVATE_KEY_SIZE     = 256
27
 
28
use32
29
 
30
        db      'MENUET01'      ; signature
31
        dd      1               ; header version
32
        dd      start           ; entry point
33
        dd      i_end           ; initialized size
34
        dd      mem+4096        ; required memory
35
        dd      mem+4096        ; stack pointer
6469 hidnplayr 36
        dd      params          ; parameters
6419 hidnplayr 37
        dd      0               ; path
38
 
39
include '../../macros.inc'
6469 hidnplayr 40
;include '../../struct.inc'
6419 hidnplayr 41
purge mov,add,sub
42
include '../../proc32.inc'
43
include '../../dll.inc'
44
include '../../debug-fdo.inc'
45
include '../../network.inc'
6469 hidnplayr 46
include '../../develop/libraries/libcrash/trunk/libcrash.inc'
6419 hidnplayr 47
 
48
include 'mcodes.inc'
49
include 'ssh_transport.inc'
50
include 'dh_gex.inc'
51
 
52
include 'mpint.inc'
53
include 'random.inc'
54
include 'aes256.inc'
55
include 'aes256-ctr.inc'
56
include 'aes256-cbc.inc'
6469 hidnplayr 57
include 'hmac_sha256.inc'
6419 hidnplayr 58
 
59
; macros for network byte order
60
macro dd_n op {
61
   dd 0 or (((op) and 0FF000000h) shr 24) or \
62
           (((op) and 000FF0000h) shr  8) or \
63
           (((op) and 00000FF00h) shl  8) or \
64
           (((op) and 0000000FFh) shl 24)
65
}
66
 
67
macro dw_n op {
68
   dw 0 or (((op) and 0FF00h) shr 8) or \
69
           (((op) and 000FFh) shl 8)
70
}
71
 
6469 hidnplayr 72
proc dump_hex _ptr, _length
73
if __DEBUG_LEVEL__ <= 1
74
        pushad
75
 
76
        mov     esi, [_ptr]
77
        mov     ecx, [_length]
78
  .next_dword:
79
        lodsd
80
        bswap   eax
81
        DEBUGF  1,'%x',eax
82
        loop    .next_dword
83
        DEBUGF  1,'\n'
84
 
85
        popad
86
        ret
87
end if
88
endp
89
 
90
struct  ssh_connection
91
 
92
; Connection
93
 
94
        hostname                rb 1024
95
 
96
        socketnum               dd ?
97
 
98
        sockaddr                dw ?            ; Address family
99
        port                    dw ?
100
        ip                      dd ?
101
                                rb 10
102
 
103
; Encryption/Decryption
104
 
105
        rx_crypt_proc           dd ?
106
        tx_crypt_proc           dd ?
107
        rx_crypt_ctx_ptr        dd ?
108
        tx_crypt_ctx_ptr        dd ?
109
        rx_crypt_blocksize      dd ?
110
        tx_crypt_blocksize      dd ?
111
 
112
; Message authentication
113
 
114
        rx_mac_proc             dd ?
115
        tx_mac_proc             dd ?
116
        rx_mac_ctx              hmac_sha256_context
117
        tx_mac_ctx              hmac_sha256_context
118
        rx_mac_length           dd ?
119
        tx_mac_length           dd ?
120
 
121
; Buffers
122
 
123
        rx_seq                  dd ?            ; Packet sequence number for MAC
124
        rx_buffer               ssh_packet_header
125
                                rb BUFFERSIZE-sizeof.ssh_packet_header
126
 
127
        tx_seq                  dd ?            ; Packet sequence number for MAC
128
        tx_buffer               ssh_packet_header
129
                                rb BUFFERSIZE-sizeof.ssh_packet_header
130
 
131
        send_data               dw ?
132
 
133
; Output from key exchange
134
        dh_K                    dd ?            ; Shared Secret (Big endian)
135
                                rb MAX_BITS/8
136
        dh_K_length             dd ?            ; Length in little endian
137
 
138
        dh_H                    rb 32           ; Exchange Hash
139
        session_id_prefix       db ?
140
        session_id              rb 32
141
        rx_iv                   rb 32           ; Rx initialisation vector
142
        tx_iv                   rb 32           ; Tx initialisation vector
143
        rx_enc_key              rb 32           ; Rx encryption key
144
        tx_enc_key              rb 32           ; Tx encryption key
145
        rx_int_key              rb 32           ; Rx integrity key
146
        tx_int_key              rb 32           ; Tx integrity key
147
 
148
; Diffie Hellman
149
        dh_p                    dd ?
150
                                rb MAX_BITS/8
151
        dh_g                    dd ?
152
                                rb MAX_BITS/8
153
        dh_x                    dd ?
154
                                rb MAX_BITS/8
155
        dh_e                    dd ?
156
                                rb MAX_BITS/8
157
        dh_f                    dd ?
158
                                rb MAX_BITS/8
159
 
160
        dh_signature            dd ?
161
                                rb MAX_BITS/8
162
 
163
        temp_ctx                ctx_sha224256
164
        k_h_ctx                 ctx_sha224256
165
 
166
ends
167
 
6419 hidnplayr 168
start:
169
        mcall   68, 11          ; Init heap
170
 
6469 hidnplayr 171
        DEBUGF  2, "SSH: Loading libraries\n"
6419 hidnplayr 172
        stdcall dll.Load, @IMPORT
173
        test    eax, eax
174
        jnz     exit
175
 
6469 hidnplayr 176
        DEBUGF  2, "SSH: Init PRNG\n"
6419 hidnplayr 177
        call    init_random
178
 
6469 hidnplayr 179
        DEBUGF  2, "SSH: Init Console\n"
6419 hidnplayr 180
        invoke  con_start, 1
181
        invoke  con_init, 80, 25, 80, 25, title
182
 
6469 hidnplayr 183
; Check for parameters TODO
184
;        cmp     byte[params], 0
185
;        jne     resolve
6419 hidnplayr 186
 
187
main:
188
        invoke  con_cls
189
; Welcome user
190
        invoke  con_write_asciiz, str1
191
 
192
prompt:
193
; write prompt
194
        invoke  con_write_asciiz, str2
195
; read string
6469 hidnplayr 196
        mov     esi, con.hostname
6419 hidnplayr 197
        invoke  con_gets, esi, 256
198
; check for exit
199
        test    eax, eax
200
        jz      done
201
        cmp     byte[esi], 10
202
        jz      done
203
 
204
resolve:
6469 hidnplayr 205
        mov     [con.sockaddr], AF_INET4
206
        mov     [con.port], 22 shl 8
6419 hidnplayr 207
 
208
; delete terminating '\n'
6469 hidnplayr 209
        mov     esi, con.hostname
6419 hidnplayr 210
  @@:
211
        lodsb
212
        cmp     al, ':'
213
        je      .do_port
214
        cmp     al, 0x20
215
        ja      @r
216
        mov     byte[esi-1], 0
217
        jmp     .done
218
 
219
  .do_port:
220
        xor     eax, eax
221
        xor     ebx, ebx
222
        mov     byte[esi-1], 0
223
  .portloop:
224
        lodsb
225
        cmp     al, 0x20
226
        jbe     .port_done
227
        sub     al, '0'
228
        jb      hostname_error
229
        cmp     al, 9
230
        ja      hostname_error
6469 hidnplayr 231
        lea     ebx, [ebx*4+ebx]
6419 hidnplayr 232
        shl     ebx, 1
233
        add     ebx, eax
234
        jmp     .portloop
235
 
236
  .port_done:
237
        xchg    bl, bh
6469 hidnplayr 238
        mov     [con.port], bx
6419 hidnplayr 239
 
240
  .done:
241
 
242
; resolve name
243
        push    esp     ; reserve stack place
244
        push    esp
6469 hidnplayr 245
        invoke  getaddrinfo, con.hostname, 0, 0
6419 hidnplayr 246
        pop     esi
247
; test for error
248
        test    eax, eax
249
        jnz     dns_error
250
 
251
        invoke  con_cls
252
        invoke  con_write_asciiz, str3
6469 hidnplayr 253
        invoke  con_write_asciiz, con.hostname
6419 hidnplayr 254
 
255
; write results
256
        invoke  con_write_asciiz, str8
257
 
258
; convert IP address to decimal notation
259
        mov     eax, [esi+addrinfo.ai_addr]
260
        mov     eax, [eax+sockaddr_in.sin_addr]
6469 hidnplayr 261
        mov     [con.ip], eax
6419 hidnplayr 262
        invoke  inet_ntoa, eax
263
; write result
264
        invoke  con_write_asciiz, eax
265
; free allocated memory
266
        invoke  freeaddrinfo, esi
267
 
268
        invoke  con_write_asciiz, str9
269
 
270
        mcall   40, EVM_STACK + EVM_KEY
271
        invoke  con_cls
272
 
273
; Create socket
274
        mcall   socket, AF_INET4, SOCK_STREAM, 0
275
        cmp     eax, -1
276
        jz      socket_err
6469 hidnplayr 277
        mov     [con.socketnum], eax
6419 hidnplayr 278
 
279
; Connect
6469 hidnplayr 280
        DEBUGF  2, "Connecting to server\n"
281
        mcall   connect, [con.socketnum], con.sockaddr, 18
6419 hidnplayr 282
        test    eax, eax
283
        jnz     socket_err
284
 
6469 hidnplayr 285
; Start calculating hash
286
        invoke  sha256_init, con.temp_ctx
6419 hidnplayr 287
; HASH: string  V_C, the client's version string (CR and NL excluded)
6469 hidnplayr 288
        invoke  sha256_update, con.temp_ctx, ssh_ident_ha, ssh_ident.length+4-2
6419 hidnplayr 289
 
6469 hidnplayr 290
; >> Send our identification string
291
        DEBUGF  2, "Sending ID string\n"
292
        mcall   send, [con.socketnum], ssh_ident, ssh_ident.length, 0
6419 hidnplayr 293
        cmp     eax, -1
294
        je      socket_err
295
 
6469 hidnplayr 296
; << Check protocol version of server
297
        mcall   recv, [con.socketnum], con.rx_buffer, BUFFERSIZE, 0
6419 hidnplayr 298
        cmp     eax, -1
299
        je      socket_err
300
 
6469 hidnplayr 301
        DEBUGF  2, "Received ID string\n"
302
        cmp     dword[con.rx_buffer], "SSH-"
6419 hidnplayr 303
        jne     proto_err
6469 hidnplayr 304
        cmp     dword[con.rx_buffer+4], "2.0-"
6419 hidnplayr 305
        jne     proto_err
306
 
307
; HASH: string  V_S, the server's version string (CR and NL excluded)
308
        lea     edx, [eax+2]
309
        sub     eax, 2
310
        bswap   eax
6469 hidnplayr 311
        mov     dword[con.rx_buffer-4], eax
312
        invoke  sha256_update, con.temp_ctx, con.rx_buffer-4, edx
6419 hidnplayr 313
 
6469 hidnplayr 314
; >> Key Exchange init
315
        mov     [con.rx_seq], 0
316
        mov     [con.tx_seq], 0
317
        mov     [con.rx_crypt_blocksize], 4             ; minimum blocksize
318
        mov     [con.tx_crypt_blocksize], 4
319
        mov     [con.rx_crypt_proc], 0
320
        mov     [con.tx_crypt_proc], 0
321
        mov     [con.rx_mac_proc], 0
322
        mov     [con.tx_mac_proc], 0
323
        mov     [con.rx_mac_length], 0
324
        mov     [con.tx_mac_length], 0
325
 
326
        DEBUGF  2, "Sending KEX init\n"
6419 hidnplayr 327
        mov     edi, ssh_kex.cookie
328
        call    MBRandom
329
        stosd
330
        call    MBRandom
331
        stosd
332
        call    MBRandom
333
        stosd
334
        call    MBRandom
335
        stosd
6469 hidnplayr 336
        stdcall ssh_send_packet, con, ssh_kex, ssh_kex.length, 0
6419 hidnplayr 337
        cmp     eax, -1
338
        je      socket_err
339
 
340
; HASH: string  I_C, the payload of the client's SSH_MSG_KEXINIT
6469 hidnplayr 341
        mov     eax, dword[con.tx_buffer+ssh_packet_header.packet_length]
6419 hidnplayr 342
        bswap   eax
6469 hidnplayr 343
        movzx   ebx, [con.tx_buffer+ssh_packet_header.padding_length]
6419 hidnplayr 344
        sub     eax, ebx
345
        dec     eax
346
        lea     edx, [eax+4]
347
        bswap   eax
6469 hidnplayr 348
        mov     dword[con.tx_buffer+1], eax
349
        invoke  sha256_update, con.temp_ctx, con.tx_buffer+1, edx
6419 hidnplayr 350
 
6469 hidnplayr 351
; << Check key exchange init of server
352
        stdcall ssh_recv_packet, con, 0
6419 hidnplayr 353
        cmp     eax, -1
354
        je      socket_err
355
 
6469 hidnplayr 356
        cmp     [con.rx_buffer.message_code], SSH_MSG_KEXINIT
6419 hidnplayr 357
        jne     proto_err
6469 hidnplayr 358
        DEBUGF  2, "Received KEX init\n"
6419 hidnplayr 359
 
6469 hidnplayr 360
        lea     esi, [con.rx_buffer+sizeof.ssh_packet_header+16]
6419 hidnplayr 361
        lodsd
362
        bswap   eax
363
        DEBUGF  1, "kex_algorithms: %s\n", esi
364
        add     esi, eax
365
        lodsd
366
        bswap   eax
367
        DEBUGF  1, "server_host_key_algorithms: %s\n", esi
368
        add     esi, eax
369
        lodsd
370
        bswap   eax
371
        DEBUGF  1, "encryption_algorithms_client_to_server: %s\n", esi
372
        add     esi, eax
373
        lodsd
374
        bswap   eax
375
        DEBUGF  1, "encryption_algorithms_server_to_client: %s\n", esi
376
        add     esi, eax
377
        lodsd
378
        bswap   eax
379
        DEBUGF  1, "mac_algorithms_client_to_server: %s\n", esi
380
        add     esi, eax
381
        lodsd
382
        bswap   eax
383
        DEBUGF  1, "mac_algorithms_server_to_client: %s\n", esi
384
        add     esi, eax
385
        lodsd
386
        bswap   eax
387
        DEBUGF  1, "compression_algorithms_client_to_server: %s\n", esi
388
        add     esi, eax
389
        lodsd
390
        bswap   eax
391
        DEBUGF  1, "compression_algorithms_server_to_client: %s\n", esi
392
        add     esi, eax
393
        lodsd
394
        bswap   eax
395
        DEBUGF  1, "languages_client_to_server: %s\n", esi
396
        add     esi, eax
397
        lodsd
398
        bswap   eax
399
        DEBUGF  1, "languages_server_to_client: %s\n", esi
400
        add     esi, eax
401
        lodsb
402
        DEBUGF  1, "KEX First Packet Follows: %u\n", al
403
 
6469 hidnplayr 404
        ; TODO: parse this structure and init procedures accordingly
6419 hidnplayr 405
 
406
; HASH: string I_S, the payload of the servers's SSH_MSG_KEXINIT
6469 hidnplayr 407
        mov     eax, dword[con.rx_buffer+ssh_packet_header.packet_length]
408
        movzx   ebx, [con.rx_buffer+ssh_packet_header.padding_length]
6419 hidnplayr 409
        sub     eax, ebx
410
        dec     eax
411
        lea     edx, [eax+4]
412
        bswap   eax
6469 hidnplayr 413
        mov     dword[con.rx_buffer+sizeof.ssh_packet_header-5], eax
414
        invoke  sha256_update, con.temp_ctx, con.rx_buffer+sizeof.ssh_packet_header-5, edx
6419 hidnplayr 415
 
416
; Exchange keys with the server
6469 hidnplayr 417
 
6419 hidnplayr 418
        stdcall dh_gex
419
        test    eax, eax
420
        jnz     exit
421
 
422
; Set keys
423
 
6469 hidnplayr 424
        DEBUGF  2, "SSH: Setting encryption keys\n"
6419 hidnplayr 425
 
6469 hidnplayr 426
        stdcall aes256_cbc_init, con.rx_iv
427
        mov     [con.rx_crypt_ctx_ptr], eax
428
 
429
        stdcall aes256_set_decrypt_key, eax, con.rx_enc_key
430
        mov     [con.rx_crypt_proc], aes256_cbc_decrypt
431
        mov     [con.rx_crypt_blocksize], AES256_BLOCKSIZE
432
 
433
        stdcall aes256_cbc_init, con.tx_iv
434
        mov     [con.tx_crypt_ctx_ptr], eax
435
 
436
        stdcall aes256_set_encrypt_key, eax, con.tx_enc_key
437
        mov     [con.tx_crypt_proc], aes256_cbc_encrypt
438
        mov     [con.tx_crypt_blocksize], AES256_BLOCKSIZE
439
 
440
        stdcall hmac_sha256_setkey, con.rx_mac_ctx, con.rx_int_key, SHA256_HASH_SIZE
441
        mov     [con.rx_mac_proc], hmac_sha256
442
        mov     [con.rx_mac_length], SHA256_HASH_SIZE
443
 
444
        stdcall hmac_sha256_setkey, con.tx_mac_ctx, con.tx_int_key, SHA256_HASH_SIZE
445
        mov     [con.tx_mac_proc], hmac_sha256
446
        mov     [con.tx_mac_length], SHA256_HASH_SIZE
447
 
448
; TODO: erase all keys from memory and free the memory
449
 
450
; >> Request service (user-auth)
451
 
452
        DEBUGF  2, "SSH: Requesting service\n"
453
 
454
        stdcall ssh_send_packet, con, ssh_request_service, ssh_request_service.length, 0
455
        cmp     eax, -1
456
        je      socket_err
457
 
458
; << Check for service acceptance
459
 
460
        stdcall ssh_recv_packet, con, 0
461
        cmp     eax, -1
462
        je      socket_err
463
 
464
        cmp     [con.rx_buffer.message_code], SSH_MSG_SERVICE_ACCEPT
465
        jne     proto_err
466
 
467
; >> Request user authentication
468
 
469
; TODO: Request username from the user
470
;        invoke  con_write_asciiz, str12
471
;        invoke  con_gets, username, 256
472
;        test    eax, eax
473
;        jz      done
474
 
475
; TODO: implement password authentication
476
 
477
        DEBUGF  2, "SSH: User authentication\n"
478
 
479
        stdcall ssh_send_packet, con, ssh_request_userauth, ssh_request_userauth.length, 0
480
        cmp     eax, -1
481
        je      socket_err
482
 
483
; << Check for userauth acceptance
484
 
485
        stdcall ssh_recv_packet, con, 0
486
        cmp     eax, -1
487
        je      socket_err
488
 
489
        cmp     [con.rx_buffer.message_code], SSH_MSG_USERAUTH_SUCCESS
490
        jne     proto_err
491
 
492
; >> Open channel
493
 
494
        DEBUGF  2, "SSH: Open channel\n"
495
 
496
        stdcall ssh_send_packet, con, ssh_channel_open, ssh_channel_open.length, 0
497
        cmp     eax, -1
498
        je      socket_err
499
 
500
; << Check for channel open confirmation
501
 
502
        stdcall ssh_recv_packet, con, 0
503
        cmp     eax, -1
504
        je      socket_err
505
 
506
        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_OPEN_CONFIRMATION
507
        jne     proto_err
508
 
509
; >> Channel request: pty
510
 
511
        DEBUGF  2, "SSH: Request pty\n"
512
 
513
        stdcall ssh_send_packet, con, ssh_channel_request, ssh_channel_request.length, 0
514
        cmp     eax, -1
515
        je      socket_err
516
 
517
; << Check for channel request confirmation
518
 
519
        stdcall ssh_recv_packet, con, 0
520
        cmp     eax, -1
521
        je      socket_err
522
 
523
        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_SUCCESS
524
        jne     proto_err
525
 
526
; >> Channel request: shell
527
 
528
        DEBUGF  2, "SSH: Request shell\n"
529
 
530
        stdcall ssh_send_packet, con, ssh_shell_request, ssh_shell_request.length, 0
531
        cmp     eax, -1
532
        je      socket_err
533
 
534
; << Check for channel request confirmation (FIXME: this may not be first packet!)
535
 
536
;        stdcall ssh_recv_packet, con, 0
537
;        cmp     eax, -1
538
;        je      socket_err
539
 
540
;        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_SUCCESS
541
;        jne     proto_err
542
 
6419 hidnplayr 543
; Launch network thread
544
        mcall   18, 7
545
        push    eax
546
        mcall   51, 1, thread, mem - 2048
547
        pop     ecx
548
        mcall   18, 3
549
 
550
mainloop:
551
        call    [con_get_flags]
552
        test    eax, 0x200                      ; con window closed?
553
        jnz     exit
554
 
6469 hidnplayr 555
        stdcall ssh_recv_packet, con, 0
556
        cmp     eax, 0
557
        jbe     closed
6419 hidnplayr 558
 
6469 hidnplayr 559
        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_DATA
560
        jne     .dump
561
 
562
        mov     eax, dword[con.rx_buffer.message_code+5]
563
        bswap   eax
6419 hidnplayr 564
        DEBUGF  1, 'SSH: got %u bytes of data !\n', eax
565
 
6469 hidnplayr 566
        lea     esi, [con.rx_buffer.message_code+5+4]
6419 hidnplayr 567
        mov     ecx, eax
6469 hidnplayr 568
        lea     edi, [esi + eax]
569
        mov     byte [edi], 0
570
        invoke  con_write_asciiz, esi
571
        jmp     mainloop
572
 
573
  .dump:
574
        lea     esi, [con.rx_buffer]
575
        mov     ecx, eax
6419 hidnplayr 576
        pusha
577
@@:
578
        lodsb
579
        DEBUGF  1, "%x ", eax:2
580
        dec     ecx
581
        jnz     @r
582
        popa
6469 hidnplayr 583
        DEBUGF  1, "\n"
6419 hidnplayr 584
        jmp     mainloop
585
 
6469 hidnplayr 586
 
6419 hidnplayr 587
proto_err:
6469 hidnplayr 588
        DEBUGF  3, "SSH: protocol error\n"
6419 hidnplayr 589
        invoke  con_write_asciiz, str7
590
        jmp     prompt
591
 
592
socket_err:
6469 hidnplayr 593
        DEBUGF  3, "SSH: socket error %d\n", ebx
6419 hidnplayr 594
        invoke  con_write_asciiz, str6
595
        jmp     prompt
596
 
597
dns_error:
6469 hidnplayr 598
        DEBUGF  3, "SSH: DNS error %d\n", eax
6419 hidnplayr 599
        invoke  con_write_asciiz, str5
600
        jmp     prompt
601
 
602
hostname_error:
603
        invoke  con_write_asciiz, str10
604
        jmp     prompt
605
 
606
closed:
607
        invoke  con_write_asciiz, str11
608
        jmp     prompt
609
 
610
done:
611
        invoke  con_exit, 1
612
exit:
6469 hidnplayr 613
        DEBUGF  3, "SSH: Exiting\n"
614
        mcall   close, [con.socketnum]
6419 hidnplayr 615
        mcall   -1
616
 
617
 
618
thread:
619
        mcall   40, 0
620
  .loop:
621
        invoke  con_getch2
6469 hidnplayr 622
        mov     [ssh_channel_data+9], al
623
        stdcall ssh_send_packet, con, ssh_channel_data, ssh_channel_data.length, 0
6419 hidnplayr 624
 
625
        invoke  con_get_flags
626
        test    eax, 0x200                      ; con window closed?
627
        jz      .loop
628
        mcall   -1
629
 
630
; data
631
title   db      'Secure Shell',0
632
str1    db      'SSH client for KolibriOS',10,10,\
633
                'Please enter URL of SSH server (host:port)',10,10,0
634
str2    db      '> ',0
635
str3    db      'Connecting to ',0
636
str4    db      10,0
637
str5    db      'Name resolution failed.',10,10,0
638
str6    db      'A socket error occured.',10,10,0
639
str7    db      'A protocol error occured.',10,10,0
640
str8    db      ' (',0
641
str9    db      ')',10,0
642
str10   db      'Invalid hostname.',10,10,0
643
str11   db      10,'Remote host closed the connection.',10,10,0
6469 hidnplayr 644
str12   db      'Enter username: ',0
6419 hidnplayr 645
 
646
ssh_ident_ha:
647
        dd_n (ssh_ident.length-2)
648
ssh_ident:
6469 hidnplayr 649
        db "SSH-2.0-KolibriOS_SSH_0.02",13,10
6419 hidnplayr 650
  .length = $ - ssh_ident
651
 
652
ssh_kex:
653
        db SSH_MSG_KEXINIT
654
  .cookie:
655
        rd 4
656
  .kex_algorithms:
657
        dd_n .server_host_key_algorithms - .kex_algorithms - 4
658
        db "diffie-hellman-group-exchange-sha256" ; diffie-hellman-group-exchange-sha1
659
  .server_host_key_algorithms:
660
        dd_n .encryption_algorithms_client_to_server - .server_host_key_algorithms - 4
661
        db "ssh-rsa"                    ;,ssh-dss
662
  .encryption_algorithms_client_to_server:
663
        dd_n .encryption_algorithms_server_to_client - .encryption_algorithms_client_to_server - 4
664
        db "aes256-cbc"                 ;,aes256-ctr,aes256-cbc,rijndael-cbc@lysator.liu.se,aes192-ctr,aes192-cbc,aes128-ctr,aes128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc,arcfour256,arcfour128"
665
  .encryption_algorithms_server_to_client:
666
        dd_n .mac_algorithms_client_to_server - .encryption_algorithms_server_to_client - 4
667
        db "aes256-cbc"                 ;,aes256-ctr,aes256-cbc,rijndael-cbc@lysator.liu.se,aes192-ctr,aes192-cbc,aes128-ctr,aes128-cbc,blowfish-ctr,blowfish-cbc,3des-ctr,3des-cbc,arcfour256,arcfour128"
668
  .mac_algorithms_client_to_server:
669
        dd_n .mac_algorithms_server_to_client - .mac_algorithms_client_to_server - 4
670
        db "hmac-sha2-256"              ;,hmac-sha1,hmac-sha1-96,hmac-md5"
671
  .mac_algorithms_server_to_client:
672
        dd_n .compression_algorithms_client_to_server - .mac_algorithms_server_to_client - 4
673
        db "hmac-sha2-256"              ;,hmac-sha1,hmac-sha1-96,hmac-md5"
674
  .compression_algorithms_client_to_server:
675
        dd_n .compression_algorithms_server_to_client - .compression_algorithms_client_to_server - 4
676
        db "none"                       ;,zlib"
677
  .compression_algorithms_server_to_client:
678
        dd_n .languages_client_to_server - .compression_algorithms_server_to_client - 4
679
        db "none"                       ;,zlib"
680
  .languages_client_to_server:
681
        dd_n .languages_server_to_client - .languages_client_to_server - 4
682
        db ""
683
  .languages_server_to_client:
684
        dd_n .first_kex_packet_follows - .languages_server_to_client - 4
685
        db ""
686
  .first_kex_packet_follows:
687
        db 0
688
  .reserved:
689
        dd_n 0
690
  .length = $ - ssh_kex
691
 
692
 
693
ssh_gex_req:
694
        db SSH_MSG_KEX_DH_GEX_REQUEST
6469 hidnplayr 695
        dd_n 128                        ; DH GEX min
696
        dd_n 256                        ; DH GEX number of bits
697
        dd_n 512                        ; DH GEX Max
6419 hidnplayr 698
  .length = $ - ssh_gex_req
699
 
700
 
701
ssh_new_keys:
702
        db SSH_MSG_NEWKEYS
703
  .length = $ - ssh_new_keys
704
 
705
 
6469 hidnplayr 706
ssh_request_service:
707
        db SSH_MSG_SERVICE_REQUEST
708
        dd_n 12                         ; String length
709
        db "ssh-userauth"               ; Service name
710
  .length = $ - ssh_request_service
711
 
712
 
713
ssh_request_userauth:
714
        db SSH_MSG_USERAUTH_REQUEST
715
        dd_n 12
716
        dd_n 8
717
        db "username"                   ; user name in ISO-10646 UTF-8 encoding [RFC3629]
718
        dd_n 14
719
        db "ssh-connection"             ; service name in US-ASCII
720
        dd_n 4
721
        db "none"                       ; method name in US-ASCII
722
; Other options: publickey, password, hostbased
723
  .length = $ - ssh_request_userauth
724
 
725
 
726
ssh_channel_open:
727
        db SSH_MSG_CHANNEL_OPEN
728
        dd_n 7
729
        db "session"
730
        dd_n 0                          ; Sender channel
731
        dd_n 1024                       ; Initial window size
732
        dd_n 1024                       ; maximum packet size
733
  .length = $ - ssh_channel_open
734
 
735
ssh_channel_request:
736
        db SSH_MSG_CHANNEL_REQUEST
737
        dd_n 0                          ; Recipient channel
738
        dd_n 7
739
        db "pty-req"
740
        db 1                            ; Bool: want reply
741
        dd_n 5
742
        db "xterm"
743
        dd_n 80                         ; terminal width (rows)
744
        dd_n 25                         ; terminal height (rows)
745
        dd_n 0                          ; terminal width (pixels)
746
        dd_n 0                          ; terminal height (pixels)
747
 
748
        dd_n 0                          ; list of supported opcodes
749
  .length = $ - ssh_channel_request
750
 
751
ssh_shell_request:
752
        db SSH_MSG_CHANNEL_REQUEST
753
        dd_n 0                          ; Recipient channel
754
        dd_n 5
755
        db "shell"
756
        db 1                            ; Bool: want reply
757
  .length = $ - ssh_shell_request
758
 
759
ssh_channel_data:
760
        db SSH_MSG_CHANNEL_DATA
761
        dd_n 0                          ; Sender channel
762
        dd_n 1
763
        db ?
764
  .length = $ - ssh_channel_data
765
 
766
 
6419 hidnplayr 767
include_debug_strings
768
 
769
align 4
770
@IMPORT:
771
 
772
library network, 'network.obj', \
6469 hidnplayr 773
        console, 'console.obj', \
774
        libcrash, 'libcrash.obj'
6419 hidnplayr 775
 
776
import  network, \
777
        getaddrinfo, 'getaddrinfo', \
778
        freeaddrinfo, 'freeaddrinfo', \
779
        inet_ntoa, 'inet_ntoa'
780
 
781
import  console, \
782
        con_start, 'START', \
783
        con_init, 'con_init', \
784
        con_write_asciiz, 'con_write_asciiz', \
785
        con_exit, 'con_exit', \
786
        con_gets, 'con_gets', \
787
        con_cls, 'con_cls', \
788
        con_getch2, 'con_getch2', \
789
        con_set_cursor_pos, 'con_set_cursor_pos', \
790
        con_write_string, 'con_write_string', \
791
        con_get_flags,  'con_get_flags'
792
 
6469 hidnplayr 793
import  libcrash, \
794
        sha256_init, 'sha256_init', \
795
        sha256_update, 'sha256_update', \
796
        sha256_final, 'sha256_final'
6419 hidnplayr 797
 
798
IncludeIGlobals
799
 
800
i_end:
801
 
802
IncludeUGlobals
803
 
6469 hidnplayr 804
params          rb 1024
6419 hidnplayr 805
 
6469 hidnplayr 806
con             ssh_connection
6419 hidnplayr 807
 
6469 hidnplayr 808
; Temporary values      ; To be removed FIXME
809
mpint_tmp       rb MPINT_MAX_LEN+4
6419 hidnplayr 810
 
811
 
812
mem: