7e9748
diff -up openssh-7.6p1/ssh-pkcs11-client.c.pkcs11-ecdsa openssh-7.6p1/ssh-pkcs11-client.c
7e9748
--- openssh-7.6p1/ssh-pkcs11-client.c.pkcs11-ecdsa	2018-02-16 13:25:59.426469253 +0100
7e9748
+++ openssh-7.6p1/ssh-pkcs11-client.c	2018-02-16 13:25:59.428469265 +0100
7e9748
@@ -31,6 +31,15 @@
7e9748
 #include <errno.h>
7e9748
 
7e9748
 #include <openssl/rsa.h>
7e9748
+#ifdef OPENSSL_HAS_ECC
7e9748
+#include <openssl/ecdsa.h>
7e9748
+#if ((defined(LIBRESSL_VERSION_NUMBER) && \
7e9748
+	(LIBRESSL_VERSION_NUMBER >= 0x20010002L))) || \
7e9748
+	(defined(ECDSA_F_ECDSA_METHOD_NEW)) || \
7e9748
+	(OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+#define ENABLE_PKCS11_ECDSA 1
7e9748
+#endif
7e9748
+#endif
7e9748
 
7e9748
 #include "pathnames.h"
7e9748
 #include "xmalloc.h"
7e9748
@@ -139,9 +147,9 @@ pkcs11_rsa_private_encrypt(int flen, con
7e9748
 	return (ret);
7e9748
 }
7e9748
 
7e9748
-/* redirect the private key encrypt operation to the ssh-pkcs11-helper */
7e9748
+/* redirect the RSA private key encrypt operation to the ssh-pkcs11-helper */
7e9748
 static int
7e9748
-wrap_key(RSA *rsa)
7e9748
+wrap_rsa_key(RSA *rsa)
7e9748
 {
7e9748
 	static RSA_METHOD helper_rsa;
7e9748
 
c60b55
@@ -152,6 +160,88 @@ wrap_key(RSA *rsa)
7e9748
 	return (0);
7e9748
 }
7e9748
 
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+static ECDSA_SIG *
7e9748
+pkcs11_ecdsa_private_sign(const unsigned char *from, int flen,
7e9748
+    const BIGNUM *inv, const BIGNUM *rp, EC_KEY * ecdsa)
7e9748
+{
c60b55
+	struct sshkey *key = NULL;
7e9748
+	u_char *blob, *signature = NULL;
bbf61d
+	size_t blen, slen = 0;
c60b55
+	struct sshbuf *msg = NULL;
7e9748
+	ECDSA_SIG *ret = NULL;
7e9748
+	BIGNUM *r = NULL, *s = NULL;
bbf61d
+	int rv;
7e9748
+
c60b55
+	if ((key = sshkey_new(KEY_ECDSA)) == NULL)
c60b55
+		fatal("%s: sshkey_new failed", __func__);
bbf61d
+	key->ecdsa = ecdsa;
bbf61d
+	key->ecdsa_nid = sshkey_ecdsa_key_to_nid(ecdsa);
bbf61d
+	if (sshkey_to_blob(key, &blob, &blen) == 0)
c60b55
+		goto out;
bbf61d
+	if ((msg = sshbuf_new()) == NULL)
bbf61d
+		fatal("%s: sshbuf_new failed", __func__);
bbf61d
+	if ((rv = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
bbf61d
+	    (rv = sshbuf_put_string(msg, blob, blen)) != 0 ||
bbf61d
+	    (rv = sshbuf_put_string(msg, from, flen)) != 0 ||
bbf61d
+	    (rv = sshbuf_put_u32(msg, 0)) != 0)
bbf61d
+		fatal("%s: buffer error: %s", __func__, ssh_err(rv));
7e9748
+	free(blob);
bbf61d
+	send_msg(msg);
bbf61d
+	sshbuf_reset(msg);
7e9748
+
bbf61d
+	if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
bbf61d
+		if ((rv = sshbuf_get_string(msg, &signature, &slen)) != 0)
bbf61d
+			fatal("%s: buffer error: %s", __func__, ssh_err(rv));
bbf61d
+		if (slen <= (size_t)ECDSA_size(ecdsa)) {
7e9748
+			int nlen = slen / 2;
7e9748
+			ret = ECDSA_SIG_new();
7e9748
+			r = BN_new();
7e9748
+			s = BN_new();
7e9748
+			BN_bin2bn(&signature[0], nlen, r);
7e9748
+			BN_bin2bn(&signature[nlen], nlen, s);
7e9748
+			ECDSA_SIG_set0(ret, r, s);
7e9748
+		}
7e9748
+		free(signature);
7e9748
+	}
c60b55
+out:
c60b55
+	sshkey_free(key);
bbf61d
+	sshbuf_free(msg);
7e9748
+	return (ret);
7e9748
+}
7e9748
+
7e9748
+/* redirect the ECDSA private key encrypt operation to the ssh-pkcs11-helper */
7e9748
+static int
7e9748
+wrap_ecdsa_key(EC_KEY *ecdsa) {
7e9748
+#if (OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+	static EC_KEY_METHOD *helper_ecdsa = NULL;
7e9748
+	if (helper_ecdsa == NULL) {
7e9748
+		const EC_KEY_METHOD *def = EC_KEY_get_default_method();
7e9748
+		helper_ecdsa = EC_KEY_METHOD_new(def);
7e9748
+		EC_KEY_METHOD_set_sign(helper_ecdsa, NULL, NULL, pkcs11_ecdsa_private_sign);
7e9748
+	}
7e9748
+	EC_KEY_set_method(ecdsa, helper_ecdsa);
7e9748
+#else
7e9748
+	static ECDSA_METHOD *helper_ecdsa = NULL;
7e9748
+	if(helper_ecdsa == NULL) {
7e9748
+		const ECDSA_METHOD *def = ECDSA_get_default_method();
7e9748
+# ifdef ECDSA_F_ECDSA_METHOD_NEW
7e9748
+		helper_ecdsa = ECDSA_METHOD_new((ECDSA_METHOD *)def);
7e9748
+		ECDSA_METHOD_set_name(helper_ecdsa, "ssh-pkcs11-helper-ecdsa");
7e9748
+		ECDSA_METHOD_set_sign(helper_ecdsa, pkcs11_ecdsa_private_sign);
7e9748
+# else
7e9748
+		helper_ecdsa = xcalloc(1, sizeof(*helper_ecdsa));
7e9748
+		memcpy(helper_ecdsa, def, sizeof(*helper_ecdsa));
7e9748
+		helper_ecdsa->name = "ssh-pkcs11-helper-ecdsa";
7e9748
+		helper_ecdsa->ecdsa_do_sign = pkcs11_ecdsa_private_sign;
7e9748
+# endif
7e9748
+	}
7e9748
+	ECDSA_set_method(ecdsa, helper_ecdsa);
7e9748
+#endif
7e9748
+	return (0);
7e9748
+}
7e9748
+#endif
7e9748
+
7e9748
 static int
7e9748
 pkcs11_start_helper(void)
7e9748
 {
7e9748
@@ -212,7 +281,15 @@ pkcs11_add_provider(char *name, char *pi
bbf61d
 				    __func__, ssh_err(r));
bbf61d
 			if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
bbf61d
 				fatal("%s: bad key: %s", __func__, ssh_err(r));
7e9748
-			wrap_key(k->rsa);
7e9748
+			if(k->type == KEY_RSA) {
7e9748
+				 wrap_rsa_key(k->rsa);
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+			} else if(k->type == KEY_ECDSA) {
7e9748
+				 wrap_ecdsa_key(k->ecdsa);
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+			} else {
7e9748
+				/* Unsupported type */
7e9748
+			}
7e9748
 			(*keysp)[i] = k;
7e9748
 			free(blob);
7e9748
 		}
7e9748
diff -up openssh-7.6p1/ssh-pkcs11.c.pkcs11-ecdsa openssh-7.6p1/ssh-pkcs11.c
7e9748
--- openssh-7.6p1/ssh-pkcs11.c.pkcs11-ecdsa	2018-02-16 13:25:59.427469259 +0100
7e9748
+++ openssh-7.6p1/ssh-pkcs11.c	2018-02-16 13:44:51.270554797 +0100
7e9748
@@ -32,6 +32,16 @@
7e9748
 #include "openbsd-compat/sys-queue.h"
7e9748
 
7e9748
 #include <openssl/x509.h>
7e9748
+#include <openssl/rsa.h>
7e9748
+#ifdef OPENSSL_HAS_ECC
7e9748
+#include <openssl/ecdsa.h>
7e9748
+#if ((defined(LIBRESSL_VERSION_NUMBER) && \
7e9748
+	(LIBRESSL_VERSION_NUMBER >= 0x20010002L))) || \
7e9748
+	(defined(ECDSA_F_ECDSA_METHOD_NEW)) || \
7e9748
+	(OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+#define ENABLE_PKCS11_ECDSA 1
7e9748
+#endif
7e9748
+#endif
7e9748
 
7e9748
 #define CRYPTOKI_COMPAT
7e9748
 #include "pkcs11.h"
7e9748
@@ -67,6 +76,7 @@ TAILQ_HEAD(, pkcs11_provider) pkcs11_pro
7e9748
 struct pkcs11_key {
7e9748
 	struct pkcs11_provider	*provider;
7e9748
 	CK_ULONG		slotidx;
7e9748
+	CK_ULONG		key_type;
7e9748
 	int			(*orig_finish)(RSA *rsa);
7e9748
 	RSA_METHOD		rsa_method;
af10de
 	char			*keyid;
7e9748
@@ -75,6 +85,9 @@ struct pkcs11_key {
7e9748
 };
7e9748
 
7e9748
 int pkcs11_interactive = 0;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+static int pkcs11_key_idx = -1;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 
7e9748
 /*
7e9748
  * This can't be in the ssh-pkcs11-uri, becase we can not depend on
7e9748
@@ -289,6 +302,40 @@ pkcs11_find(struct pkcs11_provider *p, C
7e9748
 	return (ret);
7e9748
 }
7e9748
 
7e9748
+int pkcs11_login(struct pkcs11_key *k11, CK_FUNCTION_LIST *f, struct pkcs11_slotinfo *si) {
7e9748
+	char			*pin = NULL, prompt[1024];
7e9748
+	CK_RV			rv;
7e9748
+	if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
7e9748
+		if (!pkcs11_interactive) {
7e9748
+			error("need pin entry%s", (si->token.flags &
7e9748
+			    CKF_PROTECTED_AUTHENTICATION_PATH) ?
7e9748
+			    " on reader keypad" : "");
7e9748
+			return (-1);
7e9748
+		}
7e9748
+		if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
7e9748
+			verbose("Deferring PIN entry to reader keypad.");
7e9748
+		else {
7e9748
+			snprintf(prompt, sizeof(prompt),
7e9748
+			    "Enter PIN for '%s': ", si->token.label);
7e9748
+			pin = read_passphrase(prompt, RP_ALLOW_EOF);
7e9748
+			if (pin == NULL)
7e9748
+				return (-1);	/* bail out */
7e9748
+		}
7e9748
+		rv = f->C_Login(si->session, CKU_USER, (u_char *)pin,
7e9748
+		    (pin != NULL) ? strlen(pin) : 0);
7e9748
+		if (pin != NULL) {
7e9748
+			explicit_bzero(pin, strlen(pin));
7e9748
+			free(pin);
7e9748
+		}
7e9748
+		if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
7e9748
+			error("C_Login failed: %lu", rv);
7e9748
+			return (-1);
7e9748
+		}
7e9748
+		si->logged_in = 1;
7e9748
+	}
7e9748
+	return 0;
7e9748
+}
7e9748
+
7e9748
 /* openssl callback doing the actual signing operation */
7e9748
 static int
7e9748
 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
7e9748
@@ -310,7 +357,6 @@ pkcs11_rsa_private_encrypt(int flen, con
7e9748
 		{CKA_ID, NULL, 0},
7e9748
 		{CKA_SIGN, NULL, sizeof(true_val) }
7e9748
 	};
7e9748
-	char			*pin = NULL, prompt[1024];
7e9748
 	int			rval = -1;
7e9748
 
7e9748
 	key_filter[0].pValue = &private_key_class;
7e9748
@@ -326,33 +372,8 @@ pkcs11_rsa_private_encrypt(int flen, con
7e9748
 	}
af10de
 	f = k11->provider->module->function_list;
af10de
 	si = &k11->provider->module->slotinfo[k11->slotidx];
7e9748
-	if ((si->token.flags & CKF_LOGIN_REQUIRED) && !si->logged_in) {
7e9748
-		if (!pkcs11_interactive) {
7e9748
-			error("need pin entry%s", (si->token.flags &
7e9748
-			    CKF_PROTECTED_AUTHENTICATION_PATH) ?
7e9748
-			    " on reader keypad" : "");
7e9748
-			return (-1);
7e9748
-		}
7e9748
-		if (si->token.flags & CKF_PROTECTED_AUTHENTICATION_PATH)
7e9748
-			verbose("Deferring PIN entry to reader keypad.");
7e9748
-		else {
7e9748
-			snprintf(prompt, sizeof(prompt),
7e9748
-			    "Enter PIN for '%s': ", si->token.label);
7e9748
-			pin = read_passphrase(prompt, RP_ALLOW_EOF);
7e9748
-			if (pin == NULL)
7e9748
-				return (-1);	/* bail out */
7e9748
-		}
7e9748
-		rv = f->C_Login(si->session, CKU_USER, (u_char *)pin,
7e9748
-		    (pin != NULL) ? strlen(pin) : 0);
7e9748
-		if (pin != NULL) {
7e9748
-			explicit_bzero(pin, strlen(pin));
7e9748
-			free(pin);
7e9748
-		}
7e9748
-		if (rv != CKR_OK && rv != CKR_USER_ALREADY_LOGGED_IN) {
7e9748
-			error("C_Login failed: %lu", rv);
7e9748
-			return (-1);
7e9748
-		}
7e9748
-		si->logged_in = 1;
7e9748
+	if(pkcs11_login(k11, f, si)) {
7e9748
+		return (-1);
7e9748
 	}
7e9748
 	key_filter[1].pValue = k11->keyid;
7e9748
 	key_filter[1].ulValueLen = k11->keyid_len;
7e9748
@@ -390,6 +411,7 @@ pkcs11_rsa_wrap(struct pkcs11_provider *
7e9748
 	const RSA_METHOD	*def = RSA_get_default_method();
7e9748
 
7e9748
 	k11 = xcalloc(1, sizeof(*k11));
7e9748
+	k11->key_type = CKK_RSA;
7e9748
 	k11->provider = provider;
7e9748
 	provider->refcount++;	/* provider referenced by RSA key */
7e9748
 	k11->slotidx = slotidx;
7e9748
@@ -415,6 +437,184 @@ pkcs11_rsa_wrap(struct pkcs11_provider *
7e9748
 	return (0);
7e9748
 }
7e9748
 
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+static ECDSA_SIG *pkcs11_ecdsa_sign(const unsigned char *dgst, int dgst_len,
7e9748
+                                    const BIGNUM *inv, const BIGNUM *rp,
7e9748
+                                    EC_KEY *ecdsa) {
7e9748
+	struct pkcs11_key	*k11;
7e9748
+	struct pkcs11_slotinfo	*si;
7e9748
+	CK_FUNCTION_LIST	*f;
7e9748
+	CK_OBJECT_HANDLE	obj;
7e9748
+	CK_ULONG		tlen = 0;
7e9748
+	CK_RV			rv;
7e9748
+	CK_OBJECT_CLASS	private_key_class = CKO_PRIVATE_KEY;
7e9748
+	CK_BBOOL		true_val = CK_TRUE;
7e9748
+	CK_MECHANISM		mech = {
7e9748
+		CKM_ECDSA, NULL_PTR, 0
7e9748
+	};
7e9748
+	CK_ATTRIBUTE		key_filter[] = {
7e9748
+		{CKA_CLASS, NULL, sizeof(private_key_class) },
7e9748
+		{CKA_ID, NULL, 0},
7e9748
+		{CKA_SIGN, NULL, sizeof(true_val) }
7e9748
+	};
7e9748
+	ECDSA_SIG  		*rval = NULL;
7e9748
+	key_filter[0].pValue = &private_key_class;
7e9748
+	key_filter[2].pValue = &true_val;
7e9748
+
7e9748
+ #if (OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+	if ((k11 = (struct pkcs11_key *)EC_KEY_get_ex_data(ecdsa, pkcs11_key_idx)) == NULL) {
7e9748
+		error("EC_KEY_get_ex_data failed for ecdsa %p", ecdsa);
7e9748
+ #else
7e9748
+	if ((k11 = (struct pkcs11_key *)ECDSA_get_ex_data(ecdsa, pkcs11_key_idx)) == NULL) {
7e9748
+		error("ECDSA_get_ex_data failed for ecdsa %p", ecdsa);
7e9748
+ #endif
7e9748
+		return NULL;
7e9748
+	}
7e9748
+	if (!k11->provider || !k11->provider->valid) {
7e9748
+		error("no pkcs11 (valid) provider for ecdsa %p", ecdsa);
7e9748
+		return NULL;
7e9748
+	}
af10de
+	f = k11->provider->module->function_list;
af10de
+	si = &k11->provider->module->slotinfo[k11->slotidx];
7e9748
+	if(pkcs11_login(k11, f, si)) {
7e9748
+		return NULL;
7e9748
+	}
7e9748
+	key_filter[1].pValue = k11->keyid;
7e9748
+	key_filter[1].ulValueLen = k11->keyid_len;
7e9748
+	/* try to find object w/CKA_SIGN first, retry w/o */
7e9748
+	if (pkcs11_find(k11->provider, k11->slotidx, key_filter, 3, &obj) < 0 &&
7e9748
+	    pkcs11_find(k11->provider, k11->slotidx, key_filter, 2, &obj) < 0) {
7e9748
+		error("cannot find private key");
7e9748
+	} else if ((rv = f->C_SignInit(si->session, &mech, obj)) != CKR_OK) {
7e9748
+		error("C_SignInit failed: %lu", rv);
7e9748
+	} else {
7e9748
+		CK_BYTE_PTR buf = NULL;
7e9748
+		BIGNUM *r = NULL, *s = NULL;
7e9748
+		int nlen;
7e9748
+		/* Make a call to C_Sign to find out the size of the signature */
7e9748
+		rv = f->C_Sign(si->session, (CK_BYTE *)dgst, dgst_len, NULL, &tlen);
7e9748
+		if (rv != CKR_OK) {
7e9748
+			error("C_Sign failed: %lu", rv);
7e9748
+			return NULL;
7e9748
+		}
7e9748
+		if ((buf = xmalloc(tlen)) == NULL) {
7e9748
+			error("failure to allocate signature buffer");
7e9748
+			return NULL;
7e9748
+		}
7e9748
+		rv = f->C_Sign(si->session, (CK_BYTE *)dgst, dgst_len, buf, &tlen);
7e9748
+		if (rv != CKR_OK) {
7e9748
+			error("C_Sign failed: %lu", rv);
7e9748
+		}
7e9748
+
7e9748
+		if ((rval = ECDSA_SIG_new()) == NULL ||
7e9748
+		    (r = BN_new()) == NULL ||
7e9748
+		    (s = BN_new()) == NULL) {
7e9748
+			error("failure to allocate ECDSA signature");
7e9748
+		} else {
7e9748
+			/*
7e9748
+			 * ECDSA signature is 2 large integers of same size returned
7e9748
+			 * concatenated by PKCS#11, we separate them to create an
7e9748
+			 * ECDSA_SIG for OpenSSL.
7e9748
+			 */
7e9748
+			nlen = tlen / 2;
7e9748
+			BN_bin2bn(&buf[0], nlen, r);
7e9748
+			BN_bin2bn(&buf[nlen], nlen, s);
7e9748
+			ECDSA_SIG_set0(rval, r, s);
7e9748
+		}
7e9748
+		free(buf);
7e9748
+	}
7e9748
+	return (rval);
7e9748
+}
7e9748
+
7e9748
+#if (OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+static EC_KEY_METHOD *get_pkcs11_ecdsa_method(void) {
7e9748
+	static EC_KEY_METHOD *pkcs11_ecdsa_method = NULL;
7e9748
+	if(pkcs11_key_idx == -1) {
7e9748
+		pkcs11_key_idx = EC_KEY_get_ex_new_index(0, NULL, NULL, NULL, 0);
7e9748
+	}
7e9748
+	if (pkcs11_ecdsa_method == NULL) {
7e9748
+		const EC_KEY_METHOD *def = EC_KEY_get_default_method();
7e9748
+		pkcs11_ecdsa_method = EC_KEY_METHOD_new(def);
7e9748
+		EC_KEY_METHOD_set_sign(pkcs11_ecdsa_method, NULL, NULL, pkcs11_ecdsa_sign);
7e9748
+	}
7e9748
+#else
7e9748
+static ECDSA_METHOD *get_pkcs11_ecdsa_method(void) {
7e9748
+	static ECDSA_METHOD *pkcs11_ecdsa_method = NULL;
7e9748
+	if(pkcs11_key_idx == -1) {
7e9748
+		pkcs11_key_idx = ECDSA_get_ex_new_index(0, NULL, NULL, NULL, 0);
7e9748
+	}
7e9748
+	if(pkcs11_ecdsa_method == NULL) {
7e9748
+		const ECDSA_METHOD *def = ECDSA_get_default_method();
7e9748
+ #ifdef ECDSA_F_ECDSA_METHOD_NEW
7e9748
+		pkcs11_ecdsa_method = ECDSA_METHOD_new((ECDSA_METHOD *)def);
7e9748
+		ECDSA_METHOD_set_name(pkcs11_ecdsa_method, "pkcs11");
7e9748
+		ECDSA_METHOD_set_sign(pkcs11_ecdsa_method, pkcs11_ecdsa_sign);
7e9748
+ #else
7e9748
+		pkcs11_ecdsa_method = xcalloc(1, sizeof(*pkcs11_ecdsa_method));
7e9748
+		memcpy(pkcs11_ecdsa_method, def, sizeof(*pkcs11_ecdsa_method));
7e9748
+		pkcs11_ecdsa_method->name = "pkcs11";
7e9748
+		pkcs11_ecdsa_method->ecdsa_do_sign = pkcs11_ecdsa_sign;
7e9748
+ #endif
7e9748
+	}
7e9748
+#endif
7e9748
+	return pkcs11_ecdsa_method;
7e9748
+}
7e9748
+
7e9748
+static int
7e9748
+pkcs11_ecdsa_wrap(struct pkcs11_provider *provider, CK_ULONG slotidx,
7e9748
+                  CK_ATTRIBUTE *keyid_attrib, CK_ATTRIBUTE *label_attrib, EC_KEY *ecdsa)
7e9748
+{
7e9748
+	struct pkcs11_key *k11;
7e9748
+	k11 = xcalloc(1, sizeof(*k11));
7e9748
+	k11->key_type = CKK_EC;
7e9748
+	k11->provider = provider;
7e9748
+	provider->refcount++; /* provider referenced by ECDSA key */
7e9748
+	k11->slotidx = slotidx;
7e9748
+	/* identify key object on smartcard */
7e9748
+	k11->keyid_len = keyid_attrib->ulValueLen;
7e9748
+	if (k11->keyid_len > 0) {
7e9748
+		k11->keyid = xmalloc(k11->keyid_len);
7e9748
+		memcpy(k11->keyid, keyid_attrib->pValue, k11->keyid_len);
7e9748
+	}
7e9748
+	if (label_attrib->ulValueLen > 0 ) {
7e9748
+		k11->label = xmalloc(label_attrib->ulValueLen+1);
7e9748
+		memcpy(k11->label, label_attrib->pValue, label_attrib->ulValueLen);
7e9748
+		k11->label[label_attrib->ulValueLen] = 0;
7e9748
+	}
7e9748
+ #if (OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+	EC_KEY_set_method(ecdsa, get_pkcs11_ecdsa_method());
7e9748
+	EC_KEY_set_ex_data(ecdsa, pkcs11_key_idx, k11);
7e9748
+ #else
7e9748
+	ECDSA_set_method(ecdsa, get_pkcs11_ecdsa_method());
7e9748
+	ECDSA_set_ex_data(ecdsa, pkcs11_key_idx, k11);
7e9748
+ #endif
7e9748
+	return (0);
7e9748
+}
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+
7e9748
+int pkcs11_del_key(struct sshkey *key) {
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+	if(key->type == KEY_ECDSA) {
7e9748
+		struct pkcs11_key *k11 = (struct pkcs11_key *)
7e9748
+ #if (OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+			EC_KEY_get_ex_data(key->ecdsa, pkcs11_key_idx);
7e9748
+ #else
7e9748
+			ECDSA_get_ex_data(key->ecdsa, pkcs11_key_idx);
7e9748
+ #endif
7e9748
+		if (k11 == NULL) {
7e9748
+			error("EC_KEY_get_ex_data failed for ecdsa %p", key->ecdsa);
7e9748
+		} else {
7e9748
+			if (k11->provider)
7e9748
+				pkcs11_provider_unref(k11->provider);
7e9748
+			free(k11->keyid);
7e9748
+			free(k11);
7e9748
+		}
7e9748
+	}
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+	sshkey_free(key);
7e9748
+	return (0);
7e9748
+}
7e9748
+
7e9748
 /* remove trailing spaces */
7e9748
 static void
7e9748
 rmspace(u_char *buf, size_t len)
7e9748
@@ -482,11 +646,13 @@ static int
7e9748
 pkcs11_fetch_keys(struct pkcs11_provider *p, CK_ULONG slotidx,
7e9748
     struct sshkey ***keysp, int *nkeys, struct pkcs11_uri *uri)
7e9748
 {
7e9748
-	size_t filter_size = 1;
7e9748
+	size_t filter_size = 2;
7e9748
+	CK_KEY_TYPE pubkey_type = CKK_RSA;
7e9748
 	CK_OBJECT_CLASS	pubkey_class = CKO_PUBLIC_KEY;
7e9748
 	CK_OBJECT_CLASS	cert_class = CKO_CERTIFICATE;
7e9748
 	CK_ATTRIBUTE		pubkey_filter[] = {
7e9748
 		{ CKA_CLASS, NULL, sizeof(pubkey_class) },
7e9748
+		{ CKA_KEY_TYPE, NULL, sizeof(pubkey_type) },
7e9748
 		{ CKA_ID, NULL, 0 },
7e9748
 		{ CKA_LABEL, NULL, 0 }
7e9748
 	};
7e9748
@@ -507,29 +673,60 @@ pkcs11_fetch_keys(struct pkcs11_provider
7e9748
 		{ CKA_SUBJECT, NULL, 0 },
7e9748
 		{ CKA_VALUE, NULL, 0 }
7e9748
 	};
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+	CK_KEY_TYPE	        ecdsa_type = CKK_EC;
7e9748
+	CK_ATTRIBUTE		ecdsa_filter[] = {
7e9748
+		{ CKA_CLASS, NULL, sizeof(pubkey_class) },
7e9748
+		{ CKA_KEY_TYPE, NULL, sizeof(ecdsa_type) },
7e9748
+		{ CKA_ID, NULL, 0 },
7e9748
+		{ CKA_LABEL, NULL, 0 }
7e9748
+	};
7e9748
+	CK_ATTRIBUTE		ecdsa_attribs[] = {
7e9748
+		{ CKA_ID, NULL, 0 },
7e9748
+		{ CKA_LABEL, NULL, 0 },
7e9748
+		{ CKA_EC_PARAMS, NULL, 0 },
7e9748
+		{ CKA_EC_POINT, NULL, 0 }
7e9748
+	};
7e9748
+	ecdsa_filter[0].pValue = &pubkey_class;
7e9748
+	ecdsa_filter[1].pValue = &ecdsa_type;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 	pubkey_filter[0].pValue = &pubkey_class;
7e9748
+	pubkey_filter[1].pValue = &pubkey_type;
7e9748
 	cert_filter[0].pValue = &cert_class;
7e9748
 
7e9748
 	if (uri->id != NULL) {
7e9748
 		pubkey_filter[filter_size].pValue = uri->id;
7e9748
 		pubkey_filter[filter_size].ulValueLen = uri->id_len;
7e9748
-		cert_filter[filter_size].pValue = uri->id;
7e9748
-		cert_filter[filter_size].ulValueLen = uri->id_len;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+		ecdsa_filter[filter_size].pValue = uri->id;
7e9748
+		ecdsa_filter[filter_size].ulValueLen = uri->id_len;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+		cert_filter[filter_size-1].pValue = uri->id;
7e9748
+		cert_filter[filter_size-1].ulValueLen = uri->id_len;
7e9748
 		filter_size++;
7e9748
 	}
7e9748
 	if (uri->object != NULL) {
7e9748
 		pubkey_filter[filter_size].pValue = uri->object;
7e9748
 		pubkey_filter[filter_size].ulValueLen = strlen(uri->object);
7e9748
 		pubkey_filter[filter_size].type = CKA_LABEL;
7e9748
-		cert_filter[filter_size].pValue = uri->object;
7e9748
-		cert_filter[filter_size].ulValueLen = strlen(uri->object);
7e9748
-		cert_filter[filter_size].type = CKA_LABEL;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+		ecdsa_filter[filter_size].pValue = uri->object;
7e9748
+		ecdsa_filter[filter_size].ulValueLen = strlen(uri->object);
7e9748
+		ecdsa_filter[filter_size].type = CKA_LABEL;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+		cert_filter[filter_size-1].pValue = uri->object;
7e9748
+		cert_filter[filter_size-1].ulValueLen = strlen(uri->object);
7e9748
+		cert_filter[filter_size-1].type = CKA_LABEL;
7e9748
 		filter_size++;
7e9748
 	}
7e9748
 
7e9748
 	if (pkcs11_fetch_keys_filter(p, slotidx, pubkey_filter, filter_size,
7e9748
 	    pubkey_attribs, keysp, nkeys) < 0 ||
7e9748
-	    pkcs11_fetch_keys_filter(p, slotidx, cert_filter, filter_size,
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+	    pkcs11_fetch_keys_filter(p, slotidx, ecdsa_filter, filter_size,
7e9748
+	    ecdsa_attribs, keysp, nkeys) < 0||
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+	    pkcs11_fetch_keys_filter(p, slotidx, cert_filter, filter_size - 1,
7e9748
 	    cert_attribs, keysp, nkeys) < 0)
7e9748
 		return (-1);
7e9748
 	return (0);
7e9748
@@ -553,6 +746,11 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
7e9748
 {
7e9748
 	struct sshkey		*key;
7e9748
 	RSA			*rsa;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+	EC_KEY			*ecdsa;
7e9748
+#else
7e9748
+	void			*ecdsa;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 	X509 			*x509;
7e9748
 	EVP_PKEY		*evp = NULL;
7e9748
 	int			i;
7e9748
@@ -608,6 +806,9 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
7e9748
 		 * or ID, label, subject and value for certificates.
7e9748
 		 */
7e9748
 		rsa = NULL;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+		ecdsa = NULL;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 		if ((rv = f->C_GetAttributeValue(session, obj, attribs, nattribs))
7e9748
 		    != CKR_OK) {
7e9748
 			error("C_GetAttributeValue failed: %lu", rv);
7e9748
@@ -620,6 +821,45 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
7e9748
 				rsa->e = BN_bin2bn(attribs[3].pValue,
7e9748
 				    attribs[3].ulValueLen, NULL);
7e9748
 			}
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+		} else if (attribs[2].type == CKA_EC_PARAMS ) {
7e9748
+			if ((ecdsa = EC_KEY_new()) == NULL) {
7e9748
+				error("EC_KEY_new failed");
7e9748
+			} else {
7e9748
+				const unsigned char *ptr1 = attribs[2].pValue;
7e9748
+				const unsigned char *ptr2 = attribs[3].pValue;
7e9748
+				CK_ULONG len1 = attribs[2].ulValueLen;
7e9748
+				CK_ULONG len2 = attribs[3].ulValueLen;
7e9748
+				ASN1_OCTET_STRING *point = NULL;
7e9748
+
7e9748
+				/*
7e9748
+				 * CKA_EC_PARAMS contains the curve parameters of the key
7e9748
+				 * either referenced as an OID or directly with all values.
7e9748
+				 * CKA_EC_POINT contains the point (public key) on the curve.
7e9748
+				 * The point is should be returned inside a DER-encoded
7e9748
+				 * ASN.1 OCTET STRING value (but some implementation).
7e9748
+				 */
7e9748
+				if ((point = d2i_ASN1_OCTET_STRING(NULL, &ptr2, len2))) {
7e9748
+					/* Pointing to OCTET STRING content */
7e9748
+					ptr2 = point->data;
7e9748
+					len2 = point->length;
7e9748
+				} else {
7e9748
+					/* No OCTET STRING */
7e9748
+					ptr2 = attribs[3].pValue;
7e9748
+				}
7e9748
+
7e9748
+				if((d2i_ECParameters(&ecdsa, &ptr1, len1) == NULL) ||
7e9748
+				   (o2i_ECPublicKey(&ecdsa, &ptr2, len2) == NULL)) {
7e9748
+					EC_KEY_free(ecdsa);
7e9748
+					ecdsa = NULL;
7e9748
+					error("EC public key parsing failed");
7e9748
+				}
7e9748
+
7e9748
+				if(point) {
7e9748
+					ASN1_OCTET_STRING_free(point);
7e9748
+				}
7e9748
+			}
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 		} else {
7e9748
 			cp = attribs[3].pValue;
7e9748
 			if ((x509 = X509_new()) == NULL) {
7e9748
@@ -639,13 +879,28 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
af10de
 			X509_free(x509);
7e9748
 			EVP_PKEY_free(evp);
7e9748
 		}
7e9748
-		if (rsa && rsa->n && rsa->e &&
7e9748
-		    pkcs11_rsa_wrap(p, slotidx, &attribs[0], &attribs[1], rsa) == 0) {
7e9748
-			if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
7e9748
-				fatal("sshkey_new failed");
7e9748
-			key->rsa = rsa;
7e9748
-			key->type = KEY_RSA;
7e9748
-			key->flags |= SSHKEY_FLAG_EXT;
7e9748
+		key = NULL;
7e9748
+		if (rsa || ecdsa) {
7e9748
+			if (rsa && rsa->n && rsa->e &&
7e9748
+			    pkcs11_rsa_wrap(p, slotidx, &attribs[0], &attribs[1], rsa) == 0) {
7e9748
+				if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
7e9748
+					fatal("sshkey_new failed");
7e9748
+				key->rsa = rsa;
7e9748
+				key->type = KEY_RSA;
7e9748
+				key->flags |= SSHKEY_FLAG_EXT;
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+			} else if(ecdsa && pkcs11_ecdsa_wrap(p, slotidx, &attribs[0], &attribs[1], ecdsa) == 0) {
7e9748
+				if ((key = sshkey_new(KEY_UNSPEC)) == NULL)
7e9748
+					fatal("sshkey_new failed");
7e9748
+				key->ecdsa = ecdsa;
7e9748
+				key->ecdsa_nid = sshkey_ecdsa_key_to_nid(ecdsa);
7e9748
+				key->type = KEY_ECDSA;
7e9748
+				key->flags |= SSHKEY_FLAG_EXT;
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+			}
7e9748
+		}
7e9748
+
7e9748
+		if(key) {
7e9748
 			if (pkcs11_key_included(keysp, nkeys, key)) {
7e9748
 				sshkey_free(key);
7e9748
 			} else {
7e9748
@@ -658,6 +913,10 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
7e9748
 			}
7e9748
 		} else if (rsa) {
7e9748
 			RSA_free(rsa);
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+		} else if (ecdsa) {
7e9748
+			EC_KEY_free(ecdsa);
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
 		}
7e9748
 		for (i = 0; i < nattribs; i++)
7e9748
 			free(attribs[i].pValue);
7e9748
diff -up openssh-7.6p1/ssh-pkcs11-helper.c.pkcs11-ecdsa openssh-7.6p1/ssh-pkcs11-helper.c
7e9748
--- openssh-7.6p1/ssh-pkcs11-helper.c.pkcs11-ecdsa	2017-10-02 21:34:26.000000000 +0200
7e9748
+++ openssh-7.6p1/ssh-pkcs11-helper.c	2018-02-16 13:25:59.428469265 +0100
7e9748
@@ -24,6 +24,17 @@
7e9748
 
7e9748
 #include "openbsd-compat/sys-queue.h"
7e9748
 
7e9748
+#include <openssl/rsa.h>
7e9748
+#ifdef OPENSSL_HAS_ECC
7e9748
+#include <openssl/ecdsa.h>
7e9748
+#if ((defined(LIBRESSL_VERSION_NUMBER) && \
7e9748
+	(LIBRESSL_VERSION_NUMBER >= 0x20010002L))) || \
7e9748
+	(defined(ECDSA_F_ECDSA_METHOD_NEW)) || \
7e9748
+	(OPENSSL_VERSION_NUMBER >= 0x00010100L)
7e9748
+#define ENABLE_PKCS11_ECDSA 1
7e9748
+#endif
7e9748
+#endif
7e9748
+
7e9748
 #include <stdarg.h>
7e9748
 #include <string.h>
7e9748
 #include <unistd.h>
7e9748
@@ -80,7 +90,7 @@ del_keys_by_name(char *name)
7e9748
 		if (!strcmp(ki->providername, name)) {
7e9748
 			TAILQ_REMOVE(&pkcs11_keylist, ki, next);
7e9748
 			free(ki->providername);
af10de
-			sshkey_free(ki->key);
7e9748
+			pkcs11_del_key(ki->key);
7e9748
 			free(ki);
7e9748
 		}
7e9748
 	}
7e9748
@@ -164,6 +174,20 @@ process_del(void)
af10de
 	sshbuf_free(msg);
7e9748
 }
7e9748
 
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+static u_int EC_KEY_order_size(EC_KEY *key)
7e9748
+{
7e9748
+	const EC_GROUP *group = EC_KEY_get0_group(key);
7e9748
+	BIGNUM *order = BN_new();
7e9748
+	u_int nbytes = 0;
7e9748
+	if ((group != NULL) && (order != NULL) && EC_GROUP_get_order(group, order, NULL)) {
7e9748
+		nbytes = BN_num_bytes(order);
7e9748
+	}
7e9748
+	BN_clear_free(order);
7e9748
+	return nbytes;
7e9748
+}
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+
7e9748
 static void
7e9748
 process_sign(void)
7e9748
 {
7e9748
@@ -180,14 +204,38 @@ process_sign(void)
af10de
 	else {
7e9748
 		if ((found = lookup_key(key)) != NULL) {
7e9748
 #ifdef WITH_OPENSSL
7e9748
-			int ret;
7e9748
-
7e9748
-			slen = RSA_size(key->rsa);
7e9748
-			signature = xmalloc(slen);
7e9748
-			if ((ret = RSA_private_encrypt(dlen, data, signature,
7e9748
-			    found->rsa, RSA_PKCS1_PADDING)) != -1) {
7e9748
-				slen = ret;
7e9748
-				ok = 0;
7e9748
+			if(found->type == KEY_RSA) {
7e9748
+				int ret;
7e9748
+				slen = RSA_size(key->rsa);
7e9748
+				signature = xmalloc(slen);
7e9748
+				if ((ret = RSA_private_encrypt(dlen, data, signature,
7e9748
+											   found->rsa, RSA_PKCS1_PADDING)) != -1) {
7e9748
+					slen = ret;
7e9748
+					ok = 0;
7e9748
+				}
7e9748
+#ifdef ENABLE_PKCS11_ECDSA
7e9748
+			} else if(found->type == KEY_ECDSA) {
7e9748
+				ECDSA_SIG *sig;
7e9748
+				const BIGNUM *r = NULL, *s = NULL;
7e9748
+				if ((sig = ECDSA_do_sign(data, dlen, found->ecdsa)) != NULL) {
7e9748
+					/* PKCS11 2.3.1 recommends both r and s to have the order size for
7e9748
+					   backward compatiblity */
7e9748
+					ECDSA_SIG_get0(sig, &r, &s);
7e9748
+					u_int o_len = EC_KEY_order_size(found->ecdsa);
7e9748
+					u_int r_len = BN_num_bytes(r);
7e9748
+					u_int s_len = BN_num_bytes(s);
7e9748
+					if (o_len > 0 && r_len <= o_len && s_len <= o_len) {
7e9748
+						signature = xcalloc(2, o_len);
7e9748
+						BN_bn2bin(r, signature + o_len - r_len);
7e9748
+						BN_bn2bin(s, signature + (2 * o_len) - s_len);
7e9748
+						slen = 2 * o_len;
7e9748
+						ok = 0;
7e9748
+					}
7e9748
+					ECDSA_SIG_free(sig);
7e9748
+				}
7e9748
+#endif /* ENABLE_PKCS11_ECDSA */
7e9748
+			} else {
7e9748
+				/* Unsupported type */
7e9748
 			}
7e9748
 #endif /* WITH_OPENSSL */
7e9748
 		}
7e9748
diff -up openssh-7.6p1/ssh-pkcs11.h.pkcs11-ecdsa openssh-7.6p1/ssh-pkcs11.h
7e9748
--- openssh-7.6p1/ssh-pkcs11.h.pkcs11-ecdsa	2018-02-16 13:25:59.429469272 +0100
7e9748
+++ openssh-7.6p1/ssh-pkcs11.h	2018-02-16 13:45:29.623800048 +0100
7e9748
@@ -20,6 +20,7 @@
7e9748
 int	pkcs11_init(int);
7e9748
 void	pkcs11_terminate(void);
7e9748
 int	pkcs11_add_provider(char *, char *, struct sshkey ***);
7e9748
+int	pkcs11_del_key(struct sshkey *);
7e9748
 int	pkcs11_add_provider_by_uri(struct pkcs11_uri *, char *, struct sshkey ***);
7e9748
 int	pkcs11_del_provider(char *);
7e9748
 int	pkcs11_uri_write(const struct sshkey *, FILE *);
077597
077597
077597
077597
diff -up openssh-7.6p1/ssh-pkcs11.c.old openssh-7.6p1/ssh-pkcs11.c
077597
--- openssh-7.6p1/ssh-pkcs11.c.old	2018-02-16 16:43:08.861520255 +0100
077597
+++ openssh-7.6p1/ssh-pkcs11.c	2018-02-16 16:56:35.312601451 +0100
077597
@@ -917,13 +917,28 @@ pkcs11_fetch_keys_filter(struct pkcs11_p
077597
 			} else if (d2i_X509(&x509, &cp, attribs[3].ulValueLen)
077597
 			    == NULL) {
077597
 				error("d2i_X509 failed");
077597
-			} else if ((evp = X509_get_pubkey(x509)) == NULL ||
077597
-			    evp->type != EVP_PKEY_RSA ||
077597
-			    evp->pkey.rsa == NULL) {
077597
-				debug("X509_get_pubkey failed or no rsa");
077597
-			} else if ((rsa = RSAPublicKey_dup(evp->pkey.rsa))
077597
-			    == NULL) {
077597
-				error("RSAPublicKey_dup");
077597
+			} else if ((evp = X509_get_pubkey(x509)) == NULL) {
077597
+				debug("X509_get_pubkey failed");
077597
+			} else {
077597
+				switch (evp->type) {
077597
+				case EVP_PKEY_RSA:
077597
+					if (evp->pkey.rsa == NULL)
077597
+						debug("Missing RSA key");
077597
+					else if ((rsa = RSAPublicKey_dup(
077597
+					    evp->pkey.rsa)) == NULL)
077597
+						error("RSAPublicKey_dup failed");
077597
+					break;
077597
+				case EVP_PKEY_EC:
077597
+					if (evp->pkey.ecdsa == NULL)
077597
+						debug("Missing ECDSA key");
077597
+					else if ((ecdsa = EC_KEY_dup(
077597
+					    evp->pkey.ecdsa)) == NULL)
077597
+						error("EC_KEY_dup failed");
077597
+					break;
077597
+				default:
077597
+					debug("not a RSA or ECDSA key");
077597
+					break;
077597
+				}
077597
 			}
af10de
 			X509_free(x509);
af10de
 			EVP_PKEY_free(evp);