1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.8 2018/02/05 05:37:46 tb 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 "pathnames.h"
43 #include "ssh-pkcs11.h"
45 /* borrows code from sftp-server and ssh-agent */
54 int mlen = buffer_len(m);
57 if (atomicio(vwrite, fd, buf, 4) != 4 ||
58 atomicio(vwrite, fd, buffer_ptr(m),
59 buffer_len(m)) != buffer_len(m))
60 error("write to helper failed");
61 buffer_consume(m, mlen);
70 if ((len = atomicio(read, fd, buf, 4)) != 4) {
71 error("read from helper failed: %u", len);
76 fatal("response too long: %u", len);
77 /* read len bytes into m */
83 if (atomicio(read, fd, buf, l) != l) {
84 error("response from helper failed.");
87 buffer_append(m, buf, l);
90 return (buffer_get_char(m));
94 pkcs11_init(int interactive)
100 pkcs11_terminate(void)
107 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
110 struct sshkey key; /* XXX */
111 u_char *blob, *signature = NULL;
112 u_int blen, slen = 0;
116 if (padding != RSA_PKCS1_PADDING)
120 if (key_to_blob(&key, &blob, &blen) == 0)
123 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
124 buffer_put_string(&msg, blob, blen);
125 buffer_put_string(&msg, from, flen);
126 buffer_put_int(&msg, 0);
131 if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
132 signature = buffer_get_string(&msg, &slen);
133 if (slen <= (u_int)RSA_size(rsa)) {
134 memcpy(to, signature, slen);
143 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
147 static RSA_METHOD *helper_rsa;
149 if ((helper_rsa = RSA_meth_dup(RSA_get_default_method())) == NULL)
150 return (-1); /* XXX but caller isn't checking */
151 RSA_meth_set1_name(helper_rsa, "ssh-pkcs11-helper");
152 RSA_meth_set_priv_enc(helper_rsa, pkcs11_rsa_private_encrypt);
153 RSA_set_method(rsa, helper_rsa);
158 pkcs11_start_helper(void)
162 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
163 error("socketpair: %s", strerror(errno));
166 if ((pid = fork()) == -1) {
167 error("fork: %s", strerror(errno));
169 } else if (pid == 0) {
170 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
171 (dup2(pair[1], STDOUT_FILENO) == -1)) {
172 fprintf(stderr, "dup2: %s\n", strerror(errno));
177 execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
179 fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
189 pkcs11_add_provider(char *name, char *pin, Key ***keysp)
197 if (fd < 0 && pkcs11_start_helper() < 0)
201 buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
202 buffer_put_cstring(&msg, name);
203 buffer_put_cstring(&msg, pin);
207 if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
208 nkeys = buffer_get_int(&msg);
209 *keysp = xcalloc(nkeys, sizeof(Key *));
210 for (i = 0; i < nkeys; i++) {
211 blob = buffer_get_string(&msg, &blen);
212 free(buffer_get_string(&msg, NULL));
213 k = key_from_blob(blob, blen);
226 pkcs11_del_provider(char *name)
232 buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
233 buffer_put_cstring(&msg, name);
234 buffer_put_cstring(&msg, "");
238 if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
244 #endif /* ENABLE_PKCS11 */