Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * This file houses the main functions for the iSCSI CHAP support |
| 3 | * |
Nicholas Bellinger | 4c76251 | 2013-09-05 15:29:12 -0700 | [diff] [blame] | 4 | * (c) Copyright 2007-2013 Datera, Inc. |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 19 | #include <crypto/hash.h> |
Andy Shevchenko | f2b56af | 2011-09-30 14:39:54 +0300 | [diff] [blame] | 20 | #include <linux/kernel.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 21 | #include <linux/string.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 22 | #include <linux/err.h> |
Bart Van Assche | 8dcf07b | 2016-11-14 15:47:14 -0800 | [diff] [blame] | 23 | #include <linux/random.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 24 | #include <linux/scatterlist.h> |
Sagi Grimberg | 67f091f | 2015-01-07 14:57:31 +0200 | [diff] [blame] | 25 | #include <target/iscsi/iscsi_target_core.h> |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 26 | #include "iscsi_target_nego.h" |
| 27 | #include "iscsi_target_auth.h" |
| 28 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 29 | static int chap_string_to_hex(unsigned char *dst, unsigned char *src, int len) |
| 30 | { |
Nicholas Bellinger | ddca8f3 | 2011-12-06 05:24:19 +0000 | [diff] [blame] | 31 | int j = DIV_ROUND_UP(len, 2), rc; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 32 | |
Nicholas Bellinger | ddca8f3 | 2011-12-06 05:24:19 +0000 | [diff] [blame] | 33 | rc = hex2bin(dst, src, j); |
| 34 | if (rc < 0) |
| 35 | pr_debug("CHAP string contains non hex digit symbols\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 36 | |
| 37 | dst[j] = '\0'; |
| 38 | return j; |
| 39 | } |
| 40 | |
| 41 | static void chap_binaryhex_to_asciihex(char *dst, char *src, int src_len) |
| 42 | { |
| 43 | int i; |
| 44 | |
| 45 | for (i = 0; i < src_len; i++) { |
| 46 | sprintf(&dst[i*2], "%02x", (int) src[i] & 0xff); |
| 47 | } |
| 48 | } |
| 49 | |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 50 | static int chap_gen_challenge( |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 51 | struct iscsi_conn *conn, |
| 52 | int caller, |
| 53 | char *c_str, |
| 54 | unsigned int *c_len) |
| 55 | { |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 56 | int ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 57 | unsigned char challenge_asciihex[CHAP_CHALLENGE_LENGTH * 2 + 1]; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 58 | struct iscsi_chap *chap = conn->auth_protocol; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 59 | |
| 60 | memset(challenge_asciihex, 0, CHAP_CHALLENGE_LENGTH * 2 + 1); |
| 61 | |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 62 | ret = get_random_bytes_wait(chap->challenge, CHAP_CHALLENGE_LENGTH); |
| 63 | if (unlikely(ret)) |
| 64 | return ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 65 | chap_binaryhex_to_asciihex(challenge_asciihex, chap->challenge, |
| 66 | CHAP_CHALLENGE_LENGTH); |
| 67 | /* |
| 68 | * Set CHAP_C, and copy the generated challenge into c_str. |
| 69 | */ |
| 70 | *c_len += sprintf(c_str + *c_len, "CHAP_C=0x%s", challenge_asciihex); |
| 71 | *c_len += 1; |
| 72 | |
| 73 | pr_debug("[%s] Sending CHAP_C=0x%s\n\n", (caller) ? "server" : "client", |
| 74 | challenge_asciihex); |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 75 | return 0; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Tejas Vaykole | 3160723 | 2014-05-30 11:13:47 +0530 | [diff] [blame] | 78 | static int chap_check_algorithm(const char *a_str) |
| 79 | { |
| 80 | char *tmp, *orig, *token; |
| 81 | |
| 82 | tmp = kstrdup(a_str, GFP_KERNEL); |
| 83 | if (!tmp) { |
| 84 | pr_err("Memory allocation failed for CHAP_A temporary buffer\n"); |
| 85 | return CHAP_DIGEST_UNKNOWN; |
| 86 | } |
| 87 | orig = tmp; |
| 88 | |
| 89 | token = strsep(&tmp, "="); |
| 90 | if (!token) |
| 91 | goto out; |
| 92 | |
| 93 | if (strcmp(token, "CHAP_A")) { |
| 94 | pr_err("Unable to locate CHAP_A key\n"); |
| 95 | goto out; |
| 96 | } |
| 97 | while (token) { |
| 98 | token = strsep(&tmp, ","); |
| 99 | if (!token) |
| 100 | goto out; |
| 101 | |
| 102 | if (!strncmp(token, "5", 1)) { |
| 103 | pr_debug("Selected MD5 Algorithm\n"); |
| 104 | kfree(orig); |
| 105 | return CHAP_DIGEST_MD5; |
| 106 | } |
| 107 | } |
| 108 | out: |
| 109 | kfree(orig); |
| 110 | return CHAP_DIGEST_UNKNOWN; |
| 111 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 112 | |
| 113 | static struct iscsi_chap *chap_server_open( |
| 114 | struct iscsi_conn *conn, |
| 115 | struct iscsi_node_auth *auth, |
| 116 | const char *a_str, |
| 117 | char *aic_str, |
| 118 | unsigned int *aic_len) |
| 119 | { |
Tejas Vaykole | 3160723 | 2014-05-30 11:13:47 +0530 | [diff] [blame] | 120 | int ret; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 121 | struct iscsi_chap *chap; |
| 122 | |
| 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" |
| 126 | " Initiator ACL\n"); |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | conn->auth_protocol = kzalloc(sizeof(struct iscsi_chap), GFP_KERNEL); |
| 131 | if (!conn->auth_protocol) |
| 132 | return NULL; |
| 133 | |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 134 | chap = conn->auth_protocol; |
Tejas Vaykole | 3160723 | 2014-05-30 11:13:47 +0530 | [diff] [blame] | 135 | ret = chap_check_algorithm(a_str); |
| 136 | switch (ret) { |
| 137 | case CHAP_DIGEST_MD5: |
| 138 | pr_debug("[server] Got CHAP_A=5\n"); |
| 139 | /* |
| 140 | * Send back CHAP_A set to MD5. |
| 141 | */ |
| 142 | *aic_len = sprintf(aic_str, "CHAP_A=5"); |
| 143 | *aic_len += 1; |
| 144 | chap->digest_type = CHAP_DIGEST_MD5; |
| 145 | pr_debug("[server] Sending CHAP_A=%d\n", chap->digest_type); |
| 146 | break; |
| 147 | case CHAP_DIGEST_UNKNOWN: |
| 148 | default: |
| 149 | pr_err("Unsupported CHAP_A value\n"); |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 150 | kfree(conn->auth_protocol); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 151 | return NULL; |
| 152 | } |
Tejas Vaykole | 3160723 | 2014-05-30 11:13:47 +0530 | [diff] [blame] | 153 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 154 | /* |
| 155 | * Set Identifier. |
| 156 | */ |
Andy Grover | 60bfcf8 | 2013-10-09 11:05:58 -0700 | [diff] [blame] | 157 | chap->id = conn->tpg->tpg_chap_id++; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 158 | *aic_len += sprintf(aic_str + *aic_len, "CHAP_I=%d", chap->id); |
| 159 | *aic_len += 1; |
| 160 | pr_debug("[server] Sending CHAP_I=%d\n", chap->id); |
| 161 | /* |
| 162 | * Generate Challenge. |
| 163 | */ |
Jason A. Donenfeld | 6787ab8 | 2017-06-07 22:34:26 -0400 | [diff] [blame] | 164 | if (chap_gen_challenge(conn, 1, aic_str, aic_len) < 0) { |
| 165 | kfree(conn->auth_protocol); |
| 166 | return NULL; |
| 167 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 168 | |
| 169 | return chap; |
| 170 | } |
| 171 | |
| 172 | static void chap_close(struct iscsi_conn *conn) |
| 173 | { |
| 174 | kfree(conn->auth_protocol); |
| 175 | conn->auth_protocol = NULL; |
| 176 | } |
| 177 | |
| 178 | static int chap_server_compute_md5( |
| 179 | struct iscsi_conn *conn, |
| 180 | struct iscsi_node_auth *auth, |
| 181 | char *nr_in_ptr, |
| 182 | char *nr_out_ptr, |
| 183 | unsigned int *nr_out_len) |
| 184 | { |
Nicholas Bellinger | bc704fb | 2011-11-28 01:02:07 -0800 | [diff] [blame] | 185 | unsigned long id; |
Andy Grover | 7ac9ad1 | 2013-03-04 13:52:09 -0800 | [diff] [blame] | 186 | unsigned char id_as_uchar; |
Nicholas Bellinger | bc704fb | 2011-11-28 01:02:07 -0800 | [diff] [blame] | 187 | unsigned char digest[MD5_SIGNATURE_SIZE]; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 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]; |
Eric Seppanen | 86784c6 | 2013-11-20 14:19:52 -0800 | [diff] [blame] | 194 | size_t compare_len; |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 195 | struct iscsi_chap *chap = conn->auth_protocol; |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 196 | struct crypto_shash *tfm = NULL; |
| 197 | struct shash_desc *desc = NULL; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 198 | int auth_ret = -1, ret, challenge_len; |
| 199 | |
| 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); |
| 207 | |
| 208 | challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL); |
| 209 | if (!challenge) { |
| 210 | pr_err("Unable to allocate challenge buffer\n"); |
| 211 | goto out; |
| 212 | } |
| 213 | |
| 214 | challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL); |
| 215 | if (!challenge_binhex) { |
| 216 | pr_err("Unable to allocate challenge_binhex buffer\n"); |
| 217 | goto out; |
| 218 | } |
| 219 | /* |
| 220 | * Extract CHAP_N. |
| 221 | */ |
| 222 | if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n, |
| 223 | &type) < 0) { |
| 224 | pr_err("Could not find CHAP_N.\n"); |
| 225 | goto out; |
| 226 | } |
| 227 | if (type == HEX) { |
| 228 | pr_err("Could not find CHAP_N.\n"); |
| 229 | goto out; |
| 230 | } |
| 231 | |
Eric Seppanen | 86784c6 | 2013-11-20 14:19:52 -0800 | [diff] [blame] | 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) { |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 235 | pr_err("CHAP_N values do not match!\n"); |
| 236 | goto out; |
| 237 | } |
| 238 | pr_debug("[server] Got CHAP_N=%s\n", chap_n); |
| 239 | /* |
| 240 | * Extract CHAP_R. |
| 241 | */ |
| 242 | if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r, |
| 243 | &type) < 0) { |
| 244 | pr_err("Could not find CHAP_R.\n"); |
| 245 | goto out; |
| 246 | } |
| 247 | if (type != HEX) { |
| 248 | pr_err("Could not find CHAP_R.\n"); |
| 249 | goto out; |
| 250 | } |
| 251 | |
| 252 | pr_debug("[server] Got CHAP_R=%s\n", chap_r); |
| 253 | chap_string_to_hex(client_digest, chap_r, strlen(chap_r)); |
| 254 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 255 | tfm = crypto_alloc_shash("md5", 0, 0); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 256 | if (IS_ERR(tfm)) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 257 | tfm = NULL; |
| 258 | pr_err("Unable to allocate struct crypto_shash\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 259 | goto out; |
| 260 | } |
| 261 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 262 | desc = kmalloc(sizeof(*desc) + crypto_shash_descsize(tfm), GFP_KERNEL); |
| 263 | if (!desc) { |
| 264 | pr_err("Unable to allocate struct shash_desc\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 265 | goto out; |
| 266 | } |
| 267 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 268 | desc->tfm = tfm; |
| 269 | desc->flags = 0; |
| 270 | |
| 271 | ret = crypto_shash_init(desc); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 272 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 273 | pr_err("crypto_shash_init() failed\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 274 | goto out; |
| 275 | } |
| 276 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 277 | ret = crypto_shash_update(desc, &chap->id, 1); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 278 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 279 | pr_err("crypto_shash_update() failed for id\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 280 | goto out; |
| 281 | } |
| 282 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 283 | ret = crypto_shash_update(desc, (char *)&auth->password, |
| 284 | strlen(auth->password)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 285 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 286 | pr_err("crypto_shash_update() failed for password\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 287 | goto out; |
| 288 | } |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 289 | |
| 290 | ret = crypto_shash_finup(desc, chap->challenge, |
| 291 | CHAP_CHALLENGE_LENGTH, server_digest); |
| 292 | if (ret < 0) { |
| 293 | pr_err("crypto_shash_finup() failed for challenge\n"); |
| 294 | goto out; |
| 295 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 296 | |
| 297 | chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE); |
| 298 | pr_debug("[server] MD5 Server Digest: %s\n", response); |
| 299 | |
| 300 | if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) { |
| 301 | pr_debug("[server] MD5 Digests do not match!\n\n"); |
| 302 | goto out; |
| 303 | } else |
Masanari Iida | c01e015 | 2016-04-20 00:27:33 +0900 | [diff] [blame] | 304 | pr_debug("[server] MD5 Digests match, CHAP connection" |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 305 | " successful.\n\n"); |
| 306 | /* |
| 307 | * One way authentication has succeeded, return now if mutual |
| 308 | * authentication is not enabled. |
| 309 | */ |
| 310 | if (!auth->authenticate_target) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 311 | auth_ret = 0; |
| 312 | goto out; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 313 | } |
| 314 | /* |
| 315 | * Get CHAP_I. |
| 316 | */ |
| 317 | if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) { |
| 318 | pr_err("Could not find CHAP_I.\n"); |
| 319 | goto out; |
| 320 | } |
| 321 | |
| 322 | if (type == HEX) |
Nicholas Bellinger | b06eef6 | 2014-06-13 04:05:16 +0000 | [diff] [blame] | 323 | ret = kstrtoul(&identifier[2], 0, &id); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 324 | else |
Nicholas Bellinger | b06eef6 | 2014-06-13 04:05:16 +0000 | [diff] [blame] | 325 | ret = kstrtoul(identifier, 0, &id); |
| 326 | |
| 327 | if (ret < 0) { |
| 328 | pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret); |
| 329 | goto out; |
| 330 | } |
Nicholas Bellinger | bc704fb | 2011-11-28 01:02:07 -0800 | [diff] [blame] | 331 | if (id > 255) { |
| 332 | pr_err("chap identifier: %lu greater than 255\n", id); |
| 333 | goto out; |
| 334 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 335 | /* |
| 336 | * RFC 1994 says Identifier is no more than octet (8 bits). |
| 337 | */ |
Nicholas Bellinger | bc704fb | 2011-11-28 01:02:07 -0800 | [diff] [blame] | 338 | pr_debug("[server] Got CHAP_I=%lu\n", id); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 339 | /* |
| 340 | * Get CHAP_C. |
| 341 | */ |
| 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"); |
| 345 | goto out; |
| 346 | } |
| 347 | |
| 348 | if (type != HEX) { |
| 349 | pr_err("Could not find CHAP_C.\n"); |
| 350 | goto out; |
| 351 | } |
| 352 | pr_debug("[server] Got CHAP_C=%s\n", challenge); |
| 353 | challenge_len = chap_string_to_hex(challenge_binhex, challenge, |
| 354 | strlen(challenge)); |
| 355 | if (!challenge_len) { |
| 356 | pr_err("Unable to convert incoming challenge\n"); |
| 357 | goto out; |
| 358 | } |
Nicholas Bellinger | e4fae23 | 2014-06-13 04:28:31 +0000 | [diff] [blame] | 359 | if (challenge_len > 1024) { |
| 360 | pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n"); |
| 361 | goto out; |
| 362 | } |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 363 | /* |
Nicholas Bellinger | 1d2b60a | 2014-06-05 18:08:57 -0700 | [diff] [blame] | 364 | * During mutual authentication, the CHAP_C generated by the |
| 365 | * initiator must not match the original CHAP_C generated by |
| 366 | * the target. |
| 367 | */ |
| 368 | if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) { |
| 369 | pr_err("initiator CHAP_C matches target CHAP_C, failing" |
| 370 | " login attempt\n"); |
| 371 | goto out; |
| 372 | } |
| 373 | /* |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 374 | * Generate CHAP_N and CHAP_R for mutual authentication. |
| 375 | */ |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 376 | ret = crypto_shash_init(desc); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 377 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 378 | pr_err("crypto_shash_init() failed\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 379 | goto out; |
| 380 | } |
| 381 | |
Andy Grover | 7ac9ad1 | 2013-03-04 13:52:09 -0800 | [diff] [blame] | 382 | /* To handle both endiannesses */ |
| 383 | id_as_uchar = id; |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 384 | ret = crypto_shash_update(desc, &id_as_uchar, 1); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 385 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 386 | pr_err("crypto_shash_update() failed for id\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 387 | goto out; |
| 388 | } |
| 389 | |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 390 | ret = crypto_shash_update(desc, auth->password_mutual, |
| 391 | strlen(auth->password_mutual)); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 392 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 393 | pr_err("crypto_shash_update() failed for" |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 394 | " password_mutual\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 395 | goto out; |
| 396 | } |
| 397 | /* |
| 398 | * Convert received challenge to binary hex. |
| 399 | */ |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 400 | ret = crypto_shash_finup(desc, challenge_binhex, challenge_len, |
| 401 | digest); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 402 | if (ret < 0) { |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 403 | pr_err("crypto_shash_finup() failed for ma challenge\n"); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 404 | goto out; |
| 405 | } |
| 406 | |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 407 | /* |
| 408 | * Generate CHAP_N and CHAP_R. |
| 409 | */ |
| 410 | *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual); |
| 411 | *nr_out_len += 1; |
| 412 | pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual); |
| 413 | /* |
| 414 | * Convert response from binary hex to ascii hext. |
| 415 | */ |
| 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", |
| 418 | response); |
| 419 | *nr_out_len += 1; |
| 420 | pr_debug("[server] Sending CHAP_R=0x%s\n", response); |
| 421 | auth_ret = 0; |
| 422 | out: |
Herbert Xu | 69110e3 | 2016-01-24 21:19:52 +0800 | [diff] [blame] | 423 | kzfree(desc); |
| 424 | crypto_free_shash(tfm); |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 425 | kfree(challenge); |
| 426 | kfree(challenge_binhex); |
| 427 | return auth_ret; |
| 428 | } |
| 429 | |
| 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) |
| 436 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 437 | struct iscsi_chap *chap = conn->auth_protocol; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 438 | |
| 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; |
| 449 | } |
| 450 | } |
| 451 | |
| 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) |
| 459 | { |
Jörn Engel | 8359cf4 | 2011-11-24 02:05:51 +0100 | [diff] [blame] | 460 | struct iscsi_chap *chap = conn->auth_protocol; |
Nicholas Bellinger | e48354c | 2011-07-23 06:43:04 +0000 | [diff] [blame] | 461 | |
| 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; |
| 474 | } |
| 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; |
| 481 | } |
| 482 | |
| 483 | return 2; |
| 484 | } |