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/random.h>
24 #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 int chap_string_to_hex(unsigned char *dst
, unsigned char *src
, int len
)
31 int j
= DIV_ROUND_UP(len
, 2), rc
;
33 rc
= hex2bin(dst
, src
, j
);
35 pr_debug("CHAP string contains non hex digit symbols\n");
41 static void chap_binaryhex_to_asciihex(char *dst
, char *src
, int src_len
)
45 for (i
= 0; i
< src_len
; i
++) {
46 sprintf(&dst
[i
*2], "%02x", (int) src
[i
] & 0xff);
50 static int chap_gen_challenge(
51 struct iscsi_conn
*conn
,
57 unsigned char challenge_asciihex
[CHAP_CHALLENGE_LENGTH
* 2 + 1];
58 struct iscsi_chap
*chap
= conn
->auth_protocol
;
60 memset(challenge_asciihex
, 0, CHAP_CHALLENGE_LENGTH
* 2 + 1);
62 ret
= get_random_bytes_wait(chap
->challenge
, CHAP_CHALLENGE_LENGTH
);
65 chap_binaryhex_to_asciihex(challenge_asciihex
, chap
->challenge
,
66 CHAP_CHALLENGE_LENGTH
);
68 * Set CHAP_C, and copy the generated challenge into c_str.
70 *c_len
+= sprintf(c_str
+ *c_len
, "CHAP_C=0x%s", challenge_asciihex
);
73 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller
) ? "server" : "client",
78 static int chap_check_algorithm(const char *a_str
)
80 char *tmp
, *orig
, *token
;
82 tmp
= kstrdup(a_str
, GFP_KERNEL
);
84 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
85 return CHAP_DIGEST_UNKNOWN
;
89 token
= strsep(&tmp
, "=");
93 if (strcmp(token
, "CHAP_A")) {
94 pr_err("Unable to locate CHAP_A key\n");
98 token
= strsep(&tmp
, ",");
102 if (!strncmp(token
, "5", 1)) {
103 pr_debug("Selected MD5 Algorithm\n");
105 return CHAP_DIGEST_MD5
;
110 return CHAP_DIGEST_UNKNOWN
;
113 static struct iscsi_chap
*chap_server_open(
114 struct iscsi_conn
*conn
,
115 struct iscsi_node_auth
*auth
,
118 unsigned int *aic_len
)
121 struct iscsi_chap
*chap
;
123 if (!(auth
->naf_flags
& NAF_USERID_SET
) ||
124 !(auth
->naf_flags
& NAF_PASSWORD_SET
)) {
125 pr_err("CHAP user or password not set for"
130 conn
->auth_protocol
= kzalloc(sizeof(struct iscsi_chap
), GFP_KERNEL
);
131 if (!conn
->auth_protocol
)
134 chap
= conn
->auth_protocol
;
135 ret
= chap_check_algorithm(a_str
);
137 case CHAP_DIGEST_MD5
:
138 pr_debug("[server] Got CHAP_A=5\n");
140 * Send back CHAP_A set to MD5.
142 *aic_len
= sprintf(aic_str
, "CHAP_A=5");
144 chap
->digest_type
= CHAP_DIGEST_MD5
;
145 pr_debug("[server] Sending CHAP_A=%d\n", chap
->digest_type
);
147 case CHAP_DIGEST_UNKNOWN
:
149 pr_err("Unsupported CHAP_A value\n");
150 kfree(conn
->auth_protocol
);
157 chap
->id
= conn
->tpg
->tpg_chap_id
++;
158 *aic_len
+= sprintf(aic_str
+ *aic_len
, "CHAP_I=%d", chap
->id
);
160 pr_debug("[server] Sending CHAP_I=%d\n", chap
->id
);
162 * Generate Challenge.
164 if (chap_gen_challenge(conn
, 1, aic_str
, aic_len
) < 0) {
165 kfree(conn
->auth_protocol
);
172 static void chap_close(struct iscsi_conn
*conn
)
174 kfree(conn
->auth_protocol
);
175 conn
->auth_protocol
= NULL
;
178 static int chap_server_compute_md5(
179 struct iscsi_conn
*conn
,
180 struct iscsi_node_auth
*auth
,
183 unsigned int *nr_out_len
)
186 unsigned char id_as_uchar
;
187 unsigned char digest
[MD5_SIGNATURE_SIZE
];
188 unsigned char type
, response
[MD5_SIGNATURE_SIZE
* 2 + 2];
189 unsigned char identifier
[10], *challenge
= NULL
;
190 unsigned char *challenge_binhex
= NULL
;
191 unsigned char client_digest
[MD5_SIGNATURE_SIZE
];
192 unsigned char server_digest
[MD5_SIGNATURE_SIZE
];
193 unsigned char chap_n
[MAX_CHAP_N_SIZE
], chap_r
[MAX_RESPONSE_LENGTH
];
195 struct iscsi_chap
*chap
= conn
->auth_protocol
;
196 struct crypto_shash
*tfm
= NULL
;
197 struct shash_desc
*desc
= NULL
;
198 int auth_ret
= -1, ret
, challenge_len
;
200 memset(identifier
, 0, 10);
201 memset(chap_n
, 0, MAX_CHAP_N_SIZE
);
202 memset(chap_r
, 0, MAX_RESPONSE_LENGTH
);
203 memset(digest
, 0, MD5_SIGNATURE_SIZE
);
204 memset(response
, 0, MD5_SIGNATURE_SIZE
* 2 + 2);
205 memset(client_digest
, 0, MD5_SIGNATURE_SIZE
);
206 memset(server_digest
, 0, MD5_SIGNATURE_SIZE
);
208 challenge
= kzalloc(CHAP_CHALLENGE_STR_LEN
, GFP_KERNEL
);
210 pr_err("Unable to allocate challenge buffer\n");
214 challenge_binhex
= kzalloc(CHAP_CHALLENGE_STR_LEN
, GFP_KERNEL
);
215 if (!challenge_binhex
) {
216 pr_err("Unable to allocate challenge_binhex buffer\n");
222 if (extract_param(nr_in_ptr
, "CHAP_N", MAX_CHAP_N_SIZE
, chap_n
,
224 pr_err("Could not find CHAP_N.\n");
228 pr_err("Could not find CHAP_N.\n");
232 /* Include the terminating NULL in the compare */
233 compare_len
= strlen(auth
->userid
) + 1;
234 if (strncmp(chap_n
, auth
->userid
, compare_len
) != 0) {
235 pr_err("CHAP_N values do not match!\n");
238 pr_debug("[server] Got CHAP_N=%s\n", chap_n
);
242 if (extract_param(nr_in_ptr
, "CHAP_R", MAX_RESPONSE_LENGTH
, chap_r
,
244 pr_err("Could not find CHAP_R.\n");
248 pr_err("Could not find CHAP_R.\n");
252 pr_debug("[server] Got CHAP_R=%s\n", chap_r
);
253 chap_string_to_hex(client_digest
, chap_r
, strlen(chap_r
));
255 tfm
= crypto_alloc_shash("md5", 0, 0);
258 pr_err("Unable to allocate struct crypto_shash\n");
262 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(tfm
), GFP_KERNEL
);
264 pr_err("Unable to allocate struct shash_desc\n");
271 ret
= crypto_shash_init(desc
);
273 pr_err("crypto_shash_init() failed\n");
277 ret
= crypto_shash_update(desc
, &chap
->id
, 1);
279 pr_err("crypto_shash_update() failed for id\n");
283 ret
= crypto_shash_update(desc
, (char *)&auth
->password
,
284 strlen(auth
->password
));
286 pr_err("crypto_shash_update() failed for password\n");
290 ret
= crypto_shash_finup(desc
, chap
->challenge
,
291 CHAP_CHALLENGE_LENGTH
, server_digest
);
293 pr_err("crypto_shash_finup() failed for challenge\n");
297 chap_binaryhex_to_asciihex(response
, server_digest
, MD5_SIGNATURE_SIZE
);
298 pr_debug("[server] MD5 Server Digest: %s\n", response
);
300 if (memcmp(server_digest
, client_digest
, MD5_SIGNATURE_SIZE
) != 0) {
301 pr_debug("[server] MD5 Digests do not match!\n\n");
304 pr_debug("[server] MD5 Digests match, CHAP connection"
307 * One way authentication has succeeded, return now if mutual
308 * authentication is not enabled.
310 if (!auth
->authenticate_target
) {
317 if (extract_param(nr_in_ptr
, "CHAP_I", 10, identifier
, &type
) < 0) {
318 pr_err("Could not find CHAP_I.\n");
323 ret
= kstrtoul(&identifier
[2], 0, &id
);
325 ret
= kstrtoul(identifier
, 0, &id
);
328 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret
);
332 pr_err("chap identifier: %lu greater than 255\n", id
);
336 * RFC 1994 says Identifier is no more than octet (8 bits).
338 pr_debug("[server] Got CHAP_I=%lu\n", id
);
342 if (extract_param(nr_in_ptr
, "CHAP_C", CHAP_CHALLENGE_STR_LEN
,
343 challenge
, &type
) < 0) {
344 pr_err("Could not find CHAP_C.\n");
349 pr_err("Could not find CHAP_C.\n");
352 pr_debug("[server] Got CHAP_C=%s\n", challenge
);
353 challenge_len
= chap_string_to_hex(challenge_binhex
, challenge
,
355 if (!challenge_len
) {
356 pr_err("Unable to convert incoming challenge\n");
359 if (challenge_len
> 1024) {
360 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
364 * During mutual authentication, the CHAP_C generated by the
365 * initiator must not match the original CHAP_C generated by
368 if (!memcmp(challenge_binhex
, chap
->challenge
, CHAP_CHALLENGE_LENGTH
)) {
369 pr_err("initiator CHAP_C matches target CHAP_C, failing"
374 * Generate CHAP_N and CHAP_R for mutual authentication.
376 ret
= crypto_shash_init(desc
);
378 pr_err("crypto_shash_init() failed\n");
382 /* To handle both endiannesses */
384 ret
= crypto_shash_update(desc
, &id_as_uchar
, 1);
386 pr_err("crypto_shash_update() failed for id\n");
390 ret
= crypto_shash_update(desc
, auth
->password_mutual
,
391 strlen(auth
->password_mutual
));
393 pr_err("crypto_shash_update() failed for"
394 " password_mutual\n");
398 * Convert received challenge to binary hex.
400 ret
= crypto_shash_finup(desc
, challenge_binhex
, challenge_len
,
403 pr_err("crypto_shash_finup() failed for ma challenge\n");
408 * Generate CHAP_N and CHAP_R.
410 *nr_out_len
= sprintf(nr_out_ptr
, "CHAP_N=%s", auth
->userid_mutual
);
412 pr_debug("[server] Sending CHAP_N=%s\n", auth
->userid_mutual
);
414 * Convert response from binary hex to ascii hext.
416 chap_binaryhex_to_asciihex(response
, digest
, MD5_SIGNATURE_SIZE
);
417 *nr_out_len
+= sprintf(nr_out_ptr
+ *nr_out_len
, "CHAP_R=0x%s",
420 pr_debug("[server] Sending CHAP_R=0x%s\n", response
);
425 crypto_free_shash(tfm
);
427 kfree(challenge_binhex
);
431 static int chap_got_response(
432 struct iscsi_conn
*conn
,
433 struct iscsi_node_auth
*auth
,
436 unsigned int *nr_out_len
)
438 struct iscsi_chap
*chap
= conn
->auth_protocol
;
440 switch (chap
->digest_type
) {
441 case CHAP_DIGEST_MD5
:
442 if (chap_server_compute_md5(conn
, auth
, nr_in_ptr
,
443 nr_out_ptr
, nr_out_len
) < 0)
447 pr_err("Unknown CHAP digest type %d!\n",
454 struct iscsi_conn
*conn
,
455 struct iscsi_node_auth
*auth
,
461 struct iscsi_chap
*chap
= conn
->auth_protocol
;
464 chap
= chap_server_open(conn
, auth
, in_text
, out_text
, out_len
);
467 chap
->chap_state
= CHAP_STAGE_SERVER_AIC
;
469 } else if (chap
->chap_state
== CHAP_STAGE_SERVER_AIC
) {
470 convert_null_to_semi(in_text
, *in_len
);
471 if (chap_got_response(conn
, auth
, in_text
, out_text
,
476 if (auth
->authenticate_target
)
477 chap
->chap_state
= CHAP_STAGE_SERVER_NR
;