iscsi_target_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*******************************************************************************
  2. * This file houses the main functions for the iSCSI CHAP support
  3. *
  4. * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
  5. *
  6. * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
  7. *
  8. * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. ******************************************************************************/
  20. #include <linux/kernel.h>
  21. #include <linux/string.h>
  22. #include <linux/crypto.h>
  23. #include <linux/err.h>
  24. #include <linux/scatterlist.h>
  25. #include "iscsi_target_core.h"
  26. #include "iscsi_target_nego.h"
  27. #include "iscsi_target_auth.h"
  28. static int chap_string_to_hex(unsigned char *dst, unsigned char *src, int len)
  29. {
  30. int j = DIV_ROUND_UP(len, 2), rc;
  31. rc = hex2bin(dst, src, j);
  32. if (rc < 0)
  33. pr_debug("CHAP string contains non hex digit symbols\n");
  34. dst[j] = '\0';
  35. return j;
  36. }
  37. static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
  38. {
  39. int i;
  40. for (i = 0; i < src_len; i++) {
  41. sprintf(&dst[i*2], "%02x", (int) src[i] & 0xff);
  42. }
  43. }
  44. static void chap_set_random(char *data, int length)
  45. {
  46. long r;
  47. unsigned n;
  48. while (length > 0) {
  49. get_random_bytes(&r, sizeof(long));
  50. r = r ^ (r >> 8);
  51. r = r ^ (r >> 4);
  52. n = r & 0x7;
  53. get_random_bytes(&r, sizeof(long));
  54. r = r ^ (r >> 8);
  55. r = r ^ (r >> 5);
  56. n = (n << 3) | (r & 0x7);
  57. get_random_bytes(&r, sizeof(long));
  58. r = r ^ (r >> 8);
  59. r = r ^ (r >> 5);
  60. n = (n << 2) | (r & 0x3);
  61. *data++ = n;
  62. length--;
  63. }
  64. }
  65. static void chap_gen_challenge(
  66. struct iscsi_conn *conn,
  67. int caller,
  68. char *c_str,
  69. unsigned int *c_len)
  70. {
  71. unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
  72. struct iscsi_chap *chap = conn->auth_protocol;
  73. memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
  74. chap_set_random(chap->challenge, CHAP_CHALLENGE_LENGTH);
  75. chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
  76. CHAP_CHALLENGE_LENGTH);
  77. /*
  78. * Set CHAP_C, and copy the generated challenge into c_str.
  79. */
  80. *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
  81. *c_len += 1;
  82. pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
  83. challenge_asciihex);
  84. }
  85. static struct iscsi_chap *chap_server_open(
  86. struct iscsi_conn *conn,
  87. struct iscsi_node_auth *auth,
  88. const char *a_str,
  89. char *aic_str,
  90. unsigned int *aic_len)
  91. {
  92. struct iscsi_chap *chap;
  93. if (!(auth->naf_flags & NAF_USERID_SET) ||
  94. !(auth->naf_flags & NAF_PASSWORD_SET)) {
  95. pr_err("CHAP user or password not set for"
  96. " Initiator ACL\n");
  97. return NULL;
  98. }
  99. conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
  100. if (!conn->auth_protocol)
  101. return NULL;
  102. chap = conn->auth_protocol;
  103. /*
  104. * We only support MD5 MDA presently.
  105. */
  106. if (strncmp(a_str, "CHAP_A=5", 8)) {
  107. pr_err("CHAP_A is not MD5.\n");
  108. return NULL;
  109. }
  110. pr_debug("[server] Got CHAP_A=5\n");
  111. /*
  112. * Send back CHAP_A set to MD5.
  113. */
  114. *aic_len = sprintf(aic_str, "CHAP_A=5");
  115. *aic_len += 1;
  116. chap->digest_type = CHAP_DIGEST_MD5;
  117. pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
  118. /*
  119. * Set Identifier.
  120. */
  121. chap->id = ISCSI_TPG_C(conn)->tpg_chap_id++;
  122. *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
  123. *aic_len += 1;
  124. pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
  125. /*
  126. * Generate Challenge.
  127. */
  128. chap_gen_challenge(conn, 1, aic_str, aic_len);
  129. return chap;
  130. }
  131. static void chap_close(struct iscsi_conn *conn)
  132. {
  133. kfree(conn->auth_protocol);
  134. conn->auth_protocol = NULL;
  135. }
  136. static int chap_server_compute_md5(
  137. struct iscsi_conn *conn,
  138. struct iscsi_node_auth *auth,
  139. char *nr_in_ptr,
  140. char *nr_out_ptr,
  141. unsigned int *nr_out_len)
  142. {
  143. char *endptr;
  144. unsigned long id;
  145. unsigned char id_as_uchar;
  146. unsigned char digest[MD5_SIGNATURE_SIZE];
  147. unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
  148. unsigned char identifier[10], *challenge = NULL;
  149. unsigned char *challenge_binhex = NULL;
  150. unsigned char client_digest[MD5_SIGNATURE_SIZE];
  151. unsigned char server_digest[MD5_SIGNATURE_SIZE];
  152. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  153. size_t compare_len;
  154. struct iscsi_chap *chap = conn->auth_protocol;
  155. struct crypto_hash *tfm;
  156. struct hash_desc desc;
  157. struct scatterlist sg;
  158. int auth_ret = -1, ret, challenge_len;
  159. memset(identifier, 0, 10);
  160. memset(chap_n, 0, MAX_CHAP_N_SIZE);
  161. memset(chap_r, 0, MAX_RESPONSE_LENGTH);
  162. memset(digest, 0, MD5_SIGNATURE_SIZE);
  163. memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
  164. memset(client_digest, 0, MD5_SIGNATURE_SIZE);
  165. memset(server_digest, 0, MD5_SIGNATURE_SIZE);
  166. challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  167. if (!challenge) {
  168. pr_err("Unable to allocate challenge buffer\n");
  169. goto out;
  170. }
  171. challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  172. if (!challenge_binhex) {
  173. pr_err("Unable to allocate challenge_binhex buffer\n");
  174. goto out;
  175. }
  176. /*
  177. * Extract CHAP_N.
  178. */
  179. if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
  180. &type) < 0) {
  181. pr_err("Could not find CHAP_N.\n");
  182. goto out;
  183. }
  184. if (type == HEX) {
  185. pr_err("Could not find CHAP_N.\n");
  186. goto out;
  187. }
  188. /* Include the terminating NULL in the compare */
  189. compare_len = strlen(auth->userid) + 1;
  190. if (strncmp(chap_n, auth->userid, compare_len) != 0) {
  191. pr_err("CHAP_N values do not match!\n");
  192. goto out;
  193. }
  194. pr_debug("[server] Got CHAP_N=%s\n", chap_n);
  195. /*
  196. * Extract CHAP_R.
  197. */
  198. if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
  199. &type) < 0) {
  200. pr_err("Could not find CHAP_R.\n");
  201. goto out;
  202. }
  203. if (type != HEX) {
  204. pr_err("Could not find CHAP_R.\n");
  205. goto out;
  206. }
  207. pr_debug("[server] Got CHAP_R=%s\n", chap_r);
  208. chap_string_to_hex(client_digest, chap_r, strlen(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. chap_binaryhex_to_asciihex(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. id = simple_strtoul(&identifier[2], &endptr, 0);
  276. else
  277. id = simple_strtoul(identifier, &endptr, 0);
  278. if (id > 255) {
  279. pr_err("chap identifier: %lu greater than 255\n", id);
  280. goto out;
  281. }
  282. /*
  283. * RFC 1994 says Identifier is no more than octet (8 bits).
  284. */
  285. pr_debug("[server] Got CHAP_I=%lu\n", id);
  286. /*
  287. * Get CHAP_C.
  288. */
  289. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  290. challenge, &type) < 0) {
  291. pr_err("Could not find CHAP_C.\n");
  292. goto out;
  293. }
  294. if (type != HEX) {
  295. pr_err("Could not find CHAP_C.\n");
  296. goto out;
  297. }
  298. pr_debug("[server] Got CHAP_C=%s\n", challenge);
  299. challenge_len = chap_string_to_hex(challenge_binhex, challenge,
  300. strlen(challenge));
  301. if (!challenge_len) {
  302. pr_err("Unable to convert incoming challenge\n");
  303. goto out;
  304. }
  305. /*
  306. * During mutual authentication, the CHAP_C generated by the
  307. * initiator must not match the original CHAP_C generated by
  308. * the target.
  309. */
  310. if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
  311. pr_err("initiator CHAP_C matches target CHAP_C, failing"
  312. " login attempt\n");
  313. goto out;
  314. }
  315. /*
  316. * Generate CHAP_N and CHAP_R for mutual authentication.
  317. */
  318. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  319. if (IS_ERR(tfm)) {
  320. pr_err("Unable to allocate struct crypto_hash\n");
  321. goto out;
  322. }
  323. desc.tfm = tfm;
  324. desc.flags = 0;
  325. ret = crypto_hash_init(&desc);
  326. if (ret < 0) {
  327. pr_err("crypto_hash_init() failed\n");
  328. crypto_free_hash(tfm);
  329. goto out;
  330. }
  331. /* To handle both endiannesses */
  332. id_as_uchar = id;
  333. sg_init_one(&sg, &id_as_uchar, 1);
  334. ret = crypto_hash_update(&desc, &sg, 1);
  335. if (ret < 0) {
  336. pr_err("crypto_hash_update() failed for id\n");
  337. crypto_free_hash(tfm);
  338. goto out;
  339. }
  340. sg_init_one(&sg, auth->password_mutual,
  341. strlen(auth->password_mutual));
  342. ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
  343. if (ret < 0) {
  344. pr_err("crypto_hash_update() failed for"
  345. " password_mutual\n");
  346. crypto_free_hash(tfm);
  347. goto out;
  348. }
  349. /*
  350. * Convert received challenge to binary hex.
  351. */
  352. sg_init_one(&sg, challenge_binhex, challenge_len);
  353. ret = crypto_hash_update(&desc, &sg, challenge_len);
  354. if (ret < 0) {
  355. pr_err("crypto_hash_update() failed for ma challenge\n");
  356. crypto_free_hash(tfm);
  357. goto out;
  358. }
  359. ret = crypto_hash_final(&desc, digest);
  360. if (ret < 0) {
  361. pr_err("crypto_hash_final() failed for ma digest\n");
  362. crypto_free_hash(tfm);
  363. goto out;
  364. }
  365. crypto_free_hash(tfm);
  366. /*
  367. * Generate CHAP_N and CHAP_R.
  368. */
  369. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  370. *nr_out_len += 1;
  371. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  372. /*
  373. * Convert response from binary hex to ascii hext.
  374. */
  375. chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
  376. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  377. response);
  378. *nr_out_len += 1;
  379. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  380. auth_ret = 0;
  381. out:
  382. kfree(challenge);
  383. kfree(challenge_binhex);
  384. return auth_ret;
  385. }
  386. static int chap_got_response(
  387. struct iscsi_conn *conn,
  388. struct iscsi_node_auth *auth,
  389. char *nr_in_ptr,
  390. char *nr_out_ptr,
  391. unsigned int *nr_out_len)
  392. {
  393. struct iscsi_chap *chap = conn->auth_protocol;
  394. switch (chap->digest_type) {
  395. case CHAP_DIGEST_MD5:
  396. if (chap_server_compute_md5(conn, auth, nr_in_ptr,
  397. nr_out_ptr, nr_out_len) < 0)
  398. return -1;
  399. return 0;
  400. default:
  401. pr_err("Unknown CHAP digest type %d!\n",
  402. chap->digest_type);
  403. return -1;
  404. }
  405. }
  406. u32 chap_main_loop(
  407. struct iscsi_conn *conn,
  408. struct iscsi_node_auth *auth,
  409. char *in_text,
  410. char *out_text,
  411. int *in_len,
  412. int *out_len)
  413. {
  414. struct iscsi_chap *chap = conn->auth_protocol;
  415. if (!chap) {
  416. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  417. if (!chap)
  418. return 2;
  419. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  420. return 0;
  421. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  422. convert_null_to_semi(in_text, *in_len);
  423. if (chap_got_response(conn, auth, in_text, out_text,
  424. out_len) < 0) {
  425. chap_close(conn);
  426. return 2;
  427. }
  428. if (auth->authenticate_target)
  429. chap->chap_state = CHAP_STAGE_SERVER_NR;
  430. else
  431. *out_len = 0;
  432. chap_close(conn);
  433. return 1;
  434. }
  435. return 2;
  436. }