blob: c23d73b7e0b38274e68922c0b27447872696d053 [file] [log] [blame]
Damien Miller7650bc62001-01-30 09:27:26 +11001/* $OpenBSD: ssh-agent.c,v 1.49 2001/01/29 19:47:31 markus Exp $ */
Damien Miller792c5111999-10-29 09:47:09 +10002
Damien Millerd4a8b7e1999-10-27 13:42:43 +10003/*
Damien Miller95def091999-11-25 00:26:21 +11004 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11007 * The authentication agent program.
Damien Millerad833b32000-08-23 10:46:23 +10008 *
Damien Millere4340be2000-09-16 13:29:08 +11009 * As far as I am concerned, the code I have written for this software
10 * can be used freely for any purpose. Any derived versions of this
11 * software must be clearly marked as such, and if the derived work is
12 * incompatible with the protocol description in the RFC file, it must be
13 * called by a name other than "ssh" or "Secure Shell".
14 *
Damien Millerad833b32000-08-23 10:46:23 +100015 * SSH2 implementation,
16 * Copyright (c) 2000 Markus Friedl. All rights reserved.
Damien Millere4340be2000-09-16 13:29:08 +110017 *
18 * Redistribution and use in source and binary forms, with or without
19 * modification, are permitted provided that the following conditions
20 * are met:
21 * 1. Redistributions of source code must retain the above copyright
22 * notice, this list of conditions and the following disclaimer.
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
29 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
32 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
33 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
34 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
35 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
36 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110037 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100038
39#include "includes.h"
Damien Miller7650bc62001-01-30 09:27:26 +110040RCSID("$OpenBSD: ssh-agent.c,v 1.49 2001/01/29 19:47:31 markus Exp $");
Ben Lindstrom226cfa02001-01-22 05:34:40 +000041
42#include <openssl/evp.h>
43#include <openssl/md5.h>
Damien Millerd4a8b7e1999-10-27 13:42:43 +100044
45#include "ssh.h"
46#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100047#include "buffer.h"
48#include "bufaux.h"
49#include "xmalloc.h"
50#include "packet.h"
51#include "getput.h"
52#include "mpaux.h"
Damien Miller994cf142000-07-21 10:19:44 +100053#include "key.h"
54#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000055#include "cipher.h"
Damien Millerad833b32000-08-23 10:46:23 +100056#include "kex.h"
Damien Miller62cee002000-09-23 17:15:56 +110057#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000058#include "log.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100059
Damien Miller95def091999-11-25 00:26:21 +110060typedef struct {
61 int fd;
62 enum {
63 AUTH_UNUSED, AUTH_SOCKET, AUTH_CONNECTION
64 } type;
65 Buffer input;
66 Buffer output;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100067} SocketEntry;
68
Ben Lindstrom46c16222000-12-22 01:43:59 +000069u_int sockets_alloc = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100070SocketEntry *sockets = NULL;
71
Damien Miller95def091999-11-25 00:26:21 +110072typedef struct {
Damien Millerad833b32000-08-23 10:46:23 +100073 Key *key;
Damien Miller95def091999-11-25 00:26:21 +110074 char *comment;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075} Identity;
76
Damien Millerad833b32000-08-23 10:46:23 +100077typedef struct {
78 int nentries;
79 Identity *identities;
80} Idtab;
81
82/* private key table, one per protocol version */
83Idtab idtable[3];
Damien Millerd4a8b7e1999-10-27 13:42:43 +100084
85int max_fd = 0;
86
87/* pid of shell == parent of agent */
Damien Miller166fca82000-04-20 07:42:21 +100088pid_t parent_pid = -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089
90/* pathname and directory for AUTH_SOCKET */
91char socket_name[1024];
92char socket_dir[1024];
93
Damien Miller95def091999-11-25 00:26:21 +110094#ifdef HAVE___PROGNAME
95extern char *__progname;
Ben Lindstrom49a79c02000-11-17 03:47:20 +000096#else
97char *__progname;
98#endif
Damien Miller95def091999-11-25 00:26:21 +110099
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000100int prepare_select(fd_set **, fd_set **, int *);
101
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000102void
Damien Millerad833b32000-08-23 10:46:23 +1000103idtab_init(void)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000104{
Damien Millerad833b32000-08-23 10:46:23 +1000105 int i;
106 for (i = 0; i <=2; i++){
107 idtable[i].identities = NULL;
108 idtable[i].nentries = 0;
109 }
110}
111
112/* return private key table for requested protocol version */
113Idtab *
114idtab_lookup(int version)
115{
116 if (version < 1 || version > 2)
117 fatal("internal error, bad protocol version %d", version);
118 return &idtable[version];
119}
120
121/* return matching private key for given public key */
122Key *
123lookup_private_key(Key *key, int *idx, int version)
124{
125 int i;
126 Idtab *tab = idtab_lookup(version);
127 for (i = 0; i < tab->nentries; i++) {
128 if (key_equal(key, tab->identities[i].key)) {
129 if (idx != NULL)
130 *idx = i;
131 return tab->identities[i].key;
132 }
133 }
134 return NULL;
135}
136
137/* send list of supported public keys to 'client' */
138void
139process_request_identities(SocketEntry *e, int version)
140{
141 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100142 Buffer msg;
143 int i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000144
Damien Miller95def091999-11-25 00:26:21 +1100145 buffer_init(&msg);
Damien Millerad833b32000-08-23 10:46:23 +1000146 buffer_put_char(&msg, (version == 1) ?
147 SSH_AGENT_RSA_IDENTITIES_ANSWER : SSH2_AGENT_IDENTITIES_ANSWER);
148 buffer_put_int(&msg, tab->nentries);
149 for (i = 0; i < tab->nentries; i++) {
150 Identity *id = &tab->identities[i];
Damien Miller0bc1bd82000-11-13 22:57:25 +1100151 if (id->key->type == KEY_RSA1) {
Damien Millerad833b32000-08-23 10:46:23 +1000152 buffer_put_int(&msg, BN_num_bits(id->key->rsa->n));
153 buffer_put_bignum(&msg, id->key->rsa->e);
154 buffer_put_bignum(&msg, id->key->rsa->n);
155 } else {
Ben Lindstrom46c16222000-12-22 01:43:59 +0000156 u_char *blob;
157 u_int blen;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100158 key_to_blob(id->key, &blob, &blen);
Damien Millerad833b32000-08-23 10:46:23 +1000159 buffer_put_string(&msg, blob, blen);
160 xfree(blob);
161 }
162 buffer_put_cstring(&msg, id->comment);
Damien Miller95def091999-11-25 00:26:21 +1100163 }
164 buffer_put_int(&e->output, buffer_len(&msg));
165 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
166 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000167}
168
Damien Millerad833b32000-08-23 10:46:23 +1000169/* ssh1 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170void
Damien Millerad833b32000-08-23 10:46:23 +1000171process_authentication_challenge1(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172{
Damien Millerad833b32000-08-23 10:46:23 +1000173 Key *key, *private;
174 BIGNUM *challenge;
175 int i, len;
Damien Miller95def091999-11-25 00:26:21 +1100176 Buffer msg;
177 MD5_CTX md;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000178 u_char buf[32], mdbuf[16], session_id[16];
179 u_int response_type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000180
Damien Miller95def091999-11-25 00:26:21 +1100181 buffer_init(&msg);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100182 key = key_new(KEY_RSA1);
Damien Miller95def091999-11-25 00:26:21 +1100183 challenge = BN_new();
Damien Millerad833b32000-08-23 10:46:23 +1000184
185 buffer_get_int(&e->input); /* ignored */
186 buffer_get_bignum(&e->input, key->rsa->e);
187 buffer_get_bignum(&e->input, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100188 buffer_get_bignum(&e->input, challenge);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000189
Damien Millerad833b32000-08-23 10:46:23 +1000190 /* Only protocol 1.1 is supported */
191 if (buffer_len(&e->input) == 0)
192 goto failure;
193 buffer_get(&e->input, (char *) session_id, 16);
194 response_type = buffer_get_int(&e->input);
195 if (response_type != 1)
196 goto failure;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000197
Damien Millerad833b32000-08-23 10:46:23 +1000198 private = lookup_private_key(key, NULL, 1);
199 if (private != NULL) {
200 /* Decrypt the challenge using the private key. */
Damien Miller7650bc62001-01-30 09:27:26 +1100201 if (rsa_private_decrypt(challenge, challenge, private->rsa) <= 0)
202 goto failure;
Damien Millerfd7c9111999-11-08 16:15:55 +1100203
Damien Millerad833b32000-08-23 10:46:23 +1000204 /* The response is MD5 of decrypted challenge plus session id. */
205 len = BN_num_bytes(challenge);
206 if (len <= 0 || len > 32) {
207 log("process_authentication_challenge: bad challenge length %d", len);
208 goto failure;
Damien Miller95def091999-11-25 00:26:21 +1100209 }
Damien Millerad833b32000-08-23 10:46:23 +1000210 memset(buf, 0, 32);
211 BN_bn2bin(challenge, buf + 32 - len);
212 MD5_Init(&md);
213 MD5_Update(&md, buf, 32);
214 MD5_Update(&md, session_id, 16);
215 MD5_Final(mdbuf, &md);
216
217 /* Send the response. */
218 buffer_put_char(&msg, SSH_AGENT_RSA_RESPONSE);
219 for (i = 0; i < 16; i++)
220 buffer_put_char(&msg, mdbuf[i]);
221 goto send;
222 }
223
224failure:
225 /* Unknown identity or protocol error. Send failure. */
Damien Miller95def091999-11-25 00:26:21 +1100226 buffer_put_char(&msg, SSH_AGENT_FAILURE);
227send:
228 buffer_put_int(&e->output, buffer_len(&msg));
Damien Millerad833b32000-08-23 10:46:23 +1000229 buffer_append(&e->output, buffer_ptr(&msg), buffer_len(&msg));
230 key_free(key);
Damien Miller95def091999-11-25 00:26:21 +1100231 BN_clear_free(challenge);
Damien Millerad833b32000-08-23 10:46:23 +1000232 buffer_free(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000233}
234
Damien Millerad833b32000-08-23 10:46:23 +1000235/* ssh2 only */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000236void
Damien Millerad833b32000-08-23 10:46:23 +1000237process_sign_request2(SocketEntry *e)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000238{
Damien Millerad833b32000-08-23 10:46:23 +1000239 extern int datafellows;
240 Key *key, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000241 u_char *blob, *data, *signature = NULL;
242 u_int blen, dlen, slen = 0;
Damien Miller62cee002000-09-23 17:15:56 +1100243 int flags;
Damien Millerad833b32000-08-23 10:46:23 +1000244 Buffer msg;
245 int ok = -1;
246
247 datafellows = 0;
Kevin Stevesfa72dda2000-12-15 18:39:12 +0000248
Damien Millerad833b32000-08-23 10:46:23 +1000249 blob = buffer_get_string(&e->input, &blen);
250 data = buffer_get_string(&e->input, &dlen);
Damien Miller62cee002000-09-23 17:15:56 +1100251
252 flags = buffer_get_int(&e->input);
253 if (flags & SSH_AGENT_OLD_SIGNATURE)
254 datafellows = SSH_BUG_SIGBLOB;
Damien Millerad833b32000-08-23 10:46:23 +1000255
Damien Miller0bc1bd82000-11-13 22:57:25 +1100256 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000257 if (key != NULL) {
258 private = lookup_private_key(key, NULL, 2);
259 if (private != NULL)
Damien Miller0bc1bd82000-11-13 22:57:25 +1100260 ok = key_sign(private, &signature, &slen, data, dlen);
Damien Millerad833b32000-08-23 10:46:23 +1000261 }
262 key_free(key);
263 buffer_init(&msg);
264 if (ok == 0) {
265 buffer_put_char(&msg, SSH2_AGENT_SIGN_RESPONSE);
266 buffer_put_string(&msg, signature, slen);
267 } else {
268 buffer_put_char(&msg, SSH_AGENT_FAILURE);
269 }
270 buffer_put_int(&e->output, buffer_len(&msg));
271 buffer_append(&e->output, buffer_ptr(&msg),
272 buffer_len(&msg));
273 buffer_free(&msg);
274 xfree(data);
275 xfree(blob);
276 if (signature != NULL)
277 xfree(signature);
278}
279
280/* shared */
281void
282process_remove_identity(SocketEntry *e, int version)
283{
284 Key *key = NULL, *private;
Ben Lindstrom46c16222000-12-22 01:43:59 +0000285 u_char *blob;
286 u_int blen;
287 u_int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000288 int success = 0;
Damien Miller7e8e8201999-11-16 13:37:16 +1100289
Damien Millerad833b32000-08-23 10:46:23 +1000290 switch(version){
291 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100292 key = key_new(KEY_RSA1);
Damien Millerad833b32000-08-23 10:46:23 +1000293 bits = buffer_get_int(&e->input);
294 buffer_get_bignum(&e->input, key->rsa->e);
295 buffer_get_bignum(&e->input, key->rsa->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000296
Damien Millerad833b32000-08-23 10:46:23 +1000297 if (bits != key_size(key))
298 log("Warning: identity keysize mismatch: actual %d, announced %d",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000299 key_size(key), bits);
Damien Millerad833b32000-08-23 10:46:23 +1000300 break;
301 case 2:
302 blob = buffer_get_string(&e->input, &blen);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100303 key = key_from_blob(blob, blen);
Damien Millerad833b32000-08-23 10:46:23 +1000304 xfree(blob);
305 break;
306 }
307 if (key != NULL) {
308 int idx;
309 private = lookup_private_key(key, &idx, version);
310 if (private != NULL) {
Damien Miller5428f641999-11-25 11:54:57 +1100311 /*
312 * We have this key. Free the old key. Since we
313 * don\'t want to leave empty slots in the middle of
Ben Lindstrom14920292000-11-21 21:24:55 +0000314 * the array, we actually free the key there and move
315 * all the entries between the empty slot and the end
316 * of the array.
Damien Miller5428f641999-11-25 11:54:57 +1100317 */
Damien Millerad833b32000-08-23 10:46:23 +1000318 Idtab *tab = idtab_lookup(version);
319 key_free(tab->identities[idx].key);
320 xfree(tab->identities[idx].comment);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100321 if (tab->nentries < 1)
322 fatal("process_remove_identity: "
323 "internal error: tab->nentries %d",
324 tab->nentries);
Ben Lindstrom14920292000-11-21 21:24:55 +0000325 if (idx != tab->nentries - 1) {
326 int i;
327 for (i = idx; i < tab->nentries - 1; i++)
328 tab->identities[i] = tab->identities[i+1];
329 }
330 tab->identities[tab->nentries - 1].key = NULL;
331 tab->identities[tab->nentries - 1].comment = NULL;
Damien Millerad833b32000-08-23 10:46:23 +1000332 tab->nentries--;
333 success = 1;
Damien Miller95def091999-11-25 00:26:21 +1100334 }
Damien Millerad833b32000-08-23 10:46:23 +1000335 key_free(key);
336 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000337 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000338 buffer_put_char(&e->output,
339 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340}
341
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000342void
Damien Millerad833b32000-08-23 10:46:23 +1000343process_remove_all_identities(SocketEntry *e, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000344{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000345 u_int i;
Damien Millerad833b32000-08-23 10:46:23 +1000346 Idtab *tab = idtab_lookup(version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000347
Damien Miller95def091999-11-25 00:26:21 +1100348 /* Loop over all identities and clear the keys. */
Damien Millerad833b32000-08-23 10:46:23 +1000349 for (i = 0; i < tab->nentries; i++) {
350 key_free(tab->identities[i].key);
351 xfree(tab->identities[i].comment);
Damien Miller95def091999-11-25 00:26:21 +1100352 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Mark that there are no identities. */
Damien Millerad833b32000-08-23 10:46:23 +1000355 tab->nentries = 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000356
357 /* Send success. */
358 buffer_put_int(&e->output, 1);
359 buffer_put_char(&e->output, SSH_AGENT_SUCCESS);
360 return;
Damien Miller95def091999-11-25 00:26:21 +1100361}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000362
Damien Miller95def091999-11-25 00:26:21 +1100363void
Damien Miller0bc1bd82000-11-13 22:57:25 +1100364generate_additional_parameters(RSA *rsa)
365{
366 BIGNUM *aux;
367 BN_CTX *ctx;
368 /* Generate additional parameters */
369 aux = BN_new();
370 ctx = BN_CTX_new();
371
372 BN_sub(aux, rsa->q, BN_value_one());
373 BN_mod(rsa->dmq1, rsa->d, aux, ctx);
374
375 BN_sub(aux, rsa->p, BN_value_one());
376 BN_mod(rsa->dmp1, rsa->d, aux, ctx);
377
378 BN_clear_free(aux);
379 BN_CTX_free(ctx);
380}
381
382void
Damien Millerad833b32000-08-23 10:46:23 +1000383process_add_identity(SocketEntry *e, int version)
Damien Miller95def091999-11-25 00:26:21 +1100384{
Damien Millerad833b32000-08-23 10:46:23 +1000385 Key *k = NULL;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100386 char *type_name;
Damien Millerad833b32000-08-23 10:46:23 +1000387 char *comment;
Damien Miller0bc1bd82000-11-13 22:57:25 +1100388 int type, success = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000389 Idtab *tab = idtab_lookup(version);
Damien Miller95def091999-11-25 00:26:21 +1100390
Damien Millerad833b32000-08-23 10:46:23 +1000391 switch (version) {
392 case 1:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100393 k = key_new_private(KEY_RSA1);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000394 buffer_get_int(&e->input); /* ignored */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100395 buffer_get_bignum(&e->input, k->rsa->n);
396 buffer_get_bignum(&e->input, k->rsa->e);
397 buffer_get_bignum(&e->input, k->rsa->d);
398 buffer_get_bignum(&e->input, k->rsa->iqmp);
Damien Miller95def091999-11-25 00:26:21 +1100399
Damien Millerad833b32000-08-23 10:46:23 +1000400 /* SSH and SSL have p and q swapped */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100401 buffer_get_bignum(&e->input, k->rsa->q); /* p */
402 buffer_get_bignum(&e->input, k->rsa->p); /* q */
Damien Miller95def091999-11-25 00:26:21 +1100403
Damien Millerad833b32000-08-23 10:46:23 +1000404 /* Generate additional parameters */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100405 generate_additional_parameters(k->rsa);
Damien Millerad833b32000-08-23 10:46:23 +1000406 break;
407 case 2:
Damien Miller0bc1bd82000-11-13 22:57:25 +1100408 type_name = buffer_get_string(&e->input, NULL);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000409 type = key_type_from_name(type_name);
Damien Miller0bc1bd82000-11-13 22:57:25 +1100410 xfree(type_name);
411 switch(type) {
412 case KEY_DSA:
413 k = key_new_private(type);
414 buffer_get_bignum2(&e->input, k->dsa->p);
415 buffer_get_bignum2(&e->input, k->dsa->q);
416 buffer_get_bignum2(&e->input, k->dsa->g);
417 buffer_get_bignum2(&e->input, k->dsa->pub_key);
418 buffer_get_bignum2(&e->input, k->dsa->priv_key);
419 break;
420 case KEY_RSA:
421 k = key_new_private(type);
422 buffer_get_bignum2(&e->input, k->rsa->n);
423 buffer_get_bignum2(&e->input, k->rsa->e);
424 buffer_get_bignum2(&e->input, k->rsa->d);
425 buffer_get_bignum2(&e->input, k->rsa->iqmp);
426 buffer_get_bignum2(&e->input, k->rsa->p);
427 buffer_get_bignum2(&e->input, k->rsa->q);
428
429 /* Generate additional parameters */
430 generate_additional_parameters(k->rsa);
431 break;
432 default:
Damien Millerad833b32000-08-23 10:46:23 +1000433 buffer_clear(&e->input);
Damien Millerad833b32000-08-23 10:46:23 +1000434 goto send;
Damien Miller95def091999-11-25 00:26:21 +1100435 }
Damien Millerad833b32000-08-23 10:46:23 +1000436 break;
437 }
Damien Millerad833b32000-08-23 10:46:23 +1000438 comment = buffer_get_string(&e->input, NULL);
439 if (k == NULL) {
440 xfree(comment);
441 goto send;
442 }
443 success = 1;
444 if (lookup_private_key(k, NULL, version) == NULL) {
445 if (tab->nentries == 0)
446 tab->identities = xmalloc(sizeof(Identity));
447 else
448 tab->identities = xrealloc(tab->identities,
449 (tab->nentries + 1) * sizeof(Identity));
450 tab->identities[tab->nentries].key = k;
451 tab->identities[tab->nentries].comment = comment;
452 /* Increment the number of identities. */
453 tab->nentries++;
454 } else {
455 key_free(k);
456 xfree(comment);
457 }
458send:
Damien Miller95def091999-11-25 00:26:21 +1100459 buffer_put_int(&e->output, 1);
Damien Millerad833b32000-08-23 10:46:23 +1000460 buffer_put_char(&e->output,
461 success ? SSH_AGENT_SUCCESS : SSH_AGENT_FAILURE);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000462}
463
Damien Millerad833b32000-08-23 10:46:23 +1000464/* dispatch incoming messages */
465
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000466void
467process_message(SocketEntry *e)
468{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000469 u_int msg_len;
470 u_int type;
471 u_char *cp;
Damien Miller95def091999-11-25 00:26:21 +1100472 if (buffer_len(&e->input) < 5)
473 return; /* Incomplete message. */
Ben Lindstrom46c16222000-12-22 01:43:59 +0000474 cp = (u_char *) buffer_ptr(&e->input);
Damien Miller95def091999-11-25 00:26:21 +1100475 msg_len = GET_32BIT(cp);
476 if (msg_len > 256 * 1024) {
477 shutdown(e->fd, SHUT_RDWR);
478 close(e->fd);
479 e->type = AUTH_UNUSED;
480 return;
481 }
482 if (buffer_len(&e->input) < msg_len + 4)
483 return;
484 buffer_consume(&e->input, 4);
485 type = buffer_get_char(&e->input);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000486
Damien Miller95def091999-11-25 00:26:21 +1100487 switch (type) {
Damien Millerad833b32000-08-23 10:46:23 +1000488 /* ssh1 */
Damien Miller95def091999-11-25 00:26:21 +1100489 case SSH_AGENTC_RSA_CHALLENGE:
Damien Millerad833b32000-08-23 10:46:23 +1000490 process_authentication_challenge1(e);
491 break;
492 case SSH_AGENTC_REQUEST_RSA_IDENTITIES:
493 process_request_identities(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100494 break;
495 case SSH_AGENTC_ADD_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000496 process_add_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100497 break;
498 case SSH_AGENTC_REMOVE_RSA_IDENTITY:
Damien Millerad833b32000-08-23 10:46:23 +1000499 process_remove_identity(e, 1);
Damien Miller95def091999-11-25 00:26:21 +1100500 break;
501 case SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES:
Damien Millerad833b32000-08-23 10:46:23 +1000502 process_remove_all_identities(e, 1);
503 break;
504 /* ssh2 */
505 case SSH2_AGENTC_SIGN_REQUEST:
506 process_sign_request2(e);
507 break;
508 case SSH2_AGENTC_REQUEST_IDENTITIES:
509 process_request_identities(e, 2);
510 break;
511 case SSH2_AGENTC_ADD_IDENTITY:
512 process_add_identity(e, 2);
513 break;
514 case SSH2_AGENTC_REMOVE_IDENTITY:
515 process_remove_identity(e, 2);
516 break;
517 case SSH2_AGENTC_REMOVE_ALL_IDENTITIES:
518 process_remove_all_identities(e, 2);
Damien Miller95def091999-11-25 00:26:21 +1100519 break;
520 default:
521 /* Unknown message. Respond with failure. */
522 error("Unknown message %d", type);
523 buffer_clear(&e->input);
524 buffer_put_int(&e->output, 1);
525 buffer_put_char(&e->output, SSH_AGENT_FAILURE);
526 break;
527 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000528}
529
530void
531new_socket(int type, int fd)
532{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000533 u_int i, old_alloc;
Damien Miller95def091999-11-25 00:26:21 +1100534 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
535 error("fcntl O_NONBLOCK: %s", strerror(errno));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000536
Damien Miller95def091999-11-25 00:26:21 +1100537 if (fd > max_fd)
538 max_fd = fd;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000539
Damien Miller95def091999-11-25 00:26:21 +1100540 for (i = 0; i < sockets_alloc; i++)
541 if (sockets[i].type == AUTH_UNUSED) {
542 sockets[i].fd = fd;
543 sockets[i].type = type;
544 buffer_init(&sockets[i].input);
545 buffer_init(&sockets[i].output);
546 return;
547 }
548 old_alloc = sockets_alloc;
549 sockets_alloc += 10;
550 if (sockets)
551 sockets = xrealloc(sockets, sockets_alloc * sizeof(sockets[0]));
552 else
553 sockets = xmalloc(sockets_alloc * sizeof(sockets[0]));
554 for (i = old_alloc; i < sockets_alloc; i++)
555 sockets[i].type = AUTH_UNUSED;
556 sockets[old_alloc].type = type;
557 sockets[old_alloc].fd = fd;
558 buffer_init(&sockets[old_alloc].input);
559 buffer_init(&sockets[old_alloc].output);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000560}
561
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000562int
563prepare_select(fd_set **fdrp, fd_set **fdwp, int *fdl)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000564{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000565 u_int i, sz;
566 int n = 0;
567
568 for (i = 0; i < sockets_alloc; i++) {
Damien Miller95def091999-11-25 00:26:21 +1100569 switch (sockets[i].type) {
570 case AUTH_SOCKET:
571 case AUTH_CONNECTION:
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000572 n = MAX(n, sockets[i].fd);
Damien Miller95def091999-11-25 00:26:21 +1100573 break;
574 case AUTH_UNUSED:
575 break;
576 default:
577 fatal("Unknown socket type %d", sockets[i].type);
578 break;
579 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000580 }
581
582 sz = howmany(n+1, NFDBITS) * sizeof(fd_mask);
583 if (*fdrp == NULL || n > *fdl) {
584 if (*fdrp)
585 free(*fdrp);
586 if (*fdwp)
587 free(*fdwp);
588 *fdrp = xmalloc(sz);
589 *fdwp = xmalloc(sz);
590 *fdl = n;
591 }
592 memset(*fdrp, 0, sz);
593 memset(*fdwp, 0, sz);
594
595 for (i = 0; i < sockets_alloc; i++) {
596 switch (sockets[i].type) {
597 case AUTH_SOCKET:
598 case AUTH_CONNECTION:
599 FD_SET(sockets[i].fd, *fdrp);
600 if (buffer_len(&sockets[i].output) > 0)
601 FD_SET(sockets[i].fd, *fdwp);
602 break;
603 default:
604 break;
605 }
606 }
607 return (1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000608}
609
Damien Miller4af51302000-04-16 11:18:38 +1000610void
Damien Miller95def091999-11-25 00:26:21 +1100611after_select(fd_set *readset, fd_set *writeset)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000612{
Ben Lindstrom46c16222000-12-22 01:43:59 +0000613 u_int i;
Damien Miller95def091999-11-25 00:26:21 +1100614 int len, sock;
Damien Miller7684ee12000-03-17 23:40:15 +1100615 socklen_t slen;
Damien Miller95def091999-11-25 00:26:21 +1100616 char buf[1024];
617 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618
Damien Miller95def091999-11-25 00:26:21 +1100619 for (i = 0; i < sockets_alloc; i++)
620 switch (sockets[i].type) {
621 case AUTH_UNUSED:
622 break;
623 case AUTH_SOCKET:
624 if (FD_ISSET(sockets[i].fd, readset)) {
Damien Miller7684ee12000-03-17 23:40:15 +1100625 slen = sizeof(sunaddr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000626 sock = accept(sockets[i].fd,
627 (struct sockaddr *) &sunaddr, &slen);
Damien Miller95def091999-11-25 00:26:21 +1100628 if (sock < 0) {
629 perror("accept from AUTH_SOCKET");
630 break;
631 }
632 new_socket(AUTH_CONNECTION, sock);
633 }
634 break;
635 case AUTH_CONNECTION:
636 if (buffer_len(&sockets[i].output) > 0 &&
637 FD_ISSET(sockets[i].fd, writeset)) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000638 len = write(sockets[i].fd,
639 buffer_ptr(&sockets[i].output),
640 buffer_len(&sockets[i].output));
Damien Miller95def091999-11-25 00:26:21 +1100641 if (len <= 0) {
642 shutdown(sockets[i].fd, SHUT_RDWR);
643 close(sockets[i].fd);
644 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000645 buffer_free(&sockets[i].input);
646 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100647 break;
648 }
649 buffer_consume(&sockets[i].output, len);
650 }
651 if (FD_ISSET(sockets[i].fd, readset)) {
652 len = read(sockets[i].fd, buf, sizeof(buf));
653 if (len <= 0) {
654 shutdown(sockets[i].fd, SHUT_RDWR);
655 close(sockets[i].fd);
656 sockets[i].type = AUTH_UNUSED;
Damien Millera552faf2000-04-21 15:55:20 +1000657 buffer_free(&sockets[i].input);
658 buffer_free(&sockets[i].output);
Damien Miller95def091999-11-25 00:26:21 +1100659 break;
660 }
661 buffer_append(&sockets[i].input, buf, len);
662 process_message(&sockets[i]);
663 }
664 break;
665 default:
666 fatal("Unknown type %d", sockets[i].type);
667 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000668}
669
670void
671check_parent_exists(int sig)
672{
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000673 int save_errno = errno;
674
Damien Miller166fca82000-04-20 07:42:21 +1000675 if (parent_pid != -1 && kill(parent_pid, 0) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100676 /* printf("Parent has died - Authentication agent exiting.\n"); */
677 exit(1);
678 }
679 signal(SIGALRM, check_parent_exists);
680 alarm(10);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000681 errno = save_errno;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000682}
683
Damien Miller792c5111999-10-29 09:47:09 +1000684void
685cleanup_socket(void)
686{
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000687 if (socket_name[0])
688 unlink(socket_name);
689 if (socket_dir[0])
690 rmdir(socket_dir);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000691}
692
Damien Miller792c5111999-10-29 09:47:09 +1000693void
694cleanup_exit(int i)
695{
Damien Miller95def091999-11-25 00:26:21 +1100696 cleanup_socket();
697 exit(i);
Damien Miller792c5111999-10-29 09:47:09 +1000698}
699
700void
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000701cleanup_handler(int sig)
702{
703 cleanup_socket();
704 _exit(2);
705}
706
707void
Damien Miller792c5111999-10-29 09:47:09 +1000708usage()
709{
Damien Miller95def091999-11-25 00:26:21 +1100710 fprintf(stderr, "ssh-agent version %s\n", SSH_VERSION);
711 fprintf(stderr, "Usage: %s [-c | -s] [-k] [command {args...]]\n",
Kevin Steves29265862001-01-24 14:06:28 +0000712 __progname);
Damien Miller95def091999-11-25 00:26:21 +1100713 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000714}
715
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000716int
717main(int ac, char **av)
718{
Damien Miller95def091999-11-25 00:26:21 +1100719 int sock, c_flag = 0, k_flag = 0, s_flag = 0, ch;
720 struct sockaddr_un sunaddr;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000721#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000722 struct rlimit rlim;
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000723#endif
Damien Miller95def091999-11-25 00:26:21 +1100724 pid_t pid;
725 char *shell, *format, *pidstr, pidstrbuf[1 + 3 * sizeof pid];
Damien Miller615f9392000-05-17 22:53:33 +1000726 extern int optind;
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000727 fd_set *readsetp = NULL, *writesetp = NULL;
Kevin Stevesde41bc62000-12-14 00:04:08 +0000728
Ben Lindstrom49a79c02000-11-17 03:47:20 +0000729 __progname = get_progname(av[0]);
Damien Millerf9b625c2000-07-09 22:42:32 +1000730 init_rng();
Kevin Stevesef4eea92001-02-05 12:42:17 +0000731
Damien Millerd0cff3e2000-04-20 23:12:58 +1000732#ifdef __GNU_LIBRARY__
733 while ((ch = getopt(ac, av, "+cks")) != -1) {
734#else /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100735 while ((ch = getopt(ac, av, "cks")) != -1) {
Damien Millerd0cff3e2000-04-20 23:12:58 +1000736#endif /* __GNU_LIBRARY__ */
Damien Miller95def091999-11-25 00:26:21 +1100737 switch (ch) {
738 case 'c':
739 if (s_flag)
740 usage();
741 c_flag++;
742 break;
743 case 'k':
744 k_flag++;
745 break;
746 case 's':
747 if (c_flag)
748 usage();
749 s_flag++;
750 break;
751 default:
752 usage();
753 }
Damien Miller792c5111999-10-29 09:47:09 +1000754 }
Damien Miller95def091999-11-25 00:26:21 +1100755 ac -= optind;
756 av += optind;
Damien Miller792c5111999-10-29 09:47:09 +1000757
Damien Miller95def091999-11-25 00:26:21 +1100758 if (ac > 0 && (c_flag || k_flag || s_flag))
759 usage();
Damien Miller792c5111999-10-29 09:47:09 +1000760
Damien Miller95def091999-11-25 00:26:21 +1100761 if (ac == 0 && !c_flag && !k_flag && !s_flag) {
762 shell = getenv("SHELL");
763 if (shell != NULL && strncmp(shell + strlen(shell) - 3, "csh", 3) == 0)
764 c_flag = 1;
Damien Miller792c5111999-10-29 09:47:09 +1000765 }
Damien Miller95def091999-11-25 00:26:21 +1100766 if (k_flag) {
767 pidstr = getenv(SSH_AGENTPID_ENV_NAME);
768 if (pidstr == NULL) {
769 fprintf(stderr, "%s not set, cannot kill agent\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000770 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100771 exit(1);
772 }
773 pid = atoi(pidstr);
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000774 if (pid < 1) {
Damien Miller95def091999-11-25 00:26:21 +1100775 fprintf(stderr, "%s=\"%s\", which is not a good PID\n",
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000776 SSH_AGENTPID_ENV_NAME, pidstr);
Damien Miller95def091999-11-25 00:26:21 +1100777 exit(1);
778 }
779 if (kill(pid, SIGTERM) == -1) {
780 perror("kill");
781 exit(1);
782 }
783 format = c_flag ? "unsetenv %s;\n" : "unset %s;\n";
784 printf(format, SSH_AUTHSOCKET_ENV_NAME);
785 printf(format, SSH_AGENTPID_ENV_NAME);
786 printf("echo Agent pid %d killed;\n", pid);
787 exit(0);
Damien Miller792c5111999-10-29 09:47:09 +1000788 }
Damien Miller95def091999-11-25 00:26:21 +1100789 parent_pid = getpid();
790
791 /* Create private directory for agent socket */
792 strlcpy(socket_dir, "/tmp/ssh-XXXXXXXX", sizeof socket_dir);
793 if (mkdtemp(socket_dir) == NULL) {
794 perror("mkdtemp: private socket dir");
795 exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000796 }
Damien Miller95def091999-11-25 00:26:21 +1100797 snprintf(socket_name, sizeof socket_name, "%s/agent.%d", socket_dir,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000798 parent_pid);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000799
Damien Miller5428f641999-11-25 11:54:57 +1100800 /*
801 * Create socket early so it will exist before command gets run from
802 * the parent.
803 */
Damien Miller95def091999-11-25 00:26:21 +1100804 sock = socket(AF_UNIX, SOCK_STREAM, 0);
805 if (sock < 0) {
806 perror("socket");
807 cleanup_exit(1);
Damien Miller792c5111999-10-29 09:47:09 +1000808 }
Damien Miller95def091999-11-25 00:26:21 +1100809 memset(&sunaddr, 0, sizeof(sunaddr));
810 sunaddr.sun_family = AF_UNIX;
811 strlcpy(sunaddr.sun_path, socket_name, sizeof(sunaddr.sun_path));
812 if (bind(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
813 perror("bind");
814 cleanup_exit(1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000815 }
Damien Miller95def091999-11-25 00:26:21 +1100816 if (listen(sock, 5) < 0) {
817 perror("listen");
818 cleanup_exit(1);
819 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000820
Damien Miller5428f641999-11-25 11:54:57 +1100821 /*
822 * Fork, and have the parent execute the command, if any, or present
823 * the socket data. The child continues as the authentication agent.
824 */
Damien Miller95def091999-11-25 00:26:21 +1100825 pid = fork();
826 if (pid == -1) {
827 perror("fork");
828 exit(1);
829 }
830 if (pid != 0) { /* Parent - execute the given command. */
831 close(sock);
832 snprintf(pidstrbuf, sizeof pidstrbuf, "%d", pid);
833 if (ac == 0) {
834 format = c_flag ? "setenv %s %s;\n" : "%s=%s; export %s;\n";
835 printf(format, SSH_AUTHSOCKET_ENV_NAME, socket_name,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000836 SSH_AUTHSOCKET_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100837 printf(format, SSH_AGENTPID_ENV_NAME, pidstrbuf,
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000838 SSH_AGENTPID_ENV_NAME);
Damien Miller95def091999-11-25 00:26:21 +1100839 printf("echo Agent pid %d;\n", pid);
840 exit(0);
841 }
Damien Millere4340be2000-09-16 13:29:08 +1100842 if (setenv(SSH_AUTHSOCKET_ENV_NAME, socket_name, 1) == -1 ||
843 setenv(SSH_AGENTPID_ENV_NAME, pidstrbuf, 1) == -1) {
844 perror("setenv");
845 exit(1);
846 }
Damien Miller95def091999-11-25 00:26:21 +1100847 execvp(av[0], av);
848 perror(av[0]);
849 exit(1);
850 }
851 close(0);
852 close(1);
853 close(2);
854
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000855#ifdef HAVE_SETRLIMIT
Ben Lindstromc72745a2000-12-02 19:03:54 +0000856 /* deny core dumps, since memory contains unencrypted private keys */
857 rlim.rlim_cur = rlim.rlim_max = 0;
858 if (setrlimit(RLIMIT_CORE, &rlim) < 0) {
859 perror("setrlimit rlimit_core failed");
860 cleanup_exit(1);
861 }
Ben Lindstrom2c467a22000-12-27 04:57:41 +0000862#endif
Damien Miller95def091999-11-25 00:26:21 +1100863 if (setsid() == -1) {
864 perror("setsid");
865 cleanup_exit(1);
866 }
867 if (atexit(cleanup_socket) < 0) {
868 perror("atexit");
869 cleanup_exit(1);
870 }
871 new_socket(AUTH_SOCKET, sock);
872 if (ac > 0) {
873 signal(SIGALRM, check_parent_exists);
874 alarm(10);
875 }
Damien Millerad833b32000-08-23 10:46:23 +1000876 idtab_init();
Damien Miller95def091999-11-25 00:26:21 +1100877 signal(SIGINT, SIG_IGN);
878 signal(SIGPIPE, SIG_IGN);
Ben Lindstrom77808ab2001-01-26 05:10:34 +0000879 signal(SIGHUP, cleanup_handler);
880 signal(SIGTERM, cleanup_handler);
Damien Miller95def091999-11-25 00:26:21 +1100881 while (1) {
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000882 prepare_select(&readsetp, &writesetp, &max_fd);
883 if (select(max_fd + 1, readsetp, writesetp, NULL, NULL) < 0) {
Damien Miller95def091999-11-25 00:26:21 +1100884 if (errno == EINTR)
885 continue;
886 exit(1);
887 }
Ben Lindstrom226cfa02001-01-22 05:34:40 +0000888 after_select(readsetp, writesetp);
Damien Miller95def091999-11-25 00:26:21 +1100889 }
890 /* NOTREACHED */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000891}