2021-08-02 20:40:01 +02:00
|
|
|
; sshlib_transport.inc - SSH transport layer
|
|
|
|
;
|
2024-03-06 21:22:01 +01:00
|
|
|
; Copyright (C) 2016-2024 Jeffrey Amelynck
|
2021-08-02 20:40:01 +02:00
|
|
|
;
|
|
|
|
; This program is free software: you can redistribute it and/or modify
|
|
|
|
; it under the terms of the GNU General Public License as published by
|
|
|
|
; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
; (at your option) any later version.
|
|
|
|
;
|
|
|
|
; This program is distributed in the hope that it will be useful,
|
|
|
|
; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
; GNU General Public License for more details.
|
|
|
|
;
|
|
|
|
; You should have received a copy of the GNU General Public License
|
|
|
|
; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
align 16
|
|
|
|
sshlib_recv_packet: ; con_ptr, flags
|
2021-08-02 20:40:01 +02:00
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
mov eax, [esp+4]
|
|
|
|
jmp [eax+sshlib_connection.rx_proc]
|
2021-08-02 20:40:01 +02:00
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
align 16
|
2024-03-09 21:05:21 +01:00
|
|
|
sshlib_send_packet: ; con_ptr, buf, payload_size, flags
|
2021-08-02 20:40:01 +02:00
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
mov eax, [esp+4]
|
|
|
|
jmp [eax+sshlib_connection.tx_proc]
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
align 16
|
|
|
|
proc sshlib_recv_packet_clear con_ptr, flags
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
locals
|
|
|
|
data_length dd ? ; Total length of packet without MAC
|
|
|
|
endl
|
|
|
|
|
|
|
|
DEBUGF 3, "> "
|
|
|
|
; Receive first block (Read length, padding length, message code)
|
|
|
|
mov ebx, [con_ptr]
|
2024-03-06 21:22:01 +01:00
|
|
|
mov ecx, [ebx + sshlib_connection.socketnum]
|
|
|
|
mov esi, 4
|
|
|
|
lea edx, [ebx + sshlib_connection.rx_buffer]
|
2021-08-02 20:40:01 +02:00
|
|
|
mov edi, [flags]
|
|
|
|
mcall recv
|
|
|
|
cmp eax, 0
|
|
|
|
jle .sock_fail
|
|
|
|
sub [ssh_chan.rcv_wnd], eax ;;; FIXME
|
|
|
|
DEBUGF 1, "chunk = %u ", eax
|
|
|
|
mov ebx, [con_ptr]
|
2024-03-06 21:22:01 +01:00
|
|
|
cmp eax, 4
|
2021-08-02 20:40:01 +02:00
|
|
|
jne .proto_fail ; TODO: handle receives of 1, 2, and 3 bytes correctly
|
|
|
|
|
|
|
|
; Check data length
|
|
|
|
mov esi, [ebx + sshlib_connection.rx_buffer.packet_length]
|
|
|
|
bswap esi ; convert length to little endian
|
2024-03-06 21:22:01 +01:00
|
|
|
mov [ebx + sshlib_connection.rx_buffer.packet_length], esi
|
2021-08-02 20:40:01 +02:00
|
|
|
DEBUGF 1, "packet length=%u ", esi
|
|
|
|
cmp esi, BUFFERSIZE
|
|
|
|
ja .proto_fail ; packet is too large
|
2024-03-06 21:22:01 +01:00
|
|
|
test ecx, ecx
|
|
|
|
jz .proto_fail
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
; Receive remaining data
|
2024-03-06 21:22:01 +01:00
|
|
|
lea edx, [ebx + sshlib_connection.rx_buffer]
|
|
|
|
add edx, 4
|
|
|
|
mov ecx, [ebx + sshlib_connection.socketnum]
|
2021-08-02 20:40:01 +02:00
|
|
|
mov edi, [flags]
|
|
|
|
.receive_loop:
|
|
|
|
DEBUGF 3, "want %d bytes.. ", esi
|
|
|
|
mcall recv
|
|
|
|
cmp eax, 0
|
|
|
|
jle .sock_fail
|
|
|
|
sub [ssh_chan.rcv_wnd], eax ;;; FIXME
|
|
|
|
DEBUGF 3, "got %d bytes\n", eax
|
|
|
|
add edx, eax
|
|
|
|
sub esi, eax
|
|
|
|
jnz .receive_loop
|
2024-03-06 21:22:01 +01:00
|
|
|
.packet_complete:
|
2021-08-02 20:40:01 +02:00
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
; Return useful data length to the caller via eax register
|
2021-08-02 20:40:01 +02:00
|
|
|
mov ebx, [con_ptr]
|
2024-03-06 21:22:01 +01:00
|
|
|
mov eax, [ebx + sshlib_connection.rx_buffer.packet_length]
|
|
|
|
movzx ebx, [ebx + sshlib_connection.rx_buffer.padding_length]
|
|
|
|
sub eax, ebx
|
2021-08-02 20:40:01 +02:00
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
; Update sequence counter
|
|
|
|
mov ebx, [con_ptr]
|
|
|
|
add byte[ebx + sshlib_connection.rx_mac_seqnr+3], 1
|
|
|
|
adc byte[ebx + sshlib_connection.rx_mac_seqnr+2], 0
|
|
|
|
adc byte[ebx + sshlib_connection.rx_mac_seqnr+1], 0
|
|
|
|
adc byte[ebx + sshlib_connection.rx_mac_seqnr+0], 0
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
DEBUGF 1, "useful data length=%u\n", eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.sock_fail:
|
|
|
|
DEBUGF 3, "ssh_recv_packet failed!\n"
|
|
|
|
mov eax, SSHLIB_ERR_SOCKET
|
|
|
|
ret
|
|
|
|
|
|
|
|
.proto_fail:
|
|
|
|
DEBUGF 3, "ssh_recv_packet protocol failure!\n"
|
|
|
|
mov eax, SSHLIB_ERR_PROTOCOL
|
|
|
|
xor ebx, ebx
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
align 16
|
|
|
|
proc sshlib_send_packet_clear con_ptr, buf, payload_size, flags
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
locals
|
|
|
|
packet_size dd ?
|
|
|
|
endl
|
|
|
|
DEBUGF 2, "< "
|
|
|
|
|
|
|
|
; Check how many bytes we should pad
|
|
|
|
mov eax, [payload_size]
|
|
|
|
inc eax ; padding length byte
|
|
|
|
lea edx, [eax+4] ; total packet size (without padding and MAC)
|
|
|
|
mov [packet_size], edx
|
|
|
|
|
|
|
|
mov ecx, [con_ptr]
|
|
|
|
mov ebx, [ecx+sshlib_connection.tx_pad_size]
|
|
|
|
dec ebx
|
|
|
|
and edx, ebx
|
|
|
|
neg edx
|
|
|
|
add edx, [ecx+sshlib_connection.tx_pad_size]
|
|
|
|
add edx, [ecx+sshlib_connection.tx_pad_size]
|
|
|
|
DEBUGF 1, "padding %u bytes ", edx
|
|
|
|
add [packet_size], edx ; total packet size with padding
|
|
|
|
|
|
|
|
; Start building the packet
|
|
|
|
; First comes the packet length, in network byte order ofcourse.
|
|
|
|
add eax, edx
|
2024-03-06 21:22:01 +01:00
|
|
|
DEBUGF 2, "total size: %u ", eax
|
2021-08-02 20:40:01 +02:00
|
|
|
bswap eax
|
|
|
|
lea edi, [ecx+sshlib_connection.tx_buffer]
|
|
|
|
stosd
|
|
|
|
; Then the padding length
|
|
|
|
mov al, dl
|
|
|
|
stosb
|
|
|
|
; And the actual payload bytes
|
|
|
|
mov esi, [buf]
|
|
|
|
mov ecx, [payload_size]
|
|
|
|
rep movsb
|
|
|
|
|
|
|
|
; Append the packet with #edx padding bytes.
|
|
|
|
; Since we must pad at least 8 bytes, we can always use DWORD writes.
|
|
|
|
; First do an (unaligned) write exactly following the data
|
|
|
|
dec edx
|
|
|
|
mov esi, edx
|
|
|
|
shr esi, 2 ; number dwords
|
|
|
|
mov ebx, edx
|
|
|
|
and ebx, 3
|
|
|
|
inc ebx ; number bytes in first write (1-4)
|
2024-03-06 21:22:01 +01:00
|
|
|
mov dword[edi], 0
|
2021-08-02 20:40:01 +02:00
|
|
|
add edi, ebx
|
|
|
|
; Then, do as many aligned writes as nescessary
|
2024-03-06 21:22:01 +01:00
|
|
|
xor eax, eax
|
2021-08-02 20:40:01 +02:00
|
|
|
@@:
|
|
|
|
stosd
|
|
|
|
dec esi
|
|
|
|
jnz @r
|
|
|
|
|
|
|
|
; Send the packet
|
|
|
|
mov ebx, [con_ptr]
|
2024-03-06 21:22:01 +01:00
|
|
|
mov ecx, [ebx + sshlib_connection.socketnum]
|
|
|
|
lea edx, [ebx + sshlib_connection.tx_buffer]
|
2021-08-02 20:40:01 +02:00
|
|
|
mov esi, [packet_size]
|
|
|
|
mov edi, [flags]
|
|
|
|
mcall send
|
|
|
|
|
2024-03-06 21:22:01 +01:00
|
|
|
; Update sequence counter
|
|
|
|
mov ebx, [con_ptr]
|
|
|
|
add byte[ebx + sshlib_connection.tx_mac_seqnr+3], 1
|
|
|
|
adc byte[ebx + sshlib_connection.tx_mac_seqnr+2], 0
|
|
|
|
adc byte[ebx + sshlib_connection.tx_mac_seqnr+1], 0
|
|
|
|
adc byte[ebx + sshlib_connection.tx_mac_seqnr+0], 0
|
|
|
|
|
|
|
|
DEBUGF 2, "\n"
|
2021-08-02 20:40:01 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
endp
|
|
|
|
|