1 /*******************************************************************************
2 * This file houses the main functions for the iSCSI CHAP support
4 * (c) Copyright 2007-2013 Datera, Inc.
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
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.
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 ******************************************************************************/
19 #include <crypto/hash.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/err.h>
23 #include <linux/scatterlist.h>
25 #include <target/iscsi/iscsi_target_core.h>
26 #include "iscsi_target_nego.h"
27 #include "iscsi_target_auth.h"
29 static void chap_gen_challenge(
30 struct iscsi_conn *conn,
35 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
36 struct iscsi_chap *chap = conn->auth_protocol;
38 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
40 get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH);
41 bin2hex(challenge_asciihex, chap->challenge,
42 CHAP_CHALLENGE_LENGTH);
44 * Set CHAP_C, and copy the generated challenge into c_str.
46 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
49 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
53 static int chap_check_algorithm(const char *a_str)
55 char *tmp, *orig, *token;
57 tmp = kstrdup(a_str, GFP_KERNEL);
59 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
60 return CHAP_DIGEST_UNKNOWN;
64 token = strsep(&tmp, "=");
68 if (strcmp(token, "CHAP_A")) {
69 pr_err("Unable to locate CHAP_A key\n");
73 token = strsep(&tmp, ",");
77 if (!strcmp(token, "5")) {
78 pr_debug("Selected MD5 Algorithm\n");
80 return CHAP_DIGEST_MD5;
85 return CHAP_DIGEST_UNKNOWN;
88 static struct iscsi_chap *chap_server_open(
89 struct iscsi_conn *conn,
90 struct iscsi_node_auth *auth,
93 unsigned int *aic_len)
96 struct iscsi_chap *chap;
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"
105 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
106 if (!conn->auth_protocol)
109 chap = conn->auth_protocol;
110 ret = chap_check_algorithm(a_str);
112 case CHAP_DIGEST_MD5:
113 pr_debug("[server] Got CHAP_A=5\n");
115 * Send back CHAP_A set to MD5.
117 *aic_len = sprintf(aic_str, "CHAP_A=5");
119 chap->digest_type = CHAP_DIGEST_MD5;
120 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
122 case CHAP_DIGEST_UNKNOWN:
124 pr_err("Unsupported CHAP_A value\n");
131 chap->id = conn->tpg->tpg_chap_id++;
132 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
134 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
136 * Generate Challenge.
138 chap_gen_challenge(conn, 1, aic_str, aic_len);
143 static void chap_close(struct iscsi_conn *conn)
145 kfree(conn->auth_protocol);
146 conn->auth_protocol = NULL;
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");
249 ret = crypto_shash_init(desc);
251 pr_err("crypto_shash_init() failed\n");
255 ret = crypto_shash_update(desc, &chap->id, 1);
257 pr_err("crypto_shash_update() failed for id\n");
261 ret = crypto_shash_update(desc, (char *)&auth->password,
262 strlen(auth->password));
264 pr_err("crypto_shash_update() failed for password\n");
268 ret = crypto_shash_finup(desc, chap->challenge,
269 CHAP_CHALLENGE_LENGTH, server_digest);
271 pr_err("crypto_shash_finup() failed for challenge\n");
275 bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
276 pr_debug("[server] MD5 Server Digest: %s\n", response);
278 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
279 pr_debug("[server] MD5 Digests do not match!\n\n");
282 pr_debug("[server] MD5 Digests match, CHAP connection"
285 * One way authentication has succeeded, return now if mutual
286 * authentication is not enabled.
288 if (!auth->authenticate_target) {
295 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
296 pr_err("Could not find CHAP_I.\n");
301 ret = kstrtoul(&identifier[2], 0, &id);
303 ret = kstrtoul(identifier, 0, &id);
306 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
310 pr_err("chap identifier: %lu greater than 255\n", id);
314 * RFC 1994 says Identifier is no more than octet (8 bits).
316 pr_debug("[server] Got CHAP_I=%lu\n", id);
320 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
321 challenge, &type) < 0) {
322 pr_err("Could not find CHAP_C.\n");
327 pr_err("Could not find CHAP_C.\n");
330 challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
331 if (!challenge_len) {
332 pr_err("Unable to convert incoming challenge\n");
335 if (challenge_len > 1024) {
336 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
339 if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
340 pr_err("Malformed CHAP_C\n");
343 pr_debug("[server] Got CHAP_C=%s\n", challenge);
345 * During mutual authentication, the CHAP_C generated by the
346 * initiator must not match the original CHAP_C generated by
349 if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
350 pr_err("initiator CHAP_C matches target CHAP_C, failing"
355 * Generate CHAP_N and CHAP_R for mutual authentication.
357 ret = crypto_shash_init(desc);
359 pr_err("crypto_shash_init() failed\n");
363 /* To handle both endiannesses */
365 ret = crypto_shash_update(desc, &id_as_uchar, 1);
367 pr_err("crypto_shash_update() failed for id\n");
371 ret = crypto_shash_update(desc, auth->password_mutual,
372 strlen(auth->password_mutual));
374 pr_err("crypto_shash_update() failed for"
375 " password_mutual\n");
379 * Convert received challenge to binary hex.
381 ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
384 pr_err("crypto_shash_finup() failed for ma challenge\n");
389 * Generate CHAP_N and CHAP_R.
391 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
393 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
395 * Convert response from binary hex to ascii hext.
397 bin2hex(response, digest, MD5_SIGNATURE_SIZE);
398 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
401 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
406 crypto_free_shash(tfm);
408 kfree(challenge_binhex);
412 static int chap_got_response(
413 struct iscsi_conn *conn,
414 struct iscsi_node_auth *auth,
417 unsigned int *nr_out_len)
419 struct iscsi_chap *chap = conn->auth_protocol;
421 switch (chap->digest_type) {
422 case CHAP_DIGEST_MD5:
423 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
424 nr_out_ptr, nr_out_len) < 0)
428 pr_err("Unknown CHAP digest type %d!\n",
435 struct iscsi_conn *conn,
436 struct iscsi_node_auth *auth,
442 struct iscsi_chap *chap = conn->auth_protocol;
445 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
448 chap->chap_state = CHAP_STAGE_SERVER_AIC;
450 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
451 convert_null_to_semi(in_text, *in_len);
452 if (chap_got_response(conn, auth, in_text, out_text,
457 if (auth->authenticate_target)
458 chap->chap_state = CHAP_STAGE_SERVER_NR;