iscsi_target_auth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 struct iscsi_chap *chap_server_open(
  63. struct iscsi_conn *conn,
  64. struct iscsi_node_auth *auth,
  65. const char *a_str,
  66. char *aic_str,
  67. unsigned int *aic_len)
  68. {
  69. struct iscsi_chap *chap;
  70. if (!(auth->naf_flags & NAF_USERID_SET) ||
  71. !(auth->naf_flags & NAF_PASSWORD_SET)) {
  72. pr_err("CHAP user or password not set for"
  73. " Initiator ACL\n");
  74. return NULL;
  75. }
  76. conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
  77. if (!conn->auth_protocol)
  78. return NULL;
  79. chap = conn->auth_protocol;
  80. /*
  81. * We only support MD5 MDA presently.
  82. */
  83. if (strncmp(a_str, "CHAP_A=5", 8)) {
  84. pr_err("CHAP_A is not MD5.\n");
  85. return NULL;
  86. }
  87. pr_debug("[server] Got CHAP_A=5\n");
  88. /*
  89. * Send back CHAP_A set to MD5.
  90. */
  91. *aic_len = sprintf(aic_str, "CHAP_A=5");
  92. *aic_len += 1;
  93. chap->digest_type = CHAP_DIGEST_MD5;
  94. pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
  95. /*
  96. * Set Identifier.
  97. */
  98. chap->id = conn->tpg->tpg_chap_id++;
  99. *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
  100. *aic_len += 1;
  101. pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
  102. /*
  103. * Generate Challenge.
  104. */
  105. chap_gen_challenge(conn, 1, aic_str, aic_len);
  106. return chap;
  107. }
  108. static void chap_close(struct iscsi_conn *conn)
  109. {
  110. kfree(conn->auth_protocol);
  111. conn->auth_protocol = NULL;
  112. }
  113. static int chap_server_compute_md5(
  114. struct iscsi_conn *conn,
  115. struct iscsi_node_auth *auth,
  116. char *nr_in_ptr,
  117. char *nr_out_ptr,
  118. unsigned int *nr_out_len)
  119. {
  120. char *endptr;
  121. unsigned long id;
  122. unsigned char id_as_uchar;
  123. unsigned char digest[MD5_SIGNATURE_SIZE];
  124. unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
  125. unsigned char identifier[10], *challenge = NULL;
  126. unsigned char *challenge_binhex = NULL;
  127. unsigned char client_digest[MD5_SIGNATURE_SIZE];
  128. unsigned char server_digest[MD5_SIGNATURE_SIZE];
  129. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  130. size_t compare_len;
  131. struct iscsi_chap *chap = conn->auth_protocol;
  132. struct crypto_hash *tfm;
  133. struct hash_desc desc;
  134. struct scatterlist sg;
  135. int auth_ret = -1, ret, challenge_len;
  136. memset(identifier, 0, 10);
  137. memset(chap_n, 0, MAX_CHAP_N_SIZE);
  138. memset(chap_r, 0, MAX_RESPONSE_LENGTH);
  139. memset(digest, 0, MD5_SIGNATURE_SIZE);
  140. memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
  141. memset(client_digest, 0, MD5_SIGNATURE_SIZE);
  142. memset(server_digest, 0, MD5_SIGNATURE_SIZE);
  143. challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  144. if (!challenge) {
  145. pr_err("Unable to allocate challenge buffer\n");
  146. goto out;
  147. }
  148. challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  149. if (!challenge_binhex) {
  150. pr_err("Unable to allocate challenge_binhex buffer\n");
  151. goto out;
  152. }
  153. /*
  154. * Extract CHAP_N.
  155. */
  156. if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
  157. &type) < 0) {
  158. pr_err("Could not find CHAP_N.\n");
  159. goto out;
  160. }
  161. if (type == HEX) {
  162. pr_err("Could not find CHAP_N.\n");
  163. goto out;
  164. }
  165. /* Include the terminating NULL in the compare */
  166. compare_len = strlen(auth->userid) + 1;
  167. if (strncmp(chap_n, auth->userid, compare_len) != 0) {
  168. pr_err("CHAP_N values do not match!\n");
  169. goto out;
  170. }
  171. pr_debug("[server] Got CHAP_N=%s\n", chap_n);
  172. /*
  173. * Extract CHAP_R.
  174. */
  175. if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
  176. &type) < 0) {
  177. pr_err("Could not find CHAP_R.\n");
  178. goto out;
  179. }
  180. if (type != HEX) {
  181. pr_err("Could not find CHAP_R.\n");
  182. goto out;
  183. }
  184. pr_debug("[server] Got CHAP_R=%s\n", chap_r);
  185. chap_string_to_hex(client_digest, chap_r, strlen(chap_r));
  186. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  187. if (IS_ERR(tfm)) {
  188. pr_err("Unable to allocate struct crypto_hash\n");
  189. goto out;
  190. }
  191. desc.tfm = tfm;
  192. desc.flags = 0;
  193. ret = crypto_hash_init(&desc);
  194. if (ret < 0) {
  195. pr_err("crypto_hash_init() failed\n");
  196. crypto_free_hash(tfm);
  197. goto out;
  198. }
  199. sg_init_one(&sg, &chap->id, 1);
  200. ret = crypto_hash_update(&desc, &sg, 1);
  201. if (ret < 0) {
  202. pr_err("crypto_hash_update() failed for id\n");
  203. crypto_free_hash(tfm);
  204. goto out;
  205. }
  206. sg_init_one(&sg, &auth->password, strlen(auth->password));
  207. ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
  208. if (ret < 0) {
  209. pr_err("crypto_hash_update() failed for password\n");
  210. crypto_free_hash(tfm);
  211. goto out;
  212. }
  213. sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH);
  214. ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
  215. if (ret < 0) {
  216. pr_err("crypto_hash_update() failed for challenge\n");
  217. crypto_free_hash(tfm);
  218. goto out;
  219. }
  220. ret = crypto_hash_final(&desc, server_digest);
  221. if (ret < 0) {
  222. pr_err("crypto_hash_final() failed for server digest\n");
  223. crypto_free_hash(tfm);
  224. goto out;
  225. }
  226. crypto_free_hash(tfm);
  227. chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
  228. pr_debug("[server] MD5 Server Digest: %s\n", response);
  229. if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
  230. pr_debug("[server] MD5 Digests do not match!\n\n");
  231. goto out;
  232. } else
  233. pr_debug("[server] MD5 Digests match, CHAP connetication"
  234. " successful.\n\n");
  235. /*
  236. * One way authentication has succeeded, return now if mutual
  237. * authentication is not enabled.
  238. */
  239. if (!auth->authenticate_target) {
  240. kfree(challenge);
  241. kfree(challenge_binhex);
  242. return 0;
  243. }
  244. /*
  245. * Get CHAP_I.
  246. */
  247. if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
  248. pr_err("Could not find CHAP_I.\n");
  249. goto out;
  250. }
  251. if (type == HEX)
  252. id = simple_strtoul(&identifier[2], &endptr, 0);
  253. else
  254. id = simple_strtoul(identifier, &endptr, 0);
  255. if (id > 255) {
  256. pr_err("chap identifier: %lu greater than 255\n", id);
  257. goto out;
  258. }
  259. /*
  260. * RFC 1994 says Identifier is no more than octet (8 bits).
  261. */
  262. pr_debug("[server] Got CHAP_I=%lu\n", id);
  263. /*
  264. * Get CHAP_C.
  265. */
  266. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  267. challenge, &type) < 0) {
  268. pr_err("Could not find CHAP_C.\n");
  269. goto out;
  270. }
  271. if (type != HEX) {
  272. pr_err("Could not find CHAP_C.\n");
  273. goto out;
  274. }
  275. pr_debug("[server] Got CHAP_C=%s\n", challenge);
  276. challenge_len = chap_string_to_hex(challenge_binhex, challenge,
  277. strlen(challenge));
  278. if (!challenge_len) {
  279. pr_err("Unable to convert incoming challenge\n");
  280. goto out;
  281. }
  282. /*
  283. * Generate CHAP_N and CHAP_R for mutual authentication.
  284. */
  285. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  286. if (IS_ERR(tfm)) {
  287. pr_err("Unable to allocate struct crypto_hash\n");
  288. goto out;
  289. }
  290. desc.tfm = tfm;
  291. desc.flags = 0;
  292. ret = crypto_hash_init(&desc);
  293. if (ret < 0) {
  294. pr_err("crypto_hash_init() failed\n");
  295. crypto_free_hash(tfm);
  296. goto out;
  297. }
  298. /* To handle both endiannesses */
  299. id_as_uchar = id;
  300. sg_init_one(&sg, &id_as_uchar, 1);
  301. ret = crypto_hash_update(&desc, &sg, 1);
  302. if (ret < 0) {
  303. pr_err("crypto_hash_update() failed for id\n");
  304. crypto_free_hash(tfm);
  305. goto out;
  306. }
  307. sg_init_one(&sg, auth->password_mutual,
  308. strlen(auth->password_mutual));
  309. ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
  310. if (ret < 0) {
  311. pr_err("crypto_hash_update() failed for"
  312. " password_mutual\n");
  313. crypto_free_hash(tfm);
  314. goto out;
  315. }
  316. /*
  317. * Convert received challenge to binary hex.
  318. */
  319. sg_init_one(&sg, challenge_binhex, challenge_len);
  320. ret = crypto_hash_update(&desc, &sg, challenge_len);
  321. if (ret < 0) {
  322. pr_err("crypto_hash_update() failed for ma challenge\n");
  323. crypto_free_hash(tfm);
  324. goto out;
  325. }
  326. ret = crypto_hash_final(&desc, digest);
  327. if (ret < 0) {
  328. pr_err("crypto_hash_final() failed for ma digest\n");
  329. crypto_free_hash(tfm);
  330. goto out;
  331. }
  332. crypto_free_hash(tfm);
  333. /*
  334. * Generate CHAP_N and CHAP_R.
  335. */
  336. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  337. *nr_out_len += 1;
  338. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  339. /*
  340. * Convert response from binary hex to ascii hext.
  341. */
  342. chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
  343. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  344. response);
  345. *nr_out_len += 1;
  346. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  347. auth_ret = 0;
  348. out:
  349. kfree(challenge);
  350. kfree(challenge_binhex);
  351. return auth_ret;
  352. }
  353. static int chap_got_response(
  354. struct iscsi_conn *conn,
  355. struct iscsi_node_auth *auth,
  356. char *nr_in_ptr,
  357. char *nr_out_ptr,
  358. unsigned int *nr_out_len)
  359. {
  360. struct iscsi_chap *chap = conn->auth_protocol;
  361. switch (chap->digest_type) {
  362. case CHAP_DIGEST_MD5:
  363. if (chap_server_compute_md5(conn, auth, nr_in_ptr,
  364. nr_out_ptr, nr_out_len) < 0)
  365. return -1;
  366. return 0;
  367. default:
  368. pr_err("Unknown CHAP digest type %d!\n",
  369. chap->digest_type);
  370. return -1;
  371. }
  372. }
  373. u32 chap_main_loop(
  374. struct iscsi_conn *conn,
  375. struct iscsi_node_auth *auth,
  376. char *in_text,
  377. char *out_text,
  378. int *in_len,
  379. int *out_len)
  380. {
  381. struct iscsi_chap *chap = conn->auth_protocol;
  382. if (!chap) {
  383. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  384. if (!chap)
  385. return 2;
  386. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  387. return 0;
  388. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  389. convert_null_to_semi(in_text, *in_len);
  390. if (chap_got_response(conn, auth, in_text, out_text,
  391. out_len) < 0) {
  392. chap_close(conn);
  393. return 2;
  394. }
  395. if (auth->authenticate_target)
  396. chap->chap_state = CHAP_STAGE_SERVER_NR;
  397. else
  398. *out_len = 0;
  399. chap_close(conn);
  400. return 1;
  401. }
  402. return 2;
  403. }