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 */ 405f4aad16aSMichael Halcrow static int 406f4aad16aSMichael 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; 412f4aad16aSMichael Halcrow char *auth_tok_sig; 413dddfa461SMichael Halcrow char *netlink_message; 414dddfa461SMichael Halcrow size_t netlink_message_length; 415dddfa461SMichael Halcrow int rc; 416dddfa461SMichael Halcrow 417f4aad16aSMichael Halcrow if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) { 418f4aad16aSMichael Halcrow printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 419f4aad16aSMichael Halcrow auth_tok->token_type); 420f4aad16aSMichael Halcrow goto out; 421f4aad16aSMichael Halcrow } 422f4aad16aSMichael 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 ecryptfs_auth_tok_list_item *auth_tok_list_item; 473e0869cc1SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 474dddfa461SMichael Halcrow 475e0869cc1SMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 476e0869cc1SMichael Halcrow auth_tok_list_head, list) { 477e0869cc1SMichael Halcrow list_del(&auth_tok_list_item->list); 478dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 479dddfa461SMichael Halcrow auth_tok_list_item); 480dddfa461SMichael Halcrow } 481dddfa461SMichael Halcrow } 482dddfa461SMichael Halcrow 483dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 484dddfa461SMichael Halcrow 485dddfa461SMichael Halcrow /** 486dddfa461SMichael Halcrow * parse_tag_1_packet 487dddfa461SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 488dddfa461SMichael Halcrow * contents. 489dddfa461SMichael Halcrow * @data: The raw bytes of the packet. 490dddfa461SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 491dddfa461SMichael Halcrow * a new authentication token will be placed at the end 492dddfa461SMichael Halcrow * of this list for this packet. 493dddfa461SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 494dddfa461SMichael Halcrow * allocates; sets the memory address of the pointer to 495dddfa461SMichael Halcrow * NULL on error. This object is added to the 496dddfa461SMichael Halcrow * auth_tok_list. 497dddfa461SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 498dddfa461SMichael Halcrow * into this memory location; zero on error. 499dddfa461SMichael Halcrow * 500dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 501dddfa461SMichael Halcrow */ 502dddfa461SMichael Halcrow static int 503dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 504dddfa461SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 505dddfa461SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 506dddfa461SMichael Halcrow size_t *packet_size, size_t max_packet_size) 507dddfa461SMichael Halcrow { 508dddfa461SMichael Halcrow size_t body_size; 509dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 510dddfa461SMichael Halcrow size_t length_size; 511dddfa461SMichael Halcrow int rc = 0; 512dddfa461SMichael Halcrow 513dddfa461SMichael Halcrow (*packet_size) = 0; 514dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 51513218179SMichael Halcrow /** 51613218179SMichael Halcrow * This format is inspired by OpenPGP; see RFC 2440 51713218179SMichael Halcrow * packet tag 1 51813218179SMichael Halcrow * 51913218179SMichael Halcrow * Tag 1 identifier (1 byte) 52013218179SMichael Halcrow * Max Tag 1 packet size (max 3 bytes) 52113218179SMichael Halcrow * Version (1 byte) 52213218179SMichael Halcrow * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 52313218179SMichael Halcrow * Cipher identifier (1 byte) 52413218179SMichael Halcrow * Encrypted key size (arbitrary) 52513218179SMichael Halcrow * 52613218179SMichael Halcrow * 12 bytes minimum packet size 527dddfa461SMichael Halcrow */ 52813218179SMichael Halcrow if (unlikely(max_packet_size < 12)) { 52913218179SMichael Halcrow printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 530dddfa461SMichael Halcrow rc = -EINVAL; 531dddfa461SMichael Halcrow goto out; 532dddfa461SMichael Halcrow } 533dddfa461SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 53413218179SMichael Halcrow printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 535dddfa461SMichael Halcrow ECRYPTFS_TAG_1_PACKET_TYPE); 536dddfa461SMichael Halcrow rc = -EINVAL; 537dddfa461SMichael Halcrow goto out; 538dddfa461SMichael Halcrow } 539dddfa461SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 540dddfa461SMichael Halcrow * at end of function upon failure */ 541dddfa461SMichael Halcrow auth_tok_list_item = 54213218179SMichael Halcrow kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 543dddfa461SMichael Halcrow GFP_KERNEL); 544dddfa461SMichael Halcrow if (!auth_tok_list_item) { 54513218179SMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 546dddfa461SMichael Halcrow rc = -ENOMEM; 547dddfa461SMichael Halcrow goto out; 548dddfa461SMichael Halcrow } 549dddfa461SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 55013218179SMichael Halcrow if ((rc = parse_packet_length(&data[(*packet_size)], &body_size, 55113218179SMichael Halcrow &length_size))) { 55213218179SMichael Halcrow printk(KERN_WARNING "Error parsing packet length; " 553dddfa461SMichael Halcrow "rc = [%d]\n", rc); 554dddfa461SMichael Halcrow goto out_free; 555dddfa461SMichael Halcrow } 55613218179SMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 55713218179SMichael Halcrow printk(KERN_WARNING "Invalid body size ([%d])\n", body_size); 558dddfa461SMichael Halcrow rc = -EINVAL; 559dddfa461SMichael Halcrow goto out_free; 560dddfa461SMichael Halcrow } 561dddfa461SMichael Halcrow (*packet_size) += length_size; 562dddfa461SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 56313218179SMichael Halcrow printk(KERN_WARNING "Packet size exceeds max\n"); 564dddfa461SMichael Halcrow rc = -EINVAL; 565dddfa461SMichael Halcrow goto out_free; 566dddfa461SMichael Halcrow } 567dddfa461SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 56813218179SMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 56913218179SMichael Halcrow data[(*packet_size) - 1]); 570dddfa461SMichael Halcrow rc = -EINVAL; 571dddfa461SMichael Halcrow goto out_free; 572dddfa461SMichael Halcrow } 573dddfa461SMichael Halcrow ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 574dddfa461SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 575dddfa461SMichael Halcrow *packet_size += ECRYPTFS_SIG_SIZE; 576dddfa461SMichael Halcrow /* This byte is skipped because the kernel does not need to 577dddfa461SMichael Halcrow * know which public key encryption algorithm was used */ 578dddfa461SMichael Halcrow (*packet_size)++; 579dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 58013218179SMichael Halcrow body_size - (ECRYPTFS_SIG_SIZE + 2); 581dddfa461SMichael Halcrow if ((*new_auth_tok)->session_key.encrypted_key_size 582dddfa461SMichael Halcrow > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 58313218179SMichael Halcrow printk(KERN_WARNING "Tag 1 packet contains key larger " 584dddfa461SMichael Halcrow "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); 585dddfa461SMichael Halcrow rc = -EINVAL; 586dddfa461SMichael Halcrow goto out; 587dddfa461SMichael Halcrow } 588dddfa461SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 58913218179SMichael Halcrow &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 590dddfa461SMichael Halcrow (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 591dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags &= 592dddfa461SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 593dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags |= 594dddfa461SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 595dddfa461SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 59613218179SMichael Halcrow (*new_auth_tok)->flags = 0; 597e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 598e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 599e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 600e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 601dddfa461SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 602dddfa461SMichael Halcrow goto out; 603dddfa461SMichael Halcrow out_free: 604dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 605dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 606dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 607dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 608dddfa461SMichael Halcrow auth_tok_list_item); 609dddfa461SMichael Halcrow out: 610dddfa461SMichael Halcrow if (rc) 611dddfa461SMichael Halcrow (*packet_size) = 0; 612dddfa461SMichael Halcrow return rc; 613dddfa461SMichael Halcrow } 614dddfa461SMichael Halcrow 615237fead6SMichael Halcrow /** 616237fead6SMichael Halcrow * parse_tag_3_packet 617237fead6SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 618237fead6SMichael Halcrow * contents. 619237fead6SMichael Halcrow * @data: The raw bytes of the packet. 620237fead6SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 621237fead6SMichael Halcrow * a new authentication token will be placed at the end 622237fead6SMichael Halcrow * of this list for this packet. 623237fead6SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 624237fead6SMichael Halcrow * allocates; sets the memory address of the pointer to 625237fead6SMichael Halcrow * NULL on error. This object is added to the 626237fead6SMichael Halcrow * auth_tok_list. 627237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 628237fead6SMichael Halcrow * into this memory location; zero on error. 629237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 630237fead6SMichael Halcrow * 631237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 632237fead6SMichael Halcrow */ 633237fead6SMichael Halcrow static int 634237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 635237fead6SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 636237fead6SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 637237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 638237fead6SMichael Halcrow { 639237fead6SMichael Halcrow size_t body_size; 640237fead6SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 641237fead6SMichael Halcrow size_t length_size; 642dddfa461SMichael Halcrow int rc = 0; 643237fead6SMichael Halcrow 644237fead6SMichael Halcrow (*packet_size) = 0; 645237fead6SMichael Halcrow (*new_auth_tok) = NULL; 646c59becfcSMichael Halcrow /** 647c59becfcSMichael Halcrow *This format is inspired by OpenPGP; see RFC 2440 648c59becfcSMichael Halcrow * packet tag 3 649c59becfcSMichael Halcrow * 650c59becfcSMichael Halcrow * Tag 3 identifier (1 byte) 651c59becfcSMichael Halcrow * Max Tag 3 packet size (max 3 bytes) 652c59becfcSMichael Halcrow * Version (1 byte) 653c59becfcSMichael Halcrow * Cipher code (1 byte) 654c59becfcSMichael Halcrow * S2K specifier (1 byte) 655c59becfcSMichael Halcrow * Hash identifier (1 byte) 656c59becfcSMichael Halcrow * Salt (ECRYPTFS_SALT_SIZE) 657c59becfcSMichael Halcrow * Hash iterations (1 byte) 658c59becfcSMichael Halcrow * Encrypted key (arbitrary) 659c59becfcSMichael Halcrow * 660c59becfcSMichael Halcrow * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 661237fead6SMichael Halcrow */ 662c59becfcSMichael Halcrow if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 663c59becfcSMichael Halcrow printk(KERN_ERR "Max packet size too large\n"); 664237fead6SMichael Halcrow rc = -EINVAL; 665237fead6SMichael Halcrow goto out; 666237fead6SMichael Halcrow } 667237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 668c59becfcSMichael Halcrow printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 669237fead6SMichael Halcrow ECRYPTFS_TAG_3_PACKET_TYPE); 670237fead6SMichael Halcrow rc = -EINVAL; 671237fead6SMichael Halcrow goto out; 672237fead6SMichael Halcrow } 673237fead6SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 674237fead6SMichael Halcrow * at end of function upon failure */ 675237fead6SMichael Halcrow auth_tok_list_item = 676c3762229SRobert P. J. Day kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 677237fead6SMichael Halcrow if (!auth_tok_list_item) { 678c59becfcSMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 679237fead6SMichael Halcrow rc = -ENOMEM; 680237fead6SMichael Halcrow goto out; 681237fead6SMichael Halcrow } 682237fead6SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 683c59becfcSMichael Halcrow if ((rc = parse_packet_length(&data[(*packet_size)], &body_size, 684c59becfcSMichael Halcrow &length_size))) { 685c59becfcSMichael Halcrow printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 686c59becfcSMichael Halcrow rc); 687237fead6SMichael Halcrow goto out_free; 688237fead6SMichael Halcrow } 689c59becfcSMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 690c59becfcSMichael Halcrow printk(KERN_WARNING "Invalid body size ([%d])\n", body_size); 691237fead6SMichael Halcrow rc = -EINVAL; 692237fead6SMichael Halcrow goto out_free; 693237fead6SMichael Halcrow } 694237fead6SMichael Halcrow (*packet_size) += length_size; 695237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 696c59becfcSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 697237fead6SMichael Halcrow rc = -EINVAL; 698237fead6SMichael Halcrow goto out_free; 699237fead6SMichael Halcrow } 700237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 701c59becfcSMichael Halcrow (body_size - (ECRYPTFS_SALT_SIZE + 5)); 702237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x04)) { 703c59becfcSMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 704c59becfcSMichael Halcrow data[(*packet_size) - 1]); 705237fead6SMichael Halcrow rc = -EINVAL; 706237fead6SMichael Halcrow goto out_free; 707237fead6SMichael Halcrow } 708237fead6SMichael Halcrow ecryptfs_cipher_code_to_string(crypt_stat->cipher, 709237fead6SMichael Halcrow (u16)data[(*packet_size)]); 710237fead6SMichael Halcrow /* A little extra work to differentiate among the AES key 711237fead6SMichael Halcrow * sizes; see RFC2440 */ 712237fead6SMichael Halcrow switch(data[(*packet_size)++]) { 713237fead6SMichael Halcrow case RFC2440_CIPHER_AES_192: 714237fead6SMichael Halcrow crypt_stat->key_size = 24; 715237fead6SMichael Halcrow break; 716237fead6SMichael Halcrow default: 717237fead6SMichael Halcrow crypt_stat->key_size = 718237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 719237fead6SMichael Halcrow } 720237fead6SMichael Halcrow ecryptfs_init_crypt_ctx(crypt_stat); 721237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 722c59becfcSMichael Halcrow printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 723237fead6SMichael Halcrow rc = -ENOSYS; 724237fead6SMichael Halcrow goto out_free; 725237fead6SMichael Halcrow } 726237fead6SMichael Halcrow /* TODO: finish the hash mapping */ 727237fead6SMichael Halcrow switch (data[(*packet_size)++]) { 728237fead6SMichael Halcrow case 0x01: /* See RFC2440 for these numbers and their mappings */ 729237fead6SMichael Halcrow /* Choose MD5 */ 730237fead6SMichael Halcrow memcpy((*new_auth_tok)->token.password.salt, 731237fead6SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 732237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; 733237fead6SMichael Halcrow /* This conversion was taken straight from RFC2440 */ 734237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_iterations = 735237fead6SMichael Halcrow ((u32) 16 + (data[(*packet_size)] & 15)) 736237fead6SMichael Halcrow << ((data[(*packet_size)] >> 4) + 6); 737237fead6SMichael Halcrow (*packet_size)++; 738c59becfcSMichael Halcrow /* Friendly reminder: 739c59becfcSMichael Halcrow * (*new_auth_tok)->session_key.encrypted_key_size = 740c59becfcSMichael Halcrow * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 741237fead6SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 742237fead6SMichael Halcrow &data[(*packet_size)], 743237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 744237fead6SMichael Halcrow (*packet_size) += 745237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 746237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags &= 747237fead6SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 748237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags |= 749237fead6SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 750c59becfcSMichael Halcrow (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 751237fead6SMichael Halcrow break; 752237fead6SMichael Halcrow default: 753237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 754237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 755237fead6SMichael Halcrow rc = -ENOSYS; 756237fead6SMichael Halcrow goto out_free; 757237fead6SMichael Halcrow } 758237fead6SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 759237fead6SMichael Halcrow /* TODO: Parametarize; we might actually want userspace to 760237fead6SMichael Halcrow * decrypt the session key. */ 761e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 762e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 763e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 764e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 765237fead6SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 766237fead6SMichael Halcrow goto out; 767237fead6SMichael Halcrow out_free: 768237fead6SMichael Halcrow (*new_auth_tok) = NULL; 769237fead6SMichael Halcrow memset(auth_tok_list_item, 0, 770237fead6SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 771237fead6SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 772237fead6SMichael Halcrow auth_tok_list_item); 773237fead6SMichael Halcrow out: 774237fead6SMichael Halcrow if (rc) 775237fead6SMichael Halcrow (*packet_size) = 0; 776237fead6SMichael Halcrow return rc; 777237fead6SMichael Halcrow } 778237fead6SMichael Halcrow 779237fead6SMichael Halcrow /** 780237fead6SMichael Halcrow * parse_tag_11_packet 781237fead6SMichael Halcrow * @data: The raw bytes of the packet 782237fead6SMichael Halcrow * @contents: This function writes the data contents of the literal 783237fead6SMichael Halcrow * packet into this memory location 784237fead6SMichael Halcrow * @max_contents_bytes: The maximum number of bytes that this function 785237fead6SMichael Halcrow * is allowed to write into contents 786237fead6SMichael Halcrow * @tag_11_contents_size: This function writes the size of the parsed 787237fead6SMichael Halcrow * contents into this memory location; zero on 788237fead6SMichael Halcrow * error 789237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 790237fead6SMichael Halcrow * into this memory location; zero on error 791237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 792237fead6SMichael Halcrow * 793237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 794237fead6SMichael Halcrow */ 795237fead6SMichael Halcrow static int 796237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents, 797237fead6SMichael Halcrow size_t max_contents_bytes, size_t *tag_11_contents_size, 798237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 799237fead6SMichael Halcrow { 800237fead6SMichael Halcrow size_t body_size; 801237fead6SMichael Halcrow size_t length_size; 802dddfa461SMichael Halcrow int rc = 0; 803237fead6SMichael Halcrow 804237fead6SMichael Halcrow (*packet_size) = 0; 805237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 806f648104aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 807f648104aSMichael Halcrow * packet tag 11 808f648104aSMichael Halcrow * 809f648104aSMichael Halcrow * Tag 11 identifier (1 byte) 810f648104aSMichael Halcrow * Max Tag 11 packet size (max 3 bytes) 811f648104aSMichael Halcrow * Binary format specifier (1 byte) 812f648104aSMichael Halcrow * Filename length (1 byte) 813f648104aSMichael Halcrow * Filename ("_CONSOLE") (8 bytes) 814f648104aSMichael Halcrow * Modification date (4 bytes) 815f648104aSMichael Halcrow * Literal data (arbitrary) 816f648104aSMichael Halcrow * 817f648104aSMichael Halcrow * We need at least 16 bytes of data for the packet to even be 818f648104aSMichael Halcrow * valid. 819237fead6SMichael Halcrow */ 820f648104aSMichael Halcrow if (max_packet_size < 16) { 821f648104aSMichael Halcrow printk(KERN_ERR "Maximum packet size too small\n"); 822237fead6SMichael Halcrow rc = -EINVAL; 823237fead6SMichael Halcrow goto out; 824237fead6SMichael Halcrow } 825237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 826f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 827237fead6SMichael Halcrow rc = -EINVAL; 828237fead6SMichael Halcrow goto out; 829237fead6SMichael Halcrow } 830f648104aSMichael Halcrow if ((rc = parse_packet_length(&data[(*packet_size)], &body_size, 831f648104aSMichael Halcrow &length_size))) { 832f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 833f648104aSMichael Halcrow goto out; 834f648104aSMichael Halcrow } 835f648104aSMichael Halcrow if (body_size < 14) { 836f648104aSMichael Halcrow printk(KERN_WARNING "Invalid body size ([%d])\n", body_size); 837f648104aSMichael Halcrow rc = -EINVAL; 838237fead6SMichael Halcrow goto out; 839237fead6SMichael Halcrow } 840237fead6SMichael Halcrow (*packet_size) += length_size; 841f648104aSMichael Halcrow (*tag_11_contents_size) = (body_size - 14); 842237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 843f648104aSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 844237fead6SMichael Halcrow rc = -EINVAL; 845237fead6SMichael Halcrow goto out; 846237fead6SMichael Halcrow } 847237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x62) { 848f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 849237fead6SMichael Halcrow rc = -EINVAL; 850237fead6SMichael Halcrow goto out; 851237fead6SMichael Halcrow } 852237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x08) { 853f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 854237fead6SMichael Halcrow rc = -EINVAL; 855237fead6SMichael Halcrow goto out; 856237fead6SMichael Halcrow } 857f648104aSMichael Halcrow (*packet_size) += 12; /* Ignore filename and modification date */ 858237fead6SMichael Halcrow memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 859237fead6SMichael Halcrow (*packet_size) += (*tag_11_contents_size); 860237fead6SMichael Halcrow out: 861237fead6SMichael Halcrow if (rc) { 862237fead6SMichael Halcrow (*packet_size) = 0; 863237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 864237fead6SMichael Halcrow } 865237fead6SMichael Halcrow return rc; 866237fead6SMichael Halcrow } 867237fead6SMichael Halcrow 868f4aad16aSMichael Halcrow static int 869f4aad16aSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig( 870f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok **global_auth_tok, 871f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 872f4aad16aSMichael Halcrow { 873f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *walker; 874f4aad16aSMichael Halcrow int rc = 0; 875f4aad16aSMichael Halcrow 876f4aad16aSMichael Halcrow (*global_auth_tok) = NULL; 877f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 878f4aad16aSMichael Halcrow list_for_each_entry(walker, 879f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 880f4aad16aSMichael Halcrow mount_crypt_stat_list) { 881f4aad16aSMichael Halcrow if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { 882f4aad16aSMichael Halcrow (*global_auth_tok) = walker; 883f4aad16aSMichael Halcrow goto out; 884f4aad16aSMichael Halcrow } 885f4aad16aSMichael Halcrow } 886f4aad16aSMichael Halcrow rc = -EINVAL; 887f4aad16aSMichael Halcrow out: 888f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 889f4aad16aSMichael Halcrow return rc; 890f4aad16aSMichael Halcrow } 891f4aad16aSMichael Halcrow 892237fead6SMichael Halcrow /** 893f4aad16aSMichael Halcrow * ecryptfs_verify_version 894f4aad16aSMichael Halcrow * @version: The version number to confirm 895f4aad16aSMichael Halcrow * 896f4aad16aSMichael Halcrow * Returns zero on good version; non-zero otherwise 897f4aad16aSMichael Halcrow */ 898f4aad16aSMichael Halcrow static int ecryptfs_verify_version(u16 version) 899f4aad16aSMichael Halcrow { 900f4aad16aSMichael Halcrow int rc = 0; 901f4aad16aSMichael Halcrow unsigned char major; 902f4aad16aSMichael Halcrow unsigned char minor; 903f4aad16aSMichael Halcrow 904f4aad16aSMichael Halcrow major = ((version >> 8) & 0xFF); 905f4aad16aSMichael Halcrow minor = (version & 0xFF); 906f4aad16aSMichael Halcrow if (major != ECRYPTFS_VERSION_MAJOR) { 907f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 908f4aad16aSMichael Halcrow "Expected [%d]; got [%d]\n", 909f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MAJOR, major); 910f4aad16aSMichael Halcrow rc = -EINVAL; 911f4aad16aSMichael Halcrow goto out; 912f4aad16aSMichael Halcrow } 913f4aad16aSMichael Halcrow if (minor != ECRYPTFS_VERSION_MINOR) { 914f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 915f4aad16aSMichael Halcrow "Expected [%d]; got [%d]\n", 916f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MINOR, minor); 917f4aad16aSMichael Halcrow rc = -EINVAL; 918f4aad16aSMichael Halcrow goto out; 919f4aad16aSMichael Halcrow } 920f4aad16aSMichael Halcrow out: 921f4aad16aSMichael Halcrow return rc; 922f4aad16aSMichael Halcrow } 923f4aad16aSMichael Halcrow 924f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 925f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 926f4aad16aSMichael Halcrow char *sig) 927f4aad16aSMichael Halcrow { 928f4aad16aSMichael Halcrow int rc = 0; 929f4aad16aSMichael Halcrow 930f4aad16aSMichael Halcrow (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 931f4aad16aSMichael Halcrow if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 932f4aad16aSMichael Halcrow printk(KERN_ERR "Could not find key with description: [%s]\n", 933f4aad16aSMichael Halcrow sig); 934f4aad16aSMichael Halcrow process_request_key_err(PTR_ERR(*auth_tok_key)); 935f4aad16aSMichael Halcrow rc = -EINVAL; 936f4aad16aSMichael Halcrow goto out; 937f4aad16aSMichael Halcrow } 938f4aad16aSMichael Halcrow (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); 939f4aad16aSMichael Halcrow if (ecryptfs_verify_version((*auth_tok)->version)) { 940f4aad16aSMichael Halcrow printk(KERN_ERR 941f4aad16aSMichael Halcrow "Data structure version mismatch. " 942f4aad16aSMichael Halcrow "Userspace tools must match eCryptfs " 943f4aad16aSMichael Halcrow "kernel module with major version [%d] " 944f4aad16aSMichael Halcrow "and minor version [%d]\n", 945f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MAJOR, 946f4aad16aSMichael Halcrow ECRYPTFS_VERSION_MINOR); 947f4aad16aSMichael Halcrow rc = -EINVAL; 948f4aad16aSMichael Halcrow goto out; 949f4aad16aSMichael Halcrow } 950f4aad16aSMichael Halcrow if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 951f4aad16aSMichael Halcrow && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 952f4aad16aSMichael Halcrow printk(KERN_ERR "Invalid auth_tok structure " 953f4aad16aSMichael Halcrow "returned from key query\n"); 954f4aad16aSMichael Halcrow rc = -EINVAL; 955f4aad16aSMichael Halcrow goto out; 956f4aad16aSMichael Halcrow } 957f4aad16aSMichael Halcrow out: 958f4aad16aSMichael Halcrow return rc; 959f4aad16aSMichael Halcrow } 960f4aad16aSMichael Halcrow 961f4aad16aSMichael Halcrow /** 962f4aad16aSMichael Halcrow * ecryptfs_find_auth_tok_for_sig 963f4aad16aSMichael Halcrow * @auth_tok: Set to the matching auth_tok; NULL if not found 964f4aad16aSMichael Halcrow * @crypt_stat: inode crypt_stat crypto context 965f4aad16aSMichael Halcrow * @sig: Sig of auth_tok to find 966f4aad16aSMichael Halcrow * 967f4aad16aSMichael Halcrow * For now, this function simply looks at the registered auth_tok's 968f4aad16aSMichael Halcrow * linked off the mount_crypt_stat, so all the auth_toks that can be 969f4aad16aSMichael Halcrow * used must be registered at mount time. This function could 970f4aad16aSMichael Halcrow * potentially try a lot harder to find auth_tok's (e.g., by calling 971f4aad16aSMichael Halcrow * out to ecryptfsd to dynamically retrieve an auth_tok object) so 972f4aad16aSMichael Halcrow * that static registration of auth_tok's will no longer be necessary. 973f4aad16aSMichael Halcrow * 974f4aad16aSMichael Halcrow * Returns zero on no error; non-zero on error 975f4aad16aSMichael Halcrow */ 976f4aad16aSMichael Halcrow static int 977f4aad16aSMichael Halcrow ecryptfs_find_auth_tok_for_sig( 978f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 979f4aad16aSMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, char *sig) 980f4aad16aSMichael Halcrow { 981f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 982f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 983f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *global_auth_tok; 984f4aad16aSMichael Halcrow int rc = 0; 985f4aad16aSMichael Halcrow 986f4aad16aSMichael Halcrow (*auth_tok) = NULL; 987f4aad16aSMichael Halcrow if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 988f4aad16aSMichael Halcrow mount_crypt_stat, sig)) { 989f4aad16aSMichael Halcrow struct key *auth_tok_key; 990f4aad16aSMichael Halcrow 991f4aad16aSMichael Halcrow rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, 992f4aad16aSMichael Halcrow sig); 993f4aad16aSMichael Halcrow } else 994f4aad16aSMichael Halcrow (*auth_tok) = global_auth_tok->global_auth_tok; 995f4aad16aSMichael Halcrow return rc; 996f4aad16aSMichael Halcrow } 997f4aad16aSMichael Halcrow 998f4aad16aSMichael Halcrow /** 999f4aad16aSMichael Halcrow * decrypt_passphrase_encrypted_session_key - Decrypt the session key 1000f4aad16aSMichael Halcrow * with the given auth_tok. 1001237fead6SMichael Halcrow * 1002237fead6SMichael Halcrow * Returns Zero on success; non-zero error otherwise. 1003237fead6SMichael Halcrow */ 1004f4aad16aSMichael Halcrow static int 1005f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1006237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1007237fead6SMichael Halcrow { 1008f4aad16aSMichael Halcrow struct scatterlist dst_sg; 1009f4aad16aSMichael Halcrow struct scatterlist src_sg; 1010237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 10118bba066fSMichael Halcrow struct blkcipher_desc desc = { 10128bba066fSMichael Halcrow .flags = CRYPTO_TFM_REQ_MAY_SLEEP 10138bba066fSMichael Halcrow }; 10148bba066fSMichael Halcrow int rc = 0; 1015237fead6SMichael Halcrow 1016f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1017f4aad16aSMichael Halcrow ecryptfs_printk( 1018f4aad16aSMichael Halcrow KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1019f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1020f4aad16aSMichael Halcrow ecryptfs_dump_hex( 1021f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1022f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1023f4aad16aSMichael Halcrow } 1024f4aad16aSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 1025f4aad16aSMichael Halcrow crypt_stat->cipher); 1026f4aad16aSMichael Halcrow if (unlikely(rc)) { 1027f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1028f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1029f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1030237fead6SMichael Halcrow goto out; 1031237fead6SMichael Halcrow } 1032f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1033f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size, 1034f4aad16aSMichael Halcrow &src_sg, 1)) != 1) { 1035f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1036f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key to scatterlist; " 1037f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1038f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1039f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size); 1040f4aad16aSMichael Halcrow goto out; 1041237fead6SMichael Halcrow } 1042f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size = 1043f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size; 1044f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1045f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size, 1046f4aad16aSMichael Halcrow &dst_sg, 1)) != 1) { 1047f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1048f4aad16aSMichael Halcrow "auth_tok->session_key.decrypted_key to scatterlist; " 1049f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]\n", rc); 1050f4aad16aSMichael Halcrow goto out; 1051f4aad16aSMichael Halcrow } 1052237fead6SMichael Halcrow mutex_lock(tfm_mutex); 1053f4aad16aSMichael Halcrow rc = crypto_blkcipher_setkey( 1054f4aad16aSMichael Halcrow desc.tfm, auth_tok->token.password.session_key_encryption_key, 1055237fead6SMichael Halcrow crypt_stat->key_size); 1056f4aad16aSMichael Halcrow if (unlikely(rc < 0)) { 1057f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1058e5d9cbdeSMichael Halcrow printk(KERN_ERR "Error setting key for crypto context\n"); 1059e5d9cbdeSMichael Halcrow rc = -EINVAL; 1060f4aad16aSMichael Halcrow goto out; 1061e5d9cbdeSMichael Halcrow } 1062f4aad16aSMichael Halcrow rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, 1063237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size); 1064f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1065f4aad16aSMichael Halcrow if (unlikely(rc)) { 10668bba066fSMichael Halcrow printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1067f4aad16aSMichael Halcrow goto out; 10688bba066fSMichael Halcrow } 1069237fead6SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1070237fead6SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1071237fead6SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1072e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1073f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1074f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", 1075f4aad16aSMichael Halcrow crypt_stat->key_size); 1076237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1077237fead6SMichael Halcrow crypt_stat->key_size); 1078f4aad16aSMichael Halcrow } 1079237fead6SMichael Halcrow out: 1080237fead6SMichael Halcrow return rc; 1081237fead6SMichael Halcrow } 1082237fead6SMichael Halcrow 1083f4aad16aSMichael Halcrow int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1084f4aad16aSMichael Halcrow { 1085f4aad16aSMichael Halcrow int rc = 0; 1086f4aad16aSMichael Halcrow 1087f4aad16aSMichael Halcrow (*sig) = NULL; 1088f4aad16aSMichael Halcrow switch (auth_tok->token_type) { 1089f4aad16aSMichael Halcrow case ECRYPTFS_PASSWORD: 1090f4aad16aSMichael Halcrow (*sig) = auth_tok->token.password.signature; 1091f4aad16aSMichael Halcrow break; 1092f4aad16aSMichael Halcrow case ECRYPTFS_PRIVATE_KEY: 1093f4aad16aSMichael Halcrow (*sig) = auth_tok->token.private_key.signature; 1094f4aad16aSMichael Halcrow break; 1095f4aad16aSMichael Halcrow default: 1096f4aad16aSMichael Halcrow printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1097f4aad16aSMichael Halcrow auth_tok->token_type); 1098f4aad16aSMichael Halcrow rc = -EINVAL; 1099f4aad16aSMichael Halcrow } 1100f4aad16aSMichael Halcrow return rc; 1101f4aad16aSMichael Halcrow } 1102f4aad16aSMichael Halcrow 1103237fead6SMichael Halcrow /** 1104237fead6SMichael Halcrow * ecryptfs_parse_packet_set 1105237fead6SMichael Halcrow * @dest: The header page in memory 1106237fead6SMichael Halcrow * @version: Version of file format, to guide parsing behavior 1107237fead6SMichael Halcrow * 1108237fead6SMichael Halcrow * Get crypt_stat to have the file's session key if the requisite key 1109237fead6SMichael Halcrow * is available to decrypt the session key. 1110237fead6SMichael Halcrow * 1111237fead6SMichael Halcrow * Returns Zero if a valid authentication token was retrieved and 1112237fead6SMichael Halcrow * processed; negative value for file not encrypted or for error 1113237fead6SMichael Halcrow * conditions. 1114237fead6SMichael Halcrow */ 1115237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1116237fead6SMichael Halcrow unsigned char *src, 1117237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1118237fead6SMichael Halcrow { 1119237fead6SMichael Halcrow size_t i = 0; 1120f4aad16aSMichael Halcrow size_t found_auth_tok; 1121237fead6SMichael Halcrow size_t next_packet_is_auth_tok_packet; 1122237fead6SMichael Halcrow struct list_head auth_tok_list; 1123f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *matching_auth_tok = NULL; 1124237fead6SMichael Halcrow struct ecryptfs_auth_tok *candidate_auth_tok = NULL; 1125f4aad16aSMichael Halcrow char *candidate_auth_tok_sig; 1126237fead6SMichael Halcrow size_t packet_size; 1127237fead6SMichael Halcrow struct ecryptfs_auth_tok *new_auth_tok; 1128237fead6SMichael Halcrow unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1129f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1130237fead6SMichael Halcrow size_t tag_11_contents_size; 1131237fead6SMichael Halcrow size_t tag_11_packet_size; 1132dddfa461SMichael Halcrow int rc = 0; 1133237fead6SMichael Halcrow 1134237fead6SMichael Halcrow INIT_LIST_HEAD(&auth_tok_list); 1135f4aad16aSMichael Halcrow /* Parse the header to find as many packets as we can; these will be 1136237fead6SMichael Halcrow * added the our &auth_tok_list */ 1137237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 1; 1138237fead6SMichael Halcrow while (next_packet_is_auth_tok_packet) { 1139237fead6SMichael Halcrow size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); 1140237fead6SMichael Halcrow 1141237fead6SMichael Halcrow switch (src[i]) { 1142237fead6SMichael Halcrow case ECRYPTFS_TAG_3_PACKET_TYPE: 1143237fead6SMichael Halcrow rc = parse_tag_3_packet(crypt_stat, 1144237fead6SMichael Halcrow (unsigned char *)&src[i], 1145237fead6SMichael Halcrow &auth_tok_list, &new_auth_tok, 1146237fead6SMichael Halcrow &packet_size, max_packet_size); 1147237fead6SMichael Halcrow if (rc) { 1148237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1149237fead6SMichael Halcrow "tag 3 packet\n"); 1150237fead6SMichael Halcrow rc = -EIO; 1151237fead6SMichael Halcrow goto out_wipe_list; 1152237fead6SMichael Halcrow } 1153237fead6SMichael Halcrow i += packet_size; 1154237fead6SMichael Halcrow rc = parse_tag_11_packet((unsigned char *)&src[i], 1155237fead6SMichael Halcrow sig_tmp_space, 1156237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1157237fead6SMichael Halcrow &tag_11_contents_size, 1158237fead6SMichael Halcrow &tag_11_packet_size, 1159237fead6SMichael Halcrow max_packet_size); 1160237fead6SMichael Halcrow if (rc) { 1161237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No valid " 1162237fead6SMichael Halcrow "(ecryptfs-specific) literal " 1163237fead6SMichael Halcrow "packet containing " 1164237fead6SMichael Halcrow "authentication token " 1165237fead6SMichael Halcrow "signature found after " 1166237fead6SMichael Halcrow "tag 3 packet\n"); 1167237fead6SMichael Halcrow rc = -EIO; 1168237fead6SMichael Halcrow goto out_wipe_list; 1169237fead6SMichael Halcrow } 1170237fead6SMichael Halcrow i += tag_11_packet_size; 1171237fead6SMichael Halcrow if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1172237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Expected " 1173237fead6SMichael Halcrow "signature of size [%d]; " 1174237fead6SMichael Halcrow "read size [%d]\n", 1175237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1176237fead6SMichael Halcrow tag_11_contents_size); 1177237fead6SMichael Halcrow rc = -EIO; 1178237fead6SMichael Halcrow goto out_wipe_list; 1179237fead6SMichael Halcrow } 1180237fead6SMichael Halcrow ecryptfs_to_hex(new_auth_tok->token.password.signature, 1181237fead6SMichael Halcrow sig_tmp_space, tag_11_contents_size); 1182237fead6SMichael Halcrow new_auth_tok->token.password.signature[ 1183237fead6SMichael Halcrow ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1184e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1185237fead6SMichael Halcrow break; 1186dddfa461SMichael Halcrow case ECRYPTFS_TAG_1_PACKET_TYPE: 1187dddfa461SMichael Halcrow rc = parse_tag_1_packet(crypt_stat, 1188dddfa461SMichael Halcrow (unsigned char *)&src[i], 1189dddfa461SMichael Halcrow &auth_tok_list, &new_auth_tok, 1190dddfa461SMichael Halcrow &packet_size, max_packet_size); 1191dddfa461SMichael Halcrow if (rc) { 1192dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1193dddfa461SMichael Halcrow "tag 1 packet\n"); 1194dddfa461SMichael Halcrow rc = -EIO; 1195dddfa461SMichael Halcrow goto out_wipe_list; 1196dddfa461SMichael Halcrow } 1197dddfa461SMichael Halcrow i += packet_size; 1198e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1199dddfa461SMichael Halcrow break; 1200237fead6SMichael Halcrow case ECRYPTFS_TAG_11_PACKET_TYPE: 1201237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1202237fead6SMichael Halcrow "(Tag 11 not allowed by itself)\n"); 1203237fead6SMichael Halcrow rc = -EIO; 1204237fead6SMichael Halcrow goto out_wipe_list; 1205237fead6SMichael Halcrow break; 1206237fead6SMichael Halcrow default: 1207237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "No packet at offset " 1208237fead6SMichael Halcrow "[%d] of the file header; hex value of " 1209237fead6SMichael Halcrow "character is [0x%.2x]\n", i, src[i]); 1210237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 0; 1211237fead6SMichael Halcrow } 1212237fead6SMichael Halcrow } 1213237fead6SMichael Halcrow if (list_empty(&auth_tok_list)) { 1214f4aad16aSMichael Halcrow printk(KERN_ERR "The lower file appears to be a non-encrypted " 1215f4aad16aSMichael Halcrow "eCryptfs file; this is not supported in this version " 1216f4aad16aSMichael Halcrow "of the eCryptfs kernel module\n"); 1217f4aad16aSMichael Halcrow rc = -EINVAL; 1218237fead6SMichael Halcrow goto out; 1219237fead6SMichael Halcrow } 1220f4aad16aSMichael Halcrow /* auth_tok_list contains the set of authentication tokens 1221f4aad16aSMichael Halcrow * parsed from the metadata. We need to find a matching 1222f4aad16aSMichael Halcrow * authentication token that has the secret component(s) 1223f4aad16aSMichael Halcrow * necessary to decrypt the EFEK in the auth_tok parsed from 1224f4aad16aSMichael Halcrow * the metadata. There may be several potential matches, but 1225f4aad16aSMichael Halcrow * just one will be sufficient to decrypt to get the FEK. */ 1226f4aad16aSMichael Halcrow find_next_matching_auth_tok: 1227f4aad16aSMichael Halcrow found_auth_tok = 0; 1228f4aad16aSMichael Halcrow list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1229237fead6SMichael Halcrow candidate_auth_tok = &auth_tok_list_item->auth_tok; 1230237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1231237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1232237fead6SMichael Halcrow "Considering cadidate auth tok:\n"); 1233237fead6SMichael Halcrow ecryptfs_dump_auth_tok(candidate_auth_tok); 1234237fead6SMichael Halcrow } 1235f4aad16aSMichael Halcrow if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 1236f4aad16aSMichael Halcrow candidate_auth_tok))) { 1237f4aad16aSMichael Halcrow printk(KERN_ERR 1238f4aad16aSMichael Halcrow "Unrecognized candidate auth tok type: [%d]\n", 1239f4aad16aSMichael Halcrow candidate_auth_tok->token_type); 1240f4aad16aSMichael Halcrow rc = -EINVAL; 1241f4aad16aSMichael Halcrow goto out_wipe_list; 1242f4aad16aSMichael Halcrow } 1243f4aad16aSMichael Halcrow if ((rc = ecryptfs_find_auth_tok_for_sig( 1244f4aad16aSMichael Halcrow &matching_auth_tok, crypt_stat, 1245f4aad16aSMichael Halcrow candidate_auth_tok_sig))) 1246f4aad16aSMichael Halcrow rc = 0; 1247f4aad16aSMichael Halcrow if (matching_auth_tok) { 1248237fead6SMichael Halcrow found_auth_tok = 1; 1249f4aad16aSMichael Halcrow goto found_matching_auth_tok; 1250237fead6SMichael Halcrow } 1251237fead6SMichael Halcrow } 1252237fead6SMichael Halcrow if (!found_auth_tok) { 1253f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Could not find a usable " 1254f4aad16aSMichael Halcrow "authentication token\n"); 1255237fead6SMichael Halcrow rc = -EIO; 1256237fead6SMichael Halcrow goto out_wipe_list; 1257dddfa461SMichael Halcrow } 1258f4aad16aSMichael Halcrow found_matching_auth_tok: 1259e2bd99ecSMichael Halcrow if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1260dddfa461SMichael Halcrow memcpy(&(candidate_auth_tok->token.private_key), 1261f4aad16aSMichael Halcrow &(matching_auth_tok->token.private_key), 1262dddfa461SMichael Halcrow sizeof(struct ecryptfs_private_key)); 1263f4aad16aSMichael Halcrow rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1264dddfa461SMichael Halcrow crypt_stat); 1265dddfa461SMichael Halcrow } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1266237fead6SMichael Halcrow memcpy(&(candidate_auth_tok->token.password), 1267f4aad16aSMichael Halcrow &(matching_auth_tok->token.password), 1268237fead6SMichael Halcrow sizeof(struct ecryptfs_password)); 1269f4aad16aSMichael Halcrow rc = decrypt_passphrase_encrypted_session_key( 1270f4aad16aSMichael Halcrow candidate_auth_tok, crypt_stat); 1271dddfa461SMichael Halcrow } 1272237fead6SMichael Halcrow if (rc) { 1273f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1274f4aad16aSMichael Halcrow 1275f4aad16aSMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1276f4aad16aSMichael Halcrow "session key for authentication token with sig " 1277f4aad16aSMichael Halcrow "[%.*s]; rc = [%d]. Removing auth tok " 1278f4aad16aSMichael Halcrow "candidate from the list and searching for " 1279f4aad16aSMichael Halcrow "the next match.\n", candidate_auth_tok_sig, 1280f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE_HEX, rc); 1281f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, 1282f4aad16aSMichael Halcrow auth_tok_list_item_tmp, 1283f4aad16aSMichael Halcrow &auth_tok_list, list) { 1284f4aad16aSMichael Halcrow if (candidate_auth_tok 1285f4aad16aSMichael Halcrow == &auth_tok_list_item->auth_tok) { 1286f4aad16aSMichael Halcrow list_del(&auth_tok_list_item->list); 1287f4aad16aSMichael Halcrow kmem_cache_free( 1288f4aad16aSMichael Halcrow ecryptfs_auth_tok_list_item_cache, 1289f4aad16aSMichael Halcrow auth_tok_list_item); 1290f4aad16aSMichael Halcrow goto find_next_matching_auth_tok; 1291f4aad16aSMichael Halcrow } 1292f4aad16aSMichael Halcrow } 1293f4aad16aSMichael Halcrow BUG(); 1294237fead6SMichael Halcrow } 1295237fead6SMichael Halcrow rc = ecryptfs_compute_root_iv(crypt_stat); 1296237fead6SMichael Halcrow if (rc) { 1297237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error computing " 1298237fead6SMichael Halcrow "the root IV\n"); 1299237fead6SMichael Halcrow goto out_wipe_list; 1300237fead6SMichael Halcrow } 1301237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 1302237fead6SMichael Halcrow if (rc) { 1303237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1304237fead6SMichael Halcrow "context for cipher [%s]; rc = [%d]\n", 1305237fead6SMichael Halcrow crypt_stat->cipher, rc); 1306237fead6SMichael Halcrow } 1307237fead6SMichael Halcrow out_wipe_list: 1308237fead6SMichael Halcrow wipe_auth_tok_list(&auth_tok_list); 1309237fead6SMichael Halcrow out: 1310237fead6SMichael Halcrow return rc; 1311237fead6SMichael Halcrow } 1312f4aad16aSMichael Halcrow 1313dddfa461SMichael Halcrow static int 1314dddfa461SMichael Halcrow pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, 1315dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1316dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec) 1317dddfa461SMichael Halcrow { 1318dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx = NULL; 1319dddfa461SMichael Halcrow char *netlink_payload; 1320dddfa461SMichael Halcrow size_t netlink_payload_length; 1321dddfa461SMichael Halcrow struct ecryptfs_message *msg; 1322dddfa461SMichael Halcrow int rc; 1323dddfa461SMichael Halcrow 1324dddfa461SMichael Halcrow rc = write_tag_66_packet(auth_tok->token.private_key.signature, 1325dddfa461SMichael Halcrow ecryptfs_code_for_cipher_string(crypt_stat), 1326dddfa461SMichael Halcrow crypt_stat, &netlink_payload, 1327dddfa461SMichael Halcrow &netlink_payload_length); 1328dddfa461SMichael Halcrow if (rc) { 1329dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 1330dddfa461SMichael Halcrow goto out; 1331dddfa461SMichael Halcrow } 1332dddfa461SMichael Halcrow rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload, 1333dddfa461SMichael Halcrow netlink_payload_length, &msg_ctx); 1334dddfa461SMichael Halcrow if (rc) { 1335dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); 1336dddfa461SMichael Halcrow goto out; 1337dddfa461SMichael Halcrow } 1338dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1339dddfa461SMichael Halcrow if (rc) { 1340dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 1341dddfa461SMichael Halcrow "from the user space daemon\n"); 1342dddfa461SMichael Halcrow rc = -EIO; 1343dddfa461SMichael Halcrow goto out; 1344dddfa461SMichael Halcrow } 1345dddfa461SMichael Halcrow rc = parse_tag_67_packet(key_rec, msg); 1346dddfa461SMichael Halcrow if (rc) 1347dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 1348dddfa461SMichael Halcrow kfree(msg); 1349dddfa461SMichael Halcrow out: 1350dddfa461SMichael Halcrow if (netlink_payload) 1351dddfa461SMichael Halcrow kfree(netlink_payload); 1352dddfa461SMichael Halcrow return rc; 1353dddfa461SMichael Halcrow } 1354dddfa461SMichael Halcrow /** 1355dddfa461SMichael Halcrow * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 1356dddfa461SMichael Halcrow * @dest: Buffer into which to write the packet 1357dddfa461SMichael Halcrow * @max: Maximum number of bytes that can be writtn 1358dddfa461SMichael Halcrow * @packet_size: This function will write the number of bytes that end 1359dddfa461SMichael Halcrow * up constituting the packet; set to zero on error 1360dddfa461SMichael Halcrow * 1361dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 1362dddfa461SMichael Halcrow */ 1363dddfa461SMichael Halcrow static int 1364f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes, 1365f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 1366dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1367dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 1368dddfa461SMichael Halcrow { 1369dddfa461SMichael Halcrow size_t i; 1370dddfa461SMichael Halcrow size_t encrypted_session_key_valid = 0; 1371dddfa461SMichael Halcrow size_t packet_size_length; 1372f4aad16aSMichael Halcrow size_t max_packet_size; 1373dddfa461SMichael Halcrow int rc = 0; 1374dddfa461SMichael Halcrow 1375dddfa461SMichael Halcrow (*packet_size) = 0; 1376dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 1377dddfa461SMichael Halcrow ECRYPTFS_SIG_SIZE); 1378dddfa461SMichael Halcrow encrypted_session_key_valid = 0; 1379dddfa461SMichael Halcrow for (i = 0; i < crypt_stat->key_size; i++) 1380dddfa461SMichael Halcrow encrypted_session_key_valid |= 1381dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key[i]; 1382dddfa461SMichael Halcrow if (encrypted_session_key_valid) { 1383dddfa461SMichael Halcrow memcpy(key_rec->enc_key, 1384dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key, 1385dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size); 1386dddfa461SMichael Halcrow goto encrypted_session_key_set; 1387dddfa461SMichael Halcrow } 1388dddfa461SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 1389dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size = 1390dddfa461SMichael Halcrow auth_tok->token.private_key.key_size; 1391dddfa461SMichael Halcrow rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); 1392dddfa461SMichael Halcrow if (rc) { 1393dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to encrypt session key " 1394dddfa461SMichael Halcrow "via a pki"); 1395dddfa461SMichael Halcrow goto out; 1396dddfa461SMichael Halcrow } 1397dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 1398dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 1399dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 1400dddfa461SMichael Halcrow } 1401dddfa461SMichael Halcrow encrypted_session_key_set: 1402f4aad16aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1403f4aad16aSMichael Halcrow * packet tag 1 */ 1404f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 1 identifier */ 1405f4aad16aSMichael Halcrow + 3 /* Max Tag 1 packet size */ 1406f4aad16aSMichael Halcrow + 1 /* Version */ 1407f4aad16aSMichael Halcrow + ECRYPTFS_SIG_SIZE /* Key identifier */ 1408f4aad16aSMichael Halcrow + 1 /* Cipher identifier */ 1409f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 1410f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 1411f4aad16aSMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 1412f4aad16aSMichael Halcrow "need up to [%d] bytes, but there are only [%d] " 1413f4aad16aSMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 1414dddfa461SMichael Halcrow rc = -EINVAL; 1415dddfa461SMichael Halcrow goto out; 1416dddfa461SMichael Halcrow } 1417dddfa461SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 1418f4aad16aSMichael Halcrow rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), 1419dddfa461SMichael Halcrow &packet_size_length); 1420dddfa461SMichael Halcrow if (rc) { 1421dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 1422dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 1423dddfa461SMichael Halcrow goto out; 1424dddfa461SMichael Halcrow } 1425dddfa461SMichael Halcrow (*packet_size) += packet_size_length; 1426dddfa461SMichael Halcrow dest[(*packet_size)++] = 0x03; /* version 3 */ 1427dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 1428dddfa461SMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 1429dddfa461SMichael Halcrow dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 1430dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 1431dddfa461SMichael Halcrow key_rec->enc_key_size); 1432dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 1433dddfa461SMichael Halcrow out: 1434dddfa461SMichael Halcrow if (rc) 1435dddfa461SMichael Halcrow (*packet_size) = 0; 1436f4aad16aSMichael Halcrow else 1437f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 1438dddfa461SMichael Halcrow return rc; 1439dddfa461SMichael Halcrow } 1440237fead6SMichael Halcrow 1441237fead6SMichael Halcrow /** 1442237fead6SMichael Halcrow * write_tag_11_packet 1443237fead6SMichael Halcrow * @dest: Target into which Tag 11 packet is to be written 1444237fead6SMichael Halcrow * @max: Maximum packet length 1445237fead6SMichael Halcrow * @contents: Byte array of contents to copy in 1446237fead6SMichael Halcrow * @contents_length: Number of bytes in contents 1447237fead6SMichael Halcrow * @packet_length: Length of the Tag 11 packet written; zero on error 1448237fead6SMichael Halcrow * 1449237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1450237fead6SMichael Halcrow */ 1451237fead6SMichael Halcrow static int 1452*146a4606SMichael Halcrow write_tag_11_packet(char *dest, int *remaining_bytes, char *contents, 1453*146a4606SMichael Halcrow size_t contents_length, size_t *packet_length) 1454237fead6SMichael Halcrow { 1455237fead6SMichael Halcrow size_t packet_size_length; 1456*146a4606SMichael Halcrow size_t max_packet_size; 1457dddfa461SMichael Halcrow int rc = 0; 1458237fead6SMichael Halcrow 1459237fead6SMichael Halcrow (*packet_length) = 0; 1460*146a4606SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1461*146a4606SMichael Halcrow * packet tag 11 */ 1462*146a4606SMichael Halcrow max_packet_size = (1 /* Tag 11 identifier */ 1463*146a4606SMichael Halcrow + 3 /* Max Tag 11 packet size */ 1464*146a4606SMichael Halcrow + 1 /* Binary format specifier */ 1465*146a4606SMichael Halcrow + 1 /* Filename length */ 1466*146a4606SMichael Halcrow + 8 /* Filename ("_CONSOLE") */ 1467*146a4606SMichael Halcrow + 4 /* Modification date */ 1468*146a4606SMichael Halcrow + contents_length); /* Literal data */ 1469*146a4606SMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 1470*146a4606SMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 1471*146a4606SMichael Halcrow "need up to [%d] bytes, but there are only [%d] " 1472*146a4606SMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 1473237fead6SMichael Halcrow rc = -EINVAL; 1474237fead6SMichael Halcrow goto out; 1475237fead6SMichael Halcrow } 1476237fead6SMichael Halcrow dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 1477237fead6SMichael Halcrow rc = write_packet_length(&dest[(*packet_length)], 1478*146a4606SMichael Halcrow (max_packet_size - 4), &packet_size_length); 1479237fead6SMichael Halcrow if (rc) { 1480*146a4606SMichael Halcrow printk(KERN_ERR "Error generating tag 11 packet header; cannot " 1481*146a4606SMichael Halcrow "generate packet length. rc = [%d]\n", rc); 1482237fead6SMichael Halcrow goto out; 1483237fead6SMichael Halcrow } 1484237fead6SMichael Halcrow (*packet_length) += packet_size_length; 1485*146a4606SMichael Halcrow dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 1486237fead6SMichael Halcrow dest[(*packet_length)++] = 8; 1487237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 1488237fead6SMichael Halcrow (*packet_length) += 8; 1489237fead6SMichael Halcrow memset(&dest[(*packet_length)], 0x00, 4); 1490237fead6SMichael Halcrow (*packet_length) += 4; 1491237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], contents, contents_length); 1492237fead6SMichael Halcrow (*packet_length) += contents_length; 1493237fead6SMichael Halcrow out: 1494237fead6SMichael Halcrow if (rc) 1495237fead6SMichael Halcrow (*packet_length) = 0; 1496*146a4606SMichael Halcrow else 1497*146a4606SMichael Halcrow (*remaining_bytes) -= (*packet_length); 1498237fead6SMichael Halcrow return rc; 1499237fead6SMichael Halcrow } 1500237fead6SMichael Halcrow 1501237fead6SMichael Halcrow /** 1502237fead6SMichael Halcrow * write_tag_3_packet 1503237fead6SMichael Halcrow * @dest: Buffer into which to write the packet 1504237fead6SMichael Halcrow * @max: Maximum number of bytes that can be written 1505237fead6SMichael Halcrow * @auth_tok: Authentication token 1506237fead6SMichael Halcrow * @crypt_stat: The cryptographic context 1507237fead6SMichael Halcrow * @key_rec: encrypted key 1508237fead6SMichael Halcrow * @packet_size: This function will write the number of bytes that end 1509237fead6SMichael Halcrow * up constituting the packet; set to zero on error 1510237fead6SMichael Halcrow * 1511237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1512237fead6SMichael Halcrow */ 1513237fead6SMichael Halcrow static int 1514f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes, 1515f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 1516237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1517237fead6SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 1518237fead6SMichael Halcrow { 1519237fead6SMichael Halcrow size_t i; 1520237fead6SMichael Halcrow size_t encrypted_session_key_valid = 0; 1521237fead6SMichael Halcrow char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 1522f4aad16aSMichael Halcrow struct scatterlist dst_sg; 1523f4aad16aSMichael Halcrow struct scatterlist src_sg; 1524237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 1525237fead6SMichael Halcrow size_t cipher_code; 1526f4aad16aSMichael Halcrow size_t packet_size_length; 1527f4aad16aSMichael Halcrow size_t max_packet_size; 1528f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1529f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 15308bba066fSMichael Halcrow struct blkcipher_desc desc = { 15318bba066fSMichael Halcrow .tfm = NULL, 15328bba066fSMichael Halcrow .flags = CRYPTO_TFM_REQ_MAY_SLEEP 15338bba066fSMichael Halcrow }; 15348bba066fSMichael Halcrow int rc = 0; 1535237fead6SMichael Halcrow 1536237fead6SMichael Halcrow (*packet_size) = 0; 1537dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 1538237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE); 1539f4aad16aSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, 1540f4aad16aSMichael Halcrow crypt_stat->cipher); 1541f4aad16aSMichael Halcrow if (unlikely(rc)) { 1542f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1543f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1544f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1545f4aad16aSMichael Halcrow goto out; 1546237fead6SMichael Halcrow } 1547f4aad16aSMichael Halcrow if (mount_crypt_stat->global_default_cipher_key_size == 0) { 1548f4aad16aSMichael Halcrow struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); 1549f4aad16aSMichael Halcrow 1550f4aad16aSMichael Halcrow printk(KERN_WARNING "No key size specified at mount; " 1551f4aad16aSMichael Halcrow "defaulting to [%d]\n", alg->max_keysize); 1552f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size = 1553f4aad16aSMichael Halcrow alg->max_keysize; 1554f4aad16aSMichael Halcrow } 1555f4aad16aSMichael Halcrow if (crypt_stat->key_size == 0) 1556f4aad16aSMichael Halcrow crypt_stat->key_size = 1557f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 1558237fead6SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 1559237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 1560237fead6SMichael Halcrow crypt_stat->key_size; 1561237fead6SMichael Halcrow if (crypt_stat->key_size == 24 1562237fead6SMichael Halcrow && strcmp("aes", crypt_stat->cipher) == 0) { 1563237fead6SMichael Halcrow memset((crypt_stat->key + 24), 0, 8); 1564237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 32; 1565f4aad16aSMichael Halcrow } else 1566f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 1567dddfa461SMichael Halcrow key_rec->enc_key_size = 1568237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size; 1569f4aad16aSMichael Halcrow encrypted_session_key_valid = 0; 1570f4aad16aSMichael Halcrow for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 1571f4aad16aSMichael Halcrow encrypted_session_key_valid |= 1572f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key[i]; 1573f4aad16aSMichael Halcrow if (encrypted_session_key_valid) { 1574f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 1575f4aad16aSMichael Halcrow "using auth_tok->session_key.encrypted_key, " 1576f4aad16aSMichael Halcrow "where key_rec->enc_key_size = [%d]\n", 1577f4aad16aSMichael Halcrow key_rec->enc_key_size); 1578f4aad16aSMichael Halcrow memcpy(key_rec->enc_key, 1579f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key, 1580f4aad16aSMichael Halcrow key_rec->enc_key_size); 1581f4aad16aSMichael Halcrow goto encrypted_session_key_set; 1582f4aad16aSMichael Halcrow } 1583dddfa461SMichael Halcrow if (auth_tok->token.password.flags & 1584dddfa461SMichael Halcrow ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 1585237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Using previously generated " 1586237fead6SMichael Halcrow "session key encryption key of size [%d]\n", 1587237fead6SMichael Halcrow auth_tok->token.password. 1588237fead6SMichael Halcrow session_key_encryption_key_bytes); 1589237fead6SMichael Halcrow memcpy(session_key_encryption_key, 1590237fead6SMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1591237fead6SMichael Halcrow crypt_stat->key_size); 1592237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1593237fead6SMichael Halcrow "Cached session key " "encryption key: \n"); 1594237fead6SMichael Halcrow if (ecryptfs_verbosity > 0) 1595237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 1596237fead6SMichael Halcrow } 1597237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1598237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 1599237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 1600237fead6SMichael Halcrow } 1601f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(crypt_stat->key, 1602f4aad16aSMichael Halcrow key_rec->enc_key_size, &src_sg, 1)) 1603f4aad16aSMichael Halcrow != 1) { 1604237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 1605f4aad16aSMichael Halcrow "for crypt_stat session key; expected rc = 1; " 1606f4aad16aSMichael Halcrow "got rc = [%d]. key_rec->enc_key_size = [%d]\n", 1607f4aad16aSMichael Halcrow rc, key_rec->enc_key_size); 1608237fead6SMichael Halcrow rc = -ENOMEM; 1609237fead6SMichael Halcrow goto out; 1610237fead6SMichael Halcrow } 1611f4aad16aSMichael Halcrow if ((rc = virt_to_scatterlist(key_rec->enc_key, 1612f4aad16aSMichael Halcrow key_rec->enc_key_size, &dst_sg, 1)) 1613f4aad16aSMichael Halcrow != 1) { 1614237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 1615f4aad16aSMichael Halcrow "for crypt_stat encrypted session key; " 1616f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1617f4aad16aSMichael Halcrow "key_rec->enc_key_size = [%d]\n", rc, 1618f4aad16aSMichael Halcrow key_rec->enc_key_size); 1619237fead6SMichael Halcrow rc = -ENOMEM; 1620237fead6SMichael Halcrow goto out; 1621237fead6SMichael Halcrow } 1622237fead6SMichael Halcrow mutex_lock(tfm_mutex); 16238bba066fSMichael Halcrow rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, 1624237fead6SMichael Halcrow crypt_stat->key_size); 1625237fead6SMichael Halcrow if (rc < 0) { 1626237fead6SMichael Halcrow mutex_unlock(tfm_mutex); 1627237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 16288bba066fSMichael Halcrow "context; rc = [%d]\n", rc); 1629237fead6SMichael Halcrow goto out; 1630237fead6SMichael Halcrow } 1631237fead6SMichael Halcrow rc = 0; 1632237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", 1633237fead6SMichael Halcrow crypt_stat->key_size); 1634f4aad16aSMichael Halcrow rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, 1635237fead6SMichael Halcrow (*key_rec).enc_key_size); 1636f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 16378bba066fSMichael Halcrow if (rc) { 16388bba066fSMichael Halcrow printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 16398bba066fSMichael Halcrow goto out; 16408bba066fSMichael Halcrow } 1641237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 1642f4aad16aSMichael Halcrow if (ecryptfs_verbosity > 0) { 1643f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", 1644f4aad16aSMichael Halcrow key_rec->enc_key_size); 1645dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, 1646dddfa461SMichael Halcrow key_rec->enc_key_size); 1647f4aad16aSMichael Halcrow } 1648237fead6SMichael Halcrow encrypted_session_key_set: 1649237fead6SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1650237fead6SMichael Halcrow * packet tag 3 */ 1651f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 3 identifier */ 1652f4aad16aSMichael Halcrow + 3 /* Max Tag 3 packet size */ 1653f4aad16aSMichael Halcrow + 1 /* Version */ 1654f4aad16aSMichael Halcrow + 1 /* Cipher code */ 1655f4aad16aSMichael Halcrow + 1 /* S2K specifier */ 1656f4aad16aSMichael Halcrow + 1 /* Hash identifier */ 1657f4aad16aSMichael Halcrow + ECRYPTFS_SALT_SIZE /* Salt */ 1658f4aad16aSMichael Halcrow + 1 /* Hash iterations */ 1659f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 1660f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 1661f4aad16aSMichael Halcrow printk(KERN_ERR "Packet too large; need up to [%d] bytes, but " 1662f4aad16aSMichael Halcrow "there are only [%d] available\n", max_packet_size, 1663f4aad16aSMichael Halcrow (*remaining_bytes)); 1664f4aad16aSMichael Halcrow rc = -EINVAL; 1665f4aad16aSMichael Halcrow goto out; 1666f4aad16aSMichael Halcrow } 1667237fead6SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 1668f4aad16aSMichael Halcrow /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 1669f4aad16aSMichael Halcrow * to get the number of octets in the actual Tag 3 packet */ 1670f4aad16aSMichael Halcrow rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), 1671237fead6SMichael Halcrow &packet_size_length); 1672237fead6SMichael Halcrow if (rc) { 1673f4aad16aSMichael Halcrow printk(KERN_ERR "Error generating tag 3 packet header; cannot " 1674f4aad16aSMichael Halcrow "generate packet length. rc = [%d]\n", rc); 1675237fead6SMichael Halcrow goto out; 1676237fead6SMichael Halcrow } 1677237fead6SMichael Halcrow (*packet_size) += packet_size_length; 1678237fead6SMichael Halcrow dest[(*packet_size)++] = 0x04; /* version 4 */ 1679f4aad16aSMichael Halcrow /* TODO: Break from RFC2440 so that arbitrary ciphers can be 1680f4aad16aSMichael Halcrow * specified with strings */ 1681237fead6SMichael Halcrow cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); 1682237fead6SMichael Halcrow if (cipher_code == 0) { 1683237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 1684237fead6SMichael Halcrow "cipher [%s]\n", crypt_stat->cipher); 1685237fead6SMichael Halcrow rc = -EINVAL; 1686237fead6SMichael Halcrow goto out; 1687237fead6SMichael Halcrow } 1688237fead6SMichael Halcrow dest[(*packet_size)++] = cipher_code; 1689237fead6SMichael Halcrow dest[(*packet_size)++] = 0x03; /* S2K */ 1690237fead6SMichael Halcrow dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 1691237fead6SMichael Halcrow memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 1692237fead6SMichael Halcrow ECRYPTFS_SALT_SIZE); 1693237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 1694237fead6SMichael Halcrow dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 1695dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 1696dddfa461SMichael Halcrow key_rec->enc_key_size); 1697dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 1698237fead6SMichael Halcrow out: 1699237fead6SMichael Halcrow if (rc) 1700237fead6SMichael Halcrow (*packet_size) = 0; 1701f4aad16aSMichael Halcrow else 1702f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 1703237fead6SMichael Halcrow return rc; 1704237fead6SMichael Halcrow } 1705237fead6SMichael Halcrow 1706eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache; 1707eb95e7ffSMichael Halcrow 1708237fead6SMichael Halcrow /** 1709237fead6SMichael Halcrow * ecryptfs_generate_key_packet_set 1710237fead6SMichael Halcrow * @dest: Virtual address from which to write the key record set 1711237fead6SMichael Halcrow * @crypt_stat: The cryptographic context from which the 1712237fead6SMichael Halcrow * authentication tokens will be retrieved 1713237fead6SMichael Halcrow * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 1714237fead6SMichael Halcrow * for the global parameters 1715237fead6SMichael Halcrow * @len: The amount written 1716237fead6SMichael Halcrow * @max: The maximum amount of data allowed to be written 1717237fead6SMichael Halcrow * 1718237fead6SMichael Halcrow * Generates a key packet set and writes it to the virtual address 1719237fead6SMichael Halcrow * passed in. 1720237fead6SMichael Halcrow * 1721237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1722237fead6SMichael Halcrow */ 1723237fead6SMichael Halcrow int 1724237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base, 1725237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1726237fead6SMichael Halcrow struct dentry *ecryptfs_dentry, size_t *len, 1727237fead6SMichael Halcrow size_t max) 1728237fead6SMichael Halcrow { 1729237fead6SMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 1730f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *global_auth_tok; 1731237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 1732237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 1733237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 1734237fead6SMichael Halcrow size_t written; 1735eb95e7ffSMichael Halcrow struct ecryptfs_key_record *key_rec; 1736f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig; 1737dddfa461SMichael Halcrow int rc = 0; 1738237fead6SMichael Halcrow 1739237fead6SMichael Halcrow (*len) = 0; 1740f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 1741eb95e7ffSMichael Halcrow key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 1742eb95e7ffSMichael Halcrow if (!key_rec) { 1743eb95e7ffSMichael Halcrow rc = -ENOMEM; 1744eb95e7ffSMichael Halcrow goto out; 1745eb95e7ffSMichael Halcrow } 1746f4aad16aSMichael Halcrow list_for_each_entry(key_sig, &crypt_stat->keysig_list, 1747f4aad16aSMichael Halcrow crypt_stat_list) { 1748f4aad16aSMichael Halcrow memset(key_rec, 0, sizeof(*key_rec)); 1749f4aad16aSMichael Halcrow rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, 1750f4aad16aSMichael Halcrow mount_crypt_stat, 1751f4aad16aSMichael Halcrow key_sig->keysig); 1752f4aad16aSMichael Halcrow if (rc) { 1753f4aad16aSMichael Halcrow printk(KERN_ERR "Error attempting to get the global " 1754f4aad16aSMichael Halcrow "auth_tok; rc = [%d]\n", rc); 1755f4aad16aSMichael Halcrow goto out_free; 1756f4aad16aSMichael Halcrow } 1757f4aad16aSMichael Halcrow if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { 1758f4aad16aSMichael Halcrow printk(KERN_WARNING 1759f4aad16aSMichael Halcrow "Skipping invalid auth tok with sig = [%s]\n", 1760f4aad16aSMichael Halcrow global_auth_tok->sig); 1761f4aad16aSMichael Halcrow continue; 1762f4aad16aSMichael Halcrow } 1763f4aad16aSMichael Halcrow auth_tok = global_auth_tok->global_auth_tok; 1764237fead6SMichael Halcrow if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 1765237fead6SMichael Halcrow rc = write_tag_3_packet((dest_base + (*len)), 1766f4aad16aSMichael Halcrow &max, auth_tok, 1767eb95e7ffSMichael Halcrow crypt_stat, key_rec, 1768237fead6SMichael Halcrow &written); 1769237fead6SMichael Halcrow if (rc) { 1770237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 1771237fead6SMichael Halcrow "writing tag 3 packet\n"); 1772eb95e7ffSMichael Halcrow goto out_free; 1773237fead6SMichael Halcrow } 1774237fead6SMichael Halcrow (*len) += written; 1775237fead6SMichael Halcrow /* Write auth tok signature packet */ 1776f4aad16aSMichael Halcrow rc = write_tag_11_packet((dest_base + (*len)), &max, 1777f4aad16aSMichael Halcrow key_rec->sig, 1778f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE, &written); 1779237fead6SMichael Halcrow if (rc) { 1780237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing " 1781237fead6SMichael Halcrow "auth tok signature packet\n"); 1782eb95e7ffSMichael Halcrow goto out_free; 1783237fead6SMichael Halcrow } 1784237fead6SMichael Halcrow (*len) += written; 1785dddfa461SMichael Halcrow } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1786dddfa461SMichael Halcrow rc = write_tag_1_packet(dest_base + (*len), 1787f4aad16aSMichael Halcrow &max, auth_tok, 1788f4aad16aSMichael Halcrow crypt_stat, key_rec, &written); 1789dddfa461SMichael Halcrow if (rc) { 1790dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 1791dddfa461SMichael Halcrow "writing tag 1 packet\n"); 1792eb95e7ffSMichael Halcrow goto out_free; 1793dddfa461SMichael Halcrow } 1794dddfa461SMichael Halcrow (*len) += written; 1795237fead6SMichael Halcrow } else { 1796237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unsupported " 1797237fead6SMichael Halcrow "authentication token type\n"); 1798237fead6SMichael Halcrow rc = -EINVAL; 1799eb95e7ffSMichael Halcrow goto out_free; 1800237fead6SMichael Halcrow } 1801f4aad16aSMichael Halcrow } 1802f4aad16aSMichael Halcrow if (likely(max > 0)) { 1803237fead6SMichael Halcrow dest_base[(*len)] = 0x00; 1804237fead6SMichael Halcrow } else { 1805237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 1806237fead6SMichael Halcrow rc = -EIO; 1807237fead6SMichael Halcrow } 1808eb95e7ffSMichael Halcrow out_free: 1809eb95e7ffSMichael Halcrow kmem_cache_free(ecryptfs_key_record_cache, key_rec); 1810237fead6SMichael Halcrow out: 1811237fead6SMichael Halcrow if (rc) 1812237fead6SMichael Halcrow (*len) = 0; 1813f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 1814237fead6SMichael Halcrow return rc; 1815237fead6SMichael Halcrow } 1816f4aad16aSMichael Halcrow 1817f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache; 1818f4aad16aSMichael Halcrow 1819f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 1820f4aad16aSMichael Halcrow { 1821f4aad16aSMichael Halcrow struct ecryptfs_key_sig *new_key_sig; 1822f4aad16aSMichael Halcrow int rc = 0; 1823f4aad16aSMichael Halcrow 1824f4aad16aSMichael Halcrow new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 1825f4aad16aSMichael Halcrow if (!new_key_sig) { 1826f4aad16aSMichael Halcrow rc = -ENOMEM; 1827f4aad16aSMichael Halcrow printk(KERN_ERR 1828f4aad16aSMichael Halcrow "Error allocating from ecryptfs_key_sig_cache\n"); 1829f4aad16aSMichael Halcrow goto out; 1830f4aad16aSMichael Halcrow } 1831f4aad16aSMichael Halcrow memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 1832f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 1833f4aad16aSMichael Halcrow list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 1834f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 1835f4aad16aSMichael Halcrow out: 1836f4aad16aSMichael Halcrow return rc; 1837f4aad16aSMichael Halcrow } 1838f4aad16aSMichael Halcrow 1839f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache; 1840f4aad16aSMichael Halcrow 1841f4aad16aSMichael Halcrow int 1842f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 1843f4aad16aSMichael Halcrow char *sig) 1844f4aad16aSMichael Halcrow { 1845f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *new_auth_tok; 1846f4aad16aSMichael Halcrow int rc = 0; 1847f4aad16aSMichael Halcrow 1848f4aad16aSMichael Halcrow new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache, 1849f4aad16aSMichael Halcrow GFP_KERNEL); 1850f4aad16aSMichael Halcrow if (!new_auth_tok) { 1851f4aad16aSMichael Halcrow rc = -ENOMEM; 1852f4aad16aSMichael Halcrow printk(KERN_ERR "Error allocating from " 1853f4aad16aSMichael Halcrow "ecryptfs_global_auth_tok_cache\n"); 1854f4aad16aSMichael Halcrow goto out; 1855f4aad16aSMichael Halcrow } 1856f4aad16aSMichael Halcrow memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 1857f4aad16aSMichael Halcrow new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 1858f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 1859f4aad16aSMichael Halcrow list_add(&new_auth_tok->mount_crypt_stat_list, 1860f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list); 1861f4aad16aSMichael Halcrow mount_crypt_stat->num_global_auth_toks++; 1862f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 1863f4aad16aSMichael Halcrow out: 1864f4aad16aSMichael Halcrow return rc; 1865f4aad16aSMichael Halcrow } 1866f4aad16aSMichael Halcrow 1867