Blame SOURCES/openssl-pkcs11-0.4.8-evp-pkey-ec-framework.patch

b4c227
From 0a2df89ba517bfbeaeadb81e42fe7bc3288b1985 Mon Sep 17 00:00:00 2001
b4c227
From: =?UTF-8?q?Micha=C5=82=20Trojnara?= <Michal.Trojnara@stunnel.org>
b4c227
Date: Thu, 23 Aug 2018 22:35:53 +0200
b4c227
Subject: [PATCH 05/23] Initial EVP_PKEY_EC framework
b4c227
b4c227
Fixes #243
b4c227
---
b4c227
 src/p11_pkey.c | 94 +++++++++++++++++++++++++++++++++++++++++---------
b4c227
 1 file changed, 78 insertions(+), 16 deletions(-)
b4c227
b4c227
diff --git a/src/p11_pkey.c b/src/p11_pkey.c
b4c227
index 45d5ad3..0efcaa4 100644
b4c227
--- a/src/p11_pkey.c
b4c227
+++ b/src/p11_pkey.c
b4c227
@@ -29,6 +29,13 @@ static int (*orig_pkey_rsa_decrypt) (EVP_PKEY_CTX *ctx,
b4c227
 	unsigned char *out, size_t *outlen,
b4c227
 	const unsigned char *in, size_t inlen);
b4c227
 
b4c227
+#ifndef OPENSSL_NO_EC
b4c227
+static int (*orig_pkey_ec_sign_init) (EVP_PKEY_CTX *ctx);
b4c227
+static int (*orig_pkey_ec_sign) (EVP_PKEY_CTX *ctx,
b4c227
+	unsigned char *sig, size_t *siglen,
b4c227
+	const unsigned char *tbs, size_t tbslen);
b4c227
+#endif /* OPENSSL_NO_EC */
b4c227
+
b4c227
 #if OPENSSL_VERSION_NUMBER < 0x10100000L || defined(LIBRESSL_VERSION_NUMBER)
b4c227
 struct evp_pkey_method_st {
b4c227
 	int pkey_id;
b4c227
@@ -490,54 +497,109 @@ static int pkcs11_pkey_rsa_decrypt(EVP_PKEY_CTX *evp_pkey_ctx,
b4c227
 
b4c227
 static EVP_PKEY_METHOD *pkcs11_pkey_method_rsa()
b4c227
 {
b4c227
-	EVP_PKEY_METHOD *orig_evp_pkey_meth_rsa, *new_evp_pkey_meth_rsa;
b4c227
+	EVP_PKEY_METHOD *orig_meth, *new_meth;
b4c227
 
b4c227
-	orig_evp_pkey_meth_rsa = (EVP_PKEY_METHOD *)EVP_PKEY_meth_find(EVP_PKEY_RSA);
b4c227
-	EVP_PKEY_meth_get_sign(orig_evp_pkey_meth_rsa,
b4c227
+	orig_meth = (EVP_PKEY_METHOD *)EVP_PKEY_meth_find(EVP_PKEY_RSA);
b4c227
+	EVP_PKEY_meth_get_sign(orig_meth,
b4c227
 		&orig_pkey_rsa_sign_init, &orig_pkey_rsa_sign);
b4c227
-	EVP_PKEY_meth_get_decrypt(orig_evp_pkey_meth_rsa,
b4c227
+	EVP_PKEY_meth_get_decrypt(orig_meth,
b4c227
 		&orig_pkey_rsa_decrypt_init,
b4c227
 		&orig_pkey_rsa_decrypt);
b4c227
 
b4c227
-	new_evp_pkey_meth_rsa = EVP_PKEY_meth_new(EVP_PKEY_RSA,
b4c227
+	new_meth = EVP_PKEY_meth_new(EVP_PKEY_RSA,
b4c227
 		EVP_PKEY_FLAG_AUTOARGLEN);
b4c227
 
b4c227
-	EVP_PKEY_meth_copy(new_evp_pkey_meth_rsa, orig_evp_pkey_meth_rsa);
b4c227
+	EVP_PKEY_meth_copy(new_meth, orig_meth);
b4c227
 
b4c227
-	EVP_PKEY_meth_set_sign(new_evp_pkey_meth_rsa,
b4c227
+	EVP_PKEY_meth_set_sign(new_meth,
b4c227
 		orig_pkey_rsa_sign_init, pkcs11_pkey_rsa_sign);
b4c227
-	EVP_PKEY_meth_set_decrypt(new_evp_pkey_meth_rsa,
b4c227
+	EVP_PKEY_meth_set_decrypt(new_meth,
b4c227
 		orig_pkey_rsa_decrypt_init, pkcs11_pkey_rsa_decrypt);
b4c227
 
b4c227
-	return new_evp_pkey_meth_rsa;
b4c227
+	return new_meth;
b4c227
+}
b4c227
+
b4c227
+#ifndef OPENSSL_NO_EC
b4c227
+
b4c227
+static int pkcs11_try_pkey_ec_sign(EVP_PKEY_CTX *evp_pkey_ctx,
b4c227
+		unsigned char *sig, size_t *siglen,
b4c227
+		const unsigned char *tbs, size_t tbslen)
b4c227
+{
b4c227
+	fprintf(stderr, "%s:%d pkcs11_try_pkey_ec_sign() not implemented\n",
b4c227
+		__FILE__, __LINE__);
b4c227
+	return -1;
b4c227
 }
b4c227
 
b4c227
+static int pkcs11_pkey_ec_sign(EVP_PKEY_CTX *evp_pkey_ctx,
b4c227
+		unsigned char *sig, size_t *siglen,
b4c227
+		const unsigned char *tbs, size_t tbslen)
b4c227
+{
b4c227
+	int ret;
b4c227
+
b4c227
+	ret = pkcs11_try_pkey_ec_sign(evp_pkey_ctx, sig, siglen, tbs, tbslen);
b4c227
+	if (ret < 0)
b4c227
+		ret = (*orig_pkey_ec_sign)(evp_pkey_ctx, sig, siglen, tbs, tbslen);
b4c227
+	return ret;
b4c227
+}
b4c227
+
b4c227
+static EVP_PKEY_METHOD *pkcs11_pkey_method_ec()
b4c227
+{
b4c227
+	EVP_PKEY_METHOD *orig_meth, *new_meth;
b4c227
+
b4c227
+	orig_meth = (EVP_PKEY_METHOD *)EVP_PKEY_meth_find(EVP_PKEY_EC);
b4c227
+	EVP_PKEY_meth_get_sign(orig_meth,
b4c227
+		&orig_pkey_ec_sign_init, &orig_pkey_ec_sign);
b4c227
+
b4c227
+	new_meth = EVP_PKEY_meth_new(EVP_PKEY_EC,
b4c227
+		EVP_PKEY_FLAG_AUTOARGLEN);
b4c227
+
b4c227
+	EVP_PKEY_meth_copy(new_meth, orig_meth);
b4c227
+
b4c227
+	EVP_PKEY_meth_set_sign(new_meth,
b4c227
+		orig_pkey_ec_sign_init, pkcs11_pkey_ec_sign);
b4c227
+
b4c227
+	return new_meth;
b4c227
+}
b4c227
+
b4c227
+#endif /* OPENSSL_NO_EC */
b4c227
+
b4c227
 int PKCS11_pkey_meths(ENGINE *e, EVP_PKEY_METHOD **pmeth,
b4c227
 		const int **nids, int nid)
b4c227
 {
b4c227
 	static int pkey_nids[] = {
b4c227
 		EVP_PKEY_RSA,
b4c227
+		EVP_PKEY_EC,
b4c227
 		0
b4c227
 	};
b4c227
 	static EVP_PKEY_METHOD *pkey_method_rsa = NULL;
b4c227
+	static EVP_PKEY_METHOD *pkey_method_ec = NULL;
b4c227
 
b4c227
 	(void)e; /* squash the unused parameter warning */
b4c227
 	/* all PKCS#11 engines currently share the same pkey_meths */
b4c227
 
b4c227
-	if (pkey_method_rsa == NULL)
b4c227
-		pkey_method_rsa = pkcs11_pkey_method_rsa();
b4c227
-	if (pkey_method_rsa == NULL)
b4c227
-		return 0;
b4c227
-
b4c227
 	if (!pmeth) { /* get the list of supported nids */
b4c227
 		*nids = pkey_nids;
b4c227
-		return 1; /* the number of returned nids */
b4c227
+		return sizeof(pkey_nids) / sizeof(int) - 1;
b4c227
 	}
b4c227
 
b4c227
 	/* get the EVP_PKEY_METHOD */
b4c227
-	if (nid == EVP_PKEY_RSA) {
b4c227
+	switch (nid) {
b4c227
+	case EVP_PKEY_RSA:
b4c227
+		if (pkey_method_rsa == NULL)
b4c227
+			pkey_method_rsa = pkcs11_pkey_method_rsa();
b4c227
+		if (pkey_method_rsa == NULL)
b4c227
+			return 0;
b4c227
 		*pmeth = pkey_method_rsa;
b4c227
 		return 1; /* success */
b4c227
+#ifndef OPENSSL_NO_EC
b4c227
+	case EVP_PKEY_EC:
b4c227
+		if (pkey_method_ec == NULL)
b4c227
+			pkey_method_ec = pkcs11_pkey_method_ec();
b4c227
+		if (pkey_method_ec == NULL)
b4c227
+			return 0;
b4c227
+		*pmeth = pkey_method_ec;
b4c227
+		return 1; /* success */
b4c227
+#endif /* OPENSSL_NO_EC */
b4c227
 	}
b4c227
 	*pmeth = NULL;
b4c227
 	return 0;
b4c227
-- 
b4c227
2.17.1
b4c227