10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
30Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
40Sstevel@tonic-gate * All rights reserved
50Sstevel@tonic-gate * Functions for connecting the local authentication agent.
60Sstevel@tonic-gate *
70Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
80Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
90Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
100Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
110Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * SSH2 implementation,
140Sstevel@tonic-gate * Copyright (c) 2000 Markus Friedl. All rights reserved.
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
170Sstevel@tonic-gate * modification, are permitted provided that the following conditions
180Sstevel@tonic-gate * are met:
190Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
200Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
210Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
220Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
230Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
240Sstevel@tonic-gate *
250Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
260Sstevel@tonic-gate * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
270Sstevel@tonic-gate * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
280Sstevel@tonic-gate * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
290Sstevel@tonic-gate * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
300Sstevel@tonic-gate * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
310Sstevel@tonic-gate * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
320Sstevel@tonic-gate * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
330Sstevel@tonic-gate * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
340Sstevel@tonic-gate * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
350Sstevel@tonic-gate */
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include "includes.h"
380Sstevel@tonic-gate RCSID("$OpenBSD: authfd.c,v 1.57 2002/09/11 18:27:26 stevesk Exp $");
390Sstevel@tonic-gate
400Sstevel@tonic-gate #include <openssl/evp.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include "ssh.h"
430Sstevel@tonic-gate #include "rsa.h"
440Sstevel@tonic-gate #include "buffer.h"
450Sstevel@tonic-gate #include "bufaux.h"
460Sstevel@tonic-gate #include "xmalloc.h"
470Sstevel@tonic-gate #include "getput.h"
480Sstevel@tonic-gate #include "key.h"
490Sstevel@tonic-gate #include "authfd.h"
500Sstevel@tonic-gate #include "cipher.h"
510Sstevel@tonic-gate #include "kex.h"
520Sstevel@tonic-gate #include "compat.h"
530Sstevel@tonic-gate #include "log.h"
540Sstevel@tonic-gate #include "atomicio.h"
550Sstevel@tonic-gate
560Sstevel@tonic-gate static int agent_present = 0;
570Sstevel@tonic-gate
580Sstevel@tonic-gate /* helper */
590Sstevel@tonic-gate int decode_reply(int type);
600Sstevel@tonic-gate
610Sstevel@tonic-gate /* macro to check for "agent failure" message */
620Sstevel@tonic-gate #define agent_failed(x) \
630Sstevel@tonic-gate ((x == SSH_AGENT_FAILURE) || (x == SSH_COM_AGENT2_FAILURE) || \
640Sstevel@tonic-gate (x == SSH2_AGENT_FAILURE))
650Sstevel@tonic-gate
660Sstevel@tonic-gate int
ssh_agent_present(void)670Sstevel@tonic-gate ssh_agent_present(void)
680Sstevel@tonic-gate {
690Sstevel@tonic-gate int authfd;
700Sstevel@tonic-gate
710Sstevel@tonic-gate if (agent_present)
720Sstevel@tonic-gate return 1;
730Sstevel@tonic-gate if ((authfd = ssh_get_authentication_socket()) == -1)
740Sstevel@tonic-gate return 0;
750Sstevel@tonic-gate else {
760Sstevel@tonic-gate ssh_close_authentication_socket(authfd);
770Sstevel@tonic-gate return 1;
780Sstevel@tonic-gate }
790Sstevel@tonic-gate }
800Sstevel@tonic-gate
810Sstevel@tonic-gate /* Returns the number of the authentication fd, or -1 if there is none. */
820Sstevel@tonic-gate
830Sstevel@tonic-gate int
ssh_get_authentication_socket(void)840Sstevel@tonic-gate ssh_get_authentication_socket(void)
850Sstevel@tonic-gate {
860Sstevel@tonic-gate const char *authsocket;
870Sstevel@tonic-gate int sock;
880Sstevel@tonic-gate struct sockaddr_un sunaddr;
890Sstevel@tonic-gate
900Sstevel@tonic-gate authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
910Sstevel@tonic-gate if (!authsocket)
920Sstevel@tonic-gate return -1;
930Sstevel@tonic-gate
940Sstevel@tonic-gate sunaddr.sun_family = AF_UNIX;
950Sstevel@tonic-gate strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path));
960Sstevel@tonic-gate
970Sstevel@tonic-gate sock = socket(AF_UNIX, SOCK_STREAM, 0);
980Sstevel@tonic-gate if (sock < 0)
990Sstevel@tonic-gate return -1;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /* close on exec */
102*9845SJan.Pechanec@Sun.COM if (fcntl(sock, F_SETFD, FD_CLOEXEC) == -1) {
1030Sstevel@tonic-gate close(sock);
1040Sstevel@tonic-gate return -1;
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate if (connect(sock, (struct sockaddr *) &sunaddr, sizeof sunaddr) < 0) {
1070Sstevel@tonic-gate close(sock);
1080Sstevel@tonic-gate return -1;
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate agent_present = 1;
1110Sstevel@tonic-gate return sock;
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate static int
ssh_request_reply(AuthenticationConnection * auth,Buffer * request,Buffer * reply)1150Sstevel@tonic-gate ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
1160Sstevel@tonic-gate {
1170Sstevel@tonic-gate int l, len;
1180Sstevel@tonic-gate char buf[1024];
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate /* Get the length of the message, and format it in the buffer. */
1210Sstevel@tonic-gate len = buffer_len(request);
1220Sstevel@tonic-gate PUT_32BIT(buf, len);
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate /* Send the length and then the packet to the agent. */
1250Sstevel@tonic-gate if (atomicio(write, auth->fd, buf, 4) != 4 ||
1260Sstevel@tonic-gate atomicio(write, auth->fd, buffer_ptr(request),
1270Sstevel@tonic-gate buffer_len(request)) != buffer_len(request)) {
1280Sstevel@tonic-gate error("Error writing to authentication socket.");
1290Sstevel@tonic-gate return 0;
1300Sstevel@tonic-gate }
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate * Wait for response from the agent. First read the length of the
1330Sstevel@tonic-gate * response packet.
1340Sstevel@tonic-gate */
1350Sstevel@tonic-gate len = 4;
1360Sstevel@tonic-gate while (len > 0) {
1370Sstevel@tonic-gate l = read(auth->fd, buf + 4 - len, len);
1380Sstevel@tonic-gate if (l == -1 && (errno == EAGAIN || errno == EINTR))
1390Sstevel@tonic-gate continue;
1400Sstevel@tonic-gate if (l <= 0) {
1410Sstevel@tonic-gate error("Error reading response length from authentication socket.");
1420Sstevel@tonic-gate return 0;
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate len -= l;
1450Sstevel@tonic-gate }
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate /* Extract the length, and check it for sanity. */
1480Sstevel@tonic-gate len = GET_32BIT(buf);
1490Sstevel@tonic-gate if (len > 256 * 1024)
1500Sstevel@tonic-gate fatal("Authentication response too long: %d", len);
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /* Read the rest of the response in to the buffer. */
1530Sstevel@tonic-gate buffer_clear(reply);
1540Sstevel@tonic-gate while (len > 0) {
1550Sstevel@tonic-gate l = len;
1560Sstevel@tonic-gate if (l > sizeof(buf))
1570Sstevel@tonic-gate l = sizeof(buf);
1580Sstevel@tonic-gate l = read(auth->fd, buf, l);
1590Sstevel@tonic-gate if (l == -1 && (errno == EAGAIN || errno == EINTR))
1600Sstevel@tonic-gate continue;
1610Sstevel@tonic-gate if (l <= 0) {
1620Sstevel@tonic-gate error("Error reading response from authentication socket.");
1630Sstevel@tonic-gate return 0;
1640Sstevel@tonic-gate }
1650Sstevel@tonic-gate buffer_append(reply, buf, l);
1660Sstevel@tonic-gate len -= l;
1670Sstevel@tonic-gate }
1680Sstevel@tonic-gate return 1;
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /*
1720Sstevel@tonic-gate * Closes the agent socket if it should be closed (depends on how it was
1730Sstevel@tonic-gate * obtained). The argument must have been returned by
1740Sstevel@tonic-gate * ssh_get_authentication_socket().
1750Sstevel@tonic-gate */
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate void
ssh_close_authentication_socket(int sock)1780Sstevel@tonic-gate ssh_close_authentication_socket(int sock)
1790Sstevel@tonic-gate {
1800Sstevel@tonic-gate if (getenv(SSH_AUTHSOCKET_ENV_NAME))
1810Sstevel@tonic-gate close(sock);
1820Sstevel@tonic-gate }
1830Sstevel@tonic-gate
1840Sstevel@tonic-gate /*
1850Sstevel@tonic-gate * Opens and connects a private socket for communication with the
1860Sstevel@tonic-gate * authentication agent. Returns the file descriptor (which must be
1870Sstevel@tonic-gate * shut down and closed by the caller when no longer needed).
1880Sstevel@tonic-gate * Returns NULL if an error occurred and the connection could not be
1890Sstevel@tonic-gate * opened.
1900Sstevel@tonic-gate */
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate AuthenticationConnection *
ssh_get_authentication_connection(void)1930Sstevel@tonic-gate ssh_get_authentication_connection(void)
1940Sstevel@tonic-gate {
1950Sstevel@tonic-gate AuthenticationConnection *auth;
1960Sstevel@tonic-gate int sock;
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate sock = ssh_get_authentication_socket();
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate /*
2010Sstevel@tonic-gate * Fail if we couldn't obtain a connection. This happens if we
2020Sstevel@tonic-gate * exited due to a timeout.
2030Sstevel@tonic-gate */
2040Sstevel@tonic-gate if (sock < 0)
2050Sstevel@tonic-gate return NULL;
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate auth = xmalloc(sizeof(*auth));
2080Sstevel@tonic-gate auth->fd = sock;
2090Sstevel@tonic-gate buffer_init(&auth->identities);
2100Sstevel@tonic-gate auth->howmany = 0;
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate return auth;
2130Sstevel@tonic-gate }
2140Sstevel@tonic-gate
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate * Closes the connection to the authentication agent and frees any associated
2170Sstevel@tonic-gate * memory.
2180Sstevel@tonic-gate */
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate void
ssh_close_authentication_connection(AuthenticationConnection * auth)2210Sstevel@tonic-gate ssh_close_authentication_connection(AuthenticationConnection *auth)
2220Sstevel@tonic-gate {
2230Sstevel@tonic-gate buffer_free(&auth->identities);
2240Sstevel@tonic-gate close(auth->fd);
2250Sstevel@tonic-gate xfree(auth);
2260Sstevel@tonic-gate }
2270Sstevel@tonic-gate
2280Sstevel@tonic-gate /* Lock/unlock agent */
2290Sstevel@tonic-gate int
ssh_lock_agent(AuthenticationConnection * auth,int lock,const char * password)2300Sstevel@tonic-gate ssh_lock_agent(AuthenticationConnection *auth, int lock, const char *password)
2310Sstevel@tonic-gate {
2320Sstevel@tonic-gate int type;
2330Sstevel@tonic-gate Buffer msg;
2340Sstevel@tonic-gate
2350Sstevel@tonic-gate buffer_init(&msg);
2360Sstevel@tonic-gate buffer_put_char(&msg, lock ? SSH_AGENTC_LOCK : SSH_AGENTC_UNLOCK);
2370Sstevel@tonic-gate buffer_put_cstring(&msg, password);
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate if (ssh_request_reply(auth, &msg, &msg) == 0) {
2400Sstevel@tonic-gate buffer_free(&msg);
2410Sstevel@tonic-gate return 0;
2420Sstevel@tonic-gate }
2430Sstevel@tonic-gate type = buffer_get_char(&msg);
2440Sstevel@tonic-gate buffer_free(&msg);
2450Sstevel@tonic-gate return decode_reply(type);
2460Sstevel@tonic-gate }
2470Sstevel@tonic-gate
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * Returns the first authentication identity held by the agent.
2500Sstevel@tonic-gate */
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate int
ssh_get_num_identities(AuthenticationConnection * auth,int version)2530Sstevel@tonic-gate ssh_get_num_identities(AuthenticationConnection *auth, int version)
2540Sstevel@tonic-gate {
2550Sstevel@tonic-gate int type, code1 = 0, code2 = 0;
2560Sstevel@tonic-gate Buffer request;
2570Sstevel@tonic-gate
2580Sstevel@tonic-gate switch (version) {
2590Sstevel@tonic-gate case 1:
2600Sstevel@tonic-gate code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
2610Sstevel@tonic-gate code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
2620Sstevel@tonic-gate break;
2630Sstevel@tonic-gate case 2:
2640Sstevel@tonic-gate code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
2650Sstevel@tonic-gate code2 = SSH2_AGENT_IDENTITIES_ANSWER;
2660Sstevel@tonic-gate break;
2670Sstevel@tonic-gate default:
2680Sstevel@tonic-gate return 0;
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate /*
2720Sstevel@tonic-gate * Send a message to the agent requesting for a list of the
2730Sstevel@tonic-gate * identities it can represent.
2740Sstevel@tonic-gate */
2750Sstevel@tonic-gate buffer_init(&request);
2760Sstevel@tonic-gate buffer_put_char(&request, code1);
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate buffer_clear(&auth->identities);
2790Sstevel@tonic-gate if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
2800Sstevel@tonic-gate buffer_free(&request);
2810Sstevel@tonic-gate return 0;
2820Sstevel@tonic-gate }
2830Sstevel@tonic-gate buffer_free(&request);
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate /* Get message type, and verify that we got a proper answer. */
2860Sstevel@tonic-gate type = buffer_get_char(&auth->identities);
2870Sstevel@tonic-gate if (agent_failed(type)) {
2880Sstevel@tonic-gate return 0;
2890Sstevel@tonic-gate } else if (type != code2) {
2900Sstevel@tonic-gate fatal("Bad authentication reply message type: %d", type);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate
2930Sstevel@tonic-gate /* Get the number of entries in the response and check it for sanity. */
2940Sstevel@tonic-gate auth->howmany = buffer_get_int(&auth->identities);
2950Sstevel@tonic-gate if (auth->howmany > 1024)
2960Sstevel@tonic-gate fatal("Too many identities in authentication reply: %d",
2970Sstevel@tonic-gate auth->howmany);
2980Sstevel@tonic-gate
2990Sstevel@tonic-gate return auth->howmany;
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate Key *
ssh_get_first_identity(AuthenticationConnection * auth,char ** comment,int version)3030Sstevel@tonic-gate ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate /* get number of identities and return the first entry (if any). */
3060Sstevel@tonic-gate if (ssh_get_num_identities(auth, version) > 0)
3070Sstevel@tonic-gate return ssh_get_next_identity(auth, comment, version);
3080Sstevel@tonic-gate return NULL;
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate
3110Sstevel@tonic-gate Key *
ssh_get_next_identity(AuthenticationConnection * auth,char ** comment,int version)3120Sstevel@tonic-gate ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
3130Sstevel@tonic-gate {
3140Sstevel@tonic-gate u_int bits;
3150Sstevel@tonic-gate u_char *blob;
3160Sstevel@tonic-gate u_int blen;
3170Sstevel@tonic-gate Key *key = NULL;
3180Sstevel@tonic-gate
3190Sstevel@tonic-gate /* Return failure if no more entries. */
3200Sstevel@tonic-gate if (auth->howmany <= 0)
3210Sstevel@tonic-gate return NULL;
3220Sstevel@tonic-gate
3230Sstevel@tonic-gate /*
3240Sstevel@tonic-gate * Get the next entry from the packet. These will abort with a fatal
3250Sstevel@tonic-gate * error if the packet is too short or contains corrupt data.
3260Sstevel@tonic-gate */
3270Sstevel@tonic-gate switch (version) {
3280Sstevel@tonic-gate case 1:
3290Sstevel@tonic-gate key = key_new(KEY_RSA1);
3300Sstevel@tonic-gate bits = buffer_get_int(&auth->identities);
3310Sstevel@tonic-gate buffer_get_bignum(&auth->identities, key->rsa->e);
3320Sstevel@tonic-gate buffer_get_bignum(&auth->identities, key->rsa->n);
3330Sstevel@tonic-gate *comment = buffer_get_string(&auth->identities, NULL);
3340Sstevel@tonic-gate if (bits != BN_num_bits(key->rsa->n))
3350Sstevel@tonic-gate log("Warning: identity keysize mismatch: actual %d, announced %u",
3360Sstevel@tonic-gate BN_num_bits(key->rsa->n), bits);
3370Sstevel@tonic-gate break;
3380Sstevel@tonic-gate case 2:
3390Sstevel@tonic-gate blob = buffer_get_string(&auth->identities, &blen);
3400Sstevel@tonic-gate *comment = buffer_get_string(&auth->identities, NULL);
3410Sstevel@tonic-gate key = key_from_blob(blob, blen);
3420Sstevel@tonic-gate xfree(blob);
3430Sstevel@tonic-gate break;
3440Sstevel@tonic-gate default:
3450Sstevel@tonic-gate return NULL;
3460Sstevel@tonic-gate break;
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate /* Decrement the number of remaining entries. */
3490Sstevel@tonic-gate auth->howmany--;
3500Sstevel@tonic-gate return key;
3510Sstevel@tonic-gate }
3520Sstevel@tonic-gate
3530Sstevel@tonic-gate /*
3540Sstevel@tonic-gate * Generates a random challenge, sends it to the agent, and waits for
3550Sstevel@tonic-gate * response from the agent. Returns true (non-zero) if the agent gave the
3560Sstevel@tonic-gate * correct answer, zero otherwise. Response type selects the style of
3570Sstevel@tonic-gate * response desired, with 0 corresponding to protocol version 1.0 (no longer
3580Sstevel@tonic-gate * supported) and 1 corresponding to protocol version 1.1.
3590Sstevel@tonic-gate */
3600Sstevel@tonic-gate
3610Sstevel@tonic-gate int
ssh_decrypt_challenge(AuthenticationConnection * auth,Key * key,BIGNUM * challenge,u_char session_id[16],u_int response_type,u_char response[16])3620Sstevel@tonic-gate ssh_decrypt_challenge(AuthenticationConnection *auth,
3630Sstevel@tonic-gate Key* key, BIGNUM *challenge,
3640Sstevel@tonic-gate u_char session_id[16],
3650Sstevel@tonic-gate u_int response_type,
3660Sstevel@tonic-gate u_char response[16])
3670Sstevel@tonic-gate {
3680Sstevel@tonic-gate Buffer buffer;
3690Sstevel@tonic-gate int success = 0;
3700Sstevel@tonic-gate int i;
3710Sstevel@tonic-gate int type;
3720Sstevel@tonic-gate
3730Sstevel@tonic-gate if (key->type != KEY_RSA1)
3740Sstevel@tonic-gate return 0;
3750Sstevel@tonic-gate if (response_type == 0) {
3760Sstevel@tonic-gate log("Compatibility with ssh protocol version 1.0 no longer supported.");
3770Sstevel@tonic-gate return 0;
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate buffer_init(&buffer);
3800Sstevel@tonic-gate buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE);
3810Sstevel@tonic-gate buffer_put_int(&buffer, BN_num_bits(key->rsa->n));
3820Sstevel@tonic-gate buffer_put_bignum(&buffer, key->rsa->e);
3830Sstevel@tonic-gate buffer_put_bignum(&buffer, key->rsa->n);
3840Sstevel@tonic-gate buffer_put_bignum(&buffer, challenge);
3850Sstevel@tonic-gate buffer_append(&buffer, session_id, 16);
3860Sstevel@tonic-gate buffer_put_int(&buffer, response_type);
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
3890Sstevel@tonic-gate buffer_free(&buffer);
3900Sstevel@tonic-gate return 0;
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate type = buffer_get_char(&buffer);
3930Sstevel@tonic-gate
3940Sstevel@tonic-gate if (agent_failed(type)) {
3950Sstevel@tonic-gate log("Agent admitted failure to authenticate using the key.");
3960Sstevel@tonic-gate } else if (type != SSH_AGENT_RSA_RESPONSE) {
3970Sstevel@tonic-gate fatal("Bad authentication response: %d", type);
3980Sstevel@tonic-gate } else {
3990Sstevel@tonic-gate success = 1;
4000Sstevel@tonic-gate /*
4010Sstevel@tonic-gate * Get the response from the packet. This will abort with a
4020Sstevel@tonic-gate * fatal error if the packet is corrupt.
4030Sstevel@tonic-gate */
4040Sstevel@tonic-gate for (i = 0; i < 16; i++)
4050Sstevel@tonic-gate response[i] = buffer_get_char(&buffer);
4060Sstevel@tonic-gate }
4070Sstevel@tonic-gate buffer_free(&buffer);
4080Sstevel@tonic-gate return success;
4090Sstevel@tonic-gate }
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate /* ask agent to sign data, returns -1 on error, 0 on success */
4120Sstevel@tonic-gate int
ssh_agent_sign(AuthenticationConnection * auth,Key * key,u_char ** sigp,u_int * lenp,u_char * data,u_int datalen)4130Sstevel@tonic-gate ssh_agent_sign(AuthenticationConnection *auth,
4140Sstevel@tonic-gate Key *key,
4150Sstevel@tonic-gate u_char **sigp, u_int *lenp,
4160Sstevel@tonic-gate u_char *data, u_int datalen)
4170Sstevel@tonic-gate {
4180Sstevel@tonic-gate Buffer msg;
4190Sstevel@tonic-gate u_char *blob;
4200Sstevel@tonic-gate u_int blen;
4210Sstevel@tonic-gate int type, flags = 0;
4220Sstevel@tonic-gate int ret = -1;
4230Sstevel@tonic-gate
4240Sstevel@tonic-gate if (key_to_blob(key, &blob, &blen) == 0)
4250Sstevel@tonic-gate return -1;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate if (datafellows & SSH_BUG_SIGBLOB)
4280Sstevel@tonic-gate flags = SSH_AGENT_OLD_SIGNATURE;
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate buffer_init(&msg);
4310Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
4320Sstevel@tonic-gate buffer_put_string(&msg, blob, blen);
4330Sstevel@tonic-gate buffer_put_string(&msg, data, datalen);
4340Sstevel@tonic-gate buffer_put_int(&msg, flags);
4350Sstevel@tonic-gate xfree(blob);
4360Sstevel@tonic-gate
4370Sstevel@tonic-gate if (ssh_request_reply(auth, &msg, &msg) == 0) {
4380Sstevel@tonic-gate buffer_free(&msg);
4390Sstevel@tonic-gate return -1;
4400Sstevel@tonic-gate }
4410Sstevel@tonic-gate type = buffer_get_char(&msg);
4420Sstevel@tonic-gate if (agent_failed(type)) {
4430Sstevel@tonic-gate log("Agent admitted failure to sign using the key.");
4440Sstevel@tonic-gate } else if (type != SSH2_AGENT_SIGN_RESPONSE) {
4450Sstevel@tonic-gate fatal("Bad authentication response: %d", type);
4460Sstevel@tonic-gate } else {
4470Sstevel@tonic-gate ret = 0;
4480Sstevel@tonic-gate *sigp = buffer_get_string(&msg, lenp);
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate buffer_free(&msg);
4510Sstevel@tonic-gate return ret;
4520Sstevel@tonic-gate }
4530Sstevel@tonic-gate
4540Sstevel@tonic-gate /* Encode key for a message to the agent. */
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate static void
ssh_encode_identity_rsa1(Buffer * b,RSA * key,const char * comment)4570Sstevel@tonic-gate ssh_encode_identity_rsa1(Buffer *b, RSA *key, const char *comment)
4580Sstevel@tonic-gate {
4590Sstevel@tonic-gate buffer_put_int(b, BN_num_bits(key->n));
4600Sstevel@tonic-gate buffer_put_bignum(b, key->n);
4610Sstevel@tonic-gate buffer_put_bignum(b, key->e);
4620Sstevel@tonic-gate buffer_put_bignum(b, key->d);
4630Sstevel@tonic-gate /* To keep within the protocol: p < q for ssh. in SSL p > q */
4640Sstevel@tonic-gate buffer_put_bignum(b, key->iqmp); /* ssh key->u */
4650Sstevel@tonic-gate buffer_put_bignum(b, key->q); /* ssh key->p, SSL key->q */
4660Sstevel@tonic-gate buffer_put_bignum(b, key->p); /* ssh key->q, SSL key->p */
4670Sstevel@tonic-gate buffer_put_cstring(b, comment);
4680Sstevel@tonic-gate }
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate static void
ssh_encode_identity_ssh2(Buffer * b,Key * key,const char * comment)4710Sstevel@tonic-gate ssh_encode_identity_ssh2(Buffer *b, Key *key, const char *comment)
4720Sstevel@tonic-gate {
4730Sstevel@tonic-gate buffer_put_cstring(b, key_ssh_name(key));
4740Sstevel@tonic-gate switch (key->type) {
4750Sstevel@tonic-gate case KEY_RSA:
4760Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->n);
4770Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->e);
4780Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->d);
4790Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->iqmp);
4800Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->p);
4810Sstevel@tonic-gate buffer_put_bignum2(b, key->rsa->q);
4820Sstevel@tonic-gate break;
4830Sstevel@tonic-gate case KEY_DSA:
4840Sstevel@tonic-gate buffer_put_bignum2(b, key->dsa->p);
4850Sstevel@tonic-gate buffer_put_bignum2(b, key->dsa->q);
4860Sstevel@tonic-gate buffer_put_bignum2(b, key->dsa->g);
4870Sstevel@tonic-gate buffer_put_bignum2(b, key->dsa->pub_key);
4880Sstevel@tonic-gate buffer_put_bignum2(b, key->dsa->priv_key);
4890Sstevel@tonic-gate break;
4900Sstevel@tonic-gate }
4910Sstevel@tonic-gate buffer_put_cstring(b, comment);
4920Sstevel@tonic-gate }
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /*
4950Sstevel@tonic-gate * Adds an identity to the authentication server. This call is not meant to
4960Sstevel@tonic-gate * be used by normal applications.
4970Sstevel@tonic-gate */
4980Sstevel@tonic-gate
4990Sstevel@tonic-gate int
ssh_add_identity_constrained(AuthenticationConnection * auth,Key * key,const char * comment,u_int life)5000Sstevel@tonic-gate ssh_add_identity_constrained(AuthenticationConnection *auth, Key *key,
5010Sstevel@tonic-gate const char *comment, u_int life)
5020Sstevel@tonic-gate {
5030Sstevel@tonic-gate Buffer msg;
5040Sstevel@tonic-gate int type, constrained = (life != 0);
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate buffer_init(&msg);
5070Sstevel@tonic-gate
5080Sstevel@tonic-gate switch (key->type) {
5090Sstevel@tonic-gate case KEY_RSA1:
5100Sstevel@tonic-gate type = constrained ?
5110Sstevel@tonic-gate SSH_AGENTC_ADD_RSA_ID_CONSTRAINED :
5120Sstevel@tonic-gate SSH_AGENTC_ADD_RSA_IDENTITY;
5130Sstevel@tonic-gate buffer_put_char(&msg, type);
5140Sstevel@tonic-gate ssh_encode_identity_rsa1(&msg, key->rsa, comment);
5150Sstevel@tonic-gate break;
5160Sstevel@tonic-gate case KEY_RSA:
5170Sstevel@tonic-gate case KEY_DSA:
5180Sstevel@tonic-gate type = constrained ?
5190Sstevel@tonic-gate SSH2_AGENTC_ADD_ID_CONSTRAINED :
5200Sstevel@tonic-gate SSH2_AGENTC_ADD_IDENTITY;
5210Sstevel@tonic-gate buffer_put_char(&msg, type);
5220Sstevel@tonic-gate ssh_encode_identity_ssh2(&msg, key, comment);
5230Sstevel@tonic-gate break;
5240Sstevel@tonic-gate default:
5250Sstevel@tonic-gate buffer_free(&msg);
5260Sstevel@tonic-gate return 0;
5270Sstevel@tonic-gate break;
5280Sstevel@tonic-gate }
5290Sstevel@tonic-gate if (constrained) {
5300Sstevel@tonic-gate if (life != 0) {
5310Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENT_CONSTRAIN_LIFETIME);
5320Sstevel@tonic-gate buffer_put_int(&msg, life);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate if (ssh_request_reply(auth, &msg, &msg) == 0) {
5360Sstevel@tonic-gate buffer_free(&msg);
5370Sstevel@tonic-gate return 0;
5380Sstevel@tonic-gate }
5390Sstevel@tonic-gate type = buffer_get_char(&msg);
5400Sstevel@tonic-gate buffer_free(&msg);
5410Sstevel@tonic-gate return decode_reply(type);
5420Sstevel@tonic-gate }
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate int
ssh_add_identity(AuthenticationConnection * auth,Key * key,const char * comment)5450Sstevel@tonic-gate ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
5460Sstevel@tonic-gate {
5470Sstevel@tonic-gate return ssh_add_identity_constrained(auth, key, comment, 0);
5480Sstevel@tonic-gate }
5490Sstevel@tonic-gate
5500Sstevel@tonic-gate /*
5510Sstevel@tonic-gate * Removes an identity from the authentication server. This call is not
5520Sstevel@tonic-gate * meant to be used by normal applications.
5530Sstevel@tonic-gate */
5540Sstevel@tonic-gate
5550Sstevel@tonic-gate int
ssh_remove_identity(AuthenticationConnection * auth,Key * key)5560Sstevel@tonic-gate ssh_remove_identity(AuthenticationConnection *auth, Key *key)
5570Sstevel@tonic-gate {
5580Sstevel@tonic-gate Buffer msg;
5590Sstevel@tonic-gate int type;
5600Sstevel@tonic-gate u_char *blob;
5610Sstevel@tonic-gate u_int blen;
5620Sstevel@tonic-gate
5630Sstevel@tonic-gate buffer_init(&msg);
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate if (key->type == KEY_RSA1) {
5660Sstevel@tonic-gate buffer_put_char(&msg, SSH_AGENTC_REMOVE_RSA_IDENTITY);
5670Sstevel@tonic-gate buffer_put_int(&msg, BN_num_bits(key->rsa->n));
5680Sstevel@tonic-gate buffer_put_bignum(&msg, key->rsa->e);
5690Sstevel@tonic-gate buffer_put_bignum(&msg, key->rsa->n);
5700Sstevel@tonic-gate } else if (key->type == KEY_DSA || key->type == KEY_RSA) {
5710Sstevel@tonic-gate key_to_blob(key, &blob, &blen);
5720Sstevel@tonic-gate buffer_put_char(&msg, SSH2_AGENTC_REMOVE_IDENTITY);
5730Sstevel@tonic-gate buffer_put_string(&msg, blob, blen);
5740Sstevel@tonic-gate xfree(blob);
5750Sstevel@tonic-gate } else {
5760Sstevel@tonic-gate buffer_free(&msg);
5770Sstevel@tonic-gate return 0;
5780Sstevel@tonic-gate }
5790Sstevel@tonic-gate if (ssh_request_reply(auth, &msg, &msg) == 0) {
5800Sstevel@tonic-gate buffer_free(&msg);
5810Sstevel@tonic-gate return 0;
5820Sstevel@tonic-gate }
5830Sstevel@tonic-gate type = buffer_get_char(&msg);
5840Sstevel@tonic-gate buffer_free(&msg);
5850Sstevel@tonic-gate return decode_reply(type);
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate * Removes all identities from the agent. This call is not meant to be used
5910Sstevel@tonic-gate * by normal applications.
5920Sstevel@tonic-gate */
5930Sstevel@tonic-gate
5940Sstevel@tonic-gate int
ssh_remove_all_identities(AuthenticationConnection * auth,int version)5950Sstevel@tonic-gate ssh_remove_all_identities(AuthenticationConnection *auth, int version)
5960Sstevel@tonic-gate {
5970Sstevel@tonic-gate Buffer msg;
5980Sstevel@tonic-gate int type;
5990Sstevel@tonic-gate int code = (version==1) ?
6000Sstevel@tonic-gate SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
6010Sstevel@tonic-gate SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
6020Sstevel@tonic-gate
6030Sstevel@tonic-gate buffer_init(&msg);
6040Sstevel@tonic-gate buffer_put_char(&msg, code);
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate if (ssh_request_reply(auth, &msg, &msg) == 0) {
6070Sstevel@tonic-gate buffer_free(&msg);
6080Sstevel@tonic-gate return 0;
6090Sstevel@tonic-gate }
6100Sstevel@tonic-gate type = buffer_get_char(&msg);
6110Sstevel@tonic-gate buffer_free(&msg);
6120Sstevel@tonic-gate return decode_reply(type);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate int
decode_reply(int type)6160Sstevel@tonic-gate decode_reply(int type)
6170Sstevel@tonic-gate {
6180Sstevel@tonic-gate switch (type) {
6190Sstevel@tonic-gate case SSH_AGENT_FAILURE:
6200Sstevel@tonic-gate case SSH_COM_AGENT2_FAILURE:
6210Sstevel@tonic-gate case SSH2_AGENT_FAILURE:
6220Sstevel@tonic-gate log("SSH_AGENT_FAILURE");
6230Sstevel@tonic-gate return 0;
6240Sstevel@tonic-gate case SSH_AGENT_SUCCESS:
6250Sstevel@tonic-gate return 1;
6260Sstevel@tonic-gate default:
6270Sstevel@tonic-gate fatal("Bad response from authentication agent: %d", type);
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate /* NOTREACHED */
6300Sstevel@tonic-gate return 0;
6310Sstevel@tonic-gate }
632