bmh10 / rpms / openssh

Forked from rpms/openssh 2 days ago
Clone
Norbert Pocs 587d7b
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac ../../openssh-8.7p1/ssh-dss.c ./ssh-dss.c
Norbert Pocs 587d7b
--- ../../openssh-8.7p1/ssh-dss.c	2023-03-08 15:35:14.669943335 +0100
Norbert Pocs 587d7b
+++ ./ssh-dss.c	2023-03-08 15:34:33.508578129 +0100
Norbert Pocs 587d7b
@@ -32,6 +32,8 @@
Norbert Pocs 587d7b
 #include <openssl/bn.h>
Norbert Pocs 587d7b
 #include <openssl/dsa.h>
Norbert Pocs 587d7b
 #include <openssl/evp.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include <stdarg.h>
Norbert Pocs 587d7b
 #include <string.h>
Norbert Pocs 587d7b
@@ -72,9 +74,8 @@
Norbert Pocs 587d7b
 	    sshkey_type_plain(key->type) != KEY_DSA)
Norbert Pocs 587d7b
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_DSA(pkey, key->dsa) != 1)
Norbert Pocs 587d7b
-		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if ((ret = ssh_create_evp_dss(key, &pkey)) != 0)
Norbert Pocs 587d7b
+    		return ret;
Norbert Pocs 587d7b
 	ret = sshkey_calculate_signature(pkey, SSH_DIGEST_SHA1, &sigb, &len,
Norbert Pocs 587d7b
 	    data, datalen);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
@@ -201,11 +202,8 @@
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_DSA(pkey, key->dsa) != 1) {
Norbert Pocs 587d7b
-		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if ((ret = ssh_create_evp_dss(key, &pkey)) != 0)
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
-	}
Norbert Pocs 587d7b
 	ret = sshkey_verify_signature(pkey, SSH_DIGEST_SHA1, data, datalen,
Norbert Pocs 587d7b
 	    sigb, slen);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
@@ -221,4 +219,63 @@
Norbert Pocs 587d7b
 		freezero(sigblob, len);
Norbert Pocs 587d7b
 	return ret;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+int
Norbert Pocs 587d7b
+ssh_create_evp_dss(const struct sshkey *k, EVP_PKEY **pkey)
Norbert Pocs 587d7b
+{
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
+  	const BIGNUM *p = NULL, *q = NULL, *g = NULL, *pub = NULL, *priv = NULL;
Norbert Pocs 587d7b
+  	int ret = 0;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	if (k == NULL)
Norbert Pocs 587d7b
+  		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) == NULL ||
Norbert Pocs 587d7b
+  	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	  	goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	DSA_get0_pqg(k->dsa, &p, &q, &g);
Norbert Pocs 587d7b
+  	DSA_get0_key(k->dsa, &pub, &priv;;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	if (p != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_P, p) != 1) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (q != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_Q, q) != 1) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (g != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_FFC_G, g) != 1) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (pub != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_PUB_KEY,
Norbert Pocs 587d7b
+	        pub) != 1) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (priv != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+	        OSSL_PKEY_PARAM_PRIV_KEY,
Norbert Pocs 587d7b
+	        priv) != 1) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs 587d7b
+  		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+out:
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+  	return ret;
Norbert Pocs 587d7b
+}
Norbert Pocs 587d7b
 #endif /* WITH_OPENSSL */
Norbert Pocs 587d7b
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac ../../openssh-8.7p1/ssh-ecdsa.c ./ssh-ecdsa.c
Norbert Pocs 587d7b
--- ../../openssh-8.7p1/ssh-ecdsa.c	2023-03-08 15:35:14.669943335 +0100
Norbert Pocs 587d7b
+++ ./ssh-ecdsa.c	2023-03-08 15:40:52.628201267 +0100
Norbert Pocs 587d7b
@@ -34,6 +34,8 @@
Norbert Pocs 587d7b
 #include <openssl/ec.h>
Norbert Pocs 587d7b
 #include <openssl/ecdsa.h>
Norbert Pocs 587d7b
 #include <openssl/evp.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include <string.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
@@ -72,9 +74,8 @@
Norbert Pocs 587d7b
 	if ((hash_alg = sshkey_ec_nid_to_hash_alg(key->ecdsa_nid)) == -1)
Norbert Pocs 587d7b
 		return SSH_ERR_INTERNAL_ERROR;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1)
Norbert Pocs 587d7b
-		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if ((ret = ssh_create_evp_ec(key->ecdsa, key->ecdsa_nid, &pkey)) != 0)
Norbert Pocs 587d7b
+		return ret;
Norbert Pocs 587d7b
 	ret = sshkey_calculate_signature(pkey, hash_alg, &sigb, &len, data,
Norbert Pocs 587d7b
 	    datalen);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
@@ -193,11 +194,8 @@
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_EC_KEY(pkey, key->ecdsa) != 1) {
Norbert Pocs 587d7b
-		ret =  SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if (ssh_create_evp_ec(key->ecdsa, key->ecdsa_nid, &pkey) != 0)
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
-	}
Norbert Pocs 587d7b
 	ret = sshkey_verify_signature(pkey, hash_alg, data, datalen, sigb, len);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
@@ -212,4 +210,76 @@
Norbert Pocs 587d7b
 	return ret;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
+int
Norbert Pocs 587d7b
+ssh_create_evp_ec(EC_KEY *k, int ecdsa_nid, EVP_PKEY **pkey)
Norbert Pocs 587d7b
+{
Norbert Pocs 587d7b
+	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs 587d7b
+	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
+  	BN_CTX *bn_ctx = NULL;
Norbert Pocs 587d7b
+  	uint8_t *pub_ser = NULL; 
Norbert Pocs 587d7b
+  	const char *group_name;
Norbert Pocs 587d7b
+  	const EC_POINT *pub = NULL;
Norbert Pocs 587d7b
+  	const BIGNUM *priv = NULL;
Norbert Pocs 587d7b
+  	int ret = 0;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if (k == NULL)
Norbert Pocs 587d7b
+    		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "EC", NULL)) == NULL ||
Norbert Pocs 587d7b
+      	    (param_bld = OSSL_PARAM_BLD_new()) == NULL ||
Norbert Pocs 587d7b
+      	    (bn_ctx = BN_CTX_new()) == NULL) {
Norbert Pocs 587d7b
+    		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+    		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+	if ((group_name = OSSL_EC_curve_nid2name(ecdsa_nid)) == NULL ||
Norbert Pocs 587d7b
+     	    OSSL_PARAM_BLD_push_utf8_string(param_bld,
Norbert Pocs 587d7b
+                OSSL_PKEY_PARAM_GROUP_NAME,
Norbert Pocs 587d7b
+                group_name,
Norbert Pocs 587d7b
+                strlen(group_name)) != 1) {
Norbert Pocs 587d7b
+    		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+    		goto out;
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+  	if ((pub = EC_KEY_get0_public_key(k)) != NULL) {
Norbert Pocs 587d7b
+    		const EC_GROUP *group;
Norbert Pocs 587d7b
+    		size_t len;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+		group = EC_KEY_get0_group(k);
Norbert Pocs 587d7b
+		len = EC_POINT_point2oct(group, pub,
Norbert Pocs 587d7b
+		    POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL);
Norbert Pocs 587d7b
+		if ((pub_ser = malloc(len)) == NULL) {
Norbert Pocs 587d7b
+			ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+			goto out;
Norbert Pocs 587d7b
+		}
Norbert Pocs 587d7b
+		EC_POINT_point2oct(group,
Norbert Pocs 587d7b
+		    pub,
Norbert Pocs 587d7b
+		    POINT_CONVERSION_UNCOMPRESSED,
Norbert Pocs 587d7b
+		    pub_ser,
Norbert Pocs 587d7b
+		    len,
Norbert Pocs 587d7b
+		    bn_ctx);
Norbert Pocs 587d7b
+		if (OSSL_PARAM_BLD_push_octet_string(param_bld,
Norbert Pocs 587d7b
+		    OSSL_PKEY_PARAM_PUB_KEY,
Norbert Pocs 587d7b
+		    pub_ser,
Norbert Pocs 587d7b
+		    len) != 1) {
Norbert Pocs 587d7b
+			ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+			goto out;
Norbert Pocs 587d7b
+		}
Norbert Pocs 587d7b
+	}
Norbert Pocs 587d7b
+  	if ((priv = EC_KEY_get0_private_key(k)) != NULL &&
Norbert Pocs 587d7b
+	    OSSL_PARAM_BLD_push_BN(param_bld,
Norbert Pocs 587d7b
+               OSSL_PKEY_PARAM_PRIV_KEY, priv) != 1) {
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs 587d7b
+    		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+    		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+out:
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+  	BN_CTX_free(bn_ctx);
Norbert Pocs 587d7b
+  	free(pub_ser);
Norbert Pocs 587d7b
+  	return ret;
Norbert Pocs 587d7b
+}
Norbert Pocs 587d7b
 #endif /* WITH_OPENSSL && OPENSSL_HAS_ECC */
Norbert Pocs 587d7b
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac ../../openssh-8.7p1/sshkey.c ./sshkey.c
Norbert Pocs 587d7b
--- ../../openssh-8.7p1/sshkey.c	2023-03-08 15:35:14.702943628 +0100
Norbert Pocs 587d7b
+++ ./sshkey.c	2023-03-08 15:39:03.354082015 +0100
Norbert Pocs 587d7b
@@ -35,6 +35,8 @@
Norbert Pocs 587d7b
 #include <openssl/err.h>
Norbert Pocs 587d7b
 #include <openssl/pem.h>
Norbert Pocs 587d7b
 #include <openssl/fips.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 #endif
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include "crypto_api.h"
Norbert Pocs 587d7b
@@ -492,13 +494,14 @@
Norbert Pocs 587d7b
 {
Norbert Pocs 587d7b
 	EVP_MD_CTX *ctx = NULL;
Norbert Pocs 587d7b
 	u_char *sig = NULL;
Norbert Pocs 587d7b
-	int ret, slen, len;
Norbert Pocs 587d7b
+	int ret, slen;
Norbert Pocs 587d7b
+	size_t len;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 	if (sigp == NULL || lenp == NULL) {
Norbert Pocs 587d7b
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	slen = EVP_PKEY_size(pkey);
Norbert Pocs 587d7b
+	slen = EVP_PKEY_get_size(pkey);
Norbert Pocs 587d7b
 	if (slen <= 0 || slen > SSHBUF_MAX_BIGNUM)
Norbert Pocs 587d7b
 		return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
@@ -511,9 +514,10 @@
Norbert Pocs 587d7b
 		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
 		goto error;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
-	if (EVP_SignInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
Norbert Pocs 587d7b
-	    EVP_SignUpdate(ctx, data, datalen) <= 0 ||
Norbert Pocs 587d7b
-	    EVP_SignFinal(ctx, sig, &len, pkey) <= 0) {
Norbert Pocs 587d7b
+	if (EVP_DigestSignInit(ctx, NULL, ssh_digest_to_md(hash_alg),
Norbert Pocs 587d7b
+	        NULL, pkey) != 1 ||
Norbert Pocs 587d7b
+	    EVP_DigestSignUpdate(ctx, data, datalen) != 1 ||
Norbert Pocs 587d7b
+	    EVP_DigestSignFinal(ctx, sig, &len) != 1) {
Norbert Pocs 587d7b
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
 		goto error;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
@@ -540,12 +544,13 @@
Norbert Pocs 587d7b
 	if ((ctx = EVP_MD_CTX_new()) == NULL) {
Norbert Pocs 587d7b
 		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
-	if (EVP_VerifyInit_ex(ctx, ssh_digest_to_md(hash_alg), NULL) <= 0 ||
Norbert Pocs 587d7b
-	    EVP_VerifyUpdate(ctx, data, datalen) <= 0) {
Norbert Pocs 587d7b
+	if (EVP_DigestVerifyInit(ctx, NULL, ssh_digest_to_md(hash_alg),
Norbert Pocs 587d7b
+	    NULL, pkey) != 1 ||
Norbert Pocs 587d7b
+	    EVP_DigestVerifyUpdate(ctx, data, datalen) != 1) {
Norbert Pocs 587d7b
 		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
 		goto done;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
-	ret = EVP_VerifyFinal(ctx, sigbuf, siglen, pkey);
Norbert Pocs 587d7b
+	ret = EVP_DigestVerifyFinal(ctx, sigbuf, siglen);
Norbert Pocs 587d7b
 	switch (ret) {
Norbert Pocs 587d7b
 	case 1:
Norbert Pocs 587d7b
 		ret = 0;
Norbert Pocs 587d7b
@@ -5038,3 +5043,27 @@
Norbert Pocs 587d7b
 	return 0;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
 #endif /* WITH_XMSS */
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+#ifdef WITH_OPENSSL
Norbert Pocs 587d7b
+EVP_PKEY *
Norbert Pocs 587d7b
+sshkey_create_evp(OSSL_PARAM_BLD *param_bld, EVP_PKEY_CTX *ctx)
Norbert Pocs 587d7b
+{
Norbert Pocs 587d7b
+  	EVP_PKEY *ret = NULL;
Norbert Pocs 587d7b
+  	OSSL_PARAM *params = NULL;
Norbert Pocs 587d7b
+  	if (param_bld == NULL || ctx == NULL) {
Norbert Pocs 587d7b
+  		debug2_f("param_bld or ctx is NULL");
Norbert Pocs 587d7b
+  		return NULL;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if ((params = OSSL_PARAM_BLD_to_param(param_bld)) == NULL) {
Norbert Pocs 587d7b
+  		debug2_f("Could not build param list");
Norbert Pocs 587d7b
+  		return NULL;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (EVP_PKEY_fromdata_init(ctx) != 1 ||
Norbert Pocs 587d7b
+  	    EVP_PKEY_fromdata(ctx, &ret, EVP_PKEY_KEYPAIR, params) != 1) {
Norbert Pocs 587d7b
+  		debug2_f("EVP_PKEY_fromdata failed");
Norbert Pocs 587d7b
+  		OSSL_PARAM_free(params);
Norbert Pocs 587d7b
+  		return NULL;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	return ret;
Norbert Pocs 587d7b
+}
Norbert Pocs 587d7b
+#endif /* WITH_OPENSSL */
Norbert Pocs 587d7b
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac ../../openssh-8.7p1/sshkey.h ./sshkey.h
Norbert Pocs 587d7b
--- ../../openssh-8.7p1/sshkey.h	2023-03-08 15:35:14.702943628 +0100
Norbert Pocs 587d7b
+++ ./sshkey.h	2023-03-08 15:34:33.509578138 +0100
Norbert Pocs 587d7b
@@ -31,6 +31,9 @@
Norbert Pocs 587d7b
 #ifdef WITH_OPENSSL
Norbert Pocs 587d7b
 #include <openssl/rsa.h>
Norbert Pocs 587d7b
 #include <openssl/dsa.h>
Norbert Pocs 587d7b
+#include <openssl/evp.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
 # ifdef OPENSSL_HAS_ECC
Norbert Pocs 587d7b
 #  include <openssl/ec.h>
Norbert Pocs 587d7b
 #  include <openssl/ecdsa.h>
Norbert Pocs 587d7b
@@ -293,6 +295,13 @@
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 void	 sshkey_sig_details_free(struct sshkey_sig_details *);
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
+#ifdef WITH_OPENSSL
Norbert Pocs 587d7b
+EVP_PKEY  *sshkey_create_evp(OSSL_PARAM_BLD *, EVP_PKEY_CTX *);
Norbert Pocs 587d7b
+int   ssh_create_evp_dss(const struct sshkey *, EVP_PKEY **);
Norbert Pocs 587d7b
+int   ssh_create_evp_rsa(const struct sshkey *, EVP_PKEY **);
Norbert Pocs 587d7b
+int   ssh_create_evp_ec(EC_KEY *, int, EVP_PKEY **);
Norbert Pocs 587d7b
+#endif /* WITH_OPENSSL */
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
 #ifdef SSHKEY_INTERNAL
Norbert Pocs 587d7b
 int ssh_rsa_sign(const struct sshkey *key,
Norbert Pocs 587d7b
     u_char **sigp, size_t *lenp, const u_char *data, size_t datalen,
Norbert Pocs 587d7b
diff --color -ru -x regress -x autom4te.cache -x '*.o' -x '*.lo' -x Makefile -x config.status -x configure~ -x configure.ac ../../openssh-8.7p1/ssh-rsa.c ./ssh-rsa.c
Norbert Pocs 587d7b
--- ../../openssh-8.7p1/ssh-rsa.c	2023-03-08 15:35:14.669943335 +0100
Norbert Pocs 587d7b
+++ ./ssh-rsa.c	2023-03-08 15:34:33.509578138 +0100
Norbert Pocs 587d7b
@@ -23,6 +23,8 @@
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include <openssl/evp.h>
Norbert Pocs 587d7b
 #include <openssl/err.h>
Norbert Pocs 587d7b
+#include <openssl/core_names.h>
Norbert Pocs 587d7b
+#include <openssl/param_build.h>
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
 #include <stdarg.h>
Norbert Pocs 587d7b
 #include <string.h>
Norbert Pocs 587d7b
@@ -172,9 +174,8 @@
Norbert Pocs 587d7b
 	if (RSA_bits(key->rsa) < SSH_RSA_MINIMUM_MODULUS_SIZE)
Norbert Pocs 587d7b
 		return SSH_ERR_KEY_LENGTH;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_RSA(pkey, key->rsa) != 1)
Norbert Pocs 587d7b
-		return SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if ((ret = ssh_create_evp_rsa(key, &pkey)) != 0)
Norbert Pocs 587d7b
+		return ret;
Norbert Pocs 587d7b
 	ret = sshkey_calculate_signature(pkey, hash_alg, &sig, &len, data,
Norbert Pocs 587d7b
 	    datalen);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
@@ -285,11 +286,8 @@
Norbert Pocs 587d7b
 		len = modlen;
Norbert Pocs 587d7b
 	}
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	if ((pkey = EVP_PKEY_new()) == NULL ||
Norbert Pocs 587d7b
-	    EVP_PKEY_set1_RSA(pkey, key->rsa) != 1) {
Norbert Pocs 587d7b
-		ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	if ((ret = ssh_create_evp_rsa(key, &pkey)) != 0)
Norbert Pocs 587d7b
 		goto out;
Norbert Pocs 587d7b
-	}
Norbert Pocs 587d7b
 	ret = openssh_RSA_verify(hash_alg, data, datalen, sigblob, len, pkey);
Norbert Pocs 587d7b
 	EVP_PKEY_free(pkey);
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
@@ -306,11 +304,9 @@
Norbert Pocs 587d7b
     u_char *sigbuf, size_t siglen, EVP_PKEY *pkey)
Norbert Pocs 587d7b
 {
Norbert Pocs 587d7b
 	size_t rsasize = 0;
Norbert Pocs 587d7b
-	const RSA *rsa;
Norbert Pocs 587d7b
 	int ret;
Norbert Pocs 587d7b
 
Norbert Pocs 587d7b
-	rsa = EVP_PKEY_get0_RSA(pkey);
Norbert Pocs 587d7b
-	rsasize = RSA_size(rsa);
Norbert Pocs 587d7b
+	rsasize = EVP_PKEY_get_size(pkey);
Norbert Pocs 587d7b
 	if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
Norbert Pocs 587d7b
 	    siglen == 0 || siglen > rsasize) {
Norbert Pocs 587d7b
 		ret = SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
@@ -323,4 +319,87 @@
Norbert Pocs 587d7b
 done:
Norbert Pocs 587d7b
 	return ret;
Norbert Pocs 587d7b
 }
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+int
Norbert Pocs 587d7b
+ssh_create_evp_rsa(const struct sshkey *k, EVP_PKEY **pkey)
Norbert Pocs 587d7b
+{
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD *param_bld = NULL;
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX *ctx = NULL;
Norbert Pocs 587d7b
+  	int ret = 0;
Norbert Pocs 587d7b
+  	const BIGNUM *n = NULL, *e = NULL, *d = NULL, *p = NULL, *q = NULL;
Norbert Pocs 587d7b
+  	const BIGNUM *dmp1 = NULL, *dmq1 = NULL, *iqmp = NULL;
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	if (k == NULL)
Norbert Pocs 587d7b
+  	  	return SSH_ERR_INVALID_ARGUMENT;
Norbert Pocs 587d7b
+  	if ((ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) == NULL ||
Norbert Pocs 587d7b
+  	    (param_bld = OSSL_PARAM_BLD_new()) == NULL) {
Norbert Pocs 587d7b
+  	  	ret = SSH_ERR_ALLOC_FAIL;
Norbert Pocs 587d7b
+  	  	goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	RSA_get0_key(k->rsa, &n, &e, &d);
Norbert Pocs 587d7b
+  	RSA_get0_factors(k->rsa, &p, &q);
Norbert Pocs 587d7b
+  	RSA_get0_crt_params(k->rsa, &dmp1, &dmq1, &iqmp);
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	if (n != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_N, n) != 1) {
Norbert Pocs 587d7b
+  	  	ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+  		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (e != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_E, e) != 1) {
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (d != NULL &&
Norbert Pocs 587d7b
+  	    OSSL_PARAM_BLD_push_BN(param_bld, OSSL_PKEY_PARAM_RSA_D, d) != 1) {
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	if ((*pkey = sshkey_create_evp(param_bld, ctx)) == NULL) {
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+  	/* setting this to param_build makes the creation process fail */
Norbert Pocs 587d7b
+  	if (p != NULL &&
Norbert Pocs 587d7b
+  	    EVP_PKEY_set_bn_param(*pkey, OSSL_PKEY_PARAM_RSA_FACTOR1, p) != 1) {
Norbert Pocs 587d7b
+		debug2_f("failed to add 'p' param");
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (q != NULL &&
Norbert Pocs 587d7b
+  	    EVP_PKEY_set_bn_param(*pkey, OSSL_PKEY_PARAM_RSA_FACTOR2, q) != 1) {
Norbert Pocs 587d7b
+		debug2_f("failed to add 'q' param");
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (dmp1 != NULL &&
Norbert Pocs 587d7b
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs 587d7b
+  	        OSSL_PKEY_PARAM_RSA_EXPONENT1, dmp1) != 1) {
Norbert Pocs 587d7b
+		debug2_f("failed to add 'dmp1' param");
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (dmq1 != NULL &&
Norbert Pocs 587d7b
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs 587d7b
+  	        OSSL_PKEY_PARAM_RSA_EXPONENT2, dmq1) != 1) {
Norbert Pocs 587d7b
+		debug2_f("failed to add 'dmq1' param");
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+  	if (iqmp != NULL &&
Norbert Pocs 587d7b
+  	    EVP_PKEY_set_bn_param(*pkey,
Norbert Pocs 587d7b
+  	        OSSL_PKEY_PARAM_RSA_COEFFICIENT1, iqmp) != 1) {
Norbert Pocs 587d7b
+		debug2_f("failed to add 'iqmp' param");
Norbert Pocs 587d7b
+		ret = SSH_ERR_LIBCRYPTO_ERROR;
Norbert Pocs 587d7b
+		goto out;
Norbert Pocs 587d7b
+  	}
Norbert Pocs 587d7b
+
Norbert Pocs 587d7b
+out:
Norbert Pocs 587d7b
+  	OSSL_PARAM_BLD_free(param_bld);
Norbert Pocs 587d7b
+  	EVP_PKEY_CTX_free(ctx);
Norbert Pocs 587d7b
+  	return ret;
Norbert Pocs 587d7b
+}
Norbert Pocs 587d7b
 #endif /* WITH_OPENSSL */