xref: /freebsd/crypto/openssh/ssh-pkcs11-client.c (revision 2284664ef9fcb0baaf59f1ef7df877c0b0f2b187)
1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.10 2018/07/09 21:59:10 markus 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 "sshbuf.h"
38 #include "log.h"
39 #include "misc.h"
40 #include "sshkey.h"
41 #include "authfd.h"
42 #include "atomicio.h"
43 #include "ssh-pkcs11.h"
44 #include "ssherr.h"
45 
46 /* borrows code from sftp-server and ssh-agent */
47 
48 int fd = -1;
49 pid_t pid = -1;
50 
51 static void
52 send_msg(struct sshbuf *m)
53 {
54 	u_char buf[4];
55 	size_t mlen = sshbuf_len(m);
56 	int r;
57 
58 	POKE_U32(buf, mlen);
59 	if (atomicio(vwrite, fd, buf, 4) != 4 ||
60 	    atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
61 	    sshbuf_len(m)) != sshbuf_len(m))
62 		error("write to helper failed");
63 	if ((r = sshbuf_consume(m, mlen)) != 0)
64 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
65 }
66 
67 static int
68 recv_msg(struct sshbuf *m)
69 {
70 	u_int l, len;
71 	u_char c, buf[1024];
72 	int r;
73 
74 	if ((len = atomicio(read, fd, buf, 4)) != 4) {
75 		error("read from helper failed: %u", len);
76 		return (0); /* XXX */
77 	}
78 	len = PEEK_U32(buf);
79 	if (len > 256 * 1024)
80 		fatal("response too long: %u", len);
81 	/* read len bytes into m */
82 	sshbuf_reset(m);
83 	while (len > 0) {
84 		l = len;
85 		if (l > sizeof(buf))
86 			l = sizeof(buf);
87 		if (atomicio(read, fd, buf, l) != l) {
88 			error("response from helper failed.");
89 			return (0); /* XXX */
90 		}
91 		if ((r = sshbuf_put(m, buf, l)) != 0)
92 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
93 		len -= l;
94 	}
95 	if ((r = sshbuf_get_u8(m, &c)) != 0)
96 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
97 	return c;
98 }
99 
100 int
101 pkcs11_init(int interactive)
102 {
103 	return (0);
104 }
105 
106 void
107 pkcs11_terminate(void)
108 {
109 	if (fd >= 0)
110 		close(fd);
111 }
112 
113 static int
114 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
115     int padding)
116 {
117 	struct sshkey key;	/* XXX */
118 	u_char *blob, *signature = NULL;
119 	size_t blen, slen = 0;
120 	int r, ret = -1;
121 	struct sshbuf *msg;
122 
123 	if (padding != RSA_PKCS1_PADDING)
124 		return (-1);
125 	key.type = KEY_RSA;
126 	key.rsa = rsa;
127 	if ((r = sshkey_to_blob(&key, &blob, &blen)) != 0) {
128 		error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
129 		return -1;
130 	}
131 	if ((msg = sshbuf_new()) == NULL)
132 		fatal("%s: sshbuf_new failed", __func__);
133 	if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
134 	    (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
135 	    (r = sshbuf_put_string(msg, from, flen)) != 0 ||
136 	    (r = sshbuf_put_u32(msg, 0)) != 0)
137 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
138 	free(blob);
139 	send_msg(msg);
140 	sshbuf_reset(msg);
141 
142 	if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
143 		if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
144 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
145 		if (slen <= (size_t)RSA_size(rsa)) {
146 			memcpy(to, signature, slen);
147 			ret = slen;
148 		}
149 		free(signature);
150 	}
151 	sshbuf_free(msg);
152 	return (ret);
153 }
154 
155 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
156 static int
157 wrap_key(RSA *rsa)
158 {
159 	static RSA_METHOD helper_rsa;
160 
161 	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
162 	helper_rsa.name = "ssh-pkcs11-helper";
163 	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
164 	RSA_set_method(rsa, &helper_rsa);
165 	return (0);
166 }
167 
168 static int
169 pkcs11_start_helper(void)
170 {
171 	int pair[2];
172 
173 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
174 		error("socketpair: %s", strerror(errno));
175 		return (-1);
176 	}
177 	if ((pid = fork()) == -1) {
178 		error("fork: %s", strerror(errno));
179 		return (-1);
180 	} else if (pid == 0) {
181 		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
182 		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
183 			fprintf(stderr, "dup2: %s\n", strerror(errno));
184 			_exit(1);
185 		}
186 		close(pair[0]);
187 		close(pair[1]);
188 		execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
189 		    (char *)NULL);
190 		fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
191 		    strerror(errno));
192 		_exit(1);
193 	}
194 	close(pair[1]);
195 	fd = pair[0];
196 	return (0);
197 }
198 
199 int
200 pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
201 {
202 	struct sshkey *k;
203 	int r;
204 	u_char *blob;
205 	size_t blen;
206 	u_int nkeys, i;
207 	struct sshbuf *msg;
208 
209 	if (fd < 0 && pkcs11_start_helper() < 0)
210 		return (-1);
211 
212 	if ((msg = sshbuf_new()) == NULL)
213 		fatal("%s: sshbuf_new failed", __func__);
214 	if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
215 	    (r = sshbuf_put_cstring(msg, name)) != 0 ||
216 	    (r = sshbuf_put_cstring(msg, pin)) != 0)
217 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
218 	send_msg(msg);
219 	sshbuf_reset(msg);
220 
221 	if (recv_msg(msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
222 		if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
223 			fatal("%s: buffer error: %s", __func__, ssh_err(r));
224 		*keysp = xcalloc(nkeys, sizeof(struct sshkey *));
225 		for (i = 0; i < nkeys; i++) {
226 			/* XXX clean up properly instead of fatal() */
227 			if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
228 			    (r = sshbuf_skip_string(msg)) != 0)
229 				fatal("%s: buffer error: %s",
230 				    __func__, ssh_err(r));
231 			if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
232 				fatal("%s: bad key: %s", __func__, ssh_err(r));
233 			wrap_key(k->rsa);
234 			(*keysp)[i] = k;
235 			free(blob);
236 		}
237 	} else {
238 		nkeys = -1;
239 	}
240 	sshbuf_free(msg);
241 	return (nkeys);
242 }
243 
244 int
245 pkcs11_del_provider(char *name)
246 {
247 	int r, ret = -1;
248 	struct sshbuf *msg;
249 
250 	if ((msg = sshbuf_new()) == NULL)
251 		fatal("%s: sshbuf_new failed", __func__);
252 	if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 ||
253 	    (r = sshbuf_put_cstring(msg, name)) != 0 ||
254 	    (r = sshbuf_put_cstring(msg, "")) != 0)
255 		fatal("%s: buffer error: %s", __func__, ssh_err(r));
256 	send_msg(msg);
257 	sshbuf_reset(msg);
258 
259 	if (recv_msg(msg) == SSH_AGENT_SUCCESS)
260 		ret = 0;
261 	sshbuf_free(msg);
262 	return (ret);
263 }
264 
265 #endif /* ENABLE_PKCS11 */
266