1*ba1276acSMatthew Dillon /* $OpenBSD: ssh-pkcs11-client.c,v 1.19 2023/12/18 14:46:56 djm Exp $ */
2*ba1276acSMatthew Dillon /*
3*ba1276acSMatthew Dillon * Copyright (c) 2010 Markus Friedl. All rights reserved.
4*ba1276acSMatthew Dillon * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
5*ba1276acSMatthew Dillon *
6*ba1276acSMatthew Dillon * Permission to use, copy, modify, and distribute this software for any
7*ba1276acSMatthew Dillon * purpose with or without fee is hereby granted, provided that the above
8*ba1276acSMatthew Dillon * copyright notice and this permission notice appear in all copies.
9*ba1276acSMatthew Dillon *
10*ba1276acSMatthew Dillon * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*ba1276acSMatthew Dillon * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*ba1276acSMatthew Dillon * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*ba1276acSMatthew Dillon * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*ba1276acSMatthew Dillon * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*ba1276acSMatthew Dillon * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*ba1276acSMatthew Dillon * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*ba1276acSMatthew Dillon */
18*ba1276acSMatthew Dillon
19*ba1276acSMatthew Dillon #include "includes.h"
20*ba1276acSMatthew Dillon
21*ba1276acSMatthew Dillon #ifdef ENABLE_PKCS11
22*ba1276acSMatthew Dillon
23*ba1276acSMatthew Dillon #include <sys/types.h>
24*ba1276acSMatthew Dillon #ifdef HAVE_SYS_TIME_H
25*ba1276acSMatthew Dillon # include <sys/time.h>
26*ba1276acSMatthew Dillon #endif
27*ba1276acSMatthew Dillon #include <sys/socket.h>
28*ba1276acSMatthew Dillon
29*ba1276acSMatthew Dillon #include <stdarg.h>
30*ba1276acSMatthew Dillon #include <string.h>
31*ba1276acSMatthew Dillon #include <unistd.h>
32*ba1276acSMatthew Dillon #include <errno.h>
33*ba1276acSMatthew Dillon #include <limits.h>
34*ba1276acSMatthew Dillon
35*ba1276acSMatthew Dillon #include <openssl/ecdsa.h>
36*ba1276acSMatthew Dillon #include <openssl/rsa.h>
37*ba1276acSMatthew Dillon
38*ba1276acSMatthew Dillon #include "pathnames.h"
39*ba1276acSMatthew Dillon #include "xmalloc.h"
40*ba1276acSMatthew Dillon #include "sshbuf.h"
41*ba1276acSMatthew Dillon #include "log.h"
42*ba1276acSMatthew Dillon #include "misc.h"
43*ba1276acSMatthew Dillon #include "sshkey.h"
44*ba1276acSMatthew Dillon #include "authfd.h"
45*ba1276acSMatthew Dillon #include "atomicio.h"
46*ba1276acSMatthew Dillon #include "ssh-pkcs11.h"
47*ba1276acSMatthew Dillon #include "ssherr.h"
48*ba1276acSMatthew Dillon
49*ba1276acSMatthew Dillon #include "openbsd-compat/openssl-compat.h"
50*ba1276acSMatthew Dillon
51*ba1276acSMatthew Dillon #if !defined(OPENSSL_HAS_ECC) || !defined(HAVE_EC_KEY_METHOD_NEW)
52*ba1276acSMatthew Dillon #define EC_KEY_METHOD void
53*ba1276acSMatthew Dillon #define EC_KEY void
54*ba1276acSMatthew Dillon #endif
55*ba1276acSMatthew Dillon
56*ba1276acSMatthew Dillon /* borrows code from sftp-server and ssh-agent */
57*ba1276acSMatthew Dillon
58*ba1276acSMatthew Dillon /*
59*ba1276acSMatthew Dillon * Maintain a list of ssh-pkcs11-helper subprocesses. These may be looked up
60*ba1276acSMatthew Dillon * by provider path or their unique EC/RSA METHOD pointers.
61*ba1276acSMatthew Dillon */
62*ba1276acSMatthew Dillon struct helper {
63*ba1276acSMatthew Dillon char *path;
64*ba1276acSMatthew Dillon pid_t pid;
65*ba1276acSMatthew Dillon int fd;
66*ba1276acSMatthew Dillon RSA_METHOD *rsa_meth;
67*ba1276acSMatthew Dillon EC_KEY_METHOD *ec_meth;
68*ba1276acSMatthew Dillon int (*rsa_finish)(RSA *rsa);
69*ba1276acSMatthew Dillon void (*ec_finish)(EC_KEY *key);
70*ba1276acSMatthew Dillon size_t nrsa, nec; /* number of active keys of each type */
71*ba1276acSMatthew Dillon };
72*ba1276acSMatthew Dillon static struct helper **helpers;
73*ba1276acSMatthew Dillon static size_t nhelpers;
74*ba1276acSMatthew Dillon
75*ba1276acSMatthew Dillon static struct helper *
helper_by_provider(const char * path)76*ba1276acSMatthew Dillon helper_by_provider(const char *path)
77*ba1276acSMatthew Dillon {
78*ba1276acSMatthew Dillon size_t i;
79*ba1276acSMatthew Dillon
80*ba1276acSMatthew Dillon for (i = 0; i < nhelpers; i++) {
81*ba1276acSMatthew Dillon if (helpers[i] == NULL || helpers[i]->path == NULL ||
82*ba1276acSMatthew Dillon helpers[i]->fd == -1)
83*ba1276acSMatthew Dillon continue;
84*ba1276acSMatthew Dillon if (strcmp(helpers[i]->path, path) == 0)
85*ba1276acSMatthew Dillon return helpers[i];
86*ba1276acSMatthew Dillon }
87*ba1276acSMatthew Dillon return NULL;
88*ba1276acSMatthew Dillon }
89*ba1276acSMatthew Dillon
90*ba1276acSMatthew Dillon static struct helper *
helper_by_rsa(const RSA * rsa)91*ba1276acSMatthew Dillon helper_by_rsa(const RSA *rsa)
92*ba1276acSMatthew Dillon {
93*ba1276acSMatthew Dillon size_t i;
94*ba1276acSMatthew Dillon const RSA_METHOD *meth;
95*ba1276acSMatthew Dillon
96*ba1276acSMatthew Dillon if ((meth = RSA_get_method(rsa)) == NULL)
97*ba1276acSMatthew Dillon return NULL;
98*ba1276acSMatthew Dillon for (i = 0; i < nhelpers; i++) {
99*ba1276acSMatthew Dillon if (helpers[i] != NULL && helpers[i]->rsa_meth == meth)
100*ba1276acSMatthew Dillon return helpers[i];
101*ba1276acSMatthew Dillon }
102*ba1276acSMatthew Dillon return NULL;
103*ba1276acSMatthew Dillon
104*ba1276acSMatthew Dillon }
105*ba1276acSMatthew Dillon
106*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
107*ba1276acSMatthew Dillon static struct helper *
helper_by_ec(const EC_KEY * ec)108*ba1276acSMatthew Dillon helper_by_ec(const EC_KEY *ec)
109*ba1276acSMatthew Dillon {
110*ba1276acSMatthew Dillon size_t i;
111*ba1276acSMatthew Dillon const EC_KEY_METHOD *meth;
112*ba1276acSMatthew Dillon
113*ba1276acSMatthew Dillon if ((meth = EC_KEY_get_method(ec)) == NULL)
114*ba1276acSMatthew Dillon return NULL;
115*ba1276acSMatthew Dillon for (i = 0; i < nhelpers; i++) {
116*ba1276acSMatthew Dillon if (helpers[i] != NULL && helpers[i]->ec_meth == meth)
117*ba1276acSMatthew Dillon return helpers[i];
118*ba1276acSMatthew Dillon }
119*ba1276acSMatthew Dillon return NULL;
120*ba1276acSMatthew Dillon
121*ba1276acSMatthew Dillon }
122*ba1276acSMatthew Dillon #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */
123*ba1276acSMatthew Dillon
124*ba1276acSMatthew Dillon static void
helper_free(struct helper * helper)125*ba1276acSMatthew Dillon helper_free(struct helper *helper)
126*ba1276acSMatthew Dillon {
127*ba1276acSMatthew Dillon size_t i;
128*ba1276acSMatthew Dillon int found = 0;
129*ba1276acSMatthew Dillon
130*ba1276acSMatthew Dillon if (helper == NULL)
131*ba1276acSMatthew Dillon return;
132*ba1276acSMatthew Dillon if (helper->path == NULL || helper->ec_meth == NULL ||
133*ba1276acSMatthew Dillon helper->rsa_meth == NULL)
134*ba1276acSMatthew Dillon fatal_f("inconsistent helper");
135*ba1276acSMatthew Dillon debug3_f("free helper for provider %s", helper->path);
136*ba1276acSMatthew Dillon for (i = 0; i < nhelpers; i++) {
137*ba1276acSMatthew Dillon if (helpers[i] == helper) {
138*ba1276acSMatthew Dillon if (found)
139*ba1276acSMatthew Dillon fatal_f("helper recorded more than once");
140*ba1276acSMatthew Dillon found = 1;
141*ba1276acSMatthew Dillon }
142*ba1276acSMatthew Dillon else if (found)
143*ba1276acSMatthew Dillon helpers[i - 1] = helpers[i];
144*ba1276acSMatthew Dillon }
145*ba1276acSMatthew Dillon if (found) {
146*ba1276acSMatthew Dillon helpers = xrecallocarray(helpers, nhelpers,
147*ba1276acSMatthew Dillon nhelpers - 1, sizeof(*helpers));
148*ba1276acSMatthew Dillon nhelpers--;
149*ba1276acSMatthew Dillon }
150*ba1276acSMatthew Dillon free(helper->path);
151*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
152*ba1276acSMatthew Dillon EC_KEY_METHOD_free(helper->ec_meth);
153*ba1276acSMatthew Dillon #endif
154*ba1276acSMatthew Dillon RSA_meth_free(helper->rsa_meth);
155*ba1276acSMatthew Dillon free(helper);
156*ba1276acSMatthew Dillon }
157*ba1276acSMatthew Dillon
158*ba1276acSMatthew Dillon static void
helper_terminate(struct helper * helper)159*ba1276acSMatthew Dillon helper_terminate(struct helper *helper)
160*ba1276acSMatthew Dillon {
161*ba1276acSMatthew Dillon if (helper == NULL) {
162*ba1276acSMatthew Dillon return;
163*ba1276acSMatthew Dillon } else if (helper->fd == -1) {
164*ba1276acSMatthew Dillon debug3_f("already terminated");
165*ba1276acSMatthew Dillon } else {
166*ba1276acSMatthew Dillon debug3_f("terminating helper for %s; "
167*ba1276acSMatthew Dillon "remaining %zu RSA %zu ECDSA",
168*ba1276acSMatthew Dillon helper->path, helper->nrsa, helper->nec);
169*ba1276acSMatthew Dillon close(helper->fd);
170*ba1276acSMatthew Dillon /* XXX waitpid() */
171*ba1276acSMatthew Dillon helper->fd = -1;
172*ba1276acSMatthew Dillon helper->pid = -1;
173*ba1276acSMatthew Dillon }
174*ba1276acSMatthew Dillon /*
175*ba1276acSMatthew Dillon * Don't delete the helper entry until there are no remaining keys
176*ba1276acSMatthew Dillon * that reference it. Otherwise, any signing operation would call
177*ba1276acSMatthew Dillon * a free'd METHOD pointer and that would be bad.
178*ba1276acSMatthew Dillon */
179*ba1276acSMatthew Dillon if (helper->nrsa == 0 && helper->nec == 0)
180*ba1276acSMatthew Dillon helper_free(helper);
181*ba1276acSMatthew Dillon }
182*ba1276acSMatthew Dillon
183*ba1276acSMatthew Dillon static void
send_msg(int fd,struct sshbuf * m)184*ba1276acSMatthew Dillon send_msg(int fd, struct sshbuf *m)
185*ba1276acSMatthew Dillon {
186*ba1276acSMatthew Dillon u_char buf[4];
187*ba1276acSMatthew Dillon size_t mlen = sshbuf_len(m);
188*ba1276acSMatthew Dillon int r;
189*ba1276acSMatthew Dillon
190*ba1276acSMatthew Dillon if (fd == -1)
191*ba1276acSMatthew Dillon return;
192*ba1276acSMatthew Dillon POKE_U32(buf, mlen);
193*ba1276acSMatthew Dillon if (atomicio(vwrite, fd, buf, 4) != 4 ||
194*ba1276acSMatthew Dillon atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
195*ba1276acSMatthew Dillon sshbuf_len(m)) != sshbuf_len(m))
196*ba1276acSMatthew Dillon error("write to helper failed");
197*ba1276acSMatthew Dillon if ((r = sshbuf_consume(m, mlen)) != 0)
198*ba1276acSMatthew Dillon fatal_fr(r, "consume");
199*ba1276acSMatthew Dillon }
200*ba1276acSMatthew Dillon
201*ba1276acSMatthew Dillon static int
recv_msg(int fd,struct sshbuf * m)202*ba1276acSMatthew Dillon recv_msg(int fd, struct sshbuf *m)
203*ba1276acSMatthew Dillon {
204*ba1276acSMatthew Dillon u_int l, len;
205*ba1276acSMatthew Dillon u_char c, buf[1024];
206*ba1276acSMatthew Dillon int r;
207*ba1276acSMatthew Dillon
208*ba1276acSMatthew Dillon sshbuf_reset(m);
209*ba1276acSMatthew Dillon if (fd == -1)
210*ba1276acSMatthew Dillon return 0; /* XXX */
211*ba1276acSMatthew Dillon if ((len = atomicio(read, fd, buf, 4)) != 4) {
212*ba1276acSMatthew Dillon error("read from helper failed: %u", len);
213*ba1276acSMatthew Dillon return (0); /* XXX */
214*ba1276acSMatthew Dillon }
215*ba1276acSMatthew Dillon len = PEEK_U32(buf);
216*ba1276acSMatthew Dillon if (len > 256 * 1024)
217*ba1276acSMatthew Dillon fatal("response too long: %u", len);
218*ba1276acSMatthew Dillon /* read len bytes into m */
219*ba1276acSMatthew Dillon while (len > 0) {
220*ba1276acSMatthew Dillon l = len;
221*ba1276acSMatthew Dillon if (l > sizeof(buf))
222*ba1276acSMatthew Dillon l = sizeof(buf);
223*ba1276acSMatthew Dillon if (atomicio(read, fd, buf, l) != l) {
224*ba1276acSMatthew Dillon error("response from helper failed.");
225*ba1276acSMatthew Dillon return (0); /* XXX */
226*ba1276acSMatthew Dillon }
227*ba1276acSMatthew Dillon if ((r = sshbuf_put(m, buf, l)) != 0)
228*ba1276acSMatthew Dillon fatal_fr(r, "sshbuf_put");
229*ba1276acSMatthew Dillon len -= l;
230*ba1276acSMatthew Dillon }
231*ba1276acSMatthew Dillon if ((r = sshbuf_get_u8(m, &c)) != 0)
232*ba1276acSMatthew Dillon fatal_fr(r, "parse type");
233*ba1276acSMatthew Dillon return c;
234*ba1276acSMatthew Dillon }
235*ba1276acSMatthew Dillon
236*ba1276acSMatthew Dillon int
pkcs11_init(int interactive)237*ba1276acSMatthew Dillon pkcs11_init(int interactive)
238*ba1276acSMatthew Dillon {
239*ba1276acSMatthew Dillon return 0;
240*ba1276acSMatthew Dillon }
241*ba1276acSMatthew Dillon
242*ba1276acSMatthew Dillon void
pkcs11_terminate(void)243*ba1276acSMatthew Dillon pkcs11_terminate(void)
244*ba1276acSMatthew Dillon {
245*ba1276acSMatthew Dillon size_t i;
246*ba1276acSMatthew Dillon
247*ba1276acSMatthew Dillon debug3_f("terminating %zu helpers", nhelpers);
248*ba1276acSMatthew Dillon for (i = 0; i < nhelpers; i++)
249*ba1276acSMatthew Dillon helper_terminate(helpers[i]);
250*ba1276acSMatthew Dillon }
251*ba1276acSMatthew Dillon
252*ba1276acSMatthew Dillon static int
rsa_encrypt(int flen,const u_char * from,u_char * to,RSA * rsa,int padding)253*ba1276acSMatthew Dillon rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
254*ba1276acSMatthew Dillon {
255*ba1276acSMatthew Dillon struct sshkey *key = NULL;
256*ba1276acSMatthew Dillon struct sshbuf *msg = NULL;
257*ba1276acSMatthew Dillon u_char *blob = NULL, *signature = NULL;
258*ba1276acSMatthew Dillon size_t blen, slen = 0;
259*ba1276acSMatthew Dillon int r, ret = -1;
260*ba1276acSMatthew Dillon struct helper *helper;
261*ba1276acSMatthew Dillon
262*ba1276acSMatthew Dillon if ((helper = helper_by_rsa(rsa)) == NULL || helper->fd == -1)
263*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 key");
264*ba1276acSMatthew Dillon debug3_f("signing with PKCS11 provider %s", helper->path);
265*ba1276acSMatthew Dillon if (padding != RSA_PKCS1_PADDING)
266*ba1276acSMatthew Dillon goto fail;
267*ba1276acSMatthew Dillon key = sshkey_new(KEY_UNSPEC);
268*ba1276acSMatthew Dillon if (key == NULL) {
269*ba1276acSMatthew Dillon error_f("sshkey_new failed");
270*ba1276acSMatthew Dillon goto fail;
271*ba1276acSMatthew Dillon }
272*ba1276acSMatthew Dillon key->type = KEY_RSA;
273*ba1276acSMatthew Dillon RSA_up_ref(rsa);
274*ba1276acSMatthew Dillon key->rsa = rsa;
275*ba1276acSMatthew Dillon if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
276*ba1276acSMatthew Dillon error_fr(r, "encode key");
277*ba1276acSMatthew Dillon goto fail;
278*ba1276acSMatthew Dillon }
279*ba1276acSMatthew Dillon if ((msg = sshbuf_new()) == NULL)
280*ba1276acSMatthew Dillon fatal_f("sshbuf_new failed");
281*ba1276acSMatthew Dillon if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
282*ba1276acSMatthew Dillon (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
283*ba1276acSMatthew Dillon (r = sshbuf_put_string(msg, from, flen)) != 0 ||
284*ba1276acSMatthew Dillon (r = sshbuf_put_u32(msg, 0)) != 0)
285*ba1276acSMatthew Dillon fatal_fr(r, "compose");
286*ba1276acSMatthew Dillon send_msg(helper->fd, msg);
287*ba1276acSMatthew Dillon sshbuf_reset(msg);
288*ba1276acSMatthew Dillon
289*ba1276acSMatthew Dillon if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
290*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
291*ba1276acSMatthew Dillon fatal_fr(r, "parse");
292*ba1276acSMatthew Dillon if (slen <= (size_t)RSA_size(rsa)) {
293*ba1276acSMatthew Dillon memcpy(to, signature, slen);
294*ba1276acSMatthew Dillon ret = slen;
295*ba1276acSMatthew Dillon }
296*ba1276acSMatthew Dillon free(signature);
297*ba1276acSMatthew Dillon }
298*ba1276acSMatthew Dillon fail:
299*ba1276acSMatthew Dillon free(blob);
300*ba1276acSMatthew Dillon sshkey_free(key);
301*ba1276acSMatthew Dillon sshbuf_free(msg);
302*ba1276acSMatthew Dillon return (ret);
303*ba1276acSMatthew Dillon }
304*ba1276acSMatthew Dillon
305*ba1276acSMatthew Dillon static int
rsa_finish(RSA * rsa)306*ba1276acSMatthew Dillon rsa_finish(RSA *rsa)
307*ba1276acSMatthew Dillon {
308*ba1276acSMatthew Dillon struct helper *helper;
309*ba1276acSMatthew Dillon
310*ba1276acSMatthew Dillon if ((helper = helper_by_rsa(rsa)) == NULL)
311*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 key");
312*ba1276acSMatthew Dillon debug3_f("free PKCS11 RSA key for provider %s", helper->path);
313*ba1276acSMatthew Dillon if (helper->rsa_finish != NULL)
314*ba1276acSMatthew Dillon helper->rsa_finish(rsa);
315*ba1276acSMatthew Dillon if (helper->nrsa == 0)
316*ba1276acSMatthew Dillon fatal_f("RSA refcount error");
317*ba1276acSMatthew Dillon helper->nrsa--;
318*ba1276acSMatthew Dillon debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
319*ba1276acSMatthew Dillon helper->path, helper->nrsa, helper->nec);
320*ba1276acSMatthew Dillon if (helper->nrsa == 0 && helper->nec == 0)
321*ba1276acSMatthew Dillon helper_terminate(helper);
322*ba1276acSMatthew Dillon return 1;
323*ba1276acSMatthew Dillon }
324*ba1276acSMatthew Dillon
325*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
326*ba1276acSMatthew Dillon static ECDSA_SIG *
ecdsa_do_sign(const unsigned char * dgst,int dgst_len,const BIGNUM * inv,const BIGNUM * rp,EC_KEY * ec)327*ba1276acSMatthew Dillon ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
328*ba1276acSMatthew Dillon const BIGNUM *rp, EC_KEY *ec)
329*ba1276acSMatthew Dillon {
330*ba1276acSMatthew Dillon struct sshkey *key = NULL;
331*ba1276acSMatthew Dillon struct sshbuf *msg = NULL;
332*ba1276acSMatthew Dillon ECDSA_SIG *ret = NULL;
333*ba1276acSMatthew Dillon const u_char *cp;
334*ba1276acSMatthew Dillon u_char *blob = NULL, *signature = NULL;
335*ba1276acSMatthew Dillon size_t blen, slen = 0;
336*ba1276acSMatthew Dillon int r, nid;
337*ba1276acSMatthew Dillon struct helper *helper;
338*ba1276acSMatthew Dillon
339*ba1276acSMatthew Dillon if ((helper = helper_by_ec(ec)) == NULL || helper->fd == -1)
340*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 key");
341*ba1276acSMatthew Dillon debug3_f("signing with PKCS11 provider %s", helper->path);
342*ba1276acSMatthew Dillon nid = sshkey_ecdsa_key_to_nid(ec);
343*ba1276acSMatthew Dillon if (nid < 0) {
344*ba1276acSMatthew Dillon error_f("couldn't get curve nid");
345*ba1276acSMatthew Dillon goto fail;
346*ba1276acSMatthew Dillon }
347*ba1276acSMatthew Dillon
348*ba1276acSMatthew Dillon key = sshkey_new(KEY_UNSPEC);
349*ba1276acSMatthew Dillon if (key == NULL) {
350*ba1276acSMatthew Dillon error_f("sshkey_new failed");
351*ba1276acSMatthew Dillon goto fail;
352*ba1276acSMatthew Dillon }
353*ba1276acSMatthew Dillon key->ecdsa = ec;
354*ba1276acSMatthew Dillon key->ecdsa_nid = nid;
355*ba1276acSMatthew Dillon key->type = KEY_ECDSA;
356*ba1276acSMatthew Dillon EC_KEY_up_ref(ec);
357*ba1276acSMatthew Dillon
358*ba1276acSMatthew Dillon if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
359*ba1276acSMatthew Dillon error_fr(r, "encode key");
360*ba1276acSMatthew Dillon goto fail;
361*ba1276acSMatthew Dillon }
362*ba1276acSMatthew Dillon if ((msg = sshbuf_new()) == NULL)
363*ba1276acSMatthew Dillon fatal_f("sshbuf_new failed");
364*ba1276acSMatthew Dillon if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
365*ba1276acSMatthew Dillon (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
366*ba1276acSMatthew Dillon (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
367*ba1276acSMatthew Dillon (r = sshbuf_put_u32(msg, 0)) != 0)
368*ba1276acSMatthew Dillon fatal_fr(r, "compose");
369*ba1276acSMatthew Dillon send_msg(helper->fd, msg);
370*ba1276acSMatthew Dillon sshbuf_reset(msg);
371*ba1276acSMatthew Dillon
372*ba1276acSMatthew Dillon if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
373*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
374*ba1276acSMatthew Dillon fatal_fr(r, "parse");
375*ba1276acSMatthew Dillon cp = signature;
376*ba1276acSMatthew Dillon ret = d2i_ECDSA_SIG(NULL, &cp, slen);
377*ba1276acSMatthew Dillon free(signature);
378*ba1276acSMatthew Dillon }
379*ba1276acSMatthew Dillon
380*ba1276acSMatthew Dillon fail:
381*ba1276acSMatthew Dillon free(blob);
382*ba1276acSMatthew Dillon sshkey_free(key);
383*ba1276acSMatthew Dillon sshbuf_free(msg);
384*ba1276acSMatthew Dillon return (ret);
385*ba1276acSMatthew Dillon }
386*ba1276acSMatthew Dillon
387*ba1276acSMatthew Dillon static void
ecdsa_do_finish(EC_KEY * ec)388*ba1276acSMatthew Dillon ecdsa_do_finish(EC_KEY *ec)
389*ba1276acSMatthew Dillon {
390*ba1276acSMatthew Dillon struct helper *helper;
391*ba1276acSMatthew Dillon
392*ba1276acSMatthew Dillon if ((helper = helper_by_ec(ec)) == NULL)
393*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 key");
394*ba1276acSMatthew Dillon debug3_f("free PKCS11 ECDSA key for provider %s", helper->path);
395*ba1276acSMatthew Dillon if (helper->ec_finish != NULL)
396*ba1276acSMatthew Dillon helper->ec_finish(ec);
397*ba1276acSMatthew Dillon if (helper->nec == 0)
398*ba1276acSMatthew Dillon fatal_f("ECDSA refcount error");
399*ba1276acSMatthew Dillon helper->nec--;
400*ba1276acSMatthew Dillon debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
401*ba1276acSMatthew Dillon helper->path, helper->nrsa, helper->nec);
402*ba1276acSMatthew Dillon if (helper->nrsa == 0 && helper->nec == 0)
403*ba1276acSMatthew Dillon helper_terminate(helper);
404*ba1276acSMatthew Dillon }
405*ba1276acSMatthew Dillon #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */
406*ba1276acSMatthew Dillon
407*ba1276acSMatthew Dillon /* redirect private key crypto operations to the ssh-pkcs11-helper */
408*ba1276acSMatthew Dillon static void
wrap_key(struct helper * helper,struct sshkey * k)409*ba1276acSMatthew Dillon wrap_key(struct helper *helper, struct sshkey *k)
410*ba1276acSMatthew Dillon {
411*ba1276acSMatthew Dillon debug3_f("wrap %s for provider %s", sshkey_type(k), helper->path);
412*ba1276acSMatthew Dillon if (k->type == KEY_RSA) {
413*ba1276acSMatthew Dillon RSA_set_method(k->rsa, helper->rsa_meth);
414*ba1276acSMatthew Dillon if (helper->nrsa++ >= INT_MAX)
415*ba1276acSMatthew Dillon fatal_f("RSA refcount error");
416*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
417*ba1276acSMatthew Dillon } else if (k->type == KEY_ECDSA) {
418*ba1276acSMatthew Dillon EC_KEY_set_method(k->ecdsa, helper->ec_meth);
419*ba1276acSMatthew Dillon if (helper->nec++ >= INT_MAX)
420*ba1276acSMatthew Dillon fatal_f("EC refcount error");
421*ba1276acSMatthew Dillon #endif
422*ba1276acSMatthew Dillon } else
423*ba1276acSMatthew Dillon fatal_f("unknown key type");
424*ba1276acSMatthew Dillon k->flags |= SSHKEY_FLAG_EXT;
425*ba1276acSMatthew Dillon debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
426*ba1276acSMatthew Dillon helper->path, helper->nrsa, helper->nec);
427*ba1276acSMatthew Dillon }
428*ba1276acSMatthew Dillon
429*ba1276acSMatthew Dillon /*
430*ba1276acSMatthew Dillon * Make a private PKCS#11-backed certificate by grafting a previously-loaded
431*ba1276acSMatthew Dillon * PKCS#11 private key and a public certificate key.
432*ba1276acSMatthew Dillon */
433*ba1276acSMatthew Dillon int
pkcs11_make_cert(const struct sshkey * priv,const struct sshkey * certpub,struct sshkey ** certprivp)434*ba1276acSMatthew Dillon pkcs11_make_cert(const struct sshkey *priv,
435*ba1276acSMatthew Dillon const struct sshkey *certpub, struct sshkey **certprivp)
436*ba1276acSMatthew Dillon {
437*ba1276acSMatthew Dillon struct helper *helper = NULL;
438*ba1276acSMatthew Dillon struct sshkey *ret;
439*ba1276acSMatthew Dillon int r;
440*ba1276acSMatthew Dillon
441*ba1276acSMatthew Dillon debug3_f("private key type %s cert type %s", sshkey_type(priv),
442*ba1276acSMatthew Dillon sshkey_type(certpub));
443*ba1276acSMatthew Dillon *certprivp = NULL;
444*ba1276acSMatthew Dillon if (!sshkey_is_cert(certpub) || sshkey_is_cert(priv) ||
445*ba1276acSMatthew Dillon !sshkey_equal_public(priv, certpub)) {
446*ba1276acSMatthew Dillon error_f("private key %s doesn't match cert %s",
447*ba1276acSMatthew Dillon sshkey_type(priv), sshkey_type(certpub));
448*ba1276acSMatthew Dillon return SSH_ERR_INVALID_ARGUMENT;
449*ba1276acSMatthew Dillon }
450*ba1276acSMatthew Dillon *certprivp = NULL;
451*ba1276acSMatthew Dillon if (priv->type == KEY_RSA) {
452*ba1276acSMatthew Dillon if ((helper = helper_by_rsa(priv->rsa)) == NULL ||
453*ba1276acSMatthew Dillon helper->fd == -1)
454*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 RSA key");
455*ba1276acSMatthew Dillon if ((r = sshkey_from_private(priv, &ret)) != 0)
456*ba1276acSMatthew Dillon fatal_fr(r, "copy key");
457*ba1276acSMatthew Dillon RSA_set_method(ret->rsa, helper->rsa_meth);
458*ba1276acSMatthew Dillon if (helper->nrsa++ >= INT_MAX)
459*ba1276acSMatthew Dillon fatal_f("RSA refcount error");
460*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
461*ba1276acSMatthew Dillon } else if (priv->type == KEY_ECDSA) {
462*ba1276acSMatthew Dillon if ((helper = helper_by_ec(priv->ecdsa)) == NULL ||
463*ba1276acSMatthew Dillon helper->fd == -1)
464*ba1276acSMatthew Dillon fatal_f("no helper for PKCS11 EC key");
465*ba1276acSMatthew Dillon if ((r = sshkey_from_private(priv, &ret)) != 0)
466*ba1276acSMatthew Dillon fatal_fr(r, "copy key");
467*ba1276acSMatthew Dillon EC_KEY_set_method(ret->ecdsa, helper->ec_meth);
468*ba1276acSMatthew Dillon if (helper->nec++ >= INT_MAX)
469*ba1276acSMatthew Dillon fatal_f("EC refcount error");
470*ba1276acSMatthew Dillon #endif
471*ba1276acSMatthew Dillon } else
472*ba1276acSMatthew Dillon fatal_f("unknown key type %s", sshkey_type(priv));
473*ba1276acSMatthew Dillon
474*ba1276acSMatthew Dillon ret->flags |= SSHKEY_FLAG_EXT;
475*ba1276acSMatthew Dillon if ((r = sshkey_to_certified(ret)) != 0 ||
476*ba1276acSMatthew Dillon (r = sshkey_cert_copy(certpub, ret)) != 0)
477*ba1276acSMatthew Dillon fatal_fr(r, "graft certificate");
478*ba1276acSMatthew Dillon debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
479*ba1276acSMatthew Dillon helper->path, helper->nrsa, helper->nec);
480*ba1276acSMatthew Dillon /* success */
481*ba1276acSMatthew Dillon *certprivp = ret;
482*ba1276acSMatthew Dillon return 0;
483*ba1276acSMatthew Dillon }
484*ba1276acSMatthew Dillon
485*ba1276acSMatthew Dillon static int
pkcs11_start_helper_methods(struct helper * helper)486*ba1276acSMatthew Dillon pkcs11_start_helper_methods(struct helper *helper)
487*ba1276acSMatthew Dillon {
488*ba1276acSMatthew Dillon RSA_METHOD *rsa_meth;
489*ba1276acSMatthew Dillon EC_KEY_METHOD *ec_meth = NULL;
490*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
491*ba1276acSMatthew Dillon int (*ec_init)(EC_KEY *key);
492*ba1276acSMatthew Dillon int (*ec_copy)(EC_KEY *dest, const EC_KEY *src);
493*ba1276acSMatthew Dillon int (*ec_set_group)(EC_KEY *key, const EC_GROUP *grp);
494*ba1276acSMatthew Dillon int (*ec_set_private)(EC_KEY *key, const BIGNUM *priv_key);
495*ba1276acSMatthew Dillon int (*ec_set_public)(EC_KEY *key, const EC_POINT *pub_key);
496*ba1276acSMatthew Dillon int (*ec_sign)(int, const unsigned char *, int, unsigned char *,
497*ba1276acSMatthew Dillon unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
498*ba1276acSMatthew Dillon
499*ba1276acSMatthew Dillon if ((ec_meth = EC_KEY_METHOD_new(EC_KEY_OpenSSL())) == NULL)
500*ba1276acSMatthew Dillon return -1;
501*ba1276acSMatthew Dillon EC_KEY_METHOD_get_sign(ec_meth, &ec_sign, NULL, NULL);
502*ba1276acSMatthew Dillon EC_KEY_METHOD_set_sign(ec_meth, ec_sign, NULL, ecdsa_do_sign);
503*ba1276acSMatthew Dillon EC_KEY_METHOD_get_init(ec_meth, &ec_init, &helper->ec_finish,
504*ba1276acSMatthew Dillon &ec_copy, &ec_set_group, &ec_set_private, &ec_set_public);
505*ba1276acSMatthew Dillon EC_KEY_METHOD_set_init(ec_meth, ec_init, ecdsa_do_finish,
506*ba1276acSMatthew Dillon ec_copy, ec_set_group, ec_set_private, ec_set_public);
507*ba1276acSMatthew Dillon #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */
508*ba1276acSMatthew Dillon
509*ba1276acSMatthew Dillon if ((rsa_meth = RSA_meth_dup(RSA_get_default_method())) == NULL)
510*ba1276acSMatthew Dillon fatal_f("RSA_meth_dup failed");
511*ba1276acSMatthew Dillon helper->rsa_finish = RSA_meth_get_finish(rsa_meth);
512*ba1276acSMatthew Dillon if (!RSA_meth_set1_name(rsa_meth, "ssh-pkcs11-helper") ||
513*ba1276acSMatthew Dillon !RSA_meth_set_priv_enc(rsa_meth, rsa_encrypt) ||
514*ba1276acSMatthew Dillon !RSA_meth_set_finish(rsa_meth, rsa_finish))
515*ba1276acSMatthew Dillon fatal_f("failed to prepare method");
516*ba1276acSMatthew Dillon
517*ba1276acSMatthew Dillon helper->ec_meth = ec_meth;
518*ba1276acSMatthew Dillon helper->rsa_meth = rsa_meth;
519*ba1276acSMatthew Dillon return 0;
520*ba1276acSMatthew Dillon }
521*ba1276acSMatthew Dillon
522*ba1276acSMatthew Dillon static struct helper *
pkcs11_start_helper(const char * path)523*ba1276acSMatthew Dillon pkcs11_start_helper(const char *path)
524*ba1276acSMatthew Dillon {
525*ba1276acSMatthew Dillon int pair[2];
526*ba1276acSMatthew Dillon char *prog, *verbosity = NULL;
527*ba1276acSMatthew Dillon struct helper *helper;
528*ba1276acSMatthew Dillon pid_t pid;
529*ba1276acSMatthew Dillon
530*ba1276acSMatthew Dillon if (nhelpers >= INT_MAX)
531*ba1276acSMatthew Dillon fatal_f("too many helpers");
532*ba1276acSMatthew Dillon debug3_f("start helper for %s", path);
533*ba1276acSMatthew Dillon if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
534*ba1276acSMatthew Dillon error_f("socketpair: %s", strerror(errno));
535*ba1276acSMatthew Dillon return NULL;
536*ba1276acSMatthew Dillon }
537*ba1276acSMatthew Dillon helper = xcalloc(1, sizeof(*helper));
538*ba1276acSMatthew Dillon if (pkcs11_start_helper_methods(helper) == -1) {
539*ba1276acSMatthew Dillon error_f("pkcs11_start_helper_methods failed");
540*ba1276acSMatthew Dillon goto fail;
541*ba1276acSMatthew Dillon }
542*ba1276acSMatthew Dillon if ((pid = fork()) == -1) {
543*ba1276acSMatthew Dillon error_f("fork: %s", strerror(errno));
544*ba1276acSMatthew Dillon fail:
545*ba1276acSMatthew Dillon close(pair[0]);
546*ba1276acSMatthew Dillon close(pair[1]);
547*ba1276acSMatthew Dillon RSA_meth_free(helper->rsa_meth);
548*ba1276acSMatthew Dillon #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
549*ba1276acSMatthew Dillon EC_KEY_METHOD_free(helper->ec_meth);
550*ba1276acSMatthew Dillon #endif
551*ba1276acSMatthew Dillon free(helper);
552*ba1276acSMatthew Dillon return NULL;
553*ba1276acSMatthew Dillon } else if (pid == 0) {
554*ba1276acSMatthew Dillon if ((dup2(pair[1], STDIN_FILENO) == -1) ||
555*ba1276acSMatthew Dillon (dup2(pair[1], STDOUT_FILENO) == -1)) {
556*ba1276acSMatthew Dillon fprintf(stderr, "dup2: %s\n", strerror(errno));
557*ba1276acSMatthew Dillon _exit(1);
558*ba1276acSMatthew Dillon }
559*ba1276acSMatthew Dillon close(pair[0]);
560*ba1276acSMatthew Dillon close(pair[1]);
561*ba1276acSMatthew Dillon prog = getenv("SSH_PKCS11_HELPER");
562*ba1276acSMatthew Dillon if (prog == NULL || strlen(prog) == 0)
563*ba1276acSMatthew Dillon prog = _PATH_SSH_PKCS11_HELPER;
564*ba1276acSMatthew Dillon if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
565*ba1276acSMatthew Dillon verbosity = "-vvv";
566*ba1276acSMatthew Dillon debug_f("starting %s %s", prog,
567*ba1276acSMatthew Dillon verbosity == NULL ? "" : verbosity);
568*ba1276acSMatthew Dillon execlp(prog, prog, verbosity, (char *)NULL);
569*ba1276acSMatthew Dillon fprintf(stderr, "exec: %s: %s\n", prog, strerror(errno));
570*ba1276acSMatthew Dillon _exit(1);
571*ba1276acSMatthew Dillon }
572*ba1276acSMatthew Dillon close(pair[1]);
573*ba1276acSMatthew Dillon helper->fd = pair[0];
574*ba1276acSMatthew Dillon helper->path = xstrdup(path);
575*ba1276acSMatthew Dillon helper->pid = pid;
576*ba1276acSMatthew Dillon debug3_f("helper %zu for \"%s\" on fd %d pid %ld", nhelpers,
577*ba1276acSMatthew Dillon helper->path, helper->fd, (long)helper->pid);
578*ba1276acSMatthew Dillon helpers = xrecallocarray(helpers, nhelpers,
579*ba1276acSMatthew Dillon nhelpers + 1, sizeof(*helpers));
580*ba1276acSMatthew Dillon helpers[nhelpers++] = helper;
581*ba1276acSMatthew Dillon return helper;
582*ba1276acSMatthew Dillon }
583*ba1276acSMatthew Dillon
584*ba1276acSMatthew Dillon int
pkcs11_add_provider(char * name,char * pin,struct sshkey *** keysp,char *** labelsp)585*ba1276acSMatthew Dillon pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp,
586*ba1276acSMatthew Dillon char ***labelsp)
587*ba1276acSMatthew Dillon {
588*ba1276acSMatthew Dillon struct sshkey *k;
589*ba1276acSMatthew Dillon int r, type;
590*ba1276acSMatthew Dillon u_char *blob;
591*ba1276acSMatthew Dillon char *label;
592*ba1276acSMatthew Dillon size_t blen;
593*ba1276acSMatthew Dillon u_int nkeys, i;
594*ba1276acSMatthew Dillon struct sshbuf *msg;
595*ba1276acSMatthew Dillon struct helper *helper;
596*ba1276acSMatthew Dillon
597*ba1276acSMatthew Dillon if ((helper = helper_by_provider(name)) == NULL &&
598*ba1276acSMatthew Dillon (helper = pkcs11_start_helper(name)) == NULL)
599*ba1276acSMatthew Dillon return -1;
600*ba1276acSMatthew Dillon
601*ba1276acSMatthew Dillon if ((msg = sshbuf_new()) == NULL)
602*ba1276acSMatthew Dillon fatal_f("sshbuf_new failed");
603*ba1276acSMatthew Dillon if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
604*ba1276acSMatthew Dillon (r = sshbuf_put_cstring(msg, name)) != 0 ||
605*ba1276acSMatthew Dillon (r = sshbuf_put_cstring(msg, pin)) != 0)
606*ba1276acSMatthew Dillon fatal_fr(r, "compose");
607*ba1276acSMatthew Dillon send_msg(helper->fd, msg);
608*ba1276acSMatthew Dillon sshbuf_reset(msg);
609*ba1276acSMatthew Dillon
610*ba1276acSMatthew Dillon type = recv_msg(helper->fd, msg);
611*ba1276acSMatthew Dillon if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
612*ba1276acSMatthew Dillon if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
613*ba1276acSMatthew Dillon fatal_fr(r, "parse nkeys");
614*ba1276acSMatthew Dillon *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
615*ba1276acSMatthew Dillon if (labelsp)
616*ba1276acSMatthew Dillon *labelsp = xcalloc(nkeys, sizeof(char *));
617*ba1276acSMatthew Dillon for (i = 0; i < nkeys; i++) {
618*ba1276acSMatthew Dillon /* XXX clean up properly instead of fatal() */
619*ba1276acSMatthew Dillon if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
620*ba1276acSMatthew Dillon (r = sshbuf_get_cstring(msg, &label, NULL)) != 0)
621*ba1276acSMatthew Dillon fatal_fr(r, "parse key");
622*ba1276acSMatthew Dillon if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
623*ba1276acSMatthew Dillon fatal_fr(r, "decode key");
624*ba1276acSMatthew Dillon wrap_key(helper, k);
625*ba1276acSMatthew Dillon (*keysp)[i] = k;
626*ba1276acSMatthew Dillon if (labelsp)
627*ba1276acSMatthew Dillon (*labelsp)[i] = label;
628*ba1276acSMatthew Dillon else
629*ba1276acSMatthew Dillon free(label);
630*ba1276acSMatthew Dillon free(blob);
631*ba1276acSMatthew Dillon }
632*ba1276acSMatthew Dillon } else if (type == SSH2_AGENT_FAILURE) {
633*ba1276acSMatthew Dillon if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
634*ba1276acSMatthew Dillon nkeys = -1;
635*ba1276acSMatthew Dillon } else {
636*ba1276acSMatthew Dillon nkeys = -1;
637*ba1276acSMatthew Dillon }
638*ba1276acSMatthew Dillon sshbuf_free(msg);
639*ba1276acSMatthew Dillon return (nkeys);
640*ba1276acSMatthew Dillon }
641*ba1276acSMatthew Dillon
642*ba1276acSMatthew Dillon int
pkcs11_del_provider(char * name)643*ba1276acSMatthew Dillon pkcs11_del_provider(char *name)
644*ba1276acSMatthew Dillon {
645*ba1276acSMatthew Dillon struct helper *helper;
646*ba1276acSMatthew Dillon
647*ba1276acSMatthew Dillon /*
648*ba1276acSMatthew Dillon * ssh-agent deletes keys before calling this, so the helper entry
649*ba1276acSMatthew Dillon * should be gone before we get here.
650*ba1276acSMatthew Dillon */
651*ba1276acSMatthew Dillon debug3_f("delete %s", name);
652*ba1276acSMatthew Dillon if ((helper = helper_by_provider(name)) != NULL)
653*ba1276acSMatthew Dillon helper_terminate(helper);
654*ba1276acSMatthew Dillon return 0;
655*ba1276acSMatthew Dillon }
656*ba1276acSMatthew Dillon #endif /* ENABLE_PKCS11 */
657