rcolebaugh / rpms / openssh

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