blob: 6cecf4863a57c2de3c76c588ce726fa422c9a4fb [file] [log] [blame]
djm@openbsd.org0c509922019-01-20 22:57:45 +00001/* $OpenBSD: ssh-pkcs11-client.c,v 1.14 2019/01/20 22:57:45 djm Exp $ */
Damien Miller7ea845e2010-02-12 09:21:02 +11002/*
3 * Copyright (c) 2010 Markus Friedl. All rights reserved.
djm@openbsd.org93f02102019-01-20 22:51:37 +00004 * Copyright (c) 2014 Pedro Martelletto. All rights reserved.
Damien Miller7ea845e2010-02-12 09:21:02 +11005 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
Damien Miller8ad0fbd2010-02-12 09:49:06 +110019#include "includes.h"
20
Damien Millerdfa41562010-02-12 10:06:28 +110021#ifdef ENABLE_PKCS11
22
Damien Miller7ea845e2010-02-12 09:21:02 +110023#include <sys/types.h>
Damien Miller8ad0fbd2010-02-12 09:49:06 +110024#ifdef HAVE_SYS_TIME_H
25# include <sys/time.h>
26#endif
Damien Miller7ea845e2010-02-12 09:21:02 +110027#include <sys/socket.h>
28
29#include <stdarg.h>
30#include <string.h>
31#include <unistd.h>
32#include <errno.h>
33
djm@openbsd.org93f02102019-01-20 22:51:37 +000034#include <openssl/ecdsa.h>
Damien Miller86687062014-07-02 15:28:02 +100035#include <openssl/rsa.h>
36
Damien Miller48f54b92018-09-13 12:13:50 +100037#include "openbsd-compat/openssl-compat.h"
38
Damien Miller7ea845e2010-02-12 09:21:02 +110039#include "pathnames.h"
40#include "xmalloc.h"
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000041#include "sshbuf.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110042#include "log.h"
43#include "misc.h"
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000044#include "sshkey.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110045#include "authfd.h"
46#include "atomicio.h"
47#include "ssh-pkcs11.h"
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000048#include "ssherr.h"
Damien Miller7ea845e2010-02-12 09:21:02 +110049
50/* borrows code from sftp-server and ssh-agent */
51
52int fd = -1;
53pid_t pid = -1;
54
55static void
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000056send_msg(struct sshbuf *m)
Damien Miller7ea845e2010-02-12 09:21:02 +110057{
58 u_char buf[4];
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000059 size_t mlen = sshbuf_len(m);
60 int r;
Damien Miller7ea845e2010-02-12 09:21:02 +110061
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000062 POKE_U32(buf, mlen);
Damien Miller7ea845e2010-02-12 09:21:02 +110063 if (atomicio(vwrite, fd, buf, 4) != 4 ||
markus@openbsd.org49f47e62018-07-09 21:59:10 +000064 atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000065 sshbuf_len(m)) != sshbuf_len(m))
Damien Miller7ea845e2010-02-12 09:21:02 +110066 error("write to helper failed");
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000067 if ((r = sshbuf_consume(m, mlen)) != 0)
68 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7ea845e2010-02-12 09:21:02 +110069}
70
71static int
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000072recv_msg(struct sshbuf *m)
Damien Miller7ea845e2010-02-12 09:21:02 +110073{
74 u_int l, len;
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000075 u_char c, buf[1024];
76 int r;
Damien Miller7ea845e2010-02-12 09:21:02 +110077
78 if ((len = atomicio(read, fd, buf, 4)) != 4) {
79 error("read from helper failed: %u", len);
80 return (0); /* XXX */
81 }
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000082 len = PEEK_U32(buf);
Damien Miller7ea845e2010-02-12 09:21:02 +110083 if (len > 256 * 1024)
84 fatal("response too long: %u", len);
85 /* read len bytes into m */
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000086 sshbuf_reset(m);
Damien Miller7ea845e2010-02-12 09:21:02 +110087 while (len > 0) {
88 l = len;
89 if (l > sizeof(buf))
90 l = sizeof(buf);
91 if (atomicio(read, fd, buf, l) != l) {
92 error("response from helper failed.");
93 return (0); /* XXX */
94 }
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000095 if ((r = sshbuf_put(m, buf, l)) != 0)
96 fatal("%s: buffer error: %s", __func__, ssh_err(r));
Damien Miller7ea845e2010-02-12 09:21:02 +110097 len -= l;
98 }
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +000099 if ((r = sshbuf_get_u8(m, &c)) != 0)
100 fatal("%s: buffer error: %s", __func__, ssh_err(r));
101 return c;
Damien Miller7ea845e2010-02-12 09:21:02 +1100102}
103
104int
105pkcs11_init(int interactive)
106{
107 return (0);
108}
109
110void
111pkcs11_terminate(void)
112{
tb@openbsd.org34843802018-02-05 05:37:46 +0000113 if (fd >= 0)
114 close(fd);
Damien Miller7ea845e2010-02-12 09:21:02 +1100115}
116
117static int
djm@openbsd.org93f02102019-01-20 22:51:37 +0000118rsa_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa, int padding)
Damien Miller7ea845e2010-02-12 09:21:02 +1100119{
djm@openbsd.org0c509922019-01-20 22:57:45 +0000120 struct sshkey *key = NULL;
121 struct sshbuf *msg = NULL;
122 u_char *blob = NULL, *signature = NULL;
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000123 size_t blen, slen = 0;
124 int r, ret = -1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100125
126 if (padding != RSA_PKCS1_PADDING)
djm@openbsd.org0c509922019-01-20 22:57:45 +0000127 goto fail;
128 key = sshkey_new(KEY_UNSPEC);
129 if (key == NULL) {
130 error("%s: sshkey_new failed", __func__);
131 goto fail;
132 }
133 key->type = KEY_RSA;
134 RSA_up_ref(rsa);
135 key->rsa = rsa;
136 if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000137 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
djm@openbsd.org0c509922019-01-20 22:57:45 +0000138 goto fail;
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000139 }
140 if ((msg = sshbuf_new()) == NULL)
141 fatal("%s: sshbuf_new failed", __func__);
142 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
143 (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
144 (r = sshbuf_put_string(msg, from, flen)) != 0 ||
145 (r = sshbuf_put_u32(msg, 0)) != 0)
146 fatal("%s: buffer error: %s", __func__, ssh_err(r));
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000147 send_msg(msg);
148 sshbuf_reset(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100149
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000150 if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
151 if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
152 fatal("%s: buffer error: %s", __func__, ssh_err(r));
153 if (slen <= (size_t)RSA_size(rsa)) {
Damien Miller7ea845e2010-02-12 09:21:02 +1100154 memcpy(to, signature, slen);
155 ret = slen;
156 }
Darren Tuckera627d422013-06-02 07:31:17 +1000157 free(signature);
Damien Miller7ea845e2010-02-12 09:21:02 +1100158 }
djm@openbsd.org0c509922019-01-20 22:57:45 +0000159 fail:
160 free(blob);
161 sshkey_free(key);
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000162 sshbuf_free(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100163 return (ret);
164}
165
djm@openbsd.org93f02102019-01-20 22:51:37 +0000166static ECDSA_SIG *
167ecdsa_do_sign(const unsigned char *dgst, int dgst_len, const BIGNUM *inv,
168 const BIGNUM *rp, EC_KEY *ec)
Damien Miller7ea845e2010-02-12 09:21:02 +1100169{
djm@openbsd.org0c509922019-01-20 22:57:45 +0000170 struct sshkey *key = NULL;
171 struct sshbuf *msg = NULL;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000172 ECDSA_SIG *ret = NULL;
djm@openbsd.org0c509922019-01-20 22:57:45 +0000173 const u_char *cp;
174 u_char *blob = NULL, *signature = NULL;
175 size_t blen, slen = 0;
176 int r, nid;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000177
djm@openbsd.org0c509922019-01-20 22:57:45 +0000178 nid = sshkey_ecdsa_key_to_nid(ec);
179 if (nid < 0) {
djm@openbsd.org93f02102019-01-20 22:51:37 +0000180 error("%s: couldn't get curve nid", __func__);
djm@openbsd.org0c509922019-01-20 22:57:45 +0000181 goto fail;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000182 }
djm@openbsd.org0c509922019-01-20 22:57:45 +0000183
184 key = sshkey_new(KEY_UNSPEC);
185 if (key == NULL) {
186 error("%s: sshkey_new failed", __func__);
187 goto fail;
188 }
189 key->ecdsa = ec;
190 key->ecdsa_nid = nid;
191 key->type = KEY_ECDSA;
192 EC_KEY_up_ref(ec);
193
194 if ((r = sshkey_to_blob(key, &blob, &blen)) != 0) {
djm@openbsd.org93f02102019-01-20 22:51:37 +0000195 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
djm@openbsd.org0c509922019-01-20 22:57:45 +0000196 goto fail;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000197 }
198 if ((msg = sshbuf_new()) == NULL)
199 fatal("%s: sshbuf_new failed", __func__);
200 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
201 (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
202 (r = sshbuf_put_string(msg, dgst, dgst_len)) != 0 ||
203 (r = sshbuf_put_u32(msg, 0)) != 0)
204 fatal("%s: buffer error: %s", __func__, ssh_err(r));
djm@openbsd.org93f02102019-01-20 22:51:37 +0000205 send_msg(msg);
206 sshbuf_reset(msg);
207
208 if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
209 if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
210 fatal("%s: buffer error: %s", __func__, ssh_err(r));
211 cp = signature;
212 ret = d2i_ECDSA_SIG(NULL, &cp, slen);
213 free(signature);
214 }
215
djm@openbsd.org0c509922019-01-20 22:57:45 +0000216 fail:
217 free(blob);
218 sshkey_free(key);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000219 sshbuf_free(msg);
220 return (ret);
221}
222
223static RSA_METHOD *helper_rsa;
224static EC_KEY_METHOD *helper_ecdsa;
225
226/* redirect private key crypto operations to the ssh-pkcs11-helper */
227static void
228wrap_key(struct sshkey *k)
229{
230 if (k->type == KEY_RSA)
231 RSA_set_method(k->rsa, helper_rsa);
232 else if (k->type == KEY_ECDSA)
233 EC_KEY_set_method(k->ecdsa, helper_ecdsa);
234 else
235 fatal("%s: unknown key type", __func__);
236}
237
238static int
239pkcs11_start_helper_methods(void)
240{
241 if (helper_ecdsa != NULL)
242 return (0);
243
244 int (*orig_sign)(int, const unsigned char *, int, unsigned char *,
245 unsigned int *, const BIGNUM *, const BIGNUM *, EC_KEY *) = NULL;
246 if (helper_ecdsa != NULL)
247 return (0);
248 helper_ecdsa = EC_KEY_METHOD_new(EC_KEY_OpenSSL());
249 if (helper_ecdsa == NULL)
250 return (-1);
251 EC_KEY_METHOD_get_sign(helper_ecdsa, &orig_sign, NULL, NULL);
252 EC_KEY_METHOD_set_sign(helper_ecdsa, orig_sign, NULL, ecdsa_do_sign);
Damien Miller7ea845e2010-02-12 09:21:02 +1100253
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000254 if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
255 fatal("%s: RSA_meth_dup failed", __func__);
256 if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") ||
djm@openbsd.org93f02102019-01-20 22:51:37 +0000257 !RSA_meth_set_priv_enc(helper_rsa, rsa_encrypt))
djm@openbsd.org482d23b2018-09-13 02:08:33 +0000258 fatal("%s: failed to prepare method", __func__);
djm@openbsd.org93f02102019-01-20 22:51:37 +0000259
Damien Miller7ea845e2010-02-12 09:21:02 +1100260 return (0);
261}
262
263static int
264pkcs11_start_helper(void)
265{
266 int pair[2];
djm@openbsd.org854bd862019-01-20 22:54:30 +0000267 char *helper;
Damien Miller7ea845e2010-02-12 09:21:02 +1100268
djm@openbsd.org93f02102019-01-20 22:51:37 +0000269 if (pkcs11_start_helper_methods() == -1) {
270 error("pkcs11_start_helper_methods failed");
271 return (-1);
272 }
273
Damien Miller7ea845e2010-02-12 09:21:02 +1100274 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
275 error("socketpair: %s", strerror(errno));
276 return (-1);
277 }
278 if ((pid = fork()) == -1) {
279 error("fork: %s", strerror(errno));
280 return (-1);
281 } else if (pid == 0) {
282 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
283 (dup2(pair[1], STDOUT_FILENO) == -1)) {
284 fprintf(stderr, "dup2: %s\n", strerror(errno));
285 _exit(1);
286 }
287 close(pair[0]);
288 close(pair[1]);
djm@openbsd.org854bd862019-01-20 22:54:30 +0000289 helper = getenv("SSH_PKCS11_HELPER");
290 if (helper == NULL || strlen(helper) == 0)
291 helper = _PATH_SSH_PKCS11_HELPER;
292 execlp(helper, helper, (char *)NULL);
293 fprintf(stderr, "exec: %s: %s\n", helper, strerror(errno));
Damien Miller7ea845e2010-02-12 09:21:02 +1100294 _exit(1);
295 }
296 close(pair[1]);
297 fd = pair[0];
298 return (0);
299}
300
301int
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000302pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
Damien Miller7ea845e2010-02-12 09:21:02 +1100303{
markus@openbsd.org54d90ac2017-05-30 08:52:19 +0000304 struct sshkey *k;
djm@openbsd.org93f02102019-01-20 22:51:37 +0000305 int r, type;
Damien Miller7ea845e2010-02-12 09:21:02 +1100306 u_char *blob;
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000307 size_t blen;
308 u_int nkeys, i;
309 struct sshbuf *msg;
Damien Miller7ea845e2010-02-12 09:21:02 +1100310
311 if (fd < 0 && pkcs11_start_helper() < 0)
312 return (-1);
313
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000314 if ((msg = sshbuf_new()) == NULL)
315 fatal("%s: sshbuf_new failed", __func__);
316 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
317 (r = sshbuf_put_cstring(msg, name)) != 0 ||
318 (r = sshbuf_put_cstring(msg, pin)) != 0)
319 fatal("%s: buffer error: %s", __func__, ssh_err(r));
320 send_msg(msg);
321 sshbuf_reset(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100322
djm@openbsd.org93f02102019-01-20 22:51:37 +0000323 type = recv_msg(msg);
324 if (type == SSH2_AGENT_IDENTITIES_ANSWER) {
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000325 if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
326 fatal("%s: buffer error: %s", __func__, ssh_err(r));
327 *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
Damien Miller7ea845e2010-02-12 09:21:02 +1100328 for (i = 0; i < nkeys; i++) {
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000329 /* XXX clean up properly instead of fatal() */
330 if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
331 (r = sshbuf_skip_string(msg)) != 0)
332 fatal("%s: buffer error: %s",
333 __func__, ssh_err(r));
334 if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
335 fatal("%s: bad key: %s", __func__, ssh_err(r));
djm@openbsd.org93f02102019-01-20 22:51:37 +0000336 wrap_key(k);
Damien Miller7ea845e2010-02-12 09:21:02 +1100337 (*keysp)[i] = k;
Darren Tuckera627d422013-06-02 07:31:17 +1000338 free(blob);
Damien Miller7ea845e2010-02-12 09:21:02 +1100339 }
djm@openbsd.org93f02102019-01-20 22:51:37 +0000340 } else if (type == SSH2_AGENT_FAILURE) {
341 if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
342 nkeys = -1;
Damien Miller7ea845e2010-02-12 09:21:02 +1100343 } else {
344 nkeys = -1;
345 }
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000346 sshbuf_free(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100347 return (nkeys);
348}
349
350int
351pkcs11_del_provider(char *name)
352{
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000353 int r, ret = -1;
354 struct sshbuf *msg;
Damien Miller7ea845e2010-02-12 09:21:02 +1100355
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000356 if ((msg = sshbuf_new()) == NULL)
357 fatal("%s: sshbuf_new failed", __func__);
358 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 ||
359 (r = sshbuf_put_cstring(msg, name)) != 0 ||
360 (r = sshbuf_put_cstring(msg, "")) != 0)
361 fatal("%s: buffer error: %s", __func__, ssh_err(r));
362 send_msg(msg);
363 sshbuf_reset(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100364
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000365 if (recv_msg(msg) == SSH_AGENT_SUCCESS)
Damien Miller7ea845e2010-02-12 09:21:02 +1100366 ret = 0;
markus@openbsd.orgff55f4a2018-07-09 20:39:28 +0000367 sshbuf_free(msg);
Damien Miller7ea845e2010-02-12 09:21:02 +1100368 return (ret);
369}
Damien Millerdfa41562010-02-12 10:06:28 +1100370
371#endif /* ENABLE_PKCS11 */