1999-10-27 03:42:43 +00:00
|
|
|
/*
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* authfd.c
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Created: Wed Mar 29 01:30:28 1995 ylo
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Functions for connecting the local authentication agent.
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
1999-11-24 13:26:21 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
2000-07-21 00:19:44 +00:00
|
|
|
RCSID("$OpenBSD: authfd.c,v 1.22 2000/07/16 08:27:20 markus Exp $");
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "ssh.h"
|
|
|
|
#include "rsa.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "bufaux.h"
|
|
|
|
#include "xmalloc.h"
|
|
|
|
#include "getput.h"
|
|
|
|
|
|
|
|
#include <openssl/rsa.h>
|
2000-07-21 00:19:44 +00:00
|
|
|
#include <openssl/dsa.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include "key.h"
|
|
|
|
#include "authfd.h"
|
|
|
|
#include "kex.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-07-11 07:31:38 +00:00
|
|
|
/* helper */
|
|
|
|
int ssh_agent_get_reply(AuthenticationConnection *auth);
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
/* Returns the number of the authentication fd, or -1 if there is none. */
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_get_authentication_socket()
|
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
const char *authsocket;
|
|
|
|
int sock;
|
|
|
|
struct sockaddr_un sunaddr;
|
|
|
|
|
|
|
|
authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
if (!authsocket)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
sunaddr.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
|
|
|
|
|
|
|
|
sock = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if (sock < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* close on exec */
|
|
|
|
if (fcntl(sock, F_SETFD, 1) == -1) {
|
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (connect(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
|
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sock;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Closes the agent socket if it should be closed (depends on how it was
|
|
|
|
* obtained). The argument must have been returned by
|
|
|
|
* ssh_get_authentication_socket().
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
void
|
1999-11-24 13:26:21 +00:00
|
|
|
ssh_close_authentication_socket(int sock)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
if (getenv(SSH_AUTHSOCKET_ENV_NAME))
|
|
|
|
close(sock);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Opens and connects a private socket for communication with the
|
|
|
|
* authentication agent. Returns the file descriptor (which must be
|
|
|
|
* shut down and closed by the caller when no longer needed).
|
|
|
|
* Returns NULL if an error occurred and the connection could not be
|
|
|
|
* opened.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
AuthenticationConnection *
|
|
|
|
ssh_get_authentication_connection()
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
AuthenticationConnection *auth;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = ssh_get_authentication_socket();
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Fail if we couldn't obtain a connection. This happens if we
|
|
|
|
* exited due to a timeout.
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
if (sock < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
auth = xmalloc(sizeof(*auth));
|
|
|
|
auth->fd = sock;
|
|
|
|
buffer_init(&auth->packet);
|
|
|
|
buffer_init(&auth->identities);
|
|
|
|
auth->howmany = 0;
|
|
|
|
|
|
|
|
return auth;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Closes the connection to the authentication agent and frees any associated
|
|
|
|
* memory.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
void
|
1999-11-24 13:26:21 +00:00
|
|
|
ssh_close_authentication_connection(AuthenticationConnection *ac)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_free(&ac->packet);
|
|
|
|
buffer_free(&ac->identities);
|
|
|
|
close(ac->fd);
|
|
|
|
xfree(ac);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Returns the first authentication identity held by the agent.
|
|
|
|
* Returns true if an identity is available, 0 otherwise.
|
|
|
|
* The caller must initialize the integers before the call, and free the
|
|
|
|
* comment after a successful call (before calling ssh_get_next_identity).
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
ssh_get_first_identity(AuthenticationConnection *auth,
|
1999-11-16 02:37:16 +00:00
|
|
|
BIGNUM *e, BIGNUM *n, char **comment)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
unsigned char msg[8192];
|
|
|
|
int len, l;
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Send a message to the agent requesting for a list of the
|
|
|
|
* identities it can represent.
|
|
|
|
*/
|
2000-07-21 00:19:44 +00:00
|
|
|
PUT_32BIT(msg, 1);
|
1999-11-24 13:26:21 +00:00
|
|
|
msg[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
|
1999-12-07 04:38:31 +00:00
|
|
|
if (atomicio(write, auth->fd, msg, 5) != 5) {
|
1999-11-24 13:26:21 +00:00
|
|
|
error("write auth->fd: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Read the length of the response. XXX implement timeouts here. */
|
|
|
|
len = 4;
|
|
|
|
while (len > 0) {
|
|
|
|
l = read(auth->fd, msg + 4 - len, len);
|
|
|
|
if (l <= 0) {
|
|
|
|
error("read auth->fd: %.100s", strerror(errno));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Extract the length, and check it for sanity. (We cannot trust
|
|
|
|
* authentication agents).
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
len = GET_32BIT(msg);
|
|
|
|
if (len < 1 || len > 256 * 1024)
|
|
|
|
fatal("Authentication reply message too long: %d\n", len);
|
|
|
|
|
|
|
|
/* Read the packet itself. */
|
|
|
|
buffer_clear(&auth->identities);
|
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(msg))
|
|
|
|
l = sizeof(msg);
|
|
|
|
l = read(auth->fd, msg, l);
|
|
|
|
if (l <= 0)
|
|
|
|
fatal("Incomplete authentication reply.");
|
|
|
|
buffer_append(&auth->identities, (char *) msg, l);
|
|
|
|
len -= l;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get message type, and verify that we got a proper answer. */
|
|
|
|
buffer_get(&auth->identities, (char *) msg, 1);
|
|
|
|
if (msg[0] != SSH_AGENT_RSA_IDENTITIES_ANSWER)
|
|
|
|
fatal("Bad authentication reply message type: %d", msg[0]);
|
|
|
|
|
|
|
|
/* Get the number of entries in the response and check it for sanity. */
|
|
|
|
auth->howmany = buffer_get_int(&auth->identities);
|
|
|
|
if (auth->howmany > 1024)
|
|
|
|
fatal("Too many identities in authentication reply: %d\n", auth->howmany);
|
|
|
|
|
|
|
|
/* Return the first entry (if any). */
|
|
|
|
return ssh_get_next_identity(auth, e, n, comment);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Returns the next authentication identity for the agent. Other functions
|
|
|
|
* can be called between this and ssh_get_first_identity or two calls of this
|
|
|
|
* function. This returns 0 if there are no more identities. The caller
|
|
|
|
* must free comment after a successful return.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
ssh_get_next_identity(AuthenticationConnection *auth,
|
1999-11-16 02:37:16 +00:00
|
|
|
BIGNUM *e, BIGNUM *n, char **comment)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
unsigned int bits;
|
1999-11-16 02:37:16 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Return failure if no more entries. */
|
|
|
|
if (auth->howmany <= 0)
|
|
|
|
return 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Get the next entry from the packet. These will abort with a fatal
|
|
|
|
* error if the packet is too short or contains corrupt data.
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
bits = buffer_get_int(&auth->identities);
|
|
|
|
buffer_get_bignum(&auth->identities, e);
|
|
|
|
buffer_get_bignum(&auth->identities, n);
|
|
|
|
*comment = buffer_get_string(&auth->identities, NULL);
|
1999-10-27 03:42:43 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
if (bits != BN_num_bits(n))
|
2000-04-30 00:00:53 +00:00
|
|
|
log("Warning: identity keysize mismatch: actual %d, announced %u",
|
|
|
|
BN_num_bits(n), bits);
|
1999-11-16 02:37:16 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Decrement the number of remaining entries. */
|
|
|
|
auth->howmany--;
|
1999-10-27 03:42:43 +00:00
|
|
|
|
1999-11-24 13:26:21 +00:00
|
|
|
return 1;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Generates a random challenge, sends it to the agent, and waits for
|
|
|
|
* response from the agent. Returns true (non-zero) if the agent gave the
|
|
|
|
* correct answer, zero otherwise. Response type selects the style of
|
|
|
|
* response desired, with 0 corresponding to protocol version 1.0 (no longer
|
|
|
|
* supported) and 1 corresponding to protocol version 1.1.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
ssh_decrypt_challenge(AuthenticationConnection *auth,
|
1999-11-24 13:26:21 +00:00
|
|
|
BIGNUM* e, BIGNUM *n, BIGNUM *challenge,
|
1999-10-27 03:42:43 +00:00
|
|
|
unsigned char session_id[16],
|
|
|
|
unsigned int response_type,
|
|
|
|
unsigned char response[16])
|
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
Buffer buffer;
|
|
|
|
unsigned char buf[8192];
|
|
|
|
int len, l, i;
|
|
|
|
|
|
|
|
/* Response type 0 is no longer supported. */
|
|
|
|
if (response_type == 0)
|
|
|
|
fatal("Compatibility with ssh protocol version 1.0 no longer supported.");
|
|
|
|
|
|
|
|
/* Format a message to the agent. */
|
|
|
|
buf[0] = SSH_AGENTC_RSA_CHALLENGE;
|
|
|
|
buffer_init(&buffer);
|
|
|
|
buffer_append(&buffer, (char *) buf, 1);
|
|
|
|
buffer_put_int(&buffer, BN_num_bits(n));
|
|
|
|
buffer_put_bignum(&buffer, e);
|
|
|
|
buffer_put_bignum(&buffer, n);
|
|
|
|
buffer_put_bignum(&buffer, challenge);
|
|
|
|
buffer_append(&buffer, (char *) session_id, 16);
|
|
|
|
buffer_put_int(&buffer, response_type);
|
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
len = buffer_len(&buffer);
|
|
|
|
PUT_32BIT(buf, len);
|
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
1999-12-07 04:38:31 +00:00
|
|
|
if (atomicio(write, auth->fd, buf, 4) != 4 ||
|
|
|
|
atomicio(write, auth->fd, buffer_ptr(&buffer),
|
|
|
|
buffer_len(&buffer)) != buffer_len(&buffer)) {
|
1999-11-24 13:26:21 +00:00
|
|
|
error("Error writing to authentication socket.");
|
|
|
|
error_cleanup:
|
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Wait for response from the agent. First read the length of the
|
|
|
|
* response packet.
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
len = 4;
|
|
|
|
while (len > 0) {
|
|
|
|
l = read(auth->fd, buf + 4 - len, len);
|
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response length from authentication socket.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the length, and check it for sanity. */
|
|
|
|
len = GET_32BIT(buf);
|
|
|
|
if (len > 256 * 1024)
|
|
|
|
fatal("Authentication response too long: %d", len);
|
|
|
|
|
|
|
|
/* Read the rest of the response in tothe buffer. */
|
|
|
|
buffer_clear(&buffer);
|
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
|
|
|
l = read(auth->fd, buf, l);
|
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response from authentication socket.");
|
|
|
|
goto error_cleanup;
|
|
|
|
}
|
|
|
|
buffer_append(&buffer, (char *) buf, l);
|
|
|
|
len -= l;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the type of the packet. */
|
|
|
|
buffer_get(&buffer, (char *) buf, 1);
|
|
|
|
|
|
|
|
/* Check for agent failure message. */
|
|
|
|
if (buf[0] == SSH_AGENT_FAILURE) {
|
|
|
|
log("Agent admitted failure to authenticate using the key.");
|
|
|
|
goto error_cleanup;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Now it must be an authentication response packet. */
|
|
|
|
if (buf[0] != SSH_AGENT_RSA_RESPONSE)
|
|
|
|
fatal("Bad authentication response: %d", buf[0]);
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Get the response from the packet. This will abort with a fatal
|
|
|
|
* error if the packet is corrupt.
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
response[i] = buffer_get_char(&buffer);
|
|
|
|
|
|
|
|
/* The buffer containing the packet is no longer needed. */
|
|
|
|
buffer_free(&buffer);
|
|
|
|
|
|
|
|
/* Correct answer. */
|
|
|
|
return 1;
|
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-07-21 00:19:44 +00:00
|
|
|
/* Encode key for a message to the agent. */
|
|
|
|
|
|
|
|
void
|
|
|
|
ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
|
|
|
|
{
|
|
|
|
buffer_clear(b);
|
|
|
|
buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
|
|
|
|
buffer_put_int(b, BN_num_bits(key->n));
|
|
|
|
buffer_put_bignum(b, key->n);
|
|
|
|
buffer_put_bignum(b, key->e);
|
|
|
|
buffer_put_bignum(b, key->d);
|
|
|
|
/* To keep within the protocol: p < q for ssh. in SSL p > q */
|
|
|
|
buffer_put_bignum(b, key->iqmp); /* ssh key->u */
|
|
|
|
buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
|
|
|
|
buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
|
|
|
|
buffer_put_string(b, comment, strlen(comment));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
|
|
|
|
{
|
|
|
|
buffer_clear(b);
|
|
|
|
buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
|
|
|
|
buffer_put_cstring(b, KEX_DSS);
|
|
|
|
buffer_put_bignum2(b, key->p);
|
|
|
|
buffer_put_bignum2(b, key->q);
|
|
|
|
buffer_put_bignum2(b, key->g);
|
|
|
|
buffer_put_bignum2(b, key->pub_key);
|
|
|
|
buffer_put_bignum2(b, key->priv_key);
|
|
|
|
buffer_put_string(b, comment, strlen(comment));
|
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Adds an identity to the authentication server. This call is not meant to
|
|
|
|
* be used by normal applications.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
2000-07-21 00:19:44 +00:00
|
|
|
ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
Buffer buffer;
|
|
|
|
unsigned char buf[8192];
|
2000-07-11 07:31:38 +00:00
|
|
|
int len;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
buffer_init(&buffer);
|
2000-07-21 00:19:44 +00:00
|
|
|
|
|
|
|
switch (key->type) {
|
|
|
|
case KEY_RSA:
|
|
|
|
ssh_encode_identity_rsa(&buffer, key->rsa, comment);
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
ssh_encode_identity_dsa(&buffer, key->dsa, comment);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
len = buffer_len(&buffer);
|
|
|
|
PUT_32BIT(buf, len);
|
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
1999-12-07 04:38:31 +00:00
|
|
|
if (atomicio(write, auth->fd, buf, 4) != 4 ||
|
|
|
|
atomicio(write, auth->fd, buffer_ptr(&buffer),
|
|
|
|
buffer_len(&buffer)) != buffer_len(&buffer)) {
|
1999-11-24 13:26:21 +00:00
|
|
|
error("Error writing to authentication socket.");
|
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-11 07:31:38 +00:00
|
|
|
buffer_free(&buffer);
|
|
|
|
return ssh_agent_get_reply(auth);
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Removes an identity from the authentication server. This call is not
|
|
|
|
* meant to be used by normal applications.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
1999-11-24 13:26:21 +00:00
|
|
|
ssh_remove_identity(AuthenticationConnection *auth, RSA *key)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
Buffer buffer;
|
2000-07-11 07:31:38 +00:00
|
|
|
unsigned char buf[5];
|
|
|
|
int len;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Format a message to the agent. */
|
|
|
|
buffer_init(&buffer);
|
|
|
|
buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY);
|
|
|
|
buffer_put_int(&buffer, BN_num_bits(key->n));
|
|
|
|
buffer_put_bignum(&buffer, key->e);
|
|
|
|
buffer_put_bignum(&buffer, key->n);
|
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
len = buffer_len(&buffer);
|
|
|
|
PUT_32BIT(buf, len);
|
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
1999-12-07 04:38:31 +00:00
|
|
|
if (atomicio(write, auth->fd, buf, 4) != 4 ||
|
|
|
|
atomicio(write, auth->fd, buffer_ptr(&buffer),
|
|
|
|
buffer_len(&buffer)) != buffer_len(&buffer)) {
|
1999-11-24 13:26:21 +00:00
|
|
|
error("Error writing to authentication socket.");
|
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-11 07:31:38 +00:00
|
|
|
buffer_free(&buffer);
|
|
|
|
return ssh_agent_get_reply(auth);
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Removes all identities from the agent. This call is not meant to be used
|
|
|
|
* by normal applications.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
1999-11-24 13:26:21 +00:00
|
|
|
ssh_remove_all_identities(AuthenticationConnection *auth)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-07-11 07:31:38 +00:00
|
|
|
unsigned char buf[5];
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
PUT_32BIT(buf, 1);
|
|
|
|
buf[4] = SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES;
|
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
1999-12-07 04:38:31 +00:00
|
|
|
if (atomicio(write, auth->fd, buf, 5) != 5) {
|
1999-11-24 13:26:21 +00:00
|
|
|
error("Error writing to authentication socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
2000-07-11 07:31:38 +00:00
|
|
|
return ssh_agent_get_reply(auth);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read for reply from agent. returns 1 for success, 0 on error
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_agent_get_reply(AuthenticationConnection *auth)
|
|
|
|
{
|
|
|
|
Buffer buffer;
|
|
|
|
unsigned char buf[8192];
|
|
|
|
int len, l, type;
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Wait for response from the agent. First read the length of the
|
|
|
|
* response packet.
|
|
|
|
*/
|
1999-11-24 13:26:21 +00:00
|
|
|
len = 4;
|
|
|
|
while (len > 0) {
|
|
|
|
l = read(auth->fd, buf + 4 - len, len);
|
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response length from authentication socket.");
|
2000-07-11 07:31:38 +00:00
|
|
|
buffer_free(&buffer);
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the length, and check it for sanity. */
|
|
|
|
len = GET_32BIT(buf);
|
|
|
|
if (len > 256 * 1024)
|
2000-07-11 07:31:38 +00:00
|
|
|
fatal("Response from agent too long: %d", len);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-07-11 07:31:38 +00:00
|
|
|
/* Read the rest of the response in to the buffer. */
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_init(&buffer);
|
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
|
|
|
l = read(auth->fd, buf, l);
|
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response from authentication socket.");
|
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buffer_append(&buffer, (char *) buf, l);
|
|
|
|
len -= l;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the type of the packet. */
|
|
|
|
type = buffer_get_char(&buffer);
|
2000-07-11 07:31:38 +00:00
|
|
|
buffer_free(&buffer);
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (type) {
|
|
|
|
case SSH_AGENT_FAILURE:
|
2000-07-21 00:19:44 +00:00
|
|
|
log("SSH_AGENT_FAILURE");
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
|
|
|
case SSH_AGENT_SUCCESS:
|
|
|
|
return 1;
|
|
|
|
default:
|
2000-07-11 07:31:38 +00:00
|
|
|
fatal("Bad response from authentication agent: %d", type);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
/* NOTREACHED */
|
|
|
|
return 0;
|
|
|
|
}
|