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