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.
7 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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 int chap_gen_challenge(
22 struct iscsi_conn *conn,
28 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
29 struct iscsi_chap *chap = conn->auth_protocol;
31 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
33 ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH);
36 bin2hex(challenge_asciihex, chap->challenge,
37 CHAP_CHALLENGE_LENGTH);
39 * Set CHAP_C, and copy the generated challenge into c_str.
41 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
44 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
49 static int chap_check_algorithm(const char *a_str)
51 char *tmp, *orig, *token;
53 tmp = kstrdup(a_str, GFP_KERNEL);
55 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
56 return CHAP_DIGEST_UNKNOWN;
60 token = strsep(&tmp, "=");
64 if (strcmp(token, "CHAP_A")) {
65 pr_err("Unable to locate CHAP_A key\n");
69 token = strsep(&tmp, ",");
73 if (!strcmp(token, "5")) {
74 pr_debug("Selected MD5 Algorithm\n");
76 return CHAP_DIGEST_MD5;
81 return CHAP_DIGEST_UNKNOWN;
84 static void chap_close(struct iscsi_conn *conn)
86 kfree(conn->auth_protocol);
87 conn->auth_protocol = NULL;
90 static struct iscsi_chap *chap_server_open(
91 struct iscsi_conn *conn,
92 struct iscsi_node_auth *auth,
95 unsigned int *aic_len)
98 struct iscsi_chap *chap;
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"
107 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
108 if (!conn->auth_protocol)
111 chap = conn->auth_protocol;
112 ret = chap_check_algorithm(a_str);
114 case CHAP_DIGEST_MD5:
115 pr_debug("[server] Got CHAP_A=5\n");
117 * Send back CHAP_A set to MD5.
119 *aic_len = sprintf(aic_str, "CHAP_A=5");
121 chap->digest_type = CHAP_DIGEST_MD5;
122 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
124 case CHAP_DIGEST_UNKNOWN:
126 pr_err("Unsupported CHAP_A value\n");
134 chap->id = conn->tpg->tpg_chap_id++;
135 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
137 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
139 * Generate Challenge.
141 if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) {
149 static int chap_server_compute_md5(
150 struct iscsi_conn *conn,
151 struct iscsi_node_auth *auth,
154 unsigned int *nr_out_len)
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];
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;
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);
179 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
181 pr_err("Unable to allocate challenge buffer\n");
185 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
186 if (!challenge_binhex) {
187 pr_err("Unable to allocate challenge_binhex buffer\n");
193 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
195 pr_err("Could not find CHAP_N.\n");
199 pr_err("Could not find CHAP_N.\n");
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");
209 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
213 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
215 pr_err("Could not find CHAP_R.\n");
219 pr_err("Could not find CHAP_R.\n");
222 if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
223 pr_err("Malformed CHAP_R\n");
226 if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
227 pr_err("Malformed CHAP_R\n");
231 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
233 tfm = crypto_alloc_shash("md5", 0, 0);
236 pr_err("Unable to allocate struct crypto_shash\n");
240 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
242 pr_err("Unable to allocate struct shash_desc\n");
248 ret = crypto_shash_init(desc);
250 pr_err("crypto_shash_init() failed\n");
254 ret = crypto_shash_update(desc, &chap->id, 1);
256 pr_err("crypto_shash_update() failed for id\n");
260 ret = crypto_shash_update(desc, (char *)&auth->password,
261 strlen(auth->password));
263 pr_err("crypto_shash_update() failed for password\n");
267 ret = crypto_shash_finup(desc, chap->challenge,
268 CHAP_CHALLENGE_LENGTH, server_digest);
270 pr_err("crypto_shash_finup() failed for challenge\n");
274 bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
275 pr_debug("[server] MD5 Server Digest: %s\n", response);
277 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
278 pr_debug("[server] MD5 Digests do not match!\n\n");
281 pr_debug("[server] MD5 Digests match, CHAP connection"
284 * One way authentication has succeeded, return now if mutual
285 * authentication is not enabled.
287 if (!auth->authenticate_target) {
294 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
295 pr_err("Could not find CHAP_I.\n");
300 ret = kstrtoul(&identifier[2], 0, &id);
302 ret = kstrtoul(identifier, 0, &id);
305 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
309 pr_err("chap identifier: %lu greater than 255\n", id);
313 * RFC 1994 says Identifier is no more than octet (8 bits).
315 pr_debug("[server] Got CHAP_I=%lu\n", id);
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");
326 pr_err("Could not find CHAP_C.\n");
329 challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
330 if (!challenge_len) {
331 pr_err("Unable to convert incoming challenge\n");
334 if (challenge_len > 1024) {
335 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
338 if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
339 pr_err("Malformed CHAP_C\n");
342 pr_debug("[server] Got CHAP_C=%s\n", challenge);
344 * During mutual authentication, the CHAP_C generated by the
345 * initiator must not match the original CHAP_C generated by
348 if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
349 pr_err("initiator CHAP_C matches target CHAP_C, failing"
354 * Generate CHAP_N and CHAP_R for mutual authentication.
356 ret = crypto_shash_init(desc);
358 pr_err("crypto_shash_init() failed\n");
362 /* To handle both endiannesses */
364 ret = crypto_shash_update(desc, &id_as_uchar, 1);
366 pr_err("crypto_shash_update() failed for id\n");
370 ret = crypto_shash_update(desc, auth->password_mutual,
371 strlen(auth->password_mutual));
373 pr_err("crypto_shash_update() failed for"
374 " password_mutual\n");
378 * Convert received challenge to binary hex.
380 ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
383 pr_err("crypto_shash_finup() failed for ma challenge\n");
388 * Generate CHAP_N and CHAP_R.
390 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
392 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
394 * Convert response from binary hex to ascii hext.
396 bin2hex(response, digest, MD5_SIGNATURE_SIZE);
397 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
400 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
405 crypto_free_shash(tfm);
407 kfree(challenge_binhex);
411 static int chap_got_response(
412 struct iscsi_conn *conn,
413 struct iscsi_node_auth *auth,
416 unsigned int *nr_out_len)
418 struct iscsi_chap *chap = conn->auth_protocol;
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)
427 pr_err("Unknown CHAP digest type %d!\n",
434 struct iscsi_conn *conn,
435 struct iscsi_node_auth *auth,
441 struct iscsi_chap *chap = conn->auth_protocol;
444 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
447 chap->chap_state = CHAP_STAGE_SERVER_AIC;
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,
456 if (auth->authenticate_target)
457 chap->chap_state = CHAP_STAGE_SERVER_NR;