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