1237fead6SMichael Halcrow /** 2237fead6SMichael Halcrow * eCryptfs: Linux filesystem encryption layer 3237fead6SMichael Halcrow * In-kernel key management code. Includes functions to parse and 4237fead6SMichael Halcrow * write authentication token-related packets with the underlying 5237fead6SMichael Halcrow * file. 6237fead6SMichael Halcrow * 7237fead6SMichael Halcrow * Copyright (C) 2004-2006 International Business Machines Corp. 8237fead6SMichael Halcrow * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> 9237fead6SMichael Halcrow * Michael C. Thompson <mcthomps@us.ibm.com> 10dddfa461SMichael Halcrow * Trevor S. Highland <trevor.highland@gmail.com> 11237fead6SMichael Halcrow * 12237fead6SMichael Halcrow * This program is free software; you can redistribute it and/or 13237fead6SMichael Halcrow * modify it under the terms of the GNU General Public License as 14237fead6SMichael Halcrow * published by the Free Software Foundation; either version 2 of the 15237fead6SMichael Halcrow * License, or (at your option) any later version. 16237fead6SMichael Halcrow * 17237fead6SMichael Halcrow * This program is distributed in the hope that it will be useful, but 18237fead6SMichael Halcrow * WITHOUT ANY WARRANTY; without even the implied warranty of 19237fead6SMichael Halcrow * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20237fead6SMichael Halcrow * General Public License for more details. 21237fead6SMichael Halcrow * 22237fead6SMichael Halcrow * You should have received a copy of the GNU General Public License 23237fead6SMichael Halcrow * along with this program; if not, write to the Free Software 24237fead6SMichael Halcrow * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 25237fead6SMichael Halcrow * 02111-1307, USA. 26237fead6SMichael Halcrow */ 27237fead6SMichael Halcrow 28237fead6SMichael Halcrow #include <linux/string.h> 29237fead6SMichael Halcrow #include <linux/syscalls.h> 30237fead6SMichael Halcrow #include <linux/pagemap.h> 31237fead6SMichael Halcrow #include <linux/key.h> 32237fead6SMichael Halcrow #include <linux/random.h> 33237fead6SMichael Halcrow #include <linux/crypto.h> 34237fead6SMichael Halcrow #include <linux/scatterlist.h> 35237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 36237fead6SMichael Halcrow 37237fead6SMichael Halcrow /** 38237fead6SMichael Halcrow * request_key returned an error instead of a valid key address; 39237fead6SMichael Halcrow * determine the type of error, make appropriate log entries, and 40237fead6SMichael Halcrow * return an error code. 41237fead6SMichael Halcrow */ 42237fead6SMichael Halcrow int process_request_key_err(long err_code) 43237fead6SMichael Halcrow { 44237fead6SMichael Halcrow int rc = 0; 45237fead6SMichael Halcrow 46237fead6SMichael Halcrow switch (err_code) { 47237fead6SMichael Halcrow case ENOKEY: 48237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "No key\n"); 49237fead6SMichael Halcrow rc = -ENOENT; 50237fead6SMichael Halcrow break; 51237fead6SMichael Halcrow case EKEYEXPIRED: 52237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key expired\n"); 53237fead6SMichael Halcrow rc = -ETIME; 54237fead6SMichael Halcrow break; 55237fead6SMichael Halcrow case EKEYREVOKED: 56237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key revoked\n"); 57237fead6SMichael Halcrow rc = -EINVAL; 58237fead6SMichael Halcrow break; 59237fead6SMichael Halcrow default: 60237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unknown error code: " 61237fead6SMichael Halcrow "[0x%.16x]\n", err_code); 62237fead6SMichael Halcrow rc = -EINVAL; 63237fead6SMichael Halcrow } 64237fead6SMichael Halcrow return rc; 65237fead6SMichael Halcrow } 66237fead6SMichael Halcrow 67237fead6SMichael Halcrow /** 68237fead6SMichael Halcrow * parse_packet_length 69237fead6SMichael Halcrow * @data: Pointer to memory containing length at offset 70237fead6SMichael Halcrow * @size: This function writes the decoded size to this memory 71237fead6SMichael Halcrow * address; zero on error 72237fead6SMichael Halcrow * @length_size: The number of bytes occupied by the encoded length 73237fead6SMichael Halcrow * 74237fead6SMichael Halcrow * Returns Zero on success 75237fead6SMichael Halcrow */ 76237fead6SMichael Halcrow static int parse_packet_length(unsigned char *data, size_t *size, 77237fead6SMichael Halcrow size_t *length_size) 78237fead6SMichael Halcrow { 79237fead6SMichael Halcrow int rc = 0; 80237fead6SMichael Halcrow 81237fead6SMichael Halcrow (*length_size) = 0; 82237fead6SMichael Halcrow (*size) = 0; 83237fead6SMichael Halcrow if (data[0] < 192) { 84237fead6SMichael Halcrow /* One-byte length */ 85dddfa461SMichael Halcrow (*size) = (unsigned char)data[0]; 86237fead6SMichael Halcrow (*length_size) = 1; 87237fead6SMichael Halcrow } else if (data[0] < 224) { 88237fead6SMichael Halcrow /* Two-byte length */ 89dddfa461SMichael Halcrow (*size) = (((unsigned char)(data[0]) - 192) * 256); 90dddfa461SMichael Halcrow (*size) += ((unsigned char)(data[1]) + 192); 91237fead6SMichael Halcrow (*length_size) = 2; 92237fead6SMichael Halcrow } else if (data[0] == 255) { 93237fead6SMichael Halcrow /* Five-byte length; we're not supposed to see this */ 94237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Five-byte packet length not " 95237fead6SMichael Halcrow "supported\n"); 96237fead6SMichael Halcrow rc = -EINVAL; 97237fead6SMichael Halcrow goto out; 98237fead6SMichael Halcrow } else { 99237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); 100237fead6SMichael Halcrow rc = -EINVAL; 101237fead6SMichael Halcrow goto out; 102237fead6SMichael Halcrow } 103237fead6SMichael Halcrow out: 104237fead6SMichael Halcrow return rc; 105237fead6SMichael Halcrow } 106237fead6SMichael Halcrow 107237fead6SMichael Halcrow /** 108237fead6SMichael Halcrow * write_packet_length 109237fead6SMichael Halcrow * @dest: The byte array target into which to write the 110237fead6SMichael Halcrow * length. Must have at least 5 bytes allocated. 111237fead6SMichael Halcrow * @size: The length to write. 112237fead6SMichael Halcrow * @packet_size_length: The number of bytes used to encode the 113237fead6SMichael Halcrow * packet length is written to this address. 114237fead6SMichael Halcrow * 115237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 116237fead6SMichael Halcrow */ 117237fead6SMichael Halcrow static int write_packet_length(char *dest, size_t size, 118237fead6SMichael Halcrow size_t *packet_size_length) 119237fead6SMichael Halcrow { 120237fead6SMichael Halcrow int rc = 0; 121237fead6SMichael Halcrow 122237fead6SMichael Halcrow if (size < 192) { 123237fead6SMichael Halcrow dest[0] = size; 124237fead6SMichael Halcrow (*packet_size_length) = 1; 125237fead6SMichael Halcrow } else if (size < 65536) { 126237fead6SMichael Halcrow dest[0] = (((size - 192) / 256) + 192); 127237fead6SMichael Halcrow dest[1] = ((size - 192) % 256); 128237fead6SMichael Halcrow (*packet_size_length) = 2; 129237fead6SMichael Halcrow } else { 130237fead6SMichael Halcrow rc = -EINVAL; 131237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 132237fead6SMichael Halcrow "Unsupported packet size: [%d]\n", size); 133237fead6SMichael Halcrow } 134237fead6SMichael Halcrow return rc; 135237fead6SMichael Halcrow } 136237fead6SMichael Halcrow 137dddfa461SMichael Halcrow static int 138dddfa461SMichael Halcrow write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, 139dddfa461SMichael Halcrow char **packet, size_t *packet_len) 140dddfa461SMichael Halcrow { 141dddfa461SMichael Halcrow size_t i = 0; 142dddfa461SMichael Halcrow size_t data_len; 143dddfa461SMichael Halcrow size_t packet_size_len; 144dddfa461SMichael Halcrow char *message; 145dddfa461SMichael Halcrow int rc; 146dddfa461SMichael Halcrow 147dddfa461SMichael Halcrow /* 148dddfa461SMichael Halcrow * ***** TAG 64 Packet Format ***** 149dddfa461SMichael Halcrow * | Content Type | 1 byte | 150dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 151dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 152dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 153dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 154dddfa461SMichael Halcrow */ 155dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX 156dddfa461SMichael Halcrow + session_key->encrypted_key_size); 157dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 158dddfa461SMichael Halcrow message = *packet; 159dddfa461SMichael Halcrow if (!message) { 160dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 161dddfa461SMichael Halcrow rc = -ENOMEM; 162dddfa461SMichael Halcrow goto out; 163dddfa461SMichael Halcrow } 164dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; 165dddfa461SMichael Halcrow rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 166dddfa461SMichael Halcrow &packet_size_len); 167dddfa461SMichael Halcrow if (rc) { 168dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 169dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 170dddfa461SMichael Halcrow goto out; 171dddfa461SMichael Halcrow } 172dddfa461SMichael Halcrow i += packet_size_len; 173dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 174dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 175dddfa461SMichael Halcrow rc = write_packet_length(&message[i], session_key->encrypted_key_size, 176dddfa461SMichael Halcrow &packet_size_len); 177dddfa461SMichael Halcrow if (rc) { 178dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 179dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 180dddfa461SMichael Halcrow goto out; 181dddfa461SMichael Halcrow } 182dddfa461SMichael Halcrow i += packet_size_len; 183dddfa461SMichael Halcrow memcpy(&message[i], session_key->encrypted_key, 184dddfa461SMichael Halcrow session_key->encrypted_key_size); 185dddfa461SMichael Halcrow i += session_key->encrypted_key_size; 186dddfa461SMichael Halcrow *packet_len = i; 187dddfa461SMichael Halcrow out: 188dddfa461SMichael Halcrow return rc; 189dddfa461SMichael Halcrow } 190dddfa461SMichael Halcrow 191dddfa461SMichael Halcrow static int 192dddfa461SMichael Halcrow parse_tag_65_packet(struct ecryptfs_session_key *session_key, u16 *cipher_code, 193dddfa461SMichael Halcrow struct ecryptfs_message *msg) 194dddfa461SMichael Halcrow { 195dddfa461SMichael Halcrow size_t i = 0; 196dddfa461SMichael Halcrow char *data; 197dddfa461SMichael Halcrow size_t data_len; 198dddfa461SMichael Halcrow size_t m_size; 199dddfa461SMichael Halcrow size_t message_len; 200dddfa461SMichael Halcrow u16 checksum = 0; 201dddfa461SMichael Halcrow u16 expected_checksum = 0; 202dddfa461SMichael Halcrow int rc; 203dddfa461SMichael Halcrow 204dddfa461SMichael Halcrow /* 205dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 206dddfa461SMichael Halcrow * | Content Type | 1 byte | 207dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 208dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 209dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 210dddfa461SMichael Halcrow */ 211dddfa461SMichael Halcrow message_len = msg->data_len; 212dddfa461SMichael Halcrow data = msg->data; 213dddfa461SMichael Halcrow if (message_len < 4) { 214dddfa461SMichael Halcrow rc = -EIO; 215dddfa461SMichael Halcrow goto out; 216dddfa461SMichael Halcrow } 217dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { 218dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); 219dddfa461SMichael Halcrow rc = -EIO; 220dddfa461SMichael Halcrow goto out; 221dddfa461SMichael Halcrow } 222dddfa461SMichael Halcrow if (data[i++]) { 223dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " 224dddfa461SMichael Halcrow "[%d]\n", data[i-1]); 225dddfa461SMichael Halcrow rc = -EIO; 226dddfa461SMichael Halcrow goto out; 227dddfa461SMichael Halcrow } 228dddfa461SMichael Halcrow rc = parse_packet_length(&data[i], &m_size, &data_len); 229dddfa461SMichael Halcrow if (rc) { 230dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 231dddfa461SMichael Halcrow "rc = [%d]\n", rc); 232dddfa461SMichael Halcrow goto out; 233dddfa461SMichael Halcrow } 234dddfa461SMichael Halcrow i += data_len; 235dddfa461SMichael Halcrow if (message_len < (i + m_size)) { 236dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "The received netlink message is " 237dddfa461SMichael Halcrow "shorter than expected\n"); 238dddfa461SMichael Halcrow rc = -EIO; 239dddfa461SMichael Halcrow goto out; 240dddfa461SMichael Halcrow } 241dddfa461SMichael Halcrow if (m_size < 3) { 242dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, 243dddfa461SMichael Halcrow "The decrypted key is not long enough to " 244dddfa461SMichael Halcrow "include a cipher code and checksum\n"); 245dddfa461SMichael Halcrow rc = -EIO; 246dddfa461SMichael Halcrow goto out; 247dddfa461SMichael Halcrow } 248dddfa461SMichael Halcrow *cipher_code = data[i++]; 249dddfa461SMichael Halcrow /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ 250dddfa461SMichael Halcrow session_key->decrypted_key_size = m_size - 3; 251dddfa461SMichael Halcrow if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { 252dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " 253dddfa461SMichael Halcrow "the maximum key size [%d]\n", 254dddfa461SMichael Halcrow session_key->decrypted_key_size, 255dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 256dddfa461SMichael Halcrow rc = -EIO; 257dddfa461SMichael Halcrow goto out; 258dddfa461SMichael Halcrow } 259dddfa461SMichael Halcrow memcpy(session_key->decrypted_key, &data[i], 260dddfa461SMichael Halcrow session_key->decrypted_key_size); 261dddfa461SMichael Halcrow i += session_key->decrypted_key_size; 262dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]) << 8; 263dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]); 264dddfa461SMichael Halcrow for (i = 0; i < session_key->decrypted_key_size; i++) 265dddfa461SMichael Halcrow checksum += session_key->decrypted_key[i]; 266dddfa461SMichael Halcrow if (expected_checksum != checksum) { 267dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Invalid checksum for file " 268dddfa461SMichael Halcrow "encryption key; expected [%x]; calculated " 269dddfa461SMichael Halcrow "[%x]\n", expected_checksum, checksum); 270dddfa461SMichael Halcrow rc = -EIO; 271dddfa461SMichael Halcrow } 272dddfa461SMichael Halcrow out: 273dddfa461SMichael Halcrow return rc; 274dddfa461SMichael Halcrow } 275dddfa461SMichael Halcrow 276dddfa461SMichael Halcrow 277dddfa461SMichael Halcrow static int 278dddfa461SMichael Halcrow write_tag_66_packet(char *signature, size_t cipher_code, 279dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, char **packet, 280dddfa461SMichael Halcrow size_t *packet_len) 281dddfa461SMichael Halcrow { 282dddfa461SMichael Halcrow size_t i = 0; 283dddfa461SMichael Halcrow size_t j; 284dddfa461SMichael Halcrow size_t data_len; 285dddfa461SMichael Halcrow size_t checksum = 0; 286dddfa461SMichael Halcrow size_t packet_size_len; 287dddfa461SMichael Halcrow char *message; 288dddfa461SMichael Halcrow int rc; 289dddfa461SMichael Halcrow 290dddfa461SMichael Halcrow /* 291dddfa461SMichael Halcrow * ***** TAG 66 Packet Format ***** 292dddfa461SMichael Halcrow * | Content Type | 1 byte | 293dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 294dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 295dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 296dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 297dddfa461SMichael Halcrow */ 298dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); 299dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 300dddfa461SMichael Halcrow message = *packet; 301dddfa461SMichael Halcrow if (!message) { 302dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 303dddfa461SMichael Halcrow rc = -ENOMEM; 304dddfa461SMichael Halcrow goto out; 305dddfa461SMichael Halcrow } 306dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; 307dddfa461SMichael Halcrow rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 308dddfa461SMichael Halcrow &packet_size_len); 309dddfa461SMichael Halcrow if (rc) { 310dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 311dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 312dddfa461SMichael Halcrow goto out; 313dddfa461SMichael Halcrow } 314dddfa461SMichael Halcrow i += packet_size_len; 315dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 316dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 317dddfa461SMichael Halcrow /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ 318dddfa461SMichael Halcrow rc = write_packet_length(&message[i], crypt_stat->key_size + 3, 319dddfa461SMichael Halcrow &packet_size_len); 320dddfa461SMichael Halcrow if (rc) { 321dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 322dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 323dddfa461SMichael Halcrow goto out; 324dddfa461SMichael Halcrow } 325dddfa461SMichael Halcrow i += packet_size_len; 326dddfa461SMichael Halcrow message[i++] = cipher_code; 327dddfa461SMichael Halcrow memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); 328dddfa461SMichael Halcrow i += crypt_stat->key_size; 329dddfa461SMichael Halcrow for (j = 0; j < crypt_stat->key_size; j++) 330dddfa461SMichael Halcrow checksum += crypt_stat->key[j]; 331dddfa461SMichael Halcrow message[i++] = (checksum / 256) % 256; 332dddfa461SMichael Halcrow message[i++] = (checksum % 256); 333dddfa461SMichael Halcrow *packet_len = i; 334dddfa461SMichael Halcrow out: 335dddfa461SMichael Halcrow return rc; 336dddfa461SMichael Halcrow } 337dddfa461SMichael Halcrow 338dddfa461SMichael Halcrow static int 339dddfa461SMichael Halcrow parse_tag_67_packet(struct ecryptfs_key_record *key_rec, 340dddfa461SMichael Halcrow struct ecryptfs_message *msg) 341dddfa461SMichael Halcrow { 342dddfa461SMichael Halcrow size_t i = 0; 343dddfa461SMichael Halcrow char *data; 344dddfa461SMichael Halcrow size_t data_len; 345dddfa461SMichael Halcrow size_t message_len; 346dddfa461SMichael Halcrow int rc; 347dddfa461SMichael Halcrow 348dddfa461SMichael Halcrow /* 349dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 350dddfa461SMichael Halcrow * | Content Type | 1 byte | 351dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 352dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 353dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 354dddfa461SMichael Halcrow */ 355dddfa461SMichael Halcrow message_len = msg->data_len; 356dddfa461SMichael Halcrow data = msg->data; 357dddfa461SMichael Halcrow /* verify that everything through the encrypted FEK size is present */ 358dddfa461SMichael Halcrow if (message_len < 4) { 359dddfa461SMichael Halcrow rc = -EIO; 360dddfa461SMichael Halcrow goto out; 361dddfa461SMichael Halcrow } 362dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { 363dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_67\n"); 364dddfa461SMichael Halcrow rc = -EIO; 365dddfa461SMichael Halcrow goto out; 366dddfa461SMichael Halcrow } 367dddfa461SMichael Halcrow if (data[i++]) { 368dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Status indicator has non zero value" 369dddfa461SMichael Halcrow " [%d]\n", data[i-1]); 370dddfa461SMichael Halcrow rc = -EIO; 371dddfa461SMichael Halcrow goto out; 372dddfa461SMichael Halcrow } 373dddfa461SMichael Halcrow rc = parse_packet_length(&data[i], &key_rec->enc_key_size, &data_len); 374dddfa461SMichael Halcrow if (rc) { 375dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 376dddfa461SMichael Halcrow "rc = [%d]\n", rc); 377dddfa461SMichael Halcrow goto out; 378dddfa461SMichael Halcrow } 379dddfa461SMichael Halcrow i += data_len; 380dddfa461SMichael Halcrow if (message_len < (i + key_rec->enc_key_size)) { 381dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "message_len [%d]; max len is [%d]\n", 382dddfa461SMichael Halcrow message_len, (i + key_rec->enc_key_size)); 383dddfa461SMichael Halcrow rc = -EIO; 384dddfa461SMichael Halcrow goto out; 385dddfa461SMichael Halcrow } 386dddfa461SMichael Halcrow if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 387dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Encrypted key_size [%d] larger than " 388dddfa461SMichael Halcrow "the maximum key size [%d]\n", 389dddfa461SMichael Halcrow key_rec->enc_key_size, 390dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 391dddfa461SMichael Halcrow rc = -EIO; 392dddfa461SMichael Halcrow goto out; 393dddfa461SMichael Halcrow } 394dddfa461SMichael Halcrow memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); 395dddfa461SMichael Halcrow out: 396dddfa461SMichael Halcrow return rc; 397dddfa461SMichael Halcrow } 398dddfa461SMichael Halcrow 399dddfa461SMichael Halcrow /** 400dddfa461SMichael Halcrow * decrypt_pki_encrypted_session_key - Decrypt the session key with 401dddfa461SMichael Halcrow * the given auth_tok. 402dddfa461SMichael Halcrow * 403dddfa461SMichael Halcrow * Returns Zero on success; non-zero error otherwise. 404dddfa461SMichael Halcrow */ 405*f4aad16aSMichael Halcrow static int 406*f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 407dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 408dddfa461SMichael Halcrow { 409dddfa461SMichael Halcrow u16 cipher_code = 0; 410dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx; 411dddfa461SMichael Halcrow struct ecryptfs_message *msg = NULL; 412*f4aad16aSMichael Halcrow char *auth_tok_sig; 413dddfa461SMichael Halcrow char *netlink_message; 414dddfa461SMichael Halcrow size_t netlink_message_length; 415dddfa461SMichael Halcrow int rc; 416dddfa461SMichael Halcrow 417*f4aad16aSMichael Halcrow if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) { 418*f4aad16aSMichael Halcrow printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 419*f4aad16aSMichael Halcrow auth_tok->token_type); 420*f4aad16aSMichael Halcrow goto out; 421*f4aad16aSMichael Halcrow } 422*f4aad16aSMichael Halcrow rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 423dddfa461SMichael Halcrow &netlink_message, &netlink_message_length); 424dddfa461SMichael Halcrow if (rc) { 425dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet"); 426dddfa461SMichael Halcrow goto out; 427dddfa461SMichael Halcrow } 428dddfa461SMichael Halcrow rc = ecryptfs_send_message(ecryptfs_transport, netlink_message, 429dddfa461SMichael Halcrow netlink_message_length, &msg_ctx); 430dddfa461SMichael Halcrow if (rc) { 431dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); 432dddfa461SMichael Halcrow goto out; 433dddfa461SMichael Halcrow } 434dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 435dddfa461SMichael Halcrow if (rc) { 436dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 437dddfa461SMichael Halcrow "from the user space daemon\n"); 438dddfa461SMichael Halcrow rc = -EIO; 439dddfa461SMichael Halcrow goto out; 440dddfa461SMichael Halcrow } 441dddfa461SMichael Halcrow rc = parse_tag_65_packet(&(auth_tok->session_key), 442dddfa461SMichael Halcrow &cipher_code, msg); 443dddfa461SMichael Halcrow if (rc) { 444dddfa461SMichael Halcrow printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 445dddfa461SMichael Halcrow rc); 446dddfa461SMichael Halcrow goto out; 447dddfa461SMichael Halcrow } 448dddfa461SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 449dddfa461SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 450dddfa461SMichael Halcrow auth_tok->session_key.decrypted_key_size); 451dddfa461SMichael Halcrow crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 452dddfa461SMichael Halcrow rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); 453dddfa461SMichael Halcrow if (rc) { 454dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 455dddfa461SMichael Halcrow cipher_code) 456dddfa461SMichael Halcrow goto out; 457dddfa461SMichael Halcrow } 458dddfa461SMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 459dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 460dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 461dddfa461SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 462dddfa461SMichael Halcrow crypt_stat->key_size); 463dddfa461SMichael Halcrow } 464dddfa461SMichael Halcrow out: 465dddfa461SMichael Halcrow if (msg) 466dddfa461SMichael Halcrow kfree(msg); 467dddfa461SMichael Halcrow return rc; 468dddfa461SMichael Halcrow } 469dddfa461SMichael Halcrow 470dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 471dddfa461SMichael Halcrow { 472dddfa461SMichael Halcrow struct list_head *walker; 473dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 474dddfa461SMichael Halcrow 475dddfa461SMichael Halcrow walker = auth_tok_list_head->next; 476dddfa461SMichael Halcrow while (walker != auth_tok_list_head) { 477dddfa461SMichael Halcrow auth_tok_list_item = 478dddfa461SMichael Halcrow list_entry(walker, struct ecryptfs_auth_tok_list_item, 479dddfa461SMichael Halcrow list); 480dddfa461SMichael Halcrow walker = auth_tok_list_item->list.next; 481dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 482dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 483dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 484dddfa461SMichael Halcrow auth_tok_list_item); 485dddfa461SMichael Halcrow } 486dddfa461SMichael Halcrow auth_tok_list_head->next = NULL; 487dddfa461SMichael Halcrow } 488dddfa461SMichael Halcrow 489dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 490dddfa461SMichael Halcrow 491dddfa461SMichael Halcrow 492dddfa461SMichael Halcrow /** 493dddfa461SMichael Halcrow * parse_tag_1_packet 494dddfa461SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 495dddfa461SMichael Halcrow * contents. 496dddfa461SMichael Halcrow * @data: The raw bytes of the packet. 497dddfa461SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 498dddfa461SMichael Halcrow * a new authentication token will be placed at the end 499dddfa461SMichael Halcrow * of this list for this packet. 500dddfa461SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 501dddfa461SMichael Halcrow * allocates; sets the memory address of the pointer to 502dddfa461SMichael Halcrow * NULL on error. This object is added to the 503dddfa461SMichael Halcrow * auth_tok_list. 504dddfa461SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 505dddfa461SMichael Halcrow * into this memory location; zero on error. 506dddfa461SMichael Halcrow * 507dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 508dddfa461SMichael Halcrow */ 509dddfa461SMichael Halcrow static int 510dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 511dddfa461SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 512dddfa461SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 513dddfa461SMichael Halcrow size_t *packet_size, size_t max_packet_size) 514dddfa461SMichael Halcrow { 515dddfa461SMichael Halcrow size_t body_size; 516dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 517dddfa461SMichael Halcrow size_t length_size; 518dddfa461SMichael Halcrow int rc = 0; 519dddfa461SMichael Halcrow 520dddfa461SMichael Halcrow (*packet_size) = 0; 521dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 522dddfa461SMichael Halcrow 523dddfa461SMichael Halcrow /* we check that: 524dddfa461SMichael Halcrow * one byte for the Tag 1 ID flag 525dddfa461SMichael Halcrow * two bytes for the body size 526dddfa461SMichael Halcrow * do not exceed the maximum_packet_size 527dddfa461SMichael Halcrow */ 528dddfa461SMichael Halcrow if (unlikely((*packet_size) + 3 > max_packet_size)) { 529dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 530dddfa461SMichael Halcrow rc = -EINVAL; 531dddfa461SMichael Halcrow goto out; 532dddfa461SMichael Halcrow } 533dddfa461SMichael Halcrow /* check for Tag 1 identifier - one byte */ 534dddfa461SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 535dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n", 536dddfa461SMichael Halcrow ECRYPTFS_TAG_1_PACKET_TYPE); 537dddfa461SMichael Halcrow rc = -EINVAL; 538dddfa461SMichael Halcrow goto out; 539dddfa461SMichael Halcrow } 540dddfa461SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 541dddfa461SMichael Halcrow * at end of function upon failure */ 542dddfa461SMichael Halcrow auth_tok_list_item = 543dddfa461SMichael Halcrow kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, 544dddfa461SMichael Halcrow GFP_KERNEL); 545dddfa461SMichael Halcrow if (!auth_tok_list_item) { 546dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 547dddfa461SMichael Halcrow rc = -ENOMEM; 548dddfa461SMichael Halcrow goto out; 549dddfa461SMichael Halcrow } 550dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 551dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 552dddfa461SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 553dddfa461SMichael Halcrow /* check for body size - one to two bytes 554dddfa461SMichael Halcrow * 555dddfa461SMichael Halcrow * ***** TAG 1 Packet Format ***** 556dddfa461SMichael Halcrow * | version number | 1 byte | 557dddfa461SMichael Halcrow * | key ID | 8 bytes | 558dddfa461SMichael Halcrow * | public key algorithm | 1 byte | 559dddfa461SMichael Halcrow * | encrypted session key | arbitrary | 560dddfa461SMichael Halcrow */ 561dddfa461SMichael Halcrow rc = parse_packet_length(&data[(*packet_size)], &body_size, 562dddfa461SMichael Halcrow &length_size); 563dddfa461SMichael Halcrow if (rc) { 564dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 565dddfa461SMichael Halcrow "rc = [%d]\n", rc); 566dddfa461SMichael Halcrow goto out_free; 567dddfa461SMichael Halcrow } 568dddfa461SMichael Halcrow if (unlikely(body_size < (0x02 + ECRYPTFS_SIG_SIZE))) { 569dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", 570dddfa461SMichael Halcrow body_size); 571dddfa461SMichael Halcrow rc = -EINVAL; 572dddfa461SMichael Halcrow goto out_free; 573dddfa461SMichael Halcrow } 574dddfa461SMichael Halcrow (*packet_size) += length_size; 575dddfa461SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 576dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 577dddfa461SMichael Halcrow rc = -EINVAL; 578dddfa461SMichael Halcrow goto out_free; 579dddfa461SMichael Halcrow } 580dddfa461SMichael Halcrow /* Version 3 (from RFC2440) - one byte */ 581dddfa461SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 582dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unknown version number " 583dddfa461SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 584dddfa461SMichael Halcrow rc = -EINVAL; 585dddfa461SMichael Halcrow goto out_free; 586dddfa461SMichael Halcrow } 587dddfa461SMichael Halcrow /* Read Signature */ 588dddfa461SMichael Halcrow ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 589dddfa461SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 590dddfa461SMichael Halcrow *packet_size += ECRYPTFS_SIG_SIZE; 591dddfa461SMichael Halcrow /* This byte is skipped because the kernel does not need to 592dddfa461SMichael Halcrow * know which public key encryption algorithm was used */ 593dddfa461SMichael Halcrow (*packet_size)++; 594dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 595dddfa461SMichael Halcrow body_size - (0x02 + ECRYPTFS_SIG_SIZE); 596dddfa461SMichael Halcrow if ((*new_auth_tok)->session_key.encrypted_key_size 597dddfa461SMichael Halcrow > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 598dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Tag 1 packet contains key larger " 599dddfa461SMichael Halcrow "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); 600dddfa461SMichael Halcrow rc = -EINVAL; 601dddfa461SMichael Halcrow goto out; 602dddfa461SMichael Halcrow } 603dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n", 604dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 605dddfa461SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 606dddfa461SMichael Halcrow &data[(*packet_size)], (body_size - 0x02 - ECRYPTFS_SIG_SIZE)); 607dddfa461SMichael Halcrow (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 608dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags &= 609dddfa461SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 610dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags |= 611dddfa461SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 612dddfa461SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 613e2bd99ecSMichael Halcrow (*new_auth_tok)->flags |= ECRYPTFS_PRIVATE_KEY; 614dddfa461SMichael Halcrow /* TODO: Why are we setting this flag here? Don't we want the 615dddfa461SMichael Halcrow * userspace to decrypt the session key? */ 616e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 617e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 618e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 619e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 620dddfa461SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 621dddfa461SMichael Halcrow goto out; 622dddfa461SMichael Halcrow out_free: 623dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 624dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 625dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 626dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 627dddfa461SMichael Halcrow auth_tok_list_item); 628dddfa461SMichael Halcrow out: 629dddfa461SMichael Halcrow if (rc) 630dddfa461SMichael Halcrow (*packet_size) = 0; 631dddfa461SMichael Halcrow return rc; 632dddfa461SMichael Halcrow } 633dddfa461SMichael Halcrow 634237fead6SMichael Halcrow /** 635237fead6SMichael Halcrow * parse_tag_3_packet 636237fead6SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 637237fead6SMichael Halcrow * contents. 638237fead6SMichael Halcrow * @data: The raw bytes of the packet. 639237fead6SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 640237fead6SMichael Halcrow * a new authentication token will be placed at the end 641237fead6SMichael Halcrow * of this list for this packet. 642237fead6SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 643237fead6SMichael Halcrow * allocates; sets the memory address of the pointer to 644237fead6SMichael Halcrow * NULL on error. This object is added to the 645237fead6SMichael Halcrow * auth_tok_list. 646237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 647237fead6SMichael Halcrow * into this memory location; zero on error. 648237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 649237fead6SMichael Halcrow * 650237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 651237fead6SMichael Halcrow */ 652237fead6SMichael Halcrow static int 653237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 654237fead6SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 655237fead6SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 656237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 657237fead6SMichael Halcrow { 658237fead6SMichael Halcrow size_t body_size; 659237fead6SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 660237fead6SMichael Halcrow size_t length_size; 661dddfa461SMichael Halcrow int rc = 0; 662237fead6SMichael Halcrow 663237fead6SMichael Halcrow (*packet_size) = 0; 664237fead6SMichael Halcrow (*new_auth_tok) = NULL; 665237fead6SMichael Halcrow 666237fead6SMichael Halcrow /* we check that: 667237fead6SMichael Halcrow * one byte for the Tag 3 ID flag 668237fead6SMichael Halcrow * two bytes for the body size 669237fead6SMichael Halcrow * do not exceed the maximum_packet_size 670237fead6SMichael Halcrow */ 671237fead6SMichael Halcrow if (unlikely((*packet_size) + 3 > max_packet_size)) { 672237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 673237fead6SMichael Halcrow rc = -EINVAL; 674237fead6SMichael Halcrow goto out; 675237fead6SMichael Halcrow } 676237fead6SMichael Halcrow 677237fead6SMichael Halcrow /* check for Tag 3 identifyer - one byte */ 678237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 679237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n", 680237fead6SMichael Halcrow ECRYPTFS_TAG_3_PACKET_TYPE); 681237fead6SMichael Halcrow rc = -EINVAL; 682237fead6SMichael Halcrow goto out; 683237fead6SMichael Halcrow } 684237fead6SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 685237fead6SMichael Halcrow * at end of function upon failure */ 686237fead6SMichael Halcrow auth_tok_list_item = 687c3762229SRobert P. J. Day kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 688237fead6SMichael Halcrow if (!auth_tok_list_item) { 689237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 690237fead6SMichael Halcrow rc = -ENOMEM; 691237fead6SMichael Halcrow goto out; 692237fead6SMichael Halcrow } 693237fead6SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 694237fead6SMichael Halcrow 695237fead6SMichael Halcrow /* check for body size - one to two bytes */ 696237fead6SMichael Halcrow rc = parse_packet_length(&data[(*packet_size)], &body_size, 697237fead6SMichael Halcrow &length_size); 698237fead6SMichael Halcrow if (rc) { 699237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 700237fead6SMichael Halcrow "rc = [%d]\n", rc); 701237fead6SMichael Halcrow goto out_free; 702237fead6SMichael Halcrow } 703237fead6SMichael Halcrow if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) { 704237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", 705237fead6SMichael Halcrow body_size); 706237fead6SMichael Halcrow rc = -EINVAL; 707237fead6SMichael Halcrow goto out_free; 708237fead6SMichael Halcrow } 709237fead6SMichael Halcrow (*packet_size) += length_size; 710237fead6SMichael Halcrow 711237fead6SMichael Halcrow /* now we know the length of the remainting Tag 3 packet size: 712237fead6SMichael Halcrow * 5 fix bytes for: version string, cipher, S2K ID, hash algo, 713237fead6SMichael Halcrow * number of hash iterations 714237fead6SMichael Halcrow * ECRYPTFS_SALT_SIZE bytes for salt 715237fead6SMichael Halcrow * body_size bytes minus the stuff above is the encrypted key size 716237fead6SMichael Halcrow */ 717237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 718237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 719237fead6SMichael Halcrow rc = -EINVAL; 720237fead6SMichael Halcrow goto out_free; 721237fead6SMichael Halcrow } 722237fead6SMichael Halcrow 723237fead6SMichael Halcrow /* There are 5 characters of additional information in the 724237fead6SMichael Halcrow * packet */ 725237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 726237fead6SMichael Halcrow body_size - (0x05 + ECRYPTFS_SALT_SIZE); 727237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n", 728237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 729237fead6SMichael Halcrow 730237fead6SMichael Halcrow /* Version 4 (from RFC2440) - one byte */ 731237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x04)) { 732237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Unknown version number " 733237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 734237fead6SMichael Halcrow rc = -EINVAL; 735237fead6SMichael Halcrow goto out_free; 736237fead6SMichael Halcrow } 737237fead6SMichael Halcrow 738237fead6SMichael Halcrow /* cipher - one byte */ 739237fead6SMichael Halcrow ecryptfs_cipher_code_to_string(crypt_stat->cipher, 740237fead6SMichael Halcrow (u16)data[(*packet_size)]); 741237fead6SMichael Halcrow /* A little extra work to differentiate among the AES key 742237fead6SMichael Halcrow * sizes; see RFC2440 */ 743237fead6SMichael Halcrow switch(data[(*packet_size)++]) { 744237fead6SMichael Halcrow case RFC2440_CIPHER_AES_192: 745237fead6SMichael Halcrow crypt_stat->key_size = 24; 746237fead6SMichael Halcrow break; 747237fead6SMichael Halcrow default: 748237fead6SMichael Halcrow crypt_stat->key_size = 749237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 750237fead6SMichael Halcrow } 751237fead6SMichael Halcrow ecryptfs_init_crypt_ctx(crypt_stat); 752237fead6SMichael Halcrow /* S2K identifier 3 (from RFC2440) */ 753237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 754237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently " 755237fead6SMichael Halcrow "supported\n"); 756237fead6SMichael Halcrow rc = -ENOSYS; 757237fead6SMichael Halcrow goto out_free; 758237fead6SMichael Halcrow } 759237fead6SMichael Halcrow 760237fead6SMichael Halcrow /* TODO: finish the hash mapping */ 761237fead6SMichael Halcrow /* hash algorithm - one byte */ 762237fead6SMichael Halcrow switch (data[(*packet_size)++]) { 763237fead6SMichael Halcrow case 0x01: /* See RFC2440 for these numbers and their mappings */ 764237fead6SMichael Halcrow /* Choose MD5 */ 765237fead6SMichael Halcrow /* salt - ECRYPTFS_SALT_SIZE bytes */ 766237fead6SMichael Halcrow memcpy((*new_auth_tok)->token.password.salt, 767237fead6SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 768237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; 769237fead6SMichael Halcrow 770237fead6SMichael Halcrow /* This conversion was taken straight from RFC2440 */ 771237fead6SMichael Halcrow /* number of hash iterations - one byte */ 772237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_iterations = 773237fead6SMichael Halcrow ((u32) 16 + (data[(*packet_size)] & 15)) 774237fead6SMichael Halcrow << ((data[(*packet_size)] >> 4) + 6); 775237fead6SMichael Halcrow (*packet_size)++; 776237fead6SMichael Halcrow 777237fead6SMichael Halcrow /* encrypted session key - 778237fead6SMichael Halcrow * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */ 779237fead6SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 780237fead6SMichael Halcrow &data[(*packet_size)], 781237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 782237fead6SMichael Halcrow (*packet_size) += 783237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 784237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags &= 785237fead6SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 786237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags |= 787237fead6SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 788237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_algo = 0x01; 789237fead6SMichael Halcrow break; 790237fead6SMichael Halcrow default: 791237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 792237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 793237fead6SMichael Halcrow rc = -ENOSYS; 794237fead6SMichael Halcrow goto out_free; 795237fead6SMichael Halcrow } 796237fead6SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 797237fead6SMichael Halcrow /* TODO: Parametarize; we might actually want userspace to 798237fead6SMichael Halcrow * decrypt the session key. */ 799e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 800e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 801e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 802e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 803237fead6SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 804237fead6SMichael Halcrow goto out; 805237fead6SMichael Halcrow out_free: 806237fead6SMichael Halcrow (*new_auth_tok) = NULL; 807237fead6SMichael Halcrow memset(auth_tok_list_item, 0, 808237fead6SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 809237fead6SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 810237fead6SMichael Halcrow auth_tok_list_item); 811237fead6SMichael Halcrow out: 812237fead6SMichael Halcrow if (rc) 813237fead6SMichael Halcrow (*packet_size) = 0; 814237fead6SMichael Halcrow return rc; 815237fead6SMichael Halcrow } 816237fead6SMichael Halcrow 817237fead6SMichael Halcrow /** 818237fead6SMichael Halcrow * parse_tag_11_packet 819237fead6SMichael Halcrow * @data: The raw bytes of the packet 820237fead6SMichael Halcrow * @contents: This function writes the data contents of the literal 821237fead6SMichael Halcrow * packet into this memory location 822237fead6SMichael Halcrow * @max_contents_bytes: The maximum number of bytes that this function 823237fead6SMichael Halcrow * is allowed to write into contents 824237fead6SMichael Halcrow * @tag_11_contents_size: This function writes the size of the parsed 825237fead6SMichael Halcrow * contents into this memory location; zero on 826237fead6SMichael Halcrow * error 827237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 828237fead6SMichael Halcrow * into this memory location; zero on error 829237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 830237fead6SMichael Halcrow * 831237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 832237fead6SMichael Halcrow */ 833237fead6SMichael Halcrow static int 834237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents, 835237fead6SMichael Halcrow size_t max_contents_bytes, size_t *tag_11_contents_size, 836237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 837237fead6SMichael Halcrow { 838237fead6SMichael Halcrow size_t body_size; 839237fead6SMichael Halcrow size_t length_size; 840dddfa461SMichael Halcrow int rc = 0; 841237fead6SMichael Halcrow 842237fead6SMichael Halcrow (*packet_size) = 0; 843237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 844237fead6SMichael Halcrow 845237fead6SMichael Halcrow /* check that: 846237fead6SMichael Halcrow * one byte for the Tag 11 ID flag 847237fead6SMichael Halcrow * two bytes for the Tag 11 length 848237fead6SMichael Halcrow * do not exceed the maximum_packet_size 849237fead6SMichael Halcrow */ 850237fead6SMichael Halcrow if (unlikely((*packet_size) + 3 > max_packet_size)) { 851237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 852237fead6SMichael Halcrow rc = -EINVAL; 853237fead6SMichael Halcrow goto out; 854237fead6SMichael Halcrow } 855237fead6SMichael Halcrow 856237fead6SMichael Halcrow /* check for Tag 11 identifyer - one byte */ 857237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 858237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 859237fead6SMichael Halcrow "Invalid tag 11 packet format\n"); 860237fead6SMichael Halcrow rc = -EINVAL; 861237fead6SMichael Halcrow goto out; 862237fead6SMichael Halcrow } 863237fead6SMichael Halcrow 864237fead6SMichael Halcrow /* get Tag 11 content length - one or two bytes */ 865237fead6SMichael Halcrow rc = parse_packet_length(&data[(*packet_size)], &body_size, 866237fead6SMichael Halcrow &length_size); 867237fead6SMichael Halcrow if (rc) { 868237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 869237fead6SMichael Halcrow "Invalid tag 11 packet format\n"); 870237fead6SMichael Halcrow goto out; 871237fead6SMichael Halcrow } 872237fead6SMichael Halcrow (*packet_size) += length_size; 873237fead6SMichael Halcrow 874237fead6SMichael Halcrow if (body_size < 13) { 875237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", 876237fead6SMichael Halcrow body_size); 877237fead6SMichael Halcrow rc = -EINVAL; 878237fead6SMichael Halcrow goto out; 879237fead6SMichael Halcrow } 880237fead6SMichael Halcrow /* We have 13 bytes of surrounding packet values */ 881237fead6SMichael Halcrow (*tag_11_contents_size) = (body_size - 13); 882237fead6SMichael Halcrow 883237fead6SMichael Halcrow /* now we know the length of the remainting Tag 11 packet size: 884237fead6SMichael Halcrow * 14 fix bytes for: special flag one, special flag two, 885237fead6SMichael Halcrow * 12 skipped bytes 886237fead6SMichael Halcrow * body_size bytes minus the stuff above is the Tag 11 content 887237fead6SMichael Halcrow */ 888237fead6SMichael Halcrow /* FIXME why is the body size one byte smaller than the actual 889237fead6SMichael Halcrow * size of the body? 890237fead6SMichael Halcrow * this seems to be an error here as well as in 891237fead6SMichael Halcrow * write_tag_11_packet() */ 892237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 893237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); 894237fead6SMichael Halcrow rc = -EINVAL; 895237fead6SMichael Halcrow goto out; 896237fead6SMichael Halcrow } 897237fead6SMichael Halcrow 898237fead6SMichael Halcrow /* special flag one - one byte */ 899237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x62) { 900237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); 901237fead6SMichael Halcrow rc = -EINVAL; 902237fead6SMichael Halcrow goto out; 903237fead6SMichael Halcrow } 904237fead6SMichael Halcrow 905237fead6SMichael Halcrow /* special flag two - one byte */ 906237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x08) { 907237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); 908237fead6SMichael Halcrow rc = -EINVAL; 909237fead6SMichael Halcrow goto out; 910237fead6SMichael Halcrow } 911237fead6SMichael Halcrow 912237fead6SMichael Halcrow /* skip the next 12 bytes */ 913237fead6SMichael Halcrow (*packet_size) += 12; /* We don't care about the filename or 914237fead6SMichael Halcrow * the timestamp */ 915237fead6SMichael Halcrow 916237fead6SMichael Halcrow /* get the Tag 11 contents - tag_11_contents_size bytes */ 917237fead6SMichael Halcrow memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 918237fead6SMichael Halcrow (*packet_size) += (*tag_11_contents_size); 919237fead6SMichael Halcrow 920237fead6SMichael Halcrow out: 921237fead6SMichael Halcrow if (rc) { 922237fead6SMichael Halcrow (*packet_size) = 0; 923237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 924237fead6SMichael Halcrow } 925237fead6SMichael Halcrow return rc; 926237fead6SMichael Halcrow } 927237fead6SMichael Halcrow 928*f4aad16aSMichael Halcrow static int 929*f4aad16aSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig( 930*f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok **global_auth_tok, 931*f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 932*f4aad16aSMichael Halcrow { 933*f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *walker; 934*f4aad16aSMichael Halcrow int rc = 0; 935*f4aad16aSMichael Halcrow 936*f4aad16aSMichael Halcrow (*global_auth_tok) = NULL; 937*f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 938*f4aad16aSMichael Halcrow list_for_each_entry(walker, 939*f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 940*f4aad16aSMichael Halcrow mount_crypt_stat_list) { 941*f4aad16aSMichael Halcrow if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { 942*f4aad16aSMichael Halcrow (*global_auth_tok) = walker; 943*f4aad16aSMichael Halcrow goto out; 944*f4aad16aSMichael Halcrow } 945*f4aad16aSMichael Halcrow } 946*f4aad16aSMichael Halcrow rc = -EINVAL; 947*f4aad16aSMichael Halcrow out: 948*f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 949*f4aad16aSMichael Halcrow return rc; 950*f4aad16aSMichael Halcrow } 951*f4aad16aSMichael Halcrow 952237fead6SMichael Halcrow /** 953*f4aad16aSMichael Halcrow * ecryptfs_verify_version 954*f4aad16aSMichael Halcrow * @version: The version number to confirm 955*f4aad16aSMichael Halcrow * 956*f4aad16aSMichael Halcrow * Returns zero on good version; non-zero otherwise 957*f4aad16aSMichael Halcrow */ 958*f4aad16aSMichael Halcrow static int ecryptfs_verify_version(u16 version) 959*f4aad16aSMichael Halcrow { 960*f4aad16aSMichael Halcrow int rc = 0; 961*f4aad16aSMichael Halcrow unsigned char major; 962*f4aad16aSMichael Halcrow unsigned char minor; 963*f4aad16aSMichael Halcrow 964*f4aad16aSMichael Halcrow major = ((version >> 8) & 0xFF); 965*f4aad16aSMichael Halcrow minor = (version & 0xFF); 966*f4aad16aSMichael Halcrow if (major != ECRYPTFS_VERSION_MAJOR) { 967*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 968*f4aad16aSMichael Halcrow "Expected [%d]; got [%d]\n", 969*f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MAJOR, major); 970*f4aad16aSMichael Halcrow rc = -EINVAL; 971*f4aad16aSMichael Halcrow goto out; 972*f4aad16aSMichael Halcrow } 973*f4aad16aSMichael Halcrow if (minor != ECRYPTFS_VERSION_MINOR) { 974*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 975*f4aad16aSMichael Halcrow "Expected [%d]; got [%d]\n", 976*f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MINOR, minor); 977*f4aad16aSMichael Halcrow rc = -EINVAL; 978*f4aad16aSMichael Halcrow goto out; 979*f4aad16aSMichael Halcrow } 980*f4aad16aSMichael Halcrow out: 981*f4aad16aSMichael Halcrow return rc; 982*f4aad16aSMichael Halcrow } 983*f4aad16aSMichael Halcrow 984*f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 985*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 986*f4aad16aSMichael Halcrow char *sig) 987*f4aad16aSMichael Halcrow { 988*f4aad16aSMichael Halcrow int rc = 0; 989*f4aad16aSMichael Halcrow 990*f4aad16aSMichael Halcrow (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 991*f4aad16aSMichael Halcrow if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 992*f4aad16aSMichael Halcrow printk(KERN_ERR "Could not find key with description: [%s]\n", 993*f4aad16aSMichael Halcrow sig); 994*f4aad16aSMichael Halcrow process_request_key_err(PTR_ERR(*auth_tok_key)); 995*f4aad16aSMichael Halcrow rc = -EINVAL; 996*f4aad16aSMichael Halcrow goto out; 997*f4aad16aSMichael Halcrow } 998*f4aad16aSMichael Halcrow (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); 999*f4aad16aSMichael Halcrow if (ecryptfs_verify_version((*auth_tok)->version)) { 1000*f4aad16aSMichael Halcrow printk(KERN_ERR 1001*f4aad16aSMichael Halcrow "Data structure version mismatch. " 1002*f4aad16aSMichael Halcrow "Userspace tools must match eCryptfs " 1003*f4aad16aSMichael Halcrow "kernel module with major version [%d] " 1004*f4aad16aSMichael Halcrow "and minor version [%d]\n", 1005*f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MAJOR, 1006*f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MINOR); 1007*f4aad16aSMichael Halcrow rc = -EINVAL; 1008*f4aad16aSMichael Halcrow goto out; 1009*f4aad16aSMichael Halcrow } 1010*f4aad16aSMichael Halcrow if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 1011*f4aad16aSMichael Halcrow && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 1012*f4aad16aSMichael Halcrow printk(KERN_ERR "Invalid auth_tok structure " 1013*f4aad16aSMichael Halcrow "returned from key query\n"); 1014*f4aad16aSMichael Halcrow rc = -EINVAL; 1015*f4aad16aSMichael Halcrow goto out; 1016*f4aad16aSMichael Halcrow } 1017*f4aad16aSMichael Halcrow out: 1018*f4aad16aSMichael Halcrow return rc; 1019*f4aad16aSMichael Halcrow } 1020*f4aad16aSMichael Halcrow 1021*f4aad16aSMichael Halcrow /** 1022*f4aad16aSMichael Halcrow * ecryptfs_find_auth_tok_for_sig 1023*f4aad16aSMichael Halcrow * @auth_tok: Set to the matching auth_tok; NULL if not found 1024*f4aad16aSMichael Halcrow * @crypt_stat: inode crypt_stat crypto context 1025*f4aad16aSMichael Halcrow * @sig: Sig of auth_tok to find 1026*f4aad16aSMichael Halcrow * 1027*f4aad16aSMichael Halcrow * For now, this function simply looks at the registered auth_tok's 1028*f4aad16aSMichael Halcrow * linked off the mount_crypt_stat, so all the auth_toks that can be 1029*f4aad16aSMichael Halcrow * used must be registered at mount time. This function could 1030*f4aad16aSMichael Halcrow * potentially try a lot harder to find auth_tok's (e.g., by calling 1031*f4aad16aSMichael Halcrow * out to ecryptfsd to dynamically retrieve an auth_tok object) so 1032*f4aad16aSMichael Halcrow * that static registration of auth_tok's will no longer be necessary. 1033*f4aad16aSMichael Halcrow * 1034*f4aad16aSMichael Halcrow * Returns zero on no error; non-zero on error 1035*f4aad16aSMichael Halcrow */ 1036*f4aad16aSMichael Halcrow static int 1037*f4aad16aSMichael Halcrow ecryptfs_find_auth_tok_for_sig( 1038*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 1039*f4aad16aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, char *sig) 1040*f4aad16aSMichael Halcrow { 1041*f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1042*f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 1043*f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *global_auth_tok; 1044*f4aad16aSMichael Halcrow int rc = 0; 1045*f4aad16aSMichael Halcrow 1046*f4aad16aSMichael Halcrow (*auth_tok) = NULL; 1047*f4aad16aSMichael Halcrow if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 1048*f4aad16aSMichael Halcrow mount_crypt_stat, sig)) { 1049*f4aad16aSMichael Halcrow struct key *auth_tok_key; 1050*f4aad16aSMichael Halcrow 1051*f4aad16aSMichael Halcrow rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, 1052*f4aad16aSMichael Halcrow sig); 1053*f4aad16aSMichael Halcrow } else 1054*f4aad16aSMichael Halcrow (*auth_tok) = global_auth_tok->global_auth_tok; 1055*f4aad16aSMichael Halcrow return rc; 1056*f4aad16aSMichael Halcrow } 1057*f4aad16aSMichael Halcrow 1058*f4aad16aSMichael Halcrow /** 1059*f4aad16aSMichael Halcrow * decrypt_passphrase_encrypted_session_key - Decrypt the session key 1060*f4aad16aSMichael Halcrow * with the given auth_tok. 1061237fead6SMichael Halcrow * 1062237fead6SMichael Halcrow * Returns Zero on success; non-zero error otherwise. 1063237fead6SMichael Halcrow */ 1064*f4aad16aSMichael Halcrow static int 1065*f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1066237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1067237fead6SMichael Halcrow { 1068*f4aad16aSMichael Halcrow struct scatterlist dst_sg; 1069*f4aad16aSMichael Halcrow struct scatterlist src_sg; 1070237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 10718bba066fSMichael Halcrow struct blkcipher_desc desc = { 10728bba066fSMichael Halcrow .flags = CRYPTO_TFM_REQ_MAY_SLEEP 10738bba066fSMichael Halcrow }; 10748bba066fSMichael Halcrow int rc = 0; 1075237fead6SMichael Halcrow 1076*f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1077*f4aad16aSMichael Halcrow ecryptfs_printk( 1078*f4aad16aSMichael Halcrow KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1079*f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1080*f4aad16aSMichael Halcrow ecryptfs_dump_hex( 1081*f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1082*f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1083*f4aad16aSMichael Halcrow } 1084*f4aad16aSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 1085*f4aad16aSMichael Halcrow crypt_stat->cipher); 1086*f4aad16aSMichael Halcrow if (unlikely(rc)) { 1087*f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1088*f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1089*f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1090237fead6SMichael Halcrow goto out; 1091237fead6SMichael Halcrow } 1092*f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1093*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size, 1094*f4aad16aSMichael Halcrow &src_sg, 1)) != 1) { 1095*f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1096*f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key to scatterlist; " 1097*f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1098*f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1099*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size); 1100*f4aad16aSMichael Halcrow goto out; 1101237fead6SMichael Halcrow } 1102*f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size = 1103*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size; 1104*f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1105*f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size, 1106*f4aad16aSMichael Halcrow &dst_sg, 1)) != 1) { 1107*f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1108*f4aad16aSMichael Halcrow "auth_tok->session_key.decrypted_key to scatterlist; " 1109*f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]\n", rc); 1110*f4aad16aSMichael Halcrow goto out; 1111*f4aad16aSMichael Halcrow } 1112237fead6SMichael Halcrow mutex_lock(tfm_mutex); 1113*f4aad16aSMichael Halcrow rc = crypto_blkcipher_setkey( 1114*f4aad16aSMichael Halcrow desc.tfm, auth_tok->token.password.session_key_encryption_key, 1115237fead6SMichael Halcrow crypt_stat->key_size); 1116*f4aad16aSMichael Halcrow if (unlikely(rc < 0)) { 1117*f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1118e5d9cbdeSMichael Halcrow printk(KERN_ERR "Error setting key for crypto context\n"); 1119e5d9cbdeSMichael Halcrow rc = -EINVAL; 1120*f4aad16aSMichael Halcrow goto out; 1121e5d9cbdeSMichael Halcrow } 1122*f4aad16aSMichael Halcrow rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, 1123237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size); 1124*f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1125*f4aad16aSMichael Halcrow if (unlikely(rc)) { 11268bba066fSMichael Halcrow printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1127*f4aad16aSMichael Halcrow goto out; 11288bba066fSMichael Halcrow } 1129237fead6SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1130237fead6SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1131237fead6SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1132e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1133*f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1134*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", 1135*f4aad16aSMichael Halcrow crypt_stat->key_size); 1136237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1137237fead6SMichael Halcrow crypt_stat->key_size); 1138*f4aad16aSMichael Halcrow } 1139237fead6SMichael Halcrow out: 1140237fead6SMichael Halcrow return rc; 1141237fead6SMichael Halcrow } 1142237fead6SMichael Halcrow 1143*f4aad16aSMichael Halcrow int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1144*f4aad16aSMichael Halcrow { 1145*f4aad16aSMichael Halcrow int rc = 0; 1146*f4aad16aSMichael Halcrow 1147*f4aad16aSMichael Halcrow (*sig) = NULL; 1148*f4aad16aSMichael Halcrow switch (auth_tok->token_type) { 1149*f4aad16aSMichael Halcrow case ECRYPTFS_PASSWORD: 1150*f4aad16aSMichael Halcrow (*sig) = auth_tok->token.password.signature; 1151*f4aad16aSMichael Halcrow break; 1152*f4aad16aSMichael Halcrow case ECRYPTFS_PRIVATE_KEY: 1153*f4aad16aSMichael Halcrow (*sig) = auth_tok->token.private_key.signature; 1154*f4aad16aSMichael Halcrow break; 1155*f4aad16aSMichael Halcrow default: 1156*f4aad16aSMichael Halcrow printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1157*f4aad16aSMichael Halcrow auth_tok->token_type); 1158*f4aad16aSMichael Halcrow rc = -EINVAL; 1159*f4aad16aSMichael Halcrow } 1160*f4aad16aSMichael Halcrow return rc; 1161*f4aad16aSMichael Halcrow } 1162*f4aad16aSMichael Halcrow 1163237fead6SMichael Halcrow /** 1164237fead6SMichael Halcrow * ecryptfs_parse_packet_set 1165237fead6SMichael Halcrow * @dest: The header page in memory 1166237fead6SMichael Halcrow * @version: Version of file format, to guide parsing behavior 1167237fead6SMichael Halcrow * 1168237fead6SMichael Halcrow * Get crypt_stat to have the file's session key if the requisite key 1169237fead6SMichael Halcrow * is available to decrypt the session key. 1170237fead6SMichael Halcrow * 1171237fead6SMichael Halcrow * Returns Zero if a valid authentication token was retrieved and 1172237fead6SMichael Halcrow * processed; negative value for file not encrypted or for error 1173237fead6SMichael Halcrow * conditions. 1174237fead6SMichael Halcrow */ 1175237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1176237fead6SMichael Halcrow unsigned char *src, 1177237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1178237fead6SMichael Halcrow { 1179237fead6SMichael Halcrow size_t i = 0; 1180*f4aad16aSMichael Halcrow size_t found_auth_tok; 1181237fead6SMichael Halcrow size_t next_packet_is_auth_tok_packet; 1182237fead6SMichael Halcrow struct list_head auth_tok_list; 1183*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *matching_auth_tok = NULL; 1184237fead6SMichael Halcrow struct ecryptfs_auth_tok *candidate_auth_tok = NULL; 1185*f4aad16aSMichael Halcrow char *candidate_auth_tok_sig; 1186237fead6SMichael Halcrow size_t packet_size; 1187237fead6SMichael Halcrow struct ecryptfs_auth_tok *new_auth_tok; 1188237fead6SMichael Halcrow unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1189*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1190237fead6SMichael Halcrow size_t tag_11_contents_size; 1191237fead6SMichael Halcrow size_t tag_11_packet_size; 1192dddfa461SMichael Halcrow int rc = 0; 1193237fead6SMichael Halcrow 1194237fead6SMichael Halcrow INIT_LIST_HEAD(&auth_tok_list); 1195*f4aad16aSMichael Halcrow /* Parse the header to find as many packets as we can; these will be 1196237fead6SMichael Halcrow * added the our &auth_tok_list */ 1197237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 1; 1198237fead6SMichael Halcrow while (next_packet_is_auth_tok_packet) { 1199237fead6SMichael Halcrow size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); 1200237fead6SMichael Halcrow 1201237fead6SMichael Halcrow switch (src[i]) { 1202237fead6SMichael Halcrow case ECRYPTFS_TAG_3_PACKET_TYPE: 1203237fead6SMichael Halcrow rc = parse_tag_3_packet(crypt_stat, 1204237fead6SMichael Halcrow (unsigned char *)&src[i], 1205237fead6SMichael Halcrow &auth_tok_list, &new_auth_tok, 1206237fead6SMichael Halcrow &packet_size, max_packet_size); 1207237fead6SMichael Halcrow if (rc) { 1208237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1209237fead6SMichael Halcrow "tag 3 packet\n"); 1210237fead6SMichael Halcrow rc = -EIO; 1211237fead6SMichael Halcrow goto out_wipe_list; 1212237fead6SMichael Halcrow } 1213237fead6SMichael Halcrow i += packet_size; 1214237fead6SMichael Halcrow rc = parse_tag_11_packet((unsigned char *)&src[i], 1215237fead6SMichael Halcrow sig_tmp_space, 1216237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1217237fead6SMichael Halcrow &tag_11_contents_size, 1218237fead6SMichael Halcrow &tag_11_packet_size, 1219237fead6SMichael Halcrow max_packet_size); 1220237fead6SMichael Halcrow if (rc) { 1221237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No valid " 1222237fead6SMichael Halcrow "(ecryptfs-specific) literal " 1223237fead6SMichael Halcrow "packet containing " 1224237fead6SMichael Halcrow "authentication token " 1225237fead6SMichael Halcrow "signature found after " 1226237fead6SMichael Halcrow "tag 3 packet\n"); 1227237fead6SMichael Halcrow rc = -EIO; 1228237fead6SMichael Halcrow goto out_wipe_list; 1229237fead6SMichael Halcrow } 1230237fead6SMichael Halcrow i += tag_11_packet_size; 1231237fead6SMichael Halcrow if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1232237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Expected " 1233237fead6SMichael Halcrow "signature of size [%d]; " 1234237fead6SMichael Halcrow "read size [%d]\n", 1235237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1236237fead6SMichael Halcrow tag_11_contents_size); 1237237fead6SMichael Halcrow rc = -EIO; 1238237fead6SMichael Halcrow goto out_wipe_list; 1239237fead6SMichael Halcrow } 1240237fead6SMichael Halcrow ecryptfs_to_hex(new_auth_tok->token.password.signature, 1241237fead6SMichael Halcrow sig_tmp_space, tag_11_contents_size); 1242237fead6SMichael Halcrow new_auth_tok->token.password.signature[ 1243237fead6SMichael Halcrow ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1244e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1245237fead6SMichael Halcrow break; 1246dddfa461SMichael Halcrow case ECRYPTFS_TAG_1_PACKET_TYPE: 1247dddfa461SMichael Halcrow rc = parse_tag_1_packet(crypt_stat, 1248dddfa461SMichael Halcrow (unsigned char *)&src[i], 1249dddfa461SMichael Halcrow &auth_tok_list, &new_auth_tok, 1250dddfa461SMichael Halcrow &packet_size, max_packet_size); 1251dddfa461SMichael Halcrow if (rc) { 1252dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1253dddfa461SMichael Halcrow "tag 1 packet\n"); 1254dddfa461SMichael Halcrow rc = -EIO; 1255dddfa461SMichael Halcrow goto out_wipe_list; 1256dddfa461SMichael Halcrow } 1257dddfa461SMichael Halcrow i += packet_size; 1258e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1259dddfa461SMichael Halcrow break; 1260237fead6SMichael Halcrow case ECRYPTFS_TAG_11_PACKET_TYPE: 1261237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1262237fead6SMichael Halcrow "(Tag 11 not allowed by itself)\n"); 1263237fead6SMichael Halcrow rc = -EIO; 1264237fead6SMichael Halcrow goto out_wipe_list; 1265237fead6SMichael Halcrow break; 1266237fead6SMichael Halcrow default: 1267237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "No packet at offset " 1268237fead6SMichael Halcrow "[%d] of the file header; hex value of " 1269237fead6SMichael Halcrow "character is [0x%.2x]\n", i, src[i]); 1270237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 0; 1271237fead6SMichael Halcrow } 1272237fead6SMichael Halcrow } 1273237fead6SMichael Halcrow if (list_empty(&auth_tok_list)) { 1274*f4aad16aSMichael Halcrow printk(KERN_ERR "The lower file appears to be a non-encrypted " 1275*f4aad16aSMichael Halcrow "eCryptfs file; this is not supported in this version " 1276*f4aad16aSMichael Halcrow "of the eCryptfs kernel module\n"); 1277*f4aad16aSMichael Halcrow rc = -EINVAL; 1278237fead6SMichael Halcrow goto out; 1279237fead6SMichael Halcrow } 1280*f4aad16aSMichael Halcrow /* auth_tok_list contains the set of authentication tokens 1281*f4aad16aSMichael Halcrow * parsed from the metadata. We need to find a matching 1282*f4aad16aSMichael Halcrow * authentication token that has the secret component(s) 1283*f4aad16aSMichael Halcrow * necessary to decrypt the EFEK in the auth_tok parsed from 1284*f4aad16aSMichael Halcrow * the metadata. There may be several potential matches, but 1285*f4aad16aSMichael Halcrow * just one will be sufficient to decrypt to get the FEK. */ 1286*f4aad16aSMichael Halcrow find_next_matching_auth_tok: 1287*f4aad16aSMichael Halcrow found_auth_tok = 0; 1288*f4aad16aSMichael Halcrow list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1289237fead6SMichael Halcrow candidate_auth_tok = &auth_tok_list_item->auth_tok; 1290237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1291237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1292237fead6SMichael Halcrow "Considering cadidate auth tok:\n"); 1293237fead6SMichael Halcrow ecryptfs_dump_auth_tok(candidate_auth_tok); 1294237fead6SMichael Halcrow } 1295*f4aad16aSMichael Halcrow if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 1296*f4aad16aSMichael Halcrow candidate_auth_tok))) { 1297*f4aad16aSMichael Halcrow printk(KERN_ERR 1298*f4aad16aSMichael Halcrow "Unrecognized candidate auth tok type: [%d]\n", 1299*f4aad16aSMichael Halcrow candidate_auth_tok->token_type); 1300*f4aad16aSMichael Halcrow rc = -EINVAL; 1301*f4aad16aSMichael Halcrow goto out_wipe_list; 1302*f4aad16aSMichael Halcrow } 1303*f4aad16aSMichael Halcrow if ((rc = ecryptfs_find_auth_tok_for_sig( 1304*f4aad16aSMichael Halcrow &matching_auth_tok, crypt_stat, 1305*f4aad16aSMichael Halcrow candidate_auth_tok_sig))) 1306*f4aad16aSMichael Halcrow rc = 0; 1307*f4aad16aSMichael Halcrow if (matching_auth_tok) { 1308237fead6SMichael Halcrow found_auth_tok = 1; 1309*f4aad16aSMichael Halcrow goto found_matching_auth_tok; 1310237fead6SMichael Halcrow } 1311237fead6SMichael Halcrow } 1312237fead6SMichael Halcrow if (!found_auth_tok) { 1313*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Could not find a usable " 1314*f4aad16aSMichael Halcrow "authentication token\n"); 1315237fead6SMichael Halcrow rc = -EIO; 1316237fead6SMichael Halcrow goto out_wipe_list; 1317dddfa461SMichael Halcrow } 1318*f4aad16aSMichael Halcrow found_matching_auth_tok: 1319e2bd99ecSMichael Halcrow if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1320dddfa461SMichael Halcrow memcpy(&(candidate_auth_tok->token.private_key), 1321*f4aad16aSMichael Halcrow &(matching_auth_tok->token.private_key), 1322dddfa461SMichael Halcrow sizeof(struct ecryptfs_private_key)); 1323*f4aad16aSMichael Halcrow rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1324dddfa461SMichael Halcrow crypt_stat); 1325dddfa461SMichael Halcrow } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1326237fead6SMichael Halcrow memcpy(&(candidate_auth_tok->token.password), 1327*f4aad16aSMichael Halcrow &(matching_auth_tok->token.password), 1328237fead6SMichael Halcrow sizeof(struct ecryptfs_password)); 1329*f4aad16aSMichael Halcrow rc = decrypt_passphrase_encrypted_session_key( 1330*f4aad16aSMichael Halcrow candidate_auth_tok, crypt_stat); 1331dddfa461SMichael Halcrow } 1332237fead6SMichael Halcrow if (rc) { 1333*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1334*f4aad16aSMichael Halcrow 1335*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1336*f4aad16aSMichael Halcrow "session key for authentication token with sig " 1337*f4aad16aSMichael Halcrow "[%.*s]; rc = [%d]. Removing auth tok " 1338*f4aad16aSMichael Halcrow "candidate from the list and searching for " 1339*f4aad16aSMichael Halcrow "the next match.\n", candidate_auth_tok_sig, 1340*f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE_HEX, rc); 1341*f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, 1342*f4aad16aSMichael Halcrow auth_tok_list_item_tmp, 1343*f4aad16aSMichael Halcrow &auth_tok_list, list) { 1344*f4aad16aSMichael Halcrow if (candidate_auth_tok 1345*f4aad16aSMichael Halcrow == &auth_tok_list_item->auth_tok) { 1346*f4aad16aSMichael Halcrow list_del(&auth_tok_list_item->list); 1347*f4aad16aSMichael Halcrow kmem_cache_free( 1348*f4aad16aSMichael Halcrow ecryptfs_auth_tok_list_item_cache, 1349*f4aad16aSMichael Halcrow auth_tok_list_item); 1350*f4aad16aSMichael Halcrow goto find_next_matching_auth_tok; 1351*f4aad16aSMichael Halcrow } 1352*f4aad16aSMichael Halcrow } 1353*f4aad16aSMichael Halcrow BUG(); 1354237fead6SMichael Halcrow } 1355237fead6SMichael Halcrow rc = ecryptfs_compute_root_iv(crypt_stat); 1356237fead6SMichael Halcrow if (rc) { 1357237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error computing " 1358237fead6SMichael Halcrow "the root IV\n"); 1359237fead6SMichael Halcrow goto out_wipe_list; 1360237fead6SMichael Halcrow } 1361237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 1362237fead6SMichael Halcrow if (rc) { 1363237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1364237fead6SMichael Halcrow "context for cipher [%s]; rc = [%d]\n", 1365237fead6SMichael Halcrow crypt_stat->cipher, rc); 1366237fead6SMichael Halcrow } 1367237fead6SMichael Halcrow out_wipe_list: 1368237fead6SMichael Halcrow wipe_auth_tok_list(&auth_tok_list); 1369237fead6SMichael Halcrow out: 1370237fead6SMichael Halcrow return rc; 1371237fead6SMichael Halcrow } 1372*f4aad16aSMichael Halcrow 1373dddfa461SMichael Halcrow static int 1374dddfa461SMichael Halcrow pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, 1375dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1376dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec) 1377dddfa461SMichael Halcrow { 1378dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx = NULL; 1379dddfa461SMichael Halcrow char *netlink_payload; 1380dddfa461SMichael Halcrow size_t netlink_payload_length; 1381dddfa461SMichael Halcrow struct ecryptfs_message *msg; 1382dddfa461SMichael Halcrow int rc; 1383dddfa461SMichael Halcrow 1384dddfa461SMichael Halcrow rc = write_tag_66_packet(auth_tok->token.private_key.signature, 1385dddfa461SMichael Halcrow ecryptfs_code_for_cipher_string(crypt_stat), 1386dddfa461SMichael Halcrow crypt_stat, &netlink_payload, 1387dddfa461SMichael Halcrow &netlink_payload_length); 1388dddfa461SMichael Halcrow if (rc) { 1389dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 1390dddfa461SMichael Halcrow goto out; 1391dddfa461SMichael Halcrow } 1392dddfa461SMichael Halcrow rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload, 1393dddfa461SMichael Halcrow netlink_payload_length, &msg_ctx); 1394dddfa461SMichael Halcrow if (rc) { 1395dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); 1396dddfa461SMichael Halcrow goto out; 1397dddfa461SMichael Halcrow } 1398dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1399dddfa461SMichael Halcrow if (rc) { 1400dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 1401dddfa461SMichael Halcrow "from the user space daemon\n"); 1402dddfa461SMichael Halcrow rc = -EIO; 1403dddfa461SMichael Halcrow goto out; 1404dddfa461SMichael Halcrow } 1405dddfa461SMichael Halcrow rc = parse_tag_67_packet(key_rec, msg); 1406dddfa461SMichael Halcrow if (rc) 1407dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 1408dddfa461SMichael Halcrow kfree(msg); 1409dddfa461SMichael Halcrow out: 1410dddfa461SMichael Halcrow if (netlink_payload) 1411dddfa461SMichael Halcrow kfree(netlink_payload); 1412dddfa461SMichael Halcrow return rc; 1413dddfa461SMichael Halcrow } 1414dddfa461SMichael Halcrow /** 1415dddfa461SMichael Halcrow * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 1416dddfa461SMichael Halcrow * @dest: Buffer into which to write the packet 1417dddfa461SMichael Halcrow * @max: Maximum number of bytes that can be writtn 1418dddfa461SMichael Halcrow * @packet_size: This function will write the number of bytes that end 1419dddfa461SMichael Halcrow * up constituting the packet; set to zero on error 1420dddfa461SMichael Halcrow * 1421dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 1422dddfa461SMichael Halcrow */ 1423dddfa461SMichael Halcrow static int 1424*f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes, 1425*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 1426dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1427dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 1428dddfa461SMichael Halcrow { 1429dddfa461SMichael Halcrow size_t i; 1430dddfa461SMichael Halcrow size_t encrypted_session_key_valid = 0; 1431dddfa461SMichael Halcrow size_t packet_size_length; 1432*f4aad16aSMichael Halcrow size_t max_packet_size; 1433dddfa461SMichael Halcrow int rc = 0; 1434dddfa461SMichael Halcrow 1435dddfa461SMichael Halcrow (*packet_size) = 0; 1436dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 1437dddfa461SMichael Halcrow ECRYPTFS_SIG_SIZE); 1438dddfa461SMichael Halcrow encrypted_session_key_valid = 0; 1439dddfa461SMichael Halcrow for (i = 0; i < crypt_stat->key_size; i++) 1440dddfa461SMichael Halcrow encrypted_session_key_valid |= 1441dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key[i]; 1442dddfa461SMichael Halcrow if (encrypted_session_key_valid) { 1443dddfa461SMichael Halcrow memcpy(key_rec->enc_key, 1444dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key, 1445dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size); 1446dddfa461SMichael Halcrow goto encrypted_session_key_set; 1447dddfa461SMichael Halcrow } 1448dddfa461SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 1449dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size = 1450dddfa461SMichael Halcrow auth_tok->token.private_key.key_size; 1451dddfa461SMichael Halcrow rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); 1452dddfa461SMichael Halcrow if (rc) { 1453dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to encrypt session key " 1454dddfa461SMichael Halcrow "via a pki"); 1455dddfa461SMichael Halcrow goto out; 1456dddfa461SMichael Halcrow } 1457dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 1458dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 1459dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 1460dddfa461SMichael Halcrow } 1461dddfa461SMichael Halcrow encrypted_session_key_set: 1462*f4aad16aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1463*f4aad16aSMichael Halcrow * packet tag 1 */ 1464*f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 1 identifier */ 1465*f4aad16aSMichael Halcrow + 3 /* Max Tag 1 packet size */ 1466*f4aad16aSMichael Halcrow + 1 /* Version */ 1467*f4aad16aSMichael Halcrow + ECRYPTFS_SIG_SIZE /* Key identifier */ 1468*f4aad16aSMichael Halcrow + 1 /* Cipher identifier */ 1469*f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 1470*f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 1471*f4aad16aSMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 1472*f4aad16aSMichael Halcrow "need up to [%d] bytes, but there are only [%d] " 1473*f4aad16aSMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 1474dddfa461SMichael Halcrow rc = -EINVAL; 1475dddfa461SMichael Halcrow goto out; 1476dddfa461SMichael Halcrow } 1477dddfa461SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 1478*f4aad16aSMichael Halcrow rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), 1479dddfa461SMichael Halcrow &packet_size_length); 1480dddfa461SMichael Halcrow if (rc) { 1481dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 1482dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 1483dddfa461SMichael Halcrow goto out; 1484dddfa461SMichael Halcrow } 1485dddfa461SMichael Halcrow (*packet_size) += packet_size_length; 1486dddfa461SMichael Halcrow dest[(*packet_size)++] = 0x03; /* version 3 */ 1487dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 1488dddfa461SMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 1489dddfa461SMichael Halcrow dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 1490dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 1491dddfa461SMichael Halcrow key_rec->enc_key_size); 1492dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 1493dddfa461SMichael Halcrow out: 1494dddfa461SMichael Halcrow if (rc) 1495dddfa461SMichael Halcrow (*packet_size) = 0; 1496*f4aad16aSMichael Halcrow else 1497*f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 1498dddfa461SMichael Halcrow return rc; 1499dddfa461SMichael Halcrow } 1500237fead6SMichael Halcrow 1501237fead6SMichael Halcrow /** 1502237fead6SMichael Halcrow * write_tag_11_packet 1503237fead6SMichael Halcrow * @dest: Target into which Tag 11 packet is to be written 1504237fead6SMichael Halcrow * @max: Maximum packet length 1505237fead6SMichael Halcrow * @contents: Byte array of contents to copy in 1506237fead6SMichael Halcrow * @contents_length: Number of bytes in contents 1507237fead6SMichael Halcrow * @packet_length: Length of the Tag 11 packet written; zero on error 1508237fead6SMichael Halcrow * 1509237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1510237fead6SMichael Halcrow */ 1511237fead6SMichael Halcrow static int 1512237fead6SMichael Halcrow write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length, 1513237fead6SMichael Halcrow size_t *packet_length) 1514237fead6SMichael Halcrow { 1515237fead6SMichael Halcrow size_t packet_size_length; 1516dddfa461SMichael Halcrow int rc = 0; 1517237fead6SMichael Halcrow 1518237fead6SMichael Halcrow (*packet_length) = 0; 1519237fead6SMichael Halcrow if ((13 + contents_length) > max) { 1520237fead6SMichael Halcrow rc = -EINVAL; 1521237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Packet length larger than " 1522237fead6SMichael Halcrow "maximum allowable\n"); 1523237fead6SMichael Halcrow goto out; 1524237fead6SMichael Halcrow } 1525237fead6SMichael Halcrow /* General packet header */ 1526237fead6SMichael Halcrow /* Packet tag */ 1527237fead6SMichael Halcrow dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 1528237fead6SMichael Halcrow /* Packet length */ 1529237fead6SMichael Halcrow rc = write_packet_length(&dest[(*packet_length)], 1530237fead6SMichael Halcrow (13 + contents_length), &packet_size_length); 1531237fead6SMichael Halcrow if (rc) { 1532237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet " 1533237fead6SMichael Halcrow "header; cannot generate packet length\n"); 1534237fead6SMichael Halcrow goto out; 1535237fead6SMichael Halcrow } 1536237fead6SMichael Halcrow (*packet_length) += packet_size_length; 1537237fead6SMichael Halcrow /* Tag 11 specific */ 1538237fead6SMichael Halcrow /* One-octet field that describes how the data is formatted */ 1539237fead6SMichael Halcrow dest[(*packet_length)++] = 0x62; /* binary data */ 1540237fead6SMichael Halcrow /* One-octet filename length followed by filename */ 1541237fead6SMichael Halcrow dest[(*packet_length)++] = 8; 1542237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 1543237fead6SMichael Halcrow (*packet_length) += 8; 1544237fead6SMichael Halcrow /* Four-octet number indicating modification date */ 1545237fead6SMichael Halcrow memset(&dest[(*packet_length)], 0x00, 4); 1546237fead6SMichael Halcrow (*packet_length) += 4; 1547237fead6SMichael Halcrow /* Remainder is literal data */ 1548237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], contents, contents_length); 1549237fead6SMichael Halcrow (*packet_length) += contents_length; 1550237fead6SMichael Halcrow out: 1551237fead6SMichael Halcrow if (rc) 1552237fead6SMichael Halcrow (*packet_length) = 0; 1553237fead6SMichael Halcrow return rc; 1554237fead6SMichael Halcrow } 1555237fead6SMichael Halcrow 1556237fead6SMichael Halcrow /** 1557237fead6SMichael Halcrow * write_tag_3_packet 1558237fead6SMichael Halcrow * @dest: Buffer into which to write the packet 1559237fead6SMichael Halcrow * @max: Maximum number of bytes that can be written 1560237fead6SMichael Halcrow * @auth_tok: Authentication token 1561237fead6SMichael Halcrow * @crypt_stat: The cryptographic context 1562237fead6SMichael Halcrow * @key_rec: encrypted key 1563237fead6SMichael Halcrow * @packet_size: This function will write the number of bytes that end 1564237fead6SMichael Halcrow * up constituting the packet; set to zero on error 1565237fead6SMichael Halcrow * 1566237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1567237fead6SMichael Halcrow */ 1568237fead6SMichael Halcrow static int 1569*f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes, 1570*f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 1571237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1572237fead6SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 1573237fead6SMichael Halcrow { 1574237fead6SMichael Halcrow size_t i; 1575237fead6SMichael Halcrow size_t encrypted_session_key_valid = 0; 1576237fead6SMichael Halcrow char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 1577*f4aad16aSMichael Halcrow struct scatterlist dst_sg; 1578*f4aad16aSMichael Halcrow struct scatterlist src_sg; 1579237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 1580237fead6SMichael Halcrow size_t cipher_code; 1581*f4aad16aSMichael Halcrow size_t packet_size_length; 1582*f4aad16aSMichael Halcrow size_t max_packet_size; 1583*f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1584*f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 15858bba066fSMichael Halcrow struct blkcipher_desc desc = { 15868bba066fSMichael Halcrow .tfm = NULL, 15878bba066fSMichael Halcrow .flags = CRYPTO_TFM_REQ_MAY_SLEEP 15888bba066fSMichael Halcrow }; 15898bba066fSMichael Halcrow int rc = 0; 1590237fead6SMichael Halcrow 1591237fead6SMichael Halcrow (*packet_size) = 0; 1592dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 1593237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE); 1594*f4aad16aSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 1595*f4aad16aSMichael Halcrow crypt_stat->cipher); 1596*f4aad16aSMichael Halcrow if (unlikely(rc)) { 1597*f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1598*f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1599*f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1600*f4aad16aSMichael Halcrow goto out; 1601237fead6SMichael Halcrow } 1602*f4aad16aSMichael Halcrow if (mount_crypt_stat->global_default_cipher_key_size == 0) { 1603*f4aad16aSMichael Halcrow struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); 1604*f4aad16aSMichael Halcrow 1605*f4aad16aSMichael Halcrow printk(KERN_WARNING "No key size specified at mount; " 1606*f4aad16aSMichael Halcrow "defaulting to [%d]\n", alg->max_keysize); 1607*f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size = 1608*f4aad16aSMichael Halcrow alg->max_keysize; 1609*f4aad16aSMichael Halcrow } 1610*f4aad16aSMichael Halcrow if (crypt_stat->key_size == 0) 1611*f4aad16aSMichael Halcrow crypt_stat->key_size = 1612*f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 1613237fead6SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 1614237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 1615237fead6SMichael Halcrow crypt_stat->key_size; 1616237fead6SMichael Halcrow if (crypt_stat->key_size == 24 1617237fead6SMichael Halcrow && strcmp("aes", crypt_stat->cipher) == 0) { 1618237fead6SMichael Halcrow memset((crypt_stat->key + 24), 0, 8); 1619237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 32; 1620*f4aad16aSMichael Halcrow } else 1621*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 1622dddfa461SMichael Halcrow key_rec->enc_key_size = 1623237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size; 1624*f4aad16aSMichael Halcrow encrypted_session_key_valid = 0; 1625*f4aad16aSMichael Halcrow for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 1626*f4aad16aSMichael Halcrow encrypted_session_key_valid |= 1627*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key[i]; 1628*f4aad16aSMichael Halcrow if (encrypted_session_key_valid) { 1629*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 1630*f4aad16aSMichael Halcrow "using auth_tok->session_key.encrypted_key, " 1631*f4aad16aSMichael Halcrow "where key_rec->enc_key_size = [%d]\n", 1632*f4aad16aSMichael Halcrow key_rec->enc_key_size); 1633*f4aad16aSMichael Halcrow memcpy(key_rec->enc_key, 1634*f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key, 1635*f4aad16aSMichael Halcrow key_rec->enc_key_size); 1636*f4aad16aSMichael Halcrow goto encrypted_session_key_set; 1637*f4aad16aSMichael Halcrow } 1638dddfa461SMichael Halcrow if (auth_tok->token.password.flags & 1639dddfa461SMichael Halcrow ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 1640237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Using previously generated " 1641237fead6SMichael Halcrow "session key encryption key of size [%d]\n", 1642237fead6SMichael Halcrow auth_tok->token.password. 1643237fead6SMichael Halcrow session_key_encryption_key_bytes); 1644237fead6SMichael Halcrow memcpy(session_key_encryption_key, 1645237fead6SMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1646237fead6SMichael Halcrow crypt_stat->key_size); 1647237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1648237fead6SMichael Halcrow "Cached session key " "encryption key: \n"); 1649237fead6SMichael Halcrow if (ecryptfs_verbosity > 0) 1650237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 1651237fead6SMichael Halcrow } 1652237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1653237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 1654237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 1655237fead6SMichael Halcrow } 1656*f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(crypt_stat->key, 1657*f4aad16aSMichael Halcrow key_rec->enc_key_size, &src_sg, 1)) 1658*f4aad16aSMichael Halcrow != 1) { 1659237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 1660*f4aad16aSMichael Halcrow "for crypt_stat session key; expected rc = 1; " 1661*f4aad16aSMichael Halcrow "got rc = [%d]. key_rec->enc_key_size = [%d]\n", 1662*f4aad16aSMichael Halcrow rc, key_rec->enc_key_size); 1663237fead6SMichael Halcrow rc = -ENOMEM; 1664237fead6SMichael Halcrow goto out; 1665237fead6SMichael Halcrow } 1666*f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(key_rec->enc_key, 1667*f4aad16aSMichael Halcrow key_rec->enc_key_size, &dst_sg, 1)) 1668*f4aad16aSMichael Halcrow != 1) { 1669237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 1670*f4aad16aSMichael Halcrow "for crypt_stat encrypted session key; " 1671*f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1672*f4aad16aSMichael Halcrow "key_rec->enc_key_size = [%d]\n", rc, 1673*f4aad16aSMichael Halcrow key_rec->enc_key_size); 1674237fead6SMichael Halcrow rc = -ENOMEM; 1675237fead6SMichael Halcrow goto out; 1676237fead6SMichael Halcrow } 1677237fead6SMichael Halcrow mutex_lock(tfm_mutex); 16788bba066fSMichael Halcrow rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, 1679237fead6SMichael Halcrow crypt_stat->key_size); 1680237fead6SMichael Halcrow if (rc < 0) { 1681237fead6SMichael Halcrow mutex_unlock(tfm_mutex); 1682237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 16838bba066fSMichael Halcrow "context; rc = [%d]\n", rc); 1684237fead6SMichael Halcrow goto out; 1685237fead6SMichael Halcrow } 1686237fead6SMichael Halcrow rc = 0; 1687237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", 1688237fead6SMichael Halcrow crypt_stat->key_size); 1689*f4aad16aSMichael Halcrow rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, 1690237fead6SMichael Halcrow (*key_rec).enc_key_size); 1691*f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 16928bba066fSMichael Halcrow if (rc) { 16938bba066fSMichael Halcrow printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 16948bba066fSMichael Halcrow goto out; 16958bba066fSMichael Halcrow } 1696237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 1697*f4aad16aSMichael Halcrow if (ecryptfs_verbosity > 0) { 1698*f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", 1699*f4aad16aSMichael Halcrow key_rec->enc_key_size); 1700dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, 1701dddfa461SMichael Halcrow key_rec->enc_key_size); 1702*f4aad16aSMichael Halcrow } 1703237fead6SMichael Halcrow encrypted_session_key_set: 1704237fead6SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1705237fead6SMichael Halcrow * packet tag 3 */ 1706*f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 3 identifier */ 1707*f4aad16aSMichael Halcrow + 3 /* Max Tag 3 packet size */ 1708*f4aad16aSMichael Halcrow + 1 /* Version */ 1709*f4aad16aSMichael Halcrow + 1 /* Cipher code */ 1710*f4aad16aSMichael Halcrow + 1 /* S2K specifier */ 1711*f4aad16aSMichael Halcrow + 1 /* Hash identifier */ 1712*f4aad16aSMichael Halcrow + ECRYPTFS_SALT_SIZE /* Salt */ 1713*f4aad16aSMichael Halcrow + 1 /* Hash iterations */ 1714*f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 1715*f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 1716*f4aad16aSMichael Halcrow printk(KERN_ERR "Packet too large; need up to [%d] bytes, but " 1717*f4aad16aSMichael Halcrow "there are only [%d] available\n", max_packet_size, 1718*f4aad16aSMichael Halcrow (*remaining_bytes)); 1719*f4aad16aSMichael Halcrow rc = -EINVAL; 1720*f4aad16aSMichael Halcrow goto out; 1721*f4aad16aSMichael Halcrow } 1722237fead6SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 1723*f4aad16aSMichael Halcrow /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 1724*f4aad16aSMichael Halcrow * to get the number of octets in the actual Tag 3 packet */ 1725*f4aad16aSMichael Halcrow rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), 1726237fead6SMichael Halcrow &packet_size_length); 1727237fead6SMichael Halcrow if (rc) { 1728*f4aad16aSMichael Halcrow printk(KERN_ERR "Error generating tag 3 packet header; cannot " 1729*f4aad16aSMichael Halcrow "generate packet length. rc = [%d]\n", rc); 1730237fead6SMichael Halcrow goto out; 1731237fead6SMichael Halcrow } 1732237fead6SMichael Halcrow (*packet_size) += packet_size_length; 1733237fead6SMichael Halcrow dest[(*packet_size)++] = 0x04; /* version 4 */ 1734*f4aad16aSMichael Halcrow /* TODO: Break from RFC2440 so that arbitrary ciphers can be 1735*f4aad16aSMichael Halcrow * specified with strings */ 1736237fead6SMichael Halcrow cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); 1737237fead6SMichael Halcrow if (cipher_code == 0) { 1738237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 1739237fead6SMichael Halcrow "cipher [%s]\n", crypt_stat->cipher); 1740237fead6SMichael Halcrow rc = -EINVAL; 1741237fead6SMichael Halcrow goto out; 1742237fead6SMichael Halcrow } 1743237fead6SMichael Halcrow dest[(*packet_size)++] = cipher_code; 1744237fead6SMichael Halcrow dest[(*packet_size)++] = 0x03; /* S2K */ 1745237fead6SMichael Halcrow dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 1746237fead6SMichael Halcrow memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 1747237fead6SMichael Halcrow ECRYPTFS_SALT_SIZE); 1748237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 1749237fead6SMichael Halcrow dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 1750dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 1751dddfa461SMichael Halcrow key_rec->enc_key_size); 1752dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 1753237fead6SMichael Halcrow out: 1754237fead6SMichael Halcrow if (rc) 1755237fead6SMichael Halcrow (*packet_size) = 0; 1756*f4aad16aSMichael Halcrow else 1757*f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 1758237fead6SMichael Halcrow return rc; 1759237fead6SMichael Halcrow } 1760237fead6SMichael Halcrow 1761eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache; 1762eb95e7ffSMichael Halcrow 1763237fead6SMichael Halcrow /** 1764237fead6SMichael Halcrow * ecryptfs_generate_key_packet_set 1765237fead6SMichael Halcrow * @dest: Virtual address from which to write the key record set 1766237fead6SMichael Halcrow * @crypt_stat: The cryptographic context from which the 1767237fead6SMichael Halcrow * authentication tokens will be retrieved 1768237fead6SMichael Halcrow * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 1769237fead6SMichael Halcrow * for the global parameters 1770237fead6SMichael Halcrow * @len: The amount written 1771237fead6SMichael Halcrow * @max: The maximum amount of data allowed to be written 1772237fead6SMichael Halcrow * 1773237fead6SMichael Halcrow * Generates a key packet set and writes it to the virtual address 1774237fead6SMichael Halcrow * passed in. 1775237fead6SMichael Halcrow * 1776237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1777237fead6SMichael Halcrow */ 1778237fead6SMichael Halcrow int 1779237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base, 1780237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1781237fead6SMichael Halcrow struct dentry *ecryptfs_dentry, size_t *len, 1782237fead6SMichael Halcrow size_t max) 1783237fead6SMichael Halcrow { 1784237fead6SMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 1785*f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *global_auth_tok; 1786237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1787237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 1788237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 1789237fead6SMichael Halcrow size_t written; 1790eb95e7ffSMichael Halcrow struct ecryptfs_key_record *key_rec; 1791*f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig; 1792dddfa461SMichael Halcrow int rc = 0; 1793237fead6SMichael Halcrow 1794237fead6SMichael Halcrow (*len) = 0; 1795*f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 1796eb95e7ffSMichael Halcrow key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 1797eb95e7ffSMichael Halcrow if (!key_rec) { 1798eb95e7ffSMichael Halcrow rc = -ENOMEM; 1799eb95e7ffSMichael Halcrow goto out; 1800eb95e7ffSMichael Halcrow } 1801*f4aad16aSMichael Halcrow list_for_each_entry(key_sig, &crypt_stat->keysig_list, 1802*f4aad16aSMichael Halcrow crypt_stat_list) { 1803*f4aad16aSMichael Halcrow memset(key_rec, 0, sizeof(*key_rec)); 1804*f4aad16aSMichael Halcrow rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 1805*f4aad16aSMichael Halcrow mount_crypt_stat, 1806*f4aad16aSMichael Halcrow key_sig->keysig); 1807*f4aad16aSMichael Halcrow if (rc) { 1808*f4aad16aSMichael Halcrow printk(KERN_ERR "Error attempting to get the global " 1809*f4aad16aSMichael Halcrow "auth_tok; rc = [%d]\n", rc); 1810*f4aad16aSMichael Halcrow goto out_free; 1811*f4aad16aSMichael Halcrow } 1812*f4aad16aSMichael Halcrow if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { 1813*f4aad16aSMichael Halcrow printk(KERN_WARNING 1814*f4aad16aSMichael Halcrow "Skipping invalid auth tok with sig = [%s]\n", 1815*f4aad16aSMichael Halcrow global_auth_tok->sig); 1816*f4aad16aSMichael Halcrow continue; 1817*f4aad16aSMichael Halcrow } 1818*f4aad16aSMichael Halcrow auth_tok = global_auth_tok->global_auth_tok; 1819237fead6SMichael Halcrow if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 1820237fead6SMichael Halcrow rc = write_tag_3_packet((dest_base + (*len)), 1821*f4aad16aSMichael Halcrow &max, auth_tok, 1822eb95e7ffSMichael Halcrow crypt_stat, key_rec, 1823237fead6SMichael Halcrow &written); 1824237fead6SMichael Halcrow if (rc) { 1825237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 1826237fead6SMichael Halcrow "writing tag 3 packet\n"); 1827eb95e7ffSMichael Halcrow goto out_free; 1828237fead6SMichael Halcrow } 1829237fead6SMichael Halcrow (*len) += written; 1830237fead6SMichael Halcrow /* Write auth tok signature packet */ 1831*f4aad16aSMichael Halcrow rc = write_tag_11_packet((dest_base + (*len)), &max, 1832*f4aad16aSMichael Halcrow key_rec->sig, 1833*f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE, &written); 1834237fead6SMichael Halcrow if (rc) { 1835237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing " 1836237fead6SMichael Halcrow "auth tok signature packet\n"); 1837eb95e7ffSMichael Halcrow goto out_free; 1838237fead6SMichael Halcrow } 1839237fead6SMichael Halcrow (*len) += written; 1840dddfa461SMichael Halcrow } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1841dddfa461SMichael Halcrow rc = write_tag_1_packet(dest_base + (*len), 1842*f4aad16aSMichael Halcrow &max, auth_tok, 1843*f4aad16aSMichael Halcrow crypt_stat, key_rec, &written); 1844dddfa461SMichael Halcrow if (rc) { 1845dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 1846dddfa461SMichael Halcrow "writing tag 1 packet\n"); 1847eb95e7ffSMichael Halcrow goto out_free; 1848dddfa461SMichael Halcrow } 1849dddfa461SMichael Halcrow (*len) += written; 1850237fead6SMichael Halcrow } else { 1851237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unsupported " 1852237fead6SMichael Halcrow "authentication token type\n"); 1853237fead6SMichael Halcrow rc = -EINVAL; 1854eb95e7ffSMichael Halcrow goto out_free; 1855237fead6SMichael Halcrow } 1856*f4aad16aSMichael Halcrow } 1857*f4aad16aSMichael Halcrow if (likely(max > 0)) { 1858237fead6SMichael Halcrow dest_base[(*len)] = 0x00; 1859237fead6SMichael Halcrow } else { 1860237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 1861237fead6SMichael Halcrow rc = -EIO; 1862237fead6SMichael Halcrow } 1863eb95e7ffSMichael Halcrow out_free: 1864eb95e7ffSMichael Halcrow kmem_cache_free(ecryptfs_key_record_cache, key_rec); 1865237fead6SMichael Halcrow out: 1866237fead6SMichael Halcrow if (rc) 1867237fead6SMichael Halcrow (*len) = 0; 1868*f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 1869237fead6SMichael Halcrow return rc; 1870237fead6SMichael Halcrow } 1871*f4aad16aSMichael Halcrow 1872*f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache; 1873*f4aad16aSMichael Halcrow 1874*f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 1875*f4aad16aSMichael Halcrow { 1876*f4aad16aSMichael Halcrow struct ecryptfs_key_sig *new_key_sig; 1877*f4aad16aSMichael Halcrow int rc = 0; 1878*f4aad16aSMichael Halcrow 1879*f4aad16aSMichael Halcrow new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 1880*f4aad16aSMichael Halcrow if (!new_key_sig) { 1881*f4aad16aSMichael Halcrow rc = -ENOMEM; 1882*f4aad16aSMichael Halcrow printk(KERN_ERR 1883*f4aad16aSMichael Halcrow "Error allocating from ecryptfs_key_sig_cache\n"); 1884*f4aad16aSMichael Halcrow goto out; 1885*f4aad16aSMichael Halcrow } 1886*f4aad16aSMichael Halcrow memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 1887*f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 1888*f4aad16aSMichael Halcrow list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 1889*f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 1890*f4aad16aSMichael Halcrow out: 1891*f4aad16aSMichael Halcrow return rc; 1892*f4aad16aSMichael Halcrow } 1893*f4aad16aSMichael Halcrow 1894*f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache; 1895*f4aad16aSMichael Halcrow 1896*f4aad16aSMichael Halcrow int 1897*f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 1898*f4aad16aSMichael Halcrow char *sig) 1899*f4aad16aSMichael Halcrow { 1900*f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *new_auth_tok; 1901*f4aad16aSMichael Halcrow int rc = 0; 1902*f4aad16aSMichael Halcrow 1903*f4aad16aSMichael Halcrow new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache, 1904*f4aad16aSMichael Halcrow GFP_KERNEL); 1905*f4aad16aSMichael Halcrow if (!new_auth_tok) { 1906*f4aad16aSMichael Halcrow rc = -ENOMEM; 1907*f4aad16aSMichael Halcrow printk(KERN_ERR "Error allocating from " 1908*f4aad16aSMichael Halcrow "ecryptfs_global_auth_tok_cache\n"); 1909*f4aad16aSMichael Halcrow goto out; 1910*f4aad16aSMichael Halcrow } 1911*f4aad16aSMichael Halcrow memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 1912*f4aad16aSMichael Halcrow new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 1913*f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 1914*f4aad16aSMichael Halcrow list_add(&new_auth_tok->mount_crypt_stat_list, 1915*f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list); 1916*f4aad16aSMichael Halcrow mount_crypt_stat->num_global_auth_toks++; 1917*f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 1918*f4aad16aSMichael Halcrow out: 1919*f4aad16aSMichael Halcrow return rc; 1920*f4aad16aSMichael Halcrow } 1921*f4aad16aSMichael Halcrow 1922