1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.10 2018/07/09 21:59:10 markus Exp $ */
3 * Copyright (c) 2010 Markus Friedl. All rights reserved.
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.
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.
22 #include <sys/types.h>
23 #ifdef HAVE_SYS_TIME_H
24 # include <sys/time.h>
26 #include <sys/socket.h>
33 #include <openssl/rsa.h>
35 #include "openbsd-compat/openssl-compat.h"
37 #include "pathnames.h"
45 #include "ssh-pkcs11.h"
48 /* borrows code from sftp-server and ssh-agent */
54 send_msg(struct sshbuf *m)
57 size_t mlen = sshbuf_len(m);
61 if (atomicio(vwrite, fd, buf, 4) != 4 ||
62 atomicio(vwrite, fd, sshbuf_mutable_ptr(m),
63 sshbuf_len(m)) != sshbuf_len(m))
64 error("write to helper failed");
65 if ((r = sshbuf_consume(m, mlen)) != 0)
66 fatal("%s: buffer error: %s", __func__, ssh_err(r));
70 recv_msg(struct sshbuf *m)
76 if ((len = atomicio(read, fd, buf, 4)) != 4) {
77 error("read from helper failed: %u", len);
82 fatal("response too long: %u", len);
83 /* read len bytes into m */
89 if (atomicio(read, fd, buf, l) != l) {
90 error("response from helper failed.");
93 if ((r = sshbuf_put(m, buf, l)) != 0)
94 fatal("%s: buffer error: %s", __func__, ssh_err(r));
97 if ((r = sshbuf_get_u8(m, &c)) != 0)
98 fatal("%s: buffer error: %s", __func__, ssh_err(r));
103 pkcs11_init(int interactive)
109 pkcs11_terminate(void)
116 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
119 struct sshkey key; /* XXX */
120 u_char *blob, *signature = NULL;
121 size_t blen, slen = 0;
125 if (padding != RSA_PKCS1_PADDING)
129 if ((r = sshkey_to_blob(&key, &blob, &blen)) != 0) {
130 error("%s: sshkey_to_blob: %s", __func__, ssh_err(r));
133 if ((msg = sshbuf_new()) == NULL)
134 fatal("%s: sshbuf_new failed", __func__);
135 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
136 (r = sshbuf_put_string(msg, blob, blen)) != 0 ||
137 (r = sshbuf_put_string(msg, from, flen)) != 0 ||
138 (r = sshbuf_put_u32(msg, 0)) != 0)
139 fatal("%s: buffer error: %s", __func__, ssh_err(r));
144 if (recv_msg(msg) == SSH2_AGENT_SIGN_RESPONSE) {
145 if ((r = sshbuf_get_string(msg, &signature, &slen)) != 0)
146 fatal("%s: buffer error: %s", __func__, ssh_err(r));
147 if (slen <= (size_t)RSA_size(rsa)) {
148 memcpy(to, signature, slen);
157 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
161 static RSA_METHOD *helper_rsa;
163 if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
164 fatal("%s: RSA_meth_dup failed", __func__);
165 if (!RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper") ||
166 !RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt))
167 fatal("%s: failed to prepare method", __func__);
168 RSA_set_method(rsa, helper_rsa);
173 pkcs11_start_helper(void)
177 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
178 error("socketpair: %s", strerror(errno));
181 if ((pid = fork()) == -1) {
182 error("fork: %s", strerror(errno));
184 } else if (pid == 0) {
185 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
186 (dup2(pair[1], STDOUT_FILENO) == -1)) {
187 fprintf(stderr, "dup2: %s\n", strerror(errno));
192 execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
194 fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
204 pkcs11_add_provider(char *name, char *pin, struct sshkey ***keysp)
213 if (fd < 0 && pkcs11_start_helper() < 0)
216 if ((msg = sshbuf_new()) == NULL)
217 fatal("%s: sshbuf_new failed", __func__);
218 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_ADD_SMARTCARD_KEY)) != 0 ||
219 (r = sshbuf_put_cstring(msg, name)) != 0 ||
220 (r = sshbuf_put_cstring(msg, pin)) != 0)
221 fatal("%s: buffer error: %s", __func__, ssh_err(r));
225 if (recv_msg(msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
226 if ((r = sshbuf_get_u32(msg, &nkeys)) != 0)
227 fatal("%s: buffer error: %s", __func__, ssh_err(r));
228 *keysp = xcalloc(nkeys, sizeof(struct sshkey *));
229 for (i = 0; i < nkeys; i++) {
230 /* XXX clean up properly instead of fatal() */
231 if ((r = sshbuf_get_string(msg, &blob, &blen)) != 0 ||
232 (r = sshbuf_skip_string(msg)) != 0)
233 fatal("%s: buffer error: %s",
234 __func__, ssh_err(r));
235 if ((r = sshkey_from_blob(blob, blen, &k)) != 0)
236 fatal("%s: bad key: %s", __func__, ssh_err(r));
249 pkcs11_del_provider(char *name)
254 if ((msg = sshbuf_new()) == NULL)
255 fatal("%s: sshbuf_new failed", __func__);
256 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY)) != 0 ||
257 (r = sshbuf_put_cstring(msg, name)) != 0 ||
258 (r = sshbuf_put_cstring(msg, "")) != 0)
259 fatal("%s: buffer error: %s", __func__, ssh_err(r));
263 if (recv_msg(msg) == SSH_AGENT_SUCCESS)
269 #endif /* ENABLE_PKCS11 */