iscsi_target_auth.c 11 KB

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