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