Annotation of src/usr.bin/ssh/authfd.c, Revision 1.28
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.28 ! markus 38: RCSID("$OpenBSD: authfd.c,v 1.27 2000/09/07 20:27:49 deraadt 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.25 markus 53: #include "dsa.h"
1.28 ! markus 54: #include "compat.h"
1.2 provos 55:
1.21 markus 56: /* helper */
1.24 markus 57: int decode_reply(int type);
1.21 markus 58:
1.1 deraadt 59: /* Returns the number of the authentication fd, or -1 if there is none. */
60:
1.2 provos 61: int
1.8 markus 62: ssh_get_authentication_socket()
1.1 deraadt 63: {
1.12 markus 64: const char *authsocket;
1.23 deraadt 65: int sock, len;
1.12 markus 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));
1.23 deraadt 74: sunaddr.sun_len = len = SUN_LEN(&sunaddr)+1;
1.12 markus 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: }
1.23 deraadt 85: if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
1.12 markus 86: close(sock);
87: return -1;
88: }
89: return sock;
1.1 deraadt 90: }
91:
1.24 markus 92: int
1.25 markus 93: ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
1.24 markus 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:
1.14 markus 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: */
1.1 deraadt 150:
1.18 markus 151: void
1.12 markus 152: ssh_close_authentication_socket(int sock)
1.1 deraadt 153: {
1.12 markus 154: if (getenv(SSH_AUTHSOCKET_ENV_NAME))
155: close(sock);
1.1 deraadt 156: }
157:
1.14 markus 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: */
1.1 deraadt 165:
1.12 markus 166: AuthenticationConnection *
167: ssh_get_authentication_connection()
1.1 deraadt 168: {
1.12 markus 169: AuthenticationConnection *auth;
170: int sock;
1.1 deraadt 171:
1.12 markus 172: sock = ssh_get_authentication_socket();
173:
1.14 markus 174: /*
175: * Fail if we couldn't obtain a connection. This happens if we
176: * exited due to a timeout.
177: */
1.12 markus 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;
1.1 deraadt 187: }
188:
1.14 markus 189: /*
190: * Closes the connection to the authentication agent and frees any associated
191: * memory.
192: */
1.1 deraadt 193:
1.18 markus 194: void
1.25 markus 195: ssh_close_authentication_connection(AuthenticationConnection *auth)
1.1 deraadt 196: {
1.25 markus 197: buffer_free(&auth->identities);
198: close(auth->fd);
199: xfree(auth);
1.1 deraadt 200: }
201:
1.14 markus 202: /*
203: * Returns the first authentication identity held by the agent.
204: */
1.1 deraadt 205:
1.25 markus 206: Key *
207: ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
1.1 deraadt 208: {
1.25 markus 209: int type, code1 = 0, code2 = 0;
1.24 markus 210: Buffer request;
1.25 markus 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: }
1.1 deraadt 224:
1.14 markus 225: /*
226: * Send a message to the agent requesting for a list of the
227: * identities it can represent.
228: */
1.24 markus 229: buffer_init(&request);
1.25 markus 230: buffer_put_char(&request, code1);
1.12 markus 231:
232: buffer_clear(&auth->identities);
1.24 markus 233: if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
234: buffer_free(&request);
1.25 markus 235: return NULL;
1.12 markus 236: }
1.24 markus 237: buffer_free(&request);
1.1 deraadt 238:
1.12 markus 239: /* Get message type, and verify that we got a proper answer. */
1.24 markus 240: type = buffer_get_char(&auth->identities);
1.25 markus 241: if (type == SSH_AGENT_FAILURE) {
242: return NULL;
243: } else if (type != code2) {
1.24 markus 244: fatal("Bad authentication reply message type: %d", type);
1.25 markus 245: }
1.12 markus 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)
1.24 markus 250: fatal("Too many identities in authentication reply: %d\n",
251: auth->howmany);
1.12 markus 252:
253: /* Return the first entry (if any). */
1.25 markus 254: return ssh_get_next_identity(auth, comment, version);
1.1 deraadt 255: }
256:
1.25 markus 257: Key *
258: ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
1.1 deraadt 259: {
1.12 markus 260: unsigned int bits;
1.25 markus 261: unsigned char *blob;
262: unsigned int blen;
263: Key *key = NULL;
1.9 markus 264:
1.12 markus 265: /* Return failure if no more entries. */
266: if (auth->howmany <= 0)
1.25 markus 267: return NULL;
1.12 markus 268:
1.14 markus 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: */
1.25 markus 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: }
1.12 markus 294: /* Decrement the number of remaining entries. */
295: auth->howmany--;
1.25 markus 296: return key;
1.1 deraadt 297: }
298:
1.14 markus 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: */
1.1 deraadt 306:
1.2 provos 307: int
308: ssh_decrypt_challenge(AuthenticationConnection *auth,
1.25 markus 309: Key* key, BIGNUM *challenge,
1.24 markus 310: unsigned char session_id[16],
311: unsigned int response_type,
312: unsigned char response[16])
1.1 deraadt 313: {
1.12 markus 314: Buffer buffer;
1.24 markus 315: int success = 0;
316: int i;
317: int type;
1.12 markus 318:
1.25 markus 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: }
1.12 markus 325: buffer_init(&buffer);
1.24 markus 326: buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
1.25 markus 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);
1.12 markus 330: buffer_put_bignum(&buffer, challenge);
331: buffer_append(&buffer, (char *) session_id, 16);
332: buffer_put_int(&buffer, response_type);
333:
1.24 markus 334: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12 markus 335: buffer_free(&buffer);
336: return 0;
337: }
1.24 markus 338: type = buffer_get_char(&buffer);
1.12 markus 339:
1.24 markus 340: if (type == SSH_AGENT_FAILURE) {
1.12 markus 341: log("Agent admitted failure to authenticate using the key.");
1.24 markus 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);
1.12 markus 352: }
353: buffer_free(&buffer);
1.24 markus 354: return success;
1.12 markus 355: }
1.1 deraadt 356:
1.25 markus 357: /* ask agent to sign data, returns -1 on error, 0 on success */
358: int
359: ssh_agent_sign(AuthenticationConnection *auth,
360: Key *key,
361: unsigned char **sigp, int *lenp,
362: unsigned char *data, int datalen)
363: {
1.28 ! markus 364: extern int datafellows;
1.25 markus 365: Buffer msg;
366: unsigned char *blob;
367: unsigned int blen;
1.28 ! markus 368: int type, flags = 0;
1.25 markus 369: int ret = -1;
370:
371: if (dsa_make_key_blob(key, &blob, &blen) == 0)
372: return -1;
373:
1.28 ! markus 374: if (datafellows & SSH_BUG_SIGBLOB)
! 375: flags = SSH_AGENT_OLD_SIGNATURE;
! 376:
1.25 markus 377: buffer_init(&msg);
378: buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
379: buffer_put_string(&msg, blob, blen);
380: buffer_put_string(&msg, data, datalen);
1.28 ! markus 381: buffer_put_int(&msg, flags);
1.25 markus 382: xfree(blob);
383:
384: if (ssh_request_reply(auth, &msg, &msg) == 0) {
385: buffer_free(&msg);
386: return -1;
387: }
388: type = buffer_get_char(&msg);
389: if (type == SSH_AGENT_FAILURE) {
390: log("Agent admitted failure to sign using the key.");
391: } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
392: fatal("Bad authentication response: %d", type);
393: } else {
394: ret = 0;
395: *sigp = buffer_get_string(&msg, lenp);
396: }
397: buffer_free(&msg);
398: return ret;
399: }
400:
1.22 markus 401: /* Encode key for a message to the agent. */
402:
403: void
404: ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
405: {
406: buffer_clear(b);
407: buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
408: buffer_put_int(b, BN_num_bits(key->n));
409: buffer_put_bignum(b, key->n);
410: buffer_put_bignum(b, key->e);
411: buffer_put_bignum(b, key->d);
412: /* To keep within the protocol: p < q for ssh. in SSL p > q */
413: buffer_put_bignum(b, key->iqmp); /* ssh key->u */
414: buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
415: buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
416: buffer_put_string(b, comment, strlen(comment));
417: }
418:
419: void
420: ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
421: {
422: buffer_clear(b);
423: buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
424: buffer_put_cstring(b, KEX_DSS);
425: buffer_put_bignum2(b, key->p);
426: buffer_put_bignum2(b, key->q);
427: buffer_put_bignum2(b, key->g);
428: buffer_put_bignum2(b, key->pub_key);
429: buffer_put_bignum2(b, key->priv_key);
430: buffer_put_string(b, comment, strlen(comment));
431: }
432:
1.14 markus 433: /*
434: * Adds an identity to the authentication server. This call is not meant to
435: * be used by normal applications.
436: */
1.1 deraadt 437:
1.18 markus 438: int
1.22 markus 439: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1 deraadt 440: {
1.25 markus 441: Buffer msg;
1.24 markus 442: int type;
1.12 markus 443:
1.25 markus 444: buffer_init(&msg);
1.22 markus 445:
446: switch (key->type) {
447: case KEY_RSA:
1.25 markus 448: ssh_encode_identity_rsa(&msg, key->rsa, comment);
1.22 markus 449: break;
450: case KEY_DSA:
1.25 markus 451: ssh_encode_identity_dsa(&msg, key->dsa, comment);
1.22 markus 452: break;
453: default:
1.25 markus 454: buffer_free(&msg);
1.22 markus 455: return 0;
456: break;
457: }
1.25 markus 458: if (ssh_request_reply(auth, &msg, &msg) == 0) {
459: buffer_free(&msg);
1.12 markus 460: return 0;
461: }
1.25 markus 462: type = buffer_get_char(&msg);
463: buffer_free(&msg);
1.24 markus 464: return decode_reply(type);
1.12 markus 465: }
1.1 deraadt 466:
1.14 markus 467: /*
468: * Removes an identity from the authentication server. This call is not
469: * meant to be used by normal applications.
470: */
1.1 deraadt 471:
1.18 markus 472: int
1.25 markus 473: ssh_remove_identity(AuthenticationConnection *auth, Key *key)
1.1 deraadt 474: {
1.25 markus 475: Buffer msg;
1.24 markus 476: int type;
1.25 markus 477: unsigned char *blob;
478: unsigned int blen;
1.12 markus 479:
1.25 markus 480: buffer_init(&msg);
1.12 markus 481:
1.25 markus 482: if (key->type == KEY_RSA) {
483: buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
484: buffer_put_int(&msg, BN_num_bits(key->rsa->n));
485: buffer_put_bignum(&msg, key->rsa->e);
486: buffer_put_bignum(&msg, key->rsa->n);
487: } else if (key->type == KEY_DSA) {
488: dsa_make_key_blob(key, &blob, &blen);
489: buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
490: buffer_put_string(&msg, blob, blen);
491: xfree(blob);
492: } else {
493: buffer_free(&msg);
494: return 0;
495: }
496: if (ssh_request_reply(auth, &msg, &msg) == 0) {
497: buffer_free(&msg);
1.12 markus 498: return 0;
499: }
1.25 markus 500: type = buffer_get_char(&msg);
501: buffer_free(&msg);
1.24 markus 502: return decode_reply(type);
1.12 markus 503: }
504:
1.14 markus 505: /*
506: * Removes all identities from the agent. This call is not meant to be used
507: * by normal applications.
508: */
1.1 deraadt 509:
1.18 markus 510: int
1.25 markus 511: ssh_remove_all_identities(AuthenticationConnection *auth, int version)
1.1 deraadt 512: {
1.25 markus 513: Buffer msg;
1.24 markus 514: int type;
1.25 markus 515: int code = (version==1) ?
516: SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
517: SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
1.12 markus 518:
1.25 markus 519: buffer_init(&msg);
520: buffer_put_char(&msg, code);
1.12 markus 521:
1.25 markus 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.21 markus 529: }
530:
531: int
1.24 markus 532: decode_reply(int type)
1.21 markus 533: {
1.12 markus 534: switch (type) {
535: case SSH_AGENT_FAILURE:
1.24 markus 536: log("SSH_AGENT_FAILURE");
1.12 markus 537: return 0;
538: case SSH_AGENT_SUCCESS:
539: return 1;
540: default:
1.21 markus 541: fatal("Bad response from authentication agent: %d", type);
1.12 markus 542: }
543: /* NOTREACHED */
544: return 0;
545: }