Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 2 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 3 | * authfd.c |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 4 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 5 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 6 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 7 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | * All rights reserved |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 9 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 10 | * Created: Wed Mar 29 01:30:28 1995 ylo |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 11 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 12 | * Functions for connecting the local authentication agent. |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 13 | * |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 14 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 15 | |
| 16 | #include "includes.h" |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 17 | RCSID("$OpenBSD: authfd.c,v 1.21 2000/06/26 09:22:29 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
| 19 | #include "ssh.h" |
| 20 | #include "rsa.h" |
| 21 | #include "authfd.h" |
| 22 | #include "buffer.h" |
| 23 | #include "bufaux.h" |
| 24 | #include "xmalloc.h" |
| 25 | #include "getput.h" |
| 26 | |
| 27 | #include <openssl/rsa.h> |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 28 | |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 29 | /* helper */ |
| 30 | int ssh_agent_get_reply(AuthenticationConnection *auth); |
| 31 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | /* Returns the number of the authentication fd, or -1 if there is none. */ |
| 33 | |
| 34 | int |
| 35 | ssh_get_authentication_socket() |
| 36 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 37 | const char *authsocket; |
| 38 | int sock; |
| 39 | struct sockaddr_un sunaddr; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 40 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME); |
| 42 | if (!authsocket) |
| 43 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 44 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 45 | sunaddr.sun_family = AF_UNIX; |
| 46 | strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 47 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 48 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 49 | if (sock < 0) |
| 50 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 51 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 52 | /* close on exec */ |
| 53 | if (fcntl(sock, F_SETFD, 1) == -1) { |
| 54 | close(sock); |
| 55 | return -1; |
| 56 | } |
| 57 | if (connect(sock, (struct sockaddr *) & sunaddr, sizeof(sunaddr)) < 0) { |
| 58 | close(sock); |
| 59 | return -1; |
| 60 | } |
| 61 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 62 | } |
| 63 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 64 | /* |
| 65 | * Closes the agent socket if it should be closed (depends on how it was |
| 66 | * obtained). The argument must have been returned by |
| 67 | * ssh_get_authentication_socket(). |
| 68 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 69 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 70 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 71 | ssh_close_authentication_socket(int sock) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 72 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 73 | if (getenv(SSH_AUTHSOCKET_ENV_NAME)) |
| 74 | close(sock); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 75 | } |
| 76 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 77 | /* |
| 78 | * Opens and connects a private socket for communication with the |
| 79 | * authentication agent. Returns the file descriptor (which must be |
| 80 | * shut down and closed by the caller when no longer needed). |
| 81 | * Returns NULL if an error occurred and the connection could not be |
| 82 | * opened. |
| 83 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 84 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 85 | AuthenticationConnection * |
| 86 | ssh_get_authentication_connection() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 87 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 88 | AuthenticationConnection *auth; |
| 89 | int sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 90 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 91 | sock = ssh_get_authentication_socket(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 92 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 93 | /* |
| 94 | * Fail if we couldn't obtain a connection. This happens if we |
| 95 | * exited due to a timeout. |
| 96 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 97 | if (sock < 0) |
| 98 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 99 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 100 | auth = xmalloc(sizeof(*auth)); |
| 101 | auth->fd = sock; |
| 102 | buffer_init(&auth->packet); |
| 103 | buffer_init(&auth->identities); |
| 104 | auth->howmany = 0; |
| 105 | |
| 106 | return auth; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 107 | } |
| 108 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 109 | /* |
| 110 | * Closes the connection to the authentication agent and frees any associated |
| 111 | * memory. |
| 112 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 113 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 114 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 115 | ssh_close_authentication_connection(AuthenticationConnection *ac) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 116 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 117 | buffer_free(&ac->packet); |
| 118 | buffer_free(&ac->identities); |
| 119 | close(ac->fd); |
| 120 | xfree(ac); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 121 | } |
| 122 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 123 | /* |
| 124 | * Returns the first authentication identity held by the agent. |
| 125 | * Returns true if an identity is available, 0 otherwise. |
| 126 | * The caller must initialize the integers before the call, and free the |
| 127 | * comment after a successful call (before calling ssh_get_next_identity). |
| 128 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 129 | |
| 130 | int |
| 131 | ssh_get_first_identity(AuthenticationConnection *auth, |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 132 | BIGNUM *e, BIGNUM *n, char **comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 133 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | unsigned char msg[8192]; |
| 135 | int len, l; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 136 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 137 | /* |
| 138 | * Send a message to the agent requesting for a list of the |
| 139 | * identities it can represent. |
| 140 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 141 | msg[0] = 0; |
| 142 | msg[1] = 0; |
| 143 | msg[2] = 0; |
| 144 | msg[3] = 1; |
| 145 | msg[4] = SSH_AGENTC_REQUEST_RSA_IDENTITIES; |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 146 | if (atomicio(write, auth->fd, msg, 5) != 5) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 147 | error("write auth->fd: %.100s", strerror(errno)); |
| 148 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 149 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 150 | /* Read the length of the response. XXX implement timeouts here. */ |
| 151 | len = 4; |
| 152 | while (len > 0) { |
| 153 | l = read(auth->fd, msg + 4 - len, len); |
| 154 | if (l <= 0) { |
| 155 | error("read auth->fd: %.100s", strerror(errno)); |
| 156 | return 0; |
| 157 | } |
| 158 | len -= l; |
| 159 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 160 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 161 | /* |
| 162 | * Extract the length, and check it for sanity. (We cannot trust |
| 163 | * authentication agents). |
| 164 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 165 | len = GET_32BIT(msg); |
| 166 | if (len < 1 || len > 256 * 1024) |
| 167 | fatal("Authentication reply message too long: %d\n", len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 168 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 169 | /* Read the packet itself. */ |
| 170 | buffer_clear(&auth->identities); |
| 171 | while (len > 0) { |
| 172 | l = len; |
| 173 | if (l > sizeof(msg)) |
| 174 | l = sizeof(msg); |
| 175 | l = read(auth->fd, msg, l); |
| 176 | if (l <= 0) |
| 177 | fatal("Incomplete authentication reply."); |
| 178 | buffer_append(&auth->identities, (char *) msg, l); |
| 179 | len -= l; |
| 180 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 181 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 182 | /* Get message type, and verify that we got a proper answer. */ |
| 183 | buffer_get(&auth->identities, (char *) msg, 1); |
| 184 | if (msg[0] != SSH_AGENT_RSA_IDENTITIES_ANSWER) |
| 185 | fatal("Bad authentication reply message type: %d", msg[0]); |
| 186 | |
| 187 | /* Get the number of entries in the response and check it for sanity. */ |
| 188 | auth->howmany = buffer_get_int(&auth->identities); |
| 189 | if (auth->howmany > 1024) |
| 190 | fatal("Too many identities in authentication reply: %d\n", auth->howmany); |
| 191 | |
| 192 | /* Return the first entry (if any). */ |
| 193 | return ssh_get_next_identity(auth, e, n, comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 194 | } |
| 195 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 196 | /* |
| 197 | * Returns the next authentication identity for the agent. Other functions |
| 198 | * can be called between this and ssh_get_first_identity or two calls of this |
| 199 | * function. This returns 0 if there are no more identities. The caller |
| 200 | * must free comment after a successful return. |
| 201 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 202 | |
| 203 | int |
| 204 | ssh_get_next_identity(AuthenticationConnection *auth, |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 205 | BIGNUM *e, BIGNUM *n, char **comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 206 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 207 | unsigned int bits; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 208 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 209 | /* Return failure if no more entries. */ |
| 210 | if (auth->howmany <= 0) |
| 211 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 212 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 213 | /* |
| 214 | * Get the next entry from the packet. These will abort with a fatal |
| 215 | * error if the packet is too short or contains corrupt data. |
| 216 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 217 | bits = buffer_get_int(&auth->identities); |
| 218 | buffer_get_bignum(&auth->identities, e); |
| 219 | buffer_get_bignum(&auth->identities, n); |
| 220 | *comment = buffer_get_string(&auth->identities, NULL); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 221 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 222 | if (bits != BN_num_bits(n)) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 223 | log("Warning: identity keysize mismatch: actual %d, announced %u", |
| 224 | BN_num_bits(n), bits); |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 225 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 226 | /* Decrement the number of remaining entries. */ |
| 227 | auth->howmany--; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 228 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 229 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 230 | } |
| 231 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 232 | /* |
| 233 | * Generates a random challenge, sends it to the agent, and waits for |
| 234 | * response from the agent. Returns true (non-zero) if the agent gave the |
| 235 | * correct answer, zero otherwise. Response type selects the style of |
| 236 | * response desired, with 0 corresponding to protocol version 1.0 (no longer |
| 237 | * supported) and 1 corresponding to protocol version 1.1. |
| 238 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 239 | |
| 240 | int |
| 241 | ssh_decrypt_challenge(AuthenticationConnection *auth, |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 242 | BIGNUM* e, BIGNUM *n, BIGNUM *challenge, |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 243 | unsigned char session_id[16], |
| 244 | unsigned int response_type, |
| 245 | unsigned char response[16]) |
| 246 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 247 | Buffer buffer; |
| 248 | unsigned char buf[8192]; |
| 249 | int len, l, i; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 250 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 251 | /* Response type 0 is no longer supported. */ |
| 252 | if (response_type == 0) |
| 253 | fatal("Compatibility with ssh protocol version 1.0 no longer supported."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 254 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 255 | /* Format a message to the agent. */ |
| 256 | buf[0] = SSH_AGENTC_RSA_CHALLENGE; |
| 257 | buffer_init(&buffer); |
| 258 | buffer_append(&buffer, (char *) buf, 1); |
| 259 | buffer_put_int(&buffer, BN_num_bits(n)); |
| 260 | buffer_put_bignum(&buffer, e); |
| 261 | buffer_put_bignum(&buffer, n); |
| 262 | buffer_put_bignum(&buffer, challenge); |
| 263 | buffer_append(&buffer, (char *) session_id, 16); |
| 264 | buffer_put_int(&buffer, response_type); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 265 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 266 | /* Get the length of the message, and format it in the buffer. */ |
| 267 | len = buffer_len(&buffer); |
| 268 | PUT_32BIT(buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 269 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 270 | /* Send the length and then the packet to the agent. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 271 | if (atomicio(write, auth->fd, buf, 4) != 4 || |
| 272 | atomicio(write, auth->fd, buffer_ptr(&buffer), |
| 273 | buffer_len(&buffer)) != buffer_len(&buffer)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 274 | error("Error writing to authentication socket."); |
| 275 | error_cleanup: |
| 276 | buffer_free(&buffer); |
| 277 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 278 | } |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 279 | /* |
| 280 | * Wait for response from the agent. First read the length of the |
| 281 | * response packet. |
| 282 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 283 | len = 4; |
| 284 | while (len > 0) { |
| 285 | l = read(auth->fd, buf + 4 - len, len); |
| 286 | if (l <= 0) { |
| 287 | error("Error reading response length from authentication socket."); |
| 288 | goto error_cleanup; |
| 289 | } |
| 290 | len -= l; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 291 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 292 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 293 | /* Extract the length, and check it for sanity. */ |
| 294 | len = GET_32BIT(buf); |
| 295 | if (len > 256 * 1024) |
| 296 | fatal("Authentication response too long: %d", len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 297 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 298 | /* Read the rest of the response in tothe buffer. */ |
| 299 | buffer_clear(&buffer); |
| 300 | while (len > 0) { |
| 301 | l = len; |
| 302 | if (l > sizeof(buf)) |
| 303 | l = sizeof(buf); |
| 304 | l = read(auth->fd, buf, l); |
| 305 | if (l <= 0) { |
| 306 | error("Error reading response from authentication socket."); |
| 307 | goto error_cleanup; |
| 308 | } |
| 309 | buffer_append(&buffer, (char *) buf, l); |
| 310 | len -= l; |
| 311 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 312 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 313 | /* Get the type of the packet. */ |
| 314 | buffer_get(&buffer, (char *) buf, 1); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 315 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | /* Check for agent failure message. */ |
| 317 | if (buf[0] == SSH_AGENT_FAILURE) { |
| 318 | log("Agent admitted failure to authenticate using the key."); |
| 319 | goto error_cleanup; |
| 320 | } |
| 321 | /* Now it must be an authentication response packet. */ |
| 322 | if (buf[0] != SSH_AGENT_RSA_RESPONSE) |
| 323 | fatal("Bad authentication response: %d", buf[0]); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 324 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 325 | /* |
| 326 | * Get the response from the packet. This will abort with a fatal |
| 327 | * error if the packet is corrupt. |
| 328 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 329 | for (i = 0; i < 16; i++) |
| 330 | response[i] = buffer_get_char(&buffer); |
| 331 | |
| 332 | /* The buffer containing the packet is no longer needed. */ |
| 333 | buffer_free(&buffer); |
| 334 | |
| 335 | /* Correct answer. */ |
| 336 | return 1; |
| 337 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 338 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 339 | /* |
| 340 | * Adds an identity to the authentication server. This call is not meant to |
| 341 | * be used by normal applications. |
| 342 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 343 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 344 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 345 | ssh_add_identity(AuthenticationConnection *auth, |
| 346 | RSA * key, const char *comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 347 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 348 | Buffer buffer; |
| 349 | unsigned char buf[8192]; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 350 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 351 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 352 | /* Format a message to the agent. */ |
| 353 | buffer_init(&buffer); |
| 354 | buffer_put_char(&buffer, SSH_AGENTC_ADD_RSA_IDENTITY); |
| 355 | buffer_put_int(&buffer, BN_num_bits(key->n)); |
| 356 | buffer_put_bignum(&buffer, key->n); |
| 357 | buffer_put_bignum(&buffer, key->e); |
| 358 | buffer_put_bignum(&buffer, key->d); |
| 359 | /* To keep within the protocol: p < q for ssh. in SSL p > q */ |
| 360 | buffer_put_bignum(&buffer, key->iqmp); /* ssh key->u */ |
| 361 | buffer_put_bignum(&buffer, key->q); /* ssh key->p, SSL key->q */ |
| 362 | buffer_put_bignum(&buffer, key->p); /* ssh key->q, SSL key->p */ |
| 363 | buffer_put_string(&buffer, comment, strlen(comment)); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 364 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 365 | /* Get the length of the message, and format it in the buffer. */ |
| 366 | len = buffer_len(&buffer); |
| 367 | PUT_32BIT(buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 368 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 369 | /* Send the length and then the packet to the agent. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 370 | if (atomicio(write, auth->fd, buf, 4) != 4 || |
| 371 | atomicio(write, auth->fd, buffer_ptr(&buffer), |
| 372 | buffer_len(&buffer)) != buffer_len(&buffer)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 373 | error("Error writing to authentication socket."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 374 | buffer_free(&buffer); |
| 375 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 376 | } |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 377 | buffer_free(&buffer); |
| 378 | return ssh_agent_get_reply(auth); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | } |
| 380 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 381 | /* |
| 382 | * Removes an identity from the authentication server. This call is not |
| 383 | * meant to be used by normal applications. |
| 384 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 385 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 386 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 387 | ssh_remove_identity(AuthenticationConnection *auth, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 388 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 389 | Buffer buffer; |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 390 | unsigned char buf[5]; |
| 391 | int len; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 392 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | /* Format a message to the agent. */ |
| 394 | buffer_init(&buffer); |
| 395 | buffer_put_char(&buffer, SSH_AGENTC_REMOVE_RSA_IDENTITY); |
| 396 | buffer_put_int(&buffer, BN_num_bits(key->n)); |
| 397 | buffer_put_bignum(&buffer, key->e); |
| 398 | buffer_put_bignum(&buffer, key->n); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 399 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 400 | /* Get the length of the message, and format it in the buffer. */ |
| 401 | len = buffer_len(&buffer); |
| 402 | PUT_32BIT(buf, len); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 403 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 404 | /* Send the length and then the packet to the agent. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 405 | if (atomicio(write, auth->fd, buf, 4) != 4 || |
| 406 | atomicio(write, auth->fd, buffer_ptr(&buffer), |
| 407 | buffer_len(&buffer)) != buffer_len(&buffer)) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 408 | error("Error writing to authentication socket."); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 409 | buffer_free(&buffer); |
| 410 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 411 | } |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 412 | buffer_free(&buffer); |
| 413 | return ssh_agent_get_reply(auth); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 414 | } |
| 415 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 416 | /* |
| 417 | * Removes all identities from the agent. This call is not meant to be used |
| 418 | * by normal applications. |
| 419 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 420 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 421 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 422 | ssh_remove_all_identities(AuthenticationConnection *auth) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | { |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 424 | unsigned char buf[5]; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 425 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 426 | /* Get the length of the message, and format it in the buffer. */ |
| 427 | PUT_32BIT(buf, 1); |
| 428 | buf[4] = SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 429 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 430 | /* Send the length and then the packet to the agent. */ |
Damien Miller | 037a0dc | 1999-12-07 15:38:31 +1100 | [diff] [blame] | 431 | if (atomicio(write, auth->fd, buf, 5) != 5) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 432 | error("Error writing to authentication socket."); |
| 433 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 434 | } |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 435 | return ssh_agent_get_reply(auth); |
| 436 | } |
| 437 | |
| 438 | /* |
| 439 | * Read for reply from agent. returns 1 for success, 0 on error |
| 440 | */ |
| 441 | |
| 442 | int |
| 443 | ssh_agent_get_reply(AuthenticationConnection *auth) |
| 444 | { |
| 445 | Buffer buffer; |
| 446 | unsigned char buf[8192]; |
| 447 | int len, l, type; |
| 448 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 449 | /* |
| 450 | * Wait for response from the agent. First read the length of the |
| 451 | * response packet. |
| 452 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 453 | len = 4; |
| 454 | while (len > 0) { |
| 455 | l = read(auth->fd, buf + 4 - len, len); |
| 456 | if (l <= 0) { |
| 457 | error("Error reading response length from authentication socket."); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 458 | buffer_free(&buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | len -= l; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 462 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 463 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 464 | /* Extract the length, and check it for sanity. */ |
| 465 | len = GET_32BIT(buf); |
| 466 | if (len > 256 * 1024) |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 467 | fatal("Response from agent too long: %d", len); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 468 | |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 469 | /* Read the rest of the response in to the buffer. */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 470 | buffer_init(&buffer); |
| 471 | while (len > 0) { |
| 472 | l = len; |
| 473 | if (l > sizeof(buf)) |
| 474 | l = sizeof(buf); |
| 475 | l = read(auth->fd, buf, l); |
| 476 | if (l <= 0) { |
| 477 | error("Error reading response from authentication socket."); |
| 478 | buffer_free(&buffer); |
| 479 | return 0; |
| 480 | } |
| 481 | buffer_append(&buffer, (char *) buf, l); |
| 482 | len -= l; |
| 483 | } |
| 484 | |
| 485 | /* Get the type of the packet. */ |
| 486 | type = buffer_get_char(&buffer); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 487 | buffer_free(&buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 488 | switch (type) { |
| 489 | case SSH_AGENT_FAILURE: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 490 | return 0; |
| 491 | case SSH_AGENT_SUCCESS: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 492 | return 1; |
| 493 | default: |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 494 | fatal("Bad response from authentication agent: %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 495 | } |
| 496 | /* NOTREACHED */ |
| 497 | return 0; |
| 498 | } |