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