Linux 3.4.102
[linux/fpc-iii.git] / drivers / target / iscsi / iscsi_target_auth.c
blobcbb04f37ec3675b8459b213d82e500d3d7db1ff5
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);
36 if (rc < 0)
37 pr_debug("CHAP string contains non hex digit symbols\n");
39 dst[j] = '\0';
40 return j;
43 static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
45 int i;
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)
54 long r;
55 unsigned n;
57 while (length > 0) {
58 get_random_bytes(&r, sizeof(long));
59 r = r ^ (r >> 8);
60 r = r ^ (r >> 4);
61 n = r & 0x7;
63 get_random_bytes(&r, sizeof(long));
64 r = r ^ (r >> 8);
65 r = r ^ (r >> 5);
66 n = (n << 3) | (r & 0x7);
68 get_random_bytes(&r, sizeof(long));
69 r = r ^ (r >> 8);
70 r = r ^ (r >> 5);
71 n = (n << 2) | (r & 0x3);
73 *data++ = n;
74 length--;
78 static void chap_gen_challenge(
79 struct iscsi_conn *conn,
80 int caller,
81 char *c_str,
82 unsigned int *c_len)
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);
96 *c_len += 1;
98 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
99 challenge_asciihex);
103 static struct iscsi_chap *chap_server_open(
104 struct iscsi_conn *conn,
105 struct iscsi_node_auth *auth,
106 const char *a_str,
107 char *aic_str,
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"
115 " Initiator ACL\n");
116 return NULL;
119 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
120 if (!conn->auth_protocol)
121 return NULL;
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");
129 return NULL;
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");
136 *aic_len += 1;
137 chap->digest_type = CHAP_DIGEST_MD5;
138 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
140 * Set Identifier.
142 chap->id = ISCSI_TPG_C(conn)->tpg_chap_id++;
143 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
144 *aic_len += 1;
145 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
147 * Generate Challenge.
149 chap_gen_challenge(conn, 1, aic_str, aic_len);
151 return chap;
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,
163 char *nr_in_ptr,
164 char *nr_out_ptr,
165 unsigned int *nr_out_len)
167 char *endptr;
168 unsigned long id;
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 size_t compare_len;
178 struct iscsi_chap *chap = conn->auth_protocol;
179 struct crypto_hash *tfm;
180 struct hash_desc desc;
181 struct scatterlist sg;
182 int auth_ret = -1, ret, challenge_len;
184 memset(identifier, 0, 10);
185 memset(chap_n, 0, MAX_CHAP_N_SIZE);
186 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
187 memset(digest, 0, MD5_SIGNATURE_SIZE);
188 memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
189 memset(client_digest, 0, MD5_SIGNATURE_SIZE);
190 memset(server_digest, 0, MD5_SIGNATURE_SIZE);
192 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
193 if (!challenge) {
194 pr_err("Unable to allocate challenge buffer\n");
195 goto out;
198 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
199 if (!challenge_binhex) {
200 pr_err("Unable to allocate challenge_binhex buffer\n");
201 goto out;
204 * Extract CHAP_N.
206 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
207 &type) < 0) {
208 pr_err("Could not find CHAP_N.\n");
209 goto out;
211 if (type == HEX) {
212 pr_err("Could not find CHAP_N.\n");
213 goto out;
216 /* Include the terminating NULL in the compare */
217 compare_len = strlen(auth->userid) + 1;
218 if (strncmp(chap_n, auth->userid, compare_len) != 0) {
219 pr_err("CHAP_N values do not match!\n");
220 goto out;
222 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
224 * Extract CHAP_R.
226 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
227 &type) < 0) {
228 pr_err("Could not find CHAP_R.\n");
229 goto out;
231 if (type != HEX) {
232 pr_err("Could not find CHAP_R.\n");
233 goto out;
236 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
237 chap_string_to_hex(client_digest, chap_r, strlen(chap_r));
239 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
240 if (IS_ERR(tfm)) {
241 pr_err("Unable to allocate struct crypto_hash\n");
242 goto out;
244 desc.tfm = tfm;
245 desc.flags = 0;
247 ret = crypto_hash_init(&desc);
248 if (ret < 0) {
249 pr_err("crypto_hash_init() failed\n");
250 crypto_free_hash(tfm);
251 goto out;
254 sg_init_one(&sg, &chap->id, 1);
255 ret = crypto_hash_update(&desc, &sg, 1);
256 if (ret < 0) {
257 pr_err("crypto_hash_update() failed for id\n");
258 crypto_free_hash(tfm);
259 goto out;
262 sg_init_one(&sg, &auth->password, strlen(auth->password));
263 ret = crypto_hash_update(&desc, &sg, strlen(auth->password));
264 if (ret < 0) {
265 pr_err("crypto_hash_update() failed for password\n");
266 crypto_free_hash(tfm);
267 goto out;
270 sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH);
271 ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH);
272 if (ret < 0) {
273 pr_err("crypto_hash_update() failed for challenge\n");
274 crypto_free_hash(tfm);
275 goto out;
278 ret = crypto_hash_final(&desc, server_digest);
279 if (ret < 0) {
280 pr_err("crypto_hash_final() failed for server digest\n");
281 crypto_free_hash(tfm);
282 goto out;
284 crypto_free_hash(tfm);
286 chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
287 pr_debug("[server] MD5 Server Digest: %s\n", response);
289 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
290 pr_debug("[server] MD5 Digests do not match!\n\n");
291 goto out;
292 } else
293 pr_debug("[server] MD5 Digests match, CHAP connetication"
294 " successful.\n\n");
296 * One way authentication has succeeded, return now if mutual
297 * authentication is not enabled.
299 if (!auth->authenticate_target) {
300 kfree(challenge);
301 kfree(challenge_binhex);
302 return 0;
305 * Get CHAP_I.
307 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
308 pr_err("Could not find CHAP_I.\n");
309 goto out;
312 if (type == HEX)
313 id = simple_strtoul(&identifier[2], &endptr, 0);
314 else
315 id = simple_strtoul(identifier, &endptr, 0);
316 if (id > 255) {
317 pr_err("chap identifier: %lu greater than 255\n", id);
318 goto out;
321 * RFC 1994 says Identifier is no more than octet (8 bits).
323 pr_debug("[server] Got CHAP_I=%lu\n", id);
325 * Get CHAP_C.
327 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
328 challenge, &type) < 0) {
329 pr_err("Could not find CHAP_C.\n");
330 goto out;
333 if (type != HEX) {
334 pr_err("Could not find CHAP_C.\n");
335 goto out;
337 pr_debug("[server] Got CHAP_C=%s\n", challenge);
338 challenge_len = chap_string_to_hex(challenge_binhex, challenge,
339 strlen(challenge));
340 if (!challenge_len) {
341 pr_err("Unable to convert incoming challenge\n");
342 goto out;
345 * During mutual authentication, the CHAP_C generated by the
346 * initiator must not match the original CHAP_C generated by
347 * the target.
349 if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
350 pr_err("initiator CHAP_C matches target CHAP_C, failing"
351 " login attempt\n");
352 goto out;
355 * Generate CHAP_N and CHAP_R for mutual authentication.
357 tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC);
358 if (IS_ERR(tfm)) {
359 pr_err("Unable to allocate struct crypto_hash\n");
360 goto out;
362 desc.tfm = tfm;
363 desc.flags = 0;
365 ret = crypto_hash_init(&desc);
366 if (ret < 0) {
367 pr_err("crypto_hash_init() failed\n");
368 crypto_free_hash(tfm);
369 goto out;
372 /* To handle both endiannesses */
373 id_as_uchar = id;
374 sg_init_one(&sg, &id_as_uchar, 1);
375 ret = crypto_hash_update(&desc, &sg, 1);
376 if (ret < 0) {
377 pr_err("crypto_hash_update() failed for id\n");
378 crypto_free_hash(tfm);
379 goto out;
382 sg_init_one(&sg, auth->password_mutual,
383 strlen(auth->password_mutual));
384 ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual));
385 if (ret < 0) {
386 pr_err("crypto_hash_update() failed for"
387 " password_mutual\n");
388 crypto_free_hash(tfm);
389 goto out;
392 * Convert received challenge to binary hex.
394 sg_init_one(&sg, challenge_binhex, challenge_len);
395 ret = crypto_hash_update(&desc, &sg, challenge_len);
396 if (ret < 0) {
397 pr_err("crypto_hash_update() failed for ma challenge\n");
398 crypto_free_hash(tfm);
399 goto out;
402 ret = crypto_hash_final(&desc, digest);
403 if (ret < 0) {
404 pr_err("crypto_hash_final() failed for ma digest\n");
405 crypto_free_hash(tfm);
406 goto out;
408 crypto_free_hash(tfm);
410 * Generate CHAP_N and CHAP_R.
412 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
413 *nr_out_len += 1;
414 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
416 * Convert response from binary hex to ascii hext.
418 chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
419 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
420 response);
421 *nr_out_len += 1;
422 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
423 auth_ret = 0;
424 out:
425 kfree(challenge);
426 kfree(challenge_binhex);
427 return auth_ret;
430 static int chap_got_response(
431 struct iscsi_conn *conn,
432 struct iscsi_node_auth *auth,
433 char *nr_in_ptr,
434 char *nr_out_ptr,
435 unsigned int *nr_out_len)
437 struct iscsi_chap *chap = conn->auth_protocol;
439 switch (chap->digest_type) {
440 case CHAP_DIGEST_MD5:
441 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
442 nr_out_ptr, nr_out_len) < 0)
443 return -1;
444 return 0;
445 default:
446 pr_err("Unknown CHAP digest type %d!\n",
447 chap->digest_type);
448 return -1;
452 u32 chap_main_loop(
453 struct iscsi_conn *conn,
454 struct iscsi_node_auth *auth,
455 char *in_text,
456 char *out_text,
457 int *in_len,
458 int *out_len)
460 struct iscsi_chap *chap = conn->auth_protocol;
462 if (!chap) {
463 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
464 if (!chap)
465 return 2;
466 chap->chap_state = CHAP_STAGE_SERVER_AIC;
467 return 0;
468 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
469 convert_null_to_semi(in_text, *in_len);
470 if (chap_got_response(conn, auth, in_text, out_text,
471 out_len) < 0) {
472 chap_close(conn);
473 return 2;
475 if (auth->authenticate_target)
476 chap->chap_state = CHAP_STAGE_SERVER_NR;
477 else
478 *out_len = 0;
479 chap_close(conn);
480 return 1;
483 return 2;