xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-pkcs11-client.c (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: ssh-pkcs11-client.c,v 1.12 2018/04/06 18:59:00 christos Exp $	*/
2 /* $OpenBSD: ssh-pkcs11-client.c,v 1.8 2018/02/05 05:37:46 tb Exp $ */
3 /*
4  * Copyright (c) 2010 Markus Friedl.  All rights reserved.
5  *
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 #include "includes.h"
19 __RCSID("$NetBSD: ssh-pkcs11-client.c,v 1.12 2018/04/06 18:59:00 christos Exp $");
20 
21 #include <sys/types.h>
22 #include <sys/time.h>
23 #include <sys/socket.h>
24 
25 #include <stdarg.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <errno.h>
29 
30 #include <openssl/rsa.h>
31 
32 #include "pathnames.h"
33 #include "xmalloc.h"
34 #include "buffer.h"
35 #include "log.h"
36 #include "misc.h"
37 #include "key.h"
38 #include "authfd.h"
39 #include "atomicio.h"
40 #include "ssh-pkcs11.h"
41 
42 /* borrows code from sftp-server and ssh-agent */
43 
44 int fd = -1;
45 pid_t pid = -1;
46 
47 static void
48 send_msg(Buffer *m)
49 {
50 	u_char buf[4];
51 	int mlen = buffer_len(m);
52 
53 	put_u32(buf, mlen);
54 	if (atomicio(vwrite, fd, buf, 4) != 4 ||
55 	    atomicio(vwrite, fd, buffer_ptr(m),
56 	    buffer_len(m)) != buffer_len(m))
57 		error("write to helper failed");
58 	buffer_consume(m, mlen);
59 }
60 
61 static int
62 recv_msg(Buffer *m)
63 {
64 	u_int l, len;
65 	u_char buf[1024];
66 
67 	if ((len = atomicio(read, fd, buf, 4)) != 4) {
68 		error("read from helper failed: %u", len);
69 		return (0); /* XXX */
70 	}
71 	len = get_u32(buf);
72 	if (len > 256 * 1024)
73 		fatal("response too long: %u", len);
74 	/* read len bytes into m */
75 	buffer_clear(m);
76 	while (len > 0) {
77 		l = len;
78 		if (l > sizeof(buf))
79 			l = sizeof(buf);
80 		if (atomicio(read, fd, buf, l) != l) {
81 			error("response from helper failed.");
82 			return (0); /* XXX */
83 		}
84 		buffer_append(m, buf, l);
85 		len -= l;
86 	}
87 	return (buffer_get_char(m));
88 }
89 
90 int
91 pkcs11_init(int interactive)
92 {
93 	return (0);
94 }
95 
96 void
97 pkcs11_terminate(void)
98 {
99 	if (fd >= 0)
100 		close(fd);
101 }
102 
103 static int
104 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
105     int padding)
106 {
107 	struct sshkey key;	/* XXX */
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 	free(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 		free(signature);
135 	}
136 	buffer_free(&msg);
137 	return (ret);
138 }
139 
140 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
141 static int
142 wrap_key(RSA *rsa)
143 {
144 	static RSA_METHOD *helper_rsa;
145 
146 	if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
147 		return (-1); /* XXX but caller isn't checking */
148 	RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
149 	RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
150 	RSA_set_method(rsa, helper_rsa);
151 	return (0);
152 }
153 
154 static int
155 pkcs11_start_helper(void)
156 {
157 	int pair[2];
158 
159 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
160 		error("socketpair: %s", strerror(errno));
161 		return (-1);
162 	}
163 	if ((pid = fork()) == -1) {
164 		error("fork: %s", strerror(errno));
165 		return (-1);
166 	} else if (pid == 0) {
167 		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
168 		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
169 			fprintf(stderr, "dup2: %s\n", strerror(errno));
170 			_exit(1);
171 		}
172 		close(pair[0]);
173 		close(pair[1]);
174 		execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
175 		    (char *)NULL);
176 		fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
177 		    strerror(errno));
178 		_exit(1);
179 	}
180 	close(pair[1]);
181 	fd = pair[0];
182 	return (0);
183 }
184 
185 int
186 pkcs11_add_provider(char *name, char *pin, Key ***keysp)
187 {
188 	struct sshkey *k;
189 	int i, nkeys;
190 	u_char *blob;
191 	u_int blen;
192 	Buffer msg;
193 
194 	if (fd < 0 && pkcs11_start_helper() < 0)
195 		return (-1);
196 
197 	buffer_init(&msg);
198 	buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
199 	buffer_put_cstring(&msg, name);
200 	buffer_put_cstring(&msg, pin);
201 	send_msg(&msg);
202 	buffer_clear(&msg);
203 
204 	if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
205 		nkeys = buffer_get_int(&msg);
206 		*keysp = xcalloc(nkeys, sizeof(Key *));
207 		for (i = 0; i < nkeys; i++) {
208 			blob = buffer_get_string(&msg, &blen);
209 			free(buffer_get_string(&msg, NULL));
210 			k = key_from_blob(blob, blen);
211 			wrap_key(k->rsa);
212 			(*keysp)[i] = k;
213 			free(blob);
214 		}
215 	} else {
216 		nkeys = -1;
217 	}
218 	buffer_free(&msg);
219 	return (nkeys);
220 }
221 
222 int
223 pkcs11_del_provider(char *name)
224 {
225 	int ret = -1;
226 	Buffer msg;
227 
228 	buffer_init(&msg);
229 	buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
230 	buffer_put_cstring(&msg, name);
231 	buffer_put_cstring(&msg, "");
232 	send_msg(&msg);
233 	buffer_clear(&msg);
234 
235 	if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
236 		ret = 0;
237 	buffer_free(&msg);
238 	return (ret);
239 }
240