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