Blame SOURCES/libssh-SHA256.patch

3cd302
From f3f140e65f0e58fc37b04dbe4173d6ecda0127ac Mon Sep 17 00:00:00 2001
3cd302
From: Jan-Niklas Burfeind <libssh@aiyionpri.me>
3cd302
Date: Thu, 9 Aug 2018 11:00:00 +0200
3cd302
Subject: dh: Add SSH_PUBLICKEY_HASH_SHA256 to ssh_get_publickey_hash()
3cd302
3cd302
Signed-off-by: Jan-Niklas Burfeind <libssh@aiyionpri.me>
3cd302
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
3cd302
(cherry picked from commit 1499b38aef17beac8b438522535daf428600d529)
3cd302
---
3cd302
 include/libssh/libssh.h |  3 ++-
3cd302
 src/dh.c                | 23 +++++++++++++++++++++++
3cd302
 2 files changed, 25 insertions(+), 1 deletion(-)
3cd302
3cd302
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
3cd302
index 37214898..320dc032 100644
3cd302
--- a/include/libssh/libssh.h
3cd302
+++ b/include/libssh/libssh.h
3cd302
@@ -444,7 +444,8 @@ LIBSSH_API int ssh_get_publickey(ssh_session session, ssh_key *key);
3cd302
 
3cd302
 enum ssh_publickey_hash_type {
3cd302
     SSH_PUBLICKEY_HASH_SHA1,
3cd302
-    SSH_PUBLICKEY_HASH_MD5
3cd302
+    SSH_PUBLICKEY_HASH_MD5,
3cd302
+    SSH_PUBLICKEY_HASH_SHA256
3cd302
 };
3cd302
 LIBSSH_API int ssh_get_publickey_hash(const ssh_key key,
3cd302
                                       enum ssh_publickey_hash_type type,
3cd302
diff --git a/src/dh.c b/src/dh.c
3cd302
index d27b66eb..bf1ade8b 100644
3cd302
--- a/src/dh.c
3cd302
+++ b/src/dh.c
3cd302
@@ -1039,6 +1039,29 @@ int ssh_get_publickey_hash(const ssh_key key,
3cd302
             *hlen = SHA_DIGEST_LEN;
3cd302
         }
3cd302
         break;
3cd302
+    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
+        {
3cd302
+            SHA256CTX ctx;
3cd302
+
3cd302
+            h = malloc(SHA256_DIGEST_LEN);
3cd302
+            if (h == NULL) {
3cd302
+                rc = -1;
3cd302
+                goto out;
3cd302
+            }
3cd302
+
3cd302
+            ctx = sha256_init();
3cd302
+            if (ctx == NULL) {
3cd302
+                free(h);
3cd302
+                rc = -1;
3cd302
+                goto out;
3cd302
+            }
3cd302
+
3cd302
+            sha256_update(ctx, ssh_string_data(blob), ssh_string_len(blob));
3cd302
+            sha256_final(h, ctx);
3cd302
+
3cd302
+            *hlen = SHA256_DIGEST_LEN;
3cd302
+        }
3cd302
+        break;
3cd302
     case SSH_PUBLICKEY_HASH_MD5:
3cd302
         {
3cd302
             MD5CTX ctx;
3cd302
-- 
3cd302
cgit v1.2.1
3cd302
3cd302
From 9c62d6dfcd798d28895f5dd1b76a28524bcf18d3 Mon Sep 17 00:00:00 2001
3cd302
From: Jan-Niklas Burfeind <libssh@aiyionpri.me>
3cd302
Date: Thu, 9 Aug 2018 11:00:00 +0200
3cd302
Subject: dh: Add ssh_print_hash() function which can deal with sha256
3cd302
3cd302
Signed-off-by: Jan-Niklas Burfeind <libssh@aiyionpri.me>
3cd302
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
3cd302
(cherry picked from commit f32cb706752d8dc35ad53a64f51e432cc0bc41cd)
3cd302
---
3cd302
 include/libssh/libssh.h |  1 +
3cd302
 src/dh.c                | 80 +++++++++++++++++++++++++++++++++++++++++++++++++
3cd302
 2 files changed, 81 insertions(+)
3cd302
3cd302
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
3cd302
index 320dc032..f6cce1e4 100644
3cd302
--- a/include/libssh/libssh.h
3cd302
+++ b/include/libssh/libssh.h
3cd302
@@ -564,6 +564,7 @@ LIBSSH_API int ssh_pki_export_pubkey_file(const ssh_key key,
3cd302
 
3cd302
 LIBSSH_API const char *ssh_pki_key_ecdsa_name(const ssh_key key);
3cd302
 
3cd302
+LIBSSH_API void ssh_print_hash(enum ssh_publickey_hash_type type, unsigned char *hash, size_t len);
3cd302
 LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len);
3cd302
 LIBSSH_API int ssh_send_ignore (ssh_session session, const char *data);
3cd302
 LIBSSH_API int ssh_send_debug (ssh_session session, const char *message, int always_display);
3cd302
diff --git a/src/dh.c b/src/dh.c
3cd302
index bf1ade8b..66a0e704 100644
3cd302
--- a/src/dh.c
3cd302
+++ b/src/dh.c
3cd302
@@ -1097,6 +1097,38 @@ out:
3cd302
     return rc;
3cd302
 }
3cd302
 
3cd302
+/**
3cd302
+ * @internal
3cd302
+ *
3cd302
+ * @brief Convert a buffer into an unpadded base64 string.
3cd302
+ * The caller has to free the memory.
3cd302
+ *
3cd302
+ * @param  hash         What should be converted to a base64 string.
3cd302
+ *
3cd302
+ * @param  len          Length of the buffer to convert.
3cd302
+ *
3cd302
+ * @return              The base64 string or NULL on error.
3cd302
+ *
3cd302
+ * @see ssh_string_free_char()
3cd302
+ */
3cd302
+static char *ssh_get_b64_unpadded(const unsigned char *hash, size_t len)
3cd302
+{
3cd302
+    char *b64_padded = NULL;
3cd302
+    char *b64_unpadded = NULL;
3cd302
+    size_t k;
3cd302
+
3cd302
+    b64_padded = (char *)bin_to_base64(hash, (int)len);
3cd302
+    if (b64_padded == NULL) {
3cd302
+        return NULL;
3cd302
+    }
3cd302
+    for (k = strlen(b64_padded); k != 0 && b64_padded[k-1] == '='; k--);
3cd302
+
3cd302
+    b64_unpadded = strndup(b64_padded, k);
3cd302
+    SAFE_FREE(b64_padded);
3cd302
+
3cd302
+    return b64_unpadded;
3cd302
+}
3cd302
+
3cd302
 /**
3cd302
  * @brief Convert a buffer into a colon separated hex string.
3cd302
  * The caller has to free the memory.
3cd302
@@ -1134,6 +1166,54 @@ char *ssh_get_hexa(const unsigned char *what, size_t len) {
3cd302
   return hexa;
3cd302
 }
3cd302
 
3cd302
+/**
3cd302
+ * @brief Print a hash as a human-readable hex- or base64-string.
3cd302
+ *
3cd302
+ * This function prints hex strings if the given hash is a md5 sum.
3cd302
+ * But prints unpadded base64 strings for sha sums.
3cd302
+ * Either way, the output is prepended by the hash-type.
3cd302
+ *
3cd302
+ * @param  type         Which sort of hash is given.
3cd302
+ *
3cd302
+ * @param  hash         What should be converted to a base64 string.
3cd302
+ *
3cd302
+ * @param  len          Length of the buffer to convert.
3cd302
+ */
3cd302
+void ssh_print_hash(enum ssh_publickey_hash_type type,
3cd302
+                    unsigned char *hash,
3cd302
+                    size_t len) {
3cd302
+    const char *prefix = "UNKNOWN";
3cd302
+    char *fingerprint = NULL;
3cd302
+
3cd302
+    switch (type) {
3cd302
+    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
+    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
+        fingerprint = ssh_get_b64_unpadded(hash, len);
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_MD5:
3cd302
+        fingerprint = ssh_get_hexa(hash, len);
3cd302
+        break;
3cd302
+    }
3cd302
+    if (fingerprint == NULL) {
3cd302
+        return;
3cd302
+    }
3cd302
+
3cd302
+    switch (type) {
3cd302
+    case SSH_PUBLICKEY_HASH_MD5:
3cd302
+        prefix = "MD5";
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
+        prefix = "SHA1";
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
+        prefix = "SHA256";
3cd302
+        break;
3cd302
+    }
3cd302
+    fprintf(stderr, "%s:%s\n", prefix, fingerprint);
3cd302
+
3cd302
+    SAFE_FREE(fingerprint);
3cd302
+}
3cd302
+
3cd302
 /**
3cd302
  * @brief Print a buffer as colon separated hex string.
3cd302
  *
3cd302
-- 
3cd302
cgit v1.2.1
3cd302
3cd302
From 7a7c0a54bc24391fcff0aaccd983de621cb3e60d Mon Sep 17 00:00:00 2001
3cd302
From: Andreas Schneider <asn@cryptomilk.org>
3cd302
Date: Sun, 2 Sep 2018 15:45:41 +0200
3cd302
Subject: dh: Add ssh_get_fingerprint_hash()
3cd302
3cd302
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
3cd302
(cherry picked from commit bbed139ecab26cb46b0bb3a21fa4cd2a4f12dadd)
3cd302
---
3cd302
 include/libssh/libssh.h |  3 ++
3cd302
 src/dh.c                | 73 +++++++++++++++++++++++++++++++++++++++++++++++++
3cd302
 2 files changed, 76 insertions(+)
3cd302
3cd302
diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h
3cd302
index f6cce1e4..7f59abe4 100644
3cd302
--- a/include/libssh/libssh.h
3cd302
+++ b/include/libssh/libssh.h
3cd302
@@ -564,6 +564,9 @@ LIBSSH_API int ssh_pki_export_pubkey_file(const ssh_key key,
3cd302
 
3cd302
 LIBSSH_API const char *ssh_pki_key_ecdsa_name(const ssh_key key);
3cd302
 
3cd302
+LIBSSH_API char *ssh_get_fingerprint_hash(enum ssh_publickey_hash_type type,
3cd302
+                                          unsigned char *hash,
3cd302
+                                          size_t len);
3cd302
 LIBSSH_API void ssh_print_hash(enum ssh_publickey_hash_type type, unsigned char *hash, size_t len);
3cd302
 LIBSSH_API void ssh_print_hexa(const char *descr, const unsigned char *what, size_t len);
3cd302
 LIBSSH_API int ssh_send_ignore (ssh_session session, const char *data);
3cd302
diff --git a/src/dh.c b/src/dh.c
3cd302
index 66a0e704..38298b2d 100644
3cd302
--- a/src/dh.c
3cd302
+++ b/src/dh.c
3cd302
@@ -1166,6 +1166,79 @@ char *ssh_get_hexa(const unsigned char *what, size_t len) {
3cd302
   return hexa;
3cd302
 }
3cd302
 
3cd302
+/**
3cd302
+ * @brief Get a hash as a human-readable hex- or base64-string.
3cd302
+ *
3cd302
+ * This gets an allocated fingerprint hash. It is a hex strings if the given
3cd302
+ * hash is a md5 sum.  If it is a SHA sum, it will return an unpadded base64
3cd302
+ * strings.  Either way, the output is prepended by the hash-type.
3cd302
+ *
3cd302
+ * @param  type         Which sort of hash is given.
3cd302
+ *
3cd302
+ * @param  hash         What should be converted to a base64 string.
3cd302
+ *
3cd302
+ * @param  len          Length of the buffer to convert.
3cd302
+ *
3cd302
+ * @return Returns the allocated fingerprint hash or NULL on error.
3cd302
+ *
3cd302
+ * @see ssh_string_free_char()
3cd302
+ */
3cd302
+char *ssh_get_fingerprint_hash(enum ssh_publickey_hash_type type,
3cd302
+                               unsigned char *hash,
3cd302
+                               size_t len)
3cd302
+{
3cd302
+    const char *prefix = "UNKNOWN";
3cd302
+    char *fingerprint = NULL;
3cd302
+    char *str = NULL;
3cd302
+    size_t str_len;
3cd302
+    int rc;
3cd302
+
3cd302
+    switch (type) {
3cd302
+    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
+    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
+        fingerprint = ssh_get_b64_unpadded(hash, len);
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_MD5:
3cd302
+        fingerprint = ssh_get_hexa(hash, len);
3cd302
+        break;
3cd302
+    }
3cd302
+    if (fingerprint == NULL) {
3cd302
+        return NULL;
3cd302
+    }
3cd302
+
3cd302
+    switch (type) {
3cd302
+    case SSH_PUBLICKEY_HASH_MD5:
3cd302
+        prefix = "MD5";
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
+        prefix = "SHA1";
3cd302
+        break;
3cd302
+    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
+        prefix = "SHA256";
3cd302
+        break;
3cd302
+    }
3cd302
+
3cd302
+    str_len = strlen(prefix);
3cd302
+    if (str_len + 1 + strlen(fingerprint) + 1 < str_len) {
3cd302
+        SAFE_FREE(fingerprint);
3cd302
+        return NULL;
3cd302
+    }
3cd302
+    str_len += 1 + strlen(fingerprint) + 1;
3cd302
+
3cd302
+    str = malloc(str_len);
3cd302
+    if (str == NULL) {
3cd302
+        SAFE_FREE(fingerprint);
3cd302
+        return NULL;
3cd302
+    }
3cd302
+    rc = snprintf(str, str_len, "%s:%s", prefix, fingerprint);
3cd302
+    SAFE_FREE(fingerprint);
3cd302
+    if (rc < 0 || rc < (int)(str_len - 1)) {
3cd302
+        SAFE_FREE(str);
3cd302
+    }
3cd302
+
3cd302
+    return str;
3cd302
+}
3cd302
+
3cd302
 /**
3cd302
  * @brief Print a hash as a human-readable hex- or base64-string.
3cd302
  *
3cd302
-- 
3cd302
cgit v1.2.1
3cd302
3cd302
From e765c1400a724cc5009fd03395ef54b28de9c296 Mon Sep 17 00:00:00 2001
3cd302
From: Andreas Schneider <asn@cryptomilk.org>
3cd302
Date: Sun, 2 Sep 2018 15:47:41 +0200
3cd302
Subject: dh: Use ssh_get_fingerprint_hash() in ssh_print_hash()
3cd302
3cd302
Signed-off-by: Andreas Schneider <asn@cryptomilk.org>
3cd302
(cherry picked from commit 92aa2cf4963b714d0f30d4fb0f9e609200224f7a)
3cd302
---
3cd302
 src/dh.c | 29 ++++++-----------------------
3cd302
 1 file changed, 6 insertions(+), 23 deletions(-)
3cd302
3cd302
diff --git a/src/dh.c b/src/dh.c
3cd302
index 38298b2d..d7182798 100644
3cd302
--- a/src/dh.c
3cd302
+++ b/src/dh.c
3cd302
@@ -1254,35 +1254,18 @@ char *ssh_get_fingerprint_hash(enum ssh_publickey_hash_type type,
3cd302
  */
3cd302
 void ssh_print_hash(enum ssh_publickey_hash_type type,
3cd302
                     unsigned char *hash,
3cd302
-                    size_t len) {
3cd302
-    const char *prefix = "UNKNOWN";
3cd302
+                    size_t len)
3cd302
+{
3cd302
     char *fingerprint = NULL;
3cd302
 
3cd302
-    switch (type) {
3cd302
-    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
-    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
-        fingerprint = ssh_get_b64_unpadded(hash, len);
3cd302
-        break;
3cd302
-    case SSH_PUBLICKEY_HASH_MD5:
3cd302
-        fingerprint = ssh_get_hexa(hash, len);
3cd302
-        break;
3cd302
-    }
3cd302
+    fingerprint = ssh_get_fingerprint_hash(type,
3cd302
+                                           hash,
3cd302
+                                           len);
3cd302
     if (fingerprint == NULL) {
3cd302
         return;
3cd302
     }
3cd302
 
3cd302
-    switch (type) {
3cd302
-    case SSH_PUBLICKEY_HASH_MD5:
3cd302
-        prefix = "MD5";
3cd302
-        break;
3cd302
-    case SSH_PUBLICKEY_HASH_SHA1:
3cd302
-        prefix = "SHA1";
3cd302
-        break;
3cd302
-    case SSH_PUBLICKEY_HASH_SHA256:
3cd302
-        prefix = "SHA256";
3cd302
-        break;
3cd302
-    }
3cd302
-    fprintf(stderr, "%s:%s\n", prefix, fingerprint);
3cd302
+    fprintf(stderr, "%s\n", fingerprint);
3cd302
 
3cd302
     SAFE_FREE(fingerprint);
3cd302
 }
3cd302
-- 
3cd302
cgit v1.2.1
3cd302