Subversion Repositories Kolibri OS

Rev

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

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