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/string.h>
22 #include <linux/crypto.h>
23 #include <linux/err.h>
24 #include <linux/scatterlist.h>
26 #include "iscsi_target_core.h"
27 #include "iscsi_target_nego.h"
28 #include "iscsi_target_auth.h"
30 static unsigned char chap_asciihex_to_binaryhex(unsigned char val[2])
32 unsigned char result = 0;
36 if ((val[0] >= 'a') && (val[0] <= 'f'))
37 result = ((val[0] - 'a' + 10) & 0xf) << 4;
39 if ((val[0] >= 'A') && (val[0] <= 'F'))
40 result = ((val[0] - 'A' + 10) & 0xf) << 4;
42 result = ((val[0] - '0') & 0xf) << 4;
46 if ((val[1] >= 'a') && (val[1] <= 'f'))
47 result |= ((val[1] - 'a' + 10) & 0xf);
49 if ((val[1] >= 'A') && (val[1] <= 'F'))
50 result |= ((val[1] - 'A' + 10) & 0xf);
52 result |= ((val[1] - '0') & 0xf);
57 static int chap_string_to_hex(unsigned char *dst, unsigned char *src, int len)
61 for (i = 0; i < len; i += 2) {
62 dst[j++] = (unsigned char) chap_asciihex_to_binaryhex(&src[i]);
69 static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
73 for (i = 0; i < src_len; i++) {
74 sprintf(&dst[i*2], "%02x", (int) src[i] & 0xff);
78 static void chap_set_random(char *data, int length)
84 get_random_bytes(&r, sizeof(long));
89 get_random_bytes(&r, sizeof(long));
92 n = (n << 3) | (r & 0x7);
94 get_random_bytes(&r, sizeof(long));
97 n = (n << 2) | (r & 0x3);
104 static void chap_gen_challenge(
105 struct iscsi_conn *conn,
110 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
111 struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
113 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
115 chap_set_random(chap->challenge, CHAP_CHALLENGE_LENGTH);
116 chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
117 CHAP_CHALLENGE_LENGTH);
119 * Set CHAP_C, and copy the generated challenge into c_str.
121 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
124 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
129 static struct iscsi_chap *chap_server_open(
130 struct iscsi_conn *conn,
131 struct iscsi_node_auth *auth,
134 unsigned int *aic_len)
136 struct iscsi_chap *chap;
138 if (!(auth->naf_flags & NAF_USERID_SET) ||
139 !(auth->naf_flags & NAF_PASSWORD_SET)) {
140 pr_err("CHAP user or password not set for"
145 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
146 if (!conn->auth_protocol)
149 chap = (struct iscsi_chap *) conn->auth_protocol;
151 * We only support MD5 MDA presently.
153 if (strncmp(a_str, "CHAP_A=5", 8)) {
154 pr_err("CHAP_A is not MD5.\n");
157 pr_debug("[server] Got CHAP_A=5\n");
159 * Send back CHAP_A set to MD5.
161 *aic_len = sprintf(aic_str, "CHAP_A=5");
163 chap->digest_type = CHAP_DIGEST_MD5;
164 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
168 chap->id = ISCSI_TPG_C(conn)->tpg_chap_id++;
169 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
171 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
173 * Generate Challenge.
175 chap_gen_challenge(conn, 1, aic_str, aic_len);
180 static void chap_close(struct iscsi_conn *conn)
182 kfree(conn->auth_protocol);
183 conn->auth_protocol = NULL;
186 static int chap_server_compute_md5(
187 struct iscsi_conn *conn,
188 struct iscsi_node_auth *auth,
191 unsigned int *nr_out_len)
194 unsigned char id, digest[MD5_SIGNATURE_SIZE];
195 unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
196 unsigned char identifier[10], *challenge = NULL;
197 unsigned char *challenge_binhex = NULL;
198 unsigned char client_digest[MD5_SIGNATURE_SIZE];
199 unsigned char server_digest[MD5_SIGNATURE_SIZE];
200 unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
201 struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
202 struct crypto_hash *tfm;
203 struct hash_desc desc;
204 struct scatterlist sg;
205 int auth_ret = -1, ret, challenge_len;
207 memset(identifier, 0, 10);
208 memset(chap_n, 0, MAX_CHAP_N_SIZE);
209 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
210 memset(digest, 0, MD5_SIGNATURE_SIZE);
211 memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
212 memset(client_digest, 0, MD5_SIGNATURE_SIZE);
213 memset(server_digest, 0, MD5_SIGNATURE_SIZE);
215 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
217 pr_err("Unable to allocate challenge buffer\n");
221 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
222 if (!challenge_binhex) {
223 pr_err("Unable to allocate challenge_binhex buffer\n");
229 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
231 pr_err("Could not find CHAP_N.\n");
235 pr_err("Could not find CHAP_N.\n");
239 if (memcmp(chap_n, auth->userid, strlen(auth->userid)) != 0) {
240 pr_err("CHAP_N values do not match!\n");
243 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
247 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
249 pr_err("Could not find CHAP_R.\n");
253 pr_err("Could not find CHAP_R.\n");
257 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
258 chap_string_to_hex(client_digest, chap_r, strlen(chap_r));
260 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
262 pr_err("Unable to allocate struct crypto_hash\n");
268 ret = crypto_hash_init(&desc);
270 pr_err("crypto_hash_init() failed\n");
271 crypto_free_hash(tfm);
275 sg_init_one(&sg, (void *)&chap->id, 1);
276 ret = crypto_hash_update(&desc, &sg, 1);
278 pr_err("crypto_hash_update() failed for id\n");
279 crypto_free_hash(tfm);
283 sg_init_one(&sg, (void *)&auth->password, strlen(auth->password));
284 ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
286 pr_err("crypto_hash_update() failed for password\n");
287 crypto_free_hash(tfm);
291 sg_init_one(&sg, (void *)chap->challenge, CHAP_CHALLENGE_LENGTH);
292 ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
294 pr_err("crypto_hash_update() failed for challenge\n");
295 crypto_free_hash(tfm);
299 ret = crypto_hash_final(&desc, server_digest);
301 pr_err("crypto_hash_final() failed for server digest\n");
302 crypto_free_hash(tfm);
305 crypto_free_hash(tfm);
307 chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
308 pr_debug("[server] MD5 Server Digest: %s\n", response);
310 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
311 pr_debug("[server] MD5 Digests do not match!\n\n");
314 pr_debug("[server] MD5 Digests match, CHAP connetication"
317 * One way authentication has succeeded, return now if mutual
318 * authentication is not enabled.
320 if (!auth->authenticate_target) {
322 kfree(challenge_binhex);
328 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
329 pr_err("Could not find CHAP_I.\n");
334 id = (unsigned char)simple_strtoul((char *)&identifier[2],
337 id = (unsigned char)simple_strtoul(identifier, &endptr, 0);
339 * RFC 1994 says Identifier is no more than octet (8 bits).
341 pr_debug("[server] Got CHAP_I=%d\n", id);
345 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
346 challenge, &type) < 0) {
347 pr_err("Could not find CHAP_C.\n");
352 pr_err("Could not find CHAP_C.\n");
355 pr_debug("[server] Got CHAP_C=%s\n", challenge);
356 challenge_len = chap_string_to_hex(challenge_binhex, challenge,
358 if (!challenge_len) {
359 pr_err("Unable to convert incoming challenge\n");
363 * Generate CHAP_N and CHAP_R for mutual authentication.
365 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
367 pr_err("Unable to allocate struct crypto_hash\n");
373 ret = crypto_hash_init(&desc);
375 pr_err("crypto_hash_init() failed\n");
376 crypto_free_hash(tfm);
380 sg_init_one(&sg, (void *)&id, 1);
381 ret = crypto_hash_update(&desc, &sg, 1);
383 pr_err("crypto_hash_update() failed for id\n");
384 crypto_free_hash(tfm);
388 sg_init_one(&sg, (void *)auth->password_mutual,
389 strlen(auth->password_mutual));
390 ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
392 pr_err("crypto_hash_update() failed for"
393 " password_mutual\n");
394 crypto_free_hash(tfm);
398 * Convert received challenge to binary hex.
400 sg_init_one(&sg, (void *)challenge_binhex, challenge_len);
401 ret = crypto_hash_update(&desc, &sg, challenge_len);
403 pr_err("crypto_hash_update() failed for ma challenge\n");
404 crypto_free_hash(tfm);
408 ret = crypto_hash_final(&desc, digest);
410 pr_err("crypto_hash_final() failed for ma digest\n");
411 crypto_free_hash(tfm);
414 crypto_free_hash(tfm);
416 * Generate CHAP_N and CHAP_R.
418 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
420 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
422 * Convert response from binary hex to ascii hext.
424 chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
425 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
428 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
432 kfree(challenge_binhex);
436 static int chap_got_response(
437 struct iscsi_conn *conn,
438 struct iscsi_node_auth *auth,
441 unsigned int *nr_out_len)
443 struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
445 switch (chap->digest_type) {
446 case CHAP_DIGEST_MD5:
447 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
448 nr_out_ptr, nr_out_len) < 0)
452 pr_err("Unknown CHAP digest type %d!\n",
459 struct iscsi_conn *conn,
460 struct iscsi_node_auth *auth,
466 struct iscsi_chap *chap = (struct iscsi_chap *) conn->auth_protocol;
469 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
472 chap->chap_state = CHAP_STAGE_SERVER_AIC;
474 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
475 convert_null_to_semi(in_text, *in_len);
476 if (chap_got_response(conn, auth, in_text, out_text,
481 if (auth->authenticate_target)
482 chap->chap_state = CHAP_STAGE_SERVER_NR;