]> andersk Git - openssh.git/blob - authfd.c
d9427d3776acce1e1d36d8729124bf76c1ed0f43
[openssh.git] / authfd.c
1 /*
2  *
3  * authfd.c
4  *
5  * Author: Tatu Ylonen <ylo@cs.hut.fi>
6  *
7  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8  *                    All rights reserved
9  *
10  * Created: Wed Mar 29 01:30:28 1995 ylo
11  *
12  * Functions for connecting the local authentication agent.
13  *
14  * SSH2 implementation,
15  * Copyright (c) 2000 Markus Friedl. All rights reserved.
16  *
17  */
18
19 #include "includes.h"
20 RCSID("$OpenBSD: authfd.c,v 1.25 2000/08/19 21:34:42 markus Exp $");
21
22 #include "ssh.h"
23 #include "rsa.h"
24 #include "buffer.h"
25 #include "bufaux.h"
26 #include "xmalloc.h"
27 #include "getput.h"
28
29 #include <openssl/rsa.h>
30 #include <openssl/dsa.h>
31 #include <openssl/evp.h>
32 #include "key.h"
33 #include "authfd.h"
34 #include "kex.h"
35 #include "dsa.h"
36
37 /* helper */
38 int     decode_reply(int type);
39
40 /* Returns the number of the authentication fd, or -1 if there is none. */
41
42 int
43 ssh_get_authentication_socket()
44 {
45         const char *authsocket;
46         int sock, len;
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));
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 */
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         }
70         if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
71                 close(sock);
72                 return -1;
73         }
74         return sock;
75 }
76
77 int
78 ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
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
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  */
135
136 void
137 ssh_close_authentication_socket(int sock)
138 {
139         if (getenv(SSH_AUTHSOCKET_ENV_NAME))
140                 close(sock);
141 }
142
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  */
150
151 AuthenticationConnection *
152 ssh_get_authentication_connection()
153 {
154         AuthenticationConnection *auth;
155         int sock;
156
157         sock = ssh_get_authentication_socket();
158
159         /*
160          * Fail if we couldn't obtain a connection.  This happens if we
161          * exited due to a timeout.
162          */
163         if (sock < 0)
164                 return NULL;
165
166         auth = xmalloc(sizeof(*auth));
167         auth->fd = sock;
168         buffer_init(&auth->identities);
169         auth->howmany = 0;
170
171         return auth;
172 }
173
174 /*
175  * Closes the connection to the authentication agent and frees any associated
176  * memory.
177  */
178
179 void
180 ssh_close_authentication_connection(AuthenticationConnection *auth)
181 {
182         buffer_free(&auth->identities);
183         close(auth->fd);
184         xfree(auth);
185 }
186
187 /*
188  * Returns the first authentication identity held by the agent.
189  */
190
191 Key *
192 ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
193 {
194         int type, code1 = 0, code2 = 0;
195         Buffer request;
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         }
209
210         /*
211          * Send a message to the agent requesting for a list of the
212          * identities it can represent.
213          */
214         buffer_init(&request);
215         buffer_put_char(&request, code1);
216
217         buffer_clear(&auth->identities);
218         if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
219                 buffer_free(&request);
220                 return NULL;
221         }
222         buffer_free(&request);
223
224         /* Get message type, and verify that we got a proper answer. */
225         type = buffer_get_char(&auth->identities);
226         if (type == SSH_AGENT_FAILURE) {
227                 return NULL;
228         } else if (type != code2) {
229                 fatal("Bad authentication reply message type: %d", type);
230         }
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)
235                 fatal("Too many identities in authentication reply: %d\n",
236                     auth->howmany);
237
238         /* Return the first entry (if any). */
239         return ssh_get_next_identity(auth, comment, version);
240 }
241
242 Key *
243 ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
244 {
245         unsigned int bits;
246         unsigned char *blob;
247         unsigned int blen;
248         Key *key = NULL;
249
250         /* Return failure if no more entries. */
251         if (auth->howmany <= 0)
252                 return NULL;
253
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          */
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         }
279         /* Decrement the number of remaining entries. */
280         auth->howmany--;
281         return key;
282 }
283
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  */
291
292 int
293 ssh_decrypt_challenge(AuthenticationConnection *auth,
294     Key* key, BIGNUM *challenge,
295     unsigned char session_id[16],
296     unsigned int response_type,
297     unsigned char response[16])
298 {
299         Buffer buffer;
300         int success = 0;
301         int i;
302         int type;
303
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         }
310         buffer_init(&buffer);
311         buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
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);
315         buffer_put_bignum(&buffer, challenge);
316         buffer_append(&buffer, (char *) session_id, 16);
317         buffer_put_int(&buffer, response_type);
318
319         if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
320                 buffer_free(&buffer);
321                 return 0;
322         }
323         type = buffer_get_char(&buffer);
324
325         if (type == SSH_AGENT_FAILURE) {
326                 log("Agent admitted failure to authenticate using the key.");
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);
337         }
338         buffer_free(&buffer);
339         return success;
340 }
341
342 /* ask agent to sign data, returns -1 on error, 0 on success */
343 int
344 ssh_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
381 /* Encode key for a message to the agent. */
382
383 void
384 ssh_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
399 void
400 ssh_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
413 /*
414  * Adds an identity to the authentication server.  This call is not meant to
415  * be used by normal applications.
416  */
417
418 int
419 ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
420 {
421         Buffer msg;
422         int type;
423
424         buffer_init(&msg);
425
426         switch (key->type) {
427         case KEY_RSA:
428                 ssh_encode_identity_rsa(&msg, key->rsa, comment);
429                 break;
430         case KEY_DSA:
431                 ssh_encode_identity_dsa(&msg, key->dsa, comment);
432                 break;
433         default:
434                 buffer_free(&msg);
435                 return 0;
436                 break;
437         }
438         if (ssh_request_reply(auth, &msg, &msg) == 0) {
439                 buffer_free(&msg);
440                 return 0;
441         }
442         type = buffer_get_char(&msg);
443         buffer_free(&msg);
444         return decode_reply(type);
445 }
446
447 /*
448  * Removes an identity from the authentication server.  This call is not
449  * meant to be used by normal applications.
450  */
451
452 int
453 ssh_remove_identity(AuthenticationConnection *auth, Key *key)
454 {
455         Buffer msg;
456         int type;
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);
474                 return 0;
475         }
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);
482         return decode_reply(type);
483 }
484
485 /*
486  * Removes all identities from the agent.  This call is not meant to be used
487  * by normal applications.
488  */
489
490 int
491 ssh_remove_all_identities(AuthenticationConnection *auth, int version)
492 {
493         Buffer msg;
494         int type;
495         int code = (version==1) ?
496                 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
497                 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
498
499         buffer_init(&msg);
500         buffer_put_char(&msg, code);
501
502         if (ssh_request_reply(auth, &msg, &msg) == 0) {
503                 buffer_free(&msg);
504                 return 0;
505         }
506         type = buffer_get_char(&msg);
507         buffer_free(&msg);
508         return decode_reply(type);
509 }
510
511 int 
512 decode_reply(int type)
513 {
514         switch (type) {
515         case SSH_AGENT_FAILURE:
516                 log("SSH_AGENT_FAILURE");
517                 return 0;
518         case SSH_AGENT_SUCCESS:
519                 return 1;
520         default:
521                 fatal("Bad response from authentication agent: %d", type);
522         }
523         /* NOTREACHED */
524         return 0;
525 }
This page took 0.0644 seconds and 3 git commands to generate.