1*535af610SEd Maste /* $OpenBSD: ssh-pkcs11-client.c,v 1.18 2023/07/19 14:03:45 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> 33*535af610SEd 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 49*535af610SEd Maste #include "openbsd-compat/openssl-compat.h" 50*535af610SEd Maste 51*535af610SEd Maste #if !defined(OPENSSL_HAS_ECC) || !defined(HAVE_EC_KEY_METHOD_NEW) 52*535af610SEd Maste #define EC_KEY_METHOD void 53*535af610SEd Maste #define EC_KEY void 54*535af610SEd Maste #endif 55*535af610SEd Maste 56b15c8340SDag-Erling Smørgrav /* borrows code from sftp-server and ssh-agent */ 57b15c8340SDag-Erling Smørgrav 58*535af610SEd Maste /* 59*535af610SEd Maste * Maintain a list of ssh-pkcs11-helper subprocesses. These may be looked up 60*535af610SEd Maste * by provider path or their unique EC/RSA METHOD pointers. 61*535af610SEd Maste */ 62*535af610SEd Maste struct helper { 63*535af610SEd Maste char *path; 64*535af610SEd Maste pid_t pid; 65*535af610SEd Maste int fd; 66*535af610SEd Maste RSA_METHOD *rsa_meth; 67*535af610SEd Maste EC_KEY_METHOD *ec_meth; 68*535af610SEd Maste int (*rsa_finish)(RSA *rsa); 69*535af610SEd Maste void (*ec_finish)(EC_KEY *key); 70*535af610SEd Maste size_t nrsa, nec; /* number of active keys of each type */ 71*535af610SEd Maste }; 72*535af610SEd Maste static struct helper **helpers; 73*535af610SEd Maste static size_t nhelpers; 74*535af610SEd Maste 75*535af610SEd Maste static struct helper * 76*535af610SEd Maste helper_by_provider(const char *path) 77*535af610SEd Maste { 78*535af610SEd Maste size_t i; 79*535af610SEd Maste 80*535af610SEd Maste for (i = 0; i < nhelpers; i++) { 81*535af610SEd Maste if (helpers[i] == NULL || helpers[i]->path == NULL || 82*535af610SEd Maste helpers[i]->fd == -1) 83*535af610SEd Maste continue; 84*535af610SEd Maste if (strcmp(helpers[i]->path, path) == 0) 85*535af610SEd Maste return helpers[i]; 86*535af610SEd Maste } 87*535af610SEd Maste return NULL; 88*535af610SEd Maste } 89*535af610SEd Maste 90*535af610SEd Maste static struct helper * 91*535af610SEd Maste helper_by_rsa(const RSA *rsa) 92*535af610SEd Maste { 93*535af610SEd Maste size_t i; 94*535af610SEd Maste const RSA_METHOD *meth; 95*535af610SEd Maste 96*535af610SEd Maste if ((meth = RSA_get_method(rsa)) == NULL) 97*535af610SEd Maste return NULL; 98*535af610SEd Maste for (i = 0; i < nhelpers; i++) { 99*535af610SEd Maste if (helpers[i] != NULL && helpers[i]->rsa_meth == meth) 100*535af610SEd Maste return helpers[i]; 101*535af610SEd Maste } 102*535af610SEd Maste return NULL; 103*535af610SEd Maste 104*535af610SEd Maste } 105*535af610SEd Maste 106*535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 107*535af610SEd Maste static struct helper * 108*535af610SEd Maste helper_by_ec(const EC_KEY *ec) 109*535af610SEd Maste { 110*535af610SEd Maste size_t i; 111*535af610SEd Maste const EC_KEY_METHOD *meth; 112*535af610SEd Maste 113*535af610SEd Maste if ((meth = EC_KEY_get_method(ec)) == NULL) 114*535af610SEd Maste return NULL; 115*535af610SEd Maste for (i = 0; i < nhelpers; i++) { 116*535af610SEd Maste if (helpers[i] != NULL && helpers[i]->ec_meth == meth) 117*535af610SEd Maste return helpers[i]; 118*535af610SEd Maste } 119*535af610SEd Maste return NULL; 120*535af610SEd Maste 121*535af610SEd Maste } 122*535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */ 123b15c8340SDag-Erling Smørgrav 124b15c8340SDag-Erling Smørgrav static void 125*535af610SEd Maste helper_free(struct helper *helper) 126*535af610SEd Maste { 127*535af610SEd Maste size_t i; 128*535af610SEd Maste int found = 0; 129*535af610SEd Maste 130*535af610SEd Maste if (helper == NULL) 131*535af610SEd Maste return; 132*535af610SEd Maste if (helper->path == NULL || helper->ec_meth == NULL || 133*535af610SEd Maste helper->rsa_meth == NULL) 134*535af610SEd Maste fatal_f("inconsistent helper"); 135*535af610SEd Maste debug3_f("free helper for provider %s", helper->path); 136*535af610SEd Maste for (i = 0; i < nhelpers; i++) { 137*535af610SEd Maste if (helpers[i] == helper) { 138*535af610SEd Maste if (found) 139*535af610SEd Maste fatal_f("helper recorded more than once"); 140*535af610SEd Maste found = 1; 141*535af610SEd Maste } 142*535af610SEd Maste else if (found) 143*535af610SEd Maste helpers[i - 1] = helpers[i]; 144*535af610SEd Maste } 145*535af610SEd Maste if (found) { 146*535af610SEd Maste helpers = xrecallocarray(helpers, nhelpers, 147*535af610SEd Maste nhelpers - 1, sizeof(*helpers)); 148*535af610SEd Maste nhelpers--; 149*535af610SEd Maste } 150*535af610SEd Maste free(helper->path); 151*535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 152*535af610SEd Maste EC_KEY_METHOD_free(helper->ec_meth); 153*535af610SEd Maste #endif 154*535af610SEd Maste RSA_meth_free(helper->rsa_meth); 155*535af610SEd Maste free(helper); 156*535af610SEd Maste } 157*535af610SEd Maste 158*535af610SEd Maste static void 159*535af610SEd Maste helper_terminate(struct helper *helper) 160*535af610SEd Maste { 161*535af610SEd Maste if (helper == NULL) { 162*535af610SEd Maste return; 163*535af610SEd Maste } else if (helper->fd == -1) { 164*535af610SEd Maste debug3_f("already terminated"); 165*535af610SEd Maste } else { 166*535af610SEd Maste debug3_f("terminating helper for %s; " 167*535af610SEd Maste "remaining %zu RSA %zu ECDSA", 168*535af610SEd Maste helper->path, helper->nrsa, helper->nec); 169*535af610SEd Maste close(helper->fd); 170*535af610SEd Maste /* XXX waitpid() */ 171*535af610SEd Maste helper->fd = -1; 172*535af610SEd Maste helper->pid = -1; 173*535af610SEd Maste } 174*535af610SEd Maste /* 175*535af610SEd Maste * Don't delete the helper entry until there are no remaining keys 176*535af610SEd Maste * that reference it. Otherwise, any signing operation would call 177*535af610SEd Maste * a free'd METHOD pointer and that would be bad. 178*535af610SEd Maste */ 179*535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 180*535af610SEd Maste helper_free(helper); 181*535af610SEd Maste } 182*535af610SEd Maste 183*535af610SEd Maste static void 184*535af610SEd 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 190*535af610SEd Maste if (fd == -1) 191*535af610SEd 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 202*535af610SEd 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 208*535af610SEd Maste sshbuf_reset(m); 209*535af610SEd Maste if (fd == -1) 210*535af610SEd 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 { 239*535af610SEd 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 { 245*535af610SEd Maste size_t i; 246*535af610SEd Maste 247*535af610SEd Maste debug3_f("terminating %zu helpers", nhelpers); 248*535af610SEd Maste for (i = 0; i < nhelpers; i++) 249*535af610SEd 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; 260*535af610SEd Maste struct helper *helper; 261b15c8340SDag-Erling Smørgrav 262*535af610SEd Maste if ((helper = helper_by_rsa(rsa)) == NULL || helper->fd == -1) 263*535af610SEd Maste fatal_f("no helper for PKCS11 key"); 264*535af610SEd 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"); 286*535af610SEd Maste send_msg(helper->fd, msg); 287190cef3dSDag-Erling Smørgrav sshbuf_reset(msg); 288b15c8340SDag-Erling Smørgrav 289*535af610SEd 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 305*535af610SEd Maste static int 306*535af610SEd Maste rsa_finish(RSA *rsa) 307*535af610SEd Maste { 308*535af610SEd Maste struct helper *helper; 309*535af610SEd Maste 310*535af610SEd Maste if ((helper = helper_by_rsa(rsa)) == NULL) 311*535af610SEd Maste fatal_f("no helper for PKCS11 key"); 312*535af610SEd Maste debug3_f("free PKCS11 RSA key for provider %s", helper->path); 313*535af610SEd Maste if (helper->rsa_finish != NULL) 314*535af610SEd Maste helper->rsa_finish(rsa); 315*535af610SEd Maste if (helper->nrsa == 0) 316*535af610SEd Maste fatal_f("RSA refcount error"); 317*535af610SEd Maste helper->nrsa--; 318*535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 319*535af610SEd Maste helper->path, helper->nrsa, helper->nec); 320*535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 321*535af610SEd Maste helper_terminate(helper); 322*535af610SEd Maste return 1; 323*535af610SEd Maste } 324*535af610SEd 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; 337*535af610SEd Maste struct helper *helper; 33819261079SEd Maste 339*535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL || helper->fd == -1) 340*535af610SEd Maste fatal_f("no helper for PKCS11 key"); 341*535af610SEd 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"); 369*535af610SEd Maste send_msg(helper->fd, msg); 37019261079SEd Maste sshbuf_reset(msg); 37119261079SEd Maste 372*535af610SEd 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 387*535af610SEd Maste static void 388*535af610SEd Maste ecdsa_do_finish(EC_KEY *ec) 389*535af610SEd Maste { 390*535af610SEd Maste struct helper *helper; 391*535af610SEd Maste 392*535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL) 393*535af610SEd Maste fatal_f("no helper for PKCS11 key"); 394*535af610SEd Maste debug3_f("free PKCS11 ECDSA key for provider %s", helper->path); 395*535af610SEd Maste if (helper->ec_finish != NULL) 396*535af610SEd Maste helper->ec_finish(ec); 397*535af610SEd Maste if (helper->nec == 0) 398*535af610SEd Maste fatal_f("ECDSA refcount error"); 399*535af610SEd Maste helper->nec--; 400*535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 401*535af610SEd Maste helper->path, helper->nrsa, helper->nec); 402*535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0) 403*535af610SEd Maste helper_terminate(helper); 404*535af610SEd Maste } 405*535af610SEd 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 409*535af610SEd Maste wrap_key(struct helper *helper, struct sshkey *k) 41019261079SEd Maste { 411*535af610SEd Maste debug3_f("wrap %s for provider %s", sshkey_type(k), helper->path); 412*535af610SEd Maste if (k->type == KEY_RSA) { 413*535af610SEd Maste RSA_set_method(k->rsa, helper->rsa_meth); 414*535af610SEd Maste if (helper->nrsa++ >= INT_MAX) 415*535af610SEd Maste fatal_f("RSA refcount error"); 4161323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 417*535af610SEd Maste } else if (k->type == KEY_ECDSA) { 418*535af610SEd Maste EC_KEY_set_method(k->ecdsa, helper->ec_meth); 419*535af610SEd Maste if (helper->nec++ >= INT_MAX) 420*535af610SEd Maste fatal_f("EC refcount error"); 421*535af610SEd Maste #endif 422*535af610SEd Maste } else 42319261079SEd Maste fatal_f("unknown key type"); 424*535af610SEd Maste k->flags |= SSHKEY_FLAG_EXT; 425*535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA", 426*535af610SEd Maste helper->path, helper->nrsa, helper->nec); 42719261079SEd Maste } 42819261079SEd Maste 42919261079SEd Maste static int 430*535af610SEd Maste pkcs11_start_helper_methods(struct helper *helper) 43119261079SEd Maste { 432*535af610SEd Maste RSA_METHOD *rsa_meth; 433*535af610SEd Maste EC_KEY_METHOD *ec_meth = NULL; 4341323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 435*535af610SEd Maste int (*ec_init)(EC_KEY *key); 436*535af610SEd Maste int (*ec_copy)(EC_KEY *dest, const EC_KEY *src); 437*535af610SEd Maste int (*ec_set_group)(EC_KEY *key, const EC_GROUP *grp); 438*535af610SEd Maste int (*ec_set_private)(EC_KEY *key, const BIGNUM *priv_key); 439*535af610SEd Maste int (*ec_set_public)(EC_KEY *key, const EC_POINT *pub_key); 440*535af610SEd Maste int (*ec_sign)(int, const unsigned char *, int, unsigned char *, 44119261079SEd Maste unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL; 442b15c8340SDag-Erling Smørgrav 443*535af610SEd Maste if ((ec_meth = EC_KEY_METHOD_new(EC_KEY_OpenSSL())) == NULL) 444*535af610SEd Maste return -1; 445*535af610SEd Maste EC_KEY_METHOD_get_sign(ec_meth, &ec_sign, NULL, NULL); 446*535af610SEd Maste EC_KEY_METHOD_set_sign(ec_meth, ec_sign, NULL, ecdsa_do_sign); 447*535af610SEd Maste EC_KEY_METHOD_get_init(ec_meth, &ec_init, &helper->ec_finish, 448*535af610SEd Maste &ec_copy, &ec_set_group, &ec_set_private, &ec_set_public); 449*535af610SEd Maste EC_KEY_METHOD_set_init(ec_meth, ec_init, ecdsa_do_finish, 450*535af610SEd Maste ec_copy, ec_set_group, ec_set_private, ec_set_public); 451*535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */ 452*535af610SEd Maste 453*535af610SEd Maste if ((rsa_meth = RSA_meth_dup(RSA_get_default_method())) == NULL) 45419261079SEd Maste fatal_f("RSA_meth_dup failed"); 455*535af610SEd Maste helper->rsa_finish = RSA_meth_get_finish(rsa_meth); 456*535af610SEd Maste if (!RSA_meth_set1_name(rsa_meth, "ssh-pkcs11-helper") || 457*535af610SEd Maste !RSA_meth_set_priv_enc(rsa_meth, rsa_encrypt) || 458*535af610SEd Maste !RSA_meth_set_finish(rsa_meth, rsa_finish)) 45919261079SEd Maste fatal_f("failed to prepare method"); 46019261079SEd Maste 461*535af610SEd Maste helper->ec_meth = ec_meth; 462*535af610SEd Maste helper->rsa_meth = rsa_meth; 463*535af610SEd Maste return 0; 464b15c8340SDag-Erling Smørgrav } 465b15c8340SDag-Erling Smørgrav 466*535af610SEd Maste static struct helper * 467*535af610SEd Maste pkcs11_start_helper(const char *path) 468b15c8340SDag-Erling Smørgrav { 469b15c8340SDag-Erling Smørgrav int pair[2]; 470*535af610SEd Maste char *prog, *verbosity = NULL; 471*535af610SEd Maste struct helper *helper; 472*535af610SEd Maste pid_t pid; 47319261079SEd Maste 474*535af610SEd Maste if (nhelpers >= INT_MAX) 475*535af610SEd Maste fatal_f("too many helpers"); 476*535af610SEd Maste debug3_f("start helper for %s", path); 477b15c8340SDag-Erling Smørgrav if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) { 478*535af610SEd Maste error_f("socketpair: %s", strerror(errno)); 479*535af610SEd Maste return NULL; 480*535af610SEd Maste } 481*535af610SEd Maste helper = xcalloc(1, sizeof(*helper)); 482*535af610SEd Maste if (pkcs11_start_helper_methods(helper) == -1) { 483*535af610SEd Maste error_f("pkcs11_start_helper_methods failed"); 484*535af610SEd Maste goto fail; 485b15c8340SDag-Erling Smørgrav } 486b15c8340SDag-Erling Smørgrav if ((pid = fork()) == -1) { 487*535af610SEd Maste error_f("fork: %s", strerror(errno)); 488*535af610SEd Maste fail: 489*535af610SEd Maste close(pair[0]); 490*535af610SEd Maste close(pair[1]); 491*535af610SEd Maste RSA_meth_free(helper->rsa_meth); 492*535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) 493*535af610SEd Maste EC_KEY_METHOD_free(helper->ec_meth); 494*535af610SEd Maste #endif 495*535af610SEd Maste free(helper); 496*535af610SEd Maste return NULL; 497b15c8340SDag-Erling Smørgrav } else if (pid == 0) { 498b15c8340SDag-Erling Smørgrav if ((dup2(pair[1], STDIN_FILENO) == -1) || 499b15c8340SDag-Erling Smørgrav (dup2(pair[1], STDOUT_FILENO) == -1)) { 500b15c8340SDag-Erling Smørgrav fprintf(stderr, "dup2: %s\n", strerror(errno)); 501b15c8340SDag-Erling Smørgrav _exit(1); 502b15c8340SDag-Erling Smørgrav } 503b15c8340SDag-Erling Smørgrav close(pair[0]); 504b15c8340SDag-Erling Smørgrav close(pair[1]); 505*535af610SEd Maste prog = getenv("SSH_PKCS11_HELPER"); 506*535af610SEd Maste if (prog == NULL || strlen(prog) == 0) 507*535af610SEd Maste prog = _PATH_SSH_PKCS11_HELPER; 508*535af610SEd Maste if (log_level_get() >= SYSLOG_LEVEL_DEBUG1) 509*535af610SEd Maste verbosity = "-vvv"; 510*535af610SEd Maste debug_f("starting %s %s", prog, 51119261079SEd Maste verbosity == NULL ? "" : verbosity); 512*535af610SEd Maste execlp(prog, prog, verbosity, (char *)NULL); 513*535af610SEd Maste fprintf(stderr, "exec: %s: %s\n", prog, strerror(errno)); 514b15c8340SDag-Erling Smørgrav _exit(1); 515b15c8340SDag-Erling Smørgrav } 516b15c8340SDag-Erling Smørgrav close(pair[1]); 517*535af610SEd Maste helper->fd = pair[0]; 518*535af610SEd Maste helper->path = xstrdup(path); 519*535af610SEd Maste helper->pid = pid; 520*535af610SEd Maste debug3_f("helper %zu for \"%s\" on fd %d pid %ld", nhelpers, 521*535af610SEd Maste helper->path, helper->fd, (long)helper->pid); 522*535af610SEd Maste helpers = xrecallocarray(helpers, nhelpers, 523*535af610SEd Maste nhelpers + 1, sizeof(*helpers)); 524*535af610SEd Maste helpers[nhelpers++] = helper; 525*535af610SEd Maste return helper; 526b15c8340SDag-Erling Smørgrav } 527b15c8340SDag-Erling Smørgrav 528b15c8340SDag-Erling Smørgrav int 52919261079SEd Maste pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp, 53019261079SEd Maste char ***labelsp) 531b15c8340SDag-Erling Smørgrav { 5324f52dfbbSDag-Erling Smørgrav struct sshkey *k; 53319261079SEd Maste int r, type; 534b15c8340SDag-Erling Smørgrav u_char *blob; 53519261079SEd Maste char *label; 536190cef3dSDag-Erling Smørgrav size_t blen; 537190cef3dSDag-Erling Smørgrav u_int nkeys, i; 538190cef3dSDag-Erling Smørgrav struct sshbuf *msg; 539*535af610SEd Maste struct helper *helper; 540b15c8340SDag-Erling Smørgrav 541*535af610SEd Maste if ((helper = helper_by_provider(name)) == NULL && 542*535af610SEd Maste (helper = pkcs11_start_helper(name)) == NULL) 543*535af610SEd Maste return -1; 544b15c8340SDag-Erling Smørgrav 545190cef3dSDag-Erling Smørgrav if ((msg = sshbuf_new()) == NULL) 54619261079SEd Maste fatal_f("sshbuf_new failed"); 547190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 || 548190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, name)) != 0 || 549190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, pin)) != 0) 55019261079SEd Maste fatal_fr(r, "compose"); 551*535af610SEd Maste send_msg(helper->fd, msg); 552190cef3dSDag-Erling Smørgrav sshbuf_reset(msg); 553b15c8340SDag-Erling Smørgrav 554*535af610SEd Maste type = recv_msg(helper->fd, msg); 55519261079SEd Maste if (type == SSH2_AGENT_IDENTITIES_ANSWER) { 556190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) 55719261079SEd Maste fatal_fr(r, "parse nkeys"); 558190cef3dSDag-Erling Smørgrav *keysp = xcalloc(nkeys, sizeof(struct sshkey *)); 55919261079SEd Maste if (labelsp) 56019261079SEd Maste *labelsp = xcalloc(nkeys, sizeof(char *)); 561b15c8340SDag-Erling Smørgrav for (i = 0; i < nkeys; i++) { 562190cef3dSDag-Erling Smørgrav /* XXX clean up properly instead of fatal() */ 563190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 || 56419261079SEd Maste (r = sshbuf_get_cstring(msg, &label, NULL)) != 0) 56519261079SEd Maste fatal_fr(r, "parse key"); 566190cef3dSDag-Erling Smørgrav if ((r = sshkey_from_blob(blob, blen, &k)) != 0) 56719261079SEd Maste fatal_fr(r, "decode key"); 568*535af610SEd Maste wrap_key(helper, k); 569b15c8340SDag-Erling Smørgrav (*keysp)[i] = k; 57019261079SEd Maste if (labelsp) 57119261079SEd Maste (*labelsp)[i] = label; 57219261079SEd Maste else 57319261079SEd Maste free(label); 574e4a9863fSDag-Erling Smørgrav free(blob); 575b15c8340SDag-Erling Smørgrav } 57619261079SEd Maste } else if (type == SSH2_AGENT_FAILURE) { 57719261079SEd Maste if ((r = sshbuf_get_u32(msg, &nkeys)) != 0) 57819261079SEd Maste nkeys = -1; 579b15c8340SDag-Erling Smørgrav } else { 580b15c8340SDag-Erling Smørgrav nkeys = -1; 581b15c8340SDag-Erling Smørgrav } 582190cef3dSDag-Erling Smørgrav sshbuf_free(msg); 583b15c8340SDag-Erling Smørgrav return (nkeys); 584b15c8340SDag-Erling Smørgrav } 585b15c8340SDag-Erling Smørgrav 586b15c8340SDag-Erling Smørgrav int 587b15c8340SDag-Erling Smørgrav pkcs11_del_provider(char *name) 588b15c8340SDag-Erling Smørgrav { 589*535af610SEd Maste struct helper *helper; 590b15c8340SDag-Erling Smørgrav 591*535af610SEd Maste /* 592*535af610SEd Maste * ssh-agent deletes keys before calling this, so the helper entry 593*535af610SEd Maste * should be gone before we get here. 594*535af610SEd Maste */ 595*535af610SEd Maste debug3_f("delete %s", name); 596*535af610SEd Maste if ((helper = helper_by_provider(name)) != NULL) 597*535af610SEd Maste helper_terminate(helper); 598*535af610SEd Maste return 0; 599b15c8340SDag-Erling Smørgrav } 600b15c8340SDag-Erling Smørgrav #endif /* ENABLE_PKCS11 */ 601