mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-11 10:40:27 +09:00
Cleanup and add more error checks to _privatekey_from_file().
git-svn-id: svn+ssh://svn.berlios.de/svnroot/repos/libssh/trunk@566 7dcaeef0-15fb-0310-b436-a5af3365683c
This commit is contained in:
@@ -738,8 +738,8 @@ PRIVATE_KEY *privatekey_from_file(SSH_SESSION *session, const char *filename,
|
|||||||
/* same that privatekey_from_file() but without any passphrase things. */
|
/* same that privatekey_from_file() but without any passphrase things. */
|
||||||
PRIVATE_KEY *_privatekey_from_file(void *session, const char *filename,
|
PRIVATE_KEY *_privatekey_from_file(void *session, const char *filename,
|
||||||
int type) {
|
int type) {
|
||||||
FILE *file=fopen(filename,"r");
|
PRIVATE_KEY *privkey = NULL;
|
||||||
PRIVATE_KEY *privkey;
|
FILE *file = NULL;
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
gcry_sexp_t dsa = NULL;
|
gcry_sexp_t dsa = NULL;
|
||||||
gcry_sexp_t rsa = NULL;
|
gcry_sexp_t rsa = NULL;
|
||||||
@@ -748,54 +748,78 @@ PRIVATE_KEY *_privatekey_from_file(void *session, const char *filename,
|
|||||||
DSA *dsa = NULL;
|
DSA *dsa = NULL;
|
||||||
RSA *rsa = NULL;
|
RSA *rsa = NULL;
|
||||||
#endif
|
#endif
|
||||||
if(!file){
|
|
||||||
ssh_set_error(session,SSH_REQUEST_DENIED,"Error opening %s : %s",filename,strerror(errno));
|
file = fopen(filename,"r");
|
||||||
|
if (file == NULL) {
|
||||||
|
ssh_set_error(session, SSH_REQUEST_DENIED,
|
||||||
|
"Error opening %s: %s", filename, strerror(errno));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if(type==TYPE_DSS){
|
|
||||||
|
switch (type) {
|
||||||
|
case TYPE_DSS:
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
valid = read_dsa_privatekey(file, &dsa, NULL, NULL, NULL);
|
valid = read_dsa_privatekey(file, &dsa, NULL, NULL, NULL);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
|
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename);
|
||||||
,filename);
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
#elif defined HAVE_LIBCRYPTO
|
||||||
dsa = PEM_read_DSAPrivateKey(file, NULL, NULL, NULL);
|
dsa = PEM_read_DSAPrivateKey(file, NULL, NULL, NULL);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if(!dsa){
|
|
||||||
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
|
if (dsa == NULL) {
|
||||||
": %s",filename,ERR_error_string(ERR_get_error(),NULL));
|
ssh_set_error(session, SSH_FATAL,
|
||||||
|
"Parsing private key %s: %s",
|
||||||
|
filename, ERR_error_string(ERR_get_error(), NULL));
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
}
|
break;
|
||||||
else if (type==TYPE_RSA){
|
case TYPE_RSA:
|
||||||
#ifdef HAVE_LIBGCRYPT
|
#ifdef HAVE_LIBGCRYPT
|
||||||
valid = read_rsa_privatekey(file, &rsa, NULL, NULL, NULL);
|
valid = read_rsa_privatekey(file, &rsa, NULL, NULL, NULL);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
|
|
||||||
if (!valid) {
|
if (!valid) {
|
||||||
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
|
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename);
|
||||||
,filename);
|
|
||||||
#elif defined HAVE_LIBCRYPTO
|
#elif defined HAVE_LIBCRYPTO
|
||||||
rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
|
rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
|
||||||
|
|
||||||
fclose(file);
|
fclose(file);
|
||||||
if(!rsa){
|
|
||||||
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
|
if (rsa == NULL) {
|
||||||
": %s",filename,ERR_error_string(ERR_get_error(),NULL));
|
ssh_set_error(session, SSH_FATAL,
|
||||||
|
"Parsing private key %s: %s",
|
||||||
|
filename, ERR_error_string(ERR_get_error(), NULL));
|
||||||
#endif
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
} else {
|
break;
|
||||||
|
default:
|
||||||
ssh_set_error(session, SSH_FATAL, "Invalid private key type %d", type);
|
ssh_set_error(session, SSH_FATAL, "Invalid private key type %d", type);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
privkey = malloc(sizeof(PRIVATE_KEY));
|
privkey = malloc(sizeof(PRIVATE_KEY));
|
||||||
if (privkey == NULL) {
|
if (privkey == NULL) {
|
||||||
|
#ifdef HAVE_LIBGCRYPT
|
||||||
|
gcry_sexp_release(dsa);
|
||||||
|
gcry_sexp_release(rsa);
|
||||||
|
#elif defined HAVE_LIBCRYPTO
|
||||||
|
DSA_free(dsa);
|
||||||
|
RSA_free(rsa);
|
||||||
|
#endif
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
privkey->type = type;
|
privkey->type = type;
|
||||||
privkey->dsa_priv = dsa;
|
privkey->dsa_priv = dsa;
|
||||||
privkey->rsa_priv = rsa;
|
privkey->rsa_priv = rsa;
|
||||||
|
|
||||||
return privkey;
|
return privkey;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user