]> andersk Git - openssh.git/blame - auth-rsa.c
- markus@cvs.openbsd.org 2002/03/14 16:38:26
[openssh.git] / auth-rsa.c
CommitLineData
8efc0c15 1/*
5260325f 2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5260325f 3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
4 * All rights reserved
5260325f 5 * RSA-based authentication. This code determines whether to admit a login
6 * based on RSA authentication. This file also contains functions to check
7 * validity of the host key.
6ae2364d 8 *
bcbf86ec 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".
5260325f 14 */
8efc0c15 15
16#include "includes.h"
54a5250f 17RCSID("$OpenBSD: auth-rsa.c,v 1.50 2001/12/28 14:50:54 markus Exp $");
42f11eb2 18
19#include <openssl/rsa.h>
20#include <openssl/md5.h>
8efc0c15 21
22#include "rsa.h"
23#include "packet.h"
24#include "xmalloc.h"
42f11eb2 25#include "ssh1.h"
8efc0c15 26#include "mpaux.h"
27#include "uidswap.h"
4fe2af09 28#include "match.h"
38c295d6 29#include "auth-options.h"
42f11eb2 30#include "pathnames.h"
31#include "log.h"
32#include "servconf.h"
33#include "auth.h"
46df736f 34#include "hostfile.h"
94ec8c6b 35
36/* import */
37extern ServerOptions options;
38
aa3378df 39/*
40 * Session identifier that is used to bind key exchange and authentication
41 * responses to a particular session.
42 */
1e3b8b07 43extern u_char session_id[16];
8efc0c15 44
aa3378df 45/*
46 * The .ssh/authorized_keys file contains public keys, one per line, in the
47 * following format:
48 * options bits e n comment
49 * where bits, e and n are decimal numbers,
50 * and comment is any string of characters up to newline. The maximum
51 * length of a line is 8000 characters. See the documentation for a
52 * description of the options.
53 */
8efc0c15 54
aa3378df 55/*
56 * Performs the RSA authentication challenge-response dialog with the client,
57 * and returns true (non-zero) if the client gave the correct answer to
58 * our challenge; returns zero if the client gives a wrong answer.
59 */
8efc0c15 60
61int
4fe2af09 62auth_rsa_challenge_dialog(RSA *pk)
8efc0c15 63{
c8d54615 64 BIGNUM *challenge, *encrypted_challenge;
c8d54615 65 BN_CTX *ctx;
1e3b8b07 66 u_char buf[32], mdbuf[16], response[16];
5260325f 67 MD5_CTX md;
1e3b8b07 68 u_int i;
54a5250f 69 int len;
5260325f 70
b775c6f2 71 if ((encrypted_challenge = BN_new()) == NULL)
72 fatal("auth_rsa_challenge_dialog: BN_new() failed");
73 if ((challenge = BN_new()) == NULL)
74 fatal("auth_rsa_challenge_dialog: BN_new() failed");
5260325f 75
76 /* Generate a random challenge. */
77 BN_rand(challenge, 256, 0, 0);
b775c6f2 78 if ((ctx = BN_CTX_new()) == NULL)
79 fatal("auth_rsa_challenge_dialog: BN_CTX_new() failed");
4fe2af09 80 BN_mod(challenge, challenge, pk->n, ctx);
c8d54615 81 BN_CTX_free(ctx);
5260325f 82
5260325f 83 /* Encrypt the challenge with the public key. */
84 rsa_public_encrypt(encrypted_challenge, challenge, pk);
5260325f 85
86 /* Send the encrypted challenge to the client. */
87 packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
88 packet_put_bignum(encrypted_challenge);
89 packet_send();
c8d54615 90 BN_clear_free(encrypted_challenge);
5260325f 91 packet_write_wait();
92
c8d54615 93 /* Wait for a response. */
54a5250f 94 packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
c8d54615 95 for (i = 0; i < 16; i++)
96 response[i] = packet_get_char();
95500969 97 packet_check_eom();
c8d54615 98
5260325f 99 /* The response is MD5 of decrypted challenge plus session id. */
100 len = BN_num_bytes(challenge);
101 if (len <= 0 || len > 32)
102 fatal("auth_rsa_challenge_dialog: bad challenge length %d", len);
103 memset(buf, 0, 32);
104 BN_bn2bin(challenge, buf + 32 - len);
105 MD5_Init(&md);
106 MD5_Update(&md, buf, 32);
107 MD5_Update(&md, session_id, 16);
108 MD5_Final(mdbuf, &md);
5260325f 109 BN_clear_free(challenge);
5260325f 110
111 /* Verify that the response is the original challenge. */
112 if (memcmp(response, mdbuf, 16) != 0) {
113 /* Wrong answer. */
114 return 0;
115 }
116 /* Correct answer. */
117 return 1;
8efc0c15 118}
119
aa3378df 120/*
121 * Performs the RSA authentication dialog with the client. This returns
122 * 0 if the client could not be authenticated, and 1 if authentication was
123 * successful. This may exit if there is a serious protocol violation.
124 */
8efc0c15 125
126int
6fa724bc 127auth_rsa(struct passwd *pw, BIGNUM *client_n)
8efc0c15 128{
c8445989 129 char line[8192], *file;
5260325f 130 int authenticated;
1e3b8b07 131 u_int bits;
5260325f 132 FILE *f;
1e3b8b07 133 u_long linenum = 0;
5260325f 134 struct stat st;
46df736f 135 Key *key;
136 char *fp;
5260325f 137
94ec8c6b 138 /* no user given */
139 if (pw == NULL)
140 return 0;
141
5260325f 142 /* Temporarily use the user's uid. */
63bd8c36 143 temporarily_use_uid(pw);
5260325f 144
145 /* The authorized keys. */
c8445989 146 file = authorized_keys_file(pw);
147 debug("trying public RSA key file %s", file);
5260325f 148
149 /* Fail quietly if file does not exist */
150 if (stat(file, &st) < 0) {
151 /* Restore the privileged uid. */
152 restore_uid();
c8445989 153 xfree(file);
5260325f 154 return 0;
8efc0c15 155 }
5260325f 156 /* Open the file containing the authorized keys. */
157 f = fopen(file, "r");
158 if (!f) {
159 /* Restore the privileged uid. */
160 restore_uid();
161 packet_send_debug("Could not open %.900s for reading.", file);
162 packet_send_debug("If your home is on an NFS volume, it may need to be world-readable.");
c8445989 163 xfree(file);
5260325f 164 return 0;
8efc0c15 165 }
c8445989 166 if (options.strict_modes &&
6978866a 167 secure_filename(f, file, pw, line, sizeof(line)) != 0) {
c8445989 168 xfree(file);
169 fclose(f);
170 log("Authentication refused: %s", line);
171 packet_send_debug("Authentication refused: %s", line);
172 restore_uid();
173 return 0;
5260325f 174 }
175 /* Flag indicating whether authentication has succeeded. */
176 authenticated = 0;
177
46df736f 178 key = key_new(KEY_RSA1);
5260325f 179
aa3378df 180 /*
181 * Go though the accepted keys, looking for the current key. If
182 * found, perform a challenge-response dialog to verify that the
183 * user really has the corresponding private key.
184 */
5260325f 185 while (fgets(line, sizeof(line), f)) {
186 char *cp;
187 char *options;
188
189 linenum++;
190
aa3378df 191 /* Skip leading whitespace, empty and comment lines. */
192 for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
193 ;
5260325f 194 if (!*cp || *cp == '\n' || *cp == '#')
195 continue;
196
aa3378df 197 /*
198 * Check if there are options for this key, and if so,
199 * save their starting address and skip the option part
200 * for now. If there are no options, set the starting
201 * address to NULL.
202 */
5260325f 203 if (*cp < '0' || *cp > '9') {
204 int quoted = 0;
205 options = cp;
206 for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
207 if (*cp == '\\' && cp[1] == '"')
208 cp++; /* Skip both */
209 else if (*cp == '"')
210 quoted = !quoted;
8efc0c15 211 }
5260325f 212 } else
213 options = NULL;
214
215 /* Parse the key from the line. */
46df736f 216 if (hostfile_read_key(&cp, &bits, key) == 0) {
96a7b0cc 217 debug("%.100s, line %lu: non ssh1 key syntax",
42f11eb2 218 file, linenum);
5260325f 219 continue;
8efc0c15 220 }
5260325f 221 /* cp now points to the comment part. */
222
5260325f 223 /* Check if the we have found the desired key (identified by its modulus). */
46df736f 224 if (BN_cmp(key->rsa->n, client_n) != 0)
aa3378df 225 continue;
5260325f 226
57112b5a 227 /* check the real bits */
46df736f 228 if (bits != BN_num_bits(key->rsa->n))
1bdee08c 229 log("Warning: %s, line %lu: keysize mismatch: "
57112b5a 230 "actual %d vs. announced %d.",
46df736f 231 file, linenum, BN_num_bits(key->rsa->n), bits);
57112b5a 232
5260325f 233 /* We have found the desired key. */
d343d900 234 /*
235 * If our options do not allow this key to be used,
236 * do not send challenge.
237 */
42f11eb2 238 if (!auth_parse_options(pw, options, file, linenum))
d343d900 239 continue;
5260325f 240
241 /* Perform the challenge-response dialog for this key. */
46df736f 242 if (!auth_rsa_challenge_dialog(key->rsa)) {
5260325f 243 /* Wrong response. */
244 verbose("Wrong response to RSA authentication challenge.");
245 packet_send_debug("Wrong response to RSA authentication challenge.");
2975f58d 246 /*
247 * Break out of the loop. Otherwise we might send
248 * another challenge and break the protocol.
249 */
250 break;
8efc0c15 251 }
aa3378df 252 /*
253 * Correct response. The client has been successfully
254 * authenticated. Note that we have not yet processed the
255 * options; this will be reset if the options cause the
256 * authentication to be rejected.
aa3378df 257 * Break out of the loop if authentication was successful;
258 * otherwise continue searching.
259 */
33de75a3 260 authenticated = 1;
46df736f 261
262 fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
263 verbose("Found matching %s key: %s",
264 key_type(key), fp);
265 xfree(fp);
266
33de75a3 267 break;
8efc0c15 268 }
269
5260325f 270 /* Restore the privileged uid. */
271 restore_uid();
8efc0c15 272
5260325f 273 /* Close the file. */
c8445989 274 xfree(file);
5260325f 275 fclose(f);
8efc0c15 276
46df736f 277 key_free(key);
8efc0c15 278
5260325f 279 if (authenticated)
280 packet_send_debug("RSA authentication accepted.");
94ec8c6b 281 else
282 auth_clear_options();
8efc0c15 283
5260325f 284 /* Return authentication result. */
285 return authenticated;
8efc0c15 286}
This page took 0.173409 seconds and 5 git commands to generate.