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