Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
9106 hidnplayr 1
;    sshlib_transport.inc - SSH transport layer
6419 hidnplayr 2
;
9987 hidnplayr 3
;    Copyright (C) 2016-2024 Jeffrey Amelynck
6419 hidnplayr 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
 
6469 hidnplayr 18
 
9987 hidnplayr 19
align 16
20
sshlib_recv_packet:     ; con_ptr, flags
6469 hidnplayr 21
 
9987 hidnplayr 22
        mov     eax, [esp+4]
23
        jmp     [eax+sshlib_connection.rx_proc]
6419 hidnplayr 24
 
9987 hidnplayr 25
align 16
26
sshlib_send_packet:     ; con_ptr, flags
6419 hidnplayr 27
 
9987 hidnplayr 28
        mov     eax, [esp+4]
29
        jmp     [eax+sshlib_connection.tx_proc]
9106 hidnplayr 30
 
31
 
9987 hidnplayr 32
align 16
33
proc sshlib_recv_packet_clear con_ptr, flags
9071 hidnplayr 34
 
6419 hidnplayr 35
locals
6469 hidnplayr 36
        data_length     dd ?    ; Total length of packet without MAC
6419 hidnplayr 37
endl
38
 
9106 hidnplayr 39
        DEBUGF  3, "> "
6419 hidnplayr 40
; Receive first block (Read length, padding length, message code)
9106 hidnplayr 41
        mov     ebx, [con_ptr]
9987 hidnplayr 42
        mov     ecx, [ebx + sshlib_connection.socketnum]
43
        mov     esi, 4
44
        lea     edx, [ebx + sshlib_connection.rx_buffer]
6469 hidnplayr 45
        mov     edi, [flags]
46
        mcall   recv
9106 hidnplayr 47
        cmp     eax, 0
48
        jle     .sock_fail
49
        sub     [ssh_chan.rcv_wnd], eax  ;;; FIXME
6469 hidnplayr 50
        DEBUGF  1, "chunk = %u ", eax
9106 hidnplayr 51
        mov     ebx, [con_ptr]
9987 hidnplayr 52
        cmp     eax, 4
9106 hidnplayr 53
        jne     .proto_fail     ; TODO: handle receives of 1, 2, and 3 bytes correctly
6419 hidnplayr 54
 
6469 hidnplayr 55
; Check data length
9106 hidnplayr 56
        mov     esi, [ebx + sshlib_connection.rx_buffer.packet_length]
6469 hidnplayr 57
        bswap   esi                                             ; convert length to little endian
9987 hidnplayr 58
        mov     [ebx + sshlib_connection.rx_buffer.packet_length], esi
6469 hidnplayr 59
        DEBUGF  1, "packet length=%u ", esi
60
        cmp     esi, BUFFERSIZE
9106 hidnplayr 61
        ja      .proto_fail                                     ; packet is too large
9987 hidnplayr 62
        test    ecx, ecx
63
        jz      .proto_fail
6419 hidnplayr 64
 
6469 hidnplayr 65
; Receive remaining data
9987 hidnplayr 66
        lea     edx, [ebx + sshlib_connection.rx_buffer]
67
        add     edx, 4
68
        mov     ecx, [ebx + sshlib_connection.socketnum]
6469 hidnplayr 69
        mov     edi, [flags]
6419 hidnplayr 70
  .receive_loop:
9106 hidnplayr 71
        DEBUGF  3, "want %d bytes.. ", esi
6469 hidnplayr 72
        mcall   recv
6419 hidnplayr 73
        cmp     eax, 0
9106 hidnplayr 74
        jle     .sock_fail
75
        sub     [ssh_chan.rcv_wnd], eax             ;;; FIXME
76
        DEBUGF  3, "got %d bytes\n", eax
6469 hidnplayr 77
        add     edx, eax
78
        sub     esi, eax
79
        jnz     .receive_loop
9987 hidnplayr 80
  .packet_complete:
6419 hidnplayr 81
 
9987 hidnplayr 82
; Return useful data length to the caller via eax register
9106 hidnplayr 83
        mov     ebx, [con_ptr]
9987 hidnplayr 84
        mov     eax, [ebx + sshlib_connection.rx_buffer.packet_length]
85
        movzx   ebx, [ebx + sshlib_connection.rx_buffer.padding_length]
86
        sub     eax, ebx
6419 hidnplayr 87
 
9987 hidnplayr 88
; Update sequence counter
89
        mov     ebx, [con_ptr]
90
        add     byte[ebx + sshlib_connection.rx_mac_seqnr+3], 1
91
        adc     byte[ebx + sshlib_connection.rx_mac_seqnr+2], 0
92
        adc     byte[ebx + sshlib_connection.rx_mac_seqnr+1], 0
93
        adc     byte[ebx + sshlib_connection.rx_mac_seqnr+0], 0
6419 hidnplayr 94
 
6469 hidnplayr 95
        DEBUGF  1, "useful data length=%u\n", eax
6419 hidnplayr 96
        ret
97
 
9106 hidnplayr 98
  .sock_fail:
6469 hidnplayr 99
        DEBUGF  3, "ssh_recv_packet failed!\n"
9106 hidnplayr 100
        mov     eax, SSHLIB_ERR_SOCKET
6419 hidnplayr 101
        ret
102
 
9106 hidnplayr 103
  .proto_fail:
104
        DEBUGF  3, "ssh_recv_packet protocol failure!\n"
105
        mov     eax, SSHLIB_ERR_PROTOCOL
106
        xor     ebx, ebx
107
        ret
108
 
6419 hidnplayr 109
endp
110
 
111
 
9987 hidnplayr 112
align 16
113
proc sshlib_send_packet_clear con_ptr, buf, payload_size, flags
6419 hidnplayr 114
 
115
locals
6469 hidnplayr 116
        packet_size    dd ?
6419 hidnplayr 117
endl
6469 hidnplayr 118
        DEBUGF  2, "< "
6419 hidnplayr 119
 
9071 hidnplayr 120
; Check how many bytes we should pad
6469 hidnplayr 121
        mov     eax, [payload_size]
122
        inc     eax                     ; padding length byte
123
        lea     edx, [eax+4]            ; total packet size (without padding and MAC)
124
        mov     [packet_size], edx
9071 hidnplayr 125
 
9106 hidnplayr 126
        mov     ecx, [con_ptr]
127
        mov     ebx, [ecx+sshlib_connection.tx_pad_size]
6419 hidnplayr 128
        dec     ebx
129
        and     edx, ebx
130
        neg     edx
9106 hidnplayr 131
        add     edx, [ecx+sshlib_connection.tx_pad_size]
132
        add     edx, [ecx+sshlib_connection.tx_pad_size]
6469 hidnplayr 133
        DEBUGF  1, "padding %u bytes ", edx
9071 hidnplayr 134
        add     [packet_size], edx      ; total packet size with padding
6419 hidnplayr 135
 
9071 hidnplayr 136
; Start building the packet
137
; First comes the packet length, in network byte order ofcourse.
6419 hidnplayr 138
        add     eax, edx
9987 hidnplayr 139
        DEBUGF  2, "total size: %u ", eax
6419 hidnplayr 140
        bswap   eax
9106 hidnplayr 141
        lea     edi, [ecx+sshlib_connection.tx_buffer]
9071 hidnplayr 142
        stosd
143
; Then the padding length
6419 hidnplayr 144
        mov     al, dl
9071 hidnplayr 145
        stosb
146
; And the actual payload bytes
6419 hidnplayr 147
        mov     esi, [buf]
6469 hidnplayr 148
        mov     ecx, [payload_size]
6419 hidnplayr 149
        rep movsb
150
 
9071 hidnplayr 151
; Append the packet with #edx padding bytes.
152
; Since we must pad at least 8 bytes, we can always use DWORD writes.
153
; First do an (unaligned) write exactly following the data
154
        dec     edx
155
        mov     esi, edx
156
        shr     esi, 2          ; number dwords
6419 hidnplayr 157
        mov     ebx, edx
158
        and     ebx, 3
9071 hidnplayr 159
        inc     ebx             ; number bytes in first write (1-4)
9987 hidnplayr 160
        mov     dword[edi], 0
6419 hidnplayr 161
        add     edi, ebx
9071 hidnplayr 162
; Then, do as many aligned writes as nescessary
9987 hidnplayr 163
        xor     eax, eax
6419 hidnplayr 164
  @@:
165
        stosd
166
        dec     esi
167
        jnz     @r
168
 
6469 hidnplayr 169
; Send the packet
9106 hidnplayr 170
        mov     ebx, [con_ptr]
9987 hidnplayr 171
        mov     ecx, [ebx + sshlib_connection.socketnum]
172
        lea     edx, [ebx + sshlib_connection.tx_buffer]
6469 hidnplayr 173
        mov     esi, [packet_size]
174
        mov     edi, [flags]
175
        mcall   send
176
 
9987 hidnplayr 177
; Update sequence counter
178
        mov     ebx, [con_ptr]
179
        add     byte[ebx + sshlib_connection.tx_mac_seqnr+3], 1
180
        adc     byte[ebx + sshlib_connection.tx_mac_seqnr+2], 0
181
        adc     byte[ebx + sshlib_connection.tx_mac_seqnr+1], 0
182
        adc     byte[ebx + sshlib_connection.tx_mac_seqnr+0], 0
6469 hidnplayr 183
 
9987 hidnplayr 184
        DEBUGF  2, "\n"
185
 
6419 hidnplayr 186
        ret
187
 
6469 hidnplayr 188
endp
189