authfd.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* 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.
*
* 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".
*
* SSH2 implementation,
* 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
*
* 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.
*/
#include "includes.h"
#pragma ident "%Z%%M% %I% %E% SMI"
#include "ssh.h"
#include "rsa.h"
#include "buffer.h"
#include "bufaux.h"
#include "xmalloc.h"
#include "getput.h"
#include "key.h"
#include "authfd.h"
#include "cipher.h"
#include "kex.h"
#include "compat.h"
#include "log.h"
#include "atomicio.h"
static int agent_present = 0;
/* helper */
int decode_reply(int type);
/* macro to check for "agent failure" message */
#define agent_failed(x) \
((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \
(x == SSH2_AGENT_FAILURE))
int
ssh_agent_present(void)
{
int authfd;
if (agent_present)
return 1;
return 0;
else {
return 1;
}
}
/* Returns the number of the authentication fd, or -1 if there is none. */
int
{
const char *authsocket;
int sock;
struct sockaddr_un sunaddr;
if (!authsocket)
return -1;
if (sock < 0)
return -1;
/* close on exec */
return -1;
}
return -1;
}
agent_present = 1;
return sock;
}
static int
{
int l, len;
char buf[1024];
/* Get the length of the message, and format it in the buffer. */
/* Send the length and then the packet to the agent. */
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) {
continue;
if (l <= 0) {
error("Error reading response length from authentication socket.");
return 0;
}
len -= l;
}
/* Extract the length, and check it for sanity. */
/* Read the rest of the response in to the buffer. */
while (len > 0) {
l = len;
if (l > sizeof(buf))
l = sizeof(buf);
continue;
if (l <= 0) {
error("Error reading response from authentication socket.");
return 0;
}
len -= l;
}
return 1;
}
/*
* 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().
*/
void
{
}
/*
* 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.
*/
{
int sock;
/*
* Fail if we couldn't obtain a connection. This happens if we
* exited due to a timeout.
*/
if (sock < 0)
return NULL;
return auth;
}
/*
* Closes the connection to the authentication agent and frees any associated
* memory.
*/
void
{
}
int
{
int type;
buffer_init(&msg);
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return decode_reply(type);
}
/*
* Returns the first authentication identity held by the agent.
*/
int
{
switch (version) {
case 1:
break;
case 2:
break;
default:
return 0;
}
/*
* Send a message to the agent requesting for a list of the
* identities it can represent.
*/
return 0;
}
/* Get message type, and verify that we got a proper answer. */
if (agent_failed(type)) {
return 0;
}
/* Get the number of entries in the response and check it for sanity. */
fatal("Too many identities in authentication reply: %d",
}
Key *
{
/* get number of identities and return the first entry (if any). */
return NULL;
}
Key *
{
/* Return failure if no more entries. */
return NULL;
/*
* Get the next entry from the packet. These will abort with a fatal
* error if the packet is too short or contains corrupt data.
*/
switch (version) {
case 1:
log("Warning: identity keysize mismatch: actual %d, announced %u",
break;
case 2:
break;
default:
return NULL;
break;
}
/* Decrement the number of remaining entries. */
return key;
}
/*
* 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
{
int success = 0;
int i;
int type;
return 0;
if (response_type == 0) {
log("Compatibility with ssh protocol version 1.0 no longer supported.");
return 0;
}
return 0;
}
if (agent_failed(type)) {
log("Agent admitted failure to authenticate using the key.");
} else if (type != SSH_AGENT_RSA_RESPONSE) {
} 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++)
}
return success;
}
/* ask agent to sign data, returns -1 on error, 0 on success */
int
{
extern int datafellows;
int ret = -1;
return -1;
if (datafellows & SSH_BUG_SIGBLOB)
buffer_init(&msg);
buffer_free(&msg);
return -1;
}
if (agent_failed(type)) {
log("Agent admitted failure to sign using the key.");
} else if (type != SSH2_AGENT_SIGN_RESPONSE) {
} else {
ret = 0;
}
buffer_free(&msg);
return ret;
}
/* Encode key for a message to the agent. */
static void
{
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 */
}
static void
{
case KEY_RSA:
break;
case KEY_DSA:
break;
}
}
/*
* Adds an identity to the authentication server. This call is not meant to
* be used by normal applications.
*/
int
{
buffer_init(&msg);
case KEY_RSA1:
type = constrained ?
break;
case KEY_RSA:
case KEY_DSA:
type = constrained ?
break;
default:
buffer_free(&msg);
return 0;
break;
}
if (constrained) {
if (life != 0) {
}
}
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return decode_reply(type);
}
int
{
}
/*
* Removes an identity from the authentication server. This call is not
* meant to be used by normal applications.
*/
int
{
int type;
buffer_init(&msg);
} else {
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return decode_reply(type);
}
int
{
int type;
buffer_init(&msg);
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return decode_reply(type);
}
/*
* Removes all identities from the agent. This call is not meant to be used
* by normal applications.
*/
int
{
int type;
buffer_init(&msg);
buffer_free(&msg);
return 0;
}
buffer_free(&msg);
return decode_reply(type);
}
int
decode_reply(int type)
{
switch (type) {
case SSH_AGENT_FAILURE:
case SSH_COM_AGENT2_FAILURE:
case SSH2_AGENT_FAILURE:
log("SSH_AGENT_FAILURE");
return 0;
case SSH_AGENT_SUCCESS:
return 1;
default:
}
/* NOTREACHED */
return 0;
}