1 /* $OpenBSD: ssh-pkcs11-client.c,v 1.2 2010/02/24 06:12:53 djm 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 "pathnames.h"
41 #include "ssh-pkcs11.h"
43 /* borrows code from sftp-server and ssh-agent */
52 int mlen
= buffer_len(m
);
55 if (atomicio(vwrite
, fd
, buf
, 4) != 4 ||
56 atomicio(vwrite
, fd
, buffer_ptr(m
),
57 buffer_len(m
)) != buffer_len(m
))
58 error("write to helper failed");
59 buffer_consume(m
, mlen
);
68 if ((len
= atomicio(read
, fd
, buf
, 4)) != 4) {
69 error("read from helper failed: %u", len
);
74 fatal("response too long: %u", len
);
75 /* read len bytes into m */
81 if (atomicio(read
, fd
, buf
, l
) != l
) {
82 error("response from helper failed.");
85 buffer_append(m
, buf
, l
);
88 return (buffer_get_char(m
));
92 pkcs11_init(int interactive
)
98 pkcs11_terminate(void)
104 pkcs11_rsa_private_encrypt(int flen
, const u_char
*from
, u_char
*to
, RSA
*rsa
,
108 u_char
*blob
, *signature
= NULL
;
109 u_int blen
, slen
= 0;
113 if (padding
!= RSA_PKCS1_PADDING
)
117 if (key_to_blob(&key
, &blob
, &blen
) == 0)
120 buffer_put_char(&msg
, SSH2_AGENTC_SIGN_REQUEST
);
121 buffer_put_string(&msg
, blob
, blen
);
122 buffer_put_string(&msg
, from
, flen
);
123 buffer_put_int(&msg
, 0);
127 if (recv_msg(&msg
) == SSH2_AGENT_SIGN_RESPONSE
) {
128 signature
= buffer_get_string(&msg
, &slen
);
129 if (slen
<= (u_int
)RSA_size(rsa
)) {
130 memcpy(to
, signature
, slen
);
138 /* redirect the private key encrypt operation to the ssh-pkcs11-helper */
142 static RSA_METHOD helper_rsa
;
144 memcpy(&helper_rsa
, RSA_get_default_method(), sizeof(helper_rsa
));
145 helper_rsa
.name
= "ssh-pkcs11-helper";
146 helper_rsa
.rsa_priv_enc
= pkcs11_rsa_private_encrypt
;
147 RSA_set_method(rsa
, &helper_rsa
);
152 pkcs11_start_helper(void)
156 if (socketpair(AF_UNIX
, SOCK_STREAM
, 0, pair
) == -1) {
157 error("socketpair: %s", strerror(errno
));
160 if ((pid
= fork()) == -1) {
161 error("fork: %s", strerror(errno
));
163 } else if (pid
== 0) {
164 if ((dup2(pair
[1], STDIN_FILENO
) == -1) ||
165 (dup2(pair
[1], STDOUT_FILENO
) == -1)) {
166 fprintf(stderr
, "dup2: %s\n", strerror(errno
));
171 execlp(_PATH_SSH_PKCS11_HELPER
, _PATH_SSH_PKCS11_HELPER
,
173 fprintf(stderr
, "exec: %s: %s\n", _PATH_SSH_PKCS11_HELPER
,
183 pkcs11_add_provider(char *name
, char *pin
, Key
***keysp
)
191 if (fd
< 0 && pkcs11_start_helper() < 0)
195 buffer_put_char(&msg
, SSH_AGENTC_ADD_SMARTCARD_KEY
);
196 buffer_put_cstring(&msg
, name
);
197 buffer_put_cstring(&msg
, pin
);
201 if (recv_msg(&msg
) == SSH2_AGENT_IDENTITIES_ANSWER
) {
202 nkeys
= buffer_get_int(&msg
);
203 *keysp
= xcalloc(nkeys
, sizeof(Key
*));
204 for (i
= 0; i
< nkeys
; i
++) {
205 blob
= buffer_get_string(&msg
, &blen
);
206 xfree(buffer_get_string(&msg
, NULL
));
207 k
= key_from_blob(blob
, blen
);
220 pkcs11_del_provider(char *name
)
226 buffer_put_char(&msg
, SSH_AGENTC_REMOVE_SMARTCARD_KEY
);
227 buffer_put_cstring(&msg
, name
);
228 buffer_put_cstring(&msg
, "");
232 if (recv_msg(&msg
) == SSH_AGENT_SUCCESS
)
238 #endif /* ENABLE_PKCS11 */