17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * Author: Tatu Ylonen <ylo@cs.hut.fi>
37c478bd9Sstevel@tonic-gate * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
47c478bd9Sstevel@tonic-gate * All rights reserved
57c478bd9Sstevel@tonic-gate * RSA-based authentication. This code determines whether to admit a login
67c478bd9Sstevel@tonic-gate * based on RSA authentication. This file also contains functions to check
77c478bd9Sstevel@tonic-gate * validity of the host key.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * As far as I am concerned, the code I have written for this software
107c478bd9Sstevel@tonic-gate * can be used freely for any purpose. Any derived versions of this
117c478bd9Sstevel@tonic-gate * software must be clearly marked as such, and if the derived work is
127c478bd9Sstevel@tonic-gate * incompatible with the protocol description in the RFC file, it must be
137c478bd9Sstevel@tonic-gate * called by a name other than "ssh" or "Secure Shell".
147c478bd9Sstevel@tonic-gate */
157c478bd9Sstevel@tonic-gate
167c478bd9Sstevel@tonic-gate #include "includes.h"
177c478bd9Sstevel@tonic-gate RCSID("$OpenBSD: auth-rsa.c,v 1.56 2002/06/10 16:53:06 stevesk Exp $");
187c478bd9Sstevel@tonic-gate
197c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate #include <openssl/rsa.h>
227c478bd9Sstevel@tonic-gate #include <openssl/md5.h>
237c478bd9Sstevel@tonic-gate
247c478bd9Sstevel@tonic-gate #include "rsa.h"
257c478bd9Sstevel@tonic-gate #include "packet.h"
267c478bd9Sstevel@tonic-gate #include "xmalloc.h"
277c478bd9Sstevel@tonic-gate #include "ssh1.h"
287c478bd9Sstevel@tonic-gate #include "mpaux.h"
297c478bd9Sstevel@tonic-gate #include "uidswap.h"
307c478bd9Sstevel@tonic-gate #include "match.h"
317c478bd9Sstevel@tonic-gate #include "auth-options.h"
327c478bd9Sstevel@tonic-gate #include "pathnames.h"
337c478bd9Sstevel@tonic-gate #include "log.h"
347c478bd9Sstevel@tonic-gate #include "servconf.h"
357c478bd9Sstevel@tonic-gate #include "auth.h"
367c478bd9Sstevel@tonic-gate #include "hostfile.h"
377c478bd9Sstevel@tonic-gate #include "ssh.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /* import */
407c478bd9Sstevel@tonic-gate extern ServerOptions options;
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate /*
437c478bd9Sstevel@tonic-gate * Session identifier that is used to bind key exchange and authentication
447c478bd9Sstevel@tonic-gate * responses to a particular session.
457c478bd9Sstevel@tonic-gate */
467c478bd9Sstevel@tonic-gate extern u_char session_id[16];
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate * The .ssh/authorized_keys file contains public keys, one per line, in the
507c478bd9Sstevel@tonic-gate * following format:
517c478bd9Sstevel@tonic-gate * options bits e n comment
527c478bd9Sstevel@tonic-gate * where bits, e and n are decimal numbers,
537c478bd9Sstevel@tonic-gate * and comment is any string of characters up to newline. The maximum
547c478bd9Sstevel@tonic-gate * length of a line is 8000 characters. See the documentation for a
557c478bd9Sstevel@tonic-gate * description of the options.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate BIGNUM *
auth_rsa_generate_challenge(Key * key)597c478bd9Sstevel@tonic-gate auth_rsa_generate_challenge(Key *key)
607c478bd9Sstevel@tonic-gate {
617c478bd9Sstevel@tonic-gate BIGNUM *challenge;
627c478bd9Sstevel@tonic-gate BN_CTX *ctx;
637c478bd9Sstevel@tonic-gate
647c478bd9Sstevel@tonic-gate if ((challenge = BN_new()) == NULL)
657c478bd9Sstevel@tonic-gate fatal("auth_rsa_generate_challenge: BN_new() failed");
667c478bd9Sstevel@tonic-gate /* Generate a random challenge. */
677c478bd9Sstevel@tonic-gate BN_rand(challenge, 256, 0, 0);
687c478bd9Sstevel@tonic-gate if ((ctx = BN_CTX_new()) == NULL)
697c478bd9Sstevel@tonic-gate fatal("auth_rsa_generate_challenge: BN_CTX_new() failed");
707c478bd9Sstevel@tonic-gate BN_mod(challenge, challenge, key->rsa->n, ctx);
717c478bd9Sstevel@tonic-gate BN_CTX_free(ctx);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate return challenge;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate int
auth_rsa_verify_response(Key * key,BIGNUM * challenge,u_char response[16])777c478bd9Sstevel@tonic-gate auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate u_char buf[32], mdbuf[16];
807c478bd9Sstevel@tonic-gate MD5_CTX md;
817c478bd9Sstevel@tonic-gate int len;
827c478bd9Sstevel@tonic-gate
837c478bd9Sstevel@tonic-gate /* don't allow short keys */
847c478bd9Sstevel@tonic-gate if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
857c478bd9Sstevel@tonic-gate error("auth_rsa_verify_response: RSA modulus too small: %d < minimum %d bits",
867c478bd9Sstevel@tonic-gate BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
877c478bd9Sstevel@tonic-gate return (0);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate /* The response is MD5 of decrypted challenge plus session id. */
917c478bd9Sstevel@tonic-gate len = BN_num_bytes(challenge);
927c478bd9Sstevel@tonic-gate if (len <= 0 || len > 32)
937c478bd9Sstevel@tonic-gate fatal("auth_rsa_verify_response: bad challenge length %d", len);
947c478bd9Sstevel@tonic-gate memset(buf, 0, 32);
957c478bd9Sstevel@tonic-gate BN_bn2bin(challenge, buf + 32 - len);
967c478bd9Sstevel@tonic-gate MD5_Init(&md);
977c478bd9Sstevel@tonic-gate MD5_Update(&md, buf, 32);
987c478bd9Sstevel@tonic-gate MD5_Update(&md, session_id, 16);
997c478bd9Sstevel@tonic-gate MD5_Final(mdbuf, &md);
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate /* Verify that the response is the original challenge. */
1027c478bd9Sstevel@tonic-gate if (memcmp(response, mdbuf, 16) != 0) {
1037c478bd9Sstevel@tonic-gate /* Wrong answer. */
1047c478bd9Sstevel@tonic-gate return (0);
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate /* Correct answer. */
1077c478bd9Sstevel@tonic-gate return (1);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * Performs the RSA authentication challenge-response dialog with the client,
1127c478bd9Sstevel@tonic-gate * and returns true (non-zero) if the client gave the correct answer to
1137c478bd9Sstevel@tonic-gate * our challenge; returns zero if the client gives a wrong answer.
1147c478bd9Sstevel@tonic-gate */
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate int
auth_rsa_challenge_dialog(Key * key)1177c478bd9Sstevel@tonic-gate auth_rsa_challenge_dialog(Key *key)
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate BIGNUM *challenge, *encrypted_challenge;
1207c478bd9Sstevel@tonic-gate u_char response[16];
1217c478bd9Sstevel@tonic-gate int i, success;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate if ((encrypted_challenge = BN_new()) == NULL)
1247c478bd9Sstevel@tonic-gate fatal("auth_rsa_challenge_dialog: BN_new() failed");
1257c478bd9Sstevel@tonic-gate
126*9a8058b5Sjp161948 challenge = auth_rsa_generate_challenge(key);
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate /* Encrypt the challenge with the public key. */
1297c478bd9Sstevel@tonic-gate rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate /* Send the encrypted challenge to the client. */
1327c478bd9Sstevel@tonic-gate packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
1337c478bd9Sstevel@tonic-gate packet_put_bignum(encrypted_challenge);
1347c478bd9Sstevel@tonic-gate packet_send();
1357c478bd9Sstevel@tonic-gate BN_clear_free(encrypted_challenge);
1367c478bd9Sstevel@tonic-gate packet_write_wait();
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate /* Wait for a response. */
1397c478bd9Sstevel@tonic-gate packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
1407c478bd9Sstevel@tonic-gate for (i = 0; i < 16; i++)
1417c478bd9Sstevel@tonic-gate response[i] = packet_get_char();
1427c478bd9Sstevel@tonic-gate packet_check_eom();
1437c478bd9Sstevel@tonic-gate
144*9a8058b5Sjp161948 success = auth_rsa_verify_response(key, challenge, response);
1457c478bd9Sstevel@tonic-gate BN_clear_free(challenge);
1467c478bd9Sstevel@tonic-gate return (success);
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate
1497c478bd9Sstevel@tonic-gate /*
1507c478bd9Sstevel@tonic-gate * check if there's user key matching client_n,
1517c478bd9Sstevel@tonic-gate * return key if login is allowed, NULL otherwise
1527c478bd9Sstevel@tonic-gate */
1537c478bd9Sstevel@tonic-gate
1547c478bd9Sstevel@tonic-gate int
auth_rsa_key_allowed(struct passwd * pw,BIGNUM * client_n,Key ** rkey)1557c478bd9Sstevel@tonic-gate auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
1567c478bd9Sstevel@tonic-gate {
1577c478bd9Sstevel@tonic-gate char line[8192], *file;
1587c478bd9Sstevel@tonic-gate int allowed = 0;
1597c478bd9Sstevel@tonic-gate u_int bits;
1607c478bd9Sstevel@tonic-gate FILE *f;
1617c478bd9Sstevel@tonic-gate u_long linenum = 0;
1627c478bd9Sstevel@tonic-gate struct stat st;
1637c478bd9Sstevel@tonic-gate Key *key;
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate /* Temporarily use the user's uid. */
1667c478bd9Sstevel@tonic-gate temporarily_use_uid(pw);
1677c478bd9Sstevel@tonic-gate
1687c478bd9Sstevel@tonic-gate /* The authorized keys. */
1697c478bd9Sstevel@tonic-gate file = authorized_keys_file(pw);
1707c478bd9Sstevel@tonic-gate debug("trying public RSA key file %s", file);
1717c478bd9Sstevel@tonic-gate
1727c478bd9Sstevel@tonic-gate /* Fail quietly if file does not exist */
1737c478bd9Sstevel@tonic-gate if (stat(file, &st) < 0) {
1747c478bd9Sstevel@tonic-gate /* Restore the privileged uid. */
1757c478bd9Sstevel@tonic-gate restore_uid();
1767c478bd9Sstevel@tonic-gate xfree(file);
1777c478bd9Sstevel@tonic-gate return (0);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate /* Open the file containing the authorized keys. */
1807c478bd9Sstevel@tonic-gate f = fopen(file, "r");
1817c478bd9Sstevel@tonic-gate if (!f) {
1827c478bd9Sstevel@tonic-gate /* Restore the privileged uid. */
1837c478bd9Sstevel@tonic-gate restore_uid();
1847c478bd9Sstevel@tonic-gate xfree(file);
1857c478bd9Sstevel@tonic-gate return (0);
1867c478bd9Sstevel@tonic-gate }
1877c478bd9Sstevel@tonic-gate if (options.strict_modes &&
1887c478bd9Sstevel@tonic-gate secure_filename(f, file, pw, line, sizeof(line)) != 0) {
1897c478bd9Sstevel@tonic-gate xfree(file);
1907c478bd9Sstevel@tonic-gate fclose(f);
1917c478bd9Sstevel@tonic-gate log("Authentication refused: %s", line);
1927c478bd9Sstevel@tonic-gate restore_uid();
1937c478bd9Sstevel@tonic-gate return (0);
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate
1967c478bd9Sstevel@tonic-gate /* Flag indicating whether the key is allowed. */
1977c478bd9Sstevel@tonic-gate allowed = 0;
1987c478bd9Sstevel@tonic-gate
1997c478bd9Sstevel@tonic-gate key = key_new(KEY_RSA1);
2007c478bd9Sstevel@tonic-gate
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate * Go though the accepted keys, looking for the current key. If
2037c478bd9Sstevel@tonic-gate * found, perform a challenge-response dialog to verify that the
2047c478bd9Sstevel@tonic-gate * user really has the corresponding private key.
2057c478bd9Sstevel@tonic-gate */
2067c478bd9Sstevel@tonic-gate while (fgets(line, sizeof(line), f)) {
2077c478bd9Sstevel@tonic-gate char *cp;
2087c478bd9Sstevel@tonic-gate char *options;
2097c478bd9Sstevel@tonic-gate
2107c478bd9Sstevel@tonic-gate linenum++;
2117c478bd9Sstevel@tonic-gate
2127c478bd9Sstevel@tonic-gate /* Skip leading whitespace, empty and comment lines. */
2137c478bd9Sstevel@tonic-gate for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
2147c478bd9Sstevel@tonic-gate ;
2157c478bd9Sstevel@tonic-gate if (!*cp || *cp == '\n' || *cp == '#')
2167c478bd9Sstevel@tonic-gate continue;
2177c478bd9Sstevel@tonic-gate
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate * Check if there are options for this key, and if so,
2207c478bd9Sstevel@tonic-gate * save their starting address and skip the option part
2217c478bd9Sstevel@tonic-gate * for now. If there are no options, set the starting
2227c478bd9Sstevel@tonic-gate * address to NULL.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate if (*cp < '0' || *cp > '9') {
2257c478bd9Sstevel@tonic-gate int quoted = 0;
2267c478bd9Sstevel@tonic-gate options = cp;
2277c478bd9Sstevel@tonic-gate for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
2287c478bd9Sstevel@tonic-gate if (*cp == '\\' && cp[1] == '"')
2297c478bd9Sstevel@tonic-gate cp++; /* Skip both */
2307c478bd9Sstevel@tonic-gate else if (*cp == '"')
2317c478bd9Sstevel@tonic-gate quoted = !quoted;
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate } else
2347c478bd9Sstevel@tonic-gate options = NULL;
2357c478bd9Sstevel@tonic-gate
2367c478bd9Sstevel@tonic-gate /* Parse the key from the line. */
2377c478bd9Sstevel@tonic-gate if (hostfile_read_key(&cp, &bits, key) == 0) {
2387c478bd9Sstevel@tonic-gate debug("%.100s, line %lu: non ssh1 key syntax",
2397c478bd9Sstevel@tonic-gate file, linenum);
2407c478bd9Sstevel@tonic-gate continue;
2417c478bd9Sstevel@tonic-gate }
2427c478bd9Sstevel@tonic-gate /* cp now points to the comment part. */
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate /* Check if the we have found the desired key (identified by its modulus). */
2457c478bd9Sstevel@tonic-gate if (BN_cmp(key->rsa->n, client_n) != 0)
2467c478bd9Sstevel@tonic-gate continue;
2477c478bd9Sstevel@tonic-gate
2487c478bd9Sstevel@tonic-gate /* check the real bits */
2497c478bd9Sstevel@tonic-gate if (bits != BN_num_bits(key->rsa->n))
2507c478bd9Sstevel@tonic-gate log("Warning: %s, line %lu: keysize mismatch: "
2517c478bd9Sstevel@tonic-gate "actual %d vs. announced %d.",
2527c478bd9Sstevel@tonic-gate file, linenum, BN_num_bits(key->rsa->n), bits);
2537c478bd9Sstevel@tonic-gate
2547c478bd9Sstevel@tonic-gate /* We have found the desired key. */
2557c478bd9Sstevel@tonic-gate /*
2567c478bd9Sstevel@tonic-gate * If our options do not allow this key to be used,
2577c478bd9Sstevel@tonic-gate * do not send challenge.
2587c478bd9Sstevel@tonic-gate */
2597c478bd9Sstevel@tonic-gate if (!auth_parse_options(pw, options, file, linenum))
2607c478bd9Sstevel@tonic-gate continue;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate /* break out, this key is allowed */
2637c478bd9Sstevel@tonic-gate allowed = 1;
2647c478bd9Sstevel@tonic-gate break;
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate
2677c478bd9Sstevel@tonic-gate /* Restore the privileged uid. */
2687c478bd9Sstevel@tonic-gate restore_uid();
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate /* Close the file. */
2717c478bd9Sstevel@tonic-gate xfree(file);
2727c478bd9Sstevel@tonic-gate fclose(f);
2737c478bd9Sstevel@tonic-gate
2747c478bd9Sstevel@tonic-gate /* return key if allowed */
2757c478bd9Sstevel@tonic-gate if (allowed && rkey != NULL)
2767c478bd9Sstevel@tonic-gate *rkey = key;
2777c478bd9Sstevel@tonic-gate else
2787c478bd9Sstevel@tonic-gate key_free(key);
2797c478bd9Sstevel@tonic-gate return (allowed);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate * Performs the RSA authentication dialog with the client. This returns
2847c478bd9Sstevel@tonic-gate * 0 if the client could not be authenticated, and 1 if authentication was
2857c478bd9Sstevel@tonic-gate * successful. This may exit if there is a serious protocol violation.
2867c478bd9Sstevel@tonic-gate */
2877c478bd9Sstevel@tonic-gate int
auth_rsa(struct passwd * pw,BIGNUM * client_n)2887c478bd9Sstevel@tonic-gate auth_rsa(struct passwd *pw, BIGNUM *client_n)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate Key *key;
2917c478bd9Sstevel@tonic-gate char *fp;
2927c478bd9Sstevel@tonic-gate
2937c478bd9Sstevel@tonic-gate /* no user given */
2947c478bd9Sstevel@tonic-gate if (pw == NULL)
2957c478bd9Sstevel@tonic-gate return 0;
2967c478bd9Sstevel@tonic-gate
297*9a8058b5Sjp161948 if (!auth_rsa_key_allowed(pw, client_n, &key)) {
2987c478bd9Sstevel@tonic-gate auth_clear_options();
2997c478bd9Sstevel@tonic-gate return (0);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate /* Perform the challenge-response dialog for this key. */
3037c478bd9Sstevel@tonic-gate if (!auth_rsa_challenge_dialog(key)) {
3047c478bd9Sstevel@tonic-gate /* Wrong response. */
3057c478bd9Sstevel@tonic-gate verbose("Wrong response to RSA authentication challenge.");
3067c478bd9Sstevel@tonic-gate packet_send_debug("Wrong response to RSA authentication challenge.");
3077c478bd9Sstevel@tonic-gate /*
3087c478bd9Sstevel@tonic-gate * Break out of the loop. Otherwise we might send
3097c478bd9Sstevel@tonic-gate * another challenge and break the protocol.
3107c478bd9Sstevel@tonic-gate */
3117c478bd9Sstevel@tonic-gate key_free(key);
3127c478bd9Sstevel@tonic-gate return (0);
3137c478bd9Sstevel@tonic-gate }
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate * Correct response. The client has been successfully
3167c478bd9Sstevel@tonic-gate * authenticated. Note that we have not yet processed the
3177c478bd9Sstevel@tonic-gate * options; this will be reset if the options cause the
3187c478bd9Sstevel@tonic-gate * authentication to be rejected.
3197c478bd9Sstevel@tonic-gate */
3207c478bd9Sstevel@tonic-gate fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
3217c478bd9Sstevel@tonic-gate verbose("Found matching %s key: %s",
3227c478bd9Sstevel@tonic-gate key_type(key), fp);
3237c478bd9Sstevel@tonic-gate xfree(fp);
3247c478bd9Sstevel@tonic-gate key_free(key);
3257c478bd9Sstevel@tonic-gate
3267c478bd9Sstevel@tonic-gate packet_send_debug("RSA authentication accepted.");
3277c478bd9Sstevel@tonic-gate return (1);
3287c478bd9Sstevel@tonic-gate }
329