1*3d9fd9fcSEd Maste /* $OpenBSD: ssh-pkcs11-client.c,v 1.20 2024/08/15 00:51:51 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 *
helper_by_provider(const char * path)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 *
helper_by_rsa(const RSA * rsa)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 *
helper_by_ec(const EC_KEY * ec)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
helper_free(struct helper * helper)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
helper_terminate(struct helper * helper)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
send_msg(int fd,struct sshbuf * m)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
recv_msg(int fd,struct sshbuf * m)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
pkcs11_init(int interactive)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
pkcs11_terminate(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
rsa_encrypt(int flen,const u_char * from,u_char * to,RSA * rsa,int padding)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;
267*3d9fd9fcSEd Maste if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
26819261079SEd Maste error_f("sshkey_new failed");
26919261079SEd Maste goto fail;
27019261079SEd Maste }
271*3d9fd9fcSEd Maste if ((key->pkey = EVP_PKEY_new()) == NULL ||
272*3d9fd9fcSEd Maste EVP_PKEY_set1_RSA(key->pkey, rsa) != 1) {
273*3d9fd9fcSEd Maste error_f("pkey setup failed");
274*3d9fd9fcSEd Maste goto fail;
275*3d9fd9fcSEd Maste }
276*3d9fd9fcSEd Maste
27719261079SEd Maste key->type = KEY_RSA;
27819261079SEd Maste if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
27919261079SEd Maste error_fr(r, "encode key");
28019261079SEd Maste goto fail;
281190cef3dSDag-Erling Smørgrav }
282190cef3dSDag-Erling Smørgrav if ((msg = sshbuf_new()) == NULL)
28319261079SEd Maste fatal_f("sshbuf_new failed");
284190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
285190cef3dSDag-Erling Smørgrav (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
286190cef3dSDag-Erling Smørgrav (r = sshbuf_put_string(msg, from, flen)) != 0 ||
287190cef3dSDag-Erling Smørgrav (r = sshbuf_put_u32(msg, 0)) != 0)
28819261079SEd Maste fatal_fr(r, "compose");
289535af610SEd Maste send_msg(helper->fd, msg);
290190cef3dSDag-Erling Smørgrav sshbuf_reset(msg);
291b15c8340SDag-Erling Smørgrav
292535af610SEd Maste if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
293190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
29419261079SEd Maste fatal_fr(r, "parse");
295190cef3dSDag-Erling Smørgrav if (slen <= (size_t)RSA_size(rsa)) {
296b15c8340SDag-Erling Smørgrav memcpy(to, signature, slen);
297b15c8340SDag-Erling Smørgrav ret = slen;
298b15c8340SDag-Erling Smørgrav }
299e4a9863fSDag-Erling Smørgrav free(signature);
300b15c8340SDag-Erling Smørgrav }
30119261079SEd Maste fail:
30219261079SEd Maste free(blob);
30319261079SEd Maste sshkey_free(key);
304190cef3dSDag-Erling Smørgrav sshbuf_free(msg);
305b15c8340SDag-Erling Smørgrav return (ret);
306b15c8340SDag-Erling Smørgrav }
307b15c8340SDag-Erling Smørgrav
308535af610SEd Maste static int
rsa_finish(RSA * rsa)309535af610SEd Maste rsa_finish(RSA *rsa)
310535af610SEd Maste {
311535af610SEd Maste struct helper *helper;
312535af610SEd Maste
313535af610SEd Maste if ((helper = helper_by_rsa(rsa)) == NULL)
314535af610SEd Maste fatal_f("no helper for PKCS11 key");
315535af610SEd Maste debug3_f("free PKCS11 RSA key for provider %s", helper->path);
316535af610SEd Maste if (helper->rsa_finish != NULL)
317535af610SEd Maste helper->rsa_finish(rsa);
318535af610SEd Maste if (helper->nrsa == 0)
319535af610SEd Maste fatal_f("RSA refcount error");
320535af610SEd Maste helper->nrsa--;
321535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
322535af610SEd Maste helper->path, helper->nrsa, helper->nec);
323535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0)
324535af610SEd Maste helper_terminate(helper);
325535af610SEd Maste return 1;
326535af610SEd Maste }
327535af610SEd Maste
3281323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
32919261079SEd Maste static ECDSA_SIG *
ecdsa_do_sign(const unsigned char * dgst,int dgst_len,const BIGNUM * inv,const BIGNUM * rp,EC_KEY * ec)33019261079SEd Maste ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
33119261079SEd Maste const BIGNUM *rp, EC_KEY *ec)
332b15c8340SDag-Erling Smørgrav {
33319261079SEd Maste struct sshkey *key = NULL;
33419261079SEd Maste struct sshbuf *msg = NULL;
33519261079SEd Maste ECDSA_SIG *ret = NULL;
33619261079SEd Maste const u_char *cp;
33719261079SEd Maste u_char *blob = NULL, *signature = NULL;
33819261079SEd Maste size_t blen, slen = 0;
33919261079SEd Maste int r, nid;
340535af610SEd Maste struct helper *helper;
34119261079SEd Maste
342535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL || helper->fd == -1)
343535af610SEd Maste fatal_f("no helper for PKCS11 key");
344535af610SEd Maste debug3_f("signing with PKCS11 provider %s", helper->path);
34519261079SEd Maste
346*3d9fd9fcSEd Maste if ((key = sshkey_new(KEY_UNSPEC)) == NULL) {
34719261079SEd Maste error_f("sshkey_new failed");
34819261079SEd Maste goto fail;
34919261079SEd Maste }
350*3d9fd9fcSEd Maste if ((key->pkey = EVP_PKEY_new()) == NULL ||
351*3d9fd9fcSEd Maste EVP_PKEY_set1_EC_KEY(key->pkey, ec) != 1) {
352*3d9fd9fcSEd Maste error("pkey setup failed");
353*3d9fd9fcSEd Maste goto fail;
354*3d9fd9fcSEd Maste }
355*3d9fd9fcSEd Maste if ((nid = sshkey_ecdsa_pkey_to_nid(key->pkey)) < 0) {
356*3d9fd9fcSEd Maste error("couldn't get curve nid");
357*3d9fd9fcSEd Maste goto fail;
358*3d9fd9fcSEd Maste }
35919261079SEd Maste key->ecdsa_nid = nid;
36019261079SEd Maste key->type = KEY_ECDSA;
36119261079SEd Maste
36219261079SEd Maste if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
36319261079SEd Maste error_fr(r, "encode key");
36419261079SEd Maste goto fail;
36519261079SEd Maste }
36619261079SEd Maste if ((msg = sshbuf_new()) == NULL)
36719261079SEd Maste fatal_f("sshbuf_new failed");
36819261079SEd Maste if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
36919261079SEd Maste (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
37019261079SEd Maste (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
37119261079SEd Maste (r = sshbuf_put_u32(msg, 0)) != 0)
37219261079SEd Maste fatal_fr(r, "compose");
373535af610SEd Maste send_msg(helper->fd, msg);
37419261079SEd Maste sshbuf_reset(msg);
37519261079SEd Maste
376535af610SEd Maste if (recv_msg(helper->fd, msg) == SSH2_AGENT_SIGN_RESPONSE) {
37719261079SEd Maste if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
37819261079SEd Maste fatal_fr(r, "parse");
37919261079SEd Maste cp = signature;
38019261079SEd Maste ret = d2i_ECDSA_SIG(NULL, &cp, slen);
38119261079SEd Maste free(signature);
38219261079SEd Maste }
38319261079SEd Maste
38419261079SEd Maste fail:
38519261079SEd Maste free(blob);
38619261079SEd Maste sshkey_free(key);
38719261079SEd Maste sshbuf_free(msg);
38819261079SEd Maste return (ret);
38919261079SEd Maste }
39019261079SEd Maste
391535af610SEd Maste static void
ecdsa_do_finish(EC_KEY * ec)392535af610SEd Maste ecdsa_do_finish(EC_KEY *ec)
393535af610SEd Maste {
394535af610SEd Maste struct helper *helper;
395535af610SEd Maste
396535af610SEd Maste if ((helper = helper_by_ec(ec)) == NULL)
397535af610SEd Maste fatal_f("no helper for PKCS11 key");
398535af610SEd Maste debug3_f("free PKCS11 ECDSA key for provider %s", helper->path);
399535af610SEd Maste if (helper->ec_finish != NULL)
400535af610SEd Maste helper->ec_finish(ec);
401535af610SEd Maste if (helper->nec == 0)
402535af610SEd Maste fatal_f("ECDSA refcount error");
403535af610SEd Maste helper->nec--;
404535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
405535af610SEd Maste helper->path, helper->nrsa, helper->nec);
406535af610SEd Maste if (helper->nrsa == 0 && helper->nec == 0)
407535af610SEd Maste helper_terminate(helper);
408535af610SEd Maste }
409535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */
41019261079SEd Maste
41119261079SEd Maste /* redirect private key crypto operations to the ssh-pkcs11-helper */
41219261079SEd Maste static void
wrap_key(struct helper * helper,struct sshkey * k)413535af610SEd Maste wrap_key(struct helper *helper, struct sshkey *k)
41419261079SEd Maste {
415*3d9fd9fcSEd Maste RSA *rsa = NULL;
416*3d9fd9fcSEd Maste EC_KEY *ecdsa = NULL;
417*3d9fd9fcSEd Maste
418535af610SEd Maste debug3_f("wrap %s for provider %s", sshkey_type(k), helper->path);
419535af610SEd Maste if (k->type == KEY_RSA) {
420*3d9fd9fcSEd Maste if ((rsa = EVP_PKEY_get1_RSA(k->pkey)) == NULL)
421*3d9fd9fcSEd Maste fatal_f("no RSA key");
422*3d9fd9fcSEd Maste if (RSA_set_method(rsa, helper->rsa_meth) != 1)
423*3d9fd9fcSEd Maste fatal_f("RSA_set_method failed");
424535af610SEd Maste if (helper->nrsa++ >= INT_MAX)
425535af610SEd Maste fatal_f("RSA refcount error");
426*3d9fd9fcSEd Maste if (EVP_PKEY_set1_RSA(k->pkey, rsa) != 1)
427*3d9fd9fcSEd Maste fatal_f("EVP_PKEY_set1_RSA failed");
428*3d9fd9fcSEd Maste RSA_free(rsa);
4291323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
430535af610SEd Maste } else if (k->type == KEY_ECDSA) {
431*3d9fd9fcSEd Maste if ((ecdsa = EVP_PKEY_get1_EC_KEY(k->pkey)) == NULL)
432*3d9fd9fcSEd Maste fatal_f("no ECDSA key");
433*3d9fd9fcSEd Maste if (EC_KEY_set_method(ecdsa, helper->ec_meth) != 1)
434*3d9fd9fcSEd Maste fatal_f("EC_KEY_set_method failed");
435535af610SEd Maste if (helper->nec++ >= INT_MAX)
436535af610SEd Maste fatal_f("EC refcount error");
437*3d9fd9fcSEd Maste if (EVP_PKEY_set1_EC_KEY(k->pkey, ecdsa) != 1)
438*3d9fd9fcSEd Maste fatal_f("EVP_PKEY_set1_EC_KEY failed");
439*3d9fd9fcSEd Maste EC_KEY_free(ecdsa);
440535af610SEd Maste #endif
441535af610SEd Maste } else
44219261079SEd Maste fatal_f("unknown key type");
443535af610SEd Maste k->flags |= SSHKEY_FLAG_EXT;
444535af610SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
445535af610SEd Maste helper->path, helper->nrsa, helper->nec);
44619261079SEd Maste }
44719261079SEd Maste
448069ac184SEd Maste /*
449069ac184SEd Maste * Make a private PKCS#11-backed certificate by grafting a previously-loaded
450069ac184SEd Maste * PKCS#11 private key and a public certificate key.
451069ac184SEd Maste */
452069ac184SEd Maste int
pkcs11_make_cert(const struct sshkey * priv,const struct sshkey * certpub,struct sshkey ** certprivp)453069ac184SEd Maste pkcs11_make_cert(const struct sshkey *priv,
454069ac184SEd Maste const struct sshkey *certpub, struct sshkey **certprivp)
455069ac184SEd Maste {
456069ac184SEd Maste struct helper *helper = NULL;
457069ac184SEd Maste struct sshkey *ret;
458069ac184SEd Maste int r;
459*3d9fd9fcSEd Maste RSA *rsa_priv = NULL, *rsa_cert = NULL;
460*3d9fd9fcSEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
461*3d9fd9fcSEd Maste EC_KEY *ec_priv = NULL, *ec_cert = NULL;
462*3d9fd9fcSEd Maste #endif
463069ac184SEd Maste
464069ac184SEd Maste debug3_f("private key type %s cert type %s", sshkey_type(priv),
465069ac184SEd Maste sshkey_type(certpub));
466069ac184SEd Maste *certprivp = NULL;
467069ac184SEd Maste if (!sshkey_is_cert(certpub) || sshkey_is_cert(priv) ||
468069ac184SEd Maste !sshkey_equal_public(priv, certpub)) {
469069ac184SEd Maste error_f("private key %s doesn't match cert %s",
470069ac184SEd Maste sshkey_type(priv), sshkey_type(certpub));
471069ac184SEd Maste return SSH_ERR_INVALID_ARGUMENT;
472069ac184SEd Maste }
473069ac184SEd Maste *certprivp = NULL;
474069ac184SEd Maste if (priv->type == KEY_RSA) {
475*3d9fd9fcSEd Maste if ((rsa_priv = EVP_PKEY_get1_RSA(priv->pkey)) == NULL)
476*3d9fd9fcSEd Maste fatal_f("no RSA pkey");
477*3d9fd9fcSEd Maste if ((helper = helper_by_rsa(rsa_priv)) == NULL ||
478069ac184SEd Maste helper->fd == -1)
479069ac184SEd Maste fatal_f("no helper for PKCS11 RSA key");
480069ac184SEd Maste if ((r = sshkey_from_private(priv, &ret)) != 0)
481069ac184SEd Maste fatal_fr(r, "copy key");
482*3d9fd9fcSEd Maste if ((rsa_cert = EVP_PKEY_get1_RSA(ret->pkey)) == NULL)
483*3d9fd9fcSEd Maste fatal_f("no RSA cert pkey");
484*3d9fd9fcSEd Maste if (RSA_set_method(rsa_cert, helper->rsa_meth) != 1)
485*3d9fd9fcSEd Maste fatal_f("RSA_set_method failed");
486069ac184SEd Maste if (helper->nrsa++ >= INT_MAX)
487069ac184SEd Maste fatal_f("RSA refcount error");
488*3d9fd9fcSEd Maste if (EVP_PKEY_set1_RSA(ret->pkey, rsa_cert) != 1)
489*3d9fd9fcSEd Maste fatal_f("EVP_PKEY_set1_RSA failed");
490*3d9fd9fcSEd Maste RSA_free(rsa_priv);
491*3d9fd9fcSEd Maste RSA_free(rsa_cert);
492a91a2465SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
493069ac184SEd Maste } else if (priv->type == KEY_ECDSA) {
494*3d9fd9fcSEd Maste if ((ec_priv = EVP_PKEY_get1_EC_KEY(priv->pkey)) == NULL)
495*3d9fd9fcSEd Maste fatal_f("no EC pkey");
496*3d9fd9fcSEd Maste if ((helper = helper_by_ec(ec_priv)) == NULL ||
497069ac184SEd Maste helper->fd == -1)
498069ac184SEd Maste fatal_f("no helper for PKCS11 EC key");
499069ac184SEd Maste if ((r = sshkey_from_private(priv, &ret)) != 0)
500069ac184SEd Maste fatal_fr(r, "copy key");
501*3d9fd9fcSEd Maste if ((ec_cert = EVP_PKEY_get1_EC_KEY(ret->pkey)) == NULL)
502*3d9fd9fcSEd Maste fatal_f("no EC cert pkey");
503*3d9fd9fcSEd Maste if (EC_KEY_set_method(ec_cert, helper->ec_meth) != 1)
504*3d9fd9fcSEd Maste fatal_f("EC_KEY_set_method failed");
505069ac184SEd Maste if (helper->nec++ >= INT_MAX)
506069ac184SEd Maste fatal_f("EC refcount error");
507*3d9fd9fcSEd Maste if (EVP_PKEY_set1_EC_KEY(ret->pkey, ec_cert) != 1)
508*3d9fd9fcSEd Maste fatal_f("EVP_PKEY_set1_EC_KEY failed");
509*3d9fd9fcSEd Maste EC_KEY_free(ec_priv);
510*3d9fd9fcSEd Maste EC_KEY_free(ec_cert);
511a91a2465SEd Maste #endif
512069ac184SEd Maste } else
513069ac184SEd Maste fatal_f("unknown key type %s", sshkey_type(priv));
514069ac184SEd Maste
515069ac184SEd Maste ret->flags |= SSHKEY_FLAG_EXT;
516069ac184SEd Maste if ((r = sshkey_to_certified(ret)) != 0 ||
517069ac184SEd Maste (r = sshkey_cert_copy(certpub, ret)) != 0)
518069ac184SEd Maste fatal_fr(r, "graft certificate");
519069ac184SEd Maste debug3_f("provider %s remaining keys: %zu RSA %zu ECDSA",
520069ac184SEd Maste helper->path, helper->nrsa, helper->nec);
521069ac184SEd Maste /* success */
522069ac184SEd Maste *certprivp = ret;
523069ac184SEd Maste return 0;
524069ac184SEd Maste }
525069ac184SEd Maste
52619261079SEd Maste static int
pkcs11_start_helper_methods(struct helper * helper)527535af610SEd Maste pkcs11_start_helper_methods(struct helper *helper)
52819261079SEd Maste {
529*3d9fd9fcSEd Maste RSA_METHOD *rsa_meth = NULL;
530535af610SEd Maste EC_KEY_METHOD *ec_meth = NULL;
5311323ec57SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
532535af610SEd Maste int (*ec_init)(EC_KEY *key);
533535af610SEd Maste int (*ec_copy)(EC_KEY *dest, const EC_KEY *src);
534535af610SEd Maste int (*ec_set_group)(EC_KEY *key, const EC_GROUP *grp);
535535af610SEd Maste int (*ec_set_private)(EC_KEY *key, const BIGNUM *priv_key);
536535af610SEd Maste int (*ec_set_public)(EC_KEY *key, const EC_POINT *pub_key);
537535af610SEd Maste int (*ec_sign)(int, const unsigned char *, int, unsigned char *,
53819261079SEd Maste unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
539b15c8340SDag-Erling Smørgrav
540535af610SEd Maste if ((ec_meth = EC_KEY_METHOD_new(EC_KEY_OpenSSL())) == NULL)
541535af610SEd Maste return -1;
542535af610SEd Maste EC_KEY_METHOD_get_sign(ec_meth, &ec_sign, NULL, NULL);
543535af610SEd Maste EC_KEY_METHOD_set_sign(ec_meth, ec_sign, NULL, ecdsa_do_sign);
544535af610SEd Maste EC_KEY_METHOD_get_init(ec_meth, &ec_init, &helper->ec_finish,
545535af610SEd Maste &ec_copy, &ec_set_group, &ec_set_private, &ec_set_public);
546535af610SEd Maste EC_KEY_METHOD_set_init(ec_meth, ec_init, ecdsa_do_finish,
547535af610SEd Maste ec_copy, ec_set_group, ec_set_private, ec_set_public);
548535af610SEd Maste #endif /* defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW) */
549535af610SEd Maste
550535af610SEd Maste if ((rsa_meth = RSA_meth_dup(RSA_get_default_method())) == NULL)
55119261079SEd Maste fatal_f("RSA_meth_dup failed");
552535af610SEd Maste helper->rsa_finish = RSA_meth_get_finish(rsa_meth);
553535af610SEd Maste if (!RSA_meth_set1_name(rsa_meth, "ssh-pkcs11-helper") ||
554535af610SEd Maste !RSA_meth_set_priv_enc(rsa_meth, rsa_encrypt) ||
555535af610SEd Maste !RSA_meth_set_finish(rsa_meth, rsa_finish))
55619261079SEd Maste fatal_f("failed to prepare method");
55719261079SEd Maste
558535af610SEd Maste helper->ec_meth = ec_meth;
559535af610SEd Maste helper->rsa_meth = rsa_meth;
560535af610SEd Maste return 0;
561b15c8340SDag-Erling Smørgrav }
562b15c8340SDag-Erling Smørgrav
563535af610SEd Maste static struct helper *
pkcs11_start_helper(const char * path)564535af610SEd Maste pkcs11_start_helper(const char *path)
565b15c8340SDag-Erling Smørgrav {
566b15c8340SDag-Erling Smørgrav int pair[2];
567535af610SEd Maste char *prog, *verbosity = NULL;
568535af610SEd Maste struct helper *helper;
569535af610SEd Maste pid_t pid;
57019261079SEd Maste
571535af610SEd Maste if (nhelpers >= INT_MAX)
572535af610SEd Maste fatal_f("too many helpers");
573535af610SEd Maste debug3_f("start helper for %s", path);
574b15c8340SDag-Erling Smørgrav if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
575535af610SEd Maste error_f("socketpair: %s", strerror(errno));
576535af610SEd Maste return NULL;
577535af610SEd Maste }
578535af610SEd Maste helper = xcalloc(1, sizeof(*helper));
579535af610SEd Maste if (pkcs11_start_helper_methods(helper) == -1) {
580535af610SEd Maste error_f("pkcs11_start_helper_methods failed");
581535af610SEd Maste goto fail;
582b15c8340SDag-Erling Smørgrav }
583b15c8340SDag-Erling Smørgrav if ((pid = fork()) == -1) {
584535af610SEd Maste error_f("fork: %s", strerror(errno));
585535af610SEd Maste fail:
586535af610SEd Maste close(pair[0]);
587535af610SEd Maste close(pair[1]);
588535af610SEd Maste RSA_meth_free(helper->rsa_meth);
589535af610SEd Maste #if defined(OPENSSL_HAS_ECC) && defined(HAVE_EC_KEY_METHOD_NEW)
590535af610SEd Maste EC_KEY_METHOD_free(helper->ec_meth);
591535af610SEd Maste #endif
592535af610SEd Maste free(helper);
593535af610SEd Maste return NULL;
594b15c8340SDag-Erling Smørgrav } else if (pid == 0) {
595b15c8340SDag-Erling Smørgrav if ((dup2(pair[1], STDIN_FILENO) == -1) ||
596b15c8340SDag-Erling Smørgrav (dup2(pair[1], STDOUT_FILENO) == -1)) {
597b15c8340SDag-Erling Smørgrav fprintf(stderr, "dup2: %s\n", strerror(errno));
598b15c8340SDag-Erling Smørgrav _exit(1);
599b15c8340SDag-Erling Smørgrav }
600b15c8340SDag-Erling Smørgrav close(pair[0]);
601b15c8340SDag-Erling Smørgrav close(pair[1]);
602535af610SEd Maste prog = getenv("SSH_PKCS11_HELPER");
603535af610SEd Maste if (prog == NULL || strlen(prog) == 0)
604535af610SEd Maste prog = _PATH_SSH_PKCS11_HELPER;
605535af610SEd Maste if (log_level_get() >= SYSLOG_LEVEL_DEBUG1)
606535af610SEd Maste verbosity = "-vvv";
607535af610SEd Maste debug_f("starting %s %s", prog,
60819261079SEd Maste verbosity == NULL ? "" : verbosity);
609535af610SEd Maste execlp(prog, prog, verbosity, (char *)NULL);
610535af610SEd Maste fprintf(stderr, "exec: %s: %s\n", prog, strerror(errno));
611b15c8340SDag-Erling Smørgrav _exit(1);
612b15c8340SDag-Erling Smørgrav }
613b15c8340SDag-Erling Smørgrav close(pair[1]);
614535af610SEd Maste helper->fd = pair[0];
615535af610SEd Maste helper->path = xstrdup(path);
616535af610SEd Maste helper->pid = pid;
617535af610SEd Maste debug3_f("helper %zu for \"%s\" on fd %d pid %ld", nhelpers,
618535af610SEd Maste helper->path, helper->fd, (long)helper->pid);
619535af610SEd Maste helpers = xrecallocarray(helpers, nhelpers,
620535af610SEd Maste nhelpers + 1, sizeof(*helpers));
621535af610SEd Maste helpers[nhelpers++] = helper;
622535af610SEd Maste return helper;
623b15c8340SDag-Erling Smørgrav }
624b15c8340SDag-Erling Smørgrav
625b15c8340SDag-Erling Smørgrav int
pkcs11_add_provider(char * name,char * pin,struct sshkey *** keysp,char *** labelsp)62619261079SEd Maste pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp,
62719261079SEd Maste char ***labelsp)
628b15c8340SDag-Erling Smørgrav {
6294f52dfbbSDag-Erling Smørgrav struct sshkey *k;
63019261079SEd Maste int r, type;
631b15c8340SDag-Erling Smørgrav u_char *blob;
63219261079SEd Maste char *label;
633190cef3dSDag-Erling Smørgrav size_t blen;
634190cef3dSDag-Erling Smørgrav u_int nkeys, i;
635190cef3dSDag-Erling Smørgrav struct sshbuf *msg;
636535af610SEd Maste struct helper *helper;
637b15c8340SDag-Erling Smørgrav
638535af610SEd Maste if ((helper = helper_by_provider(name)) == NULL &&
639535af610SEd Maste (helper = pkcs11_start_helper(name)) == NULL)
640535af610SEd Maste return -1;
641b15c8340SDag-Erling Smørgrav
642190cef3dSDag-Erling Smørgrav if ((msg = sshbuf_new()) == NULL)
64319261079SEd Maste fatal_f("sshbuf_new failed");
644190cef3dSDag-Erling Smørgrav if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
645190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, name)) != 0 ||
646190cef3dSDag-Erling Smørgrav (r = sshbuf_put_cstring(msg, pin)) != 0)
64719261079SEd Maste fatal_fr(r, "compose");
648535af610SEd Maste send_msg(helper->fd, msg);
649190cef3dSDag-Erling Smørgrav sshbuf_reset(msg);
650b15c8340SDag-Erling Smørgrav
651535af610SEd Maste type = recv_msg(helper->fd, msg);
65219261079SEd Maste if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
653190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
65419261079SEd Maste fatal_fr(r, "parse nkeys");
655190cef3dSDag-Erling Smørgrav *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
65619261079SEd Maste if (labelsp)
65719261079SEd Maste *labelsp = xcalloc(nkeys, sizeof(char *));
658b15c8340SDag-Erling Smørgrav for (i = 0; i < nkeys; i++) {
659190cef3dSDag-Erling Smørgrav /* XXX clean up properly instead of fatal() */
660190cef3dSDag-Erling Smørgrav if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
66119261079SEd Maste (r = sshbuf_get_cstring(msg, &label, NULL)) != 0)
66219261079SEd Maste fatal_fr(r, "parse key");
663190cef3dSDag-Erling Smørgrav if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
66419261079SEd Maste fatal_fr(r, "decode key");
665535af610SEd Maste wrap_key(helper, k);
666b15c8340SDag-Erling Smørgrav (*keysp)[i] = k;
66719261079SEd Maste if (labelsp)
66819261079SEd Maste (*labelsp)[i] = label;
66919261079SEd Maste else
67019261079SEd Maste free(label);
671e4a9863fSDag-Erling Smørgrav free(blob);
672b15c8340SDag-Erling Smørgrav }
67319261079SEd Maste } else if (type == SSH2_AGENT_FAILURE) {
67419261079SEd Maste if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
67519261079SEd Maste nkeys = -1;
676b15c8340SDag-Erling Smørgrav } else {
677b15c8340SDag-Erling Smørgrav nkeys = -1;
678b15c8340SDag-Erling Smørgrav }
679190cef3dSDag-Erling Smørgrav sshbuf_free(msg);
680b15c8340SDag-Erling Smørgrav return (nkeys);
681b15c8340SDag-Erling Smørgrav }
682b15c8340SDag-Erling Smørgrav
683b15c8340SDag-Erling Smørgrav int
pkcs11_del_provider(char * name)684b15c8340SDag-Erling Smørgrav pkcs11_del_provider(char *name)
685b15c8340SDag-Erling Smørgrav {
686535af610SEd Maste struct helper *helper;
687b15c8340SDag-Erling Smørgrav
688535af610SEd Maste /*
689535af610SEd Maste * ssh-agent deletes keys before calling this, so the helper entry
690535af610SEd Maste * should be gone before we get here.
691535af610SEd Maste */
692535af610SEd Maste debug3_f("delete %s", name);
693535af610SEd Maste if ((helper = helper_by_provider(name)) != NULL)
694535af610SEd Maste helper_terminate(helper);
695535af610SEd Maste return 0;
696b15c8340SDag-Erling Smørgrav }
697b15c8340SDag-Erling Smørgrav #endif /* ENABLE_PKCS11 */
698