char* digest_authentication_encode(const char *line, const char *user, const char *passwd, const char *method, const char *path, int count, const char *cnonce) { char *realm = NULL, *opaque = NULL, *nonce = NULL, *qop = NULL; char nc[9]; sprintf(nc, "%08x", count); const char *ptr = line; param_token name, value; while (extract_param(&ptr, &name, &value, ',')) { int namelen = name.e - name.b; int valuelen = value.e - value.b; if (strncasecmp(name.b, "realm" , namelen) == 0) { strncpy(realm = calloc(valuelen + 1, 1), value.b, valuelen); realm[valuelen] = '\0'; } else if (strncasecmp(name.b, "opaque", namelen) == 0) { strncpy(opaque = calloc(valuelen + 1, 1), value.b, valuelen); opaque[valuelen] = '\0'; } else if (strncasecmp(name.b, "nonce" , namelen) == 0) { strncpy(nonce = calloc(valuelen + 1, 1), value.b, valuelen); nonce[valuelen] = '\0'; } else if (strncasecmp(name.b, "qop" , namelen) == 0) { strncpy(qop = calloc(valuelen + 1, 1), value.b, valuelen); qop[valuelen] = '\0'; } } if (!realm || !nonce || !user || !passwd || !path || !method) { free(realm); free(opaque); free(nonce); free(qop); return NULL; } if (qop && strncasecmp(qop, "auth", 5) != 0) { /* FIXME: currently don't support auth-int, only "auth" is supported */ free(realm); free(opaque); free(nonce); free(qop); return NULL; } /* calculate the digest value */ md5_state_t ctx; md5_byte_t hash[MD5_HASHLEN]; char a1buf[MD5_HASHLEN * 2 + 1], a2buf[MD5_HASHLEN * 2 + 1]; char response[MD5_HASHLEN * 2 + 1]; /* A1 = username-value ":" realm-value ":" passwd */ md5_init(&ctx); md5_append(&ctx, (md5_byte_t*)user, strlen(user)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)realm, strlen(realm)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)passwd, strlen(passwd)); md5_finish(&ctx, hash); dump_hash(a1buf, hash); /* A2 = Method ":" digest-uri-value */ md5_init(&ctx); md5_append(&ctx, (md5_byte_t*)method, strlen(method)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)path, strlen(path)); md5_finish(&ctx, hash); dump_hash(a2buf, hash); /* qop set: request-digest = H(A1) ":" nonce-value ":" nc-value ":" cnonce-value ":" qop-value ":" H(A2) */ /* not set: request-digest = H(A1) ":" nonce-value ":" H(A2) */ md5_init(&ctx); md5_append(&ctx, (md5_byte_t*)a1buf, strlen(a1buf)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)nonce, strlen(nonce)); md5_append(&ctx, (md5_byte_t*)":", 1); if (qop) { md5_append(&ctx, (md5_byte_t*)nc, strlen(nc)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)cnonce, strlen(cnonce)); md5_append(&ctx, (md5_byte_t*)":", 1); md5_append(&ctx, (md5_byte_t*)qop, strlen(qop)); md5_append(&ctx, (md5_byte_t*)":", 1); } md5_append(&ctx, (md5_byte_t*)a2buf, strlen(a2buf)); md5_finish(&ctx, hash); dump_hash(response, hash); /* prepare the final string */ int len = 256; len += strlen(user); len += strlen(realm); len += strlen(nonce); len += strlen(path); len += strlen(response); if (qop) { len += strlen(qop); len += strlen(nc); len += strlen(cnonce); } if (opaque) { len += strlen(opaque); } char *res = (char*)malloc(len); if (!qop) { sprintf(res, "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", user, realm, nonce, path, response); } else { sprintf(res, "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", qop=%s, nc=%s, cnonce=\"%s\"", user, realm, nonce, path, response, qop, nc, cnonce); } if (opaque) { char *p = res + strlen(res); strcat (p, ", opaque=\""); strcat (p, opaque); strcat (p, "\""); } free(realm); free(opaque); free(nonce); free(qop); return res; }
static int chap_server_compute_md5( struct iscsi_conn *conn, struct iscsi_node_auth *auth, char *nr_in_ptr, char *nr_out_ptr, unsigned int *nr_out_len) { unsigned long id; unsigned char id_as_uchar; unsigned char digest[MD5_SIGNATURE_SIZE]; unsigned char type, response[MD5_SIGNATURE_SIZE * 2 + 2]; unsigned char identifier[10], *challenge = NULL; unsigned char *challenge_binhex = NULL; unsigned char client_digest[MD5_SIGNATURE_SIZE]; unsigned char server_digest[MD5_SIGNATURE_SIZE]; unsigned char chap_n[MAX_CHAP_N_SIZE], chap_r[MAX_RESPONSE_LENGTH]; size_t compare_len; struct iscsi_chap *chap = conn->auth_protocol; struct crypto_hash *tfm; struct hash_desc desc; struct scatterlist sg; int auth_ret = -1, ret, challenge_len; memset(identifier, 0, 10); memset(chap_n, 0, MAX_CHAP_N_SIZE); memset(chap_r, 0, MAX_RESPONSE_LENGTH); memset(digest, 0, MD5_SIGNATURE_SIZE); memset(response, 0, MD5_SIGNATURE_SIZE * 2 + 2); memset(client_digest, 0, MD5_SIGNATURE_SIZE); memset(server_digest, 0, MD5_SIGNATURE_SIZE); challenge = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL); if (!challenge) { pr_err("Unable to allocate challenge buffer\n"); goto out; } challenge_binhex = kzalloc(CHAP_CHALLENGE_STR_LEN, GFP_KERNEL); if (!challenge_binhex) { pr_err("Unable to allocate challenge_binhex buffer\n"); goto out; } /* * Extract CHAP_N. */ if (extract_param(nr_in_ptr, "CHAP_N", MAX_CHAP_N_SIZE, chap_n, &type) < 0) { pr_err("Could not find CHAP_N.\n"); goto out; } if (type == HEX) { pr_err("Could not find CHAP_N.\n"); goto out; } /* Include the terminating NULL in the compare */ compare_len = strlen(auth->userid) + 1; if (strncmp(chap_n, auth->userid, compare_len) != 0) { pr_err("CHAP_N values do not match!\n"); goto out; } pr_debug("[server] Got CHAP_N=%s\n", chap_n); /* * Extract CHAP_R. */ if (extract_param(nr_in_ptr, "CHAP_R", MAX_RESPONSE_LENGTH, chap_r, &type) < 0) { pr_err("Could not find CHAP_R.\n"); goto out; } if (type != HEX) { pr_err("Could not find CHAP_R.\n"); goto out; } pr_debug("[server] Got CHAP_R=%s\n", chap_r); chap_string_to_hex(client_digest, chap_r, strlen(chap_r)); tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); if (IS_ERR(tfm)) { pr_err("Unable to allocate struct crypto_hash\n"); goto out; } desc.tfm = tfm; desc.flags = 0; ret = crypto_hash_init(&desc); if (ret < 0) { pr_err("crypto_hash_init() failed\n"); crypto_free_hash(tfm); goto out; } sg_init_one(&sg, &chap->id, 1); ret = crypto_hash_update(&desc, &sg, 1); if (ret < 0) { pr_err("crypto_hash_update() failed for id\n"); crypto_free_hash(tfm); goto out; } sg_init_one(&sg, &auth->password, strlen(auth->password)); ret = crypto_hash_update(&desc, &sg, strlen(auth->password)); if (ret < 0) { pr_err("crypto_hash_update() failed for password\n"); crypto_free_hash(tfm); goto out; } sg_init_one(&sg, chap->challenge, CHAP_CHALLENGE_LENGTH); ret = crypto_hash_update(&desc, &sg, CHAP_CHALLENGE_LENGTH); if (ret < 0) { pr_err("crypto_hash_update() failed for challenge\n"); crypto_free_hash(tfm); goto out; } ret = crypto_hash_final(&desc, server_digest); if (ret < 0) { pr_err("crypto_hash_final() failed for server digest\n"); crypto_free_hash(tfm); goto out; } crypto_free_hash(tfm); chap_binaryhex_to_asciihex(response, server_digest, MD5_SIGNATURE_SIZE); pr_debug("[server] MD5 Server Digest: %s\n", response); if (memcmp(server_digest, client_digest, MD5_SIGNATURE_SIZE) != 0) { pr_debug("[server] MD5 Digests do not match!\n\n"); goto out; } else pr_debug("[server] MD5 Digests match, CHAP connetication" " successful.\n\n"); /* * One way authentication has succeeded, return now if mutual * authentication is not enabled. */ if (!auth->authenticate_target) { kfree(challenge); kfree(challenge_binhex); return 0; } /* * Get CHAP_I. */ if (extract_param(nr_in_ptr, "CHAP_I", 10, identifier, &type) < 0) { pr_err("Could not find CHAP_I.\n"); goto out; } if (type == HEX) ret = kstrtoul(&identifier[2], 0, &id); else ret = kstrtoul(identifier, 0, &id); if (ret < 0) { pr_err("kstrtoul() failed for CHAP identifier: %d\n", ret); goto out; } if (id > 255) { pr_err("chap identifier: %lu greater than 255\n", id); goto out; } /* * RFC 1994 says Identifier is no more than octet (8 bits). */ pr_debug("[server] Got CHAP_I=%lu\n", id); /* * Get CHAP_C. */ if (extract_param(nr_in_ptr, "CHAP_C", CHAP_CHALLENGE_STR_LEN, challenge, &type) < 0) { pr_err("Could not find CHAP_C.\n"); goto out; } if (type != HEX) { pr_err("Could not find CHAP_C.\n"); goto out; } pr_debug("[server] Got CHAP_C=%s\n", challenge); challenge_len = chap_string_to_hex(challenge_binhex, challenge, strlen(challenge)); if (!challenge_len) { pr_err("Unable to convert incoming challenge\n"); goto out; } if (challenge_len > 1024) { pr_err("CHAP_C exceeds maximum binary size of 1024 bytes\n"); goto out; } /* * During mutual authentication, the CHAP_C generated by the * initiator must not match the original CHAP_C generated by * the target. */ if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) { pr_err("initiator CHAP_C matches target CHAP_C, failing" " login attempt\n"); goto out; } /* * During mutual authentication, the CHAP_C generated by the * initiator must not match the original CHAP_C generated by * the target. */ if (!memcmp(challenge_binhex, chap->challenge, CHAP_CHALLENGE_LENGTH)) { pr_err("initiator CHAP_C matches target CHAP_C, failing" " login attempt\n"); goto out; } /* * Generate CHAP_N and CHAP_R for mutual authentication. */ tfm = crypto_alloc_hash("md5", 0, CRYPTO_ALG_ASYNC); if (IS_ERR(tfm)) { pr_err("Unable to allocate struct crypto_hash\n"); goto out; } desc.tfm = tfm; desc.flags = 0; ret = crypto_hash_init(&desc); if (ret < 0) { pr_err("crypto_hash_init() failed\n"); crypto_free_hash(tfm); goto out; } /* To handle both endiannesses */ id_as_uchar = id; sg_init_one(&sg, &id_as_uchar, 1); ret = crypto_hash_update(&desc, &sg, 1); if (ret < 0) { pr_err("crypto_hash_update() failed for id\n"); crypto_free_hash(tfm); goto out; } sg_init_one(&sg, auth->password_mutual, strlen(auth->password_mutual)); ret = crypto_hash_update(&desc, &sg, strlen(auth->password_mutual)); if (ret < 0) { pr_err("crypto_hash_update() failed for" " password_mutual\n"); crypto_free_hash(tfm); goto out; } /* * Convert received challenge to binary hex. */ sg_init_one(&sg, challenge_binhex, challenge_len); ret = crypto_hash_update(&desc, &sg, challenge_len); if (ret < 0) { pr_err("crypto_hash_update() failed for ma challenge\n"); crypto_free_hash(tfm); goto out; } ret = crypto_hash_final(&desc, digest); if (ret < 0) { pr_err("crypto_hash_final() failed for ma digest\n"); crypto_free_hash(tfm); goto out; } crypto_free_hash(tfm); /* * Generate CHAP_N and CHAP_R. */ *nr_out_len = sprintf(nr_out_ptr, "CHAP_N=%s", auth->userid_mutual); *nr_out_len += 1; pr_debug("[server] Sending CHAP_N=%s\n", auth->userid_mutual); /* * Convert response from binary hex to ascii hext. */ chap_binaryhex_to_asciihex(response, digest, MD5_SIGNATURE_SIZE); *nr_out_len += sprintf(nr_out_ptr + *nr_out_len, "CHAP_R=0x%s", response); *nr_out_len += 1; pr_debug("[server] Sending CHAP_R=0x%s\n", response); auth_ret = 0; out: kfree(challenge); kfree(challenge_binhex); return auth_ret; }