blob: f5b76a331b9c743a3679ae69022f288c60b0f229 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
5 * file.
6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -080010 * Trevor S. Highland <trevor.highland@gmail.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/string.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070029#include <linux/syscalls.h>
30#include <linux/pagemap.h>
31#include <linux/key.h>
32#include <linux/random.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
37/**
38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and
40 * return an error code.
41 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -070042static int process_request_key_err(long err_code)
Michael Halcrow237fead2006-10-04 02:16:22 -070043{
44 int rc = 0;
45
46 switch (err_code) {
Eric Sandeen982363c2008-07-23 21:30:04 -070047 case -ENOKEY:
Michael Halcrow237fead2006-10-04 02:16:22 -070048 ecryptfs_printk(KERN_WARNING, "No key\n");
49 rc = -ENOENT;
50 break;
Eric Sandeen982363c2008-07-23 21:30:04 -070051 case -EKEYEXPIRED:
Michael Halcrow237fead2006-10-04 02:16:22 -070052 ecryptfs_printk(KERN_WARNING, "Key expired\n");
53 rc = -ETIME;
54 break;
Eric Sandeen982363c2008-07-23 21:30:04 -070055 case -EKEYREVOKED:
Michael Halcrow237fead2006-10-04 02:16:22 -070056 ecryptfs_printk(KERN_WARNING, "Key revoked\n");
57 rc = -EINVAL;
58 break;
59 default:
60 ecryptfs_printk(KERN_WARNING, "Unknown error code: "
61 "[0x%.16x]\n", err_code);
62 rc = -EINVAL;
63 }
64 return rc;
65}
66
Michael Halcrow237fead2006-10-04 02:16:22 -070067/**
Michael Halcrowf66e8832008-04-29 00:59:51 -070068 * ecryptfs_parse_packet_length
Michael Halcrow237fead2006-10-04 02:16:22 -070069 * @data: Pointer to memory containing length at offset
70 * @size: This function writes the decoded size to this memory
71 * address; zero on error
72 * @length_size: The number of bytes occupied by the encoded length
73 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -070074 * Returns zero on success; non-zero on error
Michael Halcrow237fead2006-10-04 02:16:22 -070075 */
Michael Halcrowf66e8832008-04-29 00:59:51 -070076int ecryptfs_parse_packet_length(unsigned char *data, size_t *size,
77 size_t *length_size)
Michael Halcrow237fead2006-10-04 02:16:22 -070078{
79 int rc = 0;
80
81 (*length_size) = 0;
82 (*size) = 0;
83 if (data[0] < 192) {
84 /* One-byte length */
Michael Halcrowdddfa462007-02-12 00:53:44 -080085 (*size) = (unsigned char)data[0];
Michael Halcrow237fead2006-10-04 02:16:22 -070086 (*length_size) = 1;
87 } else if (data[0] < 224) {
88 /* Two-byte length */
Michael Halcrowdddfa462007-02-12 00:53:44 -080089 (*size) = (((unsigned char)(data[0]) - 192) * 256);
90 (*size) += ((unsigned char)(data[1]) + 192);
Michael Halcrow237fead2006-10-04 02:16:22 -070091 (*length_size) = 2;
92 } else if (data[0] == 255) {
93 /* Five-byte length; we're not supposed to see this */
94 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
95 "supported\n");
96 rc = -EINVAL;
97 goto out;
98 } else {
99 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
100 rc = -EINVAL;
101 goto out;
102 }
103out:
104 return rc;
105}
106
107/**
Michael Halcrowf66e8832008-04-29 00:59:51 -0700108 * ecryptfs_write_packet_length
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700109 * @dest: The byte array target into which to write the length. Must
110 * have at least 5 bytes allocated.
Michael Halcrow237fead2006-10-04 02:16:22 -0700111 * @size: The length to write.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700112 * @packet_size_length: The number of bytes used to encode the packet
113 * length is written to this address.
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 *
115 * Returns zero on success; non-zero on error.
116 */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700117int ecryptfs_write_packet_length(char *dest, size_t size,
118 size_t *packet_size_length)
Michael Halcrow237fead2006-10-04 02:16:22 -0700119{
120 int rc = 0;
121
122 if (size < 192) {
123 dest[0] = size;
124 (*packet_size_length) = 1;
125 } else if (size < 65536) {
126 dest[0] = (((size - 192) / 256) + 192);
127 dest[1] = ((size - 192) % 256);
128 (*packet_size_length) = 2;
129 } else {
130 rc = -EINVAL;
131 ecryptfs_printk(KERN_WARNING,
132 "Unsupported packet size: [%d]\n", size);
133 }
134 return rc;
135}
136
Michael Halcrowdddfa462007-02-12 00:53:44 -0800137static int
138write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
139 char **packet, size_t *packet_len)
140{
141 size_t i = 0;
142 size_t data_len;
143 size_t packet_size_len;
144 char *message;
145 int rc;
146
147 /*
148 * ***** TAG 64 Packet Format *****
149 * | Content Type | 1 byte |
150 * | Key Identifier Size | 1 or 2 bytes |
151 * | Key Identifier | arbitrary |
152 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
153 * | Encrypted File Encryption Key | arbitrary |
154 */
155 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
156 + session_key->encrypted_key_size);
157 *packet = kmalloc(data_len, GFP_KERNEL);
158 message = *packet;
159 if (!message) {
160 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
161 rc = -ENOMEM;
162 goto out;
163 }
164 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700165 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800167 if (rc) {
168 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
169 "header; cannot generate packet length\n");
170 goto out;
171 }
172 i += packet_size_len;
173 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
174 i += ECRYPTFS_SIG_SIZE_HEX;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700175 rc = ecryptfs_write_packet_length(&message[i],
176 session_key->encrypted_key_size,
177 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800178 if (rc) {
179 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
180 "header; cannot generate packet length\n");
181 goto out;
182 }
183 i += packet_size_len;
184 memcpy(&message[i], session_key->encrypted_key,
185 session_key->encrypted_key_size);
186 i += session_key->encrypted_key_size;
187 *packet_len = i;
188out:
189 return rc;
190}
191
192static int
Trevor Highland19e66a62008-02-06 01:38:36 -0800193parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code,
Michael Halcrowdddfa462007-02-12 00:53:44 -0800194 struct ecryptfs_message *msg)
195{
196 size_t i = 0;
197 char *data;
198 size_t data_len;
199 size_t m_size;
200 size_t message_len;
201 u16 checksum = 0;
202 u16 expected_checksum = 0;
203 int rc;
204
205 /*
206 * ***** TAG 65 Packet Format *****
207 * | Content Type | 1 byte |
208 * | Status Indicator | 1 byte |
209 * | File Encryption Key Size | 1 or 2 bytes |
210 * | File Encryption Key | arbitrary |
211 */
212 message_len = msg->data_len;
213 data = msg->data;
214 if (message_len < 4) {
215 rc = -EIO;
216 goto out;
217 }
218 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
219 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
220 rc = -EIO;
221 goto out;
222 }
223 if (data[i++]) {
224 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
225 "[%d]\n", data[i-1]);
226 rc = -EIO;
227 goto out;
228 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700229 rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800230 if (rc) {
231 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
232 "rc = [%d]\n", rc);
233 goto out;
234 }
235 i += data_len;
236 if (message_len < (i + m_size)) {
237 ecryptfs_printk(KERN_ERR, "The received netlink message is "
238 "shorter than expected\n");
239 rc = -EIO;
240 goto out;
241 }
242 if (m_size < 3) {
243 ecryptfs_printk(KERN_ERR,
244 "The decrypted key is not long enough to "
245 "include a cipher code and checksum\n");
246 rc = -EIO;
247 goto out;
248 }
249 *cipher_code = data[i++];
250 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
251 session_key->decrypted_key_size = m_size - 3;
252 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
253 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
254 "the maximum key size [%d]\n",
255 session_key->decrypted_key_size,
256 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
257 rc = -EIO;
258 goto out;
259 }
260 memcpy(session_key->decrypted_key, &data[i],
261 session_key->decrypted_key_size);
262 i += session_key->decrypted_key_size;
263 expected_checksum += (unsigned char)(data[i++]) << 8;
264 expected_checksum += (unsigned char)(data[i++]);
265 for (i = 0; i < session_key->decrypted_key_size; i++)
266 checksum += session_key->decrypted_key[i];
267 if (expected_checksum != checksum) {
268 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
269 "encryption key; expected [%x]; calculated "
270 "[%x]\n", expected_checksum, checksum);
271 rc = -EIO;
272 }
273out:
274 return rc;
275}
276
277
278static int
Trevor Highland19e66a62008-02-06 01:38:36 -0800279write_tag_66_packet(char *signature, u8 cipher_code,
Michael Halcrowdddfa462007-02-12 00:53:44 -0800280 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
281 size_t *packet_len)
282{
283 size_t i = 0;
284 size_t j;
285 size_t data_len;
286 size_t checksum = 0;
287 size_t packet_size_len;
288 char *message;
289 int rc;
290
291 /*
292 * ***** TAG 66 Packet Format *****
293 * | Content Type | 1 byte |
294 * | Key Identifier Size | 1 or 2 bytes |
295 * | Key Identifier | arbitrary |
296 * | File Encryption Key Size | 1 or 2 bytes |
297 * | File Encryption Key | arbitrary |
298 */
299 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
300 *packet = kmalloc(data_len, GFP_KERNEL);
301 message = *packet;
302 if (!message) {
303 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
304 rc = -ENOMEM;
305 goto out;
306 }
307 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700308 rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
309 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800310 if (rc) {
311 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
312 "header; cannot generate packet length\n");
313 goto out;
314 }
315 i += packet_size_len;
316 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
317 i += ECRYPTFS_SIG_SIZE_HEX;
318 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
Michael Halcrowf66e8832008-04-29 00:59:51 -0700319 rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3,
320 &packet_size_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800321 if (rc) {
322 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
323 "header; cannot generate packet length\n");
324 goto out;
325 }
326 i += packet_size_len;
327 message[i++] = cipher_code;
328 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
329 i += crypt_stat->key_size;
330 for (j = 0; j < crypt_stat->key_size; j++)
331 checksum += crypt_stat->key[j];
332 message[i++] = (checksum / 256) % 256;
333 message[i++] = (checksum % 256);
334 *packet_len = i;
335out:
336 return rc;
337}
338
339static int
340parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
341 struct ecryptfs_message *msg)
342{
343 size_t i = 0;
344 char *data;
345 size_t data_len;
346 size_t message_len;
347 int rc;
348
349 /*
350 * ***** TAG 65 Packet Format *****
351 * | Content Type | 1 byte |
352 * | Status Indicator | 1 byte |
353 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
354 * | Encrypted File Encryption Key | arbitrary |
355 */
356 message_len = msg->data_len;
357 data = msg->data;
358 /* verify that everything through the encrypted FEK size is present */
359 if (message_len < 4) {
360 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700361 printk(KERN_ERR "%s: message_len is [%Zd]; minimum acceptable "
362 "message length is [%d]\n", __func__, message_len, 4);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800363 goto out;
364 }
365 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800366 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700367 printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n",
368 __func__);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800369 goto out;
370 }
371 if (data[i++]) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800372 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700373 printk(KERN_ERR "%s: Status indicator has non zero "
374 "value [%d]\n", __func__, data[i-1]);
375
Michael Halcrowdddfa462007-02-12 00:53:44 -0800376 goto out;
377 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700378 rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size,
379 &data_len);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800380 if (rc) {
381 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
382 "rc = [%d]\n", rc);
383 goto out;
384 }
385 i += data_len;
386 if (message_len < (i + key_rec->enc_key_size)) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800387 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700388 printk(KERN_ERR "%s: message_len [%Zd]; max len is [%Zd]\n",
389 __func__, message_len, (i + key_rec->enc_key_size));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800390 goto out;
391 }
392 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrowdddfa462007-02-12 00:53:44 -0800393 rc = -EIO;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700394 printk(KERN_ERR "%s: Encrypted key_size [%Zd] larger than "
395 "the maximum key size [%d]\n", __func__,
396 key_rec->enc_key_size,
397 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800398 goto out;
399 }
400 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
401out:
402 return rc;
403}
404
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700405static int
406ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
407{
408 int rc = 0;
409
410 (*sig) = NULL;
411 switch (auth_tok->token_type) {
412 case ECRYPTFS_PASSWORD:
413 (*sig) = auth_tok->token.password.signature;
414 break;
415 case ECRYPTFS_PRIVATE_KEY:
416 (*sig) = auth_tok->token.private_key.signature;
417 break;
418 default:
419 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
420 auth_tok->token_type);
421 rc = -EINVAL;
422 }
423 return rc;
424}
425
Michael Halcrowdddfa462007-02-12 00:53:44 -0800426/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700427 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
428 * @auth_tok: The key authentication token used to decrypt the session key
429 * @crypt_stat: The cryptographic context
Michael Halcrowdddfa462007-02-12 00:53:44 -0800430 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700431 * Returns zero on success; non-zero error otherwise.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800432 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700433static int
434decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
435 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -0800436{
Trevor Highland19e66a62008-02-06 01:38:36 -0800437 u8 cipher_code = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800438 struct ecryptfs_msg_ctx *msg_ctx;
439 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700440 char *auth_tok_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800441 char *netlink_message;
442 size_t netlink_message_length;
443 int rc;
444
Michael Halcrow5dda6992007-10-16 01:28:06 -0700445 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
446 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700447 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
448 auth_tok->token_type);
449 goto out;
450 }
451 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800452 &netlink_message, &netlink_message_length);
453 if (rc) {
Michael Halcrowf66e8832008-04-29 00:59:51 -0700454 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800455 goto out;
456 }
457 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
458 netlink_message_length, &msg_ctx);
459 if (rc) {
460 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
461 goto out;
462 }
463 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
464 if (rc) {
465 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
466 "from the user space daemon\n");
467 rc = -EIO;
468 goto out;
469 }
470 rc = parse_tag_65_packet(&(auth_tok->session_key),
471 &cipher_code, msg);
472 if (rc) {
473 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
474 rc);
475 goto out;
476 }
477 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
478 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
479 auth_tok->session_key.decrypted_key_size);
480 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
481 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
482 if (rc) {
483 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
484 cipher_code)
485 goto out;
486 }
487 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
488 if (ecryptfs_verbosity > 0) {
489 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
490 ecryptfs_dump_hex(crypt_stat->key,
491 crypt_stat->key_size);
492 }
493out:
494 if (msg)
495 kfree(msg);
496 return rc;
497}
498
499static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
500{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800501 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700502 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800503
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700504 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
505 auth_tok_list_head, list) {
506 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800507 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
508 auth_tok_list_item);
509 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800510}
511
512struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
513
Michael Halcrowdddfa462007-02-12 00:53:44 -0800514/**
515 * parse_tag_1_packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700516 * @crypt_stat: The cryptographic context to modify based on packet contents
Michael Halcrowdddfa462007-02-12 00:53:44 -0800517 * @data: The raw bytes of the packet.
518 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700519 * a new authentication token will be placed at the
520 * end of this list for this packet.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800521 * @new_auth_tok: Pointer to a pointer to memory that this function
522 * allocates; sets the memory address of the pointer to
523 * NULL on error. This object is added to the
524 * auth_tok_list.
525 * @packet_size: This function writes the size of the parsed packet
526 * into this memory location; zero on error.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700527 * @max_packet_size: The maximum allowable packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800528 *
529 * Returns zero on success; non-zero on error.
530 */
531static int
532parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
533 unsigned char *data, struct list_head *auth_tok_list,
534 struct ecryptfs_auth_tok **new_auth_tok,
535 size_t *packet_size, size_t max_packet_size)
536{
537 size_t body_size;
538 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
539 size_t length_size;
540 int rc = 0;
541
542 (*packet_size) = 0;
543 (*new_auth_tok) = NULL;
Michael Halcrow132181792007-10-16 01:27:56 -0700544 /**
545 * This format is inspired by OpenPGP; see RFC 2440
546 * packet tag 1
547 *
548 * Tag 1 identifier (1 byte)
549 * Max Tag 1 packet size (max 3 bytes)
550 * Version (1 byte)
551 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
552 * Cipher identifier (1 byte)
553 * Encrypted key size (arbitrary)
554 *
555 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800556 */
Michael Halcrow132181792007-10-16 01:27:56 -0700557 if (unlikely(max_packet_size < 12)) {
558 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800559 rc = -EINVAL;
560 goto out;
561 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800562 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow132181792007-10-16 01:27:56 -0700563 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
564 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800565 rc = -EINVAL;
566 goto out;
567 }
568 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
569 * at end of function upon failure */
570 auth_tok_list_item =
Michael Halcrow132181792007-10-16 01:27:56 -0700571 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
572 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800573 if (!auth_tok_list_item) {
Michael Halcrow132181792007-10-16 01:27:56 -0700574 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800575 rc = -ENOMEM;
576 goto out;
577 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800578 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700579 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
580 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700581 if (rc) {
Michael Halcrow132181792007-10-16 01:27:56 -0700582 printk(KERN_WARNING "Error parsing packet length; "
583 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800584 goto out_free;
585 }
Michael Halcrow132181792007-10-16 01:27:56 -0700586 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700587 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800588 rc = -EINVAL;
589 goto out_free;
590 }
591 (*packet_size) += length_size;
592 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow132181792007-10-16 01:27:56 -0700593 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800594 rc = -EINVAL;
595 goto out_free;
596 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800597 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow132181792007-10-16 01:27:56 -0700598 printk(KERN_WARNING "Unknown version number [%d]\n",
599 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800600 rc = -EINVAL;
601 goto out_free;
602 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800603 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
604 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
605 *packet_size += ECRYPTFS_SIG_SIZE;
606 /* This byte is skipped because the kernel does not need to
607 * know which public key encryption algorithm was used */
608 (*packet_size)++;
609 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow132181792007-10-16 01:27:56 -0700610 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800611 if ((*new_auth_tok)->session_key.encrypted_key_size
612 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow132181792007-10-16 01:27:56 -0700613 printk(KERN_WARNING "Tag 1 packet contains key larger "
614 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800615 rc = -EINVAL;
616 goto out;
617 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800618 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow132181792007-10-16 01:27:56 -0700619 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800620 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
621 (*new_auth_tok)->session_key.flags &=
622 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
623 (*new_auth_tok)->session_key.flags |=
624 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
625 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow132181792007-10-16 01:27:56 -0700626 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800627 (*new_auth_tok)->session_key.flags &=
628 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
629 (*new_auth_tok)->session_key.flags &=
630 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800631 list_add(&auth_tok_list_item->list, auth_tok_list);
632 goto out;
633out_free:
634 (*new_auth_tok) = NULL;
635 memset(auth_tok_list_item, 0,
636 sizeof(struct ecryptfs_auth_tok_list_item));
637 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
638 auth_tok_list_item);
639out:
640 if (rc)
641 (*packet_size) = 0;
642 return rc;
643}
644
Michael Halcrow237fead2006-10-04 02:16:22 -0700645/**
646 * parse_tag_3_packet
647 * @crypt_stat: The cryptographic context to modify based on packet
648 * contents.
649 * @data: The raw bytes of the packet.
650 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
651 * a new authentication token will be placed at the end
652 * of this list for this packet.
653 * @new_auth_tok: Pointer to a pointer to memory that this function
654 * allocates; sets the memory address of the pointer to
655 * NULL on error. This object is added to the
656 * auth_tok_list.
657 * @packet_size: This function writes the size of the parsed packet
658 * into this memory location; zero on error.
659 * @max_packet_size: maximum number of bytes to parse
660 *
661 * Returns zero on success; non-zero on error.
662 */
663static int
664parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
665 unsigned char *data, struct list_head *auth_tok_list,
666 struct ecryptfs_auth_tok **new_auth_tok,
667 size_t *packet_size, size_t max_packet_size)
668{
Michael Halcrow237fead2006-10-04 02:16:22 -0700669 size_t body_size;
670 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
671 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800672 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700673
674 (*packet_size) = 0;
675 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700676 /**
677 *This format is inspired by OpenPGP; see RFC 2440
678 * packet tag 3
679 *
680 * Tag 3 identifier (1 byte)
681 * Max Tag 3 packet size (max 3 bytes)
682 * Version (1 byte)
683 * Cipher code (1 byte)
684 * S2K specifier (1 byte)
685 * Hash identifier (1 byte)
686 * Salt (ECRYPTFS_SALT_SIZE)
687 * Hash iterations (1 byte)
688 * Encrypted key (arbitrary)
689 *
690 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -0700691 */
Michael Halcrowc59becf2007-10-16 01:27:56 -0700692 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
693 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700694 rc = -EINVAL;
695 goto out;
696 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700697 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700698 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
699 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700700 rc = -EINVAL;
701 goto out;
702 }
703 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
704 * at end of function upon failure */
705 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800706 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700707 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700708 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700709 rc = -ENOMEM;
710 goto out;
711 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700712 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowf66e8832008-04-29 00:59:51 -0700713 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
714 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700715 if (rc) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700716 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
717 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700718 goto out_free;
719 }
Michael Halcrowc59becf2007-10-16 01:27:56 -0700720 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700721 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 rc = -EINVAL;
723 goto out_free;
724 }
725 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700726 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700727 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700728 rc = -EINVAL;
729 goto out_free;
730 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700731 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -0700732 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Michael Halcrow237fead2006-10-04 02:16:22 -0700733 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700734 printk(KERN_WARNING "Unknown version number [%d]\n",
735 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -0700736 rc = -EINVAL;
737 goto out_free;
738 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700739 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
740 (u16)data[(*packet_size)]);
741 /* A little extra work to differentiate among the AES key
742 * sizes; see RFC2440 */
743 switch(data[(*packet_size)++]) {
744 case RFC2440_CIPHER_AES_192:
745 crypt_stat->key_size = 24;
746 break;
747 default:
748 crypt_stat->key_size =
749 (*new_auth_tok)->session_key.encrypted_key_size;
750 }
751 ecryptfs_init_crypt_ctx(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700753 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700754 rc = -ENOSYS;
755 goto out_free;
756 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700757 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -0700758 switch (data[(*packet_size)++]) {
759 case 0x01: /* See RFC2440 for these numbers and their mappings */
760 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700761 memcpy((*new_auth_tok)->token.password.salt,
762 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
763 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700764 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700765 (*new_auth_tok)->token.password.hash_iterations =
766 ((u32) 16 + (data[(*packet_size)] & 15))
767 << ((data[(*packet_size)] >> 4) + 6);
768 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700769 /* Friendly reminder:
770 * (*new_auth_tok)->session_key.encrypted_key_size =
771 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -0700772 memcpy((*new_auth_tok)->session_key.encrypted_key,
773 &data[(*packet_size)],
774 (*new_auth_tok)->session_key.encrypted_key_size);
775 (*packet_size) +=
776 (*new_auth_tok)->session_key.encrypted_key_size;
777 (*new_auth_tok)->session_key.flags &=
778 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
779 (*new_auth_tok)->session_key.flags |=
780 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700781 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700782 break;
783 default:
784 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
785 "[%d]\n", data[(*packet_size) - 1]);
786 rc = -ENOSYS;
787 goto out_free;
788 }
789 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
790 /* TODO: Parametarize; we might actually want userspace to
791 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800792 (*new_auth_tok)->session_key.flags &=
793 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
794 (*new_auth_tok)->session_key.flags &=
795 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700796 list_add(&auth_tok_list_item->list, auth_tok_list);
797 goto out;
798out_free:
799 (*new_auth_tok) = NULL;
800 memset(auth_tok_list_item, 0,
801 sizeof(struct ecryptfs_auth_tok_list_item));
802 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
803 auth_tok_list_item);
804out:
805 if (rc)
806 (*packet_size) = 0;
807 return rc;
808}
809
810/**
811 * parse_tag_11_packet
812 * @data: The raw bytes of the packet
813 * @contents: This function writes the data contents of the literal
814 * packet into this memory location
815 * @max_contents_bytes: The maximum number of bytes that this function
816 * is allowed to write into contents
817 * @tag_11_contents_size: This function writes the size of the parsed
818 * contents into this memory location; zero on
819 * error
820 * @packet_size: This function writes the size of the parsed packet
821 * into this memory location; zero on error
822 * @max_packet_size: maximum number of bytes to parse
823 *
824 * Returns zero on success; non-zero on error.
825 */
826static int
827parse_tag_11_packet(unsigned char *data, unsigned char *contents,
828 size_t max_contents_bytes, size_t *tag_11_contents_size,
829 size_t *packet_size, size_t max_packet_size)
830{
Michael Halcrow237fead2006-10-04 02:16:22 -0700831 size_t body_size;
832 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800833 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700834
835 (*packet_size) = 0;
836 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -0700837 /* This format is inspired by OpenPGP; see RFC 2440
838 * packet tag 11
839 *
840 * Tag 11 identifier (1 byte)
841 * Max Tag 11 packet size (max 3 bytes)
842 * Binary format specifier (1 byte)
843 * Filename length (1 byte)
844 * Filename ("_CONSOLE") (8 bytes)
845 * Modification date (4 bytes)
846 * Literal data (arbitrary)
847 *
848 * We need at least 16 bytes of data for the packet to even be
849 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -0700850 */
Michael Halcrowf6481042007-10-16 01:27:57 -0700851 if (max_packet_size < 16) {
852 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700853 rc = -EINVAL;
854 goto out;
855 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700856 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700857 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700858 rc = -EINVAL;
859 goto out;
860 }
Michael Halcrowf66e8832008-04-29 00:59:51 -0700861 rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size,
862 &length_size);
Michael Halcrow5dda6992007-10-16 01:28:06 -0700863 if (rc) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700864 printk(KERN_WARNING "Invalid tag 11 packet format\n");
865 goto out;
866 }
867 if (body_size < 14) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700868 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowf6481042007-10-16 01:27:57 -0700869 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700870 goto out;
871 }
872 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -0700873 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -0700874 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700875 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700876 rc = -EINVAL;
877 goto out;
878 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700879 if (data[(*packet_size)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700880 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700881 rc = -EINVAL;
882 goto out;
883 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700884 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700885 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700886 rc = -EINVAL;
887 goto out;
888 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700889 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -0700890 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
891 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700892out:
893 if (rc) {
894 (*packet_size) = 0;
895 (*tag_11_contents_size) = 0;
896 }
897 return rc;
898}
899
Michael Halcrowf4aad162007-10-16 01:27:53 -0700900static int
901ecryptfs_find_global_auth_tok_for_sig(
902 struct ecryptfs_global_auth_tok **global_auth_tok,
903 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
904{
905 struct ecryptfs_global_auth_tok *walker;
906 int rc = 0;
907
908 (*global_auth_tok) = NULL;
909 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
910 list_for_each_entry(walker,
911 &mount_crypt_stat->global_auth_tok_list,
912 mount_crypt_stat_list) {
913 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
914 (*global_auth_tok) = walker;
915 goto out;
916 }
917 }
918 rc = -EINVAL;
919out:
920 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
921 return rc;
922}
923
Michael Halcrow237fead2006-10-04 02:16:22 -0700924/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700925 * ecryptfs_verify_version
926 * @version: The version number to confirm
927 *
928 * Returns zero on good version; non-zero otherwise
929 */
930static int ecryptfs_verify_version(u16 version)
931{
932 int rc = 0;
933 unsigned char major;
934 unsigned char minor;
935
936 major = ((version >> 8) & 0xFF);
937 minor = (version & 0xFF);
938 if (major != ECRYPTFS_VERSION_MAJOR) {
939 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
940 "Expected [%d]; got [%d]\n",
941 ECRYPTFS_VERSION_MAJOR, major);
942 rc = -EINVAL;
943 goto out;
944 }
945 if (minor != ECRYPTFS_VERSION_MINOR) {
946 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
947 "Expected [%d]; got [%d]\n",
948 ECRYPTFS_VERSION_MINOR, minor);
949 rc = -EINVAL;
950 goto out;
951 }
952out:
953 return rc;
954}
955
956int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
957 struct ecryptfs_auth_tok **auth_tok,
958 char *sig)
959{
960 int rc = 0;
961
962 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
963 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
964 printk(KERN_ERR "Could not find key with description: [%s]\n",
965 sig);
Eric Sandeen982363c2008-07-23 21:30:04 -0700966 rc = process_request_key_err(PTR_ERR(*auth_tok_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -0700967 goto out;
968 }
969 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
970 if (ecryptfs_verify_version((*auth_tok)->version)) {
971 printk(KERN_ERR
972 "Data structure version mismatch. "
973 "Userspace tools must match eCryptfs "
974 "kernel module with major version [%d] "
975 "and minor version [%d]\n",
976 ECRYPTFS_VERSION_MAJOR,
977 ECRYPTFS_VERSION_MINOR);
978 rc = -EINVAL;
979 goto out;
980 }
981 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
982 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
983 printk(KERN_ERR "Invalid auth_tok structure "
984 "returned from key query\n");
985 rc = -EINVAL;
986 goto out;
987 }
988out:
989 return rc;
990}
991
992/**
993 * ecryptfs_find_auth_tok_for_sig
994 * @auth_tok: Set to the matching auth_tok; NULL if not found
995 * @crypt_stat: inode crypt_stat crypto context
996 * @sig: Sig of auth_tok to find
997 *
998 * For now, this function simply looks at the registered auth_tok's
999 * linked off the mount_crypt_stat, so all the auth_toks that can be
1000 * used must be registered at mount time. This function could
1001 * potentially try a lot harder to find auth_tok's (e.g., by calling
1002 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
1003 * that static registration of auth_tok's will no longer be necessary.
1004 *
1005 * Returns zero on no error; non-zero on error
1006 */
1007static int
1008ecryptfs_find_auth_tok_for_sig(
1009 struct ecryptfs_auth_tok **auth_tok,
1010 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1011{
1012 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1013 crypt_stat->mount_crypt_stat;
1014 struct ecryptfs_global_auth_tok *global_auth_tok;
1015 int rc = 0;
1016
1017 (*auth_tok) = NULL;
1018 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1019 mount_crypt_stat, sig)) {
1020 struct key *auth_tok_key;
1021
1022 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1023 sig);
1024 } else
1025 (*auth_tok) = global_auth_tok->global_auth_tok;
1026 return rc;
1027}
1028
1029/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001030 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1031 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1032 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001033 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001034 * Returns zero on success; non-zero error otherwise
Michael Halcrow237fead2006-10-04 02:16:22 -07001035 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001036static int
1037decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1038 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001039{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001040 struct scatterlist dst_sg;
1041 struct scatterlist src_sg;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001042 struct mutex *tfm_mutex;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001043 struct blkcipher_desc desc = {
1044 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1045 };
1046 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001047
Jens Axboe60c74f82007-10-22 19:43:30 +02001048 sg_init_table(&dst_sg, 1);
1049 sg_init_table(&src_sg, 1);
1050
Michael Halcrowf4aad162007-10-16 01:27:53 -07001051 if (unlikely(ecryptfs_verbosity > 0)) {
1052 ecryptfs_printk(
1053 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1054 auth_tok->token.password.session_key_encryption_key_bytes);
1055 ecryptfs_dump_hex(
1056 auth_tok->token.password.session_key_encryption_key,
1057 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001058 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001059 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1060 crypt_stat->cipher);
1061 if (unlikely(rc)) {
1062 printk(KERN_ERR "Internal error whilst attempting to get "
1063 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1064 crypt_stat->cipher, rc);
1065 goto out;
1066 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001067 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1068 auth_tok->session_key.encrypted_key_size,
1069 &src_sg, 1);
1070 if (rc != 1) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001071 printk(KERN_ERR "Internal error whilst attempting to convert "
1072 "auth_tok->session_key.encrypted_key to scatterlist; "
1073 "expected rc = 1; got rc = [%d]. "
1074 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1075 auth_tok->session_key.encrypted_key_size);
1076 goto out;
1077 }
1078 auth_tok->session_key.decrypted_key_size =
1079 auth_tok->session_key.encrypted_key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001080 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1081 auth_tok->session_key.decrypted_key_size,
1082 &dst_sg, 1);
1083 if (rc != 1) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001084 printk(KERN_ERR "Internal error whilst attempting to convert "
1085 "auth_tok->session_key.decrypted_key to scatterlist; "
1086 "expected rc = 1; got rc = [%d]\n", rc);
1087 goto out;
1088 }
1089 mutex_lock(tfm_mutex);
1090 rc = crypto_blkcipher_setkey(
1091 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1092 crypt_stat->key_size);
1093 if (unlikely(rc < 0)) {
1094 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001095 printk(KERN_ERR "Error setting key for crypto context\n");
1096 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001097 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001098 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001099 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001100 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001101 mutex_unlock(tfm_mutex);
1102 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001103 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001104 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001105 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001106 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1107 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1108 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001109 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001110 if (unlikely(ecryptfs_verbosity > 0)) {
1111 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1112 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001113 ecryptfs_dump_hex(crypt_stat->key,
1114 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001115 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001116out:
1117 return rc;
1118}
1119
1120/**
1121 * ecryptfs_parse_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001122 * @crypt_stat: The cryptographic context
1123 * @src: Virtual address of region of memory containing the packets
1124 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
Michael Halcrow237fead2006-10-04 02:16:22 -07001125 *
1126 * Get crypt_stat to have the file's session key if the requisite key
1127 * is available to decrypt the session key.
1128 *
1129 * Returns Zero if a valid authentication token was retrieved and
1130 * processed; negative value for file not encrypted or for error
1131 * conditions.
1132 */
1133int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1134 unsigned char *src,
1135 struct dentry *ecryptfs_dentry)
1136{
1137 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001138 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001139 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001140 struct list_head auth_tok_list;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001141 struct ecryptfs_auth_tok *matching_auth_tok;
1142 struct ecryptfs_auth_tok *candidate_auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001143 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001144 size_t packet_size;
1145 struct ecryptfs_auth_tok *new_auth_tok;
1146 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001147 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001148 size_t tag_11_contents_size;
1149 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001150 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001151
1152 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001153 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001154 * added the our &auth_tok_list */
1155 next_packet_is_auth_tok_packet = 1;
1156 while (next_packet_is_auth_tok_packet) {
1157 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1158
1159 switch (src[i]) {
1160 case ECRYPTFS_TAG_3_PACKET_TYPE:
1161 rc = parse_tag_3_packet(crypt_stat,
1162 (unsigned char *)&src[i],
1163 &auth_tok_list, &new_auth_tok,
1164 &packet_size, max_packet_size);
1165 if (rc) {
1166 ecryptfs_printk(KERN_ERR, "Error parsing "
1167 "tag 3 packet\n");
1168 rc = -EIO;
1169 goto out_wipe_list;
1170 }
1171 i += packet_size;
1172 rc = parse_tag_11_packet((unsigned char *)&src[i],
1173 sig_tmp_space,
1174 ECRYPTFS_SIG_SIZE,
1175 &tag_11_contents_size,
1176 &tag_11_packet_size,
1177 max_packet_size);
1178 if (rc) {
1179 ecryptfs_printk(KERN_ERR, "No valid "
1180 "(ecryptfs-specific) literal "
1181 "packet containing "
1182 "authentication token "
1183 "signature found after "
1184 "tag 3 packet\n");
1185 rc = -EIO;
1186 goto out_wipe_list;
1187 }
1188 i += tag_11_packet_size;
1189 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1190 ecryptfs_printk(KERN_ERR, "Expected "
1191 "signature of size [%d]; "
1192 "read size [%d]\n",
1193 ECRYPTFS_SIG_SIZE,
1194 tag_11_contents_size);
1195 rc = -EIO;
1196 goto out_wipe_list;
1197 }
1198 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1199 sig_tmp_space, tag_11_contents_size);
1200 new_auth_tok->token.password.signature[
1201 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001202 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001203 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001204 case ECRYPTFS_TAG_1_PACKET_TYPE:
1205 rc = parse_tag_1_packet(crypt_stat,
1206 (unsigned char *)&src[i],
1207 &auth_tok_list, &new_auth_tok,
1208 &packet_size, max_packet_size);
1209 if (rc) {
1210 ecryptfs_printk(KERN_ERR, "Error parsing "
1211 "tag 1 packet\n");
1212 rc = -EIO;
1213 goto out_wipe_list;
1214 }
1215 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001216 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001217 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001218 case ECRYPTFS_TAG_11_PACKET_TYPE:
1219 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1220 "(Tag 11 not allowed by itself)\n");
1221 rc = -EIO;
1222 goto out_wipe_list;
1223 break;
1224 default:
1225 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1226 "[%d] of the file header; hex value of "
1227 "character is [0x%.2x]\n", i, src[i]);
1228 next_packet_is_auth_tok_packet = 0;
1229 }
1230 }
1231 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001232 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1233 "eCryptfs file; this is not supported in this version "
1234 "of the eCryptfs kernel module\n");
1235 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001236 goto out;
1237 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001238 /* auth_tok_list contains the set of authentication tokens
1239 * parsed from the metadata. We need to find a matching
1240 * authentication token that has the secret component(s)
1241 * necessary to decrypt the EFEK in the auth_tok parsed from
1242 * the metadata. There may be several potential matches, but
1243 * just one will be sufficient to decrypt to get the FEK. */
1244find_next_matching_auth_tok:
1245 found_auth_tok = 0;
1246 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001247 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1248 if (unlikely(ecryptfs_verbosity > 0)) {
1249 ecryptfs_printk(KERN_DEBUG,
1250 "Considering cadidate auth tok:\n");
1251 ecryptfs_dump_auth_tok(candidate_auth_tok);
1252 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001253 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1254 candidate_auth_tok);
1255 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001256 printk(KERN_ERR
1257 "Unrecognized candidate auth tok type: [%d]\n",
1258 candidate_auth_tok->token_type);
1259 rc = -EINVAL;
1260 goto out_wipe_list;
1261 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001262 ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat,
1263 candidate_auth_tok_sig);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001264 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001265 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001266 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001267 }
1268 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001269 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001270 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1271 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001272 rc = -EIO;
1273 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001274 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001275found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001276 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001277 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001278 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001279 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001280 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001281 crypt_stat);
1282 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001283 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001284 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001285 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001286 rc = decrypt_passphrase_encrypted_session_key(
1287 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001288 }
1289 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001290 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1291
1292 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1293 "session key for authentication token with sig "
1294 "[%.*s]; rc = [%d]. Removing auth tok "
1295 "candidate from the list and searching for "
1296 "the next match.\n", candidate_auth_tok_sig,
1297 ECRYPTFS_SIG_SIZE_HEX, rc);
1298 list_for_each_entry_safe(auth_tok_list_item,
1299 auth_tok_list_item_tmp,
1300 &auth_tok_list, list) {
1301 if (candidate_auth_tok
1302 == &auth_tok_list_item->auth_tok) {
1303 list_del(&auth_tok_list_item->list);
1304 kmem_cache_free(
1305 ecryptfs_auth_tok_list_item_cache,
1306 auth_tok_list_item);
1307 goto find_next_matching_auth_tok;
1308 }
1309 }
1310 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001311 }
1312 rc = ecryptfs_compute_root_iv(crypt_stat);
1313 if (rc) {
1314 ecryptfs_printk(KERN_ERR, "Error computing "
1315 "the root IV\n");
1316 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001317 }
1318 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1319 if (rc) {
1320 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1321 "context for cipher [%s]; rc = [%d]\n",
1322 crypt_stat->cipher, rc);
1323 }
1324out_wipe_list:
1325 wipe_auth_tok_list(&auth_tok_list);
1326out:
1327 return rc;
1328}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001329
Michael Halcrowdddfa462007-02-12 00:53:44 -08001330static int
1331pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1332 struct ecryptfs_crypt_stat *crypt_stat,
1333 struct ecryptfs_key_record *key_rec)
1334{
1335 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1336 char *netlink_payload;
1337 size_t netlink_payload_length;
1338 struct ecryptfs_message *msg;
1339 int rc;
1340
1341 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1342 ecryptfs_code_for_cipher_string(crypt_stat),
1343 crypt_stat, &netlink_payload,
1344 &netlink_payload_length);
1345 if (rc) {
1346 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1347 goto out;
1348 }
1349 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1350 netlink_payload_length, &msg_ctx);
1351 if (rc) {
1352 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1353 goto out;
1354 }
1355 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1356 if (rc) {
1357 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1358 "from the user space daemon\n");
1359 rc = -EIO;
1360 goto out;
1361 }
1362 rc = parse_tag_67_packet(key_rec, msg);
1363 if (rc)
1364 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1365 kfree(msg);
1366out:
1367 if (netlink_payload)
1368 kfree(netlink_payload);
1369 return rc;
1370}
1371/**
1372 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1373 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001374 * @remaining_bytes: Maximum number of bytes that can be writtn
1375 * @auth_tok: The authentication token used for generating the tag 1 packet
1376 * @crypt_stat: The cryptographic context
1377 * @key_rec: The key record struct for the tag 1 packet
Michael Halcrowdddfa462007-02-12 00:53:44 -08001378 * @packet_size: This function will write the number of bytes that end
1379 * up constituting the packet; set to zero on error
1380 *
1381 * Returns zero on success; non-zero on error.
1382 */
1383static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001384write_tag_1_packet(char *dest, size_t *remaining_bytes,
1385 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001386 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001387 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1388{
1389 size_t i;
1390 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001391 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001392 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001393 int rc = 0;
1394
1395 (*packet_size) = 0;
1396 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1397 ECRYPTFS_SIG_SIZE);
1398 encrypted_session_key_valid = 0;
1399 for (i = 0; i < crypt_stat->key_size; i++)
1400 encrypted_session_key_valid |=
1401 auth_tok->session_key.encrypted_key[i];
1402 if (encrypted_session_key_valid) {
1403 memcpy(key_rec->enc_key,
1404 auth_tok->session_key.encrypted_key,
1405 auth_tok->session_key.encrypted_key_size);
1406 goto encrypted_session_key_set;
1407 }
1408 if (auth_tok->session_key.encrypted_key_size == 0)
1409 auth_tok->session_key.encrypted_key_size =
1410 auth_tok->token.private_key.key_size;
1411 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1412 if (rc) {
Michael Halcrowf66e8832008-04-29 00:59:51 -07001413 printk(KERN_ERR "Failed to encrypt session key via a key "
1414 "module; rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001415 goto out;
1416 }
1417 if (ecryptfs_verbosity > 0) {
1418 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1419 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1420 }
1421encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001422 /* This format is inspired by OpenPGP; see RFC 2440
1423 * packet tag 1 */
1424 max_packet_size = (1 /* Tag 1 identifier */
1425 + 3 /* Max Tag 1 packet size */
1426 + 1 /* Version */
1427 + ECRYPTFS_SIG_SIZE /* Key identifier */
1428 + 1 /* Cipher identifier */
1429 + key_rec->enc_key_size); /* Encrypted key size */
1430 if (max_packet_size > (*remaining_bytes)) {
1431 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001432 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001433 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001434 rc = -EINVAL;
1435 goto out;
1436 }
1437 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -07001438 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1439 (max_packet_size - 4),
1440 &packet_size_length);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001441 if (rc) {
1442 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1443 "header; cannot generate packet length\n");
1444 goto out;
1445 }
1446 (*packet_size) += packet_size_length;
1447 dest[(*packet_size)++] = 0x03; /* version 3 */
1448 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1449 (*packet_size) += ECRYPTFS_SIG_SIZE;
1450 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1451 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1452 key_rec->enc_key_size);
1453 (*packet_size) += key_rec->enc_key_size;
1454out:
1455 if (rc)
1456 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001457 else
1458 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001459 return rc;
1460}
Michael Halcrow237fead2006-10-04 02:16:22 -07001461
1462/**
1463 * write_tag_11_packet
1464 * @dest: Target into which Tag 11 packet is to be written
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001465 * @remaining_bytes: Maximum packet length
Michael Halcrow237fead2006-10-04 02:16:22 -07001466 * @contents: Byte array of contents to copy in
1467 * @contents_length: Number of bytes in contents
1468 * @packet_length: Length of the Tag 11 packet written; zero on error
1469 *
1470 * Returns zero on success; non-zero on error.
1471 */
1472static int
Andrew Morton81acbcd2007-10-16 01:27:59 -07001473write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
Michael Halcrow146a4602007-10-16 01:27:58 -07001474 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07001475{
Michael Halcrow237fead2006-10-04 02:16:22 -07001476 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001477 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001478 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001479
1480 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001481 /* This format is inspired by OpenPGP; see RFC 2440
1482 * packet tag 11 */
1483 max_packet_size = (1 /* Tag 11 identifier */
1484 + 3 /* Max Tag 11 packet size */
1485 + 1 /* Binary format specifier */
1486 + 1 /* Filename length */
1487 + 8 /* Filename ("_CONSOLE") */
1488 + 4 /* Modification date */
1489 + contents_length); /* Literal data */
1490 if (max_packet_size > (*remaining_bytes)) {
1491 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001492 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrow146a4602007-10-16 01:27:58 -07001493 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07001494 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001495 goto out;
1496 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001497 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrowf66e8832008-04-29 00:59:51 -07001498 rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
1499 (max_packet_size - 4),
1500 &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001501 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07001502 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1503 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001504 goto out;
1505 }
1506 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001507 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07001508 dest[(*packet_length)++] = 8;
1509 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1510 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07001511 memset(&dest[(*packet_length)], 0x00, 4);
1512 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07001513 memcpy(&dest[(*packet_length)], contents, contents_length);
1514 (*packet_length) += contents_length;
1515 out:
1516 if (rc)
1517 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001518 else
1519 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001520 return rc;
1521}
1522
1523/**
1524 * write_tag_3_packet
1525 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001526 * @remaining_bytes: Maximum number of bytes that can be written
Michael Halcrow237fead2006-10-04 02:16:22 -07001527 * @auth_tok: Authentication token
1528 * @crypt_stat: The cryptographic context
1529 * @key_rec: encrypted key
1530 * @packet_size: This function will write the number of bytes that end
1531 * up constituting the packet; set to zero on error
1532 *
1533 * Returns zero on success; non-zero on error.
1534 */
1535static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001536write_tag_3_packet(char *dest, size_t *remaining_bytes,
1537 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001538 struct ecryptfs_crypt_stat *crypt_stat,
1539 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1540{
Michael Halcrow237fead2006-10-04 02:16:22 -07001541 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001542 size_t encrypted_session_key_valid = 0;
1543 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001544 struct scatterlist dst_sg;
1545 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001546 struct mutex *tfm_mutex = NULL;
Trevor Highland19e66a62008-02-06 01:38:36 -08001547 u8 cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001548 size_t packet_size_length;
1549 size_t max_packet_size;
1550 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1551 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001552 struct blkcipher_desc desc = {
1553 .tfm = NULL,
1554 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1555 };
1556 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001557
1558 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001559 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001560 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001561 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1562 crypt_stat->cipher);
1563 if (unlikely(rc)) {
1564 printk(KERN_ERR "Internal error whilst attempting to get "
1565 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1566 crypt_stat->cipher, rc);
1567 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001568 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001569 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1570 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1571
1572 printk(KERN_WARNING "No key size specified at mount; "
1573 "defaulting to [%d]\n", alg->max_keysize);
1574 mount_crypt_stat->global_default_cipher_key_size =
1575 alg->max_keysize;
1576 }
1577 if (crypt_stat->key_size == 0)
1578 crypt_stat->key_size =
1579 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001580 if (auth_tok->session_key.encrypted_key_size == 0)
1581 auth_tok->session_key.encrypted_key_size =
1582 crypt_stat->key_size;
1583 if (crypt_stat->key_size == 24
1584 && strcmp("aes", crypt_stat->cipher) == 0) {
1585 memset((crypt_stat->key + 24), 0, 8);
1586 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001587 } else
1588 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001589 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001590 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001591 encrypted_session_key_valid = 0;
1592 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1593 encrypted_session_key_valid |=
1594 auth_tok->session_key.encrypted_key[i];
1595 if (encrypted_session_key_valid) {
1596 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1597 "using auth_tok->session_key.encrypted_key, "
1598 "where key_rec->enc_key_size = [%d]\n",
1599 key_rec->enc_key_size);
1600 memcpy(key_rec->enc_key,
1601 auth_tok->session_key.encrypted_key,
1602 key_rec->enc_key_size);
1603 goto encrypted_session_key_set;
1604 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001605 if (auth_tok->token.password.flags &
1606 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001607 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1608 "session key encryption key of size [%d]\n",
1609 auth_tok->token.password.
1610 session_key_encryption_key_bytes);
1611 memcpy(session_key_encryption_key,
1612 auth_tok->token.password.session_key_encryption_key,
1613 crypt_stat->key_size);
1614 ecryptfs_printk(KERN_DEBUG,
1615 "Cached session key " "encryption key: \n");
1616 if (ecryptfs_verbosity > 0)
1617 ecryptfs_dump_hex(session_key_encryption_key, 16);
1618 }
1619 if (unlikely(ecryptfs_verbosity > 0)) {
1620 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1621 ecryptfs_dump_hex(session_key_encryption_key, 16);
1622 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001623 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
1624 &src_sg, 1);
1625 if (rc != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001626 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001627 "for crypt_stat session key; expected rc = 1; "
1628 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1629 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001630 rc = -ENOMEM;
1631 goto out;
1632 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001633 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
1634 &dst_sg, 1);
1635 if (rc != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001636 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001637 "for crypt_stat encrypted session key; "
1638 "expected rc = 1; got rc = [%d]. "
1639 "key_rec->enc_key_size = [%d]\n", rc,
1640 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001641 rc = -ENOMEM;
1642 goto out;
1643 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001644 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001645 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1646 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001647 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001648 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001649 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001650 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001651 goto out;
1652 }
1653 rc = 0;
1654 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1655 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001656 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001657 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001658 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001659 if (rc) {
1660 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1661 goto out;
1662 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001663 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001664 if (ecryptfs_verbosity > 0) {
1665 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1666 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001667 ecryptfs_dump_hex(key_rec->enc_key,
1668 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001669 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001670encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001671 /* This format is inspired by OpenPGP; see RFC 2440
1672 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001673 max_packet_size = (1 /* Tag 3 identifier */
1674 + 3 /* Max Tag 3 packet size */
1675 + 1 /* Version */
1676 + 1 /* Cipher code */
1677 + 1 /* S2K specifier */
1678 + 1 /* Hash identifier */
1679 + ECRYPTFS_SALT_SIZE /* Salt */
1680 + 1 /* Hash iterations */
1681 + key_rec->enc_key_size); /* Encrypted key size */
1682 if (max_packet_size > (*remaining_bytes)) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001683 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
1684 "there are only [%td] available\n", max_packet_size,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001685 (*remaining_bytes));
1686 rc = -EINVAL;
1687 goto out;
1688 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001689 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001690 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1691 * to get the number of octets in the actual Tag 3 packet */
Michael Halcrowf66e8832008-04-29 00:59:51 -07001692 rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
1693 (max_packet_size - 4),
1694 &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001695 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001696 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1697 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001698 goto out;
1699 }
1700 (*packet_size) += packet_size_length;
1701 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001702 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1703 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001704 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1705 if (cipher_code == 0) {
1706 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1707 "cipher [%s]\n", crypt_stat->cipher);
1708 rc = -EINVAL;
1709 goto out;
1710 }
1711 dest[(*packet_size)++] = cipher_code;
1712 dest[(*packet_size)++] = 0x03; /* S2K */
1713 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1714 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1715 ECRYPTFS_SALT_SIZE);
1716 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1717 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001718 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1719 key_rec->enc_key_size);
1720 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001721out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001722 if (rc)
1723 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001724 else
1725 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001726 return rc;
1727}
1728
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001729struct kmem_cache *ecryptfs_key_record_cache;
1730
Michael Halcrow237fead2006-10-04 02:16:22 -07001731/**
1732 * ecryptfs_generate_key_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001733 * @dest_base: Virtual address from which to write the key record set
Michael Halcrow237fead2006-10-04 02:16:22 -07001734 * @crypt_stat: The cryptographic context from which the
1735 * authentication tokens will be retrieved
1736 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1737 * for the global parameters
1738 * @len: The amount written
1739 * @max: The maximum amount of data allowed to be written
1740 *
1741 * Generates a key packet set and writes it to the virtual address
1742 * passed in.
1743 *
1744 * Returns zero on success; non-zero on error.
1745 */
1746int
1747ecryptfs_generate_key_packet_set(char *dest_base,
1748 struct ecryptfs_crypt_stat *crypt_stat,
1749 struct dentry *ecryptfs_dentry, size_t *len,
1750 size_t max)
1751{
Michael Halcrow237fead2006-10-04 02:16:22 -07001752 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001753 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001754 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1755 &ecryptfs_superblock_to_private(
1756 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1757 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001758 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001759 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001760 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001761
1762 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001763 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001764 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1765 if (!key_rec) {
1766 rc = -ENOMEM;
1767 goto out;
1768 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001769 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1770 crypt_stat_list) {
1771 memset(key_rec, 0, sizeof(*key_rec));
1772 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1773 mount_crypt_stat,
1774 key_sig->keysig);
1775 if (rc) {
1776 printk(KERN_ERR "Error attempting to get the global "
1777 "auth_tok; rc = [%d]\n", rc);
1778 goto out_free;
1779 }
1780 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1781 printk(KERN_WARNING
1782 "Skipping invalid auth tok with sig = [%s]\n",
1783 global_auth_tok->sig);
1784 continue;
1785 }
1786 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001787 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1788 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001789 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001790 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001791 &written);
1792 if (rc) {
1793 ecryptfs_printk(KERN_WARNING, "Error "
1794 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001795 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001796 }
1797 (*len) += written;
1798 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001799 rc = write_tag_11_packet((dest_base + (*len)), &max,
1800 key_rec->sig,
1801 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001802 if (rc) {
1803 ecryptfs_printk(KERN_ERR, "Error writing "
1804 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001805 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001806 }
1807 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001808 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1809 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001810 &max, auth_tok,
1811 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001812 if (rc) {
1813 ecryptfs_printk(KERN_WARNING, "Error "
1814 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001815 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001816 }
1817 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001818 } else {
1819 ecryptfs_printk(KERN_WARNING, "Unsupported "
1820 "authentication token type\n");
1821 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001822 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001823 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001824 }
1825 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001826 dest_base[(*len)] = 0x00;
1827 } else {
1828 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1829 rc = -EIO;
1830 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001831out_free:
1832 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001833out:
1834 if (rc)
1835 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001836 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001837 return rc;
1838}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001839
1840struct kmem_cache *ecryptfs_key_sig_cache;
1841
1842int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1843{
1844 struct ecryptfs_key_sig *new_key_sig;
1845 int rc = 0;
1846
1847 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1848 if (!new_key_sig) {
1849 rc = -ENOMEM;
1850 printk(KERN_ERR
1851 "Error allocating from ecryptfs_key_sig_cache\n");
1852 goto out;
1853 }
1854 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1855 mutex_lock(&crypt_stat->keysig_list_mutex);
1856 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1857 mutex_unlock(&crypt_stat->keysig_list_mutex);
1858out:
1859 return rc;
1860}
1861
1862struct kmem_cache *ecryptfs_global_auth_tok_cache;
1863
1864int
1865ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1866 char *sig)
1867{
1868 struct ecryptfs_global_auth_tok *new_auth_tok;
1869 int rc = 0;
1870
Eric Sandeen459e2162007-12-17 16:19:52 -08001871 new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001872 GFP_KERNEL);
1873 if (!new_auth_tok) {
1874 rc = -ENOMEM;
1875 printk(KERN_ERR "Error allocating from "
1876 "ecryptfs_global_auth_tok_cache\n");
1877 goto out;
1878 }
1879 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1880 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1881 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1882 list_add(&new_auth_tok->mount_crypt_stat_list,
1883 &mount_crypt_stat->global_auth_tok_list);
1884 mount_crypt_stat->num_global_auth_toks++;
1885 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1886out:
1887 return rc;
1888}
1889