iscsi_target_auth.c 12 KB

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