5 * Author: Tatu Ylonen <ylo@cs.hut.fi>
7 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
10 * Created: Wed Mar 29 01:30:28 1995 ylo
12 * Functions for connecting the local authentication agent.
28 #include <openssl/rsa.h>
34 /* Returns the number of the authentication fd, or -1 if there is none. */
37 ssh_get_authentication_socket()
39 const char *authsocket;
41 struct sockaddr_un sunaddr;
43 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
47 sunaddr.sun_family = AF_UNIX;
48 strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
50 sock = socket(AF_UNIX, SOCK_STREAM, 0);
55 if (fcntl(sock, F_SETFD, 1) == -1) {
59 if (connect(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) {
67 * Closes the agent socket if it should be closed (depends on how it was
68 * obtained). The argument must have been returned by
69 * ssh_get_authentication_socket().
73 ssh_close_authentication_socket(int sock)
75 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
80 * Opens and connects a private socket for communication with the
81 * authentication agent. Returns the file descriptor (which must be
82 * shut down and closed by the caller when no longer needed).
83 * Returns NULL if an error occurred and the connection could not be
87 AuthenticationConnection *
88 ssh_get_authentication_connection()
90 AuthenticationConnection *auth;
93 sock = ssh_get_authentication_socket();
96 * Fail if we couldn't obtain a connection. This happens if we
97 * exited due to a timeout.
102 auth = xmalloc(sizeof(*auth));
104 buffer_init(&auth->packet);
105 buffer_init(&auth->identities);
112 * Closes the connection to the authentication agent and frees any associated
117 ssh_close_authentication_connection(AuthenticationConnection *ac)
119 buffer_free(&ac->packet);
120 buffer_free(&ac->identities);
126 * Returns the first authentication identity held by the agent.
127 * Returns true if an identity is available, 0 otherwise.
128 * The caller must initialize the integers before the call, and free the
129 * comment after a successful call (before calling ssh_get_next_identity).
133 ssh_get_first_identity(AuthenticationConnection *auth,
134 BIGNUM *e, BIGNUM *n, char **comment)
136 unsigned char msg[8192];
140 * Send a message to the agent requesting for a list of the
141 * identities it can represent.
147 msg[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
148 if (write(auth->fd, msg, 5) != 5) {
149 error("write auth->fd: %.100s", strerror(errno));
152 /* Read the length of the response. XXX implement timeouts here. */
155 l = read(auth->fd, msg + 4 - len, len);
157 error("read auth->fd: %.100s", strerror(errno));
164 * Extract the length, and check it for sanity. (We cannot trust
165 * authentication agents).
167 len = GET_32BIT(msg);
168 if (len < 1 || len > 256 * 1024)
169 fatal("Authentication reply message too long: %d\n", len);
171 /* Read the packet itself. */
172 buffer_clear(&auth->identities);
177 l = read(auth->fd, msg, l);
179 fatal("Incomplete authentication reply.");
180 buffer_append(&auth->identities, (char *) msg, l);
184 /* Get message type, and verify that we got a proper answer. */
185 buffer_get(&auth->identities, (char *) msg, 1);
186 if (msg[0] != SSH_AGENT_RSA_IDENTITIES_ANSWER)
187 fatal("Bad authentication reply message type: %d", msg[0]);
189 /* Get the number of entries in the response and check it for sanity. */
190 auth->howmany = buffer_get_int(&auth->identities);
191 if (auth->howmany > 1024)
192 fatal("Too many identities in authentication reply: %d\n", auth->howmany);
194 /* Return the first entry (if any). */
195 return ssh_get_next_identity(auth, e, n, comment);
199 * Returns the next authentication identity for the agent. Other functions
200 * can be called between this and ssh_get_first_identity or two calls of this
201 * function. This returns 0 if there are no more identities. The caller
202 * must free comment after a successful return.
206 ssh_get_next_identity(AuthenticationConnection *auth,
207 BIGNUM *e, BIGNUM *n, char **comment)
211 /* Return failure if no more entries. */
212 if (auth->howmany <= 0)
216 * Get the next entry from the packet. These will abort with a fatal
217 * error if the packet is too short or contains corrupt data.
219 bits = buffer_get_int(&auth->identities);
220 buffer_get_bignum(&auth->identities, e);
221 buffer_get_bignum(&auth->identities, n);
222 *comment = buffer_get_string(&auth->identities, NULL);
224 if (bits != BN_num_bits(n))
225 error("Warning: keysize mismatch: actual %d, announced %u",
226 BN_num_bits(n), bits);
228 /* Decrement the number of remaining entries. */
235 * Generates a random challenge, sends it to the agent, and waits for
236 * response from the agent. Returns true (non-zero) if the agent gave the
237 * correct answer, zero otherwise. Response type selects the style of
238 * response desired, with 0 corresponding to protocol version 1.0 (no longer
239 * supported) and 1 corresponding to protocol version 1.1.
243 ssh_decrypt_challenge(AuthenticationConnection *auth,
244 BIGNUM* e, BIGNUM *n, BIGNUM *challenge,
245 unsigned char session_id[16],
246 unsigned int response_type,
247 unsigned char response[16])
250 unsigned char buf[8192];
253 /* Response type 0 is no longer supported. */
254 if (response_type == 0)
255 fatal("Compatibility with ssh protocol version 1.0 no longer supported.");
257 /* Format a message to the agent. */
258 buf[0] = SSH_AGENTC_RSA_CHALLENGE;
259 buffer_init(&buffer);
260 buffer_append(&buffer, (char *) buf, 1);
261 buffer_put_int(&buffer, BN_num_bits(n));
262 buffer_put_bignum(&buffer, e);
263 buffer_put_bignum(&buffer, n);
264 buffer_put_bignum(&buffer, challenge);
265 buffer_append(&buffer, (char *) session_id, 16);
266 buffer_put_int(&buffer, response_type);
268 /* Get the length of the message, and format it in the buffer. */
269 len = buffer_len(&buffer);
272 /* Send the length and then the packet to the agent. */
273 if (write(auth->fd, buf, 4) != 4 ||
274 write(auth->fd, buffer_ptr(&buffer), buffer_len(&buffer)) !=
275 buffer_len(&buffer)) {
276 error("Error writing to authentication socket.");
278 buffer_free(&buffer);
282 * Wait for response from the agent. First read the length of the
287 l = read(auth->fd, buf + 4 - len, len);
289 error("Error reading response length from authentication socket.");
295 /* Extract the length, and check it for sanity. */
296 len = GET_32BIT(buf);
297 if (len > 256 * 1024)
298 fatal("Authentication response too long: %d", len);
300 /* Read the rest of the response in tothe buffer. */
301 buffer_clear(&buffer);
306 l = read(auth->fd, buf, l);
308 error("Error reading response from authentication socket.");
311 buffer_append(&buffer, (char *) buf, l);
315 /* Get the type of the packet. */
316 buffer_get(&buffer, (char *) buf, 1);
318 /* Check for agent failure message. */
319 if (buf[0] == SSH_AGENT_FAILURE) {
320 log("Agent admitted failure to authenticate using the key.");
323 /* Now it must be an authentication response packet. */
324 if (buf[0] != SSH_AGENT_RSA_RESPONSE)
325 fatal("Bad authentication response: %d", buf[0]);
328 * Get the response from the packet. This will abort with a fatal
329 * error if the packet is corrupt.
331 for (i = 0; i < 16; i++)
332 response[i] = buffer_get_char(&buffer);
334 /* The buffer containing the packet is no longer needed. */
335 buffer_free(&buffer);
337 /* Correct answer. */
342 * Adds an identity to the authentication server. This call is not meant to
343 * be used by normal applications.
347 ssh_add_identity(AuthenticationConnection *auth,
348 RSA * key, const char *comment)
351 unsigned char buf[8192];
354 /* Format a message to the agent. */
355 buffer_init(&buffer);
356 buffer_put_char(&buffer, SSH_AGENTC_ADD_RSA_IDENTITY);
357 buffer_put_int(&buffer, BN_num_bits(key->n));
358 buffer_put_bignum(&buffer, key->n);
359 buffer_put_bignum(&buffer, key->e);
360 buffer_put_bignum(&buffer, key->d);
361 /* To keep within the protocol: p < q for ssh. in SSL p > q */
362 buffer_put_bignum(&buffer, key->iqmp); /* ssh key->u */
363 buffer_put_bignum(&buffer, key->q); /* ssh key->p, SSL key->q */
364 buffer_put_bignum(&buffer, key->p); /* ssh key->q, SSL key->p */
365 buffer_put_string(&buffer, comment, strlen(comment));
367 /* Get the length of the message, and format it in the buffer. */
368 len = buffer_len(&buffer);
371 /* Send the length and then the packet to the agent. */
372 if (write(auth->fd, buf, 4) != 4 ||
373 write(auth->fd, buffer_ptr(&buffer), buffer_len(&buffer)) !=
374 buffer_len(&buffer)) {
375 error("Error writing to authentication socket.");
377 buffer_free(&buffer);
380 /* Wait for response from the agent. First read the length of the
384 l = read(auth->fd, buf + 4 - len, len);
386 error("Error reading response length from authentication socket.");
392 /* Extract the length, and check it for sanity. */
393 len = GET_32BIT(buf);
394 if (len > 256 * 1024)
395 fatal("Add identity response too long: %d", len);
397 /* Read the rest of the response in tothe buffer. */
398 buffer_clear(&buffer);
403 l = read(auth->fd, buf, l);
405 error("Error reading response from authentication socket.");
408 buffer_append(&buffer, (char *) buf, l);
412 /* Get the type of the packet. */
413 type = buffer_get_char(&buffer);
415 case SSH_AGENT_FAILURE:
416 buffer_free(&buffer);
418 case SSH_AGENT_SUCCESS:
419 buffer_free(&buffer);
422 fatal("Bad response to add identity from authentication agent: %d",
430 * Removes an identity from the authentication server. This call is not
431 * meant to be used by normal applications.
435 ssh_remove_identity(AuthenticationConnection *auth, RSA *key)
438 unsigned char buf[8192];
441 /* Format a message to the agent. */
442 buffer_init(&buffer);
443 buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY);
444 buffer_put_int(&buffer, BN_num_bits(key->n));
445 buffer_put_bignum(&buffer, key->e);
446 buffer_put_bignum(&buffer, key->n);
448 /* Get the length of the message, and format it in the buffer. */
449 len = buffer_len(&buffer);
452 /* Send the length and then the packet to the agent. */
453 if (write(auth->fd, buf, 4) != 4 ||
454 write(auth->fd, buffer_ptr(&buffer), buffer_len(&buffer)) !=
455 buffer_len(&buffer)) {
456 error("Error writing to authentication socket.");
458 buffer_free(&buffer);
462 * Wait for response from the agent. First read the length of the
467 l = read(auth->fd, buf + 4 - len, len);
469 error("Error reading response length from authentication socket.");
475 /* Extract the length, and check it for sanity. */
476 len = GET_32BIT(buf);
477 if (len > 256 * 1024)
478 fatal("Remove identity response too long: %d", len);
480 /* Read the rest of the response in tothe buffer. */
481 buffer_clear(&buffer);
486 l = read(auth->fd, buf, l);
488 error("Error reading response from authentication socket.");
491 buffer_append(&buffer, (char *) buf, l);
495 /* Get the type of the packet. */
496 type = buffer_get_char(&buffer);
498 case SSH_AGENT_FAILURE:
499 buffer_free(&buffer);
501 case SSH_AGENT_SUCCESS:
502 buffer_free(&buffer);
505 fatal("Bad response to remove identity from authentication agent: %d",
513 * Removes all identities from the agent. This call is not meant to be used
514 * by normal applications.
518 ssh_remove_all_identities(AuthenticationConnection *auth)
521 unsigned char buf[8192];
524 /* Get the length of the message, and format it in the buffer. */
526 buf[4] = SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES;
528 /* Send the length and then the packet to the agent. */
529 if (write(auth->fd, buf, 5) != 5) {
530 error("Error writing to authentication socket.");
534 * Wait for response from the agent. First read the length of the
539 l = read(auth->fd, buf + 4 - len, len);
541 error("Error reading response length from authentication socket.");
547 /* Extract the length, and check it for sanity. */
548 len = GET_32BIT(buf);
549 if (len > 256 * 1024)
550 fatal("Remove identity response too long: %d", len);
552 /* Read the rest of the response into the buffer. */
553 buffer_init(&buffer);
558 l = read(auth->fd, buf, l);
560 error("Error reading response from authentication socket.");
561 buffer_free(&buffer);
564 buffer_append(&buffer, (char *) buf, l);
568 /* Get the type of the packet. */
569 type = buffer_get_char(&buffer);
571 case SSH_AGENT_FAILURE:
572 buffer_free(&buffer);
574 case SSH_AGENT_SUCCESS:
575 buffer_free(&buffer);
578 fatal("Bad response to remove identity from authentication agent: %d",