iscsi_target_auth.c 14 KB

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