sshと公開鍵暗号について勉強した

ひととおりsshを理解すると
http://unoh.github.com/2010/03/12/ssh_config.html
を見て、役に立つ。

ssh公開鍵暗号をつかって通信しているので、まずはこれを抑えよう。
(厳密には共通鍵暗号を併用してます。公開鍵暗号で安全な通信を確立した後、共通鍵を共有して速い通信を行なっている)


わかっていなかったこと

githubなどでssh通信をする時、githubに複数のpublic keyを登録することができる。
クライアントから通信をはじめるとき、どのpublic keyで暗号化するのかを、どう判断しているのか? クライアントからのリクエスト時に指定、もしくはユーザー又はコンピュータを特定する情報がリクエストに含まれていて、かつ公開鍵にもふくまれていて、それで照合するのか?(もしくは単純に、githubにpublic keyを登録時にユーザーと対にしているのか?=>いや、これは、登録時のGUIからは全くしていない)
上の疑問に対するワイの答えは
クライアントがsshするとサーバー側は乱数を適当な公開鍵で暗号化し、それをクライアントに投げつける。クライアントは秘密鍵で複合し、それを送り返す。サーバー側は送り返された数字を答えとして、正解であれば通信を確立する。(ここからはただの推測、サーバー側は登録されている公開鍵を次々に試していき、正解がでたところで通信を確立する。実装として良くないから多分違う方法だろうが。。)
openSSHの仕様を見てみると、もっと正しい答えが!!
ssh時に公開鍵を送ってるっぽいね。そんでサーバーに登録されている公開鍵と照合。
このクライアントを特定する方法はいくつかあって、preferableauthntificaitonで設定できるのね。
以下、抜粋:http://www.openbsd.org/cgi-bin/man.cgi?query=ssh&sektion=1
PreferredAuthentications.

Host-based authentication works as follows: If the machine the user logs
in from is listed in /etc/hosts.equiv or /etc/shosts.equiv on the remote
machine, and the user names are the same on both sides, or if the files
~/.rhosts or ~/.shosts exist in the user's home directory on the remote
machine and contain a line containing the name of the client machine and
the name of the user on that machine, the user is considered for login.
Additionally, the server must be able to verify the client's host key
(see the description of /etc/ssh/ssh_known_hosts and ~/.ssh/known_hosts,
below) for login to be permitted. This authentication method closes
security holes due to IP spoofing, DNS spoofing, and routing spoofing.
[Note to the administrator: /etc/hosts.equiv, ~/.rhosts, and the
rlogin/rsh protocol in general, are inherently insecure and should be
disabled if security is desired.]

Public key authentication works as follows: The scheme is based on
public-key cryptography, using cryptosystems where encryption and
decryption are done using separate keys, and it is unfeasible to derive
the decryption key from the encryption key. The idea is that each user
creates a public/private key pair for authentication purposes. The
server knows the public key, and only the user knows the private key.
ssh implements public key authentication protocol automatically, using
one of the DSA, ECDSA or RSA algorithms. Protocol 1 is restricted to
using only RSA keys, but protocol 2 may use any. The HISTORY section of
ssl(8) contains a brief discussion of the DSA and RSA algorithms.

The file ~/.ssh/authorized_keys lists the public keys that are permitted
for logging in. When the user logs in, the ssh program tells the server
which key pair it would like to use for authentication. The client
proves that it has access to the private key and the server checks that
the corresponding public key is authorized to accept the account.

The user creates his/her key pair by running ssh-keygen(1). This stores
the private key in ~/.ssh/identity (protocol 1), ~/.ssh/id_dsa (protocol
2 DSA), ~/.ssh/id_ecdsa (protocol 2 ECDSA), or ~/.ssh/id_rsa (protocol 2
RSA) and stores the public key in ~/.ssh/identity.pub (protocol 1),
~/.ssh/id_dsa.pub (protocol 2 DSA), ~/.ssh/id_ecdsa.pub (protocol 2
ECDSA), or ~/.ssh/id_rsa.pub (protocol 2 RSA) in the user's home
directory. The user should then copy the public key to
~/.ssh/authorized_keys in his/her home directory on the remote machine.
The authorized_keys file corresponds to the conventional ~/.rhosts file,
and has one key per line, though the lines can be very long. After this,
the user can log in without giving the password.

図解:その一


他の参照したサイト:
sshコマンドを端的に説明しているところが役に立った。
http://osksn2.hep.sci.osaka-u.ac.jp/~naga/miscellaneous/winssh/winssha-2.html
openSSH仕様
http://www.openbsd.org/cgi-bin/man.cgi?query=ssh&sektion=1