iscsi_target_auth.c 12 KB

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