jcs's openbsd hax
openbsd
1/* $OpenBSD: authfd.c,v 1.138 2026/02/14 00:18:34 jsg Exp $ */
2/*
3 * Author: Tatu Ylonen <ylo@cs.hut.fi>
4 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5 * All rights reserved
6 * Functions for connecting the local authentication agent.
7 *
8 * 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".
13 *
14 * 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.
36 */
37
38
39#include <sys/types.h>
40#include <sys/un.h>
41#include <sys/socket.h>
42
43#include <fcntl.h>
44#include <stdlib.h>
45#include <string.h>
46#include <stdarg.h>
47#include <unistd.h>
48#include <errno.h>
49
50#include "ssh.h"
51#include "sshbuf.h"
52#include "sshkey.h"
53#include "authfd.h"
54#include "log.h"
55#include "atomicio.h"
56#include "ssherr.h"
57
58#define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */
59#define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */
60
61/* macro to check for "agent failure" message */
62#define agent_failed(x) \
63 ((x == SSH_AGENT_FAILURE) || \
64 (x == SSH_COM_AGENT2_FAILURE) || \
65 (x == SSH2_AGENT_FAILURE))
66
67/* Convert success/failure response from agent to a err.h status */
68static int
69decode_reply(u_char type)
70{
71 if (agent_failed(type))
72 return SSH_ERR_AGENT_FAILURE;
73 else if (type == SSH_AGENT_SUCCESS)
74 return 0;
75 else
76 return SSH_ERR_INVALID_FORMAT;
77}
78
79/*
80 * Opens an authentication socket at the provided path and stores the file
81 * descriptor in fdp. Returns 0 on success and an error on failure.
82 */
83int
84ssh_get_authentication_socket_path(const char *authsocket, int *fdp)
85{
86 int sock, oerrno;
87 struct sockaddr_un sunaddr;
88
89 debug3_f("path '%s'", authsocket);
90 memset(&sunaddr, 0, sizeof(sunaddr));
91 sunaddr.sun_family = AF_UNIX;
92 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
93
94 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
95 return SSH_ERR_SYSTEM_ERROR;
96
97 /* close on exec */
98 if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 ||
99 connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) {
100 oerrno = errno;
101 close(sock);
102 errno = oerrno;
103 return SSH_ERR_SYSTEM_ERROR;
104 }
105 if (fdp != NULL)
106 *fdp = sock;
107 else
108 close(sock);
109 return 0;
110}
111
112/*
113 * Opens the default authentication socket and stores the file descriptor in
114 * fdp. Returns 0 on success and an error on failure.
115 */
116int
117ssh_get_authentication_socket(int *fdp)
118{
119 const char *authsocket;
120
121 if (fdp != NULL)
122 *fdp = -1;
123
124 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
125 if (authsocket == NULL || *authsocket == '\0')
126 return SSH_ERR_AGENT_NOT_PRESENT;
127
128 return ssh_get_authentication_socket_path(authsocket, fdp);
129}
130
131/* Communicate with agent: send request and read reply */
132static int
133ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply)
134{
135 int r;
136 size_t l, len;
137 char buf[1024];
138
139 /* Get the length of the message, and format it in the buffer. */
140 len = sshbuf_len(request);
141 POKE_U32(buf, len);
142
143 /* Send the length and then the packet to the agent. */
144 if (atomicio(vwrite, sock, buf, 4) != 4 ||
145 atomicio(vwrite, sock, sshbuf_mutable_ptr(request),
146 sshbuf_len(request)) != sshbuf_len(request))
147 return SSH_ERR_AGENT_COMMUNICATION;
148 /*
149 * Wait for response from the agent. First read the length of the
150 * response packet.
151 */
152 if (atomicio(read, sock, buf, 4) != 4)
153 return SSH_ERR_AGENT_COMMUNICATION;
154
155 /* Extract the length, and check it for sanity. */
156 len = PEEK_U32(buf);
157 if (len > MAX_AGENT_REPLY_LEN)
158 return SSH_ERR_INVALID_FORMAT;
159
160 /* Read the rest of the response in to the buffer. */
161 sshbuf_reset(reply);
162 while (len > 0) {
163 l = len;
164 if (l > sizeof(buf))
165 l = sizeof(buf);
166 if (atomicio(read, sock, buf, l) != l)
167 return SSH_ERR_AGENT_COMMUNICATION;
168 if ((r = sshbuf_put(reply, buf, l)) != 0)
169 return r;
170 len -= l;
171 }
172 return 0;
173}
174
175/* Communicate with agent: sent request, read and decode status reply */
176static int
177ssh_request_reply_decode(int sock, struct sshbuf *request)
178{
179 struct sshbuf *reply;
180 int r;
181 u_char type;
182
183 if ((reply = sshbuf_new()) == NULL)
184 return SSH_ERR_ALLOC_FAIL;
185 if ((r = ssh_request_reply(sock, request, reply)) != 0 ||
186 (r = sshbuf_get_u8(reply, &type)) != 0 ||
187 (r = decode_reply(type)) != 0)
188 goto out;
189 /* success */
190 r = 0;
191 out:
192 sshbuf_free(reply);
193 return r;
194}
195
196/*
197 * Closes the agent socket if it should be closed (depends on how it was
198 * obtained). The argument must have been returned by
199 * ssh_get_authentication_socket().
200 */
201void
202ssh_close_authentication_socket(int sock)
203{
204 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
205 close(sock);
206}
207
208/* Lock/unlock agent */
209int
210ssh_lock_agent(int sock, int lock, const char *password)
211{
212 int r;
213 u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK;
214 struct sshbuf *msg;
215
216 if ((msg = sshbuf_new()) == NULL)
217 return SSH_ERR_ALLOC_FAIL;
218 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
219 (r = sshbuf_put_cstring(msg, password)) != 0 ||
220 (r = ssh_request_reply_decode(sock, msg)) != 0)
221 goto out;
222 /* success */
223 r = 0;
224 out:
225 sshbuf_free(msg);
226 return r;
227}
228
229
230static int
231deserialise_identity2(struct sshbuf *ids, struct sshkey **keyp, char **commentp)
232{
233 int r;
234 char *comment = NULL;
235 const u_char *blob;
236 size_t blen;
237
238 if ((r = sshbuf_get_string_direct(ids, &blob, &blen)) != 0 ||
239 (r = sshbuf_get_cstring(ids, &comment, NULL)) != 0)
240 goto out;
241 if ((r = sshkey_from_blob(blob, blen, keyp)) != 0)
242 goto out;
243 if (commentp != NULL) {
244 *commentp = comment;
245 comment = NULL;
246 }
247 r = 0;
248 out:
249 free(comment);
250 return r;
251}
252
253/*
254 * Fetch list of identities held by the agent.
255 */
256int
257ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp)
258{
259 u_char type;
260 u_int32_t num, i;
261 struct sshbuf *msg;
262 struct ssh_identitylist *idl = NULL;
263 int r;
264
265 /*
266 * Send a message to the agent requesting for a list of the
267 * identities it can represent.
268 */
269 if ((msg = sshbuf_new()) == NULL)
270 return SSH_ERR_ALLOC_FAIL;
271 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0)
272 goto out;
273
274 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
275 goto out;
276
277 /* Get message type, and verify that we got a proper answer. */
278 if ((r = sshbuf_get_u8(msg, &type)) != 0)
279 goto out;
280 if (agent_failed(type)) {
281 r = SSH_ERR_AGENT_FAILURE;
282 goto out;
283 } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) {
284 r = SSH_ERR_INVALID_FORMAT;
285 goto out;
286 }
287
288 /* Get the number of entries in the response and check it for sanity. */
289 if ((r = sshbuf_get_u32(msg, &num)) != 0)
290 goto out;
291 if (num > MAX_AGENT_IDENTITIES) {
292 r = SSH_ERR_INVALID_FORMAT;
293 goto out;
294 }
295 if (num == 0) {
296 r = SSH_ERR_AGENT_NO_IDENTITIES;
297 goto out;
298 }
299
300 /* Deserialise the response into a list of keys/comments */
301 if ((idl = calloc(1, sizeof(*idl))) == NULL ||
302 (idl->keys = calloc(num, sizeof(*idl->keys))) == NULL ||
303 (idl->comments = calloc(num, sizeof(*idl->comments))) == NULL) {
304 r = SSH_ERR_ALLOC_FAIL;
305 goto out;
306 }
307 for (i = 0; i < num;) {
308 if ((r = deserialise_identity2(msg, &(idl->keys[i]),
309 &(idl->comments[i]))) != 0) {
310 if (r == SSH_ERR_KEY_TYPE_UNKNOWN) {
311 /* Gracefully skip unknown key types */
312 num--;
313 continue;
314 } else
315 goto out;
316 }
317 i++;
318 }
319 idl->nkeys = num;
320 *idlp = idl;
321 idl = NULL;
322 r = 0;
323 out:
324 sshbuf_free(msg);
325 if (idl != NULL)
326 ssh_free_identitylist(idl);
327 return r;
328}
329
330void
331ssh_free_identitylist(struct ssh_identitylist *idl)
332{
333 size_t i;
334
335 if (idl == NULL)
336 return;
337 for (i = 0; i < idl->nkeys; i++) {
338 if (idl->keys != NULL)
339 sshkey_free(idl->keys[i]);
340 if (idl->comments != NULL)
341 free(idl->comments[i]);
342 }
343 free(idl->keys);
344 free(idl->comments);
345 free(idl);
346}
347
348/*
349 * Check if the ssh agent has a given key.
350 * Returns 0 if found, or a negative SSH_ERR_* error code on failure.
351 */
352int
353ssh_agent_has_key(int sock, const struct sshkey *key)
354{
355 int r, ret = SSH_ERR_KEY_NOT_FOUND;
356 size_t i;
357 struct ssh_identitylist *idlist = NULL;
358
359 if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) {
360 return r;
361 }
362
363 for (i = 0; i < idlist->nkeys; i++) {
364 if (sshkey_equal_public(idlist->keys[i], key)) {
365 ret = 0;
366 break;
367 }
368 }
369
370 ssh_free_identitylist(idlist);
371 return ret;
372}
373
374/*
375 * Sends a challenge (typically from a server via ssh(1)) to the agent,
376 * and waits for a response from the agent.
377 * Returns true (non-zero) if the agent gave the correct answer, zero
378 * otherwise.
379 */
380
381
382/* encode signature algorithm in flag bits, so we can keep the msg format */
383static u_int
384agent_encode_alg(const struct sshkey *key, const char *alg)
385{
386 if (alg != NULL && sshkey_type_plain(key->type) == KEY_RSA) {
387 if (strcmp(alg, "rsa-sha2-256") == 0 ||
388 strcmp(alg, "rsa-sha2-256-cert-v01@openssh.com") == 0)
389 return SSH_AGENT_RSA_SHA2_256;
390 if (strcmp(alg, "rsa-sha2-512") == 0 ||
391 strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0)
392 return SSH_AGENT_RSA_SHA2_512;
393 }
394 return 0;
395}
396
397/* ask agent to sign data, returns err.h code on error, 0 on success */
398int
399ssh_agent_sign(int sock, const struct sshkey *key,
400 u_char **sigp, size_t *lenp,
401 const u_char *data, size_t datalen, const char *alg, u_int compat)
402{
403 struct sshbuf *msg;
404 u_char *sig = NULL, type = 0;
405 size_t len = 0;
406 u_int flags = 0;
407 int r = SSH_ERR_INTERNAL_ERROR;
408
409 *sigp = NULL;
410 *lenp = 0;
411
412 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE)
413 return SSH_ERR_INVALID_ARGUMENT;
414 if ((msg = sshbuf_new()) == NULL)
415 return SSH_ERR_ALLOC_FAIL;
416 flags |= agent_encode_alg(key, alg);
417 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 ||
418 (r = sshkey_puts(key, msg)) != 0 ||
419 (r = sshbuf_put_string(msg, data, datalen)) != 0 ||
420 (r = sshbuf_put_u32(msg, flags)) != 0)
421 goto out;
422 if ((r = ssh_request_reply(sock, msg, msg)) != 0)
423 goto out;
424 if ((r = sshbuf_get_u8(msg, &type)) != 0)
425 goto out;
426 if (agent_failed(type)) {
427 r = SSH_ERR_AGENT_FAILURE;
428 goto out;
429 } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
430 r = SSH_ERR_INVALID_FORMAT;
431 goto out;
432 }
433 if ((r = sshbuf_get_string(msg, &sig, &len)) != 0)
434 goto out;
435 /*
436 * Check what we actually got back from the agent, in case it returned
437 * an incorrect RSA signature algorithm (e.g. "ssh-rsa" (RSA/SHA1) vs.
438 * "rsa-sha2-256").
439 * We don't do this for FIDO signatures as webauthn vs plain are just
440 * different signature formats and not entirely different algorithms.
441 */
442 if (!sshkey_is_sk(key) &&
443 (r = sshkey_check_sigtype(sig, len, alg)) != 0)
444 goto out;
445 /* success */
446 *sigp = sig;
447 *lenp = len;
448 sig = NULL;
449 len = 0;
450 r = 0;
451 out:
452 freezero(sig, len);
453 sshbuf_free(msg);
454 return r;
455}
456
457/* Encode key for a message to the agent. */
458
459static int
460encode_dest_constraint_hop(struct sshbuf *m,
461 const struct dest_constraint_hop *dch)
462{
463 struct sshbuf *b;
464 u_int i;
465 int r;
466
467 if ((b = sshbuf_new()) == NULL)
468 return SSH_ERR_ALLOC_FAIL;
469 if ((r = sshbuf_put_cstring(b, dch->user)) != 0 ||
470 (r = sshbuf_put_cstring(b, dch->hostname)) != 0 ||
471 (r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
472 goto out;
473 for (i = 0; i < dch->nkeys; i++) {
474 if ((r = sshkey_puts(dch->keys[i], b)) != 0 ||
475 (r = sshbuf_put_u8(b, dch->key_is_ca[i] != 0)) != 0)
476 goto out;
477 }
478 if ((r = sshbuf_put_stringb(m, b)) != 0)
479 goto out;
480 /* success */
481 r = 0;
482 out:
483 sshbuf_free(b);
484 return r;
485}
486
487static int
488encode_dest_constraint(struct sshbuf *m, const struct dest_constraint *dc)
489{
490 struct sshbuf *b;
491 int r;
492
493 if ((b = sshbuf_new()) == NULL)
494 return SSH_ERR_ALLOC_FAIL;
495 if ((r = encode_dest_constraint_hop(b, &dc->from)) != 0 ||
496 (r = encode_dest_constraint_hop(b, &dc->to)) != 0 ||
497 (r = sshbuf_put_string(b, NULL, 0)) != 0) /* reserved */
498 goto out;
499 if ((r = sshbuf_put_stringb(m, b)) != 0)
500 goto out;
501 /* success */
502 r = 0;
503 out:
504 sshbuf_free(b);
505 return r;
506}
507
508static int
509encode_constraints(struct sshbuf *m, u_int life, u_int confirm,
510 const char *provider,
511 struct dest_constraint **dest_constraints, size_t ndest_constraints,
512 int cert_only, struct sshkey **certs, size_t ncerts)
513{
514 int r;
515 struct sshbuf *b = NULL;
516 size_t i;
517
518 if (life != 0) {
519 if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_LIFETIME)) != 0 ||
520 (r = sshbuf_put_u32(m, life)) != 0)
521 goto out;
522 }
523 if (confirm != 0) {
524 if ((r = sshbuf_put_u8(m, SSH_AGENT_CONSTRAIN_CONFIRM)) != 0)
525 goto out;
526 }
527 if (provider != NULL) {
528 if ((r = sshbuf_put_u8(m,
529 SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
530 (r = sshbuf_put_cstring(m,
531 "sk-provider@openssh.com")) != 0 ||
532 (r = sshbuf_put_cstring(m, provider)) != 0)
533 goto out;
534 }
535 if (dest_constraints != NULL && ndest_constraints > 0) {
536 if ((b = sshbuf_new()) == NULL) {
537 r = SSH_ERR_ALLOC_FAIL;
538 goto out;
539 }
540 for (i = 0; i < ndest_constraints; i++) {
541 if ((r = encode_dest_constraint(b,
542 dest_constraints[i])) != 0)
543 goto out;
544 }
545 if ((r = sshbuf_put_u8(m,
546 SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
547 (r = sshbuf_put_cstring(m,
548 "restrict-destination-v00@openssh.com")) != 0 ||
549 (r = sshbuf_put_stringb(m, b)) != 0)
550 goto out;
551 sshbuf_free(b);
552 b = NULL;
553 }
554 if (ncerts != 0) {
555 if ((b = sshbuf_new()) == NULL) {
556 r = SSH_ERR_ALLOC_FAIL;
557 goto out;
558 }
559 for (i = 0; i < ncerts; i++) {
560 if ((r = sshkey_puts(certs[i], b)) != 0)
561 goto out;
562 }
563 if ((r = sshbuf_put_u8(m,
564 SSH_AGENT_CONSTRAIN_EXTENSION)) != 0 ||
565 (r = sshbuf_put_cstring(m,
566 "associated-certs-v00@openssh.com")) != 0 ||
567 (r = sshbuf_put_u8(m, cert_only != 0)) != 0 ||
568 (r = sshbuf_put_stringb(m, b)) != 0)
569 goto out;
570 sshbuf_free(b);
571 b = NULL;
572 }
573 r = 0;
574 out:
575 sshbuf_free(b);
576 return r;
577}
578
579/*
580 * Adds an identity to the authentication server.
581 * This call is intended only for use by ssh-add(1) and like applications.
582 */
583int
584ssh_add_identity_constrained(int sock, struct sshkey *key,
585 const char *comment, u_int life, u_int confirm,
586 const char *provider, struct dest_constraint **dest_constraints,
587 size_t ndest_constraints)
588{
589 struct sshbuf *msg;
590 int r, constrained = (life || confirm || provider || dest_constraints);
591 u_char type;
592
593 if ((msg = sshbuf_new()) == NULL)
594 return SSH_ERR_ALLOC_FAIL;
595
596 switch (key->type) {
597#ifdef WITH_OPENSSL
598 case KEY_RSA:
599 case KEY_RSA_CERT:
600 case KEY_ECDSA:
601 case KEY_ECDSA_CERT:
602 case KEY_ECDSA_SK:
603 case KEY_ECDSA_SK_CERT:
604#endif
605 case KEY_ED25519:
606 case KEY_ED25519_CERT:
607 case KEY_ED25519_SK:
608 case KEY_ED25519_SK_CERT:
609 type = constrained ?
610 SSH2_AGENTC_ADD_ID_CONSTRAINED :
611 SSH2_AGENTC_ADD_IDENTITY;
612 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
613 (r = sshkey_private_serialize(key, msg)) != 0 ||
614 (r = sshbuf_put_cstring(msg, comment)) != 0)
615 goto out;
616 break;
617 default:
618 r = SSH_ERR_INVALID_ARGUMENT;
619 goto out;
620 }
621 if (constrained &&
622 (r = encode_constraints(msg, life, confirm, provider,
623 dest_constraints, ndest_constraints, 0, NULL, 0)) != 0)
624 goto out;
625 if ((r = ssh_request_reply_decode(sock, msg)) != 0)
626 goto out;
627 /* success */
628 r = 0;
629 out:
630 sshbuf_free(msg);
631 return r;
632}
633
634/*
635 * Removes an identity from the authentication server.
636 * This call is intended only for use by ssh-add(1) and like applications.
637 */
638int
639ssh_remove_identity(int sock, const struct sshkey *key)
640{
641 struct sshbuf *msg;
642 int r;
643 u_char *blob = NULL;
644 size_t blen;
645
646 if ((msg = sshbuf_new()) == NULL)
647 return SSH_ERR_ALLOC_FAIL;
648
649 if (key->type != KEY_UNSPEC) {
650 if ((r = sshkey_to_blob(key, &blob, &blen)) != 0)
651 goto out;
652 if ((r = sshbuf_put_u8(msg,
653 SSH2_AGENTC_REMOVE_IDENTITY)) != 0 ||
654 (r = sshbuf_put_string(msg, blob, blen)) != 0)
655 goto out;
656 } else {
657 r = SSH_ERR_INVALID_ARGUMENT;
658 goto out;
659 }
660 if ((r = ssh_request_reply_decode(sock, msg)) != 0)
661 goto out;
662 /* success */
663 r = 0;
664 out:
665 if (blob != NULL)
666 freezero(blob, blen);
667 sshbuf_free(msg);
668 return r;
669}
670
671/*
672 * Add/remove an token-based identity from the authentication server.
673 * This call is intended only for use by ssh-add(1) and like applications.
674 */
675int
676ssh_update_card(int sock, int add, const char *reader_id, const char *pin,
677 u_int life, u_int confirm,
678 struct dest_constraint **dest_constraints, size_t ndest_constraints,
679 int cert_only, struct sshkey **certs, size_t ncerts)
680{
681 struct sshbuf *msg;
682 int r, constrained = (life || confirm || dest_constraints || certs);
683 u_char type;
684
685 if (add) {
686 type = constrained ?
687 SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
688 SSH_AGENTC_ADD_SMARTCARD_KEY;
689 } else
690 type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
691
692 if ((msg = sshbuf_new()) == NULL)
693 return SSH_ERR_ALLOC_FAIL;
694 if ((r = sshbuf_put_u8(msg, type)) != 0 ||
695 (r = sshbuf_put_cstring(msg, reader_id)) != 0 ||
696 (r = sshbuf_put_cstring(msg, pin)) != 0)
697 goto out;
698 if (constrained &&
699 (r = encode_constraints(msg, life, confirm, NULL,
700 dest_constraints, ndest_constraints,
701 cert_only, certs, ncerts)) != 0)
702 goto out;
703 if ((r = ssh_request_reply_decode(sock, msg)) != 0)
704 goto out;
705 /* success */
706 r = 0;
707 out:
708 sshbuf_free(msg);
709 return r;
710}
711
712/*
713 * Removes all identities from the agent.
714 * This call is intended only for use by ssh-add(1) and like applications.
715 *
716 * This supports the SSH protocol 1 message to because, when clearing all
717 * keys from an agent, we generally want to clear both protocol v1 and v2
718 * keys.
719 */
720int
721ssh_remove_all_identities(int sock, int version)
722{
723 struct sshbuf *msg;
724 u_char type = (version == 1) ?
725 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
726 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
727 int r;
728
729 if ((msg = sshbuf_new()) == NULL)
730 return SSH_ERR_ALLOC_FAIL;
731 if ((r = sshbuf_put_u8(msg, type)) != 0)
732 goto out;
733 if ((r = ssh_request_reply_decode(sock, msg)) != 0)
734 goto out;
735 /* success */
736 r = 0;
737 out:
738 sshbuf_free(msg);
739 return r;
740}
741
742/* Binds a session ID to a hostkey via the initial KEX signature. */
743int
744ssh_agent_bind_hostkey(int sock, const struct sshkey *key,
745 const struct sshbuf *session_id, const struct sshbuf *signature,
746 int forwarding)
747{
748 struct sshbuf *msg;
749 int r;
750
751 if (key == NULL || session_id == NULL || signature == NULL)
752 return SSH_ERR_INVALID_ARGUMENT;
753 if ((msg = sshbuf_new()) == NULL)
754 return SSH_ERR_ALLOC_FAIL;
755 if ((r = sshbuf_put_u8(msg, SSH_AGENTC_EXTENSION)) != 0 ||
756 (r = sshbuf_put_cstring(msg, "session-bind@openssh.com")) != 0 ||
757 (r = sshkey_puts(key, msg)) != 0 ||
758 (r = sshbuf_put_stringb(msg, session_id)) != 0 ||
759 (r = sshbuf_put_stringb(msg, signature)) != 0 ||
760 (r = sshbuf_put_u8(msg, forwarding ? 1 : 0)) != 0)
761 goto out;
762 if ((r = ssh_request_reply_decode(sock, msg)) != 0)
763 goto out;
764 /* success */
765 r = 0;
766 out:
767 sshbuf_free(msg);
768 return r;
769}