Subversion Repositories Kolibri OS

Rev

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

  1. ;    sshlib_host.inc - SSH remote host authentication
  2. ;
  3. ;    Copyright (C) 2021 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. ; https://datatracker.ietf.org/doc/html/rfc4253#section-6.6
  19. ; https://datatracker.ietf.org/doc/html/rfc3447
  20.  
  21. proc sshlib_host_verify  con_ptr, str_host_key, str_signature, message, message_len
  22.  
  23. locals
  24.         known_key_sz rb MAX_PUBLIC_KEY_SIZE
  25. endl
  26.  
  27.         mov     eax, [con_ptr]
  28.         cmp     [eax+sshlib_connection.algo_hostkey], SSHLIB_HOSTKEY_RSA
  29.         je      .rsa
  30.         ; ..add more here
  31.         mov     eax, SSHLIB_ERR_HKEY_NO_ALGO
  32.         ret
  33.  
  34.   .rsa:
  35.         stdcall sshlib_host_verify_rsa, [str_host_key], [str_signature], [message], [message_len]
  36.         test    eax, eax
  37.         jnz     .err
  38.  
  39.   .lookup:
  40. ;        lea     eax, [known_key_sz]
  41. ;        mov     ebx, [con_ptr]
  42. ;        lea     ebx, [ebx + sshlib_connection.hostname_sz]
  43. ;        invoke  ini_get_str, known_hosts_file, ebx, ssh_rsa_sz, eax, MAX_PUBLIC_KEY_SIZE, null_sz
  44. ;        test    eax, eax
  45. ;        jnz     .unknown
  46.  
  47. ; TODO: verify cached host key
  48. ;        jne     .mismatch
  49.  
  50.         jmp     .unknown        ; FIXME
  51.  
  52.         xor     eax, eax
  53.         ret
  54.  
  55.   .mismatch:
  56.         lea     eax, [known_key_sz]
  57.         stdcall sshlib_callback_hostkey_problem, [con_ptr], SSHLIB_HOSTKEY_PROBLEM_MISMATCH, eax
  58.         cmp     eax, SSHLIB_HOSTKEY_ACCEPT
  59.         je      .store
  60.         ret
  61.  
  62.   .unknown:
  63.         lea     eax, [known_key_sz]
  64.         stdcall sshlib_callback_hostkey_problem, [con_ptr], SSHLIB_HOSTKEY_PROBLEM_UNKNOWN, eax
  65.         cmp     eax, SSHLIB_HOSTKEY_ACCEPT
  66.         je      .store
  67.         ret
  68.  
  69.   .store:
  70. ; TODO: write to know_hosts file and fall-through
  71.  
  72.         ret
  73.  
  74.   .err:
  75.         ret
  76.  
  77. endp
  78.  
  79.  
  80. ; https://datatracker.ietf.org/doc/html/rfc3447#section-8.2.2
  81. ; RSASSA-PKCS1-V1_5-VERIFY
  82. proc sshlib_host_verify_rsa str_host_key, str_signature, M, message_len
  83.  
  84. locals
  85.         h_ctx                   dd ?
  86.  
  87. ; Signer's RSA public key
  88.         mpint_e                 dd ?    ; public exponent
  89.         mpint_n                 dd ?    ; modulus
  90.  
  91.         mpint_m                 dd ?
  92.  
  93.         EM                      dd ?
  94.         EM_accent               dd ?
  95.  
  96.         mpint_s                 dd ?    ; rsa_signature_blob
  97.  
  98.  
  99. ;        k       dd ?    ; length of RSA modulus n
  100.  
  101. endl
  102.  
  103.         DEBUGF  3, "SSH: Performing RSA verification\n"
  104.  
  105.         mcall   68, 12, sizeof.crash_ctx + 5*(MAX_BITS/8+4)
  106.         test    eax, eax
  107.         jz      .err_nomem
  108.         mov     [h_ctx], eax
  109.         add     eax, sizeof.crash_ctx
  110.         mov     [mpint_e], eax
  111.         add     eax, MAX_BITS/8+4
  112.         mov     [mpint_n], eax
  113.         add     eax, MAX_BITS/8+4
  114.         mov     [mpint_m], eax
  115.         add     eax, MAX_BITS/8+4
  116.         mov     [EM], eax
  117.         add     eax, MAX_BITS/8+4
  118.         mov     [EM_accent], eax
  119.         add     eax, MAX_BITS/8+4
  120.         mov     [mpint_s], eax
  121. ;        add     eax, MAX_BITS/8+4
  122.  
  123. ; Host key
  124.         mov     esi, [str_host_key]
  125.         mov     ecx, [esi]
  126.         bswap   ecx
  127.         cmp     ecx, MAX_PUBLIC_KEY_SIZE
  128.         ja      .err_key
  129. ; Host key type (string)
  130.         cmp     dword[esi+4], 0x07000000
  131.         jne     .err_key
  132.         cmp     dword[esi+8], 'ssh-'
  133.         jne     .err_key
  134.         cmp     dword[esi+11], '-rsa'
  135.         jne     .err_key
  136.         add     esi, 4+4+7
  137. ; mpint e
  138.         stdcall mpint_to_little_endian, [mpint_e], esi
  139.         add     esi, eax
  140.         add     esi, 4
  141. ; mpint n
  142.         stdcall mpint_to_little_endian, [mpint_n], esi
  143. ;        mov     [k], eax             ;; HMMMM FIXME, 0-byte..
  144.  
  145. ; Signature
  146.         mov     esi, [str_signature]
  147.         mov     ecx, [esi]
  148.         bswap   ecx             ; TODO: check length
  149. ; Host key type (string)
  150.         cmp     dword[esi+4], 0x07000000
  151.         jne     .err_signature
  152.         cmp     dword[esi+8], 'ssh-'
  153.         jne     .err_signature
  154.         cmp     dword[esi+11], '-rsa'
  155.         jne     .err_signature
  156.         add     esi, 4+4+7
  157. ; RSA signature blob
  158.         stdcall mpint_to_little_endian, [mpint_s], esi
  159. ;        cmp     eax, [k]
  160.  ;;;       jne     .err_signature
  161.  
  162. ; RSAVP1
  163.         stdcall mpint_modexp, [mpint_m], [mpint_s], [mpint_e], [mpint_n]
  164. ; I2OSP
  165.         stdcall mpint_shrink, [mpint_m]
  166.         stdcall mpint_grow, [mpint_m], 256
  167.         stdcall mpint_to_big_endian, [EM], [mpint_m]
  168.  
  169. ; EMSA-PKCS1-v1_5
  170.         invoke  sha1_init, [h_ctx]
  171.         invoke  sha1_update, [h_ctx], [M], [message_len]
  172.         invoke  sha1_final, [h_ctx]
  173.  
  174.         mov     edi, [EM_accent]
  175.         mov     al, 0x00
  176.         stosb
  177.         mov     al, 0x01
  178.         stosb
  179.         mov     ecx, 256 - (rsa_sha1_t.len + 3 + SHA1_HASH_SIZE)
  180.         mov     al, 0xff
  181.         rep stosb
  182.         mov     al, 0x00
  183.         stosb
  184.         mov     esi, rsa_sha1_t
  185.         mov     ecx, rsa_sha1_t.len
  186.         rep movsb
  187.         mov     esi, [h_ctx]
  188.         mov     ecx, SHA1_HASH_SIZE
  189.         rep movsb
  190.  
  191. ; Compare EM with EM_accent
  192.         mov     esi, [EM]
  193.         add     esi, 4
  194.         mov     edi, [EM_accent]
  195.         mov     ecx, 256/4
  196.         xor     eax, eax
  197.   .ct_cmp_loop:
  198.         mov     ebx, [esi]
  199.         xor     ebx, [edi]
  200.         or      eax, ebx
  201.         lea     esi, [esi+4]
  202.         lea     edi, [edi+4]
  203.         dec     ecx
  204.         jnz     .ct_cmp_loop
  205.  
  206.         push    eax
  207.         mcall   68, 13, [h_ctx]
  208.         pop     eax
  209.  
  210.         test    eax, eax
  211.         jnz     .fail
  212.  
  213.         DEBUGF  3, "SSH: RSA verification OK!\n"
  214.  
  215.         ret
  216.  
  217.   .fail:
  218.         DEBUGF  3, "SSH: RSA verification failed!\n"
  219.         mov     eax, SSHLIB_ERR_HKEY_VERIFY_FAIL
  220.         ret
  221.  
  222.   .err_nomem:
  223.         mov     eax, SSHLIB_ERR_NOMEM
  224.         ret
  225.  
  226.   .err_signature:
  227.         mov     eax, SSHLIB_ERR_HKEY_SIGNATURE
  228.         ret
  229.  
  230.   .err_key:
  231.         mov     eax, SSHLIB_ERR_HKEY_PUBLIC_KEY
  232.         ret
  233.  
  234. endp
  235.  
  236.  
  237. iglobal
  238.  
  239.         rsa_sha1_t      db 0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x0E, 0x03, 0x02, 0x1A, 0x05, 0x00, 0x04, 0x14
  240.         .len = $ - rsa_sha1_t
  241.  
  242. endg
  243.  
  244.