PageRenderTime 125ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/openssh/auth-rsa.c

https://github.com/clibrepo/04f6ea02286af71632b72d73554979a61da424cf681886674b21ecd595715c12
C | 324 lines | 186 code | 45 blank | 93 comment | 55 complexity | 241049ee9d1bb118963af5ddb0aee2bc MD5 | raw file
  1. /* $OpenBSD: auth-rsa.c,v 1.74 2010/03/04 10:36:03 djm Exp $ */
  2. /*
  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.
  9. *
  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".
  15. */
  16. #include <sys/types.h>
  17. #include <sys/stat.h>
  18. #include <openssl/rsa.h>
  19. #include <openssl/md5.h>
  20. #include <pwd.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include "xmalloc.h"
  24. #include "rsa.h"
  25. #include "packet.h"
  26. #include "ssh1.h"
  27. #include "uidswap.h"
  28. #include "match.h"
  29. #include "buffer.h"
  30. #include "auth-options.h"
  31. #include "pathnames.h"
  32. #include "log.h"
  33. #include "servconf.h"
  34. #include "key.h"
  35. #include "hostfile.h"
  36. #include "auth.h"
  37. #ifdef GSSAPI
  38. #include "ssh-gss.h"
  39. #endif
  40. #include "monitor_wrap.h"
  41. #include "ssh.h"
  42. #include "misc.h"
  43. /* import */
  44. extern ServerOptions options;
  45. /*
  46. * Session identifier that is used to bind key exchange and authentication
  47. * responses to a particular session.
  48. */
  49. extern u_char session_id[16];
  50. /*
  51. * The .ssh/authorized_keys file contains public keys, one per line, in the
  52. * following format:
  53. * options bits e n comment
  54. * where bits, e and n are decimal numbers,
  55. * and comment is any string of characters up to newline. The maximum
  56. * length of a line is SSH_MAX_PUBKEY_BYTES characters. See sshd(8) for a
  57. * description of the options.
  58. */
  59. BIGNUM *
  60. auth_rsa_generate_challenge(Key *key)
  61. {
  62. BIGNUM *challenge;
  63. BN_CTX *ctx;
  64. if ((challenge = BN_new()) == NULL)
  65. fatal("auth_rsa_generate_challenge: BN_new() failed");
  66. /* Generate a random challenge. */
  67. if (BN_rand(challenge, 256, 0, 0) == 0)
  68. fatal("auth_rsa_generate_challenge: BN_rand failed");
  69. if ((ctx = BN_CTX_new()) == NULL)
  70. fatal("auth_rsa_generate_challenge: BN_CTX_new failed");
  71. if (BN_mod(challenge, challenge, key->rsa->n, ctx) == 0)
  72. fatal("auth_rsa_generate_challenge: BN_mod failed");
  73. BN_CTX_free(ctx);
  74. return challenge;
  75. }
  76. int
  77. auth_rsa_verify_response(Key *key, BIGNUM *challenge, u_char response[16])
  78. {
  79. u_char buf[32], mdbuf[16];
  80. MD5_CTX md;
  81. int len;
  82. if (auth_key_is_revoked(key))
  83. return 0;
  84. /* don't allow short keys */
  85. if (BN_num_bits(key->rsa->n) < SSH_RSA_MINIMUM_MODULUS_SIZE) {
  86. error("auth_rsa_verify_response: RSA modulus too small: %d < minimum %d bits",
  87. BN_num_bits(key->rsa->n), SSH_RSA_MINIMUM_MODULUS_SIZE);
  88. return (0);
  89. }
  90. /* The response is MD5 of decrypted challenge plus session id. */
  91. len = BN_num_bytes(challenge);
  92. if (len <= 0 || len > 32)
  93. fatal("auth_rsa_verify_response: bad challenge length %d", len);
  94. memset(buf, 0, 32);
  95. BN_bn2bin(challenge, buf + 32 - len);
  96. MD5_Init(&md);
  97. MD5_Update(&md, buf, 32);
  98. MD5_Update(&md, session_id, 16);
  99. MD5_Final(mdbuf, &md);
  100. /* Verify that the response is the original challenge. */
  101. if (memcmp(response, mdbuf, 16) != 0) {
  102. /* Wrong answer. */
  103. return (0);
  104. }
  105. /* Correct answer. */
  106. return (1);
  107. }
  108. /*
  109. * Performs the RSA authentication challenge-response dialog with the client,
  110. * and returns true (non-zero) if the client gave the correct answer to
  111. * our challenge; returns zero if the client gives a wrong answer.
  112. */
  113. int
  114. auth_rsa_challenge_dialog(Key *key)
  115. {
  116. BIGNUM *challenge, *encrypted_challenge;
  117. u_char response[16];
  118. int i, success;
  119. if ((encrypted_challenge = BN_new()) == NULL)
  120. fatal("auth_rsa_challenge_dialog: BN_new() failed");
  121. challenge = PRIVSEP(auth_rsa_generate_challenge(key));
  122. /* Encrypt the challenge with the public key. */
  123. rsa_public_encrypt(encrypted_challenge, challenge, key->rsa);
  124. /* Send the encrypted challenge to the client. */
  125. packet_start(SSH_SMSG_AUTH_RSA_CHALLENGE);
  126. packet_put_bignum(encrypted_challenge);
  127. packet_send();
  128. BN_clear_free(encrypted_challenge);
  129. packet_write_wait();
  130. /* Wait for a response. */
  131. packet_read_expect(SSH_CMSG_AUTH_RSA_RESPONSE);
  132. for (i = 0; i < 16; i++)
  133. response[i] = (u_char)packet_get_char();
  134. packet_check_eom();
  135. success = PRIVSEP(auth_rsa_verify_response(key, challenge, response));
  136. BN_clear_free(challenge);
  137. return (success);
  138. }
  139. /*
  140. * check if there's user key matching client_n,
  141. * return key if login is allowed, NULL otherwise
  142. */
  143. int
  144. auth_rsa_key_allowed(struct passwd *pw, BIGNUM *client_n, Key **rkey)
  145. {
  146. char line[SSH_MAX_PUBKEY_BYTES], *file;
  147. int allowed = 0;
  148. u_int bits;
  149. FILE *f;
  150. u_long linenum = 0;
  151. Key *key;
  152. /* Temporarily use the user's uid. */
  153. temporarily_use_uid(pw);
  154. /* The authorized keys. */
  155. file = authorized_keys_file(pw);
  156. debug("trying public RSA key file %s", file);
  157. f = auth_openkeyfile(file, pw, options.strict_modes);
  158. if (!f) {
  159. xfree(file);
  160. restore_uid();
  161. return (0);
  162. }
  163. /* Flag indicating whether the key is allowed. */
  164. allowed = 0;
  165. key = key_new(KEY_RSA1);
  166. /*
  167. * Go though the accepted keys, looking for the current key. If
  168. * found, perform a challenge-response dialog to verify that the
  169. * user really has the corresponding private key.
  170. */
  171. while (read_keyfile_line(f, file, line, sizeof(line), &linenum) != -1) {
  172. char *cp;
  173. char *key_options;
  174. int keybits;
  175. /* Skip leading whitespace, empty and comment lines. */
  176. for (cp = line; *cp == ' ' || *cp == '\t'; cp++)
  177. ;
  178. if (!*cp || *cp == '\n' || *cp == '#')
  179. continue;
  180. /*
  181. * Check if there are options for this key, and if so,
  182. * save their starting address and skip the option part
  183. * for now. If there are no options, set the starting
  184. * address to NULL.
  185. */
  186. if (*cp < '0' || *cp > '9') {
  187. int quoted = 0;
  188. key_options = cp;
  189. for (; *cp && (quoted || (*cp != ' ' && *cp != '\t')); cp++) {
  190. if (*cp == '\\' && cp[1] == '"')
  191. cp++; /* Skip both */
  192. else if (*cp == '"')
  193. quoted = !quoted;
  194. }
  195. } else
  196. key_options = NULL;
  197. /* Parse the key from the line. */
  198. if (hostfile_read_key(&cp, &bits, key) == 0) {
  199. debug("%.100s, line %lu: non ssh1 key syntax",
  200. file, linenum);
  201. continue;
  202. }
  203. /* cp now points to the comment part. */
  204. /* Check if the we have found the desired key (identified by its modulus). */
  205. if (BN_cmp(key->rsa->n, client_n) != 0)
  206. continue;
  207. /* check the real bits */
  208. keybits = BN_num_bits(key->rsa->n);
  209. if (keybits < 0 || bits != (u_int)keybits)
  210. logit("Warning: %s, line %lu: keysize mismatch: "
  211. "actual %d vs. announced %d.",
  212. file, linenum, BN_num_bits(key->rsa->n), bits);
  213. /* We have found the desired key. */
  214. /*
  215. * If our options do not allow this key to be used,
  216. * do not send challenge.
  217. */
  218. if (!auth_parse_options(pw, key_options, file, linenum))
  219. continue;
  220. /* break out, this key is allowed */
  221. allowed = 1;
  222. break;
  223. }
  224. /* Restore the privileged uid. */
  225. restore_uid();
  226. /* Close the file. */
  227. xfree(file);
  228. fclose(f);
  229. /* return key if allowed */
  230. if (allowed && rkey != NULL)
  231. *rkey = key;
  232. else
  233. key_free(key);
  234. return (allowed);
  235. }
  236. /*
  237. * Performs the RSA authentication dialog with the client. This returns
  238. * 0 if the client could not be authenticated, and 1 if authentication was
  239. * successful. This may exit if there is a serious protocol violation.
  240. */
  241. int
  242. auth_rsa(Authctxt *authctxt, BIGNUM *client_n)
  243. {
  244. Key *key;
  245. char *fp;
  246. struct passwd *pw = authctxt->pw;
  247. /* no user given */
  248. if (!authctxt->valid)
  249. return 0;
  250. if (!PRIVSEP(auth_rsa_key_allowed(pw, client_n, &key))) {
  251. auth_clear_options();
  252. return (0);
  253. }
  254. /* Perform the challenge-response dialog for this key. */
  255. if (!auth_rsa_challenge_dialog(key)) {
  256. /* Wrong response. */
  257. verbose("Wrong response to RSA authentication challenge.");
  258. packet_send_debug("Wrong response to RSA authentication challenge.");
  259. /*
  260. * Break out of the loop. Otherwise we might send
  261. * another challenge and break the protocol.
  262. */
  263. key_free(key);
  264. return (0);
  265. }
  266. /*
  267. * Correct response. The client has been successfully
  268. * authenticated. Note that we have not yet processed the
  269. * options; this will be reset if the options cause the
  270. * authentication to be rejected.
  271. */
  272. fp = key_fingerprint(key, SSH_FP_MD5, SSH_FP_HEX);
  273. verbose("Found matching %s key: %s",
  274. key_type(key), fp);
  275. xfree(fp);
  276. key_free(key);
  277. packet_send_debug("RSA authentication accepted.");
  278. return (1);
  279. }