iscsi_target_auth.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 = (struct iscsi_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 = (struct iscsi_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 char id, digest[MD5_SIGNATURE_SIZE];
  145. unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
  146. unsigned char identifier[10], *challenge = NULL;
  147. unsigned char *challenge_binhex = NULL;
  148. unsigned char client_digest[MD5_SIGNATURE_SIZE];
  149. unsigned char server_digest[MD5_SIGNATURE_SIZE];
  150. unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
  151. struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
  152. struct crypto_hash *tfm;
  153. struct hash_desc desc;
  154. struct scatterlist sg;
  155. int auth_ret = -1, ret, challenge_len;
  156. memset(identifier, 0, 10);
  157. memset(chap_n, 0, MAX_CHAP_N_SIZE);
  158. memset(chap_r, 0, MAX_RESPONSE_LENGTH);
  159. memset(digest, 0, MD5_SIGNATURE_SIZE);
  160. memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
  161. memset(client_digest, 0, MD5_SIGNATURE_SIZE);
  162. memset(server_digest, 0, MD5_SIGNATURE_SIZE);
  163. challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  164. if (!challenge) {
  165. pr_err("Unable to allocate challenge buffer\n");
  166. goto out;
  167. }
  168. challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
  169. if (!challenge_binhex) {
  170. pr_err("Unable to allocate challenge_binhex buffer\n");
  171. goto out;
  172. }
  173. /*
  174. * Extract CHAP_N.
  175. */
  176. if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
  177. &type) < 0) {
  178. pr_err("Could not find CHAP_N.\n");
  179. goto out;
  180. }
  181. if (type == HEX) {
  182. pr_err("Could not find CHAP_N.\n");
  183. goto out;
  184. }
  185. if (memcmp(chap_n, auth->userid, strlen(auth->userid)) != 0) {
  186. pr_err("CHAP_N values do not match!\n");
  187. goto out;
  188. }
  189. pr_debug("[server] Got CHAP_N=%s\n", chap_n);
  190. /*
  191. * Extract CHAP_R.
  192. */
  193. if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
  194. &type) < 0) {
  195. pr_err("Could not find CHAP_R.\n");
  196. goto out;
  197. }
  198. if (type != HEX) {
  199. pr_err("Could not find CHAP_R.\n");
  200. goto out;
  201. }
  202. pr_debug("[server] Got CHAP_R=%s\n", chap_r);
  203. chap_string_to_hex(client_digest, chap_r, strlen(chap_r));
  204. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  205. if (IS_ERR(tfm)) {
  206. pr_err("Unable to allocate struct crypto_hash\n");
  207. goto out;
  208. }
  209. desc.tfm = tfm;
  210. desc.flags = 0;
  211. ret = crypto_hash_init(&desc);
  212. if (ret < 0) {
  213. pr_err("crypto_hash_init() failed\n");
  214. crypto_free_hash(tfm);
  215. goto out;
  216. }
  217. sg_init_one(&sg, (void *)&chap->id, 1);
  218. ret = crypto_hash_update(&desc, &sg, 1);
  219. if (ret < 0) {
  220. pr_err("crypto_hash_update() failed for id\n");
  221. crypto_free_hash(tfm);
  222. goto out;
  223. }
  224. sg_init_one(&sg, (void *)&auth->password, strlen(auth->password));
  225. ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
  226. if (ret < 0) {
  227. pr_err("crypto_hash_update() failed for password\n");
  228. crypto_free_hash(tfm);
  229. goto out;
  230. }
  231. sg_init_one(&sg, (void *)chap->challenge, CHAP_CHALLENGE_LENGTH);
  232. ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
  233. if (ret < 0) {
  234. pr_err("crypto_hash_update() failed for challenge\n");
  235. crypto_free_hash(tfm);
  236. goto out;
  237. }
  238. ret = crypto_hash_final(&desc, server_digest);
  239. if (ret < 0) {
  240. pr_err("crypto_hash_final() failed for server digest\n");
  241. crypto_free_hash(tfm);
  242. goto out;
  243. }
  244. crypto_free_hash(tfm);
  245. chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
  246. pr_debug("[server] MD5 Server Digest: %s\n", response);
  247. if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
  248. pr_debug("[server] MD5 Digests do not match!\n\n");
  249. goto out;
  250. } else
  251. pr_debug("[server] MD5 Digests match, CHAP connetication"
  252. " successful.\n\n");
  253. /*
  254. * One way authentication has succeeded, return now if mutual
  255. * authentication is not enabled.
  256. */
  257. if (!auth->authenticate_target) {
  258. kfree(challenge);
  259. kfree(challenge_binhex);
  260. return 0;
  261. }
  262. /*
  263. * Get CHAP_I.
  264. */
  265. if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
  266. pr_err("Could not find CHAP_I.\n");
  267. goto out;
  268. }
  269. if (type == HEX)
  270. id = (unsigned char)simple_strtoul((char *)&identifier[2],
  271. &endptr, 0);
  272. else
  273. id = (unsigned char)simple_strtoul(identifier, &endptr, 0);
  274. /*
  275. * RFC 1994 says Identifier is no more than octet (8 bits).
  276. */
  277. pr_debug("[server] Got CHAP_I=%d\n", id);
  278. /*
  279. * Get CHAP_C.
  280. */
  281. if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
  282. challenge, &type) < 0) {
  283. pr_err("Could not find CHAP_C.\n");
  284. goto out;
  285. }
  286. if (type != HEX) {
  287. pr_err("Could not find CHAP_C.\n");
  288. goto out;
  289. }
  290. pr_debug("[server] Got CHAP_C=%s\n", challenge);
  291. challenge_len = chap_string_to_hex(challenge_binhex, challenge,
  292. strlen(challenge));
  293. if (!challenge_len) {
  294. pr_err("Unable to convert incoming challenge\n");
  295. goto out;
  296. }
  297. /*
  298. * Generate CHAP_N and CHAP_R for mutual authentication.
  299. */
  300. tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
  301. if (IS_ERR(tfm)) {
  302. pr_err("Unable to allocate struct crypto_hash\n");
  303. goto out;
  304. }
  305. desc.tfm = tfm;
  306. desc.flags = 0;
  307. ret = crypto_hash_init(&desc);
  308. if (ret < 0) {
  309. pr_err("crypto_hash_init() failed\n");
  310. crypto_free_hash(tfm);
  311. goto out;
  312. }
  313. sg_init_one(&sg, (void *)&id, 1);
  314. ret = crypto_hash_update(&desc, &sg, 1);
  315. if (ret < 0) {
  316. pr_err("crypto_hash_update() failed for id\n");
  317. crypto_free_hash(tfm);
  318. goto out;
  319. }
  320. sg_init_one(&sg, (void *)auth->password_mutual,
  321. strlen(auth->password_mutual));
  322. ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
  323. if (ret < 0) {
  324. pr_err("crypto_hash_update() failed for"
  325. " password_mutual\n");
  326. crypto_free_hash(tfm);
  327. goto out;
  328. }
  329. /*
  330. * Convert received challenge to binary hex.
  331. */
  332. sg_init_one(&sg, (void *)challenge_binhex, challenge_len);
  333. ret = crypto_hash_update(&desc, &sg, challenge_len);
  334. if (ret < 0) {
  335. pr_err("crypto_hash_update() failed for ma challenge\n");
  336. crypto_free_hash(tfm);
  337. goto out;
  338. }
  339. ret = crypto_hash_final(&desc, digest);
  340. if (ret < 0) {
  341. pr_err("crypto_hash_final() failed for ma digest\n");
  342. crypto_free_hash(tfm);
  343. goto out;
  344. }
  345. crypto_free_hash(tfm);
  346. /*
  347. * Generate CHAP_N and CHAP_R.
  348. */
  349. *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
  350. *nr_out_len += 1;
  351. pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
  352. /*
  353. * Convert response from binary hex to ascii hext.
  354. */
  355. chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
  356. *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
  357. response);
  358. *nr_out_len += 1;
  359. pr_debug("[server] Sending CHAP_R=0x%s\n", response);
  360. auth_ret = 0;
  361. out:
  362. kfree(challenge);
  363. kfree(challenge_binhex);
  364. return auth_ret;
  365. }
  366. static int chap_got_response(
  367. struct iscsi_conn *conn,
  368. struct iscsi_node_auth *auth,
  369. char *nr_in_ptr,
  370. char *nr_out_ptr,
  371. unsigned int *nr_out_len)
  372. {
  373. struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
  374. switch (chap->digest_type) {
  375. case CHAP_DIGEST_MD5:
  376. if (chap_server_compute_md5(conn, auth, nr_in_ptr,
  377. nr_out_ptr, nr_out_len) < 0)
  378. return -1;
  379. return 0;
  380. default:
  381. pr_err("Unknown CHAP digest type %d!\n",
  382. chap->digest_type);
  383. return -1;
  384. }
  385. }
  386. u32 chap_main_loop(
  387. struct iscsi_conn *conn,
  388. struct iscsi_node_auth *auth,
  389. char *in_text,
  390. char *out_text,
  391. int *in_len,
  392. int *out_len)
  393. {
  394. struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
  395. if (!chap) {
  396. chap = chap_server_open(conn, auth, in_text, out_text, out_len);
  397. if (!chap)
  398. return 2;
  399. chap->chap_state = CHAP_STAGE_SERVER_AIC;
  400. return 0;
  401. } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
  402. convert_null_to_semi(in_text, *in_len);
  403. if (chap_got_response(conn, auth, in_text, out_text,
  404. out_len) < 0) {
  405. chap_close(conn);
  406. return 2;
  407. }
  408. if (auth->authenticate_target)
  409. chap->chap_state = CHAP_STAGE_SERVER_NR;
  410. else
  411. *out_len = 0;
  412. chap_close(conn);
  413. return 1;
  414. }
  415. return 2;
  416. }