1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*******************************************************************************
3 * This file houses the main functions for the iSCSI CHAP support
5 * (c) Copyright 2007-2013 Datera, Inc.
9 ******************************************************************************/
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"
21 static char *chap_get_digest_name(const int digest_type)
23 switch (digest_type) {
26 case CHAP_DIGEST_SHA1:
28 case CHAP_DIGEST_SHA256:
30 case CHAP_DIGEST_SHA3_256:
37 static int chap_gen_challenge(
38 struct iscsit_conn *conn,
44 unsigned char *challenge_asciihex;
45 struct iscsi_chap *chap = conn->auth_protocol;
47 challenge_asciihex = kzalloc(chap->challenge_len * 2 + 1, GFP_KERNEL);
48 if (!challenge_asciihex)
51 memset(chap->challenge, 0, MAX_CHAP_CHALLENGE_LEN);
53 ret = get_random_bytes_wait(chap->challenge, chap->challenge_len);
57 bin2hex(challenge_asciihex, chap->challenge,
60 * Set CHAP_C, and copy the generated challenge into c_str.
62 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
65 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
69 kfree(challenge_asciihex);
73 static int chap_test_algorithm(const char *name)
75 struct crypto_shash *tfm;
77 tfm = crypto_alloc_shash(name, 0, 0);
81 crypto_free_shash(tfm);
85 static int chap_check_algorithm(const char *a_str)
87 char *tmp, *orig, *token, *digest_name;
89 int r = CHAP_DIGEST_UNKNOWN;
91 tmp = kstrdup(a_str, GFP_KERNEL);
93 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
94 return CHAP_DIGEST_UNKNOWN;
98 token = strsep(&tmp, "=");
102 if (strcmp(token, "CHAP_A")) {
103 pr_err("Unable to locate CHAP_A key\n");
107 token = strsep(&tmp, ",");
111 if (kstrtol(token, 10, &digest_type))
114 digest_name = chap_get_digest_name(digest_type);
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);
131 static void chap_close(struct iscsit_conn *conn)
133 kfree(conn->auth_protocol);
134 conn->auth_protocol = NULL;
137 static struct iscsi_chap *chap_server_open(
138 struct iscsit_conn *conn,
139 struct iscsi_node_auth *auth,
142 unsigned int *aic_len)
145 struct iscsi_chap *chap;
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"
154 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
155 if (!conn->auth_protocol)
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;
164 case CHAP_DIGEST_SHA1:
165 chap->digest_size = SHA1_SIGNATURE_SIZE;
167 case CHAP_DIGEST_SHA256:
168 chap->digest_size = SHA256_SIGNATURE_SIZE;
170 case CHAP_DIGEST_SHA3_256:
171 chap->digest_size = SHA3_256_SIGNATURE_SIZE;
173 case CHAP_DIGEST_UNKNOWN:
175 pr_err("Unsupported CHAP_A value\n");
180 chap->digest_name = chap_get_digest_name(digest_type);
182 /* Tie the challenge length to the digest size */
183 chap->challenge_len = chap->digest_size;
185 pr_debug("[server] Got CHAP_A=%d\n", digest_type);
186 *aic_len = sprintf(aic_str, "CHAP_A=%d", digest_type);
188 pr_debug("[server] Sending CHAP_A=%d\n", digest_type);
193 chap->id = conn->tpg->tpg_chap_id++;
194 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
196 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
198 * Generate Challenge.
200 if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
208 static const char base64_lookup_table[] =
209 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
211 static int chap_base64_decode(u8 *dst, const char *src, size_t len)
213 int i, bits = 0, ac = 0;
217 for (i = 0; i < len; i++) {
221 p = strchr(base64_lookup_table, src[i]);
222 if (p == NULL || src[i] == 0)
226 ac += (p - base64_lookup_table);
229 *cp++ = (ac >> (bits - 8)) & 0xff;
230 ac &= ~(BIT(16) - BIT(bits - 8));
240 static int chap_server_compute_hash(
241 struct iscsit_conn *conn,
242 struct iscsi_node_auth *auth,
245 unsigned int *nr_out_len)
248 unsigned char id_as_uchar;
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];
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;
263 digest = kzalloc(chap->digest_size, GFP_KERNEL);
265 pr_err("Unable to allocate the digest buffer\n");
269 response = kzalloc(chap->digest_size * 2 + 2, GFP_KERNEL);
271 pr_err("Unable to allocate the response buffer\n");
275 client_digest = kzalloc(chap->digest_size, GFP_KERNEL);
276 if (!client_digest) {
277 pr_err("Unable to allocate the client_digest buffer\n");
281 server_digest = kzalloc(chap->digest_size, GFP_KERNEL);
282 if (!server_digest) {
283 pr_err("Unable to allocate the server_digest buffer\n");
287 memset(identifier, 0, 10);
288 memset(chap_n, 0, MAX_CHAP_N_SIZE);
289 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
291 initiatorchg = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
293 pr_err("Unable to allocate challenge buffer\n");
297 initiatorchg_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
298 if (!initiatorchg_binhex) {
299 pr_err("Unable to allocate initiatorchg_binhex buffer\n");
305 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
307 pr_err("Could not find CHAP_N.\n");
311 pr_err("Could not find CHAP_N.\n");
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");
321 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
325 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
327 pr_err("Could not find CHAP_R.\n");
333 if (strlen(chap_r) != chap->digest_size * 2) {
334 pr_err("Malformed CHAP_R\n");
337 if (hex2bin(client_digest, chap_r, chap->digest_size) < 0) {
338 pr_err("Malformed CHAP_R: invalid HEX\n");
343 if (chap_base64_decode(client_digest, chap_r, strlen(chap_r)) !=
345 pr_err("Malformed CHAP_R: invalid BASE64\n");
350 pr_err("Could not find CHAP_R\n");
354 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
356 tfm = crypto_alloc_shash(chap->digest_name, 0, 0);
359 pr_err("Unable to allocate struct crypto_shash\n");
363 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
365 pr_err("Unable to allocate struct shash_desc\n");
371 ret = crypto_shash_init(desc);
373 pr_err("crypto_shash_init() failed\n");
377 ret = crypto_shash_update(desc, &chap->id, 1);
379 pr_err("crypto_shash_update() failed for id\n");
383 ret = crypto_shash_update(desc, (char *)&auth->password,
384 strlen(auth->password));
386 pr_err("crypto_shash_update() failed for password\n");
390 ret = crypto_shash_finup(desc, chap->challenge,
391 chap->challenge_len, server_digest);
393 pr_err("crypto_shash_finup() failed for challenge\n");
397 bin2hex(response, server_digest, chap->digest_size);
398 pr_debug("[server] %s Server Digest: %s\n",
399 chap->digest_name, response);
401 if (memcmp(server_digest, client_digest, chap->digest_size) != 0) {
402 pr_debug("[server] %s Digests do not match!\n\n",
406 pr_debug("[server] %s Digests match, CHAP connection"
407 " successful.\n\n", chap->digest_name);
409 * One way authentication has succeeded, return now if mutual
410 * authentication is not enabled.
412 if (!auth->authenticate_target) {
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");
426 pr_err("Could not find CHAP_I.\n");
431 ret = kstrtoul(&identifier[2], 0, &id);
433 ret = kstrtoul(identifier, 0, &id);
436 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
440 pr_err("chap identifier: %lu greater than 255\n", id);
444 * RFC 1994 says Identifier is no more than octet (8 bits).
446 pr_debug("[server] Got CHAP_I=%lu\n", id);
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");
458 initiatorchg_len = DIV_ROUND_UP(strlen(initiatorchg), 2);
459 if (!initiatorchg_len) {
460 pr_err("Unable to convert incoming challenge\n");
463 if (initiatorchg_len > 1024) {
464 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
468 if (hex2bin(initiatorchg_binhex, initiatorchg,
469 initiatorchg_len) < 0) {
470 pr_err("Malformed CHAP_C: invalid HEX\n");
475 initiatorchg_len = chap_base64_decode(initiatorchg_binhex,
477 strlen(initiatorchg));
478 if (initiatorchg_len < 0) {
479 pr_err("Malformed CHAP_C: invalid BASE64\n");
482 if (!initiatorchg_len) {
483 pr_err("Unable to convert incoming challenge\n");
486 if (initiatorchg_len > 1024) {
487 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
492 pr_err("Could not find CHAP_C.\n");
496 pr_debug("[server] Got CHAP_C=%s\n", initiatorchg);
498 * During mutual authentication, the CHAP_C generated by the
499 * initiator must not match the original CHAP_C generated by
502 if (initiatorchg_len == chap->challenge_len &&
503 !memcmp(initiatorchg_binhex, chap->challenge,
505 pr_err("initiator CHAP_C matches target CHAP_C, failing"
510 * Generate CHAP_N and CHAP_R for mutual authentication.
512 ret = crypto_shash_init(desc);
514 pr_err("crypto_shash_init() failed\n");
518 /* To handle both endiannesses */
520 ret = crypto_shash_update(desc, &id_as_uchar, 1);
522 pr_err("crypto_shash_update() failed for id\n");
526 ret = crypto_shash_update(desc, auth->password_mutual,
527 strlen(auth->password_mutual));
529 pr_err("crypto_shash_update() failed for"
530 " password_mutual\n");
534 * Convert received challenge to binary hex.
536 ret = crypto_shash_finup(desc, initiatorchg_binhex, initiatorchg_len,
539 pr_err("crypto_shash_finup() failed for ma challenge\n");
544 * Generate CHAP_N and CHAP_R.
546 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
548 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
550 * Convert response from binary hex to ascii hext.
552 bin2hex(response, digest, chap->digest_size);
553 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
556 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
559 kfree_sensitive(desc);
561 crypto_free_shash(tfm);
563 kfree(initiatorchg_binhex);
566 kfree(server_digest);
567 kfree(client_digest);
572 struct iscsit_conn *conn,
573 struct iscsi_node_auth *auth,
579 struct iscsi_chap *chap = conn->auth_protocol;
582 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
585 chap->chap_state = CHAP_STAGE_SERVER_AIC;
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,
594 if (auth->authenticate_target)
595 chap->chap_state = CHAP_STAGE_SERVER_NR;