Subject: Re: known hosts API, updated

Re: known hosts API, updated

From: Ben Kibbey <bjk_at_luxsci.net>
Date: Sat, 4 Jul 2009 20:01:32 -0400

On Sat, Jul 04, 2009 at 10:35:52PM +0200, Daniel Stenberg wrote:
> On Sat, 4 Jul 2009, Ben Kibbey wrote:
>
>> I'm not sure if you wanted to change the API or not but the following
>> adds another argument to libssh2_session_hostkey() to set the key type:
>> 1=rsa, 2=dss and 0=unknown:
>
> Thanks!
>
> I'm perfectly fine with changing the API since it hasn't ever been in a
> release yet. But I would like to get public defines for the key types
> (instead of referring to 0, 1 and 2) and perhaps even an updated man page
> for the function!

OK. Here's the updated version:

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 <libssh2.h>
 
 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..33b9af1 100644
--- a/src/hostkey.c
+++ b/src/hostkey.c
@@ -456,6 +456,23 @@ 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 +480,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)

-- 
Ben Kibbey (bjk) @ FreeNode/OFTC/Jabber
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2009-07-05