1999-10-27 03:42:43 +00:00
|
|
|
/*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* Functions for connecting the local authentication agent.
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*
|
2000-08-23 00:46:23 +00:00
|
|
|
* SSH2 implementation,
|
2000-09-16 02:29:08 +00:00
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2000-08-23 00:46:23 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1999-11-24 13:26:21 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
2001-04-05 23:26:32 +00:00
|
|
|
RCSID("$OpenBSD: authfd.c,v 1.39 2001/04/05 10:42:48 markus Exp $");
|
2001-01-22 05:34:40 +00:00
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
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"
|
2000-07-21 00:19:44 +00:00
|
|
|
#include "key.h"
|
|
|
|
#include "authfd.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "cipher.h"
|
2000-07-21 00:19:44 +00:00
|
|
|
#include "kex.h"
|
2000-09-23 06:15:56 +00:00
|
|
|
#include "compat.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "atomicio.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-07-11 07:31:38 +00:00
|
|
|
/* helper */
|
2000-08-18 03:59:06 +00:00
|
|
|
int decode_reply(int type);
|
2000-07-11 07:31:38 +00:00
|
|
|
|
2000-10-14 05:23:11 +00:00
|
|
|
/* macro to check for "agent failure" message */
|
|
|
|
#define agent_failed(x) \
|
|
|
|
((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE))
|
|
|
|
|
1999-10-27 03:42:43 +00:00
|
|
|
/* Returns the number of the authentication fd, or -1 if there is none. */
|
|
|
|
|
|
|
|
int
|
2000-12-22 01:43:59 +00:00
|
|
|
ssh_get_authentication_socket(void)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
const char *authsocket;
|
2000-08-18 03:59:06 +00:00
|
|
|
int sock, len;
|
1999-11-24 13:26:21 +00:00
|
|
|
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));
|
2000-08-18 03:59:06 +00:00
|
|
|
len = SUN_LEN(&sunaddr)+1;
|
2001-03-05 06:33:23 +00:00
|
|
|
#ifdef HAVE_SUN_LEN_IN_SOCKADDR_UN
|
|
|
|
sunaddr.sun_len = len;
|
2000-08-18 03:59:06 +00:00
|
|
|
#endif /* HAVE_SUN_LEN_IN_SOCKADDR_UN */
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2000-08-18 03:59:06 +00:00
|
|
|
if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
|
1999-11-24 13:26:21 +00:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return sock;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2000-08-18 03:59:06 +00:00
|
|
|
int
|
2000-08-23 00:46:23 +00:00
|
|
|
ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
|
2000-08-18 03:59:06 +00:00
|
|
|
{
|
|
|
|
int l, len;
|
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
len = buffer_len(request);
|
|
|
|
PUT_32BIT(buf, len);
|
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
|
|
|
if (atomicio(write, auth->fd, buf, 4) != 4 ||
|
|
|
|
atomicio(write, auth->fd, buffer_ptr(request),
|
|
|
|
buffer_len(request)) != buffer_len(request)) {
|
|
|
|
error("Error writing to authentication socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Wait for response from the agent. First read the length of the
|
|
|
|
* response packet.
|
|
|
|
*/
|
|
|
|
len = 4;
|
|
|
|
while (len > 0) {
|
|
|
|
l = read(auth->fd, buf + 4 - len, len);
|
2001-03-06 03:31:34 +00:00
|
|
|
if (l == -1 && (errno == EAGAIN || errno == EINTR))
|
2001-04-05 23:26:32 +00:00
|
|
|
continue;
|
2000-08-18 03:59:06 +00:00
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response length from authentication socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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 to the buffer. */
|
|
|
|
buffer_clear(reply);
|
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
|
|
|
l = read(auth->fd, buf, l);
|
2001-03-06 03:31:34 +00:00
|
|
|
if (l == -1 && (errno == EAGAIN || errno == EINTR))
|
2001-04-05 23:26:32 +00:00
|
|
|
continue;
|
2000-08-18 03:59:06 +00:00
|
|
|
if (l <= 0) {
|
|
|
|
error("Error reading response from authentication socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
buffer_append(reply, (char *) buf, l);
|
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
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 *
|
2000-12-22 01:43:59 +00:00
|
|
|
ssh_get_authentication_connection(void)
|
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->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
|
2000-08-23 00:46:23 +00:00
|
|
|
ssh_close_authentication_connection(AuthenticationConnection *auth)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_free(&auth->identities);
|
|
|
|
close(auth->fd);
|
|
|
|
xfree(auth);
|
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.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
int
|
|
|
|
ssh_get_num_identities(AuthenticationConnection *auth, int version)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-08-23 00:46:23 +00:00
|
|
|
int type, code1 = 0, code2 = 0;
|
2000-08-18 03:59:06 +00:00
|
|
|
Buffer request;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
|
|
|
switch(version){
|
|
|
|
case 1:
|
|
|
|
code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
|
|
|
|
code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
|
|
|
|
code2 = SSH2_AGENT_IDENTITIES_ANSWER;
|
|
|
|
break;
|
|
|
|
default:
|
2000-11-13 11:57:25 +00:00
|
|
|
return 0;
|
2000-08-23 00:46:23 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
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-08-18 03:59:06 +00:00
|
|
|
buffer_init(&request);
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_put_char(&request, code1);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
buffer_clear(&auth->identities);
|
2000-08-18 03:59:06 +00:00
|
|
|
if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
|
|
|
|
buffer_free(&request);
|
2000-11-13 11:57:25 +00:00
|
|
|
return 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
2000-08-18 03:59:06 +00:00
|
|
|
buffer_free(&request);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get message type, and verify that we got a proper answer. */
|
2000-08-18 03:59:06 +00:00
|
|
|
type = buffer_get_char(&auth->identities);
|
2000-10-14 05:23:11 +00:00
|
|
|
if (agent_failed(type)) {
|
2000-11-13 11:57:25 +00:00
|
|
|
return 0;
|
2000-08-23 00:46:23 +00:00
|
|
|
} else if (type != code2) {
|
2000-08-18 03:59:06 +00:00
|
|
|
fatal("Bad authentication reply message type: %d", type);
|
2000-08-23 00:46:23 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the number of entries in the response and check it for sanity. */
|
|
|
|
auth->howmany = buffer_get_int(&auth->identities);
|
|
|
|
if (auth->howmany > 1024)
|
2001-03-05 07:47:23 +00:00
|
|
|
fatal("Too many identities in authentication reply: %d",
|
2000-08-18 03:59:06 +00:00
|
|
|
auth->howmany);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
return auth->howmany;
|
|
|
|
}
|
|
|
|
|
|
|
|
Key *
|
|
|
|
ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
|
|
|
|
{
|
|
|
|
/* get number of identities and return the first entry (if any). */
|
|
|
|
if (ssh_get_num_identities(auth, version) > 0)
|
|
|
|
return ssh_get_next_identity(auth, comment, version);
|
|
|
|
return NULL;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
Key *
|
|
|
|
ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-12-22 01:43:59 +00:00
|
|
|
u_int bits;
|
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-08-23 00:46:23 +00:00
|
|
|
Key *key = NULL;
|
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)
|
2000-08-23 00:46:23 +00:00
|
|
|
return NULL;
|
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.
|
|
|
|
*/
|
2000-08-23 00:46:23 +00:00
|
|
|
switch(version){
|
|
|
|
case 1:
|
2000-11-13 11:57:25 +00:00
|
|
|
key = key_new(KEY_RSA1);
|
2000-08-23 00:46:23 +00:00
|
|
|
bits = buffer_get_int(&auth->identities);
|
|
|
|
buffer_get_bignum(&auth->identities, key->rsa->e);
|
|
|
|
buffer_get_bignum(&auth->identities, key->rsa->n);
|
|
|
|
*comment = buffer_get_string(&auth->identities, NULL);
|
|
|
|
if (bits != BN_num_bits(key->rsa->n))
|
|
|
|
log("Warning: identity keysize mismatch: actual %d, announced %u",
|
|
|
|
BN_num_bits(key->rsa->n), bits);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
blob = buffer_get_string(&auth->identities, &blen);
|
|
|
|
*comment = buffer_get_string(&auth->identities, NULL);
|
2000-11-13 11:57:25 +00:00
|
|
|
key = key_from_blob(blob, blen);
|
2000-08-23 00:46:23 +00:00
|
|
|
xfree(blob);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
break;
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
/* Decrement the number of remaining entries. */
|
|
|
|
auth->howmany--;
|
2000-08-23 00:46:23 +00:00
|
|
|
return key;
|
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,
|
2000-08-23 00:46:23 +00:00
|
|
|
Key* key, BIGNUM *challenge,
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char session_id[16],
|
|
|
|
u_int response_type,
|
|
|
|
u_char response[16])
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
Buffer buffer;
|
2000-08-18 03:59:06 +00:00
|
|
|
int success = 0;
|
|
|
|
int i;
|
|
|
|
int type;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
if (key->type != KEY_RSA1)
|
2000-08-23 00:46:23 +00:00
|
|
|
return 0;
|
|
|
|
if (response_type == 0) {
|
|
|
|
log("Compatibility with ssh protocol version 1.0 no longer supported.");
|
|
|
|
return 0;
|
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_init(&buffer);
|
2000-08-18 03:59:06 +00:00
|
|
|
buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
|
|
|
|
buffer_put_bignum(&buffer, key->rsa->e);
|
|
|
|
buffer_put_bignum(&buffer, key->rsa->n);
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_put_bignum(&buffer, challenge);
|
|
|
|
buffer_append(&buffer, (char *) session_id, 16);
|
|
|
|
buffer_put_int(&buffer, response_type);
|
|
|
|
|
2000-08-18 03:59:06 +00:00
|
|
|
if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-08-18 03:59:06 +00:00
|
|
|
type = buffer_get_char(&buffer);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-10-14 05:23:11 +00:00
|
|
|
if (agent_failed(type)) {
|
1999-11-24 13:26:21 +00:00
|
|
|
log("Agent admitted failure to authenticate using the key.");
|
2000-08-18 03:59:06 +00:00
|
|
|
} else if (type != SSH_AGENT_RSA_RESPONSE) {
|
|
|
|
fatal("Bad authentication response: %d", type);
|
|
|
|
} else {
|
|
|
|
success = 1;
|
|
|
|
/*
|
|
|
|
* Get the response from the packet. This will abort with a
|
|
|
|
* fatal error if the packet is corrupt.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < 16; i++)
|
|
|
|
response[i] = buffer_get_char(&buffer);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
buffer_free(&buffer);
|
2000-08-18 03:59:06 +00:00
|
|
|
return success;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
/* ask agent to sign data, returns -1 on error, 0 on success */
|
|
|
|
int
|
|
|
|
ssh_agent_sign(AuthenticationConnection *auth,
|
|
|
|
Key *key,
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char **sigp, int *lenp,
|
|
|
|
u_char *data, int datalen)
|
2000-08-23 00:46:23 +00:00
|
|
|
{
|
2000-09-23 06:15:56 +00:00
|
|
|
extern int datafellows;
|
2000-08-23 00:46:23 +00:00
|
|
|
Buffer msg;
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-09-23 06:15:56 +00:00
|
|
|
int type, flags = 0;
|
2000-08-23 00:46:23 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
if (key_to_blob(key, &blob, &blen) == 0)
|
2000-08-23 00:46:23 +00:00
|
|
|
return -1;
|
|
|
|
|
2000-09-23 06:15:56 +00:00
|
|
|
if (datafellows & SSH_BUG_SIGBLOB)
|
|
|
|
flags = SSH_AGENT_OLD_SIGNATURE;
|
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_init(&msg);
|
|
|
|
buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
|
|
|
|
buffer_put_string(&msg, blob, blen);
|
|
|
|
buffer_put_string(&msg, data, datalen);
|
2000-09-23 06:15:56 +00:00
|
|
|
buffer_put_int(&msg, flags);
|
2000-08-23 00:46:23 +00:00
|
|
|
xfree(blob);
|
|
|
|
|
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
type = buffer_get_char(&msg);
|
2000-10-14 05:23:11 +00:00
|
|
|
if (agent_failed(type)) {
|
2000-08-23 00:46:23 +00:00
|
|
|
log("Agent admitted failure to sign using the key.");
|
|
|
|
} else if (type != SSH2_AGENT_SIGN_RESPONSE) {
|
|
|
|
fatal("Bad authentication response: %d", type);
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
*sigp = buffer_get_string(&msg, lenp);
|
|
|
|
}
|
|
|
|
buffer_free(&msg);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2000-07-21 00:19:44 +00:00
|
|
|
/* Encode key for a message to the agent. */
|
|
|
|
|
|
|
|
void
|
2000-11-13 11:57:25 +00:00
|
|
|
ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
|
2000-07-21 00:19:44 +00:00
|
|
|
{
|
|
|
|
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
|
2000-11-13 11:57:25 +00:00
|
|
|
ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
|
2000-07-21 00:19:44 +00:00
|
|
|
{
|
|
|
|
buffer_clear(b);
|
|
|
|
buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
|
2000-11-13 11:57:25 +00:00
|
|
|
buffer_put_cstring(b, key_ssh_name(key));
|
|
|
|
switch(key->type){
|
|
|
|
case KEY_RSA:
|
|
|
|
buffer_put_bignum2(b, key->rsa->n);
|
|
|
|
buffer_put_bignum2(b, key->rsa->e);
|
|
|
|
buffer_put_bignum2(b, key->rsa->d);
|
|
|
|
buffer_put_bignum2(b, key->rsa->iqmp);
|
|
|
|
buffer_put_bignum2(b, key->rsa->p);
|
|
|
|
buffer_put_bignum2(b, key->rsa->q);
|
|
|
|
break;
|
|
|
|
case KEY_DSA:
|
|
|
|
buffer_put_bignum2(b, key->dsa->p);
|
|
|
|
buffer_put_bignum2(b, key->dsa->q);
|
|
|
|
buffer_put_bignum2(b, key->dsa->g);
|
|
|
|
buffer_put_bignum2(b, key->dsa->pub_key);
|
|
|
|
buffer_put_bignum2(b, key->dsa->priv_key);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer_put_cstring(b, comment);
|
2000-07-21 00:19:44 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2000-08-23 00:46:23 +00:00
|
|
|
Buffer msg;
|
2000-08-18 03:59:06 +00:00
|
|
|
int type;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_init(&msg);
|
2000-07-21 00:19:44 +00:00
|
|
|
|
|
|
|
switch (key->type) {
|
2000-11-13 11:57:25 +00:00
|
|
|
case KEY_RSA1:
|
|
|
|
ssh_encode_identity_rsa1(&msg, key->rsa, comment);
|
2000-07-21 00:19:44 +00:00
|
|
|
break;
|
2000-11-13 11:57:25 +00:00
|
|
|
case KEY_RSA:
|
2000-07-21 00:19:44 +00:00
|
|
|
case KEY_DSA:
|
2000-11-13 11:57:25 +00:00
|
|
|
ssh_encode_identity_ssh2(&msg, key, comment);
|
2000-07-21 00:19:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_free(&msg);
|
2000-07-21 00:19:44 +00:00
|
|
|
return 0;
|
|
|
|
break;
|
|
|
|
}
|
2000-08-23 00:46:23 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-08-23 00:46:23 +00:00
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
2000-08-18 03:59:06 +00:00
|
|
|
return decode_reply(type);
|
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
|
2000-08-23 00:46:23 +00:00
|
|
|
ssh_remove_identity(AuthenticationConnection *auth, Key *key)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-08-23 00:46:23 +00:00
|
|
|
Buffer msg;
|
2000-08-18 03:59:06 +00:00
|
|
|
int type;
|
2000-12-22 01:43:59 +00:00
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
|
|
|
buffer_init(&msg);
|
|
|
|
|
2000-11-13 11:57:25 +00:00
|
|
|
if (key->type == KEY_RSA1) {
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
|
|
|
|
buffer_put_int(&msg, BN_num_bits(key->rsa->n));
|
|
|
|
buffer_put_bignum(&msg, key->rsa->e);
|
|
|
|
buffer_put_bignum(&msg, key->rsa->n);
|
2000-11-13 11:57:25 +00:00
|
|
|
} else if (key->type == KEY_DSA || key->type == KEY_RSA) {
|
|
|
|
key_to_blob(key, &blob, &blen);
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
|
|
|
|
buffer_put_string(&msg, blob, blen);
|
|
|
|
xfree(blob);
|
|
|
|
} else {
|
|
|
|
buffer_free(&msg);
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-08-23 00:46:23 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
2000-08-18 03:59:06 +00:00
|
|
|
return decode_reply(type);
|
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
|
2000-08-23 00:46:23 +00:00
|
|
|
ssh_remove_all_identities(AuthenticationConnection *auth, int version)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2000-08-23 00:46:23 +00:00
|
|
|
Buffer msg;
|
2000-08-18 03:59:06 +00:00
|
|
|
int type;
|
2000-08-23 00:46:23 +00:00
|
|
|
int code = (version==1) ?
|
|
|
|
SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
|
|
|
|
SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
buffer_init(&msg);
|
|
|
|
buffer_put_char(&msg, code);
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2000-08-23 00:46:23 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
1999-11-24 13:26:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-08-23 00:46:23 +00:00
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
2000-08-18 03:59:06 +00:00
|
|
|
return decode_reply(type);
|
2000-07-11 07:31:38 +00:00
|
|
|
}
|
|
|
|
|
2001-02-05 12:42:17 +00:00
|
|
|
int
|
2000-08-18 03:59:06 +00:00
|
|
|
decode_reply(int type)
|
2000-07-11 07:31:38 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
switch (type) {
|
|
|
|
case SSH_AGENT_FAILURE:
|
2000-10-14 05:23:11 +00:00
|
|
|
case SSH_COM_AGENT2_FAILURE:
|
2000-08-18 03:59:06 +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;
|
|
|
|
}
|