iscsi_target_auth.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * This file houses the main functions for the iSCSI CHAP support
  4. *
  5. * (c) Copyright 2007-2013 Datera, Inc.
  6. *
  7. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  8. *
  9. ******************************************************************************/
  10. #include <crypto/hash.h>
  11. #include <linux/kernel.h>
  12. #include <linux/string.h>
  13. #include <linux/err.h>
  14. #include <linux/hex.h>
  15. #include <linux/random.h>
  16. #include <linux/scatterlist.h>
  17. #include <target/iscsi/iscsi_target_core.h>
  18. #include "iscsi_target_nego.h"
  19. #include "iscsi_target_auth.h"
  20. static char *chap_get_digest_name(const int digest_type)
  21. {
  22. switch (digest_type) {
  23. case CHAP_DIGEST_MD5:
  24. return "md5";
  25. case CHAP_DIGEST_SHA1:
  26. return "sha1";
  27. case CHAP_DIGEST_SHA256:
  28. return "sha256";
  29. case CHAP_DIGEST_SHA3_256:
  30. return "sha3-256";
  31. default:
  32. return NULL;
  33. }
  34. }
  35. static int chap_gen_challenge(
  36. struct iscsit_conn *conn,
  37. int caller,
  38. char *c_str,
  39. unsigned int *c_len)
  40. {
  41. int ret;
  42. unsigned char *challenge_asciihex;
  43. struct iscsi_chap *chap = conn->auth_protocol;
  44. challenge_asciihex = kzalloc(chap->challenge_len * 2 + 1, GFP_KERNEL);
  45. if (!challenge_asciihex)
  46. return -ENOMEM;
  47. memset(chap->challenge, 0, MAX_CHAP_CHALLENGE_LEN);
  48. ret = get_random_bytes_wait(chap->challenge, chap->challenge_len);
  49. if (unlikely(ret))
  50. goto out;
  51. bin2hex(challenge_asciihex, chap->challenge,
  52. chap->challenge_len);
  53. /*
  54. * Set CHAP_C, and copy the generated challenge into c_str.
  55. */
  56. *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
  57. *c_len += 1;
  58. pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
  59. challenge_asciihex);
  60. out:
  61. kfree(challenge_asciihex);
  62. return ret;
  63. }
  64. static int chap_test_algorithm(const char *name)
  65. {
  66. struct crypto_shash *tfm;
  67. tfm = crypto_alloc_shash(name, 0, 0);
  68. if (IS_ERR(tfm))
  69. return -1;
  70. crypto_free_shash(tfm);
  71. return 0;
  72. }
  73. static int chap_check_algorithm(const char *a_str)
  74. {
  75. char *tmp, *orig, *token, *digest_name;
  76. long digest_type;
  77. int r = CHAP_DIGEST_UNKNOWN;
  78. tmp = kstrdup(a_str, GFP_KERNEL);
  79. if (!tmp) {
  80. pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
  81. return CHAP_DIGEST_UNKNOWN;
  82. }
  83. orig = tmp;
  84. token = strsep(&tmp, "=");
  85. if (!token)
  86. goto out;
  87. if (strcmp(token, "CHAP_A")) {
  88. pr_err("Unable to locate CHAP_A key\n");
  89. goto out;
  90. }
  91. while (token) {
  92. token = strsep(&tmp, ",");
  93. if (!token)
  94. goto out;
  95. if (kstrtol(token, 10, &digest_type))
  96. continue;
  97. digest_name = chap_get_digest_name(digest_type);
  98. if (!digest_name)
  99. continue;
  100. pr_debug("Selected %s Algorithm\n", digest_name);
  101. if (chap_test_algorithm(digest_name) < 0) {
  102. pr_err("failed to allocate %s algo\n", digest_name);
  103. } else {
  104. r = digest_type;
  105. goto out;
  106. }
  107. }
  108. out:
  109. kfree(orig);
  110. return r;
  111. }
  112. static void chap_close(struct iscsit_conn *conn)
  113. {
  114. kfree(conn->auth_protocol);
  115. conn->auth_protocol = NULL;
  116. }
  117. static struct iscsi_chap *chap_server_open(
  118. struct iscsit_conn *conn,
  119. struct iscsi_node_auth *auth,
  120. const char *a_str,
  121. char *aic_str,
  122. unsigned int *aic_len)
  123. {
  124. int digest_type;
  125. struct iscsi_chap *chap;
  126. if (!(auth->naf_flags & NAF_USERID_SET) ||
  127. !(auth->naf_flags & NAF_PASSWORD_SET)) {
  128. pr_err("CHAP user or password not set for"
  129. " Initiator ACL\n");
  130. return NULL;
  131. }
  132. conn->auth_protocol = kzalloc_obj(struct iscsi_chap);
  133. if (!conn->auth_protocol)
  134. return NULL;
  135. chap = conn->auth_protocol;
  136. digest_type = chap_check_algorithm(a_str);
  137. switch (digest_type) {
  138. case CHAP_DIGEST_MD5:
  139. chap->digest_size = MD5_SIGNATURE_SIZE;
  140. break;
  141. case CHAP_DIGEST_SHA1:
  142. chap->digest_size = SHA1_SIGNATURE_SIZE;
  143. break;
  144. case CHAP_DIGEST_SHA256:
  145. chap->digest_size = SHA256_SIGNATURE_SIZE;
  146. break;
  147. case CHAP_DIGEST_SHA3_256:
  148. chap->digest_size = SHA3_256_SIGNATURE_SIZE;
  149. break;
  150. case CHAP_DIGEST_UNKNOWN:
  151. default:
  152. pr_err("Unsupported CHAP_A value\n");
  153. chap_close(conn);
  154. return NULL;
  155. }
  156. chap->digest_name = chap_get_digest_name(digest_type);
  157. /* Tie the challenge length to the digest size */
  158. chap->challenge_len = chap->digest_size;
  159. pr_debug("[server] Got CHAP_A=%d\n", digest_type);
  160. *aic_len = sprintf(aic_str, "CHAP_A=%d", digest_type);
  161. *aic_len += 1;
  162. pr_debug("[server] Sending CHAP_A=%d\n", digest_type);
  163. /*
  164. * Set Identifier.
  165. */
  166. chap->id = conn->tpg->tpg_chap_id++;
  167. *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
  168. *aic_len += 1;
  169. pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
  170. /*
  171. * Generate Challenge.
  172. */
  173. if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
  174. chap_close(conn);
  175. return NULL;
  176. }
  177. return chap;
  178. }
  179. static const char base64_lookup_table[] =
  180. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  181. static int chap_base64_decode(u8 *dst, const char *src, size_t len)
  182. {
  183. int i, bits = 0, ac = 0;
  184. const char *p;
  185. u8 *cp = dst;
  186. for (i = 0; i < len; i++) {
  187. if (src[i] == '=')
  188. return cp - dst;
  189. p = strchr(base64_lookup_table, src[i]);
  190. if (p == NULL || src[i] == 0)
  191. return -2;
  192. ac <<= 6;
  193. ac += (p - base64_lookup_table);
  194. bits += 6;
  195. if (bits >= 8) {
  196. *cp++ = (ac >> (bits - 8)) & 0xff;
  197. ac &= ~(BIT(16) - BIT(bits - 8));
  198. bits -= 8;
  199. }
  200. }
  201. if (ac)
  202. return -1;
  203. return cp - dst;
  204. }
  205. static int chap_server_compute_hash(
  206. struct iscsit_conn *conn,
  207. struct iscsi_node_auth *auth,
  208. char *nr_in_ptr,
  209. char *nr_out_ptr,
  210. unsigned int *nr_out_len)
  211. {
  212. unsigned long id;
  213. unsigned char id_as_uchar;
  214. unsigned char type;
  215. unsigned char identifier[10], *initiatorchg = NULL;
  216. unsigned char *initiatorchg_binhex = NULL;
  217. unsigned char *digest = NULL;
  218. unsigned char *response = NULL;
  219. unsigned char *client_digest = NULL;
  220. unsigned char *server_digest = NULL;
  221. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  222. size_t compare_len;
  223. struct iscsi_chap *chap = conn->auth_protocol;
  224. struct crypto_shash *tfm = NULL;
  225. struct shash_desc *desc = NULL;
  226. int auth_ret = -1, ret, initiatorchg_len;
  227. digest = kzalloc(chap->digest_size, GFP_KERNEL);
  228. if (!digest) {
  229. pr_err("Unable to allocate the digest buffer\n");
  230. goto out;
  231. }
  232. response = kzalloc(chap->digest_size * 2 + 2, GFP_KERNEL);
  233. if (!response) {
  234. pr_err("Unable to allocate the response buffer\n");
  235. goto out;
  236. }
  237. client_digest = kzalloc(chap->digest_size, GFP_KERNEL);
  238. if (!client_digest) {
  239. pr_err("Unable to allocate the client_digest buffer\n");
  240. goto out;
  241. }
  242. server_digest = kzalloc(chap->digest_size, GFP_KERNEL);
  243. if (!server_digest) {
  244. pr_err("Unable to allocate the server_digest buffer\n");
  245. goto out;
  246. }
  247. memset(identifier, 0, 10);
  248. memset(chap_n, 0, MAX_CHAP_N_SIZE);
  249. memset(chap_r, 0, MAX_RESPONSE_LENGTH);
  250. initiatorchg = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  251. if (!initiatorchg) {
  252. pr_err("Unable to allocate challenge buffer\n");
  253. goto out;
  254. }
  255. initiatorchg_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  256. if (!initiatorchg_binhex) {
  257. pr_err("Unable to allocate initiatorchg_binhex buffer\n");
  258. goto out;
  259. }
  260. /*
  261. * Extract CHAP_N.
  262. */
  263. if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
  264. &type) < 0) {
  265. pr_err("Could not find CHAP_N.\n");
  266. goto out;
  267. }
  268. if (type == HEX) {
  269. pr_err("Could not find CHAP_N.\n");
  270. goto out;
  271. }
  272. /* Include the terminating NULL in the compare */
  273. compare_len = strlen(auth->userid) + 1;
  274. if (strncmp(chap_n, auth->userid, compare_len) != 0) {
  275. pr_err("CHAP_N values do not match!\n");
  276. goto out;
  277. }
  278. pr_debug("[server] Got CHAP_N=%s\n", chap_n);
  279. /*
  280. * Extract CHAP_R.
  281. */
  282. if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
  283. &type) < 0) {
  284. pr_err("Could not find CHAP_R.\n");
  285. goto out;
  286. }
  287. switch (type) {
  288. case HEX:
  289. if (strlen(chap_r) != chap->digest_size * 2) {
  290. pr_err("Malformed CHAP_R\n");
  291. goto out;
  292. }
  293. if (hex2bin(client_digest, chap_r, chap->digest_size) < 0) {
  294. pr_err("Malformed CHAP_R: invalid HEX\n");
  295. goto out;
  296. }
  297. break;
  298. case BASE64:
  299. if (chap_base64_decode(client_digest, chap_r, strlen(chap_r)) !=
  300. chap->digest_size) {
  301. pr_err("Malformed CHAP_R: invalid BASE64\n");
  302. goto out;
  303. }
  304. break;
  305. default:
  306. pr_err("Could not find CHAP_R\n");
  307. goto out;
  308. }
  309. pr_debug("[server] Got CHAP_R=%s\n", chap_r);
  310. tfm = crypto_alloc_shash(chap->digest_name, 0, 0);
  311. if (IS_ERR(tfm)) {
  312. tfm = NULL;
  313. pr_err("Unable to allocate struct crypto_shash\n");
  314. goto out;
  315. }
  316. desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
  317. if (!desc) {
  318. pr_err("Unable to allocate struct shash_desc\n");
  319. goto out;
  320. }
  321. desc->tfm = tfm;
  322. ret = crypto_shash_init(desc);
  323. if (ret < 0) {
  324. pr_err("crypto_shash_init() failed\n");
  325. goto out;
  326. }
  327. ret = crypto_shash_update(desc, &chap->id, 1);
  328. if (ret < 0) {
  329. pr_err("crypto_shash_update() failed for id\n");
  330. goto out;
  331. }
  332. ret = crypto_shash_update(desc, (char *)&auth->password,
  333. strlen(auth->password));
  334. if (ret < 0) {
  335. pr_err("crypto_shash_update() failed for password\n");
  336. goto out;
  337. }
  338. ret = crypto_shash_finup(desc, chap->challenge,
  339. chap->challenge_len, server_digest);
  340. if (ret < 0) {
  341. pr_err("crypto_shash_finup() failed for challenge\n");
  342. goto out;
  343. }
  344. bin2hex(response, server_digest, chap->digest_size);
  345. pr_debug("[server] %s Server Digest: %s\n",
  346. chap->digest_name, response);
  347. if (memcmp(server_digest, client_digest, chap->digest_size) != 0) {
  348. pr_debug("[server] %s Digests do not match!\n\n",
  349. chap->digest_name);
  350. goto out;
  351. } else
  352. pr_debug("[server] %s Digests match, CHAP connection"
  353. " successful.\n\n", chap->digest_name);
  354. /*
  355. * One way authentication has succeeded, return now if mutual
  356. * authentication is not enabled.
  357. */
  358. if (!auth->authenticate_target) {
  359. auth_ret = 0;
  360. goto out;
  361. }
  362. /*
  363. * Get CHAP_I.
  364. */
  365. ret = extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type);
  366. if (ret == -ENOENT) {
  367. pr_debug("Could not find CHAP_I. Initiator uses One way authentication.\n");
  368. auth_ret = 0;
  369. goto out;
  370. }
  371. if (ret < 0) {
  372. pr_err("Could not find CHAP_I.\n");
  373. goto out;
  374. }
  375. if (type == HEX)
  376. ret = kstrtoul(&identifier[2], 0, &id);
  377. else
  378. ret = kstrtoul(identifier, 0, &id);
  379. if (ret < 0) {
  380. pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
  381. goto out;
  382. }
  383. if (id > 255) {
  384. pr_err("chap identifier: %lu greater than 255\n", id);
  385. goto out;
  386. }
  387. /*
  388. * RFC 1994 says Identifier is no more than octet (8 bits).
  389. */
  390. pr_debug("[server] Got CHAP_I=%lu\n", id);
  391. /*
  392. * Get CHAP_C.
  393. */
  394. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  395. initiatorchg, &type) < 0) {
  396. pr_err("Could not find CHAP_C.\n");
  397. goto out;
  398. }
  399. switch (type) {
  400. case HEX:
  401. initiatorchg_len = DIV_ROUND_UP(strlen(initiatorchg), 2);
  402. if (!initiatorchg_len) {
  403. pr_err("Unable to convert incoming challenge\n");
  404. goto out;
  405. }
  406. if (initiatorchg_len > 1024) {
  407. pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
  408. goto out;
  409. }
  410. if (hex2bin(initiatorchg_binhex, initiatorchg,
  411. initiatorchg_len) < 0) {
  412. pr_err("Malformed CHAP_C: invalid HEX\n");
  413. goto out;
  414. }
  415. break;
  416. case BASE64:
  417. initiatorchg_len = chap_base64_decode(initiatorchg_binhex,
  418. initiatorchg,
  419. strlen(initiatorchg));
  420. if (initiatorchg_len < 0) {
  421. pr_err("Malformed CHAP_C: invalid BASE64\n");
  422. goto out;
  423. }
  424. if (!initiatorchg_len) {
  425. pr_err("Unable to convert incoming challenge\n");
  426. goto out;
  427. }
  428. if (initiatorchg_len > 1024) {
  429. pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
  430. goto out;
  431. }
  432. break;
  433. default:
  434. pr_err("Could not find CHAP_C.\n");
  435. goto out;
  436. }
  437. pr_debug("[server] Got CHAP_C=%s\n", initiatorchg);
  438. /*
  439. * During mutual authentication, the CHAP_C generated by the
  440. * initiator must not match the original CHAP_C generated by
  441. * the target.
  442. */
  443. if (initiatorchg_len == chap->challenge_len &&
  444. !memcmp(initiatorchg_binhex, chap->challenge,
  445. initiatorchg_len)) {
  446. pr_err("initiator CHAP_C matches target CHAP_C, failing"
  447. " login attempt\n");
  448. goto out;
  449. }
  450. /*
  451. * Generate CHAP_N and CHAP_R for mutual authentication.
  452. */
  453. ret = crypto_shash_init(desc);
  454. if (ret < 0) {
  455. pr_err("crypto_shash_init() failed\n");
  456. goto out;
  457. }
  458. /* To handle both endiannesses */
  459. id_as_uchar = id;
  460. ret = crypto_shash_update(desc, &id_as_uchar, 1);
  461. if (ret < 0) {
  462. pr_err("crypto_shash_update() failed for id\n");
  463. goto out;
  464. }
  465. ret = crypto_shash_update(desc, auth->password_mutual,
  466. strlen(auth->password_mutual));
  467. if (ret < 0) {
  468. pr_err("crypto_shash_update() failed for"
  469. " password_mutual\n");
  470. goto out;
  471. }
  472. /*
  473. * Convert received challenge to binary hex.
  474. */
  475. ret = crypto_shash_finup(desc, initiatorchg_binhex, initiatorchg_len,
  476. digest);
  477. if (ret < 0) {
  478. pr_err("crypto_shash_finup() failed for ma challenge\n");
  479. goto out;
  480. }
  481. /*
  482. * Generate CHAP_N and CHAP_R.
  483. */
  484. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  485. *nr_out_len += 1;
  486. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  487. /*
  488. * Convert response from binary hex to ascii hext.
  489. */
  490. bin2hex(response, digest, chap->digest_size);
  491. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  492. response);
  493. *nr_out_len += 1;
  494. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  495. auth_ret = 0;
  496. out:
  497. kfree_sensitive(desc);
  498. if (tfm)
  499. crypto_free_shash(tfm);
  500. kfree(initiatorchg);
  501. kfree(initiatorchg_binhex);
  502. kfree(digest);
  503. kfree(response);
  504. kfree(server_digest);
  505. kfree(client_digest);
  506. return auth_ret;
  507. }
  508. u32 chap_main_loop(
  509. struct iscsit_conn *conn,
  510. struct iscsi_node_auth *auth,
  511. char *in_text,
  512. char *out_text,
  513. int *in_len,
  514. int *out_len)
  515. {
  516. struct iscsi_chap *chap = conn->auth_protocol;
  517. if (!chap) {
  518. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  519. if (!chap)
  520. return 2;
  521. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  522. return 0;
  523. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  524. convert_null_to_semi(in_text, *in_len);
  525. if (chap_server_compute_hash(conn, auth, in_text, out_text,
  526. out_len) < 0) {
  527. chap_close(conn);
  528. return 2;
  529. }
  530. if (auth->authenticate_target)
  531. chap->chap_state = CHAP_STAGE_SERVER_NR;
  532. else
  533. *out_len = 0;
  534. chap_close(conn);
  535. return 1;
  536. }
  537. return 2;
  538. }