1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.6 2015/12/11 00:20:04 mmcc 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)
106 pkcs11_rsa_private_encrypt(int flen, const u_char *from, u_char *to, RSA *rsa,
110 u_char *blob, *signature = NULL;
111 u_int blen, slen = 0;
115 if (padding != RSA_PKCS1_PADDING)
119 if (key_to_blob(&key, &blob, &blen) == 0)
122 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
123 buffer_put_string(&msg, blob, blen);
124 buffer_put_string(&msg, from, flen);
125 buffer_put_int(&msg, 0);
130 if (recv_msg(&msg) == SSH2_AGENT_SIGN_RESPONSE) {
131 signature = buffer_get_string(&msg, &slen);
132 if (slen <= (u_int)RSA_size(rsa)) {
133 memcpy(to, signature, slen);
142 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
146 static RSA_METHOD helper_rsa;
148 memcpy(&helper_rsa, RSA_get_default_method(), sizeof(helper_rsa));
149 helper_rsa.name = "ssh-pkcs11-helper";
150 helper_rsa.rsa_priv_enc = pkcs11_rsa_private_encrypt;
151 RSA_set_method(rsa, &helper_rsa);
156 pkcs11_start_helper(void)
160 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pair) == -1) {
161 error("socketpair: %s", strerror(errno));
164 if ((pid = fork()) == -1) {
165 error("fork: %s", strerror(errno));
167 } else if (pid == 0) {
168 if ((dup2(pair[1], STDIN_FILENO) == -1) ||
169 (dup2(pair[1], STDOUT_FILENO) == -1)) {
170 fprintf(stderr, "dup2: %s\n", strerror(errno));
175 execlp(_PATH_SSH_PKCS11_HELPER, _PATH_SSH_PKCS11_HELPER,
177 fprintf(stderr, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER,
187 pkcs11_add_provider(char *name, char *pin, Key ***keysp)
195 if (fd < 0 && pkcs11_start_helper() < 0)
199 buffer_put_char(&msg, SSH_AGENTC_ADD_SMARTCARD_KEY);
200 buffer_put_cstring(&msg, name);
201 buffer_put_cstring(&msg, pin);
205 if (recv_msg(&msg) == SSH2_AGENT_IDENTITIES_ANSWER) {
206 nkeys = buffer_get_int(&msg);
207 *keysp = xcalloc(nkeys, sizeof(Key *));
208 for (i = 0; i < nkeys; i++) {
209 blob = buffer_get_string(&msg, &blen);
210 free(buffer_get_string(&msg, NULL));
211 k = key_from_blob(blob, blen);
224 pkcs11_del_provider(char *name)
230 buffer_put_char(&msg, SSH_AGENTC_REMOVE_SMARTCARD_KEY);
231 buffer_put_cstring(&msg, name);
232 buffer_put_cstring(&msg, "");
236 if (recv_msg(&msg) == SSH_AGENT_SUCCESS)
242 #endif /* ENABLE_PKCS11 */