xref: /netbsd-src/crypto/external/bsd/openssh/dist/ssh-pkcs11-client.c (revision 6a493d6bc668897c91594964a732d38505b70cbb)
1 /*	$NetBSD: ssh-pkcs11-client.c,v 1.4 2013/11/08 19:18:25 christos Exp $	*/
2 /* $OpenBSD: ssh-pkcs11-client.c,v 1.4 2013/05/17 00:13:14 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.4 2013/11/08 19:18:25 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 "pathnames.h"
31 #include "xmalloc.h"
32 #include "buffer.h"
33 #include "log.h"
34 #include "misc.h"
35 #include "key.h"
36 #include "authfd.h"
37 #include "atomicio.h"
38 #include "ssh-pkcs11.h"
39 
40 /* borrows code from sftp-server and ssh-agent */
41 
42 int fd = -1;
43 pid_t pid = -1;
44 
45 static void
46 send_msg(Buffer *m)
47 {
48 	u_char buf[4];
49 	int mlen = buffer_len(m);
50 
51 	put_u32(buf, mlen);
52 	if (atomicio(vwrite, fd, buf, 4) != 4 ||
53 	    atomicio(vwrite, fd, buffer_ptr(m),
54 	    buffer_len(m)) != buffer_len(m))
55 		error("write to helper failed");
56 	buffer_consume(m, mlen);
57 }
58 
59 static int
60 recv_msg(Buffer *m)
61 {
62 	u_int l, len;
63 	u_char buf[1024];
64 
65 	if ((len = atomicio(read, fd, buf, 4)) != 4) {
66 		error("read from helper failed: %u", len);
67 		return (0); /* XXX */
68 	}
69 	len = get_u32(buf);
70 	if (len > 256 * 1024)
71 		fatal("response too long: %u", len);
72 	/* read len bytes into m */
73 	buffer_clear(m);
74 	while (len > 0) {
75 		l = len;
76 		if (l > sizeof(buf))
77 			l = sizeof(buf);
78 		if (atomicio(read, fd, buf, l) != l) {
79 			error("response from helper failed.");
80 			return (0); /* XXX */
81 		}
82 		buffer_append(m, buf, l);
83 		len -= l;
84 	}
85 	return (buffer_get_char(m));
86 }
87 
88 int
89 pkcs11_init(int interactive)
90 {
91 	return (0);
92 }
93 
94 void
95 pkcs11_terminate(void)
96 {
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 	Key key;
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 *) 0);
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 	Key *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