summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoe Orton <joe@manyfish.uk>2020-06-18 19:24:40 +0100
committerJoe Orton <joe@manyfish.uk>2020-06-18 19:24:40 +0100
commitff0479efa7eae395a0054197fb3cf81c2048e5da (patch)
tree96650877a712be54b5e197adf452164efa5918e7
parent2218c82e22d9512c01387ce2543a4f4cf68a35bb (diff)
downloadneon-git-ff0479efa7eae395a0054197fb3cf81c2048e5da.tar.gz
Provide ne_vstrhash() implementation using GnuTLS:
* src/ne_string.c (ne__strhash2hex): Moved here... * src/ne_openssl.c (hash2hex): ... from here. (ne_vstrhash): Adjust accordingly. * src/ne_gnutls.c (ne_vstrhash): Implement.
-rw-r--r--src/ne_gnutls.c31
-rw-r--r--src/ne_internal.h4
-rw-r--r--src/ne_openssl.c16
-rw-r--r--src/ne_string.c17
4 files changed, 52 insertions, 16 deletions
diff --git a/src/ne_gnutls.c b/src/ne_gnutls.c
index 7d502be..7d7e661 100644
--- a/src/ne_gnutls.c
+++ b/src/ne_gnutls.c
@@ -33,6 +33,7 @@
#include <errno.h>
#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
#include <gnutls/pkcs12.h>
#ifdef NE_HAVE_TS_SSL
@@ -1505,3 +1506,33 @@ void ne__ssl_exit(void)
gnutls_global_deinit();
#endif
}
+
+char *ne_vstrhash(unsigned int flags, va_list ap)
+{
+ gnutls_digest_algorithm_t alg;
+ gnutls_hash_hd_t hd;
+ unsigned char *out;
+ const char *arg;
+ unsigned len;
+ char *rv;
+
+ switch (flags) {
+ case NE_HASH_MD5: alg = GNUTLS_DIG_MD5; break;
+ case NE_HASH_SHA256: alg = GNUTLS_DIG_SHA256; break;
+ default: return NULL;
+ }
+
+ if (gnutls_hash_init(&hd, alg) < 0)
+ return NULL;
+
+ while ((arg = va_arg(ap, const char *)) != NULL)
+ gnutls_hash(hd, arg, strlen(arg));
+
+ len = gnutls_hash_get_len(alg);
+ out = ne_malloc(len);
+ gnutls_hash_deinit(hd, out);
+
+ rv = ne__strhash2hex(out, len);
+ ne_free(out);
+ return rv;
+}
diff --git a/src/ne_internal.h b/src/ne_internal.h
index c9c6eff..910842b 100644
--- a/src/ne_internal.h
+++ b/src/ne_internal.h
@@ -83,4 +83,8 @@
#endif
#endif /* NE_LFS */
+/* Return malloc-allocated ASCII hexadecimal representation of
+ * input. */
+NE_PRIVATE char *ne__strhash2hex(unsigned char *digest, size_t len);
+
#endif /* NE_INTERNAL_H */
diff --git a/src/ne_openssl.c b/src/ne_openssl.c
index f1ba34f..49ffaa8 100644
--- a/src/ne_openssl.c
+++ b/src/ne_openssl.c
@@ -1139,20 +1139,6 @@ int ne_ssl_cert_digest(const ne_ssl_certificate *cert, char *digest)
return 0;
}
-static char *hash2hex(unsigned char *digest, size_t len)
-{
- char *rv = ne_malloc(len * 2 + 1);
- size_t n;
-
- for (n = 0; n < len; n++) {
- rv[n*2] = NE_HEX2ASC(digest[n] >> 4);
- rv[n*2+1] = NE_HEX2ASC(digest[n] & 0x0f);
- }
-
- rv[len*2] = '\0';
- return rv;
-}
-
char *ne_vstrhash(unsigned int flags, va_list ap)
{
EVP_MD_CTX *ctx;
@@ -1179,7 +1165,7 @@ char *ne_vstrhash(unsigned int flags, va_list ap)
EVP_DigestFinal_ex(ctx, v, &vlen);
EVP_MD_CTX_free(ctx);
- return hash2hex(v, vlen);
+ return ne__strhash2hex(v, vlen);
}
#if defined(NE_HAVE_TS_SSL) && OPENSSL_VERSION_NUMBER < 0x10100000L
diff --git a/src/ne_string.c b/src/ne_string.c
index 187fa58..ce683cc 100644
--- a/src/ne_string.c
+++ b/src/ne_string.c
@@ -38,8 +38,9 @@
#include "ne_alloc.h"
#include "ne_string.h"
+#include "ne_internal.h"
-#if !defined(HAVE_OPENSSL)
+#ifndef NE_HAVE_SSL
#include "ne_md5.h"
#define NEED_VSTRHASH
#endif
@@ -654,6 +655,20 @@ char *ne_vstrhash(unsigned int flags, va_list ap)
}
#endif
+char *ne__strhash2hex(unsigned char *digest, size_t len)
+{
+ char *rv = ne_malloc(len * 2 + 1);
+ size_t n;
+
+ for (n = 0; n < len; n++) {
+ rv[n*2] = NE_HEX2ASC(digest[n] >> 4);
+ rv[n*2+1] = NE_HEX2ASC(digest[n] & 0x0f);
+ }
+
+ rv[len*2] = '\0';
+ return rv;
+}
+
/* Determines whether a character is valid in a regular parameter (NQ)
* not (QT). Per https://tools.ietf.org/html/rfc5987#section-3.2.1
* every character in attr-char is NQ, everything else is QT. */