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

2016-12-28 23:44:56 +08:00
 kingphar

一直以为在 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 文件里的哪一个公钥的?

3905 次点击
所在节点    问与答
6 条回复
Citrus
2016-12-28 23:48:25 +08:00
先补充一个知识,私钥可以导出公钥,反之不行。回答那句为啥公钥删了也能用。
BSD
2016-12-28 23:52:15 +08:00
文件里面有 id@hostname
kingphar
2016-12-28 23:58:34 +08:00
@Citrus 意思是通过私钥能推算出公钥的值?

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

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

用 Module 生成指纹,然后比较指纹就可以匹配了
lhbc
2016-12-29 14:32:02 +08:00
@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 的密钥对,私钥是包含公钥的

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/330883

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX