2015-01-14 21:05:27 +01:00
|
|
|
/* $OpenBSD: authfd.c,v 1.94 2015/01/14 20:05:27 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
|
|
|
|
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>
|
2015-01-14 21:05:27 +01:00
|
|
|
#include <errno.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"
|
2015-01-14 21:05:27 +01:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
2000-07-21 02:19:44 +02:00
|
|
|
#include "authfd.h"
|
2001-01-22 06:34:40 +01:00
|
|
|
#include "cipher.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"
|
2015-01-14 21:05:27 +01:00
|
|
|
#include "ssherr.h"
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
#define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */
|
|
|
|
#define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */
|
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) \
|
2015-01-14 21:05:27 +01: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
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* Convert success/failure response from agent to a err.h status */
|
|
|
|
static int
|
|
|
|
decode_reply(u_char type)
|
2002-09-12 01:52:46 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
if (agent_failed(type))
|
|
|
|
return SSH_ERR_AGENT_FAILURE;
|
|
|
|
else if (type == SSH_AGENT_SUCCESS)
|
2002-09-12 01:52:46 +02:00
|
|
|
return 0;
|
2015-01-14 21:05:27 +01:00
|
|
|
else
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2002-09-12 01:52:46 +02:00
|
|
|
}
|
|
|
|
|
1999-10-27 05:42:43 +02:00
|
|
|
/* Returns the number of the authentication fd, or -1 if there is none. */
|
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_get_authentication_socket(int *fdp)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
1999-11-24 14:26:21 +01:00
|
|
|
const char *authsocket;
|
2015-01-14 21:05:27 +01:00
|
|
|
int sock, oerrno;
|
1999-11-24 14:26:21 +01:00
|
|
|
struct sockaddr_un sunaddr;
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if (fdp != NULL)
|
|
|
|
*fdp = -1;
|
|
|
|
|
1999-11-24 14:26:21 +01:00
|
|
|
authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
if (!authsocket)
|
2015-01-14 21:05:27 +01:00
|
|
|
return SSH_ERR_AGENT_NOT_PRESENT;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2014-02-04 01:18:20 +01:00
|
|
|
memset(&sunaddr, 0, sizeof(sunaddr));
|
1999-11-24 14:26:21 +01:00
|
|
|
sunaddr.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) < 0)
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
/* close on exec */
|
2015-01-14 21:05:27 +01:00
|
|
|
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
|
|
|
|
connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) < 0) {
|
|
|
|
oerrno = errno;
|
1999-11-24 14:26:21 +01:00
|
|
|
close(sock);
|
2015-01-14 21:05:27 +01:00
|
|
|
errno = oerrno;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
if (fdp != NULL)
|
|
|
|
*fdp = sock;
|
|
|
|
else
|
1999-11-24 14:26:21 +01:00
|
|
|
close(sock);
|
2015-01-14 21:05:27 +01:00
|
|
|
return 0;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* Communicate with agent: send request and read reply */
|
2001-06-25 07:01:22 +02:00
|
|
|
static int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
|
2000-08-18 05:59:06 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
int r;
|
|
|
|
size_t 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. */
|
2015-01-14 21:05:27 +01:00
|
|
|
len = sshbuf_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. */
|
2015-01-14 21:05:27 +01:00
|
|
|
if (atomicio(vwrite, sock, buf, 4) != 4 ||
|
|
|
|
atomicio(vwrite, sock, (u_char *)sshbuf_ptr(request),
|
|
|
|
sshbuf_len(request)) != sshbuf_len(request))
|
|
|
|
return SSH_ERR_AGENT_COMMUNICATION;
|
2000-08-18 05:59:06 +02:00
|
|
|
/*
|
|
|
|
* Wait for response from the agent. First read the length of the
|
|
|
|
* response packet.
|
|
|
|
*/
|
2015-01-14 21:05:27 +01:00
|
|
|
if (atomicio(read, sock, buf, 4) != 4)
|
|
|
|
return SSH_ERR_AGENT_COMMUNICATION;
|
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);
|
2015-01-14 21:05:27 +01:00
|
|
|
if (len > MAX_AGENT_REPLY_LEN)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2000-08-18 05:59:06 +02:00
|
|
|
|
|
|
|
/* Read the rest of the response in to the buffer. */
|
2015-01-14 21:05:27 +01:00
|
|
|
sshbuf_reset(reply);
|
2000-08-18 05:59:06 +02:00
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
2015-01-14 21:05:27 +01:00
|
|
|
if (atomicio(read, sock, buf, l) != l)
|
|
|
|
return SSH_ERR_AGENT_COMMUNICATION;
|
|
|
|
if ((r = sshbuf_put(reply, buf, l)) != 0)
|
|
|
|
return r;
|
2000-08-18 05:59:06 +02:00
|
|
|
len -= l;
|
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
return 0;
|
2000-08-18 05:59:06 +02:00
|
|
|
}
|
|
|
|
|
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().
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* Lock/unlock agent */
|
|
|
|
int
|
|
|
|
ssh_lock_agent(int sock, int lock, const char *password)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
int r;
|
|
|
|
u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
|
|
|
|
struct sshbuf *msg;
|
|
|
|
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(msg, password)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = decode_reply(type);
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
#ifdef WITH_SSH1
|
|
|
|
static int
|
|
|
|
deserialise_identity1(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshkey *key;
|
|
|
|
int r, keybits;
|
|
|
|
u_int32_t bits;
|
|
|
|
char *comment = NULL;
|
|
|
|
|
|
|
|
if ((key = sshkey_new(KEY_RSA1)) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_get_u32(ids, &bits)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(ids, key->rsa->e)) != 0 ||
|
|
|
|
(r = sshbuf_get_bignum1(ids, key->rsa->n)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
keybits = BN_num_bits(key->rsa->n);
|
|
|
|
/* XXX previously we just warned here. I think we should be strict */
|
|
|
|
if (keybits < 0 || bits != (u_int)keybits) {
|
|
|
|
r = SSH_ERR_KEY_BITS_MISMATCH;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (keyp != NULL) {
|
|
|
|
*keyp = key;
|
|
|
|
key = NULL;
|
|
|
|
}
|
|
|
|
if (commentp != NULL) {
|
|
|
|
*commentp = comment;
|
|
|
|
comment = NULL;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshkey_free(key);
|
|
|
|
free(comment);
|
|
|
|
return r;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
#endif
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
static int
|
|
|
|
deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
|
2002-06-06 23:52:03 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
int r;
|
|
|
|
char *comment = NULL;
|
|
|
|
const u_char *blob;
|
|
|
|
size_t blen;
|
|
|
|
|
|
|
|
if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
|
|
|
|
(r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (commentp != NULL) {
|
|
|
|
*commentp = comment;
|
|
|
|
comment = NULL;
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(comment);
|
|
|
|
return r;
|
2002-06-06 23:52:03 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2015-01-14 21:05:27 +01:00
|
|
|
* Fetch list of identities held by the agent.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2000-11-13 12:57:25 +01:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_fetch_identitylist(int sock, int version, struct ssh_identitylist **idlp)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
u_char type, code1 = 0, code2 = 0;
|
|
|
|
u_int32_t num, i;
|
|
|
|
struct sshbuf *msg;
|
|
|
|
struct ssh_identitylist *idl = NULL;
|
|
|
|
int r;
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* Determine request and expected response types */
|
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:
|
2015-01-14 21:05:27 +01:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
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.
|
|
|
|
*/
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, code1)) != 0)
|
|
|
|
goto out;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
|
|
|
/* Get message type, and verify that we got a proper answer. */
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
2000-10-14 07:23:11 +02:00
|
|
|
if (agent_failed(type)) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_AGENT_FAILURE;
|
|
|
|
goto out;
|
2000-08-23 02:46:23 +02:00
|
|
|
} else if (type != code2) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
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. */
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_get_u32(msg, &num)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (num > MAX_AGENT_IDENTITIES) {
|
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (num == 0) {
|
|
|
|
r = SSH_ERR_AGENT_NO_IDENTITIES;
|
|
|
|
goto out;
|
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* Deserialise the response into a list of keys/comments */
|
|
|
|
if ((idl = calloc(1, sizeof(*idl))) == NULL ||
|
|
|
|
(idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
|
|
|
|
(idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < num;) {
|
|
|
|
switch (version) {
|
|
|
|
case 1:
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = deserialise_identity1(msg,
|
|
|
|
&(idl->keys[i]), &(idl->comments[i]))) != 0)
|
|
|
|
goto out;
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2015-01-14 21:05:27 +01:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
if ((r = deserialise_identity2(msg,
|
|
|
|
&(idl->keys[i]), &(idl->comments[i]))) != 0) {
|
|
|
|
if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
|
|
|
|
/* Gracefully skip unknown key types */
|
|
|
|
num--;
|
|
|
|
continue;
|
|
|
|
} else
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
idl->nkeys = num;
|
|
|
|
*idlp = idl;
|
|
|
|
idl = NULL;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
if (idl != NULL)
|
|
|
|
ssh_free_identitylist(idl);
|
|
|
|
return r;
|
|
|
|
}
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
void
|
|
|
|
ssh_free_identitylist(struct ssh_identitylist *idl)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (idl == NULL)
|
|
|
|
return;
|
|
|
|
for (i = 0; i < idl->nkeys; i++) {
|
|
|
|
if (idl->keys != NULL)
|
|
|
|
sshkey_free(idl->keys[i]);
|
|
|
|
if (idl->comments != NULL)
|
|
|
|
free(idl->comments[i]);
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
free(idl);
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2015-01-14 21:05:27 +01:00
|
|
|
* Sends a challenge (typically from a server via ssh(1)) to the agent,
|
|
|
|
* and waits for a response from the agent.
|
|
|
|
* Returns true (non-zero) if the agent gave the correct answer, zero
|
|
|
|
* otherwise.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
1999-10-27 05:42:43 +02:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_decrypt_challenge(int sock, struct sshkey* key, BIGNUM *challenge,
|
|
|
|
u_char session_id[16], u_char response[16])
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
u_char type;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2000-11-13 12:57:25 +01:00
|
|
|
if (key->type != KEY_RSA1)
|
2015-01-14 21:05:27 +01:00
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, SSH_AGENTC_RSA_CHALLENGE)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(msg, challenge)) != 0 ||
|
|
|
|
(r = sshbuf_put(msg, session_id, 16)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, 1)) != 0) /* Response type for proto 1.1 */
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
2000-10-14 07:23:11 +02:00
|
|
|
if (agent_failed(type)) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_AGENT_FAILURE;
|
|
|
|
goto out;
|
2000-08-18 05:59:06 +02:00
|
|
|
} else if (type != SSH_AGENT_RSA_RESPONSE) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
1999-10-27 05:42:43 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_get(msg, response, 16)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
1999-10-27 05:42:43 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/* ask agent to sign data, returns err.h code on error, 0 on success */
|
2000-08-23 02:46:23 +02:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_agent_sign(int sock, struct sshkey *key,
|
|
|
|
u_char **sigp, size_t *lenp,
|
|
|
|
const u_char *data, size_t datalen, u_int compat)
|
2000-08-23 02:46:23 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
u_char *blob = NULL, type;
|
|
|
|
size_t blen = 0, len = 0;
|
|
|
|
u_int flags = 0;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (sigp != NULL)
|
|
|
|
*sigp = NULL;
|
|
|
|
if (lenp != NULL)
|
|
|
|
*lenp = 0;
|
|
|
|
|
|
|
|
if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if (compat & SSH_BUG_SIGBLOB)
|
|
|
|
flags |= SSH_AGENT_OLD_SIGNATURE;
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(msg, blob, blen)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(msg, data, datalen)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, flags)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg) != 0))
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
2000-10-14 07:23:11 +02:00
|
|
|
if (agent_failed(type)) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_AGENT_FAILURE;
|
|
|
|
goto out;
|
2000-08-23 02:46:23 +02:00
|
|
|
} else if (type != SSH2_AGENT_SIGN_RESPONSE) {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_get_string(msg, sigp, &len)) != 0)
|
|
|
|
goto out;
|
|
|
|
*lenp = len;
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
if (blob != NULL) {
|
|
|
|
explicit_bzero(blob, blen);
|
|
|
|
free(blob);
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
|
|
|
|
2000-07-21 02:19:44 +02:00
|
|
|
/* Encode key for a message to the agent. */
|
|
|
|
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
2015-01-14 21:05:27 +01:00
|
|
|
static int
|
|
|
|
ssh_encode_identity_rsa1(struct sshbuf *b, RSA *key, const char *comment)
|
2000-07-21 02:19:44 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
int r;
|
|
|
|
|
2000-07-21 02:19:44 +02:00
|
|
|
/* To keep within the protocol: p < q for ssh. in SSL p > q */
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_put_u32(b, BN_num_bits(key->n))) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->n)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->e)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->d)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->iqmp)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->q)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(b, key->p)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, comment)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
2000-07-21 02:19:44 +02:00
|
|
|
}
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2000-07-21 02:19:44 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
static int
|
|
|
|
ssh_encode_identity_ssh2(struct sshbuf *b, struct sshkey *key,
|
|
|
|
const char *comment)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((r = sshkey_private_serialize(key, b)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, comment)) != 0)
|
|
|
|
return r;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
encode_constraints(struct sshbuf *m, u_int life, u_int confirm)
|
2000-07-21 02:19:44 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
int r;
|
|
|
|
|
|
|
|
if (life != 0) {
|
|
|
|
if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, life)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (confirm != 0) {
|
|
|
|
if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
return r;
|
2000-07-21 02:19:44 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2015-01-14 21:05:27 +01:00
|
|
|
* Adds an identity to the authentication server.
|
|
|
|
* This call is intended only for use by ssh-add(1) and like applications.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2000-04-16 03:18:38 +02:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_add_identity_constrained(int sock, struct sshkey *key, const char *comment,
|
|
|
|
u_int life, u_int confirm)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r, constrained = (life || confirm);
|
|
|
|
u_char type;
|
1999-11-24 14:26:21 +01:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2000-07-21 02:19:44 +02:00
|
|
|
|
|
|
|
switch (key->type) {
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
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;
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0 ||
|
|
|
|
(r = ssh_encode_identity_rsa1(msg, key->rsa, comment)) != 0)
|
|
|
|
goto out;
|
2000-07-21 02:19:44 +02:00
|
|
|
break;
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
|
|
|
#ifdef WITH_OPENSSL
|
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:
|
2014-05-15 06:24:09 +02:00
|
|
|
#endif
|
2013-12-07 01:24:01 +01:00
|
|
|
case KEY_ED25519:
|
|
|
|
case KEY_ED25519_CERT:
|
2002-06-21 02:08:39 +02:00
|
|
|
type = constrained ?
|
|
|
|
SSH2_AGENTC_ADD_ID_CONSTRAINED :
|
|
|
|
SSH2_AGENTC_ADD_IDENTITY;
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0 ||
|
|
|
|
(r = ssh_encode_identity_ssh2(msg, key, comment)) != 0)
|
|
|
|
goto out;
|
2000-07-21 02:19:44 +02:00
|
|
|
break;
|
|
|
|
default:
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
2002-06-21 02:08:39 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
if (constrained &&
|
|
|
|
(r = encode_constraints(msg, life, confirm)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = decode_reply(type);
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2015-01-14 21:05:27 +01:00
|
|
|
* Removes an identity from the authentication server.
|
|
|
|
* This call is intended only for use by ssh-add(1) and like applications.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2000-04-16 03:18:38 +02:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_remove_identity(int sock, struct sshkey *key)
|
1999-10-27 05:42:43 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
u_char type, *blob = NULL;
|
|
|
|
size_t blen;
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2000-08-23 02:46:23 +02:00
|
|
|
|
2014-05-15 06:24:09 +02:00
|
|
|
#ifdef WITH_SSH1
|
2000-11-13 12:57:25 +01:00
|
|
|
if (key->type == KEY_RSA1) {
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshbuf_put_u8(msg,
|
|
|
|
SSH_AGENTC_REMOVE_RSA_IDENTITY)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, BN_num_bits(key->rsa->n))) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(msg, key->rsa->e)) != 0 ||
|
|
|
|
(r = sshbuf_put_bignum1(msg, key->rsa->n)) != 0)
|
|
|
|
goto out;
|
2014-05-15 06:24:09 +02:00
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (key->type != KEY_UNSPEC) {
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_put_u8(msg,
|
|
|
|
SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(msg, blob, blen)) != 0)
|
|
|
|
goto out;
|
2000-08-23 02:46:23 +02:00
|
|
|
} else {
|
2015-01-14 21:05:27 +01:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = decode_reply(type);
|
|
|
|
out:
|
|
|
|
if (blob != NULL) {
|
|
|
|
explicit_bzero(blob, blen);
|
|
|
|
free(blob);
|
2000-08-23 02:46:23 +02:00
|
|
|
}
|
2015-01-14 21:05:27 +01:00
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
/*
|
|
|
|
* Add/remove an token-based identity from the authentication server.
|
|
|
|
* This call is intended only for use by ssh-add(1) and like applications.
|
|
|
|
*/
|
2001-07-04 05:50:02 +02:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
|
|
|
|
u_int life, u_int confirm)
|
2001-07-04 05:50:02 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r, constrained = (life || confirm);
|
|
|
|
u_char type;
|
2003-06-11 14:06:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
2015-01-14 21:05:27 +01:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(msg, pin)) != 0)
|
|
|
|
goto out;
|
|
|
|
if (constrained &&
|
|
|
|
(r = encode_constraints(msg, life, confirm)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = decode_reply(type);
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
2001-07-04 05:50:02 +02:00
|
|
|
}
|
|
|
|
|
1999-11-25 01:54:57 +01:00
|
|
|
/*
|
2015-01-14 21:05:27 +01:00
|
|
|
* Removes all identities from the agent.
|
|
|
|
* This call is intended only for use by ssh-add(1) and like applications.
|
1999-11-25 01:54:57 +01:00
|
|
|
*/
|
2001-02-05 13:42:17 +01:00
|
|
|
int
|
2015-01-14 21:05:27 +01:00
|
|
|
ssh_remove_all_identities(int sock, int version)
|
2000-07-11 09:31:38 +02:00
|
|
|
{
|
2015-01-14 21:05:27 +01:00
|
|
|
struct sshbuf *msg;
|
|
|
|
u_char type = (version == 1) ?
|
|
|
|
SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
|
|
|
|
SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
|
|
|
r = decode_reply(type);
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 14:26:21 +01:00
|
|
|
}
|