blob: f06e74ea10d389fcb70f62ea1e16faaad623faae [file] [log] [blame]
Nicholas Bellingere48354c2011-07-23 06:43:04 +00001/*******************************************************************************
2 * This file houses the main functions for the iSCSI CHAP support
3 *
Nicholas Bellinger4c762512013-09-05 15:29:12 -07004 * (c) Copyright 2007-2013 Datera, Inc.
Nicholas Bellingere48354c2011-07-23 06:43:04 +00005 *
6 * Author: Nicholas A. Bellinger <nab@linux-iscsi.org>
7 *
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.
12 *
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 ******************************************************************************/
18
Herbert Xu69110e32016-01-24 21:19:52 +080019#include <crypto/hash.h>
Andy Shevchenkof2b56af2011-09-30 14:39:54 +030020#include <linux/kernel.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000021#include <linux/string.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000022#include <linux/err.h>
23#include <linux/scatterlist.h>
24
Sagi Grimberg67f091f2015-01-07 14:57:31 +020025#include <target/iscsi/iscsi_target_core.h>
Nicholas Bellingere48354c2011-07-23 06:43:04 +000026#include "iscsi_target_nego.h"
27#include "iscsi_target_auth.h"
28
Nicholas Bellingere48354c2011-07-23 06:43:04 +000029static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len)
30{
31 int i;
32
33 for (i = 0; i < src_len; i++) {
34 sprintf(&dst[i*2], "%02x", (int) src[i] & 0xff);
35 }
36}
37
Nicholas Bellingere48354c2011-07-23 06:43:04 +000038static void chap_gen_challenge(
39 struct iscsi_conn *conn,
40 int caller,
41 char *c_str,
42 unsigned int *c_len)
43{
44 unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
Jörn Engel8359cf42011-11-24 02:05:51 +010045 struct iscsi_chap *chap = conn->auth_protocol;
Nicholas Bellingere48354c2011-07-23 06:43:04 +000046
47 memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
48
Andy Grover98e2eeb2013-03-04 13:52:07 -080049 get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH);
Nicholas Bellingere48354c2011-07-23 06:43:04 +000050 chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge,
51 CHAP_CHALLENGE_LENGTH);
52 /*
53 * Set CHAP_C, and copy the generated challenge into c_str.
54 */
55 *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
56 *c_len += 1;
57
58 pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
59 challenge_asciihex);
60}
61
Tejas Vaykole31607232014-05-30 11:13:47 +053062static int chap_check_algorithm(const char *a_str)
63{
64 char *tmp, *orig, *token;
65
66 tmp = kstrdup(a_str, GFP_KERNEL);
67 if (!tmp) {
68 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
69 return CHAP_DIGEST_UNKNOWN;
70 }
71 orig = tmp;
72
73 token = strsep(&tmp, "=");
74 if (!token)
75 goto out;
76
77 if (strcmp(token, "CHAP_A")) {
78 pr_err("Unable to locate CHAP_A key\n");
79 goto out;
80 }
81 while (token) {
82 token = strsep(&tmp, ",");
83 if (!token)
84 goto out;
85
86 if (!strncmp(token, "5", 1)) {
87 pr_debug("Selected MD5 Algorithm\n");
88 kfree(orig);
89 return CHAP_DIGEST_MD5;
90 }
91 }
92out:
93 kfree(orig);
94 return CHAP_DIGEST_UNKNOWN;
95}
Nicholas Bellingere48354c2011-07-23 06:43:04 +000096
97static struct iscsi_chap *chap_server_open(
98 struct iscsi_conn *conn,
99 struct iscsi_node_auth *auth,
100 const char *a_str,
101 char *aic_str,
102 unsigned int *aic_len)
103{
Tejas Vaykole31607232014-05-30 11:13:47 +0530104 int ret;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000105 struct iscsi_chap *chap;
106
107 if (!(auth->naf_flags & NAF_USERID_SET) ||
108 !(auth->naf_flags & NAF_PASSWORD_SET)) {
109 pr_err("CHAP user or password not set for"
110 " Initiator ACL\n");
111 return NULL;
112 }
113
114 conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
115 if (!conn->auth_protocol)
116 return NULL;
117
Jörn Engel8359cf42011-11-24 02:05:51 +0100118 chap = conn->auth_protocol;
Tejas Vaykole31607232014-05-30 11:13:47 +0530119 ret = chap_check_algorithm(a_str);
120 switch (ret) {
121 case CHAP_DIGEST_MD5:
122 pr_debug("[server] Got CHAP_A=5\n");
123 /*
124 * Send back CHAP_A set to MD5.
125 */
126 *aic_len = sprintf(aic_str, "CHAP_A=5");
127 *aic_len += 1;
128 chap->digest_type = CHAP_DIGEST_MD5;
129 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
130 break;
131 case CHAP_DIGEST_UNKNOWN:
132 default:
133 pr_err("Unsupported CHAP_A value\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000134 return NULL;
135 }
Tejas Vaykole31607232014-05-30 11:13:47 +0530136
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000137 /*
138 * Set Identifier.
139 */
Andy Grover60bfcf82013-10-09 11:05:58 -0700140 chap->id = conn->tpg->tpg_chap_id++;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000141 *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
142 *aic_len += 1;
143 pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
144 /*
145 * Generate Challenge.
146 */
147 chap_gen_challenge(conn, 1, aic_str, aic_len);
148
149 return chap;
150}
151
152static void chap_close(struct iscsi_conn *conn)
153{
154 kfree(conn->auth_protocol);
155 conn->auth_protocol = NULL;
156}
157
158static int chap_server_compute_md5(
159 struct iscsi_conn *conn,
160 struct iscsi_node_auth *auth,
161 char *nr_in_ptr,
162 char *nr_out_ptr,
163 unsigned int *nr_out_len)
164{
Nicholas Bellingerbc704fb2011-11-28 01:02:07 -0800165 unsigned long id;
Andy Grover7ac9ad12013-03-04 13:52:09 -0800166 unsigned char id_as_uchar;
Nicholas Bellingerbc704fb2011-11-28 01:02:07 -0800167 unsigned char digest[MD5_SIGNATURE_SIZE];
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000168 unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
169 unsigned char identifier[10], *challenge = NULL;
170 unsigned char *challenge_binhex = NULL;
171 unsigned char client_digest[MD5_SIGNATURE_SIZE];
172 unsigned char server_digest[MD5_SIGNATURE_SIZE];
173 unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
Eric Seppanen86784c62013-11-20 14:19:52 -0800174 size_t compare_len;
Jörn Engel8359cf42011-11-24 02:05:51 +0100175 struct iscsi_chap *chap = conn->auth_protocol;
Herbert Xu69110e32016-01-24 21:19:52 +0800176 struct crypto_shash *tfm = NULL;
177 struct shash_desc *desc = NULL;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000178 int auth_ret = -1, ret, challenge_len;
179
180 memset(identifier, 0, 10);
181 memset(chap_n, 0, MAX_CHAP_N_SIZE);
182 memset(chap_r, 0, MAX_RESPONSE_LENGTH);
183 memset(digest, 0, MD5_SIGNATURE_SIZE);
184 memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
185 memset(client_digest, 0, MD5_SIGNATURE_SIZE);
186 memset(server_digest, 0, MD5_SIGNATURE_SIZE);
187
188 challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
189 if (!challenge) {
190 pr_err("Unable to allocate challenge buffer\n");
191 goto out;
192 }
193
194 challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
195 if (!challenge_binhex) {
196 pr_err("Unable to allocate challenge_binhex buffer\n");
197 goto out;
198 }
199 /*
200 * Extract CHAP_N.
201 */
202 if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
203 &type) < 0) {
204 pr_err("Could not find CHAP_N.\n");
205 goto out;
206 }
207 if (type == HEX) {
208 pr_err("Could not find CHAP_N.\n");
209 goto out;
210 }
211
Eric Seppanen86784c62013-11-20 14:19:52 -0800212 /* Include the terminating NULL in the compare */
213 compare_len = strlen(auth->userid) + 1;
214 if (strncmp(chap_n, auth->userid, compare_len) != 0) {
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000215 pr_err("CHAP_N values do not match!\n");
216 goto out;
217 }
218 pr_debug("[server] Got CHAP_N=%s\n", chap_n);
219 /*
220 * Extract CHAP_R.
221 */
222 if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
223 &type) < 0) {
224 pr_err("Could not find CHAP_R.\n");
225 goto out;
226 }
227 if (type != HEX) {
228 pr_err("Could not find CHAP_R.\n");
229 goto out;
230 }
Vincent Pelletier5eeb3972018-09-09 04:09:26 +0000231 if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
232 pr_err("Malformed CHAP_R\n");
233 goto out;
234 }
235 if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
236 pr_err("Malformed CHAP_R\n");
237 goto out;
238 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000239
240 pr_debug("[server] Got CHAP_R=%s\n", chap_r);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000241
Herbert Xu69110e32016-01-24 21:19:52 +0800242 tfm = crypto_alloc_shash("md5", 0, 0);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000243 if (IS_ERR(tfm)) {
Herbert Xu69110e32016-01-24 21:19:52 +0800244 tfm = NULL;
245 pr_err("Unable to allocate struct crypto_shash\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000246 goto out;
247 }
248
Herbert Xu69110e32016-01-24 21:19:52 +0800249 desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
250 if (!desc) {
251 pr_err("Unable to allocate struct shash_desc\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000252 goto out;
253 }
254
Herbert Xu69110e32016-01-24 21:19:52 +0800255 desc->tfm = tfm;
256 desc->flags = 0;
257
258 ret = crypto_shash_init(desc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000259 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800260 pr_err("crypto_shash_init() failed\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000261 goto out;
262 }
263
Herbert Xu69110e32016-01-24 21:19:52 +0800264 ret = crypto_shash_update(desc, &chap->id, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000265 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800266 pr_err("crypto_shash_update() failed for id\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000267 goto out;
268 }
269
Herbert Xu69110e32016-01-24 21:19:52 +0800270 ret = crypto_shash_update(desc, (char *)&auth->password,
271 strlen(auth->password));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000272 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800273 pr_err("crypto_shash_update() failed for password\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000274 goto out;
275 }
Herbert Xu69110e32016-01-24 21:19:52 +0800276
277 ret = crypto_shash_finup(desc, chap->challenge,
278 CHAP_CHALLENGE_LENGTH, server_digest);
279 if (ret < 0) {
280 pr_err("crypto_shash_finup() failed for challenge\n");
281 goto out;
282 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000283
284 chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE);
285 pr_debug("[server] MD5 Server Digest: %s\n", response);
286
287 if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
288 pr_debug("[server] MD5 Digests do not match!\n\n");
289 goto out;
290 } else
Masanari Iidac01e0152016-04-20 00:27:33 +0900291 pr_debug("[server] MD5 Digests match, CHAP connection"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000292 " successful.\n\n");
293 /*
294 * One way authentication has succeeded, return now if mutual
295 * authentication is not enabled.
296 */
297 if (!auth->authenticate_target) {
Herbert Xu69110e32016-01-24 21:19:52 +0800298 auth_ret = 0;
299 goto out;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000300 }
301 /*
302 * Get CHAP_I.
303 */
304 if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
305 pr_err("Could not find CHAP_I.\n");
306 goto out;
307 }
308
309 if (type == HEX)
Nicholas Bellingerb06eef62014-06-13 04:05:16 +0000310 ret = kstrtoul(&identifier[2], 0, &id);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000311 else
Nicholas Bellingerb06eef62014-06-13 04:05:16 +0000312 ret = kstrtoul(identifier, 0, &id);
313
314 if (ret < 0) {
315 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
316 goto out;
317 }
Nicholas Bellingerbc704fb2011-11-28 01:02:07 -0800318 if (id > 255) {
319 pr_err("chap identifier: %lu greater than 255\n", id);
320 goto out;
321 }
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000322 /*
323 * RFC 1994 says Identifier is no more than octet (8 bits).
324 */
Nicholas Bellingerbc704fb2011-11-28 01:02:07 -0800325 pr_debug("[server] Got CHAP_I=%lu\n", id);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000326 /*
327 * Get CHAP_C.
328 */
329 if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
330 challenge, &type) < 0) {
331 pr_err("Could not find CHAP_C.\n");
332 goto out;
333 }
334
335 if (type != HEX) {
336 pr_err("Could not find CHAP_C.\n");
337 goto out;
338 }
Vincent Pelletier5eeb3972018-09-09 04:09:26 +0000339 challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000340 if (!challenge_len) {
341 pr_err("Unable to convert incoming challenge\n");
342 goto out;
343 }
Nicholas Bellingere4fae232014-06-13 04:28:31 +0000344 if (challenge_len > 1024) {
345 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
346 goto out;
347 }
Vincent Pelletier5eeb3972018-09-09 04:09:26 +0000348 if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
349 pr_err("Malformed CHAP_C\n");
350 goto out;
351 }
352 pr_debug("[server] Got CHAP_C=%s\n", challenge);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000353 /*
Nicholas Bellinger1d2b60a2014-06-05 18:08:57 -0700354 * During mutual authentication, the CHAP_C generated by the
355 * initiator must not match the original CHAP_C generated by
356 * the target.
357 */
358 if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) {
359 pr_err("initiator CHAP_C matches target CHAP_C, failing"
360 " login attempt\n");
361 goto out;
362 }
363 /*
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000364 * Generate CHAP_N and CHAP_R for mutual authentication.
365 */
Herbert Xu69110e32016-01-24 21:19:52 +0800366 ret = crypto_shash_init(desc);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000367 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800368 pr_err("crypto_shash_init() failed\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000369 goto out;
370 }
371
Andy Grover7ac9ad12013-03-04 13:52:09 -0800372 /* To handle both endiannesses */
373 id_as_uchar = id;
Herbert Xu69110e32016-01-24 21:19:52 +0800374 ret = crypto_shash_update(desc, &id_as_uchar, 1);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000375 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800376 pr_err("crypto_shash_update() failed for id\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000377 goto out;
378 }
379
Herbert Xu69110e32016-01-24 21:19:52 +0800380 ret = crypto_shash_update(desc, auth->password_mutual,
381 strlen(auth->password_mutual));
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000382 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800383 pr_err("crypto_shash_update() failed for"
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000384 " password_mutual\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000385 goto out;
386 }
387 /*
388 * Convert received challenge to binary hex.
389 */
Herbert Xu69110e32016-01-24 21:19:52 +0800390 ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
391 digest);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000392 if (ret < 0) {
Herbert Xu69110e32016-01-24 21:19:52 +0800393 pr_err("crypto_shash_finup() failed for ma challenge\n");
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000394 goto out;
395 }
396
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000397 /*
398 * Generate CHAP_N and CHAP_R.
399 */
400 *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
401 *nr_out_len += 1;
402 pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
403 /*
404 * Convert response from binary hex to ascii hext.
405 */
406 chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE);
407 *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
408 response);
409 *nr_out_len += 1;
410 pr_debug("[server] Sending CHAP_R=0x%s\n", response);
411 auth_ret = 0;
412out:
Herbert Xu69110e32016-01-24 21:19:52 +0800413 kzfree(desc);
David Disseldorpfffc0fc2017-12-13 18:22:30 +0100414 if (tfm)
415 crypto_free_shash(tfm);
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000416 kfree(challenge);
417 kfree(challenge_binhex);
418 return auth_ret;
419}
420
421static int chap_got_response(
422 struct iscsi_conn *conn,
423 struct iscsi_node_auth *auth,
424 char *nr_in_ptr,
425 char *nr_out_ptr,
426 unsigned int *nr_out_len)
427{
Jörn Engel8359cf42011-11-24 02:05:51 +0100428 struct iscsi_chap *chap = conn->auth_protocol;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000429
430 switch (chap->digest_type) {
431 case CHAP_DIGEST_MD5:
432 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
433 nr_out_ptr, nr_out_len) < 0)
434 return -1;
435 return 0;
436 default:
437 pr_err("Unknown CHAP digest type %d!\n",
438 chap->digest_type);
439 return -1;
440 }
441}
442
443u32 chap_main_loop(
444 struct iscsi_conn *conn,
445 struct iscsi_node_auth *auth,
446 char *in_text,
447 char *out_text,
448 int *in_len,
449 int *out_len)
450{
Jörn Engel8359cf42011-11-24 02:05:51 +0100451 struct iscsi_chap *chap = conn->auth_protocol;
Nicholas Bellingere48354c2011-07-23 06:43:04 +0000452
453 if (!chap) {
454 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
455 if (!chap)
456 return 2;
457 chap->chap_state = CHAP_STAGE_SERVER_AIC;
458 return 0;
459 } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
460 convert_null_to_semi(in_text, *in_len);
461 if (chap_got_response(conn, auth, in_text, out_text,
462 out_len) < 0) {
463 chap_close(conn);
464 return 2;
465 }
466 if (auth->authenticate_target)
467 chap->chap_state = CHAP_STAGE_SERVER_NR;
468 else
469 *out_len = 0;
470 chap_close(conn);
471 return 1;
472 }
473
474 return 2;
475}