ssh-pkcs11-client.c revision 296781
1/* $OpenBSD: ssh-pkcs11-client.c,v 1.6 2015/12/11 00:20:04 mmcc 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 <openssl/rsa.h>
34
35#include "pathnames.h"
36#include "xmalloc.h"
37#include "buffer.h"
38#include "log.h"
39#include "misc.h"
40#include "key.h"
41#include "authfd.h"
42#include "atomicio.h"
43#include "ssh-pkcs11.h"
44
45/* borrows code from sftp-server and ssh-agent */
46
47int fd = -1;
48pid_t pid = -1;
49
50static void
51send_msg(Buffer *m)
52{
53	u_char buf[4];
54	int mlen = buffer_len(m);
55
56	put_u32(buf, mlen);
57	if (atomicio(vwrite, fd, buf, 4) != 4 ||
58	    atomicio(vwrite, fd, buffer_ptr(m),
59	    buffer_len(m)) != buffer_len(m))
60		error("write to helper failed");
61	buffer_consume(m, mlen);
62}
63
64static int
65recv_msg(Buffer *m)
66{
67	u_int l, len;
68	u_char buf[1024];
69
70	if ((len = atomicio(read, fd, buf, 4)) != 4) {
71		error("read from helper failed: %u", len);
72		return (0); /* XXX */
73	}
74	len = get_u32(buf);
75	if (len > 256 * 1024)
76		fatal("response too long: %u", len);
77	/* read len bytes into m */
78	buffer_clear(m);
79	while (len > 0) {
80		l = len;
81		if (l > sizeof(buf))
82			l = sizeof(buf);
83		if (atomicio(read, fd, buf, l) != l) {
84			error("response from helper failed.");
85			return (0); /* XXX */
86		}
87		buffer_append(m, buf, l);
88		len -= l;
89	}
90	return (buffer_get_char(m));
91}
92
93int
94pkcs11_init(int interactive)
95{
96	return (0);
97}
98
99void
100pkcs11_terminate(void)
101{
102	close(fd);
103}
104
105static int
106pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
107    int padding)
108{
109	Key key;
110	u_char *blob, *signature = NULL;
111	u_int blen, slen = 0;
112	int ret = -1;
113	Buffer msg;
114
115	if (padding != RSA_PKCS1_PADDING)
116		return (-1);
117	key.type = KEY_RSA;
118	key.rsa = rsa;
119	if (key_to_blob(&key, &blob, &blen) == 0)
120		return -1;
121	buffer_init(&msg);
122	buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
123	buffer_put_string(&msg, blob, blen);
124	buffer_put_string(&msg, from, flen);
125	buffer_put_int(&msg, 0);
126	free(blob);
127	send_msg(&msg);
128	buffer_clear(&msg);
129
130	if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
131		signature = buffer_get_string(&msg, &slen);
132		if (slen <= (u_int)RSA_size(rsa)) {
133			memcpy(to, signature, slen);
134			ret = slen;
135		}
136		free(signature);
137	}
138	buffer_free(&msg);
139	return (ret);
140}
141
142/* redirect the private key encrypt operation to the ssh-pkcs11-helper */
143static int
144wrap_key(RSA *rsa)
145{
146	static RSA_METHOD helper_rsa;
147
148	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
149	helper_rsa.name = "ssh-pkcs11-helper";
150	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
151	RSA_set_method(rsa, &helper_rsa);
152	return (0);
153}
154
155static int
156pkcs11_start_helper(void)
157{
158	int pair[2];
159
160	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
161		error("socketpair: %s", strerror(errno));
162		return (-1);
163	}
164	if ((pid = fork()) == -1) {
165		error("fork: %s", strerror(errno));
166		return (-1);
167	} else if (pid == 0) {
168		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
169		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
170			fprintf(stderr, "dup2: %s\n", strerror(errno));
171			_exit(1);
172		}
173		close(pair[0]);
174		close(pair[1]);
175		execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
176		    (char *)NULL);
177		fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
178		    strerror(errno));
179		_exit(1);
180	}
181	close(pair[1]);
182	fd = pair[0];
183	return (0);
184}
185
186int
187pkcs11_add_provider(char *name, char *pin, Key ***keysp)
188{
189	Key *k;
190	int i, nkeys;
191	u_char *blob;
192	u_int blen;
193	Buffer msg;
194
195	if (fd < 0 && pkcs11_start_helper() < 0)
196		return (-1);
197
198	buffer_init(&msg);
199	buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
200	buffer_put_cstring(&msg, name);
201	buffer_put_cstring(&msg, pin);
202	send_msg(&msg);
203	buffer_clear(&msg);
204
205	if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
206		nkeys = buffer_get_int(&msg);
207		*keysp = xcalloc(nkeys, sizeof(Key *));
208		for (i = 0; i < nkeys; i++) {
209			blob = buffer_get_string(&msg, &blen);
210			free(buffer_get_string(&msg, NULL));
211			k = key_from_blob(blob, blen);
212			wrap_key(k->rsa);
213			(*keysp)[i] = k;
214			free(blob);
215		}
216	} else {
217		nkeys = -1;
218	}
219	buffer_free(&msg);
220	return (nkeys);
221}
222
223int
224pkcs11_del_provider(char *name)
225{
226	int ret = -1;
227	Buffer msg;
228
229	buffer_init(&msg);
230	buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
231	buffer_put_cstring(&msg, name);
232	buffer_put_cstring(&msg, "");
233	send_msg(&msg);
234	buffer_clear(&msg);
235
236	if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
237		ret = 0;
238	buffer_free(&msg);
239	return (ret);
240}
241
242#endif /* ENABLE_PKCS11 */
243