In this question I asked, why the SSH-Host key shown when connecting to a gitlab repository does not match the one shown on the instance configuration page. Nevertheless, the accepted answer says that a man-in-the-middle attack seems very much unlikely (and I agree on that).

However, just out of interest, suppose there was such an attack at place. This seems to be preventable if git uses the repository's deploy keys or the user's ssh-keys for encrypting the connection. That is, because I can log in on gitlab via an https connection (which then I know to be secure) and place the keys there. Therefore, an attacker would be unable to decrypt the data transmissions.

My question is therefore: Does git use the ssh user keys or the repository's deploy keys for encryption?

2 Answers 2


tl;dr: No, this will not work. The fact that the server knows your public key does not authenticate the server, even if you try to keep the public key secret.

This seems to be preventable if git uses the repository's deploy keys or the user's ssh-keys for encrypting the connection

Several issues here:

  1. git has no such concept as a "repository deploy key", this is part of GitLab. It appears to be simply a public key configured to allow read-only access to the repository, intended for use in code deployment (you may already understand this, but your question isn't fully clear to me).
  2. Using your ssh key to connect to something authenticates you by proving that you have the private key. It doesn't authenticate the server. This is why host keys are necessary.
  3. ssh keys aren't used to encrypt the connection, only to authenticated an already established secure connection (establishing the secure connection is a separate part of the ssh protocol)

Public keys are called public for a reason, you should never assume it to be private even if you just generated it to add to GitLab over HTTPS and don't use it anywhere else.

ssh authentication involves sending the public key to the server to see if it is allowed (called "public key blob" in RFC 4252 section 7). This is done after establishing a secure connection so that it can't be intercepted, but that doesn't matter if the session was established with a malicious server. An impostor could simply reply with "Why yes, I will accept that key to authenticate that user!" and fool you into thinking it's the legitimate server. The only correct way to authenticate a server with ssh is by using its host key.


No. Git does not encrypt any data directly, and depends on the transport layer for encryption of data in transit. (SSH or HTTPS in most cases.)

You must log in to answer this question.

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