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 | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 17 | RCSID("$OpenBSD: authfd.c,v 1.24 2000/08/15 19:20:46 markus Exp $"); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 18 | |
| 19 | #include "ssh.h" |
| 20 | #include "rsa.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 21 | #include "buffer.h" |
| 22 | #include "bufaux.h" |
| 23 | #include "xmalloc.h" |
| 24 | #include "getput.h" |
| 25 | |
| 26 | #include <openssl/rsa.h> |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 27 | #include <openssl/dsa.h> |
| 28 | #include <openssl/evp.h> |
| 29 | #include "key.h" |
| 30 | #include "authfd.h" |
| 31 | #include "kex.h" |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 32 | |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 33 | /* helper */ |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 34 | int decode_reply(int type); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 35 | |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 36 | /* Returns the number of the authentication fd, or -1 if there is none. */ |
| 37 | |
| 38 | int |
| 39 | ssh_get_authentication_socket() |
| 40 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 41 | const char *authsocket; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 42 | int sock, len; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 43 | struct sockaddr_un sunaddr; |
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 | authsocket = getenv(SSH_AUTHSOCKET_ENV_NAME); |
| 46 | if (!authsocket) |
| 47 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 48 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 49 | sunaddr.sun_family = AF_UNIX; |
| 50 | strlcpy(sunaddr.sun_path, authsocket, sizeof(sunaddr.sun_path)); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 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 */ |
Damien Miller | 10f6f6b | 1999-11-17 17:29:08 +1100 | [diff] [blame] | 56 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 57 | sock = socket(AF_UNIX, SOCK_STREAM, 0); |
| 58 | if (sock < 0) |
| 59 | return -1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 60 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 61 | /* close on exec */ |
| 62 | if (fcntl(sock, F_SETFD, 1) == -1) { |
| 63 | close(sock); |
| 64 | return -1; |
| 65 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 66 | if (connect(sock, (struct sockaddr *) & sunaddr, len) < 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 67 | close(sock); |
| 68 | return -1; |
| 69 | } |
| 70 | return sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 71 | } |
| 72 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 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 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 132 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 133 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 134 | ssh_close_authentication_socket(int sock) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 135 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 136 | if (getenv(SSH_AUTHSOCKET_ENV_NAME)) |
| 137 | close(sock); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 138 | } |
| 139 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 147 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 148 | AuthenticationConnection * |
| 149 | ssh_get_authentication_connection() |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 150 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 151 | AuthenticationConnection *auth; |
| 152 | int sock; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 153 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 154 | sock = ssh_get_authentication_socket(); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 155 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 156 | /* |
| 157 | * Fail if we couldn't obtain a connection. This happens if we |
| 158 | * exited due to a timeout. |
| 159 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 160 | if (sock < 0) |
| 161 | return NULL; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 162 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 170 | } |
| 171 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 172 | /* |
| 173 | * Closes the connection to the authentication agent and frees any associated |
| 174 | * memory. |
| 175 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 176 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 177 | void |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 178 | ssh_close_authentication_connection(AuthenticationConnection *ac) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 179 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 180 | buffer_free(&ac->packet); |
| 181 | buffer_free(&ac->identities); |
| 182 | close(ac->fd); |
| 183 | xfree(ac); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 184 | } |
| 185 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 192 | |
| 193 | int |
| 194 | ssh_get_first_identity(AuthenticationConnection *auth, |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 195 | BIGNUM *e, BIGNUM *n, char **comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 196 | { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 197 | Buffer request; |
| 198 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 199 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 200 | /* |
| 201 | * Send a message to the agent requesting for a list of the |
| 202 | * identities it can represent. |
| 203 | */ |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 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); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 210 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 211 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 212 | buffer_free(&request); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 213 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 214 | /* Get message type, and verify that we got a proper answer. */ |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 215 | type = buffer_get_char(&auth->identities); |
| 216 | if (type != SSH_AGENT_RSA_IDENTITIES_ANSWER) |
| 217 | fatal("Bad authentication reply message type: %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 222 | fatal("Too many identities in authentication reply: %d\n", |
| 223 | auth->howmany); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 224 | |
| 225 | /* Return the first entry (if any). */ |
| 226 | return ssh_get_next_identity(auth, e, n, comment); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 227 | } |
| 228 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 235 | |
| 236 | int |
| 237 | ssh_get_next_identity(AuthenticationConnection *auth, |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 238 | BIGNUM *e, BIGNUM *n, char **comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 239 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 240 | unsigned int bits; |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 241 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 242 | /* Return failure if no more entries. */ |
| 243 | if (auth->howmany <= 0) |
| 244 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 245 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
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 | if (bits != BN_num_bits(n)) |
Damien Miller | bd483e7 | 2000-04-30 10:00:53 +1000 | [diff] [blame] | 256 | log("Warning: identity keysize mismatch: actual %d, announced %u", |
| 257 | BN_num_bits(n), bits); |
Damien Miller | 7e8e820 | 1999-11-16 13:37:16 +1100 | [diff] [blame] | 258 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 259 | /* Decrement the number of remaining entries. */ |
| 260 | auth->howmany--; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 261 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 262 | return 1; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 263 | } |
| 264 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 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 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 272 | |
| 273 | int |
| 274 | ssh_decrypt_challenge(AuthenticationConnection *auth, |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 275 | BIGNUM* e, BIGNUM *n, BIGNUM *challenge, |
| 276 | unsigned char session_id[16], |
| 277 | unsigned int response_type, |
| 278 | unsigned char response[16]) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 279 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 280 | Buffer buffer; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 281 | int success = 0; |
| 282 | int i; |
| 283 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 284 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 285 | if (response_type == 0) |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 286 | fatal("Compatibility with ssh protocol version " |
| 287 | "1.0 no longer supported."); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 288 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 289 | buffer_init(&buffer); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 290 | buffer_put_char(&buffer, SSH_AGENTC_RSA_CHALLENGE); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 297 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 298 | if (ssh_request_reply(auth, &buffer, &buffer) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 299 | buffer_free(&buffer); |
| 300 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 301 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 302 | type = buffer_get_char(&buffer); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 303 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 304 | if (type == SSH_AGENT_FAILURE) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 305 | log("Agent admitted failure to authenticate using the key."); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 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); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 316 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 317 | buffer_free(&buffer); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 318 | return success; |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 319 | } |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 320 | |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 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 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 353 | /* |
| 354 | * Adds an identity to the authentication server. This call is not meant to |
| 355 | * be used by normal applications. |
| 356 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 357 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 358 | int |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 359 | ssh_add_identity(AuthenticationConnection *auth, Key *key, const char *comment) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 360 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 361 | Buffer buffer; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 362 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 363 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 364 | buffer_init(&buffer); |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 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 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 378 | if (ssh_request_reply(auth, &buffer, &buffer) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 379 | buffer_free(&buffer); |
| 380 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 381 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 382 | type = buffer_get_char(&buffer); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 383 | buffer_free(&buffer); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 384 | return decode_reply(type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 385 | } |
| 386 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 387 | /* |
| 388 | * Removes an identity from the authentication server. This call is not |
| 389 | * meant to be used by normal applications. |
| 390 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 391 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 392 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 393 | ssh_remove_identity(AuthenticationConnection *auth, RSA *key) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 394 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 395 | Buffer buffer; |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 396 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 397 | |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 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); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 403 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 404 | if (ssh_request_reply(auth, &buffer, &buffer) == 0) { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 405 | buffer_free(&buffer); |
| 406 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 407 | } |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 408 | type = buffer_get_char(&buffer); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 409 | buffer_free(&buffer); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 410 | return decode_reply(type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 411 | } |
| 412 | |
Damien Miller | 5428f64 | 1999-11-25 11:54:57 +1100 | [diff] [blame] | 413 | /* |
| 414 | * Removes all identities from the agent. This call is not meant to be used |
| 415 | * by normal applications. |
| 416 | */ |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 417 | |
Damien Miller | 4af5130 | 2000-04-16 11:18:38 +1000 | [diff] [blame] | 418 | int |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 419 | ssh_remove_all_identities(AuthenticationConnection *auth) |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 420 | { |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 421 | Buffer buffer; |
| 422 | int type; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 423 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 424 | buffer_init(&buffer); |
| 425 | buffer_put_char(&buffer, SSH_AGENTC_REMOVE_ALL_RSA_IDENTITIES); |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 426 | |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 427 | if (ssh_request_reply(auth, &buffer, &buffer) == 0) { |
| 428 | buffer_free(&buffer); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 429 | return 0; |
Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 430 | } |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 431 | type = buffer_get_char(&buffer); |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 432 | buffer_free(&buffer); |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 433 | return decode_reply(type); |
| 434 | } |
| 435 | |
| 436 | int |
| 437 | decode_reply(int type) |
| 438 | { |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 439 | switch (type) { |
| 440 | case SSH_AGENT_FAILURE: |
Damien Miller | 942da03 | 2000-08-18 13:59:06 +1000 | [diff] [blame] | 441 | log("SSH_AGENT_FAILURE"); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 442 | return 0; |
| 443 | case SSH_AGENT_SUCCESS: |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 444 | return 1; |
| 445 | default: |
Damien Miller | 3702396 | 2000-07-11 17:31:38 +1000 | [diff] [blame] | 446 | fatal("Bad response from authentication agent: %d", type); |
Damien Miller | 95def09 | 1999-11-25 00:26:21 +1100 | [diff] [blame] | 447 | } |
| 448 | /* NOTREACHED */ |
| 449 | return 0; |
| 450 | } |