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_gen_challenge(
30 struct iscsi_conn
*conn
,
36 unsigned char challenge_asciihex
[CHAP_CHALLENGE_LENGTH
* 2 + 1];
37 struct iscsi_chap
*chap
= conn
->auth_protocol
;
39 memset(challenge_asciihex
, 0, CHAP_CHALLENGE_LENGTH
* 2 + 1);
41 ret
= get_random_bytes_wait(chap
->challenge
, CHAP_CHALLENGE_LENGTH
);
44 bin2hex(challenge_asciihex
, chap
->challenge
,
45 CHAP_CHALLENGE_LENGTH
);
47 * Set CHAP_C, and copy the generated challenge into c_str.
49 *c_len
+= sprintf(c_str
+ *c_len
, "CHAP_C=0x%s", challenge_asciihex
);
52 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller
) ? "server" : "client",
57 static int chap_check_algorithm(const char *a_str
)
59 char *tmp
, *orig
, *token
;
61 tmp
= kstrdup(a_str
, GFP_KERNEL
);
63 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
64 return CHAP_DIGEST_UNKNOWN
;
68 token
= strsep(&tmp
, "=");
72 if (strcmp(token
, "CHAP_A")) {
73 pr_err("Unable to locate CHAP_A key\n");
77 token
= strsep(&tmp
, ",");
81 if (!strncmp(token
, "5", 1)) {
82 pr_debug("Selected MD5 Algorithm\n");
84 return CHAP_DIGEST_MD5
;
89 return CHAP_DIGEST_UNKNOWN
;
92 static struct iscsi_chap
*chap_server_open(
93 struct iscsi_conn
*conn
,
94 struct iscsi_node_auth
*auth
,
97 unsigned int *aic_len
)
100 struct iscsi_chap
*chap
;
102 if (!(auth
->naf_flags
& NAF_USERID_SET
) ||
103 !(auth
->naf_flags
& NAF_PASSWORD_SET
)) {
104 pr_err("CHAP user or password not set for"
109 conn
->auth_protocol
= kzalloc(sizeof(struct iscsi_chap
), GFP_KERNEL
);
110 if (!conn
->auth_protocol
)
113 chap
= conn
->auth_protocol
;
114 ret
= chap_check_algorithm(a_str
);
116 case CHAP_DIGEST_MD5
:
117 pr_debug("[server] Got CHAP_A=5\n");
119 * Send back CHAP_A set to MD5.
121 *aic_len
= sprintf(aic_str
, "CHAP_A=5");
123 chap
->digest_type
= CHAP_DIGEST_MD5
;
124 pr_debug("[server] Sending CHAP_A=%d\n", chap
->digest_type
);
126 case CHAP_DIGEST_UNKNOWN
:
128 pr_err("Unsupported CHAP_A value\n");
129 kfree(conn
->auth_protocol
);
136 chap
->id
= conn
->tpg
->tpg_chap_id
++;
137 *aic_len
+= sprintf(aic_str
+ *aic_len
, "CHAP_I=%d", chap
->id
);
139 pr_debug("[server] Sending CHAP_I=%d\n", chap
->id
);
141 * Generate Challenge.
143 if (chap_gen_challenge(conn
, 1, aic_str
, aic_len
) < 0) {
144 kfree(conn
->auth_protocol
);
151 static void chap_close(struct iscsi_conn
*conn
)
153 kfree(conn
->auth_protocol
);
154 conn
->auth_protocol
= NULL
;
157 static int chap_server_compute_md5(
158 struct iscsi_conn
*conn
,
159 struct iscsi_node_auth
*auth
,
162 unsigned int *nr_out_len
)
165 unsigned char id_as_uchar
;
166 unsigned char digest
[MD5_SIGNATURE_SIZE
];
167 unsigned char type
, response
[MD5_SIGNATURE_SIZE
* 2 + 2];
168 unsigned char identifier
[10], *challenge
= NULL
;
169 unsigned char *challenge_binhex
= NULL
;
170 unsigned char client_digest
[MD5_SIGNATURE_SIZE
];
171 unsigned char server_digest
[MD5_SIGNATURE_SIZE
];
172 unsigned char chap_n
[MAX_CHAP_N_SIZE
], chap_r
[MAX_RESPONSE_LENGTH
];
174 struct iscsi_chap
*chap
= conn
->auth_protocol
;
175 struct crypto_shash
*tfm
= NULL
;
176 struct shash_desc
*desc
= NULL
;
177 int auth_ret
= -1, ret
, challenge_len
;
179 memset(identifier
, 0, 10);
180 memset(chap_n
, 0, MAX_CHAP_N_SIZE
);
181 memset(chap_r
, 0, MAX_RESPONSE_LENGTH
);
182 memset(digest
, 0, MD5_SIGNATURE_SIZE
);
183 memset(response
, 0, MD5_SIGNATURE_SIZE
* 2 + 2);
184 memset(client_digest
, 0, MD5_SIGNATURE_SIZE
);
185 memset(server_digest
, 0, MD5_SIGNATURE_SIZE
);
187 challenge
= kzalloc(CHAP_CHALLENGE_STR_LEN
, GFP_KERNEL
);
189 pr_err("Unable to allocate challenge buffer\n");
193 challenge_binhex
= kzalloc(CHAP_CHALLENGE_STR_LEN
, GFP_KERNEL
);
194 if (!challenge_binhex
) {
195 pr_err("Unable to allocate challenge_binhex buffer\n");
201 if (extract_param(nr_in_ptr
, "CHAP_N", MAX_CHAP_N_SIZE
, chap_n
,
203 pr_err("Could not find CHAP_N.\n");
207 pr_err("Could not find CHAP_N.\n");
211 /* Include the terminating NULL in the compare */
212 compare_len
= strlen(auth
->userid
) + 1;
213 if (strncmp(chap_n
, auth
->userid
, compare_len
) != 0) {
214 pr_err("CHAP_N values do not match!\n");
217 pr_debug("[server] Got CHAP_N=%s\n", chap_n
);
221 if (extract_param(nr_in_ptr
, "CHAP_R", MAX_RESPONSE_LENGTH
, chap_r
,
223 pr_err("Could not find CHAP_R.\n");
227 pr_err("Could not find CHAP_R.\n");
230 if (strlen(chap_r
) != MD5_SIGNATURE_SIZE
* 2) {
231 pr_err("Malformed CHAP_R\n");
234 if (hex2bin(client_digest
, chap_r
, MD5_SIGNATURE_SIZE
) < 0) {
235 pr_err("Malformed CHAP_R\n");
239 pr_debug("[server] Got CHAP_R=%s\n", chap_r
);
241 tfm
= crypto_alloc_shash("md5", 0, 0);
244 pr_err("Unable to allocate struct crypto_shash\n");
248 desc
= kmalloc(sizeof(*desc
) + crypto_shash_descsize(tfm
), GFP_KERNEL
);
250 pr_err("Unable to allocate struct shash_desc\n");
257 ret
= crypto_shash_init(desc
);
259 pr_err("crypto_shash_init() failed\n");
263 ret
= crypto_shash_update(desc
, &chap
->id
, 1);
265 pr_err("crypto_shash_update() failed for id\n");
269 ret
= crypto_shash_update(desc
, (char *)&auth
->password
,
270 strlen(auth
->password
));
272 pr_err("crypto_shash_update() failed for password\n");
276 ret
= crypto_shash_finup(desc
, chap
->challenge
,
277 CHAP_CHALLENGE_LENGTH
, server_digest
);
279 pr_err("crypto_shash_finup() failed for challenge\n");
283 bin2hex(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 connection"
293 * One way authentication has succeeded, return now if mutual
294 * authentication is not enabled.
296 if (!auth
->authenticate_target
) {
303 if (extract_param(nr_in_ptr
, "CHAP_I", 10, identifier
, &type
) < 0) {
304 pr_err("Could not find CHAP_I.\n");
309 ret
= kstrtoul(&identifier
[2], 0, &id
);
311 ret
= kstrtoul(identifier
, 0, &id
);
314 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret
);
318 pr_err("chap identifier: %lu greater than 255\n", id
);
322 * RFC 1994 says Identifier is no more than octet (8 bits).
324 pr_debug("[server] Got CHAP_I=%lu\n", id
);
328 if (extract_param(nr_in_ptr
, "CHAP_C", CHAP_CHALLENGE_STR_LEN
,
329 challenge
, &type
) < 0) {
330 pr_err("Could not find CHAP_C.\n");
335 pr_err("Could not find CHAP_C.\n");
338 challenge_len
= DIV_ROUND_UP(strlen(challenge
), 2);
339 if (!challenge_len
) {
340 pr_err("Unable to convert incoming challenge\n");
343 if (challenge_len
> 1024) {
344 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
347 if (hex2bin(challenge_binhex
, challenge
, challenge_len
) < 0) {
348 pr_err("Malformed CHAP_C\n");
351 pr_debug("[server] Got CHAP_C=%s\n", challenge
);
353 * During mutual authentication, the CHAP_C generated by the
354 * initiator must not match the original CHAP_C generated by
357 if (!memcmp(challenge_binhex
, chap
->challenge
, CHAP_CHALLENGE_LENGTH
)) {
358 pr_err("initiator CHAP_C matches target CHAP_C, failing"
363 * Generate CHAP_N and CHAP_R for mutual authentication.
365 ret
= crypto_shash_init(desc
);
367 pr_err("crypto_shash_init() failed\n");
371 /* To handle both endiannesses */
373 ret
= crypto_shash_update(desc
, &id_as_uchar
, 1);
375 pr_err("crypto_shash_update() failed for id\n");
379 ret
= crypto_shash_update(desc
, auth
->password_mutual
,
380 strlen(auth
->password_mutual
));
382 pr_err("crypto_shash_update() failed for"
383 " password_mutual\n");
387 * Convert received challenge to binary hex.
389 ret
= crypto_shash_finup(desc
, challenge_binhex
, challenge_len
,
392 pr_err("crypto_shash_finup() failed for ma challenge\n");
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 bin2hex(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
);
414 crypto_free_shash(tfm
);
416 kfree(challenge_binhex
);
420 static int chap_got_response(
421 struct iscsi_conn
*conn
,
422 struct iscsi_node_auth
*auth
,
425 unsigned int *nr_out_len
)
427 struct iscsi_chap
*chap
= conn
->auth_protocol
;
429 switch (chap
->digest_type
) {
430 case CHAP_DIGEST_MD5
:
431 if (chap_server_compute_md5(conn
, auth
, nr_in_ptr
,
432 nr_out_ptr
, nr_out_len
) < 0)
436 pr_err("Unknown CHAP digest type %d!\n",
443 struct iscsi_conn
*conn
,
444 struct iscsi_node_auth
*auth
,
450 struct iscsi_chap
*chap
= conn
->auth_protocol
;
453 chap
= chap_server_open(conn
, auth
, in_text
, out_text
, out_len
);
456 chap
->chap_state
= CHAP_STAGE_SERVER_AIC
;
458 } else if (chap
->chap_state
== CHAP_STAGE_SERVER_AIC
) {
459 convert_null_to_semi(in_text
, *in_len
);
460 if (chap_got_response(conn
, auth
, in_text
, out_text
,
465 if (auth
->authenticate_target
)
466 chap
->chap_state
= CHAP_STAGE_SERVER_NR
;