From 5b599fbf40cd56482786be56e70a7a2a53f79929 Mon Sep 17 00:00:00 2001 From: Daniel Stenberg Date: Tue, 7 Jul 2009 00:25:17 +0200 Subject: [PATCH] Ben Kibbey added a type parameter to the libssh2_session_hostkey() function, which hasn't yet been in a public release so changing the API is fine! --- docs/libssh2_session_hostkey.3 | 7 ++++++- example/simple/ssh2_exec.c | 3 ++- include/libssh2.h | 7 ++++++- src/hostkey.c | 26 +++++++++++++++++++++++++- 4 files changed, 39 insertions(+), 4 deletions(-) diff --git a/docs/libssh2_session_hostkey.3 b/docs/libssh2_session_hostkey.3 index d3f8b93..c6bd00e 100644 --- a/docs/libssh2_session_hostkey.3 +++ b/docs/libssh2_session_hostkey.3 @@ -7,10 +7,15 @@ libssh2_session_hostkey - get the remote key #include const char *libssh2_session_hostkey(LIBSSH2_SESSION *session, - size_t *len); + size_t *len, int *type); .SH DESCRIPTION Returns a pointer to the current host key, the value \fIlen\fP points to will get the length of the key. + +The value \fItype\fP points to the type of hostkey which is one of: +LIBSSH2_HOSTKEY_TYPE_RSA, LIBSSH2_HOSTKEY_TYPE_DSS, or +LIBSSH2_HOSTKEY_TYPE_UNKNOWN. + .SH RETURN VALUE A pointer, or NULL if something went wrong. .SH SEE ALSO diff --git a/example/simple/ssh2_exec.c b/example/simple/ssh2_exec.c index 1ad0597..e55a6d8 100644 --- a/example/simple/ssh2_exec.c +++ b/example/simple/ssh2_exec.c @@ -86,6 +86,7 @@ int main(int argc, char *argv[]) int bytecount = 0; size_t len; LIBSSH2_KNOWNHOSTS *nh; + int type; #ifdef WIN32 WSADATA wsadata; @@ -154,7 +155,7 @@ int main(int argc, char *argv[]) libssh2_knownhost_writefile(nh, "dumpfile", LIBSSH2_KNOWNHOST_FILE_OPENSSH); - fingerprint = libssh2_session_hostkey(session, &len); + fingerprint = libssh2_session_hostkey(session, &len, &type); if(fingerprint) { struct libssh2_knownhost *host; int check = libssh2_knownhost_check(nh, (char *)hostname, diff --git a/include/libssh2.h b/include/libssh2.h index 169f704..2bc2435 100644 --- a/include/libssh2.h +++ b/include/libssh2.h @@ -294,6 +294,11 @@ typedef struct _LIBSSH2_POLLFD { #define LIBSSH2_HOSTKEY_HASH_MD5 1 #define LIBSSH2_HOSTKEY_HASH_SHA1 2 +/* Hostkey Types */ +#define LIBSSH2_HOSTKEY_TYPE_UNKNOWN 0 +#define LIBSSH2_HOSTKEY_TYPE_RSA 1 +#define LIBSSH2_HOSTKEY_TYPE_DSS 2 + /* Disconnect Codes (defined by SSH protocol) */ #define SSH_DISCONNECT_HOST_NOT_ALLOWED_TO_CONNECT 1 #define SSH_DISCONNECT_PROTOCOL_ERROR 2 @@ -381,7 +386,7 @@ LIBSSH2_API const char *libssh2_hostkey_hash(LIBSSH2_SESSION *session, int hash_type); LIBSSH2_API const char *libssh2_session_hostkey(LIBSSH2_SESSION *session, - size_t *len); + size_t *len, int *type); LIBSSH2_API int libssh2_session_method_pref(LIBSSH2_SESSION *session, int method_type, diff --git a/src/hostkey.c b/src/hostkey.c index a336080..98a88d5 100644 --- a/src/hostkey.c +++ b/src/hostkey.c @@ -456,6 +456,27 @@ libssh2_hostkey_hash(LIBSSH2_SESSION * session, int hash_type) } } +static int hostkey_type(const unsigned char *hostkey, size_t len) +{ + const unsigned char rsa[] = { + 0, 0, 0, 0x07, 's', 's', 'h', '-', 'r', 's', 'a' + }; + const unsigned char dss[] = { + 0, 0, 0, 0x07, 's', 's', 'h', '-', 'd', 's', 's' + }; + + if (len < 11) + return LIBSSH2_HOSTKEY_TYPE_UNKNOWN; + + if (!memcmp(rsa, hostkey, 11)) + return LIBSSH2_HOSTKEY_TYPE_RSA; + + if (!memcmp(dss, hostkey, 11)) + return LIBSSH2_HOSTKEY_TYPE_DSS; + + return LIBSSH2_HOSTKEY_TYPE_UNKNOWN; +} + /* * libssh2_session_hostkey() * @@ -463,11 +484,14 @@ libssh2_hostkey_hash(LIBSSH2_SESSION * session, int hash_type) * */ LIBSSH2_API const char * -libssh2_session_hostkey(LIBSSH2_SESSION *session, size_t *len) +libssh2_session_hostkey(LIBSSH2_SESSION *session, size_t *len, int *type) { if(session->server_hostkey_len) { if(len) *len = session->server_hostkey_len; + if (type) + *type = hostkey_type(session->server_hostkey, + session->server_hostkey_len); return (char *) session->server_hostkey; } if(len)