Subversion Repositories Kolibri OS

Rev

Rev 6419 | Rev 6922 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6419 Rev 6469
Line 16... Line 16...
16
;    along with this program.  If not, see .
16
;    along with this program.  If not, see .
Line 17... Line 17...
17
 
17
 
Line 18... Line 18...
18
format binary as ""
18
format binary as ""
19
 
19
 
Line 20... Line 20...
20
__DEBUG__       = 1
20
__DEBUG__       = 1
21
__DEBUG_LEVEL__ = 1
21
__DEBUG_LEVEL__ = 2             ; 1: Extreme debugging, 2: Debugging, 3: Errors only
Line 22... Line 22...
22
 
22
 
Line 31... Line 31...
31
        dd      1               ; header version
31
        dd      1               ; header version
32
        dd      start           ; entry point
32
        dd      start           ; entry point
33
        dd      i_end           ; initialized size
33
        dd      i_end           ; initialized size
34
        dd      mem+4096        ; required memory
34
        dd      mem+4096        ; required memory
35
        dd      mem+4096        ; stack pointer
35
        dd      mem+4096        ; stack pointer
36
        dd      hostname        ; parameters
36
        dd      params          ; parameters
37
        dd      0               ; path
37
        dd      0               ; path
Line 38... Line 38...
38
 
38
 
-
 
39
include '../../macros.inc'
39
include '../../macros.inc'
40
;include '../../struct.inc'
40
purge mov,add,sub
41
purge mov,add,sub
41
include '../../proc32.inc'
42
include '../../proc32.inc'
42
include '../../dll.inc'
43
include '../../dll.inc'
43
include '../../debug-fdo.inc'
44
include '../../debug-fdo.inc'
44
include '../../network.inc'
45
include '../../network.inc'
Line 45... Line 46...
45
;include '../../develop/libraries/libcrash/trunk/libcrash.inc'
46
include '../../develop/libraries/libcrash/trunk/libcrash.inc'
46
 
47
 
47
include 'mcodes.inc'
48
include 'mcodes.inc'
Line 48... Line 49...
48
include 'ssh_transport.inc'
49
include 'ssh_transport.inc'
49
include 'dh_gex.inc'
50
include 'dh_gex.inc'
50
 
51
 
51
include 'mpint.inc'
52
include 'mpint.inc'
52
include 'random.inc'
53
include 'random.inc'
53
include 'aes256.inc'
54
include 'aes256.inc'
Line 54... Line 55...
54
include 'aes256-ctr.inc'
55
include 'aes256-ctr.inc'
55
include 'aes256-cbc.inc'
56
include 'aes256-cbc.inc'
56
include '../../fs/kfar/trunk/kfar_arc/sha256.inc'
57
include 'hmac_sha256.inc'
57
 
58
 
Line 66... Line 67...
66
macro dw_n op {
67
macro dw_n op {
67
   dw 0 or (((op) and 0FF00h) shr 8) or \
68
   dw 0 or (((op) and 0FF00h) shr 8) or \
68
           (((op) and 000FFh) shl 8)
69
           (((op) and 000FFh) shl 8)
69
}
70
}
Line -... Line 71...
-
 
71
 
-
 
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
70
 
167
 
71
start:
168
start:
Line 72... Line 169...
72
        mcall   68, 11          ; Init heap
169
        mcall   68, 11          ; Init heap
73
 
170
 
74
        DEBUGF  1, "SSH: Loading libraries\n"
171
        DEBUGF  2, "SSH: Loading libraries\n"
75
        stdcall dll.Load, @IMPORT
172
        stdcall dll.Load, @IMPORT
Line 76... Line 173...
76
        test    eax, eax
173
        test    eax, eax
77
        jnz     exit
174
        jnz     exit
Line 78... Line 175...
78
 
175
 
79
        DEBUGF  1, "SSH: Init PRNG\n"
176
        DEBUGF  2, "SSH: Init PRNG\n"
80
        call    init_random
177
        call    init_random
Line 81... Line 178...
81
 
178
 
82
        DEBUGF  1, "SSH: Init Console\n"
179
        DEBUGF  2, "SSH: Init Console\n"
83
        invoke  con_start, 1
180
        invoke  con_start, 1
Line 84... Line 181...
84
        invoke  con_init, 80, 25, 80, 25, title
181
        invoke  con_init, 80, 25, 80, 25, title
85
 
182
 
86
; Check for parameters
183
; Check for parameters TODO
87
        cmp     byte[hostname], 0
184
;        cmp     byte[params], 0
Line 88... Line 185...
88
        jne     resolve
185
;        jne     resolve
89
 
186
 
90
main:
187
main:
91
        invoke  con_cls
188
        invoke  con_cls
92
; Welcome user
189
; Welcome user
93
        invoke  con_write_asciiz, str1
190
        invoke  con_write_asciiz, str1
94
 
191
 
95
prompt:
192
prompt:
96
; write prompt
193
; write prompt
97
        invoke  con_write_asciiz, str2
194
        invoke  con_write_asciiz, str2
98
; read string
195
; read string
Line 99... Line 196...
99
        mov     esi, hostname
196
        mov     esi, con.hostname
-
 
197
        invoke  con_gets, esi, 256
100
        invoke  con_gets, esi, 256
198
; check for exit
Line 101... Line 199...
101
; check for exit
199
        test    eax, eax
102
        test    eax, eax
200
        jz      done
103
        jz      done
201
        cmp     byte[esi], 10
104
        cmp     byte[esi], 10
202
        jz      done
105
        jz      done
203
 
106
 
204
resolve:
107
resolve:
205
        mov     [con.sockaddr], AF_INET4
Line 135... Line 233...
135
        add     ebx, eax
233
        add     ebx, eax
136
        jmp     .portloop
234
        jmp     .portloop
Line 137... Line 235...
137
 
235
 
138
  .port_done:
236
  .port_done:
139
        xchg    bl, bh
237
        xchg    bl, bh
Line 140... Line 238...
140
        mov     [sockaddr1.port], bx
238
        mov     [con.port], bx
Line 141... Line 239...
141
 
239
 
142
  .done:
240
  .done:
143
 
241
 
144
; resolve name
242
; resolve name
145
        push    esp     ; reserve stack place
243
        push    esp     ; reserve stack place
146
        push    esp
244
        push    esp
147
        invoke  getaddrinfo, hostname, 0, 0
245
        invoke  getaddrinfo, con.hostname, 0, 0
148
        pop     esi
246
        pop     esi
Line 149... Line 247...
149
; test for error
247
; test for error
150
        test    eax, eax
248
        test    eax, eax
151
        jnz     dns_error
249
        jnz     dns_error
Line 152... Line 250...
152
 
250
 
153
        invoke  con_cls
251
        invoke  con_cls
Line 154... Line 252...
154
        invoke  con_write_asciiz, str3
252
        invoke  con_write_asciiz, str3
155
        invoke  con_write_asciiz, hostname
253
        invoke  con_write_asciiz, con.hostname
156
 
254
 
157
; write results
255
; write results
158
        invoke  con_write_asciiz, str8
256
        invoke  con_write_asciiz, str8
159
 
257
 
160
; convert IP address to decimal notation
258
; convert IP address to decimal notation
161
        mov     eax, [esi+addrinfo.ai_addr]
259
        mov     eax, [esi+addrinfo.ai_addr]
162
        mov     eax, [eax+sockaddr_in.sin_addr]
260
        mov     eax, [eax+sockaddr_in.sin_addr]
Line 174... Line 272...
174
 
272
 
175
; Create socket
273
; Create socket
176
        mcall   socket, AF_INET4, SOCK_STREAM, 0
274
        mcall   socket, AF_INET4, SOCK_STREAM, 0
177
        cmp     eax, -1
275
        cmp     eax, -1
178
        jz      socket_err
276
        jz      socket_err
Line 179... Line 277...
179
        mov     [socketnum], eax
277
        mov     [con.socketnum], eax
-
 
278
 
180
 
279
; Connect
181
; Connect
280
        DEBUGF  2, "Connecting to server\n"
182
        mcall   connect, [socketnum], sockaddr1, 18
281
        mcall   connect, [con.socketnum], con.sockaddr, 18
Line 183... Line 282...
183
        test    eax, eax
282
        test    eax, eax
184
        jnz     socket_err
283
        jnz     socket_err
185
 
284
 
186
; Start calculating hash meanwhile
-
 
187
        call    sha256_init
285
; Start calculating hash
188
; HASH: string  V_C, the client's version string (CR and NL excluded)
-
 
Line 189... Line 286...
189
        mov     esi, ssh_ident_ha
286
        invoke  sha256_init, con.temp_ctx
190
        mov     edx, ssh_ident.length+4-2
287
; HASH: string  V_C, the client's version string (CR and NL excluded)
191
        call    sha256_update
288
        invoke  sha256_update, con.temp_ctx, ssh_ident_ha, ssh_ident.length+4-2
192
 
289
 
193
; Send our identification string
290
; >> Send our identification string
Line 194... Line 291...
194
        DEBUGF  1, "Sending ID string\n"
291
        DEBUGF  2, "Sending ID string\n"
195
        mcall   send, [socketnum], ssh_ident, ssh_ident.length, 0
292
        mcall   send, [con.socketnum], ssh_ident, ssh_ident.length, 0
196
        cmp     eax, -1
293
        cmp     eax, -1
197
        je      socket_err
294
        je      socket_err
Line 198... Line 295...
198
 
295
 
199
; Check protocol version of server
296
; << Check protocol version of server
200
        mcall   recv, [socketnum], rx_buffer, BUFFERSIZE, 0
297
        mcall   recv, [con.socketnum], con.rx_buffer, BUFFERSIZE, 0
201
        cmp     eax, -1
298
        cmp     eax, -1
202
        je      socket_err
299
        je      socket_err
Line 203... Line 300...
203
 
300
 
204
        DEBUGF  1, "Received ID string\n"
301
        DEBUGF  2, "Received ID string\n"
205
        cmp     dword[rx_buffer], "SSH-"
302
        cmp     dword[con.rx_buffer], "SSH-"
206
        jne     proto_err
303
        jne     proto_err
207
        cmp     dword[rx_buffer+4], "2.0-"
304
        cmp     dword[con.rx_buffer+4], "2.0-"
-
 
305
        jne     proto_err
-
 
306
 
-
 
307
; HASH: string  V_S, the server's version string (CR and NL excluded)
208
        jne     proto_err
308
        lea     edx, [eax+2]
209
 
309
        sub     eax, 2
-
 
310
        bswap   eax
-
 
311
        mov     dword[con.rx_buffer-4], eax
-
 
312
        invoke  sha256_update, con.temp_ctx, con.rx_buffer-4, edx
-
 
313
 
-
 
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
Line 210... Line -...
210
; HASH: string  V_S, the server's version string (CR and NL excluded)
-
 
211
        lea     edx, [eax+2]
318
        mov     [con.tx_crypt_blocksize], 4
212
        sub     eax, 2
319
        mov     [con.rx_crypt_proc], 0
213
        bswap   eax
320
        mov     [con.tx_crypt_proc], 0
214
        mov     [rx_buffer-4], eax
321
        mov     [con.rx_mac_proc], 0
215
        mov     esi, rx_buffer-4
322
        mov     [con.tx_mac_proc], 0
216
        call    sha256_update
323
        mov     [con.rx_mac_length], 0
217
 
324
        mov     [con.tx_mac_length], 0
218
; Key Exchange init
325
 
219
        DEBUGF  1, "Sending KEX init\n"
326
        DEBUGF  2, "Sending KEX init\n"
220
        mov     edi, ssh_kex.cookie
327
        mov     edi, ssh_kex.cookie
221
        call    MBRandom
328
        call    MBRandom
222
        stosd
329
        stosd
223
        call    MBRandom
330
        call    MBRandom
Line 224... Line 331...
224
        stosd
331
        stosd
225
        call    MBRandom
332
        call    MBRandom
226
        stosd
333
        stosd
227
        call    MBRandom
334
        call    MBRandom
228
        stosd
335
        stosd
229
        stdcall ssh_send_packet, [socketnum], ssh_kex, ssh_kex.length, 0
336
        stdcall ssh_send_packet, con, ssh_kex, ssh_kex.length, 0
230
        cmp     eax, -1
337
        cmp     eax, -1
231
        je      socket_err
338
        je      socket_err
232
 
339
 
233
; HASH: string  I_C, the payload of the client's SSH_MSG_KEXINIT
340
; HASH: string  I_C, the payload of the client's SSH_MSG_KEXINIT
234
        mov     eax, [tx_buffer+ssh_header.length]
-
 
Line 235... Line 341...
235
        bswap   eax
341
        mov     eax, dword[con.tx_buffer+ssh_packet_header.packet_length]
236
        movzx   ebx, [tx_buffer+ssh_header.padding]
342
        bswap   eax
237
        sub     eax, ebx
343
        movzx   ebx, [con.tx_buffer+ssh_packet_header.padding_length]
238
        dec     eax
344
        sub     eax, ebx
Line 239... Line 345...
239
        lea     edx, [eax+4]
345
        dec     eax
240
        bswap   eax
346
        lea     edx, [eax+4]
241
        mov     [tx_buffer+1], eax
347
        bswap   eax
Line 242... Line 348...
242
        mov     esi, tx_buffer+1
348
        mov     dword[con.tx_buffer+1], eax
243
        call    sha256_update
349
        invoke  sha256_update, con.temp_ctx, con.tx_buffer+1, edx
244
 
350
 
245
; Check key exchange init of server
351
; << Check key exchange init of server
246
        stdcall ssh_recv_packet, [socketnum], rx_buffer, BUFFERSIZE, 0
352
        stdcall ssh_recv_packet, con, 0
247
        cmp     eax, -1
353
        cmp     eax, -1
Line 293... Line 399...
293
        DEBUGF  1, "languages_server_to_client: %s\n", esi
399
        DEBUGF  1, "languages_server_to_client: %s\n", esi
294
        add     esi, eax
400
        add     esi, eax
295
        lodsb
401
        lodsb
296
        DEBUGF  1, "KEX First Packet Follows: %u\n", al
402
        DEBUGF  1, "KEX First Packet Follows: %u\n", al
Line 297... Line 403...
297
 
403
 
Line 298... Line 404...
298
        ; TODO
404
        ; TODO: parse this structure and init procedures accordingly
299
 
405
 
300
; HASH: string I_S, the payload of the servers's SSH_MSG_KEXINIT
406
; HASH: string I_S, the payload of the servers's SSH_MSG_KEXINIT
301
        mov     eax, [rx_buffer+ssh_header.length]
407
        mov     eax, dword[con.rx_buffer+ssh_packet_header.packet_length]
302
        movzx   ebx, [rx_buffer+ssh_header.padding]
408
        movzx   ebx, [con.rx_buffer+ssh_packet_header.padding_length]
303
        sub     eax, ebx
409
        sub     eax, ebx
304
        dec     eax
410
        dec     eax
305
        lea     edx, [eax+4]
411
        lea     edx, [eax+4]
306
        bswap   eax
412
        bswap   eax
307
        mov     [rx_buffer+sizeof.ssh_header-5], eax
-
 
Line 308... Line 413...
308
        mov     esi, rx_buffer+sizeof.ssh_header-5
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
309
        call    sha256_update
415
 
310
 
416
; Exchange keys with the server
311
; Exchange keys with the server
417
 
Line 312... Line 418...
312
        stdcall dh_gex
418
        stdcall dh_gex
-
 
419
        test    eax, eax
313
        test    eax, eax
420
        jnz     exit
-
 
421
 
314
        jnz     exit
422
; Set keys
315
 
423
 
-
 
424
        DEBUGF  2, "SSH: Setting encryption keys\n"
316
; Set keys
425
 
317
        DEBUGF  1, "SSH: Init encryption\n"
426
        stdcall aes256_cbc_init, con.rx_iv
318
        stdcall aes256_cbc_init, rx_iv
427
        mov     [con.rx_crypt_ctx_ptr], eax
319
        mov     [rx_context], eax
428
 
320
        stdcall aes256_set_encrypt_key, [rx_context], rx_enc_key
-
 
321
        mov     [decrypt_proc], aes256_cbc_decrypt
429
        stdcall aes256_set_decrypt_key, eax, con.rx_enc_key
322
        mov     [rx_blocksize], 32
430
        mov     [con.rx_crypt_proc], aes256_cbc_decrypt
-
 
431
        mov     [con.rx_crypt_blocksize], AES256_BLOCKSIZE
323
 
432
 
324
        DEBUGF  1, "SSH: Init decryption\n"
433
        stdcall aes256_cbc_init, con.tx_iv
325
        stdcall aes256_cbc_init, 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
Line 326... Line 538...
326
        mov     [tx_context], eax
538
;        je      socket_err
327
        stdcall aes256_set_decrypt_key, [tx_context], tx_enc_key
539
 
328
        mov     [encrypt_proc], aes256_cbc_encrypt
540
;        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_SUCCESS
329
        mov     [tx_blocksize], 32
541
;        jne     proto_err
Line 338... Line 550...
338
mainloop:
550
mainloop:
339
        call    [con_get_flags]
551
        call    [con_get_flags]
340
        test    eax, 0x200                      ; con window closed?
552
        test    eax, 0x200                      ; con window closed?
341
        jnz     exit
553
        jnz     exit
Line 342... Line 554...
342
 
554
 
343
        stdcall ssh_recv_packet, [socketnum], rx_buffer, BUFFERSIZE, 0
555
        stdcall ssh_recv_packet, con, 0
344
        cmp     eax, -1
556
        cmp     eax, 0
-
 
557
        jbe     closed
-
 
558
 
-
 
559
        cmp     [con.rx_buffer.message_code], SSH_MSG_CHANNEL_DATA
Line -... Line 560...
-
 
560
        jne     .dump
-
 
561
 
345
        je      closed
562
        mov     eax, dword[con.rx_buffer.message_code+5]
Line -... Line 563...
-
 
563
        bswap   eax
-
 
564
        DEBUGF  1, 'SSH: got %u bytes of data !\n', eax
-
 
565
 
-
 
566
        lea     esi, [con.rx_buffer.message_code+5+4]
-
 
567
        mov     ecx, eax
-
 
568
        lea     edi, [esi + eax]
-
 
569
        mov     byte [edi], 0
-
 
570
        invoke  con_write_asciiz, esi
346
 
571
        jmp     mainloop
347
        DEBUGF  1, 'SSH: got %u bytes of data !\n', eax
572
 
348
 
573
  .dump:
349
        mov     esi, rx_buffer
574
        lea     esi, [con.rx_buffer]
350
        mov     ecx, eax
575
        mov     ecx, eax
351
        pusha
576
        pusha
352
@@:
577
@@:
353
        lodsb
578
        lodsb
354
        DEBUGF  1, "%x ", eax:2
579
        DEBUGF  1, "%x ", eax:2
355
        dec     ecx
-
 
356
        jnz     @r
580
        dec     ecx
357
        popa
-
 
358
        lea     edi, [esi + eax]
581
        jnz     @r
Line -... Line 582...
-
 
582
        popa
359
        mov     byte [edi], 0
583
        DEBUGF  1, "\n"
360
        invoke  con_write_asciiz, esi
584
        jmp     mainloop
361
        jmp     mainloop
585
 
362
 
586
 
Line 363... Line 587...
363
proto_err:
587
proto_err:
364
        DEBUGF  1, "SSH: protocol error\n"
588
        DEBUGF  3, "SSH: protocol error\n"
365
        invoke  con_write_asciiz, str7
589
        invoke  con_write_asciiz, str7
366
        jmp     prompt
590
        jmp     prompt
Line 367... Line 591...
367
 
591
 
368
socket_err:
592
socket_err:
369
        DEBUGF  1, "SSH: socket error %d\n", ebx
593
        DEBUGF  3, "SSH: socket error %d\n", ebx
370
        invoke  con_write_asciiz, str6
594
        invoke  con_write_asciiz, str6
Line 371... Line 595...
371
        jmp     prompt
595
        jmp     prompt
372
 
596
 
Line 384... Line 608...
384
        jmp     prompt
608
        jmp     prompt
Line 385... Line 609...
385
 
609
 
386
done:
610
done:
387
        invoke  con_exit, 1
611
        invoke  con_exit, 1
388
exit:
612
exit:
389
        DEBUGF  1, "SSH: Exiting\n"
613
        DEBUGF  3, "SSH: Exiting\n"
390
        mcall   close, [socketnum]
614
        mcall   close, [con.socketnum]
Line 391... Line 615...
391
        mcall   -1
615
        mcall   -1
392
 
616
 
393
 
617
 
394
thread:
618
thread:
395
        mcall   40, 0
619
        mcall   40, 0
396
  .loop:
-
 
397
        invoke  con_getch2
-
 
398
        mov     [send_data], ax
-
 
399
        xor     esi, esi
-
 
400
        inc     esi
-
 
401
        test    al, al
-
 
402
        jnz     @f
620
  .loop:
Line 403... Line 621...
403
        inc     esi
621
        invoke  con_getch2
404
  @@:
622
        mov     [ssh_channel_data+9], al
405
        stdcall ssh_send_packet, [socketnum], send_data, 0
623
        stdcall ssh_send_packet, con, ssh_channel_data, ssh_channel_data.length, 0
406
 
624
 
Line 421... Line 639...
421
str7    db      'A protocol error occured.',10,10,0
639
str7    db      'A protocol error occured.',10,10,0
422
str8    db      ' (',0
640
str8    db      ' (',0
423
str9    db      ')',10,0
641
str9    db      ')',10,0
424
str10   db      'Invalid hostname.',10,10,0
642
str10   db      'Invalid hostname.',10,10,0
425
str11   db      10,'Remote host closed the connection.',10,10,0
643
str11   db      10,'Remote host closed the connection.',10,10,0
426
 
-
 
427
sockaddr1:
-
 
428
        dw AF_INET4
644
str12   db      'Enter username: ',0
429
  .port dw 0
-
 
430
  .ip   dd 0
-
 
431
        rb 10
-
 
Line 432... Line 645...
432
 
645
 
433
ssh_ident_ha:
646
ssh_ident_ha:
434
        dd_n (ssh_ident.length-2)
647
        dd_n (ssh_ident.length-2)
435
ssh_ident:
648
ssh_ident:
436
        db "SSH-2.0-KolibriOS_SSH_0.01",13,10
649
        db "SSH-2.0-KolibriOS_SSH_0.02",13,10
Line 437... Line 650...
437
  .length = $ - ssh_ident
650
  .length = $ - ssh_ident
438
 
651
 
439
ssh_kex:
652
ssh_kex:
Line 488... Line 701...
488
ssh_new_keys:
701
ssh_new_keys:
489
        db SSH_MSG_NEWKEYS
702
        db SSH_MSG_NEWKEYS
490
  .length = $ - ssh_new_keys
703
  .length = $ - ssh_new_keys
Line 491... Line 704...
491
 
704
 
-
 
705
 
-
 
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
Line 492... Line 763...
492
 
763
        db ?
-
 
764
  .length = $ - ssh_channel_data
493
include_debug_strings
765
 
494
 
766
 
Line 495... Line 767...
495
 
767
include_debug_strings
496
; import
768
 
497
align 4
769
align 4
Line 498... Line 770...
498
@IMPORT:
770
@IMPORT:
499
 
771
 
500
library network, 'network.obj', \
772
library network, 'network.obj', \
501
        console, 'console.obj';, \
773
        console, 'console.obj', \
Line 516... Line 788...
516
        con_getch2, 'con_getch2', \
788
        con_getch2, 'con_getch2', \
517
        con_set_cursor_pos, 'con_set_cursor_pos', \
789
        con_set_cursor_pos, 'con_set_cursor_pos', \
518
        con_write_string, 'con_write_string', \
790
        con_write_string, 'con_write_string', \
519
        con_get_flags,  'con_get_flags'
791
        con_get_flags,  'con_get_flags'
Line 520... Line 792...
520
 
792
 
-
 
793
import  libcrash, \
-
 
794
        sha256_init, 'sha256_init', \
521
;import  libcrash, \
795
        sha256_update, 'sha256_update', \
Line 522... Line 796...
522
;        crash.hash, 'crash_hash'
796
        sha256_final, 'sha256_final'
Line 523... Line 797...
523
 
797
 
Line 524... Line -...
524
IncludeIGlobals
-
 
525
 
-
 
526
i_end:
-
 
527
 
-
 
528
decrypt_proc    dd dummy_encrypt
-
 
529
encrypt_proc    dd dummy_encrypt
-
 
530
rx_blocksize    dd 4
-
 
531
tx_blocksize    dd 4
798
IncludeIGlobals
Line 532... Line -...
532
rx_context      dd ?
-
 
533
tx_context      dd ?
-
 
534
 
-
 
535
IncludeUGlobals
-
 
536
 
-
 
537
socketnum       dd ?
-
 
538
rx_packet_length dd ?   ;;;;;
-
 
539
rx_buffer:      rb BUFFERSIZE+1
799
 
540
tx_buffer:      rb BUFFERSIZE+1
-
 
541
 
-
 
542
send_data       dw ?
-
 
543
 
-
 
544
hostname        rb 1024
-
 
545
 
-
 
546
; Diffie Hellman variables
-
 
547
dh_p            dd ?
-
 
548
                rb MAX_BITS/8
-
 
549
dh_g            dd ?
-
 
550
                rb MAX_BITS/8
-
 
551
dh_x            dd ?
-
 
552
                rb MAX_BITS/8
-
 
553
dh_e            dd ?
-
 
554
                rb MAX_BITS/8
-
 
555
dh_f            dd ?
-
 
556
                rb MAX_BITS/8
-
 
557
 
-
 
558
dh_signature    dd ?
-
 
559
                rb MAX_BITS/8
-
 
Line 560... Line -...
560
 
-
 
561
; Output from key exchange
-
 
562
dh_K            dd ?            ; Shared Secret (Big endian)
-
 
563
                rb MAX_BITS/8
-
 
564
  .length       dd ?            ; Length in little endian
-
 
565
 
800
i_end:
566
dh_H            rb 32           ; Exchange Hash
-
 
567
session_id      rb 32
-
 
Line 568... Line 801...
568
rx_iv           rb 32           ; Rx initialisation vector
801
 
569
tx_iv           rb 32           ; Tx initialisation vector
802
IncludeUGlobals
Line -... Line 803...
-
 
803
 
570
rx_enc_key      rb 32           ; Rx encryption key
804
params          rb 1024