Annotation of src/usr.bin/ssh/authfd.c, Revision 1.24
1.1 deraadt 1: /*
1.18 markus 2: *
1.13 deraadt 3: * authfd.c
1.18 markus 4: *
1.13 deraadt 5: * Author: Tatu Ylonen <ylo@cs.hut.fi>
1.18 markus 6: *
1.13 deraadt 7: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: * All rights reserved
1.18 markus 9: *
1.13 deraadt 10: * Created: Wed Mar 29 01:30:28 1995 ylo
1.18 markus 11: *
1.13 deraadt 12: * Functions for connecting the local authentication agent.
1.18 markus 13: *
1.13 deraadt 14: */
1.1 deraadt 15:
16: #include "includes.h"
1.24 ! markus 17: RCSID("$OpenBSD: authfd.c,v 1.23 2000/08/02 06:23:30 deraadt Exp $");
1.1 deraadt 18:
19: #include "ssh.h"
20: #include "rsa.h"
21: #include "buffer.h"
22: #include "bufaux.h"
23: #include "xmalloc.h"
24: #include "getput.h"
25:
1.17 markus 26: #include <openssl/rsa.h>
1.22 markus 27: #include <openssl/dsa.h>
28: #include <openssl/evp.h>
29: #include "key.h"
30: #include "authfd.h"
31: #include "kex.h"
1.2 provos 32:
1.21 markus 33: /* helper */
1.24 ! markus 34: int decode_reply(int type);
1.21 markus 35:
1.1 deraadt 36: /* Returns the number of the authentication fd, or -1 if there is none. */
37:
1.2 provos 38: int
1.8 markus 39: ssh_get_authentication_socket()
1.1 deraadt 40: {
1.12 markus 41: const char *authsocket;
1.23 deraadt 42: int sock, len;
1.12 markus 43: struct sockaddr_un sunaddr;
44:
45: authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
46: if (!authsocket)
47: return -1;
48:
49: sunaddr.sun_family = AF_UNIX;
50: strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
1.23 deraadt 51: sunaddr.sun_len = len = SUN_LEN(&sunaddr)+1;
1.12 markus 52:
53: sock = socket(AF_UNIX, SOCK_STREAM, 0);
54: if (sock < 0)
55: return -1;
56:
57: /* close on exec */
58: if (fcntl(sock, F_SETFD, 1) == -1) {
59: close(sock);
60: return -1;
61: }
1.23 deraadt 62: if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
1.12 markus 63: close(sock);
64: return -1;
65: }
66: return sock;
1.1 deraadt 67: }
68:
1.24 ! markus 69: int
! 70: ssh_request_reply(AuthenticationConnection *auth,
! 71: Buffer *request, Buffer *reply)
! 72: {
! 73: int l, len;
! 74: char buf[1024];
! 75:
! 76: /* Get the length of the message, and format it in the buffer. */
! 77: len = buffer_len(request);
! 78: PUT_32BIT(buf, len);
! 79:
! 80: /* Send the length and then the packet to the agent. */
! 81: if (atomicio(write, auth->fd, buf, 4) != 4 ||
! 82: atomicio(write, auth->fd, buffer_ptr(request),
! 83: buffer_len(request)) != buffer_len(request)) {
! 84: error("Error writing to authentication socket.");
! 85: return 0;
! 86: }
! 87: /*
! 88: * Wait for response from the agent. First read the length of the
! 89: * response packet.
! 90: */
! 91: len = 4;
! 92: while (len > 0) {
! 93: l = read(auth->fd, buf + 4 - len, len);
! 94: if (l <= 0) {
! 95: error("Error reading response length from authentication socket.");
! 96: return 0;
! 97: }
! 98: len -= l;
! 99: }
! 100:
! 101: /* Extract the length, and check it for sanity. */
! 102: len = GET_32BIT(buf);
! 103: if (len > 256 * 1024)
! 104: fatal("Authentication response too long: %d", len);
! 105:
! 106: /* Read the rest of the response in to the buffer. */
! 107: buffer_clear(reply);
! 108: while (len > 0) {
! 109: l = len;
! 110: if (l > sizeof(buf))
! 111: l = sizeof(buf);
! 112: l = read(auth->fd, buf, l);
! 113: if (l <= 0) {
! 114: error("Error reading response from authentication socket.");
! 115: return 0;
! 116: }
! 117: buffer_append(reply, (char *) buf, l);
! 118: len -= l;
! 119: }
! 120: return 1;
! 121: }
! 122:
1.14 markus 123: /*
124: * Closes the agent socket if it should be closed (depends on how it was
125: * obtained). The argument must have been returned by
126: * ssh_get_authentication_socket().
127: */
1.1 deraadt 128:
1.18 markus 129: void
1.12 markus 130: ssh_close_authentication_socket(int sock)
1.1 deraadt 131: {
1.12 markus 132: if (getenv(SSH_AUTHSOCKET_ENV_NAME))
133: close(sock);
1.1 deraadt 134: }
135:
1.14 markus 136: /*
137: * Opens and connects a private socket for communication with the
138: * authentication agent. Returns the file descriptor (which must be
139: * shut down and closed by the caller when no longer needed).
140: * Returns NULL if an error occurred and the connection could not be
141: * opened.
142: */
1.1 deraadt 143:
1.12 markus 144: AuthenticationConnection *
145: ssh_get_authentication_connection()
1.1 deraadt 146: {
1.12 markus 147: AuthenticationConnection *auth;
148: int sock;
1.1 deraadt 149:
1.12 markus 150: sock = ssh_get_authentication_socket();
151:
1.14 markus 152: /*
153: * Fail if we couldn't obtain a connection. This happens if we
154: * exited due to a timeout.
155: */
1.12 markus 156: if (sock < 0)
157: return NULL;
158:
159: auth = xmalloc(sizeof(*auth));
160: auth->fd = sock;
161: buffer_init(&auth->packet);
162: buffer_init(&auth->identities);
163: auth->howmany = 0;
164:
165: return auth;
1.1 deraadt 166: }
167:
1.14 markus 168: /*
169: * Closes the connection to the authentication agent and frees any associated
170: * memory.
171: */
1.1 deraadt 172:
1.18 markus 173: void
1.12 markus 174: ssh_close_authentication_connection(AuthenticationConnection *ac)
1.1 deraadt 175: {
1.12 markus 176: buffer_free(&ac->packet);
177: buffer_free(&ac->identities);
178: close(ac->fd);
179: xfree(ac);
1.1 deraadt 180: }
181:
1.14 markus 182: /*
183: * Returns the first authentication identity held by the agent.
184: * Returns true if an identity is available, 0 otherwise.
185: * The caller must initialize the integers before the call, and free the
186: * comment after a successful call (before calling ssh_get_next_identity).
187: */
1.1 deraadt 188:
1.2 provos 189: int
190: ssh_get_first_identity(AuthenticationConnection *auth,
1.24 ! markus 191: BIGNUM *e, BIGNUM *n, char **comment)
1.1 deraadt 192: {
1.24 ! markus 193: Buffer request;
! 194: int type;
1.1 deraadt 195:
1.14 markus 196: /*
197: * Send a message to the agent requesting for a list of the
198: * identities it can represent.
199: */
1.24 ! markus 200: buffer_init(&request);
! 201: buffer_put_char(&request, SSH_AGENTC_REQUEST_RSA_IDENTITIES);
1.12 markus 202:
203: buffer_clear(&auth->identities);
1.24 ! markus 204: if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
! 205: buffer_free(&request);
! 206: return 0;
1.12 markus 207: }
1.24 ! markus 208: buffer_free(&request);
1.1 deraadt 209:
1.12 markus 210: /* Get message type, and verify that we got a proper answer. */
1.24 ! markus 211: type = buffer_get_char(&auth->identities);
! 212: if (type != SSH_AGENT_RSA_IDENTITIES_ANSWER)
! 213: fatal("Bad authentication reply message type: %d", type);
1.12 markus 214:
215: /* Get the number of entries in the response and check it for sanity. */
216: auth->howmany = buffer_get_int(&auth->identities);
217: if (auth->howmany > 1024)
1.24 ! markus 218: fatal("Too many identities in authentication reply: %d\n",
! 219: auth->howmany);
1.12 markus 220:
221: /* Return the first entry (if any). */
222: return ssh_get_next_identity(auth, e, n, comment);
1.1 deraadt 223: }
224:
1.14 markus 225: /*
226: * Returns the next authentication identity for the agent. Other functions
227: * can be called between this and ssh_get_first_identity or two calls of this
228: * function. This returns 0 if there are no more identities. The caller
229: * must free comment after a successful return.
230: */
1.1 deraadt 231:
1.2 provos 232: int
233: ssh_get_next_identity(AuthenticationConnection *auth,
1.24 ! markus 234: BIGNUM *e, BIGNUM *n, char **comment)
1.1 deraadt 235: {
1.12 markus 236: unsigned int bits;
1.9 markus 237:
1.12 markus 238: /* Return failure if no more entries. */
239: if (auth->howmany <= 0)
240: return 0;
241:
1.14 markus 242: /*
243: * Get the next entry from the packet. These will abort with a fatal
244: * error if the packet is too short or contains corrupt data.
245: */
1.12 markus 246: bits = buffer_get_int(&auth->identities);
247: buffer_get_bignum(&auth->identities, e);
248: buffer_get_bignum(&auth->identities, n);
249: *comment = buffer_get_string(&auth->identities, NULL);
250:
251: if (bits != BN_num_bits(n))
1.19 markus 252: log("Warning: identity keysize mismatch: actual %d, announced %u",
253: BN_num_bits(n), bits);
1.9 markus 254:
1.12 markus 255: /* Decrement the number of remaining entries. */
256: auth->howmany--;
1.1 deraadt 257:
1.12 markus 258: return 1;
1.1 deraadt 259: }
260:
1.14 markus 261: /*
262: * Generates a random challenge, sends it to the agent, and waits for
263: * response from the agent. Returns true (non-zero) if the agent gave the
264: * correct answer, zero otherwise. Response type selects the style of
265: * response desired, with 0 corresponding to protocol version 1.0 (no longer
266: * supported) and 1 corresponding to protocol version 1.1.
267: */
1.1 deraadt 268:
1.2 provos 269: int
270: ssh_decrypt_challenge(AuthenticationConnection *auth,
1.24 ! markus 271: BIGNUM* e, BIGNUM *n, BIGNUM *challenge,
! 272: unsigned char session_id[16],
! 273: unsigned int response_type,
! 274: unsigned char response[16])
1.1 deraadt 275: {
1.12 markus 276: Buffer buffer;
1.24 ! markus 277: int success = 0;
! 278: int i;
! 279: int type;
1.12 markus 280:
281: if (response_type == 0)
1.24 ! markus 282: fatal("Compatibility with ssh protocol version "
! 283: "1.0 no longer supported.");
1.12 markus 284:
285: buffer_init(&buffer);
1.24 ! markus 286: buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
1.12 markus 287: buffer_put_int(&buffer, BN_num_bits(n));
288: buffer_put_bignum(&buffer, e);
289: buffer_put_bignum(&buffer, n);
290: buffer_put_bignum(&buffer, challenge);
291: buffer_append(&buffer, (char *) session_id, 16);
292: buffer_put_int(&buffer, response_type);
293:
1.24 ! markus 294: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12 markus 295: buffer_free(&buffer);
296: return 0;
297: }
1.24 ! markus 298: type = buffer_get_char(&buffer);
1.12 markus 299:
1.24 ! markus 300: if (type == SSH_AGENT_FAILURE) {
1.12 markus 301: log("Agent admitted failure to authenticate using the key.");
1.24 ! markus 302: } else if (type != SSH_AGENT_RSA_RESPONSE) {
! 303: fatal("Bad authentication response: %d", type);
! 304: } else {
! 305: success = 1;
! 306: /*
! 307: * Get the response from the packet. This will abort with a
! 308: * fatal error if the packet is corrupt.
! 309: */
! 310: for (i = 0; i < 16; i++)
! 311: response[i] = buffer_get_char(&buffer);
1.12 markus 312: }
313: buffer_free(&buffer);
1.24 ! markus 314: return success;
1.12 markus 315: }
1.1 deraadt 316:
1.22 markus 317: /* Encode key for a message to the agent. */
318:
319: void
320: ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
321: {
322: buffer_clear(b);
323: buffer_put_char(b, SSH_AGENTC_ADD_RSA_IDENTITY);
324: buffer_put_int(b, BN_num_bits(key->n));
325: buffer_put_bignum(b, key->n);
326: buffer_put_bignum(b, key->e);
327: buffer_put_bignum(b, key->d);
328: /* To keep within the protocol: p < q for ssh. in SSL p > q */
329: buffer_put_bignum(b, key->iqmp); /* ssh key->u */
330: buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
331: buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
332: buffer_put_string(b, comment, strlen(comment));
333: }
334:
335: void
336: ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
337: {
338: buffer_clear(b);
339: buffer_put_char(b, SSH2_AGENTC_ADD_IDENTITY);
340: buffer_put_cstring(b, KEX_DSS);
341: buffer_put_bignum2(b, key->p);
342: buffer_put_bignum2(b, key->q);
343: buffer_put_bignum2(b, key->g);
344: buffer_put_bignum2(b, key->pub_key);
345: buffer_put_bignum2(b, key->priv_key);
346: buffer_put_string(b, comment, strlen(comment));
347: }
348:
1.14 markus 349: /*
350: * Adds an identity to the authentication server. This call is not meant to
351: * be used by normal applications.
352: */
1.1 deraadt 353:
1.18 markus 354: int
1.22 markus 355: ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
1.1 deraadt 356: {
1.12 markus 357: Buffer buffer;
1.24 ! markus 358: int type;
1.12 markus 359:
360: buffer_init(&buffer);
1.22 markus 361:
362: switch (key->type) {
363: case KEY_RSA:
364: ssh_encode_identity_rsa(&buffer, key->rsa, comment);
365: break;
366: case KEY_DSA:
367: ssh_encode_identity_dsa(&buffer, key->dsa, comment);
368: break;
369: default:
370: buffer_free(&buffer);
371: return 0;
372: break;
373: }
1.24 ! markus 374: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12 markus 375: buffer_free(&buffer);
376: return 0;
377: }
1.24 ! markus 378: type = buffer_get_char(&buffer);
1.21 markus 379: buffer_free(&buffer);
1.24 ! markus 380: return decode_reply(type);
1.12 markus 381: }
1.1 deraadt 382:
1.14 markus 383: /*
384: * Removes an identity from the authentication server. This call is not
385: * meant to be used by normal applications.
386: */
1.1 deraadt 387:
1.18 markus 388: int
1.12 markus 389: ssh_remove_identity(AuthenticationConnection *auth, RSA *key)
1.1 deraadt 390: {
1.12 markus 391: Buffer buffer;
1.24 ! markus 392: int type;
1.12 markus 393:
394: buffer_init(&buffer);
395: buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY);
396: buffer_put_int(&buffer, BN_num_bits(key->n));
397: buffer_put_bignum(&buffer, key->e);
398: buffer_put_bignum(&buffer, key->n);
399:
1.24 ! markus 400: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
1.12 markus 401: buffer_free(&buffer);
402: return 0;
403: }
1.24 ! markus 404: type = buffer_get_char(&buffer);
1.21 markus 405: buffer_free(&buffer);
1.24 ! markus 406: return decode_reply(type);
1.12 markus 407: }
408:
1.14 markus 409: /*
410: * Removes all identities from the agent. This call is not meant to be used
411: * by normal applications.
412: */
1.1 deraadt 413:
1.18 markus 414: int
1.12 markus 415: ssh_remove_all_identities(AuthenticationConnection *auth)
1.1 deraadt 416: {
1.24 ! markus 417: Buffer buffer;
! 418: int type;
1.12 markus 419:
1.24 ! markus 420: buffer_init(&buffer);
! 421: buffer_put_char(&buffer, SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES);
1.12 markus 422:
1.24 ! markus 423: if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
! 424: buffer_free(&buffer);
1.12 markus 425: return 0;
426: }
1.24 ! markus 427: type = buffer_get_char(&buffer);
! 428: buffer_free(&buffer);
! 429: return decode_reply(type);
1.21 markus 430: }
431:
432: int
1.24 ! markus 433: decode_reply(int type)
1.21 markus 434: {
1.12 markus 435: switch (type) {
436: case SSH_AGENT_FAILURE:
1.24 ! markus 437: log("SSH_AGENT_FAILURE");
1.12 markus 438: return 0;
439: case SSH_AGENT_SUCCESS:
440: return 1;
441: default:
1.21 markus 442: fatal("Bad response from authentication agent: %d", type);
1.12 markus 443: }
444: /* NOTREACHED */
445: return 0;
446: }