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