Linux 2.6.19.7
[linux-2.6/suspend2-2.6.19.git] / fs / ecryptfs / keystore.c
blobc3746f56d1627ab2246d5086731103353f9537d3
1 /**
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.
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>
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of the
14 * License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24 * 02111-1307, USA.
27 #include <linux/string.h>
28 #include <linux/sched.h>
29 #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"
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.
42 int process_request_key_err(long err_code)
44 int rc = 0;
46 switch (err_code) {
47 case ENOKEY:
48 ecryptfs_printk(KERN_WARNING, "No key\n");
49 rc = -ENOENT;
50 break;
51 case EKEYEXPIRED:
52 ecryptfs_printk(KERN_WARNING, "Key expired\n");
53 rc = -ETIME;
54 break;
55 case EKEYREVOKED:
56 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;
64 return rc;
67 static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
69 struct list_head *walker;
70 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
72 walker = auth_tok_list_head->next;
73 while (walker != auth_tok_list_head) {
74 auth_tok_list_item =
75 list_entry(walker, struct ecryptfs_auth_tok_list_item,
76 list);
77 walker = auth_tok_list_item->list.next;
78 memset(auth_tok_list_item, 0,
79 sizeof(struct ecryptfs_auth_tok_list_item));
80 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
81 auth_tok_list_item);
85 struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
87 /**
88 * parse_packet_length
89 * @data: Pointer to memory containing length at offset
90 * @size: This function writes the decoded size to this memory
91 * address; zero on error
92 * @length_size: The number of bytes occupied by the encoded length
94 * Returns Zero on success
96 static int parse_packet_length(unsigned char *data, size_t *size,
97 size_t *length_size)
99 int rc = 0;
101 (*length_size) = 0;
102 (*size) = 0;
103 if (data[0] < 192) {
104 /* One-byte length */
105 (*size) = data[0];
106 (*length_size) = 1;
107 } else if (data[0] < 224) {
108 /* Two-byte length */
109 (*size) = ((data[0] - 192) * 256);
110 (*size) += (data[1] + 192);
111 (*length_size) = 2;
112 } else if (data[0] == 255) {
113 /* Five-byte length; we're not supposed to see this */
114 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
115 "supported\n");
116 rc = -EINVAL;
117 goto out;
118 } else {
119 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
120 rc = -EINVAL;
121 goto out;
123 out:
124 return rc;
128 * write_packet_length
129 * @dest: The byte array target into which to write the
130 * length. Must have at least 5 bytes allocated.
131 * @size: The length to write.
132 * @packet_size_length: The number of bytes used to encode the
133 * packet length is written to this address.
135 * Returns zero on success; non-zero on error.
137 static int write_packet_length(char *dest, size_t size,
138 size_t *packet_size_length)
140 int rc = 0;
142 if (size < 192) {
143 dest[0] = size;
144 (*packet_size_length) = 1;
145 } else if (size < 65536) {
146 dest[0] = (((size - 192) / 256) + 192);
147 dest[1] = ((size - 192) % 256);
148 (*packet_size_length) = 2;
149 } else {
150 rc = -EINVAL;
151 ecryptfs_printk(KERN_WARNING,
152 "Unsupported packet size: [%d]\n", size);
154 return rc;
158 * parse_tag_3_packet
159 * @crypt_stat: The cryptographic context to modify based on packet
160 * contents.
161 * @data: The raw bytes of the packet.
162 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
163 * a new authentication token will be placed at the end
164 * of this list for this packet.
165 * @new_auth_tok: Pointer to a pointer to memory that this function
166 * allocates; sets the memory address of the pointer to
167 * NULL on error. This object is added to the
168 * auth_tok_list.
169 * @packet_size: This function writes the size of the parsed packet
170 * into this memory location; zero on error.
171 * @max_packet_size: maximum number of bytes to parse
173 * Returns zero on success; non-zero on error.
175 static int
176 parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
177 unsigned char *data, struct list_head *auth_tok_list,
178 struct ecryptfs_auth_tok **new_auth_tok,
179 size_t *packet_size, size_t max_packet_size)
181 int rc = 0;
182 size_t body_size;
183 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
184 size_t length_size;
186 (*packet_size) = 0;
187 (*new_auth_tok) = NULL;
189 /* we check that:
190 * one byte for the Tag 3 ID flag
191 * two bytes for the body size
192 * do not exceed the maximum_packet_size
194 if (unlikely((*packet_size) + 3 > max_packet_size)) {
195 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
196 rc = -EINVAL;
197 goto out;
200 /* check for Tag 3 identifyer - one byte */
201 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
202 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
203 ECRYPTFS_TAG_3_PACKET_TYPE);
204 rc = -EINVAL;
205 goto out;
207 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
208 * at end of function upon failure */
209 auth_tok_list_item =
210 kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, SLAB_KERNEL);
211 if (!auth_tok_list_item) {
212 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
213 rc = -ENOMEM;
214 goto out;
216 memset(auth_tok_list_item, 0,
217 sizeof(struct ecryptfs_auth_tok_list_item));
218 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
220 /* check for body size - one to two bytes */
221 rc = parse_packet_length(&data[(*packet_size)], &body_size,
222 &length_size);
223 if (rc) {
224 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
225 "rc = [%d]\n", rc);
226 goto out_free;
228 if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
229 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
230 body_size);
231 rc = -EINVAL;
232 goto out_free;
234 (*packet_size) += length_size;
236 /* now we know the length of the remainting Tag 3 packet size:
237 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
238 * number of hash iterations
239 * ECRYPTFS_SALT_SIZE bytes for salt
240 * body_size bytes minus the stuff above is the encrypted key size
242 if (unlikely((*packet_size) + body_size > max_packet_size)) {
243 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
244 rc = -EINVAL;
245 goto out_free;
248 /* There are 5 characters of additional information in the
249 * packet */
250 (*new_auth_tok)->session_key.encrypted_key_size =
251 body_size - (0x05 + ECRYPTFS_SALT_SIZE);
252 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
253 (*new_auth_tok)->session_key.encrypted_key_size);
255 /* Version 4 (from RFC2440) - one byte */
256 if (unlikely(data[(*packet_size)++] != 0x04)) {
257 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
258 "[%d]\n", data[(*packet_size) - 1]);
259 rc = -EINVAL;
260 goto out_free;
263 /* cipher - one byte */
264 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
265 (u16)data[(*packet_size)]);
266 /* A little extra work to differentiate among the AES key
267 * sizes; see RFC2440 */
268 switch(data[(*packet_size)++]) {
269 case RFC2440_CIPHER_AES_192:
270 crypt_stat->key_size = 24;
271 break;
272 default:
273 crypt_stat->key_size =
274 (*new_auth_tok)->session_key.encrypted_key_size;
276 ecryptfs_init_crypt_ctx(crypt_stat);
277 /* S2K identifier 3 (from RFC2440) */
278 if (unlikely(data[(*packet_size)++] != 0x03)) {
279 ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
280 "supported\n");
281 rc = -ENOSYS;
282 goto out_free;
285 /* TODO: finish the hash mapping */
286 /* hash algorithm - one byte */
287 switch (data[(*packet_size)++]) {
288 case 0x01: /* See RFC2440 for these numbers and their mappings */
289 /* Choose MD5 */
290 /* salt - ECRYPTFS_SALT_SIZE bytes */
291 memcpy((*new_auth_tok)->token.password.salt,
292 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
293 (*packet_size) += ECRYPTFS_SALT_SIZE;
295 /* This conversion was taken straight from RFC2440 */
296 /* number of hash iterations - one byte */
297 (*new_auth_tok)->token.password.hash_iterations =
298 ((u32) 16 + (data[(*packet_size)] & 15))
299 << ((data[(*packet_size)] >> 4) + 6);
300 (*packet_size)++;
302 /* encrypted session key -
303 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
304 memcpy((*new_auth_tok)->session_key.encrypted_key,
305 &data[(*packet_size)],
306 (*new_auth_tok)->session_key.encrypted_key_size);
307 (*packet_size) +=
308 (*new_auth_tok)->session_key.encrypted_key_size;
309 (*new_auth_tok)->session_key.flags &=
310 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
311 (*new_auth_tok)->session_key.flags |=
312 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
313 (*new_auth_tok)->token.password.hash_algo = 0x01;
314 break;
315 default:
316 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
317 "[%d]\n", data[(*packet_size) - 1]);
318 rc = -ENOSYS;
319 goto out_free;
321 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
322 /* TODO: Parametarize; we might actually want userspace to
323 * decrypt the session key. */
324 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
325 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
326 ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
327 ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
328 list_add(&auth_tok_list_item->list, auth_tok_list);
329 goto out;
330 out_free:
331 (*new_auth_tok) = NULL;
332 memset(auth_tok_list_item, 0,
333 sizeof(struct ecryptfs_auth_tok_list_item));
334 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
335 auth_tok_list_item);
336 out:
337 if (rc)
338 (*packet_size) = 0;
339 return rc;
343 * parse_tag_11_packet
344 * @data: The raw bytes of the packet
345 * @contents: This function writes the data contents of the literal
346 * packet into this memory location
347 * @max_contents_bytes: The maximum number of bytes that this function
348 * is allowed to write into contents
349 * @tag_11_contents_size: This function writes the size of the parsed
350 * contents into this memory location; zero on
351 * error
352 * @packet_size: This function writes the size of the parsed packet
353 * into this memory location; zero on error
354 * @max_packet_size: maximum number of bytes to parse
356 * Returns zero on success; non-zero on error.
358 static int
359 parse_tag_11_packet(unsigned char *data, unsigned char *contents,
360 size_t max_contents_bytes, size_t *tag_11_contents_size,
361 size_t *packet_size, size_t max_packet_size)
363 int rc = 0;
364 size_t body_size;
365 size_t length_size;
367 (*packet_size) = 0;
368 (*tag_11_contents_size) = 0;
370 /* check that:
371 * one byte for the Tag 11 ID flag
372 * two bytes for the Tag 11 length
373 * do not exceed the maximum_packet_size
375 if (unlikely((*packet_size) + 3 > max_packet_size)) {
376 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
377 rc = -EINVAL;
378 goto out;
381 /* check for Tag 11 identifyer - one byte */
382 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
383 ecryptfs_printk(KERN_WARNING,
384 "Invalid tag 11 packet format\n");
385 rc = -EINVAL;
386 goto out;
389 /* get Tag 11 content length - one or two bytes */
390 rc = parse_packet_length(&data[(*packet_size)], &body_size,
391 &length_size);
392 if (rc) {
393 ecryptfs_printk(KERN_WARNING,
394 "Invalid tag 11 packet format\n");
395 goto out;
397 (*packet_size) += length_size;
399 if (body_size < 13) {
400 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
401 body_size);
402 rc = -EINVAL;
403 goto out;
405 /* We have 13 bytes of surrounding packet values */
406 (*tag_11_contents_size) = (body_size - 13);
408 /* now we know the length of the remainting Tag 11 packet size:
409 * 14 fix bytes for: special flag one, special flag two,
410 * 12 skipped bytes
411 * body_size bytes minus the stuff above is the Tag 11 content
413 /* FIXME why is the body size one byte smaller than the actual
414 * size of the body?
415 * this seems to be an error here as well as in
416 * write_tag_11_packet() */
417 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
418 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
419 rc = -EINVAL;
420 goto out;
423 /* special flag one - one byte */
424 if (data[(*packet_size)++] != 0x62) {
425 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
426 rc = -EINVAL;
427 goto out;
430 /* special flag two - one byte */
431 if (data[(*packet_size)++] != 0x08) {
432 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
433 rc = -EINVAL;
434 goto out;
437 /* skip the next 12 bytes */
438 (*packet_size) += 12; /* We don't care about the filename or
439 * the timestamp */
441 /* get the Tag 11 contents - tag_11_contents_size bytes */
442 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
443 (*packet_size) += (*tag_11_contents_size);
445 out:
446 if (rc) {
447 (*packet_size) = 0;
448 (*tag_11_contents_size) = 0;
450 return rc;
454 * decrypt_session_key - Decrypt the session key with the given auth_tok.
456 * Returns Zero on success; non-zero error otherwise.
458 static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
459 struct ecryptfs_crypt_stat *crypt_stat)
461 struct ecryptfs_password *password_s_ptr;
462 struct scatterlist src_sg[2], dst_sg[2];
463 struct mutex *tfm_mutex = NULL;
464 /* TODO: Use virt_to_scatterlist for these */
465 char *encrypted_session_key;
466 char *session_key;
467 struct blkcipher_desc desc = {
468 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
470 int rc = 0;
472 password_s_ptr = &auth_tok->token.password;
473 if (ECRYPTFS_CHECK_FLAG(password_s_ptr->flags,
474 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))
475 ecryptfs_printk(KERN_DEBUG, "Session key encryption key "
476 "set; skipping key generation\n");
477 ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])"
478 ":\n",
479 password_s_ptr->session_key_encryption_key_bytes);
480 if (ecryptfs_verbosity > 0)
481 ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key,
482 password_s_ptr->
483 session_key_encryption_key_bytes);
484 if (!strcmp(crypt_stat->cipher,
485 crypt_stat->mount_crypt_stat->global_default_cipher_name)
486 && crypt_stat->mount_crypt_stat->global_key_tfm) {
487 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
488 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
489 } else {
490 char *full_alg_name;
492 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
493 crypt_stat->cipher,
494 "ecb");
495 if (rc)
496 goto out;
497 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
498 CRYPTO_ALG_ASYNC);
499 kfree(full_alg_name);
500 if (IS_ERR(desc.tfm)) {
501 rc = PTR_ERR(desc.tfm);
502 printk(KERN_ERR "Error allocating crypto context; "
503 "rc = [%d]\n", rc);
504 goto out;
506 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
508 if (tfm_mutex)
509 mutex_lock(tfm_mutex);
510 rc = crypto_blkcipher_setkey(desc.tfm,
511 password_s_ptr->session_key_encryption_key,
512 crypt_stat->key_size);
513 if (rc < 0) {
514 printk(KERN_ERR "Error setting key for crypto context\n");
515 rc = -EINVAL;
516 goto out_free_tfm;
518 /* TODO: virt_to_scatterlist */
519 encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
520 if (!encrypted_session_key) {
521 ecryptfs_printk(KERN_ERR, "Out of memory\n");
522 rc = -ENOMEM;
523 goto out_free_tfm;
525 session_key = (char *)__get_free_page(GFP_KERNEL);
526 if (!session_key) {
527 kfree(encrypted_session_key);
528 ecryptfs_printk(KERN_ERR, "Out of memory\n");
529 rc = -ENOMEM;
530 goto out_free_tfm;
532 memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
533 auth_tok->session_key.encrypted_key_size);
534 src_sg[0].page = virt_to_page(encrypted_session_key);
535 src_sg[0].offset = 0;
536 BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
537 src_sg[0].length = auth_tok->session_key.encrypted_key_size;
538 dst_sg[0].page = virt_to_page(session_key);
539 dst_sg[0].offset = 0;
540 auth_tok->session_key.decrypted_key_size =
541 auth_tok->session_key.encrypted_key_size;
542 dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
543 rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
544 auth_tok->session_key.encrypted_key_size);
545 if (rc) {
546 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
547 goto out_free_memory;
549 auth_tok->session_key.decrypted_key_size =
550 auth_tok->session_key.encrypted_key_size;
551 memcpy(auth_tok->session_key.decrypted_key, session_key,
552 auth_tok->session_key.decrypted_key_size);
553 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
554 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
555 auth_tok->session_key.decrypted_key_size);
556 ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
557 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
558 if (ecryptfs_verbosity > 0)
559 ecryptfs_dump_hex(crypt_stat->key,
560 crypt_stat->key_size);
561 out_free_memory:
562 memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
563 free_page((unsigned long)encrypted_session_key);
564 memset(session_key, 0, PAGE_CACHE_SIZE);
565 free_page((unsigned long)session_key);
566 out_free_tfm:
567 if (tfm_mutex)
568 mutex_unlock(tfm_mutex);
569 else
570 crypto_free_blkcipher(desc.tfm);
571 out:
572 return rc;
576 * ecryptfs_parse_packet_set
577 * @dest: The header page in memory
578 * @version: Version of file format, to guide parsing behavior
580 * Get crypt_stat to have the file's session key if the requisite key
581 * is available to decrypt the session key.
583 * Returns Zero if a valid authentication token was retrieved and
584 * processed; negative value for file not encrypted or for error
585 * conditions.
587 int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
588 unsigned char *src,
589 struct dentry *ecryptfs_dentry)
591 size_t i = 0;
592 int rc = 0;
593 size_t found_auth_tok = 0;
594 size_t next_packet_is_auth_tok_packet;
595 char sig[ECRYPTFS_SIG_SIZE_HEX];
596 struct list_head auth_tok_list;
597 struct list_head *walker;
598 struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
599 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
600 &ecryptfs_superblock_to_private(
601 ecryptfs_dentry->d_sb)->mount_crypt_stat;
602 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
603 size_t packet_size;
604 struct ecryptfs_auth_tok *new_auth_tok;
605 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
606 size_t tag_11_contents_size;
607 size_t tag_11_packet_size;
609 INIT_LIST_HEAD(&auth_tok_list);
610 /* Parse the header to find as many packets as we can, these will be
611 * added the our &auth_tok_list */
612 next_packet_is_auth_tok_packet = 1;
613 while (next_packet_is_auth_tok_packet) {
614 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
616 switch (src[i]) {
617 case ECRYPTFS_TAG_3_PACKET_TYPE:
618 rc = parse_tag_3_packet(crypt_stat,
619 (unsigned char *)&src[i],
620 &auth_tok_list, &new_auth_tok,
621 &packet_size, max_packet_size);
622 if (rc) {
623 ecryptfs_printk(KERN_ERR, "Error parsing "
624 "tag 3 packet\n");
625 rc = -EIO;
626 goto out_wipe_list;
628 i += packet_size;
629 rc = parse_tag_11_packet((unsigned char *)&src[i],
630 sig_tmp_space,
631 ECRYPTFS_SIG_SIZE,
632 &tag_11_contents_size,
633 &tag_11_packet_size,
634 max_packet_size);
635 if (rc) {
636 ecryptfs_printk(KERN_ERR, "No valid "
637 "(ecryptfs-specific) literal "
638 "packet containing "
639 "authentication token "
640 "signature found after "
641 "tag 3 packet\n");
642 rc = -EIO;
643 goto out_wipe_list;
645 i += tag_11_packet_size;
646 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
647 ecryptfs_printk(KERN_ERR, "Expected "
648 "signature of size [%d]; "
649 "read size [%d]\n",
650 ECRYPTFS_SIG_SIZE,
651 tag_11_contents_size);
652 rc = -EIO;
653 goto out_wipe_list;
655 ecryptfs_to_hex(new_auth_tok->token.password.signature,
656 sig_tmp_space, tag_11_contents_size);
657 new_auth_tok->token.password.signature[
658 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
659 ECRYPTFS_SET_FLAG(crypt_stat->flags,
660 ECRYPTFS_ENCRYPTED);
661 break;
662 case ECRYPTFS_TAG_11_PACKET_TYPE:
663 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
664 "(Tag 11 not allowed by itself)\n");
665 rc = -EIO;
666 goto out_wipe_list;
667 break;
668 default:
669 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
670 "[%d] of the file header; hex value of "
671 "character is [0x%.2x]\n", i, src[i]);
672 next_packet_is_auth_tok_packet = 0;
675 if (list_empty(&auth_tok_list)) {
676 rc = -EINVAL; /* Do not support non-encrypted files in
677 * the 0.1 release */
678 goto out;
680 /* If we have a global auth tok, then we should try to use
681 * it */
682 if (mount_crypt_stat->global_auth_tok) {
683 memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
684 ECRYPTFS_SIG_SIZE_HEX);
685 chosen_auth_tok = mount_crypt_stat->global_auth_tok;
686 } else
687 BUG(); /* We should always have a global auth tok in
688 * the 0.1 release */
689 /* Scan list to see if our chosen_auth_tok works */
690 list_for_each(walker, &auth_tok_list) {
691 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
692 auth_tok_list_item =
693 list_entry(walker, struct ecryptfs_auth_tok_list_item,
694 list);
695 candidate_auth_tok = &auth_tok_list_item->auth_tok;
696 if (unlikely(ecryptfs_verbosity > 0)) {
697 ecryptfs_printk(KERN_DEBUG,
698 "Considering cadidate auth tok:\n");
699 ecryptfs_dump_auth_tok(candidate_auth_tok);
701 /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
702 if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
703 && !strncmp(candidate_auth_tok->token.password.signature,
704 sig, ECRYPTFS_SIG_SIZE_HEX)) {
705 found_auth_tok = 1;
706 goto leave_list;
707 /* TODO: Transfer the common salt into the
708 * crypt_stat salt */
711 leave_list:
712 if (!found_auth_tok) {
713 ecryptfs_printk(KERN_ERR, "Could not find authentication "
714 "token on temporary list for sig [%.*s]\n",
715 ECRYPTFS_SIG_SIZE_HEX, sig);
716 rc = -EIO;
717 goto out_wipe_list;
718 } else {
719 memcpy(&(candidate_auth_tok->token.password),
720 &(chosen_auth_tok->token.password),
721 sizeof(struct ecryptfs_password));
722 rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
723 if (rc) {
724 ecryptfs_printk(KERN_ERR, "Error decrypting the "
725 "session key\n");
726 goto out_wipe_list;
728 rc = ecryptfs_compute_root_iv(crypt_stat);
729 if (rc) {
730 ecryptfs_printk(KERN_ERR, "Error computing "
731 "the root IV\n");
732 goto out_wipe_list;
735 rc = ecryptfs_init_crypt_ctx(crypt_stat);
736 if (rc) {
737 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
738 "context for cipher [%s]; rc = [%d]\n",
739 crypt_stat->cipher, rc);
741 out_wipe_list:
742 wipe_auth_tok_list(&auth_tok_list);
743 out:
744 return rc;
748 * write_tag_11_packet
749 * @dest: Target into which Tag 11 packet is to be written
750 * @max: Maximum packet length
751 * @contents: Byte array of contents to copy in
752 * @contents_length: Number of bytes in contents
753 * @packet_length: Length of the Tag 11 packet written; zero on error
755 * Returns zero on success; non-zero on error.
757 static int
758 write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
759 size_t *packet_length)
761 int rc = 0;
762 size_t packet_size_length;
764 (*packet_length) = 0;
765 if ((13 + contents_length) > max) {
766 rc = -EINVAL;
767 ecryptfs_printk(KERN_ERR, "Packet length larger than "
768 "maximum allowable\n");
769 goto out;
771 /* General packet header */
772 /* Packet tag */
773 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
774 /* Packet length */
775 rc = write_packet_length(&dest[(*packet_length)],
776 (13 + contents_length), &packet_size_length);
777 if (rc) {
778 ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
779 "header; cannot generate packet length\n");
780 goto out;
782 (*packet_length) += packet_size_length;
783 /* Tag 11 specific */
784 /* One-octet field that describes how the data is formatted */
785 dest[(*packet_length)++] = 0x62; /* binary data */
786 /* One-octet filename length followed by filename */
787 dest[(*packet_length)++] = 8;
788 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
789 (*packet_length) += 8;
790 /* Four-octet number indicating modification date */
791 memset(&dest[(*packet_length)], 0x00, 4);
792 (*packet_length) += 4;
793 /* Remainder is literal data */
794 memcpy(&dest[(*packet_length)], contents, contents_length);
795 (*packet_length) += contents_length;
796 out:
797 if (rc)
798 (*packet_length) = 0;
799 return rc;
803 * write_tag_3_packet
804 * @dest: Buffer into which to write the packet
805 * @max: Maximum number of bytes that can be written
806 * @auth_tok: Authentication token
807 * @crypt_stat: The cryptographic context
808 * @key_rec: encrypted key
809 * @packet_size: This function will write the number of bytes that end
810 * up constituting the packet; set to zero on error
812 * Returns zero on success; non-zero on error.
814 static int
815 write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
816 struct ecryptfs_crypt_stat *crypt_stat,
817 struct ecryptfs_key_record *key_rec, size_t *packet_size)
819 size_t i;
820 size_t signature_is_valid = 0;
821 size_t encrypted_session_key_valid = 0;
822 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
823 struct scatterlist dest_sg[2];
824 struct scatterlist src_sg[2];
825 struct mutex *tfm_mutex = NULL;
826 size_t key_rec_size;
827 size_t packet_size_length;
828 size_t cipher_code;
829 struct blkcipher_desc desc = {
830 .tfm = NULL,
831 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
833 int rc = 0;
835 (*packet_size) = 0;
836 /* Check for a valid signature on the auth_tok */
837 for (i = 0; i < ECRYPTFS_SIG_SIZE_HEX; i++)
838 signature_is_valid |= auth_tok->token.password.signature[i];
839 if (!signature_is_valid)
840 BUG();
841 ecryptfs_from_hex((*key_rec).sig, auth_tok->token.password.signature,
842 ECRYPTFS_SIG_SIZE);
843 encrypted_session_key_valid = 0;
844 for (i = 0; i < crypt_stat->key_size; i++)
845 encrypted_session_key_valid |=
846 auth_tok->session_key.encrypted_key[i];
847 if (encrypted_session_key_valid) {
848 memcpy((*key_rec).enc_key,
849 auth_tok->session_key.encrypted_key,
850 auth_tok->session_key.encrypted_key_size);
851 goto encrypted_session_key_set;
853 if (auth_tok->session_key.encrypted_key_size == 0)
854 auth_tok->session_key.encrypted_key_size =
855 crypt_stat->key_size;
856 if (crypt_stat->key_size == 24
857 && strcmp("aes", crypt_stat->cipher) == 0) {
858 memset((crypt_stat->key + 24), 0, 8);
859 auth_tok->session_key.encrypted_key_size = 32;
861 (*key_rec).enc_key_size =
862 auth_tok->session_key.encrypted_key_size;
863 if (ECRYPTFS_CHECK_FLAG(auth_tok->token.password.flags,
864 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET)) {
865 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
866 "session key encryption key of size [%d]\n",
867 auth_tok->token.password.
868 session_key_encryption_key_bytes);
869 memcpy(session_key_encryption_key,
870 auth_tok->token.password.session_key_encryption_key,
871 crypt_stat->key_size);
872 ecryptfs_printk(KERN_DEBUG,
873 "Cached session key " "encryption key: \n");
874 if (ecryptfs_verbosity > 0)
875 ecryptfs_dump_hex(session_key_encryption_key, 16);
877 if (unlikely(ecryptfs_verbosity > 0)) {
878 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
879 ecryptfs_dump_hex(session_key_encryption_key, 16);
881 rc = virt_to_scatterlist(crypt_stat->key,
882 (*key_rec).enc_key_size, src_sg, 2);
883 if (!rc) {
884 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
885 "for crypt_stat session key\n");
886 rc = -ENOMEM;
887 goto out;
889 rc = virt_to_scatterlist((*key_rec).enc_key,
890 (*key_rec).enc_key_size, dest_sg, 2);
891 if (!rc) {
892 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
893 "for crypt_stat encrypted session key\n");
894 rc = -ENOMEM;
895 goto out;
897 if (!strcmp(crypt_stat->cipher,
898 crypt_stat->mount_crypt_stat->global_default_cipher_name)
899 && crypt_stat->mount_crypt_stat->global_key_tfm) {
900 desc.tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
901 tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
902 } else {
903 char *full_alg_name;
905 rc = ecryptfs_crypto_api_algify_cipher_name(&full_alg_name,
906 crypt_stat->cipher,
907 "ecb");
908 if (rc)
909 goto out;
910 desc.tfm = crypto_alloc_blkcipher(full_alg_name, 0,
911 CRYPTO_ALG_ASYNC);
912 kfree(full_alg_name);
913 if (IS_ERR(desc.tfm)) {
914 rc = PTR_ERR(desc.tfm);
915 ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
916 "context for cipher [%s]; rc = [%d]\n",
917 crypt_stat->cipher, rc);
918 goto out;
920 crypto_blkcipher_set_flags(desc.tfm, CRYPTO_TFM_REQ_WEAK_KEY);
922 if (tfm_mutex)
923 mutex_lock(tfm_mutex);
924 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
925 crypt_stat->key_size);
926 if (rc < 0) {
927 if (tfm_mutex)
928 mutex_unlock(tfm_mutex);
929 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
930 "context; rc = [%d]\n", rc);
931 goto out;
933 rc = 0;
934 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
935 crypt_stat->key_size);
936 rc = crypto_blkcipher_encrypt(&desc, dest_sg, src_sg,
937 (*key_rec).enc_key_size);
938 if (rc) {
939 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
940 goto out;
942 if (tfm_mutex)
943 mutex_unlock(tfm_mutex);
944 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
945 if (ecryptfs_verbosity > 0)
946 ecryptfs_dump_hex((*key_rec).enc_key,
947 (*key_rec).enc_key_size);
948 encrypted_session_key_set:
949 /* Now we have a valid key_rec. Append it to the
950 * key_rec set. */
951 key_rec_size = (sizeof(struct ecryptfs_key_record)
952 - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
953 + ((*key_rec).enc_key_size));
954 /* TODO: Include a packet size limit as a parameter to this
955 * function once we have multi-packet headers (for versions
956 * later than 0.1 */
957 if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
958 ecryptfs_printk(KERN_ERR, "Keyset too large\n");
959 rc = -EINVAL;
960 goto out;
962 /* TODO: Packet size limit */
963 /* We have 5 bytes of surrounding packet data */
964 if ((0x05 + ECRYPTFS_SALT_SIZE
965 + (*key_rec).enc_key_size) >= max) {
966 ecryptfs_printk(KERN_ERR, "Authentication token is too "
967 "large\n");
968 rc = -EINVAL;
969 goto out;
971 /* This format is inspired by OpenPGP; see RFC 2440
972 * packet tag 3 */
973 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
974 /* ver+cipher+s2k+hash+salt+iter+enc_key */
975 rc = write_packet_length(&dest[(*packet_size)],
976 (0x05 + ECRYPTFS_SALT_SIZE
977 + (*key_rec).enc_key_size),
978 &packet_size_length);
979 if (rc) {
980 ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
981 "header; cannot generate packet length\n");
982 goto out;
984 (*packet_size) += packet_size_length;
985 dest[(*packet_size)++] = 0x04; /* version 4 */
986 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
987 if (cipher_code == 0) {
988 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
989 "cipher [%s]\n", crypt_stat->cipher);
990 rc = -EINVAL;
991 goto out;
993 dest[(*packet_size)++] = cipher_code;
994 dest[(*packet_size)++] = 0x03; /* S2K */
995 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
996 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
997 ECRYPTFS_SALT_SIZE);
998 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
999 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
1000 memcpy(&dest[(*packet_size)], (*key_rec).enc_key,
1001 (*key_rec).enc_key_size);
1002 (*packet_size) += (*key_rec).enc_key_size;
1003 out:
1004 if (desc.tfm && !tfm_mutex)
1005 crypto_free_blkcipher(desc.tfm);
1006 if (rc)
1007 (*packet_size) = 0;
1008 return rc;
1012 * ecryptfs_generate_key_packet_set
1013 * @dest: Virtual address from which to write the key record set
1014 * @crypt_stat: The cryptographic context from which the
1015 * authentication tokens will be retrieved
1016 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1017 * for the global parameters
1018 * @len: The amount written
1019 * @max: The maximum amount of data allowed to be written
1021 * Generates a key packet set and writes it to the virtual address
1022 * passed in.
1024 * Returns zero on success; non-zero on error.
1027 ecryptfs_generate_key_packet_set(char *dest_base,
1028 struct ecryptfs_crypt_stat *crypt_stat,
1029 struct dentry *ecryptfs_dentry, size_t *len,
1030 size_t max)
1032 int rc = 0;
1033 struct ecryptfs_auth_tok *auth_tok;
1034 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1035 &ecryptfs_superblock_to_private(
1036 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1037 size_t written;
1038 struct ecryptfs_key_record key_rec;
1040 (*len) = 0;
1041 if (mount_crypt_stat->global_auth_tok) {
1042 auth_tok = mount_crypt_stat->global_auth_tok;
1043 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1044 rc = write_tag_3_packet((dest_base + (*len)),
1045 max, auth_tok,
1046 crypt_stat, &key_rec,
1047 &written);
1048 if (rc) {
1049 ecryptfs_printk(KERN_WARNING, "Error "
1050 "writing tag 3 packet\n");
1051 goto out;
1053 (*len) += written;
1054 /* Write auth tok signature packet */
1055 rc = write_tag_11_packet(
1056 (dest_base + (*len)),
1057 (max - (*len)),
1058 key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
1059 if (rc) {
1060 ecryptfs_printk(KERN_ERR, "Error writing "
1061 "auth tok signature packet\n");
1062 goto out;
1064 (*len) += written;
1065 } else {
1066 ecryptfs_printk(KERN_WARNING, "Unsupported "
1067 "authentication token type\n");
1068 rc = -EINVAL;
1069 goto out;
1071 if (rc) {
1072 ecryptfs_printk(KERN_WARNING, "Error writing "
1073 "authentication token packet with sig "
1074 "= [%s]\n",
1075 mount_crypt_stat->global_auth_tok_sig);
1076 rc = -EIO;
1077 goto out;
1079 } else
1080 BUG();
1081 if (likely((max - (*len)) > 0)) {
1082 dest_base[(*len)] = 0x00;
1083 } else {
1084 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1085 rc = -EIO;
1087 out:
1088 if (rc)
1089 (*len) = 0;
1090 return rc;