xref: /freebsd/crypto/openssh/ssh-pkcs11-client.c (revision d0ba1baed3f6e4936a0c1b89c25f6c59168ef6de)
1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.8 2018/02/05 05:37:46 tb 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 
47 int fd = -1;
48 pid_t pid = -1;
49 
50 static void
51 send_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 
64 static int
65 recv_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 
93 int
94 pkcs11_init(int interactive)
95 {
96 	return (0);
97 }
98 
99 void
100 pkcs11_terminate(void)
101 {
102 	if (fd >= 0)
103 		close(fd);
104 }
105 
106 static int
107 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
108     int padding)
109 {
110 	struct sshkey key;	/* XXX */
111 	u_char *blob, *signature = NULL;
112 	u_int blen, slen = 0;
113 	int ret = -1;
114 	Buffer msg;
115 
116 	if (padding != RSA_PKCS1_PADDING)
117 		return (-1);
118 	key.type = KEY_RSA;
119 	key.rsa = rsa;
120 	if (key_to_blob(&key, &blob, &blen) == 0)
121 		return -1;
122 	buffer_init(&msg);
123 	buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
124 	buffer_put_string(&msg, blob, blen);
125 	buffer_put_string(&msg, from, flen);
126 	buffer_put_int(&msg, 0);
127 	free(blob);
128 	send_msg(&msg);
129 	buffer_clear(&msg);
130 
131 	if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
132 		signature = buffer_get_string(&msg, &slen);
133 		if (slen <= (u_int)RSA_size(rsa)) {
134 			memcpy(to, signature, slen);
135 			ret = slen;
136 		}
137 		free(signature);
138 	}
139 	buffer_free(&msg);
140 	return (ret);
141 }
142 
143 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
144 static int
145 wrap_key(RSA *rsa)
146 {
147 	static RSA_METHOD helper_rsa;
148 
149 	memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
150 	helper_rsa.name = "ssh-pkcs11-helper";
151 	helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
152 	RSA_set_method(rsa, &helper_rsa);
153 	return (0);
154 }
155 
156 static int
157 pkcs11_start_helper(void)
158 {
159 	int pair[2];
160 
161 	if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
162 		error("socketpair: %s", strerror(errno));
163 		return (-1);
164 	}
165 	if ((pid = fork()) == -1) {
166 		error("fork: %s", strerror(errno));
167 		return (-1);
168 	} else if (pid == 0) {
169 		if ((dup2(pair[1], STDIN_FILENO) == -1) ||
170 		    (dup2(pair[1], STDOUT_FILENO) == -1)) {
171 			fprintf(stderr, "dup2: %s\n", strerror(errno));
172 			_exit(1);
173 		}
174 		close(pair[0]);
175 		close(pair[1]);
176 		execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
177 		    (char *)NULL);
178 		fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
179 		    strerror(errno));
180 		_exit(1);
181 	}
182 	close(pair[1]);
183 	fd = pair[0];
184 	return (0);
185 }
186 
187 int
188 pkcs11_add_provider(char *name, char *pin, Key ***keysp)
189 {
190 	struct sshkey *k;
191 	int i, nkeys;
192 	u_char *blob;
193 	u_int blen;
194 	Buffer msg;
195 
196 	if (fd < 0 && pkcs11_start_helper() < 0)
197 		return (-1);
198 
199 	buffer_init(&msg);
200 	buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
201 	buffer_put_cstring(&msg, name);
202 	buffer_put_cstring(&msg, pin);
203 	send_msg(&msg);
204 	buffer_clear(&msg);
205 
206 	if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
207 		nkeys = buffer_get_int(&msg);
208 		*keysp = xcalloc(nkeys, sizeof(Key *));
209 		for (i = 0; i < nkeys; i++) {
210 			blob = buffer_get_string(&msg, &blen);
211 			free(buffer_get_string(&msg, NULL));
212 			k = key_from_blob(blob, blen);
213 			wrap_key(k->rsa);
214 			(*keysp)[i] = k;
215 			free(blob);
216 		}
217 	} else {
218 		nkeys = -1;
219 	}
220 	buffer_free(&msg);
221 	return (nkeys);
222 }
223 
224 int
225 pkcs11_del_provider(char *name)
226 {
227 	int ret = -1;
228 	Buffer msg;
229 
230 	buffer_init(&msg);
231 	buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
232 	buffer_put_cstring(&msg, name);
233 	buffer_put_cstring(&msg, "");
234 	send_msg(&msg);
235 	buffer_clear(&msg);
236 
237 	if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
238 		ret = 0;
239 	buffer_free(&msg);
240 	return (ret);
241 }
242 
243 #endif /* ENABLE_PKCS11 */
244