iscsi_target_auth.c 12 KB

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