iscsi_target_auth.c 12 KB

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