iscsi_target_auth.c 11 KB

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