1*069ac184SEd Maste /* $OpenBSD: ssh-pkcs11-client.c,v 1.19 2023/12/18 14:46:56 djm Exp $ */ 2b15c8340SDag-Erling Smørgrav /* 3b15c8340SDag-Erling Smørgrav * Copyright (c) 2010 Markus Friedl. All rights reserved. 419261079SEd Maste * Copyright (c) 2014 Pedro Martelletto. All rights reserved. 5b15c8340SDag-Erling Smørgrav * 6b15c8340SDag-Erling Smørgrav * Permission to use, copy, modify, and distribute this software for any 7b15c8340SDag-Erling Smørgrav * purpose with or without fee is hereby granted, provided that the above 8b15c8340SDag-Erling Smørgrav * copyright notice and this permission notice appear in all copies. 9b15c8340SDag-Erling Smørgrav * 10b15c8340SDag-Erling Smørgrav * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11b15c8340SDag-Erling Smørgrav * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12b15c8340SDag-Erling Smørgrav * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13b15c8340SDag-Erling Smørgrav * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14b15c8340SDag-Erling Smørgrav * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15b15c8340SDag-Erling Smørgrav * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16b15c8340SDag-Erling Smørgrav * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17b15c8340SDag-Erling Smørgrav */ 18b15c8340SDag-Erling Smørgrav 19b15c8340SDag-Erling Smørgrav #include "includes.h" 20b15c8340SDag-Erling Smørgrav 21b15c8340SDag-Erling Smørgrav #ifdef ENABLE_PKCS11 22b15c8340SDag-Erling Smørgrav 23b15c8340SDag-Erling Smørgrav #include <sys/types.h> 24b15c8340SDag-Erling Smørgrav #ifdef HAVE_SYS_TIME_H 25b15c8340SDag-Erling Smørgrav # include <sys/time.h> 26b15c8340SDag-Erling Smørgrav #endif 27b15c8340SDag-Erling Smørgrav #include <sys/socket.h> 28b15c8340SDag-Erling Smørgrav 29b15c8340SDag-Erling Smørgrav #include <stdarg.h> 30b15c8340SDag-Erling Smørgrav #include <string.h> 31b15c8340SDag-Erling Smørgrav #include <unistd.h> 32b15c8340SDag-Erling Smørgrav #include <errno.h> 33535af610SEd Maste #include <limits.h> 34b15c8340SDag-Erling Smørgrav 3519261079SEd Maste #include <openssl/ecdsa.h> 36a0ee8cc6SDag-Erling Smørgrav #include <openssl/rsa.h> 37a0ee8cc6SDag-Erling Smørgrav 38b15c8340SDag-Erling Smørgrav #include "pathnames.h" 39b15c8340SDag-Erling Smørgrav #include "xmalloc.h" 40190cef3dSDag-Erling Smørgrav #include "sshbuf.h" 41b15c8340SDag-Erling Smørgrav #include "log.h" 42b15c8340SDag-Erling Smørgrav #include "misc.h" 43190cef3dSDag-Erling Smørgrav #include "sshkey.h" 44b15c8340SDag-Erling Smørgrav #include "authfd.h" 45b15c8340SDag-Erling Smørgrav #include "atomicio.h" 46b15c8340SDag-Erling Smørgrav #include "ssh-pkcs11.h" 47190cef3dSDag-Erling Smørgrav #include "ssherr.h" 48b15c8340SDag-Erling Smørgrav 49535af610SEd Maste #include "openbsd-compat/openssl-compat.h" 50535af610SEd Maste 51535af610SEd Maste #if !defined(OPENSSL_HAS_ECC) || !defined(HAVE_EC_KEY_METHOD_NEW) 52535af610SEd Maste #define EC_KEY_METHOD void 53535af610SEd Maste #define EC_KEY void 54535af610SEd Maste #endif 55535af610SEd Maste 56b15c8340SDag-Erling Smørgrav /* borrows code from sftp-server and ssh-agent */ 57b15c8340SDag-Erling Smørgrav 58535af610SEd Maste /* 59535af610SEd Maste * Maintain a list of ssh-pkcs11-helper subprocesses. These may be looked up 60535af610SEd Maste * by provider path or their unique EC/RSA METHOD pointers. 61535af610SEd Maste */ 62535af610SEd Maste struct helper { 63535af610SEd Maste char *path; 64535af610SEd Maste pid_t pid; 65535af610SEd Maste int fd; 66535af610SEd Maste RSA_METHOD *rsa_meth; 67535af610SEd Maste EC_KEY_METHOD *ec_meth; 68535af610SEd Maste int (*rsa_finish)(RSA *rsa); 69535af610SEd Maste void (*ec_finish)(EC_KEY *key); 70535af610SEd Maste size_t nrsa, nec; /* number of active keys of each type */ 71535af610SEd Maste }; 72535af610SEd Maste static struct helper **helpers; 73535af610SEd Maste static size_t nhelpers; 74535af610SEd Maste 75535af610SEd Maste static struct helper * 76535af610SEd Maste helper_by_provider(const char *path) 77535af610SEd Maste { 78535af610SEd Maste size_t i; 79535af610SEd Maste 80535af610SEd Maste for (i = 0; i < nhelpers; i++) { 81535af610SEd Maste if (helpers[i] == NULL || helpers[i]->path == NULL || 82535af610SEd Maste helpers[i]->fd == -1) 83535af610SEd Maste continue; 84535af610SEd Maste if (strcmp(helpers[i]->path, path) == 0) 85535af610SEd Maste return helpers[i]; 86535af610SEd Maste } 87535af610SEd Maste return NULL; 88535af610SEd Maste } 89535af610SEd Maste 90535af610SEd Maste static struct helper * 91535af610SEd Maste helper_by_rsa(const RSA *rsa) 92535af610SEd Maste { 93535af610SEd Maste size_t i; 94535af610SEd Maste const RSA_METHOD *meth; 95535af610SEd Maste 96535af610SEd Maste if ((meth = RSA_get_method(rsa)) == NULL) 97535af610SEd Maste return NULL; 98535af610SEd Maste for (i = 0; i < nhelpers; i++) { 99535af610SEd Maste if (helpers[i] != NULL && helpers[i]->rsa_meth == meth) 100535af610SEd Maste return helpers[i]; 101535af610SEd Maste } 102535af610SEd Maste return NULL; 103535af610SEd Maste 104535af610SEd Maste } 105535af610SEd Maste 106535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 107535af610SEd Maste static struct helper * 108535af610SEd Maste helper_by_ec(const EC_KEY *ec) 109535af610SEd Maste { 110535af610SEd Maste size_t i; 111535af610SEd Maste const EC_KEY_METHOD *meth; 112535af610SEd Maste 113535af610SEd Maste if ((meth = EC_KEY_get_method(ec)) == NULL) 114535af610SEd Maste return NULL; 115535af610SEd Maste for (i = 0; i < nhelpers; i++) { 116535af610SEd Maste if (helpers[i] != NULL && helpers[i]->ec_meth == meth) 117535af610SEd Maste return helpers[i]; 118535af610SEd Maste } 119535af610SEd Maste return NULL; 120535af610SEd Maste 121535af610SEd Maste } 122535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */ 123b15c8340SDag-Erling Smørgrav 124b15c8340SDag-Erling Smørgrav static void 125535af610SEd Maste helper_free(struct helper *helper) 126535af610SEd Maste { 127535af610SEd Maste size_t i; 128535af610SEd Maste int found = 0; 129535af610SEd Maste 130535af610SEd Maste if (helper == NULL) 131535af610SEd Maste return; 132535af610SEd Maste if (helper->path == NULL || helper->ec_meth == NULL || 133535af610SEd Maste helper->rsa_meth == NULL) 134535af610SEd Maste fatal_f("inconsistent helper"); 135535af610SEd Maste debug3_f("free helper for provider %s", helper->path); 136535af610SEd Maste for (i = 0; i < nhelpers; i++) { 137535af610SEd Maste if (helpers[i] == helper) { 138535af610SEd Maste if (found) 139535af610SEd Maste fatal_f("helper recorded more than once"); 140535af610SEd Maste found = 1; 141535af610SEd Maste } 142535af610SEd Maste else if (found) 143535af610SEd Maste helpers[i - 1] = helpers[i]; 144535af610SEd Maste } 145535af610SEd Maste if (found) { 146535af610SEd Maste helpers = xrecallocarray(helpers, nhelpers, 147535af610SEd Maste nhelpers - 1, sizeof(*helpers)); 148535af610SEd Maste nhelpers--; 149535af610SEd Maste } 150535af610SEd Maste free(helper->path); 151535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 152535af610SEd Maste EC_KEY_METHOD_free(helper->ec_meth); 153535af610SEd Maste #endif 154535af610SEd Maste RSA_meth_free(helper->rsa_meth); 155535af610SEd Maste free(helper); 156535af610SEd Maste } 157535af610SEd Maste 158535af610SEd Maste static void 159535af610SEd Maste helper_terminate(struct helper *helper) 160535af610SEd Maste { 161535af610SEd Maste if (helper == NULL) { 162535af610SEd Maste return; 163535af610SEd Maste } else if (helper->fd == -1) { 164535af610SEd Maste debug3_f("already terminated"); 165535af610SEd Maste } else { 166535af610SEd Maste debug3_f("terminating helper for %s; " 167535af610SEd Maste "remaining %zu RSA %zu ECDSA", 168535af610SEd Maste helper->path, helper->nrsa, helper->nec); 169535af610SEd Maste close(helper->fd); 170535af610SEd Maste /* XXX waitpid() */ 171535af610SEd Maste helper->fd = -1; 172535af610SEd Maste helper->pid = -1; 173535af610SEd Maste } 174535af610SEd Maste /* 175535af610SEd Maste * Don't delete the helper entry until there are no remaining keys 176535af610SEd Maste * that reference it. Otherwise, any signing operation would call 177535af610SEd Maste * a free'd METHOD pointer and that would be bad. 178535af610SEd Maste */ 179535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 180535af610SEd Maste helper_free(helper); 181535af610SEd Maste } 182535af610SEd Maste 183535af610SEd Maste static void 184535af610SEd Maste send_msg(int fd, struct sshbuf *m) 185b15c8340SDag-Erling Smørgrav { 186b15c8340SDag-Erling Smørgrav u_char buf[4]; 187190cef3dSDag-Erling Smørgrav size_t mlen = sshbuf_len(m); 188190cef3dSDag-Erling Smørgrav int r; 189b15c8340SDag-Erling Smørgrav 190535af610SEd Maste if (fd == -1) 191535af610SEd Maste return; 192190cef3dSDag-Erling Smørgrav POKE_U32(buf, mlen); 193b15c8340SDag-Erling Smørgrav if (atomicio(vwrite, fd, buf, 4) != 4 || 194190cef3dSDag-Erling Smørgrav atomicio(vwrite, fd, sshbuf_mutable_ptr(m), 195190cef3dSDag-Erling Smørgrav sshbuf_len(m)) != sshbuf_len(m)) 196b15c8340SDag-Erling Smørgrav error("write to helper failed"); 197190cef3dSDag-Erling Smørgrav if ((r = sshbuf_consume(m, mlen)) != 0) 19819261079SEd Maste fatal_fr(r, "consume"); 199b15c8340SDag-Erling Smørgrav } 200b15c8340SDag-Erling Smørgrav 201b15c8340SDag-Erling Smørgrav static int 202535af610SEd Maste recv_msg(int fd, struct sshbuf *m) 203b15c8340SDag-Erling Smørgrav { 204b15c8340SDag-Erling Smørgrav u_int l, len; 205190cef3dSDag-Erling Smørgrav u_char c, buf[1024]; 206190cef3dSDag-Erling Smørgrav int r; 207b15c8340SDag-Erling Smørgrav 208535af610SEd Maste sshbuf_reset(m); 209535af610SEd Maste if (fd == -1) 210535af610SEd Maste return 0; /* XXX */ 211b15c8340SDag-Erling Smørgrav if ((len = atomicio(read, fd, buf, 4)) != 4) { 212b15c8340SDag-Erling Smørgrav error("read from helper failed: %u", len); 213b15c8340SDag-Erling Smørgrav return (0); /* XXX */ 214b15c8340SDag-Erling Smørgrav } 215190cef3dSDag-Erling Smørgrav len = PEEK_U32(buf); 216b15c8340SDag-Erling Smørgrav if (len > 256 * 1024) 217b15c8340SDag-Erling Smørgrav fatal("response too long: %u", len); 218b15c8340SDag-Erling Smørgrav /* read len bytes into m */ 219b15c8340SDag-Erling Smørgrav while (len > 0) { 220b15c8340SDag-Erling Smørgrav l = len; 221b15c8340SDag-Erling Smørgrav if (l > sizeof(buf)) 222b15c8340SDag-Erling Smørgrav l = sizeof(buf); 223b15c8340SDag-Erling Smørgrav if (atomicio(read, fd, buf, l) != l) { 224b15c8340SDag-Erling Smørgrav error("response from helper failed."); 225b15c8340SDag-Erling Smørgrav return (0); /* XXX */ 226b15c8340SDag-Erling Smørgrav } 227190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put(m, buf, l)) != 0) 22819261079SEd Maste fatal_fr(r, "sshbuf_put"); 229b15c8340SDag-Erling Smørgrav len -= l; 230b15c8340SDag-Erling Smørgrav } 231190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_u8(m, &c)) != 0) 23219261079SEd Maste fatal_fr(r, "parse type"); 233190cef3dSDag-Erling Smørgrav return c; 234b15c8340SDag-Erling Smørgrav } 235b15c8340SDag-Erling Smørgrav 236b15c8340SDag-Erling Smørgrav int 237b15c8340SDag-Erling Smørgrav pkcs11_init(int interactive) 238b15c8340SDag-Erling Smørgrav { 239535af610SEd Maste return 0; 240b15c8340SDag-Erling Smørgrav } 241b15c8340SDag-Erling Smørgrav 242b15c8340SDag-Erling Smørgrav void 243b15c8340SDag-Erling Smørgrav pkcs11_terminate(void) 244b15c8340SDag-Erling Smørgrav { 245535af610SEd Maste size_t i; 246535af610SEd Maste 247535af610SEd Maste debug3_f("terminating %zu helpers", nhelpers); 248535af610SEd Maste for (i = 0; i < nhelpers; i++) 249535af610SEd Maste helper_terminate(helpers[i]); 250b15c8340SDag-Erling Smørgrav } 251b15c8340SDag-Erling Smørgrav 252b15c8340SDag-Erling Smørgrav static int 25319261079SEd Maste rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding) 254b15c8340SDag-Erling Smørgrav { 25519261079SEd Maste struct sshkey *key = NULL; 25619261079SEd Maste struct sshbuf *msg = NULL; 25719261079SEd Maste u_char *blob = NULL, *signature = NULL; 258190cef3dSDag-Erling Smørgrav size_t blen, slen = 0; 259190cef3dSDag-Erling Smørgrav int r, ret = -1; 260535af610SEd Maste struct helper *helper; 261b15c8340SDag-Erling Smørgrav 262535af610SEd Maste if ((helper = helper_by_rsa(rsa)) == NULL || helper->fd == -1) 263535af610SEd Maste fatal_f("no helper for PKCS11 key"); 264535af610SEd Maste debug3_f("signing with PKCS11 provider %s", helper->path); 265b15c8340SDag-Erling Smørgrav if (padding != RSA_PKCS1_PADDING) 26619261079SEd Maste goto fail; 26719261079SEd Maste key = sshkey_new(KEY_UNSPEC); 26819261079SEd Maste if (key == NULL) { 26919261079SEd Maste error_f("sshkey_new failed"); 27019261079SEd Maste goto fail; 27119261079SEd Maste } 27219261079SEd Maste key->type = KEY_RSA; 27319261079SEd Maste RSA_up_ref(rsa); 27419261079SEd Maste key->rsa = rsa; 27519261079SEd Maste if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) { 27619261079SEd Maste error_fr(r, "encode key"); 27719261079SEd Maste goto fail; 278190cef3dSDag-Erling Smørgrav } 279190cef3dSDag-Erling Smørgrav if ((msg = sshbuf_new()) == NULL) 28019261079SEd Maste fatal_f("sshbuf_new failed"); 281190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || 282190cef3dSDag-Erling Smørgrav (r = sshbuf_put_string(msg, blob, blen)) != 0 || 283190cef3dSDag-Erling Smørgrav (r = sshbuf_put_string(msg, from, flen)) != 0 || 284190cef3dSDag-Erling Smørgrav (r = sshbuf_put_u32(msg, 0)) != 0) 28519261079SEd Maste fatal_fr(r, "compose"); 286535af610SEd Maste send_msg(helper->fd, msg); 287190cef3dSDag-Erling Smørgrav sshbuf_reset(msg); 288b15c8340SDag-Erling Smørgrav 289535af610SEd Maste if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) { 290190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0) 29119261079SEd Maste fatal_fr(r, "parse"); 292190cef3dSDag-Erling Smørgrav if (slen <= (size_t)RSA_size(rsa)) { 293b15c8340SDag-Erling Smørgrav memcpy(to, signature, slen); 294b15c8340SDag-Erling Smørgrav ret = slen; 295b15c8340SDag-Erling Smørgrav } 296e4a9863fSDag-Erling Smørgrav free(signature); 297b15c8340SDag-Erling Smørgrav } 29819261079SEd Maste fail: 29919261079SEd Maste free(blob); 30019261079SEd Maste sshkey_free(key); 301190cef3dSDag-Erling Smørgrav sshbuf_free(msg); 302b15c8340SDag-Erling Smørgrav return (ret); 303b15c8340SDag-Erling Smørgrav } 304b15c8340SDag-Erling Smørgrav 305535af610SEd Maste static int 306535af610SEd Maste rsa_finish(RSA *rsa) 307535af610SEd Maste { 308535af610SEd Maste struct helper *helper; 309535af610SEd Maste 310535af610SEd Maste if ((helper = helper_by_rsa(rsa)) == NULL) 311535af610SEd Maste fatal_f("no helper for PKCS11 key"); 312535af610SEd Maste debug3_f("free PKCS11 RSA key for provider %s", helper->path); 313535af610SEd Maste if (helper->rsa_finish != NULL) 314535af610SEd Maste helper->rsa_finish(rsa); 315535af610SEd Maste if (helper->nrsa == 0) 316535af610SEd Maste fatal_f("RSA refcount error"); 317535af610SEd Maste helper->nrsa--; 318535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 319535af610SEd Maste helper->path, helper->nrsa, helper->nec); 320535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 321535af610SEd Maste helper_terminate(helper); 322535af610SEd Maste return 1; 323535af610SEd Maste } 324535af610SEd Maste 3251323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 32619261079SEd Maste static ECDSA_SIG * 32719261079SEd Maste ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv, 32819261079SEd Maste const BIGNUM *rp, EC_KEY *ec) 329b15c8340SDag-Erling Smørgrav { 33019261079SEd Maste struct sshkey *key = NULL; 33119261079SEd Maste struct sshbuf *msg = NULL; 33219261079SEd Maste ECDSA_SIG *ret = NULL; 33319261079SEd Maste const u_char *cp; 33419261079SEd Maste u_char *blob = NULL, *signature = NULL; 33519261079SEd Maste size_t blen, slen = 0; 33619261079SEd Maste int r, nid; 337535af610SEd Maste struct helper *helper; 33819261079SEd Maste 339535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL || helper->fd == -1) 340535af610SEd Maste fatal_f("no helper for PKCS11 key"); 341535af610SEd Maste debug3_f("signing with PKCS11 provider %s", helper->path); 34219261079SEd Maste nid = sshkey_ecdsa_key_to_nid(ec); 34319261079SEd Maste if (nid < 0) { 34419261079SEd Maste error_f("couldn't get curve nid"); 34519261079SEd Maste goto fail; 34619261079SEd Maste } 34719261079SEd Maste 34819261079SEd Maste key = sshkey_new(KEY_UNSPEC); 34919261079SEd Maste if (key == NULL) { 35019261079SEd Maste error_f("sshkey_new failed"); 35119261079SEd Maste goto fail; 35219261079SEd Maste } 35319261079SEd Maste key->ecdsa = ec; 35419261079SEd Maste key->ecdsa_nid = nid; 35519261079SEd Maste key->type = KEY_ECDSA; 35619261079SEd Maste EC_KEY_up_ref(ec); 35719261079SEd Maste 35819261079SEd Maste if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) { 35919261079SEd Maste error_fr(r, "encode key"); 36019261079SEd Maste goto fail; 36119261079SEd Maste } 36219261079SEd Maste if ((msg = sshbuf_new()) == NULL) 36319261079SEd Maste fatal_f("sshbuf_new failed"); 36419261079SEd Maste if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || 36519261079SEd Maste (r = sshbuf_put_string(msg, blob, blen)) != 0 || 36619261079SEd Maste (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 || 36719261079SEd Maste (r = sshbuf_put_u32(msg, 0)) != 0) 36819261079SEd Maste fatal_fr(r, "compose"); 369535af610SEd Maste send_msg(helper->fd, msg); 37019261079SEd Maste sshbuf_reset(msg); 37119261079SEd Maste 372535af610SEd Maste if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) { 37319261079SEd Maste if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0) 37419261079SEd Maste fatal_fr(r, "parse"); 37519261079SEd Maste cp = signature; 37619261079SEd Maste ret = d2i_ECDSA_SIG(NULL, &cp, slen); 37719261079SEd Maste free(signature); 37819261079SEd Maste } 37919261079SEd Maste 38019261079SEd Maste fail: 38119261079SEd Maste free(blob); 38219261079SEd Maste sshkey_free(key); 38319261079SEd Maste sshbuf_free(msg); 38419261079SEd Maste return (ret); 38519261079SEd Maste } 38619261079SEd Maste 387535af610SEd Maste static void 388535af610SEd Maste ecdsa_do_finish(EC_KEY *ec) 389535af610SEd Maste { 390535af610SEd Maste struct helper *helper; 391535af610SEd Maste 392535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL) 393535af610SEd Maste fatal_f("no helper for PKCS11 key"); 394535af610SEd Maste debug3_f("free PKCS11 ECDSA key for provider %s", helper->path); 395535af610SEd Maste if (helper->ec_finish != NULL) 396535af610SEd Maste helper->ec_finish(ec); 397535af610SEd Maste if (helper->nec == 0) 398535af610SEd Maste fatal_f("ECDSA refcount error"); 399535af610SEd Maste helper->nec--; 400535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 401535af610SEd Maste helper->path, helper->nrsa, helper->nec); 402535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 403535af610SEd Maste helper_terminate(helper); 404535af610SEd Maste } 405535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */ 40619261079SEd Maste 40719261079SEd Maste /* redirect private key crypto operations to the ssh-pkcs11-helper */ 40819261079SEd Maste static void 409535af610SEd Maste wrap_key(struct helper *helper, struct sshkey *k) 41019261079SEd Maste { 411535af610SEd Maste debug3_f("wrap %s for provider %s", sshkey_type(k), helper->path); 412535af610SEd Maste if (k->type == KEY_RSA) { 413535af610SEd Maste RSA_set_method(k->rsa, helper->rsa_meth); 414535af610SEd Maste if (helper->nrsa++ >= INT_MAX) 415535af610SEd Maste fatal_f("RSA refcount error"); 4161323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 417535af610SEd Maste } else if (k->type == KEY_ECDSA) { 418535af610SEd Maste EC_KEY_set_method(k->ecdsa, helper->ec_meth); 419535af610SEd Maste if (helper->nec++ >= INT_MAX) 420535af610SEd Maste fatal_f("EC refcount error"); 421535af610SEd Maste #endif 422535af610SEd Maste } else 42319261079SEd Maste fatal_f("unknown key type"); 424535af610SEd Maste k->flags |= SSHKEY_FLAG_EXT; 425535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 426535af610SEd Maste helper->path, helper->nrsa, helper->nec); 42719261079SEd Maste } 42819261079SEd Maste 429*069ac184SEd Maste /* 430*069ac184SEd Maste * Make a private PKCS#11-backed certificate by grafting a previously-loaded 431*069ac184SEd Maste * PKCS#11 private key and a public certificate key. 432*069ac184SEd Maste */ 433*069ac184SEd Maste int 434*069ac184SEd Maste pkcs11_make_cert(const struct sshkey *priv, 435*069ac184SEd Maste const struct sshkey *certpub, struct sshkey **certprivp) 436*069ac184SEd Maste { 437*069ac184SEd Maste struct helper *helper = NULL; 438*069ac184SEd Maste struct sshkey *ret; 439*069ac184SEd Maste int r; 440*069ac184SEd Maste 441*069ac184SEd Maste debug3_f("private key type %s cert type %s", sshkey_type(priv), 442*069ac184SEd Maste sshkey_type(certpub)); 443*069ac184SEd Maste *certprivp = NULL; 444*069ac184SEd Maste if (!sshkey_is_cert(certpub) || sshkey_is_cert(priv) || 445*069ac184SEd Maste !sshkey_equal_public(priv, certpub)) { 446*069ac184SEd Maste error_f("private key %s doesn't match cert %s", 447*069ac184SEd Maste sshkey_type(priv), sshkey_type(certpub)); 448*069ac184SEd Maste return SSH_ERR_INVALID_ARGUMENT; 449*069ac184SEd Maste } 450*069ac184SEd Maste *certprivp = NULL; 451*069ac184SEd Maste if (priv->type == KEY_RSA) { 452*069ac184SEd Maste if ((helper = helper_by_rsa(priv->rsa)) == NULL || 453*069ac184SEd Maste helper->fd == -1) 454*069ac184SEd Maste fatal_f("no helper for PKCS11 RSA key"); 455*069ac184SEd Maste if ((r = sshkey_from_private(priv, &ret)) != 0) 456*069ac184SEd Maste fatal_fr(r, "copy key"); 457*069ac184SEd Maste RSA_set_method(ret->rsa, helper->rsa_meth); 458*069ac184SEd Maste if (helper->nrsa++ >= INT_MAX) 459*069ac184SEd Maste fatal_f("RSA refcount error"); 460*069ac184SEd Maste } else if (priv->type == KEY_ECDSA) { 461*069ac184SEd Maste if ((helper = helper_by_ec(priv->ecdsa)) == NULL || 462*069ac184SEd Maste helper->fd == -1) 463*069ac184SEd Maste fatal_f("no helper for PKCS11 EC key"); 464*069ac184SEd Maste if ((r = sshkey_from_private(priv, &ret)) != 0) 465*069ac184SEd Maste fatal_fr(r, "copy key"); 466*069ac184SEd Maste EC_KEY_set_method(ret->ecdsa, helper->ec_meth); 467*069ac184SEd Maste if (helper->nec++ >= INT_MAX) 468*069ac184SEd Maste fatal_f("EC refcount error"); 469*069ac184SEd Maste } else 470*069ac184SEd Maste fatal_f("unknown key type %s", sshkey_type(priv)); 471*069ac184SEd Maste 472*069ac184SEd Maste ret->flags |= SSHKEY_FLAG_EXT; 473*069ac184SEd Maste if ((r = sshkey_to_certified(ret)) != 0 || 474*069ac184SEd Maste (r = sshkey_cert_copy(certpub, ret)) != 0) 475*069ac184SEd Maste fatal_fr(r, "graft certificate"); 476*069ac184SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 477*069ac184SEd Maste helper->path, helper->nrsa, helper->nec); 478*069ac184SEd Maste /* success */ 479*069ac184SEd Maste *certprivp = ret; 480*069ac184SEd Maste return 0; 481*069ac184SEd Maste } 482*069ac184SEd Maste 48319261079SEd Maste static int 484535af610SEd Maste pkcs11_start_helper_methods(struct helper *helper) 48519261079SEd Maste { 486535af610SEd Maste RSA_METHOD *rsa_meth; 487535af610SEd Maste EC_KEY_METHOD *ec_meth = NULL; 4881323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 489535af610SEd Maste int (*ec_init)(EC_KEY *key); 490535af610SEd Maste int (*ec_copy)(EC_KEY *dest, const EC_KEY *src); 491535af610SEd Maste int (*ec_set_group)(EC_KEY *key, const EC_GROUP *grp); 492535af610SEd Maste int (*ec_set_private)(EC_KEY *key, const BIGNUM *priv_key); 493535af610SEd Maste int (*ec_set_public)(EC_KEY *key, const EC_POINT *pub_key); 494535af610SEd Maste int (*ec_sign)(int, const unsigned char *, int, unsigned char *, 49519261079SEd Maste unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL; 496b15c8340SDag-Erling Smørgrav 497535af610SEd Maste if ((ec_meth = EC_KEY_METHOD_new(EC_KEY_OpenSSL())) == NULL) 498535af610SEd Maste return -1; 499535af610SEd Maste EC_KEY_METHOD_get_sign(ec_meth, &ec_sign, NULL, NULL); 500535af610SEd Maste EC_KEY_METHOD_set_sign(ec_meth, ec_sign, NULL, ecdsa_do_sign); 501535af610SEd Maste EC_KEY_METHOD_get_init(ec_meth, &ec_init, &helper->ec_finish, 502535af610SEd Maste &ec_copy, &ec_set_group, &ec_set_private, &ec_set_public); 503535af610SEd Maste EC_KEY_METHOD_set_init(ec_meth, ec_init, ecdsa_do_finish, 504535af610SEd Maste ec_copy, ec_set_group, ec_set_private, ec_set_public); 505535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */ 506535af610SEd Maste 507535af610SEd Maste if ((rsa_meth = RSA_meth_dup(RSA_get_default_method())) == NULL) 50819261079SEd Maste fatal_f("RSA_meth_dup failed"); 509535af610SEd Maste helper->rsa_finish = RSA_meth_get_finish(rsa_meth); 510535af610SEd Maste if (!RSA_meth_set1_name(rsa_meth, "ssh-pkcs11-helper") || 511535af610SEd Maste !RSA_meth_set_priv_enc(rsa_meth, rsa_encrypt) || 512535af610SEd Maste !RSA_meth_set_finish(rsa_meth, rsa_finish)) 51319261079SEd Maste fatal_f("failed to prepare method"); 51419261079SEd Maste 515535af610SEd Maste helper->ec_meth = ec_meth; 516535af610SEd Maste helper->rsa_meth = rsa_meth; 517535af610SEd Maste return 0; 518b15c8340SDag-Erling Smørgrav } 519b15c8340SDag-Erling Smørgrav 520535af610SEd Maste static struct helper * 521535af610SEd Maste pkcs11_start_helper(const char *path) 522b15c8340SDag-Erling Smørgrav { 523b15c8340SDag-Erling Smørgrav int pair[2]; 524535af610SEd Maste char *prog, *verbosity = NULL; 525535af610SEd Maste struct helper *helper; 526535af610SEd Maste pid_t pid; 52719261079SEd Maste 528535af610SEd Maste if (nhelpers >= INT_MAX) 529535af610SEd Maste fatal_f("too many helpers"); 530535af610SEd Maste debug3_f("start helper for %s", path); 531b15c8340SDag-Erling Smørgrav if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { 532535af610SEd Maste error_f("socketpair: %s", strerror(errno)); 533535af610SEd Maste return NULL; 534535af610SEd Maste } 535535af610SEd Maste helper = xcalloc(1, sizeof(*helper)); 536535af610SEd Maste if (pkcs11_start_helper_methods(helper) == -1) { 537535af610SEd Maste error_f("pkcs11_start_helper_methods failed"); 538535af610SEd Maste goto fail; 539b15c8340SDag-Erling Smørgrav } 540b15c8340SDag-Erling Smørgrav if ((pid = fork()) == -1) { 541535af610SEd Maste error_f("fork: %s", strerror(errno)); 542535af610SEd Maste fail: 543535af610SEd Maste close(pair[0]); 544535af610SEd Maste close(pair[1]); 545535af610SEd Maste RSA_meth_free(helper->rsa_meth); 546535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 547535af610SEd Maste EC_KEY_METHOD_free(helper->ec_meth); 548535af610SEd Maste #endif 549535af610SEd Maste free(helper); 550535af610SEd Maste return NULL; 551b15c8340SDag-Erling Smørgrav } else if (pid == 0) { 552b15c8340SDag-Erling Smørgrav if ((dup2(pair[1], STDIN_FILENO) == -1) || 553b15c8340SDag-Erling Smørgrav (dup2(pair[1], STDOUT_FILENO) == -1)) { 554b15c8340SDag-Erling Smørgrav fprintf(stderr, "dup2: %s\n", strerror(errno)); 555b15c8340SDag-Erling Smørgrav _exit(1); 556b15c8340SDag-Erling Smørgrav } 557b15c8340SDag-Erling Smørgrav close(pair[0]); 558b15c8340SDag-Erling Smørgrav close(pair[1]); 559535af610SEd Maste prog = getenv("SSH_PKCS11_HELPER"); 560535af610SEd Maste if (prog == NULL || strlen(prog) == 0) 561535af610SEd Maste prog = _PATH_SSH_PKCS11_HELPER; 562535af610SEd Maste if (log_level_get() >= SYSLOG_LEVEL_DEBUG1) 563535af610SEd Maste verbosity = "-vvv"; 564535af610SEd Maste debug_f("starting %s %s", prog, 56519261079SEd Maste verbosity == NULL ? "" : verbosity); 566535af610SEd Maste execlp(prog, prog, verbosity, (char *)NULL); 567535af610SEd Maste fprintf(stderr, "exec: %s: %s\n", prog, strerror(errno)); 568b15c8340SDag-Erling Smørgrav _exit(1); 569b15c8340SDag-Erling Smørgrav } 570b15c8340SDag-Erling Smørgrav close(pair[1]); 571535af610SEd Maste helper->fd = pair[0]; 572535af610SEd Maste helper->path = xstrdup(path); 573535af610SEd Maste helper->pid = pid; 574535af610SEd Maste debug3_f("helper %zu for \"%s\" on fd %d pid %ld", nhelpers, 575535af610SEd Maste helper->path, helper->fd, (long)helper->pid); 576535af610SEd Maste helpers = xrecallocarray(helpers, nhelpers, 577535af610SEd Maste nhelpers + 1, sizeof(*helpers)); 578535af610SEd Maste helpers[nhelpers++] = helper; 579535af610SEd Maste return helper; 580b15c8340SDag-Erling Smørgrav } 581b15c8340SDag-Erling Smørgrav 582b15c8340SDag-Erling Smørgrav int 58319261079SEd Maste pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp, 58419261079SEd Maste char ***labelsp) 585b15c8340SDag-Erling Smørgrav { 5864f52dfbbSDag-Erling Smørgrav struct sshkey *k; 58719261079SEd Maste int r, type; 588b15c8340SDag-Erling Smørgrav u_char *blob; 58919261079SEd Maste char *label; 590190cef3dSDag-Erling Smørgrav size_t blen; 591190cef3dSDag-Erling Smørgrav u_int nkeys, i; 592190cef3dSDag-Erling Smørgrav struct sshbuf *msg; 593535af610SEd Maste struct helper *helper; 594b15c8340SDag-Erling Smørgrav 595535af610SEd Maste if ((helper = helper_by_provider(name)) == NULL && 596535af610SEd Maste (helper = pkcs11_start_helper(name)) == NULL) 597535af610SEd Maste return -1; 598b15c8340SDag-Erling Smørgrav 599190cef3dSDag-Erling Smørgrav if ((msg = sshbuf_new()) == NULL) 60019261079SEd Maste fatal_f("sshbuf_new failed"); 601190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 || 602190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, name)) != 0 || 603190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, pin)) != 0) 60419261079SEd Maste fatal_fr(r, "compose"); 605535af610SEd Maste send_msg(helper->fd, msg); 606190cef3dSDag-Erling Smørgrav sshbuf_reset(msg); 607b15c8340SDag-Erling Smørgrav 608535af610SEd Maste type = recv_msg(helper->fd, msg); 60919261079SEd Maste if (type == SSH2_AGENT_IDENTITIES_ANSWER) { 610190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) 61119261079SEd Maste fatal_fr(r, "parse nkeys"); 612190cef3dSDag-Erling Smørgrav *keysp = xcalloc(nkeys, sizeof(struct sshkey *)); 61319261079SEd Maste if (labelsp) 61419261079SEd Maste *labelsp = xcalloc(nkeys, sizeof(char *)); 615b15c8340SDag-Erling Smørgrav for (i = 0; i < nkeys; i++) { 616190cef3dSDag-Erling Smørgrav /* XXX clean up properly instead of fatal() */ 617190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 || 61819261079SEd Maste (r = sshbuf_get_cstring(msg, &label, NULL)) != 0) 61919261079SEd Maste fatal_fr(r, "parse key"); 620190cef3dSDag-Erling Smørgrav if ((r = sshkey_from_blob(blob, blen, &k)) != 0) 62119261079SEd Maste fatal_fr(r, "decode key"); 622535af610SEd Maste wrap_key(helper, k); 623b15c8340SDag-Erling Smørgrav (*keysp)[i] = k; 62419261079SEd Maste if (labelsp) 62519261079SEd Maste (*labelsp)[i] = label; 62619261079SEd Maste else 62719261079SEd Maste free(label); 628e4a9863fSDag-Erling Smørgrav free(blob); 629b15c8340SDag-Erling Smørgrav } 63019261079SEd Maste } else if (type == SSH2_AGENT_FAILURE) { 63119261079SEd Maste if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) 63219261079SEd Maste nkeys = -1; 633b15c8340SDag-Erling Smørgrav } else { 634b15c8340SDag-Erling Smørgrav nkeys = -1; 635b15c8340SDag-Erling Smørgrav } 636190cef3dSDag-Erling Smørgrav sshbuf_free(msg); 637b15c8340SDag-Erling Smørgrav return (nkeys); 638b15c8340SDag-Erling Smørgrav } 639b15c8340SDag-Erling Smørgrav 640b15c8340SDag-Erling Smørgrav int 641b15c8340SDag-Erling Smørgrav pkcs11_del_provider(char *name) 642b15c8340SDag-Erling Smørgrav { 643535af610SEd Maste struct helper *helper; 644b15c8340SDag-Erling Smørgrav 645535af610SEd Maste /* 646535af610SEd Maste * ssh-agent deletes keys before calling this, so the helper entry 647535af610SEd Maste * should be gone before we get here. 648535af610SEd Maste */ 649535af610SEd Maste debug3_f("delete %s", name); 650535af610SEd Maste if ((helper = helper_by_provider(name)) != NULL) 651535af610SEd Maste helper_terminate(helper); 652535af610SEd Maste return 0; 653b15c8340SDag-Erling Smørgrav } 654b15c8340SDag-Erling Smørgrav #endif /* ENABLE_PKCS11 */ 655