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