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:
Andreas Schneider
2009-04-20 08:58:08 +00:00
parent 2f51befc0f
commit 9001a34cd3

View File

@@ -738,65 +738,89 @@ 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;
int valid; int valid;
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
DSA *dsa=NULL; DSA *dsa = NULL;
RSA *rsa=NULL; RSA *rsa = NULL;
#endif
file = fopen(filename,"r");
if (file == NULL) {
ssh_set_error(session, SSH_REQUEST_DENIED,
"Error opening %s: %s", filename, strerror(errno));
return NULL;
}
switch (type) {
case TYPE_DSS:
#ifdef HAVE_LIBGCRYPT
valid = read_dsa_privatekey(file, &dsa, NULL, NULL, NULL);
fclose(file);
if (!valid) {
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename);
#elif defined HAVE_LIBCRYPTO
dsa = PEM_read_DSAPrivateKey(file, NULL, NULL, NULL);
fclose(file);
if (dsa == NULL) {
ssh_set_error(session, SSH_FATAL,
"Parsing private key %s: %s",
filename, ERR_error_string(ERR_get_error(), NULL));
#endif #endif
if(!file){
ssh_set_error(session,SSH_REQUEST_DENIED,"Error opening %s : %s",filename,strerror(errno));
return NULL; return NULL;
} }
if(type==TYPE_DSS){ break;
case TYPE_RSA:
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
valid=read_dsa_privatekey(file,&dsa,NULL,NULL,NULL); valid = read_rsa_privatekey(file, &rsa, NULL, NULL, NULL);
fclose(file);
if(!valid){ fclose(file);
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
,filename); if (!valid) {
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename);
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
dsa=PEM_read_DSAPrivateKey(file,NULL,NULL,NULL); rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
fclose(file);
if(!dsa){ fclose(file);
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
": %s",filename,ERR_error_string(ERR_get_error(),NULL)); if (rsa == NULL) {
ssh_set_error(session, SSH_FATAL,
"Parsing private key %s: %s",
filename, ERR_error_string(ERR_get_error(), NULL));
#endif #endif
return NULL;
}
}
else if (type==TYPE_RSA){
#ifdef HAVE_LIBGCRYPT
valid=read_rsa_privatekey(file,&rsa,NULL,NULL,NULL);
fclose(file);
if(!valid){
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
,filename);
#elif defined HAVE_LIBCRYPTO
rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL);
fclose(file);
if(!rsa){
ssh_set_error(session,SSH_FATAL,"parsing private key %s"
": %s",filename,ERR_error_string(ERR_get_error(),NULL));
#endif
return NULL;
}
} else {
ssh_set_error(session,SSH_FATAL,"Invalid private key type %d",type);
return NULL; return NULL;
} }
privkey = malloc(sizeof(PRIVATE_KEY)); break;
if (privkey == NULL) { default:
return NULL; ssh_set_error(session, SSH_FATAL, "Invalid private key type %d", type);
} return NULL;
privkey->type=type; }
privkey->dsa_priv=dsa;
privkey->rsa_priv=rsa; privkey = malloc(sizeof(PRIVATE_KEY));
return privkey; 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;
}
privkey->type = type;
privkey->dsa_priv = dsa;
privkey->rsa_priv = rsa;
return privkey;
} }
/** \brief deallocate a private key /** \brief deallocate a private key