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