iscsi_target_auth.c 12 KB

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