3

I want to setup an SFTP connection between my computer and a server.

I generated a pair of keys on my computer and wrote my public key in "authorized_keys" file on the server. I'm sure it works because when I try to connect from a computer that doesn't have my private key (I know, nobody else is supposed to have it), a password is asked.

According to this image, my server is Bob and my computer is Alice. When the server is sending a message, it uses my public key to encrypt it and I use my private key to decrypt it. But if I send a message to the server, it's not encrypted, is it? If it is, how is it encrypted?

From what I understood about asymmetrical cryptography, if I want to encrypt messages that I send, I have to generate a pair of keys on the server and put its public key in my computer's "authorized_keys" file, right?

How can I verify that the connection is secured in both way (sending and receiving)?

Thanks!

1
  • Yes it is secured and authenticated in both ways. Unless you really force a strange configuration ssh will not (anymore) set up a unauthenticated connection. You can verify it by looking at the debug output of the client or tcpdump the tcp stream and inspect the handshake messages. And BTW encrypting to the public key of the server might not be the only way to established a shared session key. You could also use keyagreement like ECDHE (which is what is usually done in addition to authenticating each party)
    – eckes
    Commented Mar 16, 2018 at 21:30

2 Answers 2

2

There are 2 major things happening in SSH connections:

Server Authentification & encryption

The server sends you his public key and you have to trust it. You could manually get it before that and in an ideal security environment you would never connect to an SSH server BEFORE you know his public key is correct. That what CAs are for, they sign a servers public key. In most SSH environments you just accept the pubkey of the server as client. This is the initial "do you want to trust this server and add it to your list?"-question. The server pubkey is stored under .ssh/known_hosts at the client in Linux systems.

The actual encryption of the connection isn't asymetical. This is a huge misconception many people have about private/public-key encryption. It would be WAY too slow. What really happens is that server and client generate a shared secret (aka a long password) which is symmetrical encryption for this one session. The client and server use asymmetrical encryption until they agree upon a shared secret. After that they switch to symmetrical encryption with this shared secret as key.

This type of encryption is the most common and called hybrid encryption, though nearly everybody (wrongly) calls it asymmetrical encryption.

An example for "real", pure asymmetrical encryption is Mail encryption with PGP, because EVERY message is encrypted asymmetrically.

Also: The shared secret isn't stored permanently, every new session a new shared secret is negociated.

Client Authentification

This is a whole different thing, this is what can be password and/or key-based authentification. The public key of the client (located under ~/.ssh/id_rsa.pub) must be present in the servers authorized_keys file (e.g. for root: /root/.ssh/authorized_keys). Before ssh-copy-id existed people would do something like

    cat ~/.ssh/id_rsa.pub | ssh root@server "cat >>  ~/.ssh/authorized_keys"

to append your key to the servers authorized_keys.

The client certificate is NOT used for encryption, just for authentication.

IMPORTANT Edit: Made post more clear regarding ssh-copy-id to prevent misunderstandings.

As of now, ssh-copy-id is the best practice way to add a clients public key to a server. I just posted the cat method to show which files are manipulated on both sides to show the connection between private and public keys and how they are stored.

When using cat there is a risk to forget a ">" for example which would overwrite your authorized_keys file (in Linux ">>" means append, ">" means overwrite). Be responsible when manipulating configuration files directly. Thanks @Rallph for pointing it out.

2
  • 1
    Though, it's simpler (and safer?) to use ssh-copy-id ~/.ssh/id_rsa.pub root@server to upload the public key on the server
    – Raphaël
    Commented Mar 15, 2018 at 10:07
  • @Rallph Yes, that's why I wrote "Before ssh-copy-id you would do..." The "traditional" method I gave as example manipulated the actual files, thus was more transparent for the user (what it does exactly). Also of course, it was more dangerous, forget one ">" and you basically wipe your authorized keys. You're right, I should've been more specific, will edit that.
    – Broco
    Commented Mar 15, 2018 at 11:03
1

The server has its own key pair. When you connect for the first time to an SSH server, you should get prompted by your SSH/SFTP client, if you trust the server's host key (=public key).

Only after you carefully verify that it's indeed a legitimate public key of the server, your connection is secure. See my article Where do I get SSH host key fingerprint to authorize the server?

Also note neither your key pair, nor the server's keypair is actually used for encrypting the data. That's a way more complicated.

See also:

2
  • Ho I see, this key is written in "known_hosts", isn't it ?
    – Raphaël
    Commented Mar 15, 2018 at 8:17
  • Yes, with OpenSSH client. Other clients can use a different cache. Commented Mar 15, 2018 at 8:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .