1 /* $OpenBSD: authfd.c,v 1.123 2020/03/06 18:24:39 markus 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 <signal.h> 47 #include <string.h> 48 #include <stdarg.h> 49 #include <unistd.h> 50 #include <errno.h> 51 52 #include "xmalloc.h" 53 #include "ssh.h" 54 #include "sshbuf.h" 55 #include "sshkey.h" 56 #include "authfd.h" 57 #include "cipher.h" 58 #include "compat.h" 59 #include "log.h" 60 #include "atomicio.h" 61 #include "misc.h" 62 #include "ssherr.h" 63 64 #define MAX_AGENT_IDENTITIES 2048 /* Max keys in agent reply */ 65 #define MAX_AGENT_REPLY_LEN (256 * 1024) /* Max bytes in agent reply */ 66 67 /* macro to check for "agent failure" message */ 68 #define agent_failed(x) \ 69 ((x == SSH_AGENT_FAILURE) || \ 70 (x == SSH_COM_AGENT2_FAILURE) || \ 71 (x == SSH2_AGENT_FAILURE)) 72 73 /* Convert success/failure response from agent to a err.h status */ 74 static int 75 decode_reply(u_char type) 76 { 77 if (agent_failed(type)) 78 return SSH_ERR_AGENT_FAILURE; 79 else if (type == SSH_AGENT_SUCCESS) 80 return 0; 81 else 82 return SSH_ERR_INVALID_FORMAT; 83 } 84 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 */ 89 int 90 ssh_get_authentication_socket_path(const char *authsocket, int *fdp) 91 { 92 int sock, oerrno; 93 struct sockaddr_un sunaddr; 94 95 memset(&sunaddr, 0, sizeof(sunaddr)); 96 sunaddr.sun_family = AF_UNIX; 97 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); 98 99 if ((sock = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) 100 return SSH_ERR_SYSTEM_ERROR; 101 102 /* close on exec */ 103 if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1 || 104 connect(sock, (struct sockaddr *)&sunaddr, sizeof(sunaddr)) == -1) { 105 oerrno = errno; 106 close(sock); 107 errno = oerrno; 108 return SSH_ERR_SYSTEM_ERROR; 109 } 110 if (fdp != NULL) 111 *fdp = sock; 112 else 113 close(sock); 114 return 0; 115 } 116 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 136 /* Communicate with agent: send request and read reply */ 137 static int 138 ssh_request_reply(int sock, struct sshbuf *request, struct sshbuf *reply) 139 { 140 int r; 141 size_t l, len; 142 char buf[1024]; 143 144 /* Get the length of the message, and format it in the buffer. */ 145 len = sshbuf_len(request); 146 POKE_U32(buf, len); 147 148 /* Send the length and then the packet to the agent. */ 149 if (atomicio(vwrite, sock, buf, 4) != 4 || 150 atomicio(vwrite, sock, sshbuf_mutable_ptr(request), 151 sshbuf_len(request)) != sshbuf_len(request)) 152 return SSH_ERR_AGENT_COMMUNICATION; 153 /* 154 * Wait for response from the agent. First read the length of the 155 * response packet. 156 */ 157 if (atomicio(read, sock, buf, 4) != 4) 158 return SSH_ERR_AGENT_COMMUNICATION; 159 160 /* Extract the length, and check it for sanity. */ 161 len = PEEK_U32(buf); 162 if (len > MAX_AGENT_REPLY_LEN) 163 return SSH_ERR_INVALID_FORMAT; 164 165 /* Read the rest of the response in to the buffer. */ 166 sshbuf_reset(reply); 167 while (len > 0) { 168 l = len; 169 if (l > sizeof(buf)) 170 l = sizeof(buf); 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; 175 len -= l; 176 } 177 return 0; 178 } 179 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 */ 185 void 186 ssh_close_authentication_socket(int sock) 187 { 188 if (getenv(SSH_AUTHSOCKET_ENV_NAME)) 189 close(sock); 190 } 191 192 /* Lock/unlock agent */ 193 int 194 ssh_lock_agent(int sock, int lock, const char *password) 195 { 196 int r; 197 u_char type = lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK; 198 struct sshbuf *msg; 199 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 } 214 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; 237 } 238 239 /* 240 * Fetch list of identities held by the agent. 241 */ 242 int 243 ssh_fetch_identitylist(int sock, struct ssh_identitylist **idlp) 244 { 245 u_char type; 246 u_int32_t num, i; 247 struct sshbuf *msg; 248 struct ssh_identitylist *idl = NULL; 249 int r; 250 251 /* 252 * Send a message to the agent requesting for a list of the 253 * identities it can represent. 254 */ 255 if ((msg = sshbuf_new()) == NULL) 256 return SSH_ERR_ALLOC_FAIL; 257 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_REQUEST_IDENTITIES)) != 0) 258 goto out; 259 260 if ((r = ssh_request_reply(sock, msg, msg)) != 0) 261 goto out; 262 263 /* Get message type, and verify that we got a proper answer. */ 264 if ((r = sshbuf_get_u8(msg, &type)) != 0) 265 goto out; 266 if (agent_failed(type)) { 267 r = SSH_ERR_AGENT_FAILURE; 268 goto out; 269 } else if (type != SSH2_AGENT_IDENTITIES_ANSWER) { 270 r = SSH_ERR_INVALID_FORMAT; 271 goto out; 272 } 273 274 /* Get the number of entries in the response and check it for sanity. */ 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; 280 } 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;) { 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; 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 } 329 free(idl->keys); 330 free(idl->comments); 331 free(idl); 332 } 333 334 /* 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 345 if ((r = ssh_fetch_identitylist(sock, &idlist)) != 0) { 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 /* 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. 365 */ 366 367 368 /* encode signature algorithm in flag bits, so we can keep the msg format */ 369 static u_int 370 agent_encode_alg(const struct sshkey *key, const char *alg) 371 { 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) 375 return SSH_AGENT_RSA_SHA2_256; 376 if (strcmp(alg, "rsa-sha2-512") == 0 || 377 strcmp(alg, "rsa-sha2-512-cert-v01@openssh.com") == 0) 378 return SSH_AGENT_RSA_SHA2_512; 379 } 380 return 0; 381 } 382 383 /* ask agent to sign data, returns err.h code on error, 0 on success */ 384 int 385 ssh_agent_sign(int sock, const struct sshkey *key, 386 u_char **sigp, size_t *lenp, 387 const u_char *data, size_t datalen, const char *alg, u_int compat) 388 { 389 struct sshbuf *msg; 390 u_char *sig = NULL, type = 0; 391 size_t len = 0; 392 u_int flags = 0; 393 int r = SSH_ERR_INTERNAL_ERROR; 394 395 *sigp = NULL; 396 *lenp = 0; 397 398 if (datalen > SSH_KEY_MAX_SIGN_DATA_SIZE) 399 return SSH_ERR_INVALID_ARGUMENT; 400 if ((msg = sshbuf_new()) == NULL) 401 return SSH_ERR_ALLOC_FAIL; 402 flags |= agent_encode_alg(key, alg); 403 if ((r = sshbuf_put_u8(msg, SSH2_AGENTC_SIGN_REQUEST)) != 0 || 404 (r = sshkey_puts(key, msg)) != 0 || 405 (r = sshbuf_put_string(msg, data, datalen)) != 0 || 406 (r = sshbuf_put_u32(msg, flags)) != 0) 407 goto out; 408 if ((r = ssh_request_reply(sock, msg, msg)) != 0) 409 goto out; 410 if ((r = sshbuf_get_u8(msg, &type)) != 0) 411 goto out; 412 if (agent_failed(type)) { 413 r = SSH_ERR_AGENT_FAILURE; 414 goto out; 415 } else if (type != SSH2_AGENT_SIGN_RESPONSE) { 416 r = SSH_ERR_INVALID_FORMAT; 417 goto out; 418 } 419 if ((r = sshbuf_get_string(msg, &sig, &len)) != 0) 420 goto out; 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; 426 *lenp = len; 427 sig = NULL; 428 len = 0; 429 r = 0; 430 out: 431 freezero(sig, len); 432 sshbuf_free(msg); 433 return r; 434 } 435 436 /* Encode key for a message to the agent. */ 437 438 439 static int 440 encode_constraints(struct sshbuf *m, u_int life, u_int confirm, u_int maxsign, 441 const char *provider) 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 } 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 } 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 } 467 r = 0; 468 out: 469 return r; 470 } 471 472 /* 473 * Adds an identity to the authentication server. 474 * This call is intended only for use by ssh-add(1) and like applications. 475 */ 476 int 477 ssh_add_identity_constrained(int sock, struct sshkey *key, 478 const char *comment, u_int life, u_int confirm, u_int maxsign, 479 const char *provider) 480 { 481 struct sshbuf *msg; 482 int r, constrained = (life || confirm || maxsign || provider); 483 u_char type; 484 485 if ((msg = sshbuf_new()) == NULL) 486 return SSH_ERR_ALLOC_FAIL; 487 488 switch (key->type) { 489 #ifdef WITH_OPENSSL 490 case KEY_RSA: 491 case KEY_RSA_CERT: 492 case KEY_DSA: 493 case KEY_DSA_CERT: 494 case KEY_ECDSA: 495 case KEY_ECDSA_CERT: 496 case KEY_ECDSA_SK: 497 case KEY_ECDSA_SK_CERT: 498 #endif 499 case KEY_ED25519: 500 case KEY_ED25519_CERT: 501 case KEY_ED25519_SK: 502 case KEY_ED25519_SK_CERT: 503 case KEY_XMSS: 504 case KEY_XMSS_CERT: 505 type = constrained ? 506 SSH2_AGENTC_ADD_ID_CONSTRAINED : 507 SSH2_AGENTC_ADD_IDENTITY; 508 if ((r = sshbuf_put_u8(msg, type)) != 0 || 509 (r = sshkey_private_serialize_maxsign(key, msg, maxsign, 510 NULL)) != 0 || 511 (r = sshbuf_put_cstring(msg, comment)) != 0) 512 goto out; 513 break; 514 default: 515 r = SSH_ERR_INVALID_ARGUMENT; 516 goto out; 517 } 518 if (constrained && 519 (r = encode_constraints(msg, life, confirm, maxsign, 520 provider)) != 0) 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; 530 } 531 532 /* 533 * Removes an identity from the authentication server. 534 * This call is intended only for use by ssh-add(1) and like applications. 535 */ 536 int 537 ssh_remove_identity(int sock, struct sshkey *key) 538 { 539 struct sshbuf *msg; 540 int r; 541 u_char type, *blob = NULL; 542 size_t blen; 543 544 if ((msg = sshbuf_new()) == NULL) 545 return SSH_ERR_ALLOC_FAIL; 546 547 if (key->type != KEY_UNSPEC) { 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; 554 } else { 555 r = SSH_ERR_INVALID_ARGUMENT; 556 goto out; 557 } 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: 564 if (blob != NULL) 565 freezero(blob, blen); 566 sshbuf_free(msg); 567 return r; 568 } 569 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 */ 574 int 575 ssh_update_card(int sock, int add, const char *reader_id, const char *pin, 576 u_int life, u_int confirm) 577 { 578 struct sshbuf *msg; 579 int r, constrained = (life || confirm); 580 u_char type; 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; 588 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 && 596 (r = encode_constraints(msg, life, confirm, 0, NULL)) != 0) 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; 606 } 607 608 /* 609 * Removes all identities from the agent. 610 * This call is intended only for use by ssh-add(1) and like applications. 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. 615 */ 616 int 617 ssh_remove_all_identities(int sock, int version) 618 { 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; 624 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; 637 } 638