Use BIO* in _privatekey_from_file [Oliver Stöneberg]

_privatekey_from_file: moved FILE* into HAVE_LIBGCRYPT code / added missing #ifdef's to default case of switch [Oliver Stöneberg]
This commit is contained in:
Oliver Stöneberg
2011-05-02 09:25:12 -07:00
committed by milo
parent f503c4a3e1
commit dcb50cc0c8

View File

@@ -828,41 +828,51 @@ enum ssh_keytypes_e ssh_privatekey_type(ssh_private_key privatekey){
ssh_private_key _privatekey_from_file(void *session, const char *filename, ssh_private_key _privatekey_from_file(void *session, const char *filename,
int type) { int type) {
ssh_private_key privkey = NULL; ssh_private_key privkey = NULL;
FILE *file = NULL;
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT
FILE *file = NULL;
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;
BIO *bio = NULL;
#endif #endif
#ifdef HAVE_LIBGCRYPT
file = fopen(filename,"r"); file = fopen(filename,"r");
if (file == NULL) { if (file == NULL) {
ssh_set_error(session, SSH_REQUEST_DENIED, ssh_set_error(session, SSH_REQUEST_DENIED,
"Error opening %s: %s", filename, strerror(errno)); "Error opening %s: %s", filename, strerror(errno));
return NULL; return NULL;
} }
#elif defined HAVE_LIBCRYPTO
bio = BIO_new_file(filename,"r");
if (bio == NULL) {
ssh_set_error(session, SSH_FATAL, "Could not create BIO.");
return NULL;
}
#endif
switch (type) { switch (type) {
case SSH_KEYTYPE_DSS: case SSH_KEYTYPE_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);
if (!valid) { if (!valid) {
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename); 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); dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL);
BIO_free(bio);
if (dsa == NULL) { if (dsa == NULL) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session, SSH_FATAL,
"Parsing private key %s: %s", "Parsing private key %s: %s",
filename, ERR_error_string(ERR_get_error(), NULL)); filename, ERR_error_string(ERR_get_error(), NULL));
#else
{
#endif #endif
fclose(file);
return NULL; return NULL;
} }
break; break;
@@ -870,30 +880,33 @@ ssh_private_key _privatekey_from_file(void *session, const char *filename,
#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);
if (!valid) { if (!valid) {
ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename); ssh_set_error(session, SSH_FATAL, "Parsing private key %s", filename);
#elif defined HAVE_LIBCRYPTO #elif defined HAVE_LIBCRYPTO
rsa = PEM_read_RSAPrivateKey(file, NULL, NULL, NULL); rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL);
BIO_free(bio);
if (rsa == NULL) { if (rsa == NULL) {
ssh_set_error(session, SSH_FATAL, ssh_set_error(session, SSH_FATAL,
"Parsing private key %s: %s", "Parsing private key %s: %s",
filename, ERR_error_string(ERR_get_error(), NULL)); filename, ERR_error_string(ERR_get_error(), NULL));
#else
{
#endif #endif
fclose(file);
return NULL; return NULL;
} }
break; break;
default: default:
#ifdef HAVE_LIBGCRYPT
fclose(file); fclose(file);
#elif defined HAVE_LIBCRYPTO
BIO_free(bio);
#endif
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;
} }
fclose(file);
privkey = malloc(sizeof(struct ssh_private_key_struct)); privkey = malloc(sizeof(struct ssh_private_key_struct));
if (privkey == NULL) { if (privkey == NULL) {
#ifdef HAVE_LIBGCRYPT #ifdef HAVE_LIBGCRYPT