iscsi_target_auth.c 12 KB

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