1 /* $OpenBSD: ssh-pkcs11-helper.c,v 1.13 2017/05/30 08:52:19 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.
20 #include <sys/types.h>
21 #ifdef HAVE_SYS_TIME_H
22 # include <sys/time.h>
25 #include "openbsd-compat/sys-queue.h"
38 #include "ssh-pkcs11.h"
42 /* borrows code from sftp-server and ssh-agent */
44 struct pkcs11_keyinfo
{
47 TAILQ_ENTRY(pkcs11_keyinfo
) next
;
50 TAILQ_HEAD(, pkcs11_keyinfo
) pkcs11_keylist
;
52 #define MAX_MSG_LENGTH 10240 /*XXX*/
55 #define get_int() buffer_get_int(&iqueue);
56 #define get_string(lenp) buffer_get_string(&iqueue, lenp);
58 /* input and output queue */
63 add_key(struct sshkey
*k
, char *name
)
65 struct pkcs11_keyinfo
*ki
;
67 ki
= xcalloc(1, sizeof(*ki
));
68 ki
->providername
= xstrdup(name
);
70 TAILQ_INSERT_TAIL(&pkcs11_keylist
, ki
, next
);
74 del_keys_by_name(char *name
)
76 struct pkcs11_keyinfo
*ki
, *nxt
;
78 for (ki
= TAILQ_FIRST(&pkcs11_keylist
); ki
; ki
= nxt
) {
79 nxt
= TAILQ_NEXT(ki
, next
);
80 if (!strcmp(ki
->providername
, name
)) {
81 TAILQ_REMOVE(&pkcs11_keylist
, ki
, next
);
82 free(ki
->providername
);
89 /* lookup matching 'private' key */
90 static struct sshkey
*
91 lookup_key(struct sshkey
*k
)
93 struct pkcs11_keyinfo
*ki
;
95 TAILQ_FOREACH(ki
, &pkcs11_keylist
, next
) {
96 debug("check %p %s", ki
, ki
->providername
);
97 if (key_equal(k
, ki
->key
))
106 int mlen
= buffer_len(m
);
108 buffer_put_int(&oqueue
, mlen
);
109 buffer_append(&oqueue
, buffer_ptr(m
), mlen
);
110 buffer_consume(m
, mlen
);
117 struct sshkey
**keys
;
124 name
= get_string(NULL
);
125 pin
= get_string(NULL
);
126 if ((nkeys
= pkcs11_add_provider(name
, pin
, &keys
)) > 0) {
127 buffer_put_char(&msg
, SSH2_AGENT_IDENTITIES_ANSWER
);
128 buffer_put_int(&msg
, nkeys
);
129 for (i
= 0; i
< nkeys
; i
++) {
130 if (key_to_blob(keys
[i
], &blob
, &blen
) == 0)
132 buffer_put_string(&msg
, blob
, blen
);
133 buffer_put_cstring(&msg
, name
);
135 add_key(keys
[i
], name
);
139 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
154 name
= get_string(NULL
);
155 pin
= get_string(NULL
);
156 del_keys_by_name(name
);
157 if (pkcs11_del_provider(name
) == 0)
158 buffer_put_char(&msg
, SSH_AGENT_SUCCESS
);
160 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
170 u_char
*blob
, *data
, *signature
= NULL
;
171 u_int blen
, dlen
, slen
= 0;
173 struct sshkey
*key
, *found
;
176 blob
= get_string(&blen
);
177 data
= get_string(&dlen
);
178 (void)get_int(); /* XXX ignore flags */
180 if ((key
= key_from_blob(blob
, blen
)) != NULL
) {
181 if ((found
= lookup_key(key
)) != NULL
) {
185 slen
= RSA_size(key
->rsa
);
186 signature
= xmalloc(slen
);
187 if ((ret
= RSA_private_encrypt(dlen
, data
, signature
,
188 found
->rsa
, RSA_PKCS1_PADDING
)) != -1) {
192 #endif /* WITH_OPENSSL */
198 buffer_put_char(&msg
, SSH2_AGENT_SIGN_RESPONSE
);
199 buffer_put_string(&msg
, signature
, slen
);
201 buffer_put_char(&msg
, SSH_AGENT_FAILURE
);
219 buf_len
= buffer_len(&iqueue
);
221 return; /* Incomplete message. */
222 cp
= buffer_ptr(&iqueue
);
223 msg_len
= get_u32(cp
);
224 if (msg_len
> MAX_MSG_LENGTH
) {
225 error("bad message len %d", msg_len
);
228 if (buf_len
< msg_len
+ 4)
230 buffer_consume(&iqueue
, 4);
232 type
= buffer_get_char(&iqueue
);
234 case SSH_AGENTC_ADD_SMARTCARD_KEY
:
235 debug("process_add");
238 case SSH_AGENTC_REMOVE_SMARTCARD_KEY
:
239 debug("process_del");
242 case SSH2_AGENTC_SIGN_REQUEST
:
243 debug("process_sign");
247 error("Unknown message %d", type
);
250 /* discard the remaining bytes from the current packet */
251 if (buf_len
< buffer_len(&iqueue
)) {
252 error("iqueue grew unexpectedly");
255 consumed
= buf_len
- buffer_len(&iqueue
);
256 if (msg_len
< consumed
) {
257 error("msg_len %d < consumed %d", msg_len
, consumed
);
260 if (msg_len
> consumed
)
261 buffer_consume(&iqueue
, msg_len
- consumed
);
272 main(int argc
, char **argv
)
275 int in
, out
, max
, log_stderr
= 0;
276 ssize_t len
, olen
, set_size
;
277 SyslogFacility log_facility
= SYSLOG_FACILITY_AUTH
;
278 LogLevel log_level
= SYSLOG_LEVEL_ERROR
;
281 extern char *__progname
;
283 ssh_malloc_init(); /* must be called before any mallocs */
284 TAILQ_INIT(&pkcs11_keylist
);
288 __progname
= ssh_get_progname(argv
[0]);
290 log_init(__progname
, log_level
, log_facility
, log_stderr
);
301 buffer_init(&iqueue
);
302 buffer_init(&oqueue
);
304 set_size
= howmany(max
+ 1, NFDBITS
) * sizeof(fd_mask
);
305 rset
= xmalloc(set_size
);
306 wset
= xmalloc(set_size
);
309 memset(rset
, 0, set_size
);
310 memset(wset
, 0, set_size
);
313 * Ensure that we can read a full buffer and handle
314 * the worst-case length packet it can generate,
315 * otherwise apply backpressure by stopping reads.
317 if (buffer_check_alloc(&iqueue
, sizeof(buf
)) &&
318 buffer_check_alloc(&oqueue
, MAX_MSG_LENGTH
))
321 olen
= buffer_len(&oqueue
);
325 if (select(max
+1, rset
, wset
, NULL
, NULL
) < 0) {
328 error("select: %s", strerror(errno
));
332 /* copy stdin to iqueue */
333 if (FD_ISSET(in
, rset
)) {
334 len
= read(in
, buf
, sizeof buf
);
338 } else if (len
< 0) {
339 error("read: %s", strerror(errno
));
342 buffer_append(&iqueue
, buf
, len
);
345 /* send oqueue to stdout */
346 if (FD_ISSET(out
, wset
)) {
347 len
= write(out
, buffer_ptr(&oqueue
), olen
);
349 error("write: %s", strerror(errno
));
352 buffer_consume(&oqueue
, len
);
357 * Process requests from client if we can fit the results
358 * into the output buffer, otherwise stop processing input
359 * and let the output queue drain.
361 if (buffer_check_alloc(&oqueue
, MAX_MSG_LENGTH
))
365 #else /* ENABLE_PKCS11 */
367 main(int argc
, char **argv
)
369 extern char *__progname
;
371 __progname
= ssh_get_progname(argv
[0]);
372 log_init(__progname
, SYSLOG_LEVEL_ERROR
, SYSLOG_FACILITY_AUTH
, 0);
373 fatal("PKCS#11 support disabled at compile time");
375 #endif /* ENABLE_PKCS11 */