blob: 4b647a62833c51e318fd15ecd8669b656070b6d8 [file] [log] [blame]
markus@openbsd.orgbc30b442020-03-06 18:24:39 +00001/* $OpenBSD: authfd.c,v 1.123 2020/03/06 18:24:39 markus Exp $ */
Damien Millerd4a8b7e1999-10-27 13:42:43 +10002/*
Damien Miller95def091999-11-25 00:26:21 +11003 * Author: Tatu Ylonen <ylo@cs.hut.fi>
Damien Miller95def091999-11-25 00:26:21 +11004 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
Damien Miller95def091999-11-25 00:26:21 +11006 * Functions for connecting the local authentication agent.
Damien Miller4af51302000-04-16 11:18:38 +10007 *
Damien Millere4340be2000-09-16 13:29:08 +11008 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
Damien Millerad833b32000-08-23 10:46:23 +100013 *
Damien Millere4340be2000-09-16 13:29:08 +110014 * SSH2 implementation,
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
16 *
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
19 * are met:
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Damien Miller95def091999-11-25 00:26:21 +110036 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100037
38#include "includes.h"
Damien Miller574c41f2006-03-15 11:40:10 +110039
40#include <sys/types.h>
41#include <sys/un.h>
Damien Millere3b60b52006-07-10 21:08:03 +100042#include <sys/socket.h>
Ben Lindstrom226cfa02001-01-22 05:34:40 +000043
Damien Miller57cf6382006-07-10 21:13:46 +100044#include <fcntl.h>
Damien Millere7a1e5c2006-08-05 11:34:19 +100045#include <stdlib.h>
Damien Millerd7834352006-08-05 12:39:39 +100046#include <signal.h>
Damien Millere3476ed2006-07-24 14:13:33 +100047#include <string.h>
deraadt@openbsd.org72687c82019-11-13 04:47:52 +000048#include <stdarg.h>
Damien Millere6b3b612006-07-24 14:01:23 +100049#include <unistd.h>
djm@openbsd.org141efe42015-01-14 20:05:27 +000050#include <errno.h>
Damien Miller57cf6382006-07-10 21:13:46 +100051
Damien Millerd7834352006-08-05 12:39:39 +100052#include "xmalloc.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100053#include "ssh.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000054#include "sshbuf.h"
55#include "sshkey.h"
Damien Miller994cf142000-07-21 10:19:44 +100056#include "authfd.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000057#include "cipher.h"
Damien Miller62cee002000-09-23 17:15:56 +110058#include "compat.h"
Ben Lindstrom226cfa02001-01-22 05:34:40 +000059#include "log.h"
60#include "atomicio.h"
Damien Miller3f941882006-03-31 23:13:02 +110061#include "misc.h"
djm@openbsd.org141efe42015-01-14 20:05:27 +000062#include "ssherr.h"
Damien Millerd4a8b7e1999-10-27 13:42:43 +100063
djm@openbsd.org141efe42015-01-14 20:05:27 +000064#define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */
65#define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */
Damien Miller37023962000-07-11 17:31:38 +100066
Damien Miller874d77b2000-10-14 16:23:11 +110067/* macro to check for "agent failure" message */
68#define agent_failed(x) \
djm@openbsd.org141efe42015-01-14 20:05:27 +000069 ((x == SSH_AGENT_FAILURE) || \
70 (x == SSH_COM_AGENT2_FAILURE) || \
Ben Lindstromcb72e4f2002-06-21 00:41:51 +000071 (x == SSH2_AGENT_FAILURE))
Damien Miller874d77b2000-10-14 16:23:11 +110072
djm@openbsd.org141efe42015-01-14 20:05:27 +000073/* Convert success/failure response from agent to a err.h status */
74static int
75decode_reply(u_char type)
Damien Miller789e95d2002-09-12 09:52:46 +100076{
djm@openbsd.org141efe42015-01-14 20:05:27 +000077 if (agent_failed(type))
78 return SSH_ERR_AGENT_FAILURE;
79 else if (type == SSH_AGENT_SUCCESS)
Damien Miller789e95d2002-09-12 09:52:46 +100080 return 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +000081 else
82 return SSH_ERR_INVALID_FORMAT;
Damien Miller789e95d2002-09-12 09:52:46 +100083}
84
djm@openbsd.org40be78f2019-12-21 02:19:13 +000085/*
86 * Opens an authentication socket at the provided path and stores the file
87 * descriptor in fdp. Returns 0 on success and an error on failure.
88 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +100089int
djm@openbsd.org40be78f2019-12-21 02:19:13 +000090ssh_get_authentication_socket_path(const char *authsocket, int *fdp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +100091{
djm@openbsd.org141efe42015-01-14 20:05:27 +000092 int sock, oerrno;
Damien Miller95def091999-11-25 00:26:21 +110093 struct sockaddr_un sunaddr;
Damien Millerd4a8b7e1999-10-27 13:42:43 +100094
Damien Miller1d2c4562014-02-04 11:18:20 +110095 memset(&sunaddr, 0, sizeof(sunaddr));
Damien Miller95def091999-11-25 00:26:21 +110096 sunaddr.sun_family = AF_UNIX;
97 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
Damien Miller10f6f6b1999-11-17 17:29:08 +110098
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +000099 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000100 return SSH_ERR_SYSTEM_ERROR;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000101
Damien Miller95def091999-11-25 00:26:21 +1100102 /* close on exec */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000103 if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
deraadt@openbsd.org4d28fa72019-06-28 13:35:04 +0000104 connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000105 oerrno = errno;
Damien Miller95def091999-11-25 00:26:21 +1100106 close(sock);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000107 errno = oerrno;
108 return SSH_ERR_SYSTEM_ERROR;
Damien Miller95def091999-11-25 00:26:21 +1100109 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000110 if (fdp != NULL)
111 *fdp = sock;
112 else
Damien Miller95def091999-11-25 00:26:21 +1100113 close(sock);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000114 return 0;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000115}
116
djm@openbsd.org40be78f2019-12-21 02:19:13 +0000117/*
118 * Opens the default authentication socket and stores the file descriptor in
119 * fdp. Returns 0 on success and an error on failure.
120 */
121int
122ssh_get_authentication_socket(int *fdp)
123{
124 const char *authsocket;
125
126 if (fdp != NULL)
127 *fdp = -1;
128
129 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
130 if (authsocket == NULL || *authsocket == '\0')
131 return SSH_ERR_AGENT_NOT_PRESENT;
132
133 return ssh_get_authentication_socket_path(authsocket, fdp);
134}
135
djm@openbsd.org141efe42015-01-14 20:05:27 +0000136/* Communicate with agent: send request and read reply */
Ben Lindstrombba81212001-06-25 05:01:22 +0000137static int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000138ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
Damien Miller942da032000-08-18 13:59:06 +1000139{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000140 int r;
141 size_t l, len;
Damien Miller942da032000-08-18 13:59:06 +1000142 char buf[1024];
143
144 /* Get the length of the message, and format it in the buffer. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000145 len = sshbuf_len(request);
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000146 POKE_U32(buf, len);
Damien Miller942da032000-08-18 13:59:06 +1000147
148 /* Send the length and then the packet to the agent. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000149 if (atomicio(vwrite, sock, buf, 4) != 4 ||
markus@openbsd.org49f47e62018-07-09 21:59:10 +0000150 atomicio(vwrite, sock, sshbuf_mutable_ptr(request),
djm@openbsd.org141efe42015-01-14 20:05:27 +0000151 sshbuf_len(request)) != sshbuf_len(request))
152 return SSH_ERR_AGENT_COMMUNICATION;
Damien Miller942da032000-08-18 13:59:06 +1000153 /*
154 * Wait for response from the agent. First read the length of the
155 * response packet.
156 */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000157 if (atomicio(read, sock, buf, 4) != 4)
158 return SSH_ERR_AGENT_COMMUNICATION;
Damien Miller942da032000-08-18 13:59:06 +1000159
160 /* Extract the length, and check it for sanity. */
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000161 len = PEEK_U32(buf);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000162 if (len > MAX_AGENT_REPLY_LEN)
163 return SSH_ERR_INVALID_FORMAT;
Damien Miller942da032000-08-18 13:59:06 +1000164
165 /* Read the rest of the response in to the buffer. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000166 sshbuf_reset(reply);
Damien Miller942da032000-08-18 13:59:06 +1000167 while (len > 0) {
168 l = len;
169 if (l > sizeof(buf))
170 l = sizeof(buf);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000171 if (atomicio(read, sock, buf, l) != l)
172 return SSH_ERR_AGENT_COMMUNICATION;
173 if ((r = sshbuf_put(reply, buf, l)) != 0)
174 return r;
Damien Miller942da032000-08-18 13:59:06 +1000175 len -= l;
176 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000177 return 0;
Damien Miller942da032000-08-18 13:59:06 +1000178}
179
Damien Miller5428f641999-11-25 11:54:57 +1100180/*
181 * Closes the agent socket if it should be closed (depends on how it was
182 * obtained). The argument must have been returned by
183 * ssh_get_authentication_socket().
184 */
Damien Miller4af51302000-04-16 11:18:38 +1000185void
Damien Miller95def091999-11-25 00:26:21 +1100186ssh_close_authentication_socket(int sock)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000187{
Damien Miller95def091999-11-25 00:26:21 +1100188 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
189 close(sock);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000190}
191
Ben Lindstrom2f717042002-06-06 21:52:03 +0000192/* Lock/unlock agent */
193int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000194ssh_lock_agent(int sock, int lock, const char *password)
Ben Lindstrom2f717042002-06-06 21:52:03 +0000195{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000196 int r;
197 u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
198 struct sshbuf *msg;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000199
djm@openbsd.org141efe42015-01-14 20:05:27 +0000200 if ((msg = sshbuf_new()) == NULL)
201 return SSH_ERR_ALLOC_FAIL;
202 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
203 (r = sshbuf_put_cstring(msg, password)) != 0)
204 goto out;
205 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
206 goto out;
207 if ((r = sshbuf_get_u8(msg, &type)) != 0)
208 goto out;
209 r = decode_reply(type);
210 out:
211 sshbuf_free(msg);
212 return r;
213}
Ben Lindstrom2f717042002-06-06 21:52:03 +0000214
djm@openbsd.org141efe42015-01-14 20:05:27 +0000215
216static int
217deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
218{
219 int r;
220 char *comment = NULL;
221 const u_char *blob;
222 size_t blen;
223
224 if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
225 (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
226 goto out;
227 if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
228 goto out;
229 if (commentp != NULL) {
230 *commentp = comment;
231 comment = NULL;
232 }
233 r = 0;
234 out:
235 free(comment);
236 return r;
Ben Lindstrom2f717042002-06-06 21:52:03 +0000237}
238
Damien Miller5428f641999-11-25 11:54:57 +1100239/*
djm@openbsd.org141efe42015-01-14 20:05:27 +0000240 * Fetch list of identities held by the agent.
Damien Miller5428f641999-11-25 11:54:57 +1100241 */
Damien Miller0bc1bd82000-11-13 22:57:25 +1100242int
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000243ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000244{
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000245 u_char type;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000246 u_int32_t num, i;
247 struct sshbuf *msg;
248 struct ssh_identitylist *idl = NULL;
249 int r;
Damien Millerad833b32000-08-23 10:46:23 +1000250
Damien Miller5428f641999-11-25 11:54:57 +1100251 /*
252 * Send a message to the agent requesting for a list of the
253 * identities it can represent.
254 */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000255 if ((msg = sshbuf_new()) == NULL)
256 return SSH_ERR_ALLOC_FAIL;
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000257 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000258 goto out;
Damien Miller942da032000-08-18 13:59:06 +1000259
djm@openbsd.org141efe42015-01-14 20:05:27 +0000260 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
261 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000262
Damien Miller95def091999-11-25 00:26:21 +1100263 /* Get message type, and verify that we got a proper answer. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000264 if ((r = sshbuf_get_u8(msg, &type)) != 0)
265 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +1100266 if (agent_failed(type)) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000267 r = SSH_ERR_AGENT_FAILURE;
268 goto out;
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000269 } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000270 r = SSH_ERR_INVALID_FORMAT;
271 goto out;
Damien Millerad833b32000-08-23 10:46:23 +1000272 }
Damien Miller95def091999-11-25 00:26:21 +1100273
274 /* Get the number of entries in the response and check it for sanity. */
djm@openbsd.org141efe42015-01-14 20:05:27 +0000275 if ((r = sshbuf_get_u32(msg, &num)) != 0)
276 goto out;
277 if (num > MAX_AGENT_IDENTITIES) {
278 r = SSH_ERR_INVALID_FORMAT;
279 goto out;
Damien Millerad833b32000-08-23 10:46:23 +1000280 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000281 if (num == 0) {
282 r = SSH_ERR_AGENT_NO_IDENTITIES;
283 goto out;
284 }
285
286 /* Deserialise the response into a list of keys/comments */
287 if ((idl = calloc(1, sizeof(*idl))) == NULL ||
288 (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
289 (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
290 r = SSH_ERR_ALLOC_FAIL;
291 goto out;
292 }
293 for (i = 0; i < num;) {
naddy@openbsd.org3e371bd2017-05-05 10:42:49 +0000294 if ((r = deserialise_identity2(msg, &(idl->keys[i]),
295 &(idl->comments[i]))) != 0) {
296 if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
297 /* Gracefully skip unknown key types */
298 num--;
299 continue;
300 } else
301 goto out;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000302 }
303 i++;
304 }
305 idl->nkeys = num;
306 *idlp = idl;
307 idl = NULL;
308 r = 0;
309 out:
310 sshbuf_free(msg);
311 if (idl != NULL)
312 ssh_free_identitylist(idl);
313 return r;
314}
315
316void
317ssh_free_identitylist(struct ssh_identitylist *idl)
318{
319 size_t i;
320
321 if (idl == NULL)
322 return;
323 for (i = 0; i < idl->nkeys; i++) {
324 if (idl->keys != NULL)
325 sshkey_free(idl->keys[i]);
326 if (idl->comments != NULL)
327 free(idl->comments[i]);
328 }
djm@openbsd.org2ab5a842019-09-03 08:28:30 +0000329 free(idl->keys);
330 free(idl->comments);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000331 free(idl);
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000332}
333
Damien Miller5428f641999-11-25 11:54:57 +1100334/*
djm@openbsd.org06af3582019-09-03 08:29:15 +0000335 * Check if the ssh agent has a given key.
336 * Returns 0 if found, or a negative SSH_ERR_* error code on failure.
337 */
338int
339ssh_agent_has_key(int sock, struct sshkey *key)
340{
341 int r, ret = SSH_ERR_KEY_NOT_FOUND;
342 size_t i;
343 struct ssh_identitylist *idlist = NULL;
344
markus@openbsd.orgbc30b442020-03-06 18:24:39 +0000345 if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) {
djm@openbsd.org06af3582019-09-03 08:29:15 +0000346 return r;
347 }
348
349 for (i = 0; i < idlist->nkeys; i++) {
350 if (sshkey_equal_public(idlist->keys[i], key)) {
351 ret = 0;
352 break;
353 }
354 }
355
356 ssh_free_identitylist(idlist);
357 return ret;
358}
359
360/*
djm@openbsd.org141efe42015-01-14 20:05:27 +0000361 * Sends a challenge (typically from a server via ssh(1)) to the agent,
362 * and waits for a response from the agent.
363 * Returns true (non-zero) if the agent gave the correct answer, zero
364 * otherwise.
Damien Miller5428f641999-11-25 11:54:57 +1100365 */
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000366
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000367
djm@openbsd.org001aa552018-04-10 00:10:49 +0000368/* encode signature algorithm in flag bits, so we can keep the msg format */
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000369static u_int
djm@openbsd.orga98339e2017-06-28 01:09:22 +0000370agent_encode_alg(const struct sshkey *key, const char *alg)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000371{
djm@openbsd.org007a88b2018-12-27 23:02:11 +0000372 if (alg != NULL && sshkey_type_plain(key->type) == KEY_RSA) {
373 if (strcmp(alg, "rsa-sha2-256") == 0 ||
374 strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000375 return SSH_AGENT_RSA_SHA2_256;
djm@openbsd.org007a88b2018-12-27 23:02:11 +0000376 if (strcmp(alg, "rsa-sha2-512") == 0 ||
377 strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000378 return SSH_AGENT_RSA_SHA2_512;
379 }
380 return 0;
381}
382
djm@openbsd.org141efe42015-01-14 20:05:27 +0000383/* ask agent to sign data, returns err.h code on error, 0 on success */
Damien Millerad833b32000-08-23 10:46:23 +1000384int
djm@openbsd.orga98339e2017-06-28 01:09:22 +0000385ssh_agent_sign(int sock, const struct sshkey *key,
djm@openbsd.org141efe42015-01-14 20:05:27 +0000386 u_char **sigp, size_t *lenp,
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000387 const u_char *data, size_t datalen, const char *alg, u_int compat)
Damien Millerad833b32000-08-23 10:46:23 +1000388{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000389 struct sshbuf *msg;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000390 u_char *sig = NULL, type = 0;
391 size_t len = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000392 u_int flags = 0;
393 int r = SSH_ERR_INTERNAL_ERROR;
Damien Millerad833b32000-08-23 10:46:23 +1000394
markus@openbsd.orgfecede02015-03-26 19:32:19 +0000395 *sigp = NULL;
396 *lenp = 0;
Damien Millerad833b32000-08-23 10:46:23 +1000397
djm@openbsd.org141efe42015-01-14 20:05:27 +0000398 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
399 return SSH_ERR_INVALID_ARGUMENT;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000400 if ((msg = sshbuf_new()) == NULL)
401 return SSH_ERR_ALLOC_FAIL;
markus@openbsd.org76c9fbb2015-12-04 16:41:28 +0000402 flags |= agent_encode_alg(key, alg);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000403 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000404 (r = sshkey_puts(key, msg)) != 0 ||
djm@openbsd.org141efe42015-01-14 20:05:27 +0000405 (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
406 (r = sshbuf_put_u32(msg, flags)) != 0)
407 goto out;
jsg@openbsd.orgf3a3ea12015-09-02 07:51:12 +0000408 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000409 goto out;
410 if ((r = sshbuf_get_u8(msg, &type)) != 0)
411 goto out;
Damien Miller874d77b2000-10-14 16:23:11 +1100412 if (agent_failed(type)) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000413 r = SSH_ERR_AGENT_FAILURE;
414 goto out;
Damien Millerad833b32000-08-23 10:46:23 +1000415 } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000416 r = SSH_ERR_INVALID_FORMAT;
417 goto out;
Damien Millerad833b32000-08-23 10:46:23 +1000418 }
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000419 if ((r = sshbuf_get_string(msg, &sig, &len)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000420 goto out;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000421 /* Check what we actually got back from the agent. */
422 if ((r = sshkey_check_sigtype(sig, len, alg)) != 0)
423 goto out;
424 /* success */
425 *sigp = sig;
markus@openbsd.orgfecede02015-03-26 19:32:19 +0000426 *lenp = len;
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000427 sig = NULL;
428 len = 0;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000429 r = 0;
430 out:
djm@openbsd.org4ba0d542018-07-03 11:39:54 +0000431 freezero(sig, len);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000432 sshbuf_free(msg);
433 return r;
Damien Millerad833b32000-08-23 10:46:23 +1000434}
435
Damien Miller994cf142000-07-21 10:19:44 +1000436/* Encode key for a message to the agent. */
437
Damien Miller994cf142000-07-21 10:19:44 +1000438
djm@openbsd.org141efe42015-01-14 20:05:27 +0000439static int
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000440encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign,
441 const char *provider)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000442{
443 int r;
444
445 if (life != 0) {
446 if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
447 (r = sshbuf_put_u32(m, life)) != 0)
448 goto out;
449 }
450 if (confirm != 0) {
451 if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
452 goto out;
453 }
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000454 if (maxsign != 0) {
455 if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_MAXSIGN)) != 0 ||
456 (r = sshbuf_put_u32(m, maxsign)) != 0)
457 goto out;
458 }
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000459 if (provider != NULL) {
460 if ((r = sshbuf_put_u8(m,
461 SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
462 (r = sshbuf_put_cstring(m,
463 "sk-provider@openssh.com")) != 0 ||
464 (r = sshbuf_put_cstring(m, provider)) != 0)
465 goto out;
466 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000467 r = 0;
468 out:
469 return r;
Damien Miller994cf142000-07-21 10:19:44 +1000470}
471
Damien Miller5428f641999-11-25 11:54:57 +1100472/*
djm@openbsd.org141efe42015-01-14 20:05:27 +0000473 * Adds an identity to the authentication server.
474 * This call is intended only for use by ssh-add(1) and like applications.
Damien Miller5428f641999-11-25 11:54:57 +1100475 */
Damien Miller4af51302000-04-16 11:18:38 +1000476int
djm@openbsd.org4f7a56d2019-06-21 04:21:04 +0000477ssh_add_identity_constrained(int sock, struct sshkey *key,
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000478 const char *comment, u_int life, u_int confirm, u_int maxsign,
479 const char *provider)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000480{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000481 struct sshbuf *msg;
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000482 int r, constrained = (life || confirm || maxsign || provider);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000483 u_char type;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000484
djm@openbsd.org141efe42015-01-14 20:05:27 +0000485 if ((msg = sshbuf_new()) == NULL)
486 return SSH_ERR_ALLOC_FAIL;
Damien Miller994cf142000-07-21 10:19:44 +1000487
488 switch (key->type) {
Damien Miller1f0311c2014-05-15 14:24:09 +1000489#ifdef WITH_OPENSSL
Damien Miller0bc1bd82000-11-13 22:57:25 +1100490 case KEY_RSA:
Damien Miller0a80ca12010-02-27 07:55:05 +1100491 case KEY_RSA_CERT:
Damien Miller994cf142000-07-21 10:19:44 +1000492 case KEY_DSA:
Damien Miller0a80ca12010-02-27 07:55:05 +1100493 case KEY_DSA_CERT:
Damien Millereb8b60e2010-08-31 22:41:14 +1000494 case KEY_ECDSA:
495 case KEY_ECDSA_CERT:
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000496 case KEY_ECDSA_SK:
497 case KEY_ECDSA_SK_CERT:
Damien Miller1f0311c2014-05-15 14:24:09 +1000498#endif
Damien Miller5be9d9e2013-12-07 11:24:01 +1100499 case KEY_ED25519:
500 case KEY_ED25519_CERT:
markus@openbsd.org2c557442019-11-12 19:33:08 +0000501 case KEY_ED25519_SK:
502 case KEY_ED25519_SK_CERT:
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000503 case KEY_XMSS:
504 case KEY_XMSS_CERT:
Ben Lindstrom2b266b72002-06-21 00:08:39 +0000505 type = constrained ?
506 SSH2_AGENTC_ADD_ID_CONSTRAINED :
507 SSH2_AGENTC_ADD_IDENTITY;
djm@openbsd.org141efe42015-01-14 20:05:27 +0000508 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
markus@openbsd.org1b11ea72018-02-23 15:58:37 +0000509 (r = sshkey_private_serialize_maxsign(key, msg, maxsign,
510 NULL)) != 0 ||
511 (r = sshbuf_put_cstring(msg, comment)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000512 goto out;
Damien Miller994cf142000-07-21 10:19:44 +1000513 break;
514 default:
djm@openbsd.org141efe42015-01-14 20:05:27 +0000515 r = SSH_ERR_INVALID_ARGUMENT;
516 goto out;
Damien Miller994cf142000-07-21 10:19:44 +1000517 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000518 if (constrained &&
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000519 (r = encode_constraints(msg, life, confirm, maxsign,
520 provider)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000521 goto out;
522 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
523 goto out;
524 if ((r = sshbuf_get_u8(msg, &type)) != 0)
525 goto out;
526 r = decode_reply(type);
527 out:
528 sshbuf_free(msg);
529 return r;
Damien Miller95def091999-11-25 00:26:21 +1100530}
531
Damien Miller5428f641999-11-25 11:54:57 +1100532/*
djm@openbsd.org141efe42015-01-14 20:05:27 +0000533 * Removes an identity from the authentication server.
534 * This call is intended only for use by ssh-add(1) and like applications.
Damien Miller5428f641999-11-25 11:54:57 +1100535 */
Damien Miller4af51302000-04-16 11:18:38 +1000536int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000537ssh_remove_identity(int sock, struct sshkey *key)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000538{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000539 struct sshbuf *msg;
540 int r;
541 u_char type, *blob = NULL;
542 size_t blen;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000543
djm@openbsd.org141efe42015-01-14 20:05:27 +0000544 if ((msg = sshbuf_new()) == NULL)
545 return SSH_ERR_ALLOC_FAIL;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000546
Damien Miller1f0311c2014-05-15 14:24:09 +1000547 if (key->type != KEY_UNSPEC) {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000548 if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
549 goto out;
550 if ((r = sshbuf_put_u8(msg,
551 SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
552 (r = sshbuf_put_string(msg, blob, blen)) != 0)
553 goto out;
Damien Millerad833b32000-08-23 10:46:23 +1000554 } else {
djm@openbsd.org141efe42015-01-14 20:05:27 +0000555 r = SSH_ERR_INVALID_ARGUMENT;
556 goto out;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000557 }
djm@openbsd.org141efe42015-01-14 20:05:27 +0000558 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
559 goto out;
560 if ((r = sshbuf_get_u8(msg, &type)) != 0)
561 goto out;
562 r = decode_reply(type);
563 out:
jsg@openbsd.orgd5ba1c02020-02-26 13:40:09 +0000564 if (blob != NULL)
565 freezero(blob, blen);
djm@openbsd.org141efe42015-01-14 20:05:27 +0000566 sshbuf_free(msg);
567 return r;
Damien Miller95def091999-11-25 00:26:21 +1100568}
569
djm@openbsd.org141efe42015-01-14 20:05:27 +0000570/*
571 * Add/remove an token-based identity from the authentication server.
572 * This call is intended only for use by ssh-add(1) and like applications.
573 */
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000574int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000575ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
576 u_int life, u_int confirm)
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000577{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000578 struct sshbuf *msg;
579 int r, constrained = (life || confirm);
580 u_char type;
Damien Millerd94f20d2003-06-11 22:06:33 +1000581
582 if (add) {
583 type = constrained ?
584 SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
585 SSH_AGENTC_ADD_SMARTCARD_KEY;
586 } else
587 type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000588
djm@openbsd.org141efe42015-01-14 20:05:27 +0000589 if ((msg = sshbuf_new()) == NULL)
590 return SSH_ERR_ALLOC_FAIL;
591 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
592 (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
593 (r = sshbuf_put_cstring(msg, pin)) != 0)
594 goto out;
595 if (constrained &&
djm@openbsd.orgb9dd14d2019-10-31 21:19:14 +0000596 (r = encode_constraints(msg, life, confirm, 0, NULL)) != 0)
djm@openbsd.org141efe42015-01-14 20:05:27 +0000597 goto out;
598 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
599 goto out;
600 if ((r = sshbuf_get_u8(msg, &type)) != 0)
601 goto out;
602 r = decode_reply(type);
603 out:
604 sshbuf_free(msg);
605 return r;
Ben Lindstrom036a6b22001-07-04 03:50:02 +0000606}
607
Damien Miller5428f641999-11-25 11:54:57 +1100608/*
djm@openbsd.org141efe42015-01-14 20:05:27 +0000609 * Removes all identities from the agent.
610 * This call is intended only for use by ssh-add(1) and like applications.
djm@openbsd.org744bde72017-05-04 06:10:57 +0000611 *
612 * This supports the SSH protocol 1 message to because, when clearing all
613 * keys from an agent, we generally want to clear both protocol v1 and v2
614 * keys.
Damien Miller5428f641999-11-25 11:54:57 +1100615 */
Damien Miller4af51302000-04-16 11:18:38 +1000616int
djm@openbsd.org141efe42015-01-14 20:05:27 +0000617ssh_remove_all_identities(int sock, int version)
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000618{
djm@openbsd.org141efe42015-01-14 20:05:27 +0000619 struct sshbuf *msg;
620 u_char type = (version == 1) ?
621 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
622 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
623 int r;
Damien Millerd4a8b7e1999-10-27 13:42:43 +1000624
djm@openbsd.org141efe42015-01-14 20:05:27 +0000625 if ((msg = sshbuf_new()) == NULL)
626 return SSH_ERR_ALLOC_FAIL;
627 if ((r = sshbuf_put_u8(msg, type)) != 0)
628 goto out;
629 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
630 goto out;
631 if ((r = sshbuf_get_u8(msg, &type)) != 0)
632 goto out;
633 r = decode_reply(type);
634 out:
635 sshbuf_free(msg);
636 return r;
Damien Miller95def091999-11-25 00:26:21 +1100637}