Annotation of src/usr.bin/ssh/authfd.c, Revision 1.37
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.37 ! millert 38: RCSID("$OpenBSD: authfd.c,v 1.36 2001/03/01 02:11:25 deraadt Exp $");
1.33 markus 39:
40: #include <openssl/evp.h>
1.1 deraadt 41:
42: #include "ssh.h"
43: #include "rsa.h"
44: #include "buffer.h"
45: #include "bufaux.h"
46: #include "xmalloc.h"
47: #include "getput.h"
1.22 markus 48: #include "key.h"
49: #include "authfd.h"
1.33 markus 50: #include "cipher.h"
1.22 markus 51: #include "kex.h"
1.28 markus 52: #include "compat.h"
1.33 markus 53: #include "log.h"
54: #include "atomicio.h"
1.2 provos 55:
1.21 markus 56: /* helper */
1.24 markus 57: int decode_reply(int type);
1.21 markus 58:
1.29 markus 59: /* macro to check for "agent failure" message */
60: #define agent_failed(x) \
61: ((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE))
62:
1.1 deraadt 63: /* Returns the number of the authentication fd, or -1 if there is none. */
64:
1.2 provos 65: int
1.32 markus 66: ssh_get_authentication_socket(void)
1.1 deraadt 67: {
1.12 markus 68: const char *authsocket;
1.23 deraadt 69: int sock, len;
1.12 markus 70: struct sockaddr_un sunaddr;
71:
72: authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
73: if (!authsocket)
74: return -1;
75:
76: sunaddr.sun_family = AF_UNIX;
77: strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
1.36 deraadt 78: len = SUN_LEN(&sunaddr)+1;
79: sunaddr.sun_len = len;
1.12 markus 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: }
1.23 deraadt 90: if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
1.12 markus 91: close(sock);
92: return -1;
93: }
94: return sock;
1.1 deraadt 95: }
96:
1.24 markus 97: int
1.25 markus 98: ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
1.24 markus 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:
1.14 markus 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: */
1.1 deraadt 155:
1.18 markus 156: void
1.12 markus 157: ssh_close_authentication_socket(int sock)
1.1 deraadt 158: {
1.12 markus 159: if (getenv(SSH_AUTHSOCKET_ENV_NAME))
160: close(sock);
1.1 deraadt 161: }
162:
1.14 markus 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: */
1.1 deraadt 170:
1.12 markus 171: AuthenticationConnection *
1.32 markus 172: ssh_get_authentication_connection(void)
1.1 deraadt 173: {
1.12 markus 174: AuthenticationConnection *auth;
175: int sock;
1.1 deraadt 176:
1.12 markus 177: sock = ssh_get_authentication_socket();
178:
1.14 markus 179: /*
180: * Fail if we couldn't obtain a connection. This happens if we
181: * exited due to a timeout.
182: */
1.12 markus 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;
1.1 deraadt 192: }
193:
1.14 markus 194: /*
195: * Closes the connection to the authentication agent and frees any associated
196: * memory.
197: */
1.1 deraadt 198:
1.18 markus 199: void
1.25 markus 200: ssh_close_authentication_connection(AuthenticationConnection *auth)
1.1 deraadt 201: {
1.25 markus 202: buffer_free(&auth->identities);
203: close(auth->fd);
204: xfree(auth);
1.1 deraadt 205: }
206:
1.14 markus 207: /*
208: * Returns the first authentication identity held by the agent.
209: */
1.1 deraadt 210:
1.30 markus 211: int
212: ssh_get_num_identities(AuthenticationConnection *auth, int version)
1.1 deraadt 213: {
1.25 markus 214: int type, code1 = 0, code2 = 0;
1.24 markus 215: Buffer request;
1.25 markus 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:
1.30 markus 227: return 0;
1.25 markus 228: }
1.1 deraadt 229:
1.14 markus 230: /*
231: * Send a message to the agent requesting for a list of the
232: * identities it can represent.
233: */
1.24 markus 234: buffer_init(&request);
1.25 markus 235: buffer_put_char(&request, code1);
1.12 markus 236:
237: buffer_clear(&auth->identities);
1.24 markus 238: if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
239: buffer_free(&request);
1.30 markus 240: return 0;
1.12 markus 241: }
1.24 markus 242: buffer_free(&request);
1.1 deraadt 243:
1.12 markus 244: /* Get message type, and verify that we got a proper answer. */
1.24 markus 245: type = buffer_get_char(&auth->identities);
1.29 markus 246: if (agent_failed(type)) {
1.30 markus 247: return 0;
1.25 markus 248: } else if (type != code2) {
1.24 markus 249: fatal("Bad authentication reply message type: %d", type);
1.25 markus 250: }
1.12 markus 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)
1.37 ! millert 255: fatal("Too many identities in authentication reply: %d",
1.24 markus 256: auth->howmany);
1.12 markus 257:
1.30 markus 258: return auth->howmany;
259: }
260:
261: Key *
262: ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
263: {
264: /* get number of identities and return the first entry (if any). */
265: if (ssh_get_num_identities(auth, version) > 0)
266: return ssh_get_next_identity(auth, comment, version);
267: return NULL;
1.1 deraadt 268: }
269:
1.25 markus 270: Key *
271: ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
1.1 deraadt 272: {
1.31 markus 273: u_int bits;
274: u_char *blob;
275: u_int blen;
1.25 markus 276: Key *key = NULL;
1.9 markus 277:
1.12 markus 278: /* Return failure if no more entries. */
279: if (auth->howmany <= 0)
1.25 markus 280: return NULL;
1.12 markus 281:
1.14 markus 282: /*
283: * Get the next entry from the packet. These will abort with a fatal
284: * error if the packet is too short or contains corrupt data.
285: */
1.25 markus 286: switch(version){
287: case 1:
1.30 markus 288: key = key_new(KEY_RSA1);
1.25 markus 289: bits = buffer_get_int(&auth->identities);
290: buffer_get_bignum(&auth->identities, key->rsa->e);
291: buffer_get_bignum(&auth->identities, key->rsa->n);
292: *comment = buffer_get_string(&auth->identities, NULL);
293: if (bits != BN_num_bits(key->rsa->n))
294: log("Warning: identity keysize mismatch: actual %d, announced %u",
295: BN_num_bits(key->rsa->n), bits);
296: break;
297: case 2:
298: blob = buffer_get_string(&auth->identities, &blen);
299: *comment = buffer_get_string(&auth->identities, NULL);
1.30 markus 300: key = key_from_blob(blob, blen);
1.25 markus 301: xfree(blob);
302: break;
303: default:
304: return NULL;
305: break;
306: }
1.12 markus 307: /* Decrement the number of remaining entries. */
308: auth->howmany--;
1.25 markus 309: return key;
1.1 deraadt 310: }
311:
1.14 markus 312: /*
313: * Generates a random challenge, sends it to the agent, and waits for
314: * response from the agent. Returns true (non-zero) if the agent gave the
315: * correct answer, zero otherwise. Response type selects the style of
316: * response desired, with 0 corresponding to protocol version 1.0 (no longer
317: * supported) and 1 corresponding to protocol version 1.1.
318: */
1.1 deraadt 319:
1.2 provos 320: int
321: ssh_decrypt_challenge(AuthenticationConnection *auth,
1.25 markus 322: Key* key, BIGNUM *challenge,
1.31 markus 323: u_char session_id[16],
324: u_int response_type,
325: u_char response[16])
1.1 deraadt 326: {
1.12 markus 327: Buffer buffer;
1.24 markus 328: int success = 0;
329: int i;
330: int type;
1.12 markus 331:
1.30 markus 332: if (key->type != KEY_RSA1)
1.25 markus 333: return 0;
334: if (response_type == 0) {
335: log("Compatibility with ssh protocol version 1.0 no longer supported.");
336: return 0;
337: }
1.12 markus 338: buffer_init(&buffer);
1.24 markus 339: buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
1.25 markus 340: buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
341: buffer_put_bignum(&buffer, key->rsa->e);
342: buffer_put_bignum(&buffer, key->rsa->n);
1.12 markus 343: buffer_put_bignum(&buffer, challenge);
344: buffer_append(&buffer, (char *) session_id, 16);
345: buffer_put_int(&buffer, response_type);
346:
1.24 markus 347: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12 markus 348: buffer_free(&buffer);
349: return 0;
350: }
1.24 markus 351: type = buffer_get_char(&buffer);
1.12 markus 352:
1.29 markus 353: if (agent_failed(type)) {
1.12 markus 354: log("Agent admitted failure to authenticate using the key.");
1.24 markus 355: } else if (type != SSH_AGENT_RSA_RESPONSE) {
356: fatal("Bad authentication response: %d", type);
357: } else {
358: success = 1;
359: /*
360: * Get the response from the packet. This will abort with a
361: * fatal error if the packet is corrupt.
362: */
363: for (i = 0; i < 16; i++)
364: response[i] = buffer_get_char(&buffer);
1.12 markus 365: }
366: buffer_free(&buffer);
1.24 markus 367: return success;
1.12 markus 368: }
1.1 deraadt 369:
1.25 markus 370: /* ask agent to sign data, returns -1 on error, 0 on success */
371: int
372: ssh_agent_sign(AuthenticationConnection *auth,
373: Key *key,
1.31 markus 374: u_char **sigp, int *lenp,
375: u_char *data, int datalen)
1.25 markus 376: {
1.28 markus 377: extern int datafellows;
1.25 markus 378: Buffer msg;
1.31 markus 379: u_char *blob;
380: u_int blen;
1.28 markus 381: int type, flags = 0;
1.25 markus 382: int ret = -1;
383:
1.30 markus 384: if (key_to_blob(key, &blob, &blen) == 0)
1.25 markus 385: return -1;
386:
1.28 markus 387: if (datafellows & SSH_BUG_SIGBLOB)
388: flags = SSH_AGENT_OLD_SIGNATURE;
389:
1.25 markus 390: buffer_init(&msg);
391: buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
392: buffer_put_string(&msg, blob, blen);
393: buffer_put_string(&msg, data, datalen);
1.28 markus 394: buffer_put_int(&msg, flags);
1.25 markus 395: xfree(blob);
396:
397: if (ssh_request_reply(auth, &msg, &msg) == 0) {
398: buffer_free(&msg);
399: return -1;
400: }
401: type = buffer_get_char(&msg);
1.29 markus 402: if (agent_failed(type)) {
1.25 markus 403: log("Agent admitted failure to sign using the key.");
404: } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
405: fatal("Bad authentication response: %d", type);
406: } else {
407: ret = 0;
408: *sigp = buffer_get_string(&msg, lenp);
409: }
410: buffer_free(&msg);
411: return ret;
412: }
413:
1.22 markus 414: /* Encode key for a message to the agent. */
415:
416: void
1.30 markus 417: ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
1.22 markus 418: {
419: buffer_clear(b);
420: buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
421: buffer_put_int(b, BN_num_bits(key->n));
422: buffer_put_bignum(b, key->n);
423: buffer_put_bignum(b, key->e);
424: buffer_put_bignum(b, key->d);
425: /* To keep within the protocol: p < q for ssh. in SSL p > q */
426: buffer_put_bignum(b, key->iqmp); /* ssh key->u */
427: buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
428: buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
429: buffer_put_string(b, comment, strlen(comment));
430: }
431:
432: void
1.30 markus 433: ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
1.22 markus 434: {
435: buffer_clear(b);
436: buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
1.30 markus 437: buffer_put_cstring(b, key_ssh_name(key));
438: switch(key->type){
439: case KEY_RSA:
440: buffer_put_bignum2(b, key->rsa->n);
441: buffer_put_bignum2(b, key->rsa->e);
442: buffer_put_bignum2(b, key->rsa->d);
443: buffer_put_bignum2(b, key->rsa->iqmp);
444: buffer_put_bignum2(b, key->rsa->p);
445: buffer_put_bignum2(b, key->rsa->q);
446: break;
447: case KEY_DSA:
448: buffer_put_bignum2(b, key->dsa->p);
449: buffer_put_bignum2(b, key->dsa->q);
450: buffer_put_bignum2(b, key->dsa->g);
451: buffer_put_bignum2(b, key->dsa->pub_key);
452: buffer_put_bignum2(b, key->dsa->priv_key);
453: break;
454: }
455: buffer_put_cstring(b, comment);
1.22 markus 456: }
457:
1.14 markus 458: /*
459: * Adds an identity to the authentication server. This call is not meant to
460: * be used by normal applications.
461: */
1.1 deraadt 462:
1.18 markus 463: int
1.22 markus 464: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1 deraadt 465: {
1.25 markus 466: Buffer msg;
1.24 markus 467: int type;
1.12 markus 468:
1.25 markus 469: buffer_init(&msg);
1.22 markus 470:
471: switch (key->type) {
1.30 markus 472: case KEY_RSA1:
473: ssh_encode_identity_rsa1(&msg, key->rsa, comment);
474: break;
1.22 markus 475: case KEY_RSA:
476: case KEY_DSA:
1.30 markus 477: ssh_encode_identity_ssh2(&msg, key, comment);
1.22 markus 478: break;
479: default:
1.25 markus 480: buffer_free(&msg);
1.22 markus 481: return 0;
482: break;
483: }
1.25 markus 484: if (ssh_request_reply(auth, &msg, &msg) == 0) {
485: buffer_free(&msg);
1.12 markus 486: return 0;
487: }
1.25 markus 488: type = buffer_get_char(&msg);
489: buffer_free(&msg);
1.24 markus 490: return decode_reply(type);
1.12 markus 491: }
1.1 deraadt 492:
1.14 markus 493: /*
494: * Removes an identity from the authentication server. This call is not
495: * meant to be used by normal applications.
496: */
1.1 deraadt 497:
1.18 markus 498: int
1.25 markus 499: ssh_remove_identity(AuthenticationConnection *auth, Key *key)
1.1 deraadt 500: {
1.25 markus 501: Buffer msg;
1.24 markus 502: int type;
1.31 markus 503: u_char *blob;
504: u_int blen;
1.12 markus 505:
1.25 markus 506: buffer_init(&msg);
1.12 markus 507:
1.30 markus 508: if (key->type == KEY_RSA1) {
1.25 markus 509: buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
510: buffer_put_int(&msg, BN_num_bits(key->rsa->n));
511: buffer_put_bignum(&msg, key->rsa->e);
512: buffer_put_bignum(&msg, key->rsa->n);
1.30 markus 513: } else if (key->type == KEY_DSA || key->type == KEY_RSA) {
514: key_to_blob(key, &blob, &blen);
1.25 markus 515: buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
516: buffer_put_string(&msg, blob, blen);
517: xfree(blob);
518: } else {
519: buffer_free(&msg);
520: return 0;
521: }
522: if (ssh_request_reply(auth, &msg, &msg) == 0) {
523: buffer_free(&msg);
1.12 markus 524: return 0;
525: }
1.25 markus 526: type = buffer_get_char(&msg);
527: buffer_free(&msg);
1.24 markus 528: return decode_reply(type);
1.12 markus 529: }
530:
1.14 markus 531: /*
532: * Removes all identities from the agent. This call is not meant to be used
533: * by normal applications.
534: */
1.1 deraadt 535:
1.18 markus 536: int
1.25 markus 537: ssh_remove_all_identities(AuthenticationConnection *auth, int version)
1.1 deraadt 538: {
1.25 markus 539: Buffer msg;
1.24 markus 540: int type;
1.25 markus 541: int code = (version==1) ?
542: SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
543: SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
1.12 markus 544:
1.25 markus 545: buffer_init(&msg);
546: buffer_put_char(&msg, code);
1.12 markus 547:
1.25 markus 548: if (ssh_request_reply(auth, &msg, &msg) == 0) {
549: buffer_free(&msg);
1.12 markus 550: return 0;
551: }
1.25 markus 552: type = buffer_get_char(&msg);
553: buffer_free(&msg);
1.24 markus 554: return decode_reply(type);
1.21 markus 555: }
556:
1.35 stevesk 557: int
1.24 markus 558: decode_reply(int type)
1.21 markus 559: {
1.12 markus 560: switch (type) {
561: case SSH_AGENT_FAILURE:
1.29 markus 562: case SSH_COM_AGENT2_FAILURE:
1.24 markus 563: log("SSH_AGENT_FAILURE");
1.12 markus 564: return 0;
565: case SSH_AGENT_SUCCESS:
566: return 1;
567: default:
1.21 markus 568: fatal("Bad response from authentication agent: %d", type);
1.12 markus 569: }
570: /* NOTREACHED */
571: return 0;
572: }