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