aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAndreas Schneider <asn@cryptomilk.org>2013-11-06 16:39:00 +0100
committerAndreas Schneider <asn@cryptomilk.org>2013-11-06 17:11:24 +0100
commit9bf9d52e21fc629140b27adb291a3d4e30b7122d (patch)
tree450ad9d057d4265b4fd3f4a668111f20febfab50 /src
parent965000129e9eeb27e1ab8805ff3286eae65ebf9b (diff)
downloadlibssh-9bf9d52e21fc629140b27adb291a3d4e30b7122d.tar.gz
libssh-9bf9d52e21fc629140b27adb291a3d4e30b7122d.tar.xz
libssh-9bf9d52e21fc629140b27adb291a3d4e30b7122d.zip
dh: Add new ssh_get_publickey_hash() function.
Diffstat (limited to 'src')
-rw-r--r--src/dh.c121
1 files changed, 102 insertions, 19 deletions
diff --git a/src/dh.c b/src/dh.c
index f96a94a3..c22c849f 100644
--- a/src/dh.c
+++ b/src/dh.c
@@ -1047,25 +1047,7 @@ error:
*/
/**
- * @brief Allocates a buffer with the MD5 hash of the server public key.
- *
- * This function allows you to get a MD5 hash of the public key. You can then
- * print this hash in a human-readable form to the user so that he is able to
- * verify it. Use ssh_get_hexa() or ssh_print_hexa() to display it.
- *
- * @param[in] session The SSH session to use.
- *
- * @param[in] hash The buffer to allocate.
- *
- * @return The bytes allocated or < 0 on error.
- *
- * @warning It is very important that you verify at some moment that the hash
- * matches a known server. If you don't do it, cryptography wont help
- * you at making things secure
- *
- * @see ssh_is_server_known()
- * @see ssh_get_hexa()
- * @see ssh_print_hexa()
+ * @deprecated Use ssh_get_publickey_hash()
*/
int ssh_get_pubkey_hash(ssh_session session, unsigned char **hash) {
ssh_string pubkey;
@@ -1142,6 +1124,107 @@ int ssh_get_publickey(ssh_session session, ssh_key *key)
key);
}
+/**
+ * @brief Allocates a buffer with the hash of the public key.
+ *
+ * This function allows you to get a hash of the public key. You can then
+ * print this hash in a human-readable form to the user so that he is able to
+ * verify it. Use ssh_get_hexa() or ssh_print_hexa() to display it.
+ *
+ * @param[in] key The public key to create the hash for.
+ *
+ * @param[in] type The type of the hash you want.
+ *
+ * @param[in] hash A pointer to store the allocated buffer. It can be
+ * freed using ssh_clean_pubkey_hash().
+ *
+ * @param[in] hlen The length of the hash.
+ *
+ * @return 0 on success, -1 if an error occured.
+ *
+ * @warning It is very important that you verify at some moment that the hash
+ * matches a known server. If you don't do it, cryptography wont help
+ * you at making things secure.
+ * OpenSSH uses SHA1 to print public key digests.
+ *
+ * @see ssh_is_server_known()
+ * @see ssh_get_hexa()
+ * @see ssh_print_hexa()
+ * @see ssh_clean_pubkey_hash()
+ */
+int ssh_get_publickey_hash(const ssh_key key,
+ enum ssh_publickey_hash_type type,
+ unsigned char **hash,
+ size_t *hlen)
+{
+ ssh_string blob;
+ unsigned char *h;
+ int rc;
+
+ rc = ssh_pki_export_pubkey_blob(key, &blob);
+ if (rc < 0) {
+ return rc;
+ }
+
+ switch (type) {
+ case SSH_PUBLICKEY_HASH_SHA1:
+ {
+ SHACTX ctx;
+
+ h = malloc(SHA_DIGEST_LEN);
+ if (h == NULL) {
+ rc = -1;
+ goto out;
+ }
+
+ ctx = sha1_init();
+ if (ctx == NULL) {
+ free(h);
+ rc = -1;
+ goto out;
+ }
+
+ sha1_update(ctx, ssh_string_data(blob), ssh_string_len(blob));
+ sha1_final(h, ctx);
+
+ *hlen = SHA_DIGEST_LEN;
+ }
+ break;
+ case SSH_PUBLICKEY_HASH_MD5:
+ {
+ MD5CTX ctx;
+
+ h = malloc(MD5_DIGEST_LEN);
+ if (h == NULL) {
+ rc = -1;
+ goto out;
+ }
+
+ ctx = md5_init();
+ if (ctx == NULL) {
+ free(h);
+ rc = -1;
+ goto out;
+ }
+
+ md5_update(ctx, ssh_string_data(blob), ssh_string_len(blob));
+ md5_final(h, ctx);
+
+ *hlen = MD5_DIGEST_LEN;
+ }
+ break;
+ default:
+ rc = -1;
+ goto out;
+ }
+
+ *hash = h;
+ rc = 0;
+out:
+ ssh_string_free(blob);
+ return rc;
+}
+
/** @} */
/* vim: set ts=4 sw=4 et cindent: */