• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/crypto.h>
22 #include <linux/err.h>
23 #include <linux/scatterlist.h>
24 
25 #include <target/iscsi/iscsi_target_core.h>
26 #include "iscsi_target_nego.h"
27 #include "iscsi_target_auth.h"
28 
chap_gen_challenge(struct iscsi_conn * conn,int caller,char * c_str,unsigned int * c_len)29 static void chap_gen_challenge(
30 	struct iscsi_conn *conn,
31 	int caller,
32 	char *c_str,
33 	unsigned int *c_len)
34 {
35 	unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
36 	struct iscsi_chap *chap = conn->auth_protocol;
37 
38 	memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
39 
40 	get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH);
41 	bin2hex(challenge_asciihex, chap->challenge,
42 				CHAP_CHALLENGE_LENGTH);
43 	/*
44 	 * Set CHAP_C, and copy the generated challenge into c_str.
45 	 */
46 	*c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
47 	*c_len += 1;
48 
49 	pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
50 			challenge_asciihex);
51 }
52 
chap_check_algorithm(const char * a_str)53 static int chap_check_algorithm(const char *a_str)
54 {
55 	char *tmp, *orig, *token;
56 
57 	tmp = kstrdup(a_str, GFP_KERNEL);
58 	if (!tmp) {
59 		pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
60 		return CHAP_DIGEST_UNKNOWN;
61 	}
62 	orig = tmp;
63 
64 	token = strsep(&tmp, "=");
65 	if (!token)
66 		goto out;
67 
68 	if (strcmp(token, "CHAP_A")) {
69 		pr_err("Unable to locate CHAP_A key\n");
70 		goto out;
71 	}
72 	while (token) {
73 		token = strsep(&tmp, ",");
74 		if (!token)
75 			goto out;
76 
77 		if (!strcmp(token, "5")) {
78 			pr_debug("Selected MD5 Algorithm\n");
79 			kfree(orig);
80 			return CHAP_DIGEST_MD5;
81 		}
82 	}
83 out:
84 	kfree(orig);
85 	return CHAP_DIGEST_UNKNOWN;
86 }
87 
chap_server_open(struct iscsi_conn * conn,struct iscsi_node_auth * auth,const char * a_str,char * aic_str,unsigned int * aic_len)88 static struct iscsi_chap *chap_server_open(
89 	struct iscsi_conn *conn,
90 	struct iscsi_node_auth *auth,
91 	const char *a_str,
92 	char *aic_str,
93 	unsigned int *aic_len)
94 {
95 	int ret;
96 	struct iscsi_chap *chap;
97 
98 	if (!(auth->naf_flags & NAF_USERID_SET) ||
99 	    !(auth->naf_flags & NAF_PASSWORD_SET)) {
100 		pr_err("CHAP user or password not set for"
101 				" Initiator ACL\n");
102 		return NULL;
103 	}
104 
105 	conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
106 	if (!conn->auth_protocol)
107 		return NULL;
108 
109 	chap = conn->auth_protocol;
110 	ret = chap_check_algorithm(a_str);
111 	switch (ret) {
112 	case CHAP_DIGEST_MD5:
113 		pr_debug("[server] Got CHAP_A=5\n");
114 		/*
115 		 * Send back CHAP_A set to MD5.
116 		*/
117 		*aic_len = sprintf(aic_str, "CHAP_A=5");
118 		*aic_len += 1;
119 		chap->digest_type = CHAP_DIGEST_MD5;
120 		pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
121 		break;
122 	case CHAP_DIGEST_UNKNOWN:
123 	default:
124 		pr_err("Unsupported CHAP_A value\n");
125 		return NULL;
126 	}
127 
128 	/*
129 	 * Set Identifier.
130 	 */
131 	chap->id = conn->tpg->tpg_chap_id++;
132 	*aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
133 	*aic_len += 1;
134 	pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
135 	/*
136 	 * Generate Challenge.
137 	 */
138 	chap_gen_challenge(conn, 1, aic_str, aic_len);
139 
140 	return chap;
141 }
142 
chap_close(struct iscsi_conn * conn)143 static void chap_close(struct iscsi_conn *conn)
144 {
145 	kfree(conn->auth_protocol);
146 	conn->auth_protocol = NULL;
147 }
148 
chap_server_compute_md5(struct iscsi_conn * conn,struct iscsi_node_auth * auth,char * nr_in_ptr,char * nr_out_ptr,unsigned int * nr_out_len)149 static int chap_server_compute_md5(
150 	struct iscsi_conn *conn,
151 	struct iscsi_node_auth *auth,
152 	char *nr_in_ptr,
153 	char *nr_out_ptr,
154 	unsigned int *nr_out_len)
155 {
156 	unsigned long id;
157 	unsigned char id_as_uchar;
158 	unsigned char digest[MD5_SIGNATURE_SIZE];
159 	unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
160 	unsigned char identifier[10], *challenge = NULL;
161 	unsigned char *challenge_binhex = NULL;
162 	unsigned char client_digest[MD5_SIGNATURE_SIZE];
163 	unsigned char server_digest[MD5_SIGNATURE_SIZE];
164 	unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
165 	size_t compare_len;
166 	struct iscsi_chap *chap = conn->auth_protocol;
167 	struct crypto_hash *tfm;
168 	struct hash_desc desc;
169 	struct scatterlist sg;
170 	int auth_ret = -1, ret, challenge_len;
171 
172 	memset(identifier, 0, 10);
173 	memset(chap_n, 0, MAX_CHAP_N_SIZE);
174 	memset(chap_r, 0, MAX_RESPONSE_LENGTH);
175 	memset(digest, 0, MD5_SIGNATURE_SIZE);
176 	memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
177 	memset(client_digest, 0, MD5_SIGNATURE_SIZE);
178 	memset(server_digest, 0, MD5_SIGNATURE_SIZE);
179 
180 	challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
181 	if (!challenge) {
182 		pr_err("Unable to allocate challenge buffer\n");
183 		goto out;
184 	}
185 
186 	challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
187 	if (!challenge_binhex) {
188 		pr_err("Unable to allocate challenge_binhex buffer\n");
189 		goto out;
190 	}
191 	/*
192 	 * Extract CHAP_N.
193 	 */
194 	if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
195 				&type) < 0) {
196 		pr_err("Could not find CHAP_N.\n");
197 		goto out;
198 	}
199 	if (type == HEX) {
200 		pr_err("Could not find CHAP_N.\n");
201 		goto out;
202 	}
203 
204 	/* Include the terminating NULL in the compare */
205 	compare_len = strlen(auth->userid) + 1;
206 	if (strncmp(chap_n, auth->userid, compare_len) != 0) {
207 		pr_err("CHAP_N values do not match!\n");
208 		goto out;
209 	}
210 	pr_debug("[server] Got CHAP_N=%s\n", chap_n);
211 	/*
212 	 * Extract CHAP_R.
213 	 */
214 	if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
215 				&type) < 0) {
216 		pr_err("Could not find CHAP_R.\n");
217 		goto out;
218 	}
219 	if (type != HEX) {
220 		pr_err("Could not find CHAP_R.\n");
221 		goto out;
222 	}
223 	if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
224 		pr_err("Malformed CHAP_R\n");
225 		goto out;
226 	}
227 	if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
228 		pr_err("Malformed CHAP_R\n");
229 		goto out;
230 	}
231 
232 	pr_debug("[server] Got CHAP_R=%s\n", chap_r);
233 
234 	tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
235 	if (IS_ERR(tfm)) {
236 		pr_err("Unable to allocate struct crypto_hash\n");
237 		goto out;
238 	}
239 	desc.tfm = tfm;
240 	desc.flags = 0;
241 
242 	ret = crypto_hash_init(&desc);
243 	if (ret < 0) {
244 		pr_err("crypto_hash_init() failed\n");
245 		crypto_free_hash(tfm);
246 		goto out;
247 	}
248 
249 	sg_init_one(&sg, &chap->id, 1);
250 	ret = crypto_hash_update(&desc, &sg, 1);
251 	if (ret < 0) {
252 		pr_err("crypto_hash_update() failed for id\n");
253 		crypto_free_hash(tfm);
254 		goto out;
255 	}
256 
257 	sg_init_one(&sg, &auth->password, strlen(auth->password));
258 	ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
259 	if (ret < 0) {
260 		pr_err("crypto_hash_update() failed for password\n");
261 		crypto_free_hash(tfm);
262 		goto out;
263 	}
264 
265 	sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH);
266 	ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
267 	if (ret < 0) {
268 		pr_err("crypto_hash_update() failed for challenge\n");
269 		crypto_free_hash(tfm);
270 		goto out;
271 	}
272 
273 	ret = crypto_hash_final(&desc, server_digest);
274 	if (ret < 0) {
275 		pr_err("crypto_hash_final() failed for server digest\n");
276 		crypto_free_hash(tfm);
277 		goto out;
278 	}
279 	crypto_free_hash(tfm);
280 
281 	bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
282 	pr_debug("[server] MD5 Server Digest: %s\n", response);
283 
284 	if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
285 		pr_debug("[server] MD5 Digests do not match!\n\n");
286 		goto out;
287 	} else
288 		pr_debug("[server] MD5 Digests match, CHAP connetication"
289 				" successful.\n\n");
290 	/*
291 	 * One way authentication has succeeded, return now if mutual
292 	 * authentication is not enabled.
293 	 */
294 	if (!auth->authenticate_target) {
295 		kfree(challenge);
296 		kfree(challenge_binhex);
297 		return 0;
298 	}
299 	/*
300 	 * Get CHAP_I.
301 	 */
302 	if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
303 		pr_err("Could not find CHAP_I.\n");
304 		goto out;
305 	}
306 
307 	if (type == HEX)
308 		ret = kstrtoul(&identifier[2], 0, &id);
309 	else
310 		ret = kstrtoul(identifier, 0, &id);
311 
312 	if (ret < 0) {
313 		pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
314 		goto out;
315 	}
316 	if (id > 255) {
317 		pr_err("chap identifier: %lu greater than 255\n", id);
318 		goto out;
319 	}
320 	/*
321 	 * RFC 1994 says Identifier is no more than octet (8 bits).
322 	 */
323 	pr_debug("[server] Got CHAP_I=%lu\n", id);
324 	/*
325 	 * Get CHAP_C.
326 	 */
327 	if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
328 			challenge, &type) < 0) {
329 		pr_err("Could not find CHAP_C.\n");
330 		goto out;
331 	}
332 
333 	if (type != HEX) {
334 		pr_err("Could not find CHAP_C.\n");
335 		goto out;
336 	}
337 	challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
338 	if (!challenge_len) {
339 		pr_err("Unable to convert incoming challenge\n");
340 		goto out;
341 	}
342 	if (challenge_len > 1024) {
343 		pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
344 		goto out;
345 	}
346 	if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
347 		pr_err("Malformed CHAP_C\n");
348 		goto out;
349 	}
350 	pr_debug("[server] Got CHAP_C=%s\n", challenge);
351 	/*
352 	 * During mutual authentication, the CHAP_C generated by the
353 	 * initiator must not match the original CHAP_C generated by
354 	 * the target.
355 	 */
356 	if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
357 		pr_err("initiator CHAP_C matches target CHAP_C, failing"
358 		       " login attempt\n");
359 		goto out;
360 	}
361 	/*
362 	 * Generate CHAP_N and CHAP_R for mutual authentication.
363 	 */
364 	tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
365 	if (IS_ERR(tfm)) {
366 		pr_err("Unable to allocate struct crypto_hash\n");
367 		goto out;
368 	}
369 	desc.tfm = tfm;
370 	desc.flags = 0;
371 
372 	ret = crypto_hash_init(&desc);
373 	if (ret < 0) {
374 		pr_err("crypto_hash_init() failed\n");
375 		crypto_free_hash(tfm);
376 		goto out;
377 	}
378 
379 	/* To handle both endiannesses */
380 	id_as_uchar = id;
381 	sg_init_one(&sg, &id_as_uchar, 1);
382 	ret = crypto_hash_update(&desc, &sg, 1);
383 	if (ret < 0) {
384 		pr_err("crypto_hash_update() failed for id\n");
385 		crypto_free_hash(tfm);
386 		goto out;
387 	}
388 
389 	sg_init_one(&sg, auth->password_mutual,
390 				strlen(auth->password_mutual));
391 	ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
392 	if (ret < 0) {
393 		pr_err("crypto_hash_update() failed for"
394 				" password_mutual\n");
395 		crypto_free_hash(tfm);
396 		goto out;
397 	}
398 	/*
399 	 * Convert received challenge to binary hex.
400 	 */
401 	sg_init_one(&sg, challenge_binhex, challenge_len);
402 	ret = crypto_hash_update(&desc, &sg, challenge_len);
403 	if (ret < 0) {
404 		pr_err("crypto_hash_update() failed for ma challenge\n");
405 		crypto_free_hash(tfm);
406 		goto out;
407 	}
408 
409 	ret = crypto_hash_final(&desc, digest);
410 	if (ret < 0) {
411 		pr_err("crypto_hash_final() failed for ma digest\n");
412 		crypto_free_hash(tfm);
413 		goto out;
414 	}
415 	crypto_free_hash(tfm);
416 	/*
417 	 * Generate CHAP_N and CHAP_R.
418 	 */
419 	*nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
420 	*nr_out_len += 1;
421 	pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
422 	/*
423 	 * Convert response from binary hex to ascii hext.
424 	 */
425 	bin2hex(response, digest, MD5_SIGNATURE_SIZE);
426 	*nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
427 			response);
428 	*nr_out_len += 1;
429 	pr_debug("[server] Sending CHAP_R=0x%s\n", response);
430 	auth_ret = 0;
431 out:
432 	kfree(challenge);
433 	kfree(challenge_binhex);
434 	return auth_ret;
435 }
436 
chap_got_response(struct iscsi_conn * conn,struct iscsi_node_auth * auth,char * nr_in_ptr,char * nr_out_ptr,unsigned int * nr_out_len)437 static int chap_got_response(
438 	struct iscsi_conn *conn,
439 	struct iscsi_node_auth *auth,
440 	char *nr_in_ptr,
441 	char *nr_out_ptr,
442 	unsigned int *nr_out_len)
443 {
444 	struct iscsi_chap *chap = conn->auth_protocol;
445 
446 	switch (chap->digest_type) {
447 	case CHAP_DIGEST_MD5:
448 		if (chap_server_compute_md5(conn, auth, nr_in_ptr,
449 				nr_out_ptr, nr_out_len) < 0)
450 			return -1;
451 		return 0;
452 	default:
453 		pr_err("Unknown CHAP digest type %d!\n",
454 				chap->digest_type);
455 		return -1;
456 	}
457 }
458 
chap_main_loop(struct iscsi_conn * conn,struct iscsi_node_auth * auth,char * in_text,char * out_text,int * in_len,int * out_len)459 u32 chap_main_loop(
460 	struct iscsi_conn *conn,
461 	struct iscsi_node_auth *auth,
462 	char *in_text,
463 	char *out_text,
464 	int *in_len,
465 	int *out_len)
466 {
467 	struct iscsi_chap *chap = conn->auth_protocol;
468 
469 	if (!chap) {
470 		chap = chap_server_open(conn, auth, in_text, out_text, out_len);
471 		if (!chap)
472 			return 2;
473 		chap->chap_state = CHAP_STAGE_SERVER_AIC;
474 		return 0;
475 	} else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
476 		convert_null_to_semi(in_text, *in_len);
477 		if (chap_got_response(conn, auth, in_text, out_text,
478 				out_len) < 0) {
479 			chap_close(conn);
480 			return 2;
481 		}
482 		if (auth->authenticate_target)
483 			chap->chap_state = CHAP_STAGE_SERVER_NR;
484 		else
485 			*out_len = 0;
486 		chap_close(conn);
487 		return 1;
488 	}
489 
490 	return 2;
491 }
492