dtucker@openbsd.org | 66be4fe | 2017-03-10 03:53:11 +0000 | [diff] [blame] | 1 | /* $OpenBSD: sshconnect1.c,v 1.80 2017/03/10 03:53:11 dtucker Exp $ */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 2 | /* |
| 3 | * Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 4 | * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 5 | * All rights reserved |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 6 | * Code to connect to a remote host, and to perform the client side of the |
| 7 | * login (authentication) dialog. |
| 8 | * |
Damien Miller | e4340be | 2000-09-16 13:29:08 +1100 | [diff] [blame] | 9 | * As far as I am concerned, the code I have written for this software |
| 10 | * can be used freely for any purpose. Any derived versions of this |
| 11 | * software must be clearly marked as such, and if the derived work is |
| 12 | * incompatible with the protocol description in the RFC file, it must be |
| 13 | * called by a name other than "ssh" or "Secure Shell". |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "includes.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 17 | |
Damien Miller | 76c0480 | 2015-01-13 19:38:18 +1100 | [diff] [blame] | 18 | #ifdef WITH_SSH1 |
| 19 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <sys/socket.h> |
| 22 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 23 | #include <openssl/bn.h> |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 24 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 25 | #include <errno.h> |
Damien Miller | ded319c | 2006-09-01 15:38:36 +1000 | [diff] [blame] | 26 | #include <stdarg.h> |
Damien Miller | a7a73ee | 2006-08-05 11:37:59 +1000 | [diff] [blame] | 27 | #include <stdio.h> |
Damien Miller | e7a1e5c | 2006-08-05 11:34:19 +1000 | [diff] [blame] | 28 | #include <stdlib.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 29 | #include <string.h> |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 30 | #include <signal.h> |
| 31 | #include <pwd.h> |
Damien Miller | e3476ed | 2006-07-24 14:13:33 +1000 | [diff] [blame] | 32 | |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 33 | #include "xmalloc.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 34 | #include "ssh.h" |
| 35 | #include "ssh1.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 36 | #include "rsa.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 37 | #include "buffer.h" |
| 38 | #include "packet.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 39 | #include "key.h" |
| 40 | #include "cipher.h" |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 41 | #include "kex.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 42 | #include "uidswap.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 43 | #include "log.h" |
Damien Miller | 7acefbb | 2014-07-18 14:11:24 +1000 | [diff] [blame] | 44 | #include "misc.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 45 | #include "readconf.h" |
Damien Miller | 994cf14 | 2000-07-21 10:19:44 +1000 | [diff] [blame] | 46 | #include "authfd.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 47 | #include "sshconnect.h" |
| 48 | #include "authfile.h" |
Ben Lindstrom | 226cfa0 | 2001-01-22 05:34:40 +0000 | [diff] [blame] | 49 | #include "canohost.h" |
Damien Miller | d783435 | 2006-08-05 12:39:39 +1000 | [diff] [blame] | 50 | #include "hostfile.h" |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 51 | #include "auth.h" |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 52 | #include "digest.h" |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 53 | #include "ssherr.h" |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 54 | |
| 55 | /* Session id for the current session. */ |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 56 | u_char session_id[16]; |
| 57 | u_int supported_authentications = 0; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 58 | |
| 59 | extern Options options; |
| 60 | extern char *__progname; |
| 61 | |
| 62 | /* |
| 63 | * Checks if the user has an authentication agent, and if so, tries to |
| 64 | * authenticate using the agent. |
| 65 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 66 | static int |
Ben Lindstrom | 31ca54a | 2001-02-09 02:11:24 +0000 | [diff] [blame] | 67 | try_agent_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 68 | { |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 69 | int r, type, agent_fd, ret = 0; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 70 | u_char response[16]; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 71 | size_t i; |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 72 | BIGNUM *challenge; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 73 | struct ssh_identitylist *idlist = NULL; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 74 | |
| 75 | /* Get connection to the agent. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 76 | if ((r = ssh_get_authentication_socket(&agent_fd)) != 0) { |
| 77 | if (r != SSH_ERR_AGENT_NOT_PRESENT) |
| 78 | debug("%s: ssh_get_authentication_socket: %s", |
| 79 | __func__, ssh_err(r)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 80 | return 0; |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 81 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 82 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 83 | if ((challenge = BN_new()) == NULL) |
| 84 | fatal("try_agent_authentication: BN_new failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 85 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 86 | /* Loop through identities served by the agent. */ |
| 87 | if ((r = ssh_fetch_identitylist(agent_fd, 1, &idlist)) != 0) { |
| 88 | if (r != SSH_ERR_AGENT_NO_IDENTITIES) |
| 89 | debug("%s: ssh_fetch_identitylist: %s", |
| 90 | __func__, ssh_err(r)); |
| 91 | goto out; |
| 92 | } |
| 93 | for (i = 0; i < idlist->nkeys; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 94 | /* Try this identity. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 95 | debug("Trying RSA authentication via agent with '%.100s'", |
| 96 | idlist->comments[i]); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 97 | |
| 98 | /* Tell the server that we are willing to authenticate using this key. */ |
| 99 | packet_start(SSH_CMSG_AUTH_RSA); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 100 | packet_put_bignum(idlist->keys[i]->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 101 | packet_send(); |
| 102 | packet_write_wait(); |
| 103 | |
| 104 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 105 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 106 | |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 107 | /* The server sends failure if it doesn't like our key or |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 108 | does not support RSA authentication. */ |
| 109 | if (type == SSH_SMSG_FAILURE) { |
| 110 | debug("Server refused our key."); |
| 111 | continue; |
| 112 | } |
| 113 | /* Otherwise it should have sent a challenge. */ |
| 114 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 115 | packet_disconnect("Protocol error during RSA authentication: %d", |
| 116 | type); |
| 117 | |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 118 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 119 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 120 | |
| 121 | debug("Received RSA challenge from server."); |
| 122 | |
| 123 | /* Ask the agent to decrypt the challenge. */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 124 | if ((r = ssh_decrypt_challenge(agent_fd, idlist->keys[i], |
| 125 | challenge, session_id, response)) != 0) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 126 | /* |
| 127 | * The agent failed to authenticate this identifier |
| 128 | * although it advertised it supports this. Just |
| 129 | * return a wrong value. |
| 130 | */ |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 131 | logit("Authentication agent failed to decrypt " |
| 132 | "challenge: %s", ssh_err(r)); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 133 | explicit_bzero(response, sizeof(response)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 134 | } |
| 135 | debug("Sending response to RSA challenge."); |
| 136 | |
| 137 | /* Send the decrypted challenge back to the server. */ |
| 138 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 139 | for (i = 0; i < 16; i++) |
| 140 | packet_put_char(response[i]); |
| 141 | packet_send(); |
| 142 | packet_write_wait(); |
| 143 | |
| 144 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 145 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 146 | |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 147 | /* |
| 148 | * The server returns success if it accepted the |
| 149 | * authentication. |
| 150 | */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 151 | if (type == SSH_SMSG_SUCCESS) { |
Damien Miller | ad833b3 | 2000-08-23 10:46:23 +1000 | [diff] [blame] | 152 | debug("RSA authentication accepted by server."); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 153 | ret = 1; |
| 154 | break; |
| 155 | } else if (type != SSH_SMSG_FAILURE) |
| 156 | packet_disconnect("Protocol error waiting RSA auth " |
| 157 | "response: %d", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 158 | } |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 159 | if (ret != 1) |
| 160 | debug("RSA authentication using agent refused."); |
| 161 | out: |
| 162 | ssh_free_identitylist(idlist); |
| 163 | ssh_close_authentication_socket(agent_fd); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 164 | BN_clear_free(challenge); |
djm@openbsd.org | 141efe4 | 2015-01-14 20:05:27 +0000 | [diff] [blame] | 165 | return ret; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Computes the proper response to a RSA challenge, and sends the response to |
| 170 | * the server. |
| 171 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 172 | static void |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 173 | respond_to_rsa_challenge(BIGNUM * challenge, RSA * prv) |
| 174 | { |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 175 | u_char buf[32], response[16]; |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 176 | struct ssh_digest_ctx *md; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 177 | int i, len; |
| 178 | |
| 179 | /* Decrypt the challenge using the private key. */ |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 180 | /* XXX think about Bleichenbacher, too */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 181 | if (rsa_private_decrypt(challenge, challenge, prv) != 0) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 182 | packet_disconnect( |
| 183 | "respond_to_rsa_challenge: rsa_private_decrypt failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 184 | |
| 185 | /* Compute the response. */ |
| 186 | /* The response is MD5 of decrypted challenge plus session id. */ |
| 187 | len = BN_num_bytes(challenge); |
Damien Miller | eccb9de | 2005-06-17 12:59:34 +1000 | [diff] [blame] | 188 | if (len <= 0 || (u_int)len > sizeof(buf)) |
Damien Miller | 7650bc6 | 2001-01-30 09:27:26 +1100 | [diff] [blame] | 189 | packet_disconnect( |
| 190 | "respond_to_rsa_challenge: bad challenge length %d", len); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 191 | |
| 192 | memset(buf, 0, sizeof(buf)); |
| 193 | BN_bn2bin(challenge, buf + sizeof(buf) - len); |
Damien Miller | 4a1c7aa | 2014-02-04 11:03:36 +1100 | [diff] [blame] | 194 | if ((md = ssh_digest_start(SSH_DIGEST_MD5)) == NULL || |
| 195 | ssh_digest_update(md, buf, 32) < 0 || |
| 196 | ssh_digest_update(md, session_id, 16) < 0 || |
| 197 | ssh_digest_final(md, response, sizeof(response)) < 0) |
| 198 | fatal("%s: md5 failed", __func__); |
| 199 | ssh_digest_free(md); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 200 | |
| 201 | debug("Sending response to host key RSA challenge."); |
| 202 | |
| 203 | /* Send the response back to the server. */ |
| 204 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 205 | for (i = 0; i < 16; i++) |
| 206 | packet_put_char(response[i]); |
| 207 | packet_send(); |
| 208 | packet_write_wait(); |
| 209 | |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 210 | explicit_bzero(buf, sizeof(buf)); |
| 211 | explicit_bzero(response, sizeof(response)); |
| 212 | explicit_bzero(&md, sizeof(md)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Checks if the user has authentication file, and if so, tries to authenticate |
| 217 | * the user using it. |
| 218 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 219 | static int |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 220 | try_rsa_authentication(int idx) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 221 | { |
| 222 | BIGNUM *challenge; |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 223 | Key *public, *private; |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 224 | char buf[300], *passphrase = NULL, *comment, *authfile; |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 225 | int i, perm_ok = 1, type, quit; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 226 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 227 | public = options.identity_keys[idx]; |
| 228 | authfile = options.identity_files[idx]; |
| 229 | comment = xstrdup(authfile); |
| 230 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 231 | debug("Trying RSA authentication with key '%.100s'", comment); |
| 232 | |
| 233 | /* Tell the server that we are willing to authenticate using this key. */ |
| 234 | packet_start(SSH_CMSG_AUTH_RSA); |
| 235 | packet_put_bignum(public->rsa->n); |
| 236 | packet_send(); |
| 237 | packet_write_wait(); |
| 238 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 239 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 240 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 241 | |
| 242 | /* |
Damien Miller | 788f212 | 2005-11-05 15:14:59 +1100 | [diff] [blame] | 243 | * The server responds with failure if it doesn't like our key or |
| 244 | * doesn't support RSA authentication. |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 245 | */ |
| 246 | if (type == SSH_SMSG_FAILURE) { |
| 247 | debug("Server refused our key."); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 248 | free(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 249 | return 0; |
| 250 | } |
| 251 | /* Otherwise, the server should respond with a challenge. */ |
| 252 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 253 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 254 | |
| 255 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 256 | if ((challenge = BN_new()) == NULL) |
| 257 | fatal("try_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 258 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 259 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 260 | |
| 261 | debug("Received RSA challenge from server."); |
| 262 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 263 | /* |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 264 | * If the key is not stored in external hardware, we have to |
| 265 | * load the private key. Try first with empty passphrase; if it |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 266 | * fails, ask for a passphrase. |
| 267 | */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 268 | if (public->flags & SSHKEY_FLAG_EXT) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 269 | private = public; |
| 270 | else |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 271 | private = key_load_private_type(KEY_RSA1, authfile, "", NULL, |
| 272 | &perm_ok); |
| 273 | if (private == NULL && !options.batch_mode && perm_ok) { |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 274 | snprintf(buf, sizeof(buf), |
| 275 | "Enter passphrase for RSA key '%.100s': ", comment); |
| 276 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 277 | passphrase = read_passphrase(buf, 0); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 278 | if (strcmp(passphrase, "") != 0) { |
| 279 | private = key_load_private_type(KEY_RSA1, |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 280 | authfile, passphrase, NULL, NULL); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 281 | quit = 0; |
| 282 | } else { |
| 283 | debug2("no passphrase given, try next key"); |
| 284 | quit = 1; |
| 285 | } |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 286 | if (private != NULL || quit) |
| 287 | break; |
| 288 | debug2("bad passphrase given, try again..."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 289 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 290 | } |
jcs@openbsd.org | f361df4 | 2015-11-15 22:26:49 +0000 | [diff] [blame] | 291 | |
| 292 | if (private != NULL) |
| 293 | maybe_add_key_to_agent(authfile, private, comment, passphrase); |
| 294 | |
| 295 | if (passphrase != NULL) { |
| 296 | explicit_bzero(passphrase, strlen(passphrase)); |
| 297 | free(passphrase); |
| 298 | } |
| 299 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 300 | /* We no longer need the comment. */ |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 301 | free(comment); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 302 | |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 303 | if (private == NULL) { |
Darren Tucker | 232b76f | 2006-05-06 17:41:51 +1000 | [diff] [blame] | 304 | if (!options.batch_mode && perm_ok) |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 305 | error("Bad passphrase."); |
| 306 | |
| 307 | /* Send a dummy response packet to avoid protocol error. */ |
| 308 | packet_start(SSH_CMSG_AUTH_RSA_RESPONSE); |
| 309 | for (i = 0; i < 16; i++) |
| 310 | packet_put_char(0); |
| 311 | packet_send(); |
| 312 | packet_write_wait(); |
| 313 | |
| 314 | /* Expect the server to reject it... */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 315 | packet_read_expect(SSH_SMSG_FAILURE); |
Ben Lindstrom | 0520945 | 2001-06-25 05:16:02 +0000 | [diff] [blame] | 316 | BN_clear_free(challenge); |
| 317 | return 0; |
| 318 | } |
| 319 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 320 | /* Compute and send a response to the challenge. */ |
| 321 | respond_to_rsa_challenge(challenge, private->rsa); |
| 322 | |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 323 | /* Destroy the private key unless it in external hardware. */ |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 324 | if (!(private->flags & SSHKEY_FLAG_EXT)) |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 325 | key_free(private); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 326 | |
| 327 | /* We no longer need the challenge. */ |
| 328 | BN_clear_free(challenge); |
| 329 | |
| 330 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 331 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 332 | if (type == SSH_SMSG_SUCCESS) { |
| 333 | debug("RSA authentication accepted by server."); |
| 334 | return 1; |
| 335 | } |
| 336 | if (type != SSH_SMSG_FAILURE) |
| 337 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 338 | debug("RSA authentication refused."); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * Tries to authenticate the user using combined rhosts or /etc/hosts.equiv |
| 344 | * authentication and RSA host authentication. |
| 345 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 346 | static int |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 347 | try_rhosts_rsa_authentication(const char *local_user, Key * host_key) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 348 | { |
| 349 | int type; |
| 350 | BIGNUM *challenge; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 351 | |
| 352 | debug("Trying rhosts or /etc/hosts.equiv with RSA host authentication."); |
| 353 | |
| 354 | /* Tell the server that we are willing to authenticate using this key. */ |
| 355 | packet_start(SSH_CMSG_AUTH_RHOSTS_RSA); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 356 | packet_put_cstring(local_user); |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 357 | packet_put_int(BN_num_bits(host_key->rsa->n)); |
| 358 | packet_put_bignum(host_key->rsa->e); |
| 359 | packet_put_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 360 | packet_send(); |
| 361 | packet_write_wait(); |
| 362 | |
| 363 | /* Wait for server's response. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 364 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 365 | |
| 366 | /* The server responds with failure if it doesn't admit our |
| 367 | .rhosts authentication or doesn't know our host key. */ |
| 368 | if (type == SSH_SMSG_FAILURE) { |
| 369 | debug("Server refused our rhosts authentication or host key."); |
| 370 | return 0; |
| 371 | } |
| 372 | /* Otherwise, the server should respond with a challenge. */ |
| 373 | if (type != SSH_SMSG_AUTH_RSA_CHALLENGE) |
| 374 | packet_disconnect("Protocol error during RSA authentication: %d", type); |
| 375 | |
| 376 | /* Get the challenge from the packet. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 377 | if ((challenge = BN_new()) == NULL) |
| 378 | fatal("try_rhosts_rsa_authentication: BN_new failed"); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 379 | packet_get_bignum(challenge); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 380 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 381 | |
| 382 | debug("Received RSA challenge for host key from server."); |
| 383 | |
| 384 | /* Compute a response to the challenge. */ |
Ben Lindstrom | d0fca42 | 2001-03-26 13:44:06 +0000 | [diff] [blame] | 385 | respond_to_rsa_challenge(challenge, host_key->rsa); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 386 | |
| 387 | /* We no longer need the challenge. */ |
| 388 | BN_clear_free(challenge); |
| 389 | |
| 390 | /* Wait for response from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 391 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 392 | if (type == SSH_SMSG_SUCCESS) { |
| 393 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication accepted by server."); |
| 394 | return 1; |
| 395 | } |
| 396 | if (type != SSH_SMSG_FAILURE) |
| 397 | packet_disconnect("Protocol error waiting RSA auth response: %d", type); |
| 398 | debug("Rhosts or /etc/hosts.equiv with RSA host authentication refused."); |
| 399 | return 0; |
| 400 | } |
| 401 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 402 | /* |
| 403 | * Tries to authenticate with any string-based challenge/response system. |
| 404 | * Note that the client code is not tied to s/key or TIS. |
| 405 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 406 | static int |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 407 | try_challenge_response_authentication(void) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 408 | { |
| 409 | int type, i; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 410 | u_int clen; |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 411 | char prompt[1024]; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 412 | char *challenge, *response; |
Ben Lindstrom | bdfb4df | 2001-10-03 17:12:43 +0000 | [diff] [blame] | 413 | |
| 414 | debug("Doing challenge response authentication."); |
| 415 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 416 | for (i = 0; i < options.number_of_password_prompts; i++) { |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 417 | /* request a challenge */ |
| 418 | packet_start(SSH_CMSG_AUTH_TIS); |
| 419 | packet_send(); |
| 420 | packet_write_wait(); |
| 421 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 422 | type = packet_read(); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 423 | if (type != SSH_SMSG_FAILURE && |
| 424 | type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
| 425 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 426 | "to SSH_CMSG_AUTH_TIS", type); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 427 | } |
| 428 | if (type != SSH_SMSG_AUTH_TIS_CHALLENGE) { |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 429 | debug("No challenge."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | } |
| 432 | challenge = packet_get_string(&clen); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 433 | packet_check_eom(); |
Ben Lindstrom | db65e8f | 2001-01-19 04:26:52 +0000 | [diff] [blame] | 434 | snprintf(prompt, sizeof prompt, "%s%s", challenge, |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 435 | strchr(challenge, '\n') ? "" : "\nResponse: "); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 436 | free(challenge); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 437 | if (i != 0) |
| 438 | error("Permission denied, please try again."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 439 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 440 | logit("WARNING: Encryption is disabled! " |
Damien Miller | f61c015 | 2002-04-23 20:56:02 +1000 | [diff] [blame] | 441 | "Response will be transmitted in clear text."); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 442 | response = read_passphrase(prompt, 0); |
| 443 | if (strcmp(response, "") == 0) { |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 444 | free(response); |
Ben Lindstrom | a65c78a | 2000-12-10 22:57:30 +0000 | [diff] [blame] | 445 | break; |
| 446 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 447 | packet_start(SSH_CMSG_AUTH_TIS_RESPONSE); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 448 | ssh_put_password(response); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 449 | explicit_bzero(response, strlen(response)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 450 | free(response); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 451 | packet_send(); |
| 452 | packet_write_wait(); |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 453 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 454 | if (type == SSH_SMSG_SUCCESS) |
| 455 | return 1; |
| 456 | if (type != SSH_SMSG_FAILURE) |
| 457 | packet_disconnect("Protocol error: got %d in response " |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 458 | "to SSH_CMSG_AUTH_TIS_RESPONSE", type); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 459 | } |
| 460 | /* failure */ |
| 461 | return 0; |
| 462 | } |
| 463 | |
| 464 | /* |
| 465 | * Tries to authenticate with plain passwd authentication. |
| 466 | */ |
Ben Lindstrom | bba8121 | 2001-06-25 05:01:22 +0000 | [diff] [blame] | 467 | static int |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 468 | try_password_authentication(char *prompt) |
| 469 | { |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 470 | int type, i; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 471 | char *password; |
| 472 | |
| 473 | debug("Doing password authentication."); |
| 474 | if (options.cipher == SSH_CIPHER_NONE) |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 475 | logit("WARNING: Encryption is disabled! Password will be transmitted in clear text."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 476 | for (i = 0; i < options.number_of_password_prompts; i++) { |
| 477 | if (i != 0) |
| 478 | error("Permission denied, please try again."); |
| 479 | password = read_passphrase(prompt, 0); |
| 480 | packet_start(SSH_CMSG_AUTH_PASSWORD); |
Damien Miller | 79438cc | 2001-02-16 12:34:57 +1100 | [diff] [blame] | 481 | ssh_put_password(password); |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 482 | explicit_bzero(password, strlen(password)); |
Darren Tucker | a627d42 | 2013-06-02 07:31:17 +1000 | [diff] [blame] | 483 | free(password); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 484 | packet_send(); |
| 485 | packet_write_wait(); |
| 486 | |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 487 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 488 | if (type == SSH_SMSG_SUCCESS) |
| 489 | return 1; |
| 490 | if (type != SSH_SMSG_FAILURE) |
| 491 | packet_disconnect("Protocol error: got %d in response to passwd auth", type); |
| 492 | } |
| 493 | /* failure */ |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | /* |
| 498 | * SSH1 key exchange |
| 499 | */ |
| 500 | void |
| 501 | ssh_kex(char *host, struct sockaddr *hostaddr) |
| 502 | { |
| 503 | int i; |
| 504 | BIGNUM *key; |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 505 | Key *host_key, *server_key; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 506 | int bits, rbits; |
| 507 | int ssh_cipher_default = SSH_CIPHER_3DES; |
Ben Lindstrom | 46c1622 | 2000-12-22 01:43:59 +0000 | [diff] [blame] | 508 | u_char session_key[SSH_SESSION_KEY_LENGTH]; |
| 509 | u_char cookie[8]; |
| 510 | u_int supported_ciphers; |
| 511 | u_int server_flags, client_flags; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 512 | |
| 513 | debug("Waiting for server public key."); |
| 514 | |
| 515 | /* Wait for a public key packet from the server. */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 516 | packet_read_expect(SSH_SMSG_PUBLIC_KEY); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 517 | |
| 518 | /* Get cookie from the packet. */ |
| 519 | for (i = 0; i < 8; i++) |
| 520 | cookie[i] = packet_get_char(); |
| 521 | |
| 522 | /* Get the public key. */ |
dtucker@openbsd.org | 66be4fe | 2017-03-10 03:53:11 +0000 | [diff] [blame] | 523 | if ((server_key = key_new(KEY_RSA1)) == NULL) |
| 524 | fatal("%s: key_new(KEY_RSA1) failed", __func__); |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 525 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 526 | packet_get_bignum(server_key->rsa->e); |
| 527 | packet_get_bignum(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 528 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 529 | rbits = BN_num_bits(server_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 530 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 531 | logit("Warning: Server lies about size of server public key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 532 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 533 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 534 | } |
| 535 | /* Get the host key. */ |
dtucker@openbsd.org | 66be4fe | 2017-03-10 03:53:11 +0000 | [diff] [blame] | 536 | if ((host_key = key_new(KEY_RSA1)) == NULL) |
| 537 | fatal("%s: key_new(KEY_RSA1) failed", __func__); |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 538 | bits = packet_get_int(); |
Damien Miller | d432ccf | 2002-01-22 23:14:44 +1100 | [diff] [blame] | 539 | packet_get_bignum(host_key->rsa->e); |
| 540 | packet_get_bignum(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 541 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 542 | rbits = BN_num_bits(host_key->rsa->n); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 543 | if (bits != rbits) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 544 | logit("Warning: Server lies about size of server host key: " |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 545 | "actual size is %d bits vs. announced %d.", rbits, bits); |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 546 | logit("Warning: This may be due to an old implementation of ssh."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Get protocol flags. */ |
| 550 | server_flags = packet_get_int(); |
| 551 | packet_set_protocol_flags(server_flags); |
| 552 | |
| 553 | supported_ciphers = packet_get_int(); |
| 554 | supported_authentications = packet_get_int(); |
Damien Miller | 48b03fc | 2002-01-22 23:11:40 +1100 | [diff] [blame] | 555 | packet_check_eom(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 556 | |
| 557 | debug("Received server public key (%d bits) and host key (%d bits).", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 558 | BN_num_bits(server_key->rsa->n), BN_num_bits(host_key->rsa->n)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 559 | |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 560 | if (verify_host_key(host, hostaddr, host_key) == -1) |
Damien Miller | 59d9fb9 | 2001-10-10 15:03:11 +1000 | [diff] [blame] | 561 | fatal("Host key verification failed."); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 562 | |
| 563 | client_flags = SSH_PROTOFLAG_SCREEN_NUMBER | SSH_PROTOFLAG_HOST_IN_FWD_OPEN; |
| 564 | |
Darren Tucker | e14e005 | 2004-05-13 16:30:44 +1000 | [diff] [blame] | 565 | derive_ssh1_session_id(host_key->rsa->n, server_key->rsa->n, cookie, session_id); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 566 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 567 | /* |
| 568 | * Generate an encryption key for the session. The key is a 256 bit |
| 569 | * random number, interpreted as a 32-byte key, with the least |
| 570 | * significant 8 bits being the first byte of the key. |
| 571 | */ |
natano@openbsd.org | 4927108 | 2016-09-19 07:52:42 +0000 | [diff] [blame] | 572 | arc4random_buf(session_key, sizeof(session_key)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 573 | |
| 574 | /* |
| 575 | * According to the protocol spec, the first byte of the session key |
| 576 | * is the highest byte of the integer. The session key is xored with |
| 577 | * the first 16 bytes of the session id. |
| 578 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 579 | if ((key = BN_new()) == NULL) |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 580 | fatal("ssh_kex: BN_new failed"); |
| 581 | if (BN_set_word(key, 0) == 0) |
| 582 | fatal("ssh_kex: BN_set_word failed"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 583 | for (i = 0; i < SSH_SESSION_KEY_LENGTH; i++) { |
Darren Tucker | 0bc8557 | 2006-11-07 23:14:41 +1100 | [diff] [blame] | 584 | if (BN_lshift(key, key, 8) == 0) |
| 585 | fatal("ssh_kex: BN_lshift failed"); |
| 586 | if (i < 16) { |
| 587 | if (BN_add_word(key, session_key[i] ^ session_id[i]) |
| 588 | == 0) |
| 589 | fatal("ssh_kex: BN_add_word failed"); |
| 590 | } else { |
| 591 | if (BN_add_word(key, session_key[i]) == 0) |
| 592 | fatal("ssh_kex: BN_add_word failed"); |
| 593 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /* |
| 597 | * Encrypt the integer using the public key and host key of the |
| 598 | * server (key with smaller modulus first). |
| 599 | */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 600 | if (BN_cmp(server_key->rsa->n, host_key->rsa->n) < 0) { |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 601 | /* Public key has smaller modulus. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 602 | if (BN_num_bits(host_key->rsa->n) < |
| 603 | BN_num_bits(server_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 604 | fatal("respond_to_rsa_challenge: host_key %d < server_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 605 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 606 | BN_num_bits(host_key->rsa->n), |
| 607 | BN_num_bits(server_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 608 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 609 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 610 | if (rsa_public_encrypt(key, key, server_key->rsa) != 0 || |
| 611 | rsa_public_encrypt(key, key, host_key->rsa) != 0) |
| 612 | fatal("%s: rsa_public_encrypt failed", __func__); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 613 | } else { |
| 614 | /* Host key has smaller modulus (or they are equal). */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 615 | if (BN_num_bits(server_key->rsa->n) < |
| 616 | BN_num_bits(host_key->rsa->n) + SSH_KEY_BITS_RESERVED) { |
| 617 | fatal("respond_to_rsa_challenge: server_key %d < host_key %d + " |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 618 | "SSH_KEY_BITS_RESERVED %d", |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 619 | BN_num_bits(server_key->rsa->n), |
| 620 | BN_num_bits(host_key->rsa->n), |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 621 | SSH_KEY_BITS_RESERVED); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 622 | } |
Damien Miller | 8668706 | 2014-07-02 15:28:02 +1000 | [diff] [blame] | 623 | if (rsa_public_encrypt(key, key, host_key->rsa) != 0 || |
| 624 | rsa_public_encrypt(key, key, server_key->rsa) != 0) |
| 625 | fatal("%s: rsa_public_encrypt failed", __func__); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | /* Destroy the public keys since we no longer need them. */ |
Damien Miller | da75516 | 2002-01-22 23:09:22 +1100 | [diff] [blame] | 629 | key_free(server_key); |
| 630 | key_free(host_key); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 631 | |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 632 | if (options.cipher == SSH_CIPHER_NOT_SET) { |
| 633 | if (cipher_mask_ssh1(1) & supported_ciphers & (1 << ssh_cipher_default)) |
| 634 | options.cipher = ssh_cipher_default; |
Darren Tucker | 5cb30ad | 2004-08-12 22:40:24 +1000 | [diff] [blame] | 635 | } else if (options.cipher == SSH_CIPHER_INVALID || |
Damien Miller | e39cacc | 2000-11-29 12:18:44 +1100 | [diff] [blame] | 636 | !(cipher_mask_ssh1(1) & (1 << options.cipher))) { |
Damien Miller | 996acd2 | 2003-04-09 20:59:48 +1000 | [diff] [blame] | 637 | logit("No valid SSH1 cipher, using %.100s instead.", |
Damien Miller | 874d77b | 2000-10-14 16:23:11 +1100 | [diff] [blame] | 638 | cipher_name(ssh_cipher_default)); |
| 639 | options.cipher = ssh_cipher_default; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 640 | } |
| 641 | /* Check that the selected cipher is supported. */ |
| 642 | if (!(supported_ciphers & (1 << options.cipher))) |
| 643 | fatal("Selected cipher type %.100s not supported by server.", |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 644 | cipher_name(options.cipher)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 645 | |
| 646 | debug("Encryption type: %.100s", cipher_name(options.cipher)); |
| 647 | |
| 648 | /* Send the encrypted session key to the server. */ |
| 649 | packet_start(SSH_CMSG_SESSION_KEY); |
| 650 | packet_put_char(options.cipher); |
| 651 | |
| 652 | /* Send the cookie back to the server. */ |
| 653 | for (i = 0; i < 8; i++) |
| 654 | packet_put_char(cookie[i]); |
| 655 | |
| 656 | /* Send and destroy the encrypted encryption key integer. */ |
| 657 | packet_put_bignum(key); |
| 658 | BN_clear_free(key); |
| 659 | |
| 660 | /* Send protocol flags. */ |
| 661 | packet_put_int(client_flags); |
| 662 | |
| 663 | /* Send the packet now. */ |
| 664 | packet_send(); |
| 665 | packet_write_wait(); |
| 666 | |
| 667 | debug("Sent encrypted session key."); |
| 668 | |
| 669 | /* Set the encryption key. */ |
| 670 | packet_set_encryption_key(session_key, SSH_SESSION_KEY_LENGTH, options.cipher); |
| 671 | |
Damien Miller | a5103f4 | 2014-02-04 11:20:14 +1100 | [diff] [blame] | 672 | /* |
| 673 | * We will no longer need the session key here. |
| 674 | * Destroy any extra copies. |
| 675 | */ |
| 676 | explicit_bzero(session_key, sizeof(session_key)); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * Expect a success message from the server. Note that this message |
| 680 | * will be received in encrypted form. |
| 681 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 682 | packet_read_expect(SSH_SMSG_SUCCESS); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 683 | |
| 684 | debug("Received encrypted confirmation."); |
| 685 | } |
| 686 | |
| 687 | /* |
| 688 | * Authenticate user |
| 689 | */ |
| 690 | void |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 691 | ssh_userauth1(const char *local_user, const char *server_user, char *host, |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 692 | Sensitive *sensitive) |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 693 | { |
| 694 | int i, type; |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 695 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 696 | if (supported_authentications == 0) |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 697 | fatal("ssh_userauth1: server supports no auth methods"); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 698 | |
| 699 | /* Send the name of the user to log in as on the server. */ |
| 700 | packet_start(SSH_CMSG_USER); |
Ben Lindstrom | 664408d | 2001-06-09 01:42:01 +0000 | [diff] [blame] | 701 | packet_put_cstring(server_user); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 702 | packet_send(); |
| 703 | packet_write_wait(); |
| 704 | |
| 705 | /* |
| 706 | * The server should respond with success if no authentication is |
| 707 | * needed (the user has no password). Otherwise the server responds |
| 708 | * with failure. |
| 709 | */ |
Damien Miller | dff5099 | 2002-01-22 23:16:32 +1100 | [diff] [blame] | 710 | type = packet_read(); |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 711 | |
| 712 | /* check whether the connection was accepted without authentication. */ |
| 713 | if (type == SSH_SMSG_SUCCESS) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 714 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 715 | if (type != SSH_SMSG_FAILURE) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 716 | packet_disconnect("Protocol error: got %d in response to SSH_CMSG_USER", type); |
Damien Miller | 9f0f5c6 | 2001-12-21 14:45:46 +1100 | [diff] [blame] | 717 | |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 718 | /* |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 719 | * Try .rhosts or /etc/hosts.equiv authentication with RSA host |
| 720 | * authentication. |
| 721 | */ |
| 722 | if ((supported_authentications & (1 << SSH_AUTH_RHOSTS_RSA)) && |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 723 | options.rhosts_rsa_authentication) { |
Ben Lindstrom | 1bad256 | 2002-06-06 19:57:33 +0000 | [diff] [blame] | 724 | for (i = 0; i < sensitive->nkeys; i++) { |
| 725 | if (sensitive->keys[i] != NULL && |
| 726 | sensitive->keys[i]->type == KEY_RSA1 && |
| 727 | try_rhosts_rsa_authentication(local_user, |
| 728 | sensitive->keys[i])) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 729 | goto success; |
Ben Lindstrom | 5eabda3 | 2001-04-12 23:34:34 +0000 | [diff] [blame] | 730 | } |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 731 | } |
| 732 | /* Try RSA authentication if the server supports it. */ |
| 733 | if ((supported_authentications & (1 << SSH_AUTH_RSA)) && |
| 734 | options.rsa_authentication) { |
| 735 | /* |
| 736 | * Try RSA authentication using the authentication agent. The |
| 737 | * agent is tried first because no passphrase is needed for |
| 738 | * it, whereas identity files may require passphrases. |
| 739 | */ |
| 740 | if (try_agent_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 741 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 742 | |
| 743 | /* Try RSA authentication for each identity. */ |
| 744 | for (i = 0; i < options.num_identity_files; i++) |
Ben Lindstrom | 266dfdf | 2001-03-09 00:12:22 +0000 | [diff] [blame] | 745 | if (options.identity_keys[i] != NULL && |
| 746 | options.identity_keys[i]->type == KEY_RSA1 && |
Ben Lindstrom | c5b6800 | 2001-07-04 04:52:03 +0000 | [diff] [blame] | 747 | try_rsa_authentication(i)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 748 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 749 | } |
Ben Lindstrom | 95fb2dd | 2001-01-23 03:12:10 +0000 | [diff] [blame] | 750 | /* Try challenge response authentication if the server supports it. */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 751 | if ((supported_authentications & (1 << SSH_AUTH_TIS)) && |
Ben Lindstrom | 551ea37 | 2001-06-05 18:56:16 +0000 | [diff] [blame] | 752 | options.challenge_response_authentication && !options.batch_mode) { |
| 753 | if (try_challenge_response_authentication()) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 754 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 755 | } |
| 756 | /* Try password authentication if the server supports it. */ |
| 757 | if ((supported_authentications & (1 << SSH_AUTH_PASSWORD)) && |
| 758 | options.password_authentication && !options.batch_mode) { |
| 759 | char prompt[80]; |
| 760 | |
Ben Lindstrom | e055716 | 2001-02-11 00:00:24 +0000 | [diff] [blame] | 761 | snprintf(prompt, sizeof(prompt), "%.30s@%.128s's password: ", |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 762 | server_user, host); |
| 763 | if (try_password_authentication(prompt)) |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 764 | goto success; |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 765 | } |
| 766 | /* All authentication methods have failed. Exit with an error message. */ |
| 767 | fatal("Permission denied."); |
| 768 | /* NOTREACHED */ |
Ben Lindstrom | ec95ed9 | 2001-07-04 04:21:14 +0000 | [diff] [blame] | 769 | |
| 770 | success: |
Damien Miller | 40eb1d8 | 2001-07-14 12:16:59 +1000 | [diff] [blame] | 771 | return; /* need statement after label */ |
Damien Miller | eba71ba | 2000-04-29 23:57:08 +1000 | [diff] [blame] | 772 | } |
Damien Miller | 76c0480 | 2015-01-13 19:38:18 +1100 | [diff] [blame] | 773 | |
| 774 | #endif /* WITH_SSH1 */ |