vishalmishra434 / rpms / openssh

Forked from rpms/openssh a month ago
Clone
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/digest.h openssh-9.3p1-patched/digest.h
Norbert Pocs fb40f0
--- openssh-9.3p1/digest.h	2023-03-15 22:28:19.000000000 +0100
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/digest.h	2023-06-06 15:52:25.602551466 +0200
Norbert Pocs fb40f0
@@ -32,6 +32,12 @@
Norbert Pocs fb40f0
 struct sshbuf;
Norbert Pocs fb40f0
 struct ssh_digest_ctx;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+#ifdef WITH_OPENSSL
Norbert Pocs fb40f0
+#include <openssl/evp.h>
Norbert Pocs fb40f0
+/* Converts internal digest representation to the OpenSSL one */
Norbert Pocs fb40f0
+const EVP_MD *ssh_digest_to_md(int digest_type);
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 /* Looks up a digest algorithm by name */
Norbert Pocs fb40f0
 int ssh_digest_alg_by_name(const char *name);
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/digest-openssl.c openssh-9.3p1-patched/digest-openssl.c
Norbert Pocs fb40f0
--- openssh-9.3p1/digest-openssl.c	2023-03-15 22:28:19.000000000 +0100
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/digest-openssl.c	2023-06-06 15:52:25.601551454 +0200
Norbert Pocs fb40f0
@@ -64,6 +64,22 @@
Norbert Pocs fb40f0
 	{ -1,			NULL,		0,	NULL },
Norbert Pocs fb40f0
 };
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+const EVP_MD *
Norbert Pocs fb40f0
+ssh_digest_to_md(int digest_type)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	switch (digest_type) {
Norbert Pocs fb40f0
+	case SSH_DIGEST_SHA1:
Norbert Pocs fb40f0
+		return EVP_sha1();
Norbert Pocs fb40f0
+	case SSH_DIGEST_SHA256:
Norbert Pocs fb40f0
+		return EVP_sha256();
Norbert Pocs fb40f0
+	case SSH_DIGEST_SHA384:
Norbert Pocs fb40f0
+		return EVP_sha384();
Norbert Pocs fb40f0
+	case SSH_DIGEST_SHA512:
Norbert Pocs fb40f0
+		return EVP_sha512();
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	return NULL;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 static const struct ssh_digest *
Norbert Pocs fb40f0
 ssh_digest_by_alg(int alg)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-dss.c openssh-9.3p1-patched/ssh-dss.c
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-dss.c	2023-03-15 22:28:19.000000000 +0100
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-dss.c	2023-06-06 15:52:25.624551743 +0200
Norbert Pocs fb40f0
@@ -32,6 +32,8 @@
Norbert Pocs fb40f0
 #include <openssl/bn.h>
Norbert Pocs fb40f0
 #include <openssl/dsa.h>
Norbert Pocs fb40f0
 #include <openssl/evp.h>
Norbert Pocs fb40f0
+#include <openssl/core_names.h>
Norbert Pocs fb40f0
+#include <openssl/param_build.h>
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include <stdarg.h>
Norbert Pocs fb40f0
 #include <string.h>
Norbert Pocs fb40f0
@@ -261,11 +263,15 @@
Norbert Pocs fb40f0
     const u_char *data, size_t datalen,
Norbert Pocs fb40f0
     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
 	DSA_SIG *sig = NULL;
Norbert Pocs fb40f0
 	const BIGNUM *sig_r, *sig_s;
Norbert Pocs fb40f0
-	u_char digest[SSH_DIGEST_MAX_LENGTH], sigblob[SIGBLOB_LEN];
Norbert Pocs fb40f0
-	size_t rlen, slen, len, dlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
Norbert Pocs fb40f0
+	u_char sigblob[SIGBLOB_LEN];
Norbert Pocs fb40f0
+	size_t rlen, slen;
Norbert Pocs fb40f0
+	int len;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL;
Norbert Pocs fb40f0
+	u_char *sigb = NULL;
Norbert Pocs fb40f0
+	const u_char *psig = NULL;
Norbert Pocs fb40f0
 	int ret = SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if (lenp != NULL)
Norbert Pocs fb40f0
@@ -276,17 +282,23 @@
Norbert Pocs fb40f0
 	if (key == NULL || key->dsa == NULL ||
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_DSA)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-	if (dlen == 0)
Norbert Pocs fb40f0
-		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, datalen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
+  	if ((ret = ssh_create_evp_dss(key, &pkey)) != 0)
Norbert Pocs fb40f0
+    		return ret;
Norbert Pocs fb40f0
+	ret = sshkey_calculate_signature(pkey, SSH_DIGEST_SHA1, &sigb, &len,
Norbert Pocs fb40f0
+	    data, datalen);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+	if (ret < 0) {
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((sig = DSA_do_sign(digest, dlen, key->dsa)) == NULL) {
Norbert Pocs fb40f0
+	psig = sigb;
Norbert Pocs fb40f0
+	if ((sig = d2i_DSA_SIG(NULL, &psig, len)) == NULL) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
+	free(sigb);
Norbert Pocs fb40f0
+	sigb = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	DSA_SIG_get0(sig, &sig_r, &sig_s);
Norbert Pocs fb40f0
 	rlen = BN_num_bytes(sig_r);
Norbert Pocs fb40f0
@@ -319,7 +331,7 @@
Norbert Pocs fb40f0
 		*lenp = len;
Norbert Pocs fb40f0
 	ret = 0;
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	explicit_bzero(digest, sizeof(digest));
Norbert Pocs fb40f0
+	free(sigb);
Norbert Pocs fb40f0
 	DSA_SIG_free(sig);
Norbert Pocs fb40f0
 	sshbuf_free(b);
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
@@ -331,20 +343,20 @@
Norbert Pocs fb40f0
     const u_char *data, size_t dlen, const char *alg, u_int compat,
Norbert Pocs fb40f0
     struct sshkey_sig_details **detailsp)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
 	DSA_SIG *dsig = NULL;
Norbert Pocs fb40f0
 	BIGNUM *sig_r = NULL, *sig_s = NULL;
Norbert Pocs fb40f0
-	u_char digest[SSH_DIGEST_MAX_LENGTH], *sigblob = NULL;
Norbert Pocs fb40f0
-	size_t len, hlen = ssh_digest_bytes(SSH_DIGEST_SHA1);
Norbert Pocs fb40f0
+	u_char *sigblob = NULL;
Norbert Pocs fb40f0
+	size_t len, slen;
Norbert Pocs fb40f0
 	int ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL;
Norbert Pocs fb40f0
 	char *ktype = NULL;
Norbert Pocs fb40f0
+	u_char *sigb = NULL, *psig = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if (key == NULL || key->dsa == NULL ||
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_DSA ||
Norbert Pocs fb40f0
 	    sig == NULL || siglen == 0)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-	if (hlen == 0)
Norbert Pocs fb40f0
-		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	/* fetch signature */
Norbert Pocs fb40f0
 	if ((b = sshbuf_from(sig, siglen)) == NULL)
Norbert Pocs fb40f0
@@ -386,25 +398,28 @@
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
 	sig_r = sig_s = NULL; /* transferred */
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	/* sha1 the data */
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(SSH_DIGEST_SHA1, data, dlen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
+	if ((slen = i2d_DSA_SIG(dsig, NULL)) == 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-	switch (DSA_do_verify(digest, hlen, dsig, key->dsa)) {
Norbert Pocs fb40f0
-	case 1:
Norbert Pocs fb40f0
-		ret = 0;
Norbert Pocs fb40f0
-		break;
Norbert Pocs fb40f0
-	case 0:
Norbert Pocs fb40f0
-		ret = SSH_ERR_SIGNATURE_INVALID;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	if ((sigb = malloc(slen)) == NULL) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
-	default:
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	psig = sigb;
Norbert Pocs fb40f0
+	if ((slen = i2d_DSA_SIG(dsig, &psig)) == 0) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+  	if ((ret = ssh_create_evp_dss(key, &pkey)) != 0)
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	ret = sshkey_verify_signature(pkey, SSH_DIGEST_SHA1, data, dlen,
Norbert Pocs fb40f0
+	    sigb, slen);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	explicit_bzero(digest, sizeof(digest));
Norbert Pocs fb40f0
+	free(sigb);
Norbert Pocs fb40f0
 	DSA_SIG_free(dsig);
Norbert Pocs fb40f0
 	BN_clear_free(sig_r);
Norbert Pocs fb40f0
 	BN_clear_free(sig_s);
Norbert Pocs fb40f0
@@ -415,6 +430,65 @@
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+ssh_create_evp_dss(const struct sshkey *k, EVP_PKEY **pkey)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+  	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs fb40f0
+  	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs fb40f0
+  	const BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
Norbert Pocs fb40f0
+  	int ret = 0;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if (k == NULL)
Norbert Pocs fb40f0
+  		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) == NULL ||
Norbert Pocs fb40f0
+  	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+  	  	goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	DSA_get0_pqg(k->dsa, &p, &q, &g);
Norbert Pocs fb40f0
+  	DSA_get0_key(k->dsa, &pub, &priv;;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if (p != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_P, p) != 1) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (q != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_Q, q) != 1) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (g != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_G, g) != 1) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (pub != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs fb40f0
+	        OSSL_PKEY_PARAM_PUB_KEY,
Norbert Pocs fb40f0
+	        pub) != 1) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (priv != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs fb40f0
+	        OSSL_PKEY_PARAM_PRIV_KEY,
Norbert Pocs fb40f0
+	        priv) != 1) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs fb40f0
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+out:
Norbert Pocs fb40f0
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs fb40f0
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+  	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 static const struct sshkey_impl_funcs sshkey_dss_funcs = {
Norbert Pocs fb40f0
 	/* .size = */		ssh_dss_size,
Norbert Pocs fb40f0
 	/* .alloc = */		ssh_dss_alloc,
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-ecdsa.c openssh-9.3p1-patched/ssh-ecdsa.c
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-ecdsa.c	2023-03-15 22:28:19.000000000 +0100
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-ecdsa.c	2023-06-06 15:52:25.626551768 +0200
Norbert Pocs fb40f0
@@ -34,6 +34,8 @@
Norbert Pocs fb40f0
 #include <openssl/ec.h>
Norbert Pocs fb40f0
 #include <openssl/ecdsa.h>
Norbert Pocs fb40f0
 #include <openssl/evp.h>
Norbert Pocs fb40f0
+#include <openssl/core_names.h>
Norbert Pocs fb40f0
+#include <openssl/param_build.h>
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include <string.h>
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
@@ -126,19 +128,29 @@
Norbert Pocs fb40f0
 static int
Norbert Pocs fb40f0
 ssh_ecdsa_generate(struct sshkey *k, int bits)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
-	EC_KEY *private;
Norbert Pocs fb40f0
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs fb40f0
+	EVP_PKEY *res = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if ((k->ecdsa_nid = sshkey_ecdsa_bits_to_nid(bits)) == -1)
Norbert Pocs fb40f0
 		return SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
-	if ((private = EC_KEY_new_by_curve_name(k->ecdsa_nid)) == NULL)
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL)
Norbert Pocs fb40f0
 		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
-	if (EC_KEY_generate_key(private) != 1) {
Norbert Pocs fb40f0
-		EC_KEY_free(private);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (EVP_PKEY_keygen_init(ctx) <= 0 || EVP_PKEY_CTX_set_group_name(ctx, OBJ_nid2sn(k->ecdsa_nid)) <= 0
Norbert Pocs fb40f0
+	   || EVP_PKEY_keygen(ctx, &res) <= 0) {
Norbert Pocs fb40f0
+		EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+		EVP_PKEY_free(res);
Norbert Pocs fb40f0
 		return SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	EC_KEY_set_asn1_flag(private, OPENSSL_EC_NAMED_CURVE);
Norbert Pocs fb40f0
-	k->ecdsa = private;
Norbert Pocs fb40f0
-	return 0;
Norbert Pocs fb40f0
+	/* This function is deprecated in OpenSSL 3.0 but OpenSSH doesn't worry about it*/
Norbert Pocs fb40f0
+	k->ecdsa = EVP_PKEY_get1_EC_KEY(res);
Norbert Pocs fb40f0
+	if (k->ecdsa)
Norbert Pocs fb40f0
+		EC_KEY_set_asn1_flag(k->ecdsa, OPENSSL_EC_NAMED_CURVE);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+	EVP_PKEY_free(res);
Norbert Pocs fb40f0
+ 	return (k->ecdsa) ? 0 : SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 static int
Norbert Pocs fb40f0
@@ -228,11 +240,13 @@
Norbert Pocs fb40f0
     const u_char *data, size_t dlen,
Norbert Pocs fb40f0
     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
 	ECDSA_SIG *esig = NULL;
Norbert Pocs fb40f0
+	unsigned char *sigb = NULL;
Norbert Pocs fb40f0
+	const unsigned char *psig;
Norbert Pocs fb40f0
 	const BIGNUM *sig_r, *sig_s;
Norbert Pocs fb40f0
 	int hash_alg;
Norbert Pocs fb40f0
-	u_char digest[SSH_DIGEST_MAX_LENGTH];
Norbert Pocs fb40f0
-	size_t len, hlen;
Norbert Pocs fb40f0
+	int len;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL, *bb = NULL;
Norbert Pocs fb40f0
 	int ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
@@ -245,18 +259,33 @@
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_ECDSA)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
Norbert Pocs fb40f0
-	    (hlen = ssh_digest_bytes(hash_alg)) == 0)
Norbert Pocs fb40f0
+	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
Norbert Pocs fb40f0
 		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(hash_alg, data, dlen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+#ifdef ENABLE_PKCS11
Norbert Pocs fb40f0
+	if (is_ecdsa_pkcs11(key->ecdsa)) {
Norbert Pocs fb40f0
+		if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs fb40f0
+		    EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1)
Norbert Pocs fb40f0
+			return SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+	} else {
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+		if ((ret = ssh_create_evp_ec(key->ecdsa, key->ecdsa_nid, &pkey)) != 0)
Norbert Pocs fb40f0
+			return ret;
Norbert Pocs fb40f0
+#ifdef ENABLE_PKCS11
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+	ret = sshkey_calculate_signature(pkey, hash_alg, &sigb, &len, data,
Norbert Pocs fb40f0
+	    dlen);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+	if (ret < 0) {
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((esig = ECDSA_do_sign(digest, hlen, key->ecdsa)) == NULL) {
Norbert Pocs fb40f0
+	psig = sigb;
Norbert Pocs fb40f0
+	if (d2i_ECDSA_SIG(&esig, &psig, len) == NULL) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
 	if ((bb = sshbuf_new()) == NULL || (b = sshbuf_new()) == NULL) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
@@ -280,7 +309,7 @@
Norbert Pocs fb40f0
 		*lenp = len;
Norbert Pocs fb40f0
 	ret = 0;
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	explicit_bzero(digest, sizeof(digest));
Norbert Pocs fb40f0
+	free(sigb);
Norbert Pocs fb40f0
 	sshbuf_free(b);
Norbert Pocs fb40f0
 	sshbuf_free(bb);
Norbert Pocs fb40f0
 	ECDSA_SIG_free(esig);
Norbert Pocs fb40f0
@@ -293,22 +322,21 @@
Norbert Pocs fb40f0
     const u_char *data, size_t dlen, const char *alg, u_int compat,
Norbert Pocs fb40f0
     struct sshkey_sig_details **detailsp)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
 	ECDSA_SIG *esig = NULL;
Norbert Pocs fb40f0
 	BIGNUM *sig_r = NULL, *sig_s = NULL;
Norbert Pocs fb40f0
-	int hash_alg;
Norbert Pocs fb40f0
-	u_char digest[SSH_DIGEST_MAX_LENGTH];
Norbert Pocs fb40f0
-	size_t hlen;
Norbert Pocs fb40f0
+	int hash_alg, len;
Norbert Pocs fb40f0
 	int ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL, *sigbuf = NULL;
Norbert Pocs fb40f0
 	char *ktype = NULL;
Norbert Pocs fb40f0
+	unsigned char *sigb = NULL, *psig = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if (key == NULL || key->ecdsa == NULL ||
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_ECDSA ||
Norbert Pocs fb40f0
 	    sig == NULL || siglen == 0)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1 ||
Norbert Pocs fb40f0
-	    (hlen = ssh_digest_bytes(hash_alg)) == 0)
Norbert Pocs fb40f0
+	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
Norbert Pocs fb40f0
 		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	/* fetch signature */
Norbert Pocs fb40f0
@@ -344,28 +372,33 @@
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
 	sig_r = sig_s = NULL; /* transferred */
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if (sshbuf_len(sigbuf) != 0) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
Norbert Pocs fb40f0
+	/* Figure out the length */
Norbert Pocs fb40f0
+	if ((len = i2d_ECDSA_SIG(esig, NULL)) == 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(hash_alg, data, dlen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
-		goto out;
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-	switch (ECDSA_do_verify(digest, hlen, esig, key->ecdsa)) {
Norbert Pocs fb40f0
-	case 1:
Norbert Pocs fb40f0
-		ret = 0;
Norbert Pocs fb40f0
-		break;
Norbert Pocs fb40f0
-	case 0:
Norbert Pocs fb40f0
-		ret = SSH_ERR_SIGNATURE_INVALID;
Norbert Pocs fb40f0
+	if ((sigb = malloc(len)) == NULL) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
-	default:
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	psig = sigb;
Norbert Pocs fb40f0
+	if ((len = i2d_ECDSA_SIG(esig, &psig)) == 0) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+	if (sshbuf_len(sigbuf) != 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_UNEXPECTED_TRAILING_DATA;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if (ssh_create_evp_ec(key->ecdsa, key->ecdsa_nid, &pkey) != 0)
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	ret = sshkey_verify_signature(pkey, hash_alg, data, dlen, sigb, len);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	explicit_bzero(digest, sizeof(digest));
Norbert Pocs fb40f0
+	free(sigb);
Norbert Pocs fb40f0
 	sshbuf_free(sigbuf);
Norbert Pocs fb40f0
 	sshbuf_free(b);
Norbert Pocs fb40f0
 	ECDSA_SIG_free(esig);
Norbert Pocs fb40f0
@@ -375,6 +408,79 @@
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+ssh_create_evp_ec(EC_KEY *k, int ecdsa_nid, EVP_PKEY **pkey)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs fb40f0
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs fb40f0
+  	BN_CTX *bn_ctx = NULL;
Norbert Pocs fb40f0
+  	uint8_t *pub_ser = NULL; 
Norbert Pocs fb40f0
+  	const char *group_name;
Norbert Pocs fb40f0
+  	const EC_POINT *pub = NULL;
Norbert Pocs fb40f0
+  	const BIGNUM *priv = NULL;
Norbert Pocs fb40f0
+  	int ret = 0;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (k == NULL)
Norbert Pocs fb40f0
+    		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL ||
Norbert Pocs fb40f0
+      	    (param_bld = OSSL_PARAM_BLD_new()) == NULL ||
Norbert Pocs fb40f0
+      	    (bn_ctx = BN_CTX_new()) == NULL) {
Norbert Pocs fb40f0
+    		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+    		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if ((group_name = OSSL_EC_curve_nid2name(ecdsa_nid)) == NULL ||
Norbert Pocs fb40f0
+     	    OSSL_PARAM_BLD_push_utf8_string(param_bld,
Norbert Pocs fb40f0
+                OSSL_PKEY_PARAM_GROUP_NAME,
Norbert Pocs fb40f0
+                group_name,
Norbert Pocs fb40f0
+                strlen(group_name)) != 1) {
Norbert Pocs fb40f0
+    		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+    		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+  	if ((pub = EC_KEY_get0_public_key(k)) != NULL) {
Norbert Pocs fb40f0
+    		const EC_GROUP *group;
Norbert Pocs fb40f0
+    		size_t len;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+		group = EC_KEY_get0_group(k);
Norbert Pocs fb40f0
+		len = EC_POINT_point2oct(group, pub,
Norbert Pocs fb40f0
+		    POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Norbert Pocs fb40f0
+		if ((pub_ser = malloc(len)) == NULL) {
Norbert Pocs fb40f0
+			ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+			goto out;
Norbert Pocs fb40f0
+		}
Norbert Pocs fb40f0
+		EC_POINT_point2oct(group,
Norbert Pocs fb40f0
+		    pub,
Norbert Pocs fb40f0
+		    POINT_CONVERSION_UNCOMPRESSED,
Norbert Pocs fb40f0
+		    pub_ser,
Norbert Pocs fb40f0
+		    len,
Norbert Pocs fb40f0
+		    bn_ctx);
Norbert Pocs fb40f0
+		if (OSSL_PARAM_BLD_push_octet_string(param_bld,
Norbert Pocs fb40f0
+		    OSSL_PKEY_PARAM_PUB_KEY,
Norbert Pocs fb40f0
+		    pub_ser,
Norbert Pocs fb40f0
+		    len) != 1) {
Norbert Pocs fb40f0
+			ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+			goto out;
Norbert Pocs fb40f0
+		}
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+  	if ((priv = EC_KEY_get0_private_key(k)) != NULL &&
Norbert Pocs fb40f0
+	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs fb40f0
+               OSSL_PKEY_PARAM_PRIV_KEY, priv) != 1) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs fb40f0
+    		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+    		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+out:
Norbert Pocs fb40f0
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs fb40f0
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+  	BN_CTX_free(bn_ctx);
Norbert Pocs fb40f0
+  	free(pub_ser);
Norbert Pocs fb40f0
+  	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 /* NB. not static; used by ECDSA-SK */
Norbert Pocs fb40f0
 const struct sshkey_impl_funcs sshkey_ecdsa_funcs = {
Norbert Pocs fb40f0
 	/* .size = */		ssh_ecdsa_size,
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/sshkey.c openssh-9.3p1-patched/sshkey.c
Norbert Pocs fb40f0
--- openssh-9.3p1/sshkey.c	2023-06-06 15:53:36.608444190 +0200
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/sshkey.c	2023-06-06 15:52:25.625551756 +0200
Norbert Pocs fb40f0
@@ -34,6 +34,8 @@
Norbert Pocs fb40f0
 #include <openssl/evp.h>
Norbert Pocs fb40f0
 #include <openssl/err.h>
Norbert Pocs fb40f0
 #include <openssl/pem.h>
Norbert Pocs fb40f0
+#include <openssl/core_names.h>
Norbert Pocs fb40f0
+#include <openssl/param_build.h>
Norbert Pocs fb40f0
 #endif
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include "crypto_api.h"
Norbert Pocs fb40f0
@@ -575,6 +577,86 @@
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #ifdef WITH_OPENSSL
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+sshkey_calculate_signature(EVP_PKEY *pkey, int hash_alg, u_char **sigp,
Norbert Pocs fb40f0
+    int *lenp, const u_char *data, size_t datalen)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	EVP_MD_CTX *ctx = NULL;
Norbert Pocs fb40f0
+	u_char *sig = NULL;
Norbert Pocs fb40f0
+	int ret, slen;
Norbert Pocs fb40f0
+	size_t len;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (sigp == NULL || lenp == NULL) {
Norbert Pocs fb40f0
+		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	slen = EVP_PKEY_get_size(pkey);
Norbert Pocs fb40f0
+	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
Norbert Pocs fb40f0
+		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	len = slen;
Norbert Pocs fb40f0
+	if ((sig = malloc(slen)) == NULL) {
Norbert Pocs fb40f0
+		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if ((ctx = EVP_MD_CTX_new()) == NULL) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+		goto error;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	if (EVP_DigestSignInit(ctx, NULL, ssh_digest_to_md(hash_alg),
Norbert Pocs fb40f0
+	        NULL, pkey) != 1 ||
Norbert Pocs fb40f0
+	    EVP_DigestSignUpdate(ctx, data, datalen) != 1 ||
Norbert Pocs fb40f0
+	    EVP_DigestSignFinal(ctx, sig, &len) != 1) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto error;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	*sigp = sig;
Norbert Pocs fb40f0
+	*lenp = len;
Norbert Pocs fb40f0
+	/* Now owned by the caller */
Norbert Pocs fb40f0
+	sig = NULL;
Norbert Pocs fb40f0
+	ret = 0;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+error:
Norbert Pocs fb40f0
+	EVP_MD_CTX_free(ctx);
Norbert Pocs fb40f0
+	free(sig);
Norbert Pocs fb40f0
+	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+sshkey_verify_signature(EVP_PKEY *pkey, int hash_alg, const u_char *data,
Norbert Pocs fb40f0
+    size_t datalen, u_char *sigbuf, int siglen)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	EVP_MD_CTX *ctx = NULL;
Norbert Pocs fb40f0
+	int ret;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if ((ctx = EVP_MD_CTX_new()) == NULL) {
Norbert Pocs fb40f0
+		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	if (EVP_DigestVerifyInit(ctx, NULL, ssh_digest_to_md(hash_alg),
Norbert Pocs fb40f0
+	    NULL, pkey) != 1 ||
Norbert Pocs fb40f0
+	    EVP_DigestVerifyUpdate(ctx, data, datalen) != 1) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto done;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+	ret = EVP_DigestVerifyFinal(ctx, sigbuf, siglen);
Norbert Pocs fb40f0
+	switch (ret) {
Norbert Pocs fb40f0
+	case 1:
Norbert Pocs fb40f0
+		ret = 0;
Norbert Pocs fb40f0
+		break;
Norbert Pocs fb40f0
+	case 0:
Norbert Pocs fb40f0
+		ret = SSH_ERR_SIGNATURE_INVALID;
Norbert Pocs fb40f0
+		break;
Norbert Pocs fb40f0
+	default:
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		break;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+done:
Norbert Pocs fb40f0
+	EVP_MD_CTX_free(ctx);
Norbert Pocs fb40f0
+	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 /* XXX: these are really begging for a table-driven approach */
Norbert Pocs fb40f0
 int
Norbert Pocs fb40f0
 sshkey_curve_name_to_nid(const char *name)
Norbert Pocs fb40f0
@@ -3763,3 +3845,27 @@
Norbert Pocs fb40f0
 	return 0;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 #endif /* WITH_XMSS */
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+#ifdef WITH_OPENSSL
Norbert Pocs fb40f0
+EVP_PKEY *
Norbert Pocs fb40f0
+sshkey_create_evp(OSSL_PARAM_BLD *param_bld, EVP_PKEY_CTX *ctx)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+  	EVP_PKEY *ret = NULL;
Norbert Pocs fb40f0
+  	OSSL_PARAM *params = NULL;
Norbert Pocs fb40f0
+  	if (param_bld == NULL || ctx == NULL) {
Norbert Pocs fb40f0
+  		debug2_f("param_bld or ctx is NULL");
Norbert Pocs fb40f0
+  		return NULL;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if ((params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
Norbert Pocs fb40f0
+  		debug2_f("Could not build param list");
Norbert Pocs fb40f0
+  		return NULL;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (EVP_PKEY_fromdata_init(ctx) != 1 ||
Norbert Pocs fb40f0
+  	    EVP_PKEY_fromdata(ctx, &ret, EVP_PKEY_KEYPAIR, params) != 1) {
Norbert Pocs fb40f0
+  		debug2_f("EVP_PKEY_fromdata failed");
Norbert Pocs fb40f0
+  		OSSL_PARAM_free(params);
Norbert Pocs fb40f0
+  		return NULL;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+#endif /* WITH_OPENSSL */
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/sshkey.h openssh-9.3p1-patched/sshkey.h
Norbert Pocs fb40f0
--- openssh-9.3p1/sshkey.h	2023-06-06 15:53:36.608444190 +0200
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/sshkey.h	2023-06-06 15:52:25.626551768 +0200
Norbert Pocs fb40f0
@@ -31,6 +31,9 @@
Norbert Pocs fb40f0
 #ifdef WITH_OPENSSL
Norbert Pocs fb40f0
 #include <openssl/rsa.h>
Norbert Pocs fb40f0
 #include <openssl/dsa.h>
Norbert Pocs fb40f0
+#include <openssl/evp.h>
Norbert Pocs fb40f0
+#include <openssl/param_build.h>
Norbert Pocs fb40f0
+#include <openssl/core_names.h>
Norbert Pocs fb40f0
 # ifdef OPENSSL_HAS_ECC
Norbert Pocs fb40f0
 #  include <openssl/ec.h>
Norbert Pocs fb40f0
 #  include <openssl/ecdsa.h>
Norbert Pocs fb40f0
@@ -268,6 +271,10 @@
Norbert Pocs fb40f0
 const char	*sshkey_ssh_name_plain(const struct sshkey *);
Norbert Pocs fb40f0
 int		 sshkey_names_valid2(const char *, int);
Norbert Pocs fb40f0
 char		*sshkey_alg_list(int, int, int, char);
Norbert Pocs fb40f0
+int		 sshkey_calculate_signature(EVP_PKEY*, int, u_char **,
Norbert Pocs fb40f0
+    int *, const u_char *, size_t);
Norbert Pocs fb40f0
+int		 sshkey_verify_signature(EVP_PKEY *, int, const u_char *,
Norbert Pocs fb40f0
+    size_t, u_char *, int);
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 int	 sshkey_from_blob(const u_char *, size_t, struct sshkey **);
Norbert Pocs fb40f0
 int	 sshkey_fromb(struct sshbuf *, struct sshkey **);
Norbert Pocs fb40f0
@@ -324,6 +331,13 @@
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 void	 sshkey_sig_details_free(struct sshkey_sig_details *);
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+#ifdef WITH_OPENSSL
Norbert Pocs fb40f0
+EVP_PKEY  *sshkey_create_evp(OSSL_PARAM_BLD *, EVP_PKEY_CTX *);
Norbert Pocs fb40f0
+int   ssh_create_evp_dss(const struct sshkey *, EVP_PKEY **);
Norbert Pocs fb40f0
+int   ssh_create_evp_rsa(const struct sshkey *, EVP_PKEY **);
Norbert Pocs fb40f0
+int   ssh_create_evp_ec(EC_KEY *, int, EVP_PKEY **);
Norbert Pocs fb40f0
+#endif /* WITH_OPENSSL */
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 #ifdef SSHKEY_INTERNAL
Norbert Pocs fb40f0
 int	sshkey_sk_fields_equal(const struct sshkey *a, const struct sshkey *b);
Norbert Pocs fb40f0
 void	sshkey_sk_cleanup(struct sshkey *k);
Norbert Pocs fb40f0
@@ -338,6 +352,10 @@
Norbert Pocs fb40f0
 #endif
Norbert Pocs fb40f0
 #endif
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+#ifdef ENABLE_PKCS11
Norbert Pocs fb40f0
+int pkcs11_get_ecdsa_idx(void);
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 #if !defined(WITH_OPENSSL)
Norbert Pocs fb40f0
 # undef RSA
Norbert Pocs fb40f0
 # undef DSA
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-pkcs11.c openssh-9.3p1-patched/ssh-pkcs11.c
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-pkcs11.c	2023-06-06 15:53:36.592443989 +0200
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-pkcs11.c	2023-06-06 15:52:25.626551768 +0200
Norbert Pocs fb40f0
@@ -777,8 +777,24 @@
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	return (0);
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+is_ecdsa_pkcs11(EC_KEY *ecdsa)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	if (EC_KEY_get_ex_data(ecdsa, ec_key_idx) != NULL)
Norbert Pocs fb40f0
+		return 1;
Norbert Pocs fb40f0
+	return 0;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
 #endif /* OPENSSL_HAS_ECC && HAVE_EC_KEY_METHOD_NEW */
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+is_rsa_pkcs11(RSA *rsa)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	if (RSA_get_ex_data(rsa, rsa_idx) != NULL)
Norbert Pocs fb40f0
+		return 1;
Norbert Pocs fb40f0
+	return 0;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Jakub Jelen d77b1b
 /* remove trailing spaces. Note, that this does NOT guarantee the buffer
Jakub Jelen d77b1b
  * will be null terminated if there are no trailing spaces! */
Norbert Pocs fb40f0
 static void
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-pkcs11-client.c openssh-9.3p1-patched/ssh-pkcs11-client.c
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-pkcs11-client.c	2023-06-06 15:53:36.591443976 +0200
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-pkcs11-client.c	2023-06-06 15:52:25.626551768 +0200
Norbert Pocs fb40f0
@@ -225,8 +225,36 @@
Norbert Pocs fb40f0
 static RSA_METHOD	*helper_rsa;
Norbert Pocs fb40f0
 #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
Norbert Pocs fb40f0
 static EC_KEY_METHOD	*helper_ecdsa;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+is_ecdsa_pkcs11(EC_KEY *ecdsa)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	const EC_KEY_METHOD *meth;
Norbert Pocs fb40f0
+	ECDSA_SIG *(*sign_sig)(const unsigned char *dgst, int dgstlen,
Norbert Pocs fb40f0
+		const BIGNUM *kinv, const BIGNUM *rp, EC_KEY *eckey) = NULL;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	meth = EC_KEY_get_method(ecdsa);
Norbert Pocs fb40f0
+	EC_KEY_METHOD_get_sign(meth, NULL, NULL, &sign_sig);
Norbert Pocs fb40f0
+	if (sign_sig == ecdsa_do_sign)
Norbert Pocs fb40f0
+		return 1;
Norbert Pocs fb40f0
+	return 0;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
 #endif /* OPENSSL_HAS_ECC && HAVE_EC_KEY_METHOD_NEW */
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+is_rsa_pkcs11(RSA *rsa)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+	const RSA_METHOD *meth;
Norbert Pocs fb40f0
+	int (*priv_enc)(int flen, const unsigned char *from,
Norbert Pocs fb40f0
+        	unsigned char *to, RSA *rsa, int padding) = NULL;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	meth = RSA_get_method(rsa);
Norbert Pocs fb40f0
+	priv_enc = RSA_meth_get_priv_enc(meth);
Norbert Pocs fb40f0
+	if (priv_enc == rsa_encrypt)
Norbert Pocs fb40f0
+		return 1;
Norbert Pocs fb40f0
+	return 0;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 /* redirect private key crypto operations to the ssh-pkcs11-helper */
Norbert Pocs fb40f0
 static void
Norbert Pocs fb40f0
 wrap_key(struct sshkey *k)
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-pkcs11.h openssh-9.3p1-patched/ssh-pkcs11.h
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-pkcs11.h	2023-06-06 15:53:36.592443989 +0200
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-pkcs11.h	2023-06-06 15:52:25.626551768 +0200
Norbert Pocs fb40f0
@@ -39,6 +39,11 @@
Norbert Pocs fb40f0
 	    u_int32_t *);
Norbert Pocs fb40f0
 #endif
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+#ifdef HAVE_EC_KEY_METHOD_NEW
Norbert Pocs fb40f0
+int is_ecdsa_pkcs11(EC_KEY *ecdsa);
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+int is_rsa_pkcs11(RSA *rsa);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 #if !defined(WITH_OPENSSL) && defined(ENABLE_PKCS11)
Norbert Pocs fb40f0
 #undef ENABLE_PKCS11
Norbert Pocs fb40f0
 #endif
Norbert Pocs fb40f0
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac openssh-9.3p1/ssh-rsa.c openssh-9.3p1-patched/ssh-rsa.c
Norbert Pocs fb40f0
--- openssh-9.3p1/ssh-rsa.c	2023-03-15 22:28:19.000000000 +0100
Norbert Pocs fb40f0
+++ openssh-9.3p1-patched/ssh-rsa.c	2023-06-06 15:52:25.627551781 +0200
Norbert Pocs fb40f0
@@ -23,6 +23,8 @@
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include <openssl/evp.h>
Norbert Pocs fb40f0
 #include <openssl/err.h>
Norbert Pocs fb40f0
+#include <openssl/core_names.h>
Norbert Pocs fb40f0
+#include <openssl/param_build.h>
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include <stdarg.h>
Norbert Pocs fb40f0
 #include <string.h>
Norbert Pocs fb40f0
@@ -36,7 +38,7 @@
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 #include "openbsd-compat/openssl-compat.h"
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-static int openssh_RSA_verify(int, u_char *, size_t, u_char *, size_t, RSA *);
Norbert Pocs fb40f0
+static int openssh_RSA_verify(int, const u_char *, size_t, u_char *, size_t, EVP_PKEY *);
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 static u_int
Norbert Pocs fb40f0
 ssh_rsa_size(const struct sshkey *key)
Norbert Pocs fb40f0
@@ -131,27 +133,50 @@
Norbert Pocs fb40f0
 static int
Norbert Pocs fb40f0
 ssh_rsa_generate(struct sshkey *k, int bits)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
-	RSA *private = NULL;
Norbert Pocs fb40f0
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs fb40f0
+	EVP_PKEY *res = NULL;
Norbert Pocs fb40f0
 	BIGNUM *f4 = NULL;
Norbert Pocs fb40f0
 	int ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if (bits < SSH_RSA_MINIMUM_MODULUS_SIZE ||
Norbert Pocs fb40f0
 	    bits > SSHBUF_MAX_BIGNUM * 8)
Norbert Pocs fb40f0
 		return SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
-	if ((private = RSA_new()) == NULL || (f4 = BN_new()) == NULL) {
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL
Norbert Pocs fb40f0
+		|| (f4 = BN_new()) == NULL || !BN_set_word(f4, RSA_F4)) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	if (!BN_set_word(f4, RSA_F4) ||
Norbert Pocs fb40f0
-	    !RSA_generate_key_ex(private, bits, f4, NULL)) {
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (EVP_PKEY_keygen_init(ctx) <= 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits) <= 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (EVP_PKEY_CTX_set1_rsa_keygen_pubexp(ctx, f4) <= 0)
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	if (EVP_PKEY_keygen(ctx, &res) <= 0) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	/* This function is deprecated in OpenSSL 3.0 but OpenSSH doesn't worry about it*/
Norbert Pocs fb40f0
+	k->rsa = EVP_PKEY_get1_RSA(res);
Norbert Pocs fb40f0
+	if (k->rsa) {
Norbert Pocs fb40f0
+		ret = 0;
Norbert Pocs fb40f0
+	} else {
Norbert Pocs fb40f0
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	k->rsa = private;
Norbert Pocs fb40f0
-	private = NULL;
Norbert Pocs fb40f0
-	ret = 0;
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	RSA_free(private);
Norbert Pocs fb40f0
+	EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+	EVP_PKEY_free(res);
Norbert Pocs fb40f0
 	BN_free(f4);
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
@@ -317,21 +342,6 @@
Norbert Pocs fb40f0
 	return -1;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-static int
Norbert Pocs fb40f0
-rsa_hash_alg_nid(int type)
Norbert Pocs fb40f0
-{
Norbert Pocs fb40f0
-	switch (type) {
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA1:
Norbert Pocs fb40f0
-		return NID_sha1;
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA256:
Norbert Pocs fb40f0
-		return NID_sha256;
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA512:
Norbert Pocs fb40f0
-		return NID_sha512;
Norbert Pocs fb40f0
-	default:
Norbert Pocs fb40f0
-		return -1;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-}
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
 int
Norbert Pocs fb40f0
 ssh_rsa_complete_crt_parameters(struct sshkey *key, const BIGNUM *iqmp)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
@@ -393,11 +403,10 @@
Norbert Pocs fb40f0
     const u_char *data, size_t datalen,
Norbert Pocs fb40f0
     const char *alg, const char *sk_provider, const char *sk_pin, u_int compat)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
-	const BIGNUM *rsa_n;
Norbert Pocs fb40f0
-	u_char digest[SSH_DIGEST_MAX_LENGTH], *sig = NULL;
Norbert Pocs fb40f0
-	size_t slen = 0;
Norbert Pocs fb40f0
-	u_int hlen, len;
Norbert Pocs fb40f0
-	int nid, hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
+	u_char *sig = NULL;
Norbert Pocs fb40f0
+	int len, slen = 0;
Norbert Pocs fb40f0
+	int hash_alg, ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if (lenp != NULL)
Norbert Pocs fb40f0
@@ -409,33 +418,33 @@
Norbert Pocs fb40f0
 		hash_alg = SSH_DIGEST_SHA1;
Norbert Pocs fb40f0
 	else
Norbert Pocs fb40f0
 		hash_alg = rsa_hash_id_from_keyname(alg);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 	if (key == NULL || key->rsa == NULL || hash_alg == -1 ||
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_RSA)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
Norbert Pocs fb40f0
-	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Norbert Pocs fb40f0
-		return SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
 	slen = RSA_size(key->rsa);
Norbert Pocs fb40f0
-	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
Norbert Pocs fb40f0
-		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-	/* hash the data */
Norbert Pocs fb40f0
-	nid = rsa_hash_alg_nid(hash_alg);
Norbert Pocs fb40f0
-	if ((hlen = ssh_digest_bytes(hash_alg)) == 0)
Norbert Pocs fb40f0
-		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(hash_alg, data, datalen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
-		goto out;
Norbert Pocs fb40f0
+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Norbert Pocs fb40f0
+		return SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	if ((sig = malloc(slen)) == NULL) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
-		goto out;
Norbert Pocs fb40f0
+#ifdef ENABLE_PKCS11
Norbert Pocs fb40f0
+	if (is_rsa_pkcs11(key->rsa)) {
Norbert Pocs fb40f0
+		if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs fb40f0
+		    EVP_PKEY_set1_RSA(pkey, key->rsa) != 1)
Norbert Pocs fb40f0
+			return SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+	} else {
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+		if ((ret = ssh_create_evp_rsa(key, &pkey)) != 0)
Norbert Pocs fb40f0
+			return ret;
Norbert Pocs fb40f0
+#ifdef ENABLE_PKCS11
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-	if (RSA_sign(nid, digest, hlen, sig, &len, key->rsa) != 1) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+#endif
Norbert Pocs fb40f0
+	ret = sshkey_calculate_signature(pkey, hash_alg, &sig, &len, data,
Norbert Pocs fb40f0
+	    datalen);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+	if (ret < 0) {
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 	if (len < slen) {
Norbert Pocs fb40f0
 		size_t diff = slen - len;
Norbert Pocs fb40f0
 		memmove(sig + diff, sig, len);
Norbert Pocs fb40f0
@@ -444,6 +453,7 @@
Norbert Pocs fb40f0
 		ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 	/* encode signature */
Norbert Pocs fb40f0
 	if ((b = sshbuf_new()) == NULL) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
@@ -464,7 +474,6 @@
Norbert Pocs fb40f0
 		*lenp = len;
Norbert Pocs fb40f0
 	ret = 0;
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
-	explicit_bzero(digest, sizeof(digest));
Norbert Pocs fb40f0
 	freezero(sig, slen);
Norbert Pocs fb40f0
 	sshbuf_free(b);
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
@@ -476,10 +485,10 @@
Norbert Pocs fb40f0
     const u_char *data, size_t dlen, const char *alg, u_int compat,
Norbert Pocs fb40f0
     struct sshkey_sig_details **detailsp)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
-	const BIGNUM *rsa_n;
Norbert Pocs fb40f0
+	EVP_PKEY *pkey = NULL;
Norbert Pocs fb40f0
 	char *sigtype = NULL;
Norbert Pocs fb40f0
 	int hash_alg, want_alg, ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
-	size_t len = 0, diff, modlen, hlen;
Norbert Pocs fb40f0
+	size_t len = 0, diff, modlen;
Norbert Pocs fb40f0
 	struct sshbuf *b = NULL;
Norbert Pocs fb40f0
 	u_char digest[SSH_DIGEST_MAX_LENGTH], *osigblob, *sigblob = NULL;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
@@ -487,8 +496,7 @@
Norbert Pocs fb40f0
 	    sshkey_type_plain(key->type) != KEY_RSA ||
Norbert Pocs fb40f0
 	    sig == NULL || siglen == 0)
Norbert Pocs fb40f0
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-	RSA_get0_key(key->rsa, &rsa_n, NULL, NULL);
Norbert Pocs fb40f0
-	if (BN_num_bits(rsa_n) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Norbert Pocs fb40f0
+	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Norbert Pocs fb40f0
 		return SSH_ERR_KEY_LENGTH;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
 	if ((b = sshbuf_from(sig, siglen)) == NULL)
Norbert Pocs fb40f0
@@ -540,16 +548,13 @@
Norbert Pocs fb40f0
 		explicit_bzero(sigblob, diff);
Norbert Pocs fb40f0
 		len = modlen;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	if ((hlen = ssh_digest_bytes(hash_alg)) == 0) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
-		goto out;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	if ((ret = ssh_digest_memory(hash_alg, data, dlen,
Norbert Pocs fb40f0
-	    digest, sizeof(digest))) != 0)
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if ((ret = ssh_create_evp_rsa(key, &pkey)) != 0)
Norbert Pocs fb40f0
 		goto out;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-	ret = openssh_RSA_verify(hash_alg, digest, hlen, sigblob, len,
Norbert Pocs fb40f0
-	    key->rsa);
Norbert Pocs fb40f0
+	ret = openssh_RSA_verify(hash_alg, data, dlen, sigblob, len, pkey);
Norbert Pocs fb40f0
+	EVP_PKEY_free(pkey);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
  out:
Norbert Pocs fb40f0
 	freezero(sigblob, len);
Norbert Pocs fb40f0
 	free(sigtype);
Norbert Pocs fb40f0
@@ -558,125 +563,110 @@
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-/*
Norbert Pocs fb40f0
- * See:
Norbert Pocs fb40f0
- * http://www.rsasecurity.com/rsalabs/pkcs/pkcs-1/
Norbert Pocs fb40f0
- * ftp://ftp.rsasecurity.com/pub/pkcs/pkcs-1/pkcs-1v2-1.asn
Norbert Pocs fb40f0
- */
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-/*
Norbert Pocs fb40f0
- * id-sha1 OBJECT IDENTIFIER ::= { iso(1) identified-organization(3)
Norbert Pocs fb40f0
- *	oiw(14) secsig(3) algorithms(2) 26 }
Norbert Pocs fb40f0
- */
Norbert Pocs fb40f0
-static const u_char id_sha1[] = {
Norbert Pocs fb40f0
-	0x30, 0x21, /* type Sequence, length 0x21 (33) */
Norbert Pocs fb40f0
-	0x30, 0x09, /* type Sequence, length 0x09 */
Norbert Pocs fb40f0
-	0x06, 0x05, /* type OID, length 0x05 */
Norbert Pocs fb40f0
-	0x2b, 0x0e, 0x03, 0x02, 0x1a, /* id-sha1 OID */
Norbert Pocs fb40f0
-	0x05, 0x00, /* NULL */
Norbert Pocs fb40f0
-	0x04, 0x14  /* Octet string, length 0x14 (20), followed by sha1 hash */
Norbert Pocs fb40f0
-};
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-/*
Norbert Pocs fb40f0
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
Norbert Pocs fb40f0
- * id-sha256 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
Norbert Pocs fb40f0
- *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
Norbert Pocs fb40f0
- *      id-sha256(1) }
Norbert Pocs fb40f0
- */
Norbert Pocs fb40f0
-static const u_char id_sha256[] = {
Norbert Pocs fb40f0
-	0x30, 0x31, /* type Sequence, length 0x31 (49) */
Norbert Pocs fb40f0
-	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
Norbert Pocs fb40f0
-	0x06, 0x09, /* type OID, length 0x09 */
Norbert Pocs fb40f0
-	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, /* id-sha256 */
Norbert Pocs fb40f0
-	0x05, 0x00, /* NULL */
Norbert Pocs fb40f0
-	0x04, 0x20  /* Octet string, length 0x20 (32), followed by sha256 hash */
Norbert Pocs fb40f0
-};
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-/*
Norbert Pocs fb40f0
- * See http://csrc.nist.gov/groups/ST/crypto_apps_infra/csor/algorithms.html
Norbert Pocs fb40f0
- * id-sha512 OBJECT IDENTIFIER ::= { joint-iso-itu-t(2) country(16) us(840)
Norbert Pocs fb40f0
- *      organization(1) gov(101) csor(3) nistAlgorithm(4) hashAlgs(2)
Norbert Pocs fb40f0
- *      id-sha256(3) }
Norbert Pocs fb40f0
- */
Norbert Pocs fb40f0
-static const u_char id_sha512[] = {
Norbert Pocs fb40f0
-	0x30, 0x51, /* type Sequence, length 0x51 (81) */
Norbert Pocs fb40f0
-	0x30, 0x0d, /* type Sequence, length 0x0d (13) */
Norbert Pocs fb40f0
-	0x06, 0x09, /* type OID, length 0x09 */
Norbert Pocs fb40f0
-	0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, /* id-sha512 */
Norbert Pocs fb40f0
-	0x05, 0x00, /* NULL */
Norbert Pocs fb40f0
-	0x04, 0x40  /* Octet string, length 0x40 (64), followed by sha512 hash */
Norbert Pocs fb40f0
-};
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
 static int
Norbert Pocs fb40f0
-rsa_hash_alg_oid(int hash_alg, const u_char **oidp, size_t *oidlenp)
Norbert Pocs fb40f0
+openssh_RSA_verify(int hash_alg, const u_char *data, size_t datalen,
Norbert Pocs fb40f0
+    u_char *sigbuf, size_t siglen, EVP_PKEY *pkey)
Norbert Pocs fb40f0
 {
Norbert Pocs fb40f0
-	switch (hash_alg) {
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA1:
Norbert Pocs fb40f0
-		*oidp = id_sha1;
Norbert Pocs fb40f0
-		*oidlenp = sizeof(id_sha1);
Norbert Pocs fb40f0
-		break;
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA256:
Norbert Pocs fb40f0
-		*oidp = id_sha256;
Norbert Pocs fb40f0
-		*oidlenp = sizeof(id_sha256);
Norbert Pocs fb40f0
-		break;
Norbert Pocs fb40f0
-	case SSH_DIGEST_SHA512:
Norbert Pocs fb40f0
-		*oidp = id_sha512;
Norbert Pocs fb40f0
-		*oidlenp = sizeof(id_sha512);
Norbert Pocs fb40f0
-		break;
Norbert Pocs fb40f0
-	default:
Norbert Pocs fb40f0
-		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	return 0;
Norbert Pocs fb40f0
-}
Norbert Pocs fb40f0
+	size_t rsasize = 0;
Norbert Pocs fb40f0
+	int ret;
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
-static int
Norbert Pocs fb40f0
-openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
Norbert Pocs fb40f0
-    u_char *sigbuf, size_t siglen, RSA *rsa)
Norbert Pocs fb40f0
-{
Norbert Pocs fb40f0
-	size_t rsasize = 0, oidlen = 0, hlen = 0;
Norbert Pocs fb40f0
-	int ret, len, oidmatch, hashmatch;
Norbert Pocs fb40f0
-	const u_char *oid = NULL;
Norbert Pocs fb40f0
-	u_char *decrypted = NULL;
Norbert Pocs fb40f0
-
Norbert Pocs fb40f0
-	if ((ret = rsa_hash_alg_oid(hash_alg, &oid, &oidlen)) != 0)
Norbert Pocs fb40f0
-		return ret;
Norbert Pocs fb40f0
-	ret = SSH_ERR_INTERNAL_ERROR;
Norbert Pocs fb40f0
-	hlen = ssh_digest_bytes(hash_alg);
Norbert Pocs fb40f0
-	if (hashlen != hlen) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
-		goto done;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	rsasize = RSA_size(rsa);
Norbert Pocs fb40f0
+	rsasize = EVP_PKEY_get_size(pkey);
Norbert Pocs fb40f0
 	if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
Norbert Pocs fb40f0
 	    siglen == 0 || siglen > rsasize) {
Norbert Pocs fb40f0
 		ret = SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
 		goto done;
Norbert Pocs fb40f0
 	}
Norbert Pocs fb40f0
-	if ((decrypted = malloc(rsasize)) == NULL) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
-		goto done;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
Norbert Pocs fb40f0
-	    RSA_PKCS1_PADDING)) < 0) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
-		goto done;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	if (len < 0 || (size_t)len != hlen + oidlen) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_INVALID_FORMAT;
Norbert Pocs fb40f0
-		goto done;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
Norbert Pocs fb40f0
-	hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
Norbert Pocs fb40f0
-	if (!oidmatch || !hashmatch) {
Norbert Pocs fb40f0
-		ret = SSH_ERR_SIGNATURE_INVALID;
Norbert Pocs fb40f0
-		goto done;
Norbert Pocs fb40f0
-	}
Norbert Pocs fb40f0
-	ret = 0;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+	ret = sshkey_verify_signature(pkey, hash_alg, data, datalen,
Norbert Pocs fb40f0
+	    sigbuf, siglen);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 done:
Norbert Pocs fb40f0
-	freezero(decrypted, rsasize);
Norbert Pocs fb40f0
 	return ret;
Norbert Pocs fb40f0
 }
Norbert Pocs fb40f0
 
Norbert Pocs fb40f0
+int
Norbert Pocs fb40f0
+ssh_create_evp_rsa(const struct sshkey *k, EVP_PKEY **pkey)
Norbert Pocs fb40f0
+{
Norbert Pocs fb40f0
+  	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs fb40f0
+  	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs fb40f0
+  	int ret = 0;
Norbert Pocs fb40f0
+  	const BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
Norbert Pocs fb40f0
+  	const BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if (k == NULL)
Norbert Pocs fb40f0
+  	  	return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs fb40f0
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL ||
Norbert Pocs fb40f0
+  	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs fb40f0
+  	  	ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs fb40f0
+  	  	goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	RSA_get0_key(k->rsa, &n, &e, &d);
Norbert Pocs fb40f0
+  	RSA_get0_factors(k->rsa, &p, &q);
Norbert Pocs fb40f0
+  	RSA_get0_crt_params(k->rsa, &dmp1, &dmq1, &iqmp);
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if (n != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_N, n) != 1) {
Norbert Pocs fb40f0
+  	  	ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+  		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (e != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_E, e) != 1) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (d != NULL &&
Norbert Pocs fb40f0
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_D, d) != 1) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+  	/* setting this to param_build makes the creation process fail */
Norbert Pocs fb40f0
+  	if (p != NULL &&
Norbert Pocs fb40f0
+  	    EVP_PKEY_set_bn_param(*pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, p) != 1) {
Norbert Pocs fb40f0
+		debug2_f("failed to add 'p' param");
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (q != NULL &&
Norbert Pocs fb40f0
+  	    EVP_PKEY_set_bn_param(*pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, q) != 1) {
Norbert Pocs fb40f0
+		debug2_f("failed to add 'q' param");
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (dmp1 != NULL &&
Norbert Pocs fb40f0
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs fb40f0
+  	        OSSL_PKEY_PARAM_RSA_EXPONENT1, dmp1) != 1) {
Norbert Pocs fb40f0
+		debug2_f("failed to add 'dmp1' param");
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (dmq1 != NULL &&
Norbert Pocs fb40f0
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs fb40f0
+  	        OSSL_PKEY_PARAM_RSA_EXPONENT2, dmq1) != 1) {
Norbert Pocs fb40f0
+		debug2_f("failed to add 'dmq1' param");
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+  	if (iqmp != NULL &&
Norbert Pocs fb40f0
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs fb40f0
+  	        OSSL_PKEY_PARAM_RSA_COEFFICIENT1, iqmp) != 1) {
Norbert Pocs fb40f0
+		debug2_f("failed to add 'iqmp' param");
Norbert Pocs fb40f0
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs fb40f0
+		goto out;
Norbert Pocs fb40f0
+  	}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
+out:
Norbert Pocs fb40f0
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs fb40f0
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs fb40f0
+  	return ret;
Norbert Pocs fb40f0
+}
Norbert Pocs fb40f0
+
Norbert Pocs fb40f0
 static const struct sshkey_impl_funcs sshkey_rsa_funcs = {
Norbert Pocs fb40f0
 	/* .size = */		ssh_rsa_size,
Norbert Pocs fb40f0
 	/* .alloc = */		ssh_rsa_alloc,