Deleted Added
full compact
ssh-agent.c (57430) ssh-agent.c (57464)
1/* $OpenBSD: ssh-agent.c,v 1.25 2000/01/02 21:51:03 markus Exp $ */
2
3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 * Created: Wed Mar 29 03:46:59 1995 ylo
8 * The authentication agent program.
1/* $OpenBSD: ssh-agent.c,v 1.25 2000/01/02 21:51:03 markus Exp $ */
2
3/*
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
7 * Created: Wed Mar 29 03:46:59 1995 ylo
8 * The authentication agent program.
9 *
10 * $FreeBSD: head/crypto/openssh/ssh-agent.c 57464 2000-02-25 01:53:12Z green $
9 */
10
11#include "includes.h"
12RCSID("$OpenBSD: ssh-agent.c,v 1.25 2000/01/02 21:51:03 markus Exp $");
13
14#include "ssh.h"
15#include "rsa.h"
16#include "authfd.h"
17#include "buffer.h"
18#include "bufaux.h"
19#include "xmalloc.h"
20#include "packet.h"
21#include "getput.h"
22#include "mpaux.h"
23
11 */
12
13#include "includes.h"
14RCSID("$OpenBSD: ssh-agent.c,v 1.25 2000/01/02 21:51:03 markus Exp $");
15
16#include "ssh.h"
17#include "rsa.h"
18#include "authfd.h"
19#include "buffer.h"
20#include "bufaux.h"
21#include "xmalloc.h"
22#include "packet.h"
23#include "getput.h"
24#include "mpaux.h"
25
24#include
26#include <openssl/md5.h>
25
26typedef struct {
27 int fd;
28 enum {
29 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
30 } type;
31 Buffer input;
32 Buffer output;
33} SocketEntry;
34
35unsigned int sockets_alloc = 0;
36SocketEntry *sockets = NULL;
37
38typedef struct {
39 RSA *key;
40 char *comment;
41} Identity;
42
43unsigned int num_identities = 0;
44Identity *identities = NULL;
45
46int max_fd = 0;
47
48/* pid of shell == parent of agent */
49int parent_pid = -1;
50
51/* pathname and directory for AUTH_SOCKET */
52char socket_name[1024];
53char socket_dir[1024];
54
55extern char *__progname;
56
57void
58process_request_identity(SocketEntry *e)
59{
60 Buffer msg;
61 int i;
62
63 buffer_init(&msg);
64 buffer_put_char(&msg, SSH_AGENT_RSA_IDENTITIES_ANSWER);
65 buffer_put_int(&msg, num_identities);
66 for (i = 0; i < num_identities; i++) {
67 buffer_put_int(&msg, BN_num_bits(identities[i].key->n));
68 buffer_put_bignum(&msg, identities[i].key->e);
69 buffer_put_bignum(&msg, identities[i].key->n);
70 buffer_put_string(&msg, identities[i].comment,
71 strlen(identities[i].comment));
72 }
73 buffer_put_int(&e->output, buffer_len(&msg));
74 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
75 buffer_free(&msg);
76}
77
78void
79process_authentication_challenge(SocketEntry *e)
80{
81 int i, pub_bits, len;
82 BIGNUM *pub_e, *pub_n, *challenge;
83 Buffer msg;
84 MD5_CTX md;
85 unsigned char buf[32], mdbuf[16], session_id[16];
86 unsigned int response_type;
87
88 buffer_init(&msg);
89 pub_e = BN_new();
90 pub_n = BN_new();
91 challenge = BN_new();
92 pub_bits = buffer_get_int(&e->input);
93 buffer_get_bignum(&e->input, pub_e);
94 buffer_get_bignum(&e->input, pub_n);
95 buffer_get_bignum(&e->input, challenge);
96 if (buffer_len(&e->input) == 0) {
97 /* Compatibility code for old servers. */
98 memset(session_id, 0, 16);
99 response_type = 0;
100 } else {
101 /* New code. */
102 buffer_get(&e->input, (char *) session_id, 16);
103 response_type = buffer_get_int(&e->input);
104 }
105 for (i = 0; i < num_identities; i++)
106 if (pub_bits == BN_num_bits(identities[i].key->n) &&
107 BN_cmp(pub_e, identities[i].key->e) == 0 &&
108 BN_cmp(pub_n, identities[i].key->n) == 0) {
109 /* Decrypt the challenge using the private key. */
110 rsa_private_decrypt(challenge, challenge, identities[i].key);
111
112 /* Compute the desired response. */
113 switch (response_type) {
114 case 0:/* As of protocol 1.0 */
115 /* This response type is no longer supported. */
116 log("Compatibility with ssh protocol 1.0 no longer supported.");
117 buffer_put_char(&msg, SSH_AGENT_FAILURE);
118 goto send;
119
120 case 1:/* As of protocol 1.1 */
121 /* The response is MD5 of decrypted challenge plus session id. */
122 len = BN_num_bytes(challenge);
123
124 if (len <= 0 || len > 32) {
125 fatal("process_authentication_challenge: "
126 "bad challenge length %d", len);
127 }
128 memset(buf, 0, 32);
129 BN_bn2bin(challenge, buf + 32 - len);
130 MD5_Init(&md);
131 MD5_Update(&md, buf, 32);
132 MD5_Update(&md, session_id, 16);
133 MD5_Final(mdbuf, &md);
134 break;
135
136 default:
137 fatal("process_authentication_challenge: bad response_type %d",
138 response_type);
139 break;
140 }
141
142 /* Send the response. */
143 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
144 for (i = 0; i < 16; i++)
145 buffer_put_char(&msg, mdbuf[i]);
146
147 goto send;
148 }
149 /* Unknown identity. Send failure. */
150 buffer_put_char(&msg, SSH_AGENT_FAILURE);
151send:
152 buffer_put_int(&e->output, buffer_len(&msg));
153 buffer_append(&e->output, buffer_ptr(&msg),
154 buffer_len(&msg));
155 buffer_free(&msg);
156 BN_clear_free(pub_e);
157 BN_clear_free(pub_n);
158 BN_clear_free(challenge);
159}
160
161void
162process_remove_identity(SocketEntry *e)
163{
164 unsigned int bits;
165 unsigned int i;
166 BIGNUM *dummy, *n;
167
168 dummy = BN_new();
169 n = BN_new();
170
171 /* Get the key from the packet. */
172 bits = buffer_get_int(&e->input);
173 buffer_get_bignum(&e->input, dummy);
174 buffer_get_bignum(&e->input, n);
175
176 if (bits != BN_num_bits(n))
177 error("Warning: identity keysize mismatch: actual %d, announced %d",
178 BN_num_bits(n), bits);
179
180 /* Check if we have the key. */
181 for (i = 0; i < num_identities; i++)
182 if (BN_cmp(identities[i].key->n, n) == 0) {
183 /*
184 * We have this key. Free the old key. Since we
185 * don\'t want to leave empty slots in the middle of
186 * the array, we actually free the key there and copy
187 * data from the last entry.
188 */
189 RSA_free(identities[i].key);
190 xfree(identities[i].comment);
191 if (i < num_identities - 1)
192 identities[i] = identities[num_identities - 1];
193 num_identities--;
194 BN_clear_free(dummy);
195 BN_clear_free(n);
196
197 /* Send success. */
198 buffer_put_int(&e->output, 1);
199 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
200 return;
201 }
202 /* We did not have the key. */
203 BN_clear(dummy);
204 BN_clear(n);
205
206 /* Send failure. */
207 buffer_put_int(&e->output, 1);
208 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
209}
210
211/*
212 * Removes all identities from the agent.
213 */
214void
215process_remove_all_identities(SocketEntry *e)
216{
217 unsigned int i;
218
219 /* Loop over all identities and clear the keys. */
220 for (i = 0; i < num_identities; i++) {
221 RSA_free(identities[i].key);
222 xfree(identities[i].comment);
223 }
224
225 /* Mark that there are no identities. */
226 num_identities = 0;
227
228 /* Send success. */
229 buffer_put_int(&e->output, 1);
230 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
231 return;
232}
233
234/*
235 * Adds an identity to the agent.
236 */
237void
238process_add_identity(SocketEntry *e)
239{
240 RSA *k;
241 int i;
242 BIGNUM *aux;
243 BN_CTX *ctx;
244
245 if (num_identities == 0)
246 identities = xmalloc(sizeof(Identity));
247 else
248 identities = xrealloc(identities, (num_identities + 1) * sizeof(Identity));
249
250 identities[num_identities].key = RSA_new();
251 k = identities[num_identities].key;
252 buffer_get_int(&e->input); /* bits */
253 k->n = BN_new();
254 buffer_get_bignum(&e->input, k->n);
255 k->e = BN_new();
256 buffer_get_bignum(&e->input, k->e);
257 k->d = BN_new();
258 buffer_get_bignum(&e->input, k->d);
259 k->iqmp = BN_new();
260 buffer_get_bignum(&e->input, k->iqmp);
261 /* SSH and SSL have p and q swapped */
262 k->q = BN_new();
263 buffer_get_bignum(&e->input, k->q); /* p */
264 k->p = BN_new();
265 buffer_get_bignum(&e->input, k->p); /* q */
266
267 /* Generate additional parameters */
268 aux = BN_new();
269 ctx = BN_CTX_new();
270
271 BN_sub(aux, k->q, BN_value_one());
272 k->dmq1 = BN_new();
273 BN_mod(k->dmq1, k->d, aux, ctx);
274
275 BN_sub(aux, k->p, BN_value_one());
276 k->dmp1 = BN_new();
277 BN_mod(k->dmp1, k->d, aux, ctx);
278
279 BN_clear_free(aux);
280 BN_CTX_free(ctx);
281
282 identities[num_identities].comment = buffer_get_string(&e->input, NULL);
283
284 /* Check if we already have the key. */
285 for (i = 0; i < num_identities; i++)
286 if (BN_cmp(identities[i].key->n, k->n) == 0) {
287 /*
288 * We already have this key. Clear and free the new
289 * data and return success.
290 */
291 RSA_free(k);
292 xfree(identities[num_identities].comment);
293
294 /* Send success. */
295 buffer_put_int(&e->output, 1);
296 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
297 return;
298 }
299 /* Increment the number of identities. */
300 num_identities++;
301
302 /* Send a success message. */
303 buffer_put_int(&e->output, 1);
304 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
305}
306
307void
308process_message(SocketEntry *e)
309{
310 unsigned int msg_len;
311 unsigned int type;
312 unsigned char *cp;
313 if (buffer_len(&e->input) < 5)
314 return; /* Incomplete message. */
315 cp = (unsigned char *) buffer_ptr(&e->input);
316 msg_len = GET_32BIT(cp);
317 if (msg_len > 256 * 1024) {
318 shutdown(e->fd, SHUT_RDWR);
319 close(e->fd);
320 e->type = AUTH_UNUSED;
321 return;
322 }
323 if (buffer_len(&e->input) < msg_len + 4)
324 return;
325 buffer_consume(&e->input, 4);
326 type = buffer_get_char(&e->input);
327
328 switch (type) {
329 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
330 process_request_identity(e);
331 break;
332 case SSH_AGENTC_RSA_CHALLENGE:
333 process_authentication_challenge(e);
334 break;
335 case SSH_AGENTC_ADD_RSA_IDENTITY:
336 process_add_identity(e);
337 break;
338 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
339 process_remove_identity(e);
340 break;
341 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
342 process_remove_all_identities(e);
343 break;
344 default:
345 /* Unknown message. Respond with failure. */
346 error("Unknown message %d", type);
347 buffer_clear(&e->input);
348 buffer_put_int(&e->output, 1);
349 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
350 break;
351 }
352}
353
354void
355new_socket(int type, int fd)
356{
357 unsigned int i, old_alloc;
358 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
359 error("fcntl O_NONBLOCK: %s", strerror(errno));
360
361 if (fd > max_fd)
362 max_fd = fd;
363
364 for (i = 0; i < sockets_alloc; i++)
365 if (sockets[i].type == AUTH_UNUSED) {
366 sockets[i].fd = fd;
367 sockets[i].type = type;
368 buffer_init(&sockets[i].input);
369 buffer_init(&sockets[i].output);
370 return;
371 }
372 old_alloc = sockets_alloc;
373 sockets_alloc += 10;
374 if (sockets)
375 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
376 else
377 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
378 for (i = old_alloc; i < sockets_alloc; i++)
379 sockets[i].type = AUTH_UNUSED;
380 sockets[old_alloc].type = type;
381 sockets[old_alloc].fd = fd;
382 buffer_init(&sockets[old_alloc].input);
383 buffer_init(&sockets[old_alloc].output);
384}
385
386void
387prepare_select(fd_set *readset, fd_set *writeset)
388{
389 unsigned int i;
390 for (i = 0; i < sockets_alloc; i++)
391 switch (sockets[i].type) {
392 case AUTH_SOCKET:
393 case AUTH_CONNECTION:
394 FD_SET(sockets[i].fd, readset);
395 if (buffer_len(&sockets[i].output) > 0)
396 FD_SET(sockets[i].fd, writeset);
397 break;
398 case AUTH_UNUSED:
399 break;
400 default:
401 fatal("Unknown socket type %d", sockets[i].type);
402 break;
403 }
404}
405
406void
407after_select(fd_set *readset, fd_set *writeset)
408{
409 unsigned int i;
410 int len, sock;
411 char buf[1024];
412 struct sockaddr_un sunaddr;
413
414 for (i = 0; i < sockets_alloc; i++)
415 switch (sockets[i].type) {
416 case AUTH_UNUSED:
417 break;
418 case AUTH_SOCKET:
419 if (FD_ISSET(sockets[i].fd, readset)) {
420 len = sizeof(sunaddr);
421 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &len);
422 if (sock < 0) {
423 perror("accept from AUTH_SOCKET");
424 break;
425 }
426 new_socket(AUTH_CONNECTION, sock);
427 }
428 break;
429 case AUTH_CONNECTION:
430 if (buffer_len(&sockets[i].output) > 0 &&
431 FD_ISSET(sockets[i].fd, writeset)) {
432 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
433 buffer_len(&sockets[i].output));
434 if (len <= 0) {
435 shutdown(sockets[i].fd, SHUT_RDWR);
436 close(sockets[i].fd);
437 sockets[i].type = AUTH_UNUSED;
438 break;
439 }
440 buffer_consume(&sockets[i].output, len);
441 }
442 if (FD_ISSET(sockets[i].fd, readset)) {
443 len = read(sockets[i].fd, buf, sizeof(buf));
444 if (len <= 0) {
445 shutdown(sockets[i].fd, SHUT_RDWR);
446 close(sockets[i].fd);
447 sockets[i].type = AUTH_UNUSED;
448 break;
449 }
450 buffer_append(&sockets[i].input, buf, len);
451 process_message(&sockets[i]);
452 }
453 break;
454 default:
455 fatal("Unknown type %d", sockets[i].type);
456 }
457}
458
459void
460check_parent_exists(int sig)
461{
462 if (kill(parent_pid, 0) < 0) {
463 /* printf("Parent has died - Authentication agent exiting.\n"); */
464 exit(1);
465 }
466 signal(SIGALRM, check_parent_exists);
467 alarm(10);
468}
469
470void
471cleanup_socket(void)
472{
473 remove(socket_name);
474 rmdir(socket_dir);
475}
476
477void
478cleanup_exit(int i)
479{
480 cleanup_socket();
481 exit(i);
482}
483
484void
485usage()
486{
487 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
488 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
489 __progname);
490 exit(1);
491}
492
493int
494main(int ac, char **av)
495{
496 fd_set readset, writeset;
497 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
498 struct sockaddr_un sunaddr;
499 pid_t pid;
500 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
501
502 /* check if RSA support exists */
503 if (rsa_alive() == 0) {
504 fprintf(stderr,
505 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
506 __progname);
507 exit(1);
508 }
509 while ((ch = getopt(ac, av, "cks")) != -1) {
510 switch (ch) {
511 case 'c':
512 if (s_flag)
513 usage();
514 c_flag++;
515 break;
516 case 'k':
517 k_flag++;
518 break;
519 case 's':
520 if (c_flag)
521 usage();
522 s_flag++;
523 break;
524 default:
525 usage();
526 }
527 }
528 ac -= optind;
529 av += optind;
530
531 if (ac > 0 && (c_flag || k_flag || s_flag))
532 usage();
533
534 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
535 shell = getenv("SHELL");
536 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
537 c_flag = 1;
538 }
539 if (k_flag) {
540 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
541 if (pidstr == NULL) {
542 fprintf(stderr, "%s not set, cannot kill agent\n",
543 SSH_AGENTPID_ENV_NAME);
544 exit(1);
545 }
546 pid = atoi(pidstr);
547 if (pid < 1) { /* XXX PID_MAX check too */
548 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
549 SSH_AGENTPID_ENV_NAME, pidstr);
550 exit(1);
551 }
552 if (kill(pid, SIGTERM) == -1) {
553 perror("kill");
554 exit(1);
555 }
556 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
557 printf(format, SSH_AUTHSOCKET_ENV_NAME);
558 printf(format, SSH_AGENTPID_ENV_NAME);
559 printf("echo Agent pid %d killed;\n", pid);
560 exit(0);
561 }
562 parent_pid = getpid();
563
564 /* Create private directory for agent socket */
565 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
566 if (mkdtemp(socket_dir) == NULL) {
567 perror("mkdtemp: private socket dir");
568 exit(1);
569 }
570 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
571 parent_pid);
572
573 /*
574 * Create socket early so it will exist before command gets run from
575 * the parent.
576 */
577 sock = socket(AF_UNIX, SOCK_STREAM, 0);
578 if (sock < 0) {
579 perror("socket");
580 cleanup_exit(1);
581 }
582 memset(&sunaddr, 0, sizeof(sunaddr));
583 sunaddr.sun_family = AF_UNIX;
584 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
585 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
586 perror("bind");
587 cleanup_exit(1);
588 }
589 if (listen(sock, 5) < 0) {
590 perror("listen");
591 cleanup_exit(1);
592 }
593 /*
594 * Fork, and have the parent execute the command, if any, or present
595 * the socket data. The child continues as the authentication agent.
596 */
597 pid = fork();
598 if (pid == -1) {
599 perror("fork");
600 exit(1);
601 }
602 if (pid != 0) { /* Parent - execute the given command. */
603 close(sock);
604 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
605 if (ac == 0) {
606 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
607 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
608 SSH_AUTHSOCKET_ENV_NAME);
609 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
610 SSH_AGENTPID_ENV_NAME);
611 printf("echo Agent pid %d;\n", pid);
612 exit(0);
613 }
614 setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
615 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
616 execvp(av[0], av);
617 perror(av[0]);
618 exit(1);
619 }
620 close(0);
621 close(1);
622 close(2);
623
624 if (setsid() == -1) {
625 perror("setsid");
626 cleanup_exit(1);
627 }
628 if (atexit(cleanup_socket) < 0) {
629 perror("atexit");
630 cleanup_exit(1);
631 }
632 new_socket(AUTH_SOCKET, sock);
633 if (ac > 0) {
634 signal(SIGALRM, check_parent_exists);
635 alarm(10);
636 }
637 signal(SIGINT, SIG_IGN);
638 signal(SIGPIPE, SIG_IGN);
639 signal(SIGHUP, cleanup_exit);
640 signal(SIGTERM, cleanup_exit);
641 while (1) {
642 FD_ZERO(&readset);
643 FD_ZERO(&writeset);
644 prepare_select(&readset, &writeset);
645 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
646 if (errno == EINTR)
647 continue;
648 exit(1);
649 }
650 after_select(&readset, &writeset);
651 }
652 /* NOTREACHED */
653}
27
28typedef struct {
29 int fd;
30 enum {
31 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
32 } type;
33 Buffer input;
34 Buffer output;
35} SocketEntry;
36
37unsigned int sockets_alloc = 0;
38SocketEntry *sockets = NULL;
39
40typedef struct {
41 RSA *key;
42 char *comment;
43} Identity;
44
45unsigned int num_identities = 0;
46Identity *identities = NULL;
47
48int max_fd = 0;
49
50/* pid of shell == parent of agent */
51int parent_pid = -1;
52
53/* pathname and directory for AUTH_SOCKET */
54char socket_name[1024];
55char socket_dir[1024];
56
57extern char *__progname;
58
59void
60process_request_identity(SocketEntry *e)
61{
62 Buffer msg;
63 int i;
64
65 buffer_init(&msg);
66 buffer_put_char(&msg, SSH_AGENT_RSA_IDENTITIES_ANSWER);
67 buffer_put_int(&msg, num_identities);
68 for (i = 0; i < num_identities; i++) {
69 buffer_put_int(&msg, BN_num_bits(identities[i].key->n));
70 buffer_put_bignum(&msg, identities[i].key->e);
71 buffer_put_bignum(&msg, identities[i].key->n);
72 buffer_put_string(&msg, identities[i].comment,
73 strlen(identities[i].comment));
74 }
75 buffer_put_int(&e->output, buffer_len(&msg));
76 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
77 buffer_free(&msg);
78}
79
80void
81process_authentication_challenge(SocketEntry *e)
82{
83 int i, pub_bits, len;
84 BIGNUM *pub_e, *pub_n, *challenge;
85 Buffer msg;
86 MD5_CTX md;
87 unsigned char buf[32], mdbuf[16], session_id[16];
88 unsigned int response_type;
89
90 buffer_init(&msg);
91 pub_e = BN_new();
92 pub_n = BN_new();
93 challenge = BN_new();
94 pub_bits = buffer_get_int(&e->input);
95 buffer_get_bignum(&e->input, pub_e);
96 buffer_get_bignum(&e->input, pub_n);
97 buffer_get_bignum(&e->input, challenge);
98 if (buffer_len(&e->input) == 0) {
99 /* Compatibility code for old servers. */
100 memset(session_id, 0, 16);
101 response_type = 0;
102 } else {
103 /* New code. */
104 buffer_get(&e->input, (char *) session_id, 16);
105 response_type = buffer_get_int(&e->input);
106 }
107 for (i = 0; i < num_identities; i++)
108 if (pub_bits == BN_num_bits(identities[i].key->n) &&
109 BN_cmp(pub_e, identities[i].key->e) == 0 &&
110 BN_cmp(pub_n, identities[i].key->n) == 0) {
111 /* Decrypt the challenge using the private key. */
112 rsa_private_decrypt(challenge, challenge, identities[i].key);
113
114 /* Compute the desired response. */
115 switch (response_type) {
116 case 0:/* As of protocol 1.0 */
117 /* This response type is no longer supported. */
118 log("Compatibility with ssh protocol 1.0 no longer supported.");
119 buffer_put_char(&msg, SSH_AGENT_FAILURE);
120 goto send;
121
122 case 1:/* As of protocol 1.1 */
123 /* The response is MD5 of decrypted challenge plus session id. */
124 len = BN_num_bytes(challenge);
125
126 if (len <= 0 || len > 32) {
127 fatal("process_authentication_challenge: "
128 "bad challenge length %d", len);
129 }
130 memset(buf, 0, 32);
131 BN_bn2bin(challenge, buf + 32 - len);
132 MD5_Init(&md);
133 MD5_Update(&md, buf, 32);
134 MD5_Update(&md, session_id, 16);
135 MD5_Final(mdbuf, &md);
136 break;
137
138 default:
139 fatal("process_authentication_challenge: bad response_type %d",
140 response_type);
141 break;
142 }
143
144 /* Send the response. */
145 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
146 for (i = 0; i < 16; i++)
147 buffer_put_char(&msg, mdbuf[i]);
148
149 goto send;
150 }
151 /* Unknown identity. Send failure. */
152 buffer_put_char(&msg, SSH_AGENT_FAILURE);
153send:
154 buffer_put_int(&e->output, buffer_len(&msg));
155 buffer_append(&e->output, buffer_ptr(&msg),
156 buffer_len(&msg));
157 buffer_free(&msg);
158 BN_clear_free(pub_e);
159 BN_clear_free(pub_n);
160 BN_clear_free(challenge);
161}
162
163void
164process_remove_identity(SocketEntry *e)
165{
166 unsigned int bits;
167 unsigned int i;
168 BIGNUM *dummy, *n;
169
170 dummy = BN_new();
171 n = BN_new();
172
173 /* Get the key from the packet. */
174 bits = buffer_get_int(&e->input);
175 buffer_get_bignum(&e->input, dummy);
176 buffer_get_bignum(&e->input, n);
177
178 if (bits != BN_num_bits(n))
179 error("Warning: identity keysize mismatch: actual %d, announced %d",
180 BN_num_bits(n), bits);
181
182 /* Check if we have the key. */
183 for (i = 0; i < num_identities; i++)
184 if (BN_cmp(identities[i].key->n, n) == 0) {
185 /*
186 * We have this key. Free the old key. Since we
187 * don\'t want to leave empty slots in the middle of
188 * the array, we actually free the key there and copy
189 * data from the last entry.
190 */
191 RSA_free(identities[i].key);
192 xfree(identities[i].comment);
193 if (i < num_identities - 1)
194 identities[i] = identities[num_identities - 1];
195 num_identities--;
196 BN_clear_free(dummy);
197 BN_clear_free(n);
198
199 /* Send success. */
200 buffer_put_int(&e->output, 1);
201 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
202 return;
203 }
204 /* We did not have the key. */
205 BN_clear(dummy);
206 BN_clear(n);
207
208 /* Send failure. */
209 buffer_put_int(&e->output, 1);
210 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
211}
212
213/*
214 * Removes all identities from the agent.
215 */
216void
217process_remove_all_identities(SocketEntry *e)
218{
219 unsigned int i;
220
221 /* Loop over all identities and clear the keys. */
222 for (i = 0; i < num_identities; i++) {
223 RSA_free(identities[i].key);
224 xfree(identities[i].comment);
225 }
226
227 /* Mark that there are no identities. */
228 num_identities = 0;
229
230 /* Send success. */
231 buffer_put_int(&e->output, 1);
232 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
233 return;
234}
235
236/*
237 * Adds an identity to the agent.
238 */
239void
240process_add_identity(SocketEntry *e)
241{
242 RSA *k;
243 int i;
244 BIGNUM *aux;
245 BN_CTX *ctx;
246
247 if (num_identities == 0)
248 identities = xmalloc(sizeof(Identity));
249 else
250 identities = xrealloc(identities, (num_identities + 1) * sizeof(Identity));
251
252 identities[num_identities].key = RSA_new();
253 k = identities[num_identities].key;
254 buffer_get_int(&e->input); /* bits */
255 k->n = BN_new();
256 buffer_get_bignum(&e->input, k->n);
257 k->e = BN_new();
258 buffer_get_bignum(&e->input, k->e);
259 k->d = BN_new();
260 buffer_get_bignum(&e->input, k->d);
261 k->iqmp = BN_new();
262 buffer_get_bignum(&e->input, k->iqmp);
263 /* SSH and SSL have p and q swapped */
264 k->q = BN_new();
265 buffer_get_bignum(&e->input, k->q); /* p */
266 k->p = BN_new();
267 buffer_get_bignum(&e->input, k->p); /* q */
268
269 /* Generate additional parameters */
270 aux = BN_new();
271 ctx = BN_CTX_new();
272
273 BN_sub(aux, k->q, BN_value_one());
274 k->dmq1 = BN_new();
275 BN_mod(k->dmq1, k->d, aux, ctx);
276
277 BN_sub(aux, k->p, BN_value_one());
278 k->dmp1 = BN_new();
279 BN_mod(k->dmp1, k->d, aux, ctx);
280
281 BN_clear_free(aux);
282 BN_CTX_free(ctx);
283
284 identities[num_identities].comment = buffer_get_string(&e->input, NULL);
285
286 /* Check if we already have the key. */
287 for (i = 0; i < num_identities; i++)
288 if (BN_cmp(identities[i].key->n, k->n) == 0) {
289 /*
290 * We already have this key. Clear and free the new
291 * data and return success.
292 */
293 RSA_free(k);
294 xfree(identities[num_identities].comment);
295
296 /* Send success. */
297 buffer_put_int(&e->output, 1);
298 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
299 return;
300 }
301 /* Increment the number of identities. */
302 num_identities++;
303
304 /* Send a success message. */
305 buffer_put_int(&e->output, 1);
306 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
307}
308
309void
310process_message(SocketEntry *e)
311{
312 unsigned int msg_len;
313 unsigned int type;
314 unsigned char *cp;
315 if (buffer_len(&e->input) < 5)
316 return; /* Incomplete message. */
317 cp = (unsigned char *) buffer_ptr(&e->input);
318 msg_len = GET_32BIT(cp);
319 if (msg_len > 256 * 1024) {
320 shutdown(e->fd, SHUT_RDWR);
321 close(e->fd);
322 e->type = AUTH_UNUSED;
323 return;
324 }
325 if (buffer_len(&e->input) < msg_len + 4)
326 return;
327 buffer_consume(&e->input, 4);
328 type = buffer_get_char(&e->input);
329
330 switch (type) {
331 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
332 process_request_identity(e);
333 break;
334 case SSH_AGENTC_RSA_CHALLENGE:
335 process_authentication_challenge(e);
336 break;
337 case SSH_AGENTC_ADD_RSA_IDENTITY:
338 process_add_identity(e);
339 break;
340 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
341 process_remove_identity(e);
342 break;
343 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
344 process_remove_all_identities(e);
345 break;
346 default:
347 /* Unknown message. Respond with failure. */
348 error("Unknown message %d", type);
349 buffer_clear(&e->input);
350 buffer_put_int(&e->output, 1);
351 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
352 break;
353 }
354}
355
356void
357new_socket(int type, int fd)
358{
359 unsigned int i, old_alloc;
360 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
361 error("fcntl O_NONBLOCK: %s", strerror(errno));
362
363 if (fd > max_fd)
364 max_fd = fd;
365
366 for (i = 0; i < sockets_alloc; i++)
367 if (sockets[i].type == AUTH_UNUSED) {
368 sockets[i].fd = fd;
369 sockets[i].type = type;
370 buffer_init(&sockets[i].input);
371 buffer_init(&sockets[i].output);
372 return;
373 }
374 old_alloc = sockets_alloc;
375 sockets_alloc += 10;
376 if (sockets)
377 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
378 else
379 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
380 for (i = old_alloc; i < sockets_alloc; i++)
381 sockets[i].type = AUTH_UNUSED;
382 sockets[old_alloc].type = type;
383 sockets[old_alloc].fd = fd;
384 buffer_init(&sockets[old_alloc].input);
385 buffer_init(&sockets[old_alloc].output);
386}
387
388void
389prepare_select(fd_set *readset, fd_set *writeset)
390{
391 unsigned int i;
392 for (i = 0; i < sockets_alloc; i++)
393 switch (sockets[i].type) {
394 case AUTH_SOCKET:
395 case AUTH_CONNECTION:
396 FD_SET(sockets[i].fd, readset);
397 if (buffer_len(&sockets[i].output) > 0)
398 FD_SET(sockets[i].fd, writeset);
399 break;
400 case AUTH_UNUSED:
401 break;
402 default:
403 fatal("Unknown socket type %d", sockets[i].type);
404 break;
405 }
406}
407
408void
409after_select(fd_set *readset, fd_set *writeset)
410{
411 unsigned int i;
412 int len, sock;
413 char buf[1024];
414 struct sockaddr_un sunaddr;
415
416 for (i = 0; i < sockets_alloc; i++)
417 switch (sockets[i].type) {
418 case AUTH_UNUSED:
419 break;
420 case AUTH_SOCKET:
421 if (FD_ISSET(sockets[i].fd, readset)) {
422 len = sizeof(sunaddr);
423 sock = accept(sockets[i].fd, (struct sockaddr *) & sunaddr, &len);
424 if (sock < 0) {
425 perror("accept from AUTH_SOCKET");
426 break;
427 }
428 new_socket(AUTH_CONNECTION, sock);
429 }
430 break;
431 case AUTH_CONNECTION:
432 if (buffer_len(&sockets[i].output) > 0 &&
433 FD_ISSET(sockets[i].fd, writeset)) {
434 len = write(sockets[i].fd, buffer_ptr(&sockets[i].output),
435 buffer_len(&sockets[i].output));
436 if (len <= 0) {
437 shutdown(sockets[i].fd, SHUT_RDWR);
438 close(sockets[i].fd);
439 sockets[i].type = AUTH_UNUSED;
440 break;
441 }
442 buffer_consume(&sockets[i].output, len);
443 }
444 if (FD_ISSET(sockets[i].fd, readset)) {
445 len = read(sockets[i].fd, buf, sizeof(buf));
446 if (len <= 0) {
447 shutdown(sockets[i].fd, SHUT_RDWR);
448 close(sockets[i].fd);
449 sockets[i].type = AUTH_UNUSED;
450 break;
451 }
452 buffer_append(&sockets[i].input, buf, len);
453 process_message(&sockets[i]);
454 }
455 break;
456 default:
457 fatal("Unknown type %d", sockets[i].type);
458 }
459}
460
461void
462check_parent_exists(int sig)
463{
464 if (kill(parent_pid, 0) < 0) {
465 /* printf("Parent has died - Authentication agent exiting.\n"); */
466 exit(1);
467 }
468 signal(SIGALRM, check_parent_exists);
469 alarm(10);
470}
471
472void
473cleanup_socket(void)
474{
475 remove(socket_name);
476 rmdir(socket_dir);
477}
478
479void
480cleanup_exit(int i)
481{
482 cleanup_socket();
483 exit(i);
484}
485
486void
487usage()
488{
489 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
490 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
491 __progname);
492 exit(1);
493}
494
495int
496main(int ac, char **av)
497{
498 fd_set readset, writeset;
499 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
500 struct sockaddr_un sunaddr;
501 pid_t pid;
502 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
503
504 /* check if RSA support exists */
505 if (rsa_alive() == 0) {
506 fprintf(stderr,
507 "%s: no RSA support in libssl and libcrypto. See ssl(8).\n",
508 __progname);
509 exit(1);
510 }
511 while ((ch = getopt(ac, av, "cks")) != -1) {
512 switch (ch) {
513 case 'c':
514 if (s_flag)
515 usage();
516 c_flag++;
517 break;
518 case 'k':
519 k_flag++;
520 break;
521 case 's':
522 if (c_flag)
523 usage();
524 s_flag++;
525 break;
526 default:
527 usage();
528 }
529 }
530 ac -= optind;
531 av += optind;
532
533 if (ac > 0 && (c_flag || k_flag || s_flag))
534 usage();
535
536 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
537 shell = getenv("SHELL");
538 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
539 c_flag = 1;
540 }
541 if (k_flag) {
542 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
543 if (pidstr == NULL) {
544 fprintf(stderr, "%s not set, cannot kill agent\n",
545 SSH_AGENTPID_ENV_NAME);
546 exit(1);
547 }
548 pid = atoi(pidstr);
549 if (pid < 1) { /* XXX PID_MAX check too */
550 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
551 SSH_AGENTPID_ENV_NAME, pidstr);
552 exit(1);
553 }
554 if (kill(pid, SIGTERM) == -1) {
555 perror("kill");
556 exit(1);
557 }
558 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
559 printf(format, SSH_AUTHSOCKET_ENV_NAME);
560 printf(format, SSH_AGENTPID_ENV_NAME);
561 printf("echo Agent pid %d killed;\n", pid);
562 exit(0);
563 }
564 parent_pid = getpid();
565
566 /* Create private directory for agent socket */
567 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
568 if (mkdtemp(socket_dir) == NULL) {
569 perror("mkdtemp: private socket dir");
570 exit(1);
571 }
572 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
573 parent_pid);
574
575 /*
576 * Create socket early so it will exist before command gets run from
577 * the parent.
578 */
579 sock = socket(AF_UNIX, SOCK_STREAM, 0);
580 if (sock < 0) {
581 perror("socket");
582 cleanup_exit(1);
583 }
584 memset(&sunaddr, 0, sizeof(sunaddr));
585 sunaddr.sun_family = AF_UNIX;
586 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
587 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
588 perror("bind");
589 cleanup_exit(1);
590 }
591 if (listen(sock, 5) < 0) {
592 perror("listen");
593 cleanup_exit(1);
594 }
595 /*
596 * Fork, and have the parent execute the command, if any, or present
597 * the socket data. The child continues as the authentication agent.
598 */
599 pid = fork();
600 if (pid == -1) {
601 perror("fork");
602 exit(1);
603 }
604 if (pid != 0) { /* Parent - execute the given command. */
605 close(sock);
606 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
607 if (ac == 0) {
608 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
609 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
610 SSH_AUTHSOCKET_ENV_NAME);
611 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
612 SSH_AGENTPID_ENV_NAME);
613 printf("echo Agent pid %d;\n", pid);
614 exit(0);
615 }
616 setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1);
617 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1);
618 execvp(av[0], av);
619 perror(av[0]);
620 exit(1);
621 }
622 close(0);
623 close(1);
624 close(2);
625
626 if (setsid() == -1) {
627 perror("setsid");
628 cleanup_exit(1);
629 }
630 if (atexit(cleanup_socket) < 0) {
631 perror("atexit");
632 cleanup_exit(1);
633 }
634 new_socket(AUTH_SOCKET, sock);
635 if (ac > 0) {
636 signal(SIGALRM, check_parent_exists);
637 alarm(10);
638 }
639 signal(SIGINT, SIG_IGN);
640 signal(SIGPIPE, SIG_IGN);
641 signal(SIGHUP, cleanup_exit);
642 signal(SIGTERM, cleanup_exit);
643 while (1) {
644 FD_ZERO(&readset);
645 FD_ZERO(&writeset);
646 prepare_select(&readset, &writeset);
647 if (select(max_fd + 1, &readset, &writeset, NULL, NULL) < 0) {
648 if (errno == EINTR)
649 continue;
650 exit(1);
651 }
652 after_select(&readset, &writeset);
653 }
654 /* NOTREACHED */
655}