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