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