blob: cb7bbd5e70b37f0857c7cec55c9bae7c14b79215 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller95def091999-11-25 00:26:21 +11002 *
3 * authfd.c
4 *
5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
6 *
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
9 *
10 * Created: Wed Mar 29 01:30:28 1995 ylo
11 *
12 * Functions for connecting the local authentication agent.
13 *
14 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100015
16#include "includes.h"
Damien Miller5e7c10e1999-12-16 13:18:04 +110017RCSID("$Id: authfd.c,v 1.10 1999/12/16 02:18:04 damien Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "ssh.h"
20#include "rsa.h"
21#include "authfd.h"
22#include "buffer.h"
23#include "bufaux.h"
24#include "xmalloc.h"
25#include "getput.h"
26
Damien Miller7f6ea021999-10-28 13:25:17 +100027#ifdef HAVE_OPENSSL
Damien Millerd4a8b7e1999-10-27 13:42:43 +100028#include <openssl/rsa.h>
Damien Miller7f6ea021999-10-28 13:25:17 +100029#endif
30#ifdef HAVE_SSL
31#include <ssl/rsa.h>
32#endif
Damien Millerd4a8b7e1999-10-27 13:42:43 +100033
34/* Returns the number of the authentication fd, or -1 if there is none. */
35
36int
37ssh_get_authentication_socket()
38{
Damien Miller95def091999-11-25 00:26:21 +110039 const char *authsocket;
40 int sock;
41 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100042
Damien Miller95def091999-11-25 00:26:21 +110043 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
44 if (!authsocket)
45 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100046
Damien Miller95def091999-11-25 00:26:21 +110047 sunaddr.sun_family = AF_UNIX;
48 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
Damien Miller10f6f6b1999-11-17 17:29:08 +110049
Damien Miller95def091999-11-25 00:26:21 +110050 sock = socket(AF_UNIX, SOCK_STREAM, 0);
51 if (sock < 0)
52 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053
Damien Miller95def091999-11-25 00:26:21 +110054 /* close on exec */
55 if (fcntl(sock, F_SETFD, 1) == -1) {
56 close(sock);
57 return -1;
58 }
59 if (connect(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
60 close(sock);
61 return -1;
62 }
63 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064}
65
Damien Miller5428f641999-11-25 11:54:57 +110066/*
67 * Closes the agent socket if it should be closed (depends on how it was
68 * obtained). The argument must have been returned by
69 * ssh_get_authentication_socket().
70 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100071
Damien Miller95def091999-11-25 00:26:21 +110072void
73ssh_close_authentication_socket(int sock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100074{
Damien Miller95def091999-11-25 00:26:21 +110075 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
76 close(sock);
Damien Millerd4a8b7e1999-10-27 13:42:43 +100077}
78
Damien Miller5428f641999-11-25 11:54:57 +110079/*
80 * Opens and connects a private socket for communication with the
81 * authentication agent. Returns the file descriptor (which must be
82 * shut down and closed by the caller when no longer needed).
83 * Returns NULL if an error occurred and the connection could not be
84 * opened.
85 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100086
Damien Miller95def091999-11-25 00:26:21 +110087AuthenticationConnection *
88ssh_get_authentication_connection()
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089{
Damien Miller95def091999-11-25 00:26:21 +110090 AuthenticationConnection *auth;
91 int sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100092
Damien Miller95def091999-11-25 00:26:21 +110093 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller5428f641999-11-25 11:54:57 +110095 /*
96 * Fail if we couldn't obtain a connection. This happens if we
97 * exited due to a timeout.
98 */
Damien Miller95def091999-11-25 00:26:21 +110099 if (sock < 0)
100 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 auth = xmalloc(sizeof(*auth));
103 auth->fd = sock;
104 buffer_init(&auth->packet);
105 buffer_init(&auth->identities);
106 auth->howmany = 0;
107
108 return auth;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000109}
110
Damien Miller5428f641999-11-25 11:54:57 +1100111/*
112 * Closes the connection to the authentication agent and frees any associated
113 * memory.
114 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115
Damien Miller95def091999-11-25 00:26:21 +1100116void
117ssh_close_authentication_connection(AuthenticationConnection *ac)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000118{
Damien Miller95def091999-11-25 00:26:21 +1100119 buffer_free(&ac->packet);
120 buffer_free(&ac->identities);
121 close(ac->fd);
122 xfree(ac);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000123}
124
Damien Miller5428f641999-11-25 11:54:57 +1100125/*
126 * Returns the first authentication identity held by the agent.
127 * Returns true if an identity is available, 0 otherwise.
128 * The caller must initialize the integers before the call, and free the
129 * comment after a successful call (before calling ssh_get_next_identity).
130 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000131
132int
133ssh_get_first_identity(AuthenticationConnection *auth,
Damien Miller7e8e8201999-11-16 13:37:16 +1100134 BIGNUM *e, BIGNUM *n, char **comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135{
Damien Miller95def091999-11-25 00:26:21 +1100136 unsigned char msg[8192];
137 int len, l;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138
Damien Miller5428f641999-11-25 11:54:57 +1100139 /*
140 * Send a message to the agent requesting for a list of the
141 * identities it can represent.
142 */
Damien Miller95def091999-11-25 00:26:21 +1100143 msg[0] = 0;
144 msg[1] = 0;
145 msg[2] = 0;
146 msg[3] = 1;
147 msg[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
Damien Miller037a0dc1999-12-07 15:38:31 +1100148 if (atomicio(write, auth->fd, msg, 5) != 5) {
Damien Miller95def091999-11-25 00:26:21 +1100149 error("write auth->fd: %.100s", strerror(errno));
150 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000151 }
Damien Miller95def091999-11-25 00:26:21 +1100152 /* Read the length of the response. XXX implement timeouts here. */
153 len = 4;
154 while (len > 0) {
155 l = read(auth->fd, msg + 4 - len, len);
156 if (l <= 0) {
157 error("read auth->fd: %.100s", strerror(errno));
158 return 0;
159 }
160 len -= l;
161 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000162
Damien Miller5428f641999-11-25 11:54:57 +1100163 /*
164 * Extract the length, and check it for sanity. (We cannot trust
165 * authentication agents).
166 */
Damien Miller95def091999-11-25 00:26:21 +1100167 len = GET_32BIT(msg);
168 if (len < 1 || len > 256 * 1024)
169 fatal("Authentication reply message too long: %d\n", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000170
Damien Miller95def091999-11-25 00:26:21 +1100171 /* Read the packet itself. */
172 buffer_clear(&auth->identities);
173 while (len > 0) {
174 l = len;
175 if (l > sizeof(msg))
176 l = sizeof(msg);
177 l = read(auth->fd, msg, l);
178 if (l <= 0)
179 fatal("Incomplete authentication reply.");
180 buffer_append(&auth->identities, (char *) msg, l);
181 len -= l;
182 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000183
Damien Miller95def091999-11-25 00:26:21 +1100184 /* Get message type, and verify that we got a proper answer. */
185 buffer_get(&auth->identities, (char *) msg, 1);
186 if (msg[0] != SSH_AGENT_RSA_IDENTITIES_ANSWER)
187 fatal("Bad authentication reply message type: %d", msg[0]);
188
189 /* Get the number of entries in the response and check it for sanity. */
190 auth->howmany = buffer_get_int(&auth->identities);
191 if (auth->howmany > 1024)
192 fatal("Too many identities in authentication reply: %d\n", auth->howmany);
193
194 /* Return the first entry (if any). */
195 return ssh_get_next_identity(auth, e, n, comment);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000196}
197
Damien Miller5428f641999-11-25 11:54:57 +1100198/*
199 * Returns the next authentication identity for the agent. Other functions
200 * can be called between this and ssh_get_first_identity or two calls of this
201 * function. This returns 0 if there are no more identities. The caller
202 * must free comment after a successful return.
203 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000204
205int
206ssh_get_next_identity(AuthenticationConnection *auth,
Damien Miller7e8e8201999-11-16 13:37:16 +1100207 BIGNUM *e, BIGNUM *n, char **comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000208{
Damien Miller95def091999-11-25 00:26:21 +1100209 unsigned int bits;
Damien Miller7e8e8201999-11-16 13:37:16 +1100210
Damien Miller95def091999-11-25 00:26:21 +1100211 /* Return failure if no more entries. */
212 if (auth->howmany <= 0)
213 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000214
Damien Miller5428f641999-11-25 11:54:57 +1100215 /*
216 * Get the next entry from the packet. These will abort with a fatal
217 * error if the packet is too short or contains corrupt data.
218 */
Damien Miller95def091999-11-25 00:26:21 +1100219 bits = buffer_get_int(&auth->identities);
220 buffer_get_bignum(&auth->identities, e);
221 buffer_get_bignum(&auth->identities, n);
222 *comment = buffer_get_string(&auth->identities, NULL);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller95def091999-11-25 00:26:21 +1100224 if (bits != BN_num_bits(n))
Damien Miller5e7c10e1999-12-16 13:18:04 +1100225 error("Warning: identity keysize mismatch: actual %d, announced %u",
Damien Miller95def091999-11-25 00:26:21 +1100226 BN_num_bits(n), bits);
Damien Miller7e8e8201999-11-16 13:37:16 +1100227
Damien Miller95def091999-11-25 00:26:21 +1100228 /* Decrement the number of remaining entries. */
229 auth->howmany--;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000230
Damien Miller95def091999-11-25 00:26:21 +1100231 return 1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000232}
233
Damien Miller5428f641999-11-25 11:54:57 +1100234/*
235 * Generates a random challenge, sends it to the agent, and waits for
236 * response from the agent. Returns true (non-zero) if the agent gave the
237 * correct answer, zero otherwise. Response type selects the style of
238 * response desired, with 0 corresponding to protocol version 1.0 (no longer
239 * supported) and 1 corresponding to protocol version 1.1.
240 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000241
242int
243ssh_decrypt_challenge(AuthenticationConnection *auth,
Damien Miller95def091999-11-25 00:26:21 +1100244 BIGNUM* e, BIGNUM *n, BIGNUM *challenge,
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000245 unsigned char session_id[16],
246 unsigned int response_type,
247 unsigned char response[16])
248{
Damien Miller95def091999-11-25 00:26:21 +1100249 Buffer buffer;
250 unsigned char buf[8192];
251 int len, l, i;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000252
Damien Miller95def091999-11-25 00:26:21 +1100253 /* Response type 0 is no longer supported. */
254 if (response_type == 0)
255 fatal("Compatibility with ssh protocol version 1.0 no longer supported.");
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000256
Damien Miller95def091999-11-25 00:26:21 +1100257 /* Format a message to the agent. */
258 buf[0] = SSH_AGENTC_RSA_CHALLENGE;
259 buffer_init(&buffer);
260 buffer_append(&buffer, (char *) buf, 1);
261 buffer_put_int(&buffer, BN_num_bits(n));
262 buffer_put_bignum(&buffer, e);
263 buffer_put_bignum(&buffer, n);
264 buffer_put_bignum(&buffer, challenge);
265 buffer_append(&buffer, (char *) session_id, 16);
266 buffer_put_int(&buffer, response_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000267
Damien Miller95def091999-11-25 00:26:21 +1100268 /* Get the length of the message, and format it in the buffer. */
269 len = buffer_len(&buffer);
270 PUT_32BIT(buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000271
Damien Miller95def091999-11-25 00:26:21 +1100272 /* Send the length and then the packet to the agent. */
Damien Miller037a0dc1999-12-07 15:38:31 +1100273 if (atomicio(write, auth->fd, buf, 4) != 4 ||
274 atomicio(write, auth->fd, buffer_ptr(&buffer),
275 buffer_len(&buffer)) != buffer_len(&buffer)) {
Damien Miller95def091999-11-25 00:26:21 +1100276 error("Error writing to authentication socket.");
277error_cleanup:
278 buffer_free(&buffer);
279 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000280 }
Damien Miller5428f641999-11-25 11:54:57 +1100281 /*
282 * Wait for response from the agent. First read the length of the
283 * response packet.
284 */
Damien Miller95def091999-11-25 00:26:21 +1100285 len = 4;
286 while (len > 0) {
287 l = read(auth->fd, buf + 4 - len, len);
288 if (l <= 0) {
289 error("Error reading response length from authentication socket.");
290 goto error_cleanup;
291 }
292 len -= l;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000293 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000294
Damien Miller95def091999-11-25 00:26:21 +1100295 /* Extract the length, and check it for sanity. */
296 len = GET_32BIT(buf);
297 if (len > 256 * 1024)
298 fatal("Authentication response too long: %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000299
Damien Miller95def091999-11-25 00:26:21 +1100300 /* Read the rest of the response in tothe buffer. */
301 buffer_clear(&buffer);
302 while (len > 0) {
303 l = len;
304 if (l > sizeof(buf))
305 l = sizeof(buf);
306 l = read(auth->fd, buf, l);
307 if (l <= 0) {
308 error("Error reading response from authentication socket.");
309 goto error_cleanup;
310 }
311 buffer_append(&buffer, (char *) buf, l);
312 len -= l;
313 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000314
Damien Miller95def091999-11-25 00:26:21 +1100315 /* Get the type of the packet. */
316 buffer_get(&buffer, (char *) buf, 1);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000317
Damien Miller95def091999-11-25 00:26:21 +1100318 /* Check for agent failure message. */
319 if (buf[0] == SSH_AGENT_FAILURE) {
320 log("Agent admitted failure to authenticate using the key.");
321 goto error_cleanup;
322 }
323 /* Now it must be an authentication response packet. */
324 if (buf[0] != SSH_AGENT_RSA_RESPONSE)
325 fatal("Bad authentication response: %d", buf[0]);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000326
Damien Miller5428f641999-11-25 11:54:57 +1100327 /*
328 * Get the response from the packet. This will abort with a fatal
329 * error if the packet is corrupt.
330 */
Damien Miller95def091999-11-25 00:26:21 +1100331 for (i = 0; i < 16; i++)
332 response[i] = buffer_get_char(&buffer);
333
334 /* The buffer containing the packet is no longer needed. */
335 buffer_free(&buffer);
336
337 /* Correct answer. */
338 return 1;
339}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000340
Damien Miller5428f641999-11-25 11:54:57 +1100341/*
342 * Adds an identity to the authentication server. This call is not meant to
343 * be used by normal applications.
344 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000345
Damien Miller95def091999-11-25 00:26:21 +1100346int
347ssh_add_identity(AuthenticationConnection *auth,
348 RSA * key, const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000349{
Damien Miller95def091999-11-25 00:26:21 +1100350 Buffer buffer;
351 unsigned char buf[8192];
352 int len, l, type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000353
Damien Miller95def091999-11-25 00:26:21 +1100354 /* Format a message to the agent. */
355 buffer_init(&buffer);
356 buffer_put_char(&buffer, SSH_AGENTC_ADD_RSA_IDENTITY);
357 buffer_put_int(&buffer, BN_num_bits(key->n));
358 buffer_put_bignum(&buffer, key->n);
359 buffer_put_bignum(&buffer, key->e);
360 buffer_put_bignum(&buffer, key->d);
361 /* To keep within the protocol: p < q for ssh. in SSL p > q */
362 buffer_put_bignum(&buffer, key->iqmp); /* ssh key->u */
363 buffer_put_bignum(&buffer, key->q); /* ssh key->p, SSL key->q */
364 buffer_put_bignum(&buffer, key->p); /* ssh key->q, SSL key->p */
365 buffer_put_string(&buffer, comment, strlen(comment));
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Miller95def091999-11-25 00:26:21 +1100367 /* Get the length of the message, and format it in the buffer. */
368 len = buffer_len(&buffer);
369 PUT_32BIT(buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000370
Damien Miller95def091999-11-25 00:26:21 +1100371 /* Send the length and then the packet to the agent. */
Damien Miller037a0dc1999-12-07 15:38:31 +1100372 if (atomicio(write, auth->fd, buf, 4) != 4 ||
373 atomicio(write, auth->fd, buffer_ptr(&buffer),
374 buffer_len(&buffer)) != buffer_len(&buffer)) {
Damien Miller95def091999-11-25 00:26:21 +1100375 error("Error writing to authentication socket.");
376error_cleanup:
377 buffer_free(&buffer);
378 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000379 }
Damien Miller95def091999-11-25 00:26:21 +1100380 /* Wait for response from the agent. First read the length of the
381 response packet. */
382 len = 4;
383 while (len > 0) {
384 l = read(auth->fd, buf + 4 - len, len);
385 if (l <= 0) {
386 error("Error reading response length from authentication socket.");
387 goto error_cleanup;
388 }
389 len -= l;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000390 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000391
Damien Miller95def091999-11-25 00:26:21 +1100392 /* Extract the length, and check it for sanity. */
393 len = GET_32BIT(buf);
394 if (len > 256 * 1024)
395 fatal("Add identity response too long: %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000396
Damien Miller95def091999-11-25 00:26:21 +1100397 /* Read the rest of the response in tothe buffer. */
398 buffer_clear(&buffer);
399 while (len > 0) {
400 l = len;
401 if (l > sizeof(buf))
402 l = sizeof(buf);
403 l = read(auth->fd, buf, l);
404 if (l <= 0) {
405 error("Error reading response from authentication socket.");
406 goto error_cleanup;
407 }
408 buffer_append(&buffer, (char *) buf, l);
409 len -= l;
410 }
411
412 /* Get the type of the packet. */
413 type = buffer_get_char(&buffer);
414 switch (type) {
415 case SSH_AGENT_FAILURE:
416 buffer_free(&buffer);
417 return 0;
418 case SSH_AGENT_SUCCESS:
419 buffer_free(&buffer);
420 return 1;
421 default:
422 fatal("Bad response to add identity from authentication agent: %d",
423 type);
424 }
425 /* NOTREACHED */
426 return 0;
427}
428
Damien Miller5428f641999-11-25 11:54:57 +1100429/*
430 * Removes an identity from the authentication server. This call is not
431 * meant to be used by normal applications.
432 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000433
Damien Miller95def091999-11-25 00:26:21 +1100434int
435ssh_remove_identity(AuthenticationConnection *auth, RSA *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000436{
Damien Miller95def091999-11-25 00:26:21 +1100437 Buffer buffer;
438 unsigned char buf[8192];
439 int len, l, type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000440
Damien Miller95def091999-11-25 00:26:21 +1100441 /* Format a message to the agent. */
442 buffer_init(&buffer);
443 buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY);
444 buffer_put_int(&buffer, BN_num_bits(key->n));
445 buffer_put_bignum(&buffer, key->e);
446 buffer_put_bignum(&buffer, key->n);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000447
Damien Miller95def091999-11-25 00:26:21 +1100448 /* Get the length of the message, and format it in the buffer. */
449 len = buffer_len(&buffer);
450 PUT_32BIT(buf, len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller95def091999-11-25 00:26:21 +1100452 /* Send the length and then the packet to the agent. */
Damien Miller037a0dc1999-12-07 15:38:31 +1100453 if (atomicio(write, auth->fd, buf, 4) != 4 ||
454 atomicio(write, auth->fd, buffer_ptr(&buffer),
455 buffer_len(&buffer)) != buffer_len(&buffer)) {
Damien Miller95def091999-11-25 00:26:21 +1100456 error("Error writing to authentication socket.");
457error_cleanup:
458 buffer_free(&buffer);
459 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000460 }
Damien Miller5428f641999-11-25 11:54:57 +1100461 /*
462 * Wait for response from the agent. First read the length of the
463 * response packet.
464 */
Damien Miller95def091999-11-25 00:26:21 +1100465 len = 4;
466 while (len > 0) {
467 l = read(auth->fd, buf + 4 - len, len);
468 if (l <= 0) {
469 error("Error reading response length from authentication socket.");
470 goto error_cleanup;
471 }
472 len -= l;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000473 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000474
Damien Miller95def091999-11-25 00:26:21 +1100475 /* Extract the length, and check it for sanity. */
476 len = GET_32BIT(buf);
477 if (len > 256 * 1024)
478 fatal("Remove identity response too long: %d", len);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000479
Damien Miller95def091999-11-25 00:26:21 +1100480 /* Read the rest of the response in tothe buffer. */
481 buffer_clear(&buffer);
482 while (len > 0) {
483 l = len;
484 if (l > sizeof(buf))
485 l = sizeof(buf);
486 l = read(auth->fd, buf, l);
487 if (l <= 0) {
488 error("Error reading response from authentication socket.");
489 goto error_cleanup;
490 }
491 buffer_append(&buffer, (char *) buf, l);
492 len -= l;
493 }
494
495 /* Get the type of the packet. */
496 type = buffer_get_char(&buffer);
497 switch (type) {
498 case SSH_AGENT_FAILURE:
499 buffer_free(&buffer);
500 return 0;
501 case SSH_AGENT_SUCCESS:
502 buffer_free(&buffer);
503 return 1;
504 default:
505 fatal("Bad response to remove identity from authentication agent: %d",
506 type);
507 }
508 /* NOTREACHED */
509 return 0;
510}
511
Damien Miller5428f641999-11-25 11:54:57 +1100512/*
513 * Removes all identities from the agent. This call is not meant to be used
514 * by normal applications.
515 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000516
Damien Miller95def091999-11-25 00:26:21 +1100517int
518ssh_remove_all_identities(AuthenticationConnection *auth)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000519{
Damien Miller95def091999-11-25 00:26:21 +1100520 Buffer buffer;
521 unsigned char buf[8192];
522 int len, l, type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000523
Damien Miller95def091999-11-25 00:26:21 +1100524 /* Get the length of the message, and format it in the buffer. */
525 PUT_32BIT(buf, 1);
526 buf[4] = SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000527
Damien Miller95def091999-11-25 00:26:21 +1100528 /* Send the length and then the packet to the agent. */
Damien Miller037a0dc1999-12-07 15:38:31 +1100529 if (atomicio(write, auth->fd, buf, 5) != 5) {
Damien Miller95def091999-11-25 00:26:21 +1100530 error("Error writing to authentication socket.");
531 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000532 }
Damien Miller5428f641999-11-25 11:54:57 +1100533 /*
534 * Wait for response from the agent. First read the length of the
535 * response packet.
536 */
Damien Miller95def091999-11-25 00:26:21 +1100537 len = 4;
538 while (len > 0) {
539 l = read(auth->fd, buf + 4 - len, len);
540 if (l <= 0) {
541 error("Error reading response length from authentication socket.");
542 return 0;
543 }
544 len -= l;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000545 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller95def091999-11-25 00:26:21 +1100547 /* Extract the length, and check it for sanity. */
548 len = GET_32BIT(buf);
549 if (len > 256 * 1024)
550 fatal("Remove identity response too long: %d", len);
551
552 /* Read the rest of the response into the buffer. */
553 buffer_init(&buffer);
554 while (len > 0) {
555 l = len;
556 if (l > sizeof(buf))
557 l = sizeof(buf);
558 l = read(auth->fd, buf, l);
559 if (l <= 0) {
560 error("Error reading response from authentication socket.");
561 buffer_free(&buffer);
562 return 0;
563 }
564 buffer_append(&buffer, (char *) buf, l);
565 len -= l;
566 }
567
568 /* Get the type of the packet. */
569 type = buffer_get_char(&buffer);
570 switch (type) {
571 case SSH_AGENT_FAILURE:
572 buffer_free(&buffer);
573 return 0;
574 case SSH_AGENT_SUCCESS:
575 buffer_free(&buffer);
576 return 1;
577 default:
578 fatal("Bad response to remove identity from authentication agent: %d",
579 type);
580 }
581 /* NOTREACHED */
582 return 0;
583}