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