Commit 286369a81fc2e1d77a7a134d5c140f023af1298b

Carlos Martín Nieto 2014-09-16T02:27:16

ssh: provide our own types for host key lengths Instead of using the libssh2 defines, provide our own, which eases usage as we do not need to check whether libgit2 was built with libssh2 or not.

diff --git a/include/git2/transport.h b/include/git2/transport.h
index 4fa6372..6c568be 100644
--- a/include/git2/transport.h
+++ b/include/git2/transport.h
@@ -21,6 +21,16 @@
 GIT_BEGIN_DECL
 
 /**
+ * Type of SSH host fingerprint
+ */
+typedef enum {
+	/** MD5, 16 bytes */
+	GIT_CERT_SSH_MD5,
+	/** SHA-1, 20 bytes */
+	GIT_CERT_SSH_SHA1,
+} git_cert_ssh_type ;
+
+/**
  * Hostkey information taken from libssh2
  */
 typedef struct {
@@ -31,9 +41,9 @@ typedef struct {
 	git_cert_t cert_type;
         /**
          * A hostkey type from libssh2, either
-         * `LIBSSH2_HOSTKEY_HASH_MD5` or `LIBSSH2_HOSTKEY_HASH_SHA1`
+         * `GIT_CERT_SSH_MD5` or `GIT_CERT_SSH_SHA1`
          */
-        int type;
+	git_cert_ssh_type type;
         /**
          * Hostkey hash. If the type is MD5, only the first 16 bytes
          * will be set.
diff --git a/src/transports/ssh.c b/src/transports/ssh.c
index 2982091..7175653 100644
--- a/src/transports/ssh.c
+++ b/src/transports/ssh.c
@@ -480,23 +480,21 @@ static int _git_ssh_setup_conn(
 		goto on_error;
 
 	if (t->owner->certificate_check_cb != NULL) {
-		git_cert_hostkey cert;
+		git_cert_hostkey cert = { 0 };
 		const char *key;
-		size_t certlen;
 
 		cert.cert_type = GIT_CERT_HOSTKEY_LIBSSH2;
 
-		cert.type = LIBSSH2_HOSTKEY_HASH_SHA1;
 		key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_SHA1);
 		if (key != NULL) {
-			certlen = 20;
-			memcpy(&cert.hash, key, certlen);
+			cert.type = GIT_CERT_SSH_SHA1;
+			memcpy(&cert.hash, key, 20);
 		} else {
-			cert.type = LIBSSH2_HOSTKEY_HASH_MD5;
 			key = libssh2_hostkey_hash(session, LIBSSH2_HOSTKEY_HASH_MD5);
-			certlen = 16;
-			if (key != NULL)
-				memcpy(&cert.hash, key, certlen);
+			if (key != NULL) {
+				cert.type = GIT_CERT_SSH_MD5;
+				memcpy(&cert.hash, key, 16);
+			}
 		}
 
 		if (key == NULL) {
diff --git a/tests/online/clone.c b/tests/online/clone.c
index 2c36b3d..0dd746a 100644
--- a/tests/online/clone.c
+++ b/tests/online/clone.c
@@ -492,6 +492,8 @@ int ssh_certificate_check(git_cert *cert, int valid, void *payload)
 	key = (git_cert_hostkey *) cert;
 	git_oid_fromraw(&actual, key->hash);
 
+	cl_assert_equal_i(GIT_CERT_SSH_SHA1, key->type);
+
 	cl_assert(git_oid_equal(&expected, &actual));
 
 	return GIT_EUSER;