Subject: Re: known hosts API, updated

Re: known hosts API, updated

From: Ben Kibbey <bjk_at_luxsci.net>
Date: Sat, 4 Jul 2009 09:28:51 -0400

On Sat, Jul 04, 2009 at 09:08:45AM +0200, Daniel Stenberg wrote:
> On Fri, 3 Jul 2009, Ben Kibbey wrote:
>
>>> Hm, it just now struck me that the libssh2_session_hostkey() function
>>> is still not documented in a man page (I'll fix right away) but also
>>> that it doesn't report the key type. I think we need to fix this.
>
>> Any plans on reporting the key type before 1.2?
>
> Arg. I had forgot about this. I don't know exactly how much I'll be able
> to do myself. I do welcome patches!

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:

diff --git a/include/libssh2.h b/include/libssh2.h
index 169f704..9c96f5b 100644
--- a/include/libssh2.h
+++ b/include/libssh2.h
@@ -381,7 +381,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..3a54ffb 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 0;
+
+ if (!memcmp(rsa, hostkey, 11))
+ return 1;
+
+ if (!memcmp(dss, hostkey, 11))
+ return 2;
+
+ return 0;
+}
+
 /*
  * 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-04