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 * 462f66665c0SEric Biggers * Returns zero on valid auth tok; -EINVAL if the payload is invalid; or 463f66665c0SEric Biggers * -EKEYREVOKED if the key was revoked before we acquired its semaphore. 4640e1fc5efSRoberto Sassu */ 4650e1fc5efSRoberto Sassu static int 4660e1fc5efSRoberto Sassu ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key, 4670e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok) 4680e1fc5efSRoberto Sassu { 4690e1fc5efSRoberto Sassu int rc = 0; 4700e1fc5efSRoberto Sassu 4710e1fc5efSRoberto Sassu (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key); 472f66665c0SEric Biggers if (IS_ERR(*auth_tok)) { 473f66665c0SEric Biggers rc = PTR_ERR(*auth_tok); 474f66665c0SEric Biggers *auth_tok = NULL; 475f66665c0SEric Biggers goto out; 476f66665c0SEric Biggers } 477f66665c0SEric Biggers 4780e1fc5efSRoberto Sassu if (ecryptfs_verify_version((*auth_tok)->version)) { 4790e1fc5efSRoberto Sassu printk(KERN_ERR "Data structure version mismatch. Userspace " 4800e1fc5efSRoberto Sassu "tools must match eCryptfs kernel module with major " 4810e1fc5efSRoberto Sassu "version [%d] and minor version [%d]\n", 4820e1fc5efSRoberto Sassu ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); 4830e1fc5efSRoberto Sassu rc = -EINVAL; 4840e1fc5efSRoberto Sassu goto out; 4850e1fc5efSRoberto Sassu } 4860e1fc5efSRoberto Sassu if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD 4870e1fc5efSRoberto Sassu && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { 4880e1fc5efSRoberto Sassu printk(KERN_ERR "Invalid auth_tok structure " 4890e1fc5efSRoberto Sassu "returned from key query\n"); 4900e1fc5efSRoberto Sassu rc = -EINVAL; 4910e1fc5efSRoberto Sassu goto out; 4920e1fc5efSRoberto Sassu } 4930e1fc5efSRoberto Sassu out: 4940e1fc5efSRoberto Sassu return rc; 4950e1fc5efSRoberto Sassu } 4960e1fc5efSRoberto Sassu 497cd9d67dfSMichael Halcrow static int 4989c79f34fSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig( 4990e1fc5efSRoberto Sassu struct key **auth_tok_key, 5000e1fc5efSRoberto Sassu struct ecryptfs_auth_tok **auth_tok, 5019c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) 5029c79f34fSMichael Halcrow { 5039c79f34fSMichael Halcrow struct ecryptfs_global_auth_tok *walker; 5049c79f34fSMichael Halcrow int rc = 0; 5059c79f34fSMichael Halcrow 5060e1fc5efSRoberto Sassu (*auth_tok_key) = NULL; 5070e1fc5efSRoberto Sassu (*auth_tok) = NULL; 5089c79f34fSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 5099c79f34fSMichael Halcrow list_for_each_entry(walker, 5109c79f34fSMichael Halcrow &mount_crypt_stat->global_auth_tok_list, 5119c79f34fSMichael Halcrow mount_crypt_stat_list) { 5120e1fc5efSRoberto Sassu if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX)) 5130e1fc5efSRoberto Sassu continue; 5140e1fc5efSRoberto Sassu 5150e1fc5efSRoberto Sassu if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) { 5160e1fc5efSRoberto Sassu rc = -EINVAL; 5179c79f34fSMichael Halcrow goto out; 5189c79f34fSMichael Halcrow } 5190e1fc5efSRoberto Sassu 5200e1fc5efSRoberto Sassu rc = key_validate(walker->global_auth_tok_key); 5210e1fc5efSRoberto Sassu if (rc) { 5220e1fc5efSRoberto Sassu if (rc == -EKEYEXPIRED) 5230e1fc5efSRoberto Sassu goto out; 5240e1fc5efSRoberto Sassu goto out_invalid_auth_tok; 5259c79f34fSMichael Halcrow } 5260e1fc5efSRoberto Sassu 527b5695d04SRoberto Sassu down_write(&(walker->global_auth_tok_key->sem)); 5280e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key( 5290e1fc5efSRoberto Sassu walker->global_auth_tok_key, auth_tok); 5300e1fc5efSRoberto Sassu if (rc) 531b5695d04SRoberto Sassu goto out_invalid_auth_tok_unlock; 5320e1fc5efSRoberto Sassu 5330e1fc5efSRoberto Sassu (*auth_tok_key) = walker->global_auth_tok_key; 5340e1fc5efSRoberto Sassu key_get(*auth_tok_key); 5350e1fc5efSRoberto Sassu goto out; 5360e1fc5efSRoberto Sassu } 5370e1fc5efSRoberto Sassu rc = -ENOENT; 5380e1fc5efSRoberto Sassu goto out; 539b5695d04SRoberto Sassu out_invalid_auth_tok_unlock: 540b5695d04SRoberto Sassu up_write(&(walker->global_auth_tok_key->sem)); 5410e1fc5efSRoberto Sassu out_invalid_auth_tok: 5420e1fc5efSRoberto Sassu printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig); 5430e1fc5efSRoberto Sassu walker->flags |= ECRYPTFS_AUTH_TOK_INVALID; 5440e1fc5efSRoberto Sassu key_put(walker->global_auth_tok_key); 5450e1fc5efSRoberto Sassu walker->global_auth_tok_key = NULL; 5469c79f34fSMichael Halcrow out: 5479c79f34fSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 5489c79f34fSMichael Halcrow return rc; 5499c79f34fSMichael Halcrow } 5509c79f34fSMichael Halcrow 5519c79f34fSMichael Halcrow /** 5529c79f34fSMichael Halcrow * ecryptfs_find_auth_tok_for_sig 5539c79f34fSMichael Halcrow * @auth_tok: Set to the matching auth_tok; NULL if not found 5549c79f34fSMichael Halcrow * @crypt_stat: inode crypt_stat crypto context 5559c79f34fSMichael Halcrow * @sig: Sig of auth_tok to find 5569c79f34fSMichael Halcrow * 5579c79f34fSMichael Halcrow * For now, this function simply looks at the registered auth_tok's 5589c79f34fSMichael Halcrow * linked off the mount_crypt_stat, so all the auth_toks that can be 5599c79f34fSMichael Halcrow * used must be registered at mount time. This function could 5609c79f34fSMichael Halcrow * potentially try a lot harder to find auth_tok's (e.g., by calling 5619c79f34fSMichael Halcrow * out to ecryptfsd to dynamically retrieve an auth_tok object) so 5629c79f34fSMichael Halcrow * that static registration of auth_tok's will no longer be necessary. 5639c79f34fSMichael Halcrow * 5649c79f34fSMichael Halcrow * Returns zero on no error; non-zero on error 5659c79f34fSMichael Halcrow */ 5669c79f34fSMichael Halcrow static int 5679c79f34fSMichael Halcrow ecryptfs_find_auth_tok_for_sig( 568aee683b9SRoberto Sassu struct key **auth_tok_key, 5699c79f34fSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 5709c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 5719c79f34fSMichael Halcrow char *sig) 5729c79f34fSMichael Halcrow { 5739c79f34fSMichael Halcrow int rc = 0; 5749c79f34fSMichael Halcrow 5750e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok, 5760e1fc5efSRoberto Sassu mount_crypt_stat, sig); 5770e1fc5efSRoberto Sassu if (rc == -ENOENT) { 578f16feb51SRoberto Sassu /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the 579f16feb51SRoberto Sassu * mount_crypt_stat structure, we prevent to use auth toks that 580f16feb51SRoberto Sassu * are not inserted through the ecryptfs_add_global_auth_tok 581f16feb51SRoberto Sassu * function. 582f16feb51SRoberto Sassu */ 583f16feb51SRoberto Sassu if (mount_crypt_stat->flags 584f16feb51SRoberto Sassu & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) 585f16feb51SRoberto Sassu return -EINVAL; 586f16feb51SRoberto Sassu 587aee683b9SRoberto Sassu rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, 5889c79f34fSMichael Halcrow sig); 5890e1fc5efSRoberto Sassu } 5909c79f34fSMichael Halcrow return rc; 5919c79f34fSMichael Halcrow } 5929c79f34fSMichael Halcrow 5939c79f34fSMichael Halcrow /** 5949c79f34fSMichael Halcrow * write_tag_70_packet can gobble a lot of stack space. We stuff most 5959c79f34fSMichael Halcrow * of the function's parameters in a kmalloc'd struct to help reduce 5969c79f34fSMichael Halcrow * eCryptfs' overall stack usage. 5979c79f34fSMichael Halcrow */ 5989c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack { 5999c79f34fSMichael Halcrow u8 cipher_code; 6009c79f34fSMichael Halcrow size_t max_packet_size; 6019c79f34fSMichael Halcrow size_t packet_size_len; 6029c79f34fSMichael Halcrow size_t block_aligned_filename_size; 6039c79f34fSMichael Halcrow size_t block_size; 6049c79f34fSMichael Halcrow size_t i; 6059c79f34fSMichael Halcrow size_t j; 6069c79f34fSMichael Halcrow size_t num_rand_bytes; 6079c79f34fSMichael Halcrow struct mutex *tfm_mutex; 6089c79f34fSMichael Halcrow char *block_aligned_filename; 6099c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 6108d08dab7STyler Hicks struct scatterlist src_sg[2]; 6118d08dab7STyler Hicks struct scatterlist dst_sg[2]; 6123095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 6133095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 6149c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 6159c79f34fSMichael Halcrow char hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6169c79f34fSMichael Halcrow char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; 6173095e8e3SHerbert Xu struct crypto_shash *hash_tfm; 6183095e8e3SHerbert Xu struct shash_desc *hash_desc; 6199c79f34fSMichael Halcrow }; 6209c79f34fSMichael Halcrow 6219c79f34fSMichael Halcrow /** 6229c79f34fSMichael Halcrow * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK 6239c79f34fSMichael Halcrow * @filename: NULL-terminated filename string 6249c79f34fSMichael Halcrow * 6259c79f34fSMichael Halcrow * This is the simplest mechanism for achieving filename encryption in 6269c79f34fSMichael Halcrow * eCryptfs. It encrypts the given filename with the mount-wide 6279c79f34fSMichael Halcrow * filename encryption key (FNEK) and stores it in a packet to @dest, 6289c79f34fSMichael Halcrow * which the callee will encode and write directly into the dentry 6299c79f34fSMichael Halcrow * name. 6309c79f34fSMichael Halcrow */ 6319c79f34fSMichael Halcrow int 6329c79f34fSMichael Halcrow ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, 6339c79f34fSMichael Halcrow size_t *packet_size, 6349c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 6359c79f34fSMichael Halcrow char *filename, size_t filename_size) 6369c79f34fSMichael Halcrow { 6379c79f34fSMichael Halcrow struct ecryptfs_write_tag_70_packet_silly_stack *s; 638aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 6399c79f34fSMichael Halcrow int rc = 0; 6409c79f34fSMichael Halcrow 6413095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 642*1a0bba4fSMarkus Elfring if (!s) 643d1558f4eSHerbert Xu return -ENOMEM; 644*1a0bba4fSMarkus Elfring 6459c79f34fSMichael Halcrow (*packet_size) = 0; 646950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig( 647950983fcSRoberto Sassu &auth_tok_key, 648950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 649950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig); 650950983fcSRoberto Sassu if (rc) { 651950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 652950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, 653950983fcSRoberto Sassu mount_crypt_stat->global_default_fnek_sig, rc); 654950983fcSRoberto Sassu goto out; 655950983fcSRoberto Sassu } 6569c79f34fSMichael Halcrow rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( 6573095e8e3SHerbert Xu &s->skcipher_tfm, 6589c79f34fSMichael Halcrow &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); 6599c79f34fSMichael Halcrow if (unlikely(rc)) { 6609c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 6619c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 6629c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, rc); 6639c79f34fSMichael Halcrow goto out; 6649c79f34fSMichael Halcrow } 6659c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 6663095e8e3SHerbert Xu s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm); 6679c79f34fSMichael Halcrow /* Plus one for the \0 separator between the random prefix 6689c79f34fSMichael Halcrow * and the plaintext filename */ 6699c79f34fSMichael Halcrow s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); 6709c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); 6719c79f34fSMichael Halcrow if ((s->block_aligned_filename_size % s->block_size) != 0) { 6729c79f34fSMichael Halcrow s->num_rand_bytes += (s->block_size 6739c79f34fSMichael Halcrow - (s->block_aligned_filename_size 6749c79f34fSMichael Halcrow % s->block_size)); 6759c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->num_rand_bytes 6769c79f34fSMichael Halcrow + filename_size); 6779c79f34fSMichael Halcrow } 6789c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 6799c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 6809c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 6819c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 6829c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 6839c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 6849c79f34fSMichael Halcrow * - Consists of a minimum number of random characters, a \0 6859c79f34fSMichael Halcrow * separator, and then the filename */ 6864a26620dSTyler Hicks s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE 6879c79f34fSMichael Halcrow + s->block_aligned_filename_size); 6889c79f34fSMichael Halcrow if (dest == NULL) { 6899c79f34fSMichael Halcrow (*packet_size) = s->max_packet_size; 6909c79f34fSMichael Halcrow goto out_unlock; 6919c79f34fSMichael Halcrow } 6929c79f34fSMichael Halcrow if (s->max_packet_size > (*remaining_bytes)) { 693a8f12864SMichael Halcrow printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " 694a8f12864SMichael Halcrow "[%zd] available\n", __func__, s->max_packet_size, 6959c79f34fSMichael Halcrow (*remaining_bytes)); 6969c79f34fSMichael Halcrow rc = -EINVAL; 6979c79f34fSMichael Halcrow goto out_unlock; 6989c79f34fSMichael Halcrow } 6993095e8e3SHerbert Xu 7003095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 7013095e8e3SHerbert Xu if (!s->skcipher_req) { 7023095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 7033095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 7043095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 7053095e8e3SHerbert Xu rc = -ENOMEM; 7063095e8e3SHerbert Xu goto out_unlock; 7073095e8e3SHerbert Xu } 7083095e8e3SHerbert Xu 7093095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 7103095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 7113095e8e3SHerbert Xu 7129c79f34fSMichael Halcrow s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, 7139c79f34fSMichael Halcrow GFP_KERNEL); 7149c79f34fSMichael Halcrow if (!s->block_aligned_filename) { 7159c79f34fSMichael Halcrow rc = -ENOMEM; 7169c79f34fSMichael Halcrow goto out_unlock; 7179c79f34fSMichael Halcrow } 7189c79f34fSMichael Halcrow dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; 7199c79f34fSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[s->i], 7209c79f34fSMichael Halcrow (ECRYPTFS_SIG_SIZE 7219c79f34fSMichael Halcrow + 1 /* Cipher code */ 7229c79f34fSMichael Halcrow + s->block_aligned_filename_size), 7239c79f34fSMichael Halcrow &s->packet_size_len); 7249c79f34fSMichael Halcrow if (rc) { 7259c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error generating tag 70 packet " 7269c79f34fSMichael Halcrow "header; cannot generate packet length; rc = [%d]\n", 7279c79f34fSMichael Halcrow __func__, rc); 7289c79f34fSMichael Halcrow goto out_free_unlock; 7299c79f34fSMichael Halcrow } 7309c79f34fSMichael Halcrow s->i += s->packet_size_len; 7319c79f34fSMichael Halcrow ecryptfs_from_hex(&dest[s->i], 7329c79f34fSMichael Halcrow mount_crypt_stat->global_default_fnek_sig, 7339c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 7349c79f34fSMichael Halcrow s->i += ECRYPTFS_SIG_SIZE; 7359c79f34fSMichael Halcrow s->cipher_code = ecryptfs_code_for_cipher_string( 7369c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7379c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7389c79f34fSMichael Halcrow if (s->cipher_code == 0) { 7399c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Unable to generate code for " 740a8f12864SMichael Halcrow "cipher [%s] with key bytes [%zd]\n", __func__, 7419c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_name, 7429c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 7439c79f34fSMichael Halcrow rc = -EINVAL; 7449c79f34fSMichael Halcrow goto out_free_unlock; 7459c79f34fSMichael Halcrow } 7469c79f34fSMichael Halcrow dest[s->i++] = s->cipher_code; 7479c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 7489c79f34fSMichael Halcrow * filename encryption */ 749df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 750df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 751df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 752df6ad33bSTyler Hicks "password tokens\n", __func__); 753df6ad33bSTyler Hicks goto out_free_unlock; 754df6ad33bSTyler Hicks } 7553095e8e3SHerbert Xu s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0); 7563095e8e3SHerbert Xu if (IS_ERR(s->hash_tfm)) { 7573095e8e3SHerbert Xu rc = PTR_ERR(s->hash_tfm); 7589c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to " 7599c79f34fSMichael Halcrow "allocate hash crypto context; rc = [%d]\n", 7609c79f34fSMichael Halcrow __func__, rc); 7619c79f34fSMichael Halcrow goto out_free_unlock; 7629c79f34fSMichael Halcrow } 7633095e8e3SHerbert Xu 7643095e8e3SHerbert Xu s->hash_desc = kmalloc(sizeof(*s->hash_desc) + 7653095e8e3SHerbert Xu crypto_shash_descsize(s->hash_tfm), GFP_KERNEL); 7663095e8e3SHerbert Xu if (!s->hash_desc) { 7673095e8e3SHerbert Xu rc = -ENOMEM; 7689c79f34fSMichael Halcrow goto out_release_free_unlock; 7699c79f34fSMichael Halcrow } 7703095e8e3SHerbert Xu 7713095e8e3SHerbert Xu s->hash_desc->tfm = s->hash_tfm; 7723095e8e3SHerbert Xu s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; 7733095e8e3SHerbert Xu 7743095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, 7753095e8e3SHerbert Xu (u8 *)s->auth_tok->token.password.session_key_encryption_key, 7763095e8e3SHerbert Xu s->auth_tok->token.password.session_key_encryption_key_bytes, 7773095e8e3SHerbert Xu s->hash); 7789c79f34fSMichael Halcrow if (rc) { 7799c79f34fSMichael Halcrow printk(KERN_ERR 7803095e8e3SHerbert Xu "%s: Error computing crypto hash; rc = [%d]\n", 7819c79f34fSMichael Halcrow __func__, rc); 7829c79f34fSMichael Halcrow goto out_release_free_unlock; 7839c79f34fSMichael Halcrow } 7849c79f34fSMichael Halcrow for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { 7859c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = 7869c79f34fSMichael Halcrow s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)]; 7879c79f34fSMichael Halcrow if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE) 7889c79f34fSMichael Halcrow == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) { 7893095e8e3SHerbert Xu rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash, 7903095e8e3SHerbert Xu ECRYPTFS_TAG_70_DIGEST_SIZE, 7913095e8e3SHerbert Xu s->tmp_hash); 7929c79f34fSMichael Halcrow if (rc) { 7939c79f34fSMichael Halcrow printk(KERN_ERR 7943095e8e3SHerbert Xu "%s: Error computing crypto hash; " 7959c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 7969c79f34fSMichael Halcrow goto out_release_free_unlock; 7979c79f34fSMichael Halcrow } 7989c79f34fSMichael Halcrow memcpy(s->hash, s->tmp_hash, 7999c79f34fSMichael Halcrow ECRYPTFS_TAG_70_DIGEST_SIZE); 8009c79f34fSMichael Halcrow } 8019c79f34fSMichael Halcrow if (s->block_aligned_filename[s->j] == '\0') 8029c79f34fSMichael Halcrow s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; 8039c79f34fSMichael Halcrow } 8049c79f34fSMichael Halcrow memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, 8059c79f34fSMichael Halcrow filename_size); 8069c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->block_aligned_filename, 8078d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 8088d08dab7STyler Hicks if (rc < 1) { 8099c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8108d08dab7STyler Hicks "convert filename memory to scatterlist; rc = [%d]. " 811a8f12864SMichael Halcrow "block_aligned_filename_size = [%zd]\n", __func__, rc, 8129c79f34fSMichael Halcrow s->block_aligned_filename_size); 8139c79f34fSMichael Halcrow goto out_release_free_unlock; 8149c79f34fSMichael Halcrow } 8159c79f34fSMichael Halcrow rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, 8168d08dab7STyler Hicks s->dst_sg, 2); 8178d08dab7STyler Hicks if (rc < 1) { 8189c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 8199c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 8208d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 8218d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 8229c79f34fSMichael Halcrow goto out_release_free_unlock; 8239c79f34fSMichael Halcrow } 8249c79f34fSMichael Halcrow /* The characters in the first block effectively do the job 8259c79f34fSMichael Halcrow * of the IV here, so we just use 0's for the IV. Note the 8269c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 8279c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 8283095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 8293095e8e3SHerbert Xu s->skcipher_tfm, 8309c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8319c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8329c79f34fSMichael Halcrow if (rc < 0) { 8339c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 8349c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 8359c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 836df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 8379c79f34fSMichael Halcrow rc, 8389c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 8399c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 8409c79f34fSMichael Halcrow goto out_release_free_unlock; 8419c79f34fSMichael Halcrow } 8423095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 8433095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 8443095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(s->skcipher_req); 8459c79f34fSMichael Halcrow if (rc) { 8469c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to encrypt filename; " 8479c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 8489c79f34fSMichael Halcrow goto out_release_free_unlock; 8499c79f34fSMichael Halcrow } 8509c79f34fSMichael Halcrow s->i += s->block_aligned_filename_size; 8519c79f34fSMichael Halcrow (*packet_size) = s->i; 8529c79f34fSMichael Halcrow (*remaining_bytes) -= (*packet_size); 8539c79f34fSMichael Halcrow out_release_free_unlock: 8543095e8e3SHerbert Xu crypto_free_shash(s->hash_tfm); 8559c79f34fSMichael Halcrow out_free_unlock: 85600fcf2cbSJohannes Weiner kzfree(s->block_aligned_filename); 8579c79f34fSMichael Halcrow out_unlock: 8589c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 8599c79f34fSMichael Halcrow out: 860b5695d04SRoberto Sassu if (auth_tok_key) { 861b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 862aee683b9SRoberto Sassu key_put(auth_tok_key); 863b5695d04SRoberto Sassu } 8643095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 8653095e8e3SHerbert Xu kzfree(s->hash_desc); 8669c79f34fSMichael Halcrow kfree(s); 8679c79f34fSMichael Halcrow return rc; 8689c79f34fSMichael Halcrow } 8699c79f34fSMichael Halcrow 8709c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack { 8719c79f34fSMichael Halcrow u8 cipher_code; 8729c79f34fSMichael Halcrow size_t max_packet_size; 8739c79f34fSMichael Halcrow size_t packet_size_len; 8749c79f34fSMichael Halcrow size_t parsed_tag_70_packet_size; 8759c79f34fSMichael Halcrow size_t block_aligned_filename_size; 8769c79f34fSMichael Halcrow size_t block_size; 8779c79f34fSMichael Halcrow size_t i; 8789c79f34fSMichael Halcrow struct mutex *tfm_mutex; 8799c79f34fSMichael Halcrow char *decrypted_filename; 8809c79f34fSMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 8818d08dab7STyler Hicks struct scatterlist src_sg[2]; 8828d08dab7STyler Hicks struct scatterlist dst_sg[2]; 8833095e8e3SHerbert Xu struct crypto_skcipher *skcipher_tfm; 8843095e8e3SHerbert Xu struct skcipher_request *skcipher_req; 8859c79f34fSMichael Halcrow char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; 8869c79f34fSMichael Halcrow char iv[ECRYPTFS_MAX_IV_BYTES]; 8872a559a8bSColin Ian King char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; 8889c79f34fSMichael Halcrow }; 8899c79f34fSMichael Halcrow 8909c79f34fSMichael Halcrow /** 8919c79f34fSMichael Halcrow * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet 8929c79f34fSMichael Halcrow * @filename: This function kmalloc's the memory for the filename 8937d8bc2beSMichael Halcrow * @filename_size: This function sets this to the amount of memory 8947d8bc2beSMichael Halcrow * kmalloc'd for the filename 8957d8bc2beSMichael Halcrow * @packet_size: This function sets this to the the number of octets 8967d8bc2beSMichael Halcrow * in the packet parsed 8977d8bc2beSMichael Halcrow * @mount_crypt_stat: The mount-wide cryptographic context 8987d8bc2beSMichael Halcrow * @data: The memory location containing the start of the tag 70 8997d8bc2beSMichael Halcrow * packet 9007d8bc2beSMichael Halcrow * @max_packet_size: The maximum legal size of the packet to be parsed 9017d8bc2beSMichael Halcrow * from @data 9027d8bc2beSMichael Halcrow * 9037d8bc2beSMichael Halcrow * Returns zero on success; non-zero otherwise 9049c79f34fSMichael Halcrow */ 9059c79f34fSMichael Halcrow int 9069c79f34fSMichael Halcrow ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, 9079c79f34fSMichael Halcrow size_t *packet_size, 9089c79f34fSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 9099c79f34fSMichael Halcrow char *data, size_t max_packet_size) 9109c79f34fSMichael Halcrow { 9119c79f34fSMichael Halcrow struct ecryptfs_parse_tag_70_packet_silly_stack *s; 912aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 9139c79f34fSMichael Halcrow int rc = 0; 9149c79f34fSMichael Halcrow 9159c79f34fSMichael Halcrow (*packet_size) = 0; 9169c79f34fSMichael Halcrow (*filename_size) = 0; 9179c79f34fSMichael Halcrow (*filename) = NULL; 9183095e8e3SHerbert Xu s = kzalloc(sizeof(*s), GFP_KERNEL); 919*1a0bba4fSMarkus Elfring if (!s) 920d1558f4eSHerbert Xu return -ENOMEM; 921*1a0bba4fSMarkus Elfring 9224a26620dSTyler Hicks if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) { 923df261c52SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " 9249c79f34fSMichael Halcrow "at least [%d]\n", __func__, max_packet_size, 9254a26620dSTyler Hicks ECRYPTFS_TAG_70_MIN_METADATA_SIZE); 9269c79f34fSMichael Halcrow rc = -EINVAL; 9279c79f34fSMichael Halcrow goto out; 9289c79f34fSMichael Halcrow } 9299c79f34fSMichael Halcrow /* Octet 0: Tag 70 identifier 9309c79f34fSMichael Halcrow * Octets 1-N1: Tag 70 packet size (includes cipher identifier 9319c79f34fSMichael Halcrow * and block-aligned encrypted filename size) 9329c79f34fSMichael Halcrow * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) 9339c79f34fSMichael Halcrow * Octet N2-N3: Cipher identifier (1 octet) 9349c79f34fSMichael Halcrow * Octets N3-N4: Block-aligned encrypted filename 9359c79f34fSMichael Halcrow * - Consists of a minimum number of random numbers, a \0 9369c79f34fSMichael Halcrow * separator, and then the filename */ 9379c79f34fSMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { 9389c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " 9399c79f34fSMichael Halcrow "tag [0x%.2x]\n", __func__, 9409c79f34fSMichael Halcrow data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); 9419c79f34fSMichael Halcrow rc = -EINVAL; 9429c79f34fSMichael Halcrow goto out; 9439c79f34fSMichael Halcrow } 9449c79f34fSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], 9459c79f34fSMichael Halcrow &s->parsed_tag_70_packet_size, 9469c79f34fSMichael Halcrow &s->packet_size_len); 9479c79f34fSMichael Halcrow if (rc) { 9489c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Error parsing packet length; " 9499c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 9509c79f34fSMichael Halcrow goto out; 9519c79f34fSMichael Halcrow } 9529c79f34fSMichael Halcrow s->block_aligned_filename_size = (s->parsed_tag_70_packet_size 9539c79f34fSMichael Halcrow - ECRYPTFS_SIG_SIZE - 1); 9549c79f34fSMichael Halcrow if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) 9559c79f34fSMichael Halcrow > max_packet_size) { 956a8f12864SMichael Halcrow printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " 957a8f12864SMichael Halcrow "size is [%zd]\n", __func__, max_packet_size, 9589c79f34fSMichael Halcrow (1 + s->packet_size_len + 1 9599c79f34fSMichael Halcrow + s->block_aligned_filename_size)); 9609c79f34fSMichael Halcrow rc = -EINVAL; 9619c79f34fSMichael Halcrow goto out; 9629c79f34fSMichael Halcrow } 9639c79f34fSMichael Halcrow (*packet_size) += s->packet_size_len; 9649c79f34fSMichael Halcrow ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], 9659c79f34fSMichael Halcrow ECRYPTFS_SIG_SIZE); 9669c79f34fSMichael Halcrow s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 9679c79f34fSMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 9689c79f34fSMichael Halcrow s->cipher_code = data[(*packet_size)++]; 9699c79f34fSMichael Halcrow rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code); 9709c79f34fSMichael Halcrow if (rc) { 9719c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", 9729c79f34fSMichael Halcrow __func__, s->cipher_code); 9739c79f34fSMichael Halcrow goto out; 9749c79f34fSMichael Halcrow } 975950983fcSRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 976950983fcSRoberto Sassu &s->auth_tok, mount_crypt_stat, 977950983fcSRoberto Sassu s->fnek_sig_hex); 978950983fcSRoberto Sassu if (rc) { 979950983fcSRoberto Sassu printk(KERN_ERR "%s: Error attempting to find auth tok for " 980950983fcSRoberto Sassu "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, 981950983fcSRoberto Sassu rc); 982950983fcSRoberto Sassu goto out; 983950983fcSRoberto Sassu } 9843095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm, 9859c79f34fSMichael Halcrow &s->tfm_mutex, 9869c79f34fSMichael Halcrow s->cipher_string); 9879c79f34fSMichael Halcrow if (unlikely(rc)) { 9889c79f34fSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 9899c79f34fSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 9909c79f34fSMichael Halcrow s->cipher_string, rc); 9919c79f34fSMichael Halcrow goto out; 9929c79f34fSMichael Halcrow } 9939c79f34fSMichael Halcrow mutex_lock(s->tfm_mutex); 9949c79f34fSMichael Halcrow rc = virt_to_scatterlist(&data[(*packet_size)], 9958d08dab7STyler Hicks s->block_aligned_filename_size, s->src_sg, 2); 9968d08dab7STyler Hicks if (rc < 1) { 9979c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 9989c79f34fSMichael Halcrow "convert encrypted filename memory to scatterlist; " 9998d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10008d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10019c79f34fSMichael Halcrow goto out_unlock; 10029c79f34fSMichael Halcrow } 10039c79f34fSMichael Halcrow (*packet_size) += s->block_aligned_filename_size; 10049c79f34fSMichael Halcrow s->decrypted_filename = kmalloc(s->block_aligned_filename_size, 10059c79f34fSMichael Halcrow GFP_KERNEL); 10069c79f34fSMichael Halcrow if (!s->decrypted_filename) { 10079c79f34fSMichael Halcrow rc = -ENOMEM; 10089c79f34fSMichael Halcrow goto out_unlock; 10099c79f34fSMichael Halcrow } 10109c79f34fSMichael Halcrow rc = virt_to_scatterlist(s->decrypted_filename, 10118d08dab7STyler Hicks s->block_aligned_filename_size, s->dst_sg, 2); 10128d08dab7STyler Hicks if (rc < 1) { 10139c79f34fSMichael Halcrow printk(KERN_ERR "%s: Internal error whilst attempting to " 10149c79f34fSMichael Halcrow "convert decrypted filename memory to scatterlist; " 10158d08dab7STyler Hicks "rc = [%d]. block_aligned_filename_size = [%zd]\n", 10168d08dab7STyler Hicks __func__, rc, s->block_aligned_filename_size); 10179c79f34fSMichael Halcrow goto out_free_unlock; 10189c79f34fSMichael Halcrow } 10193095e8e3SHerbert Xu 10203095e8e3SHerbert Xu s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); 10213095e8e3SHerbert Xu if (!s->skcipher_req) { 10223095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 10233095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 10243095e8e3SHerbert Xu crypto_skcipher_driver_name(s->skcipher_tfm)); 10253095e8e3SHerbert Xu rc = -ENOMEM; 10263095e8e3SHerbert Xu goto out_free_unlock; 10273095e8e3SHerbert Xu } 10283095e8e3SHerbert Xu 10293095e8e3SHerbert Xu skcipher_request_set_callback(s->skcipher_req, 10303095e8e3SHerbert Xu CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); 10313095e8e3SHerbert Xu 10329c79f34fSMichael Halcrow /* The characters in the first block effectively do the job of 10339c79f34fSMichael Halcrow * the IV here, so we just use 0's for the IV. Note the 10349c79f34fSMichael Halcrow * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES 10359c79f34fSMichael Halcrow * >= ECRYPTFS_MAX_IV_BYTES. */ 10369c79f34fSMichael Halcrow /* TODO: Support other key modules than passphrase for 10379c79f34fSMichael Halcrow * filename encryption */ 1038df6ad33bSTyler Hicks if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { 1039df6ad33bSTyler Hicks rc = -EOPNOTSUPP; 1040df6ad33bSTyler Hicks printk(KERN_INFO "%s: Filename encryption only supports " 1041df6ad33bSTyler Hicks "password tokens\n", __func__); 1042df6ad33bSTyler Hicks goto out_free_unlock; 1043df6ad33bSTyler Hicks } 10443095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 10453095e8e3SHerbert Xu s->skcipher_tfm, 10469c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10479c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10489c79f34fSMichael Halcrow if (rc < 0) { 10499c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error setting key for crypto context; " 10509c79f34fSMichael Halcrow "rc = [%d]. s->auth_tok->token.password.session_key_" 10519c79f34fSMichael Halcrow "encryption_key = [0x%p]; mount_crypt_stat->" 1052df261c52SMichael Halcrow "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, 10539c79f34fSMichael Halcrow rc, 10549c79f34fSMichael Halcrow s->auth_tok->token.password.session_key_encryption_key, 10559c79f34fSMichael Halcrow mount_crypt_stat->global_default_fn_cipher_key_bytes); 10569c79f34fSMichael Halcrow goto out_free_unlock; 10579c79f34fSMichael Halcrow } 10583095e8e3SHerbert Xu skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, 10593095e8e3SHerbert Xu s->block_aligned_filename_size, s->iv); 10603095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(s->skcipher_req); 10619c79f34fSMichael Halcrow if (rc) { 10629c79f34fSMichael Halcrow printk(KERN_ERR "%s: Error attempting to decrypt filename; " 10639c79f34fSMichael Halcrow "rc = [%d]\n", __func__, rc); 10649c79f34fSMichael Halcrow goto out_free_unlock; 10659c79f34fSMichael Halcrow } 10669c79f34fSMichael Halcrow while (s->decrypted_filename[s->i] != '\0' 10679c79f34fSMichael Halcrow && s->i < s->block_aligned_filename_size) 10689c79f34fSMichael Halcrow s->i++; 10699c79f34fSMichael Halcrow if (s->i == s->block_aligned_filename_size) { 10709c79f34fSMichael Halcrow printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " 10719c79f34fSMichael Halcrow "find valid separator between random characters and " 10729c79f34fSMichael Halcrow "the filename\n", __func__); 10739c79f34fSMichael Halcrow rc = -EINVAL; 10749c79f34fSMichael Halcrow goto out_free_unlock; 10759c79f34fSMichael Halcrow } 10769c79f34fSMichael Halcrow s->i++; 10779c79f34fSMichael Halcrow (*filename_size) = (s->block_aligned_filename_size - s->i); 10789c79f34fSMichael Halcrow if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { 1079df261c52SMichael Halcrow printk(KERN_WARNING "%s: Filename size is [%zd], which is " 10809c79f34fSMichael Halcrow "invalid\n", __func__, (*filename_size)); 10819c79f34fSMichael Halcrow rc = -EINVAL; 10829c79f34fSMichael Halcrow goto out_free_unlock; 10839c79f34fSMichael Halcrow } 10849c79f34fSMichael Halcrow (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); 10859c79f34fSMichael Halcrow if (!(*filename)) { 10869c79f34fSMichael Halcrow rc = -ENOMEM; 10879c79f34fSMichael Halcrow goto out_free_unlock; 10889c79f34fSMichael Halcrow } 10899c79f34fSMichael Halcrow memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); 10909c79f34fSMichael Halcrow (*filename)[(*filename_size)] = '\0'; 10919c79f34fSMichael Halcrow out_free_unlock: 10929c79f34fSMichael Halcrow kfree(s->decrypted_filename); 10939c79f34fSMichael Halcrow out_unlock: 10949c79f34fSMichael Halcrow mutex_unlock(s->tfm_mutex); 10959c79f34fSMichael Halcrow out: 10969c79f34fSMichael Halcrow if (rc) { 10979c79f34fSMichael Halcrow (*packet_size) = 0; 10989c79f34fSMichael Halcrow (*filename_size) = 0; 10999c79f34fSMichael Halcrow (*filename) = NULL; 11009c79f34fSMichael Halcrow } 1101b5695d04SRoberto Sassu if (auth_tok_key) { 1102b5695d04SRoberto Sassu up_write(&(auth_tok_key->sem)); 1103aee683b9SRoberto Sassu key_put(auth_tok_key); 1104b5695d04SRoberto Sassu } 11053095e8e3SHerbert Xu skcipher_request_free(s->skcipher_req); 11069c79f34fSMichael Halcrow kfree(s); 11079c79f34fSMichael Halcrow return rc; 11089c79f34fSMichael Halcrow } 11099c79f34fSMichael Halcrow 11109c79f34fSMichael Halcrow static int 1111cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) 1112cd9d67dfSMichael Halcrow { 1113cd9d67dfSMichael Halcrow int rc = 0; 1114cd9d67dfSMichael Halcrow 1115cd9d67dfSMichael Halcrow (*sig) = NULL; 1116cd9d67dfSMichael Halcrow switch (auth_tok->token_type) { 1117cd9d67dfSMichael Halcrow case ECRYPTFS_PASSWORD: 1118cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.password.signature; 1119cd9d67dfSMichael Halcrow break; 1120cd9d67dfSMichael Halcrow case ECRYPTFS_PRIVATE_KEY: 1121cd9d67dfSMichael Halcrow (*sig) = auth_tok->token.private_key.signature; 1122cd9d67dfSMichael Halcrow break; 1123cd9d67dfSMichael Halcrow default: 1124cd9d67dfSMichael Halcrow printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", 1125cd9d67dfSMichael Halcrow auth_tok->token_type); 1126cd9d67dfSMichael Halcrow rc = -EINVAL; 1127cd9d67dfSMichael Halcrow } 1128cd9d67dfSMichael Halcrow return rc; 1129cd9d67dfSMichael Halcrow } 1130cd9d67dfSMichael Halcrow 1131dddfa461SMichael Halcrow /** 113222e78fafSMichael Halcrow * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. 113322e78fafSMichael Halcrow * @auth_tok: The key authentication token used to decrypt the session key 113422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1135dddfa461SMichael Halcrow * 113622e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise. 1137dddfa461SMichael Halcrow */ 1138f4aad16aSMichael Halcrow static int 1139f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1140dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1141dddfa461SMichael Halcrow { 114219e66a67STrevor Highland u8 cipher_code = 0; 1143dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx; 1144dddfa461SMichael Halcrow struct ecryptfs_message *msg = NULL; 1145f4aad16aSMichael Halcrow char *auth_tok_sig; 11463edc8376SGeyslan G. Bem char *payload = NULL; 1147fa519964SSimon Que size_t payload_len = 0; 1148dddfa461SMichael Halcrow int rc; 1149dddfa461SMichael Halcrow 11505dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); 11515dda6992SMichael Halcrow if (rc) { 1152f4aad16aSMichael Halcrow printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", 1153f4aad16aSMichael Halcrow auth_tok->token_type); 1154f4aad16aSMichael Halcrow goto out; 1155f4aad16aSMichael Halcrow } 1156f4aad16aSMichael Halcrow rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), 1157624ae528STyler Hicks &payload, &payload_len); 1158dddfa461SMichael Halcrow if (rc) { 1159f66e883eSMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); 1160dddfa461SMichael Halcrow goto out; 1161dddfa461SMichael Halcrow } 1162624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1163dddfa461SMichael Halcrow if (rc) { 1164624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 1165290502beSKees Cook "ecryptfsd: %d\n", rc); 1166dddfa461SMichael Halcrow goto out; 1167dddfa461SMichael Halcrow } 1168dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 1169dddfa461SMichael Halcrow if (rc) { 1170dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " 1171dddfa461SMichael Halcrow "from the user space daemon\n"); 1172dddfa461SMichael Halcrow rc = -EIO; 1173dddfa461SMichael Halcrow goto out; 1174dddfa461SMichael Halcrow } 1175dddfa461SMichael Halcrow rc = parse_tag_65_packet(&(auth_tok->session_key), 1176dddfa461SMichael Halcrow &cipher_code, msg); 1177dddfa461SMichael Halcrow if (rc) { 1178dddfa461SMichael Halcrow printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", 1179dddfa461SMichael Halcrow rc); 1180dddfa461SMichael Halcrow goto out; 1181dddfa461SMichael Halcrow } 1182dddfa461SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1183dddfa461SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1184dddfa461SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1185dddfa461SMichael Halcrow crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; 1186dddfa461SMichael Halcrow rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); 1187dddfa461SMichael Halcrow if (rc) { 1188dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", 1189dddfa461SMichael Halcrow cipher_code) 1190dddfa461SMichael Halcrow goto out; 1191dddfa461SMichael Halcrow } 1192dddfa461SMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1193dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 1194dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); 1195dddfa461SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1196dddfa461SMichael Halcrow crypt_stat->key_size); 1197dddfa461SMichael Halcrow } 1198dddfa461SMichael Halcrow out: 1199dddfa461SMichael Halcrow kfree(msg); 12003edc8376SGeyslan G. Bem kfree(payload); 1201dddfa461SMichael Halcrow return rc; 1202dddfa461SMichael Halcrow } 1203dddfa461SMichael Halcrow 1204dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) 1205dddfa461SMichael Halcrow { 1206dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1207e0869cc1SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1208dddfa461SMichael Halcrow 1209e0869cc1SMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, 1210e0869cc1SMichael Halcrow auth_tok_list_head, list) { 1211e0869cc1SMichael Halcrow list_del(&auth_tok_list_item->list); 1212dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1213dddfa461SMichael Halcrow auth_tok_list_item); 1214dddfa461SMichael Halcrow } 1215dddfa461SMichael Halcrow } 1216dddfa461SMichael Halcrow 1217dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache; 1218dddfa461SMichael Halcrow 1219dddfa461SMichael Halcrow /** 1220dddfa461SMichael Halcrow * parse_tag_1_packet 122122e78fafSMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet contents 1222dddfa461SMichael Halcrow * @data: The raw bytes of the packet. 1223dddfa461SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 122422e78fafSMichael Halcrow * a new authentication token will be placed at the 122522e78fafSMichael Halcrow * end of this list for this packet. 1226dddfa461SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1227dddfa461SMichael Halcrow * allocates; sets the memory address of the pointer to 1228dddfa461SMichael Halcrow * NULL on error. This object is added to the 1229dddfa461SMichael Halcrow * auth_tok_list. 1230dddfa461SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1231dddfa461SMichael Halcrow * into this memory location; zero on error. 123222e78fafSMichael Halcrow * @max_packet_size: The maximum allowable packet size 1233dddfa461SMichael Halcrow * 1234dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 1235dddfa461SMichael Halcrow */ 1236dddfa461SMichael Halcrow static int 1237dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, 1238dddfa461SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1239dddfa461SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1240dddfa461SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1241dddfa461SMichael Halcrow { 1242dddfa461SMichael Halcrow size_t body_size; 1243dddfa461SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1244dddfa461SMichael Halcrow size_t length_size; 1245dddfa461SMichael Halcrow int rc = 0; 1246dddfa461SMichael Halcrow 1247dddfa461SMichael Halcrow (*packet_size) = 0; 1248dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 124913218179SMichael Halcrow /** 125013218179SMichael Halcrow * This format is inspired by OpenPGP; see RFC 2440 125113218179SMichael Halcrow * packet tag 1 125213218179SMichael Halcrow * 125313218179SMichael Halcrow * Tag 1 identifier (1 byte) 125413218179SMichael Halcrow * Max Tag 1 packet size (max 3 bytes) 125513218179SMichael Halcrow * Version (1 byte) 125613218179SMichael Halcrow * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) 125713218179SMichael Halcrow * Cipher identifier (1 byte) 125813218179SMichael Halcrow * Encrypted key size (arbitrary) 125913218179SMichael Halcrow * 126013218179SMichael Halcrow * 12 bytes minimum packet size 1261dddfa461SMichael Halcrow */ 126213218179SMichael Halcrow if (unlikely(max_packet_size < 12)) { 126313218179SMichael Halcrow printk(KERN_ERR "Invalid max packet size; must be >=12\n"); 1264dddfa461SMichael Halcrow rc = -EINVAL; 1265dddfa461SMichael Halcrow goto out; 1266dddfa461SMichael Halcrow } 1267dddfa461SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { 126813218179SMichael Halcrow printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", 1269dddfa461SMichael Halcrow ECRYPTFS_TAG_1_PACKET_TYPE); 1270dddfa461SMichael Halcrow rc = -EINVAL; 1271dddfa461SMichael Halcrow goto out; 1272dddfa461SMichael Halcrow } 1273dddfa461SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1274dddfa461SMichael Halcrow * at end of function upon failure */ 1275dddfa461SMichael Halcrow auth_tok_list_item = 127613218179SMichael Halcrow kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, 1277dddfa461SMichael Halcrow GFP_KERNEL); 1278dddfa461SMichael Halcrow if (!auth_tok_list_item) { 127913218179SMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1280dddfa461SMichael Halcrow rc = -ENOMEM; 1281dddfa461SMichael Halcrow goto out; 1282dddfa461SMichael Halcrow } 1283dddfa461SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1284f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 12855dda6992SMichael Halcrow &length_size); 12865dda6992SMichael Halcrow if (rc) { 128713218179SMichael Halcrow printk(KERN_WARNING "Error parsing packet length; " 1288dddfa461SMichael Halcrow "rc = [%d]\n", rc); 1289dddfa461SMichael Halcrow goto out_free; 1290dddfa461SMichael Halcrow } 129113218179SMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { 129281acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1293dddfa461SMichael Halcrow rc = -EINVAL; 1294dddfa461SMichael Halcrow goto out_free; 1295dddfa461SMichael Halcrow } 1296dddfa461SMichael Halcrow (*packet_size) += length_size; 1297dddfa461SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 129813218179SMichael Halcrow printk(KERN_WARNING "Packet size exceeds max\n"); 1299dddfa461SMichael Halcrow rc = -EINVAL; 1300dddfa461SMichael Halcrow goto out_free; 1301dddfa461SMichael Halcrow } 1302dddfa461SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 130313218179SMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 130413218179SMichael Halcrow data[(*packet_size) - 1]); 1305dddfa461SMichael Halcrow rc = -EINVAL; 1306dddfa461SMichael Halcrow goto out_free; 1307dddfa461SMichael Halcrow } 1308dddfa461SMichael Halcrow ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, 1309dddfa461SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SIG_SIZE); 1310dddfa461SMichael Halcrow *packet_size += ECRYPTFS_SIG_SIZE; 1311dddfa461SMichael Halcrow /* This byte is skipped because the kernel does not need to 1312dddfa461SMichael Halcrow * know which public key encryption algorithm was used */ 1313dddfa461SMichael Halcrow (*packet_size)++; 1314dddfa461SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 131513218179SMichael Halcrow body_size - (ECRYPTFS_SIG_SIZE + 2); 1316dddfa461SMichael Halcrow if ((*new_auth_tok)->session_key.encrypted_key_size 1317dddfa461SMichael Halcrow > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 131813218179SMichael Halcrow printk(KERN_WARNING "Tag 1 packet contains key larger " 13190996b67dSColin Ian King "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1320dddfa461SMichael Halcrow rc = -EINVAL; 1321dddfa461SMichael Halcrow goto out; 1322dddfa461SMichael Halcrow } 1323dddfa461SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 132413218179SMichael Halcrow &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); 1325dddfa461SMichael Halcrow (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; 1326dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1327dddfa461SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1328dddfa461SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1329dddfa461SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1330dddfa461SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; 133113218179SMichael Halcrow (*new_auth_tok)->flags = 0; 1332e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1333e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1334e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1335e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1336dddfa461SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1337dddfa461SMichael Halcrow goto out; 1338dddfa461SMichael Halcrow out_free: 1339dddfa461SMichael Halcrow (*new_auth_tok) = NULL; 1340dddfa461SMichael Halcrow memset(auth_tok_list_item, 0, 1341dddfa461SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1342dddfa461SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1343dddfa461SMichael Halcrow auth_tok_list_item); 1344dddfa461SMichael Halcrow out: 1345dddfa461SMichael Halcrow if (rc) 1346dddfa461SMichael Halcrow (*packet_size) = 0; 1347dddfa461SMichael Halcrow return rc; 1348dddfa461SMichael Halcrow } 1349dddfa461SMichael Halcrow 1350237fead6SMichael Halcrow /** 1351237fead6SMichael Halcrow * parse_tag_3_packet 1352237fead6SMichael Halcrow * @crypt_stat: The cryptographic context to modify based on packet 1353237fead6SMichael Halcrow * contents. 1354237fead6SMichael Halcrow * @data: The raw bytes of the packet. 1355237fead6SMichael Halcrow * @auth_tok_list: eCryptfs parses packets into authentication tokens; 1356237fead6SMichael Halcrow * a new authentication token will be placed at the end 1357237fead6SMichael Halcrow * of this list for this packet. 1358237fead6SMichael Halcrow * @new_auth_tok: Pointer to a pointer to memory that this function 1359237fead6SMichael Halcrow * allocates; sets the memory address of the pointer to 1360237fead6SMichael Halcrow * NULL on error. This object is added to the 1361237fead6SMichael Halcrow * auth_tok_list. 1362237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1363237fead6SMichael Halcrow * into this memory location; zero on error. 1364237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1365237fead6SMichael Halcrow * 1366237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1367237fead6SMichael Halcrow */ 1368237fead6SMichael Halcrow static int 1369237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, 1370237fead6SMichael Halcrow unsigned char *data, struct list_head *auth_tok_list, 1371237fead6SMichael Halcrow struct ecryptfs_auth_tok **new_auth_tok, 1372237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1373237fead6SMichael Halcrow { 1374237fead6SMichael Halcrow size_t body_size; 1375237fead6SMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1376237fead6SMichael Halcrow size_t length_size; 1377dddfa461SMichael Halcrow int rc = 0; 1378237fead6SMichael Halcrow 1379237fead6SMichael Halcrow (*packet_size) = 0; 1380237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1381c59becfcSMichael Halcrow /** 1382c59becfcSMichael Halcrow *This format is inspired by OpenPGP; see RFC 2440 1383c59becfcSMichael Halcrow * packet tag 3 1384c59becfcSMichael Halcrow * 1385c59becfcSMichael Halcrow * Tag 3 identifier (1 byte) 1386c59becfcSMichael Halcrow * Max Tag 3 packet size (max 3 bytes) 1387c59becfcSMichael Halcrow * Version (1 byte) 1388c59becfcSMichael Halcrow * Cipher code (1 byte) 1389c59becfcSMichael Halcrow * S2K specifier (1 byte) 1390c59becfcSMichael Halcrow * Hash identifier (1 byte) 1391c59becfcSMichael Halcrow * Salt (ECRYPTFS_SALT_SIZE) 1392c59becfcSMichael Halcrow * Hash iterations (1 byte) 1393c59becfcSMichael Halcrow * Encrypted key (arbitrary) 1394c59becfcSMichael Halcrow * 1395c59becfcSMichael Halcrow * (ECRYPTFS_SALT_SIZE + 7) minimum packet size 1396237fead6SMichael Halcrow */ 1397c59becfcSMichael Halcrow if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { 1398c59becfcSMichael Halcrow printk(KERN_ERR "Max packet size too large\n"); 1399237fead6SMichael Halcrow rc = -EINVAL; 1400237fead6SMichael Halcrow goto out; 1401237fead6SMichael Halcrow } 1402237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { 1403c59becfcSMichael Halcrow printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", 1404237fead6SMichael Halcrow ECRYPTFS_TAG_3_PACKET_TYPE); 1405237fead6SMichael Halcrow rc = -EINVAL; 1406237fead6SMichael Halcrow goto out; 1407237fead6SMichael Halcrow } 1408237fead6SMichael Halcrow /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or 1409237fead6SMichael Halcrow * at end of function upon failure */ 1410237fead6SMichael Halcrow auth_tok_list_item = 1411c3762229SRobert P. J. Day kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); 1412237fead6SMichael Halcrow if (!auth_tok_list_item) { 1413c59becfcSMichael Halcrow printk(KERN_ERR "Unable to allocate memory\n"); 1414237fead6SMichael Halcrow rc = -ENOMEM; 1415237fead6SMichael Halcrow goto out; 1416237fead6SMichael Halcrow } 1417237fead6SMichael Halcrow (*new_auth_tok) = &auth_tok_list_item->auth_tok; 1418f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 14195dda6992SMichael Halcrow &length_size); 14205dda6992SMichael Halcrow if (rc) { 1421c59becfcSMichael Halcrow printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", 1422c59becfcSMichael Halcrow rc); 1423237fead6SMichael Halcrow goto out_free; 1424237fead6SMichael Halcrow } 1425c59becfcSMichael Halcrow if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { 142681acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1427237fead6SMichael Halcrow rc = -EINVAL; 1428237fead6SMichael Halcrow goto out_free; 1429237fead6SMichael Halcrow } 1430237fead6SMichael Halcrow (*packet_size) += length_size; 1431237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size > max_packet_size)) { 1432c59becfcSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1433237fead6SMichael Halcrow rc = -EINVAL; 1434237fead6SMichael Halcrow goto out_free; 1435237fead6SMichael Halcrow } 1436237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size = 1437c59becfcSMichael Halcrow (body_size - (ECRYPTFS_SALT_SIZE + 5)); 1438f151cd2cSRamon de Carvalho Valle if ((*new_auth_tok)->session_key.encrypted_key_size 1439f151cd2cSRamon de Carvalho Valle > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { 1440f151cd2cSRamon de Carvalho Valle printk(KERN_WARNING "Tag 3 packet contains key larger " 1441f151cd2cSRamon de Carvalho Valle "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); 1442f151cd2cSRamon de Carvalho Valle rc = -EINVAL; 1443f151cd2cSRamon de Carvalho Valle goto out_free; 1444f151cd2cSRamon de Carvalho Valle } 1445237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x04)) { 1446c59becfcSMichael Halcrow printk(KERN_WARNING "Unknown version number [%d]\n", 1447c59becfcSMichael Halcrow data[(*packet_size) - 1]); 1448237fead6SMichael Halcrow rc = -EINVAL; 1449237fead6SMichael Halcrow goto out_free; 1450237fead6SMichael Halcrow } 1451b0105eaeSTyler Hicks rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, 1452237fead6SMichael Halcrow (u16)data[(*packet_size)]); 1453b0105eaeSTyler Hicks if (rc) 1454b0105eaeSTyler Hicks goto out_free; 1455237fead6SMichael Halcrow /* A little extra work to differentiate among the AES key 1456237fead6SMichael Halcrow * sizes; see RFC2440 */ 1457237fead6SMichael Halcrow switch(data[(*packet_size)++]) { 1458237fead6SMichael Halcrow case RFC2440_CIPHER_AES_192: 1459237fead6SMichael Halcrow crypt_stat->key_size = 24; 1460237fead6SMichael Halcrow break; 1461237fead6SMichael Halcrow default: 1462237fead6SMichael Halcrow crypt_stat->key_size = 1463237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1464237fead6SMichael Halcrow } 1465b0105eaeSTyler Hicks rc = ecryptfs_init_crypt_ctx(crypt_stat); 1466b0105eaeSTyler Hicks if (rc) 1467b0105eaeSTyler Hicks goto out_free; 1468237fead6SMichael Halcrow if (unlikely(data[(*packet_size)++] != 0x03)) { 1469c59becfcSMichael Halcrow printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); 1470237fead6SMichael Halcrow rc = -ENOSYS; 1471237fead6SMichael Halcrow goto out_free; 1472237fead6SMichael Halcrow } 1473237fead6SMichael Halcrow /* TODO: finish the hash mapping */ 1474237fead6SMichael Halcrow switch (data[(*packet_size)++]) { 1475237fead6SMichael Halcrow case 0x01: /* See RFC2440 for these numbers and their mappings */ 1476237fead6SMichael Halcrow /* Choose MD5 */ 1477237fead6SMichael Halcrow memcpy((*new_auth_tok)->token.password.salt, 1478237fead6SMichael Halcrow &data[(*packet_size)], ECRYPTFS_SALT_SIZE); 1479237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; 1480237fead6SMichael Halcrow /* This conversion was taken straight from RFC2440 */ 1481237fead6SMichael Halcrow (*new_auth_tok)->token.password.hash_iterations = 1482237fead6SMichael Halcrow ((u32) 16 + (data[(*packet_size)] & 15)) 1483237fead6SMichael Halcrow << ((data[(*packet_size)] >> 4) + 6); 1484237fead6SMichael Halcrow (*packet_size)++; 1485c59becfcSMichael Halcrow /* Friendly reminder: 1486c59becfcSMichael Halcrow * (*new_auth_tok)->session_key.encrypted_key_size = 1487c59becfcSMichael Halcrow * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ 1488237fead6SMichael Halcrow memcpy((*new_auth_tok)->session_key.encrypted_key, 1489237fead6SMichael Halcrow &data[(*packet_size)], 1490237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size); 1491237fead6SMichael Halcrow (*packet_size) += 1492237fead6SMichael Halcrow (*new_auth_tok)->session_key.encrypted_key_size; 1493237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags &= 1494237fead6SMichael Halcrow ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1495237fead6SMichael Halcrow (*new_auth_tok)->session_key.flags |= 1496237fead6SMichael Halcrow ECRYPTFS_CONTAINS_ENCRYPTED_KEY; 1497c59becfcSMichael Halcrow (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ 1498237fead6SMichael Halcrow break; 1499237fead6SMichael Halcrow default: 1500237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " 1501237fead6SMichael Halcrow "[%d]\n", data[(*packet_size) - 1]); 1502237fead6SMichael Halcrow rc = -ENOSYS; 1503237fead6SMichael Halcrow goto out_free; 1504237fead6SMichael Halcrow } 1505237fead6SMichael Halcrow (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; 1506237fead6SMichael Halcrow /* TODO: Parametarize; we might actually want userspace to 1507237fead6SMichael Halcrow * decrypt the session key. */ 1508e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1509e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); 1510e2bd99ecSMichael Halcrow (*new_auth_tok)->session_key.flags &= 1511e2bd99ecSMichael Halcrow ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); 1512237fead6SMichael Halcrow list_add(&auth_tok_list_item->list, auth_tok_list); 1513237fead6SMichael Halcrow goto out; 1514237fead6SMichael Halcrow out_free: 1515237fead6SMichael Halcrow (*new_auth_tok) = NULL; 1516237fead6SMichael Halcrow memset(auth_tok_list_item, 0, 1517237fead6SMichael Halcrow sizeof(struct ecryptfs_auth_tok_list_item)); 1518237fead6SMichael Halcrow kmem_cache_free(ecryptfs_auth_tok_list_item_cache, 1519237fead6SMichael Halcrow auth_tok_list_item); 1520237fead6SMichael Halcrow out: 1521237fead6SMichael Halcrow if (rc) 1522237fead6SMichael Halcrow (*packet_size) = 0; 1523237fead6SMichael Halcrow return rc; 1524237fead6SMichael Halcrow } 1525237fead6SMichael Halcrow 1526237fead6SMichael Halcrow /** 1527237fead6SMichael Halcrow * parse_tag_11_packet 1528237fead6SMichael Halcrow * @data: The raw bytes of the packet 1529237fead6SMichael Halcrow * @contents: This function writes the data contents of the literal 1530237fead6SMichael Halcrow * packet into this memory location 1531237fead6SMichael Halcrow * @max_contents_bytes: The maximum number of bytes that this function 1532237fead6SMichael Halcrow * is allowed to write into contents 1533237fead6SMichael Halcrow * @tag_11_contents_size: This function writes the size of the parsed 1534237fead6SMichael Halcrow * contents into this memory location; zero on 1535237fead6SMichael Halcrow * error 1536237fead6SMichael Halcrow * @packet_size: This function writes the size of the parsed packet 1537237fead6SMichael Halcrow * into this memory location; zero on error 1538237fead6SMichael Halcrow * @max_packet_size: maximum number of bytes to parse 1539237fead6SMichael Halcrow * 1540237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 1541237fead6SMichael Halcrow */ 1542237fead6SMichael Halcrow static int 1543237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents, 1544237fead6SMichael Halcrow size_t max_contents_bytes, size_t *tag_11_contents_size, 1545237fead6SMichael Halcrow size_t *packet_size, size_t max_packet_size) 1546237fead6SMichael Halcrow { 1547237fead6SMichael Halcrow size_t body_size; 1548237fead6SMichael Halcrow size_t length_size; 1549dddfa461SMichael Halcrow int rc = 0; 1550237fead6SMichael Halcrow 1551237fead6SMichael Halcrow (*packet_size) = 0; 1552237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1553f648104aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 1554f648104aSMichael Halcrow * packet tag 11 1555f648104aSMichael Halcrow * 1556f648104aSMichael Halcrow * Tag 11 identifier (1 byte) 1557f648104aSMichael Halcrow * Max Tag 11 packet size (max 3 bytes) 1558f648104aSMichael Halcrow * Binary format specifier (1 byte) 1559f648104aSMichael Halcrow * Filename length (1 byte) 1560f648104aSMichael Halcrow * Filename ("_CONSOLE") (8 bytes) 1561f648104aSMichael Halcrow * Modification date (4 bytes) 1562f648104aSMichael Halcrow * Literal data (arbitrary) 1563f648104aSMichael Halcrow * 1564f648104aSMichael Halcrow * We need at least 16 bytes of data for the packet to even be 1565f648104aSMichael Halcrow * valid. 1566237fead6SMichael Halcrow */ 1567f648104aSMichael Halcrow if (max_packet_size < 16) { 1568f648104aSMichael Halcrow printk(KERN_ERR "Maximum packet size too small\n"); 1569237fead6SMichael Halcrow rc = -EINVAL; 1570237fead6SMichael Halcrow goto out; 1571237fead6SMichael Halcrow } 1572237fead6SMichael Halcrow if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { 1573f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1574237fead6SMichael Halcrow rc = -EINVAL; 1575237fead6SMichael Halcrow goto out; 1576237fead6SMichael Halcrow } 1577f66e883eSMichael Halcrow rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, 15785dda6992SMichael Halcrow &length_size); 15795dda6992SMichael Halcrow if (rc) { 1580f648104aSMichael Halcrow printk(KERN_WARNING "Invalid tag 11 packet format\n"); 1581f648104aSMichael Halcrow goto out; 1582f648104aSMichael Halcrow } 1583f648104aSMichael Halcrow if (body_size < 14) { 158481acbcd6SAndrew Morton printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); 1585f648104aSMichael Halcrow rc = -EINVAL; 1586237fead6SMichael Halcrow goto out; 1587237fead6SMichael Halcrow } 1588237fead6SMichael Halcrow (*packet_size) += length_size; 1589f648104aSMichael Halcrow (*tag_11_contents_size) = (body_size - 14); 1590237fead6SMichael Halcrow if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { 1591f648104aSMichael Halcrow printk(KERN_ERR "Packet size exceeds max\n"); 1592237fead6SMichael Halcrow rc = -EINVAL; 1593237fead6SMichael Halcrow goto out; 1594237fead6SMichael Halcrow } 15956352a293STyler Hicks if (unlikely((*tag_11_contents_size) > max_contents_bytes)) { 15966352a293STyler Hicks printk(KERN_ERR "Literal data section in tag 11 packet exceeds " 15976352a293STyler Hicks "expected size\n"); 15986352a293STyler Hicks rc = -EINVAL; 15996352a293STyler Hicks goto out; 16006352a293STyler Hicks } 1601237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x62) { 1602f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1603237fead6SMichael Halcrow rc = -EINVAL; 1604237fead6SMichael Halcrow goto out; 1605237fead6SMichael Halcrow } 1606237fead6SMichael Halcrow if (data[(*packet_size)++] != 0x08) { 1607f648104aSMichael Halcrow printk(KERN_WARNING "Unrecognizable packet\n"); 1608237fead6SMichael Halcrow rc = -EINVAL; 1609237fead6SMichael Halcrow goto out; 1610237fead6SMichael Halcrow } 1611f648104aSMichael Halcrow (*packet_size) += 12; /* Ignore filename and modification date */ 1612237fead6SMichael Halcrow memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); 1613237fead6SMichael Halcrow (*packet_size) += (*tag_11_contents_size); 1614237fead6SMichael Halcrow out: 1615237fead6SMichael Halcrow if (rc) { 1616237fead6SMichael Halcrow (*packet_size) = 0; 1617237fead6SMichael Halcrow (*tag_11_contents_size) = 0; 1618237fead6SMichael Halcrow } 1619237fead6SMichael Halcrow return rc; 1620237fead6SMichael Halcrow } 1621237fead6SMichael Halcrow 1622f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, 1623f4aad16aSMichael Halcrow struct ecryptfs_auth_tok **auth_tok, 1624f4aad16aSMichael Halcrow char *sig) 1625f4aad16aSMichael Halcrow { 1626f4aad16aSMichael Halcrow int rc = 0; 1627f4aad16aSMichael Halcrow 1628f4aad16aSMichael Halcrow (*auth_tok_key) = request_key(&key_type_user, sig, NULL); 1629f4aad16aSMichael Halcrow if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 16301252cc3bSRoberto Sassu (*auth_tok_key) = ecryptfs_get_encrypted_key(sig); 16311252cc3bSRoberto Sassu if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { 1632f4aad16aSMichael Halcrow printk(KERN_ERR "Could not find key with description: [%s]\n", 1633f4aad16aSMichael Halcrow sig); 1634982363c9SEric Sandeen rc = process_request_key_err(PTR_ERR(*auth_tok_key)); 16351821df04SRoberto Sassu (*auth_tok_key) = NULL; 1636f4aad16aSMichael Halcrow goto out; 1637f4aad16aSMichael Halcrow } 16381252cc3bSRoberto Sassu } 1639b5695d04SRoberto Sassu down_write(&(*auth_tok_key)->sem); 16400e1fc5efSRoberto Sassu rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok); 1641aee683b9SRoberto Sassu if (rc) { 1642b5695d04SRoberto Sassu up_write(&(*auth_tok_key)->sem); 1643aee683b9SRoberto Sassu key_put(*auth_tok_key); 1644aee683b9SRoberto Sassu (*auth_tok_key) = NULL; 16450e1fc5efSRoberto Sassu goto out; 1646f4aad16aSMichael Halcrow } 1647f4aad16aSMichael Halcrow out: 1648f4aad16aSMichael Halcrow return rc; 1649f4aad16aSMichael Halcrow } 1650f4aad16aSMichael Halcrow 1651f4aad16aSMichael Halcrow /** 165222e78fafSMichael Halcrow * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. 165322e78fafSMichael Halcrow * @auth_tok: The passphrase authentication token to use to encrypt the FEK 165422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 1655237fead6SMichael Halcrow * 165622e78fafSMichael Halcrow * Returns zero on success; non-zero error otherwise 1657237fead6SMichael Halcrow */ 1658f4aad16aSMichael Halcrow static int 1659f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, 1660237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat) 1661237fead6SMichael Halcrow { 1662ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 1663ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 1664dd8e2902SMichael Halcrow struct mutex *tfm_mutex; 16653095e8e3SHerbert Xu struct crypto_skcipher *tfm; 16663095e8e3SHerbert Xu struct skcipher_request *req = NULL; 16678bba066fSMichael Halcrow int rc = 0; 1668237fead6SMichael Halcrow 1669f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1670f4aad16aSMichael Halcrow ecryptfs_printk( 1671f4aad16aSMichael Halcrow KERN_DEBUG, "Session key encryption key (size [%d]):\n", 1672f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1673f4aad16aSMichael Halcrow ecryptfs_dump_hex( 1674f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key, 1675f4aad16aSMichael Halcrow auth_tok->token.password.session_key_encryption_key_bytes); 1676f4aad16aSMichael Halcrow } 16773095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 1678f4aad16aSMichael Halcrow crypt_stat->cipher); 1679f4aad16aSMichael Halcrow if (unlikely(rc)) { 1680f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 1681f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 1682f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 1683237fead6SMichael Halcrow goto out; 1684237fead6SMichael Halcrow } 16855dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, 1686f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size, 1687ac97b9f9SMichael Halcrow src_sg, 2); 1688ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1689f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1690f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key to scatterlist; " 1691f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 1692f4aad16aSMichael Halcrow "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, 1693f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size); 1694f4aad16aSMichael Halcrow goto out; 1695237fead6SMichael Halcrow } 1696f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size = 1697f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size; 16985dda6992SMichael Halcrow rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, 1699f4aad16aSMichael Halcrow auth_tok->session_key.decrypted_key_size, 1700ac97b9f9SMichael Halcrow dst_sg, 2); 1701ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 1702f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to convert " 1703f4aad16aSMichael Halcrow "auth_tok->session_key.decrypted_key to scatterlist; " 1704f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]\n", rc); 1705f4aad16aSMichael Halcrow goto out; 1706f4aad16aSMichael Halcrow } 1707237fead6SMichael Halcrow mutex_lock(tfm_mutex); 17083095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 17093095e8e3SHerbert Xu if (!req) { 17103095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 17113095e8e3SHerbert Xu printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " 17123095e8e3SHerbert Xu "skcipher_request_alloc for %s\n", __func__, 17133095e8e3SHerbert Xu crypto_skcipher_driver_name(tfm)); 17143095e8e3SHerbert Xu rc = -ENOMEM; 17153095e8e3SHerbert Xu goto out; 17163095e8e3SHerbert Xu } 17173095e8e3SHerbert Xu 17183095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 17193095e8e3SHerbert Xu NULL, NULL); 17203095e8e3SHerbert Xu rc = crypto_skcipher_setkey( 17213095e8e3SHerbert Xu tfm, auth_tok->token.password.session_key_encryption_key, 1722237fead6SMichael Halcrow crypt_stat->key_size); 1723f4aad16aSMichael Halcrow if (unlikely(rc < 0)) { 1724f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1725e5d9cbdeSMichael Halcrow printk(KERN_ERR "Error setting key for crypto context\n"); 1726e5d9cbdeSMichael Halcrow rc = -EINVAL; 1727f4aad16aSMichael Halcrow goto out; 1728e5d9cbdeSMichael Halcrow } 17293095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 17303095e8e3SHerbert Xu auth_tok->session_key.encrypted_key_size, 17313095e8e3SHerbert Xu NULL); 17323095e8e3SHerbert Xu rc = crypto_skcipher_decrypt(req); 1733f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 1734f4aad16aSMichael Halcrow if (unlikely(rc)) { 17358bba066fSMichael Halcrow printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); 1736f4aad16aSMichael Halcrow goto out; 17378bba066fSMichael Halcrow } 1738237fead6SMichael Halcrow auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; 1739237fead6SMichael Halcrow memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, 1740237fead6SMichael Halcrow auth_tok->session_key.decrypted_key_size); 1741e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_KEY_VALID; 1742f4aad16aSMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1743f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n", 1744f4aad16aSMichael Halcrow crypt_stat->key_size); 1745237fead6SMichael Halcrow ecryptfs_dump_hex(crypt_stat->key, 1746237fead6SMichael Halcrow crypt_stat->key_size); 1747f4aad16aSMichael Halcrow } 1748237fead6SMichael Halcrow out: 17493095e8e3SHerbert Xu skcipher_request_free(req); 1750237fead6SMichael Halcrow return rc; 1751237fead6SMichael Halcrow } 1752237fead6SMichael Halcrow 1753237fead6SMichael Halcrow /** 1754237fead6SMichael Halcrow * ecryptfs_parse_packet_set 175522e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 175622e78fafSMichael Halcrow * @src: Virtual address of region of memory containing the packets 175722e78fafSMichael Halcrow * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set 1758237fead6SMichael Halcrow * 1759237fead6SMichael Halcrow * Get crypt_stat to have the file's session key if the requisite key 1760237fead6SMichael Halcrow * is available to decrypt the session key. 1761237fead6SMichael Halcrow * 1762237fead6SMichael Halcrow * Returns Zero if a valid authentication token was retrieved and 1763237fead6SMichael Halcrow * processed; negative value for file not encrypted or for error 1764237fead6SMichael Halcrow * conditions. 1765237fead6SMichael Halcrow */ 1766237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, 1767237fead6SMichael Halcrow unsigned char *src, 1768237fead6SMichael Halcrow struct dentry *ecryptfs_dentry) 1769237fead6SMichael Halcrow { 1770237fead6SMichael Halcrow size_t i = 0; 1771f4aad16aSMichael Halcrow size_t found_auth_tok; 1772237fead6SMichael Halcrow size_t next_packet_is_auth_tok_packet; 1773237fead6SMichael Halcrow struct list_head auth_tok_list; 1774dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *matching_auth_tok; 1775dd8e2902SMichael Halcrow struct ecryptfs_auth_tok *candidate_auth_tok; 1776f4aad16aSMichael Halcrow char *candidate_auth_tok_sig; 1777237fead6SMichael Halcrow size_t packet_size; 1778237fead6SMichael Halcrow struct ecryptfs_auth_tok *new_auth_tok; 1779237fead6SMichael Halcrow unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; 1780f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item; 1781237fead6SMichael Halcrow size_t tag_11_contents_size; 1782237fead6SMichael Halcrow size_t tag_11_packet_size; 1783aee683b9SRoberto Sassu struct key *auth_tok_key = NULL; 1784dddfa461SMichael Halcrow int rc = 0; 1785237fead6SMichael Halcrow 1786237fead6SMichael Halcrow INIT_LIST_HEAD(&auth_tok_list); 1787f4aad16aSMichael Halcrow /* Parse the header to find as many packets as we can; these will be 1788237fead6SMichael Halcrow * added the our &auth_tok_list */ 1789237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 1; 1790237fead6SMichael Halcrow while (next_packet_is_auth_tok_packet) { 179109cbfeafSKirill A. Shutemov size_t max_packet_size = ((PAGE_SIZE - 8) - i); 1792237fead6SMichael Halcrow 1793237fead6SMichael Halcrow switch (src[i]) { 1794237fead6SMichael Halcrow case ECRYPTFS_TAG_3_PACKET_TYPE: 1795237fead6SMichael Halcrow rc = parse_tag_3_packet(crypt_stat, 1796237fead6SMichael Halcrow (unsigned char *)&src[i], 1797237fead6SMichael Halcrow &auth_tok_list, &new_auth_tok, 1798237fead6SMichael Halcrow &packet_size, max_packet_size); 1799237fead6SMichael Halcrow if (rc) { 1800237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1801237fead6SMichael Halcrow "tag 3 packet\n"); 1802237fead6SMichael Halcrow rc = -EIO; 1803237fead6SMichael Halcrow goto out_wipe_list; 1804237fead6SMichael Halcrow } 1805237fead6SMichael Halcrow i += packet_size; 1806237fead6SMichael Halcrow rc = parse_tag_11_packet((unsigned char *)&src[i], 1807237fead6SMichael Halcrow sig_tmp_space, 1808237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1809237fead6SMichael Halcrow &tag_11_contents_size, 1810237fead6SMichael Halcrow &tag_11_packet_size, 1811237fead6SMichael Halcrow max_packet_size); 1812237fead6SMichael Halcrow if (rc) { 1813237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "No valid " 1814237fead6SMichael Halcrow "(ecryptfs-specific) literal " 1815237fead6SMichael Halcrow "packet containing " 1816237fead6SMichael Halcrow "authentication token " 1817237fead6SMichael Halcrow "signature found after " 1818237fead6SMichael Halcrow "tag 3 packet\n"); 1819237fead6SMichael Halcrow rc = -EIO; 1820237fead6SMichael Halcrow goto out_wipe_list; 1821237fead6SMichael Halcrow } 1822237fead6SMichael Halcrow i += tag_11_packet_size; 1823237fead6SMichael Halcrow if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { 1824237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Expected " 1825237fead6SMichael Halcrow "signature of size [%d]; " 1826f24b3887STyler Hicks "read size [%zd]\n", 1827237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE, 1828237fead6SMichael Halcrow tag_11_contents_size); 1829237fead6SMichael Halcrow rc = -EIO; 1830237fead6SMichael Halcrow goto out_wipe_list; 1831237fead6SMichael Halcrow } 1832237fead6SMichael Halcrow ecryptfs_to_hex(new_auth_tok->token.password.signature, 1833237fead6SMichael Halcrow sig_tmp_space, tag_11_contents_size); 1834237fead6SMichael Halcrow new_auth_tok->token.password.signature[ 1835237fead6SMichael Halcrow ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; 1836e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1837237fead6SMichael Halcrow break; 1838dddfa461SMichael Halcrow case ECRYPTFS_TAG_1_PACKET_TYPE: 1839dddfa461SMichael Halcrow rc = parse_tag_1_packet(crypt_stat, 1840dddfa461SMichael Halcrow (unsigned char *)&src[i], 1841dddfa461SMichael Halcrow &auth_tok_list, &new_auth_tok, 1842dddfa461SMichael Halcrow &packet_size, max_packet_size); 1843dddfa461SMichael Halcrow if (rc) { 1844dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing " 1845dddfa461SMichael Halcrow "tag 1 packet\n"); 1846dddfa461SMichael Halcrow rc = -EIO; 1847dddfa461SMichael Halcrow goto out_wipe_list; 1848dddfa461SMichael Halcrow } 1849dddfa461SMichael Halcrow i += packet_size; 1850e2bd99ecSMichael Halcrow crypt_stat->flags |= ECRYPTFS_ENCRYPTED; 1851dddfa461SMichael Halcrow break; 1852237fead6SMichael Halcrow case ECRYPTFS_TAG_11_PACKET_TYPE: 1853237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Invalid packet set " 1854237fead6SMichael Halcrow "(Tag 11 not allowed by itself)\n"); 1855237fead6SMichael Halcrow rc = -EIO; 1856237fead6SMichael Halcrow goto out_wipe_list; 1857237fead6SMichael Halcrow default: 1858f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] " 1859f24b3887STyler Hicks "of the file header; hex value of " 1860237fead6SMichael Halcrow "character is [0x%.2x]\n", i, src[i]); 1861237fead6SMichael Halcrow next_packet_is_auth_tok_packet = 0; 1862237fead6SMichael Halcrow } 1863237fead6SMichael Halcrow } 1864237fead6SMichael Halcrow if (list_empty(&auth_tok_list)) { 1865f4aad16aSMichael Halcrow printk(KERN_ERR "The lower file appears to be a non-encrypted " 1866f4aad16aSMichael Halcrow "eCryptfs file; this is not supported in this version " 1867f4aad16aSMichael Halcrow "of the eCryptfs kernel module\n"); 1868f4aad16aSMichael Halcrow rc = -EINVAL; 1869237fead6SMichael Halcrow goto out; 1870237fead6SMichael Halcrow } 1871f4aad16aSMichael Halcrow /* auth_tok_list contains the set of authentication tokens 1872f4aad16aSMichael Halcrow * parsed from the metadata. We need to find a matching 1873f4aad16aSMichael Halcrow * authentication token that has the secret component(s) 1874f4aad16aSMichael Halcrow * necessary to decrypt the EFEK in the auth_tok parsed from 1875f4aad16aSMichael Halcrow * the metadata. There may be several potential matches, but 1876f4aad16aSMichael Halcrow * just one will be sufficient to decrypt to get the FEK. */ 1877f4aad16aSMichael Halcrow find_next_matching_auth_tok: 1878f4aad16aSMichael Halcrow found_auth_tok = 0; 1879f4aad16aSMichael Halcrow list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { 1880237fead6SMichael Halcrow candidate_auth_tok = &auth_tok_list_item->auth_tok; 1881237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 1882237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 1883237fead6SMichael Halcrow "Considering cadidate auth tok:\n"); 1884237fead6SMichael Halcrow ecryptfs_dump_auth_tok(candidate_auth_tok); 1885237fead6SMichael Halcrow } 18865dda6992SMichael Halcrow rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, 18875dda6992SMichael Halcrow candidate_auth_tok); 18885dda6992SMichael Halcrow if (rc) { 1889f4aad16aSMichael Halcrow printk(KERN_ERR 1890f4aad16aSMichael Halcrow "Unrecognized candidate auth tok type: [%d]\n", 1891f4aad16aSMichael Halcrow candidate_auth_tok->token_type); 1892f4aad16aSMichael Halcrow rc = -EINVAL; 1893f4aad16aSMichael Halcrow goto out_wipe_list; 1894f4aad16aSMichael Halcrow } 189539fac853SRoberto Sassu rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, 1896aee683b9SRoberto Sassu &matching_auth_tok, 18979c79f34fSMichael Halcrow crypt_stat->mount_crypt_stat, 18985dda6992SMichael Halcrow candidate_auth_tok_sig); 189939fac853SRoberto Sassu if (!rc) { 1900237fead6SMichael Halcrow found_auth_tok = 1; 1901f4aad16aSMichael Halcrow goto found_matching_auth_tok; 1902237fead6SMichael Halcrow } 1903237fead6SMichael Halcrow } 1904237fead6SMichael Halcrow if (!found_auth_tok) { 1905f4aad16aSMichael Halcrow ecryptfs_printk(KERN_ERR, "Could not find a usable " 1906f4aad16aSMichael Halcrow "authentication token\n"); 1907237fead6SMichael Halcrow rc = -EIO; 1908237fead6SMichael Halcrow goto out_wipe_list; 1909dddfa461SMichael Halcrow } 1910f4aad16aSMichael Halcrow found_matching_auth_tok: 1911e2bd99ecSMichael Halcrow if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 1912dddfa461SMichael Halcrow memcpy(&(candidate_auth_tok->token.private_key), 1913f4aad16aSMichael Halcrow &(matching_auth_tok->token.private_key), 1914dddfa461SMichael Halcrow sizeof(struct ecryptfs_private_key)); 1915b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1916b2987a5eSTyler Hicks key_put(auth_tok_key); 1917f4aad16aSMichael Halcrow rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, 1918dddfa461SMichael Halcrow crypt_stat); 1919dddfa461SMichael Halcrow } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { 1920237fead6SMichael Halcrow memcpy(&(candidate_auth_tok->token.password), 1921f4aad16aSMichael Halcrow &(matching_auth_tok->token.password), 1922237fead6SMichael Halcrow sizeof(struct ecryptfs_password)); 1923b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1924b2987a5eSTyler Hicks key_put(auth_tok_key); 1925f4aad16aSMichael Halcrow rc = decrypt_passphrase_encrypted_session_key( 1926f4aad16aSMichael Halcrow candidate_auth_tok, crypt_stat); 1927b2987a5eSTyler Hicks } else { 1928b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1929b2987a5eSTyler Hicks key_put(auth_tok_key); 1930b2987a5eSTyler Hicks rc = -EINVAL; 1931dddfa461SMichael Halcrow } 1932237fead6SMichael Halcrow if (rc) { 1933f4aad16aSMichael Halcrow struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; 1934f4aad16aSMichael Halcrow 1935f4aad16aSMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error decrypting the " 1936f4aad16aSMichael Halcrow "session key for authentication token with sig " 1937f4aad16aSMichael Halcrow "[%.*s]; rc = [%d]. Removing auth tok " 1938f4aad16aSMichael Halcrow "candidate from the list and searching for " 1939888d57bbSJoe Perches "the next match.\n", ECRYPTFS_SIG_SIZE_HEX, 1940888d57bbSJoe Perches candidate_auth_tok_sig, rc); 1941f4aad16aSMichael Halcrow list_for_each_entry_safe(auth_tok_list_item, 1942f4aad16aSMichael Halcrow auth_tok_list_item_tmp, 1943f4aad16aSMichael Halcrow &auth_tok_list, list) { 1944f4aad16aSMichael Halcrow if (candidate_auth_tok 1945f4aad16aSMichael Halcrow == &auth_tok_list_item->auth_tok) { 1946f4aad16aSMichael Halcrow list_del(&auth_tok_list_item->list); 1947f4aad16aSMichael Halcrow kmem_cache_free( 1948f4aad16aSMichael Halcrow ecryptfs_auth_tok_list_item_cache, 1949f4aad16aSMichael Halcrow auth_tok_list_item); 1950f4aad16aSMichael Halcrow goto find_next_matching_auth_tok; 1951f4aad16aSMichael Halcrow } 1952f4aad16aSMichael Halcrow } 1953f4aad16aSMichael Halcrow BUG(); 1954237fead6SMichael Halcrow } 1955237fead6SMichael Halcrow rc = ecryptfs_compute_root_iv(crypt_stat); 1956237fead6SMichael Halcrow if (rc) { 1957237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error computing " 1958237fead6SMichael Halcrow "the root IV\n"); 1959237fead6SMichael Halcrow goto out_wipe_list; 1960237fead6SMichael Halcrow } 1961237fead6SMichael Halcrow rc = ecryptfs_init_crypt_ctx(crypt_stat); 1962237fead6SMichael Halcrow if (rc) { 1963237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error initializing crypto " 1964237fead6SMichael Halcrow "context for cipher [%s]; rc = [%d]\n", 1965237fead6SMichael Halcrow crypt_stat->cipher, rc); 1966237fead6SMichael Halcrow } 1967237fead6SMichael Halcrow out_wipe_list: 1968237fead6SMichael Halcrow wipe_auth_tok_list(&auth_tok_list); 1969237fead6SMichael Halcrow out: 1970237fead6SMichael Halcrow return rc; 1971237fead6SMichael Halcrow } 1972f4aad16aSMichael Halcrow 1973dddfa461SMichael Halcrow static int 1974b2987a5eSTyler Hicks pki_encrypt_session_key(struct key *auth_tok_key, 1975b2987a5eSTyler Hicks struct ecryptfs_auth_tok *auth_tok, 1976dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 1977dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec) 1978dddfa461SMichael Halcrow { 1979dddfa461SMichael Halcrow struct ecryptfs_msg_ctx *msg_ctx = NULL; 1980624ae528STyler Hicks char *payload = NULL; 198199b373ffSTyler Hicks size_t payload_len = 0; 1982dddfa461SMichael Halcrow struct ecryptfs_message *msg; 1983dddfa461SMichael Halcrow int rc; 1984dddfa461SMichael Halcrow 1985dddfa461SMichael Halcrow rc = write_tag_66_packet(auth_tok->token.private_key.signature, 19869c79f34fSMichael Halcrow ecryptfs_code_for_cipher_string( 19879c79f34fSMichael Halcrow crypt_stat->cipher, 19889c79f34fSMichael Halcrow crypt_stat->key_size), 1989624ae528STyler Hicks crypt_stat, &payload, &payload_len); 1990b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 1991b2987a5eSTyler Hicks key_put(auth_tok_key); 1992dddfa461SMichael Halcrow if (rc) { 1993dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); 1994dddfa461SMichael Halcrow goto out; 1995dddfa461SMichael Halcrow } 1996624ae528STyler Hicks rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); 1997dddfa461SMichael Halcrow if (rc) { 1998624ae528STyler Hicks ecryptfs_printk(KERN_ERR, "Error sending message to " 1999290502beSKees Cook "ecryptfsd: %d\n", rc); 2000dddfa461SMichael Halcrow goto out; 2001dddfa461SMichael Halcrow } 2002dddfa461SMichael Halcrow rc = ecryptfs_wait_for_response(msg_ctx, &msg); 2003dddfa461SMichael Halcrow if (rc) { 2004dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " 2005dddfa461SMichael Halcrow "from the user space daemon\n"); 2006dddfa461SMichael Halcrow rc = -EIO; 2007dddfa461SMichael Halcrow goto out; 2008dddfa461SMichael Halcrow } 2009dddfa461SMichael Halcrow rc = parse_tag_67_packet(key_rec, msg); 2010dddfa461SMichael Halcrow if (rc) 2011dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); 2012dddfa461SMichael Halcrow kfree(msg); 2013dddfa461SMichael Halcrow out: 2014624ae528STyler Hicks kfree(payload); 2015dddfa461SMichael Halcrow return rc; 2016dddfa461SMichael Halcrow } 2017dddfa461SMichael Halcrow /** 2018dddfa461SMichael Halcrow * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet 2019dddfa461SMichael Halcrow * @dest: Buffer into which to write the packet 202022e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be writtn 2021b2987a5eSTyler Hicks * @auth_tok_key: The authentication token key to unlock and put when done with 2022b2987a5eSTyler Hicks * @auth_tok 202322e78fafSMichael Halcrow * @auth_tok: The authentication token used for generating the tag 1 packet 202422e78fafSMichael Halcrow * @crypt_stat: The cryptographic context 202522e78fafSMichael Halcrow * @key_rec: The key record struct for the tag 1 packet 2026dddfa461SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2027dddfa461SMichael Halcrow * up constituting the packet; set to zero on error 2028dddfa461SMichael Halcrow * 2029dddfa461SMichael Halcrow * Returns zero on success; non-zero on error. 2030dddfa461SMichael Halcrow */ 2031dddfa461SMichael Halcrow static int 2032f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes, 2033b2987a5eSTyler Hicks struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok, 2034dddfa461SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2035dddfa461SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2036dddfa461SMichael Halcrow { 2037dddfa461SMichael Halcrow size_t i; 2038dddfa461SMichael Halcrow size_t encrypted_session_key_valid = 0; 2039dddfa461SMichael Halcrow size_t packet_size_length; 2040f4aad16aSMichael Halcrow size_t max_packet_size; 2041dddfa461SMichael Halcrow int rc = 0; 2042dddfa461SMichael Halcrow 2043dddfa461SMichael Halcrow (*packet_size) = 0; 2044dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, 2045dddfa461SMichael Halcrow ECRYPTFS_SIG_SIZE); 2046dddfa461SMichael Halcrow encrypted_session_key_valid = 0; 2047dddfa461SMichael Halcrow for (i = 0; i < crypt_stat->key_size; i++) 2048dddfa461SMichael Halcrow encrypted_session_key_valid |= 2049dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2050dddfa461SMichael Halcrow if (encrypted_session_key_valid) { 2051dddfa461SMichael Halcrow memcpy(key_rec->enc_key, 2052dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key, 2053dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size); 2054b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2055b2987a5eSTyler Hicks key_put(auth_tok_key); 2056dddfa461SMichael Halcrow goto encrypted_session_key_set; 2057dddfa461SMichael Halcrow } 2058dddfa461SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2059dddfa461SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2060dddfa461SMichael Halcrow auth_tok->token.private_key.key_size; 2061b2987a5eSTyler Hicks rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat, 2062b2987a5eSTyler Hicks key_rec); 2063dddfa461SMichael Halcrow if (rc) { 2064f66e883eSMichael Halcrow printk(KERN_ERR "Failed to encrypt session key via a key " 2065f66e883eSMichael Halcrow "module; rc = [%d]\n", rc); 2066dddfa461SMichael Halcrow goto out; 2067dddfa461SMichael Halcrow } 2068dddfa461SMichael Halcrow if (ecryptfs_verbosity > 0) { 2069dddfa461SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); 2070dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); 2071dddfa461SMichael Halcrow } 2072dddfa461SMichael Halcrow encrypted_session_key_set: 2073f4aad16aSMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2074f4aad16aSMichael Halcrow * packet tag 1 */ 2075f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 1 identifier */ 2076f4aad16aSMichael Halcrow + 3 /* Max Tag 1 packet size */ 2077f4aad16aSMichael Halcrow + 1 /* Version */ 2078f4aad16aSMichael Halcrow + ECRYPTFS_SIG_SIZE /* Key identifier */ 2079f4aad16aSMichael Halcrow + 1 /* Cipher identifier */ 2080f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2081f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2082f4aad16aSMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 208381acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2084f4aad16aSMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2085dddfa461SMichael Halcrow rc = -EINVAL; 2086dddfa461SMichael Halcrow goto out; 2087dddfa461SMichael Halcrow } 2088dddfa461SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; 2089f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2090f66e883eSMichael Halcrow (max_packet_size - 4), 2091dddfa461SMichael Halcrow &packet_size_length); 2092dddfa461SMichael Halcrow if (rc) { 2093dddfa461SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " 2094dddfa461SMichael Halcrow "header; cannot generate packet length\n"); 2095dddfa461SMichael Halcrow goto out; 2096dddfa461SMichael Halcrow } 2097dddfa461SMichael Halcrow (*packet_size) += packet_size_length; 2098dddfa461SMichael Halcrow dest[(*packet_size)++] = 0x03; /* version 3 */ 2099dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); 2100dddfa461SMichael Halcrow (*packet_size) += ECRYPTFS_SIG_SIZE; 2101dddfa461SMichael Halcrow dest[(*packet_size)++] = RFC2440_CIPHER_RSA; 2102dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2103dddfa461SMichael Halcrow key_rec->enc_key_size); 2104dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2105dddfa461SMichael Halcrow out: 2106dddfa461SMichael Halcrow if (rc) 2107dddfa461SMichael Halcrow (*packet_size) = 0; 2108f4aad16aSMichael Halcrow else 2109f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2110dddfa461SMichael Halcrow return rc; 2111dddfa461SMichael Halcrow } 2112237fead6SMichael Halcrow 2113237fead6SMichael Halcrow /** 2114237fead6SMichael Halcrow * write_tag_11_packet 2115237fead6SMichael Halcrow * @dest: Target into which Tag 11 packet is to be written 211622e78fafSMichael Halcrow * @remaining_bytes: Maximum packet length 2117237fead6SMichael Halcrow * @contents: Byte array of contents to copy in 2118237fead6SMichael Halcrow * @contents_length: Number of bytes in contents 2119237fead6SMichael Halcrow * @packet_length: Length of the Tag 11 packet written; zero on error 2120237fead6SMichael Halcrow * 2121237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2122237fead6SMichael Halcrow */ 2123237fead6SMichael Halcrow static int 212481acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, 2125146a4606SMichael Halcrow size_t contents_length, size_t *packet_length) 2126237fead6SMichael Halcrow { 2127237fead6SMichael Halcrow size_t packet_size_length; 2128146a4606SMichael Halcrow size_t max_packet_size; 2129dddfa461SMichael Halcrow int rc = 0; 2130237fead6SMichael Halcrow 2131237fead6SMichael Halcrow (*packet_length) = 0; 2132146a4606SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2133146a4606SMichael Halcrow * packet tag 11 */ 2134146a4606SMichael Halcrow max_packet_size = (1 /* Tag 11 identifier */ 2135146a4606SMichael Halcrow + 3 /* Max Tag 11 packet size */ 2136146a4606SMichael Halcrow + 1 /* Binary format specifier */ 2137146a4606SMichael Halcrow + 1 /* Filename length */ 2138146a4606SMichael Halcrow + 8 /* Filename ("_CONSOLE") */ 2139146a4606SMichael Halcrow + 4 /* Modification date */ 2140146a4606SMichael Halcrow + contents_length); /* Literal data */ 2141146a4606SMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 2142146a4606SMichael Halcrow printk(KERN_ERR "Packet length larger than maximum allowable; " 214381acbcd6SAndrew Morton "need up to [%td] bytes, but there are only [%td] " 2144146a4606SMichael Halcrow "available\n", max_packet_size, (*remaining_bytes)); 2145237fead6SMichael Halcrow rc = -EINVAL; 2146237fead6SMichael Halcrow goto out; 2147237fead6SMichael Halcrow } 2148237fead6SMichael Halcrow dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; 2149f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_length)], 2150f66e883eSMichael Halcrow (max_packet_size - 4), 2151f66e883eSMichael Halcrow &packet_size_length); 2152237fead6SMichael Halcrow if (rc) { 2153146a4606SMichael Halcrow printk(KERN_ERR "Error generating tag 11 packet header; cannot " 2154146a4606SMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2155237fead6SMichael Halcrow goto out; 2156237fead6SMichael Halcrow } 2157237fead6SMichael Halcrow (*packet_length) += packet_size_length; 2158146a4606SMichael Halcrow dest[(*packet_length)++] = 0x62; /* binary data format specifier */ 2159237fead6SMichael Halcrow dest[(*packet_length)++] = 8; 2160237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], "_CONSOLE", 8); 2161237fead6SMichael Halcrow (*packet_length) += 8; 2162237fead6SMichael Halcrow memset(&dest[(*packet_length)], 0x00, 4); 2163237fead6SMichael Halcrow (*packet_length) += 4; 2164237fead6SMichael Halcrow memcpy(&dest[(*packet_length)], contents, contents_length); 2165237fead6SMichael Halcrow (*packet_length) += contents_length; 2166237fead6SMichael Halcrow out: 2167237fead6SMichael Halcrow if (rc) 2168237fead6SMichael Halcrow (*packet_length) = 0; 2169146a4606SMichael Halcrow else 2170146a4606SMichael Halcrow (*remaining_bytes) -= (*packet_length); 2171237fead6SMichael Halcrow return rc; 2172237fead6SMichael Halcrow } 2173237fead6SMichael Halcrow 2174237fead6SMichael Halcrow /** 2175237fead6SMichael Halcrow * write_tag_3_packet 2176237fead6SMichael Halcrow * @dest: Buffer into which to write the packet 217722e78fafSMichael Halcrow * @remaining_bytes: Maximum number of bytes that can be written 2178237fead6SMichael Halcrow * @auth_tok: Authentication token 2179237fead6SMichael Halcrow * @crypt_stat: The cryptographic context 2180237fead6SMichael Halcrow * @key_rec: encrypted key 2181237fead6SMichael Halcrow * @packet_size: This function will write the number of bytes that end 2182237fead6SMichael Halcrow * up constituting the packet; set to zero on error 2183237fead6SMichael Halcrow * 2184237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2185237fead6SMichael Halcrow */ 2186237fead6SMichael Halcrow static int 2187f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes, 2188f4aad16aSMichael Halcrow struct ecryptfs_auth_tok *auth_tok, 2189237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2190237fead6SMichael Halcrow struct ecryptfs_key_record *key_rec, size_t *packet_size) 2191237fead6SMichael Halcrow { 2192237fead6SMichael Halcrow size_t i; 2193237fead6SMichael Halcrow size_t encrypted_session_key_valid = 0; 2194237fead6SMichael Halcrow char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; 2195ac97b9f9SMichael Halcrow struct scatterlist dst_sg[2]; 2196ac97b9f9SMichael Halcrow struct scatterlist src_sg[2]; 2197237fead6SMichael Halcrow struct mutex *tfm_mutex = NULL; 219819e66a67STrevor Highland u8 cipher_code; 2199f4aad16aSMichael Halcrow size_t packet_size_length; 2200f4aad16aSMichael Halcrow size_t max_packet_size; 2201f4aad16aSMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2202f4aad16aSMichael Halcrow crypt_stat->mount_crypt_stat; 22033095e8e3SHerbert Xu struct crypto_skcipher *tfm; 22043095e8e3SHerbert Xu struct skcipher_request *req; 22058bba066fSMichael Halcrow int rc = 0; 2206237fead6SMichael Halcrow 2207237fead6SMichael Halcrow (*packet_size) = 0; 2208dddfa461SMichael Halcrow ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, 2209237fead6SMichael Halcrow ECRYPTFS_SIG_SIZE); 22103095e8e3SHerbert Xu rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, 2211f4aad16aSMichael Halcrow crypt_stat->cipher); 2212f4aad16aSMichael Halcrow if (unlikely(rc)) { 2213f4aad16aSMichael Halcrow printk(KERN_ERR "Internal error whilst attempting to get " 2214f4aad16aSMichael Halcrow "tfm and mutex for cipher name [%s]; rc = [%d]\n", 2215f4aad16aSMichael Halcrow crypt_stat->cipher, rc); 2216f4aad16aSMichael Halcrow goto out; 2217237fead6SMichael Halcrow } 2218f4aad16aSMichael Halcrow if (mount_crypt_stat->global_default_cipher_key_size == 0) { 2219f4aad16aSMichael Halcrow printk(KERN_WARNING "No key size specified at mount; " 22203095e8e3SHerbert Xu "defaulting to [%d]\n", 22213095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm)); 2222f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size = 22233095e8e3SHerbert Xu crypto_skcipher_default_keysize(tfm); 2224f4aad16aSMichael Halcrow } 2225f4aad16aSMichael Halcrow if (crypt_stat->key_size == 0) 2226f4aad16aSMichael Halcrow crypt_stat->key_size = 2227f4aad16aSMichael Halcrow mount_crypt_stat->global_default_cipher_key_size; 2228237fead6SMichael Halcrow if (auth_tok->session_key.encrypted_key_size == 0) 2229237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 2230237fead6SMichael Halcrow crypt_stat->key_size; 2231237fead6SMichael Halcrow if (crypt_stat->key_size == 24 2232237fead6SMichael Halcrow && strcmp("aes", crypt_stat->cipher) == 0) { 2233237fead6SMichael Halcrow memset((crypt_stat->key + 24), 0, 8); 2234237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size = 32; 2235f4aad16aSMichael Halcrow } else 2236f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; 2237dddfa461SMichael Halcrow key_rec->enc_key_size = 2238237fead6SMichael Halcrow auth_tok->session_key.encrypted_key_size; 2239f4aad16aSMichael Halcrow encrypted_session_key_valid = 0; 2240f4aad16aSMichael Halcrow for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) 2241f4aad16aSMichael Halcrow encrypted_session_key_valid |= 2242f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key[i]; 2243f4aad16aSMichael Halcrow if (encrypted_session_key_valid) { 2244f4aad16aSMichael Halcrow ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " 2245f4aad16aSMichael Halcrow "using auth_tok->session_key.encrypted_key, " 2246f24b3887STyler Hicks "where key_rec->enc_key_size = [%zd]\n", 2247f4aad16aSMichael Halcrow key_rec->enc_key_size); 2248f4aad16aSMichael Halcrow memcpy(key_rec->enc_key, 2249f4aad16aSMichael Halcrow auth_tok->session_key.encrypted_key, 2250f4aad16aSMichael Halcrow key_rec->enc_key_size); 2251f4aad16aSMichael Halcrow goto encrypted_session_key_set; 2252f4aad16aSMichael Halcrow } 2253dddfa461SMichael Halcrow if (auth_tok->token.password.flags & 2254dddfa461SMichael Halcrow ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { 2255237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Using previously generated " 2256237fead6SMichael Halcrow "session key encryption key of size [%d]\n", 2257237fead6SMichael Halcrow auth_tok->token.password. 2258237fead6SMichael Halcrow session_key_encryption_key_bytes); 2259237fead6SMichael Halcrow memcpy(session_key_encryption_key, 2260237fead6SMichael Halcrow auth_tok->token.password.session_key_encryption_key, 2261237fead6SMichael Halcrow crypt_stat->key_size); 2262237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, 2263df2e301fSJean Delvare "Cached session key encryption key:\n"); 2264237fead6SMichael Halcrow if (ecryptfs_verbosity > 0) 2265237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2266237fead6SMichael Halcrow } 2267237fead6SMichael Halcrow if (unlikely(ecryptfs_verbosity > 0)) { 2268237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); 2269237fead6SMichael Halcrow ecryptfs_dump_hex(session_key_encryption_key, 16); 2270237fead6SMichael Halcrow } 22715dda6992SMichael Halcrow rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, 2272ac97b9f9SMichael Halcrow src_sg, 2); 2273ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2274237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2275f4aad16aSMichael Halcrow "for crypt_stat session key; expected rc = 1; " 2276f24b3887STyler Hicks "got rc = [%d]. key_rec->enc_key_size = [%zd]\n", 2277f4aad16aSMichael Halcrow rc, key_rec->enc_key_size); 2278237fead6SMichael Halcrow rc = -ENOMEM; 2279237fead6SMichael Halcrow goto out; 2280237fead6SMichael Halcrow } 22815dda6992SMichael Halcrow rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, 2282ac97b9f9SMichael Halcrow dst_sg, 2); 2283ac97b9f9SMichael Halcrow if (rc < 1 || rc > 2) { 2284237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error generating scatterlist " 2285f4aad16aSMichael Halcrow "for crypt_stat encrypted session key; " 2286f4aad16aSMichael Halcrow "expected rc = 1; got rc = [%d]. " 2287f24b3887STyler Hicks "key_rec->enc_key_size = [%zd]\n", rc, 2288f4aad16aSMichael Halcrow key_rec->enc_key_size); 2289237fead6SMichael Halcrow rc = -ENOMEM; 2290237fead6SMichael Halcrow goto out; 2291237fead6SMichael Halcrow } 2292237fead6SMichael Halcrow mutex_lock(tfm_mutex); 22933095e8e3SHerbert Xu rc = crypto_skcipher_setkey(tfm, session_key_encryption_key, 2294237fead6SMichael Halcrow crypt_stat->key_size); 2295237fead6SMichael Halcrow if (rc < 0) { 2296237fead6SMichael Halcrow mutex_unlock(tfm_mutex); 2297237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error setting key for crypto " 22988bba066fSMichael Halcrow "context; rc = [%d]\n", rc); 2299237fead6SMichael Halcrow goto out; 2300237fead6SMichael Halcrow } 23013095e8e3SHerbert Xu 23023095e8e3SHerbert Xu req = skcipher_request_alloc(tfm, GFP_KERNEL); 23033095e8e3SHerbert Xu if (!req) { 23043095e8e3SHerbert Xu mutex_unlock(tfm_mutex); 23053095e8e3SHerbert Xu ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst " 23063095e8e3SHerbert Xu "attempting to skcipher_request_alloc for " 23073095e8e3SHerbert Xu "%s\n", crypto_skcipher_driver_name(tfm)); 23083095e8e3SHerbert Xu rc = -ENOMEM; 23093095e8e3SHerbert Xu goto out; 23103095e8e3SHerbert Xu } 23113095e8e3SHerbert Xu 23123095e8e3SHerbert Xu skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, 23133095e8e3SHerbert Xu NULL, NULL); 23143095e8e3SHerbert Xu 2315237fead6SMichael Halcrow rc = 0; 2316f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n", 2317237fead6SMichael Halcrow crypt_stat->key_size); 23183095e8e3SHerbert Xu skcipher_request_set_crypt(req, src_sg, dst_sg, 23193095e8e3SHerbert Xu (*key_rec).enc_key_size, NULL); 23203095e8e3SHerbert Xu rc = crypto_skcipher_encrypt(req); 2321f4aad16aSMichael Halcrow mutex_unlock(tfm_mutex); 23223095e8e3SHerbert Xu skcipher_request_free(req); 23238bba066fSMichael Halcrow if (rc) { 23248bba066fSMichael Halcrow printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); 23258bba066fSMichael Halcrow goto out; 23268bba066fSMichael Halcrow } 2327237fead6SMichael Halcrow ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); 2328f4aad16aSMichael Halcrow if (ecryptfs_verbosity > 0) { 2329f24b3887STyler Hicks ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n", 2330f4aad16aSMichael Halcrow key_rec->enc_key_size); 2331dddfa461SMichael Halcrow ecryptfs_dump_hex(key_rec->enc_key, 2332dddfa461SMichael Halcrow key_rec->enc_key_size); 2333f4aad16aSMichael Halcrow } 2334237fead6SMichael Halcrow encrypted_session_key_set: 2335237fead6SMichael Halcrow /* This format is inspired by OpenPGP; see RFC 2440 2336237fead6SMichael Halcrow * packet tag 3 */ 2337f4aad16aSMichael Halcrow max_packet_size = (1 /* Tag 3 identifier */ 2338f4aad16aSMichael Halcrow + 3 /* Max Tag 3 packet size */ 2339f4aad16aSMichael Halcrow + 1 /* Version */ 2340f4aad16aSMichael Halcrow + 1 /* Cipher code */ 2341f4aad16aSMichael Halcrow + 1 /* S2K specifier */ 2342f4aad16aSMichael Halcrow + 1 /* Hash identifier */ 2343f4aad16aSMichael Halcrow + ECRYPTFS_SALT_SIZE /* Salt */ 2344f4aad16aSMichael Halcrow + 1 /* Hash iterations */ 2345f4aad16aSMichael Halcrow + key_rec->enc_key_size); /* Encrypted key size */ 2346f4aad16aSMichael Halcrow if (max_packet_size > (*remaining_bytes)) { 234781acbcd6SAndrew Morton printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " 234881acbcd6SAndrew Morton "there are only [%td] available\n", max_packet_size, 2349f4aad16aSMichael Halcrow (*remaining_bytes)); 2350f4aad16aSMichael Halcrow rc = -EINVAL; 2351f4aad16aSMichael Halcrow goto out; 2352f4aad16aSMichael Halcrow } 2353237fead6SMichael Halcrow dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; 2354f4aad16aSMichael Halcrow /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) 2355f4aad16aSMichael Halcrow * to get the number of octets in the actual Tag 3 packet */ 2356f66e883eSMichael Halcrow rc = ecryptfs_write_packet_length(&dest[(*packet_size)], 2357f66e883eSMichael Halcrow (max_packet_size - 4), 2358237fead6SMichael Halcrow &packet_size_length); 2359237fead6SMichael Halcrow if (rc) { 2360f4aad16aSMichael Halcrow printk(KERN_ERR "Error generating tag 3 packet header; cannot " 2361f4aad16aSMichael Halcrow "generate packet length. rc = [%d]\n", rc); 2362237fead6SMichael Halcrow goto out; 2363237fead6SMichael Halcrow } 2364237fead6SMichael Halcrow (*packet_size) += packet_size_length; 2365237fead6SMichael Halcrow dest[(*packet_size)++] = 0x04; /* version 4 */ 2366f4aad16aSMichael Halcrow /* TODO: Break from RFC2440 so that arbitrary ciphers can be 2367f4aad16aSMichael Halcrow * specified with strings */ 23689c79f34fSMichael Halcrow cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, 23699c79f34fSMichael Halcrow crypt_stat->key_size); 2370237fead6SMichael Halcrow if (cipher_code == 0) { 2371237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unable to generate code for " 2372237fead6SMichael Halcrow "cipher [%s]\n", crypt_stat->cipher); 2373237fead6SMichael Halcrow rc = -EINVAL; 2374237fead6SMichael Halcrow goto out; 2375237fead6SMichael Halcrow } 2376237fead6SMichael Halcrow dest[(*packet_size)++] = cipher_code; 2377237fead6SMichael Halcrow dest[(*packet_size)++] = 0x03; /* S2K */ 2378237fead6SMichael Halcrow dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ 2379237fead6SMichael Halcrow memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, 2380237fead6SMichael Halcrow ECRYPTFS_SALT_SIZE); 2381237fead6SMichael Halcrow (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ 2382237fead6SMichael Halcrow dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ 2383dddfa461SMichael Halcrow memcpy(&dest[(*packet_size)], key_rec->enc_key, 2384dddfa461SMichael Halcrow key_rec->enc_key_size); 2385dddfa461SMichael Halcrow (*packet_size) += key_rec->enc_key_size; 2386237fead6SMichael Halcrow out: 2387237fead6SMichael Halcrow if (rc) 2388237fead6SMichael Halcrow (*packet_size) = 0; 2389f4aad16aSMichael Halcrow else 2390f4aad16aSMichael Halcrow (*remaining_bytes) -= (*packet_size); 2391237fead6SMichael Halcrow return rc; 2392237fead6SMichael Halcrow } 2393237fead6SMichael Halcrow 2394eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache; 2395eb95e7ffSMichael Halcrow 2396237fead6SMichael Halcrow /** 2397237fead6SMichael Halcrow * ecryptfs_generate_key_packet_set 239822e78fafSMichael Halcrow * @dest_base: Virtual address from which to write the key record set 2399237fead6SMichael Halcrow * @crypt_stat: The cryptographic context from which the 2400237fead6SMichael Halcrow * authentication tokens will be retrieved 2401237fead6SMichael Halcrow * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat 2402237fead6SMichael Halcrow * for the global parameters 2403237fead6SMichael Halcrow * @len: The amount written 2404237fead6SMichael Halcrow * @max: The maximum amount of data allowed to be written 2405237fead6SMichael Halcrow * 2406237fead6SMichael Halcrow * Generates a key packet set and writes it to the virtual address 2407237fead6SMichael Halcrow * passed in. 2408237fead6SMichael Halcrow * 2409237fead6SMichael Halcrow * Returns zero on success; non-zero on error. 2410237fead6SMichael Halcrow */ 2411237fead6SMichael Halcrow int 2412237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base, 2413237fead6SMichael Halcrow struct ecryptfs_crypt_stat *crypt_stat, 2414237fead6SMichael Halcrow struct dentry *ecryptfs_dentry, size_t *len, 2415237fead6SMichael Halcrow size_t max) 2416237fead6SMichael Halcrow { 2417237fead6SMichael Halcrow struct ecryptfs_auth_tok *auth_tok; 24180e1fc5efSRoberto Sassu struct key *auth_tok_key = NULL; 2419237fead6SMichael Halcrow struct ecryptfs_mount_crypt_stat *mount_crypt_stat = 2420237fead6SMichael Halcrow &ecryptfs_superblock_to_private( 2421237fead6SMichael Halcrow ecryptfs_dentry->d_sb)->mount_crypt_stat; 2422237fead6SMichael Halcrow size_t written; 2423eb95e7ffSMichael Halcrow struct ecryptfs_key_record *key_rec; 2424f4aad16aSMichael Halcrow struct ecryptfs_key_sig *key_sig; 2425dddfa461SMichael Halcrow int rc = 0; 2426237fead6SMichael Halcrow 2427237fead6SMichael Halcrow (*len) = 0; 2428f4aad16aSMichael Halcrow mutex_lock(&crypt_stat->keysig_list_mutex); 2429eb95e7ffSMichael Halcrow key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); 2430eb95e7ffSMichael Halcrow if (!key_rec) { 2431eb95e7ffSMichael Halcrow rc = -ENOMEM; 2432eb95e7ffSMichael Halcrow goto out; 2433eb95e7ffSMichael Halcrow } 2434f4aad16aSMichael Halcrow list_for_each_entry(key_sig, &crypt_stat->keysig_list, 2435f4aad16aSMichael Halcrow crypt_stat_list) { 2436f4aad16aSMichael Halcrow memset(key_rec, 0, sizeof(*key_rec)); 24370e1fc5efSRoberto Sassu rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key, 24380e1fc5efSRoberto Sassu &auth_tok, 2439f4aad16aSMichael Halcrow mount_crypt_stat, 2440f4aad16aSMichael Halcrow key_sig->keysig); 2441f4aad16aSMichael Halcrow if (rc) { 24420e1fc5efSRoberto Sassu printk(KERN_WARNING "Unable to retrieve auth tok with " 24430e1fc5efSRoberto Sassu "sig = [%s]\n", key_sig->keysig); 24440e1fc5efSRoberto Sassu rc = process_find_global_auth_tok_for_sig_err(rc); 2445f4aad16aSMichael Halcrow goto out_free; 2446f4aad16aSMichael Halcrow } 2447237fead6SMichael Halcrow if (auth_tok->token_type == ECRYPTFS_PASSWORD) { 2448237fead6SMichael Halcrow rc = write_tag_3_packet((dest_base + (*len)), 2449f4aad16aSMichael Halcrow &max, auth_tok, 2450eb95e7ffSMichael Halcrow crypt_stat, key_rec, 2451237fead6SMichael Halcrow &written); 2452b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2453b2987a5eSTyler Hicks key_put(auth_tok_key); 2454237fead6SMichael Halcrow if (rc) { 2455237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2456237fead6SMichael Halcrow "writing tag 3 packet\n"); 2457eb95e7ffSMichael Halcrow goto out_free; 2458237fead6SMichael Halcrow } 2459237fead6SMichael Halcrow (*len) += written; 2460237fead6SMichael Halcrow /* Write auth tok signature packet */ 2461f4aad16aSMichael Halcrow rc = write_tag_11_packet((dest_base + (*len)), &max, 2462f4aad16aSMichael Halcrow key_rec->sig, 2463f4aad16aSMichael Halcrow ECRYPTFS_SIG_SIZE, &written); 2464237fead6SMichael Halcrow if (rc) { 2465237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing " 2466237fead6SMichael Halcrow "auth tok signature packet\n"); 2467eb95e7ffSMichael Halcrow goto out_free; 2468237fead6SMichael Halcrow } 2469237fead6SMichael Halcrow (*len) += written; 2470dddfa461SMichael Halcrow } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { 2471b2987a5eSTyler Hicks rc = write_tag_1_packet(dest_base + (*len), &max, 2472b2987a5eSTyler Hicks auth_tok_key, auth_tok, 2473f4aad16aSMichael Halcrow crypt_stat, key_rec, &written); 2474dddfa461SMichael Halcrow if (rc) { 2475dddfa461SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Error " 2476dddfa461SMichael Halcrow "writing tag 1 packet\n"); 2477eb95e7ffSMichael Halcrow goto out_free; 2478dddfa461SMichael Halcrow } 2479dddfa461SMichael Halcrow (*len) += written; 2480237fead6SMichael Halcrow } else { 2481b2987a5eSTyler Hicks up_write(&(auth_tok_key->sem)); 2482b2987a5eSTyler Hicks key_put(auth_tok_key); 2483237fead6SMichael Halcrow ecryptfs_printk(KERN_WARNING, "Unsupported " 2484237fead6SMichael Halcrow "authentication token type\n"); 2485237fead6SMichael Halcrow rc = -EINVAL; 2486eb95e7ffSMichael Halcrow goto out_free; 2487237fead6SMichael Halcrow } 2488f4aad16aSMichael Halcrow } 2489f4aad16aSMichael Halcrow if (likely(max > 0)) { 2490237fead6SMichael Halcrow dest_base[(*len)] = 0x00; 2491237fead6SMichael Halcrow } else { 2492237fead6SMichael Halcrow ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); 2493237fead6SMichael Halcrow rc = -EIO; 2494237fead6SMichael Halcrow } 2495eb95e7ffSMichael Halcrow out_free: 2496eb95e7ffSMichael Halcrow kmem_cache_free(ecryptfs_key_record_cache, key_rec); 2497237fead6SMichael Halcrow out: 2498237fead6SMichael Halcrow if (rc) 2499237fead6SMichael Halcrow (*len) = 0; 2500f4aad16aSMichael Halcrow mutex_unlock(&crypt_stat->keysig_list_mutex); 2501237fead6SMichael Halcrow return rc; 2502237fead6SMichael Halcrow } 2503f4aad16aSMichael Halcrow 2504f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache; 2505f4aad16aSMichael Halcrow 2506f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) 2507f4aad16aSMichael Halcrow { 2508f4aad16aSMichael Halcrow struct ecryptfs_key_sig *new_key_sig; 2509f4aad16aSMichael Halcrow 2510f4aad16aSMichael Halcrow new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); 2511*1a0bba4fSMarkus Elfring if (!new_key_sig) 2512aa06117fSRoland Dreier return -ENOMEM; 2513*1a0bba4fSMarkus Elfring 2514f4aad16aSMichael Halcrow memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); 25157762e230SRoberto Sassu new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2516aa06117fSRoland Dreier /* Caller must hold keysig_list_mutex */ 2517f4aad16aSMichael Halcrow list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); 2518aa06117fSRoland Dreier 2519aa06117fSRoland Dreier return 0; 2520f4aad16aSMichael Halcrow } 2521f4aad16aSMichael Halcrow 2522f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache; 2523f4aad16aSMichael Halcrow 2524f4aad16aSMichael Halcrow int 2525f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, 252684814d64STyler Hicks char *sig, u32 global_auth_tok_flags) 2527f4aad16aSMichael Halcrow { 2528f4aad16aSMichael Halcrow struct ecryptfs_global_auth_tok *new_auth_tok; 2529f4aad16aSMichael Halcrow int rc = 0; 2530f4aad16aSMichael Halcrow 2531459e2164SEric Sandeen new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, 2532f4aad16aSMichael Halcrow GFP_KERNEL); 2533f4aad16aSMichael Halcrow if (!new_auth_tok) { 2534f4aad16aSMichael Halcrow rc = -ENOMEM; 2535f4aad16aSMichael Halcrow goto out; 2536f4aad16aSMichael Halcrow } 2537f4aad16aSMichael Halcrow memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); 253884814d64STyler Hicks new_auth_tok->flags = global_auth_tok_flags; 2539f4aad16aSMichael Halcrow new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; 2540f4aad16aSMichael Halcrow mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); 2541f4aad16aSMichael Halcrow list_add(&new_auth_tok->mount_crypt_stat_list, 2542f4aad16aSMichael Halcrow &mount_crypt_stat->global_auth_tok_list); 2543f4aad16aSMichael Halcrow mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); 2544f4aad16aSMichael Halcrow out: 2545f4aad16aSMichael Halcrow return rc; 2546f4aad16aSMichael Halcrow } 2547f4aad16aSMichael Halcrow 2548