00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include "includes.h"
00038 RCSID("$OpenBSD: authfd.c,v 1.66 2005/06/17 02:44:32 djm Exp $");
00039
00040 #include <openssl/evp.h>
00041
00042 #include "ssh.h"
00043 #include "rsa.h"
00044 #include "buffer.h"
00045 #include "bufaux.h"
00046 #include "xmalloc.h"
00047 #include "getput.h"
00048 #include "key.h"
00049 #include "authfd.h"
00050 #include "cipher.h"
00051 #include "kex.h"
00052 #include "compat.h"
00053 #include "log.h"
00054 #include "atomicio.h"
00055
00056 static int agent_present = 0;
00057
00058
00059 int decode_reply(int type);
00060
00061
00062 #define agent_failed(x) \
00063 ((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \
00064 (x == SSH2_AGENT_FAILURE))
00065
00066 int
00067 ssh_agent_present(void)
00068 {
00069 int authfd;
00070
00071 if (agent_present)
00072 return 1;
00073 if ((authfd = ssh_get_authentication_socket()) == -1)
00074 return 0;
00075 else {
00076 ssh_close_authentication_socket(authfd);
00077 return 1;
00078 }
00079 }
00080
00081
00082
00083 int
00084 ssh_get_authentication_socket(void)
00085 {
00086 const char *authsocket;
00087 int sock;
00088 struct sockaddr_un sunaddr;
00089
00090 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
00091 if (!authsocket)
00092 return -1;
00093
00094 sunaddr.sun_family = AF_UNIX;
00095 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
00096
00097 sock = socket(AF_UNIX, SOCK_STREAM, 0);
00098 if (sock < 0)
00099 return -1;
00100
00101
00102 if (fcntl(sock, F_SETFD, 1) == -1) {
00103 close(sock);
00104 return -1;
00105 }
00106 if (connect(sock, (struct sockaddr *) &sunaddr, sizeof sunaddr) < 0) {
00107 close(sock);
00108 return -1;
00109 }
00110 agent_present = 1;
00111 return sock;
00112 }
00113
00114 static int
00115 ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
00116 {
00117 u_int l, len;
00118 char buf[1024];
00119
00120
00121 len = buffer_len(request);
00122 PUT_32BIT(buf, len);
00123
00124
00125 if (atomicio(vwrite, auth->fd, buf, 4) != 4 ||
00126 atomicio(vwrite, auth->fd, buffer_ptr(request),
00127 buffer_len(request)) != buffer_len(request)) {
00128 error("Error writing to authentication socket.");
00129 return 0;
00130 }
00131
00132
00133
00134
00135 if (atomicio(read, auth->fd, buf, 4) != 4) {
00136 error("Error reading response length from authentication socket.");
00137 return 0;
00138 }
00139
00140
00141 len = GET_32BIT(buf);
00142 if (len > 256 * 1024)
00143 fatal("Authentication response too long: %u", len);
00144
00145
00146 buffer_clear(reply);
00147 while (len > 0) {
00148 l = len;
00149 if (l > sizeof(buf))
00150 l = sizeof(buf);
00151 if (atomicio(read, auth->fd, buf, l) != l) {
00152 error("Error reading response from authentication socket.");
00153 return 0;
00154 }
00155 buffer_append(reply, buf, l);
00156 len -= l;
00157 }
00158 return 1;
00159 }
00160
00161
00162
00163
00164
00165
00166
00167 void
00168 ssh_close_authentication_socket(int sock)
00169 {
00170 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
00171 close(sock);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 AuthenticationConnection *
00183 ssh_get_authentication_connection(void)
00184 {
00185 AuthenticationConnection *auth;
00186 int sock;
00187
00188 sock = ssh_get_authentication_socket();
00189
00190
00191
00192
00193
00194 if (sock < 0)
00195 return NULL;
00196
00197 auth = xmalloc(sizeof(*auth));
00198 auth->fd = sock;
00199 buffer_init(&auth->identities);
00200 auth->howmany = 0;
00201
00202 return auth;
00203 }
00204
00205
00206
00207
00208
00209
00210 void
00211 ssh_close_authentication_connection(AuthenticationConnection *auth)
00212 {
00213 buffer_free(&auth->identities);
00214 close(auth->fd);
00215 xfree(auth);
00216 }
00217
00218
00219 int
00220 ssh_lock_agent(AuthenticationConnection *auth, int lock, const char *password)
00221 {
00222 int type;
00223 Buffer msg;
00224
00225 buffer_init(&msg);
00226 buffer_put_char(&msg, lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK);
00227 buffer_put_cstring(&msg, password);
00228
00229 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00230 buffer_free(&msg);
00231 return 0;
00232 }
00233 type = buffer_get_char(&msg);
00234 buffer_free(&msg);
00235 return decode_reply(type);
00236 }
00237
00238
00239
00240
00241
00242 int
00243 ssh_get_num_identities(AuthenticationConnection *auth, int version)
00244 {
00245 int type, code1 = 0, code2 = 0;
00246 Buffer request;
00247
00248 switch (version) {
00249 case 1:
00250 code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
00251 code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
00252 break;
00253 case 2:
00254 code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
00255 code2 = SSH2_AGENT_IDENTITIES_ANSWER;
00256 break;
00257 default:
00258 return 0;
00259 }
00260
00261
00262
00263
00264
00265 buffer_init(&request);
00266 buffer_put_char(&request, code1);
00267
00268 buffer_clear(&auth->identities);
00269 if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
00270 buffer_free(&request);
00271 return 0;
00272 }
00273 buffer_free(&request);
00274
00275
00276 type = buffer_get_char(&auth->identities);
00277 if (agent_failed(type)) {
00278 return 0;
00279 } else if (type != code2) {
00280 fatal("Bad authentication reply message type: %d", type);
00281 }
00282
00283
00284 auth->howmany = buffer_get_int(&auth->identities);
00285 if ((u_int)auth->howmany > 1024)
00286 fatal("Too many identities in authentication reply: %d",
00287 auth->howmany);
00288
00289 return auth->howmany;
00290 }
00291
00292 Key *
00293 ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
00294 {
00295
00296 if (ssh_get_num_identities(auth, version) > 0)
00297 return ssh_get_next_identity(auth, comment, version);
00298 return NULL;
00299 }
00300
00301 Key *
00302 ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
00303 {
00304 int keybits;
00305 u_int bits;
00306 u_char *blob;
00307 u_int blen;
00308 Key *key = NULL;
00309
00310
00311 if (auth->howmany <= 0)
00312 return NULL;
00313
00314
00315
00316
00317
00318 switch (version) {
00319 case 1:
00320 key = key_new(KEY_RSA1);
00321 bits = buffer_get_int(&auth->identities);
00322 buffer_get_bignum(&auth->identities, key->rsa->e);
00323 buffer_get_bignum(&auth->identities, key->rsa->n);
00324 *comment = buffer_get_string(&auth->identities, NULL);
00325 keybits = BN_num_bits(key->rsa->n);
00326 if (keybits < 0 || bits != (u_int)keybits)
00327 logit("Warning: identity keysize mismatch: actual %d, announced %u",
00328 BN_num_bits(key->rsa->n), bits);
00329 break;
00330 case 2:
00331 blob = buffer_get_string(&auth->identities, &blen);
00332 *comment = buffer_get_string(&auth->identities, NULL);
00333 key = key_from_blob(blob, blen);
00334 xfree(blob);
00335 break;
00336 default:
00337 return NULL;
00338 break;
00339 }
00340
00341 auth->howmany--;
00342 return key;
00343 }
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353 int
00354 ssh_decrypt_challenge(AuthenticationConnection *auth,
00355 Key* key, BIGNUM *challenge,
00356 u_char session_id[16],
00357 u_int response_type,
00358 u_char response[16])
00359 {
00360 Buffer buffer;
00361 int success = 0;
00362 int i;
00363 int type;
00364
00365 if (key->type != KEY_RSA1)
00366 return 0;
00367 if (response_type == 0) {
00368 logit("Compatibility with ssh protocol version 1.0 no longer supported.");
00369 return 0;
00370 }
00371 buffer_init(&buffer);
00372 buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
00373 buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
00374 buffer_put_bignum(&buffer, key->rsa->e);
00375 buffer_put_bignum(&buffer, key->rsa->n);
00376 buffer_put_bignum(&buffer, challenge);
00377 buffer_append(&buffer, session_id, 16);
00378 buffer_put_int(&buffer, response_type);
00379
00380 if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
00381 buffer_free(&buffer);
00382 return 0;
00383 }
00384 type = buffer_get_char(&buffer);
00385
00386 if (agent_failed(type)) {
00387 logit("Agent admitted failure to authenticate using the key.");
00388 } else if (type != SSH_AGENT_RSA_RESPONSE) {
00389 fatal("Bad authentication response: %d", type);
00390 } else {
00391 success = 1;
00392
00393
00394
00395
00396 for (i = 0; i < 16; i++)
00397 response[i] = buffer_get_char(&buffer);
00398 }
00399 buffer_free(&buffer);
00400 return success;
00401 }
00402
00403
00404 int
00405 ssh_agent_sign(AuthenticationConnection *auth,
00406 Key *key,
00407 u_char **sigp, u_int *lenp,
00408 u_char *data, u_int datalen)
00409 {
00410 extern int datafellows;
00411 Buffer msg;
00412 u_char *blob;
00413 u_int blen;
00414 int type, flags = 0;
00415 int ret = -1;
00416
00417 if (key_to_blob(key, &blob, &blen) == 0)
00418 return -1;
00419
00420 if (datafellows & SSH_BUG_SIGBLOB)
00421 flags = SSH_AGENT_OLD_SIGNATURE;
00422
00423 buffer_init(&msg);
00424 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
00425 buffer_put_string(&msg, blob, blen);
00426 buffer_put_string(&msg, data, datalen);
00427 buffer_put_int(&msg, flags);
00428 xfree(blob);
00429
00430 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00431 buffer_free(&msg);
00432 return -1;
00433 }
00434 type = buffer_get_char(&msg);
00435 if (agent_failed(type)) {
00436 logit("Agent admitted failure to sign using the key.");
00437 } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
00438 fatal("Bad authentication response: %d", type);
00439 } else {
00440 ret = 0;
00441 *sigp = buffer_get_string(&msg, lenp);
00442 }
00443 buffer_free(&msg);
00444 return ret;
00445 }
00446
00447
00448
00449 static void
00450 ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
00451 {
00452 buffer_put_int(b, BN_num_bits(key->n));
00453 buffer_put_bignum(b, key->n);
00454 buffer_put_bignum(b, key->e);
00455 buffer_put_bignum(b, key->d);
00456
00457 buffer_put_bignum(b, key->iqmp);
00458 buffer_put_bignum(b, key->q);
00459 buffer_put_bignum(b, key->p);
00460 buffer_put_cstring(b, comment);
00461 }
00462
00463 static void
00464 ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
00465 {
00466 buffer_put_cstring(b, key_ssh_name(key));
00467 switch (key->type) {
00468 case KEY_RSA:
00469 buffer_put_bignum2(b, key->rsa->n);
00470 buffer_put_bignum2(b, key->rsa->e);
00471 buffer_put_bignum2(b, key->rsa->d);
00472 buffer_put_bignum2(b, key->rsa->iqmp);
00473 buffer_put_bignum2(b, key->rsa->p);
00474 buffer_put_bignum2(b, key->rsa->q);
00475 break;
00476 case KEY_DSA:
00477 buffer_put_bignum2(b, key->dsa->p);
00478 buffer_put_bignum2(b, key->dsa->q);
00479 buffer_put_bignum2(b, key->dsa->g);
00480 buffer_put_bignum2(b, key->dsa->pub_key);
00481 buffer_put_bignum2(b, key->dsa->priv_key);
00482 break;
00483 }
00484 buffer_put_cstring(b, comment);
00485 }
00486
00487
00488
00489
00490
00491
00492 int
00493 ssh_add_identity_constrained(AuthenticationConnection *auth, Key *key,
00494 const char *comment, u_int life, u_int confirm)
00495 {
00496 Buffer msg;
00497 int type, constrained = (life || confirm);
00498
00499 buffer_init(&msg);
00500
00501 switch (key->type) {
00502 case KEY_RSA1:
00503 type = constrained ?
00504 SSH_AGENTC_ADD_RSA_ID_CONSTRAINED :
00505 SSH_AGENTC_ADD_RSA_IDENTITY;
00506 buffer_put_char(&msg, type);
00507 ssh_encode_identity_rsa1(&msg, key->rsa, comment);
00508 break;
00509 case KEY_RSA:
00510 case KEY_DSA:
00511 type = constrained ?
00512 SSH2_AGENTC_ADD_ID_CONSTRAINED :
00513 SSH2_AGENTC_ADD_IDENTITY;
00514 buffer_put_char(&msg, type);
00515 ssh_encode_identity_ssh2(&msg, key, comment);
00516 break;
00517 default:
00518 buffer_free(&msg);
00519 return 0;
00520 break;
00521 }
00522 if (constrained) {
00523 if (life != 0) {
00524 buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
00525 buffer_put_int(&msg, life);
00526 }
00527 if (confirm != 0)
00528 buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_CONFIRM);
00529 }
00530 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00531 buffer_free(&msg);
00532 return 0;
00533 }
00534 type = buffer_get_char(&msg);
00535 buffer_free(&msg);
00536 return decode_reply(type);
00537 }
00538
00539 int
00540 ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
00541 {
00542 return ssh_add_identity_constrained(auth, key, comment, 0, 0);
00543 }
00544
00545
00546
00547
00548
00549
00550 int
00551 ssh_remove_identity(AuthenticationConnection *auth, Key *key)
00552 {
00553 Buffer msg;
00554 int type;
00555 u_char *blob;
00556 u_int blen;
00557
00558 buffer_init(&msg);
00559
00560 if (key->type == KEY_RSA1) {
00561 buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
00562 buffer_put_int(&msg, BN_num_bits(key->rsa->n));
00563 buffer_put_bignum(&msg, key->rsa->e);
00564 buffer_put_bignum(&msg, key->rsa->n);
00565 } else if (key->type == KEY_DSA || key->type == KEY_RSA) {
00566 key_to_blob(key, &blob, &blen);
00567 buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
00568 buffer_put_string(&msg, blob, blen);
00569 xfree(blob);
00570 } else {
00571 buffer_free(&msg);
00572 return 0;
00573 }
00574 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00575 buffer_free(&msg);
00576 return 0;
00577 }
00578 type = buffer_get_char(&msg);
00579 buffer_free(&msg);
00580 return decode_reply(type);
00581 }
00582
00583 int
00584 ssh_update_card(AuthenticationConnection *auth, int add,
00585 const char *reader_id, const char *pin, u_int life, u_int confirm)
00586 {
00587 Buffer msg;
00588 int type, constrained = (life || confirm);
00589
00590 if (add) {
00591 type = constrained ?
00592 SSH_AGENTC_ADD_SMARTCARD_KEY_CONSTRAINED :
00593 SSH_AGENTC_ADD_SMARTCARD_KEY;
00594 } else
00595 type = SSH_AGENTC_REMOVE_SMARTCARD_KEY;
00596
00597 buffer_init(&msg);
00598 buffer_put_char(&msg, type);
00599 buffer_put_cstring(&msg, reader_id);
00600 buffer_put_cstring(&msg, pin);
00601
00602 if (constrained) {
00603 if (life != 0) {
00604 buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
00605 buffer_put_int(&msg, life);
00606 }
00607 if (confirm != 0)
00608 buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_CONFIRM);
00609 }
00610
00611 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00612 buffer_free(&msg);
00613 return 0;
00614 }
00615 type = buffer_get_char(&msg);
00616 buffer_free(&msg);
00617 return decode_reply(type);
00618 }
00619
00620
00621
00622
00623
00624
00625 int
00626 ssh_remove_all_identities(AuthenticationConnection *auth, int version)
00627 {
00628 Buffer msg;
00629 int type;
00630 int code = (version==1) ?
00631 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
00632 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
00633
00634 buffer_init(&msg);
00635 buffer_put_char(&msg, code);
00636
00637 if (ssh_request_reply(auth, &msg, &msg) == 0) {
00638 buffer_free(&msg);
00639 return 0;
00640 }
00641 type = buffer_get_char(&msg);
00642 buffer_free(&msg);
00643 return decode_reply(type);
00644 }
00645
00646 int
00647 decode_reply(int type)
00648 {
00649 switch (type) {
00650 case SSH_AGENT_FAILURE:
00651 case SSH_COM_AGENT2_FAILURE:
00652 case SSH2_AGENT_FAILURE:
00653 logit("SSH_AGENT_FAILURE");
00654 return 0;
00655 case SSH_AGENT_SUCCESS:
00656 return 1;
00657 default:
00658 fatal("Bad response from authentication agent: %d", type);
00659 }
00660
00661 return 0;
00662 }