GNU Linux-libre 4.9.314-gnu1
[releases.git] / drivers / target / iscsi / iscsi_target_auth.c
1 /*******************************************************************************
2  * This file houses the main functions for the iSCSI CHAP support
3  *
4  * (c) Copyright 2007-2013 Datera, Inc.
5  *
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
19 #include <crypto/hash.h>
20 #include <linux/kernel.h>
21 #include <linux/string.h>
22 #include <linux/err.h>
23 #include <linux/scatterlist.h>
24
25 #include <target/iscsi/iscsi_target_core.h>
26 #include "iscsi_target_nego.h"
27 #include "iscsi_target_auth.h"
28
29 static void chap_gen_challenge(
30         struct iscsi_conn *conn,
31         int caller,
32         char *c_str,
33         unsigned int *c_len)
34 {
35         unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1];
36         struct iscsi_chap *chap = conn->auth_protocol;
37
38         memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1);
39
40         get_random_bytes(chap->challenge, CHAP_CHALLENGE_LENGTH);
41         bin2hex(challenge_asciihex, chap->challenge,
42                                 CHAP_CHALLENGE_LENGTH);
43         /*
44          * Set CHAP_C, and copy the generated challenge into c_str.
45          */
46         *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex);
47         *c_len += 1;
48
49         pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client",
50                         challenge_asciihex);
51 }
52
53 static int chap_check_algorithm(const char *a_str)
54 {
55         char *tmp, *orig, *token;
56
57         tmp = kstrdup(a_str, GFP_KERNEL);
58         if (!tmp) {
59                 pr_err("Memory allocation failed for CHAP_A temporary buffer\n");
60                 return CHAP_DIGEST_UNKNOWN;
61         }
62         orig = tmp;
63
64         token = strsep(&tmp, "=");
65         if (!token)
66                 goto out;
67
68         if (strcmp(token, "CHAP_A")) {
69                 pr_err("Unable to locate CHAP_A key\n");
70                 goto out;
71         }
72         while (token) {
73                 token = strsep(&tmp, ",");
74                 if (!token)
75                         goto out;
76
77                 if (!strcmp(token, "5")) {
78                         pr_debug("Selected MD5 Algorithm\n");
79                         kfree(orig);
80                         return CHAP_DIGEST_MD5;
81                 }
82         }
83 out:
84         kfree(orig);
85         return CHAP_DIGEST_UNKNOWN;
86 }
87
88 static struct iscsi_chap *chap_server_open(
89         struct iscsi_conn *conn,
90         struct iscsi_node_auth *auth,
91         const char *a_str,
92         char *aic_str,
93         unsigned int *aic_len)
94 {
95         int ret;
96         struct iscsi_chap *chap;
97
98         if (!(auth->naf_flags & NAF_USERID_SET) ||
99             !(auth->naf_flags & NAF_PASSWORD_SET)) {
100                 pr_err("CHAP user or password not set for"
101                                 " Initiator ACL\n");
102                 return NULL;
103         }
104
105         conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL);
106         if (!conn->auth_protocol)
107                 return NULL;
108
109         chap = conn->auth_protocol;
110         ret = chap_check_algorithm(a_str);
111         switch (ret) {
112         case CHAP_DIGEST_MD5:
113                 pr_debug("[server] Got CHAP_A=5\n");
114                 /*
115                  * Send back CHAP_A set to MD5.
116                 */
117                 *aic_len = sprintf(aic_str, "CHAP_A=5");
118                 *aic_len += 1;
119                 chap->digest_type = CHAP_DIGEST_MD5;
120                 pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type);
121                 break;
122         case CHAP_DIGEST_UNKNOWN:
123         default:
124                 pr_err("Unsupported CHAP_A value\n");
125                 return NULL;
126         }
127
128         /*
129          * Set Identifier.
130          */
131         chap->id = conn->tpg->tpg_chap_id++;
132         *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id);
133         *aic_len += 1;
134         pr_debug("[server] Sending CHAP_I=%d\n", chap->id);
135         /*
136          * Generate Challenge.
137          */
138         chap_gen_challenge(conn, 1, aic_str, aic_len);
139
140         return chap;
141 }
142
143 static void chap_close(struct iscsi_conn *conn)
144 {
145         kfree(conn->auth_protocol);
146         conn->auth_protocol = NULL;
147 }
148
149 static int chap_server_compute_md5(
150         struct iscsi_conn *conn,
151         struct iscsi_node_auth *auth,
152         char *nr_in_ptr,
153         char *nr_out_ptr,
154         unsigned int *nr_out_len)
155 {
156         unsigned long id;
157         unsigned char id_as_uchar;
158         unsigned char digest[MD5_SIGNATURE_SIZE];
159         unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2];
160         unsigned char identifier[10], *challenge = NULL;
161         unsigned char *challenge_binhex = NULL;
162         unsigned char client_digest[MD5_SIGNATURE_SIZE];
163         unsigned char server_digest[MD5_SIGNATURE_SIZE];
164         unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH];
165         size_t compare_len;
166         struct iscsi_chap *chap = conn->auth_protocol;
167         struct crypto_shash *tfm = NULL;
168         struct shash_desc *desc = NULL;
169         int auth_ret = -1, ret, challenge_len;
170
171         memset(identifier, 0, 10);
172         memset(chap_n, 0, MAX_CHAP_N_SIZE);
173         memset(chap_r, 0, MAX_RESPONSE_LENGTH);
174         memset(digest, 0, MD5_SIGNATURE_SIZE);
175         memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2);
176         memset(client_digest, 0, MD5_SIGNATURE_SIZE);
177         memset(server_digest, 0, MD5_SIGNATURE_SIZE);
178
179         challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
180         if (!challenge) {
181                 pr_err("Unable to allocate challenge buffer\n");
182                 goto out;
183         }
184
185         challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL);
186         if (!challenge_binhex) {
187                 pr_err("Unable to allocate challenge_binhex buffer\n");
188                 goto out;
189         }
190         /*
191          * Extract CHAP_N.
192          */
193         if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n,
194                                 &type) < 0) {
195                 pr_err("Could not find CHAP_N.\n");
196                 goto out;
197         }
198         if (type == HEX) {
199                 pr_err("Could not find CHAP_N.\n");
200                 goto out;
201         }
202
203         /* Include the terminating NULL in the compare */
204         compare_len = strlen(auth->userid) + 1;
205         if (strncmp(chap_n, auth->userid, compare_len) != 0) {
206                 pr_err("CHAP_N values do not match!\n");
207                 goto out;
208         }
209         pr_debug("[server] Got CHAP_N=%s\n", chap_n);
210         /*
211          * Extract CHAP_R.
212          */
213         if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r,
214                                 &type) < 0) {
215                 pr_err("Could not find CHAP_R.\n");
216                 goto out;
217         }
218         if (type != HEX) {
219                 pr_err("Could not find CHAP_R.\n");
220                 goto out;
221         }
222         if (strlen(chap_r) != MD5_SIGNATURE_SIZE * 2) {
223                 pr_err("Malformed CHAP_R\n");
224                 goto out;
225         }
226         if (hex2bin(client_digest, chap_r, MD5_SIGNATURE_SIZE) < 0) {
227                 pr_err("Malformed CHAP_R\n");
228                 goto out;
229         }
230
231         pr_debug("[server] Got CHAP_R=%s\n", chap_r);
232
233         tfm = crypto_alloc_shash("md5", 0, 0);
234         if (IS_ERR(tfm)) {
235                 tfm = NULL;
236                 pr_err("Unable to allocate struct crypto_shash\n");
237                 goto out;
238         }
239
240         desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL);
241         if (!desc) {
242                 pr_err("Unable to allocate struct shash_desc\n");
243                 goto out;
244         }
245
246         desc->tfm = tfm;
247         desc->flags = 0;
248
249         ret = crypto_shash_init(desc);
250         if (ret < 0) {
251                 pr_err("crypto_shash_init() failed\n");
252                 goto out;
253         }
254
255         ret = crypto_shash_update(desc, &chap->id, 1);
256         if (ret < 0) {
257                 pr_err("crypto_shash_update() failed for id\n");
258                 goto out;
259         }
260
261         ret = crypto_shash_update(desc, (char *)&auth->password,
262                                   strlen(auth->password));
263         if (ret < 0) {
264                 pr_err("crypto_shash_update() failed for password\n");
265                 goto out;
266         }
267
268         ret = crypto_shash_finup(desc, chap->challenge,
269                                  CHAP_CHALLENGE_LENGTH, server_digest);
270         if (ret < 0) {
271                 pr_err("crypto_shash_finup() failed for challenge\n");
272                 goto out;
273         }
274
275         bin2hex(response, server_digest, MD5_SIGNATURE_SIZE);
276         pr_debug("[server] MD5 Server Digest: %s\n", response);
277
278         if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) {
279                 pr_debug("[server] MD5 Digests do not match!\n\n");
280                 goto out;
281         } else
282                 pr_debug("[server] MD5 Digests match, CHAP connection"
283                                 " successful.\n\n");
284         /*
285          * One way authentication has succeeded, return now if mutual
286          * authentication is not enabled.
287          */
288         if (!auth->authenticate_target) {
289                 auth_ret = 0;
290                 goto out;
291         }
292         /*
293          * Get CHAP_I.
294          */
295         if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) {
296                 pr_err("Could not find CHAP_I.\n");
297                 goto out;
298         }
299
300         if (type == HEX)
301                 ret = kstrtoul(&identifier[2], 0, &id);
302         else
303                 ret = kstrtoul(identifier, 0, &id);
304
305         if (ret < 0) {
306                 pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret);
307                 goto out;
308         }
309         if (id > 255) {
310                 pr_err("chap identifier: %lu greater than 255\n", id);
311                 goto out;
312         }
313         /*
314          * RFC 1994 says Identifier is no more than octet (8 bits).
315          */
316         pr_debug("[server] Got CHAP_I=%lu\n", id);
317         /*
318          * Get CHAP_C.
319          */
320         if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN,
321                         challenge, &type) < 0) {
322                 pr_err("Could not find CHAP_C.\n");
323                 goto out;
324         }
325
326         if (type != HEX) {
327                 pr_err("Could not find CHAP_C.\n");
328                 goto out;
329         }
330         challenge_len = DIV_ROUND_UP(strlen(challenge), 2);
331         if (!challenge_len) {
332                 pr_err("Unable to convert incoming challenge\n");
333                 goto out;
334         }
335         if (challenge_len > 1024) {
336                 pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n");
337                 goto out;
338         }
339         if (hex2bin(challenge_binhex, challenge, challenge_len) < 0) {
340                 pr_err("Malformed CHAP_C\n");
341                 goto out;
342         }
343         pr_debug("[server] Got CHAP_C=%s\n", challenge);
344         /*
345          * During mutual authentication, the CHAP_C generated by the
346          * initiator must not match the original CHAP_C generated by
347          * the target.
348          */
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;
353         }
354         /*
355          * Generate CHAP_N and CHAP_R for mutual authentication.
356          */
357         ret = crypto_shash_init(desc);
358         if (ret < 0) {
359                 pr_err("crypto_shash_init() failed\n");
360                 goto out;
361         }
362
363         /* To handle both endiannesses */
364         id_as_uchar = id;
365         ret = crypto_shash_update(desc, &id_as_uchar, 1);
366         if (ret < 0) {
367                 pr_err("crypto_shash_update() failed for id\n");
368                 goto out;
369         }
370
371         ret = crypto_shash_update(desc, auth->password_mutual,
372                                   strlen(auth->password_mutual));
373         if (ret < 0) {
374                 pr_err("crypto_shash_update() failed for"
375                                 " password_mutual\n");
376                 goto out;
377         }
378         /*
379          * Convert received challenge to binary hex.
380          */
381         ret = crypto_shash_finup(desc, challenge_binhex, challenge_len,
382                                  digest);
383         if (ret < 0) {
384                 pr_err("crypto_shash_finup() failed for ma challenge\n");
385                 goto out;
386         }
387
388         /*
389          * Generate CHAP_N and CHAP_R.
390          */
391         *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual);
392         *nr_out_len += 1;
393         pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual);
394         /*
395          * Convert response from binary hex to ascii hext.
396          */
397         bin2hex(response, digest, MD5_SIGNATURE_SIZE);
398         *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s",
399                         response);
400         *nr_out_len += 1;
401         pr_debug("[server] Sending CHAP_R=0x%s\n", response);
402         auth_ret = 0;
403 out:
404         kzfree(desc);
405         if (tfm)
406                 crypto_free_shash(tfm);
407         kfree(challenge);
408         kfree(challenge_binhex);
409         return auth_ret;
410 }
411
412 static int chap_got_response(
413         struct iscsi_conn *conn,
414         struct iscsi_node_auth *auth,
415         char *nr_in_ptr,
416         char *nr_out_ptr,
417         unsigned int *nr_out_len)
418 {
419         struct iscsi_chap *chap = conn->auth_protocol;
420
421         switch (chap->digest_type) {
422         case CHAP_DIGEST_MD5:
423                 if (chap_server_compute_md5(conn, auth, nr_in_ptr,
424                                 nr_out_ptr, nr_out_len) < 0)
425                         return -1;
426                 return 0;
427         default:
428                 pr_err("Unknown CHAP digest type %d!\n",
429                                 chap->digest_type);
430                 return -1;
431         }
432 }
433
434 u32 chap_main_loop(
435         struct iscsi_conn *conn,
436         struct iscsi_node_auth *auth,
437         char *in_text,
438         char *out_text,
439         int *in_len,
440         int *out_len)
441 {
442         struct iscsi_chap *chap = conn->auth_protocol;
443
444         if (!chap) {
445                 chap = chap_server_open(conn, auth, in_text, out_text, out_len);
446                 if (!chap)
447                         return 2;
448                 chap->chap_state = CHAP_STAGE_SERVER_AIC;
449                 return 0;
450         } else if (chap->chap_state == CHAP_STAGE_SERVER_AIC) {
451                 convert_null_to_semi(in_text, *in_len);
452                 if (chap_got_response(conn, auth, in_text, out_text,
453                                 out_len) < 0) {
454                         chap_close(conn);
455                         return 2;
456                 }
457                 if (auth->authenticate_target)
458                         chap->chap_state = CHAP_STAGE_SERVER_NR;
459                 else
460                         *out_len = 0;
461                 chap_close(conn);
462                 return 1;
463         }
464
465         return 2;
466 }