1
1

userauth: Provide more informations if ssh pub key extraction fails

If the function that extracts/computes the public key from a private key
fails the errors it reports were masked by the function calling it. This
patch modifies the key extraction function to return errors using
_libssh_error() function.  The error messages are tweaked to contain
reference to the failed operaton in addition to the reason.

 * AUTHORS: - add my name
 * libgcrypt.c: _libssh2_pub_priv_keyfile(): - return a more verbose
                                               error using
                                               _libssh2_error() func.
 * openssl.c: - modify call graph of _libssh2_pub_priv_keyfile() to use
                _libssh2_error for error reporting();
 * userauth.c: - tweak functions calling _libssh2_pub_priv_keyfile() not
                 to shadow error messages
Этот коммит содержится в:
Peter Krempa 2011-12-19 15:02:15 +01:00 коммит произвёл Daniel Stenberg
родитель b8dd697796
Коммит 209de22299
4 изменённых файлов: 43 добавлений и 40 удалений

Просмотреть файл

@ -29,6 +29,7 @@ Mikhail Gusarov
Neil Gierman Neil Gierman
Olivier Hervieu Olivier Hervieu
Paul Veldkamp Paul Veldkamp
Peter Krempa
Peter O'Gorman Peter O'Gorman
Peter Stuge Peter Stuge
Romain Bondue Romain Bondue

Просмотреть файл

@ -581,8 +581,9 @@ _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
const char *privatekey, const char *privatekey,
const char *passphrase) const char *passphrase)
{ {
return -1; /* not yet supported; interpreted by userauth.c to call return _libssh_error(session, LIBSSH2_ERROR_FILE,
libssh2_error */ "Unable to extract public key from private key file: "
"Method unimplemented in libgcrypt backend");
} }
void _libssh2_init_aes_ctr(void) void _libssh2_init_aes_ctr(void)

Просмотреть файл

@ -666,10 +666,9 @@ gen_publickey_from_rsa_evp(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, method_buf); LIBSSH2_FREE(session, method_buf);
} }
_libssh2_error(session, return _libssh2_error(session,
LIBSSH2_ERROR_ALLOC, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for private key data"); "Unable to allocate memory for private key data");
return -1;
} }
static int static int
@ -721,10 +720,9 @@ gen_publickey_from_dsa_evp(LIBSSH2_SESSION *session,
LIBSSH2_FREE(session, method_buf); LIBSSH2_FREE(session, method_buf);
} }
_libssh2_error(session, return _libssh2_error(session,
LIBSSH2_ERROR_ALLOC, LIBSSH2_ERROR_ALLOC,
"Unable to allocate memory for private key data"); "Unable to allocate memory for private key data");
return -1;
} }
int int
@ -747,10 +745,10 @@ _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
bp = BIO_new_file(privatekey, "r"); bp = BIO_new_file(privatekey, "r");
if (bp == NULL) { if (bp == NULL) {
_libssh2_error(session, return _libssh2_error(session,
LIBSSH2_ERROR_FILE, LIBSSH2_ERROR_FILE,
"Unable to open private key file"); "Unable to extract public key from private key "
return -1; "file: Unable to open private key file");
} }
if (!EVP_get_cipherbyname("des")) { if (!EVP_get_cipherbyname("des")) {
/* If this cipher isn't loaded it's a pretty good indication that none /* If this cipher isn't loaded it's a pretty good indication that none
@ -765,11 +763,12 @@ _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
BIO_free(bp); BIO_free(bp);
if (pk == NULL) { if (pk == NULL) {
_libssh2_error(session, return _libssh2_error(session,
LIBSSH2_ERROR_FILE, LIBSSH2_ERROR_FILE,
"Unable to extract public key "
"from private key file: "
"Wrong passphrase or invalid/unrecognized " "Wrong passphrase or invalid/unrecognized "
"private key file format"); "private key file format");
return -1;
} }
switch (pk->type) { switch (pk->type) {
@ -784,9 +783,10 @@ _libssh2_pub_priv_keyfile(LIBSSH2_SESSION *session,
break; break;
default : default :
st = -1; st = _libssh2_error(session,
_libssh2_error(session,
LIBSSH2_ERROR_FILE, LIBSSH2_ERROR_FILE,
"Unable to extract public key "
"from private key file: "
"Unsupported private key file format"); "Unsupported private key file format");
break; break;
} }

Просмотреть файл

@ -665,14 +665,14 @@ userauth_hostbased_fromfile(LIBSSH2_SESSION *session,
} }
else { else {
/* Compute public key from private key. */ /* Compute public key from private key. */
if (_libssh2_pub_priv_keyfile(session, rc = _libssh2_pub_priv_keyfile(session,
&session->userauth_host_method, &session->userauth_host_method,
&session->userauth_host_method_len, &session->userauth_host_method_len,
&pubkeydata, &pubkeydata_len, &pubkeydata, &pubkeydata_len,
privatekey, passphrase)) privatekey, passphrase);
return _libssh2_error(session, LIBSSH2_ERROR_FILE, if (rc)
"Unable to extract public key " /* libssh2_pub_priv_keyfile calls _libssh2_error() */
"from private key file"); return rc;
} }
/* /*
@ -1237,19 +1237,20 @@ userauth_publickey_fromfile(LIBSSH2_SESSION *session,
rc = file_read_publickey(session, &session->userauth_pblc_method, rc = file_read_publickey(session, &session->userauth_pblc_method,
&session->userauth_pblc_method_len, &session->userauth_pblc_method_len,
&pubkeydata, &pubkeydata_len,publickey); &pubkeydata, &pubkeydata_len,publickey);
if(rc) if (rc)
return rc; return rc;
} }
else { else {
/* Compute public key from private key. */ /* Compute public key from private key. */
if (_libssh2_pub_priv_keyfile(session, rc = _libssh2_pub_priv_keyfile(session,
&session->userauth_pblc_method, &session->userauth_pblc_method,
&session->userauth_pblc_method_len, &session->userauth_pblc_method_len,
&pubkeydata, &pubkeydata_len, &pubkeydata, &pubkeydata_len,
privatekey, passphrase)) privatekey, passphrase);
return _libssh2_error(session, LIBSSH2_ERROR_FILE,
"Unable to extract public key " /* _libssh2_pub_priv_keyfile calls _libssh2_error() */
"from private key file"); if (rc)
return rc;
} }
} }