summaryrefslogtreecommitdiffstats
path: root/src/known_hosts.c
diff options
context:
space:
mode:
authorAris Adamantiadis <aris@0xbadc0de.be>2014-02-04 22:28:30 +0100
committerAris Adamantiadis <aris@0xbadc0de.be>2014-02-05 08:08:31 +0100
commit56f86cd4a1d774353b2095aebfdbdd73fa4276ba (patch)
tree1eed975397ea46bc0b0c8e72dccbc07551eaa98f /src/known_hosts.c
parentf265afacfbe6eab9db6a8aab39d2e0fbd2d1a1d7 (diff)
knownhosts: detect variations of ecdsa
Diffstat (limited to 'src/known_hosts.c')
-rw-r--r--src/known_hosts.c40
1 files changed, 31 insertions, 9 deletions
diff --git a/src/known_hosts.c b/src/known_hosts.c
index f2b2fde8..21f6cf29 100644
--- a/src/known_hosts.c
+++ b/src/known_hosts.c
@@ -34,7 +34,7 @@
#include "libssh/misc.h"
#include "libssh/pki.h"
#include "libssh/options.h"
-
+#include "libssh/knownhosts.h"
/*todo: remove this include */
#include "libssh/string.h"
@@ -647,29 +647,33 @@ int ssh_write_knownhost(ssh_session session) {
return 0;
}
+#define KNOWNHOSTS_MAXTYPES 10
+
/**
+ * @internal
* @brief Check which kind of host keys should be preferred for connection
* by reading the known_hosts file.
*
* @param[in] session The SSH session to use.
*
- * @returns Bitfield of supported SSH hostkey algorithms
- * SSH_ERROR on error
+ * @returns array of supported key types
+ * NULL on error
*/
-int ssh_knownhosts_algorithms(ssh_session session) {
+char **ssh_knownhosts_algorithms(ssh_session session) {
FILE *file = NULL;
char **tokens;
char *host;
char *hostport;
const char *type;
int match;
- int ret = 0;
+ char **array;
+ int i=0, j;
if (session->opts.knownhosts == NULL) {
if (ssh_options_apply(session) < 0) {
ssh_set_error(session, SSH_REQUEST_DENIED,
"Can't find a known_hosts file");
- return SSH_ERROR;
+ return NULL;
}
}
@@ -683,8 +687,13 @@ int ssh_knownhosts_algorithms(ssh_session session) {
ssh_set_error_oom(session);
SAFE_FREE(host);
SAFE_FREE(hostport);
+ return NULL;
+ }
- return SSH_ERROR;
+ array = malloc(sizeof(char *) * KNOWNHOSTS_MAXTYPES);
+ if (array==NULL){
+ ssh_set_error_oom(session);
+ return NULL;
}
do {
@@ -709,11 +718,24 @@ int ssh_knownhosts_algorithms(ssh_session session) {
/* We got a match. Now check the key type */
SSH_LOG(SSH_LOG_DEBUG, "server %s:%d has %s in known_hosts",
host, session->opts.port, type);
- ret |= 1 << ssh_key_type_from_name(type);
+ /* don't copy more than once */
+ for(j=0;j<i && match;++j){
+ if(strcmp(array[j], type)==0)
+ match=0;
+ }
+ if (match){
+ array[i] = strdup(type);
+ i++;
+ if(i>= KNOWNHOSTS_MAXTYPES-1){
+ tokens_free(tokens);
+ break;
+ }
+ }
}
tokens_free(tokens);
} while (1);
+ array[i]=NULL;
SAFE_FREE(host);
SAFE_FREE(hostport);
if (file != NULL) {
@@ -721,7 +743,7 @@ int ssh_knownhosts_algorithms(ssh_session session) {
}
/* Return the current state at end of file */
- return ret;
+ return array;
}
/** @} */