authfd.c revision 69591
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" 38RCSID("$OpenBSD: authfd.c,v 1.29 2000/10/09 21:51:00 markus Exp $"); 39RCSID("$FreeBSD: head/crypto/openssh/authfd.c 69591 2000-12-05 02:55:12Z green $"); 40 41#include "ssh.h" 42#include "rsa.h" 43#include "buffer.h" 44#include "bufaux.h" 45#include "xmalloc.h" 46#include "getput.h" 47 48#include <openssl/rsa.h> 49#include <openssl/dsa.h> 50#include <openssl/evp.h> 51#include "key.h" 52#include "authfd.h" 53#include "kex.h" 54#include "dsa.h" 55#include "compat.h" 56 57/* helper */ 58int 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 64/* Returns the number of the authentication fd, or -1 if there is none. */ 65 66int 67ssh_get_authentication_socket() 68{ 69 const char *authsocket; 70 int sock, len; 71 struct sockaddr_un sunaddr; 72 73 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME); 74 if (!authsocket) 75 return -1; 76 77 sunaddr.sun_family = AF_UNIX; 78 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); 79 sunaddr.sun_len = len = SUN_LEN(&sunaddr)+1; 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, len) < 0) { 91 close(sock); 92 return -1; 93 } 94 return sock; 95} 96 97int 98ssh_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 <= 0) { 122 error("Error reading response length from authentication socket."); 123 return 0; 124 } 125 len -= l; 126 } 127 128 /* Extract the length, and check it for sanity. */ 129 len = GET_32BIT(buf); 130 if (len > 256 * 1024) 131 fatal("Authentication response too long: %d", len); 132 133 /* Read the rest of the response in to the buffer. */ 134 buffer_clear(reply); 135 while (len > 0) { 136 l = len; 137 if (l > sizeof(buf)) 138 l = sizeof(buf); 139 l = read(auth->fd, buf, l); 140 if (l <= 0) { 141 error("Error reading response from authentication socket."); 142 return 0; 143 } 144 buffer_append(reply, (char *) buf, l); 145 len -= l; 146 } 147 return 1; 148} 149 150/* 151 * Closes the agent socket if it should be closed (depends on how it was 152 * obtained). The argument must have been returned by 153 * ssh_get_authentication_socket(). 154 */ 155 156void 157ssh_close_authentication_socket(int sock) 158{ 159 if (getenv(SSH_AUTHSOCKET_ENV_NAME)) 160 close(sock); 161} 162 163/* 164 * Opens and connects a private socket for communication with the 165 * authentication agent. Returns the file descriptor (which must be 166 * shut down and closed by the caller when no longer needed). 167 * Returns NULL if an error occurred and the connection could not be 168 * opened. 169 */ 170 171AuthenticationConnection * 172ssh_get_authentication_connection() 173{ 174 AuthenticationConnection *auth; 175 int sock; 176 177 sock = ssh_get_authentication_socket(); 178 179 /* 180 * Fail if we couldn't obtain a connection. This happens if we 181 * exited due to a timeout. 182 */ 183 if (sock < 0) 184 return NULL; 185 186 auth = xmalloc(sizeof(*auth)); 187 auth->fd = sock; 188 buffer_init(&auth->identities); 189 auth->howmany = 0; 190 191 return auth; 192} 193 194/* 195 * Closes the connection to the authentication agent and frees any associated 196 * memory. 197 */ 198 199void 200ssh_close_authentication_connection(AuthenticationConnection *auth) 201{ 202 buffer_free(&auth->identities); 203 close(auth->fd); 204 xfree(auth); 205} 206 207/* 208 * Returns the first authentication identity held by the agent. 209 */ 210 211Key * 212ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version) 213{ 214 int type, code1 = 0, code2 = 0; 215 Buffer request; 216 217 switch(version){ 218 case 1: 219 code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES; 220 code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER; 221 break; 222 case 2: 223 code1 = SSH2_AGENTC_REQUEST_IDENTITIES; 224 code2 = SSH2_AGENT_IDENTITIES_ANSWER; 225 break; 226 default: 227 return NULL; 228 } 229 230 /* 231 * Send a message to the agent requesting for a list of the 232 * identities it can represent. 233 */ 234 buffer_init(&request); 235 buffer_put_char(&request, code1); 236 237 buffer_clear(&auth->identities); 238 if (ssh_request_reply(auth, &request, &auth->identities) == 0) { 239 buffer_free(&request); 240 return NULL; 241 } 242 buffer_free(&request); 243 244 /* Get message type, and verify that we got a proper answer. */ 245 type = buffer_get_char(&auth->identities); 246 if (agent_failed(type)) { 247 return NULL; 248 } else if (type != code2) { 249 fatal("Bad authentication reply message type: %d", type); 250 } 251 252 /* Get the number of entries in the response and check it for sanity. */ 253 auth->howmany = buffer_get_int(&auth->identities); 254 if (auth->howmany > 1024) 255 fatal("Too many identities in authentication reply: %d\n", 256 auth->howmany); 257 258 /* Return the first entry (if any). */ 259 return ssh_get_next_identity(auth, comment, version); 260} 261 262Key * 263ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version) 264{ 265 unsigned int bits; 266 unsigned char *blob; 267 unsigned int blen; 268 Key *key = NULL; 269 270 /* Return failure if no more entries. */ 271 if (auth->howmany <= 0) 272 return NULL; 273 274 /* 275 * Get the next entry from the packet. These will abort with a fatal 276 * error if the packet is too short or contains corrupt data. 277 */ 278 switch(version){ 279 case 1: 280 key = key_new(KEY_RSA); 281 bits = buffer_get_int(&auth->identities); 282 buffer_get_bignum(&auth->identities, key->rsa->e); 283 buffer_get_bignum(&auth->identities, key->rsa->n); 284 *comment = buffer_get_string(&auth->identities, NULL); 285 if (bits != BN_num_bits(key->rsa->n)) 286 log("Warning: identity keysize mismatch: actual %d, announced %u", 287 BN_num_bits(key->rsa->n), bits); 288 break; 289 case 2: 290 blob = buffer_get_string(&auth->identities, &blen); 291 *comment = buffer_get_string(&auth->identities, NULL); 292 key = dsa_key_from_blob(blob, blen); 293 xfree(blob); 294 break; 295 default: 296 return NULL; 297 break; 298 } 299 /* Decrement the number of remaining entries. */ 300 auth->howmany--; 301 return key; 302} 303 304/* 305 * Generates a random challenge, sends it to the agent, and waits for 306 * response from the agent. Returns true (non-zero) if the agent gave the 307 * correct answer, zero otherwise. Response type selects the style of 308 * response desired, with 0 corresponding to protocol version 1.0 (no longer 309 * supported) and 1 corresponding to protocol version 1.1. 310 */ 311 312int 313ssh_decrypt_challenge(AuthenticationConnection *auth, 314 Key* key, BIGNUM *challenge, 315 unsigned char session_id[16], 316 unsigned int response_type, 317 unsigned char response[16]) 318{ 319 Buffer buffer; 320 int success = 0; 321 int i; 322 int type; 323 324 if (key->type != KEY_RSA) 325 return 0; 326 if (response_type == 0) { 327 log("Compatibility with ssh protocol version 1.0 no longer supported."); 328 return 0; 329 } 330 buffer_init(&buffer); 331 buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE); 332 buffer_put_int(&buffer, BN_num_bits(key->rsa->n)); 333 buffer_put_bignum(&buffer, key->rsa->e); 334 buffer_put_bignum(&buffer, key->rsa->n); 335 buffer_put_bignum(&buffer, challenge); 336 buffer_append(&buffer, (char *) session_id, 16); 337 buffer_put_int(&buffer, response_type); 338 339 if (ssh_request_reply(auth, &buffer, &buffer) == 0) { 340 buffer_free(&buffer); 341 return 0; 342 } 343 type = buffer_get_char(&buffer); 344 345 if (agent_failed(type)) { 346 log("Agent admitted failure to authenticate using the key."); 347 } else if (type != SSH_AGENT_RSA_RESPONSE) { 348 fatal("Bad authentication response: %d", type); 349 } else { 350 success = 1; 351 /* 352 * Get the response from the packet. This will abort with a 353 * fatal error if the packet is corrupt. 354 */ 355 for (i = 0; i < 16; i++) 356 response[i] = buffer_get_char(&buffer); 357 } 358 buffer_free(&buffer); 359 return success; 360} 361 362/* ask agent to sign data, returns -1 on error, 0 on success */ 363int 364ssh_agent_sign(AuthenticationConnection *auth, 365 Key *key, 366 unsigned char **sigp, int *lenp, 367 unsigned char *data, int datalen) 368{ 369 extern int datafellows; 370 Buffer msg; 371 unsigned char *blob; 372 unsigned int blen; 373 int type, flags = 0; 374 int ret = -1; 375 376 if (dsa_make_key_blob(key, &blob, &blen) == 0) 377 return -1; 378 379 if (datafellows & SSH_BUG_SIGBLOB) 380 flags = SSH_AGENT_OLD_SIGNATURE; 381 382 buffer_init(&msg); 383 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST); 384 buffer_put_string(&msg, blob, blen); 385 buffer_put_string(&msg, data, datalen); 386 buffer_put_int(&msg, flags); 387 xfree(blob); 388 389 if (ssh_request_reply(auth, &msg, &msg) == 0) { 390 buffer_free(&msg); 391 return -1; 392 } 393 type = buffer_get_char(&msg); 394 if (agent_failed(type)) { 395 log("Agent admitted failure to sign using the key."); 396 } else if (type != SSH2_AGENT_SIGN_RESPONSE) { 397 fatal("Bad authentication response: %d", type); 398 } else { 399 ret = 0; 400 *sigp = buffer_get_string(&msg, lenp); 401 } 402 buffer_free(&msg); 403 return ret; 404} 405 406/* Encode key for a message to the agent. */ 407 408void 409ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment) 410{ 411 buffer_clear(b); 412 buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY); 413 buffer_put_int(b, BN_num_bits(key->n)); 414 buffer_put_bignum(b, key->n); 415 buffer_put_bignum(b, key->e); 416 buffer_put_bignum(b, key->d); 417 /* To keep within the protocol: p < q for ssh. in SSL p > q */ 418 buffer_put_bignum(b, key->iqmp); /* ssh key->u */ 419 buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */ 420 buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */ 421 buffer_put_string(b, comment, strlen(comment)); 422} 423 424void 425ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment) 426{ 427 buffer_clear(b); 428 buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY); 429 buffer_put_cstring(b, KEX_DSS); 430 buffer_put_bignum2(b, key->p); 431 buffer_put_bignum2(b, key->q); 432 buffer_put_bignum2(b, key->g); 433 buffer_put_bignum2(b, key->pub_key); 434 buffer_put_bignum2(b, key->priv_key); 435 buffer_put_string(b, comment, strlen(comment)); 436} 437 438/* 439 * Adds an identity to the authentication server. This call is not meant to 440 * be used by normal applications. 441 */ 442 443int 444ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment) 445{ 446 Buffer msg; 447 int type; 448 449 buffer_init(&msg); 450 451 switch (key->type) { 452 case KEY_RSA: 453 ssh_encode_identity_rsa(&msg, key->rsa, comment); 454 break; 455 case KEY_DSA: 456 ssh_encode_identity_dsa(&msg, key->dsa, comment); 457 break; 458 default: 459 buffer_free(&msg); 460 return 0; 461 break; 462 } 463 if (ssh_request_reply(auth, &msg, &msg) == 0) { 464 buffer_free(&msg); 465 return 0; 466 } 467 type = buffer_get_char(&msg); 468 buffer_free(&msg); 469 return decode_reply(type); 470} 471 472/* 473 * Removes an identity from the authentication server. This call is not 474 * meant to be used by normal applications. 475 */ 476 477int 478ssh_remove_identity(AuthenticationConnection *auth, Key *key) 479{ 480 Buffer msg; 481 int type; 482 unsigned char *blob; 483 unsigned int blen; 484 485 buffer_init(&msg); 486 487 if (key->type == KEY_RSA) { 488 buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY); 489 buffer_put_int(&msg, BN_num_bits(key->rsa->n)); 490 buffer_put_bignum(&msg, key->rsa->e); 491 buffer_put_bignum(&msg, key->rsa->n); 492 } else if (key->type == KEY_DSA) { 493 dsa_make_key_blob(key, &blob, &blen); 494 buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY); 495 buffer_put_string(&msg, blob, blen); 496 xfree(blob); 497 } else { 498 buffer_free(&msg); 499 return 0; 500 } 501 if (ssh_request_reply(auth, &msg, &msg) == 0) { 502 buffer_free(&msg); 503 return 0; 504 } 505 type = buffer_get_char(&msg); 506 buffer_free(&msg); 507 return decode_reply(type); 508} 509 510/* 511 * Removes all identities from the agent. This call is not meant to be used 512 * by normal applications. 513 */ 514 515int 516ssh_remove_all_identities(AuthenticationConnection *auth, int version) 517{ 518 Buffer msg; 519 int type; 520 int code = (version==1) ? 521 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES : 522 SSH2_AGENTC_REMOVE_ALL_IDENTITIES; 523 524 buffer_init(&msg); 525 buffer_put_char(&msg, code); 526 527 if (ssh_request_reply(auth, &msg, &msg) == 0) { 528 buffer_free(&msg); 529 return 0; 530 } 531 type = buffer_get_char(&msg); 532 buffer_free(&msg); 533 return decode_reply(type); 534} 535 536int 537decode_reply(int type) 538{ 539 switch (type) { 540 case SSH_AGENT_FAILURE: 541 case SSH_COM_AGENT2_FAILURE: 542 log("SSH_AGENT_FAILURE"); 543 return 0; 544 case SSH_AGENT_SUCCESS: 545 return 1; 546 default: 547 fatal("Bad response from authentication agent: %d", type); 548 } 549 /* NOTREACHED */ 550 return 0; 551} 552