1/* $OpenBSD: ssh-pkcs11-client.c,v 1.3 2012/01/16 20:34:09 miod Exp $ */
2/*
3 * Copyright (c) 2010 Markus Friedl.  All rights reserved.
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include "includes.h"
19
20#ifdef ENABLE_PKCS11
21
22#include <sys/types.h>
23#ifdef HAVE_SYS_TIME_H
24# include <sys/time.h>
25#endif
26#include <sys/socket.h>
27
28#include <stdarg.h>
29#include <string.h>
30#include <unistd.h>
31#include <errno.h>
32
33#include "pathnames.h"
34#include "xmalloc.h"
35#include "buffer.h"
36#include "log.h"
37#include "misc.h"
38#include "key.h"
39#include "authfd.h"
40#include "atomicio.h"
41#include "ssh-pkcs11.h"
42
43/* borrows code from sftp-server and ssh-agent */
44
45int fd = -1;
46pid_t pid = -1;
47
48static void
49send_msg(Buffer *m)
50{
51	u_char buf[4];
52	int mlen = buffer_len(m);
53
54	put_u32(buf, mlen);
55	if (atomicio(vwrite, fd, buf, 4) != 4 ||
56	    atomicio(vwrite, fd, buffer_ptr(m),
57	    buffer_len(m)) != buffer_len(m))
58		error("write to helper failed");
59	buffer_consume(m, mlen);
60}
61
62static int
63recv_msg(Buffer *m)
64{
65	u_int l, len;
66	u_char buf[1024];
67
68	if ((len = atomicio(read, fd, buf, 4)) != 4) {
69		error("read from helper failed: %u", len);
70		return (0); /* XXX */
71	}
72	len = get_u32(buf);
73	if (len > 256 * 1024)
74		fatal("response too long: %u", len);
75	/* read len bytes into m */
76	buffer_clear(m);
77	while (len > 0) {
78		l = len;
79		if (l > sizeof(buf))
80			l = sizeof(buf);
81		if (atomicio(read, fd, buf, l) != l) {
82			error("response from helper failed.");
83			return (0); /* XXX */
84		}
85		buffer_append(m, buf, l);
86		len -= l;
87	}
88	return (buffer_get_char(m));
89}
90
91int
92pkcs11_init(int interactive)
93{
94	return (0);
95}
96
97void
98pkcs11_terminate(void)
99{
100	close(fd);
101}
102
103static int
104pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
105    int padding)
106{
107	Key key;
108	u_char *blob, *signature = NULL;
109	u_int blen, slen = 0;
110	int ret = -1;
111	Buffer msg;
112
113	if (padding != RSA_PKCS1_PADDING)
114		return (-1);
115	key.type = KEY_RSA;
116	key.rsa = rsa;
117	if (key_to_blob(&key, &blob, &blen) == 0)
118		return -1;
119	buffer_init(&msg);
120	buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
121	buffer_put_string(&msg, blob, blen);
122	buffer_put_string(&msg, from, flen);
123	buffer_put_int(&msg, 0);
124	xfree(blob);
125	send_msg(&msg);
126	buffer_clear(&msg);
127
128	if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
129		signature = buffer_get_string(&msg, &slen);
130		if (slen <= (u_int)RSA_size(rsa)) {
131			memcpy(to, signature, slen);
132			ret = slen;
133		}
134		xfree(signature);
135	}
136	buffer_free(&msg);
137	return (ret);
138}
139
140/* redirect the private key encrypt operation to the ssh-pkcs11-helper */
141static int
142wrap_key(RSA *rsa)
143{
144	static RSA_METHOD helper_rsa;
145
146	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
147	helper_rsa.name = "ssh-pkcs11-helper";
148	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
149	RSA_set_method(rsa, &helper_rsa);
150	return (0);
151}
152
153static int
154pkcs11_start_helper(void)
155{
156	int pair[2];
157
158	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
159		error("socketpair: %s", strerror(errno));
160		return (-1);
161	}
162	if ((pid = fork()) == -1) {
163		error("fork: %s", strerror(errno));
164		return (-1);
165	} else if (pid == 0) {
166		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
167		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
168			fprintf(stderr, "dup2: %s\n", strerror(errno));
169			_exit(1);
170		}
171		close(pair[0]);
172		close(pair[1]);
173		execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
174		    (char *) 0);
175		fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
176		    strerror(errno));
177		_exit(1);
178	}
179	close(pair[1]);
180	fd = pair[0];
181	return (0);
182}
183
184int
185pkcs11_add_provider(char *name, char *pin, Key ***keysp)
186{
187	Key *k;
188	int i, nkeys;
189	u_char *blob;
190	u_int blen;
191	Buffer msg;
192
193	if (fd < 0 && pkcs11_start_helper() < 0)
194		return (-1);
195
196	buffer_init(&msg);
197	buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
198	buffer_put_cstring(&msg, name);
199	buffer_put_cstring(&msg, pin);
200	send_msg(&msg);
201	buffer_clear(&msg);
202
203	if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
204		nkeys = buffer_get_int(&msg);
205		*keysp = xcalloc(nkeys, sizeof(Key *));
206		for (i = 0; i < nkeys; i++) {
207			blob = buffer_get_string(&msg, &blen);
208			xfree(buffer_get_string(&msg, NULL));
209			k = key_from_blob(blob, blen);
210			wrap_key(k->rsa);
211			(*keysp)[i] = k;
212			xfree(blob);
213		}
214	} else {
215		nkeys = -1;
216	}
217	buffer_free(&msg);
218	return (nkeys);
219}
220
221int
222pkcs11_del_provider(char *name)
223{
224	int ret = -1;
225	Buffer msg;
226
227	buffer_init(&msg);
228	buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
229	buffer_put_cstring(&msg, name);
230	buffer_put_cstring(&msg, "");
231	send_msg(&msg);
232	buffer_clear(&msg);
233
234	if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
235		ret = 0;
236	buffer_free(&msg);
237	return (ret);
238}
239
240#endif /* ENABLE_PKCS11 */
241