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