4369a3
From ed7ec0cdf577ffbb0b15145340cf51596ca3eb89 Mon Sep 17 00:00:00 2001
4369a3
From: Jakub Jelen <jjelen@redhat.com>
4369a3
Date: Tue, 14 May 2019 10:45:45 +0200
4369a3
Subject: [PATCH] Use high-level OpenSSL API for signatures
4369a3
4369a3
---
4369a3
 digest-openssl.c |  16 ++++
4369a3
 digest.h         |   6 ++
4369a3
 ssh-dss.c        |  65 ++++++++++------
4369a3
 ssh-ecdsa.c      |  69 ++++++++++-------
4369a3
 ssh-rsa.c        | 193 +++++++++--------------------------------------
4369a3
 sshkey.c         |  77 +++++++++++++++++++
4369a3
 sshkey.h         |   4 +
4369a3
 7 files changed, 221 insertions(+), 209 deletions(-)
4369a3
4369a3
diff --git a/digest-openssl.c b/digest-openssl.c
4369a3
index da7ed72bc..6a21d8adb 100644
4369a3
--- a/digest-openssl.c
4369a3
+++ b/digest-openssl.c
4369a3
@@ -63,6 +63,22 @@ const struct ssh_digest digests[] = {
4369a3
 	{ -1,			NULL,		0,	NULL },
4369a3
 };
4369a3
 
4369a3
+const EVP_MD *
4369a3
+ssh_digest_to_md(int digest_type)
4369a3
+{
4369a3
+	switch (digest_type) {
4369a3
+	case SSH_DIGEST_SHA1:
4369a3
+		return EVP_sha1();
4369a3
+	case SSH_DIGEST_SHA256:
4369a3
+		return EVP_sha256();
4369a3
+	case SSH_DIGEST_SHA384:
4369a3
+		return EVP_sha384();
4369a3
+	case SSH_DIGEST_SHA512:
4369a3
+		return EVP_sha512();
4369a3
+	}
4369a3
+	return NULL;
4369a3
+}
4369a3
+
4369a3
 static const struct ssh_digest *
4369a3
 ssh_digest_by_alg(int alg)
4369a3
 {
4369a3
diff --git a/digest.h b/digest.h
4369a3
index 274574d0e..c7ceeb36f 100644
4369a3
--- a/digest.h
4369a3
+++ b/digest.h
4369a3
@@ -32,6 +32,12 @@
4369a3
 struct sshbuf;
4369a3
 struct ssh_digest_ctx;
4369a3
 
4369a3
+#ifdef WITH_OPENSSL
4369a3
+#include <openssl/evp.h>
4369a3
+/* Converts internal digest representation to the OpenSSL one */
4369a3
+const EVP_MD *ssh_digest_to_md(int digest_type);
4369a3
+#endif
4369a3
+
4369a3
 /* Looks up a digest algorithm by name */
4369a3
 int ssh_digest_alg_by_name(const char *name);
4369a3
 
4369a3
diff --git a/ssh-dss.c b/ssh-dss.c
4369a3
index a23c383dc..ea45e7275 100644
4369a3
--- a/ssh-dss.c
4369a3
+++ b/ssh-dss.c
4369a3
@@ -52,11 +52,15 @@ int
4369a3
 ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
     const u_char *data, size_t datalen, u_int compat)
4369a3
 {
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 	DSA_SIG *sig = NULL;
4369a3
 	const BIGNUM *sig_r, *sig_s;
4369a3
-	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
4369a3
-	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
4369a3
+	u_char sigblob[SIGBLOB_LEN];
4369a3
+	size_t rlen, slen;
4369a3
+	int len;
4369a3
 	struct sshbuf *b = NULL;
4369a3
+	u_char *sigb = NULL;
4369a3
+	const u_char *psig = NULL;
4369a3
 	int ret = SSH_ERR_INVALID_ARGUMENT;
4369a3
 
4369a3
 	if (lenp != NULL)
4369a3
@@ -67,17 +71,24 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 	if (key == NULL || key->dsa == NULL ||
4369a3
 	    sshkey_type_plain(key->type) != KEY_DSA)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-	if (dlen == 0)
4369a3
-		return SSH_ERR_INTERNAL_ERROR;
4369a3
 
4369a3
-	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_DSA(pkey, key->dsa) != 1)
4369a3
+		return SSH_ERR_ALLOC_FAIL;
4369a3
+	ret = sshkey_calculate_signature(pkey, SSH_DIGEST_SHA1, &sigb, &len,
4369a3
+	    data, datalen);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
+	if (ret < 0) {
4369a3
 		goto out;
4369a3
+	}
4369a3
 
4369a3
-	if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
4369a3
+	psig = sigb;
4369a3
+	if ((sig = d2i_DSA_SIG(NULL, &psig, len)) == NULL) {
4369a3
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
 	}
4369a3
+	free(sigb);
4369a3
+	sigb = NULL;
4369a3
 
4369a3
 	DSA_SIG_get0(sig, &sig_r, &sig_s);
4369a3
 	rlen = BN_num_bytes(sig_r);
4369a3
@@ -110,7 +121,7 @@ ssh_dss_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 		*lenp = len;
4369a3
 	ret = 0;
4369a3
  out:
4369a3
-	explicit_bzero(digest, sizeof(digest));
4369a3
+	free(sigb);
4369a3
 	DSA_SIG_free(sig);
4369a3
 	sshbuf_free(b);
4369a3
 	return ret;
4369a3
@@ -121,20 +132,20 @@ ssh_dss_verify(const struct sshkey *key,
4369a3
     const u_char *signature, size_t signaturelen,
4369a3
     const u_char *data, size_t datalen, u_int compat)
4369a3
 {
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 	DSA_SIG *sig = NULL;
4369a3
 	BIGNUM *sig_r = NULL, *sig_s = NULL;
4369a3
-	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
4369a3
-	size_t len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
4369a3
+	u_char *sigblob = NULL;
4369a3
+	size_t len, slen;
4369a3
 	int ret = SSH_ERR_INTERNAL_ERROR;
4369a3
 	struct sshbuf *b = NULL;
4369a3
 	char *ktype = NULL;
4369a3
+	u_char *sigb = NULL, *psig = NULL;
4369a3
 
4369a3
 	if (key == NULL || key->dsa == NULL ||
4369a3
 	    sshkey_type_plain(key->type) != KEY_DSA ||
4369a3
 	    signature == NULL || signaturelen == 0)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-	if (dlen == 0)
4369a3
-		return SSH_ERR_INTERNAL_ERROR;
4369a3
 
4369a3
 	/* fetch signature */
4369a3
 	if ((b = sshbuf_from(signature, signaturelen)) == NULL)
4369a3
@@ -176,25 +187,31 @@ ssh_dss_verify(const struct sshkey *key,
4369a3
 	}
4369a3
 	sig_r = sig_s = NULL; /* transferred */
4369a3
 
4369a3
-	/* sha1 the data */
4369a3
-	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
+	if ((slen = i2d_DSA_SIG(sig, NULL)) == 0) {
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
-
4369a3
-	switch (DSA_do_verify(digest, dlen, sig, key->dsa)) {
4369a3
-	case 1:
4369a3
-		ret = 0;
4369a3
-		break;
4369a3
-	case 0:
4369a3
-		ret = SSH_ERR_SIGNATURE_INVALID;
4369a3
+	}
4369a3
+	if ((sigb = malloc(slen)) == NULL) {
4369a3
+		ret = SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
-	default:
4369a3
+	}
4369a3
+	psig = sigb;
4369a3
+	if ((slen = i2d_DSA_SIG(sig, &psig)) == 0) {
4369a3
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
 	}
4369a3
 
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_DSA(pkey, key->dsa) != 1) {
4369a3
+		ret = SSH_ERR_ALLOC_FAIL;
4369a3
+		goto out;
4369a3
+	}
4369a3
+	ret = sshkey_verify_signature(pkey, SSH_DIGEST_SHA1, data, datalen,
4369a3
+	    sigb, slen);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
+
4369a3
  out:
4369a3
-	explicit_bzero(digest, sizeof(digest));
4369a3
+	free(sigb);
4369a3
 	DSA_SIG_free(sig);
4369a3
 	BN_clear_free(sig_r);
4369a3
 	BN_clear_free(sig_s);
4369a3
diff --git a/ssh-ecdsa.c b/ssh-ecdsa.c
4369a3
index 599c7199d..b036796e8 100644
4369a3
--- a/ssh-ecdsa.c
4369a3
+++ b/ssh-ecdsa.c
4369a3
@@ -50,11 +50,13 @@ int
4369a3
 ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
     const u_char *data, size_t datalen, u_int compat)
4369a3
 {
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 	ECDSA_SIG *sig = NULL;
4369a3
+	unsigned char *sigb = NULL;
4369a3
+	const unsigned char *psig;
4369a3
 	const BIGNUM *sig_r, *sig_s;
4369a3
 	int hash_alg;
4369a3
-	u_char digest[SSH_DIGEST_MAX_LENGTH];
4369a3
-	size_t len, dlen;
4369a3
+	int len;
4369a3
 	struct sshbuf *b = NULL, *bb = NULL;
4369a3
 	int ret = SSH_ERR_INTERNAL_ERROR;
4369a3
 
4369a3
@@ -67,18 +69,24 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 	    sshkey_type_plain(key->type) != KEY_ECDSA)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
 
4369a3
-	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
4369a3
-	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
4369a3
+	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
4369a3
 		return SSH_ERR_INTERNAL_ERROR;
4369a3
-	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
+
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1)
4369a3
+		return SSH_ERR_ALLOC_FAIL;
4369a3
+	ret = sshkey_calculate_signature(pkey, hash_alg, &sigb, &len, data,
4369a3
+	    datalen);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
+	if (ret < 0) {
4369a3
 		goto out;
4369a3
+	}
4369a3
 
4369a3
-	if ((sig = ECDSA_do_sign(digest, dlen, key->ecdsa)) == NULL) {
4369a3
+	psig = sigb;
4369a3
+	if ((sig = d2i_ECDSA_SIG(NULL, &psig, len)) == NULL) {
4369a3
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
 	}
4369a3
-
4369a3
 	if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
4369a3
 		ret = SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
@@ -102,7 +110,7 @@ ssh_ecdsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 		*lenp = len;
4369a3
 	ret = 0;
4369a3
  out:
4369a3
-	explicit_bzero(digest, sizeof(digest));
4369a3
+	free(sigb);
4369a3
 	sshbuf_free(b);
4369a3
 	sshbuf_free(bb);
4369a3
 	ECDSA_SIG_free(sig);
4369a3
@@ -115,22 +123,21 @@ ssh_ecdsa_verify(const struct sshkey *key,
4369a3
     const u_char *signature, size_t signaturelen,
4369a3
     const u_char *data, size_t datalen, u_int compat)
4369a3
 {
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 	ECDSA_SIG *sig = NULL;
4369a3
 	BIGNUM *sig_r = NULL, *sig_s = NULL;
4369a3
-	int hash_alg;
4369a3
-	u_char digest[SSH_DIGEST_MAX_LENGTH];
4369a3
-	size_t dlen;
4369a3
+	int hash_alg, len;
4369a3
 	int ret = SSH_ERR_INTERNAL_ERROR;
4369a3
 	struct sshbuf *b = NULL, *sigbuf = NULL;
4369a3
 	char *ktype = NULL;
4369a3
+	unsigned char *sigb = NULL, *psig = NULL;
4369a3
 
4369a3
 	if (key == NULL || key->ecdsa == NULL ||
4369a3
 	    sshkey_type_plain(key->type) != KEY_ECDSA ||
4369a3
 	    signature == NULL || signaturelen == 0)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
 
4369a3
-	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
4369a3
-	    (dlen = ssh_digest_bytes(hash_alg)) == 0)
4369a3
+	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
4369a3
 		return SSH_ERR_INTERNAL_ERROR;
4369a3
 
4369a3
 	/* fetch signature */
4369a3
@@ -166,28 +173,36 @@ ssh_ecdsa_verify(const struct sshkey *key,
4369a3
 	}
4369a3
 	sig_r = sig_s = NULL; /* transferred */
4369a3
 
4369a3
-	if (sshbuf_len(sigbuf) != 0) {
4369a3
-		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
4369a3
+	/* Figure out the length */
4369a3
+	if ((len = i2d_ECDSA_SIG(sig, NULL)) == 0) {
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+		goto out;
4369a3
+	}
4369a3
+	if ((sigb = malloc(len)) == NULL) {
4369a3
+		ret = SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
 	}
4369a3
-	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
+	psig = sigb;
4369a3
+	if ((len = i2d_ECDSA_SIG(sig, &psig)) == 0) {
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
 		goto out;
4369a3
+	}
4369a3
 
4369a3
-	switch (ECDSA_do_verify(digest, dlen, sig, key->ecdsa)) {
4369a3
-	case 1:
4369a3
-		ret = 0;
4369a3
-		break;
4369a3
-	case 0:
4369a3
-		ret = SSH_ERR_SIGNATURE_INVALID;
4369a3
+	if (sshbuf_len(sigbuf) != 0) {
4369a3
+		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
4369a3
 		goto out;
4369a3
-	default:
4369a3
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+	}
4369a3
+
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1) {
4369a3
+		ret =  SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
 	}
4369a3
+	ret = sshkey_verify_signature(pkey, hash_alg, data, datalen, sigb, len);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
 
4369a3
  out:
4369a3
-	explicit_bzero(digest, sizeof(digest));
4369a3
+	free(sigb);
4369a3
 	sshbuf_free(sigbuf);
4369a3
 	sshbuf_free(b);
4369a3
 	ECDSA_SIG_free(sig);
4369a3
diff --git a/ssh-rsa.c b/ssh-rsa.c
4369a3
index 9b14f9a9a..8ef3a6aca 100644
4369a3
--- a/ssh-rsa.c
4369a3
+++ b/ssh-rsa.c
4369a3
@@ -37,7 +37,7 @@
4369a3
 
4369a3
 #include "openbsd-compat/openssl-compat.h"
4369a3
 
4369a3
-static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
4369a3
+static int openssh_RSA_verify(int, const u_char *, size_t, u_char *, size_t, EVP_PKEY *);
4369a3
 
4369a3
 static const char *
4369a3
 rsa_hash_alg_ident(int hash_alg)
4369a3
@@ -90,21 +90,6 @@ rsa_hash_id_from_keyname(const char *alg)
4369a3
 	return -1;
4369a3
 }
4369a3
 
4369a3
-static int
4369a3
-rsa_hash_alg_nid(int type)
4369a3
-{
4369a3
-	switch (type) {
4369a3
-	case SSH_DIGEST_SHA1:
4369a3
-		return NID_sha1;
4369a3
-	case SSH_DIGEST_SHA256:
4369a3
-		return NID_sha256;
4369a3
-	case SSH_DIGEST_SHA512:
4369a3
-		return NID_sha512;
4369a3
-	default:
4369a3
-		return -1;
4369a3
-	}
4369a3
-}
4369a3
-
4369a3
 int
4369a3
 ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
4369a3
 {
4369a3
@@ -164,11 +149,10 @@ int
4369a3
 ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
     const u_char *data, size_t datalen, const char *alg_ident)
4369a3
 {
4369a3
-	const BIGNUM *rsa_n;
4369a3
-	u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
4369a3
-	size_t slen = 0;
4369a3
-	u_int dlen, len;
4369a3
-	int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
+	u_char *sig = NULL;
4369a3
+	int len, slen = 0;
4369a3
+	int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
4369a3
 	struct sshbuf *b = NULL;
4369a3
 
4369a3
 	if (lenp != NULL)
4369a3
@@ -180,33 +164,24 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 		hash_alg = SSH_DIGEST_SHA1;
4369a3
 	else
4369a3
 		hash_alg = rsa_hash_id_from_keyname(alg_ident);
4369a3
+
4369a3
 	if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
4369a3
 	    sshkey_type_plain(key->type) != KEY_RSA)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
4369a3
-	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
4369a3
-		return SSH_ERR_KEY_LENGTH;
4369a3
 	slen = RSA_size(key->rsa);
4369a3
-	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
4369a3
-		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-
4369a3
-	/* hash the data */
4369a3
-	nid = rsa_hash_alg_nid(hash_alg);
4369a3
-	if ((dlen = ssh_digest_bytes(hash_alg)) == 0)
4369a3
-		return SSH_ERR_INTERNAL_ERROR;
4369a3
-	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
-		goto out;
4369a3
+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
4369a3
+		return SSH_ERR_KEY_LENGTH;
4369a3
 
4369a3
-	if ((sig = malloc(slen)) == NULL) {
4369a3
-		ret = SSH_ERR_ALLOC_FAIL;
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_RSA(pkey, key->rsa) != 1)
4369a3
+		return SSH_ERR_ALLOC_FAIL;
4369a3
+	ret = sshkey_calculate_signature(pkey, hash_alg, &sig, &len, data,
4369a3
+	    datalen);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
+	if (ret < 0) {
4369a3
 		goto out;
4369a3
 	}
4369a3
 
4369a3
-	if (RSA_sign(nid, digest, dlen, sig, &len, key->rsa) != 1) {
4369a3
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
-		goto out;
4369a3
-	}
4369a3
 	if (len < slen) {
4369a3
 		size_t diff = slen - len;
4369a3
 		memmove(sig + diff, sig, len);
4369a3
@@ -215,6 +190,7 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 		ret = SSH_ERR_INTERNAL_ERROR;
4369a3
 		goto out;
4369a3
 	}
4369a3
+
4369a3
 	/* encode signature */
4369a3
 	if ((b = sshbuf_new()) == NULL) {
4369a3
 		ret = SSH_ERR_ALLOC_FAIL;
4369a3
@@ -235,7 +211,6 @@ ssh_rsa_sign(const struct sshkey *key, u_char **sigp, size_t *lenp,
4369a3
 		*lenp = len;
4369a3
 	ret = 0;
4369a3
  out:
4369a3
-	explicit_bzero(digest, sizeof(digest));
4369a3
 	freezero(sig, slen);
4369a3
 	sshbuf_free(b);
4369a3
 	return ret;
4369a3
@@ -246,10 +221,10 @@ ssh_rsa_verify(const struct sshkey *key,
4369a3
     const u_char *sig, size_t siglen, const u_char *data, size_t datalen,
4369a3
     const char *alg)
4369a3
 {
4369a3
-	const BIGNUM *rsa_n;
4369a3
+	EVP_PKEY *pkey = NULL;
4369a3
 	char *sigtype = NULL;
4369a3
 	int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
4369a3
-	size_t len = 0, diff, modlen, dlen;
4369a3
+	size_t len = 0, diff, modlen;
4369a3
 	struct sshbuf *b = NULL;
4369a3
 	u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
4369a3
 
4369a3
@@ -257,8 +232,7 @@ ssh_rsa_verify(const struct sshkey *key,
4369a3
 	    sshkey_type_plain(key->type) != KEY_RSA ||
4369a3
 	    sig == NULL || siglen == 0)
4369a3
 		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
4369a3
-	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
4369a3
+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
4369a3
 		return SSH_ERR_KEY_LENGTH;
4369a3
 
4369a3
 	if ((b = sshbuf_from(sig, siglen)) == NULL)
4369a3
@@ -310,16 +284,15 @@ ssh_rsa_verify(const struct sshkey *key,
4369a3
 		explicit_bzero(sigblob, diff);
4369a3
 		len = modlen;
4369a3
 	}
4369a3
-	if ((dlen = ssh_digest_bytes(hash_alg)) == 0) {
4369a3
-		ret = SSH_ERR_INTERNAL_ERROR;
4369a3
+
4369a3
+	if ((pkey = EVP_PKEY_new()) == NULL ||
4369a3
+	    EVP_PKEY_set1_RSA(pkey, key->rsa) != 1) {
4369a3
+		ret = SSH_ERR_ALLOC_FAIL;
4369a3
 		goto out;
4369a3
 	}
4369a3
-	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
4369a3
-	    digest, sizeof(digest))) != 0)
4369a3
-		goto out;
4369a3
+	ret = openssh_RSA_verify(hash_alg, data, datalen, sigblob, len, pkey);
4369a3
+	EVP_PKEY_free(pkey);
4369a3
 
4369a3
-	ret = openssh_RSA_verify(hash_alg, digest, dlen, sigblob, len,
4369a3
-	    key->rsa);
4369a3
  out:
4369a3
 	freezero(sigblob, len);
4369a3
 	free(sigtype);
4369a3
@@ -328,122 +301,26 @@ ssh_rsa_verify(const struct sshkey *key,
4369a3
 	return ret;
4369a3
 }
4369a3
 
4369a3
-/*
4369a3
- * See:
4369a3
- * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
4369a3
- * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
4369a3
- */
4369a3
-
4369a3
-/*
4369a3
- * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
4369a3
- *	oiw(14) secsig(3) algorithms(2) 26 }
4369a3
- */
4369a3
-static const u_char id_sha1[] = {
4369a3
-	0x30, 0x21, /* type Sequence, length 0x21 (33) */
4369a3
-	0x30, 0x09, /* type Sequence, length 0x09 */
4369a3
-	0x06, 0x05, /* type OID, length 0x05 */
4369a3
-	0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
4369a3
-	0x05, 0x00, /* NULL */
4369a3
-	0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */
4369a3
-};
4369a3
-
4369a3
-/*
4369a3
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
4369a3
- * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
4369a3
- *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
4369a3
- *      id-sha256(1) }
4369a3
- */
4369a3
-static const u_char id_sha256[] = {
4369a3
-	0x30, 0x31, /* type Sequence, length 0x31 (49) */
4369a3
-	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
4369a3
-	0x06, 0x09, /* type OID, length 0x09 */
4369a3
-	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
4369a3
-	0x05, 0x00, /* NULL */
4369a3
-	0x04, 0x20  /* Octet string, length 0x20 (32), followed by sha256 hash */
4369a3
-};
4369a3
-
4369a3
-/*
4369a3
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
4369a3
- * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
4369a3
- *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
4369a3
- *      id-sha256(3) }
4369a3
- */
4369a3
-static const u_char id_sha512[] = {
4369a3
-	0x30, 0x51, /* type Sequence, length 0x51 (81) */
4369a3
-	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
4369a3
-	0x06, 0x09, /* type OID, length 0x09 */
4369a3
-	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
4369a3
-	0x05, 0x00, /* NULL */
4369a3
-	0x04, 0x40  /* Octet string, length 0x40 (64), followed by sha512 hash */
4369a3
-};
4369a3
-
4369a3
 static int
4369a3
-rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
4369a3
+openssh_RSA_verify(int hash_alg, const u_char *data, size_t datalen,
4369a3
+    u_char *sigbuf, size_t siglen, EVP_PKEY *pkey)
4369a3
 {
4369a3
-	switch (hash_alg) {
4369a3
-	case SSH_DIGEST_SHA1:
4369a3
-		*oidp = id_sha1;
4369a3
-		*oidlenp = sizeof(id_sha1);
4369a3
-		break;
4369a3
-	case SSH_DIGEST_SHA256:
4369a3
-		*oidp = id_sha256;
4369a3
-		*oidlenp = sizeof(id_sha256);
4369a3
-		break;
4369a3
-	case SSH_DIGEST_SHA512:
4369a3
-		*oidp = id_sha512;
4369a3
-		*oidlenp = sizeof(id_sha512);
4369a3
-		break;
4369a3
-	default:
4369a3
-		return SSH_ERR_INVALID_ARGUMENT;
4369a3
-	}
4369a3
-	return 0;
4369a3
-}
4369a3
+	size_t rsasize = 0;
4369a3
+	const RSA *rsa;
4369a3
+	int ret;
4369a3
 
4369a3
-static int
4369a3
-openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
4369a3
-    u_char *sigbuf, size_t siglen, RSA *rsa)
4369a3
-{
4369a3
-	size_t rsasize = 0, oidlen = 0, hlen = 0;
4369a3
-	int ret, len, oidmatch, hashmatch;
4369a3
-	const u_char *oid = NULL;
4369a3
-	u_char *decrypted = NULL;
4369a3
-
4369a3
-	if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
4369a3
-		return ret;
4369a3
-	ret = SSH_ERR_INTERNAL_ERROR;
4369a3
-	hlen = ssh_digest_bytes(hash_alg);
4369a3
-	if (hashlen != hlen) {
4369a3
-		ret = SSH_ERR_INVALID_ARGUMENT;
4369a3
-		goto done;
4369a3
-	}
4369a3
+	rsa = EVP_PKEY_get0_RSA(pkey);
4369a3
 	rsasize = RSA_size(rsa);
4369a3
 	if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
4369a3
 	    siglen == 0 || siglen > rsasize) {
4369a3
 		ret = SSH_ERR_INVALID_ARGUMENT;
4369a3
 		goto done;
4369a3
 	}
4369a3
-	if ((decrypted = malloc(rsasize)) == NULL) {
4369a3
-		ret = SSH_ERR_ALLOC_FAIL;
4369a3
-		goto done;
4369a3
-	}
4369a3
-	if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
4369a3
-	    RSA_PKCS1_PADDING)) < 0) {
4369a3
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
-		goto done;
4369a3
-	}
4369a3
-	if (len < 0 || (size_t)len != hlen + oidlen) {
4369a3
-		ret = SSH_ERR_INVALID_FORMAT;
4369a3
-		goto done;
4369a3
-	}
4369a3
-	oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
4369a3
-	hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
4369a3
-	if (!oidmatch || !hashmatch) {
4369a3
-		ret = SSH_ERR_SIGNATURE_INVALID;
4369a3
-		goto done;
4369a3
-	}
4369a3
-	ret = 0;
4369a3
+
4369a3
+	ret = sshkey_verify_signature(pkey, hash_alg, data, datalen,
4369a3
+	    sigbuf, siglen);
4369a3
+
4369a3
 done:
4369a3
-	freezero(decrypted, rsasize);
4369a3
 	return ret;
4369a3
 }
4369a3
 #endif /* WITH_OPENSSL */
4369a3
diff --git a/sshkey.c b/sshkey.c
4369a3
index ad1957762..b95ed0b10 100644
4369a3
--- a/sshkey.c
4369a3
+++ b/sshkey.c
4369a3
@@ -358,6 +358,83 @@ sshkey_type_plain(int type)
4369a3
 }
4369a3
 
4369a3
 #ifdef WITH_OPENSSL
4369a3
+int
4369a3
+sshkey_calculate_signature(EVP_PKEY *pkey, int hash_alg, u_char **sigp,
4369a3
+    int *lenp, const u_char *data, size_t datalen)
4369a3
+{
4369a3
+	EVP_MD_CTX *ctx = NULL;
4369a3
+	u_char *sig = NULL;
4369a3
+	int ret, slen, len;
4369a3
+
4369a3
+	if (sigp == NULL || lenp == NULL) {
4369a3
+		return SSH_ERR_INVALID_ARGUMENT;
4369a3
+	}
4369a3
+
4369a3
+	slen = EVP_PKEY_size(pkey);
4369a3
+	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
4369a3
+		return SSH_ERR_INVALID_ARGUMENT;
4369a3
+
4369a3
+	len = slen;
4369a3
+	if ((sig = malloc(slen)) == NULL) {
4369a3
+		return SSH_ERR_ALLOC_FAIL;
4369a3
+	}
4369a3
+
4369a3
+	if ((ctx = EVP_MD_CTX_new()) == NULL) {
4369a3
+		ret = SSH_ERR_ALLOC_FAIL;
4369a3
+		goto error;
4369a3
+	}
4369a3
+	if (EVP_SignInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
4369a3
+	    EVP_SignUpdate(ctx, data, datalen) <= 0 ||
4369a3
+	    EVP_SignFinal(ctx, sig, &len, pkey) <= 0) {
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+		goto error;
4369a3
+	}
4369a3
+
4369a3
+	*sigp = sig;
4369a3
+	*lenp = len;
4369a3
+	/* Now owned by the caller */
4369a3
+	sig = NULL;
4369a3
+	ret = 0;
4369a3
+
4369a3
+error:
4369a3
+	EVP_MD_CTX_free(ctx);
4369a3
+	free(sig);
4369a3
+	return ret;
4369a3
+}
4369a3
+
4369a3
+int
4369a3
+sshkey_verify_signature(EVP_PKEY *pkey, int hash_alg, const u_char *data,
4369a3
+    size_t datalen, u_char *sigbuf, int siglen)
4369a3
+{
4369a3
+	EVP_MD_CTX *ctx = NULL;
4369a3
+	int ret;
4369a3
+
4369a3
+	if ((ctx = EVP_MD_CTX_new()) == NULL) {
4369a3
+		return SSH_ERR_ALLOC_FAIL;
4369a3
+	}
4369a3
+	if (EVP_VerifyInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
4369a3
+	    EVP_VerifyUpdate(ctx, data, datalen) <= 0) {
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+		goto done;
4369a3
+	}
4369a3
+	ret = EVP_VerifyFinal(ctx, sigbuf, siglen, pkey);
4369a3
+	switch (ret) {
4369a3
+	case 1:
4369a3
+		ret = 0;
4369a3
+		break;
4369a3
+	case 0:
4369a3
+		ret = SSH_ERR_SIGNATURE_INVALID;
4369a3
+		break;
4369a3
+	default:
4369a3
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
4369a3
+		break;
4369a3
+	}
4369a3
+
4369a3
+done:
4369a3
+	EVP_MD_CTX_free(ctx);
4369a3
+	return ret;
4369a3
+}
4369a3
+
4369a3
 /* XXX: these are really begging for a table-driven approach */
4369a3
 int
4369a3
 sshkey_curve_name_to_nid(const char *name)
4369a3
diff --git a/sshkey.h b/sshkey.h
4369a3
index a91e60436..270901a87 100644
4369a3
--- a/sshkey.h
4369a3
+++ b/sshkey.h
4369a3
@@ -179,6 +179,10 @@ const char	*sshkey_ssh_name(const struct sshkey *);
4369a3
 const char	*sshkey_ssh_name_plain(const struct sshkey *);
4369a3
 int		 sshkey_names_valid2(const char *, int);
4369a3
 char		*sshkey_alg_list(int, int, int, char);
4369a3
+int		 sshkey_calculate_signature(EVP_PKEY*, int, u_char **,
4369a3
+    int *, const u_char *, size_t);
4369a3
+int		 sshkey_verify_signature(EVP_PKEY *, int, const u_char *,
4369a3
+    size_t, u_char *, int);
4369a3
 
4369a3
 int	 sshkey_from_blob(const u_char *, size_t, struct sshkey **);
4369a3
 int	 sshkey_fromb(struct sshbuf *, struct sshkey **);
4369a3