Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ;    ssh_transport.inc - SSH transport layer
  2. ;
  3. ;    Copyright (C) 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 <http://www.gnu.org/licenses/>.
  17.  
  18. struct  ssh_header
  19.         length          dd ?
  20.         padding         db ?
  21.         message_code    db ?
  22. ends
  23.  
  24. proc dummy_encrypt _key, _in, _out
  25.  
  26.         ret
  27. endp
  28.  
  29. proc ssh_recv_packet sock, buf, size, flags
  30.  
  31. locals
  32.         bufferptr       dd ?
  33.         remaining       dd ?
  34.         padding         dd ?
  35. endl
  36.  
  37.         DEBUGF  1, "ssh_recv_packet\n"
  38. ; Receive first block (Read length, padding length, message code)
  39.         mcall   recv, [sock], [buf], [rx_blocksize], [flags]
  40.         DEBUGF  1, "chunk = %u\n", eax
  41.         cmp     eax, [rx_blocksize]
  42.         jne     .fail       ;;;;
  43.  
  44. ;        stdcall [decrypt_proc], [rx_context], [buf], [buf]
  45.  
  46.         mov     ebx, [buf]
  47.         movzx   eax, [ebx+ssh_header.padding]
  48.         mov     [padding], eax
  49.         mov     eax, [ebx+ssh_header.length]
  50.         bswap   eax                                             ; length to little endian
  51.         mov     [ebx+ssh_header.length], eax
  52.         DEBUGF  1, "ssh_recv_packet length = %u\n", eax
  53.  
  54.         cmp     eax, [size]
  55.         ja      .fail       ;;;;
  56.  
  57.         sub     eax, [rx_blocksize]
  58.         add     eax, 4
  59.         mov     [remaining], eax
  60.         add     ebx, [rx_blocksize]
  61.         mov     [bufferptr], ebx
  62.   .receive_loop:
  63.         mcall   recv, [sock], [bufferptr], [remaining], 0
  64.         DEBUGF  1, "chunk = %u\n", eax
  65.         cmp     eax, 0
  66.         jbe     .fail
  67.         add     [bufferptr], eax
  68.         sub     [remaining], eax
  69.         ja      .receive_loop
  70.  
  71. ;  .decrypt_loop:
  72. ;        stdcall [decrypt_proc], [rx_context], [buf], [buf]
  73. ;        ja      .decrypt_loop
  74.  
  75. ;  .hmac_loop:
  76. ; TODO
  77. ;        ja      .hmac_loop
  78.  
  79. ; Return usefull data length in eax
  80.         mov     eax, [buf]
  81.         movzx   ebx, [eax+ssh_header.padding]
  82.         mov     eax, [eax+ssh_header.length]
  83.         sub     eax, ebx
  84.         DEBUGF  1, "ssh_recv_packet complete, usefull data length=%u\n", eax
  85.         ret
  86.  
  87.   .fail:
  88.         DEBUGF  1, "ssh_recv_packet failed!\n"
  89.         mov     eax, -1
  90.         ret
  91.  
  92. endp
  93.  
  94.  
  95. proc ssh_send_packet sock, buf, payloadsize, flags
  96.  
  97. locals
  98.         size    dd ?
  99. endl
  100.         DEBUGF  1, "ssh_send_packet: size=%u\n", [payloadsize]
  101.  
  102.         mov     eax, [payloadsize]
  103.         inc     eax             ; padding length byte
  104.  
  105.         lea     edx, [eax+4]    ; total packet size (without padding)
  106.         mov     [size], edx
  107.         mov     ebx, [tx_blocksize]
  108.         dec     ebx
  109.         and     edx, ebx
  110.         neg     edx
  111.         add     edx, [tx_blocksize]
  112.         cmp     edx, 4          ; minimum padding size
  113.         jae     @f
  114.         add     edx, [tx_blocksize]
  115.   @@:
  116.         DEBUGF  1, "Padding %u bytes\n", edx
  117.         add     [size], edx
  118.  
  119.         add     eax, edx
  120.         DEBUGF  1, "Total size: %u\n", eax
  121.         bswap   eax
  122.         mov     edi, tx_buffer
  123.         stosd
  124.         mov     al, dl
  125.         stosb
  126.         mov     esi, [buf]
  127. ;        cmp     esi, edi
  128. ;        je      @f
  129.         mov     ecx, [payloadsize]
  130.         rep movsb
  131. ;  @@:
  132.  
  133.         mov     ebx, edx
  134.         mov     esi, edx
  135.         and     ebx, 3
  136.         jz      @f
  137.         call    MBRandom
  138.         mov     dword[edi], eax
  139.         add     edi, ebx
  140.   @@:
  141.  
  142.         shr     esi, 2
  143.   @@:
  144.         call    MBRandom
  145.         stosd
  146.         dec     esi
  147.         jnz     @r
  148.  
  149.         mcall   send, [sock], tx_buffer, [size], [flags]
  150.  
  151.         ret
  152.  
  153. endp