iscsi_target_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. /*******************************************************************************
  2. * This file houses the main functions for the iSCSI CHAP support
  3. *
  4. * (c) Copyright 2007-2013 Datera, Inc.
  5. *
  6. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. ******************************************************************************/
  18. #include <crypto/hash.h>
  19. #include <linux/kernel.h>
  20. #include <linux/string.h>
  21. #include <linux/err.h>
  22. #include <linux/random.h>
  23. #include <linux/scatterlist.h>
  24. #include <target/iscsi/iscsi_target_core.h>
  25. #include "iscsi_target_nego.h"
  26. #include "iscsi_target_auth.h"
  27. static int chap_gen_challenge(
  28. struct iscsi_conn *conn,
  29. int caller,
  30. char *c_str,
  31. unsigned int *c_len)
  32. {
  33. int ret;
  34. unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
  35. struct iscsi_chap *chap = conn->auth_protocol;
  36. memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
  37. ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH);
  38. if (unlikely(ret))
  39. return ret;
  40. bin2hex(challenge_asciihex, chap->challenge,
  41. CHAP_CHALLENGE_LENGTH);
  42. /*
  43. * Set CHAP_C, and copy the generated challenge into c_str.
  44. */
  45. *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
  46. *c_len += 1;
  47. pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
  48. challenge_asciihex);
  49. return 0;
  50. }
  51. static int chap_check_algorithm(const char *a_str)
  52. {
  53. char *tmp, *orig, *token;
  54. tmp = kstrdup(a_str, GFP_KERNEL);
  55. if (!tmp) {
  56. pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
  57. return CHAP_DIGEST_UNKNOWN;
  58. }
  59. orig = tmp;
  60. token = strsep(&tmp, "=");
  61. if (!token)
  62. goto out;
  63. if (strcmp(token, "CHAP_A")) {
  64. pr_err("Unable to locate CHAP_A key\n");
  65. goto out;
  66. }
  67. while (token) {
  68. token = strsep(&tmp, ",");
  69. if (!token)
  70. goto out;
  71. if (!strncmp(token, "5", 1)) {
  72. pr_debug("Selected MD5 Algorithm\n");
  73. kfree(orig);
  74. return CHAP_DIGEST_MD5;
  75. }
  76. }
  77. out:
  78. kfree(orig);
  79. return CHAP_DIGEST_UNKNOWN;
  80. }
  81. static struct iscsi_chap *chap_server_open(
  82. struct iscsi_conn *conn,
  83. struct iscsi_node_auth *auth,
  84. const char *a_str,
  85. char *aic_str,
  86. unsigned int *aic_len)
  87. {
  88. int ret;
  89. struct iscsi_chap *chap;
  90. if (!(auth->naf_flags & NAF_USERID_SET) ||
  91. !(auth->naf_flags & NAF_PASSWORD_SET)) {
  92. pr_err("CHAP user or password not set for"
  93. " Initiator ACL\n");
  94. return NULL;
  95. }
  96. conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
  97. if (!conn->auth_protocol)
  98. return NULL;
  99. chap = conn->auth_protocol;
  100. ret = chap_check_algorithm(a_str);
  101. switch (ret) {
  102. case CHAP_DIGEST_MD5:
  103. pr_debug("[server] Got CHAP_A=5\n");
  104. /*
  105. * Send back CHAP_A set to MD5.
  106. */
  107. *aic_len = sprintf(aic_str, "CHAP_A=5");
  108. *aic_len += 1;
  109. chap->digest_type = CHAP_DIGEST_MD5;
  110. pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
  111. break;
  112. case CHAP_DIGEST_UNKNOWN:
  113. default:
  114. pr_err("Unsupported CHAP_A value\n");
  115. kfree(conn->auth_protocol);
  116. return NULL;
  117. }
  118. /*
  119. * Set Identifier.
  120. */
  121. chap->id = conn->tpg->tpg_chap_id++;
  122. *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
  123. *aic_len += 1;
  124. pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
  125. /*
  126. * Generate Challenge.
  127. */
  128. if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
  129. kfree(conn->auth_protocol);
  130. return NULL;
  131. }
  132. return chap;
  133. }
  134. static void chap_close(struct iscsi_conn *conn)
  135. {
  136. kfree(conn->auth_protocol);
  137. conn->auth_protocol = NULL;
  138. }
  139. static int chap_server_compute_md5(
  140. struct iscsi_conn *conn,
  141. struct iscsi_node_auth *auth,
  142. char *nr_in_ptr,
  143. char *nr_out_ptr,
  144. unsigned int *nr_out_len)
  145. {
  146. unsigned long id;
  147. unsigned char id_as_uchar;
  148. unsigned char digest[MD5_SIGNATURE_SIZE];
  149. unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
  150. unsigned char identifier[10], *challenge = NULL;
  151. unsigned char *challenge_binhex = NULL;
  152. unsigned char client_digest[MD5_SIGNATURE_SIZE];
  153. unsigned char server_digest[MD5_SIGNATURE_SIZE];
  154. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  155. size_t compare_len;
  156. struct iscsi_chap *chap = conn->auth_protocol;
  157. struct crypto_shash *tfm = NULL;
  158. struct shash_desc *desc = NULL;
  159. int auth_ret = -1, ret, challenge_len;
  160. memset(identifier, 0, 10);
  161. memset(chap_n, 0, MAX_CHAP_N_SIZE);
  162. memset(chap_r, 0, MAX_RESPONSE_LENGTH);
  163. memset(digest, 0, MD5_SIGNATURE_SIZE);
  164. memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
  165. memset(client_digest, 0, MD5_SIGNATURE_SIZE);
  166. memset(server_digest, 0, MD5_SIGNATURE_SIZE);
  167. challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  168. if (!challenge) {
  169. pr_err("Unable to allocate challenge buffer\n");
  170. goto out;
  171. }
  172. challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  173. if (!challenge_binhex) {
  174. pr_err("Unable to allocate challenge_binhex buffer\n");
  175. goto out;
  176. }
  177. /*
  178. * Extract CHAP_N.
  179. */
  180. if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
  181. &type) < 0) {
  182. pr_err("Could not find CHAP_N.\n");
  183. goto out;
  184. }
  185. if (type == HEX) {
  186. pr_err("Could not find CHAP_N.\n");
  187. goto out;
  188. }
  189. /* Include the terminating NULL in the compare */
  190. compare_len = strlen(auth->userid) + 1;
  191. if (strncmp(chap_n, auth->userid, compare_len) != 0) {
  192. pr_err("CHAP_N values do not match!\n");
  193. goto out;
  194. }
  195. pr_debug("[server] Got CHAP_N=%s\n", chap_n);
  196. /*
  197. * Extract CHAP_R.
  198. */
  199. if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
  200. &type) < 0) {
  201. pr_err("Could not find CHAP_R.\n");
  202. goto out;
  203. }
  204. if (type != HEX) {
  205. pr_err("Could not find CHAP_R.\n");
  206. goto out;
  207. }
  208. if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
  209. pr_err("Malformed CHAP_R\n");
  210. goto out;
  211. }
  212. if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
  213. pr_err("Malformed CHAP_R\n");
  214. goto out;
  215. }
  216. pr_debug("[server] Got CHAP_R=%s\n", chap_r);
  217. tfm = crypto_alloc_shash("md5", 0, 0);
  218. if (IS_ERR(tfm)) {
  219. tfm = NULL;
  220. pr_err("Unable to allocate struct crypto_shash\n");
  221. goto out;
  222. }
  223. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
  224. if (!desc) {
  225. pr_err("Unable to allocate struct shash_desc\n");
  226. goto out;
  227. }
  228. desc->tfm = tfm;
  229. desc->flags = 0;
  230. ret = crypto_shash_init(desc);
  231. if (ret < 0) {
  232. pr_err("crypto_shash_init() failed\n");
  233. goto out;
  234. }
  235. ret = crypto_shash_update(desc, &chap->id, 1);
  236. if (ret < 0) {
  237. pr_err("crypto_shash_update() failed for id\n");
  238. goto out;
  239. }
  240. ret = crypto_shash_update(desc, (char *)&auth->password,
  241. strlen(auth->password));
  242. if (ret < 0) {
  243. pr_err("crypto_shash_update() failed for password\n");
  244. goto out;
  245. }
  246. ret = crypto_shash_finup(desc, chap->challenge,
  247. CHAP_CHALLENGE_LENGTH, server_digest);
  248. if (ret < 0) {
  249. pr_err("crypto_shash_finup() failed for challenge\n");
  250. goto out;
  251. }
  252. bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
  253. pr_debug("[server] MD5 Server Digest: %s\n", response);
  254. if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
  255. pr_debug("[server] MD5 Digests do not match!\n\n");
  256. goto out;
  257. } else
  258. pr_debug("[server] MD5 Digests match, CHAP connection"
  259. " successful.\n\n");
  260. /*
  261. * One way authentication has succeeded, return now if mutual
  262. * authentication is not enabled.
  263. */
  264. if (!auth->authenticate_target) {
  265. auth_ret = 0;
  266. goto out;
  267. }
  268. /*
  269. * Get CHAP_I.
  270. */
  271. if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
  272. pr_err("Could not find CHAP_I.\n");
  273. goto out;
  274. }
  275. if (type == HEX)
  276. ret = kstrtoul(&identifier[2], 0, &id);
  277. else
  278. ret = kstrtoul(identifier, 0, &id);
  279. if (ret < 0) {
  280. pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
  281. goto out;
  282. }
  283. if (id > 255) {
  284. pr_err("chap identifier: %lu greater than 255\n", id);
  285. goto out;
  286. }
  287. /*
  288. * RFC 1994 says Identifier is no more than octet (8 bits).
  289. */
  290. pr_debug("[server] Got CHAP_I=%lu\n", id);
  291. /*
  292. * Get CHAP_C.
  293. */
  294. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  295. challenge, &type) < 0) {
  296. pr_err("Could not find CHAP_C.\n");
  297. goto out;
  298. }
  299. if (type != HEX) {
  300. pr_err("Could not find CHAP_C.\n");
  301. goto out;
  302. }
  303. challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
  304. if (!challenge_len) {
  305. pr_err("Unable to convert incoming challenge\n");
  306. goto out;
  307. }
  308. if (challenge_len > 1024) {
  309. pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
  310. goto out;
  311. }
  312. if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
  313. pr_err("Malformed CHAP_C\n");
  314. goto out;
  315. }
  316. pr_debug("[server] Got CHAP_C=%s\n", challenge);
  317. /*
  318. * During mutual authentication, the CHAP_C generated by the
  319. * initiator must not match the original CHAP_C generated by
  320. * the target.
  321. */
  322. if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
  323. pr_err("initiator CHAP_C matches target CHAP_C, failing"
  324. " login attempt\n");
  325. goto out;
  326. }
  327. /*
  328. * Generate CHAP_N and CHAP_R for mutual authentication.
  329. */
  330. ret = crypto_shash_init(desc);
  331. if (ret < 0) {
  332. pr_err("crypto_shash_init() failed\n");
  333. goto out;
  334. }
  335. /* To handle both endiannesses */
  336. id_as_uchar = id;
  337. ret = crypto_shash_update(desc, &id_as_uchar, 1);
  338. if (ret < 0) {
  339. pr_err("crypto_shash_update() failed for id\n");
  340. goto out;
  341. }
  342. ret = crypto_shash_update(desc, auth->password_mutual,
  343. strlen(auth->password_mutual));
  344. if (ret < 0) {
  345. pr_err("crypto_shash_update() failed for"
  346. " password_mutual\n");
  347. goto out;
  348. }
  349. /*
  350. * Convert received challenge to binary hex.
  351. */
  352. ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
  353. digest);
  354. if (ret < 0) {
  355. pr_err("crypto_shash_finup() failed for ma challenge\n");
  356. goto out;
  357. }
  358. /*
  359. * Generate CHAP_N and CHAP_R.
  360. */
  361. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  362. *nr_out_len += 1;
  363. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  364. /*
  365. * Convert response from binary hex to ascii hext.
  366. */
  367. bin2hex(response, digest, MD5_SIGNATURE_SIZE);
  368. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  369. response);
  370. *nr_out_len += 1;
  371. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  372. auth_ret = 0;
  373. out:
  374. kzfree(desc);
  375. if (tfm)
  376. crypto_free_shash(tfm);
  377. kfree(challenge);
  378. kfree(challenge_binhex);
  379. return auth_ret;
  380. }
  381. static int chap_got_response(
  382. struct iscsi_conn *conn,
  383. struct iscsi_node_auth *auth,
  384. char *nr_in_ptr,
  385. char *nr_out_ptr,
  386. unsigned int *nr_out_len)
  387. {
  388. struct iscsi_chap *chap = conn->auth_protocol;
  389. switch (chap->digest_type) {
  390. case CHAP_DIGEST_MD5:
  391. if (chap_server_compute_md5(conn, auth, nr_in_ptr,
  392. nr_out_ptr, nr_out_len) < 0)
  393. return -1;
  394. return 0;
  395. default:
  396. pr_err("Unknown CHAP digest type %d!\n",
  397. chap->digest_type);
  398. return -1;
  399. }
  400. }
  401. u32 chap_main_loop(
  402. struct iscsi_conn *conn,
  403. struct iscsi_node_auth *auth,
  404. char *in_text,
  405. char *out_text,
  406. int *in_len,
  407. int *out_len)
  408. {
  409. struct iscsi_chap *chap = conn->auth_protocol;
  410. if (!chap) {
  411. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  412. if (!chap)
  413. return 2;
  414. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  415. return 0;
  416. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  417. convert_null_to_semi(in_text, *in_len);
  418. if (chap_got_response(conn, auth, in_text, out_text,
  419. out_len) < 0) {
  420. chap_close(conn);
  421. return 2;
  422. }
  423. if (auth->authenticate_target)
  424. chap->chap_state = CHAP_STAGE_SERVER_NR;
  425. else
  426. *out_len = 0;
  427. chap_close(conn);
  428. return 1;
  429. }
  430. return 2;
  431. }