1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-11-30 13:01:23 +03:00

Improve check_public_key().

git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@632 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
Andreas Schneider
2009-04-27 17:59:48 +00:00
parent a19aebba18
commit b39fcd6470

View File

@@ -1104,33 +1104,52 @@ static char **ssh_get_knownhost_line(SSH_SESSION *session, FILE **file,
return NULL;
}
/** \brief Check the public key in the known host line matches the
/**
* \brief Check the public key in the known host line matches the
* public key of the currently connected server.
* \param tokens list of tokens in the known_hosts line.
* \return 1 if the key matches
* \return 0 if the key doesn't match
* \return -1 on error
*/
static int check_public_key(SSH_SESSION *session, char **tokens){
char *pubkey_64;
static int check_public_key(SSH_SESSION *session, char **tokens) {
STRING *pubkey = session->current_crypto->server_pubkey;
BUFFER *pubkey_buffer;
STRING *pubkey=session->current_crypto->server_pubkey;
char *pubkey_64;
/* ok we found some public key in known hosts file. now un-base64it */
if (alldigits(tokens[1])) {
/* openssh rsa1 format */
bignum tmpbn;
int i;
unsigned int len;
STRING *tmpstring;
unsigned int len;
int i;
pubkey_buffer = buffer_new();
if (pubkey_buffer == NULL) {
return -1;
}
tmpstring = string_from_char("ssh-rsa1");
buffer_add_ssh_string(pubkey_buffer, tmpstring);
if (tmpstring == NULL) {
buffer_free(pubkey_buffer);
return -1;
}
if (buffer_add_ssh_string(pubkey_buffer, tmpstring) < 0) {
buffer_free(pubkey_buffer);
string_free(tmpstring);
return -1;
}
string_free(tmpstring);
for (i = 2; i < 4; i++) { /* e, then n */
tmpbn = NULL;
bignum_dec2bn(tokens[i], &tmpbn);
if (tmpbn == NULL) {
buffer_free(pubkey_buffer);
return -1;
}
/* for some reason, make_bignum_string does not work
because of the padding which it does --kv */
/* tmpstring = make_bignum_string(tmpbn); */
@@ -1138,8 +1157,9 @@ static int check_public_key(SSH_SESSION *session, char **tokens){
len = bignum_num_bytes(tmpbn);
tmpstring = malloc(4 + len);
if (tmpstring == NULL) {
buffer_free(pubkey_buffer);
bignum_free(tmpbn);
return -1;
ssh_set_error(session, SSH_FATAL, "No space left");
}
tmpstring->size = htonl(len);
#ifdef HAVE_LIBGCRYPT
@@ -1148,28 +1168,38 @@ static int check_public_key(SSH_SESSION *session, char **tokens){
bignum_bn2bin(tmpbn, tmpstring->string);
#endif
bignum_free(tmpbn);
buffer_add_ssh_string(pubkey_buffer, tmpstring);
free(tmpstring);
if (buffer_add_ssh_string(pubkey_buffer, tmpstring) < 0) {
buffer_free(pubkey_buffer);
string_free(tmpstring);
bignum_free(tmpbn);
return -1;
}
string_free(tmpstring);
}
} else {
/* ssh-dss or ssh-rsa */
pubkey_64=tokens[2];
pubkey_buffer=base64_to_bin(pubkey_64);
pubkey_64 = tokens[2];
pubkey_buffer = base64_to_bin(pubkey_64);
}
if(!pubkey_buffer){
ssh_set_error(session,SSH_FATAL,"verifying that server is a known host : base 64 error");
if (pubkey_buffer == NULL) {
ssh_set_error(session, SSH_FATAL,
"Verifying that server is a known host: base64 error");
return -1;
}
if(buffer_get_len(pubkey_buffer)!=string_len(pubkey)){
if (buffer_get_len(pubkey_buffer) != string_len(pubkey)) {
buffer_free(pubkey_buffer);
return 0;
}
/* now test that they are identical */
if(memcmp(buffer_get(pubkey_buffer),pubkey->string,buffer_get_len(pubkey_buffer))!=0){
if (memcmp(buffer_get(pubkey_buffer), pubkey->string,
buffer_get_len(pubkey_buffer)) != 0) {
buffer_free(pubkey_buffer);
return 0;
}
buffer_free(pubkey_buffer);
return 1;
}