1 /* 2 * Author: Tatu Ylonen <ylo@cs.hut.fi> 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 4 * All rights reserved 5 * Functions for connecting the local authentication agent. 6 * 7 * As far as I am concerned, the code I have written for this software 8 * can be used freely for any purpose. Any derived versions of this 9 * software must be clearly marked as such, and if the derived work is 10 * incompatible with the protocol description in the RFC file, it must be 11 * called by a name other than "ssh" or "Secure Shell". 12 * 13 * SSH2 implementation, 14 * Copyright (c) 2000 Markus Friedl. All rights reserved. 15 * 16 * Redistribution and use in source and binary forms, with or without 17 * modification, are permitted provided that the following conditions 18 * are met: 19 * 1. Redistributions of source code must retain the above copyright 20 * notice, this list of conditions and the following disclaimer. 21 * 2. Redistributions in binary form must reproduce the above copyright 22 * notice, this list of conditions and the following disclaimer in the 23 * documentation and/or other materials provided with the distribution. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 27 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 28 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 29 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 34 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 35 */ 36 37 #include "includes.h" 38 RCSID("$OpenBSD: authfd.c,v 1.56 2002/06/25 16:22:42 markus Exp $"); 39 RCSID("$FreeBSD$"); 40 41 #include <openssl/evp.h> 42 43 #include "ssh.h" 44 #include "rsa.h" 45 #include "buffer.h" 46 #include "bufaux.h" 47 #include "xmalloc.h" 48 #include "getput.h" 49 #include "key.h" 50 #include "authfd.h" 51 #include "cipher.h" 52 #include "kex.h" 53 #include "compat.h" 54 #include "log.h" 55 #include "atomicio.h" 56 57 /* helper */ 58 int decode_reply(int type); 59 60 /* macro to check for "agent failure" message */ 61 #define agent_failed(x) \ 62 ((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \ 63 (x == SSH2_AGENT_FAILURE)) 64 65 /* Returns the number of the authentication fd, or -1 if there is none. */ 66 67 int 68 ssh_get_authentication_socket(void) 69 { 70 const char *authsocket; 71 int sock; 72 struct sockaddr_un sunaddr; 73 74 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME); 75 if (!authsocket) 76 return -1; 77 78 sunaddr.sun_family = AF_UNIX; 79 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); 80 81 sock = socket(AF_UNIX, SOCK_STREAM, 0); 82 if (sock < 0) 83 return -1; 84 85 /* close on exec */ 86 if (fcntl(sock, F_SETFD, 1) == -1) { 87 close(sock); 88 return -1; 89 } 90 if (connect(sock, (struct sockaddr *) &sunaddr, sizeof sunaddr) < 0) { 91 close(sock); 92 return -1; 93 } 94 return sock; 95 } 96 97 static int 98 ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply) 99 { 100 int l, len; 101 char buf[1024]; 102 103 /* Get the length of the message, and format it in the buffer. */ 104 len = buffer_len(request); 105 PUT_32BIT(buf, len); 106 107 /* Send the length and then the packet to the agent. */ 108 if (atomicio(write, auth->fd, buf, 4) != 4 || 109 atomicio(write, auth->fd, buffer_ptr(request), 110 buffer_len(request)) != buffer_len(request)) { 111 error("Error writing to authentication socket."); 112 return 0; 113 } 114 /* 115 * Wait for response from the agent. First read the length of the 116 * response packet. 117 */ 118 len = 4; 119 while (len > 0) { 120 l = read(auth->fd, buf + 4 - len, len); 121 if (l == -1 && (errno == EAGAIN || errno == EINTR)) 122 continue; 123 if (l <= 0) { 124 error("Error reading response length from authentication socket."); 125 return 0; 126 } 127 len -= l; 128 } 129 130 /* Extract the length, and check it for sanity. */ 131 len = GET_32BIT(buf); 132 if (len > 256 * 1024) 133 fatal("Authentication response too long: %d", len); 134 135 /* Read the rest of the response in to the buffer. */ 136 buffer_clear(reply); 137 while (len > 0) { 138 l = len; 139 if (l > sizeof(buf)) 140 l = sizeof(buf); 141 l = read(auth->fd, buf, l); 142 if (l == -1 && (errno == EAGAIN || errno == EINTR)) 143 continue; 144 if (l <= 0) { 145 error("Error reading response from authentication socket."); 146 return 0; 147 } 148 buffer_append(reply, buf, l); 149 len -= l; 150 } 151 return 1; 152 } 153 154 /* 155 * Closes the agent socket if it should be closed (depends on how it was 156 * obtained). The argument must have been returned by 157 * ssh_get_authentication_socket(). 158 */ 159 160 void 161 ssh_close_authentication_socket(int sock) 162 { 163 if (getenv(SSH_AUTHSOCKET_ENV_NAME)) 164 close(sock); 165 } 166 167 /* 168 * Opens and connects a private socket for communication with the 169 * authentication agent. Returns the file descriptor (which must be 170 * shut down and closed by the caller when no longer needed). 171 * Returns NULL if an error occurred and the connection could not be 172 * opened. 173 */ 174 175 AuthenticationConnection * 176 ssh_get_authentication_connection(void) 177 { 178 AuthenticationConnection *auth; 179 int sock; 180 181 sock = ssh_get_authentication_socket(); 182 183 /* 184 * Fail if we couldn't obtain a connection. This happens if we 185 * exited due to a timeout. 186 */ 187 if (sock < 0) 188 return NULL; 189 190 auth = xmalloc(sizeof(*auth)); 191 auth->fd = sock; 192 buffer_init(&auth->identities); 193 auth->howmany = 0; 194 195 return auth; 196 } 197 198 /* 199 * Closes the connection to the authentication agent and frees any associated 200 * memory. 201 */ 202 203 void 204 ssh_close_authentication_connection(AuthenticationConnection *auth) 205 { 206 buffer_free(&auth->identities); 207 close(auth->fd); 208 xfree(auth); 209 } 210 211 /* Lock/unlock agent */ 212 int 213 ssh_lock_agent(AuthenticationConnection *auth, int lock, const char *password) 214 { 215 int type; 216 Buffer msg; 217 218 buffer_init(&msg); 219 buffer_put_char(&msg, lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK); 220 buffer_put_cstring(&msg, password); 221 222 if (ssh_request_reply(auth, &msg, &msg) == 0) { 223 buffer_free(&msg); 224 return 0; 225 } 226 type = buffer_get_char(&msg); 227 buffer_free(&msg); 228 return decode_reply(type); 229 } 230 231 /* 232 * Returns the first authentication identity held by the agent. 233 */ 234 235 int 236 ssh_get_num_identities(AuthenticationConnection *auth, int version) 237 { 238 int type, code1 = 0, code2 = 0; 239 Buffer request; 240 241 switch (version) { 242 case 1: 243 code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES; 244 code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER; 245 break; 246 case 2: 247 code1 = SSH2_AGENTC_REQUEST_IDENTITIES; 248 code2 = SSH2_AGENT_IDENTITIES_ANSWER; 249 break; 250 default: 251 return 0; 252 } 253 254 /* 255 * Send a message to the agent requesting for a list of the 256 * identities it can represent. 257 */ 258 buffer_init(&request); 259 buffer_put_char(&request, code1); 260 261 buffer_clear(&auth->identities); 262 if (ssh_request_reply(auth, &request, &auth->identities) == 0) { 263 buffer_free(&request); 264 return 0; 265 } 266 buffer_free(&request); 267 268 /* Get message type, and verify that we got a proper answer. */ 269 type = buffer_get_char(&auth->identities); 270 if (agent_failed(type)) { 271 return 0; 272 } else if (type != code2) { 273 fatal("Bad authentication reply message type: %d", type); 274 } 275 276 /* Get the number of entries in the response and check it for sanity. */ 277 auth->howmany = buffer_get_int(&auth->identities); 278 if (auth->howmany > 1024) 279 fatal("Too many identities in authentication reply: %d", 280 auth->howmany); 281 282 return auth->howmany; 283 } 284 285 Key * 286 ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version) 287 { 288 /* get number of identities and return the first entry (if any). */ 289 if (ssh_get_num_identities(auth, version) > 0) 290 return ssh_get_next_identity(auth, comment, version); 291 return NULL; 292 } 293 294 Key * 295 ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version) 296 { 297 u_int bits; 298 u_char *blob; 299 u_int blen; 300 Key *key = NULL; 301 302 /* Return failure if no more entries. */ 303 if (auth->howmany <= 0) 304 return NULL; 305 306 /* 307 * Get the next entry from the packet. These will abort with a fatal 308 * error if the packet is too short or contains corrupt data. 309 */ 310 switch (version) { 311 case 1: 312 key = key_new(KEY_RSA1); 313 bits = buffer_get_int(&auth->identities); 314 buffer_get_bignum(&auth->identities, key->rsa->e); 315 buffer_get_bignum(&auth->identities, key->rsa->n); 316 *comment = buffer_get_string(&auth->identities, NULL); 317 if (bits != BN_num_bits(key->rsa->n)) 318 log("Warning: identity keysize mismatch: actual %d, announced %u", 319 BN_num_bits(key->rsa->n), bits); 320 break; 321 case 2: 322 blob = buffer_get_string(&auth->identities, &blen); 323 *comment = buffer_get_string(&auth->identities, NULL); 324 key = key_from_blob(blob, blen); 325 xfree(blob); 326 break; 327 default: 328 return NULL; 329 break; 330 } 331 /* Decrement the number of remaining entries. */ 332 auth->howmany--; 333 return key; 334 } 335 336 /* 337 * Generates a random challenge, sends it to the agent, and waits for 338 * response from the agent. Returns true (non-zero) if the agent gave the 339 * correct answer, zero otherwise. Response type selects the style of 340 * response desired, with 0 corresponding to protocol version 1.0 (no longer 341 * supported) and 1 corresponding to protocol version 1.1. 342 */ 343 344 int 345 ssh_decrypt_challenge(AuthenticationConnection *auth, 346 Key* key, BIGNUM *challenge, 347 u_char session_id[16], 348 u_int response_type, 349 u_char response[16]) 350 { 351 Buffer buffer; 352 int success = 0; 353 int i; 354 int type; 355 356 if (key->type != KEY_RSA1) 357 return 0; 358 if (response_type == 0) { 359 log("Compatibility with ssh protocol version 1.0 no longer supported."); 360 return 0; 361 } 362 buffer_init(&buffer); 363 buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE); 364 buffer_put_int(&buffer, BN_num_bits(key->rsa->n)); 365 buffer_put_bignum(&buffer, key->rsa->e); 366 buffer_put_bignum(&buffer, key->rsa->n); 367 buffer_put_bignum(&buffer, challenge); 368 buffer_append(&buffer, session_id, 16); 369 buffer_put_int(&buffer, response_type); 370 371 if (ssh_request_reply(auth, &buffer, &buffer) == 0) { 372 buffer_free(&buffer); 373 return 0; 374 } 375 type = buffer_get_char(&buffer); 376 377 if (agent_failed(type)) { 378 log("Agent admitted failure to authenticate using the key."); 379 } else if (type != SSH_AGENT_RSA_RESPONSE) { 380 fatal("Bad authentication response: %d", type); 381 } else { 382 success = 1; 383 /* 384 * Get the response from the packet. This will abort with a 385 * fatal error if the packet is corrupt. 386 */ 387 for (i = 0; i < 16; i++) 388 response[i] = buffer_get_char(&buffer); 389 } 390 buffer_free(&buffer); 391 return success; 392 } 393 394 /* ask agent to sign data, returns -1 on error, 0 on success */ 395 int 396 ssh_agent_sign(AuthenticationConnection *auth, 397 Key *key, 398 u_char **sigp, u_int *lenp, 399 u_char *data, u_int datalen) 400 { 401 extern int datafellows; 402 Buffer msg; 403 u_char *blob; 404 u_int blen; 405 int type, flags = 0; 406 int ret = -1; 407 408 if (key_to_blob(key, &blob, &blen) == 0) 409 return -1; 410 411 if (datafellows & SSH_BUG_SIGBLOB) 412 flags = SSH_AGENT_OLD_SIGNATURE; 413 414 buffer_init(&msg); 415 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST); 416 buffer_put_string(&msg, blob, blen); 417 buffer_put_string(&msg, data, datalen); 418 buffer_put_int(&msg, flags); 419 xfree(blob); 420 421 if (ssh_request_reply(auth, &msg, &msg) == 0) { 422 buffer_free(&msg); 423 return -1; 424 } 425 type = buffer_get_char(&msg); 426 if (agent_failed(type)) { 427 log("Agent admitted failure to sign using the key."); 428 } else if (type != SSH2_AGENT_SIGN_RESPONSE) { 429 fatal("Bad authentication response: %d", type); 430 } else { 431 ret = 0; 432 *sigp = buffer_get_string(&msg, lenp); 433 } 434 buffer_free(&msg); 435 return ret; 436 } 437 438 /* Encode key for a message to the agent. */ 439 440 static void 441 ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment) 442 { 443 buffer_put_int(b, BN_num_bits(key->n)); 444 buffer_put_bignum(b, key->n); 445 buffer_put_bignum(b, key->e); 446 buffer_put_bignum(b, key->d); 447 /* To keep within the protocol: p < q for ssh. in SSL p > q */ 448 buffer_put_bignum(b, key->iqmp); /* ssh key->u */ 449 buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */ 450 buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */ 451 buffer_put_cstring(b, comment); 452 } 453 454 static void 455 ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment) 456 { 457 buffer_put_cstring(b, key_ssh_name(key)); 458 switch (key->type) { 459 case KEY_RSA: 460 buffer_put_bignum2(b, key->rsa->n); 461 buffer_put_bignum2(b, key->rsa->e); 462 buffer_put_bignum2(b, key->rsa->d); 463 buffer_put_bignum2(b, key->rsa->iqmp); 464 buffer_put_bignum2(b, key->rsa->p); 465 buffer_put_bignum2(b, key->rsa->q); 466 break; 467 case KEY_DSA: 468 buffer_put_bignum2(b, key->dsa->p); 469 buffer_put_bignum2(b, key->dsa->q); 470 buffer_put_bignum2(b, key->dsa->g); 471 buffer_put_bignum2(b, key->dsa->pub_key); 472 buffer_put_bignum2(b, key->dsa->priv_key); 473 break; 474 } 475 buffer_put_cstring(b, comment); 476 } 477 478 /* 479 * Adds an identity to the authentication server. This call is not meant to 480 * be used by normal applications. 481 */ 482 483 int 484 ssh_add_identity_constrained(AuthenticationConnection *auth, Key *key, 485 const char *comment, u_int life) 486 { 487 Buffer msg; 488 int type, constrained = (life != 0); 489 490 buffer_init(&msg); 491 492 switch (key->type) { 493 case KEY_RSA1: 494 type = constrained ? 495 SSH_AGENTC_ADD_RSA_ID_CONSTRAINED : 496 SSH_AGENTC_ADD_RSA_IDENTITY; 497 buffer_put_char(&msg, type); 498 ssh_encode_identity_rsa1(&msg, key->rsa, comment); 499 break; 500 case KEY_RSA: 501 case KEY_DSA: 502 type = constrained ? 503 SSH2_AGENTC_ADD_ID_CONSTRAINED : 504 SSH2_AGENTC_ADD_IDENTITY; 505 buffer_put_char(&msg, type); 506 ssh_encode_identity_ssh2(&msg, key, comment); 507 break; 508 default: 509 buffer_free(&msg); 510 return 0; 511 break; 512 } 513 if (constrained) { 514 if (life != 0) { 515 buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME); 516 buffer_put_int(&msg, life); 517 } 518 } 519 if (ssh_request_reply(auth, &msg, &msg) == 0) { 520 buffer_free(&msg); 521 return 0; 522 } 523 type = buffer_get_char(&msg); 524 buffer_free(&msg); 525 return decode_reply(type); 526 } 527 528 int 529 ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment) 530 { 531 return ssh_add_identity_constrained(auth, key, comment, 0); 532 } 533 534 /* 535 * Removes an identity from the authentication server. This call is not 536 * meant to be used by normal applications. 537 */ 538 539 int 540 ssh_remove_identity(AuthenticationConnection *auth, Key *key) 541 { 542 Buffer msg; 543 int type; 544 u_char *blob; 545 u_int blen; 546 547 buffer_init(&msg); 548 549 if (key->type == KEY_RSA1) { 550 buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY); 551 buffer_put_int(&msg, BN_num_bits(key->rsa->n)); 552 buffer_put_bignum(&msg, key->rsa->e); 553 buffer_put_bignum(&msg, key->rsa->n); 554 } else if (key->type == KEY_DSA || key->type == KEY_RSA) { 555 key_to_blob(key, &blob, &blen); 556 buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY); 557 buffer_put_string(&msg, blob, blen); 558 xfree(blob); 559 } else { 560 buffer_free(&msg); 561 return 0; 562 } 563 if (ssh_request_reply(auth, &msg, &msg) == 0) { 564 buffer_free(&msg); 565 return 0; 566 } 567 type = buffer_get_char(&msg); 568 buffer_free(&msg); 569 return decode_reply(type); 570 } 571 572 int 573 ssh_update_card(AuthenticationConnection *auth, int add, const char *reader_id, const char *pin) 574 { 575 Buffer msg; 576 int type; 577 578 buffer_init(&msg); 579 buffer_put_char(&msg, add ? SSH_AGENTC_ADD_SMARTCARD_KEY : 580 SSH_AGENTC_REMOVE_SMARTCARD_KEY); 581 buffer_put_cstring(&msg, reader_id); 582 buffer_put_cstring(&msg, pin); 583 if (ssh_request_reply(auth, &msg, &msg) == 0) { 584 buffer_free(&msg); 585 return 0; 586 } 587 type = buffer_get_char(&msg); 588 buffer_free(&msg); 589 return decode_reply(type); 590 } 591 592 /* 593 * Removes all identities from the agent. This call is not meant to be used 594 * by normal applications. 595 */ 596 597 int 598 ssh_remove_all_identities(AuthenticationConnection *auth, int version) 599 { 600 Buffer msg; 601 int type; 602 int code = (version==1) ? 603 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES : 604 SSH2_AGENTC_REMOVE_ALL_IDENTITIES; 605 606 buffer_init(&msg); 607 buffer_put_char(&msg, code); 608 609 if (ssh_request_reply(auth, &msg, &msg) == 0) { 610 buffer_free(&msg); 611 return 0; 612 } 613 type = buffer_get_char(&msg); 614 buffer_free(&msg); 615 return decode_reply(type); 616 } 617 618 int 619 decode_reply(int type) 620 { 621 switch (type) { 622 case SSH_AGENT_FAILURE: 623 case SSH_COM_AGENT2_FAILURE: 624 case SSH2_AGENT_FAILURE: 625 log("SSH_AGENT_FAILURE"); 626 return 0; 627 case SSH_AGENT_SUCCESS: 628 return 1; 629 default: 630 fatal("Bad response from authentication agent: %d", type); 631 } 632 /* NOTREACHED */ 633 return 0; 634 } 635