xref: /openbmc/linux/fs/ecryptfs/keystore.c (revision 624ae528)
1237fead6SMichael Halcrow /**
2237fead6SMichael Halcrow  * eCryptfs: Linux filesystem encryption layer
3237fead6SMichael Halcrow  * In-kernel key management code.  Includes functions to parse and
4237fead6SMichael Halcrow  * write authentication token-related packets with the underlying
5237fead6SMichael Halcrow  * file.
6237fead6SMichael Halcrow  *
7237fead6SMichael Halcrow  * Copyright (C) 2004-2006 International Business Machines Corp.
8237fead6SMichael Halcrow  *   Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9237fead6SMichael Halcrow  *              Michael C. Thompson <mcthomps@us.ibm.com>
10dddfa461SMichael Halcrow  *              Trevor S. Highland <trevor.highland@gmail.com>
11237fead6SMichael Halcrow  *
12237fead6SMichael Halcrow  * This program is free software; you can redistribute it and/or
13237fead6SMichael Halcrow  * modify it under the terms of the GNU General Public License as
14237fead6SMichael Halcrow  * published by the Free Software Foundation; either version 2 of the
15237fead6SMichael Halcrow  * License, or (at your option) any later version.
16237fead6SMichael Halcrow  *
17237fead6SMichael Halcrow  * This program is distributed in the hope that it will be useful, but
18237fead6SMichael Halcrow  * WITHOUT ANY WARRANTY; without even the implied warranty of
19237fead6SMichael Halcrow  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20237fead6SMichael Halcrow  * General Public License for more details.
21237fead6SMichael Halcrow  *
22237fead6SMichael Halcrow  * You should have received a copy of the GNU General Public License
23237fead6SMichael Halcrow  * along with this program; if not, write to the Free Software
24237fead6SMichael Halcrow  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25237fead6SMichael Halcrow  * 02111-1307, USA.
26237fead6SMichael Halcrow  */
27237fead6SMichael Halcrow 
28237fead6SMichael Halcrow #include <linux/string.h>
29237fead6SMichael Halcrow #include <linux/syscalls.h>
30237fead6SMichael Halcrow #include <linux/pagemap.h>
31237fead6SMichael Halcrow #include <linux/key.h>
32237fead6SMichael Halcrow #include <linux/random.h>
33237fead6SMichael Halcrow #include <linux/crypto.h>
34237fead6SMichael Halcrow #include <linux/scatterlist.h>
35237fead6SMichael Halcrow #include "ecryptfs_kernel.h"
36237fead6SMichael Halcrow 
37237fead6SMichael Halcrow /**
38237fead6SMichael Halcrow  * request_key returned an error instead of a valid key address;
39237fead6SMichael Halcrow  * determine the type of error, make appropriate log entries, and
40237fead6SMichael Halcrow  * return an error code.
41237fead6SMichael Halcrow  */
42cd9d67dfSMichael Halcrow static int process_request_key_err(long err_code)
43237fead6SMichael Halcrow {
44237fead6SMichael Halcrow 	int rc = 0;
45237fead6SMichael Halcrow 
46237fead6SMichael Halcrow 	switch (err_code) {
47982363c9SEric Sandeen 	case -ENOKEY:
48237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "No key\n");
49237fead6SMichael Halcrow 		rc = -ENOENT;
50237fead6SMichael Halcrow 		break;
51982363c9SEric Sandeen 	case -EKEYEXPIRED:
52237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Key expired\n");
53237fead6SMichael Halcrow 		rc = -ETIME;
54237fead6SMichael Halcrow 		break;
55982363c9SEric Sandeen 	case -EKEYREVOKED:
56237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Key revoked\n");
57237fead6SMichael Halcrow 		rc = -EINVAL;
58237fead6SMichael Halcrow 		break;
59237fead6SMichael Halcrow 	default:
60237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Unknown error code: "
61237fead6SMichael Halcrow 				"[0x%.16x]\n", err_code);
62237fead6SMichael Halcrow 		rc = -EINVAL;
63237fead6SMichael Halcrow 	}
64237fead6SMichael Halcrow 	return rc;
65237fead6SMichael Halcrow }
66237fead6SMichael Halcrow 
67237fead6SMichael Halcrow /**
68f66e883eSMichael Halcrow  * ecryptfs_parse_packet_length
69237fead6SMichael Halcrow  * @data: Pointer to memory containing length at offset
70237fead6SMichael Halcrow  * @size: This function writes the decoded size to this memory
71237fead6SMichael Halcrow  *        address; zero on error
72237fead6SMichael Halcrow  * @length_size: The number of bytes occupied by the encoded length
73237fead6SMichael Halcrow  *
7422e78fafSMichael Halcrow  * Returns zero on success; non-zero on error
75237fead6SMichael Halcrow  */
76f66e883eSMichael Halcrow int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
77237fead6SMichael Halcrow 				 size_t *length_size)
78237fead6SMichael Halcrow {
79237fead6SMichael Halcrow 	int rc = 0;
80237fead6SMichael Halcrow 
81237fead6SMichael Halcrow 	(*length_size) = 0;
82237fead6SMichael Halcrow 	(*size) = 0;
83237fead6SMichael Halcrow 	if (data[0] < 192) {
84237fead6SMichael Halcrow 		/* One-byte length */
85dddfa461SMichael Halcrow 		(*size) = (unsigned char)data[0];
86237fead6SMichael Halcrow 		(*length_size) = 1;
87237fead6SMichael Halcrow 	} else if (data[0] < 224) {
88237fead6SMichael Halcrow 		/* Two-byte length */
89dddfa461SMichael Halcrow 		(*size) = (((unsigned char)(data[0]) - 192) * 256);
90dddfa461SMichael Halcrow 		(*size) += ((unsigned char)(data[1]) + 192);
91237fead6SMichael Halcrow 		(*length_size) = 2;
92237fead6SMichael Halcrow 	} else if (data[0] == 255) {
93237fead6SMichael Halcrow 		/* Five-byte length; we're not supposed to see this */
94237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
95237fead6SMichael Halcrow 				"supported\n");
96237fead6SMichael Halcrow 		rc = -EINVAL;
97237fead6SMichael Halcrow 		goto out;
98237fead6SMichael Halcrow 	} else {
99237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
100237fead6SMichael Halcrow 		rc = -EINVAL;
101237fead6SMichael Halcrow 		goto out;
102237fead6SMichael Halcrow 	}
103237fead6SMichael Halcrow out:
104237fead6SMichael Halcrow 	return rc;
105237fead6SMichael Halcrow }
106237fead6SMichael Halcrow 
107237fead6SMichael Halcrow /**
108f66e883eSMichael Halcrow  * ecryptfs_write_packet_length
10922e78fafSMichael Halcrow  * @dest: The byte array target into which to write the length. Must
11022e78fafSMichael Halcrow  *        have at least 5 bytes allocated.
111237fead6SMichael Halcrow  * @size: The length to write.
11222e78fafSMichael Halcrow  * @packet_size_length: The number of bytes used to encode the packet
11322e78fafSMichael Halcrow  *                      length is written to this address.
114237fead6SMichael Halcrow  *
115237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
116237fead6SMichael Halcrow  */
117f66e883eSMichael Halcrow int ecryptfs_write_packet_length(char *dest, size_t size,
118237fead6SMichael Halcrow 				 size_t *packet_size_length)
119237fead6SMichael Halcrow {
120237fead6SMichael Halcrow 	int rc = 0;
121237fead6SMichael Halcrow 
122237fead6SMichael Halcrow 	if (size < 192) {
123237fead6SMichael Halcrow 		dest[0] = size;
124237fead6SMichael Halcrow 		(*packet_size_length) = 1;
125237fead6SMichael Halcrow 	} else if (size < 65536) {
126237fead6SMichael Halcrow 		dest[0] = (((size - 192) / 256) + 192);
127237fead6SMichael Halcrow 		dest[1] = ((size - 192) % 256);
128237fead6SMichael Halcrow 		(*packet_size_length) = 2;
129237fead6SMichael Halcrow 	} else {
130237fead6SMichael Halcrow 		rc = -EINVAL;
131237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING,
132237fead6SMichael Halcrow 				"Unsupported packet size: [%d]\n", size);
133237fead6SMichael Halcrow 	}
134237fead6SMichael Halcrow 	return rc;
135237fead6SMichael Halcrow }
136237fead6SMichael Halcrow 
137dddfa461SMichael Halcrow static int
138dddfa461SMichael Halcrow write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
139dddfa461SMichael Halcrow 		    char **packet, size_t *packet_len)
140dddfa461SMichael Halcrow {
141dddfa461SMichael Halcrow 	size_t i = 0;
142dddfa461SMichael Halcrow 	size_t data_len;
143dddfa461SMichael Halcrow 	size_t packet_size_len;
144dddfa461SMichael Halcrow 	char *message;
145dddfa461SMichael Halcrow 	int rc;
146dddfa461SMichael Halcrow 
147dddfa461SMichael Halcrow 	/*
148dddfa461SMichael Halcrow 	 *              ***** TAG 64 Packet Format *****
149dddfa461SMichael Halcrow 	 *    | Content Type                       | 1 byte       |
150dddfa461SMichael Halcrow 	 *    | Key Identifier Size                | 1 or 2 bytes |
151dddfa461SMichael Halcrow 	 *    | Key Identifier                     | arbitrary    |
152dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
153dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key      | arbitrary    |
154dddfa461SMichael Halcrow 	 */
155dddfa461SMichael Halcrow 	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
156dddfa461SMichael Halcrow 		    + session_key->encrypted_key_size);
157dddfa461SMichael Halcrow 	*packet = kmalloc(data_len, GFP_KERNEL);
158dddfa461SMichael Halcrow 	message = *packet;
159dddfa461SMichael Halcrow 	if (!message) {
160dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
161dddfa461SMichael Halcrow 		rc = -ENOMEM;
162dddfa461SMichael Halcrow 		goto out;
163dddfa461SMichael Halcrow 	}
164dddfa461SMichael Halcrow 	message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
165f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166dddfa461SMichael Halcrow 					  &packet_size_len);
167dddfa461SMichael Halcrow 	if (rc) {
168dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
169dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
170dddfa461SMichael Halcrow 		goto out;
171dddfa461SMichael Halcrow 	}
172dddfa461SMichael Halcrow 	i += packet_size_len;
173dddfa461SMichael Halcrow 	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
174dddfa461SMichael Halcrow 	i += ECRYPTFS_SIG_SIZE_HEX;
175f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i],
176f66e883eSMichael Halcrow 					  session_key->encrypted_key_size,
177dddfa461SMichael Halcrow 					  &packet_size_len);
178dddfa461SMichael Halcrow 	if (rc) {
179dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
180dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
181dddfa461SMichael Halcrow 		goto out;
182dddfa461SMichael Halcrow 	}
183dddfa461SMichael Halcrow 	i += packet_size_len;
184dddfa461SMichael Halcrow 	memcpy(&message[i], session_key->encrypted_key,
185dddfa461SMichael Halcrow 	       session_key->encrypted_key_size);
186dddfa461SMichael Halcrow 	i += session_key->encrypted_key_size;
187dddfa461SMichael Halcrow 	*packet_len = i;
188dddfa461SMichael Halcrow out:
189dddfa461SMichael Halcrow 	return rc;
190dddfa461SMichael Halcrow }
191dddfa461SMichael Halcrow 
192dddfa461SMichael Halcrow static int
19319e66a67STrevor Highland parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
194dddfa461SMichael Halcrow 		    struct ecryptfs_message *msg)
195dddfa461SMichael Halcrow {
196dddfa461SMichael Halcrow 	size_t i = 0;
197dddfa461SMichael Halcrow 	char *data;
198dddfa461SMichael Halcrow 	size_t data_len;
199dddfa461SMichael Halcrow 	size_t m_size;
200dddfa461SMichael Halcrow 	size_t message_len;
201dddfa461SMichael Halcrow 	u16 checksum = 0;
202dddfa461SMichael Halcrow 	u16 expected_checksum = 0;
203dddfa461SMichael Halcrow 	int rc;
204dddfa461SMichael Halcrow 
205dddfa461SMichael Halcrow 	/*
206dddfa461SMichael Halcrow 	 *              ***** TAG 65 Packet Format *****
207dddfa461SMichael Halcrow 	 *         | Content Type             | 1 byte       |
208dddfa461SMichael Halcrow 	 *         | Status Indicator         | 1 byte       |
209dddfa461SMichael Halcrow 	 *         | File Encryption Key Size | 1 or 2 bytes |
210dddfa461SMichael Halcrow 	 *         | File Encryption Key      | arbitrary    |
211dddfa461SMichael Halcrow 	 */
212dddfa461SMichael Halcrow 	message_len = msg->data_len;
213dddfa461SMichael Halcrow 	data = msg->data;
214dddfa461SMichael Halcrow 	if (message_len < 4) {
215dddfa461SMichael Halcrow 		rc = -EIO;
216dddfa461SMichael Halcrow 		goto out;
217dddfa461SMichael Halcrow 	}
218dddfa461SMichael Halcrow 	if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
219dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
220dddfa461SMichael Halcrow 		rc = -EIO;
221dddfa461SMichael Halcrow 		goto out;
222dddfa461SMichael Halcrow 	}
223dddfa461SMichael Halcrow 	if (data[i++]) {
224dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
225dddfa461SMichael Halcrow 				"[%d]\n", data[i-1]);
226dddfa461SMichael Halcrow 		rc = -EIO;
227dddfa461SMichael Halcrow 		goto out;
228dddfa461SMichael Halcrow 	}
229f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
230dddfa461SMichael Halcrow 	if (rc) {
231dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
232dddfa461SMichael Halcrow 				"rc = [%d]\n", rc);
233dddfa461SMichael Halcrow 		goto out;
234dddfa461SMichael Halcrow 	}
235dddfa461SMichael Halcrow 	i += data_len;
236dddfa461SMichael Halcrow 	if (message_len < (i + m_size)) {
237624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd "
238624ae528STyler Hicks 				"is shorter than expected\n");
239dddfa461SMichael Halcrow 		rc = -EIO;
240dddfa461SMichael Halcrow 		goto out;
241dddfa461SMichael Halcrow 	}
242dddfa461SMichael Halcrow 	if (m_size < 3) {
243dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR,
244dddfa461SMichael Halcrow 				"The decrypted key is not long enough to "
245dddfa461SMichael Halcrow 				"include a cipher code and checksum\n");
246dddfa461SMichael Halcrow 		rc = -EIO;
247dddfa461SMichael Halcrow 		goto out;
248dddfa461SMichael Halcrow 	}
249dddfa461SMichael Halcrow 	*cipher_code = data[i++];
250dddfa461SMichael Halcrow 	/* The decrypted key includes 1 byte cipher code and 2 byte checksum */
251dddfa461SMichael Halcrow 	session_key->decrypted_key_size = m_size - 3;
252dddfa461SMichael Halcrow 	if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
253dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
254dddfa461SMichael Halcrow 				"the maximum key size [%d]\n",
255dddfa461SMichael Halcrow 				session_key->decrypted_key_size,
256dddfa461SMichael Halcrow 				ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
257dddfa461SMichael Halcrow 		rc = -EIO;
258dddfa461SMichael Halcrow 		goto out;
259dddfa461SMichael Halcrow 	}
260dddfa461SMichael Halcrow 	memcpy(session_key->decrypted_key, &data[i],
261dddfa461SMichael Halcrow 	       session_key->decrypted_key_size);
262dddfa461SMichael Halcrow 	i += session_key->decrypted_key_size;
263dddfa461SMichael Halcrow 	expected_checksum += (unsigned char)(data[i++]) << 8;
264dddfa461SMichael Halcrow 	expected_checksum += (unsigned char)(data[i++]);
265dddfa461SMichael Halcrow 	for (i = 0; i < session_key->decrypted_key_size; i++)
266dddfa461SMichael Halcrow 		checksum += session_key->decrypted_key[i];
267dddfa461SMichael Halcrow 	if (expected_checksum != checksum) {
268dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
269dddfa461SMichael Halcrow 				"encryption  key; expected [%x]; calculated "
270dddfa461SMichael Halcrow 				"[%x]\n", expected_checksum, checksum);
271dddfa461SMichael Halcrow 		rc = -EIO;
272dddfa461SMichael Halcrow 	}
273dddfa461SMichael Halcrow out:
274dddfa461SMichael Halcrow 	return rc;
275dddfa461SMichael Halcrow }
276dddfa461SMichael Halcrow 
277dddfa461SMichael Halcrow 
278dddfa461SMichael Halcrow static int
27919e66a67STrevor Highland write_tag_66_packet(char *signature, u8 cipher_code,
280dddfa461SMichael Halcrow 		    struct ecryptfs_crypt_stat *crypt_stat, char **packet,
281dddfa461SMichael Halcrow 		    size_t *packet_len)
282dddfa461SMichael Halcrow {
283dddfa461SMichael Halcrow 	size_t i = 0;
284dddfa461SMichael Halcrow 	size_t j;
285dddfa461SMichael Halcrow 	size_t data_len;
286dddfa461SMichael Halcrow 	size_t checksum = 0;
287dddfa461SMichael Halcrow 	size_t packet_size_len;
288dddfa461SMichael Halcrow 	char *message;
289dddfa461SMichael Halcrow 	int rc;
290dddfa461SMichael Halcrow 
291dddfa461SMichael Halcrow 	/*
292dddfa461SMichael Halcrow 	 *              ***** TAG 66 Packet Format *****
293dddfa461SMichael Halcrow 	 *         | Content Type             | 1 byte       |
294dddfa461SMichael Halcrow 	 *         | Key Identifier Size      | 1 or 2 bytes |
295dddfa461SMichael Halcrow 	 *         | Key Identifier           | arbitrary    |
296dddfa461SMichael Halcrow 	 *         | File Encryption Key Size | 1 or 2 bytes |
297dddfa461SMichael Halcrow 	 *         | File Encryption Key      | arbitrary    |
298dddfa461SMichael Halcrow 	 */
299dddfa461SMichael Halcrow 	data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
300dddfa461SMichael Halcrow 	*packet = kmalloc(data_len, GFP_KERNEL);
301dddfa461SMichael Halcrow 	message = *packet;
302dddfa461SMichael Halcrow 	if (!message) {
303dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
304dddfa461SMichael Halcrow 		rc = -ENOMEM;
305dddfa461SMichael Halcrow 		goto out;
306dddfa461SMichael Halcrow 	}
307dddfa461SMichael Halcrow 	message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
308f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
309dddfa461SMichael Halcrow 					  &packet_size_len);
310dddfa461SMichael Halcrow 	if (rc) {
311dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
312dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
313dddfa461SMichael Halcrow 		goto out;
314dddfa461SMichael Halcrow 	}
315dddfa461SMichael Halcrow 	i += packet_size_len;
316dddfa461SMichael Halcrow 	memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
317dddfa461SMichael Halcrow 	i += ECRYPTFS_SIG_SIZE_HEX;
318dddfa461SMichael Halcrow 	/* The encrypted key includes 1 byte cipher code and 2 byte checksum */
319f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
320dddfa461SMichael Halcrow 					  &packet_size_len);
321dddfa461SMichael Halcrow 	if (rc) {
322dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
323dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
324dddfa461SMichael Halcrow 		goto out;
325dddfa461SMichael Halcrow 	}
326dddfa461SMichael Halcrow 	i += packet_size_len;
327dddfa461SMichael Halcrow 	message[i++] = cipher_code;
328dddfa461SMichael Halcrow 	memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
329dddfa461SMichael Halcrow 	i += crypt_stat->key_size;
330dddfa461SMichael Halcrow 	for (j = 0; j < crypt_stat->key_size; j++)
331dddfa461SMichael Halcrow 		checksum += crypt_stat->key[j];
332dddfa461SMichael Halcrow 	message[i++] = (checksum / 256) % 256;
333dddfa461SMichael Halcrow 	message[i++] = (checksum % 256);
334dddfa461SMichael Halcrow 	*packet_len = i;
335dddfa461SMichael Halcrow out:
336dddfa461SMichael Halcrow 	return rc;
337dddfa461SMichael Halcrow }
338dddfa461SMichael Halcrow 
339dddfa461SMichael Halcrow static int
340dddfa461SMichael Halcrow parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
341dddfa461SMichael Halcrow 		    struct ecryptfs_message *msg)
342dddfa461SMichael Halcrow {
343dddfa461SMichael Halcrow 	size_t i = 0;
344dddfa461SMichael Halcrow 	char *data;
345dddfa461SMichael Halcrow 	size_t data_len;
346dddfa461SMichael Halcrow 	size_t message_len;
347dddfa461SMichael Halcrow 	int rc;
348dddfa461SMichael Halcrow 
349dddfa461SMichael Halcrow 	/*
350dddfa461SMichael Halcrow 	 *              ***** TAG 65 Packet Format *****
351dddfa461SMichael Halcrow 	 *    | Content Type                       | 1 byte       |
352dddfa461SMichael Halcrow 	 *    | Status Indicator                   | 1 byte       |
353dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key Size | 1 or 2 bytes |
354dddfa461SMichael Halcrow 	 *    | Encrypted File Encryption Key      | arbitrary    |
355dddfa461SMichael Halcrow 	 */
356dddfa461SMichael Halcrow 	message_len = msg->data_len;
357dddfa461SMichael Halcrow 	data = msg->data;
358dddfa461SMichael Halcrow 	/* verify that everything through the encrypted FEK size is present */
359dddfa461SMichael Halcrow 	if (message_len < 4) {
360dddfa461SMichael Halcrow 		rc = -EIO;
361f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: message_len is [%Zd]; minimum acceptable "
362f66e883eSMichael Halcrow 		       "message length is [%d]\n", __func__, message_len, 4);
363dddfa461SMichael Halcrow 		goto out;
364dddfa461SMichael Halcrow 	}
365dddfa461SMichael Halcrow 	if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
366dddfa461SMichael Halcrow 		rc = -EIO;
367f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
368f66e883eSMichael Halcrow 		       __func__);
369dddfa461SMichael Halcrow 		goto out;
370dddfa461SMichael Halcrow 	}
371dddfa461SMichael Halcrow 	if (data[i++]) {
372dddfa461SMichael Halcrow 		rc = -EIO;
373f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: Status indicator has non zero "
374f66e883eSMichael Halcrow 		       "value [%d]\n", __func__, data[i-1]);
375f66e883eSMichael Halcrow 
376dddfa461SMichael Halcrow 		goto out;
377dddfa461SMichael Halcrow 	}
378f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
379f66e883eSMichael Halcrow 					  &data_len);
380dddfa461SMichael Halcrow 	if (rc) {
381dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
382dddfa461SMichael Halcrow 				"rc = [%d]\n", rc);
383dddfa461SMichael Halcrow 		goto out;
384dddfa461SMichael Halcrow 	}
385dddfa461SMichael Halcrow 	i += data_len;
386dddfa461SMichael Halcrow 	if (message_len < (i + key_rec->enc_key_size)) {
387dddfa461SMichael Halcrow 		rc = -EIO;
388f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: message_len [%Zd]; max len is [%Zd]\n",
389f66e883eSMichael Halcrow 		       __func__, message_len, (i + key_rec->enc_key_size));
390dddfa461SMichael Halcrow 		goto out;
391dddfa461SMichael Halcrow 	}
392dddfa461SMichael Halcrow 	if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
393f66e883eSMichael Halcrow 		rc = -EIO;
394f66e883eSMichael Halcrow 		printk(KERN_ERR "%s: Encrypted key_size [%Zd] larger than "
395f66e883eSMichael Halcrow 		       "the maximum key size [%d]\n", __func__,
396dddfa461SMichael Halcrow 		       key_rec->enc_key_size,
397dddfa461SMichael Halcrow 		       ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
398dddfa461SMichael Halcrow 		goto out;
399dddfa461SMichael Halcrow 	}
400dddfa461SMichael Halcrow 	memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
401dddfa461SMichael Halcrow out:
402dddfa461SMichael Halcrow 	return rc;
403dddfa461SMichael Halcrow }
404dddfa461SMichael Halcrow 
405cd9d67dfSMichael Halcrow static int
406cd9d67dfSMichael Halcrow ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
407cd9d67dfSMichael Halcrow {
408cd9d67dfSMichael Halcrow 	int rc = 0;
409cd9d67dfSMichael Halcrow 
410cd9d67dfSMichael Halcrow 	(*sig) = NULL;
411cd9d67dfSMichael Halcrow 	switch (auth_tok->token_type) {
412cd9d67dfSMichael Halcrow 	case ECRYPTFS_PASSWORD:
413cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.password.signature;
414cd9d67dfSMichael Halcrow 		break;
415cd9d67dfSMichael Halcrow 	case ECRYPTFS_PRIVATE_KEY:
416cd9d67dfSMichael Halcrow 		(*sig) = auth_tok->token.private_key.signature;
417cd9d67dfSMichael Halcrow 		break;
418cd9d67dfSMichael Halcrow 	default:
419cd9d67dfSMichael Halcrow 		printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
420cd9d67dfSMichael Halcrow 		       auth_tok->token_type);
421cd9d67dfSMichael Halcrow 		rc = -EINVAL;
422cd9d67dfSMichael Halcrow 	}
423cd9d67dfSMichael Halcrow 	return rc;
424cd9d67dfSMichael Halcrow }
425cd9d67dfSMichael Halcrow 
426dddfa461SMichael Halcrow /**
42722e78fafSMichael Halcrow  * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
42822e78fafSMichael Halcrow  * @auth_tok: The key authentication token used to decrypt the session key
42922e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
430dddfa461SMichael Halcrow  *
43122e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise.
432dddfa461SMichael Halcrow  */
433f4aad16aSMichael Halcrow static int
434f4aad16aSMichael Halcrow decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
435dddfa461SMichael Halcrow 				  struct ecryptfs_crypt_stat *crypt_stat)
436dddfa461SMichael Halcrow {
43719e66a67STrevor Highland 	u8 cipher_code = 0;
438dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx;
439dddfa461SMichael Halcrow 	struct ecryptfs_message *msg = NULL;
440f4aad16aSMichael Halcrow 	char *auth_tok_sig;
441624ae528STyler Hicks 	char *payload;
442624ae528STyler Hicks 	size_t payload_len;
443dddfa461SMichael Halcrow 	int rc;
444dddfa461SMichael Halcrow 
4455dda6992SMichael Halcrow 	rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
4465dda6992SMichael Halcrow 	if (rc) {
447f4aad16aSMichael Halcrow 		printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
448f4aad16aSMichael Halcrow 		       auth_tok->token_type);
449f4aad16aSMichael Halcrow 		goto out;
450f4aad16aSMichael Halcrow 	}
451f4aad16aSMichael Halcrow 	rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
452624ae528STyler Hicks 				 &payload, &payload_len);
453dddfa461SMichael Halcrow 	if (rc) {
454f66e883eSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
455dddfa461SMichael Halcrow 		goto out;
456dddfa461SMichael Halcrow 	}
457624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
458dddfa461SMichael Halcrow 	if (rc) {
459624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
460624ae528STyler Hicks 				"ecryptfsd\n");
461dddfa461SMichael Halcrow 		goto out;
462dddfa461SMichael Halcrow 	}
463dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
464dddfa461SMichael Halcrow 	if (rc) {
465dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
466dddfa461SMichael Halcrow 				"from the user space daemon\n");
467dddfa461SMichael Halcrow 		rc = -EIO;
468dddfa461SMichael Halcrow 		goto out;
469dddfa461SMichael Halcrow 	}
470dddfa461SMichael Halcrow 	rc = parse_tag_65_packet(&(auth_tok->session_key),
471dddfa461SMichael Halcrow 				 &cipher_code, msg);
472dddfa461SMichael Halcrow 	if (rc) {
473dddfa461SMichael Halcrow 		printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
474dddfa461SMichael Halcrow 		       rc);
475dddfa461SMichael Halcrow 		goto out;
476dddfa461SMichael Halcrow 	}
477dddfa461SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
478dddfa461SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
479dddfa461SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
480dddfa461SMichael Halcrow 	crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
481dddfa461SMichael Halcrow 	rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
482dddfa461SMichael Halcrow 	if (rc) {
483dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
484dddfa461SMichael Halcrow 				cipher_code)
485dddfa461SMichael Halcrow 		goto out;
486dddfa461SMichael Halcrow 	}
487dddfa461SMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
488dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
489dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
490dddfa461SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
491dddfa461SMichael Halcrow 				  crypt_stat->key_size);
492dddfa461SMichael Halcrow 	}
493dddfa461SMichael Halcrow out:
494dddfa461SMichael Halcrow 	if (msg)
495dddfa461SMichael Halcrow 		kfree(msg);
496dddfa461SMichael Halcrow 	return rc;
497dddfa461SMichael Halcrow }
498dddfa461SMichael Halcrow 
499dddfa461SMichael Halcrow static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
500dddfa461SMichael Halcrow {
501dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
502e0869cc1SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
503dddfa461SMichael Halcrow 
504e0869cc1SMichael Halcrow 	list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
505e0869cc1SMichael Halcrow 				 auth_tok_list_head, list) {
506e0869cc1SMichael Halcrow 		list_del(&auth_tok_list_item->list);
507dddfa461SMichael Halcrow 		kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
508dddfa461SMichael Halcrow 				auth_tok_list_item);
509dddfa461SMichael Halcrow 	}
510dddfa461SMichael Halcrow }
511dddfa461SMichael Halcrow 
512dddfa461SMichael Halcrow struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
513dddfa461SMichael Halcrow 
514dddfa461SMichael Halcrow /**
515dddfa461SMichael Halcrow  * parse_tag_1_packet
51622e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet contents
517dddfa461SMichael Halcrow  * @data: The raw bytes of the packet.
518dddfa461SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
51922e78fafSMichael Halcrow  *                 a new authentication token will be placed at the
52022e78fafSMichael Halcrow  *                 end of this list for this packet.
521dddfa461SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
522dddfa461SMichael Halcrow  *                allocates; sets the memory address of the pointer to
523dddfa461SMichael Halcrow  *                NULL on error. This object is added to the
524dddfa461SMichael Halcrow  *                auth_tok_list.
525dddfa461SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
526dddfa461SMichael Halcrow  *               into this memory location; zero on error.
52722e78fafSMichael Halcrow  * @max_packet_size: The maximum allowable packet size
528dddfa461SMichael Halcrow  *
529dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
530dddfa461SMichael Halcrow  */
531dddfa461SMichael Halcrow static int
532dddfa461SMichael Halcrow parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
533dddfa461SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
534dddfa461SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
535dddfa461SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
536dddfa461SMichael Halcrow {
537dddfa461SMichael Halcrow 	size_t body_size;
538dddfa461SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
539dddfa461SMichael Halcrow 	size_t length_size;
540dddfa461SMichael Halcrow 	int rc = 0;
541dddfa461SMichael Halcrow 
542dddfa461SMichael Halcrow 	(*packet_size) = 0;
543dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
54413218179SMichael Halcrow 	/**
54513218179SMichael Halcrow 	 * This format is inspired by OpenPGP; see RFC 2440
54613218179SMichael Halcrow 	 * packet tag 1
54713218179SMichael Halcrow 	 *
54813218179SMichael Halcrow 	 * Tag 1 identifier (1 byte)
54913218179SMichael Halcrow 	 * Max Tag 1 packet size (max 3 bytes)
55013218179SMichael Halcrow 	 * Version (1 byte)
55113218179SMichael Halcrow 	 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
55213218179SMichael Halcrow 	 * Cipher identifier (1 byte)
55313218179SMichael Halcrow 	 * Encrypted key size (arbitrary)
55413218179SMichael Halcrow 	 *
55513218179SMichael Halcrow 	 * 12 bytes minimum packet size
556dddfa461SMichael Halcrow 	 */
55713218179SMichael Halcrow 	if (unlikely(max_packet_size < 12)) {
55813218179SMichael Halcrow 		printk(KERN_ERR "Invalid max packet size; must be >=12\n");
559dddfa461SMichael Halcrow 		rc = -EINVAL;
560dddfa461SMichael Halcrow 		goto out;
561dddfa461SMichael Halcrow 	}
562dddfa461SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
56313218179SMichael Halcrow 		printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
564dddfa461SMichael Halcrow 		       ECRYPTFS_TAG_1_PACKET_TYPE);
565dddfa461SMichael Halcrow 		rc = -EINVAL;
566dddfa461SMichael Halcrow 		goto out;
567dddfa461SMichael Halcrow 	}
568dddfa461SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
569dddfa461SMichael Halcrow 	 * at end of function upon failure */
570dddfa461SMichael Halcrow 	auth_tok_list_item =
57113218179SMichael Halcrow 		kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
572dddfa461SMichael Halcrow 				  GFP_KERNEL);
573dddfa461SMichael Halcrow 	if (!auth_tok_list_item) {
57413218179SMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
575dddfa461SMichael Halcrow 		rc = -ENOMEM;
576dddfa461SMichael Halcrow 		goto out;
577dddfa461SMichael Halcrow 	}
578dddfa461SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
579f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
5805dda6992SMichael Halcrow 					  &length_size);
5815dda6992SMichael Halcrow 	if (rc) {
58213218179SMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; "
583dddfa461SMichael Halcrow 		       "rc = [%d]\n", rc);
584dddfa461SMichael Halcrow 		goto out_free;
585dddfa461SMichael Halcrow 	}
58613218179SMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
58781acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
588dddfa461SMichael Halcrow 		rc = -EINVAL;
589dddfa461SMichael Halcrow 		goto out_free;
590dddfa461SMichael Halcrow 	}
591dddfa461SMichael Halcrow 	(*packet_size) += length_size;
592dddfa461SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
59313218179SMichael Halcrow 		printk(KERN_WARNING "Packet size exceeds max\n");
594dddfa461SMichael Halcrow 		rc = -EINVAL;
595dddfa461SMichael Halcrow 		goto out_free;
596dddfa461SMichael Halcrow 	}
597dddfa461SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
59813218179SMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
59913218179SMichael Halcrow 		       data[(*packet_size) - 1]);
600dddfa461SMichael Halcrow 		rc = -EINVAL;
601dddfa461SMichael Halcrow 		goto out_free;
602dddfa461SMichael Halcrow 	}
603dddfa461SMichael Halcrow 	ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
604dddfa461SMichael Halcrow 			&data[(*packet_size)], ECRYPTFS_SIG_SIZE);
605dddfa461SMichael Halcrow 	*packet_size += ECRYPTFS_SIG_SIZE;
606dddfa461SMichael Halcrow 	/* This byte is skipped because the kernel does not need to
607dddfa461SMichael Halcrow 	 * know which public key encryption algorithm was used */
608dddfa461SMichael Halcrow 	(*packet_size)++;
609dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
61013218179SMichael Halcrow 		body_size - (ECRYPTFS_SIG_SIZE + 2);
611dddfa461SMichael Halcrow 	if ((*new_auth_tok)->session_key.encrypted_key_size
612dddfa461SMichael Halcrow 	    > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
61313218179SMichael Halcrow 		printk(KERN_WARNING "Tag 1 packet contains key larger "
614dddfa461SMichael Halcrow 		       "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
615dddfa461SMichael Halcrow 		rc = -EINVAL;
616dddfa461SMichael Halcrow 		goto out;
617dddfa461SMichael Halcrow 	}
618dddfa461SMichael Halcrow 	memcpy((*new_auth_tok)->session_key.encrypted_key,
61913218179SMichael Halcrow 	       &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
620dddfa461SMichael Halcrow 	(*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
621dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
622dddfa461SMichael Halcrow 		~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
623dddfa461SMichael Halcrow 	(*new_auth_tok)->session_key.flags |=
624dddfa461SMichael Halcrow 		ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
625dddfa461SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
62613218179SMichael Halcrow 	(*new_auth_tok)->flags = 0;
627e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
628e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
629e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
630e2bd99ecSMichael Halcrow 		~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
631dddfa461SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
632dddfa461SMichael Halcrow 	goto out;
633dddfa461SMichael Halcrow out_free:
634dddfa461SMichael Halcrow 	(*new_auth_tok) = NULL;
635dddfa461SMichael Halcrow 	memset(auth_tok_list_item, 0,
636dddfa461SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
637dddfa461SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
638dddfa461SMichael Halcrow 			auth_tok_list_item);
639dddfa461SMichael Halcrow out:
640dddfa461SMichael Halcrow 	if (rc)
641dddfa461SMichael Halcrow 		(*packet_size) = 0;
642dddfa461SMichael Halcrow 	return rc;
643dddfa461SMichael Halcrow }
644dddfa461SMichael Halcrow 
645237fead6SMichael Halcrow /**
646237fead6SMichael Halcrow  * parse_tag_3_packet
647237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context to modify based on packet
648237fead6SMichael Halcrow  *              contents.
649237fead6SMichael Halcrow  * @data: The raw bytes of the packet.
650237fead6SMichael Halcrow  * @auth_tok_list: eCryptfs parses packets into authentication tokens;
651237fead6SMichael Halcrow  *                 a new authentication token will be placed at the end
652237fead6SMichael Halcrow  *                 of this list for this packet.
653237fead6SMichael Halcrow  * @new_auth_tok: Pointer to a pointer to memory that this function
654237fead6SMichael Halcrow  *                allocates; sets the memory address of the pointer to
655237fead6SMichael Halcrow  *                NULL on error. This object is added to the
656237fead6SMichael Halcrow  *                auth_tok_list.
657237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
658237fead6SMichael Halcrow  *               into this memory location; zero on error.
659237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
660237fead6SMichael Halcrow  *
661237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
662237fead6SMichael Halcrow  */
663237fead6SMichael Halcrow static int
664237fead6SMichael Halcrow parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
665237fead6SMichael Halcrow 		   unsigned char *data, struct list_head *auth_tok_list,
666237fead6SMichael Halcrow 		   struct ecryptfs_auth_tok **new_auth_tok,
667237fead6SMichael Halcrow 		   size_t *packet_size, size_t max_packet_size)
668237fead6SMichael Halcrow {
669237fead6SMichael Halcrow 	size_t body_size;
670237fead6SMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
671237fead6SMichael Halcrow 	size_t length_size;
672dddfa461SMichael Halcrow 	int rc = 0;
673237fead6SMichael Halcrow 
674237fead6SMichael Halcrow 	(*packet_size) = 0;
675237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
676c59becfcSMichael Halcrow 	/**
677c59becfcSMichael Halcrow 	 *This format is inspired by OpenPGP; see RFC 2440
678c59becfcSMichael Halcrow 	 * packet tag 3
679c59becfcSMichael Halcrow 	 *
680c59becfcSMichael Halcrow 	 * Tag 3 identifier (1 byte)
681c59becfcSMichael Halcrow 	 * Max Tag 3 packet size (max 3 bytes)
682c59becfcSMichael Halcrow 	 * Version (1 byte)
683c59becfcSMichael Halcrow 	 * Cipher code (1 byte)
684c59becfcSMichael Halcrow 	 * S2K specifier (1 byte)
685c59becfcSMichael Halcrow 	 * Hash identifier (1 byte)
686c59becfcSMichael Halcrow 	 * Salt (ECRYPTFS_SALT_SIZE)
687c59becfcSMichael Halcrow 	 * Hash iterations (1 byte)
688c59becfcSMichael Halcrow 	 * Encrypted key (arbitrary)
689c59becfcSMichael Halcrow 	 *
690c59becfcSMichael Halcrow 	 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
691237fead6SMichael Halcrow 	 */
692c59becfcSMichael Halcrow 	if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
693c59becfcSMichael Halcrow 		printk(KERN_ERR "Max packet size too large\n");
694237fead6SMichael Halcrow 		rc = -EINVAL;
695237fead6SMichael Halcrow 		goto out;
696237fead6SMichael Halcrow 	}
697237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
698c59becfcSMichael Halcrow 		printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
699237fead6SMichael Halcrow 		       ECRYPTFS_TAG_3_PACKET_TYPE);
700237fead6SMichael Halcrow 		rc = -EINVAL;
701237fead6SMichael Halcrow 		goto out;
702237fead6SMichael Halcrow 	}
703237fead6SMichael Halcrow 	/* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
704237fead6SMichael Halcrow 	 * at end of function upon failure */
705237fead6SMichael Halcrow 	auth_tok_list_item =
706c3762229SRobert P. J. Day 	    kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
707237fead6SMichael Halcrow 	if (!auth_tok_list_item) {
708c59becfcSMichael Halcrow 		printk(KERN_ERR "Unable to allocate memory\n");
709237fead6SMichael Halcrow 		rc = -ENOMEM;
710237fead6SMichael Halcrow 		goto out;
711237fead6SMichael Halcrow 	}
712237fead6SMichael Halcrow 	(*new_auth_tok) = &auth_tok_list_item->auth_tok;
713f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
7145dda6992SMichael Halcrow 					  &length_size);
7155dda6992SMichael Halcrow 	if (rc) {
716c59becfcSMichael Halcrow 		printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
717c59becfcSMichael Halcrow 		       rc);
718237fead6SMichael Halcrow 		goto out_free;
719237fead6SMichael Halcrow 	}
720c59becfcSMichael Halcrow 	if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
72181acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
722237fead6SMichael Halcrow 		rc = -EINVAL;
723237fead6SMichael Halcrow 		goto out_free;
724237fead6SMichael Halcrow 	}
725237fead6SMichael Halcrow 	(*packet_size) += length_size;
726237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size > max_packet_size)) {
727c59becfcSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
728237fead6SMichael Halcrow 		rc = -EINVAL;
729237fead6SMichael Halcrow 		goto out_free;
730237fead6SMichael Halcrow 	}
731237fead6SMichael Halcrow 	(*new_auth_tok)->session_key.encrypted_key_size =
732c59becfcSMichael Halcrow 		(body_size - (ECRYPTFS_SALT_SIZE + 5));
733237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x04)) {
734c59becfcSMichael Halcrow 		printk(KERN_WARNING "Unknown version number [%d]\n",
735c59becfcSMichael Halcrow 		       data[(*packet_size) - 1]);
736237fead6SMichael Halcrow 		rc = -EINVAL;
737237fead6SMichael Halcrow 		goto out_free;
738237fead6SMichael Halcrow 	}
739237fead6SMichael Halcrow 	ecryptfs_cipher_code_to_string(crypt_stat->cipher,
740237fead6SMichael Halcrow 				       (u16)data[(*packet_size)]);
741237fead6SMichael Halcrow 	/* A little extra work to differentiate among the AES key
742237fead6SMichael Halcrow 	 * sizes; see RFC2440 */
743237fead6SMichael Halcrow 	switch(data[(*packet_size)++]) {
744237fead6SMichael Halcrow 	case RFC2440_CIPHER_AES_192:
745237fead6SMichael Halcrow 		crypt_stat->key_size = 24;
746237fead6SMichael Halcrow 		break;
747237fead6SMichael Halcrow 	default:
748237fead6SMichael Halcrow 		crypt_stat->key_size =
749237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
750237fead6SMichael Halcrow 	}
751237fead6SMichael Halcrow 	ecryptfs_init_crypt_ctx(crypt_stat);
752237fead6SMichael Halcrow 	if (unlikely(data[(*packet_size)++] != 0x03)) {
753c59becfcSMichael Halcrow 		printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
754237fead6SMichael Halcrow 		rc = -ENOSYS;
755237fead6SMichael Halcrow 		goto out_free;
756237fead6SMichael Halcrow 	}
757237fead6SMichael Halcrow 	/* TODO: finish the hash mapping */
758237fead6SMichael Halcrow 	switch (data[(*packet_size)++]) {
759237fead6SMichael Halcrow 	case 0x01: /* See RFC2440 for these numbers and their mappings */
760237fead6SMichael Halcrow 		/* Choose MD5 */
761237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->token.password.salt,
762237fead6SMichael Halcrow 		       &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
763237fead6SMichael Halcrow 		(*packet_size) += ECRYPTFS_SALT_SIZE;
764237fead6SMichael Halcrow 		/* This conversion was taken straight from RFC2440 */
765237fead6SMichael Halcrow 		(*new_auth_tok)->token.password.hash_iterations =
766237fead6SMichael Halcrow 			((u32) 16 + (data[(*packet_size)] & 15))
767237fead6SMichael Halcrow 				<< ((data[(*packet_size)] >> 4) + 6);
768237fead6SMichael Halcrow 		(*packet_size)++;
769c59becfcSMichael Halcrow 		/* Friendly reminder:
770c59becfcSMichael Halcrow 		 * (*new_auth_tok)->session_key.encrypted_key_size =
771c59becfcSMichael Halcrow 		 *         (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
772237fead6SMichael Halcrow 		memcpy((*new_auth_tok)->session_key.encrypted_key,
773237fead6SMichael Halcrow 		       &data[(*packet_size)],
774237fead6SMichael Halcrow 		       (*new_auth_tok)->session_key.encrypted_key_size);
775237fead6SMichael Halcrow 		(*packet_size) +=
776237fead6SMichael Halcrow 			(*new_auth_tok)->session_key.encrypted_key_size;
777237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags &=
778237fead6SMichael Halcrow 			~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
779237fead6SMichael Halcrow 		(*new_auth_tok)->session_key.flags |=
780237fead6SMichael Halcrow 			ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
781c59becfcSMichael Halcrow 		(*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
782237fead6SMichael Halcrow 		break;
783237fead6SMichael Halcrow 	default:
784237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
785237fead6SMichael Halcrow 				"[%d]\n", data[(*packet_size) - 1]);
786237fead6SMichael Halcrow 		rc = -ENOSYS;
787237fead6SMichael Halcrow 		goto out_free;
788237fead6SMichael Halcrow 	}
789237fead6SMichael Halcrow 	(*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
790237fead6SMichael Halcrow 	/* TODO: Parametarize; we might actually want userspace to
791237fead6SMichael Halcrow 	 * decrypt the session key. */
792e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
793e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
794e2bd99ecSMichael Halcrow 	(*new_auth_tok)->session_key.flags &=
795e2bd99ecSMichael Halcrow 			    ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
796237fead6SMichael Halcrow 	list_add(&auth_tok_list_item->list, auth_tok_list);
797237fead6SMichael Halcrow 	goto out;
798237fead6SMichael Halcrow out_free:
799237fead6SMichael Halcrow 	(*new_auth_tok) = NULL;
800237fead6SMichael Halcrow 	memset(auth_tok_list_item, 0,
801237fead6SMichael Halcrow 	       sizeof(struct ecryptfs_auth_tok_list_item));
802237fead6SMichael Halcrow 	kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
803237fead6SMichael Halcrow 			auth_tok_list_item);
804237fead6SMichael Halcrow out:
805237fead6SMichael Halcrow 	if (rc)
806237fead6SMichael Halcrow 		(*packet_size) = 0;
807237fead6SMichael Halcrow 	return rc;
808237fead6SMichael Halcrow }
809237fead6SMichael Halcrow 
810237fead6SMichael Halcrow /**
811237fead6SMichael Halcrow  * parse_tag_11_packet
812237fead6SMichael Halcrow  * @data: The raw bytes of the packet
813237fead6SMichael Halcrow  * @contents: This function writes the data contents of the literal
814237fead6SMichael Halcrow  *            packet into this memory location
815237fead6SMichael Halcrow  * @max_contents_bytes: The maximum number of bytes that this function
816237fead6SMichael Halcrow  *                      is allowed to write into contents
817237fead6SMichael Halcrow  * @tag_11_contents_size: This function writes the size of the parsed
818237fead6SMichael Halcrow  *                        contents into this memory location; zero on
819237fead6SMichael Halcrow  *                        error
820237fead6SMichael Halcrow  * @packet_size: This function writes the size of the parsed packet
821237fead6SMichael Halcrow  *               into this memory location; zero on error
822237fead6SMichael Halcrow  * @max_packet_size: maximum number of bytes to parse
823237fead6SMichael Halcrow  *
824237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
825237fead6SMichael Halcrow  */
826237fead6SMichael Halcrow static int
827237fead6SMichael Halcrow parse_tag_11_packet(unsigned char *data, unsigned char *contents,
828237fead6SMichael Halcrow 		    size_t max_contents_bytes, size_t *tag_11_contents_size,
829237fead6SMichael Halcrow 		    size_t *packet_size, size_t max_packet_size)
830237fead6SMichael Halcrow {
831237fead6SMichael Halcrow 	size_t body_size;
832237fead6SMichael Halcrow 	size_t length_size;
833dddfa461SMichael Halcrow 	int rc = 0;
834237fead6SMichael Halcrow 
835237fead6SMichael Halcrow 	(*packet_size) = 0;
836237fead6SMichael Halcrow 	(*tag_11_contents_size) = 0;
837f648104aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
838f648104aSMichael Halcrow 	 * packet tag 11
839f648104aSMichael Halcrow 	 *
840f648104aSMichael Halcrow 	 * Tag 11 identifier (1 byte)
841f648104aSMichael Halcrow 	 * Max Tag 11 packet size (max 3 bytes)
842f648104aSMichael Halcrow 	 * Binary format specifier (1 byte)
843f648104aSMichael Halcrow 	 * Filename length (1 byte)
844f648104aSMichael Halcrow 	 * Filename ("_CONSOLE") (8 bytes)
845f648104aSMichael Halcrow 	 * Modification date (4 bytes)
846f648104aSMichael Halcrow 	 * Literal data (arbitrary)
847f648104aSMichael Halcrow 	 *
848f648104aSMichael Halcrow 	 * We need at least 16 bytes of data for the packet to even be
849f648104aSMichael Halcrow 	 * valid.
850237fead6SMichael Halcrow 	 */
851f648104aSMichael Halcrow 	if (max_packet_size < 16) {
852f648104aSMichael Halcrow 		printk(KERN_ERR "Maximum packet size too small\n");
853237fead6SMichael Halcrow 		rc = -EINVAL;
854237fead6SMichael Halcrow 		goto out;
855237fead6SMichael Halcrow 	}
856237fead6SMichael Halcrow 	if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
857f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
858237fead6SMichael Halcrow 		rc = -EINVAL;
859237fead6SMichael Halcrow 		goto out;
860237fead6SMichael Halcrow 	}
861f66e883eSMichael Halcrow 	rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
8625dda6992SMichael Halcrow 					  &length_size);
8635dda6992SMichael Halcrow 	if (rc) {
864f648104aSMichael Halcrow 		printk(KERN_WARNING "Invalid tag 11 packet format\n");
865f648104aSMichael Halcrow 		goto out;
866f648104aSMichael Halcrow 	}
867f648104aSMichael Halcrow 	if (body_size < 14) {
86881acbcd6SAndrew Morton 		printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
869f648104aSMichael Halcrow 		rc = -EINVAL;
870237fead6SMichael Halcrow 		goto out;
871237fead6SMichael Halcrow 	}
872237fead6SMichael Halcrow 	(*packet_size) += length_size;
873f648104aSMichael Halcrow 	(*tag_11_contents_size) = (body_size - 14);
874237fead6SMichael Halcrow 	if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
875f648104aSMichael Halcrow 		printk(KERN_ERR "Packet size exceeds max\n");
876237fead6SMichael Halcrow 		rc = -EINVAL;
877237fead6SMichael Halcrow 		goto out;
878237fead6SMichael Halcrow 	}
879237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x62) {
880f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
881237fead6SMichael Halcrow 		rc = -EINVAL;
882237fead6SMichael Halcrow 		goto out;
883237fead6SMichael Halcrow 	}
884237fead6SMichael Halcrow 	if (data[(*packet_size)++] != 0x08) {
885f648104aSMichael Halcrow 		printk(KERN_WARNING "Unrecognizable packet\n");
886237fead6SMichael Halcrow 		rc = -EINVAL;
887237fead6SMichael Halcrow 		goto out;
888237fead6SMichael Halcrow 	}
889f648104aSMichael Halcrow 	(*packet_size) += 12; /* Ignore filename and modification date */
890237fead6SMichael Halcrow 	memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
891237fead6SMichael Halcrow 	(*packet_size) += (*tag_11_contents_size);
892237fead6SMichael Halcrow out:
893237fead6SMichael Halcrow 	if (rc) {
894237fead6SMichael Halcrow 		(*packet_size) = 0;
895237fead6SMichael Halcrow 		(*tag_11_contents_size) = 0;
896237fead6SMichael Halcrow 	}
897237fead6SMichael Halcrow 	return rc;
898237fead6SMichael Halcrow }
899237fead6SMichael Halcrow 
900f4aad16aSMichael Halcrow static int
901f4aad16aSMichael Halcrow ecryptfs_find_global_auth_tok_for_sig(
902f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok **global_auth_tok,
903f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
904f4aad16aSMichael Halcrow {
905f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *walker;
906f4aad16aSMichael Halcrow 	int rc = 0;
907f4aad16aSMichael Halcrow 
908f4aad16aSMichael Halcrow 	(*global_auth_tok) = NULL;
909f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
910f4aad16aSMichael Halcrow 	list_for_each_entry(walker,
911f4aad16aSMichael Halcrow 			    &mount_crypt_stat->global_auth_tok_list,
912f4aad16aSMichael Halcrow 			    mount_crypt_stat_list) {
913f4aad16aSMichael Halcrow 		if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
914f4aad16aSMichael Halcrow 			(*global_auth_tok) = walker;
915f4aad16aSMichael Halcrow 			goto out;
916f4aad16aSMichael Halcrow 		}
917f4aad16aSMichael Halcrow 	}
918f4aad16aSMichael Halcrow 	rc = -EINVAL;
919f4aad16aSMichael Halcrow out:
920f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
921f4aad16aSMichael Halcrow 	return rc;
922f4aad16aSMichael Halcrow }
923f4aad16aSMichael Halcrow 
924237fead6SMichael Halcrow /**
925f4aad16aSMichael Halcrow  * ecryptfs_verify_version
926f4aad16aSMichael Halcrow  * @version: The version number to confirm
927f4aad16aSMichael Halcrow  *
928f4aad16aSMichael Halcrow  * Returns zero on good version; non-zero otherwise
929f4aad16aSMichael Halcrow  */
930f4aad16aSMichael Halcrow static int ecryptfs_verify_version(u16 version)
931f4aad16aSMichael Halcrow {
932f4aad16aSMichael Halcrow 	int rc = 0;
933f4aad16aSMichael Halcrow 	unsigned char major;
934f4aad16aSMichael Halcrow 	unsigned char minor;
935f4aad16aSMichael Halcrow 
936f4aad16aSMichael Halcrow 	major = ((version >> 8) & 0xFF);
937f4aad16aSMichael Halcrow 	minor = (version & 0xFF);
938f4aad16aSMichael Halcrow 	if (major != ECRYPTFS_VERSION_MAJOR) {
939f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
940f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
941f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MAJOR, major);
942f4aad16aSMichael Halcrow 		rc = -EINVAL;
943f4aad16aSMichael Halcrow 		goto out;
944f4aad16aSMichael Halcrow 	}
945f4aad16aSMichael Halcrow 	if (minor != ECRYPTFS_VERSION_MINOR) {
946f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
947f4aad16aSMichael Halcrow 				"Expected [%d]; got [%d]\n",
948f4aad16aSMichael Halcrow 				ECRYPTFS_VERSION_MINOR, minor);
949f4aad16aSMichael Halcrow 		rc = -EINVAL;
950f4aad16aSMichael Halcrow 		goto out;
951f4aad16aSMichael Halcrow 	}
952f4aad16aSMichael Halcrow out:
953f4aad16aSMichael Halcrow 	return rc;
954f4aad16aSMichael Halcrow }
955f4aad16aSMichael Halcrow 
956f4aad16aSMichael Halcrow int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
957f4aad16aSMichael Halcrow 				      struct ecryptfs_auth_tok **auth_tok,
958f4aad16aSMichael Halcrow 				      char *sig)
959f4aad16aSMichael Halcrow {
960f4aad16aSMichael Halcrow 	int rc = 0;
961f4aad16aSMichael Halcrow 
962f4aad16aSMichael Halcrow 	(*auth_tok_key) = request_key(&key_type_user, sig, NULL);
963f4aad16aSMichael Halcrow 	if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
964f4aad16aSMichael Halcrow 		printk(KERN_ERR "Could not find key with description: [%s]\n",
965f4aad16aSMichael Halcrow 		       sig);
966982363c9SEric Sandeen 		rc = process_request_key_err(PTR_ERR(*auth_tok_key));
967f4aad16aSMichael Halcrow 		goto out;
968f4aad16aSMichael Halcrow 	}
969f4aad16aSMichael Halcrow 	(*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
970f4aad16aSMichael Halcrow 	if (ecryptfs_verify_version((*auth_tok)->version)) {
971f4aad16aSMichael Halcrow 		printk(KERN_ERR
972f4aad16aSMichael Halcrow 		       "Data structure version mismatch. "
973f4aad16aSMichael Halcrow 		       "Userspace tools must match eCryptfs "
974f4aad16aSMichael Halcrow 		       "kernel module with major version [%d] "
975f4aad16aSMichael Halcrow 		       "and minor version [%d]\n",
976f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MAJOR,
977f4aad16aSMichael Halcrow 		       ECRYPTFS_VERSION_MINOR);
978f4aad16aSMichael Halcrow 		rc = -EINVAL;
979f4aad16aSMichael Halcrow 		goto out;
980f4aad16aSMichael Halcrow 	}
981f4aad16aSMichael Halcrow 	if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
982f4aad16aSMichael Halcrow 	    && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
983f4aad16aSMichael Halcrow 		printk(KERN_ERR "Invalid auth_tok structure "
984f4aad16aSMichael Halcrow 		       "returned from key query\n");
985f4aad16aSMichael Halcrow 		rc = -EINVAL;
986f4aad16aSMichael Halcrow 		goto out;
987f4aad16aSMichael Halcrow 	}
988f4aad16aSMichael Halcrow out:
989f4aad16aSMichael Halcrow 	return rc;
990f4aad16aSMichael Halcrow }
991f4aad16aSMichael Halcrow 
992f4aad16aSMichael Halcrow /**
993f4aad16aSMichael Halcrow  * ecryptfs_find_auth_tok_for_sig
994f4aad16aSMichael Halcrow  * @auth_tok: Set to the matching auth_tok; NULL if not found
995f4aad16aSMichael Halcrow  * @crypt_stat: inode crypt_stat crypto context
996f4aad16aSMichael Halcrow  * @sig: Sig of auth_tok to find
997f4aad16aSMichael Halcrow  *
998f4aad16aSMichael Halcrow  * For now, this function simply looks at the registered auth_tok's
999f4aad16aSMichael Halcrow  * linked off the mount_crypt_stat, so all the auth_toks that can be
1000f4aad16aSMichael Halcrow  * used must be registered at mount time. This function could
1001f4aad16aSMichael Halcrow  * potentially try a lot harder to find auth_tok's (e.g., by calling
1002f4aad16aSMichael Halcrow  * out to ecryptfsd to dynamically retrieve an auth_tok object) so
1003f4aad16aSMichael Halcrow  * that static registration of auth_tok's will no longer be necessary.
1004f4aad16aSMichael Halcrow  *
1005f4aad16aSMichael Halcrow  * Returns zero on no error; non-zero on error
1006f4aad16aSMichael Halcrow  */
1007f4aad16aSMichael Halcrow static int
1008f4aad16aSMichael Halcrow ecryptfs_find_auth_tok_for_sig(
1009f4aad16aSMichael Halcrow 	struct ecryptfs_auth_tok **auth_tok,
1010f4aad16aSMichael Halcrow 	struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1011f4aad16aSMichael Halcrow {
1012f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1013f4aad16aSMichael Halcrow 		crypt_stat->mount_crypt_stat;
1014f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
1015f4aad16aSMichael Halcrow 	int rc = 0;
1016f4aad16aSMichael Halcrow 
1017f4aad16aSMichael Halcrow 	(*auth_tok) = NULL;
1018f4aad16aSMichael Halcrow 	if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1019f4aad16aSMichael Halcrow 						  mount_crypt_stat, sig)) {
1020f4aad16aSMichael Halcrow 		struct key *auth_tok_key;
1021f4aad16aSMichael Halcrow 
1022f4aad16aSMichael Halcrow 		rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1023f4aad16aSMichael Halcrow 						       sig);
1024f4aad16aSMichael Halcrow 	} else
1025f4aad16aSMichael Halcrow 		(*auth_tok) = global_auth_tok->global_auth_tok;
1026f4aad16aSMichael Halcrow 	return rc;
1027f4aad16aSMichael Halcrow }
1028f4aad16aSMichael Halcrow 
1029f4aad16aSMichael Halcrow /**
103022e78fafSMichael Halcrow  * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
103122e78fafSMichael Halcrow  * @auth_tok: The passphrase authentication token to use to encrypt the FEK
103222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
1033237fead6SMichael Halcrow  *
103422e78fafSMichael Halcrow  * Returns zero on success; non-zero error otherwise
1035237fead6SMichael Halcrow  */
1036f4aad16aSMichael Halcrow static int
1037f4aad16aSMichael Halcrow decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1038237fead6SMichael Halcrow 					 struct ecryptfs_crypt_stat *crypt_stat)
1039237fead6SMichael Halcrow {
1040f4aad16aSMichael Halcrow 	struct scatterlist dst_sg;
1041f4aad16aSMichael Halcrow 	struct scatterlist src_sg;
1042dd8e2902SMichael Halcrow 	struct mutex *tfm_mutex;
10438bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
10448bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
10458bba066fSMichael Halcrow 	};
10468bba066fSMichael Halcrow 	int rc = 0;
1047237fead6SMichael Halcrow 
104860c74f81SJens Axboe 	sg_init_table(&dst_sg, 1);
104960c74f81SJens Axboe 	sg_init_table(&src_sg, 1);
105060c74f81SJens Axboe 
1051f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1052f4aad16aSMichael Halcrow 		ecryptfs_printk(
1053f4aad16aSMichael Halcrow 			KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1054f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1055f4aad16aSMichael Halcrow 		ecryptfs_dump_hex(
1056f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key,
1057f4aad16aSMichael Halcrow 			auth_tok->token.password.session_key_encryption_key_bytes);
1058f4aad16aSMichael Halcrow 	}
1059f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1060f4aad16aSMichael Halcrow 							crypt_stat->cipher);
1061f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
1062f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
1063f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1064f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
1065237fead6SMichael Halcrow 		goto out;
1066237fead6SMichael Halcrow 	}
10675dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1068f4aad16aSMichael Halcrow 				 auth_tok->session_key.encrypted_key_size,
10695dda6992SMichael Halcrow 				 &src_sg, 1);
10705dda6992SMichael Halcrow 	if (rc != 1) {
1071f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1072f4aad16aSMichael Halcrow 			"auth_tok->session_key.encrypted_key to scatterlist; "
1073f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]. "
1074f4aad16aSMichael Halcrow 		       "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1075f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key_size);
1076f4aad16aSMichael Halcrow 		goto out;
1077237fead6SMichael Halcrow 	}
1078f4aad16aSMichael Halcrow 	auth_tok->session_key.decrypted_key_size =
1079f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
10805dda6992SMichael Halcrow 	rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1081f4aad16aSMichael Halcrow 				 auth_tok->session_key.decrypted_key_size,
10825dda6992SMichael Halcrow 				 &dst_sg, 1);
10835dda6992SMichael Halcrow 	if (rc != 1) {
1084f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to convert "
1085f4aad16aSMichael Halcrow 			"auth_tok->session_key.decrypted_key to scatterlist; "
1086f4aad16aSMichael Halcrow 			"expected rc = 1; got rc = [%d]\n", rc);
1087f4aad16aSMichael Halcrow 		goto out;
1088f4aad16aSMichael Halcrow 	}
1089237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
1090f4aad16aSMichael Halcrow 	rc = crypto_blkcipher_setkey(
1091f4aad16aSMichael Halcrow 		desc.tfm, auth_tok->token.password.session_key_encryption_key,
1092237fead6SMichael Halcrow 		crypt_stat->key_size);
1093f4aad16aSMichael Halcrow 	if (unlikely(rc < 0)) {
1094f4aad16aSMichael Halcrow 		mutex_unlock(tfm_mutex);
1095e5d9cbdeSMichael Halcrow 		printk(KERN_ERR "Error setting key for crypto context\n");
1096e5d9cbdeSMichael Halcrow 		rc = -EINVAL;
1097f4aad16aSMichael Halcrow 		goto out;
1098e5d9cbdeSMichael Halcrow 	}
1099f4aad16aSMichael Halcrow 	rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
1100237fead6SMichael Halcrow 				      auth_tok->session_key.encrypted_key_size);
1101f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
1102f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
11038bba066fSMichael Halcrow 		printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
1104f4aad16aSMichael Halcrow 		goto out;
11058bba066fSMichael Halcrow 	}
1106237fead6SMichael Halcrow 	auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1107237fead6SMichael Halcrow 	memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1108237fead6SMichael Halcrow 	       auth_tok->session_key.decrypted_key_size);
1109e2bd99ecSMichael Halcrow 	crypt_stat->flags |= ECRYPTFS_KEY_VALID;
1110f4aad16aSMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1111f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1112f4aad16aSMichael Halcrow 				crypt_stat->key_size);
1113237fead6SMichael Halcrow 		ecryptfs_dump_hex(crypt_stat->key,
1114237fead6SMichael Halcrow 				  crypt_stat->key_size);
1115f4aad16aSMichael Halcrow 	}
1116237fead6SMichael Halcrow out:
1117237fead6SMichael Halcrow 	return rc;
1118237fead6SMichael Halcrow }
1119237fead6SMichael Halcrow 
1120237fead6SMichael Halcrow /**
1121237fead6SMichael Halcrow  * ecryptfs_parse_packet_set
112222e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
112322e78fafSMichael Halcrow  * @src: Virtual address of region of memory containing the packets
112422e78fafSMichael Halcrow  * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
1125237fead6SMichael Halcrow  *
1126237fead6SMichael Halcrow  * Get crypt_stat to have the file's session key if the requisite key
1127237fead6SMichael Halcrow  * is available to decrypt the session key.
1128237fead6SMichael Halcrow  *
1129237fead6SMichael Halcrow  * Returns Zero if a valid authentication token was retrieved and
1130237fead6SMichael Halcrow  * processed; negative value for file not encrypted or for error
1131237fead6SMichael Halcrow  * conditions.
1132237fead6SMichael Halcrow  */
1133237fead6SMichael Halcrow int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1134237fead6SMichael Halcrow 			      unsigned char *src,
1135237fead6SMichael Halcrow 			      struct dentry *ecryptfs_dentry)
1136237fead6SMichael Halcrow {
1137237fead6SMichael Halcrow 	size_t i = 0;
1138f4aad16aSMichael Halcrow 	size_t found_auth_tok;
1139237fead6SMichael Halcrow 	size_t next_packet_is_auth_tok_packet;
1140237fead6SMichael Halcrow 	struct list_head auth_tok_list;
1141dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *matching_auth_tok;
1142dd8e2902SMichael Halcrow 	struct ecryptfs_auth_tok *candidate_auth_tok;
1143f4aad16aSMichael Halcrow 	char *candidate_auth_tok_sig;
1144237fead6SMichael Halcrow 	size_t packet_size;
1145237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *new_auth_tok;
1146237fead6SMichael Halcrow 	unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
1147f4aad16aSMichael Halcrow 	struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
1148237fead6SMichael Halcrow 	size_t tag_11_contents_size;
1149237fead6SMichael Halcrow 	size_t tag_11_packet_size;
1150dddfa461SMichael Halcrow 	int rc = 0;
1151237fead6SMichael Halcrow 
1152237fead6SMichael Halcrow 	INIT_LIST_HEAD(&auth_tok_list);
1153f4aad16aSMichael Halcrow 	/* Parse the header to find as many packets as we can; these will be
1154237fead6SMichael Halcrow 	 * added the our &auth_tok_list */
1155237fead6SMichael Halcrow 	next_packet_is_auth_tok_packet = 1;
1156237fead6SMichael Halcrow 	while (next_packet_is_auth_tok_packet) {
1157237fead6SMichael Halcrow 		size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1158237fead6SMichael Halcrow 
1159237fead6SMichael Halcrow 		switch (src[i]) {
1160237fead6SMichael Halcrow 		case ECRYPTFS_TAG_3_PACKET_TYPE:
1161237fead6SMichael Halcrow 			rc = parse_tag_3_packet(crypt_stat,
1162237fead6SMichael Halcrow 						(unsigned char *)&src[i],
1163237fead6SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1164237fead6SMichael Halcrow 						&packet_size, max_packet_size);
1165237fead6SMichael Halcrow 			if (rc) {
1166237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1167237fead6SMichael Halcrow 						"tag 3 packet\n");
1168237fead6SMichael Halcrow 				rc = -EIO;
1169237fead6SMichael Halcrow 				goto out_wipe_list;
1170237fead6SMichael Halcrow 			}
1171237fead6SMichael Halcrow 			i += packet_size;
1172237fead6SMichael Halcrow 			rc = parse_tag_11_packet((unsigned char *)&src[i],
1173237fead6SMichael Halcrow 						 sig_tmp_space,
1174237fead6SMichael Halcrow 						 ECRYPTFS_SIG_SIZE,
1175237fead6SMichael Halcrow 						 &tag_11_contents_size,
1176237fead6SMichael Halcrow 						 &tag_11_packet_size,
1177237fead6SMichael Halcrow 						 max_packet_size);
1178237fead6SMichael Halcrow 			if (rc) {
1179237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "No valid "
1180237fead6SMichael Halcrow 						"(ecryptfs-specific) literal "
1181237fead6SMichael Halcrow 						"packet containing "
1182237fead6SMichael Halcrow 						"authentication token "
1183237fead6SMichael Halcrow 						"signature found after "
1184237fead6SMichael Halcrow 						"tag 3 packet\n");
1185237fead6SMichael Halcrow 				rc = -EIO;
1186237fead6SMichael Halcrow 				goto out_wipe_list;
1187237fead6SMichael Halcrow 			}
1188237fead6SMichael Halcrow 			i += tag_11_packet_size;
1189237fead6SMichael Halcrow 			if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1190237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Expected "
1191237fead6SMichael Halcrow 						"signature of size [%d]; "
1192237fead6SMichael Halcrow 						"read size [%d]\n",
1193237fead6SMichael Halcrow 						ECRYPTFS_SIG_SIZE,
1194237fead6SMichael Halcrow 						tag_11_contents_size);
1195237fead6SMichael Halcrow 				rc = -EIO;
1196237fead6SMichael Halcrow 				goto out_wipe_list;
1197237fead6SMichael Halcrow 			}
1198237fead6SMichael Halcrow 			ecryptfs_to_hex(new_auth_tok->token.password.signature,
1199237fead6SMichael Halcrow 					sig_tmp_space, tag_11_contents_size);
1200237fead6SMichael Halcrow 			new_auth_tok->token.password.signature[
1201237fead6SMichael Halcrow 				ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
1202e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1203237fead6SMichael Halcrow 			break;
1204dddfa461SMichael Halcrow 		case ECRYPTFS_TAG_1_PACKET_TYPE:
1205dddfa461SMichael Halcrow 			rc = parse_tag_1_packet(crypt_stat,
1206dddfa461SMichael Halcrow 						(unsigned char *)&src[i],
1207dddfa461SMichael Halcrow 						&auth_tok_list, &new_auth_tok,
1208dddfa461SMichael Halcrow 						&packet_size, max_packet_size);
1209dddfa461SMichael Halcrow 			if (rc) {
1210dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error parsing "
1211dddfa461SMichael Halcrow 						"tag 1 packet\n");
1212dddfa461SMichael Halcrow 				rc = -EIO;
1213dddfa461SMichael Halcrow 				goto out_wipe_list;
1214dddfa461SMichael Halcrow 			}
1215dddfa461SMichael Halcrow 			i += packet_size;
1216e2bd99ecSMichael Halcrow 			crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
1217dddfa461SMichael Halcrow 			break;
1218237fead6SMichael Halcrow 		case ECRYPTFS_TAG_11_PACKET_TYPE:
1219237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1220237fead6SMichael Halcrow 					"(Tag 11 not allowed by itself)\n");
1221237fead6SMichael Halcrow 			rc = -EIO;
1222237fead6SMichael Halcrow 			goto out_wipe_list;
1223237fead6SMichael Halcrow 			break;
1224237fead6SMichael Halcrow 		default:
1225237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1226237fead6SMichael Halcrow 					"[%d] of the file header; hex value of "
1227237fead6SMichael Halcrow 					"character is [0x%.2x]\n", i, src[i]);
1228237fead6SMichael Halcrow 			next_packet_is_auth_tok_packet = 0;
1229237fead6SMichael Halcrow 		}
1230237fead6SMichael Halcrow 	}
1231237fead6SMichael Halcrow 	if (list_empty(&auth_tok_list)) {
1232f4aad16aSMichael Halcrow 		printk(KERN_ERR "The lower file appears to be a non-encrypted "
1233f4aad16aSMichael Halcrow 		       "eCryptfs file; this is not supported in this version "
1234f4aad16aSMichael Halcrow 		       "of the eCryptfs kernel module\n");
1235f4aad16aSMichael Halcrow 		rc = -EINVAL;
1236237fead6SMichael Halcrow 		goto out;
1237237fead6SMichael Halcrow 	}
1238f4aad16aSMichael Halcrow 	/* auth_tok_list contains the set of authentication tokens
1239f4aad16aSMichael Halcrow 	 * parsed from the metadata. We need to find a matching
1240f4aad16aSMichael Halcrow 	 * authentication token that has the secret component(s)
1241f4aad16aSMichael Halcrow 	 * necessary to decrypt the EFEK in the auth_tok parsed from
1242f4aad16aSMichael Halcrow 	 * the metadata. There may be several potential matches, but
1243f4aad16aSMichael Halcrow 	 * just one will be sufficient to decrypt to get the FEK. */
1244f4aad16aSMichael Halcrow find_next_matching_auth_tok:
1245f4aad16aSMichael Halcrow 	found_auth_tok = 0;
1246f4aad16aSMichael Halcrow 	list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
1247237fead6SMichael Halcrow 		candidate_auth_tok = &auth_tok_list_item->auth_tok;
1248237fead6SMichael Halcrow 		if (unlikely(ecryptfs_verbosity > 0)) {
1249237fead6SMichael Halcrow 			ecryptfs_printk(KERN_DEBUG,
1250237fead6SMichael Halcrow 					"Considering cadidate auth tok:\n");
1251237fead6SMichael Halcrow 			ecryptfs_dump_auth_tok(candidate_auth_tok);
1252237fead6SMichael Halcrow 		}
12535dda6992SMichael Halcrow 		rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
12545dda6992SMichael Halcrow 					       candidate_auth_tok);
12555dda6992SMichael Halcrow 		if (rc) {
1256f4aad16aSMichael Halcrow 			printk(KERN_ERR
1257f4aad16aSMichael Halcrow 			       "Unrecognized candidate auth tok type: [%d]\n",
1258f4aad16aSMichael Halcrow 			       candidate_auth_tok->token_type);
1259f4aad16aSMichael Halcrow 			rc = -EINVAL;
1260f4aad16aSMichael Halcrow 			goto out_wipe_list;
1261f4aad16aSMichael Halcrow 		}
12625dda6992SMichael Halcrow 		ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat,
12635dda6992SMichael Halcrow 					       candidate_auth_tok_sig);
1264f4aad16aSMichael Halcrow 		if (matching_auth_tok) {
1265237fead6SMichael Halcrow 			found_auth_tok = 1;
1266f4aad16aSMichael Halcrow 			goto found_matching_auth_tok;
1267237fead6SMichael Halcrow 		}
1268237fead6SMichael Halcrow 	}
1269237fead6SMichael Halcrow 	if (!found_auth_tok) {
1270f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Could not find a usable "
1271f4aad16aSMichael Halcrow 				"authentication token\n");
1272237fead6SMichael Halcrow 		rc = -EIO;
1273237fead6SMichael Halcrow 		goto out_wipe_list;
1274dddfa461SMichael Halcrow 	}
1275f4aad16aSMichael Halcrow found_matching_auth_tok:
1276e2bd99ecSMichael Halcrow 	if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1277dddfa461SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.private_key),
1278f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.private_key),
1279dddfa461SMichael Halcrow 		       sizeof(struct ecryptfs_private_key));
1280f4aad16aSMichael Halcrow 		rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
1281dddfa461SMichael Halcrow 						       crypt_stat);
1282dddfa461SMichael Halcrow 	} else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
1283237fead6SMichael Halcrow 		memcpy(&(candidate_auth_tok->token.password),
1284f4aad16aSMichael Halcrow 		       &(matching_auth_tok->token.password),
1285237fead6SMichael Halcrow 		       sizeof(struct ecryptfs_password));
1286f4aad16aSMichael Halcrow 		rc = decrypt_passphrase_encrypted_session_key(
1287f4aad16aSMichael Halcrow 			candidate_auth_tok, crypt_stat);
1288dddfa461SMichael Halcrow 	}
1289237fead6SMichael Halcrow 	if (rc) {
1290f4aad16aSMichael Halcrow 		struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1291f4aad16aSMichael Halcrow 
1292f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1293f4aad16aSMichael Halcrow 				"session key for authentication token with sig "
1294f4aad16aSMichael Halcrow 				"[%.*s]; rc = [%d]. Removing auth tok "
1295f4aad16aSMichael Halcrow 				"candidate from the list and searching for "
1296f4aad16aSMichael Halcrow 				"the next match.\n", candidate_auth_tok_sig,
1297f4aad16aSMichael Halcrow 				ECRYPTFS_SIG_SIZE_HEX, rc);
1298f4aad16aSMichael Halcrow 		list_for_each_entry_safe(auth_tok_list_item,
1299f4aad16aSMichael Halcrow 					 auth_tok_list_item_tmp,
1300f4aad16aSMichael Halcrow 					 &auth_tok_list, list) {
1301f4aad16aSMichael Halcrow 			if (candidate_auth_tok
1302f4aad16aSMichael Halcrow 			    == &auth_tok_list_item->auth_tok) {
1303f4aad16aSMichael Halcrow 				list_del(&auth_tok_list_item->list);
1304f4aad16aSMichael Halcrow 				kmem_cache_free(
1305f4aad16aSMichael Halcrow 					ecryptfs_auth_tok_list_item_cache,
1306f4aad16aSMichael Halcrow 					auth_tok_list_item);
1307f4aad16aSMichael Halcrow 				goto find_next_matching_auth_tok;
1308f4aad16aSMichael Halcrow 			}
1309f4aad16aSMichael Halcrow 		}
1310f4aad16aSMichael Halcrow 		BUG();
1311237fead6SMichael Halcrow 	}
1312237fead6SMichael Halcrow 	rc = ecryptfs_compute_root_iv(crypt_stat);
1313237fead6SMichael Halcrow 	if (rc) {
1314237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error computing "
1315237fead6SMichael Halcrow 				"the root IV\n");
1316237fead6SMichael Halcrow 		goto out_wipe_list;
1317237fead6SMichael Halcrow 	}
1318237fead6SMichael Halcrow 	rc = ecryptfs_init_crypt_ctx(crypt_stat);
1319237fead6SMichael Halcrow 	if (rc) {
1320237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1321237fead6SMichael Halcrow 				"context for cipher [%s]; rc = [%d]\n",
1322237fead6SMichael Halcrow 				crypt_stat->cipher, rc);
1323237fead6SMichael Halcrow 	}
1324237fead6SMichael Halcrow out_wipe_list:
1325237fead6SMichael Halcrow 	wipe_auth_tok_list(&auth_tok_list);
1326237fead6SMichael Halcrow out:
1327237fead6SMichael Halcrow 	return rc;
1328237fead6SMichael Halcrow }
1329f4aad16aSMichael Halcrow 
1330dddfa461SMichael Halcrow static int
1331dddfa461SMichael Halcrow pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1332dddfa461SMichael Halcrow 			struct ecryptfs_crypt_stat *crypt_stat,
1333dddfa461SMichael Halcrow 			struct ecryptfs_key_record *key_rec)
1334dddfa461SMichael Halcrow {
1335dddfa461SMichael Halcrow 	struct ecryptfs_msg_ctx *msg_ctx = NULL;
1336624ae528STyler Hicks 	char *payload = NULL;
1337624ae528STyler Hicks 	size_t payload_len;
1338dddfa461SMichael Halcrow 	struct ecryptfs_message *msg;
1339dddfa461SMichael Halcrow 	int rc;
1340dddfa461SMichael Halcrow 
1341dddfa461SMichael Halcrow 	rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1342dddfa461SMichael Halcrow 				 ecryptfs_code_for_cipher_string(crypt_stat),
1343624ae528STyler Hicks 				 crypt_stat, &payload, &payload_len);
1344dddfa461SMichael Halcrow 	if (rc) {
1345dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1346dddfa461SMichael Halcrow 		goto out;
1347dddfa461SMichael Halcrow 	}
1348624ae528STyler Hicks 	rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
1349dddfa461SMichael Halcrow 	if (rc) {
1350624ae528STyler Hicks 		ecryptfs_printk(KERN_ERR, "Error sending message to "
1351624ae528STyler Hicks 				"ecryptfsd\n");
1352dddfa461SMichael Halcrow 		goto out;
1353dddfa461SMichael Halcrow 	}
1354dddfa461SMichael Halcrow 	rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1355dddfa461SMichael Halcrow 	if (rc) {
1356dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1357dddfa461SMichael Halcrow 				"from the user space daemon\n");
1358dddfa461SMichael Halcrow 		rc = -EIO;
1359dddfa461SMichael Halcrow 		goto out;
1360dddfa461SMichael Halcrow 	}
1361dddfa461SMichael Halcrow 	rc = parse_tag_67_packet(key_rec, msg);
1362dddfa461SMichael Halcrow 	if (rc)
1363dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1364dddfa461SMichael Halcrow 	kfree(msg);
1365dddfa461SMichael Halcrow out:
1366624ae528STyler Hicks 	kfree(payload);
1367dddfa461SMichael Halcrow 	return rc;
1368dddfa461SMichael Halcrow }
1369dddfa461SMichael Halcrow /**
1370dddfa461SMichael Halcrow  * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1371dddfa461SMichael Halcrow  * @dest: Buffer into which to write the packet
137222e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be writtn
137322e78fafSMichael Halcrow  * @auth_tok: The authentication token used for generating the tag 1 packet
137422e78fafSMichael Halcrow  * @crypt_stat: The cryptographic context
137522e78fafSMichael Halcrow  * @key_rec: The key record struct for the tag 1 packet
1376dddfa461SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
1377dddfa461SMichael Halcrow  *               up constituting the packet; set to zero on error
1378dddfa461SMichael Halcrow  *
1379dddfa461SMichael Halcrow  * Returns zero on success; non-zero on error.
1380dddfa461SMichael Halcrow  */
1381dddfa461SMichael Halcrow static int
1382f4aad16aSMichael Halcrow write_tag_1_packet(char *dest, size_t *remaining_bytes,
1383f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
1384dddfa461SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
1385dddfa461SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
1386dddfa461SMichael Halcrow {
1387dddfa461SMichael Halcrow 	size_t i;
1388dddfa461SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
1389dddfa461SMichael Halcrow 	size_t packet_size_length;
1390f4aad16aSMichael Halcrow 	size_t max_packet_size;
1391dddfa461SMichael Halcrow 	int rc = 0;
1392dddfa461SMichael Halcrow 
1393dddfa461SMichael Halcrow 	(*packet_size) = 0;
1394dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1395dddfa461SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
1396dddfa461SMichael Halcrow 	encrypted_session_key_valid = 0;
1397dddfa461SMichael Halcrow 	for (i = 0; i < crypt_stat->key_size; i++)
1398dddfa461SMichael Halcrow 		encrypted_session_key_valid |=
1399dddfa461SMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
1400dddfa461SMichael Halcrow 	if (encrypted_session_key_valid) {
1401dddfa461SMichael Halcrow 		memcpy(key_rec->enc_key,
1402dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key,
1403dddfa461SMichael Halcrow 		       auth_tok->session_key.encrypted_key_size);
1404dddfa461SMichael Halcrow 		goto encrypted_session_key_set;
1405dddfa461SMichael Halcrow 	}
1406dddfa461SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
1407dddfa461SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
1408dddfa461SMichael Halcrow 			auth_tok->token.private_key.key_size;
1409dddfa461SMichael Halcrow 	rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1410dddfa461SMichael Halcrow 	if (rc) {
1411f66e883eSMichael Halcrow 		printk(KERN_ERR "Failed to encrypt session key via a key "
1412f66e883eSMichael Halcrow 		       "module; rc = [%d]\n", rc);
1413dddfa461SMichael Halcrow 		goto out;
1414dddfa461SMichael Halcrow 	}
1415dddfa461SMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1416dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1417dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1418dddfa461SMichael Halcrow 	}
1419dddfa461SMichael Halcrow encrypted_session_key_set:
1420f4aad16aSMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1421f4aad16aSMichael Halcrow 	 * packet tag 1 */
1422f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 1 identifier */
1423f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 1 packet size */
1424f4aad16aSMichael Halcrow 			   + 1                       /* Version */
1425f4aad16aSMichael Halcrow 			   + ECRYPTFS_SIG_SIZE       /* Key identifier */
1426f4aad16aSMichael Halcrow 			   + 1                       /* Cipher identifier */
1427f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
1428f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
1429f4aad16aSMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
143081acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
1431f4aad16aSMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
1432dddfa461SMichael Halcrow 		rc = -EINVAL;
1433dddfa461SMichael Halcrow 		goto out;
1434dddfa461SMichael Halcrow 	}
1435dddfa461SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
1436f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1437f66e883eSMichael Halcrow 					  (max_packet_size - 4),
1438dddfa461SMichael Halcrow 					  &packet_size_length);
1439dddfa461SMichael Halcrow 	if (rc) {
1440dddfa461SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1441dddfa461SMichael Halcrow 				"header; cannot generate packet length\n");
1442dddfa461SMichael Halcrow 		goto out;
1443dddfa461SMichael Halcrow 	}
1444dddfa461SMichael Halcrow 	(*packet_size) += packet_size_length;
1445dddfa461SMichael Halcrow 	dest[(*packet_size)++] = 0x03; /* version 3 */
1446dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1447dddfa461SMichael Halcrow 	(*packet_size) += ECRYPTFS_SIG_SIZE;
1448dddfa461SMichael Halcrow 	dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1449dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
1450dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
1451dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
1452dddfa461SMichael Halcrow out:
1453dddfa461SMichael Halcrow 	if (rc)
1454dddfa461SMichael Halcrow 		(*packet_size) = 0;
1455f4aad16aSMichael Halcrow 	else
1456f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
1457dddfa461SMichael Halcrow 	return rc;
1458dddfa461SMichael Halcrow }
1459237fead6SMichael Halcrow 
1460237fead6SMichael Halcrow /**
1461237fead6SMichael Halcrow  * write_tag_11_packet
1462237fead6SMichael Halcrow  * @dest: Target into which Tag 11 packet is to be written
146322e78fafSMichael Halcrow  * @remaining_bytes: Maximum packet length
1464237fead6SMichael Halcrow  * @contents: Byte array of contents to copy in
1465237fead6SMichael Halcrow  * @contents_length: Number of bytes in contents
1466237fead6SMichael Halcrow  * @packet_length: Length of the Tag 11 packet written; zero on error
1467237fead6SMichael Halcrow  *
1468237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1469237fead6SMichael Halcrow  */
1470237fead6SMichael Halcrow static int
147181acbcd6SAndrew Morton write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
1472146a4606SMichael Halcrow 		    size_t contents_length, size_t *packet_length)
1473237fead6SMichael Halcrow {
1474237fead6SMichael Halcrow 	size_t packet_size_length;
1475146a4606SMichael Halcrow 	size_t max_packet_size;
1476dddfa461SMichael Halcrow 	int rc = 0;
1477237fead6SMichael Halcrow 
1478237fead6SMichael Halcrow 	(*packet_length) = 0;
1479146a4606SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1480146a4606SMichael Halcrow 	 * packet tag 11 */
1481146a4606SMichael Halcrow 	max_packet_size = (1                   /* Tag 11 identifier */
1482146a4606SMichael Halcrow 			   + 3                 /* Max Tag 11 packet size */
1483146a4606SMichael Halcrow 			   + 1                 /* Binary format specifier */
1484146a4606SMichael Halcrow 			   + 1                 /* Filename length */
1485146a4606SMichael Halcrow 			   + 8                 /* Filename ("_CONSOLE") */
1486146a4606SMichael Halcrow 			   + 4                 /* Modification date */
1487146a4606SMichael Halcrow 			   + contents_length); /* Literal data */
1488146a4606SMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
1489146a4606SMichael Halcrow 		printk(KERN_ERR "Packet length larger than maximum allowable; "
149081acbcd6SAndrew Morton 		       "need up to [%td] bytes, but there are only [%td] "
1491146a4606SMichael Halcrow 		       "available\n", max_packet_size, (*remaining_bytes));
1492237fead6SMichael Halcrow 		rc = -EINVAL;
1493237fead6SMichael Halcrow 		goto out;
1494237fead6SMichael Halcrow 	}
1495237fead6SMichael Halcrow 	dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
1496f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
1497f66e883eSMichael Halcrow 					  (max_packet_size - 4),
1498f66e883eSMichael Halcrow 					  &packet_size_length);
1499237fead6SMichael Halcrow 	if (rc) {
1500146a4606SMichael Halcrow 		printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1501146a4606SMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
1502237fead6SMichael Halcrow 		goto out;
1503237fead6SMichael Halcrow 	}
1504237fead6SMichael Halcrow 	(*packet_length) += packet_size_length;
1505146a4606SMichael Halcrow 	dest[(*packet_length)++] = 0x62; /* binary data format specifier */
1506237fead6SMichael Halcrow 	dest[(*packet_length)++] = 8;
1507237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1508237fead6SMichael Halcrow 	(*packet_length) += 8;
1509237fead6SMichael Halcrow 	memset(&dest[(*packet_length)], 0x00, 4);
1510237fead6SMichael Halcrow 	(*packet_length) += 4;
1511237fead6SMichael Halcrow 	memcpy(&dest[(*packet_length)], contents, contents_length);
1512237fead6SMichael Halcrow 	(*packet_length) += contents_length;
1513237fead6SMichael Halcrow  out:
1514237fead6SMichael Halcrow 	if (rc)
1515237fead6SMichael Halcrow 		(*packet_length) = 0;
1516146a4606SMichael Halcrow 	else
1517146a4606SMichael Halcrow 		(*remaining_bytes) -= (*packet_length);
1518237fead6SMichael Halcrow 	return rc;
1519237fead6SMichael Halcrow }
1520237fead6SMichael Halcrow 
1521237fead6SMichael Halcrow /**
1522237fead6SMichael Halcrow  * write_tag_3_packet
1523237fead6SMichael Halcrow  * @dest: Buffer into which to write the packet
152422e78fafSMichael Halcrow  * @remaining_bytes: Maximum number of bytes that can be written
1525237fead6SMichael Halcrow  * @auth_tok: Authentication token
1526237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context
1527237fead6SMichael Halcrow  * @key_rec: encrypted key
1528237fead6SMichael Halcrow  * @packet_size: This function will write the number of bytes that end
1529237fead6SMichael Halcrow  *               up constituting the packet; set to zero on error
1530237fead6SMichael Halcrow  *
1531237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1532237fead6SMichael Halcrow  */
1533237fead6SMichael Halcrow static int
1534f4aad16aSMichael Halcrow write_tag_3_packet(char *dest, size_t *remaining_bytes,
1535f4aad16aSMichael Halcrow 		   struct ecryptfs_auth_tok *auth_tok,
1536237fead6SMichael Halcrow 		   struct ecryptfs_crypt_stat *crypt_stat,
1537237fead6SMichael Halcrow 		   struct ecryptfs_key_record *key_rec, size_t *packet_size)
1538237fead6SMichael Halcrow {
1539237fead6SMichael Halcrow 	size_t i;
1540237fead6SMichael Halcrow 	size_t encrypted_session_key_valid = 0;
1541237fead6SMichael Halcrow 	char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
1542f4aad16aSMichael Halcrow 	struct scatterlist dst_sg;
1543f4aad16aSMichael Halcrow 	struct scatterlist src_sg;
1544237fead6SMichael Halcrow 	struct mutex *tfm_mutex = NULL;
154519e66a67STrevor Highland 	u8 cipher_code;
1546f4aad16aSMichael Halcrow 	size_t packet_size_length;
1547f4aad16aSMichael Halcrow 	size_t max_packet_size;
1548f4aad16aSMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1549f4aad16aSMichael Halcrow 		crypt_stat->mount_crypt_stat;
15508bba066fSMichael Halcrow 	struct blkcipher_desc desc = {
15518bba066fSMichael Halcrow 		.tfm = NULL,
15528bba066fSMichael Halcrow 		.flags = CRYPTO_TFM_REQ_MAY_SLEEP
15538bba066fSMichael Halcrow 	};
15548bba066fSMichael Halcrow 	int rc = 0;
1555237fead6SMichael Halcrow 
1556237fead6SMichael Halcrow 	(*packet_size) = 0;
1557dddfa461SMichael Halcrow 	ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
1558237fead6SMichael Halcrow 			  ECRYPTFS_SIG_SIZE);
1559f4aad16aSMichael Halcrow 	rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1560f4aad16aSMichael Halcrow 							crypt_stat->cipher);
1561f4aad16aSMichael Halcrow 	if (unlikely(rc)) {
1562f4aad16aSMichael Halcrow 		printk(KERN_ERR "Internal error whilst attempting to get "
1563f4aad16aSMichael Halcrow 		       "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1564f4aad16aSMichael Halcrow 		       crypt_stat->cipher, rc);
1565f4aad16aSMichael Halcrow 		goto out;
1566237fead6SMichael Halcrow 	}
1567f4aad16aSMichael Halcrow 	if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1568f4aad16aSMichael Halcrow 		struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1569f4aad16aSMichael Halcrow 
1570f4aad16aSMichael Halcrow 		printk(KERN_WARNING "No key size specified at mount; "
1571f4aad16aSMichael Halcrow 		       "defaulting to [%d]\n", alg->max_keysize);
1572f4aad16aSMichael Halcrow 		mount_crypt_stat->global_default_cipher_key_size =
1573f4aad16aSMichael Halcrow 			alg->max_keysize;
1574f4aad16aSMichael Halcrow 	}
1575f4aad16aSMichael Halcrow 	if (crypt_stat->key_size == 0)
1576f4aad16aSMichael Halcrow 		crypt_stat->key_size =
1577f4aad16aSMichael Halcrow 			mount_crypt_stat->global_default_cipher_key_size;
1578237fead6SMichael Halcrow 	if (auth_tok->session_key.encrypted_key_size == 0)
1579237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size =
1580237fead6SMichael Halcrow 			crypt_stat->key_size;
1581237fead6SMichael Halcrow 	if (crypt_stat->key_size == 24
1582237fead6SMichael Halcrow 	    && strcmp("aes", crypt_stat->cipher) == 0) {
1583237fead6SMichael Halcrow 		memset((crypt_stat->key + 24), 0, 8);
1584237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size = 32;
1585f4aad16aSMichael Halcrow 	} else
1586f4aad16aSMichael Halcrow 		auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
1587dddfa461SMichael Halcrow 	key_rec->enc_key_size =
1588237fead6SMichael Halcrow 		auth_tok->session_key.encrypted_key_size;
1589f4aad16aSMichael Halcrow 	encrypted_session_key_valid = 0;
1590f4aad16aSMichael Halcrow 	for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1591f4aad16aSMichael Halcrow 		encrypted_session_key_valid |=
1592f4aad16aSMichael Halcrow 			auth_tok->session_key.encrypted_key[i];
1593f4aad16aSMichael Halcrow 	if (encrypted_session_key_valid) {
1594f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1595f4aad16aSMichael Halcrow 				"using auth_tok->session_key.encrypted_key, "
1596f4aad16aSMichael Halcrow 				"where key_rec->enc_key_size = [%d]\n",
1597f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
1598f4aad16aSMichael Halcrow 		memcpy(key_rec->enc_key,
1599f4aad16aSMichael Halcrow 		       auth_tok->session_key.encrypted_key,
1600f4aad16aSMichael Halcrow 		       key_rec->enc_key_size);
1601f4aad16aSMichael Halcrow 		goto encrypted_session_key_set;
1602f4aad16aSMichael Halcrow 	}
1603dddfa461SMichael Halcrow 	if (auth_tok->token.password.flags &
1604dddfa461SMichael Halcrow 	    ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
1605237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1606237fead6SMichael Halcrow 				"session key encryption key of size [%d]\n",
1607237fead6SMichael Halcrow 				auth_tok->token.password.
1608237fead6SMichael Halcrow 				session_key_encryption_key_bytes);
1609237fead6SMichael Halcrow 		memcpy(session_key_encryption_key,
1610237fead6SMichael Halcrow 		       auth_tok->token.password.session_key_encryption_key,
1611237fead6SMichael Halcrow 		       crypt_stat->key_size);
1612237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG,
1613237fead6SMichael Halcrow 				"Cached session key " "encryption key: \n");
1614237fead6SMichael Halcrow 		if (ecryptfs_verbosity > 0)
1615237fead6SMichael Halcrow 			ecryptfs_dump_hex(session_key_encryption_key, 16);
1616237fead6SMichael Halcrow 	}
1617237fead6SMichael Halcrow 	if (unlikely(ecryptfs_verbosity > 0)) {
1618237fead6SMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1619237fead6SMichael Halcrow 		ecryptfs_dump_hex(session_key_encryption_key, 16);
1620237fead6SMichael Halcrow 	}
16215dda6992SMichael Halcrow 	rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
16225dda6992SMichael Halcrow 				 &src_sg, 1);
16235dda6992SMichael Halcrow 	if (rc != 1) {
1624237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
1625f4aad16aSMichael Halcrow 				"for crypt_stat session key; expected rc = 1; "
1626f4aad16aSMichael Halcrow 				"got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1627f4aad16aSMichael Halcrow 				rc, key_rec->enc_key_size);
1628237fead6SMichael Halcrow 		rc = -ENOMEM;
1629237fead6SMichael Halcrow 		goto out;
1630237fead6SMichael Halcrow 	}
16315dda6992SMichael Halcrow 	rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
16325dda6992SMichael Halcrow 				 &dst_sg, 1);
16335dda6992SMichael Halcrow 	if (rc != 1) {
1634237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
1635f4aad16aSMichael Halcrow 				"for crypt_stat encrypted session key; "
1636f4aad16aSMichael Halcrow 				"expected rc = 1; got rc = [%d]. "
1637f4aad16aSMichael Halcrow 				"key_rec->enc_key_size = [%d]\n", rc,
1638f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
1639237fead6SMichael Halcrow 		rc = -ENOMEM;
1640237fead6SMichael Halcrow 		goto out;
1641237fead6SMichael Halcrow 	}
1642237fead6SMichael Halcrow 	mutex_lock(tfm_mutex);
16438bba066fSMichael Halcrow 	rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1644237fead6SMichael Halcrow 				     crypt_stat->key_size);
1645237fead6SMichael Halcrow 	if (rc < 0) {
1646237fead6SMichael Halcrow 		mutex_unlock(tfm_mutex);
1647237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
16488bba066fSMichael Halcrow 				"context; rc = [%d]\n", rc);
1649237fead6SMichael Halcrow 		goto out;
1650237fead6SMichael Halcrow 	}
1651237fead6SMichael Halcrow 	rc = 0;
1652237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1653237fead6SMichael Halcrow 			crypt_stat->key_size);
1654f4aad16aSMichael Halcrow 	rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
1655237fead6SMichael Halcrow 				      (*key_rec).enc_key_size);
1656f4aad16aSMichael Halcrow 	mutex_unlock(tfm_mutex);
16578bba066fSMichael Halcrow 	if (rc) {
16588bba066fSMichael Halcrow 		printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
16598bba066fSMichael Halcrow 		goto out;
16608bba066fSMichael Halcrow 	}
1661237fead6SMichael Halcrow 	ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
1662f4aad16aSMichael Halcrow 	if (ecryptfs_verbosity > 0) {
1663f4aad16aSMichael Halcrow 		ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1664f4aad16aSMichael Halcrow 				key_rec->enc_key_size);
1665dddfa461SMichael Halcrow 		ecryptfs_dump_hex(key_rec->enc_key,
1666dddfa461SMichael Halcrow 				  key_rec->enc_key_size);
1667f4aad16aSMichael Halcrow 	}
1668237fead6SMichael Halcrow encrypted_session_key_set:
1669237fead6SMichael Halcrow 	/* This format is inspired by OpenPGP; see RFC 2440
1670237fead6SMichael Halcrow 	 * packet tag 3 */
1671f4aad16aSMichael Halcrow 	max_packet_size = (1                         /* Tag 3 identifier */
1672f4aad16aSMichael Halcrow 			   + 3                       /* Max Tag 3 packet size */
1673f4aad16aSMichael Halcrow 			   + 1                       /* Version */
1674f4aad16aSMichael Halcrow 			   + 1                       /* Cipher code */
1675f4aad16aSMichael Halcrow 			   + 1                       /* S2K specifier */
1676f4aad16aSMichael Halcrow 			   + 1                       /* Hash identifier */
1677f4aad16aSMichael Halcrow 			   + ECRYPTFS_SALT_SIZE      /* Salt */
1678f4aad16aSMichael Halcrow 			   + 1                       /* Hash iterations */
1679f4aad16aSMichael Halcrow 			   + key_rec->enc_key_size); /* Encrypted key size */
1680f4aad16aSMichael Halcrow 	if (max_packet_size > (*remaining_bytes)) {
168181acbcd6SAndrew Morton 		printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
168281acbcd6SAndrew Morton 		       "there are only [%td] available\n", max_packet_size,
1683f4aad16aSMichael Halcrow 		       (*remaining_bytes));
1684f4aad16aSMichael Halcrow 		rc = -EINVAL;
1685f4aad16aSMichael Halcrow 		goto out;
1686f4aad16aSMichael Halcrow 	}
1687237fead6SMichael Halcrow 	dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
1688f4aad16aSMichael Halcrow 	/* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1689f4aad16aSMichael Halcrow 	 * to get the number of octets in the actual Tag 3 packet */
1690f66e883eSMichael Halcrow 	rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1691f66e883eSMichael Halcrow 					  (max_packet_size - 4),
1692237fead6SMichael Halcrow 					  &packet_size_length);
1693237fead6SMichael Halcrow 	if (rc) {
1694f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1695f4aad16aSMichael Halcrow 		       "generate packet length. rc = [%d]\n", rc);
1696237fead6SMichael Halcrow 		goto out;
1697237fead6SMichael Halcrow 	}
1698237fead6SMichael Halcrow 	(*packet_size) += packet_size_length;
1699237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x04; /* version 4 */
1700f4aad16aSMichael Halcrow 	/* TODO: Break from RFC2440 so that arbitrary ciphers can be
1701f4aad16aSMichael Halcrow 	 * specified with strings */
1702237fead6SMichael Halcrow 	cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1703237fead6SMichael Halcrow 	if (cipher_code == 0) {
1704237fead6SMichael Halcrow 		ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1705237fead6SMichael Halcrow 				"cipher [%s]\n", crypt_stat->cipher);
1706237fead6SMichael Halcrow 		rc = -EINVAL;
1707237fead6SMichael Halcrow 		goto out;
1708237fead6SMichael Halcrow 	}
1709237fead6SMichael Halcrow 	dest[(*packet_size)++] = cipher_code;
1710237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x03;	/* S2K */
1711237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x01;	/* MD5 (TODO: parameterize) */
1712237fead6SMichael Halcrow 	memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1713237fead6SMichael Halcrow 	       ECRYPTFS_SALT_SIZE);
1714237fead6SMichael Halcrow 	(*packet_size) += ECRYPTFS_SALT_SIZE;	/* salt */
1715237fead6SMichael Halcrow 	dest[(*packet_size)++] = 0x60;	/* hash iterations (65536) */
1716dddfa461SMichael Halcrow 	memcpy(&dest[(*packet_size)], key_rec->enc_key,
1717dddfa461SMichael Halcrow 	       key_rec->enc_key_size);
1718dddfa461SMichael Halcrow 	(*packet_size) += key_rec->enc_key_size;
1719237fead6SMichael Halcrow out:
1720237fead6SMichael Halcrow 	if (rc)
1721237fead6SMichael Halcrow 		(*packet_size) = 0;
1722f4aad16aSMichael Halcrow 	else
1723f4aad16aSMichael Halcrow 		(*remaining_bytes) -= (*packet_size);
1724237fead6SMichael Halcrow 	return rc;
1725237fead6SMichael Halcrow }
1726237fead6SMichael Halcrow 
1727eb95e7ffSMichael Halcrow struct kmem_cache *ecryptfs_key_record_cache;
1728eb95e7ffSMichael Halcrow 
1729237fead6SMichael Halcrow /**
1730237fead6SMichael Halcrow  * ecryptfs_generate_key_packet_set
173122e78fafSMichael Halcrow  * @dest_base: Virtual address from which to write the key record set
1732237fead6SMichael Halcrow  * @crypt_stat: The cryptographic context from which the
1733237fead6SMichael Halcrow  *              authentication tokens will be retrieved
1734237fead6SMichael Halcrow  * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1735237fead6SMichael Halcrow  *                   for the global parameters
1736237fead6SMichael Halcrow  * @len: The amount written
1737237fead6SMichael Halcrow  * @max: The maximum amount of data allowed to be written
1738237fead6SMichael Halcrow  *
1739237fead6SMichael Halcrow  * Generates a key packet set and writes it to the virtual address
1740237fead6SMichael Halcrow  * passed in.
1741237fead6SMichael Halcrow  *
1742237fead6SMichael Halcrow  * Returns zero on success; non-zero on error.
1743237fead6SMichael Halcrow  */
1744237fead6SMichael Halcrow int
1745237fead6SMichael Halcrow ecryptfs_generate_key_packet_set(char *dest_base,
1746237fead6SMichael Halcrow 				 struct ecryptfs_crypt_stat *crypt_stat,
1747237fead6SMichael Halcrow 				 struct dentry *ecryptfs_dentry, size_t *len,
1748237fead6SMichael Halcrow 				 size_t max)
1749237fead6SMichael Halcrow {
1750237fead6SMichael Halcrow 	struct ecryptfs_auth_tok *auth_tok;
1751f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *global_auth_tok;
1752237fead6SMichael Halcrow 	struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1753237fead6SMichael Halcrow 		&ecryptfs_superblock_to_private(
1754237fead6SMichael Halcrow 			ecryptfs_dentry->d_sb)->mount_crypt_stat;
1755237fead6SMichael Halcrow 	size_t written;
1756eb95e7ffSMichael Halcrow 	struct ecryptfs_key_record *key_rec;
1757f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *key_sig;
1758dddfa461SMichael Halcrow 	int rc = 0;
1759237fead6SMichael Halcrow 
1760237fead6SMichael Halcrow 	(*len) = 0;
1761f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
1762eb95e7ffSMichael Halcrow 	key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1763eb95e7ffSMichael Halcrow 	if (!key_rec) {
1764eb95e7ffSMichael Halcrow 		rc = -ENOMEM;
1765eb95e7ffSMichael Halcrow 		goto out;
1766eb95e7ffSMichael Halcrow 	}
1767f4aad16aSMichael Halcrow 	list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1768f4aad16aSMichael Halcrow 			    crypt_stat_list) {
1769f4aad16aSMichael Halcrow 		memset(key_rec, 0, sizeof(*key_rec));
1770f4aad16aSMichael Halcrow 		rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1771f4aad16aSMichael Halcrow 							   mount_crypt_stat,
1772f4aad16aSMichael Halcrow 							   key_sig->keysig);
1773f4aad16aSMichael Halcrow 		if (rc) {
1774f4aad16aSMichael Halcrow 			printk(KERN_ERR "Error attempting to get the global "
1775f4aad16aSMichael Halcrow 			       "auth_tok; rc = [%d]\n", rc);
1776f4aad16aSMichael Halcrow 			goto out_free;
1777f4aad16aSMichael Halcrow 		}
1778f4aad16aSMichael Halcrow 		if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1779f4aad16aSMichael Halcrow 			printk(KERN_WARNING
1780f4aad16aSMichael Halcrow 			       "Skipping invalid auth tok with sig = [%s]\n",
1781f4aad16aSMichael Halcrow 			       global_auth_tok->sig);
1782f4aad16aSMichael Halcrow 			continue;
1783f4aad16aSMichael Halcrow 		}
1784f4aad16aSMichael Halcrow 		auth_tok = global_auth_tok->global_auth_tok;
1785237fead6SMichael Halcrow 		if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1786237fead6SMichael Halcrow 			rc = write_tag_3_packet((dest_base + (*len)),
1787f4aad16aSMichael Halcrow 						&max, auth_tok,
1788eb95e7ffSMichael Halcrow 						crypt_stat, key_rec,
1789237fead6SMichael Halcrow 						&written);
1790237fead6SMichael Halcrow 			if (rc) {
1791237fead6SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
1792237fead6SMichael Halcrow 						"writing tag 3 packet\n");
1793eb95e7ffSMichael Halcrow 				goto out_free;
1794237fead6SMichael Halcrow 			}
1795237fead6SMichael Halcrow 			(*len) += written;
1796237fead6SMichael Halcrow 			/* Write auth tok signature packet */
1797f4aad16aSMichael Halcrow 			rc = write_tag_11_packet((dest_base + (*len)), &max,
1798f4aad16aSMichael Halcrow 						 key_rec->sig,
1799f4aad16aSMichael Halcrow 						 ECRYPTFS_SIG_SIZE, &written);
1800237fead6SMichael Halcrow 			if (rc) {
1801237fead6SMichael Halcrow 				ecryptfs_printk(KERN_ERR, "Error writing "
1802237fead6SMichael Halcrow 						"auth tok signature packet\n");
1803eb95e7ffSMichael Halcrow 				goto out_free;
1804237fead6SMichael Halcrow 			}
1805237fead6SMichael Halcrow 			(*len) += written;
1806dddfa461SMichael Halcrow 		} else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1807dddfa461SMichael Halcrow 			rc = write_tag_1_packet(dest_base + (*len),
1808f4aad16aSMichael Halcrow 						&max, auth_tok,
1809f4aad16aSMichael Halcrow 						crypt_stat, key_rec, &written);
1810dddfa461SMichael Halcrow 			if (rc) {
1811dddfa461SMichael Halcrow 				ecryptfs_printk(KERN_WARNING, "Error "
1812dddfa461SMichael Halcrow 						"writing tag 1 packet\n");
1813eb95e7ffSMichael Halcrow 				goto out_free;
1814dddfa461SMichael Halcrow 			}
1815dddfa461SMichael Halcrow 			(*len) += written;
1816237fead6SMichael Halcrow 		} else {
1817237fead6SMichael Halcrow 			ecryptfs_printk(KERN_WARNING, "Unsupported "
1818237fead6SMichael Halcrow 					"authentication token type\n");
1819237fead6SMichael Halcrow 			rc = -EINVAL;
1820eb95e7ffSMichael Halcrow 			goto out_free;
1821237fead6SMichael Halcrow 		}
1822f4aad16aSMichael Halcrow 	}
1823f4aad16aSMichael Halcrow 	if (likely(max > 0)) {
1824237fead6SMichael Halcrow 		dest_base[(*len)] = 0x00;
1825237fead6SMichael Halcrow 	} else {
1826237fead6SMichael Halcrow 		ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1827237fead6SMichael Halcrow 		rc = -EIO;
1828237fead6SMichael Halcrow 	}
1829eb95e7ffSMichael Halcrow out_free:
1830eb95e7ffSMichael Halcrow 	kmem_cache_free(ecryptfs_key_record_cache, key_rec);
1831237fead6SMichael Halcrow out:
1832237fead6SMichael Halcrow 	if (rc)
1833237fead6SMichael Halcrow 		(*len) = 0;
1834f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
1835237fead6SMichael Halcrow 	return rc;
1836237fead6SMichael Halcrow }
1837f4aad16aSMichael Halcrow 
1838f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_key_sig_cache;
1839f4aad16aSMichael Halcrow 
1840f4aad16aSMichael Halcrow int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1841f4aad16aSMichael Halcrow {
1842f4aad16aSMichael Halcrow 	struct ecryptfs_key_sig *new_key_sig;
1843f4aad16aSMichael Halcrow 	int rc = 0;
1844f4aad16aSMichael Halcrow 
1845f4aad16aSMichael Halcrow 	new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1846f4aad16aSMichael Halcrow 	if (!new_key_sig) {
1847f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1848f4aad16aSMichael Halcrow 		printk(KERN_ERR
1849f4aad16aSMichael Halcrow 		       "Error allocating from ecryptfs_key_sig_cache\n");
1850f4aad16aSMichael Halcrow 		goto out;
1851f4aad16aSMichael Halcrow 	}
1852f4aad16aSMichael Halcrow 	memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1853f4aad16aSMichael Halcrow 	mutex_lock(&crypt_stat->keysig_list_mutex);
1854f4aad16aSMichael Halcrow 	list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1855f4aad16aSMichael Halcrow 	mutex_unlock(&crypt_stat->keysig_list_mutex);
1856f4aad16aSMichael Halcrow out:
1857f4aad16aSMichael Halcrow 	return rc;
1858f4aad16aSMichael Halcrow }
1859f4aad16aSMichael Halcrow 
1860f4aad16aSMichael Halcrow struct kmem_cache *ecryptfs_global_auth_tok_cache;
1861f4aad16aSMichael Halcrow 
1862f4aad16aSMichael Halcrow int
1863f4aad16aSMichael Halcrow ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1864f4aad16aSMichael Halcrow 			     char *sig)
1865f4aad16aSMichael Halcrow {
1866f4aad16aSMichael Halcrow 	struct ecryptfs_global_auth_tok *new_auth_tok;
1867f4aad16aSMichael Halcrow 	int rc = 0;
1868f4aad16aSMichael Halcrow 
1869459e2164SEric Sandeen 	new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
1870f4aad16aSMichael Halcrow 					GFP_KERNEL);
1871f4aad16aSMichael Halcrow 	if (!new_auth_tok) {
1872f4aad16aSMichael Halcrow 		rc = -ENOMEM;
1873f4aad16aSMichael Halcrow 		printk(KERN_ERR "Error allocating from "
1874f4aad16aSMichael Halcrow 		       "ecryptfs_global_auth_tok_cache\n");
1875f4aad16aSMichael Halcrow 		goto out;
1876f4aad16aSMichael Halcrow 	}
1877f4aad16aSMichael Halcrow 	memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1878f4aad16aSMichael Halcrow 	new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1879f4aad16aSMichael Halcrow 	mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1880f4aad16aSMichael Halcrow 	list_add(&new_auth_tok->mount_crypt_stat_list,
1881f4aad16aSMichael Halcrow 		 &mount_crypt_stat->global_auth_tok_list);
1882f4aad16aSMichael Halcrow 	mount_crypt_stat->num_global_auth_toks++;
1883f4aad16aSMichael Halcrow 	mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1884f4aad16aSMichael Halcrow out:
1885f4aad16aSMichael Halcrow 	return rc;
1886f4aad16aSMichael Halcrow }
1887f4aad16aSMichael Halcrow 
1888