eCryptfs: add key list structure; search keyring
[deliverable/linux.git] / fs / ecryptfs / keystore.c
CommitLineData
237fead6
MH
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.
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>
dddfa461 10 * Trevor S. Highland <trevor.highland@gmail.com>
237fead6
MH
11 *
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>
237fead6
MH
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"
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 */
42int process_request_key_err(long err_code)
43{
44 int rc = 0;
45
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;
63 }
64 return rc;
65}
66
237fead6
MH
67/**
68 * parse_packet_length
69 * @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 *
74 * Returns Zero on success
75 */
76static int parse_packet_length(unsigned char *data, size_t *size,
77 size_t *length_size)
78{
79 int rc = 0;
80
81 (*length_size) = 0;
82 (*size) = 0;
83 if (data[0] < 192) {
84 /* One-byte length */
dddfa461 85 (*size) = (unsigned char)data[0];
237fead6
MH
86 (*length_size) = 1;
87 } else if (data[0] < 224) {
88 /* Two-byte length */
dddfa461
MH
89 (*size) = (((unsigned char)(data[0]) - 192) * 256);
90 (*size) += ((unsigned char)(data[1]) + 192);
237fead6
MH
91 (*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/**
108 * write_packet_length
109 * @dest: The byte array target into which to write the
110 * length. Must have at least 5 bytes allocated.
111 * @size: The length to write.
112 * @packet_size_length: The number of bytes used to encode the
113 * packet length is written to this address.
114 *
115 * Returns zero on success; non-zero on error.
116 */
117static int write_packet_length(char *dest, size_t size,
118 size_t *packet_size_length)
119{
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
dddfa461
MH
137static 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;
165 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166 &packet_size_len);
167 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;
175 rc = write_packet_length(&message[i], session_key->encrypted_key_size,
176 &packet_size_len);
177 if (rc) {
178 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
179 "header; cannot generate packet length\n");
180 goto out;
181 }
182 i += packet_size_len;
183 memcpy(&message[i], session_key->encrypted_key,
184 session_key->encrypted_key_size);
185 i += session_key->encrypted_key_size;
186 *packet_len = i;
187out:
188 return rc;
189}
190
191static int
192parse_tag_65_packet(struct ecryptfs_session_key *session_key, u16 *cipher_code,
193 struct ecryptfs_message *msg)
194{
195 size_t i = 0;
196 char *data;
197 size_t data_len;
198 size_t m_size;
199 size_t message_len;
200 u16 checksum = 0;
201 u16 expected_checksum = 0;
202 int rc;
203
204 /*
205 * ***** TAG 65 Packet Format *****
206 * | Content Type | 1 byte |
207 * | Status Indicator | 1 byte |
208 * | File Encryption Key Size | 1 or 2 bytes |
209 * | File Encryption Key | arbitrary |
210 */
211 message_len = msg->data_len;
212 data = msg->data;
213 if (message_len < 4) {
214 rc = -EIO;
215 goto out;
216 }
217 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
218 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
219 rc = -EIO;
220 goto out;
221 }
222 if (data[i++]) {
223 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
224 "[%d]\n", data[i-1]);
225 rc = -EIO;
226 goto out;
227 }
228 rc = parse_packet_length(&data[i], &m_size, &data_len);
229 if (rc) {
230 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
231 "rc = [%d]\n", rc);
232 goto out;
233 }
234 i += data_len;
235 if (message_len < (i + m_size)) {
236 ecryptfs_printk(KERN_ERR, "The received netlink message is "
237 "shorter than expected\n");
238 rc = -EIO;
239 goto out;
240 }
241 if (m_size < 3) {
242 ecryptfs_printk(KERN_ERR,
243 "The decrypted key is not long enough to "
244 "include a cipher code and checksum\n");
245 rc = -EIO;
246 goto out;
247 }
248 *cipher_code = data[i++];
249 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
250 session_key->decrypted_key_size = m_size - 3;
251 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
252 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
253 "the maximum key size [%d]\n",
254 session_key->decrypted_key_size,
255 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
256 rc = -EIO;
257 goto out;
258 }
259 memcpy(session_key->decrypted_key, &data[i],
260 session_key->decrypted_key_size);
261 i += session_key->decrypted_key_size;
262 expected_checksum += (unsigned char)(data[i++]) << 8;
263 expected_checksum += (unsigned char)(data[i++]);
264 for (i = 0; i < session_key->decrypted_key_size; i++)
265 checksum += session_key->decrypted_key[i];
266 if (expected_checksum != checksum) {
267 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
268 "encryption key; expected [%x]; calculated "
269 "[%x]\n", expected_checksum, checksum);
270 rc = -EIO;
271 }
272out:
273 return rc;
274}
275
276
277static int
278write_tag_66_packet(char *signature, size_t cipher_code,
279 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
280 size_t *packet_len)
281{
282 size_t i = 0;
283 size_t j;
284 size_t data_len;
285 size_t checksum = 0;
286 size_t packet_size_len;
287 char *message;
288 int rc;
289
290 /*
291 * ***** TAG 66 Packet Format *****
292 * | Content Type | 1 byte |
293 * | Key Identifier Size | 1 or 2 bytes |
294 * | Key Identifier | arbitrary |
295 * | File Encryption Key Size | 1 or 2 bytes |
296 * | File Encryption Key | arbitrary |
297 */
298 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
299 *packet = kmalloc(data_len, GFP_KERNEL);
300 message = *packet;
301 if (!message) {
302 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
303 rc = -ENOMEM;
304 goto out;
305 }
306 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
307 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
308 &packet_size_len);
309 if (rc) {
310 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
311 "header; cannot generate packet length\n");
312 goto out;
313 }
314 i += packet_size_len;
315 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
316 i += ECRYPTFS_SIG_SIZE_HEX;
317 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
318 rc = write_packet_length(&message[i], crypt_stat->key_size + 3,
319 &packet_size_len);
320 if (rc) {
321 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
322 "header; cannot generate packet length\n");
323 goto out;
324 }
325 i += packet_size_len;
326 message[i++] = cipher_code;
327 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
328 i += crypt_stat->key_size;
329 for (j = 0; j < crypt_stat->key_size; j++)
330 checksum += crypt_stat->key[j];
331 message[i++] = (checksum / 256) % 256;
332 message[i++] = (checksum % 256);
333 *packet_len = i;
334out:
335 return rc;
336}
337
338static int
339parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
340 struct ecryptfs_message *msg)
341{
342 size_t i = 0;
343 char *data;
344 size_t data_len;
345 size_t message_len;
346 int rc;
347
348 /*
349 * ***** TAG 65 Packet Format *****
350 * | Content Type | 1 byte |
351 * | Status Indicator | 1 byte |
352 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
353 * | Encrypted File Encryption Key | arbitrary |
354 */
355 message_len = msg->data_len;
356 data = msg->data;
357 /* verify that everything through the encrypted FEK size is present */
358 if (message_len < 4) {
359 rc = -EIO;
360 goto out;
361 }
362 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
363 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_67\n");
364 rc = -EIO;
365 goto out;
366 }
367 if (data[i++]) {
368 ecryptfs_printk(KERN_ERR, "Status indicator has non zero value"
369 " [%d]\n", data[i-1]);
370 rc = -EIO;
371 goto out;
372 }
373 rc = parse_packet_length(&data[i], &key_rec->enc_key_size, &data_len);
374 if (rc) {
375 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
376 "rc = [%d]\n", rc);
377 goto out;
378 }
379 i += data_len;
380 if (message_len < (i + key_rec->enc_key_size)) {
381 ecryptfs_printk(KERN_ERR, "message_len [%d]; max len is [%d]\n",
382 message_len, (i + key_rec->enc_key_size));
383 rc = -EIO;
384 goto out;
385 }
386 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
387 ecryptfs_printk(KERN_ERR, "Encrypted key_size [%d] larger than "
388 "the maximum key size [%d]\n",
389 key_rec->enc_key_size,
390 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
391 rc = -EIO;
392 goto out;
393 }
394 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
395out:
396 return rc;
397}
398
399/**
400 * decrypt_pki_encrypted_session_key - Decrypt the session key with
401 * the given auth_tok.
402 *
403 * Returns Zero on success; non-zero error otherwise.
404 */
f4aad16a
MH
405static int
406decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
407 struct ecryptfs_crypt_stat *crypt_stat)
dddfa461
MH
408{
409 u16 cipher_code = 0;
410 struct ecryptfs_msg_ctx *msg_ctx;
411 struct ecryptfs_message *msg = NULL;
f4aad16a 412 char *auth_tok_sig;
dddfa461
MH
413 char *netlink_message;
414 size_t netlink_message_length;
415 int rc;
416
f4aad16a
MH
417 if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) {
418 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
419 auth_tok->token_type);
420 goto out;
421 }
422 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
dddfa461
MH
423 &netlink_message, &netlink_message_length);
424 if (rc) {
425 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
426 goto out;
427 }
428 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
429 netlink_message_length, &msg_ctx);
430 if (rc) {
431 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
432 goto out;
433 }
434 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
435 if (rc) {
436 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
437 "from the user space daemon\n");
438 rc = -EIO;
439 goto out;
440 }
441 rc = parse_tag_65_packet(&(auth_tok->session_key),
442 &cipher_code, msg);
443 if (rc) {
444 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
445 rc);
446 goto out;
447 }
448 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
449 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
450 auth_tok->session_key.decrypted_key_size);
451 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
452 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
453 if (rc) {
454 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
455 cipher_code)
456 goto out;
457 }
458 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
459 if (ecryptfs_verbosity > 0) {
460 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
461 ecryptfs_dump_hex(crypt_stat->key,
462 crypt_stat->key_size);
463 }
464out:
465 if (msg)
466 kfree(msg);
467 return rc;
468}
469
470static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
471{
472 struct list_head *walker;
473 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
474
475 walker = auth_tok_list_head->next;
476 while (walker != auth_tok_list_head) {
477 auth_tok_list_item =
478 list_entry(walker, struct ecryptfs_auth_tok_list_item,
479 list);
480 walker = auth_tok_list_item->list.next;
481 memset(auth_tok_list_item, 0,
482 sizeof(struct ecryptfs_auth_tok_list_item));
483 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
484 auth_tok_list_item);
485 }
486 auth_tok_list_head->next = NULL;
487}
488
489struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
490
491
492/**
493 * parse_tag_1_packet
494 * @crypt_stat: The cryptographic context to modify based on packet
495 * contents.
496 * @data: The raw bytes of the packet.
497 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
498 * a new authentication token will be placed at the end
499 * of this list for this packet.
500 * @new_auth_tok: Pointer to a pointer to memory that this function
501 * allocates; sets the memory address of the pointer to
502 * NULL on error. This object is added to the
503 * auth_tok_list.
504 * @packet_size: This function writes the size of the parsed packet
505 * into this memory location; zero on error.
506 *
507 * Returns zero on success; non-zero on error.
508 */
509static int
510parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
511 unsigned char *data, struct list_head *auth_tok_list,
512 struct ecryptfs_auth_tok **new_auth_tok,
513 size_t *packet_size, size_t max_packet_size)
514{
515 size_t body_size;
516 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
517 size_t length_size;
518 int rc = 0;
519
520 (*packet_size) = 0;
521 (*new_auth_tok) = NULL;
522
523 /* we check that:
524 * one byte for the Tag 1 ID flag
525 * two bytes for the body size
526 * do not exceed the maximum_packet_size
527 */
528 if (unlikely((*packet_size) + 3 > max_packet_size)) {
529 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
530 rc = -EINVAL;
531 goto out;
532 }
533 /* check for Tag 1 identifier - one byte */
534 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
535 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
536 ECRYPTFS_TAG_1_PACKET_TYPE);
537 rc = -EINVAL;
538 goto out;
539 }
540 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
541 * at end of function upon failure */
542 auth_tok_list_item =
543 kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache,
544 GFP_KERNEL);
545 if (!auth_tok_list_item) {
546 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
547 rc = -ENOMEM;
548 goto out;
549 }
550 memset(auth_tok_list_item, 0,
551 sizeof(struct ecryptfs_auth_tok_list_item));
552 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
553 /* check for body size - one to two bytes
554 *
555 * ***** TAG 1 Packet Format *****
556 * | version number | 1 byte |
557 * | key ID | 8 bytes |
558 * | public key algorithm | 1 byte |
559 * | encrypted session key | arbitrary |
560 */
561 rc = parse_packet_length(&data[(*packet_size)], &body_size,
562 &length_size);
563 if (rc) {
564 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
565 "rc = [%d]\n", rc);
566 goto out_free;
567 }
568 if (unlikely(body_size < (0x02 + ECRYPTFS_SIG_SIZE))) {
569 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
570 body_size);
571 rc = -EINVAL;
572 goto out_free;
573 }
574 (*packet_size) += length_size;
575 if (unlikely((*packet_size) + body_size > max_packet_size)) {
576 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
577 rc = -EINVAL;
578 goto out_free;
579 }
580 /* Version 3 (from RFC2440) - one byte */
581 if (unlikely(data[(*packet_size)++] != 0x03)) {
582 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
583 "[%d]\n", data[(*packet_size) - 1]);
584 rc = -EINVAL;
585 goto out_free;
586 }
587 /* Read Signature */
588 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
589 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
590 *packet_size += ECRYPTFS_SIG_SIZE;
591 /* This byte is skipped because the kernel does not need to
592 * know which public key encryption algorithm was used */
593 (*packet_size)++;
594 (*new_auth_tok)->session_key.encrypted_key_size =
595 body_size - (0x02 + ECRYPTFS_SIG_SIZE);
596 if ((*new_auth_tok)->session_key.encrypted_key_size
597 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
598 ecryptfs_printk(KERN_ERR, "Tag 1 packet contains key larger "
599 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
600 rc = -EINVAL;
601 goto out;
602 }
603 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
604 (*new_auth_tok)->session_key.encrypted_key_size);
605 memcpy((*new_auth_tok)->session_key.encrypted_key,
606 &data[(*packet_size)], (body_size - 0x02 - ECRYPTFS_SIG_SIZE));
607 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
608 (*new_auth_tok)->session_key.flags &=
609 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
610 (*new_auth_tok)->session_key.flags |=
611 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
612 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
e2bd99ec 613 (*new_auth_tok)->flags |= ECRYPTFS_PRIVATE_KEY;
dddfa461
MH
614 /* TODO: Why are we setting this flag here? Don't we want the
615 * userspace to decrypt the session key? */
e2bd99ec
MH
616 (*new_auth_tok)->session_key.flags &=
617 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
618 (*new_auth_tok)->session_key.flags &=
619 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
dddfa461
MH
620 list_add(&auth_tok_list_item->list, auth_tok_list);
621 goto out;
622out_free:
623 (*new_auth_tok) = NULL;
624 memset(auth_tok_list_item, 0,
625 sizeof(struct ecryptfs_auth_tok_list_item));
626 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
627 auth_tok_list_item);
628out:
629 if (rc)
630 (*packet_size) = 0;
631 return rc;
632}
633
237fead6
MH
634/**
635 * parse_tag_3_packet
636 * @crypt_stat: The cryptographic context to modify based on packet
637 * contents.
638 * @data: The raw bytes of the packet.
639 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
640 * a new authentication token will be placed at the end
641 * of this list for this packet.
642 * @new_auth_tok: Pointer to a pointer to memory that this function
643 * allocates; sets the memory address of the pointer to
644 * NULL on error. This object is added to the
645 * auth_tok_list.
646 * @packet_size: This function writes the size of the parsed packet
647 * into this memory location; zero on error.
648 * @max_packet_size: maximum number of bytes to parse
649 *
650 * Returns zero on success; non-zero on error.
651 */
652static int
653parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
654 unsigned char *data, struct list_head *auth_tok_list,
655 struct ecryptfs_auth_tok **new_auth_tok,
656 size_t *packet_size, size_t max_packet_size)
657{
237fead6
MH
658 size_t body_size;
659 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
660 size_t length_size;
dddfa461 661 int rc = 0;
237fead6
MH
662
663 (*packet_size) = 0;
664 (*new_auth_tok) = NULL;
665
666 /* we check that:
667 * one byte for the Tag 3 ID flag
668 * two bytes for the body size
669 * do not exceed the maximum_packet_size
670 */
671 if (unlikely((*packet_size) + 3 > max_packet_size)) {
672 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
673 rc = -EINVAL;
674 goto out;
675 }
676
677 /* check for Tag 3 identifyer - one byte */
678 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
679 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
680 ECRYPTFS_TAG_3_PACKET_TYPE);
681 rc = -EINVAL;
682 goto out;
683 }
684 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
685 * at end of function upon failure */
686 auth_tok_list_item =
c3762229 687 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
237fead6
MH
688 if (!auth_tok_list_item) {
689 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
690 rc = -ENOMEM;
691 goto out;
692 }
237fead6
MH
693 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
694
695 /* check for body size - one to two bytes */
696 rc = parse_packet_length(&data[(*packet_size)], &body_size,
697 &length_size);
698 if (rc) {
699 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
700 "rc = [%d]\n", rc);
701 goto out_free;
702 }
703 if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
704 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
705 body_size);
706 rc = -EINVAL;
707 goto out_free;
708 }
709 (*packet_size) += length_size;
710
711 /* now we know the length of the remainting Tag 3 packet size:
712 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
713 * number of hash iterations
714 * ECRYPTFS_SALT_SIZE bytes for salt
715 * body_size bytes minus the stuff above is the encrypted key size
716 */
717 if (unlikely((*packet_size) + body_size > max_packet_size)) {
718 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
719 rc = -EINVAL;
720 goto out_free;
721 }
722
723 /* There are 5 characters of additional information in the
724 * packet */
725 (*new_auth_tok)->session_key.encrypted_key_size =
726 body_size - (0x05 + ECRYPTFS_SALT_SIZE);
727 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
728 (*new_auth_tok)->session_key.encrypted_key_size);
729
730 /* Version 4 (from RFC2440) - one byte */
731 if (unlikely(data[(*packet_size)++] != 0x04)) {
732 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
733 "[%d]\n", data[(*packet_size) - 1]);
734 rc = -EINVAL;
735 goto out_free;
736 }
737
738 /* cipher - one byte */
739 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);
752 /* S2K identifier 3 (from RFC2440) */
753 if (unlikely(data[(*packet_size)++] != 0x03)) {
754 ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
755 "supported\n");
756 rc = -ENOSYS;
757 goto out_free;
758 }
759
760 /* TODO: finish the hash mapping */
761 /* hash algorithm - one byte */
762 switch (data[(*packet_size)++]) {
763 case 0x01: /* See RFC2440 for these numbers and their mappings */
764 /* Choose MD5 */
765 /* salt - ECRYPTFS_SALT_SIZE bytes */
766 memcpy((*new_auth_tok)->token.password.salt,
767 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
768 (*packet_size) += ECRYPTFS_SALT_SIZE;
769
770 /* This conversion was taken straight from RFC2440 */
771 /* number of hash iterations - one byte */
772 (*new_auth_tok)->token.password.hash_iterations =
773 ((u32) 16 + (data[(*packet_size)] & 15))
774 << ((data[(*packet_size)] >> 4) + 6);
775 (*packet_size)++;
776
777 /* encrypted session key -
778 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
779 memcpy((*new_auth_tok)->session_key.encrypted_key,
780 &data[(*packet_size)],
781 (*new_auth_tok)->session_key.encrypted_key_size);
782 (*packet_size) +=
783 (*new_auth_tok)->session_key.encrypted_key_size;
784 (*new_auth_tok)->session_key.flags &=
785 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
786 (*new_auth_tok)->session_key.flags |=
787 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
788 (*new_auth_tok)->token.password.hash_algo = 0x01;
789 break;
790 default:
791 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
792 "[%d]\n", data[(*packet_size) - 1]);
793 rc = -ENOSYS;
794 goto out_free;
795 }
796 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
797 /* TODO: Parametarize; we might actually want userspace to
798 * decrypt the session key. */
e2bd99ec
MH
799 (*new_auth_tok)->session_key.flags &=
800 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
801 (*new_auth_tok)->session_key.flags &=
802 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
237fead6
MH
803 list_add(&auth_tok_list_item->list, auth_tok_list);
804 goto out;
805out_free:
806 (*new_auth_tok) = NULL;
807 memset(auth_tok_list_item, 0,
808 sizeof(struct ecryptfs_auth_tok_list_item));
809 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
810 auth_tok_list_item);
811out:
812 if (rc)
813 (*packet_size) = 0;
814 return rc;
815}
816
817/**
818 * parse_tag_11_packet
819 * @data: The raw bytes of the packet
820 * @contents: This function writes the data contents of the literal
821 * packet into this memory location
822 * @max_contents_bytes: The maximum number of bytes that this function
823 * is allowed to write into contents
824 * @tag_11_contents_size: This function writes the size of the parsed
825 * contents into this memory location; zero on
826 * error
827 * @packet_size: This function writes the size of the parsed packet
828 * into this memory location; zero on error
829 * @max_packet_size: maximum number of bytes to parse
830 *
831 * Returns zero on success; non-zero on error.
832 */
833static int
834parse_tag_11_packet(unsigned char *data, unsigned char *contents,
835 size_t max_contents_bytes, size_t *tag_11_contents_size,
836 size_t *packet_size, size_t max_packet_size)
837{
237fead6
MH
838 size_t body_size;
839 size_t length_size;
dddfa461 840 int rc = 0;
237fead6
MH
841
842 (*packet_size) = 0;
843 (*tag_11_contents_size) = 0;
844
845 /* check that:
846 * one byte for the Tag 11 ID flag
847 * two bytes for the Tag 11 length
848 * do not exceed the maximum_packet_size
849 */
850 if (unlikely((*packet_size) + 3 > max_packet_size)) {
851 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
852 rc = -EINVAL;
853 goto out;
854 }
855
856 /* check for Tag 11 identifyer - one byte */
857 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
858 ecryptfs_printk(KERN_WARNING,
859 "Invalid tag 11 packet format\n");
860 rc = -EINVAL;
861 goto out;
862 }
863
864 /* get Tag 11 content length - one or two bytes */
865 rc = parse_packet_length(&data[(*packet_size)], &body_size,
866 &length_size);
867 if (rc) {
868 ecryptfs_printk(KERN_WARNING,
869 "Invalid tag 11 packet format\n");
870 goto out;
871 }
872 (*packet_size) += length_size;
873
874 if (body_size < 13) {
875 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
876 body_size);
877 rc = -EINVAL;
878 goto out;
879 }
880 /* We have 13 bytes of surrounding packet values */
881 (*tag_11_contents_size) = (body_size - 13);
882
883 /* now we know the length of the remainting Tag 11 packet size:
884 * 14 fix bytes for: special flag one, special flag two,
885 * 12 skipped bytes
886 * body_size bytes minus the stuff above is the Tag 11 content
887 */
888 /* FIXME why is the body size one byte smaller than the actual
889 * size of the body?
890 * this seems to be an error here as well as in
891 * write_tag_11_packet() */
892 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
893 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
894 rc = -EINVAL;
895 goto out;
896 }
897
898 /* special flag one - one byte */
899 if (data[(*packet_size)++] != 0x62) {
900 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
901 rc = -EINVAL;
902 goto out;
903 }
904
905 /* special flag two - one byte */
906 if (data[(*packet_size)++] != 0x08) {
907 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
908 rc = -EINVAL;
909 goto out;
910 }
911
912 /* skip the next 12 bytes */
913 (*packet_size) += 12; /* We don't care about the filename or
914 * the timestamp */
915
916 /* get the Tag 11 contents - tag_11_contents_size bytes */
917 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
918 (*packet_size) += (*tag_11_contents_size);
919
920out:
921 if (rc) {
922 (*packet_size) = 0;
923 (*tag_11_contents_size) = 0;
924 }
925 return rc;
926}
927
f4aad16a
MH
928static int
929ecryptfs_find_global_auth_tok_for_sig(
930 struct ecryptfs_global_auth_tok **global_auth_tok,
931 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
932{
933 struct ecryptfs_global_auth_tok *walker;
934 int rc = 0;
935
936 (*global_auth_tok) = NULL;
937 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
938 list_for_each_entry(walker,
939 &mount_crypt_stat->global_auth_tok_list,
940 mount_crypt_stat_list) {
941 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
942 (*global_auth_tok) = walker;
943 goto out;
944 }
945 }
946 rc = -EINVAL;
947out:
948 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
949 return rc;
950}
951
237fead6 952/**
f4aad16a
MH
953 * ecryptfs_verify_version
954 * @version: The version number to confirm
955 *
956 * Returns zero on good version; non-zero otherwise
957 */
958static int ecryptfs_verify_version(u16 version)
959{
960 int rc = 0;
961 unsigned char major;
962 unsigned char minor;
963
964 major = ((version >> 8) & 0xFF);
965 minor = (version & 0xFF);
966 if (major != ECRYPTFS_VERSION_MAJOR) {
967 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
968 "Expected [%d]; got [%d]\n",
969 ECRYPTFS_VERSION_MAJOR, major);
970 rc = -EINVAL;
971 goto out;
972 }
973 if (minor != ECRYPTFS_VERSION_MINOR) {
974 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
975 "Expected [%d]; got [%d]\n",
976 ECRYPTFS_VERSION_MINOR, minor);
977 rc = -EINVAL;
978 goto out;
979 }
980out:
981 return rc;
982}
983
984int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
985 struct ecryptfs_auth_tok **auth_tok,
986 char *sig)
987{
988 int rc = 0;
989
990 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
991 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
992 printk(KERN_ERR "Could not find key with description: [%s]\n",
993 sig);
994 process_request_key_err(PTR_ERR(*auth_tok_key));
995 rc = -EINVAL;
996 goto out;
997 }
998 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
999 if (ecryptfs_verify_version((*auth_tok)->version)) {
1000 printk(KERN_ERR
1001 "Data structure version mismatch. "
1002 "Userspace tools must match eCryptfs "
1003 "kernel module with major version [%d] "
1004 "and minor version [%d]\n",
1005 ECRYPTFS_VERSION_MAJOR,
1006 ECRYPTFS_VERSION_MINOR);
1007 rc = -EINVAL;
1008 goto out;
1009 }
1010 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
1011 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
1012 printk(KERN_ERR "Invalid auth_tok structure "
1013 "returned from key query\n");
1014 rc = -EINVAL;
1015 goto out;
1016 }
1017out:
1018 return rc;
1019}
1020
1021/**
1022 * ecryptfs_find_auth_tok_for_sig
1023 * @auth_tok: Set to the matching auth_tok; NULL if not found
1024 * @crypt_stat: inode crypt_stat crypto context
1025 * @sig: Sig of auth_tok to find
1026 *
1027 * For now, this function simply looks at the registered auth_tok's
1028 * linked off the mount_crypt_stat, so all the auth_toks that can be
1029 * used must be registered at mount time. This function could
1030 * potentially try a lot harder to find auth_tok's (e.g., by calling
1031 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
1032 * that static registration of auth_tok's will no longer be necessary.
1033 *
1034 * Returns zero on no error; non-zero on error
1035 */
1036static int
1037ecryptfs_find_auth_tok_for_sig(
1038 struct ecryptfs_auth_tok **auth_tok,
1039 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1040{
1041 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1042 crypt_stat->mount_crypt_stat;
1043 struct ecryptfs_global_auth_tok *global_auth_tok;
1044 int rc = 0;
1045
1046 (*auth_tok) = NULL;
1047 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1048 mount_crypt_stat, sig)) {
1049 struct key *auth_tok_key;
1050
1051 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1052 sig);
1053 } else
1054 (*auth_tok) = global_auth_tok->global_auth_tok;
1055 return rc;
1056}
1057
1058/**
1059 * decrypt_passphrase_encrypted_session_key - Decrypt the session key
1060 * with the given auth_tok.
237fead6
MH
1061 *
1062 * Returns Zero on success; non-zero error otherwise.
1063 */
f4aad16a
MH
1064static int
1065decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1066 struct ecryptfs_crypt_stat *crypt_stat)
237fead6 1067{
f4aad16a
MH
1068 struct scatterlist dst_sg;
1069 struct scatterlist src_sg;
237fead6 1070 struct mutex *tfm_mutex = NULL;
8bba066f
MH
1071 struct blkcipher_desc desc = {
1072 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1073 };
1074 int rc = 0;
237fead6 1075
f4aad16a
MH
1076 if (unlikely(ecryptfs_verbosity > 0)) {
1077 ecryptfs_printk(
1078 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1079 auth_tok->token.password.session_key_encryption_key_bytes);
1080 ecryptfs_dump_hex(
1081 auth_tok->token.password.session_key_encryption_key,
1082 auth_tok->token.password.session_key_encryption_key_bytes);
1083 }
1084 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1085 crypt_stat->cipher);
1086 if (unlikely(rc)) {
1087 printk(KERN_ERR "Internal error whilst attempting to get "
1088 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1089 crypt_stat->cipher, rc);
1090 goto out;
237fead6 1091 }
f4aad16a
MH
1092 if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1093 auth_tok->session_key.encrypted_key_size,
1094 &src_sg, 1)) != 1) {
1095 printk(KERN_ERR "Internal error whilst attempting to convert "
1096 "auth_tok->session_key.encrypted_key to scatterlist; "
1097 "expected rc = 1; got rc = [%d]. "
1098 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1099 auth_tok->session_key.encrypted_key_size);
1100 goto out;
1101 }
1102 auth_tok->session_key.decrypted_key_size =
1103 auth_tok->session_key.encrypted_key_size;
1104 if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1105 auth_tok->session_key.decrypted_key_size,
1106 &dst_sg, 1)) != 1) {
1107 printk(KERN_ERR "Internal error whilst attempting to convert "
1108 "auth_tok->session_key.decrypted_key to scatterlist; "
1109 "expected rc = 1; got rc = [%d]\n", rc);
1110 goto out;
1111 }
1112 mutex_lock(tfm_mutex);
1113 rc = crypto_blkcipher_setkey(
1114 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1115 crypt_stat->key_size);
1116 if (unlikely(rc < 0)) {
1117 mutex_unlock(tfm_mutex);
e5d9cbde
MH
1118 printk(KERN_ERR "Error setting key for crypto context\n");
1119 rc = -EINVAL;
f4aad16a 1120 goto out;
237fead6 1121 }
f4aad16a 1122 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
8bba066f 1123 auth_tok->session_key.encrypted_key_size);
f4aad16a
MH
1124 mutex_unlock(tfm_mutex);
1125 if (unlikely(rc)) {
8bba066f 1126 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
f4aad16a 1127 goto out;
8bba066f 1128 }
237fead6
MH
1129 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1130 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1131 auth_tok->session_key.decrypted_key_size);
e2bd99ec 1132 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
f4aad16a
MH
1133 if (unlikely(ecryptfs_verbosity > 0)) {
1134 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1135 crypt_stat->key_size);
237fead6
MH
1136 ecryptfs_dump_hex(crypt_stat->key,
1137 crypt_stat->key_size);
f4aad16a 1138 }
237fead6
MH
1139out:
1140 return rc;
1141}
1142
f4aad16a
MH
1143int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1144{
1145 int rc = 0;
1146
1147 (*sig) = NULL;
1148 switch (auth_tok->token_type) {
1149 case ECRYPTFS_PASSWORD:
1150 (*sig) = auth_tok->token.password.signature;
1151 break;
1152 case ECRYPTFS_PRIVATE_KEY:
1153 (*sig) = auth_tok->token.private_key.signature;
1154 break;
1155 default:
1156 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1157 auth_tok->token_type);
1158 rc = -EINVAL;
1159 }
1160 return rc;
1161}
1162
237fead6
MH
1163/**
1164 * ecryptfs_parse_packet_set
1165 * @dest: The header page in memory
1166 * @version: Version of file format, to guide parsing behavior
1167 *
1168 * Get crypt_stat to have the file's session key if the requisite key
1169 * is available to decrypt the session key.
1170 *
1171 * Returns Zero if a valid authentication token was retrieved and
1172 * processed; negative value for file not encrypted or for error
1173 * conditions.
1174 */
1175int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1176 unsigned char *src,
1177 struct dentry *ecryptfs_dentry)
1178{
1179 size_t i = 0;
f4aad16a 1180 size_t found_auth_tok;
237fead6 1181 size_t next_packet_is_auth_tok_packet;
237fead6 1182 struct list_head auth_tok_list;
f4aad16a 1183 struct ecryptfs_auth_tok *matching_auth_tok = NULL;
237fead6 1184 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
f4aad16a 1185 char *candidate_auth_tok_sig;
237fead6
MH
1186 size_t packet_size;
1187 struct ecryptfs_auth_tok *new_auth_tok;
1188 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
f4aad16a 1189 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
237fead6
MH
1190 size_t tag_11_contents_size;
1191 size_t tag_11_packet_size;
dddfa461 1192 int rc = 0;
237fead6
MH
1193
1194 INIT_LIST_HEAD(&auth_tok_list);
f4aad16a 1195 /* Parse the header to find as many packets as we can; these will be
237fead6
MH
1196 * added the our &auth_tok_list */
1197 next_packet_is_auth_tok_packet = 1;
1198 while (next_packet_is_auth_tok_packet) {
1199 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1200
1201 switch (src[i]) {
1202 case ECRYPTFS_TAG_3_PACKET_TYPE:
1203 rc = parse_tag_3_packet(crypt_stat,
1204 (unsigned char *)&src[i],
1205 &auth_tok_list, &new_auth_tok,
1206 &packet_size, max_packet_size);
1207 if (rc) {
1208 ecryptfs_printk(KERN_ERR, "Error parsing "
1209 "tag 3 packet\n");
1210 rc = -EIO;
1211 goto out_wipe_list;
1212 }
1213 i += packet_size;
1214 rc = parse_tag_11_packet((unsigned char *)&src[i],
1215 sig_tmp_space,
1216 ECRYPTFS_SIG_SIZE,
1217 &tag_11_contents_size,
1218 &tag_11_packet_size,
1219 max_packet_size);
1220 if (rc) {
1221 ecryptfs_printk(KERN_ERR, "No valid "
1222 "(ecryptfs-specific) literal "
1223 "packet containing "
1224 "authentication token "
1225 "signature found after "
1226 "tag 3 packet\n");
1227 rc = -EIO;
1228 goto out_wipe_list;
1229 }
1230 i += tag_11_packet_size;
1231 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1232 ecryptfs_printk(KERN_ERR, "Expected "
1233 "signature of size [%d]; "
1234 "read size [%d]\n",
1235 ECRYPTFS_SIG_SIZE,
1236 tag_11_contents_size);
1237 rc = -EIO;
1238 goto out_wipe_list;
1239 }
1240 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1241 sig_tmp_space, tag_11_contents_size);
1242 new_auth_tok->token.password.signature[
1243 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
e2bd99ec 1244 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
237fead6 1245 break;
dddfa461
MH
1246 case ECRYPTFS_TAG_1_PACKET_TYPE:
1247 rc = parse_tag_1_packet(crypt_stat,
1248 (unsigned char *)&src[i],
1249 &auth_tok_list, &new_auth_tok,
1250 &packet_size, max_packet_size);
1251 if (rc) {
1252 ecryptfs_printk(KERN_ERR, "Error parsing "
1253 "tag 1 packet\n");
1254 rc = -EIO;
1255 goto out_wipe_list;
1256 }
1257 i += packet_size;
e2bd99ec 1258 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
dddfa461 1259 break;
237fead6
MH
1260 case ECRYPTFS_TAG_11_PACKET_TYPE:
1261 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1262 "(Tag 11 not allowed by itself)\n");
1263 rc = -EIO;
1264 goto out_wipe_list;
1265 break;
1266 default:
1267 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1268 "[%d] of the file header; hex value of "
1269 "character is [0x%.2x]\n", i, src[i]);
1270 next_packet_is_auth_tok_packet = 0;
1271 }
1272 }
1273 if (list_empty(&auth_tok_list)) {
f4aad16a
MH
1274 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1275 "eCryptfs file; this is not supported in this version "
1276 "of the eCryptfs kernel module\n");
1277 rc = -EINVAL;
237fead6
MH
1278 goto out;
1279 }
f4aad16a
MH
1280 /* auth_tok_list contains the set of authentication tokens
1281 * parsed from the metadata. We need to find a matching
1282 * authentication token that has the secret component(s)
1283 * necessary to decrypt the EFEK in the auth_tok parsed from
1284 * the metadata. There may be several potential matches, but
1285 * just one will be sufficient to decrypt to get the FEK. */
1286find_next_matching_auth_tok:
1287 found_auth_tok = 0;
1288 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
237fead6
MH
1289 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1290 if (unlikely(ecryptfs_verbosity > 0)) {
1291 ecryptfs_printk(KERN_DEBUG,
1292 "Considering cadidate auth tok:\n");
1293 ecryptfs_dump_auth_tok(candidate_auth_tok);
1294 }
f4aad16a
MH
1295 if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1296 candidate_auth_tok))) {
1297 printk(KERN_ERR
1298 "Unrecognized candidate auth tok type: [%d]\n",
1299 candidate_auth_tok->token_type);
1300 rc = -EINVAL;
1301 goto out_wipe_list;
1302 }
1303 if ((rc = ecryptfs_find_auth_tok_for_sig(
1304 &matching_auth_tok, crypt_stat,
1305 candidate_auth_tok_sig)))
1306 rc = 0;
1307 if (matching_auth_tok) {
dddfa461 1308 found_auth_tok = 1;
f4aad16a 1309 goto found_matching_auth_tok;
237fead6
MH
1310 }
1311 }
237fead6 1312 if (!found_auth_tok) {
f4aad16a
MH
1313 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1314 "authentication token\n");
237fead6
MH
1315 rc = -EIO;
1316 goto out_wipe_list;
dddfa461 1317 }
f4aad16a 1318found_matching_auth_tok:
e2bd99ec 1319 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
dddfa461 1320 memcpy(&(candidate_auth_tok->token.private_key),
f4aad16a 1321 &(matching_auth_tok->token.private_key),
dddfa461 1322 sizeof(struct ecryptfs_private_key));
f4aad16a 1323 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
dddfa461
MH
1324 crypt_stat);
1325 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
237fead6 1326 memcpy(&(candidate_auth_tok->token.password),
f4aad16a 1327 &(matching_auth_tok->token.password),
237fead6 1328 sizeof(struct ecryptfs_password));
f4aad16a
MH
1329 rc = decrypt_passphrase_encrypted_session_key(
1330 candidate_auth_tok, crypt_stat);
dddfa461
MH
1331 }
1332 if (rc) {
f4aad16a
MH
1333 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1334
1335 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1336 "session key for authentication token with sig "
1337 "[%.*s]; rc = [%d]. Removing auth tok "
1338 "candidate from the list and searching for "
1339 "the next match.\n", candidate_auth_tok_sig,
1340 ECRYPTFS_SIG_SIZE_HEX, rc);
1341 list_for_each_entry_safe(auth_tok_list_item,
1342 auth_tok_list_item_tmp,
1343 &auth_tok_list, list) {
1344 if (candidate_auth_tok
1345 == &auth_tok_list_item->auth_tok) {
1346 list_del(&auth_tok_list_item->list);
1347 kmem_cache_free(
1348 ecryptfs_auth_tok_list_item_cache,
1349 auth_tok_list_item);
1350 goto find_next_matching_auth_tok;
1351 }
1352 }
1353 BUG();
dddfa461
MH
1354 }
1355 rc = ecryptfs_compute_root_iv(crypt_stat);
1356 if (rc) {
1357 ecryptfs_printk(KERN_ERR, "Error computing "
1358 "the root IV\n");
1359 goto out_wipe_list;
237fead6
MH
1360 }
1361 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1362 if (rc) {
1363 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1364 "context for cipher [%s]; rc = [%d]\n",
1365 crypt_stat->cipher, rc);
1366 }
1367out_wipe_list:
1368 wipe_auth_tok_list(&auth_tok_list);
1369out:
1370 return rc;
1371}
f4aad16a 1372
dddfa461
MH
1373static int
1374pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1375 struct ecryptfs_crypt_stat *crypt_stat,
1376 struct ecryptfs_key_record *key_rec)
1377{
1378 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1379 char *netlink_payload;
1380 size_t netlink_payload_length;
1381 struct ecryptfs_message *msg;
1382 int rc;
1383
1384 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1385 ecryptfs_code_for_cipher_string(crypt_stat),
1386 crypt_stat, &netlink_payload,
1387 &netlink_payload_length);
1388 if (rc) {
1389 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1390 goto out;
1391 }
1392 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1393 netlink_payload_length, &msg_ctx);
1394 if (rc) {
1395 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1396 goto out;
1397 }
1398 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1399 if (rc) {
1400 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1401 "from the user space daemon\n");
1402 rc = -EIO;
1403 goto out;
1404 }
1405 rc = parse_tag_67_packet(key_rec, msg);
1406 if (rc)
1407 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1408 kfree(msg);
1409out:
1410 if (netlink_payload)
1411 kfree(netlink_payload);
1412 return rc;
1413}
1414/**
1415 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1416 * @dest: Buffer into which to write the packet
1417 * @max: Maximum number of bytes that can be writtn
1418 * @packet_size: This function will write the number of bytes that end
1419 * up constituting the packet; set to zero on error
1420 *
1421 * Returns zero on success; non-zero on error.
1422 */
1423static int
f4aad16a
MH
1424write_tag_1_packet(char *dest, size_t *remaining_bytes,
1425 struct ecryptfs_auth_tok *auth_tok,
dddfa461 1426 struct ecryptfs_crypt_stat *crypt_stat,
dddfa461
MH
1427 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1428{
1429 size_t i;
1430 size_t encrypted_session_key_valid = 0;
dddfa461 1431 size_t packet_size_length;
f4aad16a 1432 size_t max_packet_size;
dddfa461
MH
1433 int rc = 0;
1434
1435 (*packet_size) = 0;
1436 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1437 ECRYPTFS_SIG_SIZE);
1438 encrypted_session_key_valid = 0;
1439 for (i = 0; i < crypt_stat->key_size; i++)
1440 encrypted_session_key_valid |=
1441 auth_tok->session_key.encrypted_key[i];
1442 if (encrypted_session_key_valid) {
1443 memcpy(key_rec->enc_key,
1444 auth_tok->session_key.encrypted_key,
1445 auth_tok->session_key.encrypted_key_size);
1446 goto encrypted_session_key_set;
1447 }
1448 if (auth_tok->session_key.encrypted_key_size == 0)
1449 auth_tok->session_key.encrypted_key_size =
1450 auth_tok->token.private_key.key_size;
1451 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1452 if (rc) {
1453 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1454 "via a pki");
1455 goto out;
1456 }
1457 if (ecryptfs_verbosity > 0) {
1458 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1459 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1460 }
1461encrypted_session_key_set:
f4aad16a
MH
1462 /* This format is inspired by OpenPGP; see RFC 2440
1463 * packet tag 1 */
1464 max_packet_size = (1 /* Tag 1 identifier */
1465 + 3 /* Max Tag 1 packet size */
1466 + 1 /* Version */
1467 + ECRYPTFS_SIG_SIZE /* Key identifier */
1468 + 1 /* Cipher identifier */
1469 + key_rec->enc_key_size); /* Encrypted key size */
1470 if (max_packet_size > (*remaining_bytes)) {
1471 printk(KERN_ERR "Packet length larger than maximum allowable; "
1472 "need up to [%d] bytes, but there are only [%d] "
1473 "available\n", max_packet_size, (*remaining_bytes));
dddfa461
MH
1474 rc = -EINVAL;
1475 goto out;
1476 }
1477 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
f4aad16a 1478 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
dddfa461
MH
1479 &packet_size_length);
1480 if (rc) {
1481 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1482 "header; cannot generate packet length\n");
1483 goto out;
1484 }
1485 (*packet_size) += packet_size_length;
1486 dest[(*packet_size)++] = 0x03; /* version 3 */
1487 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1488 (*packet_size) += ECRYPTFS_SIG_SIZE;
1489 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1490 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1491 key_rec->enc_key_size);
1492 (*packet_size) += key_rec->enc_key_size;
1493out:
1494 if (rc)
1495 (*packet_size) = 0;
f4aad16a
MH
1496 else
1497 (*remaining_bytes) -= (*packet_size);
dddfa461
MH
1498 return rc;
1499}
237fead6
MH
1500
1501/**
1502 * write_tag_11_packet
1503 * @dest: Target into which Tag 11 packet is to be written
1504 * @max: Maximum packet length
1505 * @contents: Byte array of contents to copy in
1506 * @contents_length: Number of bytes in contents
1507 * @packet_length: Length of the Tag 11 packet written; zero on error
1508 *
1509 * Returns zero on success; non-zero on error.
1510 */
1511static int
1512write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
1513 size_t *packet_length)
1514{
237fead6 1515 size_t packet_size_length;
dddfa461 1516 int rc = 0;
237fead6
MH
1517
1518 (*packet_length) = 0;
1519 if ((13 + contents_length) > max) {
1520 rc = -EINVAL;
1521 ecryptfs_printk(KERN_ERR, "Packet length larger than "
1522 "maximum allowable\n");
1523 goto out;
1524 }
1525 /* General packet header */
1526 /* Packet tag */
1527 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
1528 /* Packet length */
1529 rc = write_packet_length(&dest[(*packet_length)],
1530 (13 + contents_length), &packet_size_length);
1531 if (rc) {
1532 ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
1533 "header; cannot generate packet length\n");
1534 goto out;
1535 }
1536 (*packet_length) += packet_size_length;
1537 /* Tag 11 specific */
1538 /* One-octet field that describes how the data is formatted */
1539 dest[(*packet_length)++] = 0x62; /* binary data */
1540 /* One-octet filename length followed by filename */
1541 dest[(*packet_length)++] = 8;
1542 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1543 (*packet_length) += 8;
1544 /* Four-octet number indicating modification date */
1545 memset(&dest[(*packet_length)], 0x00, 4);
1546 (*packet_length) += 4;
1547 /* Remainder is literal data */
1548 memcpy(&dest[(*packet_length)], contents, contents_length);
1549 (*packet_length) += contents_length;
1550 out:
1551 if (rc)
1552 (*packet_length) = 0;
1553 return rc;
1554}
1555
1556/**
1557 * write_tag_3_packet
1558 * @dest: Buffer into which to write the packet
1559 * @max: Maximum number of bytes that can be written
1560 * @auth_tok: Authentication token
1561 * @crypt_stat: The cryptographic context
1562 * @key_rec: encrypted key
1563 * @packet_size: This function will write the number of bytes that end
1564 * up constituting the packet; set to zero on error
1565 *
1566 * Returns zero on success; non-zero on error.
1567 */
1568static int
f4aad16a
MH
1569write_tag_3_packet(char *dest, size_t *remaining_bytes,
1570 struct ecryptfs_auth_tok *auth_tok,
237fead6
MH
1571 struct ecryptfs_crypt_stat *crypt_stat,
1572 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1573{
237fead6 1574 size_t i;
237fead6
MH
1575 size_t encrypted_session_key_valid = 0;
1576 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
f4aad16a
MH
1577 struct scatterlist dst_sg;
1578 struct scatterlist src_sg;
237fead6 1579 struct mutex *tfm_mutex = NULL;
237fead6 1580 size_t cipher_code;
f4aad16a
MH
1581 size_t packet_size_length;
1582 size_t max_packet_size;
1583 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1584 crypt_stat->mount_crypt_stat;
8bba066f
MH
1585 struct blkcipher_desc desc = {
1586 .tfm = NULL,
1587 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1588 };
1589 int rc = 0;
237fead6
MH
1590
1591 (*packet_size) = 0;
dddfa461 1592 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
237fead6 1593 ECRYPTFS_SIG_SIZE);
f4aad16a
MH
1594 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1595 crypt_stat->cipher);
1596 if (unlikely(rc)) {
1597 printk(KERN_ERR "Internal error whilst attempting to get "
1598 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1599 crypt_stat->cipher, rc);
1600 goto out;
1601 }
1602 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1603 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1604
1605 printk(KERN_WARNING "No key size specified at mount; "
1606 "defaulting to [%d]\n", alg->max_keysize);
1607 mount_crypt_stat->global_default_cipher_key_size =
1608 alg->max_keysize;
237fead6 1609 }
f4aad16a
MH
1610 if (crypt_stat->key_size == 0)
1611 crypt_stat->key_size =
1612 mount_crypt_stat->global_default_cipher_key_size;
237fead6
MH
1613 if (auth_tok->session_key.encrypted_key_size == 0)
1614 auth_tok->session_key.encrypted_key_size =
1615 crypt_stat->key_size;
1616 if (crypt_stat->key_size == 24
1617 && strcmp("aes", crypt_stat->cipher) == 0) {
1618 memset((crypt_stat->key + 24), 0, 8);
1619 auth_tok->session_key.encrypted_key_size = 32;
f4aad16a
MH
1620 } else
1621 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
dddfa461 1622 key_rec->enc_key_size =
237fead6 1623 auth_tok->session_key.encrypted_key_size;
f4aad16a
MH
1624 encrypted_session_key_valid = 0;
1625 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1626 encrypted_session_key_valid |=
1627 auth_tok->session_key.encrypted_key[i];
1628 if (encrypted_session_key_valid) {
1629 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1630 "using auth_tok->session_key.encrypted_key, "
1631 "where key_rec->enc_key_size = [%d]\n",
1632 key_rec->enc_key_size);
1633 memcpy(key_rec->enc_key,
1634 auth_tok->session_key.encrypted_key,
1635 key_rec->enc_key_size);
1636 goto encrypted_session_key_set;
1637 }
dddfa461
MH
1638 if (auth_tok->token.password.flags &
1639 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
237fead6
MH
1640 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1641 "session key encryption key of size [%d]\n",
1642 auth_tok->token.password.
1643 session_key_encryption_key_bytes);
1644 memcpy(session_key_encryption_key,
1645 auth_tok->token.password.session_key_encryption_key,
1646 crypt_stat->key_size);
1647 ecryptfs_printk(KERN_DEBUG,
1648 "Cached session key " "encryption key: \n");
1649 if (ecryptfs_verbosity > 0)
1650 ecryptfs_dump_hex(session_key_encryption_key, 16);
1651 }
1652 if (unlikely(ecryptfs_verbosity > 0)) {
1653 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1654 ecryptfs_dump_hex(session_key_encryption_key, 16);
1655 }
f4aad16a
MH
1656 if ((rc = virt_to_scatterlist(crypt_stat->key,
1657 key_rec->enc_key_size, &src_sg, 1))
1658 != 1) {
237fead6 1659 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
f4aad16a
MH
1660 "for crypt_stat session key; expected rc = 1; "
1661 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1662 rc, key_rec->enc_key_size);
237fead6
MH
1663 rc = -ENOMEM;
1664 goto out;
1665 }
f4aad16a
MH
1666 if ((rc = virt_to_scatterlist(key_rec->enc_key,
1667 key_rec->enc_key_size, &dst_sg, 1))
1668 != 1) {
237fead6 1669 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
f4aad16a
MH
1670 "for crypt_stat encrypted session key; "
1671 "expected rc = 1; got rc = [%d]. "
1672 "key_rec->enc_key_size = [%d]\n", rc,
1673 key_rec->enc_key_size);
237fead6
MH
1674 rc = -ENOMEM;
1675 goto out;
1676 }
f4aad16a 1677 mutex_lock(tfm_mutex);
8bba066f
MH
1678 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1679 crypt_stat->key_size);
237fead6 1680 if (rc < 0) {
f4aad16a 1681 mutex_unlock(tfm_mutex);
237fead6 1682 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
8bba066f 1683 "context; rc = [%d]\n", rc);
237fead6
MH
1684 goto out;
1685 }
1686 rc = 0;
1687 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1688 crypt_stat->key_size);
f4aad16a 1689 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
8bba066f 1690 (*key_rec).enc_key_size);
f4aad16a 1691 mutex_unlock(tfm_mutex);
8bba066f
MH
1692 if (rc) {
1693 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1694 goto out;
1695 }
237fead6 1696 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
f4aad16a
MH
1697 if (ecryptfs_verbosity > 0) {
1698 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1699 key_rec->enc_key_size);
dddfa461
MH
1700 ecryptfs_dump_hex(key_rec->enc_key,
1701 key_rec->enc_key_size);
237fead6 1702 }
f4aad16a
MH
1703encrypted_session_key_set:
1704 /* This format is inspired by OpenPGP; see RFC 2440
1705 * packet tag 3 */
1706 max_packet_size = (1 /* Tag 3 identifier */
1707 + 3 /* Max Tag 3 packet size */
1708 + 1 /* Version */
1709 + 1 /* Cipher code */
1710 + 1 /* S2K specifier */
1711 + 1 /* Hash identifier */
1712 + ECRYPTFS_SALT_SIZE /* Salt */
1713 + 1 /* Hash iterations */
1714 + key_rec->enc_key_size); /* Encrypted key size */
1715 if (max_packet_size > (*remaining_bytes)) {
1716 printk(KERN_ERR "Packet too large; need up to [%d] bytes, but "
1717 "there are only [%d] available\n", max_packet_size,
1718 (*remaining_bytes));
237fead6
MH
1719 rc = -EINVAL;
1720 goto out;
1721 }
237fead6 1722 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
f4aad16a
MH
1723 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1724 * to get the number of octets in the actual Tag 3 packet */
1725 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
237fead6
MH
1726 &packet_size_length);
1727 if (rc) {
f4aad16a
MH
1728 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1729 "generate packet length. rc = [%d]\n", rc);
237fead6
MH
1730 goto out;
1731 }
1732 (*packet_size) += packet_size_length;
1733 dest[(*packet_size)++] = 0x04; /* version 4 */
f4aad16a
MH
1734 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1735 * specified with strings */
237fead6
MH
1736 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1737 if (cipher_code == 0) {
1738 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1739 "cipher [%s]\n", crypt_stat->cipher);
1740 rc = -EINVAL;
1741 goto out;
1742 }
1743 dest[(*packet_size)++] = cipher_code;
1744 dest[(*packet_size)++] = 0x03; /* S2K */
1745 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1746 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1747 ECRYPTFS_SALT_SIZE);
1748 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1749 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
dddfa461
MH
1750 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1751 key_rec->enc_key_size);
1752 (*packet_size) += key_rec->enc_key_size;
237fead6 1753out:
237fead6
MH
1754 if (rc)
1755 (*packet_size) = 0;
f4aad16a
MH
1756 else
1757 (*remaining_bytes) -= (*packet_size);
237fead6
MH
1758 return rc;
1759}
1760
eb95e7ff
MH
1761struct kmem_cache *ecryptfs_key_record_cache;
1762
237fead6
MH
1763/**
1764 * ecryptfs_generate_key_packet_set
1765 * @dest: Virtual address from which to write the key record set
1766 * @crypt_stat: The cryptographic context from which the
1767 * authentication tokens will be retrieved
1768 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1769 * for the global parameters
1770 * @len: The amount written
1771 * @max: The maximum amount of data allowed to be written
1772 *
1773 * Generates a key packet set and writes it to the virtual address
1774 * passed in.
1775 *
1776 * Returns zero on success; non-zero on error.
1777 */
1778int
1779ecryptfs_generate_key_packet_set(char *dest_base,
1780 struct ecryptfs_crypt_stat *crypt_stat,
1781 struct dentry *ecryptfs_dentry, size_t *len,
1782 size_t max)
1783{
237fead6 1784 struct ecryptfs_auth_tok *auth_tok;
f4aad16a 1785 struct ecryptfs_global_auth_tok *global_auth_tok;
237fead6
MH
1786 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1787 &ecryptfs_superblock_to_private(
1788 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1789 size_t written;
eb95e7ff 1790 struct ecryptfs_key_record *key_rec;
f4aad16a 1791 struct ecryptfs_key_sig *key_sig;
dddfa461 1792 int rc = 0;
237fead6
MH
1793
1794 (*len) = 0;
f4aad16a 1795 mutex_lock(&crypt_stat->keysig_list_mutex);
eb95e7ff
MH
1796 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1797 if (!key_rec) {
1798 rc = -ENOMEM;
1799 goto out;
1800 }
f4aad16a
MH
1801 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1802 crypt_stat_list) {
1803 memset(key_rec, 0, sizeof(*key_rec));
1804 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1805 mount_crypt_stat,
1806 key_sig->keysig);
1807 if (rc) {
1808 printk(KERN_ERR "Error attempting to get the global "
1809 "auth_tok; rc = [%d]\n", rc);
1810 goto out_free;
1811 }
1812 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1813 printk(KERN_WARNING
1814 "Skipping invalid auth tok with sig = [%s]\n",
1815 global_auth_tok->sig);
1816 continue;
1817 }
1818 auth_tok = global_auth_tok->global_auth_tok;
237fead6
MH
1819 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1820 rc = write_tag_3_packet((dest_base + (*len)),
f4aad16a 1821 &max, auth_tok,
eb95e7ff 1822 crypt_stat, key_rec,
237fead6
MH
1823 &written);
1824 if (rc) {
1825 ecryptfs_printk(KERN_WARNING, "Error "
1826 "writing tag 3 packet\n");
eb95e7ff 1827 goto out_free;
237fead6
MH
1828 }
1829 (*len) += written;
1830 /* Write auth tok signature packet */
f4aad16a
MH
1831 rc = write_tag_11_packet((dest_base + (*len)), &max,
1832 key_rec->sig,
1833 ECRYPTFS_SIG_SIZE, &written);
237fead6
MH
1834 if (rc) {
1835 ecryptfs_printk(KERN_ERR, "Error writing "
1836 "auth tok signature packet\n");
eb95e7ff 1837 goto out_free;
237fead6
MH
1838 }
1839 (*len) += written;
dddfa461
MH
1840 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1841 rc = write_tag_1_packet(dest_base + (*len),
f4aad16a
MH
1842 &max, auth_tok,
1843 crypt_stat, key_rec, &written);
dddfa461
MH
1844 if (rc) {
1845 ecryptfs_printk(KERN_WARNING, "Error "
1846 "writing tag 1 packet\n");
eb95e7ff 1847 goto out_free;
dddfa461
MH
1848 }
1849 (*len) += written;
237fead6
MH
1850 } else {
1851 ecryptfs_printk(KERN_WARNING, "Unsupported "
1852 "authentication token type\n");
1853 rc = -EINVAL;
eb95e7ff 1854 goto out_free;
237fead6 1855 }
f4aad16a
MH
1856 }
1857 if (likely(max > 0)) {
237fead6
MH
1858 dest_base[(*len)] = 0x00;
1859 } else {
1860 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1861 rc = -EIO;
1862 }
eb95e7ff
MH
1863out_free:
1864 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
237fead6
MH
1865out:
1866 if (rc)
1867 (*len) = 0;
f4aad16a
MH
1868 mutex_unlock(&crypt_stat->keysig_list_mutex);
1869 return rc;
1870}
1871
1872struct kmem_cache *ecryptfs_key_sig_cache;
1873
1874int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1875{
1876 struct ecryptfs_key_sig *new_key_sig;
1877 int rc = 0;
1878
1879 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1880 if (!new_key_sig) {
1881 rc = -ENOMEM;
1882 printk(KERN_ERR
1883 "Error allocating from ecryptfs_key_sig_cache\n");
1884 goto out;
1885 }
1886 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1887 mutex_lock(&crypt_stat->keysig_list_mutex);
1888 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1889 mutex_unlock(&crypt_stat->keysig_list_mutex);
1890out:
237fead6
MH
1891 return rc;
1892}
f4aad16a
MH
1893
1894struct kmem_cache *ecryptfs_global_auth_tok_cache;
1895
1896int
1897ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1898 char *sig)
1899{
1900 struct ecryptfs_global_auth_tok *new_auth_tok;
1901 int rc = 0;
1902
1903 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1904 GFP_KERNEL);
1905 if (!new_auth_tok) {
1906 rc = -ENOMEM;
1907 printk(KERN_ERR "Error allocating from "
1908 "ecryptfs_global_auth_tok_cache\n");
1909 goto out;
1910 }
1911 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1912 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1913 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1914 list_add(&new_auth_tok->mount_crypt_stat_list,
1915 &mount_crypt_stat->global_auth_tok_list);
1916 mount_crypt_stat->num_global_auth_toks++;
1917 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1918out:
1919 return rc;
1920}
1921
This page took 0.211058 seconds and 5 git commands to generate.