upstream: add support for ECDSA keys in PKCS#11 tokens

Work by markus@ and Pedro Martelletto, feedback and ok me@

OpenBSD-Commit-ID: a37d651e221341376636056512bddfc16efb4424
This commit is contained in:
djm@openbsd.org 2019-01-20 22:51:37 +00:00 committed by Damien Miller
parent aa22c20e0c
commit 93f02107f4
5 changed files with 1306 additions and 240 deletions

View File

@ -1,6 +1,7 @@
/* $OpenBSD: ssh-pkcs11-client.c,v 1.10 2018/07/09 21:59:10 markus Exp $ */ /* $OpenBSD: ssh-pkcs11-client.c,v 1.12 2019/01/20 22:51:37 djm Exp $ */
/* /*
* Copyright (c) 2010 Markus Friedl. All rights reserved. * Copyright (c) 2010 Markus Friedl. All rights reserved.
* Copyright (c) 2014 Pedro Martelletto. All rights reserved.
* *
* Permission to use, copy, modify, and distribute this software for any * Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above * purpose with or without fee is hereby granted, provided that the above
@ -30,6 +31,7 @@
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <openssl/ecdsa.h>
#include <openssl/rsa.h> #include <openssl/rsa.h>
#include "openbsd-compat/openssl-compat.h" #include "openbsd-compat/openssl-compat.h"
@ -113,8 +115,7 @@ pkcs11_terminate(void)
} }
static int static int
pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
int padding)
{ {
struct sshkey key; /* XXX */ struct sshkey key; /* XXX */
u_char *blob, *signature = NULL; u_char *blob, *signature = NULL;
@ -154,18 +155,89 @@ pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
return (ret); return (ret);
} }
/* redirect the private key encrypt operation to the ssh-pkcs11-helper */ static ECDSA_SIG *
static int ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
wrap_key(RSA *rsa) const BIGNUM *rp, EC_KEY *ec)
{ {
static RSA_METHOD *helper_rsa; struct sshkey key; /* XXX */
u_char *blob, *signature = NULL;
const u_char *cp;
size_t blen, slen = 0;
ECDSA_SIG *ret = NULL;
struct sshbuf *msg;
int r;
key.type = KEY_ECDSA;
key.ecdsa = ec;
key.ecdsa_nid = sshkey_ecdsa_key_to_nid(ec);
if (key.ecdsa_nid < 0) {
error("%s: couldn't get curve nid", __func__);
return (NULL);
}
if ((r = sshkey_to_blob(&key, &blob, &blen)) != 0) {
error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
return (NULL);
}
if ((msg = sshbuf_new()) == NULL)
fatal("%s: sshbuf_new failed", __func__);
if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
(r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
(r = sshbuf_put_u32(msg, 0)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
free(blob);
send_msg(msg);
sshbuf_reset(msg);
if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
cp = signature;
ret = d2i_ECDSA_SIG(NULL, &cp, slen);
free(signature);
}
sshbuf_free(msg);
return (ret);
}
static RSA_METHOD *helper_rsa;
static EC_KEY_METHOD *helper_ecdsa;
/* redirect private key crypto operations to the ssh-pkcs11-helper */
static void
wrap_key(struct sshkey *k)
{
if (k->type == KEY_RSA)
RSA_set_method(k->rsa, helper_rsa);
else if (k->type == KEY_ECDSA)
EC_KEY_set_method(k->ecdsa, helper_ecdsa);
else
fatal("%s: unknown key type", __func__);
}
static int
pkcs11_start_helper_methods(void)
{
if (helper_ecdsa != NULL)
return (0);
int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
if (helper_ecdsa != NULL)
return (0);
helper_ecdsa = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
if (helper_ecdsa == NULL)
return (-1);
EC_KEY_METHOD_get_sign(helper_ecdsa, &orig_sign, NULL, NULL);
EC_KEY_METHOD_set_sign(helper_ecdsa, orig_sign, NULL, ecdsa_do_sign);
if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL) if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
fatal("%s: RSA_meth_dup failed", __func__); fatal("%s: RSA_meth_dup failed", __func__);
if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") || if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") ||
!RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt)) !RSA_meth_set_priv_enc(helper_rsa, rsa_encrypt))
fatal("%s: failed to prepare method", __func__); fatal("%s: failed to prepare method", __func__);
RSA_set_method(rsa, helper_rsa);
return (0); return (0);
} }
@ -174,6 +246,11 @@ pkcs11_start_helper(void)
{ {
int pair[2]; int pair[2];
if (pkcs11_start_helper_methods() == -1) {
error("pkcs11_start_helper_methods failed");
return (-1);
}
if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
error("socketpair: %s", strerror(errno)); error("socketpair: %s", strerror(errno));
return (-1); return (-1);
@ -204,7 +281,7 @@ int
pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp) pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
{ {
struct sshkey *k; struct sshkey *k;
int r; int r, type;
u_char *blob; u_char *blob;
size_t blen; size_t blen;
u_int nkeys, i; u_int nkeys, i;
@ -222,7 +299,8 @@ pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
send_msg(msg); send_msg(msg);
sshbuf_reset(msg); sshbuf_reset(msg);
if (recv_msg(msg) == SSH2_AGENT_IDENTITIES_ANSWER) { type = recv_msg(msg);
if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r)); fatal("%s: buffer error: %s", __func__, ssh_err(r));
*keysp = xcalloc(nkeys, sizeof(struct sshkey *)); *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
@ -234,10 +312,13 @@ pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
__func__, ssh_err(r)); __func__, ssh_err(r));
if ((r = sshkey_from_blob(blob, blen, &k)) != 0) if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
fatal("%s: bad key: %s", __func__, ssh_err(r)); fatal("%s: bad key: %s", __func__, ssh_err(r));
wrap_key(k->rsa); wrap_key(k);
(*keysp)[i] = k; (*keysp)[i] = k;
free(blob); free(blob);
} }
} else if (type == SSH2_AGENT_FAILURE) {
if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
nkeys = -1;
} else { } else {
nkeys = -1; nkeys = -1;
} }

View File

@ -1,4 +1,4 @@
/* $OpenBSD: ssh-pkcs11-helper.c,v 1.14 2018/01/08 15:18:46 markus Exp $ */ /* $OpenBSD: ssh-pkcs11-helper.c,v 1.15 2019/01/20 22:51:37 djm Exp $ */
/* /*
* Copyright (c) 2010 Markus Friedl. All rights reserved. * Copyright (c) 2010 Markus Friedl. All rights reserved.
* *
@ -110,7 +110,7 @@ static void
process_add(void) process_add(void)
{ {
char *name, *pin; char *name, *pin;
struct sshkey **keys; struct sshkey **keys = NULL;
int r, i, nkeys; int r, i, nkeys;
u_char *blob; u_char *blob;
size_t blen; size_t blen;
@ -139,11 +139,13 @@ process_add(void)
free(blob); free(blob);
add_key(keys[i], name); add_key(keys[i], name);
} }
free(keys);
} else { } else {
if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0) if ((r = sshbuf_put_u8(msg, SSH_AGENT_FAILURE)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r)); fatal("%s: buffer error: %s", __func__, ssh_err(r));
if ((r = sshbuf_put_u32(msg, -nkeys)) != 0)
fatal("%s: buffer error: %s", __func__, ssh_err(r));
} }
free(keys);
free(pin); free(pin);
free(name); free(name);
send_msg(msg); send_msg(msg);
@ -192,15 +194,33 @@ process_sign(void)
else { else {
if ((found = lookup_key(key)) != NULL) { if ((found = lookup_key(key)) != NULL) {
#ifdef WITH_OPENSSL #ifdef WITH_OPENSSL
u_int xslen;
int ret; int ret;
slen = RSA_size(key->rsa); if (key->type == KEY_RSA) {
signature = xmalloc(slen); slen = RSA_size(key->rsa);
if ((ret = RSA_private_encrypt(dlen, data, signature, signature = xmalloc(slen);
found->rsa, RSA_PKCS1_PADDING)) != -1) { ret = RSA_private_encrypt(dlen, data, signature,
slen = ret; found->rsa, RSA_PKCS1_PADDING);
ok = 0; if (ret != -1) {
} slen = ret;
ok = 0;
}
} else if (key->type == KEY_ECDSA) {
xslen = ECDSA_size(key->ecdsa);
signature = xmalloc(xslen);
/* "The parameter type is ignored." */
ret = ECDSA_sign(-1, data, dlen, signature,
&xslen, found->ecdsa);
if (ret != 0)
ok = 0;
else
error("%s: ECDSA_sign"
" returns %d", __func__, ret);
slen = xslen;
} else
error("%s: don't know how to sign with key "
"type %d", __func__, (int)key->type);
#endif /* WITH_OPENSSL */ #endif /* WITH_OPENSSL */
} }
sshkey_free(key); sshkey_free(key);

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
/* $OpenBSD: ssh-pkcs11.h,v 1.4 2015/01/15 09:40:00 djm Exp $ */ /* $OpenBSD: ssh-pkcs11.h,v 1.5 2019/01/20 22:51:37 djm Exp $ */
/* /*
* Copyright (c) 2010 Markus Friedl. All rights reserved. * Copyright (c) 2010 Markus Friedl. All rights reserved.
* *
@ -14,10 +14,26 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
/* Errors for pkcs11_add_provider() */
#define SSH_PKCS11_ERR_GENERIC 1
#define SSH_PKCS11_ERR_LOGIN_FAIL 2
#define SSH_PKCS11_ERR_NO_SLOTS 3
#define SSH_PKCS11_ERR_PIN_REQUIRED 4
#define SSH_PKCS11_ERR_PIN_LOCKED 5
int pkcs11_init(int); int pkcs11_init(int);
void pkcs11_terminate(void); void pkcs11_terminate(void);
int pkcs11_add_provider(char *, char *, struct sshkey ***); int pkcs11_add_provider(char *, char *, struct sshkey ***);
int pkcs11_del_provider(char *); int pkcs11_del_provider(char *);
#ifdef WITH_PKCS11_KEYGEN
struct sshkey *
pkcs11_gakp(char *, char *, unsigned int, char *, unsigned int,
unsigned int, unsigned char, u_int32_t *);
struct sshkey *
pkcs11_destroy_keypair(char *, char *, unsigned long, unsigned char,
u_int32_t *);
#endif
#if !defined(WITH_OPENSSL) && defined(ENABLE_PKCS11) #if !defined(WITH_OPENSSL) && defined(ENABLE_PKCS11)
#undef ENABLE_PKCS11 #undef ENABLE_PKCS11

View File

@ -1,4 +1,4 @@
/* $OpenBSD: sshkey.h,v 1.30 2018/09/14 04:17:44 djm Exp $ */ /* $OpenBSD: sshkey.h,v 1.31 2019/01/20 22:51:37 djm Exp $ */
/* /*
* Copyright (c) 2000, 2001 Markus Friedl. All rights reserved. * Copyright (c) 2000, 2001 Markus Friedl. All rights reserved.
@ -33,6 +33,7 @@
#include <openssl/dsa.h> #include <openssl/dsa.h>
# ifdef OPENSSL_HAS_ECC # ifdef OPENSSL_HAS_ECC
# include <openssl/ec.h> # include <openssl/ec.h>
# include <openssl/ecdsa.h>
# else /* OPENSSL_HAS_ECC */ # else /* OPENSSL_HAS_ECC */
# define EC_KEY void # define EC_KEY void
# define EC_GROUP void # define EC_GROUP void