I think this is not feasible.
As @Eir Nym says, you fairly obviously can't translate, for example,
an MD5-hash of a password into a SHA-256 hash, without knowing the
password.
However you would think that you'd be able to translate a SHA-256
hash (according to crypt(3)) into a SHA-256 hash (according to
OpenLDAP): surely it's just a matter of identifying the salt and hash
in one format, and reordering them into the format the other expects.
No? What, it's not?!
In OpenLDAP, the format of the userPassword
attribute, though not
quite documented, is relatively straightforward: it's the fixed-length
hash of the password followed by the salt (see eg
contrib/slapd-modules/passwd/sha2
in the OpenLDAP
sources). This is also evident in notes such as this OpenLDAP FAQ-o-matic article which shows how to generate a {SSHA}
hash from password + salt.
For systems which use crypt(3), however, you'll see ‘password hashes’
of the form
$id$salt$hash
where $5$
indicates SHA-256, $6$
indicates SHA-512, and so on. The
hash
element of that is the base64 encoded form of some
function of key, salt, and (in some variants) number of encryption
rounds, but the actual function is... not documented.
- The Wikipedia page on crypt(3)
mentions that ‘Over time various algorithms have been introduced.’
- It points to the ‘Password Hashing
Competition’
specification (PHC), which narrows down the gross format of the ‘hash’
string, but not as much as you'd hope. It doesn't even
conclusively identify what base64 variant the string uses (it
appears to, but acknowledges that characters
[.-]
can appear in
a base64 string without saying what they are).
- The passlib
library
acknowledges some of the mess (‘All of the above is guesswork
based on examination of existing hashes and OS implementations’)
and recommends the PHC format.
- A linuxquestions.org
question
indicates that the relevant base64 encoding is peculiar.
So we can look at the glibc sources
for crypt. There, we discover (in crypt_util.c
) that the base64
alphabet is indeed odd, using "./0-9A-Za-b"
rather than the more
usual (and PHC-specified) "A-Za-z0-9+/"
(with occasional variants in
the last two characters). Not helpful, but we could cope if this were
the only difference.
However, the sha256-crypt.c
function in that library (for example)
takes the key and salt and does something very odd and long-winded
with them to produce the hash
value. I wouldn't presume to venture an
opinion on whether what it's doing is sensible, but it's certainly not
straightforward.
What that means is that although the crypt(3) $5$
and OpenLDAP ‘SHA-256’
hashes are on the face of it using the same cryptographic primitive,
they are actually using it in sufficiently different ways that they
amount to different hash functions so that, as with the point at the
top, you can't translate one to the other without knowing the
password. A similar point can be made about $6$
/SHA-512 passwords.
This is very annoying.
Parenthetical note: As it happens, it is possible to convert the $1$
/SMD5 ‘hashes’ between crypt(3) format and OpenLDAP:
$1$salt$hash
corresponds to
'{SMD5}' + hash + salt
where +
is simple string concatenation. But we probably shouldn't be using
MD5 hashes any more, so this doesn't much help.
Note also that you can port your crypt(3) hashes to OpenLDAP unchanged, using
{CRYPT}$1$salt$hash
if your libc is compatible (which it typically will be, if OpenLDAP is running on Linux), but OpenLDAP won't generate new passwords this way, so this is a one-way street.
Best Answer
If you're using password authentication, then SSH sends the password over the network. The connection is encrypted, so eavesdroppers can't see the password. The connection is authenticated, provided that you don't click blindly through a “The authenticity of … can't be established” message, so your password won't be sent to anyone except the legitimate server.
The boring answer to “why” is that this is what the SSH protocol requires.
The less boring answer is that password authentication has to work that way. There are ways to perform authentication that don't work this way but this is no longer simple password authentication.
Most authentication protocols that are more advanced than simple password authentication have the nice property that the client doesn't send any secret data to the server that a malicious server could use to impersonate the user on some third server. With SSH public key authentication, the most common SSH authentication method other than passwords, this works because the client sends a signature (requiring the private key) of data that includes the session identifier; if the malicious server tried to authenticate to a third server, it would have to generate a signature of data including a different session identifier, which it would be unable to do without the private key that stays on the client.
Note that if you use public key authentication, and you have to type a password to use the key, this is not password-based authentication. The password to use the key is used exclusively on the client side, to read the key from the key file. When using public key authentication, the server does not know or care whether the key was stored in an encrypted file.
Password authentication requires sending the password to the server. Sending a hash of the password instead of the password itself does not help, because then the password becomes the hash: an attacker wouldn't need to find the actual password, only the hash. An attacker could attack by finding the actual password, so there is no improvement. But if the attacker found the hash but not the password, in your proposed scheme, that would be enough. In contrast, with normal password-based authentication, the attacker must know the password, knowing the hash is not good enough, and if the password is strong enough then the attacker won't be able to find the password from the hash. In practice, the reason the attacker might know the hash but not the password is that the attacker managed to extract the database of password hashes from the server, possibly from an unprotected backup or via a vulnerability on the server. Such vulnerabilities on websites make the news pretty often.
Your proposed protocol is less good than the standard one. Don't roll your own crypto!