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 283095e8e3SHerbert Xu #include <crypto/hash.h> 293095e8e3SHerbert Xu #include <crypto/skcipher.h> 30237fead6SMichael Halcrow #include <linux/string.h> 31237fead6SMichael Halcrow #include <linux/pagemap.h> 32237fead6SMichael Halcrow #include <linux/key.h> 33237fead6SMichael Halcrow #include <linux/random.h> 34237fead6SMichael Halcrow #include <linux/scatterlist.h> 355a0e3ad6STejun Heo #include <linux/slab.h> 36237fead6SMichael Halcrow #include "ecryptfs_kernel.h" 37237fead6SMichael Halcrow 38237fead6SMichael Halcrow /** 39237fead6SMichael Halcrow * request_key returned an error instead of a valid key address; 40237fead6SMichael Halcrow * determine the type of error, make appropriate log entries, and 41237fead6SMichael Halcrow * return an error code. 42237fead6SMichael Halcrow */ 43cd9d67dfSMichael Halcrow static int process_request_key_err(long err_code) 44237fead6SMichael Halcrow { 45237fead6SMichael Halcrow int rc = 0; 46237fead6SMichael Halcrow 47237fead6SMichael Halcrow switch (err_code) { 48982363c9SEric Sandeen case -ENOKEY: 49237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "No key\n"); 50237fead6SMichael Halcrow rc = -ENOENT; 51237fead6SMichael Halcrow break; 52982363c9SEric Sandeen case -EKEYEXPIRED: 53237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key expired\n"); 54237fead6SMichael Halcrow rc = -ETIME; 55237fead6SMichael Halcrow break; 56982363c9SEric Sandeen case -EKEYREVOKED: 57237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Key revoked\n"); 58237fead6SMichael Halcrow rc = -EINVAL; 59237fead6SMichael Halcrow break; 60237fead6SMichael Halcrow default: 61237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unknown error code: " 62888d57bbSJoe Perches "[0x%.16lx]\n", err_code); 63237fead6SMichael Halcrow rc = -EINVAL; 64237fead6SMichael Halcrow } 65237fead6SMichael Halcrow return rc; 66237fead6SMichael Halcrow } 67237fead6SMichael Halcrow 680e1fc5efSRoberto Sassu static int process_find_global_auth_tok_for_sig_err(int err_code) 690e1fc5efSRoberto Sassu { 700e1fc5efSRoberto Sassu int rc = err_code; 710e1fc5efSRoberto Sassu 720e1fc5efSRoberto Sassu switch (err_code) { 730e1fc5efSRoberto Sassu case -ENOENT: 740e1fc5efSRoberto Sassu ecryptfs_printk(KERN_WARNING, "Missing auth tok\n"); 750e1fc5efSRoberto Sassu break; 760e1fc5efSRoberto Sassu case -EINVAL: 770e1fc5efSRoberto Sassu ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n"); 780e1fc5efSRoberto Sassu break; 790e1fc5efSRoberto Sassu default: 800e1fc5efSRoberto Sassu rc = process_request_key_err(err_code); 810e1fc5efSRoberto Sassu break; 820e1fc5efSRoberto Sassu } 830e1fc5efSRoberto Sassu return rc; 840e1fc5efSRoberto Sassu } 850e1fc5efSRoberto Sassu 86237fead6SMichael Halcrow /** 87f66e883eSMichael Halcrow * ecryptfs_parse_packet_length 88237fead6SMichael Halcrow * @data: Pointer to memory containing length at offset 89237fead6SMichael Halcrow * @size: This function writes the decoded size to this memory 90237fead6SMichael Halcrow * address; zero on error 91237fead6SMichael Halcrow * @length_size: The number of bytes occupied by the encoded length 92237fead6SMichael Halcrow * 9322e78fafSMichael Halcrow * Returns zero on success; non-zero on error 94237fead6SMichael Halcrow */ 95f66e883eSMichael Halcrow int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, 96237fead6SMichael Halcrow size_t *length_size) 97237fead6SMichael Halcrow { 98237fead6SMichael Halcrow int rc = 0; 99237fead6SMichael Halcrow 100237fead6SMichael Halcrow (*length_size) = 0; 101237fead6SMichael Halcrow (*size) = 0; 102237fead6SMichael Halcrow if (data[0] < 192) { 103237fead6SMichael Halcrow /* One-byte length */ 104831115afSTyler Hicks (*size) = data[0]; 105237fead6SMichael Halcrow (*length_size) = 1; 106237fead6SMichael Halcrow } else if (data[0] < 224) { 107237fead6SMichael Halcrow /* Two-byte length */ 108831115afSTyler Hicks (*size) = (data[0] - 192) * 256; 109831115afSTyler Hicks (*size) += data[1] + 192; 110237fead6SMichael Halcrow (*length_size) = 2; 111237fead6SMichael Halcrow } else if (data[0] == 255) { 11248399c0bSTyler Hicks /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 113237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Five-byte packet length not " 114237fead6SMichael Halcrow "supported\n"); 115237fead6SMichael Halcrow rc = -EINVAL; 116237fead6SMichael Halcrow goto out; 117237fead6SMichael Halcrow } else { 118237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); 119237fead6SMichael Halcrow rc = -EINVAL; 120237fead6SMichael Halcrow goto out; 121237fead6SMichael Halcrow } 122237fead6SMichael Halcrow out: 123237fead6SMichael Halcrow return rc; 124237fead6SMichael Halcrow } 125237fead6SMichael Halcrow 126237fead6SMichael Halcrow /** 127f66e883eSMichael Halcrow * ecryptfs_write_packet_length 12822e78fafSMichael Halcrow * @dest: The byte array target into which to write the length. Must 12948399c0bSTyler Hicks * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated. 130237fead6SMichael Halcrow * @size: The length to write. 13122e78fafSMichael Halcrow * @packet_size_length: The number of bytes used to encode the packet 13222e78fafSMichael Halcrow * length is written to this address. 133237fead6SMichael Halcrow * 134237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 135237fead6SMichael Halcrow */ 136f66e883eSMichael Halcrow int ecryptfs_write_packet_length(char *dest, size_t size, 137237fead6SMichael Halcrow size_t *packet_size_length) 138237fead6SMichael Halcrow { 139237fead6SMichael Halcrow int rc = 0; 140237fead6SMichael Halcrow 141237fead6SMichael Halcrow if (size < 192) { 142237fead6SMichael Halcrow dest[0] = size; 143237fead6SMichael Halcrow (*packet_size_length) = 1; 144237fead6SMichael Halcrow } else if (size < 65536) { 145237fead6SMichael Halcrow dest[0] = (((size - 192) / 256) + 192); 146237fead6SMichael Halcrow dest[1] = ((size - 192) % 256); 147237fead6SMichael Halcrow (*packet_size_length) = 2; 148237fead6SMichael Halcrow } else { 14948399c0bSTyler Hicks /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ 150237fead6SMichael Halcrow rc = -EINVAL; 151237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, 152f24b3887STyler Hicks "Unsupported packet size: [%zd]\n", size); 153237fead6SMichael Halcrow } 154237fead6SMichael Halcrow return rc; 155237fead6SMichael Halcrow } 156237fead6SMichael Halcrow 157dddfa461SMichael Halcrow static int 158dddfa461SMichael Halcrow write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, 159dddfa461SMichael Halcrow char **packet, size_t *packet_len) 160dddfa461SMichael Halcrow { 161dddfa461SMichael Halcrow size_t i = 0; 162dddfa461SMichael Halcrow size_t data_len; 163dddfa461SMichael Halcrow size_t packet_size_len; 164dddfa461SMichael Halcrow char *message; 165dddfa461SMichael Halcrow int rc; 166dddfa461SMichael Halcrow 167dddfa461SMichael Halcrow /* 168dddfa461SMichael Halcrow * ***** TAG 64 Packet Format ***** 169dddfa461SMichael Halcrow * | Content Type | 1 byte | 170dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 171dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 172dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 173dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 174dddfa461SMichael Halcrow */ 175dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX 176dddfa461SMichael Halcrow + session_key->encrypted_key_size); 177dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 178dddfa461SMichael Halcrow message = *packet; 179dddfa461SMichael Halcrow if (!message) { 180dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 181dddfa461SMichael Halcrow rc = -ENOMEM; 182dddfa461SMichael Halcrow goto out; 183dddfa461SMichael Halcrow } 184dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; 185f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 186dddfa461SMichael Halcrow &packet_size_len); 187dddfa461SMichael Halcrow if (rc) { 188dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 189dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 190dddfa461SMichael Halcrow goto out; 191dddfa461SMichael Halcrow } 192dddfa461SMichael Halcrow i += packet_size_len; 193dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 194dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 195f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], 196f66e883eSMichael Halcrow session_key->encrypted_key_size, 197dddfa461SMichael Halcrow &packet_size_len); 198dddfa461SMichael Halcrow if (rc) { 199dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " 200dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 201dddfa461SMichael Halcrow goto out; 202dddfa461SMichael Halcrow } 203dddfa461SMichael Halcrow i += packet_size_len; 204dddfa461SMichael Halcrow memcpy(&message[i], session_key->encrypted_key, 205dddfa461SMichael Halcrow session_key->encrypted_key_size); 206dddfa461SMichael Halcrow i += session_key->encrypted_key_size; 207dddfa461SMichael Halcrow *packet_len = i; 208dddfa461SMichael Halcrow out: 209dddfa461SMichael Halcrow return rc; 210dddfa461SMichael Halcrow } 211dddfa461SMichael Halcrow 212dddfa461SMichael Halcrow static int 21319e66a67STrevor Highland parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, 214dddfa461SMichael Halcrow struct ecryptfs_message *msg) 215dddfa461SMichael Halcrow { 216dddfa461SMichael Halcrow size_t i = 0; 217dddfa461SMichael Halcrow char *data; 218dddfa461SMichael Halcrow size_t data_len; 219dddfa461SMichael Halcrow size_t m_size; 220dddfa461SMichael Halcrow size_t message_len; 221dddfa461SMichael Halcrow u16 checksum = 0; 222dddfa461SMichael Halcrow u16 expected_checksum = 0; 223dddfa461SMichael Halcrow int rc; 224dddfa461SMichael Halcrow 225dddfa461SMichael Halcrow /* 226dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 227dddfa461SMichael Halcrow * | Content Type | 1 byte | 228dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 229dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 230dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 231dddfa461SMichael Halcrow */ 232dddfa461SMichael Halcrow message_len = msg->data_len; 233dddfa461SMichael Halcrow data = msg->data; 234dddfa461SMichael Halcrow if (message_len < 4) { 235dddfa461SMichael Halcrow rc = -EIO; 236dddfa461SMichael Halcrow goto out; 237dddfa461SMichael Halcrow } 238dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { 239dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); 240dddfa461SMichael Halcrow rc = -EIO; 241dddfa461SMichael Halcrow goto out; 242dddfa461SMichael Halcrow } 243dddfa461SMichael Halcrow if (data[i++]) { 244dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " 245dddfa461SMichael Halcrow "[%d]\n", data[i-1]); 246dddfa461SMichael Halcrow rc = -EIO; 247dddfa461SMichael Halcrow goto out; 248dddfa461SMichael Halcrow } 249f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); 250dddfa461SMichael Halcrow if (rc) { 251dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 252dddfa461SMichael Halcrow "rc = [%d]\n", rc); 253dddfa461SMichael Halcrow goto out; 254dddfa461SMichael Halcrow } 255dddfa461SMichael Halcrow i += data_len; 256dddfa461SMichael Halcrow if (message_len < (i + m_size)) { 257624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " 258624ae528STyler Hicks "is shorter than expected\n"); 259dddfa461SMichael Halcrow rc = -EIO; 260dddfa461SMichael Halcrow goto out; 261dddfa461SMichael Halcrow } 262dddfa461SMichael Halcrow if (m_size < 3) { 263dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, 264dddfa461SMichael Halcrow "The decrypted key is not long enough to " 265dddfa461SMichael Halcrow "include a cipher code and checksum\n"); 266dddfa461SMichael Halcrow rc = -EIO; 267dddfa461SMichael Halcrow goto out; 268dddfa461SMichael Halcrow } 269dddfa461SMichael Halcrow *cipher_code = data[i++]; 270dddfa461SMichael Halcrow /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ 271dddfa461SMichael Halcrow session_key->decrypted_key_size = m_size - 3; 272dddfa461SMichael Halcrow if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { 273dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " 274dddfa461SMichael Halcrow "the maximum key size [%d]\n", 275dddfa461SMichael Halcrow session_key->decrypted_key_size, 276dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 277dddfa461SMichael Halcrow rc = -EIO; 278dddfa461SMichael Halcrow goto out; 279dddfa461SMichael Halcrow } 280dddfa461SMichael Halcrow memcpy(session_key->decrypted_key, &data[i], 281dddfa461SMichael Halcrow session_key->decrypted_key_size); 282dddfa461SMichael Halcrow i += session_key->decrypted_key_size; 283dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]) << 8; 284dddfa461SMichael Halcrow expected_checksum += (unsigned char)(data[i++]); 285dddfa461SMichael Halcrow for (i = 0; i < session_key->decrypted_key_size; i++) 286dddfa461SMichael Halcrow checksum += session_key->decrypted_key[i]; 287dddfa461SMichael Halcrow if (expected_checksum != checksum) { 288dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Invalid checksum for file " 289dddfa461SMichael Halcrow "encryption key; expected [%x]; calculated " 290dddfa461SMichael Halcrow "[%x]\n", expected_checksum, checksum); 291dddfa461SMichael Halcrow rc = -EIO; 292dddfa461SMichael Halcrow } 293dddfa461SMichael Halcrow out: 294dddfa461SMichael Halcrow return rc; 295dddfa461SMichael Halcrow } 296dddfa461SMichael Halcrow 297dddfa461SMichael Halcrow 298dddfa461SMichael Halcrow static int 29919e66a67STrevor Highland write_tag_66_packet(char *signature, u8 cipher_code, 300dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, char **packet, 301dddfa461SMichael Halcrow size_t *packet_len) 302dddfa461SMichael Halcrow { 303dddfa461SMichael Halcrow size_t i = 0; 304dddfa461SMichael Halcrow size_t j; 305dddfa461SMichael Halcrow size_t data_len; 306dddfa461SMichael Halcrow size_t checksum = 0; 307dddfa461SMichael Halcrow size_t packet_size_len; 308dddfa461SMichael Halcrow char *message; 309dddfa461SMichael Halcrow int rc; 310dddfa461SMichael Halcrow 311dddfa461SMichael Halcrow /* 312dddfa461SMichael Halcrow * ***** TAG 66 Packet Format ***** 313dddfa461SMichael Halcrow * | Content Type | 1 byte | 314dddfa461SMichael Halcrow * | Key Identifier Size | 1 or 2 bytes | 315dddfa461SMichael Halcrow * | Key Identifier | arbitrary | 316dddfa461SMichael Halcrow * | File Encryption Key Size | 1 or 2 bytes | 317dddfa461SMichael Halcrow * | File Encryption Key | arbitrary | 318dddfa461SMichael Halcrow */ 319dddfa461SMichael Halcrow data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); 320dddfa461SMichael Halcrow *packet = kmalloc(data_len, GFP_KERNEL); 321dddfa461SMichael Halcrow message = *packet; 322dddfa461SMichael Halcrow if (!message) { 323dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); 324dddfa461SMichael Halcrow rc = -ENOMEM; 325dddfa461SMichael Halcrow goto out; 326dddfa461SMichael Halcrow } 327dddfa461SMichael Halcrow message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; 328f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, 329dddfa461SMichael Halcrow &packet_size_len); 330dddfa461SMichael Halcrow if (rc) { 331dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 332dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 333dddfa461SMichael Halcrow goto out; 334dddfa461SMichael Halcrow } 335dddfa461SMichael Halcrow i += packet_size_len; 336dddfa461SMichael Halcrow memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); 337dddfa461SMichael Halcrow i += ECRYPTFS_SIG_SIZE_HEX; 338dddfa461SMichael Halcrow /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ 339f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, 340dddfa461SMichael Halcrow &packet_size_len); 341dddfa461SMichael Halcrow if (rc) { 342dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " 343dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 344dddfa461SMichael Halcrow goto out; 345dddfa461SMichael Halcrow } 346dddfa461SMichael Halcrow i += packet_size_len; 347dddfa461SMichael Halcrow message[i++] = cipher_code; 348dddfa461SMichael Halcrow memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); 349dddfa461SMichael Halcrow i += crypt_stat->key_size; 350dddfa461SMichael Halcrow for (j = 0; j < crypt_stat->key_size; j++) 351dddfa461SMichael Halcrow checksum += crypt_stat->key[j]; 352dddfa461SMichael Halcrow message[i++] = (checksum / 256) % 256; 353dddfa461SMichael Halcrow message[i++] = (checksum % 256); 354dddfa461SMichael Halcrow *packet_len = i; 355dddfa461SMichael Halcrow out: 356dddfa461SMichael Halcrow return rc; 357dddfa461SMichael Halcrow } 358dddfa461SMichael Halcrow 359dddfa461SMichael Halcrow static int 360dddfa461SMichael Halcrow parse_tag_67_packet(struct ecryptfs_key_record *key_rec, 361dddfa461SMichael Halcrow struct ecryptfs_message *msg) 362dddfa461SMichael Halcrow { 363dddfa461SMichael Halcrow size_t i = 0; 364dddfa461SMichael Halcrow char *data; 365dddfa461SMichael Halcrow size_t data_len; 366dddfa461SMichael Halcrow size_t message_len; 367dddfa461SMichael Halcrow int rc; 368dddfa461SMichael Halcrow 369dddfa461SMichael Halcrow /* 370dddfa461SMichael Halcrow * ***** TAG 65 Packet Format ***** 371dddfa461SMichael Halcrow * | Content Type | 1 byte | 372dddfa461SMichael Halcrow * | Status Indicator | 1 byte | 373dddfa461SMichael Halcrow * | Encrypted File Encryption Key Size | 1 or 2 bytes | 374dddfa461SMichael Halcrow * | Encrypted File Encryption Key | arbitrary | 375dddfa461SMichael Halcrow */ 376dddfa461SMichael Halcrow message_len = msg->data_len; 377dddfa461SMichael Halcrow data = msg->data; 378dddfa461SMichael Halcrow /* verify that everything through the encrypted FEK size is present */ 379dddfa461SMichael Halcrow if (message_len < 4) { 380dddfa461SMichael Halcrow rc = -EIO; 381df261c52SMichael Halcrow printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable " 382f66e883eSMichael Halcrow "message length is [%d]\n", __func__, message_len, 4); 383dddfa461SMichael Halcrow goto out; 384dddfa461SMichael Halcrow } 385dddfa461SMichael Halcrow if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { 386dddfa461SMichael Halcrow rc = -EIO; 387f66e883eSMichael Halcrow printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", 388f66e883eSMichael Halcrow __func__); 389dddfa461SMichael Halcrow goto out; 390dddfa461SMichael Halcrow } 391dddfa461SMichael Halcrow if (data[i++]) { 392dddfa461SMichael Halcrow rc = -EIO; 393f66e883eSMichael Halcrow printk(KERN_ERR "%s: Status indicator has non zero " 394f66e883eSMichael Halcrow "value [%d]\n", __func__, data[i-1]); 395f66e883eSMichael Halcrow 396dddfa461SMichael Halcrow goto out; 397dddfa461SMichael Halcrow } 398f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, 399f66e883eSMichael Halcrow &data_len); 400dddfa461SMichael Halcrow if (rc) { 401dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " 402dddfa461SMichael Halcrow "rc = [%d]\n", rc); 403dddfa461SMichael Halcrow goto out; 404dddfa461SMichael Halcrow } 405dddfa461SMichael Halcrow i += data_len; 406dddfa461SMichael Halcrow if (message_len < (i + key_rec->enc_key_size)) { 407dddfa461SMichael Halcrow rc = -EIO; 408df261c52SMichael Halcrow printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n", 409f66e883eSMichael Halcrow __func__, message_len, (i + key_rec->enc_key_size)); 410dddfa461SMichael Halcrow goto out; 411dddfa461SMichael Halcrow } 412dddfa461SMichael Halcrow if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 413f66e883eSMichael Halcrow rc = -EIO; 414df261c52SMichael Halcrow printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than " 415f66e883eSMichael Halcrow "the maximum key size [%d]\n", __func__, 416dddfa461SMichael Halcrow key_rec->enc_key_size, 417dddfa461SMichael Halcrow ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); 418dddfa461SMichael Halcrow goto out; 419dddfa461SMichael Halcrow } 420dddfa461SMichael Halcrow memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); 421dddfa461SMichael Halcrow out: 422dddfa461SMichael Halcrow return rc; 423dddfa461SMichael Halcrow } 424dddfa461SMichael Halcrow 4250e1fc5efSRoberto Sassu /** 4260e1fc5efSRoberto Sassu * ecryptfs_verify_version 4270e1fc5efSRoberto Sassu * @version: The version number to confirm 4280e1fc5efSRoberto Sassu * 4290e1fc5efSRoberto Sassu * Returns zero on good version; non-zero otherwise 4300e1fc5efSRoberto Sassu */ 4310e1fc5efSRoberto Sassu static int ecryptfs_verify_version(u16 version) 4320e1fc5efSRoberto Sassu { 4330e1fc5efSRoberto Sassu int rc = 0; 4340e1fc5efSRoberto Sassu unsigned char major; 4350e1fc5efSRoberto Sassu unsigned char minor; 4360e1fc5efSRoberto Sassu 4370e1fc5efSRoberto Sassu major = ((version >> 8) & 0xFF); 4380e1fc5efSRoberto Sassu minor = (version & 0xFF); 4390e1fc5efSRoberto Sassu if (major != ECRYPTFS_VERSION_MAJOR) { 4400e1fc5efSRoberto Sassu ecryptfs_printk(KERN_ERR, "Major version number mismatch. " 4410e1fc5efSRoberto Sassu "Expected [%d]; got [%d]\n", 4420e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MAJOR, major); 4430e1fc5efSRoberto Sassu rc = -EINVAL; 4440e1fc5efSRoberto Sassu goto out; 4450e1fc5efSRoberto Sassu } 4460e1fc5efSRoberto Sassu if (minor != ECRYPTFS_VERSION_MINOR) { 4470e1fc5efSRoberto Sassu ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " 4480e1fc5efSRoberto Sassu "Expected [%d]; got [%d]\n", 4490e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MINOR, minor); 4500e1fc5efSRoberto Sassu rc = -EINVAL; 4510e1fc5efSRoberto Sassu goto out; 4520e1fc5efSRoberto Sassu } 4530e1fc5efSRoberto Sassu out: 4540e1fc5efSRoberto Sassu return rc; 4550e1fc5efSRoberto Sassu } 4560e1fc5efSRoberto Sassu 4570e1fc5efSRoberto Sassu /** 4580e1fc5efSRoberto Sassu * ecryptfs_verify_auth_tok_from_key 4590e1fc5efSRoberto Sassu * @auth_tok_key: key containing the authentication token 4600e1fc5efSRoberto Sassu * @auth_tok: authentication token 4610e1fc5efSRoberto Sassu * 4620e1fc5efSRoberto Sassu * Returns zero on valid auth tok; -EINVAL otherwise 4630e1fc5efSRoberto Sassu */ 4640e1fc5efSRoberto Sassu static int 4650e1fc5efSRoberto Sassu ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key, 4660e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok) 4670e1fc5efSRoberto Sassu { 4680e1fc5efSRoberto Sassu int rc = 0; 4690e1fc5efSRoberto Sassu 4700e1fc5efSRoberto Sassu (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key); 4710e1fc5efSRoberto Sassu if (ecryptfs_verify_version((*auth_tok)->version)) { 4720e1fc5efSRoberto Sassu printk(KERN_ERR "Data structure version mismatch. Userspace " 4730e1fc5efSRoberto Sassu "tools must match eCryptfs kernel module with major " 4740e1fc5efSRoberto Sassu "version [%d] and minor version [%d]\n", 4750e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); 4760e1fc5efSRoberto Sassu rc = -EINVAL; 4770e1fc5efSRoberto Sassu goto out; 4780e1fc5efSRoberto Sassu } 4790e1fc5efSRoberto Sassu if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 4800e1fc5efSRoberto Sassu && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 4810e1fc5efSRoberto Sassu printk(KERN_ERR "Invalid auth_tok structure " 4820e1fc5efSRoberto Sassu "returned from key query\n"); 4830e1fc5efSRoberto Sassu rc = -EINVAL; 4840e1fc5efSRoberto Sassu goto out; 4850e1fc5efSRoberto Sassu } 4860e1fc5efSRoberto Sassu out: 4870e1fc5efSRoberto Sassu return rc; 4880e1fc5efSRoberto Sassu } 4890e1fc5efSRoberto Sassu 490cd9d67dfSMichael Halcrow static int 4919c79f34fSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig( 4920e1fc5efSRoberto Sassu struct key **auth_tok_key, 4930e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok, 4949c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 4959c79f34fSMichael Halcrow { 4969c79f34fSMichael Halcrow struct ecryptfs_global_auth_tok *walker; 4979c79f34fSMichael Halcrow int rc = 0; 4989c79f34fSMichael Halcrow 4990e1fc5efSRoberto Sassu (*auth_tok_key) = NULL; 5000e1fc5efSRoberto Sassu (*auth_tok) = NULL; 5019c79f34fSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 5029c79f34fSMichael Halcrow list_for_each_entry(walker, 5039c79f34fSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 5049c79f34fSMichael Halcrow mount_crypt_stat_list) { 5050e1fc5efSRoberto Sassu if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX)) 5060e1fc5efSRoberto Sassu continue; 5070e1fc5efSRoberto Sassu 5080e1fc5efSRoberto Sassu if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) { 5090e1fc5efSRoberto Sassu rc = -EINVAL; 5109c79f34fSMichael Halcrow goto out; 5119c79f34fSMichael Halcrow } 5120e1fc5efSRoberto Sassu 5130e1fc5efSRoberto Sassu rc = key_validate(walker->global_auth_tok_key); 5140e1fc5efSRoberto Sassu if (rc) { 5150e1fc5efSRoberto Sassu if (rc == -EKEYEXPIRED) 5160e1fc5efSRoberto Sassu goto out; 5170e1fc5efSRoberto Sassu goto out_invalid_auth_tok; 5189c79f34fSMichael Halcrow } 5190e1fc5efSRoberto Sassu 520b5695d04SRoberto Sassu down_write(&(walker->global_auth_tok_key->sem)); 5210e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key( 5220e1fc5efSRoberto Sassu walker->global_auth_tok_key, auth_tok); 5230e1fc5efSRoberto Sassu if (rc) 524b5695d04SRoberto Sassu goto out_invalid_auth_tok_unlock; 5250e1fc5efSRoberto Sassu 5260e1fc5efSRoberto Sassu (*auth_tok_key) = walker->global_auth_tok_key; 5270e1fc5efSRoberto Sassu key_get(*auth_tok_key); 5280e1fc5efSRoberto Sassu goto out; 5290e1fc5efSRoberto Sassu } 5300e1fc5efSRoberto Sassu rc = -ENOENT; 5310e1fc5efSRoberto Sassu goto out; 532b5695d04SRoberto Sassu out_invalid_auth_tok_unlock: 533b5695d04SRoberto Sassu up_write(&(walker->global_auth_tok_key->sem)); 5340e1fc5efSRoberto Sassu out_invalid_auth_tok: 5350e1fc5efSRoberto Sassu printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig); 5360e1fc5efSRoberto Sassu walker->flags |= ECRYPTFS_AUTH_TOK_INVALID; 5370e1fc5efSRoberto Sassu key_put(walker->global_auth_tok_key); 5380e1fc5efSRoberto Sassu walker->global_auth_tok_key = NULL; 5399c79f34fSMichael Halcrow out: 5409c79f34fSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 5419c79f34fSMichael Halcrow return rc; 5429c79f34fSMichael Halcrow } 5439c79f34fSMichael Halcrow 5449c79f34fSMichael Halcrow /** 5459c79f34fSMichael Halcrow * ecryptfs_find_auth_tok_for_sig 5469c79f34fSMichael Halcrow * @auth_tok: Set to the matching auth_tok; NULL if not found 5479c79f34fSMichael Halcrow * @crypt_stat: inode crypt_stat crypto context 5489c79f34fSMichael Halcrow * @sig: Sig of auth_tok to find 5499c79f34fSMichael Halcrow * 5509c79f34fSMichael Halcrow * For now, this function simply looks at the registered auth_tok's 5519c79f34fSMichael Halcrow * linked off the mount_crypt_stat, so all the auth_toks that can be 5529c79f34fSMichael Halcrow * used must be registered at mount time. This function could 5539c79f34fSMichael Halcrow * potentially try a lot harder to find auth_tok's (e.g., by calling 5549c79f34fSMichael Halcrow * out to ecryptfsd to dynamically retrieve an auth_tok object) so 5559c79f34fSMichael Halcrow * that static registration of auth_tok's will no longer be necessary. 5569c79f34fSMichael Halcrow * 5579c79f34fSMichael Halcrow * Returns zero on no error; non-zero on error 5589c79f34fSMichael Halcrow */ 5599c79f34fSMichael Halcrow static int 5609c79f34fSMichael Halcrow ecryptfs_find_auth_tok_for_sig( 561aee683b9SRoberto Sassu struct key **auth_tok_key, 5629c79f34fSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 5639c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 5649c79f34fSMichael Halcrow char *sig) 5659c79f34fSMichael Halcrow { 5669c79f34fSMichael Halcrow int rc = 0; 5679c79f34fSMichael Halcrow 5680e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok, 5690e1fc5efSRoberto Sassu mount_crypt_stat, sig); 5700e1fc5efSRoberto Sassu if (rc == -ENOENT) { 571f16feb51SRoberto Sassu /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the 572f16feb51SRoberto Sassu * mount_crypt_stat structure, we prevent to use auth toks that 573f16feb51SRoberto Sassu * are not inserted through the ecryptfs_add_global_auth_tok 574f16feb51SRoberto Sassu * function. 575f16feb51SRoberto Sassu */ 576f16feb51SRoberto Sassu if (mount_crypt_stat->flags 577f16feb51SRoberto Sassu & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 578f16feb51SRoberto Sassu return -EINVAL; 579f16feb51SRoberto Sassu 580aee683b9SRoberto Sassu rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, 5819c79f34fSMichael Halcrow sig); 5820e1fc5efSRoberto Sassu } 5839c79f34fSMichael Halcrow return rc; 5849c79f34fSMichael Halcrow } 5859c79f34fSMichael Halcrow 5869c79f34fSMichael Halcrow /** 5879c79f34fSMichael Halcrow * write_tag_70_packet can gobble a lot of stack space. We stuff most 5889c79f34fSMichael Halcrow * of the function's parameters in a kmalloc'd struct to help reduce 5899c79f34fSMichael Halcrow * eCryptfs' overall stack usage. 5909c79f34fSMichael Halcrow */ 5919c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack { 5929c79f34fSMichael Halcrow u8 cipher_code; 5939c79f34fSMichael Halcrow size_t max_packet_size; 5949c79f34fSMichael Halcrow size_t packet_size_len; 5959c79f34fSMichael Halcrow size_t block_aligned_filename_size; 5969c79f34fSMichael Halcrow size_t block_size; 5979c79f34fSMichael Halcrow size_t i; 5989c79f34fSMichael Halcrow size_t j; 5999c79f34fSMichael Halcrow size_t num_rand_bytes; 6009c79f34fSMichael Halcrow struct mutex *tfm_mutex; 6019c79f34fSMichael Halcrow char *block_aligned_filename; 6029c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 6038d08dab7STyler Hicks struct scatterlist src_sg[2]; 6048d08dab7STyler Hicks struct scatterlist dst_sg[2]; 6053095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 6063095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 6079c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 6089c79f34fSMichael Halcrow char hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6099c79f34fSMichael Halcrow char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6103095e8e3SHerbert Xu struct crypto_shash *hash_tfm; 6113095e8e3SHerbert Xu struct shash_desc *hash_desc; 6129c79f34fSMichael Halcrow }; 6139c79f34fSMichael Halcrow 6149c79f34fSMichael Halcrow /** 6159c79f34fSMichael Halcrow * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK 6169c79f34fSMichael Halcrow * @filename: NULL-terminated filename string 6179c79f34fSMichael Halcrow * 6189c79f34fSMichael Halcrow * This is the simplest mechanism for achieving filename encryption in 6199c79f34fSMichael Halcrow * eCryptfs. It encrypts the given filename with the mount-wide 6209c79f34fSMichael Halcrow * filename encryption key (FNEK) and stores it in a packet to @dest, 6219c79f34fSMichael Halcrow * which the callee will encode and write directly into the dentry 6229c79f34fSMichael Halcrow * name. 6239c79f34fSMichael Halcrow */ 6249c79f34fSMichael Halcrow int 6259c79f34fSMichael Halcrow ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 6269c79f34fSMichael Halcrow size_t *packet_size, 6279c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 6289c79f34fSMichael Halcrow char *filename, size_t filename_size) 6299c79f34fSMichael Halcrow { 6309c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack *s; 631aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 6329c79f34fSMichael Halcrow int rc = 0; 6339c79f34fSMichael Halcrow 6343095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 6359c79f34fSMichael Halcrow if (!s) { 6369c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 637df261c52SMichael Halcrow "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 638*d1558f4eSHerbert Xu return -ENOMEM; 6399c79f34fSMichael Halcrow } 6409c79f34fSMichael Halcrow (*packet_size) = 0; 641950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig( 642950983fcSRoberto Sassu &auth_tok_key, 643950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 644950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig); 645950983fcSRoberto Sassu if (rc) { 646950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 647950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, 648950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig, rc); 649950983fcSRoberto Sassu goto out; 650950983fcSRoberto Sassu } 6519c79f34fSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( 6523095e8e3SHerbert Xu &s->skcipher_tfm, 6539c79f34fSMichael Halcrow &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); 6549c79f34fSMichael Halcrow if (unlikely(rc)) { 6559c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 6569c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 6579c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, rc); 6589c79f34fSMichael Halcrow goto out; 6599c79f34fSMichael Halcrow } 6609c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 6613095e8e3SHerbert Xu s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm); 6629c79f34fSMichael Halcrow /* Plus one for the \0 separator between the random prefix 6639c79f34fSMichael Halcrow * and the plaintext filename */ 6649c79f34fSMichael Halcrow s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); 6659c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); 6669c79f34fSMichael Halcrow if ((s->block_aligned_filename_size % s->block_size) != 0) { 6679c79f34fSMichael Halcrow s->num_rand_bytes += (s->block_size 6689c79f34fSMichael Halcrow - (s->block_aligned_filename_size 6699c79f34fSMichael Halcrow % s->block_size)); 6709c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes 6719c79f34fSMichael Halcrow + filename_size); 6729c79f34fSMichael Halcrow } 6739c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 6749c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 6759c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 6769c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 6779c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 6789c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 6799c79f34fSMichael Halcrow * - Consists of a minimum number of random characters, a \0 6809c79f34fSMichael Halcrow * separator, and then the filename */ 6814a26620dSTyler Hicks s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE 6829c79f34fSMichael Halcrow + s->block_aligned_filename_size); 6839c79f34fSMichael Halcrow if (dest == NULL) { 6849c79f34fSMichael Halcrow (*packet_size) = s->max_packet_size; 6859c79f34fSMichael Halcrow goto out_unlock; 6869c79f34fSMichael Halcrow } 6879c79f34fSMichael Halcrow if (s->max_packet_size > (*remaining_bytes)) { 688a8f12864SMichael Halcrow printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " 689a8f12864SMichael Halcrow "[%zd] available\n", __func__, s->max_packet_size, 6909c79f34fSMichael Halcrow (*remaining_bytes)); 6919c79f34fSMichael Halcrow rc = -EINVAL; 6929c79f34fSMichael Halcrow goto out_unlock; 6939c79f34fSMichael Halcrow } 6943095e8e3SHerbert Xu 6953095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 6963095e8e3SHerbert Xu if (!s->skcipher_req) { 6973095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 6983095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 6993095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 7003095e8e3SHerbert Xu rc = -ENOMEM; 7013095e8e3SHerbert Xu goto out_unlock; 7023095e8e3SHerbert Xu } 7033095e8e3SHerbert Xu 7043095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 7053095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 7063095e8e3SHerbert Xu 7079c79f34fSMichael Halcrow s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, 7089c79f34fSMichael Halcrow GFP_KERNEL); 7099c79f34fSMichael Halcrow if (!s->block_aligned_filename) { 7109c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 711df261c52SMichael Halcrow "kzalloc [%zd] bytes\n", __func__, 7129c79f34fSMichael Halcrow s->block_aligned_filename_size); 7139c79f34fSMichael Halcrow rc = -ENOMEM; 7149c79f34fSMichael Halcrow goto out_unlock; 7159c79f34fSMichael Halcrow } 7169c79f34fSMichael Halcrow dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; 7179c79f34fSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[s->i], 7189c79f34fSMichael Halcrow (ECRYPTFS_SIG_SIZE 7199c79f34fSMichael Halcrow + 1 /* Cipher code */ 7209c79f34fSMichael Halcrow + s->block_aligned_filename_size), 7219c79f34fSMichael Halcrow &s->packet_size_len); 7229c79f34fSMichael Halcrow if (rc) { 7239c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error generating tag 70 packet " 7249c79f34fSMichael Halcrow "header; cannot generate packet length; rc = [%d]\n", 7259c79f34fSMichael Halcrow __func__, rc); 7269c79f34fSMichael Halcrow goto out_free_unlock; 7279c79f34fSMichael Halcrow } 7289c79f34fSMichael Halcrow s->i += s->packet_size_len; 7299c79f34fSMichael Halcrow ecryptfs_from_hex(&dest[s->i], 7309c79f34fSMichael Halcrow mount_crypt_stat->global_default_fnek_sig, 7319c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 7329c79f34fSMichael Halcrow s->i += ECRYPTFS_SIG_SIZE; 7339c79f34fSMichael Halcrow s->cipher_code = ecryptfs_code_for_cipher_string( 7349c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7359c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7369c79f34fSMichael Halcrow if (s->cipher_code == 0) { 7379c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Unable to generate code for " 738a8f12864SMichael Halcrow "cipher [%s] with key bytes [%zd]\n", __func__, 7399c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7409c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7419c79f34fSMichael Halcrow rc = -EINVAL; 7429c79f34fSMichael Halcrow goto out_free_unlock; 7439c79f34fSMichael Halcrow } 7449c79f34fSMichael Halcrow dest[s->i++] = s->cipher_code; 7459c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 7469c79f34fSMichael Halcrow * filename encryption */ 747df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 748df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 749df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 750df6ad33bSTyler Hicks "password tokens\n", __func__); 751df6ad33bSTyler Hicks goto out_free_unlock; 752df6ad33bSTyler Hicks } 7533095e8e3SHerbert Xu s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0); 7543095e8e3SHerbert Xu if (IS_ERR(s->hash_tfm)) { 7553095e8e3SHerbert Xu rc = PTR_ERR(s->hash_tfm); 7569c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to " 7579c79f34fSMichael Halcrow "allocate hash crypto context; rc = [%d]\n", 7589c79f34fSMichael Halcrow __func__, rc); 7599c79f34fSMichael Halcrow goto out_free_unlock; 7609c79f34fSMichael Halcrow } 7613095e8e3SHerbert Xu 7623095e8e3SHerbert Xu s->hash_desc = kmalloc(sizeof(*s->hash_desc) + 7633095e8e3SHerbert Xu crypto_shash_descsize(s->hash_tfm), GFP_KERNEL); 7643095e8e3SHerbert Xu if (!s->hash_desc) { 7653095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 7663095e8e3SHerbert Xu "kmalloc [%zd] bytes\n", __func__, 7673095e8e3SHerbert Xu sizeof(*s->hash_desc) + 7683095e8e3SHerbert Xu crypto_shash_descsize(s->hash_tfm)); 7693095e8e3SHerbert Xu rc = -ENOMEM; 7709c79f34fSMichael Halcrow goto out_release_free_unlock; 7719c79f34fSMichael Halcrow } 7723095e8e3SHerbert Xu 7733095e8e3SHerbert Xu s->hash_desc->tfm = s->hash_tfm; 7743095e8e3SHerbert Xu s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 7753095e8e3SHerbert Xu 7763095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, 7773095e8e3SHerbert Xu (u8 *)s->auth_tok->token.password.session_key_encryption_key, 7783095e8e3SHerbert Xu s->auth_tok->token.password.session_key_encryption_key_bytes, 7793095e8e3SHerbert Xu s->hash); 7809c79f34fSMichael Halcrow if (rc) { 7819c79f34fSMichael Halcrow printk(KERN_ERR 7823095e8e3SHerbert Xu "%s: Error computing crypto hash; rc = [%d]\n", 7839c79f34fSMichael Halcrow __func__, rc); 7849c79f34fSMichael Halcrow goto out_release_free_unlock; 7859c79f34fSMichael Halcrow } 7869c79f34fSMichael Halcrow for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { 7879c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = 7889c79f34fSMichael Halcrow s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)]; 7899c79f34fSMichael Halcrow if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE) 7909c79f34fSMichael Halcrow == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) { 7913095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash, 7923095e8e3SHerbert Xu ECRYPTFS_TAG_70_DIGEST_SIZE, 7933095e8e3SHerbert Xu s->tmp_hash); 7949c79f34fSMichael Halcrow if (rc) { 7959c79f34fSMichael Halcrow printk(KERN_ERR 7963095e8e3SHerbert Xu "%s: Error computing crypto hash; " 7979c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 7989c79f34fSMichael Halcrow goto out_release_free_unlock; 7999c79f34fSMichael Halcrow } 8009c79f34fSMichael Halcrow memcpy(s->hash, s->tmp_hash, 8019c79f34fSMichael Halcrow ECRYPTFS_TAG_70_DIGEST_SIZE); 8029c79f34fSMichael Halcrow } 8039c79f34fSMichael Halcrow if (s->block_aligned_filename[s->j] == '\0') 8049c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; 8059c79f34fSMichael Halcrow } 8069c79f34fSMichael Halcrow memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, 8079c79f34fSMichael Halcrow filename_size); 8089c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->block_aligned_filename, 8098d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 8108d08dab7STyler Hicks if (rc < 1) { 8119c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8128d08dab7STyler Hicks "convert filename memory to scatterlist; rc = [%d]. " 813a8f12864SMichael Halcrow "block_aligned_filename_size = [%zd]\n", __func__, rc, 8149c79f34fSMichael Halcrow s->block_aligned_filename_size); 8159c79f34fSMichael Halcrow goto out_release_free_unlock; 8169c79f34fSMichael Halcrow } 8179c79f34fSMichael Halcrow rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, 8188d08dab7STyler Hicks s->dst_sg, 2); 8198d08dab7STyler Hicks if (rc < 1) { 8209c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8219c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 8228d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 8238d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 8249c79f34fSMichael Halcrow goto out_release_free_unlock; 8259c79f34fSMichael Halcrow } 8269c79f34fSMichael Halcrow /* The characters in the first block effectively do the job 8279c79f34fSMichael Halcrow * of the IV here, so we just use 0's for the IV. Note the 8289c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 8299c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 8303095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 8313095e8e3SHerbert Xu s->skcipher_tfm, 8329c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8339c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8349c79f34fSMichael Halcrow if (rc < 0) { 8359c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 8369c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 8379c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 838df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 8399c79f34fSMichael Halcrow rc, 8409c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8419c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8429c79f34fSMichael Halcrow goto out_release_free_unlock; 8439c79f34fSMichael Halcrow } 8443095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 8453095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 8463095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(s->skcipher_req); 8479c79f34fSMichael Halcrow if (rc) { 8489c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to encrypt filename; " 8499c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 8509c79f34fSMichael Halcrow goto out_release_free_unlock; 8519c79f34fSMichael Halcrow } 8529c79f34fSMichael Halcrow s->i += s->block_aligned_filename_size; 8539c79f34fSMichael Halcrow (*packet_size) = s->i; 8549c79f34fSMichael Halcrow (*remaining_bytes) -= (*packet_size); 8559c79f34fSMichael Halcrow out_release_free_unlock: 8563095e8e3SHerbert Xu crypto_free_shash(s->hash_tfm); 8579c79f34fSMichael Halcrow out_free_unlock: 85800fcf2cbSJohannes Weiner kzfree(s->block_aligned_filename); 8599c79f34fSMichael Halcrow out_unlock: 8609c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 8619c79f34fSMichael Halcrow out: 862b5695d04SRoberto Sassu if (auth_tok_key) { 863b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 864aee683b9SRoberto Sassu key_put(auth_tok_key); 865b5695d04SRoberto Sassu } 8663095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 8673095e8e3SHerbert Xu kzfree(s->hash_desc); 8689c79f34fSMichael Halcrow kfree(s); 8699c79f34fSMichael Halcrow return rc; 8709c79f34fSMichael Halcrow } 8719c79f34fSMichael Halcrow 8729c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack { 8739c79f34fSMichael Halcrow u8 cipher_code; 8749c79f34fSMichael Halcrow size_t max_packet_size; 8759c79f34fSMichael Halcrow size_t packet_size_len; 8769c79f34fSMichael Halcrow size_t parsed_tag_70_packet_size; 8779c79f34fSMichael Halcrow size_t block_aligned_filename_size; 8789c79f34fSMichael Halcrow size_t block_size; 8799c79f34fSMichael Halcrow size_t i; 8809c79f34fSMichael Halcrow struct mutex *tfm_mutex; 8819c79f34fSMichael Halcrow char *decrypted_filename; 8829c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 8838d08dab7STyler Hicks struct scatterlist src_sg[2]; 8848d08dab7STyler Hicks struct scatterlist dst_sg[2]; 8853095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 8863095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 8879c79f34fSMichael Halcrow char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; 8889c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 8892a559a8bSColin Ian King char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 8909c79f34fSMichael Halcrow }; 8919c79f34fSMichael Halcrow 8929c79f34fSMichael Halcrow /** 8939c79f34fSMichael Halcrow * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet 8949c79f34fSMichael Halcrow * @filename: This function kmalloc's the memory for the filename 8957d8bc2beSMichael Halcrow * @filename_size: This function sets this to the amount of memory 8967d8bc2beSMichael Halcrow * kmalloc'd for the filename 8977d8bc2beSMichael Halcrow * @packet_size: This function sets this to the the number of octets 8987d8bc2beSMichael Halcrow * in the packet parsed 8997d8bc2beSMichael Halcrow * @mount_crypt_stat: The mount-wide cryptographic context 9007d8bc2beSMichael Halcrow * @data: The memory location containing the start of the tag 70 9017d8bc2beSMichael Halcrow * packet 9027d8bc2beSMichael Halcrow * @max_packet_size: The maximum legal size of the packet to be parsed 9037d8bc2beSMichael Halcrow * from @data 9047d8bc2beSMichael Halcrow * 9057d8bc2beSMichael Halcrow * Returns zero on success; non-zero otherwise 9069c79f34fSMichael Halcrow */ 9079c79f34fSMichael Halcrow int 9089c79f34fSMichael Halcrow ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 9099c79f34fSMichael Halcrow size_t *packet_size, 9109c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 9119c79f34fSMichael Halcrow char *data, size_t max_packet_size) 9129c79f34fSMichael Halcrow { 9139c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack *s; 914aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 9159c79f34fSMichael Halcrow int rc = 0; 9169c79f34fSMichael Halcrow 9179c79f34fSMichael Halcrow (*packet_size) = 0; 9189c79f34fSMichael Halcrow (*filename_size) = 0; 9199c79f34fSMichael Halcrow (*filename) = NULL; 9203095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 9219c79f34fSMichael Halcrow if (!s) { 9229c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " 923a8f12864SMichael Halcrow "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); 924*d1558f4eSHerbert Xu return -ENOMEM; 9259c79f34fSMichael Halcrow } 9264a26620dSTyler Hicks if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) { 927df261c52SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " 9289c79f34fSMichael Halcrow "at least [%d]\n", __func__, max_packet_size, 9294a26620dSTyler Hicks ECRYPTFS_TAG_70_MIN_METADATA_SIZE); 9309c79f34fSMichael Halcrow rc = -EINVAL; 9319c79f34fSMichael Halcrow goto out; 9329c79f34fSMichael Halcrow } 9339c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 9349c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 9359c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 9369c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 9379c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 9389c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 9399c79f34fSMichael Halcrow * - Consists of a minimum number of random numbers, a \0 9409c79f34fSMichael Halcrow * separator, and then the filename */ 9419c79f34fSMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { 9429c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " 9439c79f34fSMichael Halcrow "tag [0x%.2x]\n", __func__, 9449c79f34fSMichael Halcrow data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); 9459c79f34fSMichael Halcrow rc = -EINVAL; 9469c79f34fSMichael Halcrow goto out; 9479c79f34fSMichael Halcrow } 9489c79f34fSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], 9499c79f34fSMichael Halcrow &s->parsed_tag_70_packet_size, 9509c79f34fSMichael Halcrow &s->packet_size_len); 9519c79f34fSMichael Halcrow if (rc) { 9529c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Error parsing packet length; " 9539c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 9549c79f34fSMichael Halcrow goto out; 9559c79f34fSMichael Halcrow } 9569c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->parsed_tag_70_packet_size 9579c79f34fSMichael Halcrow - ECRYPTFS_SIG_SIZE - 1); 9589c79f34fSMichael Halcrow if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) 9599c79f34fSMichael Halcrow > max_packet_size) { 960a8f12864SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " 961a8f12864SMichael Halcrow "size is [%zd]\n", __func__, max_packet_size, 9629c79f34fSMichael Halcrow (1 + s->packet_size_len + 1 9639c79f34fSMichael Halcrow + s->block_aligned_filename_size)); 9649c79f34fSMichael Halcrow rc = -EINVAL; 9659c79f34fSMichael Halcrow goto out; 9669c79f34fSMichael Halcrow } 9679c79f34fSMichael Halcrow (*packet_size) += s->packet_size_len; 9689c79f34fSMichael Halcrow ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], 9699c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 9709c79f34fSMichael Halcrow s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 9719c79f34fSMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 9729c79f34fSMichael Halcrow s->cipher_code = data[(*packet_size)++]; 9739c79f34fSMichael Halcrow rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code); 9749c79f34fSMichael Halcrow if (rc) { 9759c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", 9769c79f34fSMichael Halcrow __func__, s->cipher_code); 9779c79f34fSMichael Halcrow goto out; 9789c79f34fSMichael Halcrow } 979950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 980950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 981950983fcSRoberto Sassu s->fnek_sig_hex); 982950983fcSRoberto Sassu if (rc) { 983950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 984950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, 985950983fcSRoberto Sassu rc); 986950983fcSRoberto Sassu goto out; 987950983fcSRoberto Sassu } 9883095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm, 9899c79f34fSMichael Halcrow &s->tfm_mutex, 9909c79f34fSMichael Halcrow s->cipher_string); 9919c79f34fSMichael Halcrow if (unlikely(rc)) { 9929c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 9939c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 9949c79f34fSMichael Halcrow s->cipher_string, rc); 9959c79f34fSMichael Halcrow goto out; 9969c79f34fSMichael Halcrow } 9979c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 9989c79f34fSMichael Halcrow rc = virt_to_scatterlist(&data[(*packet_size)], 9998d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 10008d08dab7STyler Hicks if (rc < 1) { 10019c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 10029c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 10038d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10048d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10059c79f34fSMichael Halcrow goto out_unlock; 10069c79f34fSMichael Halcrow } 10079c79f34fSMichael Halcrow (*packet_size) += s->block_aligned_filename_size; 10089c79f34fSMichael Halcrow s->decrypted_filename = kmalloc(s->block_aligned_filename_size, 10099c79f34fSMichael Halcrow GFP_KERNEL); 10109c79f34fSMichael Halcrow if (!s->decrypted_filename) { 10119c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst attempting to " 1012a8f12864SMichael Halcrow "kmalloc [%zd] bytes\n", __func__, 10139c79f34fSMichael Halcrow s->block_aligned_filename_size); 10149c79f34fSMichael Halcrow rc = -ENOMEM; 10159c79f34fSMichael Halcrow goto out_unlock; 10169c79f34fSMichael Halcrow } 10179c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->decrypted_filename, 10188d08dab7STyler Hicks s->block_aligned_filename_size, s->dst_sg, 2); 10198d08dab7STyler Hicks if (rc < 1) { 10209c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 10219c79f34fSMichael Halcrow "convert decrypted filename memory to scatterlist; " 10228d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10238d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10249c79f34fSMichael Halcrow goto out_free_unlock; 10259c79f34fSMichael Halcrow } 10263095e8e3SHerbert Xu 10273095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 10283095e8e3SHerbert Xu if (!s->skcipher_req) { 10293095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 10303095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 10313095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 10323095e8e3SHerbert Xu rc = -ENOMEM; 10333095e8e3SHerbert Xu goto out_free_unlock; 10343095e8e3SHerbert Xu } 10353095e8e3SHerbert Xu 10363095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 10373095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 10383095e8e3SHerbert Xu 10399c79f34fSMichael Halcrow /* The characters in the first block effectively do the job of 10409c79f34fSMichael Halcrow * the IV here, so we just use 0's for the IV. Note the 10419c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 10429c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 10439c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 10449c79f34fSMichael Halcrow * filename encryption */ 1045df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 1046df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 1047df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 1048df6ad33bSTyler Hicks "password tokens\n", __func__); 1049df6ad33bSTyler Hicks goto out_free_unlock; 1050df6ad33bSTyler Hicks } 10513095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 10523095e8e3SHerbert Xu s->skcipher_tfm, 10539c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10549c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10559c79f34fSMichael Halcrow if (rc < 0) { 10569c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 10579c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 10589c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 1059df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 10609c79f34fSMichael Halcrow rc, 10619c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10629c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10639c79f34fSMichael Halcrow goto out_free_unlock; 10649c79f34fSMichael Halcrow } 10653095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 10663095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 10673095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(s->skcipher_req); 10689c79f34fSMichael Halcrow if (rc) { 10699c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to decrypt filename; " 10709c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 10719c79f34fSMichael Halcrow goto out_free_unlock; 10729c79f34fSMichael Halcrow } 10739c79f34fSMichael Halcrow while (s->decrypted_filename[s->i] != '\0' 10749c79f34fSMichael Halcrow && s->i < s->block_aligned_filename_size) 10759c79f34fSMichael Halcrow s->i++; 10769c79f34fSMichael Halcrow if (s->i == s->block_aligned_filename_size) { 10779c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " 10789c79f34fSMichael Halcrow "find valid separator between random characters and " 10799c79f34fSMichael Halcrow "the filename\n", __func__); 10809c79f34fSMichael Halcrow rc = -EINVAL; 10819c79f34fSMichael Halcrow goto out_free_unlock; 10829c79f34fSMichael Halcrow } 10839c79f34fSMichael Halcrow s->i++; 10849c79f34fSMichael Halcrow (*filename_size) = (s->block_aligned_filename_size - s->i); 10859c79f34fSMichael Halcrow if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { 1086df261c52SMichael Halcrow printk(KERN_WARNING "%s: Filename size is [%zd], which is " 10879c79f34fSMichael Halcrow "invalid\n", __func__, (*filename_size)); 10889c79f34fSMichael Halcrow rc = -EINVAL; 10899c79f34fSMichael Halcrow goto out_free_unlock; 10909c79f34fSMichael Halcrow } 10919c79f34fSMichael Halcrow (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); 10929c79f34fSMichael Halcrow if (!(*filename)) { 10939c79f34fSMichael Halcrow printk(KERN_ERR "%s: Out of memory whilst attempting to " 1094a8f12864SMichael Halcrow "kmalloc [%zd] bytes\n", __func__, 10959c79f34fSMichael Halcrow ((*filename_size) + 1)); 10969c79f34fSMichael Halcrow rc = -ENOMEM; 10979c79f34fSMichael Halcrow goto out_free_unlock; 10989c79f34fSMichael Halcrow } 10999c79f34fSMichael Halcrow memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); 11009c79f34fSMichael Halcrow (*filename)[(*filename_size)] = '\0'; 11019c79f34fSMichael Halcrow out_free_unlock: 11029c79f34fSMichael Halcrow kfree(s->decrypted_filename); 11039c79f34fSMichael Halcrow out_unlock: 11049c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 11059c79f34fSMichael Halcrow out: 11069c79f34fSMichael Halcrow if (rc) { 11079c79f34fSMichael Halcrow (*packet_size) = 0; 11089c79f34fSMichael Halcrow (*filename_size) = 0; 11099c79f34fSMichael Halcrow (*filename) = NULL; 11109c79f34fSMichael Halcrow } 1111b5695d04SRoberto Sassu if (auth_tok_key) { 1112b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 1113aee683b9SRoberto Sassu key_put(auth_tok_key); 1114b5695d04SRoberto Sassu } 11153095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 11169c79f34fSMichael Halcrow kfree(s); 11179c79f34fSMichael Halcrow return rc; 11189c79f34fSMichael Halcrow } 11199c79f34fSMichael Halcrow 11209c79f34fSMichael Halcrow static int 1121cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1122cd9d67dfSMichael Halcrow { 1123cd9d67dfSMichael Halcrow int rc = 0; 1124cd9d67dfSMichael Halcrow 1125cd9d67dfSMichael Halcrow (*sig) = NULL; 1126cd9d67dfSMichael Halcrow switch (auth_tok->token_type) { 1127cd9d67dfSMichael Halcrow case ECRYPTFS_PASSWORD: 1128cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.password.signature; 1129cd9d67dfSMichael Halcrow break; 1130cd9d67dfSMichael Halcrow case ECRYPTFS_PRIVATE_KEY: 1131cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.private_key.signature; 1132cd9d67dfSMichael Halcrow break; 1133cd9d67dfSMichael Halcrow default: 1134cd9d67dfSMichael Halcrow printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1135cd9d67dfSMichael Halcrow auth_tok->token_type); 1136cd9d67dfSMichael Halcrow rc = -EINVAL; 1137cd9d67dfSMichael Halcrow } 1138cd9d67dfSMichael Halcrow return rc; 1139cd9d67dfSMichael Halcrow } 1140cd9d67dfSMichael Halcrow 1141dddfa461SMichael Halcrow /** 114222e78fafSMichael Halcrow * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. 114322e78fafSMichael Halcrow * @auth_tok: The key authentication token used to decrypt the session key 114422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1145dddfa461SMichael Halcrow * 114622e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise. 1147dddfa461SMichael Halcrow */ 1148f4aad16aSMichael Halcrow static int 1149f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1150dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1151dddfa461SMichael Halcrow { 115219e66a67STrevor Highland u8 cipher_code = 0; 1153dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx; 1154dddfa461SMichael Halcrow struct ecryptfs_message *msg = NULL; 1155f4aad16aSMichael Halcrow char *auth_tok_sig; 11563edc8376SGeyslan G. Bem char *payload = NULL; 1157fa519964SSimon Que size_t payload_len = 0; 1158dddfa461SMichael Halcrow int rc; 1159dddfa461SMichael Halcrow 11605dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); 11615dda6992SMichael Halcrow if (rc) { 1162f4aad16aSMichael Halcrow printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 1163f4aad16aSMichael Halcrow auth_tok->token_type); 1164f4aad16aSMichael Halcrow goto out; 1165f4aad16aSMichael Halcrow } 1166f4aad16aSMichael Halcrow rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 1167624ae528STyler Hicks &payload, &payload_len); 1168dddfa461SMichael Halcrow if (rc) { 1169f66e883eSMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); 1170dddfa461SMichael Halcrow goto out; 1171dddfa461SMichael Halcrow } 1172624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1173dddfa461SMichael Halcrow if (rc) { 1174624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 1175290502beSKees Cook "ecryptfsd: %d\n", rc); 1176dddfa461SMichael Halcrow goto out; 1177dddfa461SMichael Halcrow } 1178dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1179dddfa461SMichael Halcrow if (rc) { 1180dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 1181dddfa461SMichael Halcrow "from the user space daemon\n"); 1182dddfa461SMichael Halcrow rc = -EIO; 1183dddfa461SMichael Halcrow goto out; 1184dddfa461SMichael Halcrow } 1185dddfa461SMichael Halcrow rc = parse_tag_65_packet(&(auth_tok->session_key), 1186dddfa461SMichael Halcrow &cipher_code, msg); 1187dddfa461SMichael Halcrow if (rc) { 1188dddfa461SMichael Halcrow printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 1189dddfa461SMichael Halcrow rc); 1190dddfa461SMichael Halcrow goto out; 1191dddfa461SMichael Halcrow } 1192dddfa461SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1193dddfa461SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1194dddfa461SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1195dddfa461SMichael Halcrow crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 1196dddfa461SMichael Halcrow rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); 1197dddfa461SMichael Halcrow if (rc) { 1198dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 1199dddfa461SMichael Halcrow cipher_code) 1200dddfa461SMichael Halcrow goto out; 1201dddfa461SMichael Halcrow } 1202dddfa461SMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1203dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 1204dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 1205dddfa461SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1206dddfa461SMichael Halcrow crypt_stat->key_size); 1207dddfa461SMichael Halcrow } 1208dddfa461SMichael Halcrow out: 1209dddfa461SMichael Halcrow kfree(msg); 12103edc8376SGeyslan G. Bem kfree(payload); 1211dddfa461SMichael Halcrow return rc; 1212dddfa461SMichael Halcrow } 1213dddfa461SMichael Halcrow 1214dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 1215dddfa461SMichael Halcrow { 1216dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1217e0869cc1SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1218dddfa461SMichael Halcrow 1219e0869cc1SMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 1220e0869cc1SMichael Halcrow auth_tok_list_head, list) { 1221e0869cc1SMichael Halcrow list_del(&auth_tok_list_item->list); 1222dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1223dddfa461SMichael Halcrow auth_tok_list_item); 1224dddfa461SMichael Halcrow } 1225dddfa461SMichael Halcrow } 1226dddfa461SMichael Halcrow 1227dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 1228dddfa461SMichael Halcrow 1229dddfa461SMichael Halcrow /** 1230dddfa461SMichael Halcrow * parse_tag_1_packet 123122e78fafSMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet contents 1232dddfa461SMichael Halcrow * @data: The raw bytes of the packet. 1233dddfa461SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 123422e78fafSMichael Halcrow * a new authentication token will be placed at the 123522e78fafSMichael Halcrow * end of this list for this packet. 1236dddfa461SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1237dddfa461SMichael Halcrow * allocates; sets the memory address of the pointer to 1238dddfa461SMichael Halcrow * NULL on error. This object is added to the 1239dddfa461SMichael Halcrow * auth_tok_list. 1240dddfa461SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1241dddfa461SMichael Halcrow * into this memory location; zero on error. 124222e78fafSMichael Halcrow * @max_packet_size: The maximum allowable packet size 1243dddfa461SMichael Halcrow * 1244dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 1245dddfa461SMichael Halcrow */ 1246dddfa461SMichael Halcrow static int 1247dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 1248dddfa461SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1249dddfa461SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1250dddfa461SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1251dddfa461SMichael Halcrow { 1252dddfa461SMichael Halcrow size_t body_size; 1253dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1254dddfa461SMichael Halcrow size_t length_size; 1255dddfa461SMichael Halcrow int rc = 0; 1256dddfa461SMichael Halcrow 1257dddfa461SMichael Halcrow (*packet_size) = 0; 1258dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 125913218179SMichael Halcrow /** 126013218179SMichael Halcrow * This format is inspired by OpenPGP; see RFC 2440 126113218179SMichael Halcrow * packet tag 1 126213218179SMichael Halcrow * 126313218179SMichael Halcrow * Tag 1 identifier (1 byte) 126413218179SMichael Halcrow * Max Tag 1 packet size (max 3 bytes) 126513218179SMichael Halcrow * Version (1 byte) 126613218179SMichael Halcrow * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 126713218179SMichael Halcrow * Cipher identifier (1 byte) 126813218179SMichael Halcrow * Encrypted key size (arbitrary) 126913218179SMichael Halcrow * 127013218179SMichael Halcrow * 12 bytes minimum packet size 1271dddfa461SMichael Halcrow */ 127213218179SMichael Halcrow if (unlikely(max_packet_size < 12)) { 127313218179SMichael Halcrow printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 1274dddfa461SMichael Halcrow rc = -EINVAL; 1275dddfa461SMichael Halcrow goto out; 1276dddfa461SMichael Halcrow } 1277dddfa461SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 127813218179SMichael Halcrow printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 1279dddfa461SMichael Halcrow ECRYPTFS_TAG_1_PACKET_TYPE); 1280dddfa461SMichael Halcrow rc = -EINVAL; 1281dddfa461SMichael Halcrow goto out; 1282dddfa461SMichael Halcrow } 1283dddfa461SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1284dddfa461SMichael Halcrow * at end of function upon failure */ 1285dddfa461SMichael Halcrow auth_tok_list_item = 128613218179SMichael Halcrow kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 1287dddfa461SMichael Halcrow GFP_KERNEL); 1288dddfa461SMichael Halcrow if (!auth_tok_list_item) { 128913218179SMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1290dddfa461SMichael Halcrow rc = -ENOMEM; 1291dddfa461SMichael Halcrow goto out; 1292dddfa461SMichael Halcrow } 1293dddfa461SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1294f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 12955dda6992SMichael Halcrow &length_size); 12965dda6992SMichael Halcrow if (rc) { 129713218179SMichael Halcrow printk(KERN_WARNING "Error parsing packet length; " 1298dddfa461SMichael Halcrow "rc = [%d]\n", rc); 1299dddfa461SMichael Halcrow goto out_free; 1300dddfa461SMichael Halcrow } 130113218179SMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 130281acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1303dddfa461SMichael Halcrow rc = -EINVAL; 1304dddfa461SMichael Halcrow goto out_free; 1305dddfa461SMichael Halcrow } 1306dddfa461SMichael Halcrow (*packet_size) += length_size; 1307dddfa461SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 130813218179SMichael Halcrow printk(KERN_WARNING "Packet size exceeds max\n"); 1309dddfa461SMichael Halcrow rc = -EINVAL; 1310dddfa461SMichael Halcrow goto out_free; 1311dddfa461SMichael Halcrow } 1312dddfa461SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 131313218179SMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 131413218179SMichael Halcrow data[(*packet_size) - 1]); 1315dddfa461SMichael Halcrow rc = -EINVAL; 1316dddfa461SMichael Halcrow goto out_free; 1317dddfa461SMichael Halcrow } 1318dddfa461SMichael Halcrow ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 1319dddfa461SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 1320dddfa461SMichael Halcrow *packet_size += ECRYPTFS_SIG_SIZE; 1321dddfa461SMichael Halcrow /* This byte is skipped because the kernel does not need to 1322dddfa461SMichael Halcrow * know which public key encryption algorithm was used */ 1323dddfa461SMichael Halcrow (*packet_size)++; 1324dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 132513218179SMichael Halcrow body_size - (ECRYPTFS_SIG_SIZE + 2); 1326dddfa461SMichael Halcrow if ((*new_auth_tok)->session_key.encrypted_key_size 1327dddfa461SMichael Halcrow > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 132813218179SMichael Halcrow printk(KERN_WARNING "Tag 1 packet contains key larger " 1329dddfa461SMichael Halcrow "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); 1330dddfa461SMichael Halcrow rc = -EINVAL; 1331dddfa461SMichael Halcrow goto out; 1332dddfa461SMichael Halcrow } 1333dddfa461SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 133413218179SMichael Halcrow &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 1335dddfa461SMichael Halcrow (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 1336dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1337dddfa461SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1338dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1339dddfa461SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1340dddfa461SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 134113218179SMichael Halcrow (*new_auth_tok)->flags = 0; 1342e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1343e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1344e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1345e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1346dddfa461SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1347dddfa461SMichael Halcrow goto out; 1348dddfa461SMichael Halcrow out_free: 1349dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 1350dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 1351dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1352dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1353dddfa461SMichael Halcrow auth_tok_list_item); 1354dddfa461SMichael Halcrow out: 1355dddfa461SMichael Halcrow if (rc) 1356dddfa461SMichael Halcrow (*packet_size) = 0; 1357dddfa461SMichael Halcrow return rc; 1358dddfa461SMichael Halcrow } 1359dddfa461SMichael Halcrow 1360237fead6SMichael Halcrow /** 1361237fead6SMichael Halcrow * parse_tag_3_packet 1362237fead6SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 1363237fead6SMichael Halcrow * contents. 1364237fead6SMichael Halcrow * @data: The raw bytes of the packet. 1365237fead6SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1366237fead6SMichael Halcrow * a new authentication token will be placed at the end 1367237fead6SMichael Halcrow * of this list for this packet. 1368237fead6SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1369237fead6SMichael Halcrow * allocates; sets the memory address of the pointer to 1370237fead6SMichael Halcrow * NULL on error. This object is added to the 1371237fead6SMichael Halcrow * auth_tok_list. 1372237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1373237fead6SMichael Halcrow * into this memory location; zero on error. 1374237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1375237fead6SMichael Halcrow * 1376237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1377237fead6SMichael Halcrow */ 1378237fead6SMichael Halcrow static int 1379237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 1380237fead6SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1381237fead6SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1382237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1383237fead6SMichael Halcrow { 1384237fead6SMichael Halcrow size_t body_size; 1385237fead6SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1386237fead6SMichael Halcrow size_t length_size; 1387dddfa461SMichael Halcrow int rc = 0; 1388237fead6SMichael Halcrow 1389237fead6SMichael Halcrow (*packet_size) = 0; 1390237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1391c59becfcSMichael Halcrow /** 1392c59becfcSMichael Halcrow *This format is inspired by OpenPGP; see RFC 2440 1393c59becfcSMichael Halcrow * packet tag 3 1394c59becfcSMichael Halcrow * 1395c59becfcSMichael Halcrow * Tag 3 identifier (1 byte) 1396c59becfcSMichael Halcrow * Max Tag 3 packet size (max 3 bytes) 1397c59becfcSMichael Halcrow * Version (1 byte) 1398c59becfcSMichael Halcrow * Cipher code (1 byte) 1399c59becfcSMichael Halcrow * S2K specifier (1 byte) 1400c59becfcSMichael Halcrow * Hash identifier (1 byte) 1401c59becfcSMichael Halcrow * Salt (ECRYPTFS_SALT_SIZE) 1402c59becfcSMichael Halcrow * Hash iterations (1 byte) 1403c59becfcSMichael Halcrow * Encrypted key (arbitrary) 1404c59becfcSMichael Halcrow * 1405c59becfcSMichael Halcrow * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 1406237fead6SMichael Halcrow */ 1407c59becfcSMichael Halcrow if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 1408c59becfcSMichael Halcrow printk(KERN_ERR "Max packet size too large\n"); 1409237fead6SMichael Halcrow rc = -EINVAL; 1410237fead6SMichael Halcrow goto out; 1411237fead6SMichael Halcrow } 1412237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 1413c59becfcSMichael Halcrow printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 1414237fead6SMichael Halcrow ECRYPTFS_TAG_3_PACKET_TYPE); 1415237fead6SMichael Halcrow rc = -EINVAL; 1416237fead6SMichael Halcrow goto out; 1417237fead6SMichael Halcrow } 1418237fead6SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1419237fead6SMichael Halcrow * at end of function upon failure */ 1420237fead6SMichael Halcrow auth_tok_list_item = 1421c3762229SRobert P. J. Day kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 1422237fead6SMichael Halcrow if (!auth_tok_list_item) { 1423c59becfcSMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1424237fead6SMichael Halcrow rc = -ENOMEM; 1425237fead6SMichael Halcrow goto out; 1426237fead6SMichael Halcrow } 1427237fead6SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1428f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 14295dda6992SMichael Halcrow &length_size); 14305dda6992SMichael Halcrow if (rc) { 1431c59becfcSMichael Halcrow printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 1432c59becfcSMichael Halcrow rc); 1433237fead6SMichael Halcrow goto out_free; 1434237fead6SMichael Halcrow } 1435c59becfcSMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 143681acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1437237fead6SMichael Halcrow rc = -EINVAL; 1438237fead6SMichael Halcrow goto out_free; 1439237fead6SMichael Halcrow } 1440237fead6SMichael Halcrow (*packet_size) += length_size; 1441237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 1442c59becfcSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1443237fead6SMichael Halcrow rc = -EINVAL; 1444237fead6SMichael Halcrow goto out_free; 1445237fead6SMichael Halcrow } 1446237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 1447c59becfcSMichael Halcrow (body_size - (ECRYPTFS_SALT_SIZE + 5)); 1448f151cd2cSRamon de Carvalho Valle if ((*new_auth_tok)->session_key.encrypted_key_size 1449f151cd2cSRamon de Carvalho Valle > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1450f151cd2cSRamon de Carvalho Valle printk(KERN_WARNING "Tag 3 packet contains key larger " 1451f151cd2cSRamon de Carvalho Valle "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1452f151cd2cSRamon de Carvalho Valle rc = -EINVAL; 1453f151cd2cSRamon de Carvalho Valle goto out_free; 1454f151cd2cSRamon de Carvalho Valle } 1455237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x04)) { 1456c59becfcSMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 1457c59becfcSMichael Halcrow data[(*packet_size) - 1]); 1458237fead6SMichael Halcrow rc = -EINVAL; 1459237fead6SMichael Halcrow goto out_free; 1460237fead6SMichael Halcrow } 1461b0105eaeSTyler Hicks rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1462237fead6SMichael Halcrow (u16)data[(*packet_size)]); 1463b0105eaeSTyler Hicks if (rc) 1464b0105eaeSTyler Hicks goto out_free; 1465237fead6SMichael Halcrow /* A little extra work to differentiate among the AES key 1466237fead6SMichael Halcrow * sizes; see RFC2440 */ 1467237fead6SMichael Halcrow switch(data[(*packet_size)++]) { 1468237fead6SMichael Halcrow case RFC2440_CIPHER_AES_192: 1469237fead6SMichael Halcrow crypt_stat->key_size = 24; 1470237fead6SMichael Halcrow break; 1471237fead6SMichael Halcrow default: 1472237fead6SMichael Halcrow crypt_stat->key_size = 1473237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1474237fead6SMichael Halcrow } 1475b0105eaeSTyler Hicks rc = ecryptfs_init_crypt_ctx(crypt_stat); 1476b0105eaeSTyler Hicks if (rc) 1477b0105eaeSTyler Hicks goto out_free; 1478237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 1479c59becfcSMichael Halcrow printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 1480237fead6SMichael Halcrow rc = -ENOSYS; 1481237fead6SMichael Halcrow goto out_free; 1482237fead6SMichael Halcrow } 1483237fead6SMichael Halcrow /* TODO: finish the hash mapping */ 1484237fead6SMichael Halcrow switch (data[(*packet_size)++]) { 1485237fead6SMichael Halcrow case 0x01: /* See RFC2440 for these numbers and their mappings */ 1486237fead6SMichael Halcrow /* Choose MD5 */ 1487237fead6SMichael Halcrow memcpy((*new_auth_tok)->token.password.salt, 1488237fead6SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 1489237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; 1490237fead6SMichael Halcrow /* This conversion was taken straight from RFC2440 */ 1491237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_iterations = 1492237fead6SMichael Halcrow ((u32) 16 + (data[(*packet_size)] & 15)) 1493237fead6SMichael Halcrow << ((data[(*packet_size)] >> 4) + 6); 1494237fead6SMichael Halcrow (*packet_size)++; 1495c59becfcSMichael Halcrow /* Friendly reminder: 1496c59becfcSMichael Halcrow * (*new_auth_tok)->session_key.encrypted_key_size = 1497c59becfcSMichael Halcrow * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 1498237fead6SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 1499237fead6SMichael Halcrow &data[(*packet_size)], 1500237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 1501237fead6SMichael Halcrow (*packet_size) += 1502237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1503237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1504237fead6SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1505237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1506237fead6SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1507c59becfcSMichael Halcrow (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 1508237fead6SMichael Halcrow break; 1509237fead6SMichael Halcrow default: 1510237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 1511237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 1512237fead6SMichael Halcrow rc = -ENOSYS; 1513237fead6SMichael Halcrow goto out_free; 1514237fead6SMichael Halcrow } 1515237fead6SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 1516237fead6SMichael Halcrow /* TODO: Parametarize; we might actually want userspace to 1517237fead6SMichael Halcrow * decrypt the session key. */ 1518e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1519e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1520e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1521e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1522237fead6SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1523237fead6SMichael Halcrow goto out; 1524237fead6SMichael Halcrow out_free: 1525237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1526237fead6SMichael Halcrow memset(auth_tok_list_item, 0, 1527237fead6SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1528237fead6SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1529237fead6SMichael Halcrow auth_tok_list_item); 1530237fead6SMichael Halcrow out: 1531237fead6SMichael Halcrow if (rc) 1532237fead6SMichael Halcrow (*packet_size) = 0; 1533237fead6SMichael Halcrow return rc; 1534237fead6SMichael Halcrow } 1535237fead6SMichael Halcrow 1536237fead6SMichael Halcrow /** 1537237fead6SMichael Halcrow * parse_tag_11_packet 1538237fead6SMichael Halcrow * @data: The raw bytes of the packet 1539237fead6SMichael Halcrow * @contents: This function writes the data contents of the literal 1540237fead6SMichael Halcrow * packet into this memory location 1541237fead6SMichael Halcrow * @max_contents_bytes: The maximum number of bytes that this function 1542237fead6SMichael Halcrow * is allowed to write into contents 1543237fead6SMichael Halcrow * @tag_11_contents_size: This function writes the size of the parsed 1544237fead6SMichael Halcrow * contents into this memory location; zero on 1545237fead6SMichael Halcrow * error 1546237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1547237fead6SMichael Halcrow * into this memory location; zero on error 1548237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1549237fead6SMichael Halcrow * 1550237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1551237fead6SMichael Halcrow */ 1552237fead6SMichael Halcrow static int 1553237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents, 1554237fead6SMichael Halcrow size_t max_contents_bytes, size_t *tag_11_contents_size, 1555237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1556237fead6SMichael Halcrow { 1557237fead6SMichael Halcrow size_t body_size; 1558237fead6SMichael Halcrow size_t length_size; 1559dddfa461SMichael Halcrow int rc = 0; 1560237fead6SMichael Halcrow 1561237fead6SMichael Halcrow (*packet_size) = 0; 1562237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1563f648104aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1564f648104aSMichael Halcrow * packet tag 11 1565f648104aSMichael Halcrow * 1566f648104aSMichael Halcrow * Tag 11 identifier (1 byte) 1567f648104aSMichael Halcrow * Max Tag 11 packet size (max 3 bytes) 1568f648104aSMichael Halcrow * Binary format specifier (1 byte) 1569f648104aSMichael Halcrow * Filename length (1 byte) 1570f648104aSMichael Halcrow * Filename ("_CONSOLE") (8 bytes) 1571f648104aSMichael Halcrow * Modification date (4 bytes) 1572f648104aSMichael Halcrow * Literal data (arbitrary) 1573f648104aSMichael Halcrow * 1574f648104aSMichael Halcrow * We need at least 16 bytes of data for the packet to even be 1575f648104aSMichael Halcrow * valid. 1576237fead6SMichael Halcrow */ 1577f648104aSMichael Halcrow if (max_packet_size < 16) { 1578f648104aSMichael Halcrow printk(KERN_ERR "Maximum packet size too small\n"); 1579237fead6SMichael Halcrow rc = -EINVAL; 1580237fead6SMichael Halcrow goto out; 1581237fead6SMichael Halcrow } 1582237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 1583f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1584237fead6SMichael Halcrow rc = -EINVAL; 1585237fead6SMichael Halcrow goto out; 1586237fead6SMichael Halcrow } 1587f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 15885dda6992SMichael Halcrow &length_size); 15895dda6992SMichael Halcrow if (rc) { 1590f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1591f648104aSMichael Halcrow goto out; 1592f648104aSMichael Halcrow } 1593f648104aSMichael Halcrow if (body_size < 14) { 159481acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1595f648104aSMichael Halcrow rc = -EINVAL; 1596237fead6SMichael Halcrow goto out; 1597237fead6SMichael Halcrow } 1598237fead6SMichael Halcrow (*packet_size) += length_size; 1599f648104aSMichael Halcrow (*tag_11_contents_size) = (body_size - 14); 1600237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 1601f648104aSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1602237fead6SMichael Halcrow rc = -EINVAL; 1603237fead6SMichael Halcrow goto out; 1604237fead6SMichael Halcrow } 16056352a293STyler Hicks if (unlikely((*tag_11_contents_size) > max_contents_bytes)) { 16066352a293STyler Hicks printk(KERN_ERR "Literal data section in tag 11 packet exceeds " 16076352a293STyler Hicks "expected size\n"); 16086352a293STyler Hicks rc = -EINVAL; 16096352a293STyler Hicks goto out; 16106352a293STyler Hicks } 1611237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x62) { 1612f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1613237fead6SMichael Halcrow rc = -EINVAL; 1614237fead6SMichael Halcrow goto out; 1615237fead6SMichael Halcrow } 1616237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x08) { 1617f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1618237fead6SMichael Halcrow rc = -EINVAL; 1619237fead6SMichael Halcrow goto out; 1620237fead6SMichael Halcrow } 1621f648104aSMichael Halcrow (*packet_size) += 12; /* Ignore filename and modification date */ 1622237fead6SMichael Halcrow memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 1623237fead6SMichael Halcrow (*packet_size) += (*tag_11_contents_size); 1624237fead6SMichael Halcrow out: 1625237fead6SMichael Halcrow if (rc) { 1626237fead6SMichael Halcrow (*packet_size) = 0; 1627237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1628237fead6SMichael Halcrow } 1629237fead6SMichael Halcrow return rc; 1630237fead6SMichael Halcrow } 1631237fead6SMichael Halcrow 1632f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 1633f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 1634f4aad16aSMichael Halcrow char *sig) 1635f4aad16aSMichael Halcrow { 1636f4aad16aSMichael Halcrow int rc = 0; 1637f4aad16aSMichael Halcrow 1638f4aad16aSMichael Halcrow (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 1639f4aad16aSMichael Halcrow if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 16401252cc3bSRoberto Sassu (*auth_tok_key) = ecryptfs_get_encrypted_key(sig); 16411252cc3bSRoberto Sassu if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 1642f4aad16aSMichael Halcrow printk(KERN_ERR "Could not find key with description: [%s]\n", 1643f4aad16aSMichael Halcrow sig); 1644982363c9SEric Sandeen rc = process_request_key_err(PTR_ERR(*auth_tok_key)); 16451821df04SRoberto Sassu (*auth_tok_key) = NULL; 1646f4aad16aSMichael Halcrow goto out; 1647f4aad16aSMichael Halcrow } 16481252cc3bSRoberto Sassu } 1649b5695d04SRoberto Sassu down_write(&(*auth_tok_key)->sem); 16500e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok); 1651aee683b9SRoberto Sassu if (rc) { 1652b5695d04SRoberto Sassu up_write(&(*auth_tok_key)->sem); 1653aee683b9SRoberto Sassu key_put(*auth_tok_key); 1654aee683b9SRoberto Sassu (*auth_tok_key) = NULL; 16550e1fc5efSRoberto Sassu goto out; 1656f4aad16aSMichael Halcrow } 1657f4aad16aSMichael Halcrow out: 1658f4aad16aSMichael Halcrow return rc; 1659f4aad16aSMichael Halcrow } 1660f4aad16aSMichael Halcrow 1661f4aad16aSMichael Halcrow /** 166222e78fafSMichael Halcrow * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. 166322e78fafSMichael Halcrow * @auth_tok: The passphrase authentication token to use to encrypt the FEK 166422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1665237fead6SMichael Halcrow * 166622e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise 1667237fead6SMichael Halcrow */ 1668f4aad16aSMichael Halcrow static int 1669f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1670237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1671237fead6SMichael Halcrow { 1672ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 1673ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 1674dd8e2902SMichael Halcrow struct mutex *tfm_mutex; 16753095e8e3SHerbert Xu struct crypto_skcipher *tfm; 16763095e8e3SHerbert Xu struct skcipher_request *req = NULL; 16778bba066fSMichael Halcrow int rc = 0; 1678237fead6SMichael Halcrow 1679f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1680f4aad16aSMichael Halcrow ecryptfs_printk( 1681f4aad16aSMichael Halcrow KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1682f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1683f4aad16aSMichael Halcrow ecryptfs_dump_hex( 1684f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1685f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1686f4aad16aSMichael Halcrow } 16873095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 1688f4aad16aSMichael Halcrow crypt_stat->cipher); 1689f4aad16aSMichael Halcrow if (unlikely(rc)) { 1690f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1691f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1692f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1693237fead6SMichael Halcrow goto out; 1694237fead6SMichael Halcrow } 16955dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1696f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size, 1697ac97b9f9SMichael Halcrow src_sg, 2); 1698ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1699f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1700f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key to scatterlist; " 1701f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1702f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1703f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size); 1704f4aad16aSMichael Halcrow goto out; 1705237fead6SMichael Halcrow } 1706f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size = 1707f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size; 17085dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1709f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size, 1710ac97b9f9SMichael Halcrow dst_sg, 2); 1711ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1712f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1713f4aad16aSMichael Halcrow "auth_tok->session_key.decrypted_key to scatterlist; " 1714f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]\n", rc); 1715f4aad16aSMichael Halcrow goto out; 1716f4aad16aSMichael Halcrow } 1717237fead6SMichael Halcrow mutex_lock(tfm_mutex); 17183095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 17193095e8e3SHerbert Xu if (!req) { 17203095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 17213095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 17223095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 17233095e8e3SHerbert Xu crypto_skcipher_driver_name(tfm)); 17243095e8e3SHerbert Xu rc = -ENOMEM; 17253095e8e3SHerbert Xu goto out; 17263095e8e3SHerbert Xu } 17273095e8e3SHerbert Xu 17283095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 17293095e8e3SHerbert Xu NULL, NULL); 17303095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 17313095e8e3SHerbert Xu tfm, auth_tok->token.password.session_key_encryption_key, 1732237fead6SMichael Halcrow crypt_stat->key_size); 1733f4aad16aSMichael Halcrow if (unlikely(rc < 0)) { 1734f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1735e5d9cbdeSMichael Halcrow printk(KERN_ERR "Error setting key for crypto context\n"); 1736e5d9cbdeSMichael Halcrow rc = -EINVAL; 1737f4aad16aSMichael Halcrow goto out; 1738e5d9cbdeSMichael Halcrow } 17393095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 17403095e8e3SHerbert Xu auth_tok->session_key.encrypted_key_size, 17413095e8e3SHerbert Xu NULL); 17423095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(req); 1743f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1744f4aad16aSMichael Halcrow if (unlikely(rc)) { 17458bba066fSMichael Halcrow printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1746f4aad16aSMichael Halcrow goto out; 17478bba066fSMichael Halcrow } 1748237fead6SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1749237fead6SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1750237fead6SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1751e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1752f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1753f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n", 1754f4aad16aSMichael Halcrow crypt_stat->key_size); 1755237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1756237fead6SMichael Halcrow crypt_stat->key_size); 1757f4aad16aSMichael Halcrow } 1758237fead6SMichael Halcrow out: 17593095e8e3SHerbert Xu skcipher_request_free(req); 1760237fead6SMichael Halcrow return rc; 1761237fead6SMichael Halcrow } 1762237fead6SMichael Halcrow 1763237fead6SMichael Halcrow /** 1764237fead6SMichael Halcrow * ecryptfs_parse_packet_set 176522e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 176622e78fafSMichael Halcrow * @src: Virtual address of region of memory containing the packets 176722e78fafSMichael Halcrow * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set 1768237fead6SMichael Halcrow * 1769237fead6SMichael Halcrow * Get crypt_stat to have the file's session key if the requisite key 1770237fead6SMichael Halcrow * is available to decrypt the session key. 1771237fead6SMichael Halcrow * 1772237fead6SMichael Halcrow * Returns Zero if a valid authentication token was retrieved and 1773237fead6SMichael Halcrow * processed; negative value for file not encrypted or for error 1774237fead6SMichael Halcrow * conditions. 1775237fead6SMichael Halcrow */ 1776237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1777237fead6SMichael Halcrow unsigned char *src, 1778237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1779237fead6SMichael Halcrow { 1780237fead6SMichael Halcrow size_t i = 0; 1781f4aad16aSMichael Halcrow size_t found_auth_tok; 1782237fead6SMichael Halcrow size_t next_packet_is_auth_tok_packet; 1783237fead6SMichael Halcrow struct list_head auth_tok_list; 1784dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *matching_auth_tok; 1785dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *candidate_auth_tok; 1786f4aad16aSMichael Halcrow char *candidate_auth_tok_sig; 1787237fead6SMichael Halcrow size_t packet_size; 1788237fead6SMichael Halcrow struct ecryptfs_auth_tok *new_auth_tok; 1789237fead6SMichael Halcrow unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1790f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1791237fead6SMichael Halcrow size_t tag_11_contents_size; 1792237fead6SMichael Halcrow size_t tag_11_packet_size; 1793aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 1794dddfa461SMichael Halcrow int rc = 0; 1795237fead6SMichael Halcrow 1796237fead6SMichael Halcrow INIT_LIST_HEAD(&auth_tok_list); 1797f4aad16aSMichael Halcrow /* Parse the header to find as many packets as we can; these will be 1798237fead6SMichael Halcrow * added the our &auth_tok_list */ 1799237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 1; 1800237fead6SMichael Halcrow while (next_packet_is_auth_tok_packet) { 1801237fead6SMichael Halcrow size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); 1802237fead6SMichael Halcrow 1803237fead6SMichael Halcrow switch (src[i]) { 1804237fead6SMichael Halcrow case ECRYPTFS_TAG_3_PACKET_TYPE: 1805237fead6SMichael Halcrow rc = parse_tag_3_packet(crypt_stat, 1806237fead6SMichael Halcrow (unsigned char *)&src[i], 1807237fead6SMichael Halcrow &auth_tok_list, &new_auth_tok, 1808237fead6SMichael Halcrow &packet_size, max_packet_size); 1809237fead6SMichael Halcrow if (rc) { 1810237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1811237fead6SMichael Halcrow "tag 3 packet\n"); 1812237fead6SMichael Halcrow rc = -EIO; 1813237fead6SMichael Halcrow goto out_wipe_list; 1814237fead6SMichael Halcrow } 1815237fead6SMichael Halcrow i += packet_size; 1816237fead6SMichael Halcrow rc = parse_tag_11_packet((unsigned char *)&src[i], 1817237fead6SMichael Halcrow sig_tmp_space, 1818237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1819237fead6SMichael Halcrow &tag_11_contents_size, 1820237fead6SMichael Halcrow &tag_11_packet_size, 1821237fead6SMichael Halcrow max_packet_size); 1822237fead6SMichael Halcrow if (rc) { 1823237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No valid " 1824237fead6SMichael Halcrow "(ecryptfs-specific) literal " 1825237fead6SMichael Halcrow "packet containing " 1826237fead6SMichael Halcrow "authentication token " 1827237fead6SMichael Halcrow "signature found after " 1828237fead6SMichael Halcrow "tag 3 packet\n"); 1829237fead6SMichael Halcrow rc = -EIO; 1830237fead6SMichael Halcrow goto out_wipe_list; 1831237fead6SMichael Halcrow } 1832237fead6SMichael Halcrow i += tag_11_packet_size; 1833237fead6SMichael Halcrow if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1834237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Expected " 1835237fead6SMichael Halcrow "signature of size [%d]; " 1836f24b3887STyler Hicks "read size [%zd]\n", 1837237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1838237fead6SMichael Halcrow tag_11_contents_size); 1839237fead6SMichael Halcrow rc = -EIO; 1840237fead6SMichael Halcrow goto out_wipe_list; 1841237fead6SMichael Halcrow } 1842237fead6SMichael Halcrow ecryptfs_to_hex(new_auth_tok->token.password.signature, 1843237fead6SMichael Halcrow sig_tmp_space, tag_11_contents_size); 1844237fead6SMichael Halcrow new_auth_tok->token.password.signature[ 1845237fead6SMichael Halcrow ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1846e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1847237fead6SMichael Halcrow break; 1848dddfa461SMichael Halcrow case ECRYPTFS_TAG_1_PACKET_TYPE: 1849dddfa461SMichael Halcrow rc = parse_tag_1_packet(crypt_stat, 1850dddfa461SMichael Halcrow (unsigned char *)&src[i], 1851dddfa461SMichael Halcrow &auth_tok_list, &new_auth_tok, 1852dddfa461SMichael Halcrow &packet_size, max_packet_size); 1853dddfa461SMichael Halcrow if (rc) { 1854dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1855dddfa461SMichael Halcrow "tag 1 packet\n"); 1856dddfa461SMichael Halcrow rc = -EIO; 1857dddfa461SMichael Halcrow goto out_wipe_list; 1858dddfa461SMichael Halcrow } 1859dddfa461SMichael Halcrow i += packet_size; 1860e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1861dddfa461SMichael Halcrow break; 1862237fead6SMichael Halcrow case ECRYPTFS_TAG_11_PACKET_TYPE: 1863237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1864237fead6SMichael Halcrow "(Tag 11 not allowed by itself)\n"); 1865237fead6SMichael Halcrow rc = -EIO; 1866237fead6SMichael Halcrow goto out_wipe_list; 1867237fead6SMichael Halcrow default: 1868f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] " 1869f24b3887STyler Hicks "of the file header; hex value of " 1870237fead6SMichael Halcrow "character is [0x%.2x]\n", i, src[i]); 1871237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 0; 1872237fead6SMichael Halcrow } 1873237fead6SMichael Halcrow } 1874237fead6SMichael Halcrow if (list_empty(&auth_tok_list)) { 1875f4aad16aSMichael Halcrow printk(KERN_ERR "The lower file appears to be a non-encrypted " 1876f4aad16aSMichael Halcrow "eCryptfs file; this is not supported in this version " 1877f4aad16aSMichael Halcrow "of the eCryptfs kernel module\n"); 1878f4aad16aSMichael Halcrow rc = -EINVAL; 1879237fead6SMichael Halcrow goto out; 1880237fead6SMichael Halcrow } 1881f4aad16aSMichael Halcrow /* auth_tok_list contains the set of authentication tokens 1882f4aad16aSMichael Halcrow * parsed from the metadata. We need to find a matching 1883f4aad16aSMichael Halcrow * authentication token that has the secret component(s) 1884f4aad16aSMichael Halcrow * necessary to decrypt the EFEK in the auth_tok parsed from 1885f4aad16aSMichael Halcrow * the metadata. There may be several potential matches, but 1886f4aad16aSMichael Halcrow * just one will be sufficient to decrypt to get the FEK. */ 1887f4aad16aSMichael Halcrow find_next_matching_auth_tok: 1888f4aad16aSMichael Halcrow found_auth_tok = 0; 1889f4aad16aSMichael Halcrow list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1890237fead6SMichael Halcrow candidate_auth_tok = &auth_tok_list_item->auth_tok; 1891237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1892237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1893237fead6SMichael Halcrow "Considering cadidate auth tok:\n"); 1894237fead6SMichael Halcrow ecryptfs_dump_auth_tok(candidate_auth_tok); 1895237fead6SMichael Halcrow } 18965dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 18975dda6992SMichael Halcrow candidate_auth_tok); 18985dda6992SMichael Halcrow if (rc) { 1899f4aad16aSMichael Halcrow printk(KERN_ERR 1900f4aad16aSMichael Halcrow "Unrecognized candidate auth tok type: [%d]\n", 1901f4aad16aSMichael Halcrow candidate_auth_tok->token_type); 1902f4aad16aSMichael Halcrow rc = -EINVAL; 1903f4aad16aSMichael Halcrow goto out_wipe_list; 1904f4aad16aSMichael Halcrow } 190539fac853SRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 1906aee683b9SRoberto Sassu &matching_auth_tok, 19079c79f34fSMichael Halcrow crypt_stat->mount_crypt_stat, 19085dda6992SMichael Halcrow candidate_auth_tok_sig); 190939fac853SRoberto Sassu if (!rc) { 1910237fead6SMichael Halcrow found_auth_tok = 1; 1911f4aad16aSMichael Halcrow goto found_matching_auth_tok; 1912237fead6SMichael Halcrow } 1913237fead6SMichael Halcrow } 1914237fead6SMichael Halcrow if (!found_auth_tok) { 1915f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Could not find a usable " 1916f4aad16aSMichael Halcrow "authentication token\n"); 1917237fead6SMichael Halcrow rc = -EIO; 1918237fead6SMichael Halcrow goto out_wipe_list; 1919dddfa461SMichael Halcrow } 1920f4aad16aSMichael Halcrow found_matching_auth_tok: 1921e2bd99ecSMichael Halcrow if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1922dddfa461SMichael Halcrow memcpy(&(candidate_auth_tok->token.private_key), 1923f4aad16aSMichael Halcrow &(matching_auth_tok->token.private_key), 1924dddfa461SMichael Halcrow sizeof(struct ecryptfs_private_key)); 1925b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1926b2987a5eSTyler Hicks key_put(auth_tok_key); 1927f4aad16aSMichael Halcrow rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1928dddfa461SMichael Halcrow crypt_stat); 1929dddfa461SMichael Halcrow } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1930237fead6SMichael Halcrow memcpy(&(candidate_auth_tok->token.password), 1931f4aad16aSMichael Halcrow &(matching_auth_tok->token.password), 1932237fead6SMichael Halcrow sizeof(struct ecryptfs_password)); 1933b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1934b2987a5eSTyler Hicks key_put(auth_tok_key); 1935f4aad16aSMichael Halcrow rc = decrypt_passphrase_encrypted_session_key( 1936f4aad16aSMichael Halcrow candidate_auth_tok, crypt_stat); 1937b2987a5eSTyler Hicks } else { 1938b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1939b2987a5eSTyler Hicks key_put(auth_tok_key); 1940b2987a5eSTyler Hicks rc = -EINVAL; 1941dddfa461SMichael Halcrow } 1942237fead6SMichael Halcrow if (rc) { 1943f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1944f4aad16aSMichael Halcrow 1945f4aad16aSMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1946f4aad16aSMichael Halcrow "session key for authentication token with sig " 1947f4aad16aSMichael Halcrow "[%.*s]; rc = [%d]. Removing auth tok " 1948f4aad16aSMichael Halcrow "candidate from the list and searching for " 1949888d57bbSJoe Perches "the next match.\n", ECRYPTFS_SIG_SIZE_HEX, 1950888d57bbSJoe Perches candidate_auth_tok_sig, rc); 1951f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, 1952f4aad16aSMichael Halcrow auth_tok_list_item_tmp, 1953f4aad16aSMichael Halcrow &auth_tok_list, list) { 1954f4aad16aSMichael Halcrow if (candidate_auth_tok 1955f4aad16aSMichael Halcrow == &auth_tok_list_item->auth_tok) { 1956f4aad16aSMichael Halcrow list_del(&auth_tok_list_item->list); 1957f4aad16aSMichael Halcrow kmem_cache_free( 1958f4aad16aSMichael Halcrow ecryptfs_auth_tok_list_item_cache, 1959f4aad16aSMichael Halcrow auth_tok_list_item); 1960f4aad16aSMichael Halcrow goto find_next_matching_auth_tok; 1961f4aad16aSMichael Halcrow } 1962f4aad16aSMichael Halcrow } 1963f4aad16aSMichael Halcrow BUG(); 1964237fead6SMichael Halcrow } 1965237fead6SMichael Halcrow rc = ecryptfs_compute_root_iv(crypt_stat); 1966237fead6SMichael Halcrow if (rc) { 1967237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error computing " 1968237fead6SMichael Halcrow "the root IV\n"); 1969237fead6SMichael Halcrow goto out_wipe_list; 1970237fead6SMichael Halcrow } 1971237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 1972237fead6SMichael Halcrow if (rc) { 1973237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1974237fead6SMichael Halcrow "context for cipher [%s]; rc = [%d]\n", 1975237fead6SMichael Halcrow crypt_stat->cipher, rc); 1976237fead6SMichael Halcrow } 1977237fead6SMichael Halcrow out_wipe_list: 1978237fead6SMichael Halcrow wipe_auth_tok_list(&auth_tok_list); 1979237fead6SMichael Halcrow out: 1980237fead6SMichael Halcrow return rc; 1981237fead6SMichael Halcrow } 1982f4aad16aSMichael Halcrow 1983dddfa461SMichael Halcrow static int 1984b2987a5eSTyler Hicks pki_encrypt_session_key(struct key *auth_tok_key, 1985b2987a5eSTyler Hicks struct ecryptfs_auth_tok *auth_tok, 1986dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1987dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec) 1988dddfa461SMichael Halcrow { 1989dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx = NULL; 1990624ae528STyler Hicks char *payload = NULL; 199199b373ffSTyler Hicks size_t payload_len = 0; 1992dddfa461SMichael Halcrow struct ecryptfs_message *msg; 1993dddfa461SMichael Halcrow int rc; 1994dddfa461SMichael Halcrow 1995dddfa461SMichael Halcrow rc = write_tag_66_packet(auth_tok->token.private_key.signature, 19969c79f34fSMichael Halcrow ecryptfs_code_for_cipher_string( 19979c79f34fSMichael Halcrow crypt_stat->cipher, 19989c79f34fSMichael Halcrow crypt_stat->key_size), 1999624ae528STyler Hicks crypt_stat, &payload, &payload_len); 2000b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2001b2987a5eSTyler Hicks key_put(auth_tok_key); 2002dddfa461SMichael Halcrow if (rc) { 2003dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 2004dddfa461SMichael Halcrow goto out; 2005dddfa461SMichael Halcrow } 2006624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 2007dddfa461SMichael Halcrow if (rc) { 2008624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 2009290502beSKees Cook "ecryptfsd: %d\n", rc); 2010dddfa461SMichael Halcrow goto out; 2011dddfa461SMichael Halcrow } 2012dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 2013dddfa461SMichael Halcrow if (rc) { 2014dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 2015dddfa461SMichael Halcrow "from the user space daemon\n"); 2016dddfa461SMichael Halcrow rc = -EIO; 2017dddfa461SMichael Halcrow goto out; 2018dddfa461SMichael Halcrow } 2019dddfa461SMichael Halcrow rc = parse_tag_67_packet(key_rec, msg); 2020dddfa461SMichael Halcrow if (rc) 2021dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 2022dddfa461SMichael Halcrow kfree(msg); 2023dddfa461SMichael Halcrow out: 2024624ae528STyler Hicks kfree(payload); 2025dddfa461SMichael Halcrow return rc; 2026dddfa461SMichael Halcrow } 2027dddfa461SMichael Halcrow /** 2028dddfa461SMichael Halcrow * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 2029dddfa461SMichael Halcrow * @dest: Buffer into which to write the packet 203022e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be writtn 2031b2987a5eSTyler Hicks * @auth_tok_key: The authentication token key to unlock and put when done with 2032b2987a5eSTyler Hicks * @auth_tok 203322e78fafSMichael Halcrow * @auth_tok: The authentication token used for generating the tag 1 packet 203422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 203522e78fafSMichael Halcrow * @key_rec: The key record struct for the tag 1 packet 2036dddfa461SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2037dddfa461SMichael Halcrow * up constituting the packet; set to zero on error 2038dddfa461SMichael Halcrow * 2039dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 2040dddfa461SMichael Halcrow */ 2041dddfa461SMichael Halcrow static int 2042f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes, 2043b2987a5eSTyler Hicks struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok, 2044dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2045dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2046dddfa461SMichael Halcrow { 2047dddfa461SMichael Halcrow size_t i; 2048dddfa461SMichael Halcrow size_t encrypted_session_key_valid = 0; 2049dddfa461SMichael Halcrow size_t packet_size_length; 2050f4aad16aSMichael Halcrow size_t max_packet_size; 2051dddfa461SMichael Halcrow int rc = 0; 2052dddfa461SMichael Halcrow 2053dddfa461SMichael Halcrow (*packet_size) = 0; 2054dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 2055dddfa461SMichael Halcrow ECRYPTFS_SIG_SIZE); 2056dddfa461SMichael Halcrow encrypted_session_key_valid = 0; 2057dddfa461SMichael Halcrow for (i = 0; i < crypt_stat->key_size; i++) 2058dddfa461SMichael Halcrow encrypted_session_key_valid |= 2059dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2060dddfa461SMichael Halcrow if (encrypted_session_key_valid) { 2061dddfa461SMichael Halcrow memcpy(key_rec->enc_key, 2062dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key, 2063dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size); 2064b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2065b2987a5eSTyler Hicks key_put(auth_tok_key); 2066dddfa461SMichael Halcrow goto encrypted_session_key_set; 2067dddfa461SMichael Halcrow } 2068dddfa461SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2069dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2070dddfa461SMichael Halcrow auth_tok->token.private_key.key_size; 2071b2987a5eSTyler Hicks rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat, 2072b2987a5eSTyler Hicks key_rec); 2073dddfa461SMichael Halcrow if (rc) { 2074f66e883eSMichael Halcrow printk(KERN_ERR "Failed to encrypt session key via a key " 2075f66e883eSMichael Halcrow "module; rc = [%d]\n", rc); 2076dddfa461SMichael Halcrow goto out; 2077dddfa461SMichael Halcrow } 2078dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 2079dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 2080dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 2081dddfa461SMichael Halcrow } 2082dddfa461SMichael Halcrow encrypted_session_key_set: 2083f4aad16aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2084f4aad16aSMichael Halcrow * packet tag 1 */ 2085f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 1 identifier */ 2086f4aad16aSMichael Halcrow + 3 /* Max Tag 1 packet size */ 2087f4aad16aSMichael Halcrow + 1 /* Version */ 2088f4aad16aSMichael Halcrow + ECRYPTFS_SIG_SIZE /* Key identifier */ 2089f4aad16aSMichael Halcrow + 1 /* Cipher identifier */ 2090f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2091f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2092f4aad16aSMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 209381acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2094f4aad16aSMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2095dddfa461SMichael Halcrow rc = -EINVAL; 2096dddfa461SMichael Halcrow goto out; 2097dddfa461SMichael Halcrow } 2098dddfa461SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 2099f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2100f66e883eSMichael Halcrow (max_packet_size - 4), 2101dddfa461SMichael Halcrow &packet_size_length); 2102dddfa461SMichael Halcrow if (rc) { 2103dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 2104dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 2105dddfa461SMichael Halcrow goto out; 2106dddfa461SMichael Halcrow } 2107dddfa461SMichael Halcrow (*packet_size) += packet_size_length; 2108dddfa461SMichael Halcrow dest[(*packet_size)++] = 0x03; /* version 3 */ 2109dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 2110dddfa461SMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 2111dddfa461SMichael Halcrow dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 2112dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2113dddfa461SMichael Halcrow key_rec->enc_key_size); 2114dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2115dddfa461SMichael Halcrow out: 2116dddfa461SMichael Halcrow if (rc) 2117dddfa461SMichael Halcrow (*packet_size) = 0; 2118f4aad16aSMichael Halcrow else 2119f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2120dddfa461SMichael Halcrow return rc; 2121dddfa461SMichael Halcrow } 2122237fead6SMichael Halcrow 2123237fead6SMichael Halcrow /** 2124237fead6SMichael Halcrow * write_tag_11_packet 2125237fead6SMichael Halcrow * @dest: Target into which Tag 11 packet is to be written 212622e78fafSMichael Halcrow * @remaining_bytes: Maximum packet length 2127237fead6SMichael Halcrow * @contents: Byte array of contents to copy in 2128237fead6SMichael Halcrow * @contents_length: Number of bytes in contents 2129237fead6SMichael Halcrow * @packet_length: Length of the Tag 11 packet written; zero on error 2130237fead6SMichael Halcrow * 2131237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2132237fead6SMichael Halcrow */ 2133237fead6SMichael Halcrow static int 213481acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, 2135146a4606SMichael Halcrow size_t contents_length, size_t *packet_length) 2136237fead6SMichael Halcrow { 2137237fead6SMichael Halcrow size_t packet_size_length; 2138146a4606SMichael Halcrow size_t max_packet_size; 2139dddfa461SMichael Halcrow int rc = 0; 2140237fead6SMichael Halcrow 2141237fead6SMichael Halcrow (*packet_length) = 0; 2142146a4606SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2143146a4606SMichael Halcrow * packet tag 11 */ 2144146a4606SMichael Halcrow max_packet_size = (1 /* Tag 11 identifier */ 2145146a4606SMichael Halcrow + 3 /* Max Tag 11 packet size */ 2146146a4606SMichael Halcrow + 1 /* Binary format specifier */ 2147146a4606SMichael Halcrow + 1 /* Filename length */ 2148146a4606SMichael Halcrow + 8 /* Filename ("_CONSOLE") */ 2149146a4606SMichael Halcrow + 4 /* Modification date */ 2150146a4606SMichael Halcrow + contents_length); /* Literal data */ 2151146a4606SMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2152146a4606SMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 215381acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2154146a4606SMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2155237fead6SMichael Halcrow rc = -EINVAL; 2156237fead6SMichael Halcrow goto out; 2157237fead6SMichael Halcrow } 2158237fead6SMichael Halcrow dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 2159f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_length)], 2160f66e883eSMichael Halcrow (max_packet_size - 4), 2161f66e883eSMichael Halcrow &packet_size_length); 2162237fead6SMichael Halcrow if (rc) { 2163146a4606SMichael Halcrow printk(KERN_ERR "Error generating tag 11 packet header; cannot " 2164146a4606SMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2165237fead6SMichael Halcrow goto out; 2166237fead6SMichael Halcrow } 2167237fead6SMichael Halcrow (*packet_length) += packet_size_length; 2168146a4606SMichael Halcrow dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 2169237fead6SMichael Halcrow dest[(*packet_length)++] = 8; 2170237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 2171237fead6SMichael Halcrow (*packet_length) += 8; 2172237fead6SMichael Halcrow memset(&dest[(*packet_length)], 0x00, 4); 2173237fead6SMichael Halcrow (*packet_length) += 4; 2174237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], contents, contents_length); 2175237fead6SMichael Halcrow (*packet_length) += contents_length; 2176237fead6SMichael Halcrow out: 2177237fead6SMichael Halcrow if (rc) 2178237fead6SMichael Halcrow (*packet_length) = 0; 2179146a4606SMichael Halcrow else 2180146a4606SMichael Halcrow (*remaining_bytes) -= (*packet_length); 2181237fead6SMichael Halcrow return rc; 2182237fead6SMichael Halcrow } 2183237fead6SMichael Halcrow 2184237fead6SMichael Halcrow /** 2185237fead6SMichael Halcrow * write_tag_3_packet 2186237fead6SMichael Halcrow * @dest: Buffer into which to write the packet 218722e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be written 2188237fead6SMichael Halcrow * @auth_tok: Authentication token 2189237fead6SMichael Halcrow * @crypt_stat: The cryptographic context 2190237fead6SMichael Halcrow * @key_rec: encrypted key 2191237fead6SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2192237fead6SMichael Halcrow * up constituting the packet; set to zero on error 2193237fead6SMichael Halcrow * 2194237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2195237fead6SMichael Halcrow */ 2196237fead6SMichael Halcrow static int 2197f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes, 2198f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 2199237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2200237fead6SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2201237fead6SMichael Halcrow { 2202237fead6SMichael Halcrow size_t i; 2203237fead6SMichael Halcrow size_t encrypted_session_key_valid = 0; 2204237fead6SMichael Halcrow char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 2205ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 2206ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 2207237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 220819e66a67STrevor Highland u8 cipher_code; 2209f4aad16aSMichael Halcrow size_t packet_size_length; 2210f4aad16aSMichael Halcrow size_t max_packet_size; 2211f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2212f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 22133095e8e3SHerbert Xu struct crypto_skcipher *tfm; 22143095e8e3SHerbert Xu struct skcipher_request *req; 22158bba066fSMichael Halcrow int rc = 0; 2216237fead6SMichael Halcrow 2217237fead6SMichael Halcrow (*packet_size) = 0; 2218dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 2219237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE); 22203095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 2221f4aad16aSMichael Halcrow crypt_stat->cipher); 2222f4aad16aSMichael Halcrow if (unlikely(rc)) { 2223f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 2224f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 2225f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 2226f4aad16aSMichael Halcrow goto out; 2227237fead6SMichael Halcrow } 2228f4aad16aSMichael Halcrow if (mount_crypt_stat->global_default_cipher_key_size == 0) { 2229f4aad16aSMichael Halcrow printk(KERN_WARNING "No key size specified at mount; " 22303095e8e3SHerbert Xu "defaulting to [%d]\n", 22313095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm)); 2232f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size = 22333095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm); 2234f4aad16aSMichael Halcrow } 2235f4aad16aSMichael Halcrow if (crypt_stat->key_size == 0) 2236f4aad16aSMichael Halcrow crypt_stat->key_size = 2237f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 2238237fead6SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2239237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2240237fead6SMichael Halcrow crypt_stat->key_size; 2241237fead6SMichael Halcrow if (crypt_stat->key_size == 24 2242237fead6SMichael Halcrow && strcmp("aes", crypt_stat->cipher) == 0) { 2243237fead6SMichael Halcrow memset((crypt_stat->key + 24), 0, 8); 2244237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 32; 2245f4aad16aSMichael Halcrow } else 2246f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 2247dddfa461SMichael Halcrow key_rec->enc_key_size = 2248237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size; 2249f4aad16aSMichael Halcrow encrypted_session_key_valid = 0; 2250f4aad16aSMichael Halcrow for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 2251f4aad16aSMichael Halcrow encrypted_session_key_valid |= 2252f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2253f4aad16aSMichael Halcrow if (encrypted_session_key_valid) { 2254f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 2255f4aad16aSMichael Halcrow "using auth_tok->session_key.encrypted_key, " 2256f24b3887STyler Hicks "where key_rec->enc_key_size = [%zd]\n", 2257f4aad16aSMichael Halcrow key_rec->enc_key_size); 2258f4aad16aSMichael Halcrow memcpy(key_rec->enc_key, 2259f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key, 2260f4aad16aSMichael Halcrow key_rec->enc_key_size); 2261f4aad16aSMichael Halcrow goto encrypted_session_key_set; 2262f4aad16aSMichael Halcrow } 2263dddfa461SMichael Halcrow if (auth_tok->token.password.flags & 2264dddfa461SMichael Halcrow ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 2265237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Using previously generated " 2266237fead6SMichael Halcrow "session key encryption key of size [%d]\n", 2267237fead6SMichael Halcrow auth_tok->token.password. 2268237fead6SMichael Halcrow session_key_encryption_key_bytes); 2269237fead6SMichael Halcrow memcpy(session_key_encryption_key, 2270237fead6SMichael Halcrow auth_tok->token.password.session_key_encryption_key, 2271237fead6SMichael Halcrow crypt_stat->key_size); 2272237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 2273df2e301fSJean Delvare "Cached session key encryption key:\n"); 2274237fead6SMichael Halcrow if (ecryptfs_verbosity > 0) 2275237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2276237fead6SMichael Halcrow } 2277237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 2278237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 2279237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2280237fead6SMichael Halcrow } 22815dda6992SMichael Halcrow rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, 2282ac97b9f9SMichael Halcrow src_sg, 2); 2283ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2284237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2285f4aad16aSMichael Halcrow "for crypt_stat session key; expected rc = 1; " 2286f24b3887STyler Hicks "got rc = [%d]. key_rec->enc_key_size = [%zd]\n", 2287f4aad16aSMichael Halcrow rc, key_rec->enc_key_size); 2288237fead6SMichael Halcrow rc = -ENOMEM; 2289237fead6SMichael Halcrow goto out; 2290237fead6SMichael Halcrow } 22915dda6992SMichael Halcrow rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, 2292ac97b9f9SMichael Halcrow dst_sg, 2); 2293ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2294237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2295f4aad16aSMichael Halcrow "for crypt_stat encrypted session key; " 2296f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 2297f24b3887STyler Hicks "key_rec->enc_key_size = [%zd]\n", rc, 2298f4aad16aSMichael Halcrow key_rec->enc_key_size); 2299237fead6SMichael Halcrow rc = -ENOMEM; 2300237fead6SMichael Halcrow goto out; 2301237fead6SMichael Halcrow } 2302237fead6SMichael Halcrow mutex_lock(tfm_mutex); 23033095e8e3SHerbert Xu rc = crypto_skcipher_setkey(tfm, session_key_encryption_key, 2304237fead6SMichael Halcrow crypt_stat->key_size); 2305237fead6SMichael Halcrow if (rc < 0) { 2306237fead6SMichael Halcrow mutex_unlock(tfm_mutex); 2307237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 23088bba066fSMichael Halcrow "context; rc = [%d]\n", rc); 2309237fead6SMichael Halcrow goto out; 2310237fead6SMichael Halcrow } 23113095e8e3SHerbert Xu 23123095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 23133095e8e3SHerbert Xu if (!req) { 23143095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 23153095e8e3SHerbert Xu ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst " 23163095e8e3SHerbert Xu "attempting to skcipher_request_alloc for " 23173095e8e3SHerbert Xu "%s\n", crypto_skcipher_driver_name(tfm)); 23183095e8e3SHerbert Xu rc = -ENOMEM; 23193095e8e3SHerbert Xu goto out; 23203095e8e3SHerbert Xu } 23213095e8e3SHerbert Xu 23223095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 23233095e8e3SHerbert Xu NULL, NULL); 23243095e8e3SHerbert Xu 2325237fead6SMichael Halcrow rc = 0; 2326f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n", 2327237fead6SMichael Halcrow crypt_stat->key_size); 23283095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 23293095e8e3SHerbert Xu (*key_rec).enc_key_size, NULL); 23303095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(req); 2331f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 23323095e8e3SHerbert Xu skcipher_request_free(req); 23338bba066fSMichael Halcrow if (rc) { 23348bba066fSMichael Halcrow printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 23358bba066fSMichael Halcrow goto out; 23368bba066fSMichael Halcrow } 2337237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 2338f4aad16aSMichael Halcrow if (ecryptfs_verbosity > 0) { 2339f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n", 2340f4aad16aSMichael Halcrow key_rec->enc_key_size); 2341dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, 2342dddfa461SMichael Halcrow key_rec->enc_key_size); 2343f4aad16aSMichael Halcrow } 2344237fead6SMichael Halcrow encrypted_session_key_set: 2345237fead6SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2346237fead6SMichael Halcrow * packet tag 3 */ 2347f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 3 identifier */ 2348f4aad16aSMichael Halcrow + 3 /* Max Tag 3 packet size */ 2349f4aad16aSMichael Halcrow + 1 /* Version */ 2350f4aad16aSMichael Halcrow + 1 /* Cipher code */ 2351f4aad16aSMichael Halcrow + 1 /* S2K specifier */ 2352f4aad16aSMichael Halcrow + 1 /* Hash identifier */ 2353f4aad16aSMichael Halcrow + ECRYPTFS_SALT_SIZE /* Salt */ 2354f4aad16aSMichael Halcrow + 1 /* Hash iterations */ 2355f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2356f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 235781acbcd6SAndrew Morton printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " 235881acbcd6SAndrew Morton "there are only [%td] available\n", max_packet_size, 2359f4aad16aSMichael Halcrow (*remaining_bytes)); 2360f4aad16aSMichael Halcrow rc = -EINVAL; 2361f4aad16aSMichael Halcrow goto out; 2362f4aad16aSMichael Halcrow } 2363237fead6SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 2364f4aad16aSMichael Halcrow /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 2365f4aad16aSMichael Halcrow * to get the number of octets in the actual Tag 3 packet */ 2366f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2367f66e883eSMichael Halcrow (max_packet_size - 4), 2368237fead6SMichael Halcrow &packet_size_length); 2369237fead6SMichael Halcrow if (rc) { 2370f4aad16aSMichael Halcrow printk(KERN_ERR "Error generating tag 3 packet header; cannot " 2371f4aad16aSMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2372237fead6SMichael Halcrow goto out; 2373237fead6SMichael Halcrow } 2374237fead6SMichael Halcrow (*packet_size) += packet_size_length; 2375237fead6SMichael Halcrow dest[(*packet_size)++] = 0x04; /* version 4 */ 2376f4aad16aSMichael Halcrow /* TODO: Break from RFC2440 so that arbitrary ciphers can be 2377f4aad16aSMichael Halcrow * specified with strings */ 23789c79f34fSMichael Halcrow cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, 23799c79f34fSMichael Halcrow crypt_stat->key_size); 2380237fead6SMichael Halcrow if (cipher_code == 0) { 2381237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 2382237fead6SMichael Halcrow "cipher [%s]\n", crypt_stat->cipher); 2383237fead6SMichael Halcrow rc = -EINVAL; 2384237fead6SMichael Halcrow goto out; 2385237fead6SMichael Halcrow } 2386237fead6SMichael Halcrow dest[(*packet_size)++] = cipher_code; 2387237fead6SMichael Halcrow dest[(*packet_size)++] = 0x03; /* S2K */ 2388237fead6SMichael Halcrow dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 2389237fead6SMichael Halcrow memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 2390237fead6SMichael Halcrow ECRYPTFS_SALT_SIZE); 2391237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 2392237fead6SMichael Halcrow dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 2393dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2394dddfa461SMichael Halcrow key_rec->enc_key_size); 2395dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2396237fead6SMichael Halcrow out: 2397237fead6SMichael Halcrow if (rc) 2398237fead6SMichael Halcrow (*packet_size) = 0; 2399f4aad16aSMichael Halcrow else 2400f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2401237fead6SMichael Halcrow return rc; 2402237fead6SMichael Halcrow } 2403237fead6SMichael Halcrow 2404eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache; 2405eb95e7ffSMichael Halcrow 2406237fead6SMichael Halcrow /** 2407237fead6SMichael Halcrow * ecryptfs_generate_key_packet_set 240822e78fafSMichael Halcrow * @dest_base: Virtual address from which to write the key record set 2409237fead6SMichael Halcrow * @crypt_stat: The cryptographic context from which the 2410237fead6SMichael Halcrow * authentication tokens will be retrieved 2411237fead6SMichael Halcrow * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 2412237fead6SMichael Halcrow * for the global parameters 2413237fead6SMichael Halcrow * @len: The amount written 2414237fead6SMichael Halcrow * @max: The maximum amount of data allowed to be written 2415237fead6SMichael Halcrow * 2416237fead6SMichael Halcrow * Generates a key packet set and writes it to the virtual address 2417237fead6SMichael Halcrow * passed in. 2418237fead6SMichael Halcrow * 2419237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2420237fead6SMichael Halcrow */ 2421237fead6SMichael Halcrow int 2422237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base, 2423237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2424237fead6SMichael Halcrow struct dentry *ecryptfs_dentry, size_t *len, 2425237fead6SMichael Halcrow size_t max) 2426237fead6SMichael Halcrow { 2427237fead6SMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 24280e1fc5efSRoberto Sassu struct key *auth_tok_key = NULL; 2429237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2430237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 2431237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 2432237fead6SMichael Halcrow size_t written; 2433eb95e7ffSMichael Halcrow struct ecryptfs_key_record *key_rec; 2434f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig; 2435dddfa461SMichael Halcrow int rc = 0; 2436237fead6SMichael Halcrow 2437237fead6SMichael Halcrow (*len) = 0; 2438f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 2439eb95e7ffSMichael Halcrow key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 2440eb95e7ffSMichael Halcrow if (!key_rec) { 2441eb95e7ffSMichael Halcrow rc = -ENOMEM; 2442eb95e7ffSMichael Halcrow goto out; 2443eb95e7ffSMichael Halcrow } 2444f4aad16aSMichael Halcrow list_for_each_entry(key_sig, &crypt_stat->keysig_list, 2445f4aad16aSMichael Halcrow crypt_stat_list) { 2446f4aad16aSMichael Halcrow memset(key_rec, 0, sizeof(*key_rec)); 24470e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key, 24480e1fc5efSRoberto Sassu &auth_tok, 2449f4aad16aSMichael Halcrow mount_crypt_stat, 2450f4aad16aSMichael Halcrow key_sig->keysig); 2451f4aad16aSMichael Halcrow if (rc) { 24520e1fc5efSRoberto Sassu printk(KERN_WARNING "Unable to retrieve auth tok with " 24530e1fc5efSRoberto Sassu "sig = [%s]\n", key_sig->keysig); 24540e1fc5efSRoberto Sassu rc = process_find_global_auth_tok_for_sig_err(rc); 2455f4aad16aSMichael Halcrow goto out_free; 2456f4aad16aSMichael Halcrow } 2457237fead6SMichael Halcrow if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 2458237fead6SMichael Halcrow rc = write_tag_3_packet((dest_base + (*len)), 2459f4aad16aSMichael Halcrow &max, auth_tok, 2460eb95e7ffSMichael Halcrow crypt_stat, key_rec, 2461237fead6SMichael Halcrow &written); 2462b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2463b2987a5eSTyler Hicks key_put(auth_tok_key); 2464237fead6SMichael Halcrow if (rc) { 2465237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2466237fead6SMichael Halcrow "writing tag 3 packet\n"); 2467eb95e7ffSMichael Halcrow goto out_free; 2468237fead6SMichael Halcrow } 2469237fead6SMichael Halcrow (*len) += written; 2470237fead6SMichael Halcrow /* Write auth tok signature packet */ 2471f4aad16aSMichael Halcrow rc = write_tag_11_packet((dest_base + (*len)), &max, 2472f4aad16aSMichael Halcrow key_rec->sig, 2473f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE, &written); 2474237fead6SMichael Halcrow if (rc) { 2475237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing " 2476237fead6SMichael Halcrow "auth tok signature packet\n"); 2477eb95e7ffSMichael Halcrow goto out_free; 2478237fead6SMichael Halcrow } 2479237fead6SMichael Halcrow (*len) += written; 2480dddfa461SMichael Halcrow } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 2481b2987a5eSTyler Hicks rc = write_tag_1_packet(dest_base + (*len), &max, 2482b2987a5eSTyler Hicks auth_tok_key, auth_tok, 2483f4aad16aSMichael Halcrow crypt_stat, key_rec, &written); 2484dddfa461SMichael Halcrow if (rc) { 2485dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2486dddfa461SMichael Halcrow "writing tag 1 packet\n"); 2487eb95e7ffSMichael Halcrow goto out_free; 2488dddfa461SMichael Halcrow } 2489dddfa461SMichael Halcrow (*len) += written; 2490237fead6SMichael Halcrow } else { 2491b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2492b2987a5eSTyler Hicks key_put(auth_tok_key); 2493237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unsupported " 2494237fead6SMichael Halcrow "authentication token type\n"); 2495237fead6SMichael Halcrow rc = -EINVAL; 2496eb95e7ffSMichael Halcrow goto out_free; 2497237fead6SMichael Halcrow } 2498f4aad16aSMichael Halcrow } 2499f4aad16aSMichael Halcrow if (likely(max > 0)) { 2500237fead6SMichael Halcrow dest_base[(*len)] = 0x00; 2501237fead6SMichael Halcrow } else { 2502237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 2503237fead6SMichael Halcrow rc = -EIO; 2504237fead6SMichael Halcrow } 2505eb95e7ffSMichael Halcrow out_free: 2506eb95e7ffSMichael Halcrow kmem_cache_free(ecryptfs_key_record_cache, key_rec); 2507237fead6SMichael Halcrow out: 2508237fead6SMichael Halcrow if (rc) 2509237fead6SMichael Halcrow (*len) = 0; 2510f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 2511237fead6SMichael Halcrow return rc; 2512237fead6SMichael Halcrow } 2513f4aad16aSMichael Halcrow 2514f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache; 2515f4aad16aSMichael Halcrow 2516f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 2517f4aad16aSMichael Halcrow { 2518f4aad16aSMichael Halcrow struct ecryptfs_key_sig *new_key_sig; 2519f4aad16aSMichael Halcrow 2520f4aad16aSMichael Halcrow new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 2521f4aad16aSMichael Halcrow if (!new_key_sig) { 2522f4aad16aSMichael Halcrow printk(KERN_ERR 2523f4aad16aSMichael Halcrow "Error allocating from ecryptfs_key_sig_cache\n"); 2524aa06117fSRoland Dreier return -ENOMEM; 2525f4aad16aSMichael Halcrow } 2526f4aad16aSMichael Halcrow memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 25277762e230SRoberto Sassu new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2528aa06117fSRoland Dreier /* Caller must hold keysig_list_mutex */ 2529f4aad16aSMichael Halcrow list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 2530aa06117fSRoland Dreier 2531aa06117fSRoland Dreier return 0; 2532f4aad16aSMichael Halcrow } 2533f4aad16aSMichael Halcrow 2534f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache; 2535f4aad16aSMichael Halcrow 2536f4aad16aSMichael Halcrow int 2537f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 253884814d64STyler Hicks char *sig, u32 global_auth_tok_flags) 2539f4aad16aSMichael Halcrow { 2540f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *new_auth_tok; 2541f4aad16aSMichael Halcrow int rc = 0; 2542f4aad16aSMichael Halcrow 2543459e2164SEric Sandeen new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, 2544f4aad16aSMichael Halcrow GFP_KERNEL); 2545f4aad16aSMichael Halcrow if (!new_auth_tok) { 2546f4aad16aSMichael Halcrow rc = -ENOMEM; 2547f4aad16aSMichael Halcrow printk(KERN_ERR "Error allocating from " 2548f4aad16aSMichael Halcrow "ecryptfs_global_auth_tok_cache\n"); 2549f4aad16aSMichael Halcrow goto out; 2550f4aad16aSMichael Halcrow } 2551f4aad16aSMichael Halcrow memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 255284814d64STyler Hicks new_auth_tok->flags = global_auth_tok_flags; 2553f4aad16aSMichael Halcrow new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2554f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 2555f4aad16aSMichael Halcrow list_add(&new_auth_tok->mount_crypt_stat_list, 2556f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list); 2557f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 2558f4aad16aSMichael Halcrow out: 2559f4aad16aSMichael Halcrow return rc; 2560f4aad16aSMichael Halcrow } 2561f4aad16aSMichael Halcrow 2562