2014-03-22 15:23:38 +00:00
|
|
|
/* $OpenBSD: authfd.c,v 1.92 2014/01/31 16:39:19 tedu Exp $ */
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
2000-09-10 08:31:17 +00:00
|
|
|
* Functions for connecting the local authentication agent.
|
2000-05-15 04:37:24 +00:00
|
|
|
*
|
2000-09-10 08:31:17 +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-05-15 04:37:24 +00:00
|
|
|
*
|
2000-09-10 08:31:17 +00:00
|
|
|
* SSH2 implementation,
|
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
2000-05-15 04:37:24 +00:00
|
|
|
*
|
2000-09-10 08:31:17 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
2000-02-24 14:29:47 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "includes.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/socket.h>
|
2001-05-04 03:57:05 +00:00
|
|
|
|
|
|
|
#include <openssl/evp.h>
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <openssl/crypto.h>
|
2014-01-30 10:56:49 +00:00
|
|
|
|
2006-09-30 13:29:51 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "xmalloc.h"
|
2000-02-24 14:29:47 +00:00
|
|
|
#include "ssh.h"
|
|
|
|
#include "rsa.h"
|
|
|
|
#include "buffer.h"
|
2000-09-10 08:31:17 +00:00
|
|
|
#include "key.h"
|
|
|
|
#include "authfd.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "cipher.h"
|
2000-09-10 08:31:17 +00:00
|
|
|
#include "kex.h"
|
2000-12-05 02:20:19 +00:00
|
|
|
#include "compat.h"
|
2001-05-04 03:57:05 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "atomicio.h"
|
2006-09-30 13:29:51 +00:00
|
|
|
#include "misc.h"
|
2000-09-10 08:31:17 +00:00
|
|
|
|
2002-10-29 09:43:00 +00:00
|
|
|
static int agent_present = 0;
|
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
/* helper */
|
|
|
|
int decode_reply(int type);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-12-05 02:20:19 +00:00
|
|
|
/* macro to check for "agent failure" message */
|
|
|
|
#define agent_failed(x) \
|
2002-03-18 09:55:03 +00:00
|
|
|
((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \
|
2002-06-23 14:01:54 +00:00
|
|
|
(x == SSH2_AGENT_FAILURE))
|
2000-12-05 02:20:19 +00:00
|
|
|
|
2002-10-29 09:43:00 +00:00
|
|
|
int
|
|
|
|
ssh_agent_present(void)
|
|
|
|
{
|
|
|
|
int authfd;
|
|
|
|
|
|
|
|
if (agent_present)
|
|
|
|
return 1;
|
|
|
|
if ((authfd = ssh_get_authentication_socket()) == -1)
|
|
|
|
return 0;
|
|
|
|
else {
|
|
|
|
ssh_close_authentication_socket(authfd);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Returns the number of the authentication fd, or -1 if there is none. */
|
|
|
|
|
|
|
|
int
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_get_authentication_socket(void)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
const char *authsocket;
|
2002-03-18 09:55:03 +00:00
|
|
|
int sock;
|
2000-02-24 14:29:47 +00:00
|
|
|
struct sockaddr_un sunaddr;
|
|
|
|
|
|
|
|
authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
if (!authsocket)
|
|
|
|
return -1;
|
|
|
|
|
2014-03-22 15:23:38 +00:00
|
|
|
memset(&sunaddr, 0, sizeof(sunaddr));
|
2000-02-24 14:29:47 +00:00
|
|
|
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 */
|
2011-09-28 08:14:41 +00:00
|
|
|
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
|
2000-02-24 14:29:47 +00:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
2006-09-30 13:29:51 +00:00
|
|
|
if (connect(sock, (struct sockaddr *)&sunaddr, sizeof sunaddr) < 0) {
|
2000-02-24 14:29:47 +00:00
|
|
|
close(sock);
|
|
|
|
return -1;
|
|
|
|
}
|
2002-10-29 09:43:00 +00:00
|
|
|
agent_present = 1;
|
2000-02-24 14:29:47 +00:00
|
|
|
return sock;
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static int
|
2000-09-10 08:31:17 +00:00
|
|
|
ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
|
|
|
|
{
|
2005-09-03 06:59:33 +00:00
|
|
|
u_int l, len;
|
2000-09-10 08:31:17 +00:00
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
|
|
|
len = buffer_len(request);
|
2006-09-30 13:29:51 +00:00
|
|
|
put_u32(buf, len);
|
2000-09-10 08:31:17 +00:00
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
2004-01-07 11:10:17 +00:00
|
|
|
if (atomicio(vwrite, auth->fd, buf, 4) != 4 ||
|
|
|
|
atomicio(vwrite, auth->fd, buffer_ptr(request),
|
2000-09-10 08:31:17 +00:00
|
|
|
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.
|
|
|
|
*/
|
2004-10-28 16:03:53 +00:00
|
|
|
if (atomicio(read, auth->fd, buf, 4) != 4) {
|
|
|
|
error("Error reading response length from authentication socket.");
|
|
|
|
return 0;
|
2000-09-10 08:31:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Extract the length, and check it for sanity. */
|
2006-09-30 13:29:51 +00:00
|
|
|
len = get_u32(buf);
|
2000-09-10 08:31:17 +00:00
|
|
|
if (len > 256 * 1024)
|
2004-02-26 10:38:49 +00:00
|
|
|
fatal("Authentication response too long: %u", len);
|
2000-09-10 08:31:17 +00:00
|
|
|
|
|
|
|
/* 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);
|
2005-09-03 06:59:33 +00:00
|
|
|
if (atomicio(read, auth->fd, buf, l) != l) {
|
2000-09-10 08:31:17 +00:00
|
|
|
error("Error reading response from authentication socket.");
|
|
|
|
return 0;
|
|
|
|
}
|
2002-06-29 11:34:13 +00:00
|
|
|
buffer_append(reply, buf, l);
|
2000-09-10 08:31:17 +00:00
|
|
|
len -= l;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +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().
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
void
|
2000-02-24 14:29:47 +00:00
|
|
|
ssh_close_authentication_socket(int sock)
|
|
|
|
{
|
|
|
|
if (getenv(SSH_AUTHSOCKET_ENV_NAME))
|
|
|
|
close(sock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
AuthenticationConnection *
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_get_authentication_connection(void)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
AuthenticationConnection *auth;
|
|
|
|
int sock;
|
|
|
|
|
|
|
|
sock = ssh_get_authentication_socket();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fail if we couldn't obtain a connection. This happens if we
|
|
|
|
* exited due to a timeout.
|
|
|
|
*/
|
|
|
|
if (sock < 0)
|
|
|
|
return NULL;
|
|
|
|
|
2013-11-11 09:13:37 +00:00
|
|
|
auth = xcalloc(1, sizeof(*auth));
|
2000-02-24 14:29:47 +00:00
|
|
|
auth->fd = sock;
|
|
|
|
buffer_init(&auth->identities);
|
|
|
|
auth->howmany = 0;
|
|
|
|
|
|
|
|
return auth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Closes the connection to the authentication agent and frees any associated
|
|
|
|
* memory.
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
void
|
2000-09-10 08:31:17 +00:00
|
|
|
ssh_close_authentication_connection(AuthenticationConnection *auth)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_free(&auth->identities);
|
|
|
|
close(auth->fd);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(auth);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2002-06-23 14:01:54 +00:00
|
|
|
/* Lock/unlock agent */
|
|
|
|
int
|
|
|
|
ssh_lock_agent(AuthenticationConnection *auth, int lock, const char *password)
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
Buffer msg;
|
|
|
|
|
|
|
|
buffer_init(&msg);
|
|
|
|
buffer_put_char(&msg, lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK);
|
|
|
|
buffer_put_cstring(&msg, password);
|
|
|
|
|
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
|
|
|
return decode_reply(type);
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Returns the first authentication identity held by the agent.
|
|
|
|
*/
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
int
|
|
|
|
ssh_get_num_identities(AuthenticationConnection *auth, int version)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2000-09-10 08:31:17 +00:00
|
|
|
int type, code1 = 0, code2 = 0;
|
|
|
|
Buffer request;
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
switch (version) {
|
2000-09-10 08:31:17 +00:00
|
|
|
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:
|
2001-05-04 03:57:05 +00:00
|
|
|
return 0;
|
2000-09-10 08:31:17 +00:00
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Send a message to the agent requesting for a list of the
|
|
|
|
* identities it can represent.
|
|
|
|
*/
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_init(&request);
|
|
|
|
buffer_put_char(&request, code1);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
buffer_clear(&auth->identities);
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
|
|
|
|
buffer_free(&request);
|
2001-05-04 03:57:05 +00:00
|
|
|
return 0;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_free(&request);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Get message type, and verify that we got a proper answer. */
|
2000-09-10 08:31:17 +00:00
|
|
|
type = buffer_get_char(&auth->identities);
|
2000-12-05 02:20:19 +00:00
|
|
|
if (agent_failed(type)) {
|
2001-05-04 03:57:05 +00:00
|
|
|
return 0;
|
2000-09-10 08:31:17 +00:00
|
|
|
} else if (type != code2) {
|
|
|
|
fatal("Bad authentication reply message type: %d", type);
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Get the number of entries in the response and check it for sanity. */
|
|
|
|
auth->howmany = buffer_get_int(&auth->identities);
|
2004-02-26 10:38:49 +00:00
|
|
|
if ((u_int)auth->howmany > 1024)
|
2001-05-04 03:57:05 +00:00
|
|
|
fatal("Too many identities in authentication reply: %d",
|
2000-09-10 08:31:17 +00:00
|
|
|
auth->howmany);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2001-05-04 03:57:05 +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;
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
Key *
|
|
|
|
ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2005-09-03 06:59:33 +00:00
|
|
|
int keybits;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_int bits;
|
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-09-10 08:31:17 +00:00
|
|
|
Key *key = NULL;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
|
|
|
/* Return failure if no more entries. */
|
|
|
|
if (auth->howmany <= 0)
|
2000-09-10 08:31:17 +00:00
|
|
|
return NULL;
|
2000-02-24 14:29:47 +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.
|
|
|
|
*/
|
2002-03-18 09:55:03 +00:00
|
|
|
switch (version) {
|
2000-09-10 08:31:17 +00:00
|
|
|
case 1:
|
2001-05-04 03:57:05 +00:00
|
|
|
key = key_new(KEY_RSA1);
|
2000-09-10 08:31:17 +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);
|
2005-09-03 06:59:33 +00:00
|
|
|
keybits = BN_num_bits(key->rsa->n);
|
|
|
|
if (keybits < 0 || bits != (u_int)keybits)
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("Warning: identity keysize mismatch: actual %d, announced %u",
|
2000-09-10 08:31:17 +00:00
|
|
|
BN_num_bits(key->rsa->n), bits);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
blob = buffer_get_string(&auth->identities, &blen);
|
|
|
|
*comment = buffer_get_string(&auth->identities, NULL);
|
2001-05-04 03:57:05 +00:00
|
|
|
key = key_from_blob(blob, blen);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(blob);
|
2000-09-10 08:31:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
/* Decrement the number of remaining entries. */
|
|
|
|
auth->howmany--;
|
2000-09-10 08:31:17 +00:00
|
|
|
return key;
|
2000-02-24 14:29:47 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
ssh_decrypt_challenge(AuthenticationConnection *auth,
|
2000-09-10 08:31:17 +00:00
|
|
|
Key* key, BIGNUM *challenge,
|
2001-05-04 03:57:05 +00:00
|
|
|
u_char session_id[16],
|
|
|
|
u_int response_type,
|
|
|
|
u_char response[16])
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
|
|
|
Buffer buffer;
|
2000-09-10 08:31:17 +00:00
|
|
|
int success = 0;
|
|
|
|
int i;
|
|
|
|
int type;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
if (key->type != KEY_RSA1)
|
2000-09-10 08:31:17 +00:00
|
|
|
return 0;
|
|
|
|
if (response_type == 0) {
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("Compatibility with ssh protocol version 1.0 no longer supported.");
|
2000-09-10 08:31:17 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
buffer_init(&buffer);
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
|
|
|
|
buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
|
|
|
|
buffer_put_bignum(&buffer, key->rsa->e);
|
|
|
|
buffer_put_bignum(&buffer, key->rsa->n);
|
2000-02-24 14:29:47 +00:00
|
|
|
buffer_put_bignum(&buffer, challenge);
|
2002-03-18 09:55:03 +00:00
|
|
|
buffer_append(&buffer, session_id, 16);
|
2000-02-24 14:29:47 +00:00
|
|
|
buffer_put_int(&buffer, response_type);
|
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
|
2000-02-24 14:29:47 +00:00
|
|
|
buffer_free(&buffer);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
type = buffer_get_char(&buffer);
|
|
|
|
|
2000-12-05 02:20:19 +00:00
|
|
|
if (agent_failed(type)) {
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("Agent admitted failure to authenticate using the key.");
|
2000-09-10 08:31:17 +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++)
|
2006-09-30 13:29:51 +00:00
|
|
|
response[i] = (u_char)buffer_get_char(&buffer);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_free(&buffer);
|
|
|
|
return success;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
/* ask agent to sign data, returns -1 on error, 0 on success */
|
|
|
|
int
|
|
|
|
ssh_agent_sign(AuthenticationConnection *auth,
|
|
|
|
Key *key,
|
2002-03-18 09:55:03 +00:00
|
|
|
u_char **sigp, u_int *lenp,
|
|
|
|
u_char *data, u_int datalen)
|
2000-09-10 08:31:17 +00:00
|
|
|
{
|
2000-12-05 02:20:19 +00:00
|
|
|
extern int datafellows;
|
2000-09-10 08:31:17 +00:00
|
|
|
Buffer msg;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-12-05 02:20:19 +00:00
|
|
|
int type, flags = 0;
|
2000-09-10 08:31:17 +00:00
|
|
|
int ret = -1;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
if (key_to_blob(key, &blob, &blen) == 0)
|
2000-09-10 08:31:17 +00:00
|
|
|
return -1;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-12-05 02:20:19 +00:00
|
|
|
if (datafellows & SSH_BUG_SIGBLOB)
|
|
|
|
flags = SSH_AGENT_OLD_SIGNATURE;
|
|
|
|
|
2000-09-10 08:31:17 +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-12-05 02:20:19 +00:00
|
|
|
buffer_put_int(&msg, flags);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(blob);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
type = buffer_get_char(&msg);
|
2000-12-05 02:20:19 +00:00
|
|
|
if (agent_failed(type)) {
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("Agent admitted failure to sign using the key.");
|
2000-09-10 08:31:17 +00:00
|
|
|
} else if (type != SSH2_AGENT_SIGN_RESPONSE) {
|
|
|
|
fatal("Bad authentication response: %d", type);
|
|
|
|
} else {
|
|
|
|
ret = 0;
|
|
|
|
*sigp = buffer_get_string(&msg, lenp);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_free(&msg);
|
|
|
|
return ret;
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
/* Encode key for a message to the agent. */
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
|
2000-09-10 08:31:17 +00:00
|
|
|
{
|
|
|
|
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 */
|
2002-03-18 09:55:03 +00:00
|
|
|
buffer_put_cstring(b, comment);
|
2000-09-10 08:31:17 +00:00
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
static void
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
|
2000-09-10 08:31:17 +00:00
|
|
|
{
|
2014-01-30 10:56:49 +00:00
|
|
|
key_private_serialize(key, b);
|
2001-05-04 03:57:05 +00:00
|
|
|
buffer_put_cstring(b, comment);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Adds an identity to the authentication server. This call is not meant to
|
|
|
|
* be used by normal applications.
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
int
|
2002-06-23 14:01:54 +00:00
|
|
|
ssh_add_identity_constrained(AuthenticationConnection *auth, Key *key,
|
2003-04-23 16:53:02 +00:00
|
|
|
const char *comment, u_int life, u_int confirm)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2000-09-10 08:31:17 +00:00
|
|
|
Buffer msg;
|
2003-04-23 16:53:02 +00:00
|
|
|
int type, constrained = (life || confirm);
|
2000-09-10 08:31:17 +00:00
|
|
|
|
|
|
|
buffer_init(&msg);
|
|
|
|
|
|
|
|
switch (key->type) {
|
2001-05-04 03:57:05 +00:00
|
|
|
case KEY_RSA1:
|
2002-06-23 14:01:54 +00:00
|
|
|
type = constrained ?
|
|
|
|
SSH_AGENTC_ADD_RSA_ID_CONSTRAINED :
|
|
|
|
SSH_AGENTC_ADD_RSA_IDENTITY;
|
|
|
|
buffer_put_char(&msg, type);
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_encode_identity_rsa1(&msg, key->rsa, comment);
|
2000-09-10 08:31:17 +00:00
|
|
|
break;
|
2001-05-04 03:57:05 +00:00
|
|
|
case KEY_RSA:
|
2010-03-08 11:19:52 +00:00
|
|
|
case KEY_RSA_CERT:
|
2010-11-08 10:45:44 +00:00
|
|
|
case KEY_RSA_CERT_V00:
|
2000-09-10 08:31:17 +00:00
|
|
|
case KEY_DSA:
|
2010-03-08 11:19:52 +00:00
|
|
|
case KEY_DSA_CERT:
|
2010-11-08 10:45:44 +00:00
|
|
|
case KEY_DSA_CERT_V00:
|
2011-02-17 11:47:40 +00:00
|
|
|
case KEY_ECDSA:
|
|
|
|
case KEY_ECDSA_CERT:
|
2014-01-30 10:56:49 +00:00
|
|
|
case KEY_ED25519:
|
|
|
|
case KEY_ED25519_CERT:
|
2002-06-23 14:01:54 +00:00
|
|
|
type = constrained ?
|
|
|
|
SSH2_AGENTC_ADD_ID_CONSTRAINED :
|
|
|
|
SSH2_AGENTC_ADD_IDENTITY;
|
|
|
|
buffer_put_char(&msg, type);
|
2001-05-04 03:57:05 +00:00
|
|
|
ssh_encode_identity_ssh2(&msg, key, comment);
|
2000-09-10 08:31:17 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
buffer_free(&msg);
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2002-06-23 14:01:54 +00:00
|
|
|
if (constrained) {
|
|
|
|
if (life != 0) {
|
|
|
|
buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
|
|
|
|
buffer_put_int(&msg, life);
|
|
|
|
}
|
2003-04-23 16:53:02 +00:00
|
|
|
if (confirm != 0)
|
|
|
|
buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_CONFIRM);
|
2002-06-23 14:01:54 +00:00
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
|
|
|
return decode_reply(type);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Removes an identity from the authentication server. This call is not
|
|
|
|
* meant to be used by normal applications.
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
int
|
2000-09-10 08:31:17 +00:00
|
|
|
ssh_remove_identity(AuthenticationConnection *auth, Key *key)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2000-09-10 08:31:17 +00:00
|
|
|
Buffer msg;
|
|
|
|
int type;
|
2001-05-04 03:57:05 +00:00
|
|
|
u_char *blob;
|
|
|
|
u_int blen;
|
2000-09-10 08:31:17 +00:00
|
|
|
|
|
|
|
buffer_init(&msg);
|
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
if (key->type == KEY_RSA1) {
|
2000-09-10 08:31:17 +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);
|
2014-01-30 10:56:49 +00:00
|
|
|
} else if (key->type != KEY_UNSPEC) {
|
2001-05-04 03:57:05 +00:00
|
|
|
key_to_blob(key, &blob, &blen);
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
|
|
|
|
buffer_put_string(&msg, blob, blen);
|
2013-09-18 17:27:38 +00:00
|
|
|
free(blob);
|
2000-09-10 08:31:17 +00:00
|
|
|
} else {
|
|
|
|
buffer_free(&msg);
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
|
|
|
return decode_reply(type);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
int
|
2004-02-26 10:38:49 +00:00
|
|
|
ssh_update_card(AuthenticationConnection *auth, int add,
|
2004-01-07 11:10:17 +00:00
|
|
|
const char *reader_id, const char *pin, u_int life, u_int confirm)
|
2002-03-18 09:55:03 +00:00
|
|
|
{
|
|
|
|
Buffer msg;
|
2004-01-07 11:10:17 +00:00
|
|
|
int type, constrained = (life || confirm);
|
|
|
|
|
|
|
|
if (add) {
|
|
|
|
type = constrained ?
|
|
|
|
SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
|
|
|
|
SSH_AGENTC_ADD_SMARTCARD_KEY;
|
|
|
|
} else
|
|
|
|
type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
|
2002-03-18 09:55:03 +00:00
|
|
|
|
|
|
|
buffer_init(&msg);
|
2004-01-07 11:10:17 +00:00
|
|
|
buffer_put_char(&msg, type);
|
2002-03-18 09:55:03 +00:00
|
|
|
buffer_put_cstring(&msg, reader_id);
|
2002-06-23 14:01:54 +00:00
|
|
|
buffer_put_cstring(&msg, pin);
|
2004-02-26 10:38:49 +00:00
|
|
|
|
2004-01-07 11:10:17 +00:00
|
|
|
if (constrained) {
|
|
|
|
if (life != 0) {
|
|
|
|
buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
|
|
|
|
buffer_put_int(&msg, life);
|
|
|
|
}
|
|
|
|
if (confirm != 0)
|
|
|
|
buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_CONFIRM);
|
|
|
|
}
|
|
|
|
|
2002-03-18 09:55:03 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
|
|
|
return decode_reply(type);
|
|
|
|
}
|
|
|
|
|
2000-02-24 14:29:47 +00:00
|
|
|
/*
|
|
|
|
* Removes all identities from the agent. This call is not meant to be used
|
|
|
|
* by normal applications.
|
|
|
|
*/
|
|
|
|
|
2000-05-15 04:37:24 +00:00
|
|
|
int
|
2000-09-10 08:31:17 +00:00
|
|
|
ssh_remove_all_identities(AuthenticationConnection *auth, int version)
|
2000-02-24 14:29:47 +00:00
|
|
|
{
|
2000-09-10 08:31:17 +00:00
|
|
|
Buffer msg;
|
|
|
|
int type;
|
|
|
|
int code = (version==1) ?
|
|
|
|
SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
|
|
|
|
SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
buffer_init(&msg);
|
|
|
|
buffer_put_char(&msg, code);
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2000-09-10 08:31:17 +00:00
|
|
|
if (ssh_request_reply(auth, &msg, &msg) == 0) {
|
|
|
|
buffer_free(&msg);
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2000-09-10 08:31:17 +00:00
|
|
|
type = buffer_get_char(&msg);
|
|
|
|
buffer_free(&msg);
|
|
|
|
return decode_reply(type);
|
|
|
|
}
|
2000-02-24 14:29:47 +00:00
|
|
|
|
2001-05-04 03:57:05 +00:00
|
|
|
int
|
2000-09-10 08:31:17 +00:00
|
|
|
decode_reply(int type)
|
|
|
|
{
|
2000-02-24 14:29:47 +00:00
|
|
|
switch (type) {
|
|
|
|
case SSH_AGENT_FAILURE:
|
2000-12-05 02:20:19 +00:00
|
|
|
case SSH_COM_AGENT2_FAILURE:
|
2002-03-18 09:55:03 +00:00
|
|
|
case SSH2_AGENT_FAILURE:
|
2004-01-07 11:10:17 +00:00
|
|
|
logit("SSH_AGENT_FAILURE");
|
2000-02-24 14:29:47 +00:00
|
|
|
return 0;
|
|
|
|
case SSH_AGENT_SUCCESS:
|
|
|
|
return 1;
|
|
|
|
default:
|
2000-09-10 08:31:17 +00:00
|
|
|
fatal("Bad response from authentication agent: %d", type);
|
2000-02-24 14:29:47 +00:00
|
|
|
}
|
|
|
|
/* NOTREACHED */
|
|
|
|
return 0;
|
|
|
|
}
|