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