blob: d9427d3776acce1e1d36d8729124bf76c1ed0f43 [file] [log] [blame]
Damien Millerd4a8b7e1999-10-27 13:42:43 +10001/*
Damien Miller4af51302000-04-16 11:18:38 +10002 *
Damien Miller95def091999-11-25 00:26:21 +11003 * authfd.c
Damien Miller4af51302000-04-16 11:18:38 +10004 *
Damien Miller95def091999-11-25 00:26:21 +11005 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller4af51302000-04-16 11:18:38 +10006 *
Damien Miller95def091999-11-25 00:26:21 +11007 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * All rights reserved
Damien Miller4af51302000-04-16 11:18:38 +10009 *
Damien Miller95def091999-11-25 00:26:21 +110010 * Created: Wed Mar 29 01:30:28 1995 ylo
Damien Miller4af51302000-04-16 11:18:38 +100011 *
Damien Miller95def091999-11-25 00:26:21 +110012 * Functions for connecting the local authentication agent.
Damien Miller4af51302000-04-16 11:18:38 +100013 *
Damien Millerad833b32000-08-23 10:46:23 +100014 * SSH2 implementation,
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 *
Damien Miller95def091999-11-25 00:26:21 +110017 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100018
19#include "includes.h"
Damien Millerad833b32000-08-23 10:46:23 +100020RCSID("$OpenBSD: authfd.c,v 1.25 2000/08/19 21:34:42 markus Exp $");
Damien Millerd4a8b7e1999-10-27 13:42:43 +100021
22#include "ssh.h"
23#include "rsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100024#include "buffer.h"
25#include "bufaux.h"
26#include "xmalloc.h"
27#include "getput.h"
28
29#include <openssl/rsa.h>
Damien Miller994cf142000-07-21 10:19:44 +100030#include <openssl/dsa.h>
31#include <openssl/evp.h>
32#include "key.h"
33#include "authfd.h"
34#include "kex.h"
Damien Millerad833b32000-08-23 10:46:23 +100035#include "dsa.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100036
Damien Miller37023962000-07-11 17:31:38 +100037/* helper */
Damien Miller942da032000-08-18 13:59:06 +100038int decode_reply(int type);
Damien Miller37023962000-07-11 17:31:38 +100039
Damien Millerd4a8b7e1999-10-27 13:42:43 +100040/* Returns the number of the authentication fd, or -1 if there is none. */
41
42int
43ssh_get_authentication_socket()
44{
Damien Miller95def091999-11-25 00:26:21 +110045 const char *authsocket;
Damien Miller942da032000-08-18 13:59:06 +100046 int sock, len;
Damien Miller95def091999-11-25 00:26:21 +110047 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100048
Damien Miller95def091999-11-25 00:26:21 +110049 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
50 if (!authsocket)
51 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100052
Damien Miller95def091999-11-25 00:26:21 +110053 sunaddr.sun_family = AF_UNIX;
54 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
Damien Miller942da032000-08-18 13:59:06 +100055#ifdef HAVE_SUN_LEN_IN_SOCKADDR_UN
56 sunaddr.sun_len = len = SUN_LEN(&sunaddr)+1;
57#else /* HAVE_SUN_LEN_IN_SOCKADDR_UN */
58 len = SUN_LEN(&sunaddr)+1;
59#endif /* HAVE_SUN_LEN_IN_SOCKADDR_UN */
Damien Miller10f6f6b1999-11-17 17:29:08 +110060
Damien Miller95def091999-11-25 00:26:21 +110061 sock = socket(AF_UNIX, SOCK_STREAM, 0);
62 if (sock < 0)
63 return -1;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100064
Damien Miller95def091999-11-25 00:26:21 +110065 /* close on exec */
66 if (fcntl(sock, F_SETFD, 1) == -1) {
67 close(sock);
68 return -1;
69 }
Damien Miller942da032000-08-18 13:59:06 +100070 if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
Damien Miller95def091999-11-25 00:26:21 +110071 close(sock);
72 return -1;
73 }
74 return sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100075}
76
Damien Miller942da032000-08-18 13:59:06 +100077int
Damien Millerad833b32000-08-23 10:46:23 +100078ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
Damien Miller942da032000-08-18 13:59:06 +100079{
80 int l, len;
81 char buf[1024];
82
83 /* Get the length of the message, and format it in the buffer. */
84 len = buffer_len(request);
85 PUT_32BIT(buf, len);
86
87 /* Send the length and then the packet to the agent. */
88 if (atomicio(write, auth->fd, buf, 4) != 4 ||
89 atomicio(write, auth->fd, buffer_ptr(request),
90 buffer_len(request)) != buffer_len(request)) {
91 error("Error writing to authentication socket.");
92 return 0;
93 }
94 /*
95 * Wait for response from the agent. First read the length of the
96 * response packet.
97 */
98 len = 4;
99 while (len > 0) {
100 l = read(auth->fd, buf + 4 - len, len);
101 if (l <= 0) {
102 error("Error reading response length from authentication socket.");
103 return 0;
104 }
105 len -= l;
106 }
107
108 /* Extract the length, and check it for sanity. */
109 len = GET_32BIT(buf);
110 if (len > 256 * 1024)
111 fatal("Authentication response too long: %d", len);
112
113 /* Read the rest of the response in to the buffer. */
114 buffer_clear(reply);
115 while (len > 0) {
116 l = len;
117 if (l > sizeof(buf))
118 l = sizeof(buf);
119 l = read(auth->fd, buf, l);
120 if (l <= 0) {
121 error("Error reading response from authentication socket.");
122 return 0;
123 }
124 buffer_append(reply, (char *) buf, l);
125 len -= l;
126 }
127 return 1;
128}
129
Damien Miller5428f641999-11-25 11:54:57 +1100130/*
131 * Closes the agent socket if it should be closed (depends on how it was
132 * obtained). The argument must have been returned by
133 * ssh_get_authentication_socket().
134 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000135
Damien Miller4af51302000-04-16 11:18:38 +1000136void
Damien Miller95def091999-11-25 00:26:21 +1100137ssh_close_authentication_socket(int sock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000138{
Damien Miller95def091999-11-25 00:26:21 +1100139 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
140 close(sock);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000141}
142
Damien Miller5428f641999-11-25 11:54:57 +1100143/*
144 * Opens and connects a private socket for communication with the
145 * authentication agent. Returns the file descriptor (which must be
146 * shut down and closed by the caller when no longer needed).
147 * Returns NULL if an error occurred and the connection could not be
148 * opened.
149 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000150
Damien Miller95def091999-11-25 00:26:21 +1100151AuthenticationConnection *
152ssh_get_authentication_connection()
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000153{
Damien Miller95def091999-11-25 00:26:21 +1100154 AuthenticationConnection *auth;
155 int sock;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000156
Damien Miller95def091999-11-25 00:26:21 +1100157 sock = ssh_get_authentication_socket();
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000158
Damien Miller5428f641999-11-25 11:54:57 +1100159 /*
160 * Fail if we couldn't obtain a connection. This happens if we
161 * exited due to a timeout.
162 */
Damien Miller95def091999-11-25 00:26:21 +1100163 if (sock < 0)
164 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000165
Damien Miller95def091999-11-25 00:26:21 +1100166 auth = xmalloc(sizeof(*auth));
167 auth->fd = sock;
Damien Miller95def091999-11-25 00:26:21 +1100168 buffer_init(&auth->identities);
169 auth->howmany = 0;
170
171 return auth;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000172}
173
Damien Miller5428f641999-11-25 11:54:57 +1100174/*
175 * Closes the connection to the authentication agent and frees any associated
176 * memory.
177 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000178
Damien Miller4af51302000-04-16 11:18:38 +1000179void
Damien Millerad833b32000-08-23 10:46:23 +1000180ssh_close_authentication_connection(AuthenticationConnection *auth)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000181{
Damien Millerad833b32000-08-23 10:46:23 +1000182 buffer_free(&auth->identities);
183 close(auth->fd);
184 xfree(auth);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000185}
186
Damien Miller5428f641999-11-25 11:54:57 +1100187/*
188 * Returns the first authentication identity held by the agent.
Damien Miller5428f641999-11-25 11:54:57 +1100189 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190
Damien Millerad833b32000-08-23 10:46:23 +1000191Key *
192ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000193{
Damien Millerad833b32000-08-23 10:46:23 +1000194 int type, code1 = 0, code2 = 0;
Damien Miller942da032000-08-18 13:59:06 +1000195 Buffer request;
Damien Millerad833b32000-08-23 10:46:23 +1000196
197 switch(version){
198 case 1:
199 code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
200 code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
201 break;
202 case 2:
203 code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
204 code2 = SSH2_AGENT_IDENTITIES_ANSWER;
205 break;
206 default:
207 return NULL;
208 }
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000209
Damien Miller5428f641999-11-25 11:54:57 +1100210 /*
211 * Send a message to the agent requesting for a list of the
212 * identities it can represent.
213 */
Damien Miller942da032000-08-18 13:59:06 +1000214 buffer_init(&request);
Damien Millerad833b32000-08-23 10:46:23 +1000215 buffer_put_char(&request, code1);
Damien Miller942da032000-08-18 13:59:06 +1000216
217 buffer_clear(&auth->identities);
218 if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
219 buffer_free(&request);
Damien Millerad833b32000-08-23 10:46:23 +1000220 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000221 }
Damien Miller942da032000-08-18 13:59:06 +1000222 buffer_free(&request);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000223
Damien Miller95def091999-11-25 00:26:21 +1100224 /* Get message type, and verify that we got a proper answer. */
Damien Miller942da032000-08-18 13:59:06 +1000225 type = buffer_get_char(&auth->identities);
Damien Millerad833b32000-08-23 10:46:23 +1000226 if (type == SSH_AGENT_FAILURE) {
227 return NULL;
228 } else if (type != code2) {
Damien Miller942da032000-08-18 13:59:06 +1000229 fatal("Bad authentication reply message type: %d", type);
Damien Millerad833b32000-08-23 10:46:23 +1000230 }
Damien Miller95def091999-11-25 00:26:21 +1100231
232 /* Get the number of entries in the response and check it for sanity. */
233 auth->howmany = buffer_get_int(&auth->identities);
234 if (auth->howmany > 1024)
Damien Miller942da032000-08-18 13:59:06 +1000235 fatal("Too many identities in authentication reply: %d\n",
236 auth->howmany);
Damien Miller95def091999-11-25 00:26:21 +1100237
238 /* Return the first entry (if any). */
Damien Millerad833b32000-08-23 10:46:23 +1000239 return ssh_get_next_identity(auth, comment, version);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000240}
241
Damien Millerad833b32000-08-23 10:46:23 +1000242Key *
243ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
Damien Miller95def091999-11-25 00:26:21 +1100245 unsigned int bits;
Damien Millerad833b32000-08-23 10:46:23 +1000246 unsigned char *blob;
247 unsigned int blen;
248 Key *key = NULL;
Damien Miller7e8e8201999-11-16 13:37:16 +1100249
Damien Miller95def091999-11-25 00:26:21 +1100250 /* Return failure if no more entries. */
251 if (auth->howmany <= 0)
Damien Millerad833b32000-08-23 10:46:23 +1000252 return NULL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000253
Damien Miller5428f641999-11-25 11:54:57 +1100254 /*
255 * Get the next entry from the packet. These will abort with a fatal
256 * error if the packet is too short or contains corrupt data.
257 */
Damien Millerad833b32000-08-23 10:46:23 +1000258 switch(version){
259 case 1:
260 key = key_new(KEY_RSA);
261 bits = buffer_get_int(&auth->identities);
262 buffer_get_bignum(&auth->identities, key->rsa->e);
263 buffer_get_bignum(&auth->identities, key->rsa->n);
264 *comment = buffer_get_string(&auth->identities, NULL);
265 if (bits != BN_num_bits(key->rsa->n))
266 log("Warning: identity keysize mismatch: actual %d, announced %u",
267 BN_num_bits(key->rsa->n), bits);
268 break;
269 case 2:
270 blob = buffer_get_string(&auth->identities, &blen);
271 *comment = buffer_get_string(&auth->identities, NULL);
272 key = dsa_key_from_blob(blob, blen);
273 xfree(blob);
274 break;
275 default:
276 return NULL;
277 break;
278 }
Damien Miller95def091999-11-25 00:26:21 +1100279 /* Decrement the number of remaining entries. */
280 auth->howmany--;
Damien Millerad833b32000-08-23 10:46:23 +1000281 return key;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000282}
283
Damien Miller5428f641999-11-25 11:54:57 +1100284/*
285 * Generates a random challenge, sends it to the agent, and waits for
286 * response from the agent. Returns true (non-zero) if the agent gave the
287 * correct answer, zero otherwise. Response type selects the style of
288 * response desired, with 0 corresponding to protocol version 1.0 (no longer
289 * supported) and 1 corresponding to protocol version 1.1.
290 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000291
292int
293ssh_decrypt_challenge(AuthenticationConnection *auth,
Damien Millerad833b32000-08-23 10:46:23 +1000294 Key* key, BIGNUM *challenge,
Damien Miller942da032000-08-18 13:59:06 +1000295 unsigned char session_id[16],
296 unsigned int response_type,
297 unsigned char response[16])
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000298{
Damien Miller95def091999-11-25 00:26:21 +1100299 Buffer buffer;
Damien Miller942da032000-08-18 13:59:06 +1000300 int success = 0;
301 int i;
302 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000303
Damien Millerad833b32000-08-23 10:46:23 +1000304 if (key->type != KEY_RSA)
305 return 0;
306 if (response_type == 0) {
307 log("Compatibility with ssh protocol version 1.0 no longer supported.");
308 return 0;
309 }
Damien Miller95def091999-11-25 00:26:21 +1100310 buffer_init(&buffer);
Damien Miller942da032000-08-18 13:59:06 +1000311 buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
Damien Millerad833b32000-08-23 10:46:23 +1000312 buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
313 buffer_put_bignum(&buffer, key->rsa->e);
314 buffer_put_bignum(&buffer, key->rsa->n);
Damien Miller95def091999-11-25 00:26:21 +1100315 buffer_put_bignum(&buffer, challenge);
316 buffer_append(&buffer, (char *) session_id, 16);
317 buffer_put_int(&buffer, response_type);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000318
Damien Miller942da032000-08-18 13:59:06 +1000319 if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
Damien Miller95def091999-11-25 00:26:21 +1100320 buffer_free(&buffer);
321 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000322 }
Damien Miller942da032000-08-18 13:59:06 +1000323 type = buffer_get_char(&buffer);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000324
Damien Miller942da032000-08-18 13:59:06 +1000325 if (type == SSH_AGENT_FAILURE) {
Damien Miller95def091999-11-25 00:26:21 +1100326 log("Agent admitted failure to authenticate using the key.");
Damien Miller942da032000-08-18 13:59:06 +1000327 } else if (type != SSH_AGENT_RSA_RESPONSE) {
328 fatal("Bad authentication response: %d", type);
329 } else {
330 success = 1;
331 /*
332 * Get the response from the packet. This will abort with a
333 * fatal error if the packet is corrupt.
334 */
335 for (i = 0; i < 16; i++)
336 response[i] = buffer_get_char(&buffer);
Damien Miller95def091999-11-25 00:26:21 +1100337 }
Damien Miller95def091999-11-25 00:26:21 +1100338 buffer_free(&buffer);
Damien Miller942da032000-08-18 13:59:06 +1000339 return success;
Damien Miller95def091999-11-25 00:26:21 +1100340}
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000341
Damien Millerad833b32000-08-23 10:46:23 +1000342/* ask agent to sign data, returns -1 on error, 0 on success */
343int
344ssh_agent_sign(AuthenticationConnection *auth,
345 Key *key,
346 unsigned char **sigp, int *lenp,
347 unsigned char *data, int datalen)
348{
349 Buffer msg;
350 unsigned char *blob;
351 unsigned int blen;
352 int type;
353 int ret = -1;
354
355 if (dsa_make_key_blob(key, &blob, &blen) == 0)
356 return -1;
357
358 buffer_init(&msg);
359 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
360 buffer_put_string(&msg, blob, blen);
361 buffer_put_string(&msg, data, datalen);
362 xfree(blob);
363
364 if (ssh_request_reply(auth, &msg, &msg) == 0) {
365 buffer_free(&msg);
366 return -1;
367 }
368 type = buffer_get_char(&msg);
369 if (type == SSH_AGENT_FAILURE) {
370 log("Agent admitted failure to sign using the key.");
371 } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
372 fatal("Bad authentication response: %d", type);
373 } else {
374 ret = 0;
375 *sigp = buffer_get_string(&msg, lenp);
376 }
377 buffer_free(&msg);
378 return ret;
379}
380
Damien Miller994cf142000-07-21 10:19:44 +1000381/* Encode key for a message to the agent. */
382
383void
384ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
385{
386 buffer_clear(b);
387 buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
388 buffer_put_int(b, BN_num_bits(key->n));
389 buffer_put_bignum(b, key->n);
390 buffer_put_bignum(b, key->e);
391 buffer_put_bignum(b, key->d);
392 /* To keep within the protocol: p < q for ssh. in SSL p > q */
393 buffer_put_bignum(b, key->iqmp); /* ssh key->u */
394 buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
395 buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
396 buffer_put_string(b, comment, strlen(comment));
397}
398
399void
400ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
401{
402 buffer_clear(b);
403 buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
404 buffer_put_cstring(b, KEX_DSS);
405 buffer_put_bignum2(b, key->p);
406 buffer_put_bignum2(b, key->q);
407 buffer_put_bignum2(b, key->g);
408 buffer_put_bignum2(b, key->pub_key);
409 buffer_put_bignum2(b, key->priv_key);
410 buffer_put_string(b, comment, strlen(comment));
411}
412
Damien Miller5428f641999-11-25 11:54:57 +1100413/*
414 * Adds an identity to the authentication server. This call is not meant to
415 * be used by normal applications.
416 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000417
Damien Miller4af51302000-04-16 11:18:38 +1000418int
Damien Miller994cf142000-07-21 10:19:44 +1000419ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000420{
Damien Millerad833b32000-08-23 10:46:23 +1000421 Buffer msg;
Damien Miller942da032000-08-18 13:59:06 +1000422 int type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000423
Damien Millerad833b32000-08-23 10:46:23 +1000424 buffer_init(&msg);
Damien Miller994cf142000-07-21 10:19:44 +1000425
426 switch (key->type) {
427 case KEY_RSA:
Damien Millerad833b32000-08-23 10:46:23 +1000428 ssh_encode_identity_rsa(&msg, key->rsa, comment);
Damien Miller994cf142000-07-21 10:19:44 +1000429 break;
430 case KEY_DSA:
Damien Millerad833b32000-08-23 10:46:23 +1000431 ssh_encode_identity_dsa(&msg, key->dsa, comment);
Damien Miller994cf142000-07-21 10:19:44 +1000432 break;
433 default:
Damien Millerad833b32000-08-23 10:46:23 +1000434 buffer_free(&msg);
Damien Miller994cf142000-07-21 10:19:44 +1000435 return 0;
436 break;
437 }
Damien Millerad833b32000-08-23 10:46:23 +1000438 if (ssh_request_reply(auth, &msg, &msg) == 0) {
439 buffer_free(&msg);
Damien Miller95def091999-11-25 00:26:21 +1100440 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000441 }
Damien Millerad833b32000-08-23 10:46:23 +1000442 type = buffer_get_char(&msg);
443 buffer_free(&msg);
Damien Miller942da032000-08-18 13:59:06 +1000444 return decode_reply(type);
Damien Miller95def091999-11-25 00:26:21 +1100445}
446
Damien Miller5428f641999-11-25 11:54:57 +1100447/*
448 * Removes an identity from the authentication server. This call is not
449 * meant to be used by normal applications.
450 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000451
Damien Miller4af51302000-04-16 11:18:38 +1000452int
Damien Millerad833b32000-08-23 10:46:23 +1000453ssh_remove_identity(AuthenticationConnection *auth, Key *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000454{
Damien Millerad833b32000-08-23 10:46:23 +1000455 Buffer msg;
Damien Miller942da032000-08-18 13:59:06 +1000456 int type;
Damien Millerad833b32000-08-23 10:46:23 +1000457 unsigned char *blob;
458 unsigned int blen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000459
Damien Millerad833b32000-08-23 10:46:23 +1000460 buffer_init(&msg);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000461
Damien Millerad833b32000-08-23 10:46:23 +1000462 if (key->type == KEY_RSA) {
463 buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
464 buffer_put_int(&msg, BN_num_bits(key->rsa->n));
465 buffer_put_bignum(&msg, key->rsa->e);
466 buffer_put_bignum(&msg, key->rsa->n);
467 } else if (key->type == KEY_DSA) {
468 dsa_make_key_blob(key, &blob, &blen);
469 buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
470 buffer_put_string(&msg, blob, blen);
471 xfree(blob);
472 } else {
473 buffer_free(&msg);
Damien Miller95def091999-11-25 00:26:21 +1100474 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000475 }
Damien Millerad833b32000-08-23 10:46:23 +1000476 if (ssh_request_reply(auth, &msg, &msg) == 0) {
477 buffer_free(&msg);
478 return 0;
479 }
480 type = buffer_get_char(&msg);
481 buffer_free(&msg);
Damien Miller942da032000-08-18 13:59:06 +1000482 return decode_reply(type);
Damien Miller95def091999-11-25 00:26:21 +1100483}
484
Damien Miller5428f641999-11-25 11:54:57 +1100485/*
486 * Removes all identities from the agent. This call is not meant to be used
487 * by normal applications.
488 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000489
Damien Miller4af51302000-04-16 11:18:38 +1000490int
Damien Millerad833b32000-08-23 10:46:23 +1000491ssh_remove_all_identities(AuthenticationConnection *auth, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000492{
Damien Millerad833b32000-08-23 10:46:23 +1000493 Buffer msg;
Damien Miller942da032000-08-18 13:59:06 +1000494 int type;
Damien Millerad833b32000-08-23 10:46:23 +1000495 int code = (version==1) ?
496 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
497 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000498
Damien Millerad833b32000-08-23 10:46:23 +1000499 buffer_init(&msg);
500 buffer_put_char(&msg, code);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000501
Damien Millerad833b32000-08-23 10:46:23 +1000502 if (ssh_request_reply(auth, &msg, &msg) == 0) {
503 buffer_free(&msg);
Damien Miller95def091999-11-25 00:26:21 +1100504 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000505 }
Damien Millerad833b32000-08-23 10:46:23 +1000506 type = buffer_get_char(&msg);
507 buffer_free(&msg);
Damien Miller942da032000-08-18 13:59:06 +1000508 return decode_reply(type);
509}
510
511int
512decode_reply(int type)
513{
Damien Miller95def091999-11-25 00:26:21 +1100514 switch (type) {
515 case SSH_AGENT_FAILURE:
Damien Miller942da032000-08-18 13:59:06 +1000516 log("SSH_AGENT_FAILURE");
Damien Miller95def091999-11-25 00:26:21 +1100517 return 0;
518 case SSH_AGENT_SUCCESS:
Damien Miller95def091999-11-25 00:26:21 +1100519 return 1;
520 default:
Damien Miller37023962000-07-11 17:31:38 +1000521 fatal("Bad response from authentication agent: %d", type);
Damien Miller95def091999-11-25 00:26:21 +1100522 }
523 /* NOTREACHED */
524 return 0;
525}