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.
14 * SSH2 implementation,
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
20 RCSID("$OpenBSD: authfd.c,v 1.25 2000/08/19 21:34:42 markus Exp $");
29 #include <openssl/rsa.h>
30 #include <openssl/dsa.h>
31 #include <openssl/evp.h>
38 int decode_reply(int type);
40 /* Returns the number of the authentication fd, or -1 if there is none. */
43 ssh_get_authentication_socket()
45 const char *authsocket;
47 struct sockaddr_un sunaddr;
49 authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME);
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 */
61 sock = socket(AF_UNIX, SOCK_STREAM, 0);
66 if (fcntl(sock, F_SETFD, 1) == -1) {
70 if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) {
78 ssh_request_reply(AuthenticationConnection *auth, Buffer *request, Buffer *reply)
83 /* Get the length of the message, and format it in the buffer. */
84 len = buffer_len(request);
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.");
95 * Wait for response from the agent. First read the length of the
100 l = read(auth->fd, buf + 4 - len, len);
102 error("Error reading response length from authentication socket.");
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);
113 /* Read the rest of the response in to the buffer. */
119 l = read(auth->fd, buf, l);
121 error("Error reading response from authentication socket.");
124 buffer_append(reply, (char *) buf, l);
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().
137 ssh_close_authentication_socket(int sock)
139 if (getenv(SSH_AUTHSOCKET_ENV_NAME))
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
151 AuthenticationConnection *
152 ssh_get_authentication_connection()
154 AuthenticationConnection *auth;
157 sock = ssh_get_authentication_socket();
160 * Fail if we couldn't obtain a connection. This happens if we
161 * exited due to a timeout.
166 auth = xmalloc(sizeof(*auth));
168 buffer_init(&auth->identities);
175 * Closes the connection to the authentication agent and frees any associated
180 ssh_close_authentication_connection(AuthenticationConnection *auth)
182 buffer_free(&auth->identities);
188 * Returns the first authentication identity held by the agent.
192 ssh_get_first_identity(AuthenticationConnection *auth, char **comment, int version)
194 int type, code1 = 0, code2 = 0;
199 code1 = SSH_AGENTC_REQUEST_RSA_IDENTITIES;
200 code2 = SSH_AGENT_RSA_IDENTITIES_ANSWER;
203 code1 = SSH2_AGENTC_REQUEST_IDENTITIES;
204 code2 = SSH2_AGENT_IDENTITIES_ANSWER;
211 * Send a message to the agent requesting for a list of the
212 * identities it can represent.
214 buffer_init(&request);
215 buffer_put_char(&request, code1);
217 buffer_clear(&auth->identities);
218 if (ssh_request_reply(auth, &request, &auth->identities) == 0) {
219 buffer_free(&request);
222 buffer_free(&request);
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) {
228 } else if (type != code2) {
229 fatal("Bad authentication reply message type: %d", type);
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",
238 /* Return the first entry (if any). */
239 return ssh_get_next_identity(auth, comment, version);
243 ssh_get_next_identity(AuthenticationConnection *auth, char **comment, int version)
250 /* Return failure if no more entries. */
251 if (auth->howmany <= 0)
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.
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);
270 blob = buffer_get_string(&auth->identities, &blen);
271 *comment = buffer_get_string(&auth->identities, NULL);
272 key = dsa_key_from_blob(blob, blen);
279 /* Decrement the number of remaining entries. */
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.
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])
304 if (key->type != KEY_RSA)
306 if (response_type == 0) {
307 log("Compatibility with ssh protocol version 1.0 no longer supported.");
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);
319 if (ssh_request_reply(auth, &buffer, &buffer) == 0) {
320 buffer_free(&buffer);
323 type = buffer_get_char(&buffer);
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);
332 * Get the response from the packet. This will abort with a
333 * fatal error if the packet is corrupt.
335 for (i = 0; i < 16; i++)
336 response[i] = buffer_get_char(&buffer);
338 buffer_free(&buffer);
342 /* ask agent to sign data, returns -1 on error, 0 on success */
344 ssh_agent_sign(AuthenticationConnection *auth,
346 unsigned char **sigp, int *lenp,
347 unsigned char *data, int datalen)
355 if (dsa_make_key_blob(key, &blob, &blen) == 0)
359 buffer_put_char(&msg, SSH2_AGENTC_SIGN_REQUEST);
360 buffer_put_string(&msg, blob, blen);
361 buffer_put_string(&msg, data, datalen);
364 if (ssh_request_reply(auth, &msg, &msg) == 0) {
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);
375 *sigp = buffer_get_string(&msg, lenp);
381 /* Encode key for a message to the agent. */
384 ssh_encode_identity_rsa(Buffer *b, RSA *key, const char *comment)
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));
400 ssh_encode_identity_dsa(Buffer *b, DSA *key, const char *comment)
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));
414 * Adds an identity to the authentication server. This call is not meant to
415 * be used by normal applications.
419 ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment)
428 ssh_encode_identity_rsa(&msg, key->rsa, comment);
431 ssh_encode_identity_dsa(&msg, key->dsa, comment);
438 if (ssh_request_reply(auth, &msg, &msg) == 0) {
442 type = buffer_get_char(&msg);
444 return decode_reply(type);
448 * Removes an identity from the authentication server. This call is not
449 * meant to be used by normal applications.
453 ssh_remove_identity(AuthenticationConnection *auth, Key *key)
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);
476 if (ssh_request_reply(auth, &msg, &msg) == 0) {
480 type = buffer_get_char(&msg);
482 return decode_reply(type);
486 * Removes all identities from the agent. This call is not meant to be used
487 * by normal applications.
491 ssh_remove_all_identities(AuthenticationConnection *auth, int version)
495 int code = (version==1) ?
496 SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES :
497 SSH2_AGENTC_REMOVE_ALL_IDENTITIES;
500 buffer_put_char(&msg, code);
502 if (ssh_request_reply(auth, &msg, &msg) == 0) {
506 type = buffer_get_char(&msg);
508 return decode_reply(type);
512 decode_reply(int type)
515 case SSH_AGENT_FAILURE:
516 log("SSH_AGENT_FAILURE");
518 case SSH_AGENT_SUCCESS:
521 fatal("Bad response from authentication agent: %d", type);