📄 keystore.c
字号:
rc = -EINVAL; goto out; }out: return rc;}int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, struct ecryptfs_auth_tok **auth_tok, char *sig){ int rc = 0; (*auth_tok_key) = request_key(&key_type_user, sig, NULL); if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { printk(KERN_ERR "Could not find key with description: [%s]\n", sig); process_request_key_err(PTR_ERR(*auth_tok_key)); rc = -EINVAL; goto out; } (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); if (ecryptfs_verify_version((*auth_tok)->version)) { printk(KERN_ERR "Data structure version mismatch. " "Userspace tools must match eCryptfs " "kernel module with major version [%d] " "and minor version [%d]\n", ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); rc = -EINVAL; goto out; } if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { printk(KERN_ERR "Invalid auth_tok structure " "returned from key query\n"); rc = -EINVAL; goto out; }out: return rc;}/** * ecryptfs_find_auth_tok_for_sig * @auth_tok: Set to the matching auth_tok; NULL if not found * @crypt_stat: inode crypt_stat crypto context * @sig: Sig of auth_tok to find * * For now, this function simply looks at the registered auth_tok's * linked off the mount_crypt_stat, so all the auth_toks that can be * used must be registered at mount time. This function could * potentially try a lot harder to find auth_tok's (e.g., by calling * out to ecryptfsd to dynamically retrieve an auth_tok object) so * that static registration of auth_tok's will no longer be necessary. * * Returns zero on no error; non-zero on error */static intecryptfs_find_auth_tok_for_sig( struct ecryptfs_auth_tok **auth_tok, struct ecryptfs_crypt_stat *crypt_stat, char *sig){ struct ecryptfs_mount_crypt_stat *mount_crypt_stat = crypt_stat->mount_crypt_stat; struct ecryptfs_global_auth_tok *global_auth_tok; int rc = 0; (*auth_tok) = NULL; if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, mount_crypt_stat, sig)) { struct key *auth_tok_key; rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, sig); } else (*auth_tok) = global_auth_tok->global_auth_tok; return rc;}/** * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. * @auth_tok: The passphrase authentication token to use to encrypt the FEK * @crypt_stat: The cryptographic context * * Returns zero on success; non-zero error otherwise */static intdecrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat){ struct scatterlist dst_sg; struct scatterlist src_sg; struct mutex *tfm_mutex; struct blkcipher_desc desc = { .flags = CRYPTO_TFM_REQ_MAY_SLEEP }; int rc = 0; sg_init_table(&dst_sg, 1); sg_init_table(&src_sg, 1); if (unlikely(ecryptfs_verbosity > 0)) { ecryptfs_printk( KERN_DEBUG, "Session key encryption key (size [%d]):\n", auth_tok->token.password.session_key_encryption_key_bytes); ecryptfs_dump_hex( auth_tok->token.password.session_key_encryption_key, auth_tok->token.password.session_key_encryption_key_bytes); } rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, crypt_stat->cipher); if (unlikely(rc)) { printk(KERN_ERR "Internal error whilst attempting to get " "tfm and mutex for cipher name [%s]; rc = [%d]\n", crypt_stat->cipher, rc); goto out; } rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, auth_tok->session_key.encrypted_key_size, &src_sg, 1); if (rc != 1) { printk(KERN_ERR "Internal error whilst attempting to convert " "auth_tok->session_key.encrypted_key to scatterlist; " "expected rc = 1; got rc = [%d]. " "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, auth_tok->session_key.encrypted_key_size); goto out; } auth_tok->session_key.decrypted_key_size = auth_tok->session_key.encrypted_key_size; rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, auth_tok->session_key.decrypted_key_size, &dst_sg, 1); if (rc != 1) { printk(KERN_ERR "Internal error whilst attempting to convert " "auth_tok->session_key.decrypted_key to scatterlist; " "expected rc = 1; got rc = [%d]\n", rc); goto out; } mutex_lock(tfm_mutex); rc = crypto_blkcipher_setkey( desc.tfm, auth_tok->token.password.session_key_encryption_key, crypt_stat->key_size); if (unlikely(rc < 0)) { mutex_unlock(tfm_mutex); printk(KERN_ERR "Error setting key for crypto context\n"); rc = -EINVAL; goto out; } rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, auth_tok->session_key.encrypted_key_size); mutex_unlock(tfm_mutex); if (unlikely(rc)) { printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); goto out; } auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, auth_tok->session_key.decrypted_key_size); crypt_stat->flags |= ECRYPTFS_KEY_VALID; if (unlikely(ecryptfs_verbosity > 0)) { ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", crypt_stat->key_size); ecryptfs_dump_hex(crypt_stat->key, crypt_stat->key_size); }out: return rc;}/** * ecryptfs_parse_packet_set * @crypt_stat: The cryptographic context * @src: Virtual address of region of memory containing the packets * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set * * Get crypt_stat to have the file's session key if the requisite key * is available to decrypt the session key. * * Returns Zero if a valid authentication token was retrieved and * processed; negative value for file not encrypted or for error * conditions. */int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, unsigned char *src, struct dentry *ecryptfs_dentry){ size_t i = 0; size_t found_auth_tok; size_t next_packet_is_auth_tok_packet; struct list_head auth_tok_list; struct ecryptfs_auth_tok *matching_auth_tok; struct ecryptfs_auth_tok *candidate_auth_tok; char *candidate_auth_tok_sig; size_t packet_size; struct ecryptfs_auth_tok *new_auth_tok; unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; struct ecryptfs_auth_tok_list_item *auth_tok_list_item; size_t tag_11_contents_size; size_t tag_11_packet_size; int rc = 0; INIT_LIST_HEAD(&auth_tok_list); /* Parse the header to find as many packets as we can; these will be * added the our &auth_tok_list */ next_packet_is_auth_tok_packet = 1; while (next_packet_is_auth_tok_packet) { size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); switch (src[i]) { case ECRYPTFS_TAG_3_PACKET_TYPE: rc = parse_tag_3_packet(crypt_stat, (unsigned char *)&src[i], &auth_tok_list, &new_auth_tok, &packet_size, max_packet_size); if (rc) { ecryptfs_printk(KERN_ERR, "Error parsing " "tag 3 packet\n"); rc = -EIO; goto out_wipe_list; } i += packet_size; rc = parse_tag_11_packet((unsigned char *)&src[i], sig_tmp_space, ECRYPTFS_SIG_SIZE, &tag_11_contents_size, &tag_11_packet_size, max_packet_size); if (rc) { ecryptfs_printk(KERN_ERR, "No valid " "(ecryptfs-specific) literal " "packet containing " "authentication token " "signature found after " "tag 3 packet\n"); rc = -EIO; goto out_wipe_list; } i += tag_11_packet_size; if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { ecryptfs_printk(KERN_ERR, "Expected " "signature of size [%d]; " "read size [%d]\n", ECRYPTFS_SIG_SIZE, tag_11_contents_size); rc = -EIO; goto out_wipe_list; } ecryptfs_to_hex(new_auth_tok->token.password.signature, sig_tmp_space, tag_11_contents_size); new_auth_tok->token.password.signature[ ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; crypt_stat->flags |= ECRYPTFS_ENCRYPTED; break; case ECRYPTFS_TAG_1_PACKET_TYPE: rc = parse_tag_1_packet(crypt_stat, (unsigned char *)&src[i], &auth_tok_list, &new_auth_tok, &packet_size, max_packet_size); if (rc) { ecryptfs_printk(KERN_ERR, "Error parsing " "tag 1 packet\n"); rc = -EIO; goto out_wipe_list; } i += packet_size; crypt_stat->flags |= ECRYPTFS_ENCRYPTED; break; case ECRYPTFS_TAG_11_PACKET_TYPE: ecryptfs_printk(KERN_WARNING, "Invalid packet set " "(Tag 11 not allowed by itself)\n"); rc = -EIO; goto out_wipe_list; break; default: ecryptfs_printk(KERN_DEBUG, "No packet at offset " "[%d] of the file header; hex value of " "character is [0x%.2x]\n", i, src[i]); next_packet_is_auth_tok_packet = 0; } } if (list_empty(&auth_tok_list)) { printk(KERN_ERR "The lower file appears to be a non-encrypted " "eCryptfs file; this is not supported in this version " "of the eCryptfs kernel module\n"); rc = -EINVAL; goto out; } /* auth_tok_list contains the set of authentication tokens * parsed from the metadata. We need to find a matching * authentication token that has the secret component(s) * necessary to decrypt the EFEK in the auth_tok parsed from * the metadata. There may be several potential matches, but * just one will be sufficient to decrypt to get the FEK. */find_next_matching_auth_tok: found_auth_tok = 0; list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { candidate_auth_tok = &auth_tok_list_item->auth_tok; if (unlikely(ecryptfs_verbosity > 0)) { ecryptfs_printk(KERN_DEBUG, "Considering cadidate auth tok:\n"); ecryptfs_dump_auth_tok(candidate_auth_tok); } rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, candidate_auth_tok); if (rc) { printk(KERN_ERR "Unrecognized candidate auth tok type: [%d]\n", candidate_auth_tok->token_type); rc = -EINVAL; goto out_wipe_list; } ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat, candidate_auth_tok_sig); if (matching_auth_tok) { found_auth_tok = 1; goto found_matching_auth_tok; } } if (!found_auth_tok) { ecryptfs_printk(KERN_ERR, "Could not find a usable " "authentication token\n"); rc = -EIO; goto out_wipe_list; }found_matching_auth_tok: if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { memcpy(&(candidate_auth_tok->token.private_key), &(matching_auth_tok->token.private_key), sizeof(struct ecryptfs_private_key)); rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, crypt_stat); } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { memcpy(&(candidate_auth_tok->token.password), &(matching_auth_tok->token.password), sizeof(struct ecryptfs_password)); rc = decrypt_passphrase_encrypted_session_key( candidate_auth_tok, crypt_stat); } if (rc) { struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; ecryptfs_printk(KERN_WARNING, "Error decrypting the " "session key for authentication token with sig " "[%.*s]; rc = [%d]. Removing auth tok " "candidate from the list and searching for " "the next match.\n", candidate_auth_tok_sig, ECRYPTFS_SIG_SIZE_HEX, rc); list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, &auth_tok_list, list) { if (candidate_auth_tok == &auth_tok_list_item->auth_tok) { list_del(&auth_tok_list_item->list); kmem_cache_free( ecryptfs_auth_tok_list_item_cache, auth_tok_list_item); goto find_next_matching_auth_tok; } } BUG(); } rc = ecryptfs_compute_root_iv(crypt_stat); if (rc) { ecryptfs_printk(KERN_ERR, "Error computing " "the root IV\n"); goto out_wipe_list; } rc = ecryptfs_init_crypt_ctx(crypt_stat); if (rc) { ecryptfs_printk(KERN_ERR, "Error initializing crypto " "context for cipher [%s]; rc = [%d]\n", crypt_stat->cipher, rc); }out_wipe_list: wipe_auth_tok_list(&auth_tok_list);out: return rc;}static intpki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_key_record *key_rec){ struct ecryptfs_msg_ctx *msg_ctx = NULL; char *netlink_payload; size_t netlink_payload_length; struct ecryptfs_message *msg; int rc; rc = write_tag_66_packet(auth_tok->token.private_key.signature, ecryptfs_code_for_cipher_string(crypt_stat), crypt_stat, &netlink_payload, &netlink_payload_length); if (rc) { ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); goto out; } rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload, netlink_payload_length, &msg_ctx); if (rc) { ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); goto out; } rc = ecryptfs_wait_for_response(msg_ctx, &msg); if (rc) { ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " "from the user space daemon\n"); rc = -EIO; goto out; } rc = parse_tag_67_packet(key_rec, msg); if (rc) ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); kfree(msg);out: if (netlink_payload) kfree(netlink_payload); return rc;}/** * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet * @dest: Buffer into which to write the packet * @remaining_bytes: Maximum number of bytes that can be writtn * @auth_tok: The authentication token used for generating the tag 1 packet * @crypt_stat: The cryptographic context * @key_rec: The key record struct for the tag 1 packet * @packet_size: This function will write the number of bytes that end * up constituting the packet; set to zero on error * * Returns zero on success; non-zero on error. */static intwrite_tag_1_packet(char *dest, size_t *remaining_bytes, struct ecryptfs_auth_tok *auth_tok, struct ecryptfs_crypt_stat *crypt_stat, struct ecryptfs_key_record *key_rec, size_t *packet_size){ size_t i; size_t encrypted_session_key_valid = 0; size_t packet_size_length; size_t max_packet_size; int rc = 0; (*packet_size) = 0; ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, ECRYPTFS_SIG_SIZE); encrypted_session_key_valid = 0; for (i = 0; i < crypt_stat->key_size; i++) encrypted_session_key_valid |= auth_tok->session_key.encrypted_key[i]; if (encrypted_session_key_valid) { memcpy(key_rec->enc_key, auth_tok->session_key.encrypted_key, auth_tok->session_key.encrypted_key_size); goto encrypted_session_key_set; } if (auth_tok->session_key.encrypted_key_size == 0) auth_tok->session_key.encrypted_key_size = auth_tok->token.private_key.key_size; rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); if (rc) { ecryptfs_printk(KERN_ERR, "Failed to encrypt session key " "via a pki"); goto out; } if (ecryptfs_verbosity > 0) { ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -