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