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