2022-04-27 11:08:55 +00:00
|
|
|
/* $OpenBSD: authfd.c,v 1.130 2022/04/27 11:08:55 dtucker Exp $ */
|
1999-10-27 03:42:43 +00:00
|
|
|
/*
|
1999-11-24 13:26:21 +00:00
|
|
|
* Author: Tatu Ylonen <ylo@cs.hut.fi>
|
|
|
|
* Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
|
|
|
|
* All rights reserved
|
|
|
|
* Functions for connecting the local authentication agent.
|
2000-04-16 01:18:38 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* As far as I am concerned, the code I have written for this software
|
|
|
|
* can be used freely for any purpose. Any derived versions of this
|
|
|
|
* software must be clearly marked as such, and if the derived work is
|
|
|
|
* incompatible with the protocol description in the RFC file, it must be
|
|
|
|
* called by a name other than "ssh" or "Secure Shell".
|
|
|
|
*
|
2000-08-23 00:46:23 +00:00
|
|
|
* SSH2 implementation,
|
2000-09-16 02:29:08 +00:00
|
|
|
* Copyright (c) 2000 Markus Friedl. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2000-08-23 00:46:23 +00:00
|
|
|
*
|
2000-09-16 02:29:08 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
1999-11-24 13:26:21 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
#include "includes.h"
|
2006-03-15 00:40:10 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/un.h>
|
2006-07-10 11:08:03 +00:00
|
|
|
#include <sys/socket.h>
|
2001-01-22 05:34:40 +00:00
|
|
|
|
2006-07-10 11:13:46 +00:00
|
|
|
#include <fcntl.h>
|
2006-08-05 01:34:19 +00:00
|
|
|
#include <stdlib.h>
|
2006-08-05 02:39:39 +00:00
|
|
|
#include <signal.h>
|
2006-07-24 04:13:33 +00:00
|
|
|
#include <string.h>
|
2019-11-13 04:47:52 +00:00
|
|
|
#include <stdarg.h>
|
2006-07-24 04:01:23 +00:00
|
|
|
#include <unistd.h>
|
2015-01-14 20:05:27 +00:00
|
|
|
#include <errno.h>
|
2006-07-10 11:13:46 +00:00
|
|
|
|
2006-08-05 02:39:39 +00:00
|
|
|
#include "xmalloc.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
#include "ssh.h"
|
2015-01-14 20:05:27 +00:00
|
|
|
#include "sshbuf.h"
|
|
|
|
#include "sshkey.h"
|
2000-07-21 00:19:44 +00:00
|
|
|
#include "authfd.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "cipher.h"
|
2000-09-23 06:15:56 +00:00
|
|
|
#include "compat.h"
|
2001-01-22 05:34:40 +00:00
|
|
|
#include "log.h"
|
|
|
|
#include "atomicio.h"
|
2006-03-31 12:13:02 +00:00
|
|
|
#include "misc.h"
|
2015-01-14 20:05:27 +00:00
|
|
|
#include "ssherr.h"
|
1999-10-27 03:42:43 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
#define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */
|
2020-10-29 02:52:43 +00:00
|
|
|
#define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */
|
2000-07-11 07:31:38 +00:00
|
|
|
|
2000-10-14 05:23:11 +00:00
|
|
|
/* macro to check for "agent failure" message */
|
|
|
|
#define agent_failed(x) \
|
2015-01-14 20:05:27 +00:00
|
|
|
((x == SSH_AGENT_FAILURE) || \
|
|
|
|
(x == SSH_COM_AGENT2_FAILURE) || \
|
2002-06-21 00:41:51 +00:00
|
|
|
(x == SSH2_AGENT_FAILURE))
|
2000-10-14 05:23:11 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
/* Convert success/failure response from agent to a err.h status */
|
|
|
|
static int
|
|
|
|
decode_reply(u_char type)
|
2002-09-11 23:52:46 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
if (agent_failed(type))
|
|
|
|
return SSH_ERR_AGENT_FAILURE;
|
|
|
|
else if (type == SSH_AGENT_SUCCESS)
|
2002-09-11 23:52:46 +00:00
|
|
|
return 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
else
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2002-09-11 23:52:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:19:13 +00:00
|
|
|
/*
|
|
|
|
* Opens an authentication socket at the provided path and stores the file
|
|
|
|
* descriptor in fdp. Returns 0 on success and an error on failure.
|
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
int
|
2019-12-21 02:19:13 +00:00
|
|
|
ssh_get_authentication_socket_path(const char *authsocket, int *fdp)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
int sock, oerrno;
|
1999-11-24 13:26:21 +00:00
|
|
|
struct sockaddr_un sunaddr;
|
|
|
|
|
2022-04-27 11:08:55 +00:00
|
|
|
debug3_f("path '%s'", authsocket);
|
2014-02-04 00:18:20 +00:00
|
|
|
memset(&sunaddr, 0, sizeof(sunaddr));
|
1999-11-24 13:26:21 +00:00
|
|
|
sunaddr.sun_family = AF_UNIX;
|
|
|
|
strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
|
|
|
|
|
2019-06-28 13:35:04 +00:00
|
|
|
if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
|
2015-01-14 20:05:27 +00:00
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* close on exec */
|
2015-01-14 20:05:27 +00:00
|
|
|
if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
|
2019-06-28 13:35:04 +00:00
|
|
|
connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
|
2015-01-14 20:05:27 +00:00
|
|
|
oerrno = errno;
|
1999-11-24 13:26:21 +00:00
|
|
|
close(sock);
|
2015-01-14 20:05:27 +00:00
|
|
|
errno = oerrno;
|
|
|
|
return SSH_ERR_SYSTEM_ERROR;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
2015-01-14 20:05:27 +00:00
|
|
|
if (fdp != NULL)
|
|
|
|
*fdp = sock;
|
|
|
|
else
|
1999-11-24 13:26:21 +00:00
|
|
|
close(sock);
|
2015-01-14 20:05:27 +00:00
|
|
|
return 0;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 02:19:13 +00:00
|
|
|
/*
|
|
|
|
* Opens the default authentication socket and stores the file descriptor in
|
|
|
|
* fdp. Returns 0 on success and an error on failure.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
ssh_get_authentication_socket(int *fdp)
|
|
|
|
{
|
|
|
|
const char *authsocket;
|
|
|
|
|
|
|
|
if (fdp != NULL)
|
|
|
|
*fdp = -1;
|
|
|
|
|
|
|
|
authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
|
|
|
|
if (authsocket == NULL || *authsocket == '\0')
|
|
|
|
return SSH_ERR_AGENT_NOT_PRESENT;
|
|
|
|
|
|
|
|
return ssh_get_authentication_socket_path(authsocket, fdp);
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
/* Communicate with agent: send request and read reply */
|
2001-06-25 05:01:22 +00:00
|
|
|
static int
|
2015-01-14 20:05:27 +00:00
|
|
|
ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
|
2000-08-18 03:59:06 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
int r;
|
|
|
|
size_t l, len;
|
2000-08-18 03:59:06 +00:00
|
|
|
char buf[1024];
|
|
|
|
|
|
|
|
/* Get the length of the message, and format it in the buffer. */
|
2015-01-14 20:05:27 +00:00
|
|
|
len = sshbuf_len(request);
|
2018-02-23 15:58:37 +00:00
|
|
|
POKE_U32(buf, len);
|
2000-08-18 03:59:06 +00:00
|
|
|
|
|
|
|
/* Send the length and then the packet to the agent. */
|
2015-01-14 20:05:27 +00:00
|
|
|
if (atomicio(vwrite, sock, buf, 4) != 4 ||
|
2018-07-09 21:59:10 +00:00
|
|
|
atomicio(vwrite, sock, sshbuf_mutable_ptr(request),
|
2015-01-14 20:05:27 +00:00
|
|
|
sshbuf_len(request)) != sshbuf_len(request))
|
|
|
|
return SSH_ERR_AGENT_COMMUNICATION;
|
2000-08-18 03:59:06 +00:00
|
|
|
/*
|
|
|
|
* Wait for response from the agent. First read the length of the
|
|
|
|
* response packet.
|
|
|
|
*/
|
2015-01-14 20:05:27 +00:00
|
|
|
if (atomicio(read, sock, buf, 4) != 4)
|
|
|
|
return SSH_ERR_AGENT_COMMUNICATION;
|
2000-08-18 03:59:06 +00:00
|
|
|
|
|
|
|
/* Extract the length, and check it for sanity. */
|
2018-02-23 15:58:37 +00:00
|
|
|
len = PEEK_U32(buf);
|
2015-01-14 20:05:27 +00:00
|
|
|
if (len > MAX_AGENT_REPLY_LEN)
|
|
|
|
return SSH_ERR_INVALID_FORMAT;
|
2000-08-18 03:59:06 +00:00
|
|
|
|
|
|
|
/* Read the rest of the response in to the buffer. */
|
2015-01-14 20:05:27 +00:00
|
|
|
sshbuf_reset(reply);
|
2000-08-18 03:59:06 +00:00
|
|
|
while (len > 0) {
|
|
|
|
l = len;
|
|
|
|
if (l > sizeof(buf))
|
|
|
|
l = sizeof(buf);
|
2015-01-14 20:05:27 +00: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 03:59:06 +00:00
|
|
|
len -= l;
|
|
|
|
}
|
2015-01-14 20:05:27 +00:00
|
|
|
return 0;
|
2000-08-18 03:59:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-26 00:46:17 +00:00
|
|
|
/* Communicate with agent: sent request, read and decode status reply */
|
|
|
|
static int
|
|
|
|
ssh_request_reply_decode(int sock, struct sshbuf *request)
|
|
|
|
{
|
|
|
|
struct sshbuf *reply;
|
|
|
|
int r;
|
|
|
|
u_char type;
|
|
|
|
|
|
|
|
if ((reply = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = ssh_request_reply(sock, request, reply)) != 0 ||
|
|
|
|
(r = sshbuf_get_u8(reply, &type)) != 0 ||
|
|
|
|
(r = decode_reply(type)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(reply);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Closes the agent socket if it should be closed (depends on how it was
|
|
|
|
* obtained). The argument must have been returned by
|
|
|
|
* ssh_get_authentication_socket().
|
|
|
|
*/
|
2000-04-16 01:18:38 +00:00
|
|
|
void
|
1999-11-24 13:26:21 +00:00
|
|
|
ssh_close_authentication_socket(int sock)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
1999-11-24 13:26:21 +00:00
|
|
|
if (getenv(SSH_AUTHSOCKET_ENV_NAME))
|
|
|
|
close(sock);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
/* Lock/unlock agent */
|
|
|
|
int
|
|
|
|
ssh_lock_agent(int sock, int lock, const char *password)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00: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 ||
|
2021-01-26 00:46:17 +00:00
|
|
|
(r = sshbuf_put_cstring(msg, password)) != 0 ||
|
|
|
|
(r = ssh_request_reply_decode(sock, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
static int
|
|
|
|
deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
|
2002-06-06 21:52:03 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00: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 21:52:03 +00:00
|
|
|
}
|
2015-01-14 20:05:27 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
free(comment);
|
|
|
|
return r;
|
2002-06-06 21:52:03 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2015-01-14 20:05:27 +00:00
|
|
|
* Fetch list of identities held by the agent.
|
1999-11-25 00:54:57 +00:00
|
|
|
*/
|
2000-11-13 11:57:25 +00:00
|
|
|
int
|
2017-05-05 10:42:49 +00:00
|
|
|
ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2017-05-05 10:42:49 +00:00
|
|
|
u_char type;
|
2015-01-14 20:05:27 +00:00
|
|
|
u_int32_t num, i;
|
|
|
|
struct sshbuf *msg;
|
|
|
|
struct ssh_identitylist *idl = NULL;
|
|
|
|
int r;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
|
|
|
* Send a message to the agent requesting for a list of the
|
|
|
|
* identities it can represent.
|
|
|
|
*/
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2017-05-05 10:42:49 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
|
|
|
goto out;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get message type, and verify that we got a proper answer. */
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
2000-10-14 05:23:11 +00:00
|
|
|
if (agent_failed(type)) {
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_AGENT_FAILURE;
|
|
|
|
goto out;
|
2017-05-05 10:42:49 +00:00
|
|
|
} else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
2000-08-23 00:46:23 +00:00
|
|
|
}
|
1999-11-24 13:26:21 +00:00
|
|
|
|
|
|
|
/* Get the number of entries in the response and check it for sanity. */
|
2015-01-14 20:05:27 +00: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 03:42:43 +00:00
|
|
|
|
2015-01-14 20:05:27 +00: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;) {
|
2017-05-05 10:42:49 +00:00
|
|
|
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;
|
2015-01-14 20:05:27 +00:00
|
|
|
}
|
|
|
|
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 03:42:43 +00:00
|
|
|
|
2015-01-14 20:05:27 +00: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 00:46:23 +00:00
|
|
|
}
|
2019-09-03 08:28:30 +00:00
|
|
|
free(idl->keys);
|
|
|
|
free(idl->comments);
|
2015-01-14 20:05:27 +00:00
|
|
|
free(idl);
|
1999-10-27 03:42:43 +00:00
|
|
|
}
|
|
|
|
|
2019-09-03 08:29:15 +00:00
|
|
|
/*
|
|
|
|
* Check if the ssh agent has a given key.
|
|
|
|
* Returns 0 if found, or a negative SSH_ERR_* error code on failure.
|
|
|
|
*/
|
|
|
|
int
|
2020-06-26 05:03:36 +00:00
|
|
|
ssh_agent_has_key(int sock, const struct sshkey *key)
|
2019-09-03 08:29:15 +00:00
|
|
|
{
|
|
|
|
int r, ret = SSH_ERR_KEY_NOT_FOUND;
|
|
|
|
size_t i;
|
|
|
|
struct ssh_identitylist *idlist = NULL;
|
|
|
|
|
2020-03-06 18:24:39 +00:00
|
|
|
if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) {
|
2019-09-03 08:29:15 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < idlist->nkeys; i++) {
|
|
|
|
if (sshkey_equal_public(idlist->keys[i], key)) {
|
|
|
|
ret = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ssh_free_identitylist(idlist);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2015-01-14 20:05:27 +00: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 00:54:57 +00:00
|
|
|
*/
|
1999-10-27 03:42:43 +00:00
|
|
|
|
|
|
|
|
2018-04-10 00:10:49 +00:00
|
|
|
/* encode signature algorithm in flag bits, so we can keep the msg format */
|
2015-12-04 16:41:28 +00:00
|
|
|
static u_int
|
2017-06-28 01:09:22 +00:00
|
|
|
agent_encode_alg(const struct sshkey *key, const char *alg)
|
2015-12-04 16:41:28 +00:00
|
|
|
{
|
2018-12-27 23:02:11 +00:00
|
|
|
if (alg != NULL && sshkey_type_plain(key->type) == KEY_RSA) {
|
|
|
|
if (strcmp(alg, "rsa-sha2-256") == 0 ||
|
|
|
|
strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
|
2015-12-04 16:41:28 +00:00
|
|
|
return SSH_AGENT_RSA_SHA2_256;
|
2018-12-27 23:02:11 +00:00
|
|
|
if (strcmp(alg, "rsa-sha2-512") == 0 ||
|
|
|
|
strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
|
2015-12-04 16:41:28 +00:00
|
|
|
return SSH_AGENT_RSA_SHA2_512;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
/* ask agent to sign data, returns err.h code on error, 0 on success */
|
2000-08-23 00:46:23 +00:00
|
|
|
int
|
2017-06-28 01:09:22 +00:00
|
|
|
ssh_agent_sign(int sock, const struct sshkey *key,
|
2015-01-14 20:05:27 +00:00
|
|
|
u_char **sigp, size_t *lenp,
|
2015-12-04 16:41:28 +00:00
|
|
|
const u_char *data, size_t datalen, const char *alg, u_int compat)
|
2000-08-23 00:46:23 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
struct sshbuf *msg;
|
2018-07-03 11:39:54 +00:00
|
|
|
u_char *sig = NULL, type = 0;
|
|
|
|
size_t len = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
u_int flags = 0;
|
|
|
|
int r = SSH_ERR_INTERNAL_ERROR;
|
|
|
|
|
2015-03-26 19:32:19 +00:00
|
|
|
*sigp = NULL;
|
|
|
|
*lenp = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
|
|
|
|
if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2015-12-04 16:41:28 +00:00
|
|
|
flags |= agent_encode_alg(key, alg);
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
|
2018-07-03 11:39:54 +00:00
|
|
|
(r = sshkey_puts(key, msg)) != 0 ||
|
2015-01-14 20:05:27 +00:00
|
|
|
(r = sshbuf_put_string(msg, data, datalen)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(msg, flags)) != 0)
|
|
|
|
goto out;
|
2015-09-02 07:51:12 +00:00
|
|
|
if ((r = ssh_request_reply(sock, msg, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_get_u8(msg, &type)) != 0)
|
|
|
|
goto out;
|
2000-10-14 05:23:11 +00:00
|
|
|
if (agent_failed(type)) {
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_AGENT_FAILURE;
|
|
|
|
goto out;
|
2000-08-23 00:46:23 +00:00
|
|
|
} else if (type != SSH2_AGENT_SIGN_RESPONSE) {
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_INVALID_FORMAT;
|
|
|
|
goto out;
|
|
|
|
}
|
2018-07-03 11:39:54 +00:00
|
|
|
if ((r = sshbuf_get_string(msg, &sig, &len)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* Check what we actually got back from the agent. */
|
|
|
|
if ((r = sshkey_check_sigtype(sig, len, alg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2018-07-03 11:39:54 +00:00
|
|
|
/* success */
|
|
|
|
*sigp = sig;
|
2015-03-26 19:32:19 +00:00
|
|
|
*lenp = len;
|
2018-07-03 11:39:54 +00:00
|
|
|
sig = NULL;
|
|
|
|
len = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
2018-07-03 11:39:54 +00:00
|
|
|
freezero(sig, len);
|
2015-01-14 20:05:27 +00:00
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
2000-08-23 00:46:23 +00:00
|
|
|
}
|
|
|
|
|
2000-07-21 00:19:44 +00:00
|
|
|
/* Encode key for a message to the agent. */
|
|
|
|
|
2021-12-19 22:10:24 +00:00
|
|
|
static int
|
|
|
|
encode_dest_constraint_hop(struct sshbuf *m,
|
|
|
|
const struct dest_constraint_hop *dch)
|
|
|
|
{
|
|
|
|
struct sshbuf *b;
|
|
|
|
u_int i;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_cstring(b, dch->user)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(b, dch->hostname)) != 0 ||
|
|
|
|
(r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
|
|
|
|
goto out;
|
|
|
|
for (i = 0; i < dch->nkeys; i++) {
|
|
|
|
if ((r = sshkey_puts(dch->keys[i], b)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(b, dch->key_is_ca[i] != 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_stringb(m, b)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
encode_dest_constraint(struct sshbuf *m, const struct dest_constraint *dc)
|
|
|
|
{
|
|
|
|
struct sshbuf *b;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if ((b = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = encode_dest_constraint_hop(b, &dc->from) != 0) ||
|
|
|
|
(r = encode_dest_constraint_hop(b, &dc->to) != 0) ||
|
|
|
|
(r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
|
|
|
|
goto out;
|
|
|
|
if ((r = sshbuf_put_stringb(m, b)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(b);
|
|
|
|
return r;
|
|
|
|
}
|
2000-07-21 00:19:44 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
static int
|
2019-10-31 21:19:14 +00:00
|
|
|
encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign,
|
2021-12-19 22:10:24 +00:00
|
|
|
const char *provider, struct dest_constraint **dest_constraints,
|
|
|
|
size_t ndest_constraints)
|
2000-07-21 00:19:44 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
int r;
|
2021-12-19 22:10:24 +00:00
|
|
|
struct sshbuf *b = NULL;
|
|
|
|
size_t i;
|
2015-01-14 20:05:27 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-02-23 15:58:37 +00:00
|
|
|
if (maxsign != 0) {
|
|
|
|
if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_MAXSIGN)) != 0 ||
|
|
|
|
(r = sshbuf_put_u32(m, maxsign)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2019-10-31 21:19:14 +00:00
|
|
|
if (provider != NULL) {
|
|
|
|
if ((r = sshbuf_put_u8(m,
|
|
|
|
SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(m,
|
|
|
|
"sk-provider@openssh.com")) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(m, provider)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2021-12-19 22:10:24 +00:00
|
|
|
if (dest_constraints != NULL && ndest_constraints > 0) {
|
|
|
|
if ((b = sshbuf_new()) == NULL) {
|
|
|
|
r = SSH_ERR_ALLOC_FAIL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
for (i = 0; i < ndest_constraints; i++) {
|
|
|
|
if ((r = encode_dest_constraint(b,
|
|
|
|
dest_constraints[i])) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if ((r = sshbuf_put_u8(m,
|
|
|
|
SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(m,
|
|
|
|
"restrict-destination-v00@openssh.com")) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(m, b)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
2015-01-14 20:05:27 +00:00
|
|
|
r = 0;
|
|
|
|
out:
|
2021-12-19 22:10:24 +00:00
|
|
|
sshbuf_free(b);
|
2015-01-14 20:05:27 +00:00
|
|
|
return r;
|
2000-07-21 00:19:44 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2015-01-14 20:05:27 +00: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 00:54:57 +00:00
|
|
|
*/
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
2019-06-21 04:21:04 +00:00
|
|
|
ssh_add_identity_constrained(int sock, struct sshkey *key,
|
2019-10-31 21:19:14 +00:00
|
|
|
const char *comment, u_int life, u_int confirm, u_int maxsign,
|
2021-12-19 22:10:24 +00:00
|
|
|
const char *provider, struct dest_constraint **dest_constraints,
|
|
|
|
size_t ndest_constraints)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
struct sshbuf *msg;
|
2021-12-19 22:10:24 +00:00
|
|
|
int r, constrained = (life || confirm || maxsign ||
|
|
|
|
provider || dest_constraints);
|
2015-01-14 20:05:27 +00:00
|
|
|
u_char type;
|
1999-11-24 13:26:21 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2000-07-21 00:19:44 +00:00
|
|
|
|
|
|
|
switch (key->type) {
|
2014-05-15 04:24:09 +00:00
|
|
|
#ifdef WITH_OPENSSL
|
2000-11-13 11:57:25 +00:00
|
|
|
case KEY_RSA:
|
2010-02-26 20:55:05 +00:00
|
|
|
case KEY_RSA_CERT:
|
2000-07-21 00:19:44 +00:00
|
|
|
case KEY_DSA:
|
2010-02-26 20:55:05 +00:00
|
|
|
case KEY_DSA_CERT:
|
2010-08-31 12:41:14 +00:00
|
|
|
case KEY_ECDSA:
|
|
|
|
case KEY_ECDSA_CERT:
|
2019-10-31 21:19:14 +00:00
|
|
|
case KEY_ECDSA_SK:
|
|
|
|
case KEY_ECDSA_SK_CERT:
|
2014-05-15 04:24:09 +00:00
|
|
|
#endif
|
2013-12-07 00:24:01 +00:00
|
|
|
case KEY_ED25519:
|
|
|
|
case KEY_ED25519_CERT:
|
2019-11-12 19:33:08 +00:00
|
|
|
case KEY_ED25519_SK:
|
|
|
|
case KEY_ED25519_SK_CERT:
|
2018-02-23 15:58:37 +00:00
|
|
|
case KEY_XMSS:
|
|
|
|
case KEY_XMSS_CERT:
|
2002-06-21 00:08:39 +00:00
|
|
|
type = constrained ?
|
|
|
|
SSH2_AGENTC_ADD_ID_CONSTRAINED :
|
|
|
|
SSH2_AGENTC_ADD_IDENTITY;
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((r = sshbuf_put_u8(msg, type)) != 0 ||
|
2018-02-23 15:58:37 +00:00
|
|
|
(r = sshkey_private_serialize_maxsign(key, msg, maxsign,
|
2020-10-19 22:49:23 +00:00
|
|
|
0)) != 0 ||
|
2018-02-23 15:58:37 +00:00
|
|
|
(r = sshbuf_put_cstring(msg, comment)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2000-07-21 00:19:44 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
2002-06-21 00:08:39 +00:00
|
|
|
}
|
2015-01-14 20:05:27 +00:00
|
|
|
if (constrained &&
|
2019-10-31 21:19:14 +00:00
|
|
|
(r = encode_constraints(msg, life, confirm, maxsign,
|
2021-12-19 22:10:24 +00:00
|
|
|
provider, dest_constraints, ndest_constraints)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
if ((r = ssh_request_reply_decode(sock, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2015-01-14 20:05:27 +00: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 00:54:57 +00:00
|
|
|
*/
|
2000-04-16 01:18:38 +00:00
|
|
|
int
|
2020-06-26 05:03:36 +00:00
|
|
|
ssh_remove_identity(int sock, const struct sshkey *key)
|
1999-10-27 03:42:43 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
2021-01-26 00:46:17 +00:00
|
|
|
u_char *blob = NULL;
|
2015-01-14 20:05:27 +00:00
|
|
|
size_t blen;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
2015-01-14 20:05:27 +00:00
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
2000-08-23 00:46:23 +00:00
|
|
|
|
2014-05-15 04:24:09 +00:00
|
|
|
if (key->type != KEY_UNSPEC) {
|
2015-01-14 20:05:27 +00: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 00:46:23 +00:00
|
|
|
} else {
|
2015-01-14 20:05:27 +00:00
|
|
|
r = SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
goto out;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
2021-01-26 00:46:17 +00:00
|
|
|
if ((r = ssh_request_reply_decode(sock, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
out:
|
2020-02-26 13:40:09 +00:00
|
|
|
if (blob != NULL)
|
|
|
|
freezero(blob, blen);
|
2015-01-14 20:05:27 +00:00
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
|
|
|
|
2015-01-14 20:05:27 +00: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 03:50:02 +00:00
|
|
|
int
|
2015-01-14 20:05:27 +00:00
|
|
|
ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
|
2021-12-19 22:10:24 +00:00
|
|
|
u_int life, u_int confirm,
|
|
|
|
struct dest_constraint **dest_constraints, size_t ndest_constraints)
|
2001-07-04 03:50:02 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00:00
|
|
|
struct sshbuf *msg;
|
|
|
|
int r, constrained = (life || confirm);
|
|
|
|
u_char type;
|
2003-06-11 12:06:33 +00: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 03:50:02 +00:00
|
|
|
|
2015-01-14 20:05:27 +00: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 &&
|
2021-12-19 22:10:24 +00:00
|
|
|
(r = encode_constraints(msg, life, confirm, 0, NULL,
|
|
|
|
dest_constraints, ndest_constraints)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
if ((r = ssh_request_reply_decode(sock, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
2001-07-04 03:50:02 +00:00
|
|
|
}
|
|
|
|
|
1999-11-25 00:54:57 +00:00
|
|
|
/*
|
2015-01-14 20:05:27 +00:00
|
|
|
* Removes all identities from the agent.
|
|
|
|
* This call is intended only for use by ssh-add(1) and like applications.
|
2017-05-04 06:10:57 +00:00
|
|
|
*
|
|
|
|
* This supports the SSH protocol 1 message to because, when clearing all
|
|
|
|
* keys from an agent, we generally want to clear both protocol v1 and v2
|
|
|
|
* keys.
|
1999-11-25 00:54:57 +00:00
|
|
|
*/
|
2001-02-05 12:42:17 +00:00
|
|
|
int
|
2015-01-14 20:05:27 +00:00
|
|
|
ssh_remove_all_identities(int sock, int version)
|
2000-07-11 07:31:38 +00:00
|
|
|
{
|
2015-01-14 20:05:27 +00: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;
|
2021-01-26 00:46:17 +00:00
|
|
|
if ((r = ssh_request_reply_decode(sock, msg)) != 0)
|
2015-01-14 20:05:27 +00:00
|
|
|
goto out;
|
2021-01-26 00:46:17 +00:00
|
|
|
/* success */
|
|
|
|
r = 0;
|
2015-01-14 20:05:27 +00:00
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
1999-11-24 13:26:21 +00:00
|
|
|
}
|
2021-12-19 22:08:48 +00:00
|
|
|
|
|
|
|
/* Binds a session ID to a hostkey via the initial KEX signature. */
|
|
|
|
int
|
|
|
|
ssh_agent_bind_hostkey(int sock, const struct sshkey *key,
|
|
|
|
const struct sshbuf *session_id, const struct sshbuf *signature,
|
|
|
|
int forwarding)
|
|
|
|
{
|
|
|
|
struct sshbuf *msg;
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (key == NULL || session_id == NULL || signature == NULL)
|
|
|
|
return SSH_ERR_INVALID_ARGUMENT;
|
|
|
|
if ((msg = sshbuf_new()) == NULL)
|
|
|
|
return SSH_ERR_ALLOC_FAIL;
|
|
|
|
if ((r = sshbuf_put_u8(msg, SSH_AGENTC_EXTENSION)) != 0 ||
|
|
|
|
(r = sshbuf_put_cstring(msg, "session-bind@openssh.com")) != 0 ||
|
|
|
|
(r = sshkey_puts(key, msg)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(msg, session_id)) != 0 ||
|
|
|
|
(r = sshbuf_put_stringb(msg, signature)) != 0 ||
|
|
|
|
(r = sshbuf_put_u8(msg, forwarding ? 1 : 0)) != 0)
|
|
|
|
goto out;
|
|
|
|
if ((r = ssh_request_reply_decode(sock, msg)) != 0)
|
|
|
|
goto out;
|
|
|
|
/* success */
|
|
|
|
r = 0;
|
|
|
|
out:
|
|
|
|
sshbuf_free(msg);
|
|
|
|
return r;
|
|
|
|
}
|