1 /*******************************************************************************
2 * This file houses the main functions for the iSCSI CHAP support
4 * \u00a9 Copyright 2007-2011 RisingTide Systems LLC.
6 * Licensed to the Linux Foundation under the General Public License (GPL) version 2.
8 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 ******************************************************************************/
21 #include <linux/kernel.h>
22 #include <linux/string.h>
23 #include <linux/crypto.h>
24 #include <linux/err.h>
25 #include <linux/scatterlist.h>
27 #include "iscsi_target_core.h"
28 #include "iscsi_target_nego.h"
29 #include "iscsi_target_auth.h"
31 static int chap_string_to_hex(unsigned char *dst, unsigned char *src, int len)
33 int j = DIV_ROUND_UP(len, 2), rc;
35 rc = hex2bin(dst, src, j);
37 pr_debug("CHAP string contains non hex digit symbols\n");
43 static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
47 for (i = 0; i < src_len; i++) {
48 sprintf(&dst[i*2], "%02x", (int) src[i] & 0xff);
52 static void chap_set_random(char *data, int length)
58 get_random_bytes(&r, sizeof(long));
63 get_random_bytes(&r, sizeof(long));
66 n = (n << 3) | (r & 0x7);
68 get_random_bytes(&r, sizeof(long));
71 n = (n << 2) | (r & 0x3);
78 static void chap_gen_challenge(
79 struct iscsi_conn *conn,
84 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
85 struct iscsi_chap *chap = conn->auth_protocol;
87 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
89 chap_set_random(chap->challenge, CHAP_CHALLENGE_LENGTH);
90 chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
91 CHAP_CHALLENGE_LENGTH);
93 * Set CHAP_C, and copy the generated challenge into c_str.
95 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
98 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
103 static struct iscsi_chap *chap_server_open(
104 struct iscsi_conn *conn,
105 struct iscsi_node_auth *auth,
108 unsigned int *aic_len)
110 struct iscsi_chap *chap;
112 if (!(auth->naf_flags & NAF_USERID_SET) ||
113 !(auth->naf_flags & NAF_PASSWORD_SET)) {
114 pr_err("CHAP user or password not set for"
119 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
120 if (!conn->auth_protocol)
123 chap = conn->auth_protocol;
125 * We only support MD5 MDA presently.
127 if (strncmp(a_str, "CHAP_A=5", 8)) {
128 pr_err("CHAP_A is not MD5.\n");
131 pr_debug("[server] Got CHAP_A=5\n");
133 * Send back CHAP_A set to MD5.
135 *aic_len = sprintf(aic_str, "CHAP_A=5");
137 chap->digest_type = CHAP_DIGEST_MD5;
138 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
142 chap->id = ISCSI_TPG_C(conn)->tpg_chap_id++;
143 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
145 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
147 * Generate Challenge.
149 chap_gen_challenge(conn, 1, aic_str, aic_len);
154 static void chap_close(struct iscsi_conn *conn)
156 kfree(conn->auth_protocol);
157 conn->auth_protocol = NULL;
160 static int chap_server_compute_md5(
161 struct iscsi_conn *conn,
162 struct iscsi_node_auth *auth,
165 unsigned int *nr_out_len)
169 unsigned char id_as_uchar;
170 unsigned char digest[MD5_SIGNATURE_SIZE];
171 unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
172 unsigned char identifier[10], *challenge = NULL;
173 unsigned char *challenge_binhex = NULL;
174 unsigned char client_digest[MD5_SIGNATURE_SIZE];
175 unsigned char server_digest[MD5_SIGNATURE_SIZE];
176 unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
177 struct iscsi_chap *chap = conn->auth_protocol;
178 struct crypto_hash *tfm;
179 struct hash_desc desc;
180 struct scatterlist sg;
181 int auth_ret = -1, ret, challenge_len;
183 memset(identifier, 0, 10);
184 memset(chap_n, 0, MAX_CHAP_N_SIZE);
185 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
186 memset(digest, 0, MD5_SIGNATURE_SIZE);
187 memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
188 memset(client_digest, 0, MD5_SIGNATURE_SIZE);
189 memset(server_digest, 0, MD5_SIGNATURE_SIZE);
191 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
193 pr_err("Unable to allocate challenge buffer\n");
197 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
198 if (!challenge_binhex) {
199 pr_err("Unable to allocate challenge_binhex buffer\n");
205 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
207 pr_err("Could not find CHAP_N.\n");
211 pr_err("Could not find CHAP_N.\n");
215 if (memcmp(chap_n, auth->userid, strlen(auth->userid)) != 0) {
216 pr_err("CHAP_N values do not match!\n");
219 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
223 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
225 pr_err("Could not find CHAP_R.\n");
229 pr_err("Could not find CHAP_R.\n");
233 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
234 chap_string_to_hex(client_digest, chap_r, strlen(chap_r));
236 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
238 pr_err("Unable to allocate struct crypto_hash\n");
244 ret = crypto_hash_init(&desc);
246 pr_err("crypto_hash_init() failed\n");
247 crypto_free_hash(tfm);
251 sg_init_one(&sg, &chap->id, 1);
252 ret = crypto_hash_update(&desc, &sg, 1);
254 pr_err("crypto_hash_update() failed for id\n");
255 crypto_free_hash(tfm);
259 sg_init_one(&sg, &auth->password, strlen(auth->password));
260 ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
262 pr_err("crypto_hash_update() failed for password\n");
263 crypto_free_hash(tfm);
267 sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH);
268 ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
270 pr_err("crypto_hash_update() failed for challenge\n");
271 crypto_free_hash(tfm);
275 ret = crypto_hash_final(&desc, server_digest);
277 pr_err("crypto_hash_final() failed for server digest\n");
278 crypto_free_hash(tfm);
281 crypto_free_hash(tfm);
283 chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
284 pr_debug("[server] MD5 Server Digest: %s\n", response);
286 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
287 pr_debug("[server] MD5 Digests do not match!\n\n");
290 pr_debug("[server] MD5 Digests match, CHAP connetication"
293 * One way authentication has succeeded, return now if mutual
294 * authentication is not enabled.
296 if (!auth->authenticate_target) {
298 kfree(challenge_binhex);
304 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
305 pr_err("Could not find CHAP_I.\n");
310 id = simple_strtoul(&identifier[2], &endptr, 0);
312 id = simple_strtoul(identifier, &endptr, 0);
314 pr_err("chap identifier: %lu greater than 255\n", id);
318 * RFC 1994 says Identifier is no more than octet (8 bits).
320 pr_debug("[server] Got CHAP_I=%lu\n", id);
324 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
325 challenge, &type) < 0) {
326 pr_err("Could not find CHAP_C.\n");
331 pr_err("Could not find CHAP_C.\n");
334 pr_debug("[server] Got CHAP_C=%s\n", challenge);
335 challenge_len = chap_string_to_hex(challenge_binhex, challenge,
337 if (!challenge_len) {
338 pr_err("Unable to convert incoming challenge\n");
342 * Generate CHAP_N and CHAP_R for mutual authentication.
344 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
346 pr_err("Unable to allocate struct crypto_hash\n");
352 ret = crypto_hash_init(&desc);
354 pr_err("crypto_hash_init() failed\n");
355 crypto_free_hash(tfm);
359 /* To handle both endiannesses */
361 sg_init_one(&sg, &id_as_uchar, 1);
362 ret = crypto_hash_update(&desc, &sg, 1);
364 pr_err("crypto_hash_update() failed for id\n");
365 crypto_free_hash(tfm);
369 sg_init_one(&sg, auth->password_mutual,
370 strlen(auth->password_mutual));
371 ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
373 pr_err("crypto_hash_update() failed for"
374 " password_mutual\n");
375 crypto_free_hash(tfm);
379 * Convert received challenge to binary hex.
381 sg_init_one(&sg, challenge_binhex, challenge_len);
382 ret = crypto_hash_update(&desc, &sg, challenge_len);
384 pr_err("crypto_hash_update() failed for ma challenge\n");
385 crypto_free_hash(tfm);
389 ret = crypto_hash_final(&desc, digest);
391 pr_err("crypto_hash_final() failed for ma digest\n");
392 crypto_free_hash(tfm);
395 crypto_free_hash(tfm);
397 * Generate CHAP_N and CHAP_R.
399 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
401 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
403 * Convert response from binary hex to ascii hext.
405 chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
406 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
409 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
413 kfree(challenge_binhex);
417 static int chap_got_response(
418 struct iscsi_conn *conn,
419 struct iscsi_node_auth *auth,
422 unsigned int *nr_out_len)
424 struct iscsi_chap *chap = conn->auth_protocol;
426 switch (chap->digest_type) {
427 case CHAP_DIGEST_MD5:
428 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
429 nr_out_ptr, nr_out_len) < 0)
433 pr_err("Unknown CHAP digest type %d!\n",
440 struct iscsi_conn *conn,
441 struct iscsi_node_auth *auth,
447 struct iscsi_chap *chap = conn->auth_protocol;
450 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
453 chap->chap_state = CHAP_STAGE_SERVER_AIC;
455 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
456 convert_null_to_semi(in_text, *in_len);
457 if (chap_got_response(conn, auth, in_text, out_text,
462 if (auth->authenticate_target)
463 chap->chap_state = CHAP_STAGE_SERVER_NR;