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