V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
kingphar
V2EX  ›  问与答

ssh 密钥登录的时候,服务端是怎么判断客户端是 authorized_keys 文件里的哪一个公钥的?

  •  
  •   kingphar · 2016-12-28 23:44:56 +08:00 · 3898 次点击
    这是一个创建于 2673 天前的主题,其中的信息可能已经有所发展或是发生改变。

    一直以为在 session key 生成后,客户机用 session key 加密传给服务机的是客户机公钥,然后服务机 session key 解密后得到公钥,比对服务机里的~/.ssh/authorized_keys 里的公钥,找到就用它加密随机数给客户机,客户机再用自身私钥解密得到随机数,最后用 session key 加密传回服务机,完成比对即可密钥登录。

    基于上面的理解,一直用 secureCRT 登录的时候用的 identity.pub (指定文件)。但今天突然发现,指定文件填 identity(私钥,不带.pub)就可以登录, identity.pub 删掉也没事。

    所以 session key 生成后,客户机传给服务机的是什么?

    google 了一下: https://www.digitalocean.com/community/tutorials/understanding-the-ssh-encryption-and-connection-process 这篇文章有描述这个过程。

    里面的部分描述:在 session key (会话密钥)生成后
    1.The client begins by sending an ID for the key pair it would like to authenticate with to the server.
    2.The server check's the authorized_keys file of the account that the client is attempting to log into for the key ID.
    3.If a public key with matching ID is found in the file, the server generates a random number and uses the public key to encrypt the number.
    4.The server sends the client this encrypted message.
    5.If the client actually has the associated private key, it will be able to decrypt the message using that key, revealing the original number.
    6.The client combines the decrypted number with the shared session key that is being used to encrypt the communication, and calculates the MD5 hash of this value.
    7.The client then sends this MD5 hash back to the server as an answer to the encrypted number message.
    8.The server uses the same shared session key and the original number that it sent to the client to calculate the MD5 value on its own. It compares its own calculation to the one that the client sent back. If these two values match, it proves that the client was in possession of the private key and the client is authenticated.

    步骤 1 的 sending an ID for the key pair.这个 ID 指的啥?欲登录的用户名?如果是欲登录用户名,那 authorized_keys 文件里存在多个这个用户名的情况呢?

    所以服务端到底是怎么判断客户端是 authorized_keys 文件里的哪一个公钥的?

    6 条回复    2016-12-29 14:32:02 +08:00
    Citrus
        1
    Citrus  
       2016-12-28 23:48:25 +08:00 via iPhone   ❤️ 1
    先补充一个知识,私钥可以导出公钥,反之不行。回答那句为啥公钥删了也能用。
    BSD
        2
    BSD  
       2016-12-28 23:52:15 +08:00   ❤️ 1
    文件里面有 id@hostname
    kingphar
        3
    kingphar  
    OP
       2016-12-28 23:58:34 +08:00 via iPhone
    @Citrus 意思是通过私钥能推算出公钥的值?

    @BSD 那要是服务器的 authorized_keys 里有多个这个 id 的 key 串呢?如何筛选是哪个
    tatsuteng
        4
    tatsuteng  
       2016-12-29 00:06:28 +08:00   ❤️ 1
    私玥本身就包含了完整的公玥信息,可以直接导出,不需要推算;文件里的 id@hostname 只起到注释的作用; key pair ID 指的是密钥对的 ID ,也就是公玥指纹吧
    lhbc
        5
    lhbc  
       2016-12-29 00:20:41 +08:00   ❤️ 2
    通过指纹

    openssl rsa -noout -text -in 私钥
    openssl x509 -noout -text -in 公钥
    查看 Module ,两者是一致的

    用 Module 生成指纹,然后比较指纹就可以匹配了
    lhbc
        6
    lhbc  
       2016-12-29 14:32:02 +08:00   ❤️ 1
    @kingphar
    @techmoe
    不好意思,拼错单词, Module --> Modulus

    对于 RSA 密钥对,用以下命令查看 modulus
    openssl rsa -noout -modulus -in <key_file> | openssl sha256
    openssl x509 -noout -modulus -in <crt_file> | openssl sha256

    ECDSA 密钥对,用以下命令查看 pub key
    openssl ec -noout -pubout -in <key_file>
    openssl x509 -noout -pubkey -in <crt_file>

    对于 SSH 的密钥对,私钥是包含公钥的
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   986 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 19:56 · PVG 03:56 · LAX 12:56 · JFK 15:56
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.