iscsi_target_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 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. unsigned long id;
  154. unsigned char id_as_uchar;
  155. unsigned char digest[MD5_SIGNATURE_SIZE];
  156. unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
  157. unsigned char identifier[10], *challenge = NULL;
  158. unsigned char *challenge_binhex = NULL;
  159. unsigned char client_digest[MD5_SIGNATURE_SIZE];
  160. unsigned char server_digest[MD5_SIGNATURE_SIZE];
  161. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  162. size_t compare_len;
  163. struct iscsi_chap *chap = conn->auth_protocol;
  164. struct crypto_hash *tfm;
  165. struct hash_desc desc;
  166. struct scatterlist sg;
  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_hash("md5", 0, CRYPTO_ALG_ASYNC);
  219. if (IS_ERR(tfm)) {
  220. pr_err("Unable to allocate struct crypto_hash\n");
  221. goto out;
  222. }
  223. desc.tfm = tfm;
  224. desc.flags = 0;
  225. ret = crypto_hash_init(&desc);
  226. if (ret < 0) {
  227. pr_err("crypto_hash_init() failed\n");
  228. crypto_free_hash(tfm);
  229. goto out;
  230. }
  231. sg_init_one(&sg, &chap->id, 1);
  232. ret = crypto_hash_update(&desc, &sg, 1);
  233. if (ret < 0) {
  234. pr_err("crypto_hash_update() failed for id\n");
  235. crypto_free_hash(tfm);
  236. goto out;
  237. }
  238. sg_init_one(&sg, &auth->password, strlen(auth->password));
  239. ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
  240. if (ret < 0) {
  241. pr_err("crypto_hash_update() failed for password\n");
  242. crypto_free_hash(tfm);
  243. goto out;
  244. }
  245. sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH);
  246. ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
  247. if (ret < 0) {
  248. pr_err("crypto_hash_update() failed for challenge\n");
  249. crypto_free_hash(tfm);
  250. goto out;
  251. }
  252. ret = crypto_hash_final(&desc, server_digest);
  253. if (ret < 0) {
  254. pr_err("crypto_hash_final() failed for server digest\n");
  255. crypto_free_hash(tfm);
  256. goto out;
  257. }
  258. crypto_free_hash(tfm);
  259. chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
  260. pr_debug("[server] MD5 Server Digest: %s\n", response);
  261. if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
  262. pr_debug("[server] MD5 Digests do not match!\n\n");
  263. goto out;
  264. } else
  265. pr_debug("[server] MD5 Digests match, CHAP connetication"
  266. " successful.\n\n");
  267. /*
  268. * One way authentication has succeeded, return now if mutual
  269. * authentication is not enabled.
  270. */
  271. if (!auth->authenticate_target) {
  272. kfree(challenge);
  273. kfree(challenge_binhex);
  274. return 0;
  275. }
  276. /*
  277. * Get CHAP_I.
  278. */
  279. if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
  280. pr_err("Could not find CHAP_I.\n");
  281. goto out;
  282. }
  283. if (type == HEX)
  284. ret = kstrtoul(&identifier[2], 0, &id);
  285. else
  286. ret = kstrtoul(identifier, 0, &id);
  287. if (ret < 0) {
  288. pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
  289. goto out;
  290. }
  291. if (id > 255) {
  292. pr_err("chap identifier: %lu greater than 255\n", id);
  293. goto out;
  294. }
  295. /*
  296. * RFC 1994 says Identifier is no more than octet (8 bits).
  297. */
  298. pr_debug("[server] Got CHAP_I=%lu\n", id);
  299. /*
  300. * Get CHAP_C.
  301. */
  302. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  303. challenge, &type) < 0) {
  304. pr_err("Could not find CHAP_C.\n");
  305. goto out;
  306. }
  307. if (type != HEX) {
  308. pr_err("Could not find CHAP_C.\n");
  309. goto out;
  310. }
  311. pr_debug("[server] Got CHAP_C=%s\n", challenge);
  312. challenge_len = chap_string_to_hex(challenge_binhex, challenge,
  313. strlen(challenge));
  314. if (!challenge_len) {
  315. pr_err("Unable to convert incoming challenge\n");
  316. goto out;
  317. }
  318. if (challenge_len > 1024) {
  319. pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
  320. goto out;
  321. }
  322. /*
  323. * During mutual authentication, the CHAP_C generated by the
  324. * initiator must not match the original CHAP_C generated by
  325. * the target.
  326. */
  327. if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
  328. pr_err("initiator CHAP_C matches target CHAP_C, failing"
  329. " login attempt\n");
  330. goto out;
  331. }
  332. /*
  333. * Generate CHAP_N and CHAP_R for mutual authentication.
  334. */
  335. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  336. if (IS_ERR(tfm)) {
  337. pr_err("Unable to allocate struct crypto_hash\n");
  338. goto out;
  339. }
  340. desc.tfm = tfm;
  341. desc.flags = 0;
  342. ret = crypto_hash_init(&desc);
  343. if (ret < 0) {
  344. pr_err("crypto_hash_init() failed\n");
  345. crypto_free_hash(tfm);
  346. goto out;
  347. }
  348. /* To handle both endiannesses */
  349. id_as_uchar = id;
  350. sg_init_one(&sg, &id_as_uchar, 1);
  351. ret = crypto_hash_update(&desc, &sg, 1);
  352. if (ret < 0) {
  353. pr_err("crypto_hash_update() failed for id\n");
  354. crypto_free_hash(tfm);
  355. goto out;
  356. }
  357. sg_init_one(&sg, auth->password_mutual,
  358. strlen(auth->password_mutual));
  359. ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
  360. if (ret < 0) {
  361. pr_err("crypto_hash_update() failed for"
  362. " password_mutual\n");
  363. crypto_free_hash(tfm);
  364. goto out;
  365. }
  366. /*
  367. * Convert received challenge to binary hex.
  368. */
  369. sg_init_one(&sg, challenge_binhex, challenge_len);
  370. ret = crypto_hash_update(&desc, &sg, challenge_len);
  371. if (ret < 0) {
  372. pr_err("crypto_hash_update() failed for ma challenge\n");
  373. crypto_free_hash(tfm);
  374. goto out;
  375. }
  376. ret = crypto_hash_final(&desc, digest);
  377. if (ret < 0) {
  378. pr_err("crypto_hash_final() failed for ma digest\n");
  379. crypto_free_hash(tfm);
  380. goto out;
  381. }
  382. crypto_free_hash(tfm);
  383. /*
  384. * Generate CHAP_N and CHAP_R.
  385. */
  386. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  387. *nr_out_len += 1;
  388. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  389. /*
  390. * Convert response from binary hex to ascii hext.
  391. */
  392. chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
  393. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  394. response);
  395. *nr_out_len += 1;
  396. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  397. auth_ret = 0;
  398. out:
  399. kfree(challenge);
  400. kfree(challenge_binhex);
  401. return auth_ret;
  402. }
  403. static int chap_got_response(
  404. struct iscsi_conn *conn,
  405. struct iscsi_node_auth *auth,
  406. char *nr_in_ptr,
  407. char *nr_out_ptr,
  408. unsigned int *nr_out_len)
  409. {
  410. struct iscsi_chap *chap = conn->auth_protocol;
  411. switch (chap->digest_type) {
  412. case CHAP_DIGEST_MD5:
  413. if (chap_server_compute_md5(conn, auth, nr_in_ptr,
  414. nr_out_ptr, nr_out_len) < 0)
  415. return -1;
  416. return 0;
  417. default:
  418. pr_err("Unknown CHAP digest type %d!\n",
  419. chap->digest_type);
  420. return -1;
  421. }
  422. }
  423. u32 chap_main_loop(
  424. struct iscsi_conn *conn,
  425. struct iscsi_node_auth *auth,
  426. char *in_text,
  427. char *out_text,
  428. int *in_len,
  429. int *out_len)
  430. {
  431. struct iscsi_chap *chap = conn->auth_protocol;
  432. if (!chap) {
  433. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  434. if (!chap)
  435. return 2;
  436. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  437. return 0;
  438. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  439. convert_null_to_semi(in_text, *in_len);
  440. if (chap_got_response(conn, auth, in_text, out_text,
  441. out_len) < 0) {
  442. chap_close(conn);
  443. return 2;
  444. }
  445. if (auth->authenticate_target)
  446. chap->chap_state = CHAP_STAGE_SERVER_NR;
  447. else
  448. *out_len = 0;
  449. chap_close(conn);
  450. return 1;
  451. }
  452. return 2;
  453. }