Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * This file houses the main functions for the iSCSI CHAP support
4 *
5 * (c) Copyright 2007-2013 Datera, Inc.
6 *
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 *
9 ******************************************************************************/
10
11#include <crypto/hash.h>
12#include <linux/kernel.h>
13#include <linux/string.h>
14#include <linux/err.h>
15#include <linux/random.h>
16#include <linux/scatterlist.h>
17#include <target/iscsi/iscsi_target_core.h>
18#include "iscsi_target_nego.h"
19#include "iscsi_target_auth.h"
20
21static char *chap_get_digest_name(const int digest_type)
22{
23 switch (digest_type) {
24 case CHAP_DIGEST_MD5:
25 return "md5";
26 case CHAP_DIGEST_SHA1:
27 return "sha1";
28 case CHAP_DIGEST_SHA256:
29 return "sha256";
30 case CHAP_DIGEST_SHA3_256:
31 return "sha3-256";
32 default:
33 return NULL;
34 }
35}
36
37static int chap_gen_challenge(
38 struct iscsit_conn *conn,
39 int caller,
40 char *c_str,
41 unsigned int *c_len)
42{
43 int ret;
44 unsigned char *challenge_asciihex;
45 struct iscsi_chap *chap = conn->auth_protocol;
46
47 challenge_asciihex = kzalloc(chap->challenge_len * 2 + 1, GFP_KERNEL);
48 if (!challenge_asciihex)
49 return -ENOMEM;
50
51 memset(chap->challenge, 0, MAX_CHAP_CHALLENGE_LEN);
52
53 ret = get_random_bytes_wait(chap->challenge, chap->challenge_len);
54 if (unlikely(ret))
55 goto out;
56
57 bin2hex(challenge_asciihex, chap->challenge,
58 chap->challenge_len);
59 /*
60 * Set CHAP_C, and copy the generated challenge into c_str.
61 */
62 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
63 *c_len += 1;
64
65 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
66 challenge_asciihex);
67
68out:
69 kfree(challenge_asciihex);
70 return ret;
71}
72
73static int chap_test_algorithm(const char *name)
74{
75 struct crypto_shash *tfm;
76
77 tfm = crypto_alloc_shash(name, 0, 0);
78 if (IS_ERR(tfm))
79 return -1;
80
81 crypto_free_shash(tfm);
82 return 0;
83}
84
85static int chap_check_algorithm(const char *a_str)
86{
87 char *tmp, *orig, *token, *digest_name;
88 long digest_type;
89 int r = CHAP_DIGEST_UNKNOWN;
90
91 tmp = kstrdup(a_str, GFP_KERNEL);
92 if (!tmp) {
93 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
94 return CHAP_DIGEST_UNKNOWN;
95 }
96 orig = tmp;
97
98 token = strsep(&tmp, "=");
99 if (!token)
100 goto out;
101
102 if (strcmp(token, "CHAP_A")) {
103 pr_err("Unable to locate CHAP_A key\n");
104 goto out;
105 }
106 while (token) {
107 token = strsep(&tmp, ",");
108 if (!token)
109 goto out;
110
111 if (kstrtol(token, 10, &digest_type))
112 continue;
113
114 digest_name = chap_get_digest_name(digest_type);
115 if (!digest_name)
116 continue;
117
118 pr_debug("Selected %s Algorithm\n", digest_name);
119 if (chap_test_algorithm(digest_name) < 0) {
120 pr_err("failed to allocate %s algo\n", digest_name);
121 } else {
122 r = digest_type;
123 goto out;
124 }
125 }
126out:
127 kfree(orig);
128 return r;
129}
130
131static void chap_close(struct iscsit_conn *conn)
132{
133 kfree(conn->auth_protocol);
134 conn->auth_protocol = NULL;
135}
136
137static struct iscsi_chap *chap_server_open(
138 struct iscsit_conn *conn,
139 struct iscsi_node_auth *auth,
140 const char *a_str,
141 char *aic_str,
142 unsigned int *aic_len)
143{
144 int digest_type;
145 struct iscsi_chap *chap;
146
147 if (!(auth->naf_flags & NAF_USERID_SET) ||
148 !(auth->naf_flags & NAF_PASSWORD_SET)) {
149 pr_err("CHAP user or password not set for"
150 " Initiator ACL\n");
151 return NULL;
152 }
153
154 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
155 if (!conn->auth_protocol)
156 return NULL;
157
158 chap = conn->auth_protocol;
159 digest_type = chap_check_algorithm(a_str);
160 switch (digest_type) {
161 case CHAP_DIGEST_MD5:
162 chap->digest_size = MD5_SIGNATURE_SIZE;
163 break;
164 case CHAP_DIGEST_SHA1:
165 chap->digest_size = SHA1_SIGNATURE_SIZE;
166 break;
167 case CHAP_DIGEST_SHA256:
168 chap->digest_size = SHA256_SIGNATURE_SIZE;
169 break;
170 case CHAP_DIGEST_SHA3_256:
171 chap->digest_size = SHA3_256_SIGNATURE_SIZE;
172 break;
173 case CHAP_DIGEST_UNKNOWN:
174 default:
175 pr_err("Unsupported CHAP_A value\n");
176 chap_close(conn);
177 return NULL;
178 }
179
180 chap->digest_name = chap_get_digest_name(digest_type);
181
182 /* Tie the challenge length to the digest size */
183 chap->challenge_len = chap->digest_size;
184
185 pr_debug("[server] Got CHAP_A=%d\n", digest_type);
186 *aic_len = sprintf(aic_str, "CHAP_A=%d", digest_type);
187 *aic_len += 1;
188 pr_debug("[server] Sending CHAP_A=%d\n", digest_type);
189
190 /*
191 * Set Identifier.
192 */
193 chap->id = conn->tpg->tpg_chap_id++;
194 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
195 *aic_len += 1;
196 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
197 /*
198 * Generate Challenge.
199 */
200 if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
201 chap_close(conn);
202 return NULL;
203 }
204
205 return chap;
206}
207
208static const char base64_lookup_table[] =
209 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
210
211static int chap_base64_decode(u8 *dst, const char *src, size_t len)
212{
213 int i, bits = 0, ac = 0;
214 const char *p;
215 u8 *cp = dst;
216
217 for (i = 0; i < len; i++) {
218 if (src[i] == '=')
219 return cp - dst;
220
221 p = strchr(base64_lookup_table, src[i]);
222 if (p == NULL || src[i] == 0)
223 return -2;
224
225 ac <<= 6;
226 ac += (p - base64_lookup_table);
227 bits += 6;
228 if (bits >= 8) {
229 *cp++ = (ac >> (bits - 8)) & 0xff;
230 ac &= ~(BIT(16) - BIT(bits - 8));
231 bits -= 8;
232 }
233 }
234 if (ac)
235 return -1;
236
237 return cp - dst;
238}
239
240static int chap_server_compute_hash(
241 struct iscsit_conn *conn,
242 struct iscsi_node_auth *auth,
243 char *nr_in_ptr,
244 char *nr_out_ptr,
245 unsigned int *nr_out_len)
246{
247 unsigned long id;
248 unsigned char id_as_uchar;
249 unsigned char type;
250 unsigned char identifier[10], *initiatorchg = NULL;
251 unsigned char *initiatorchg_binhex = NULL;
252 unsigned char *digest = NULL;
253 unsigned char *response = NULL;
254 unsigned char *client_digest = NULL;
255 unsigned char *server_digest = NULL;
256 unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
257 size_t compare_len;
258 struct iscsi_chap *chap = conn->auth_protocol;
259 struct crypto_shash *tfm = NULL;
260 struct shash_desc *desc = NULL;
261 int auth_ret = -1, ret, initiatorchg_len;
262
263 digest = kzalloc(chap->digest_size, GFP_KERNEL);
264 if (!digest) {
265 pr_err("Unable to allocate the digest buffer\n");
266 goto out;
267 }
268
269 response = kzalloc(chap->digest_size * 2 + 2, GFP_KERNEL);
270 if (!response) {
271 pr_err("Unable to allocate the response buffer\n");
272 goto out;
273 }
274
275 client_digest = kzalloc(chap->digest_size, GFP_KERNEL);
276 if (!client_digest) {
277 pr_err("Unable to allocate the client_digest buffer\n");
278 goto out;
279 }
280
281 server_digest = kzalloc(chap->digest_size, GFP_KERNEL);
282 if (!server_digest) {
283 pr_err("Unable to allocate the server_digest buffer\n");
284 goto out;
285 }
286
287 memset(identifier, 0, 10);
288 memset(chap_n, 0, MAX_CHAP_N_SIZE);
289 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
290
291 initiatorchg = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
292 if (!initiatorchg) {
293 pr_err("Unable to allocate challenge buffer\n");
294 goto out;
295 }
296
297 initiatorchg_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
298 if (!initiatorchg_binhex) {
299 pr_err("Unable to allocate initiatorchg_binhex buffer\n");
300 goto out;
301 }
302 /*
303 * Extract CHAP_N.
304 */
305 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
306 &type) < 0) {
307 pr_err("Could not find CHAP_N.\n");
308 goto out;
309 }
310 if (type == HEX) {
311 pr_err("Could not find CHAP_N.\n");
312 goto out;
313 }
314
315 /* Include the terminating NULL in the compare */
316 compare_len = strlen(auth->userid) + 1;
317 if (strncmp(chap_n, auth->userid, compare_len) != 0) {
318 pr_err("CHAP_N values do not match!\n");
319 goto out;
320 }
321 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
322 /*
323 * Extract CHAP_R.
324 */
325 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
326 &type) < 0) {
327 pr_err("Could not find CHAP_R.\n");
328 goto out;
329 }
330
331 switch (type) {
332 case HEX:
333 if (strlen(chap_r) != chap->digest_size * 2) {
334 pr_err("Malformed CHAP_R\n");
335 goto out;
336 }
337 if (hex2bin(client_digest, chap_r, chap->digest_size) < 0) {
338 pr_err("Malformed CHAP_R: invalid HEX\n");
339 goto out;
340 }
341 break;
342 case BASE64:
343 if (chap_base64_decode(client_digest, chap_r, strlen(chap_r)) !=
344 chap->digest_size) {
345 pr_err("Malformed CHAP_R: invalid BASE64\n");
346 goto out;
347 }
348 break;
349 default:
350 pr_err("Could not find CHAP_R\n");
351 goto out;
352 }
353
354 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
355
356 tfm = crypto_alloc_shash(chap->digest_name, 0, 0);
357 if (IS_ERR(tfm)) {
358 tfm = NULL;
359 pr_err("Unable to allocate struct crypto_shash\n");
360 goto out;
361 }
362
363 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
364 if (!desc) {
365 pr_err("Unable to allocate struct shash_desc\n");
366 goto out;
367 }
368
369 desc->tfm = tfm;
370
371 ret = crypto_shash_init(desc);
372 if (ret < 0) {
373 pr_err("crypto_shash_init() failed\n");
374 goto out;
375 }
376
377 ret = crypto_shash_update(desc, &chap->id, 1);
378 if (ret < 0) {
379 pr_err("crypto_shash_update() failed for id\n");
380 goto out;
381 }
382
383 ret = crypto_shash_update(desc, (char *)&auth->password,
384 strlen(auth->password));
385 if (ret < 0) {
386 pr_err("crypto_shash_update() failed for password\n");
387 goto out;
388 }
389
390 ret = crypto_shash_finup(desc, chap->challenge,
391 chap->challenge_len, server_digest);
392 if (ret < 0) {
393 pr_err("crypto_shash_finup() failed for challenge\n");
394 goto out;
395 }
396
397 bin2hex(response, server_digest, chap->digest_size);
398 pr_debug("[server] %s Server Digest: %s\n",
399 chap->digest_name, response);
400
401 if (memcmp(server_digest, client_digest, chap->digest_size) != 0) {
402 pr_debug("[server] %s Digests do not match!\n\n",
403 chap->digest_name);
404 goto out;
405 } else
406 pr_debug("[server] %s Digests match, CHAP connection"
407 " successful.\n\n", chap->digest_name);
408 /*
409 * One way authentication has succeeded, return now if mutual
410 * authentication is not enabled.
411 */
412 if (!auth->authenticate_target) {
413 auth_ret = 0;
414 goto out;
415 }
416 /*
417 * Get CHAP_I.
418 */
419 ret = extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type);
420 if (ret == -ENOENT) {
421 pr_debug("Could not find CHAP_I. Initiator uses One way authentication.\n");
422 auth_ret = 0;
423 goto out;
424 }
425 if (ret < 0) {
426 pr_err("Could not find CHAP_I.\n");
427 goto out;
428 }
429
430 if (type == HEX)
431 ret = kstrtoul(&identifier[2], 0, &id);
432 else
433 ret = kstrtoul(identifier, 0, &id);
434
435 if (ret < 0) {
436 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
437 goto out;
438 }
439 if (id > 255) {
440 pr_err("chap identifier: %lu greater than 255\n", id);
441 goto out;
442 }
443 /*
444 * RFC 1994 says Identifier is no more than octet (8 bits).
445 */
446 pr_debug("[server] Got CHAP_I=%lu\n", id);
447 /*
448 * Get CHAP_C.
449 */
450 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
451 initiatorchg, &type) < 0) {
452 pr_err("Could not find CHAP_C.\n");
453 goto out;
454 }
455
456 switch (type) {
457 case HEX:
458 initiatorchg_len = DIV_ROUND_UP(strlen(initiatorchg), 2);
459 if (!initiatorchg_len) {
460 pr_err("Unable to convert incoming challenge\n");
461 goto out;
462 }
463 if (initiatorchg_len > 1024) {
464 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
465 goto out;
466 }
467
468 if (hex2bin(initiatorchg_binhex, initiatorchg,
469 initiatorchg_len) < 0) {
470 pr_err("Malformed CHAP_C: invalid HEX\n");
471 goto out;
472 }
473 break;
474 case BASE64:
475 initiatorchg_len = chap_base64_decode(initiatorchg_binhex,
476 initiatorchg,
477 strlen(initiatorchg));
478 if (initiatorchg_len < 0) {
479 pr_err("Malformed CHAP_C: invalid BASE64\n");
480 goto out;
481 }
482 if (!initiatorchg_len) {
483 pr_err("Unable to convert incoming challenge\n");
484 goto out;
485 }
486 if (initiatorchg_len > 1024) {
487 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
488 goto out;
489 }
490 break;
491 default:
492 pr_err("Could not find CHAP_C.\n");
493 goto out;
494 }
495
496 pr_debug("[server] Got CHAP_C=%s\n", initiatorchg);
497 /*
498 * During mutual authentication, the CHAP_C generated by the
499 * initiator must not match the original CHAP_C generated by
500 * the target.
501 */
502 if (initiatorchg_len == chap->challenge_len &&
503 !memcmp(initiatorchg_binhex, chap->challenge,
504 initiatorchg_len)) {
505 pr_err("initiator CHAP_C matches target CHAP_C, failing"
506 " login attempt\n");
507 goto out;
508 }
509 /*
510 * Generate CHAP_N and CHAP_R for mutual authentication.
511 */
512 ret = crypto_shash_init(desc);
513 if (ret < 0) {
514 pr_err("crypto_shash_init() failed\n");
515 goto out;
516 }
517
518 /* To handle both endiannesses */
519 id_as_uchar = id;
520 ret = crypto_shash_update(desc, &id_as_uchar, 1);
521 if (ret < 0) {
522 pr_err("crypto_shash_update() failed for id\n");
523 goto out;
524 }
525
526 ret = crypto_shash_update(desc, auth->password_mutual,
527 strlen(auth->password_mutual));
528 if (ret < 0) {
529 pr_err("crypto_shash_update() failed for"
530 " password_mutual\n");
531 goto out;
532 }
533 /*
534 * Convert received challenge to binary hex.
535 */
536 ret = crypto_shash_finup(desc, initiatorchg_binhex, initiatorchg_len,
537 digest);
538 if (ret < 0) {
539 pr_err("crypto_shash_finup() failed for ma challenge\n");
540 goto out;
541 }
542
543 /*
544 * Generate CHAP_N and CHAP_R.
545 */
546 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
547 *nr_out_len += 1;
548 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
549 /*
550 * Convert response from binary hex to ascii hext.
551 */
552 bin2hex(response, digest, chap->digest_size);
553 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
554 response);
555 *nr_out_len += 1;
556 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
557 auth_ret = 0;
558out:
559 kfree_sensitive(desc);
560 if (tfm)
561 crypto_free_shash(tfm);
562 kfree(initiatorchg);
563 kfree(initiatorchg_binhex);
564 kfree(digest);
565 kfree(response);
566 kfree(server_digest);
567 kfree(client_digest);
568 return auth_ret;
569}
570
571u32 chap_main_loop(
572 struct iscsit_conn *conn,
573 struct iscsi_node_auth *auth,
574 char *in_text,
575 char *out_text,
576 int *in_len,
577 int *out_len)
578{
579 struct iscsi_chap *chap = conn->auth_protocol;
580
581 if (!chap) {
582 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
583 if (!chap)
584 return 2;
585 chap->chap_state = CHAP_STAGE_SERVER_AIC;
586 return 0;
587 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
588 convert_null_to_semi(in_text, *in_len);
589 if (chap_server_compute_hash(conn, auth, in_text, out_text,
590 out_len) < 0) {
591 chap_close(conn);
592 return 2;
593 }
594 if (auth->authenticate_target)
595 chap->chap_state = CHAP_STAGE_SERVER_NR;
596 else
597 *out_len = 0;
598 chap_close(conn);
599 return 1;
600 }
601
602 return 2;
603}
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*******************************************************************************
3 * This file houses the main functions for the iSCSI CHAP support
4 *
5 * (c) Copyright 2007-2013 Datera, Inc.
6 *
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
8 *
9 ******************************************************************************/
10
11#include <crypto/hash.h>
12#include <linux/kernel.h>
13#include <linux/string.h>
14#include <linux/err.h>
15#include <linux/random.h>
16#include <linux/scatterlist.h>
17#include <target/iscsi/iscsi_target_core.h>
18#include "iscsi_target_nego.h"
19#include "iscsi_target_auth.h"
20
21static int chap_gen_challenge(
22 struct iscsi_conn *conn,
23 int caller,
24 char *c_str,
25 unsigned int *c_len)
26{
27 int ret;
28 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
29 struct iscsi_chap *chap = conn->auth_protocol;
30
31 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
32
33 ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH);
34 if (unlikely(ret))
35 return ret;
36 bin2hex(challenge_asciihex, chap->challenge,
37 CHAP_CHALLENGE_LENGTH);
38 /*
39 * Set CHAP_C, and copy the generated challenge into c_str.
40 */
41 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
42 *c_len += 1;
43
44 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
45 challenge_asciihex);
46 return 0;
47}
48
49static int chap_check_algorithm(const char *a_str)
50{
51 char *tmp, *orig, *token;
52
53 tmp = kstrdup(a_str, GFP_KERNEL);
54 if (!tmp) {
55 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
56 return CHAP_DIGEST_UNKNOWN;
57 }
58 orig = tmp;
59
60 token = strsep(&tmp, "=");
61 if (!token)
62 goto out;
63
64 if (strcmp(token, "CHAP_A")) {
65 pr_err("Unable to locate CHAP_A key\n");
66 goto out;
67 }
68 while (token) {
69 token = strsep(&tmp, ",");
70 if (!token)
71 goto out;
72
73 if (!strncmp(token, "5", 1)) {
74 pr_debug("Selected MD5 Algorithm\n");
75 kfree(orig);
76 return CHAP_DIGEST_MD5;
77 }
78 }
79out:
80 kfree(orig);
81 return CHAP_DIGEST_UNKNOWN;
82}
83
84static void chap_close(struct iscsi_conn *conn)
85{
86 kfree(conn->auth_protocol);
87 conn->auth_protocol = NULL;
88}
89
90static struct iscsi_chap *chap_server_open(
91 struct iscsi_conn *conn,
92 struct iscsi_node_auth *auth,
93 const char *a_str,
94 char *aic_str,
95 unsigned int *aic_len)
96{
97 int ret;
98 struct iscsi_chap *chap;
99
100 if (!(auth->naf_flags & NAF_USERID_SET) ||
101 !(auth->naf_flags & NAF_PASSWORD_SET)) {
102 pr_err("CHAP user or password not set for"
103 " Initiator ACL\n");
104 return NULL;
105 }
106
107 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
108 if (!conn->auth_protocol)
109 return NULL;
110
111 chap = conn->auth_protocol;
112 ret = chap_check_algorithm(a_str);
113 switch (ret) {
114 case CHAP_DIGEST_MD5:
115 pr_debug("[server] Got CHAP_A=5\n");
116 /*
117 * Send back CHAP_A set to MD5.
118 */
119 *aic_len = sprintf(aic_str, "CHAP_A=5");
120 *aic_len += 1;
121 chap->digest_type = CHAP_DIGEST_MD5;
122 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
123 break;
124 case CHAP_DIGEST_UNKNOWN:
125 default:
126 pr_err("Unsupported CHAP_A value\n");
127 chap_close(conn);
128 return NULL;
129 }
130
131 /*
132 * Set Identifier.
133 */
134 chap->id = conn->tpg->tpg_chap_id++;
135 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
136 *aic_len += 1;
137 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
138 /*
139 * Generate Challenge.
140 */
141 if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
142 chap_close(conn);
143 return NULL;
144 }
145
146 return chap;
147}
148
149static 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_shash *tfm = NULL;
168 struct shash_desc *desc = NULL;
169 int auth_ret = -1, ret, challenge_len;
170
171 memset(identifier, 0, 10);
172 memset(chap_n, 0, MAX_CHAP_N_SIZE);
173 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
174 memset(digest, 0, MD5_SIGNATURE_SIZE);
175 memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
176 memset(client_digest, 0, MD5_SIGNATURE_SIZE);
177 memset(server_digest, 0, MD5_SIGNATURE_SIZE);
178
179 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
180 if (!challenge) {
181 pr_err("Unable to allocate challenge buffer\n");
182 goto out;
183 }
184
185 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
186 if (!challenge_binhex) {
187 pr_err("Unable to allocate challenge_binhex buffer\n");
188 goto out;
189 }
190 /*
191 * Extract CHAP_N.
192 */
193 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
194 &type) < 0) {
195 pr_err("Could not find CHAP_N.\n");
196 goto out;
197 }
198 if (type == HEX) {
199 pr_err("Could not find CHAP_N.\n");
200 goto out;
201 }
202
203 /* Include the terminating NULL in the compare */
204 compare_len = strlen(auth->userid) + 1;
205 if (strncmp(chap_n, auth->userid, compare_len) != 0) {
206 pr_err("CHAP_N values do not match!\n");
207 goto out;
208 }
209 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
210 /*
211 * Extract CHAP_R.
212 */
213 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
214 &type) < 0) {
215 pr_err("Could not find CHAP_R.\n");
216 goto out;
217 }
218 if (type != HEX) {
219 pr_err("Could not find CHAP_R.\n");
220 goto out;
221 }
222 if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
223 pr_err("Malformed CHAP_R\n");
224 goto out;
225 }
226 if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
227 pr_err("Malformed CHAP_R\n");
228 goto out;
229 }
230
231 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
232
233 tfm = crypto_alloc_shash("md5", 0, 0);
234 if (IS_ERR(tfm)) {
235 tfm = NULL;
236 pr_err("Unable to allocate struct crypto_shash\n");
237 goto out;
238 }
239
240 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
241 if (!desc) {
242 pr_err("Unable to allocate struct shash_desc\n");
243 goto out;
244 }
245
246 desc->tfm = tfm;
247
248 ret = crypto_shash_init(desc);
249 if (ret < 0) {
250 pr_err("crypto_shash_init() failed\n");
251 goto out;
252 }
253
254 ret = crypto_shash_update(desc, &chap->id, 1);
255 if (ret < 0) {
256 pr_err("crypto_shash_update() failed for id\n");
257 goto out;
258 }
259
260 ret = crypto_shash_update(desc, (char *)&auth->password,
261 strlen(auth->password));
262 if (ret < 0) {
263 pr_err("crypto_shash_update() failed for password\n");
264 goto out;
265 }
266
267 ret = crypto_shash_finup(desc, chap->challenge,
268 CHAP_CHALLENGE_LENGTH, server_digest);
269 if (ret < 0) {
270 pr_err("crypto_shash_finup() failed for challenge\n");
271 goto out;
272 }
273
274 bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
275 pr_debug("[server] MD5 Server Digest: %s\n", response);
276
277 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
278 pr_debug("[server] MD5 Digests do not match!\n\n");
279 goto out;
280 } else
281 pr_debug("[server] MD5 Digests match, CHAP connection"
282 " successful.\n\n");
283 /*
284 * One way authentication has succeeded, return now if mutual
285 * authentication is not enabled.
286 */
287 if (!auth->authenticate_target) {
288 auth_ret = 0;
289 goto out;
290 }
291 /*
292 * Get CHAP_I.
293 */
294 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
295 pr_err("Could not find CHAP_I.\n");
296 goto out;
297 }
298
299 if (type == HEX)
300 ret = kstrtoul(&identifier[2], 0, &id);
301 else
302 ret = kstrtoul(identifier, 0, &id);
303
304 if (ret < 0) {
305 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
306 goto out;
307 }
308 if (id > 255) {
309 pr_err("chap identifier: %lu greater than 255\n", id);
310 goto out;
311 }
312 /*
313 * RFC 1994 says Identifier is no more than octet (8 bits).
314 */
315 pr_debug("[server] Got CHAP_I=%lu\n", id);
316 /*
317 * Get CHAP_C.
318 */
319 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
320 challenge, &type) < 0) {
321 pr_err("Could not find CHAP_C.\n");
322 goto out;
323 }
324
325 if (type != HEX) {
326 pr_err("Could not find CHAP_C.\n");
327 goto out;
328 }
329 challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
330 if (!challenge_len) {
331 pr_err("Unable to convert incoming challenge\n");
332 goto out;
333 }
334 if (challenge_len > 1024) {
335 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
336 goto out;
337 }
338 if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
339 pr_err("Malformed CHAP_C\n");
340 goto out;
341 }
342 pr_debug("[server] Got CHAP_C=%s\n", challenge);
343 /*
344 * During mutual authentication, the CHAP_C generated by the
345 * initiator must not match the original CHAP_C generated by
346 * the target.
347 */
348 if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
349 pr_err("initiator CHAP_C matches target CHAP_C, failing"
350 " login attempt\n");
351 goto out;
352 }
353 /*
354 * Generate CHAP_N and CHAP_R for mutual authentication.
355 */
356 ret = crypto_shash_init(desc);
357 if (ret < 0) {
358 pr_err("crypto_shash_init() failed\n");
359 goto out;
360 }
361
362 /* To handle both endiannesses */
363 id_as_uchar = id;
364 ret = crypto_shash_update(desc, &id_as_uchar, 1);
365 if (ret < 0) {
366 pr_err("crypto_shash_update() failed for id\n");
367 goto out;
368 }
369
370 ret = crypto_shash_update(desc, auth->password_mutual,
371 strlen(auth->password_mutual));
372 if (ret < 0) {
373 pr_err("crypto_shash_update() failed for"
374 " password_mutual\n");
375 goto out;
376 }
377 /*
378 * Convert received challenge to binary hex.
379 */
380 ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
381 digest);
382 if (ret < 0) {
383 pr_err("crypto_shash_finup() failed for ma challenge\n");
384 goto out;
385 }
386
387 /*
388 * Generate CHAP_N and CHAP_R.
389 */
390 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
391 *nr_out_len += 1;
392 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
393 /*
394 * Convert response from binary hex to ascii hext.
395 */
396 bin2hex(response, digest, MD5_SIGNATURE_SIZE);
397 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
398 response);
399 *nr_out_len += 1;
400 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
401 auth_ret = 0;
402out:
403 kzfree(desc);
404 if (tfm)
405 crypto_free_shash(tfm);
406 kfree(challenge);
407 kfree(challenge_binhex);
408 return auth_ret;
409}
410
411static int chap_got_response(
412 struct iscsi_conn *conn,
413 struct iscsi_node_auth *auth,
414 char *nr_in_ptr,
415 char *nr_out_ptr,
416 unsigned int *nr_out_len)
417{
418 struct iscsi_chap *chap = conn->auth_protocol;
419
420 switch (chap->digest_type) {
421 case CHAP_DIGEST_MD5:
422 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
423 nr_out_ptr, nr_out_len) < 0)
424 return -1;
425 return 0;
426 default:
427 pr_err("Unknown CHAP digest type %d!\n",
428 chap->digest_type);
429 return -1;
430 }
431}
432
433u32 chap_main_loop(
434 struct iscsi_conn *conn,
435 struct iscsi_node_auth *auth,
436 char *in_text,
437 char *out_text,
438 int *in_len,
439 int *out_len)
440{
441 struct iscsi_chap *chap = conn->auth_protocol;
442
443 if (!chap) {
444 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
445 if (!chap)
446 return 2;
447 chap->chap_state = CHAP_STAGE_SERVER_AIC;
448 return 0;
449 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
450 convert_null_to_semi(in_text, *in_len);
451 if (chap_got_response(conn, auth, in_text, out_text,
452 out_len) < 0) {
453 chap_close(conn);
454 return 2;
455 }
456 if (auth->authenticate_target)
457 chap->chap_state = CHAP_STAGE_SERVER_NR;
458 else
459 *out_len = 0;
460 chap_close(conn);
461 return 1;
462 }
463
464 return 2;
465}