iscsi_target_auth.c 12 KB

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