Annotation of src/usr.bin/ssh/ssh-pkcs11-helper.c, Revision 1.6
1.6 ! djm 1: /* $OpenBSD: ssh-pkcs11-helper.c,v 1.5 2013/05/10 10:13:50 dtucker Exp $ */
1.1 markus 2: /*
3: * Copyright (c) 2010 Markus Friedl. All rights reserved.
4: *
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.
8: *
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.
16: */
17:
18: #include <sys/queue.h>
19: #include <sys/types.h>
20: #include <sys/time.h>
21:
22: #include <stdarg.h>
23: #include <string.h>
24: #include <unistd.h>
25: #include <errno.h>
26:
27: #include "xmalloc.h"
28: #include "buffer.h"
29: #include "log.h"
30: #include "misc.h"
31: #include "key.h"
32: #include "authfd.h"
33: #include "ssh-pkcs11.h"
34:
35: /* borrows code from sftp-server and ssh-agent */
36:
37: struct pkcs11_keyinfo {
38: Key *key;
39: char *providername;
40: TAILQ_ENTRY(pkcs11_keyinfo) next;
41: };
42:
43: TAILQ_HEAD(, pkcs11_keyinfo) pkcs11_keylist;
44:
45: #define MAX_MSG_LENGTH 10240 /*XXX*/
46:
47: /* helper */
48: #define get_int() buffer_get_int(&iqueue);
49: #define get_string(lenp) buffer_get_string(&iqueue, lenp);
50:
51: /* input and output queue */
52: Buffer iqueue;
53: Buffer oqueue;
54:
55: static void
56: add_key(Key *k, char *name)
57: {
58: struct pkcs11_keyinfo *ki;
59:
60: ki = xcalloc(1, sizeof(*ki));
61: ki->providername = xstrdup(name);
62: ki->key = k;
63: TAILQ_INSERT_TAIL(&pkcs11_keylist, ki, next);
64: }
65:
66: static void
67: del_keys_by_name(char *name)
68: {
69: struct pkcs11_keyinfo *ki, *nxt;
70:
71: for (ki = TAILQ_FIRST(&pkcs11_keylist); ki; ki = nxt) {
72: nxt = TAILQ_NEXT(ki, next);
73: if (!strcmp(ki->providername, name)) {
74: TAILQ_REMOVE(&pkcs11_keylist, ki, next);
1.6 ! djm 75: free(ki->providername);
1.1 markus 76: key_free(ki->key);
77: free(ki);
78: }
79: }
80: }
81:
82: /* lookup matching 'private' key */
83: static Key *
84: lookup_key(Key *k)
85: {
86: struct pkcs11_keyinfo *ki;
87:
88: TAILQ_FOREACH(ki, &pkcs11_keylist, next) {
89: debug("check %p %s", ki, ki->providername);
90: if (key_equal(k, ki->key))
91: return (ki->key);
92: }
93: return (NULL);
94: }
95:
96: static void
97: send_msg(Buffer *m)
98: {
99: int mlen = buffer_len(m);
100:
101: buffer_put_int(&oqueue, mlen);
102: buffer_append(&oqueue, buffer_ptr(m), mlen);
103: buffer_consume(m, mlen);
104: }
105:
106: static void
107: process_add(void)
108: {
109: char *name, *pin;
110: Key **keys;
111: int i, nkeys;
112: u_char *blob;
113: u_int blen;
114: Buffer msg;
115:
116: buffer_init(&msg);
117: name = get_string(NULL);
118: pin = get_string(NULL);
119: if ((nkeys = pkcs11_add_provider(name, pin, &keys)) > 0) {
120: buffer_put_char(&msg, SSH2_AGENT_IDENTITIES_ANSWER);
121: buffer_put_int(&msg, nkeys);
122: for (i = 0; i < nkeys; i++) {
123: key_to_blob(keys[i], &blob, &blen);
124: buffer_put_string(&msg, blob, blen);
125: buffer_put_cstring(&msg, name);
1.6 ! djm 126: free(blob);
1.1 markus 127: add_key(keys[i], name);
128: }
1.6 ! djm 129: free(keys);
1.1 markus 130: } else {
131: buffer_put_char(&msg, SSH_AGENT_FAILURE);
132: }
1.6 ! djm 133: free(pin);
! 134: free(name);
1.1 markus 135: send_msg(&msg);
136: buffer_free(&msg);
137: }
138:
139: static void
140: process_del(void)
141: {
142: char *name, *pin;
143: Buffer msg;
144:
145: buffer_init(&msg);
146: name = get_string(NULL);
147: pin = get_string(NULL);
148: del_keys_by_name(name);
149: if (pkcs11_del_provider(name) == 0)
150: buffer_put_char(&msg, SSH_AGENT_SUCCESS);
151: else
152: buffer_put_char(&msg, SSH_AGENT_FAILURE);
1.6 ! djm 153: free(pin);
! 154: free(name);
1.1 markus 155: send_msg(&msg);
156: buffer_free(&msg);
157: }
158:
159: static void
160: process_sign(void)
161: {
162: u_char *blob, *data, *signature = NULL;
163: u_int blen, dlen, slen = 0;
1.4 dtucker 164: int ok = -1, ret;
1.1 markus 165: Key *key, *found;
166: Buffer msg;
167:
168: blob = get_string(&blen);
169: data = get_string(&dlen);
1.4 dtucker 170: (void)get_int(); /* XXX ignore flags */
1.1 markus 171:
172: if ((key = key_from_blob(blob, blen)) != NULL) {
173: if ((found = lookup_key(key)) != NULL) {
174: slen = RSA_size(key->rsa);
175: signature = xmalloc(slen);
176: if ((ret = RSA_private_encrypt(dlen, data, signature,
177: found->rsa, RSA_PKCS1_PADDING)) != -1) {
178: slen = ret;
179: ok = 0;
180: }
181: }
182: key_free(key);
183: }
184: buffer_init(&msg);
185: if (ok == 0) {
186: buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
187: buffer_put_string(&msg, signature, slen);
188: } else {
189: buffer_put_char(&msg, SSH_AGENT_FAILURE);
190: }
1.6 ! djm 191: free(data);
! 192: free(blob);
! 193: free(signature);
1.1 markus 194: send_msg(&msg);
195: buffer_free(&msg);
196: }
197:
198: static void
199: process(void)
200: {
201: u_int msg_len;
202: u_int buf_len;
203: u_int consumed;
204: u_int type;
205: u_char *cp;
206:
207: buf_len = buffer_len(&iqueue);
208: if (buf_len < 5)
209: return; /* Incomplete message. */
210: cp = buffer_ptr(&iqueue);
211: msg_len = get_u32(cp);
212: if (msg_len > MAX_MSG_LENGTH) {
213: error("bad message len %d", msg_len);
214: cleanup_exit(11);
215: }
216: if (buf_len < msg_len + 4)
217: return;
218: buffer_consume(&iqueue, 4);
219: buf_len -= 4;
220: type = buffer_get_char(&iqueue);
221: switch (type) {
222: case SSH_AGENTC_ADD_SMARTCARD_KEY:
223: debug("process_add");
224: process_add();
225: break;
226: case SSH_AGENTC_REMOVE_SMARTCARD_KEY:
227: debug("process_del");
228: process_del();
229: break;
230: case SSH2_AGENTC_SIGN_REQUEST:
231: debug("process_sign");
232: process_sign();
233: break;
234: default:
235: error("Unknown message %d", type);
236: break;
237: }
238: /* discard the remaining bytes from the current packet */
239: if (buf_len < buffer_len(&iqueue)) {
240: error("iqueue grew unexpectedly");
241: cleanup_exit(255);
242: }
243: consumed = buf_len - buffer_len(&iqueue);
244: if (msg_len < consumed) {
245: error("msg_len %d < consumed %d", msg_len, consumed);
246: cleanup_exit(255);
247: }
248: if (msg_len > consumed)
249: buffer_consume(&iqueue, msg_len - consumed);
250: }
251:
252: void
253: cleanup_exit(int i)
254: {
255: /* XXX */
256: _exit(i);
257: }
258:
259: int
260: main(int argc, char **argv)
261: {
262: fd_set *rset, *wset;
263: int in, out, max, log_stderr = 0;
264: ssize_t len, olen, set_size;
265: SyslogFacility log_facility = SYSLOG_FACILITY_AUTH;
266: LogLevel log_level = SYSLOG_LEVEL_ERROR;
267: char buf[4*4096];
1.2 deraadt 268: extern char *__progname;
1.1 markus 269:
270: TAILQ_INIT(&pkcs11_keylist);
271: pkcs11_init(0);
272:
273: log_init(__progname, log_level, log_facility, log_stderr);
274:
275: in = STDIN_FILENO;
276: out = STDOUT_FILENO;
277:
278: max = 0;
279: if (in > max)
280: max = in;
281: if (out > max)
282: max = out;
283:
284: buffer_init(&iqueue);
285: buffer_init(&oqueue);
286:
287: set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
288: rset = (fd_set *)xmalloc(set_size);
289: wset = (fd_set *)xmalloc(set_size);
290:
291: for (;;) {
292: memset(rset, 0, set_size);
293: memset(wset, 0, set_size);
294:
295: /*
296: * Ensure that we can read a full buffer and handle
297: * the worst-case length packet it can generate,
298: * otherwise apply backpressure by stopping reads.
299: */
300: if (buffer_check_alloc(&iqueue, sizeof(buf)) &&
301: buffer_check_alloc(&oqueue, MAX_MSG_LENGTH))
302: FD_SET(in, rset);
303:
304: olen = buffer_len(&oqueue);
305: if (olen > 0)
306: FD_SET(out, wset);
307:
308: if (select(max+1, rset, wset, NULL, NULL) < 0) {
309: if (errno == EINTR)
310: continue;
311: error("select: %s", strerror(errno));
312: cleanup_exit(2);
313: }
314:
315: /* copy stdin to iqueue */
316: if (FD_ISSET(in, rset)) {
317: len = read(in, buf, sizeof buf);
318: if (len == 0) {
319: debug("read eof");
320: cleanup_exit(0);
321: } else if (len < 0) {
322: error("read: %s", strerror(errno));
323: cleanup_exit(1);
324: } else {
325: buffer_append(&iqueue, buf, len);
326: }
327: }
328: /* send oqueue to stdout */
329: if (FD_ISSET(out, wset)) {
330: len = write(out, buffer_ptr(&oqueue), olen);
331: if (len < 0) {
332: error("write: %s", strerror(errno));
333: cleanup_exit(1);
334: } else {
335: buffer_consume(&oqueue, len);
336: }
337: }
338:
339: /*
340: * Process requests from client if we can fit the results
341: * into the output buffer, otherwise stop processing input
342: * and let the output queue drain.
343: */
344: if (buffer_check_alloc(&oqueue, MAX_MSG_LENGTH))
345: process();
346: }
347: }