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

bind: Use ssh_key_import_private().

This commit is contained in:
Andreas Schneider
2011-08-07 17:49:18 +02:00
parent 9bed334da3
commit 38d00c0e7a

View File

@@ -34,7 +34,7 @@
#include "libssh/bind.h" #include "libssh/bind.h"
#include "libssh/libssh.h" #include "libssh/libssh.h"
#include "libssh/server.h" #include "libssh/server.h"
#include "libssh/keyfiles.h" #include "libssh/pki.h"
#include "libssh/buffer.h" #include "libssh/buffer.h"
#include "libssh/socket.h" #include "libssh/socket.h"
#include "libssh/session.h" #include "libssh/session.h"
@@ -287,10 +287,11 @@ void ssh_bind_free(ssh_bind sshbind){
int ssh_bind_accept(ssh_bind sshbind, ssh_session session) { int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
ssh_private_key dsa = NULL; ssh_key dsa = NULL;
ssh_private_key rsa = NULL; ssh_key rsa = NULL;
socket_t fd = SSH_INVALID_SOCKET; socket_t fd = SSH_INVALID_SOCKET;
int i; int i;
int rc;
if (sshbind->bindfd == SSH_INVALID_SOCKET) { if (sshbind->bindfd == SSH_INVALID_SOCKET) {
ssh_set_error(sshbind, SSH_FATAL, ssh_set_error(sshbind, SSH_FATAL,
@@ -308,18 +309,28 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
} }
if (sshbind->dsakey) { if (sshbind->dsakey) {
dsa = _privatekey_from_file(sshbind, sshbind->dsakey, SSH_KEYTYPE_DSS); rc = ssh_key_import_private(session, sshbind->dsakey, NULL, &dsa);
if (dsa == NULL) { if (rc == SSH_ERROR) {
return SSH_ERROR; return SSH_ERROR;
} }
if (ssh_key_type(dsa) != SSH_KEYTYPE_DSS) {
ssh_key_free(dsa);
return SSH_ERROR;
}
} }
if (sshbind->rsakey) { if (sshbind->rsakey) {
rsa = _privatekey_from_file(sshbind, sshbind->rsakey, SSH_KEYTYPE_RSA); rc = ssh_key_import_private(session, sshbind->rsakey, NULL, &rsa);
if (rsa == NULL) { if (rc == SSH_ERROR) {
privatekey_free(dsa); return SSH_ERROR;
return SSH_ERROR; }
}
if (ssh_key_type(rsa) != SSH_KEYTYPE_RSA &&
ssh_key_type(rsa) != SSH_KEYTYPE_RSA1) {
ssh_key_free(dsa);
return SSH_ERROR;
}
} }
fd = accept(sshbind->bindfd, NULL, NULL); fd = accept(sshbind->bindfd, NULL, NULL);
@@ -327,8 +338,9 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
ssh_set_error(sshbind, SSH_FATAL, ssh_set_error(sshbind, SSH_FATAL,
"Accepting a new connection: %s", "Accepting a new connection: %s",
strerror(errno)); strerror(errno));
privatekey_free(dsa); ssh_key_free(dsa);
privatekey_free(rsa); ssh_key_free(rsa);
return SSH_ERROR; return SSH_ERROR;
} }
@@ -340,8 +352,9 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
if (sshbind->wanted_methods[i]) { if (sshbind->wanted_methods[i]) {
session->wanted_methods[i] = strdup(sshbind->wanted_methods[i]); session->wanted_methods[i] = strdup(sshbind->wanted_methods[i]);
if (session->wanted_methods[i] == NULL) { if (session->wanted_methods[i] == NULL) {
privatekey_free(dsa); ssh_key_free(dsa);
privatekey_free(rsa); ssh_key_free(rsa);
return SSH_ERROR; return SSH_ERROR;
} }
} }
@@ -353,8 +366,9 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
SAFE_FREE(session->bindaddr); SAFE_FREE(session->bindaddr);
session->bindaddr = strdup(sshbind->bindaddr); session->bindaddr = strdup(sshbind->bindaddr);
if (session->bindaddr == NULL) { if (session->bindaddr == NULL) {
privatekey_free(dsa); ssh_key_free(dsa);
privatekey_free(rsa); ssh_key_free(rsa);
return SSH_ERROR; return SSH_ERROR;
} }
} }
@@ -366,14 +380,15 @@ int ssh_bind_accept(ssh_bind sshbind, ssh_session session) {
if (session->socket == NULL) { if (session->socket == NULL) {
/* perhaps it may be better to copy the error from session to sshbind */ /* perhaps it may be better to copy the error from session to sshbind */
ssh_set_error_oom(sshbind); ssh_set_error_oom(sshbind);
privatekey_free(dsa); ssh_key_free(dsa);
privatekey_free(rsa); ssh_key_free(rsa);
return SSH_ERROR; return SSH_ERROR;
} }
ssh_socket_set_fd(session->socket, fd); ssh_socket_set_fd(session->socket, fd);
ssh_socket_get_poll_handle_out(session->socket); ssh_socket_get_poll_handle_out(session->socket);
session->dsa_key = dsa;
session->rsa_key = rsa; session->dsa_key = ssh_pki_convert_key_to_privatekey(dsa);
session->rsa_key = ssh_pki_convert_key_to_privatekey(rsa);
return SSH_OK; return SSH_OK;
} }