Annotation of src/usr.bin/ssh/auth-rsa.c, Revision 1.68
1.68 ! stevesk 1: /* $OpenBSD: auth-rsa.c,v 1.67 2006/03/25 18:29:35 deraadt Exp $ */
1.1 provos 2: /*
1.13 deraadt 3: * Author: Tatu Ylonen <ylo@cs.hut.fi>
4: * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
5: * All rights reserved
6: * RSA-based authentication. This code determines whether to admit a login
7: * based on RSA authentication. This file also contains functions to check
8: * validity of the host key.
1.21 markus 9: *
1.28 deraadt 10: * As far as I am concerned, the code I have written for this software
11: * can be used freely for any purpose. Any derived versions of this
12: * software must be clearly marked as such, and if the derived work is
13: * incompatible with the protocol description in the RFC file, it must be
14: * called by a name other than "ssh" or "Secure Shell".
1.13 deraadt 15: */
1.1 provos 16:
17: #include "includes.h"
1.64 stevesk 18:
19: #include <sys/types.h>
20: #include <sys/stat.h>
1.38 markus 21:
22: #include <openssl/rsa.h>
23: #include <openssl/md5.h>
1.68 ! stevesk 24:
! 25: #include <pwd.h>
1.1 provos 26:
27: #include "rsa.h"
28: #include "packet.h"
29: #include "xmalloc.h"
1.35 markus 30: #include "ssh1.h"
1.1 provos 31: #include "uidswap.h"
1.19 markus 32: #include "match.h"
1.25 markus 33: #include "auth-options.h"
1.35 markus 34: #include "pathnames.h"
1.38 markus 35: #include "log.h"
36: #include "servconf.h"
37: #include "auth.h"
1.46 jakob 38: #include "hostfile.h"
1.52 provos 39: #include "monitor_wrap.h"
1.55 stevesk 40: #include "ssh.h"
1.62 dtucker 41: #include "misc.h"
1.30 markus 42:
43: /* import */
44: extern ServerOptions options;
45:
1.14 markus 46: /*
47: * Session identifier that is used to bind key exchange and authentication
48: * responses to a particular session.
49: */
1.34 markus 50: extern u_char session_id[16];
1.1 provos 51:
1.14 markus 52: /*
53: * The .ssh/authorized_keys file contains public keys, one per line, in the
54: * following format:
55: * options bits e n comment
56: * where bits, e and n are decimal numbers,
57: * and comment is any string of characters up to newline. The maximum
1.61 dtucker 58: * length of a line is SSH_MAX_PUBKEY_BYTES characters. See sshd(8) for a
1.14 markus 59: * description of the options.
60: */
61:
1.52 provos 62: BIGNUM *
1.51 markus 63: auth_rsa_generate_challenge(Key *key)
64: {
65: BIGNUM *challenge;
66: BN_CTX *ctx;
67:
68: if ((challenge = BN_new()) == NULL)
69: fatal("auth_rsa_generate_challenge: BN_new() failed");
70: /* Generate a random challenge. */
71: BN_rand(challenge, 256, 0, 0);
72: if ((ctx = BN_CTX_new()) == NULL)
73: fatal("auth_rsa_generate_challenge: BN_CTX_new() failed");
74: BN_mod(challenge, challenge, key->rsa->n, ctx);
75: BN_CTX_free(ctx);
76:
77: return challenge;
78: }
79:
1.52 provos 80: int
1.51 markus 81: auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
82: {
83: u_char buf[32], mdbuf[16];
84: MD5_CTX md;
85: int len;
1.54 markus 86:
87: /* don't allow short keys */
1.55 stevesk 88: if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
1.56 stevesk 89: error("auth_rsa_verify_response: RSA modulus too small: %d < minimum %d bits",
90: BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
1.54 markus 91: return (0);
92: }
1.51 markus 93:
94: /* The response is MD5 of decrypted challenge plus session id. */
95: len = BN_num_bytes(challenge);
96: if (len <= 0 || len > 32)
97: fatal("auth_rsa_verify_response: bad challenge length %d", len);
98: memset(buf, 0, 32);
99: BN_bn2bin(challenge, buf + 32 - len);
100: MD5_Init(&md);
101: MD5_Update(&md, buf, 32);
102: MD5_Update(&md, session_id, 16);
103: MD5_Final(mdbuf, &md);
104:
105: /* Verify that the response is the original challenge. */
106: if (memcmp(response, mdbuf, 16) != 0) {
107: /* Wrong answer. */
108: return (0);
109: }
110: /* Correct answer. */
111: return (1);
112: }
113:
1.14 markus 114: /*
115: * Performs the RSA authentication challenge-response dialog with the client,
116: * and returns true (non-zero) if the client gave the correct answer to
117: * our challenge; returns zero if the client gives a wrong answer.
118: */
1.1 provos 119:
120: int
1.51 markus 121: auth_rsa_challenge_dialog(Key *key)
1.1 provos 122: {
1.18 markus 123: BIGNUM *challenge, *encrypted_challenge;
1.51 markus 124: u_char response[16];
125: int i, success;
1.12 markus 126:
1.47 markus 127: if ((encrypted_challenge = BN_new()) == NULL)
128: fatal("auth_rsa_challenge_dialog: BN_new() failed");
1.12 markus 129:
1.52 provos 130: challenge = PRIVSEP(auth_rsa_generate_challenge(key));
1.12 markus 131:
132: /* Encrypt the challenge with the public key. */
1.51 markus 133: rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
1.12 markus 134:
135: /* Send the encrypted challenge to the client. */
136: packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
137: packet_put_bignum(encrypted_challenge);
138: packet_send();
1.18 markus 139: BN_clear_free(encrypted_challenge);
1.12 markus 140: packet_write_wait();
141:
1.18 markus 142: /* Wait for a response. */
1.50 markus 143: packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
1.18 markus 144: for (i = 0; i < 16; i++)
1.67 deraadt 145: response[i] = (u_char)packet_get_char();
1.49 markus 146: packet_check_eom();
1.18 markus 147:
1.52 provos 148: success = PRIVSEP(auth_rsa_verify_response(key, challenge, response));
1.12 markus 149: BN_clear_free(challenge);
1.51 markus 150: return (success);
1.1 provos 151: }
152:
1.14 markus 153: /*
1.51 markus 154: * check if there's user key matching client_n,
155: * return key if login is allowed, NULL otherwise
1.14 markus 156: */
1.1 provos 157:
1.52 provos 158: int
1.51 markus 159: auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1.1 provos 160: {
1.61 dtucker 161: char line[SSH_MAX_PUBKEY_BYTES], *file;
1.52 provos 162: int allowed = 0;
1.34 markus 163: u_int bits;
1.12 markus 164: FILE *f;
1.34 markus 165: u_long linenum = 0;
1.12 markus 166: struct stat st;
1.46 jakob 167: Key *key;
1.12 markus 168:
169: /* Temporarily use the user's uid. */
1.40 markus 170: temporarily_use_uid(pw);
1.12 markus 171:
172: /* The authorized keys. */
1.41 markus 173: file = authorized_keys_file(pw);
174: debug("trying public RSA key file %s", file);
1.12 markus 175:
176: /* Fail quietly if file does not exist */
177: if (stat(file, &st) < 0) {
178: /* Restore the privileged uid. */
179: restore_uid();
1.41 markus 180: xfree(file);
1.53 markus 181: return (0);
1.12 markus 182: }
183: /* Open the file containing the authorized keys. */
184: f = fopen(file, "r");
185: if (!f) {
186: /* Restore the privileged uid. */
187: restore_uid();
1.41 markus 188: xfree(file);
1.53 markus 189: return (0);
1.1 provos 190: }
1.41 markus 191: if (options.strict_modes &&
1.43 provos 192: secure_filename(f, file, pw, line, sizeof(line)) != 0) {
1.41 markus 193: xfree(file);
194: fclose(f);
1.57 itojun 195: logit("Authentication refused: %s", line);
1.41 markus 196: restore_uid();
1.53 markus 197: return (0);
1.1 provos 198: }
1.51 markus 199:
200: /* Flag indicating whether the key is allowed. */
201: allowed = 0;
1.1 provos 202:
1.46 jakob 203: key = key_new(KEY_RSA1);
1.12 markus 204:
1.14 markus 205: /*
206: * Go though the accepted keys, looking for the current key. If
207: * found, perform a challenge-response dialog to verify that the
208: * user really has the corresponding private key.
209: */
1.61 dtucker 210: while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
1.12 markus 211: char *cp;
1.60 avsm 212: char *key_options;
1.63 djm 213: int keybits;
1.12 markus 214:
1.14 markus 215: /* Skip leading whitespace, empty and comment lines. */
216: for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
217: ;
1.12 markus 218: if (!*cp || *cp == '\n' || *cp == '#')
219: continue;
220:
1.14 markus 221: /*
222: * Check if there are options for this key, and if so,
223: * save their starting address and skip the option part
224: * for now. If there are no options, set the starting
225: * address to NULL.
226: */
1.12 markus 227: if (*cp < '0' || *cp > '9') {
228: int quoted = 0;
1.60 avsm 229: key_options = cp;
1.12 markus 230: for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
231: if (*cp == '\\' && cp[1] == '"')
232: cp++; /* Skip both */
233: else if (*cp == '"')
234: quoted = !quoted;
235: }
236: } else
1.60 avsm 237: key_options = NULL;
1.1 provos 238:
1.12 markus 239: /* Parse the key from the line. */
1.46 jakob 240: if (hostfile_read_key(&cp, &bits, key) == 0) {
1.42 markus 241: debug("%.100s, line %lu: non ssh1 key syntax",
1.36 markus 242: file, linenum);
1.12 markus 243: continue;
1.1 provos 244: }
1.12 markus 245: /* cp now points to the comment part. */
246:
1.16 markus 247: /* Check if the we have found the desired key (identified by its modulus). */
1.46 jakob 248: if (BN_cmp(key->rsa->n, client_n) != 0)
1.16 markus 249: continue;
250:
1.12 markus 251: /* check the real bits */
1.63 djm 252: keybits = BN_num_bits(key->rsa->n);
253: if (keybits < 0 || bits != (u_int)keybits)
1.57 itojun 254: logit("Warning: %s, line %lu: keysize mismatch: "
1.15 markus 255: "actual %d vs. announced %d.",
1.46 jakob 256: file, linenum, BN_num_bits(key->rsa->n), bits);
1.12 markus 257:
258: /* We have found the desired key. */
1.33 markus 259: /*
260: * If our options do not allow this key to be used,
261: * do not send challenge.
262: */
1.60 avsm 263: if (!auth_parse_options(pw, key_options, file, linenum))
1.33 markus 264: continue;
1.19 markus 265:
1.51 markus 266: /* break out, this key is allowed */
267: allowed = 1;
1.32 markus 268: break;
1.1 provos 269: }
270:
1.12 markus 271: /* Restore the privileged uid. */
272: restore_uid();
273:
274: /* Close the file. */
1.41 markus 275: xfree(file);
1.12 markus 276: fclose(f);
277:
1.51 markus 278: /* return key if allowed */
279: if (allowed && rkey != NULL)
280: *rkey = key;
281: else
282: key_free(key);
283: return (allowed);
284: }
285:
286: /*
287: * Performs the RSA authentication dialog with the client. This returns
288: * 0 if the client could not be authenticated, and 1 if authentication was
289: * successful. This may exit if there is a serious protocol violation.
290: */
291: int
1.58 djm 292: auth_rsa(Authctxt *authctxt, BIGNUM *client_n)
1.51 markus 293: {
294: Key *key;
295: char *fp;
1.58 djm 296: struct passwd *pw = authctxt->pw;
1.51 markus 297:
298: /* no user given */
1.58 djm 299: if (!authctxt->valid)
1.51 markus 300: return 0;
1.1 provos 301:
1.52 provos 302: if (!PRIVSEP(auth_rsa_key_allowed(pw, client_n, &key))) {
1.31 markus 303: auth_clear_options();
1.51 markus 304: return (0);
305: }
306:
307: /* Perform the challenge-response dialog for this key. */
308: if (!auth_rsa_challenge_dialog(key)) {
309: /* Wrong response. */
310: verbose("Wrong response to RSA authentication challenge.");
311: packet_send_debug("Wrong response to RSA authentication challenge.");
312: /*
313: * Break out of the loop. Otherwise we might send
314: * another challenge and break the protocol.
315: */
316: key_free(key);
317: return (0);
318: }
319: /*
320: * Correct response. The client has been successfully
321: * authenticated. Note that we have not yet processed the
322: * options; this will be reset if the options cause the
323: * authentication to be rejected.
324: */
325: fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
326: verbose("Found matching %s key: %s",
327: key_type(key), fp);
328: xfree(fp);
329: key_free(key);
1.1 provos 330:
1.51 markus 331: packet_send_debug("RSA authentication accepted.");
332: return (1);
1.1 provos 333: }