markus@openbsd.org | bc30b44 | 2020-03-06 18:24:39 +0000 | [diff] [blame^] | 1 | /* $OpenBSD: authfd.c,v 1.123 2020/03/06 18:24:39 markus Exp $ */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 2 | /* |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 6 | * Functions for connecting the local authentication agent. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 7 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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". |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 13 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 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. |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 36 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 37 | |
| 38 | #include "includes.h" |
Damien Miller | 574c41f | 2006-03-15 11:40:10 +1100 | [diff] [blame] | 39 | |
| 40 | #include <sys/types.h> |
| 41 | #include <sys/un.h> |
Damien Miller | e3b60b5 | 2006-07-10 21:08:03 +1000 | [diff] [blame] | 42 | #include <sys/socket.h> |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 44 | #include <fcntl.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 45 | #include <stdlib.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 46 | #include <signal.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 47 | #include <string.h> |
deraadt@openbsd.org | 72687c8 | 2019-11-13 04:47:52 +0000 | [diff] [blame] | 48 | #include <stdarg.h> |
Damien Miller | e6b3b61 | 2006-07-24 14:01:23 +1000 | [diff] [blame] | 49 | #include <unistd.h> |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 50 | #include <errno.h> |
Damien Miller | 57cf638 | 2006-07-10 21:13:46 +1000 | [diff] [blame] | 51 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 52 | #include "xmalloc.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 53 | #include "ssh.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 54 | #include "sshbuf.h" |
| 55 | #include "sshkey.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 56 | #include "authfd.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 57 | #include "cipher.h" |
Damien Miller | 62cee00 | 2000-09-23 17:15:56 +1100 | [diff] [blame] | 58 | #include "compat.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 59 | #include "log.h" |
| 60 | #include "atomicio.h" |
Damien Miller | 3f94188 | 2006-03-31 23:13:02 +1100 | [diff] [blame] | 61 | #include "misc.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 62 | #include "ssherr.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 63 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 64 | #define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */ |
| 65 | #define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */ |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 66 | |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 67 | /* macro to check for "agent failure" message */ |
| 68 | #define agent_failed(x) \ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 69 | ((x == SSH_AGENT_FAILURE) || \ |
| 70 | (x == SSH_COM_AGENT2_FAILURE) || \ |
Ben Lindstrom | cb72e4f | 2002-06-21 00:41:51 +0000 | [diff] [blame] | 71 | (x == SSH2_AGENT_FAILURE)) |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 72 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 73 | /* Convert success/failure response from agent to a err.h status */ |
| 74 | static int |
| 75 | decode_reply(u_char type) |
Damien Miller | 789e95d | 2002-09-12 09:52:46 +1000 | [diff] [blame] | 76 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 77 | if (agent_failed(type)) |
| 78 | return SSH_ERR_AGENT_FAILURE; |
| 79 | else if (type == SSH_AGENT_SUCCESS) |
Damien Miller | 789e95d | 2002-09-12 09:52:46 +1000 | [diff] [blame] | 80 | return 0; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 81 | else |
| 82 | return SSH_ERR_INVALID_FORMAT; |
Damien Miller | 789e95d | 2002-09-12 09:52:46 +1000 | [diff] [blame] | 83 | } |
| 84 | |
djm@openbsd.org | 40be78f | 2019-12-21 02:19:13 +0000 | [diff] [blame] | 85 | /* |
| 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 89 | int |
djm@openbsd.org | 40be78f | 2019-12-21 02:19:13 +0000 | [diff] [blame] | 90 | ssh_get_authentication_socket_path(const char *authsocket, int *fdp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 91 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 92 | int sock, oerrno; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 93 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 94 | |
Damien Miller | 1d2c456 | 2014-02-04 11:18:20 +1100 | [diff] [blame] | 95 | memset(&sunaddr, 0, sizeof(sunaddr)); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 96 | sunaddr.sun_family = AF_UNIX; |
| 97 | strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 98 | |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 99 | if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 100 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 101 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 102 | /* close on exec */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 103 | if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 || |
deraadt@openbsd.org | 4d28fa7 | 2019-06-28 13:35:04 +0000 | [diff] [blame] | 104 | connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 105 | oerrno = errno; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 106 | close(sock); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 107 | errno = oerrno; |
| 108 | return SSH_ERR_SYSTEM_ERROR; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 109 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 110 | if (fdp != NULL) |
| 111 | *fdp = sock; |
| 112 | else |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 113 | close(sock); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 114 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 115 | } |
| 116 | |
djm@openbsd.org | 40be78f | 2019-12-21 02:19:13 +0000 | [diff] [blame] | 117 | /* |
| 118 | * Opens the default authentication socket and stores the file descriptor in |
| 119 | * fdp. Returns 0 on success and an error on failure. |
| 120 | */ |
| 121 | int |
| 122 | ssh_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.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 136 | /* Communicate with agent: send request and read reply */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 137 | static int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 138 | ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 139 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 140 | int r; |
| 141 | size_t l, len; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 142 | char buf[1024]; |
| 143 | |
| 144 | /* Get the length of the message, and format it in the buffer. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 145 | len = sshbuf_len(request); |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 146 | POKE_U32(buf, len); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 147 | |
| 148 | /* Send the length and then the packet to the agent. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 149 | if (atomicio(vwrite, sock, buf, 4) != 4 || |
markus@openbsd.org | 49f47e6 | 2018-07-09 21:59:10 +0000 | [diff] [blame] | 150 | atomicio(vwrite, sock, sshbuf_mutable_ptr(request), |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 151 | sshbuf_len(request)) != sshbuf_len(request)) |
| 152 | return SSH_ERR_AGENT_COMMUNICATION; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 153 | /* |
| 154 | * Wait for response from the agent. First read the length of the |
| 155 | * response packet. |
| 156 | */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 157 | if (atomicio(read, sock, buf, 4) != 4) |
| 158 | return SSH_ERR_AGENT_COMMUNICATION; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 159 | |
| 160 | /* Extract the length, and check it for sanity. */ |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 161 | len = PEEK_U32(buf); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 162 | if (len > MAX_AGENT_REPLY_LEN) |
| 163 | return SSH_ERR_INVALID_FORMAT; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 164 | |
| 165 | /* Read the rest of the response in to the buffer. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 166 | sshbuf_reset(reply); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 167 | while (len > 0) { |
| 168 | l = len; |
| 169 | if (l > sizeof(buf)) |
| 170 | l = sizeof(buf); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 171 | 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 Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 175 | len -= l; |
| 176 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 177 | return 0; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 178 | } |
| 179 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 180 | /* |
| 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 Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 185 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 186 | ssh_close_authentication_socket(int sock) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 187 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 188 | if (getenv(SSH_AUTHSOCKET_ENV_NAME)) |
| 189 | close(sock); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 190 | } |
| 191 | |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 192 | /* Lock/unlock agent */ |
| 193 | int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 194 | ssh_lock_agent(int sock, int lock, const char *password) |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 195 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 196 | int r; |
| 197 | u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK; |
| 198 | struct sshbuf *msg; |
Ben Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 199 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 200 | 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 Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 214 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 215 | |
| 216 | static int |
| 217 | deserialise_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 Lindstrom | 2f71704 | 2002-06-06 21:52:03 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 239 | /* |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 240 | * Fetch list of identities held by the agent. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 241 | */ |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 242 | int |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 243 | ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 244 | { |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 245 | u_char type; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 246 | u_int32_t num, i; |
| 247 | struct sshbuf *msg; |
| 248 | struct ssh_identitylist *idl = NULL; |
| 249 | int r; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 250 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 251 | /* |
| 252 | * Send a message to the agent requesting for a list of the |
| 253 | * identities it can represent. |
| 254 | */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 255 | if ((msg = sshbuf_new()) == NULL) |
| 256 | return SSH_ERR_ALLOC_FAIL; |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 257 | if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 258 | goto out; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 259 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 260 | if ((r = ssh_request_reply(sock, msg, msg)) != 0) |
| 261 | goto out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 262 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 263 | /* Get message type, and verify that we got a proper answer. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 264 | if ((r = sshbuf_get_u8(msg, &type)) != 0) |
| 265 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 266 | if (agent_failed(type)) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 267 | r = SSH_ERR_AGENT_FAILURE; |
| 268 | goto out; |
naddy@openbsd.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 269 | } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 270 | r = SSH_ERR_INVALID_FORMAT; |
| 271 | goto out; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 272 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 273 | |
| 274 | /* Get the number of entries in the response and check it for sanity. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 275 | 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 Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 280 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 281 | 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.org | 3e371bd | 2017-05-05 10:42:49 +0000 | [diff] [blame] | 294 | 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.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 302 | } |
| 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 | |
| 316 | void |
| 317 | ssh_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.org | 2ab5a84 | 2019-09-03 08:28:30 +0000 | [diff] [blame] | 329 | free(idl->keys); |
| 330 | free(idl->comments); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 331 | free(idl); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 332 | } |
| 333 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 334 | /* |
djm@openbsd.org | 06af358 | 2019-09-03 08:29:15 +0000 | [diff] [blame] | 335 | * Check if the ssh agent has a given key. |
| 336 | * Returns 0 if found, or a negative SSH_ERR_* error code on failure. |
| 337 | */ |
| 338 | int |
| 339 | ssh_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.org | bc30b44 | 2020-03-06 18:24:39 +0000 | [diff] [blame^] | 345 | if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) { |
djm@openbsd.org | 06af358 | 2019-09-03 08:29:15 +0000 | [diff] [blame] | 346 | 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.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 361 | * 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 Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 365 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 366 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 367 | |
djm@openbsd.org | 001aa55 | 2018-04-10 00:10:49 +0000 | [diff] [blame] | 368 | /* encode signature algorithm in flag bits, so we can keep the msg format */ |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 369 | static u_int |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 370 | agent_encode_alg(const struct sshkey *key, const char *alg) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 371 | { |
djm@openbsd.org | 007a88b | 2018-12-27 23:02:11 +0000 | [diff] [blame] | 372 | 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.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 375 | return SSH_AGENT_RSA_SHA2_256; |
djm@openbsd.org | 007a88b | 2018-12-27 23:02:11 +0000 | [diff] [blame] | 376 | if (strcmp(alg, "rsa-sha2-512") == 0 || |
| 377 | strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0) |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 378 | return SSH_AGENT_RSA_SHA2_512; |
| 379 | } |
| 380 | return 0; |
| 381 | } |
| 382 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 383 | /* ask agent to sign data, returns err.h code on error, 0 on success */ |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 384 | int |
djm@openbsd.org | a98339e | 2017-06-28 01:09:22 +0000 | [diff] [blame] | 385 | ssh_agent_sign(int sock, const struct sshkey *key, |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 386 | u_char **sigp, size_t *lenp, |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 387 | const u_char *data, size_t datalen, const char *alg, u_int compat) |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 388 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 389 | struct sshbuf *msg; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 390 | u_char *sig = NULL, type = 0; |
| 391 | size_t len = 0; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 392 | u_int flags = 0; |
| 393 | int r = SSH_ERR_INTERNAL_ERROR; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 394 | |
markus@openbsd.org | fecede0 | 2015-03-26 19:32:19 +0000 | [diff] [blame] | 395 | *sigp = NULL; |
| 396 | *lenp = 0; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 397 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 398 | if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE) |
| 399 | return SSH_ERR_INVALID_ARGUMENT; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 400 | if ((msg = sshbuf_new()) == NULL) |
| 401 | return SSH_ERR_ALLOC_FAIL; |
markus@openbsd.org | 76c9fbb | 2015-12-04 16:41:28 +0000 | [diff] [blame] | 402 | flags |= agent_encode_alg(key, alg); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 403 | if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 404 | (r = sshkey_puts(key, msg)) != 0 || |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 405 | (r = sshbuf_put_string(msg, data, datalen)) != 0 || |
| 406 | (r = sshbuf_put_u32(msg, flags)) != 0) |
| 407 | goto out; |
jsg@openbsd.org | f3a3ea1 | 2015-09-02 07:51:12 +0000 | [diff] [blame] | 408 | if ((r = ssh_request_reply(sock, msg, msg)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 409 | goto out; |
| 410 | if ((r = sshbuf_get_u8(msg, &type)) != 0) |
| 411 | goto out; |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 412 | if (agent_failed(type)) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 413 | r = SSH_ERR_AGENT_FAILURE; |
| 414 | goto out; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 415 | } else if (type != SSH2_AGENT_SIGN_RESPONSE) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 416 | r = SSH_ERR_INVALID_FORMAT; |
| 417 | goto out; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 418 | } |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 419 | if ((r = sshbuf_get_string(msg, &sig, &len)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 420 | goto out; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 421 | /* 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.org | fecede0 | 2015-03-26 19:32:19 +0000 | [diff] [blame] | 426 | *lenp = len; |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 427 | sig = NULL; |
| 428 | len = 0; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 429 | r = 0; |
| 430 | out: |
djm@openbsd.org | 4ba0d54 | 2018-07-03 11:39:54 +0000 | [diff] [blame] | 431 | freezero(sig, len); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 432 | sshbuf_free(msg); |
| 433 | return r; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 434 | } |
| 435 | |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 436 | /* Encode key for a message to the agent. */ |
| 437 | |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 438 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 439 | static int |
djm@openbsd.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 440 | encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign, |
| 441 | const char *provider) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 442 | { |
| 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.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 454 | 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.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 459 | 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.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 467 | r = 0; |
| 468 | out: |
| 469 | return r; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 470 | } |
| 471 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 472 | /* |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 473 | * Adds an identity to the authentication server. |
| 474 | * This call is intended only for use by ssh-add(1) and like applications. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 475 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 476 | int |
djm@openbsd.org | 4f7a56d | 2019-06-21 04:21:04 +0000 | [diff] [blame] | 477 | ssh_add_identity_constrained(int sock, struct sshkey *key, |
djm@openbsd.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 478 | const char *comment, u_int life, u_int confirm, u_int maxsign, |
| 479 | const char *provider) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 480 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 481 | struct sshbuf *msg; |
djm@openbsd.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 482 | int r, constrained = (life || confirm || maxsign || provider); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 483 | u_char type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 484 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 485 | if ((msg = sshbuf_new()) == NULL) |
| 486 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 487 | |
| 488 | switch (key->type) { |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 489 | #ifdef WITH_OPENSSL |
Damien Miller | 0bc1bd8 | 2000-11-13 22:57:25 +1100 | [diff] [blame] | 490 | case KEY_RSA: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 491 | case KEY_RSA_CERT: |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 492 | case KEY_DSA: |
Damien Miller | 0a80ca1 | 2010-02-27 07:55:05 +1100 | [diff] [blame] | 493 | case KEY_DSA_CERT: |
Damien Miller | eb8b60e | 2010-08-31 22:41:14 +1000 | [diff] [blame] | 494 | case KEY_ECDSA: |
| 495 | case KEY_ECDSA_CERT: |
djm@openbsd.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 496 | case KEY_ECDSA_SK: |
| 497 | case KEY_ECDSA_SK_CERT: |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 498 | #endif |
Damien Miller | 5be9d9e | 2013-12-07 11:24:01 +1100 | [diff] [blame] | 499 | case KEY_ED25519: |
| 500 | case KEY_ED25519_CERT: |
markus@openbsd.org | 2c55744 | 2019-11-12 19:33:08 +0000 | [diff] [blame] | 501 | case KEY_ED25519_SK: |
| 502 | case KEY_ED25519_SK_CERT: |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 503 | case KEY_XMSS: |
| 504 | case KEY_XMSS_CERT: |
Ben Lindstrom | 2b266b7 | 2002-06-21 00:08:39 +0000 | [diff] [blame] | 505 | type = constrained ? |
| 506 | SSH2_AGENTC_ADD_ID_CONSTRAINED : |
| 507 | SSH2_AGENTC_ADD_IDENTITY; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 508 | if ((r = sshbuf_put_u8(msg, type)) != 0 || |
markus@openbsd.org | 1b11ea7 | 2018-02-23 15:58:37 +0000 | [diff] [blame] | 509 | (r = sshkey_private_serialize_maxsign(key, msg, maxsign, |
| 510 | NULL)) != 0 || |
| 511 | (r = sshbuf_put_cstring(msg, comment)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 512 | goto out; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 513 | break; |
| 514 | default: |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 515 | r = SSH_ERR_INVALID_ARGUMENT; |
| 516 | goto out; |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 517 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 518 | if (constrained && |
djm@openbsd.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 519 | (r = encode_constraints(msg, life, confirm, maxsign, |
| 520 | provider)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 521 | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 530 | } |
| 531 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 532 | /* |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 533 | * Removes an identity from the authentication server. |
| 534 | * This call is intended only for use by ssh-add(1) and like applications. |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 535 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 536 | int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 537 | ssh_remove_identity(int sock, struct sshkey *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 538 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 539 | struct sshbuf *msg; |
| 540 | int r; |
| 541 | u_char type, *blob = NULL; |
| 542 | size_t blen; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 543 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 544 | if ((msg = sshbuf_new()) == NULL) |
| 545 | return SSH_ERR_ALLOC_FAIL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 546 | |
Damien Miller | 1f0311c | 2014-05-15 14:24:09 +1000 | [diff] [blame] | 547 | if (key->type != KEY_UNSPEC) { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 548 | 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 Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 554 | } else { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 555 | r = SSH_ERR_INVALID_ARGUMENT; |
| 556 | goto out; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 557 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 558 | 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.org | d5ba1c0 | 2020-02-26 13:40:09 +0000 | [diff] [blame] | 564 | if (blob != NULL) |
| 565 | freezero(blob, blen); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 566 | sshbuf_free(msg); |
| 567 | return r; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 568 | } |
| 569 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 570 | /* |
| 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 Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 574 | int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 575 | ssh_update_card(int sock, int add, const char *reader_id, const char *pin, |
| 576 | u_int life, u_int confirm) |
Ben Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 577 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 578 | struct sshbuf *msg; |
| 579 | int r, constrained = (life || confirm); |
| 580 | u_char type; |
Damien Miller | d94f20d | 2003-06-11 22:06:33 +1000 | [diff] [blame] | 581 | |
| 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 Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 588 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 589 | 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.org | b9dd14d | 2019-10-31 21:19:14 +0000 | [diff] [blame] | 596 | (r = encode_constraints(msg, life, confirm, 0, NULL)) != 0) |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 597 | 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 Lindstrom | 036a6b2 | 2001-07-04 03:50:02 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 608 | /* |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 609 | * Removes all identities from the agent. |
| 610 | * This call is intended only for use by ssh-add(1) and like applications. |
djm@openbsd.org | 744bde7 | 2017-05-04 06:10:57 +0000 | [diff] [blame] | 611 | * |
| 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 Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 615 | */ |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 616 | int |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 617 | ssh_remove_all_identities(int sock, int version) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 618 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 619 | 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 Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 624 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 625 | 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 Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 637 | } |