blob: f7debe6961d10058a608245bc46ee8fed99003cd [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
5 * file.
6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -080010 * Trevor S. Highland <trevor.highland@gmail.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/string.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070029#include <linux/syscalls.h>
30#include <linux/pagemap.h>
31#include <linux/key.h>
32#include <linux/random.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
37/**
38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and
40 * return an error code.
41 */
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
Michael Halcrow237fead2006-10-04 02:16:22 -070067/**
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 */
Michael Halcrowdddfa462007-02-12 00:53:44 -080085 (*size) = (unsigned char)data[0];
Michael Halcrow237fead2006-10-04 02:16:22 -070086 (*length_size) = 1;
87 } else if (data[0] < 224) {
88 /* Two-byte length */
Michael Halcrowdddfa462007-02-12 00:53:44 -080089 (*size) = (((unsigned char)(data[0]) - 192) * 256);
90 (*size) += ((unsigned char)(data[1]) + 192);
Michael Halcrow237fead2006-10-04 02:16:22 -070091 (*length_size) = 2;
92 } else if (data[0] == 255) {
93 /* Five-byte length; we're not supposed to see this */
94 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
95 "supported\n");
96 rc = -EINVAL;
97 goto out;
98 } else {
99 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
100 rc = -EINVAL;
101 goto out;
102 }
103out:
104 return rc;
105}
106
107/**
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
Michael Halcrowdddfa462007-02-12 00:53:44 -0800137static int
138write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
139 char **packet, size_t *packet_len)
140{
141 size_t i = 0;
142 size_t data_len;
143 size_t packet_size_len;
144 char *message;
145 int rc;
146
147 /*
148 * ***** TAG 64 Packet Format *****
149 * | Content Type | 1 byte |
150 * | Key Identifier Size | 1 or 2 bytes |
151 * | Key Identifier | arbitrary |
152 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
153 * | Encrypted File Encryption Key | arbitrary |
154 */
155 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
156 + session_key->encrypted_key_size);
157 *packet = kmalloc(data_len, GFP_KERNEL);
158 message = *packet;
159 if (!message) {
160 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
161 rc = -ENOMEM;
162 goto out;
163 }
164 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
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 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700405static int
406decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
407 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -0800408{
409 u16 cipher_code = 0;
410 struct ecryptfs_msg_ctx *msg_ctx;
411 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700412 char *auth_tok_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800413 char *netlink_message;
414 size_t netlink_message_length;
415 int rc;
416
Michael Halcrowf4aad162007-10-16 01:27:53 -0700417 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),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800423 &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{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800472 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700473 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800474
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700475 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
476 auth_tok_list_head, list) {
477 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800478 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
479 auth_tok_list_item);
480 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800481}
482
483struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
484
Michael Halcrowdddfa462007-02-12 00:53:44 -0800485/**
486 * parse_tag_1_packet
487 * @crypt_stat: The cryptographic context to modify based on packet
488 * contents.
489 * @data: The raw bytes of the packet.
490 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
491 * a new authentication token will be placed at the end
492 * of this list for this packet.
493 * @new_auth_tok: Pointer to a pointer to memory that this function
494 * allocates; sets the memory address of the pointer to
495 * NULL on error. This object is added to the
496 * auth_tok_list.
497 * @packet_size: This function writes the size of the parsed packet
498 * into this memory location; zero on error.
499 *
500 * Returns zero on success; non-zero on error.
501 */
502static int
503parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
504 unsigned char *data, struct list_head *auth_tok_list,
505 struct ecryptfs_auth_tok **new_auth_tok,
506 size_t *packet_size, size_t max_packet_size)
507{
508 size_t body_size;
509 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
510 size_t length_size;
511 int rc = 0;
512
513 (*packet_size) = 0;
514 (*new_auth_tok) = NULL;
Michael Halcrow13218172007-10-16 01:27:56 -0700515 /**
516 * This format is inspired by OpenPGP; see RFC 2440
517 * packet tag 1
518 *
519 * Tag 1 identifier (1 byte)
520 * Max Tag 1 packet size (max 3 bytes)
521 * Version (1 byte)
522 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
523 * Cipher identifier (1 byte)
524 * Encrypted key size (arbitrary)
525 *
526 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800527 */
Michael Halcrow13218172007-10-16 01:27:56 -0700528 if (unlikely(max_packet_size < 12)) {
529 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800530 rc = -EINVAL;
531 goto out;
532 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800533 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow13218172007-10-16 01:27:56 -0700534 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
535 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800536 rc = -EINVAL;
537 goto out;
538 }
539 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
540 * at end of function upon failure */
541 auth_tok_list_item =
Michael Halcrow13218172007-10-16 01:27:56 -0700542 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
543 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800544 if (!auth_tok_list_item) {
Michael Halcrow13218172007-10-16 01:27:56 -0700545 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800546 rc = -ENOMEM;
547 goto out;
548 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800549 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow13218172007-10-16 01:27:56 -0700550 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
551 &length_size))) {
552 printk(KERN_WARNING "Error parsing packet length; "
553 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800554 goto out_free;
555 }
Michael Halcrow13218172007-10-16 01:27:56 -0700556 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
557 printk(KERN_WARNING "Invalid body size ([%d])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800558 rc = -EINVAL;
559 goto out_free;
560 }
561 (*packet_size) += length_size;
562 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700563 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800564 rc = -EINVAL;
565 goto out_free;
566 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800567 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700568 printk(KERN_WARNING "Unknown version number [%d]\n",
569 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800570 rc = -EINVAL;
571 goto out_free;
572 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800573 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
574 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
575 *packet_size += ECRYPTFS_SIG_SIZE;
576 /* This byte is skipped because the kernel does not need to
577 * know which public key encryption algorithm was used */
578 (*packet_size)++;
579 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow13218172007-10-16 01:27:56 -0700580 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800581 if ((*new_auth_tok)->session_key.encrypted_key_size
582 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow13218172007-10-16 01:27:56 -0700583 printk(KERN_WARNING "Tag 1 packet contains key larger "
584 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800585 rc = -EINVAL;
586 goto out;
587 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800588 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow13218172007-10-16 01:27:56 -0700589 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800590 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
591 (*new_auth_tok)->session_key.flags &=
592 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
593 (*new_auth_tok)->session_key.flags |=
594 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
595 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow13218172007-10-16 01:27:56 -0700596 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800597 (*new_auth_tok)->session_key.flags &=
598 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
599 (*new_auth_tok)->session_key.flags &=
600 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800601 list_add(&auth_tok_list_item->list, auth_tok_list);
602 goto out;
603out_free:
604 (*new_auth_tok) = NULL;
605 memset(auth_tok_list_item, 0,
606 sizeof(struct ecryptfs_auth_tok_list_item));
607 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
608 auth_tok_list_item);
609out:
610 if (rc)
611 (*packet_size) = 0;
612 return rc;
613}
614
Michael Halcrow237fead2006-10-04 02:16:22 -0700615/**
616 * parse_tag_3_packet
617 * @crypt_stat: The cryptographic context to modify based on packet
618 * contents.
619 * @data: The raw bytes of the packet.
620 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
621 * a new authentication token will be placed at the end
622 * of this list for this packet.
623 * @new_auth_tok: Pointer to a pointer to memory that this function
624 * allocates; sets the memory address of the pointer to
625 * NULL on error. This object is added to the
626 * auth_tok_list.
627 * @packet_size: This function writes the size of the parsed packet
628 * into this memory location; zero on error.
629 * @max_packet_size: maximum number of bytes to parse
630 *
631 * Returns zero on success; non-zero on error.
632 */
633static int
634parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
635 unsigned char *data, struct list_head *auth_tok_list,
636 struct ecryptfs_auth_tok **new_auth_tok,
637 size_t *packet_size, size_t max_packet_size)
638{
Michael Halcrow237fead2006-10-04 02:16:22 -0700639 size_t body_size;
640 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
641 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800642 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700643
644 (*packet_size) = 0;
645 (*new_auth_tok) = NULL;
646
647 /* we check that:
648 * one byte for the Tag 3 ID flag
649 * two bytes for the body size
650 * do not exceed the maximum_packet_size
651 */
652 if (unlikely((*packet_size) + 3 > max_packet_size)) {
653 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
654 rc = -EINVAL;
655 goto out;
656 }
657
658 /* check for Tag 3 identifyer - one byte */
659 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
660 ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
661 ECRYPTFS_TAG_3_PACKET_TYPE);
662 rc = -EINVAL;
663 goto out;
664 }
665 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
666 * at end of function upon failure */
667 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800668 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700669 if (!auth_tok_list_item) {
670 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
671 rc = -ENOMEM;
672 goto out;
673 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700674 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
675
676 /* check for body size - one to two bytes */
677 rc = parse_packet_length(&data[(*packet_size)], &body_size,
678 &length_size);
679 if (rc) {
680 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
681 "rc = [%d]\n", rc);
682 goto out_free;
683 }
684 if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
685 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
686 body_size);
687 rc = -EINVAL;
688 goto out_free;
689 }
690 (*packet_size) += length_size;
691
692 /* now we know the length of the remainting Tag 3 packet size:
693 * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
694 * number of hash iterations
695 * ECRYPTFS_SALT_SIZE bytes for salt
696 * body_size bytes minus the stuff above is the encrypted key size
697 */
698 if (unlikely((*packet_size) + body_size > max_packet_size)) {
699 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
700 rc = -EINVAL;
701 goto out_free;
702 }
703
704 /* There are 5 characters of additional information in the
705 * packet */
706 (*new_auth_tok)->session_key.encrypted_key_size =
707 body_size - (0x05 + ECRYPTFS_SALT_SIZE);
708 ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
709 (*new_auth_tok)->session_key.encrypted_key_size);
710
711 /* Version 4 (from RFC2440) - one byte */
712 if (unlikely(data[(*packet_size)++] != 0x04)) {
713 ecryptfs_printk(KERN_DEBUG, "Unknown version number "
714 "[%d]\n", data[(*packet_size) - 1]);
715 rc = -EINVAL;
716 goto out_free;
717 }
718
719 /* cipher - one byte */
720 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
721 (u16)data[(*packet_size)]);
722 /* A little extra work to differentiate among the AES key
723 * sizes; see RFC2440 */
724 switch(data[(*packet_size)++]) {
725 case RFC2440_CIPHER_AES_192:
726 crypt_stat->key_size = 24;
727 break;
728 default:
729 crypt_stat->key_size =
730 (*new_auth_tok)->session_key.encrypted_key_size;
731 }
732 ecryptfs_init_crypt_ctx(crypt_stat);
733 /* S2K identifier 3 (from RFC2440) */
734 if (unlikely(data[(*packet_size)++] != 0x03)) {
735 ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
736 "supported\n");
737 rc = -ENOSYS;
738 goto out_free;
739 }
740
741 /* TODO: finish the hash mapping */
742 /* hash algorithm - one byte */
743 switch (data[(*packet_size)++]) {
744 case 0x01: /* See RFC2440 for these numbers and their mappings */
745 /* Choose MD5 */
746 /* salt - ECRYPTFS_SALT_SIZE bytes */
747 memcpy((*new_auth_tok)->token.password.salt,
748 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
749 (*packet_size) += ECRYPTFS_SALT_SIZE;
750
751 /* This conversion was taken straight from RFC2440 */
752 /* number of hash iterations - one byte */
753 (*new_auth_tok)->token.password.hash_iterations =
754 ((u32) 16 + (data[(*packet_size)] & 15))
755 << ((data[(*packet_size)] >> 4) + 6);
756 (*packet_size)++;
757
758 /* encrypted session key -
759 * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
760 memcpy((*new_auth_tok)->session_key.encrypted_key,
761 &data[(*packet_size)],
762 (*new_auth_tok)->session_key.encrypted_key_size);
763 (*packet_size) +=
764 (*new_auth_tok)->session_key.encrypted_key_size;
765 (*new_auth_tok)->session_key.flags &=
766 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
767 (*new_auth_tok)->session_key.flags |=
768 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
769 (*new_auth_tok)->token.password.hash_algo = 0x01;
770 break;
771 default:
772 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
773 "[%d]\n", data[(*packet_size) - 1]);
774 rc = -ENOSYS;
775 goto out_free;
776 }
777 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
778 /* TODO: Parametarize; we might actually want userspace to
779 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800780 (*new_auth_tok)->session_key.flags &=
781 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
782 (*new_auth_tok)->session_key.flags &=
783 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700784 list_add(&auth_tok_list_item->list, auth_tok_list);
785 goto out;
786out_free:
787 (*new_auth_tok) = NULL;
788 memset(auth_tok_list_item, 0,
789 sizeof(struct ecryptfs_auth_tok_list_item));
790 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
791 auth_tok_list_item);
792out:
793 if (rc)
794 (*packet_size) = 0;
795 return rc;
796}
797
798/**
799 * parse_tag_11_packet
800 * @data: The raw bytes of the packet
801 * @contents: This function writes the data contents of the literal
802 * packet into this memory location
803 * @max_contents_bytes: The maximum number of bytes that this function
804 * is allowed to write into contents
805 * @tag_11_contents_size: This function writes the size of the parsed
806 * contents into this memory location; zero on
807 * error
808 * @packet_size: This function writes the size of the parsed packet
809 * into this memory location; zero on error
810 * @max_packet_size: maximum number of bytes to parse
811 *
812 * Returns zero on success; non-zero on error.
813 */
814static int
815parse_tag_11_packet(unsigned char *data, unsigned char *contents,
816 size_t max_contents_bytes, size_t *tag_11_contents_size,
817 size_t *packet_size, size_t max_packet_size)
818{
Michael Halcrow237fead2006-10-04 02:16:22 -0700819 size_t body_size;
820 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800821 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700822
823 (*packet_size) = 0;
824 (*tag_11_contents_size) = 0;
825
826 /* check that:
827 * one byte for the Tag 11 ID flag
828 * two bytes for the Tag 11 length
829 * do not exceed the maximum_packet_size
830 */
831 if (unlikely((*packet_size) + 3 > max_packet_size)) {
832 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
833 rc = -EINVAL;
834 goto out;
835 }
836
837 /* check for Tag 11 identifyer - one byte */
838 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
839 ecryptfs_printk(KERN_WARNING,
840 "Invalid tag 11 packet format\n");
841 rc = -EINVAL;
842 goto out;
843 }
844
845 /* get Tag 11 content length - one or two bytes */
846 rc = parse_packet_length(&data[(*packet_size)], &body_size,
847 &length_size);
848 if (rc) {
849 ecryptfs_printk(KERN_WARNING,
850 "Invalid tag 11 packet format\n");
851 goto out;
852 }
853 (*packet_size) += length_size;
854
855 if (body_size < 13) {
856 ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
857 body_size);
858 rc = -EINVAL;
859 goto out;
860 }
861 /* We have 13 bytes of surrounding packet values */
862 (*tag_11_contents_size) = (body_size - 13);
863
864 /* now we know the length of the remainting Tag 11 packet size:
865 * 14 fix bytes for: special flag one, special flag two,
866 * 12 skipped bytes
867 * body_size bytes minus the stuff above is the Tag 11 content
868 */
869 /* FIXME why is the body size one byte smaller than the actual
870 * size of the body?
871 * this seems to be an error here as well as in
872 * write_tag_11_packet() */
873 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
874 ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
875 rc = -EINVAL;
876 goto out;
877 }
878
879 /* special flag one - one byte */
880 if (data[(*packet_size)++] != 0x62) {
881 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
882 rc = -EINVAL;
883 goto out;
884 }
885
886 /* special flag two - one byte */
887 if (data[(*packet_size)++] != 0x08) {
888 ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
889 rc = -EINVAL;
890 goto out;
891 }
892
893 /* skip the next 12 bytes */
894 (*packet_size) += 12; /* We don't care about the filename or
895 * the timestamp */
896
897 /* get the Tag 11 contents - tag_11_contents_size bytes */
898 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
899 (*packet_size) += (*tag_11_contents_size);
900
901out:
902 if (rc) {
903 (*packet_size) = 0;
904 (*tag_11_contents_size) = 0;
905 }
906 return rc;
907}
908
Michael Halcrowf4aad162007-10-16 01:27:53 -0700909static int
910ecryptfs_find_global_auth_tok_for_sig(
911 struct ecryptfs_global_auth_tok **global_auth_tok,
912 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
913{
914 struct ecryptfs_global_auth_tok *walker;
915 int rc = 0;
916
917 (*global_auth_tok) = NULL;
918 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
919 list_for_each_entry(walker,
920 &mount_crypt_stat->global_auth_tok_list,
921 mount_crypt_stat_list) {
922 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
923 (*global_auth_tok) = walker;
924 goto out;
925 }
926 }
927 rc = -EINVAL;
928out:
929 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
930 return rc;
931}
932
Michael Halcrow237fead2006-10-04 02:16:22 -0700933/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700934 * ecryptfs_verify_version
935 * @version: The version number to confirm
936 *
937 * Returns zero on good version; non-zero otherwise
938 */
939static int ecryptfs_verify_version(u16 version)
940{
941 int rc = 0;
942 unsigned char major;
943 unsigned char minor;
944
945 major = ((version >> 8) & 0xFF);
946 minor = (version & 0xFF);
947 if (major != ECRYPTFS_VERSION_MAJOR) {
948 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
949 "Expected [%d]; got [%d]\n",
950 ECRYPTFS_VERSION_MAJOR, major);
951 rc = -EINVAL;
952 goto out;
953 }
954 if (minor != ECRYPTFS_VERSION_MINOR) {
955 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
956 "Expected [%d]; got [%d]\n",
957 ECRYPTFS_VERSION_MINOR, minor);
958 rc = -EINVAL;
959 goto out;
960 }
961out:
962 return rc;
963}
964
965int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
966 struct ecryptfs_auth_tok **auth_tok,
967 char *sig)
968{
969 int rc = 0;
970
971 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
972 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
973 printk(KERN_ERR "Could not find key with description: [%s]\n",
974 sig);
975 process_request_key_err(PTR_ERR(*auth_tok_key));
976 rc = -EINVAL;
977 goto out;
978 }
979 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
980 if (ecryptfs_verify_version((*auth_tok)->version)) {
981 printk(KERN_ERR
982 "Data structure version mismatch. "
983 "Userspace tools must match eCryptfs "
984 "kernel module with major version [%d] "
985 "and minor version [%d]\n",
986 ECRYPTFS_VERSION_MAJOR,
987 ECRYPTFS_VERSION_MINOR);
988 rc = -EINVAL;
989 goto out;
990 }
991 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
992 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
993 printk(KERN_ERR "Invalid auth_tok structure "
994 "returned from key query\n");
995 rc = -EINVAL;
996 goto out;
997 }
998out:
999 return rc;
1000}
1001
1002/**
1003 * ecryptfs_find_auth_tok_for_sig
1004 * @auth_tok: Set to the matching auth_tok; NULL if not found
1005 * @crypt_stat: inode crypt_stat crypto context
1006 * @sig: Sig of auth_tok to find
1007 *
1008 * For now, this function simply looks at the registered auth_tok's
1009 * linked off the mount_crypt_stat, so all the auth_toks that can be
1010 * used must be registered at mount time. This function could
1011 * potentially try a lot harder to find auth_tok's (e.g., by calling
1012 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
1013 * that static registration of auth_tok's will no longer be necessary.
1014 *
1015 * Returns zero on no error; non-zero on error
1016 */
1017static int
1018ecryptfs_find_auth_tok_for_sig(
1019 struct ecryptfs_auth_tok **auth_tok,
1020 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1021{
1022 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1023 crypt_stat->mount_crypt_stat;
1024 struct ecryptfs_global_auth_tok *global_auth_tok;
1025 int rc = 0;
1026
1027 (*auth_tok) = NULL;
1028 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1029 mount_crypt_stat, sig)) {
1030 struct key *auth_tok_key;
1031
1032 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1033 sig);
1034 } else
1035 (*auth_tok) = global_auth_tok->global_auth_tok;
1036 return rc;
1037}
1038
1039/**
1040 * decrypt_passphrase_encrypted_session_key - Decrypt the session key
1041 * with the given auth_tok.
Michael Halcrow237fead2006-10-04 02:16:22 -07001042 *
1043 * Returns Zero on success; non-zero error otherwise.
1044 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001045static int
1046decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1047 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001048{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001049 struct scatterlist dst_sg;
1050 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001051 struct mutex *tfm_mutex = NULL;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001052 struct blkcipher_desc desc = {
1053 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1054 };
1055 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001056
Michael Halcrowf4aad162007-10-16 01:27:53 -07001057 if (unlikely(ecryptfs_verbosity > 0)) {
1058 ecryptfs_printk(
1059 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1060 auth_tok->token.password.session_key_encryption_key_bytes);
1061 ecryptfs_dump_hex(
1062 auth_tok->token.password.session_key_encryption_key,
1063 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001064 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001065 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1066 crypt_stat->cipher);
1067 if (unlikely(rc)) {
1068 printk(KERN_ERR "Internal error whilst attempting to get "
1069 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1070 crypt_stat->cipher, rc);
1071 goto out;
1072 }
1073 if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1074 auth_tok->session_key.encrypted_key_size,
1075 &src_sg, 1)) != 1) {
1076 printk(KERN_ERR "Internal error whilst attempting to convert "
1077 "auth_tok->session_key.encrypted_key to scatterlist; "
1078 "expected rc = 1; got rc = [%d]. "
1079 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1080 auth_tok->session_key.encrypted_key_size);
1081 goto out;
1082 }
1083 auth_tok->session_key.decrypted_key_size =
1084 auth_tok->session_key.encrypted_key_size;
1085 if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1086 auth_tok->session_key.decrypted_key_size,
1087 &dst_sg, 1)) != 1) {
1088 printk(KERN_ERR "Internal error whilst attempting to convert "
1089 "auth_tok->session_key.decrypted_key to scatterlist; "
1090 "expected rc = 1; got rc = [%d]\n", rc);
1091 goto out;
1092 }
1093 mutex_lock(tfm_mutex);
1094 rc = crypto_blkcipher_setkey(
1095 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1096 crypt_stat->key_size);
1097 if (unlikely(rc < 0)) {
1098 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001099 printk(KERN_ERR "Error setting key for crypto context\n");
1100 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001101 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001102 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001103 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001104 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001105 mutex_unlock(tfm_mutex);
1106 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001107 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001108 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001109 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001110 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1111 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1112 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001113 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001114 if (unlikely(ecryptfs_verbosity > 0)) {
1115 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1116 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001117 ecryptfs_dump_hex(crypt_stat->key,
1118 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001119 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001120out:
1121 return rc;
1122}
1123
Michael Halcrowf4aad162007-10-16 01:27:53 -07001124int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1125{
1126 int rc = 0;
1127
1128 (*sig) = NULL;
1129 switch (auth_tok->token_type) {
1130 case ECRYPTFS_PASSWORD:
1131 (*sig) = auth_tok->token.password.signature;
1132 break;
1133 case ECRYPTFS_PRIVATE_KEY:
1134 (*sig) = auth_tok->token.private_key.signature;
1135 break;
1136 default:
1137 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1138 auth_tok->token_type);
1139 rc = -EINVAL;
1140 }
1141 return rc;
1142}
1143
Michael Halcrow237fead2006-10-04 02:16:22 -07001144/**
1145 * ecryptfs_parse_packet_set
1146 * @dest: The header page in memory
1147 * @version: Version of file format, to guide parsing behavior
1148 *
1149 * Get crypt_stat to have the file's session key if the requisite key
1150 * is available to decrypt the session key.
1151 *
1152 * Returns Zero if a valid authentication token was retrieved and
1153 * processed; negative value for file not encrypted or for error
1154 * conditions.
1155 */
1156int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1157 unsigned char *src,
1158 struct dentry *ecryptfs_dentry)
1159{
1160 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001161 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001162 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001163 struct list_head auth_tok_list;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001164 struct ecryptfs_auth_tok *matching_auth_tok = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001165 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001166 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001167 size_t packet_size;
1168 struct ecryptfs_auth_tok *new_auth_tok;
1169 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001170 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001171 size_t tag_11_contents_size;
1172 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001173 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001174
1175 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001176 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001177 * added the our &auth_tok_list */
1178 next_packet_is_auth_tok_packet = 1;
1179 while (next_packet_is_auth_tok_packet) {
1180 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1181
1182 switch (src[i]) {
1183 case ECRYPTFS_TAG_3_PACKET_TYPE:
1184 rc = parse_tag_3_packet(crypt_stat,
1185 (unsigned char *)&src[i],
1186 &auth_tok_list, &new_auth_tok,
1187 &packet_size, max_packet_size);
1188 if (rc) {
1189 ecryptfs_printk(KERN_ERR, "Error parsing "
1190 "tag 3 packet\n");
1191 rc = -EIO;
1192 goto out_wipe_list;
1193 }
1194 i += packet_size;
1195 rc = parse_tag_11_packet((unsigned char *)&src[i],
1196 sig_tmp_space,
1197 ECRYPTFS_SIG_SIZE,
1198 &tag_11_contents_size,
1199 &tag_11_packet_size,
1200 max_packet_size);
1201 if (rc) {
1202 ecryptfs_printk(KERN_ERR, "No valid "
1203 "(ecryptfs-specific) literal "
1204 "packet containing "
1205 "authentication token "
1206 "signature found after "
1207 "tag 3 packet\n");
1208 rc = -EIO;
1209 goto out_wipe_list;
1210 }
1211 i += tag_11_packet_size;
1212 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1213 ecryptfs_printk(KERN_ERR, "Expected "
1214 "signature of size [%d]; "
1215 "read size [%d]\n",
1216 ECRYPTFS_SIG_SIZE,
1217 tag_11_contents_size);
1218 rc = -EIO;
1219 goto out_wipe_list;
1220 }
1221 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1222 sig_tmp_space, tag_11_contents_size);
1223 new_auth_tok->token.password.signature[
1224 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001225 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001226 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001227 case ECRYPTFS_TAG_1_PACKET_TYPE:
1228 rc = parse_tag_1_packet(crypt_stat,
1229 (unsigned char *)&src[i],
1230 &auth_tok_list, &new_auth_tok,
1231 &packet_size, max_packet_size);
1232 if (rc) {
1233 ecryptfs_printk(KERN_ERR, "Error parsing "
1234 "tag 1 packet\n");
1235 rc = -EIO;
1236 goto out_wipe_list;
1237 }
1238 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001239 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001240 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001241 case ECRYPTFS_TAG_11_PACKET_TYPE:
1242 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1243 "(Tag 11 not allowed by itself)\n");
1244 rc = -EIO;
1245 goto out_wipe_list;
1246 break;
1247 default:
1248 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1249 "[%d] of the file header; hex value of "
1250 "character is [0x%.2x]\n", i, src[i]);
1251 next_packet_is_auth_tok_packet = 0;
1252 }
1253 }
1254 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001255 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1256 "eCryptfs file; this is not supported in this version "
1257 "of the eCryptfs kernel module\n");
1258 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001259 goto out;
1260 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001261 /* auth_tok_list contains the set of authentication tokens
1262 * parsed from the metadata. We need to find a matching
1263 * authentication token that has the secret component(s)
1264 * necessary to decrypt the EFEK in the auth_tok parsed from
1265 * the metadata. There may be several potential matches, but
1266 * just one will be sufficient to decrypt to get the FEK. */
1267find_next_matching_auth_tok:
1268 found_auth_tok = 0;
1269 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001270 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1271 if (unlikely(ecryptfs_verbosity > 0)) {
1272 ecryptfs_printk(KERN_DEBUG,
1273 "Considering cadidate auth tok:\n");
1274 ecryptfs_dump_auth_tok(candidate_auth_tok);
1275 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001276 if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1277 candidate_auth_tok))) {
1278 printk(KERN_ERR
1279 "Unrecognized candidate auth tok type: [%d]\n",
1280 candidate_auth_tok->token_type);
1281 rc = -EINVAL;
1282 goto out_wipe_list;
1283 }
1284 if ((rc = ecryptfs_find_auth_tok_for_sig(
1285 &matching_auth_tok, crypt_stat,
1286 candidate_auth_tok_sig)))
1287 rc = 0;
1288 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001289 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001290 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001291 }
1292 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001293 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001294 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1295 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001296 rc = -EIO;
1297 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001298 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001299found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001300 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001301 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001302 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001303 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001304 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001305 crypt_stat);
1306 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001307 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001308 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001309 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001310 rc = decrypt_passphrase_encrypted_session_key(
1311 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001312 }
1313 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001314 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1315
1316 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1317 "session key for authentication token with sig "
1318 "[%.*s]; rc = [%d]. Removing auth tok "
1319 "candidate from the list and searching for "
1320 "the next match.\n", candidate_auth_tok_sig,
1321 ECRYPTFS_SIG_SIZE_HEX, rc);
1322 list_for_each_entry_safe(auth_tok_list_item,
1323 auth_tok_list_item_tmp,
1324 &auth_tok_list, list) {
1325 if (candidate_auth_tok
1326 == &auth_tok_list_item->auth_tok) {
1327 list_del(&auth_tok_list_item->list);
1328 kmem_cache_free(
1329 ecryptfs_auth_tok_list_item_cache,
1330 auth_tok_list_item);
1331 goto find_next_matching_auth_tok;
1332 }
1333 }
1334 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001335 }
1336 rc = ecryptfs_compute_root_iv(crypt_stat);
1337 if (rc) {
1338 ecryptfs_printk(KERN_ERR, "Error computing "
1339 "the root IV\n");
1340 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001341 }
1342 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1343 if (rc) {
1344 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1345 "context for cipher [%s]; rc = [%d]\n",
1346 crypt_stat->cipher, rc);
1347 }
1348out_wipe_list:
1349 wipe_auth_tok_list(&auth_tok_list);
1350out:
1351 return rc;
1352}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001353
Michael Halcrowdddfa462007-02-12 00:53:44 -08001354static int
1355pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1356 struct ecryptfs_crypt_stat *crypt_stat,
1357 struct ecryptfs_key_record *key_rec)
1358{
1359 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1360 char *netlink_payload;
1361 size_t netlink_payload_length;
1362 struct ecryptfs_message *msg;
1363 int rc;
1364
1365 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1366 ecryptfs_code_for_cipher_string(crypt_stat),
1367 crypt_stat, &netlink_payload,
1368 &netlink_payload_length);
1369 if (rc) {
1370 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1371 goto out;
1372 }
1373 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1374 netlink_payload_length, &msg_ctx);
1375 if (rc) {
1376 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1377 goto out;
1378 }
1379 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1380 if (rc) {
1381 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1382 "from the user space daemon\n");
1383 rc = -EIO;
1384 goto out;
1385 }
1386 rc = parse_tag_67_packet(key_rec, msg);
1387 if (rc)
1388 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1389 kfree(msg);
1390out:
1391 if (netlink_payload)
1392 kfree(netlink_payload);
1393 return rc;
1394}
1395/**
1396 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1397 * @dest: Buffer into which to write the packet
1398 * @max: Maximum number of bytes that can be writtn
1399 * @packet_size: This function will write the number of bytes that end
1400 * up constituting the packet; set to zero on error
1401 *
1402 * Returns zero on success; non-zero on error.
1403 */
1404static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001405write_tag_1_packet(char *dest, size_t *remaining_bytes,
1406 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001407 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001408 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1409{
1410 size_t i;
1411 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001412 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001413 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001414 int rc = 0;
1415
1416 (*packet_size) = 0;
1417 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1418 ECRYPTFS_SIG_SIZE);
1419 encrypted_session_key_valid = 0;
1420 for (i = 0; i < crypt_stat->key_size; i++)
1421 encrypted_session_key_valid |=
1422 auth_tok->session_key.encrypted_key[i];
1423 if (encrypted_session_key_valid) {
1424 memcpy(key_rec->enc_key,
1425 auth_tok->session_key.encrypted_key,
1426 auth_tok->session_key.encrypted_key_size);
1427 goto encrypted_session_key_set;
1428 }
1429 if (auth_tok->session_key.encrypted_key_size == 0)
1430 auth_tok->session_key.encrypted_key_size =
1431 auth_tok->token.private_key.key_size;
1432 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1433 if (rc) {
1434 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1435 "via a pki");
1436 goto out;
1437 }
1438 if (ecryptfs_verbosity > 0) {
1439 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1440 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1441 }
1442encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001443 /* This format is inspired by OpenPGP; see RFC 2440
1444 * packet tag 1 */
1445 max_packet_size = (1 /* Tag 1 identifier */
1446 + 3 /* Max Tag 1 packet size */
1447 + 1 /* Version */
1448 + ECRYPTFS_SIG_SIZE /* Key identifier */
1449 + 1 /* Cipher identifier */
1450 + key_rec->enc_key_size); /* Encrypted key size */
1451 if (max_packet_size > (*remaining_bytes)) {
1452 printk(KERN_ERR "Packet length larger than maximum allowable; "
1453 "need up to [%d] bytes, but there are only [%d] "
1454 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001455 rc = -EINVAL;
1456 goto out;
1457 }
1458 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001459 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001460 &packet_size_length);
1461 if (rc) {
1462 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1463 "header; cannot generate packet length\n");
1464 goto out;
1465 }
1466 (*packet_size) += packet_size_length;
1467 dest[(*packet_size)++] = 0x03; /* version 3 */
1468 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1469 (*packet_size) += ECRYPTFS_SIG_SIZE;
1470 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1471 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1472 key_rec->enc_key_size);
1473 (*packet_size) += key_rec->enc_key_size;
1474out:
1475 if (rc)
1476 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001477 else
1478 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001479 return rc;
1480}
Michael Halcrow237fead2006-10-04 02:16:22 -07001481
1482/**
1483 * write_tag_11_packet
1484 * @dest: Target into which Tag 11 packet is to be written
1485 * @max: Maximum packet length
1486 * @contents: Byte array of contents to copy in
1487 * @contents_length: Number of bytes in contents
1488 * @packet_length: Length of the Tag 11 packet written; zero on error
1489 *
1490 * Returns zero on success; non-zero on error.
1491 */
1492static int
1493write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
1494 size_t *packet_length)
1495{
Michael Halcrow237fead2006-10-04 02:16:22 -07001496 size_t packet_size_length;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001497 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001498
1499 (*packet_length) = 0;
1500 if ((13 + contents_length) > max) {
1501 rc = -EINVAL;
1502 ecryptfs_printk(KERN_ERR, "Packet length larger than "
1503 "maximum allowable\n");
1504 goto out;
1505 }
1506 /* General packet header */
1507 /* Packet tag */
1508 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
1509 /* Packet length */
1510 rc = write_packet_length(&dest[(*packet_length)],
1511 (13 + contents_length), &packet_size_length);
1512 if (rc) {
1513 ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
1514 "header; cannot generate packet length\n");
1515 goto out;
1516 }
1517 (*packet_length) += packet_size_length;
1518 /* Tag 11 specific */
1519 /* One-octet field that describes how the data is formatted */
1520 dest[(*packet_length)++] = 0x62; /* binary data */
1521 /* One-octet filename length followed by filename */
1522 dest[(*packet_length)++] = 8;
1523 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1524 (*packet_length) += 8;
1525 /* Four-octet number indicating modification date */
1526 memset(&dest[(*packet_length)], 0x00, 4);
1527 (*packet_length) += 4;
1528 /* Remainder is literal data */
1529 memcpy(&dest[(*packet_length)], contents, contents_length);
1530 (*packet_length) += contents_length;
1531 out:
1532 if (rc)
1533 (*packet_length) = 0;
1534 return rc;
1535}
1536
1537/**
1538 * write_tag_3_packet
1539 * @dest: Buffer into which to write the packet
1540 * @max: Maximum number of bytes that can be written
1541 * @auth_tok: Authentication token
1542 * @crypt_stat: The cryptographic context
1543 * @key_rec: encrypted key
1544 * @packet_size: This function will write the number of bytes that end
1545 * up constituting the packet; set to zero on error
1546 *
1547 * Returns zero on success; non-zero on error.
1548 */
1549static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001550write_tag_3_packet(char *dest, size_t *remaining_bytes,
1551 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001552 struct ecryptfs_crypt_stat *crypt_stat,
1553 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1554{
Michael Halcrow237fead2006-10-04 02:16:22 -07001555 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001556 size_t encrypted_session_key_valid = 0;
1557 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001558 struct scatterlist dst_sg;
1559 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001560 struct mutex *tfm_mutex = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001561 size_t cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001562 size_t packet_size_length;
1563 size_t max_packet_size;
1564 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1565 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001566 struct blkcipher_desc desc = {
1567 .tfm = NULL,
1568 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1569 };
1570 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001571
1572 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001573 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001574 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001575 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1576 crypt_stat->cipher);
1577 if (unlikely(rc)) {
1578 printk(KERN_ERR "Internal error whilst attempting to get "
1579 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1580 crypt_stat->cipher, rc);
1581 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001582 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001583 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1584 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1585
1586 printk(KERN_WARNING "No key size specified at mount; "
1587 "defaulting to [%d]\n", alg->max_keysize);
1588 mount_crypt_stat->global_default_cipher_key_size =
1589 alg->max_keysize;
1590 }
1591 if (crypt_stat->key_size == 0)
1592 crypt_stat->key_size =
1593 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001594 if (auth_tok->session_key.encrypted_key_size == 0)
1595 auth_tok->session_key.encrypted_key_size =
1596 crypt_stat->key_size;
1597 if (crypt_stat->key_size == 24
1598 && strcmp("aes", crypt_stat->cipher) == 0) {
1599 memset((crypt_stat->key + 24), 0, 8);
1600 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001601 } else
1602 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001603 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001604 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001605 encrypted_session_key_valid = 0;
1606 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1607 encrypted_session_key_valid |=
1608 auth_tok->session_key.encrypted_key[i];
1609 if (encrypted_session_key_valid) {
1610 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1611 "using auth_tok->session_key.encrypted_key, "
1612 "where key_rec->enc_key_size = [%d]\n",
1613 key_rec->enc_key_size);
1614 memcpy(key_rec->enc_key,
1615 auth_tok->session_key.encrypted_key,
1616 key_rec->enc_key_size);
1617 goto encrypted_session_key_set;
1618 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001619 if (auth_tok->token.password.flags &
1620 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001621 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1622 "session key encryption key of size [%d]\n",
1623 auth_tok->token.password.
1624 session_key_encryption_key_bytes);
1625 memcpy(session_key_encryption_key,
1626 auth_tok->token.password.session_key_encryption_key,
1627 crypt_stat->key_size);
1628 ecryptfs_printk(KERN_DEBUG,
1629 "Cached session key " "encryption key: \n");
1630 if (ecryptfs_verbosity > 0)
1631 ecryptfs_dump_hex(session_key_encryption_key, 16);
1632 }
1633 if (unlikely(ecryptfs_verbosity > 0)) {
1634 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1635 ecryptfs_dump_hex(session_key_encryption_key, 16);
1636 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001637 if ((rc = virt_to_scatterlist(crypt_stat->key,
1638 key_rec->enc_key_size, &src_sg, 1))
1639 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001640 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001641 "for crypt_stat session key; expected rc = 1; "
1642 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1643 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001644 rc = -ENOMEM;
1645 goto out;
1646 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001647 if ((rc = virt_to_scatterlist(key_rec->enc_key,
1648 key_rec->enc_key_size, &dst_sg, 1))
1649 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001650 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001651 "for crypt_stat encrypted session key; "
1652 "expected rc = 1; got rc = [%d]. "
1653 "key_rec->enc_key_size = [%d]\n", rc,
1654 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001655 rc = -ENOMEM;
1656 goto out;
1657 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001658 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001659 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1660 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001661 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001662 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001663 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001664 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001665 goto out;
1666 }
1667 rc = 0;
1668 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1669 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001670 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001671 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001672 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001673 if (rc) {
1674 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1675 goto out;
1676 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001677 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001678 if (ecryptfs_verbosity > 0) {
1679 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1680 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001681 ecryptfs_dump_hex(key_rec->enc_key,
1682 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001683 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001684encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001685 /* This format is inspired by OpenPGP; see RFC 2440
1686 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001687 max_packet_size = (1 /* Tag 3 identifier */
1688 + 3 /* Max Tag 3 packet size */
1689 + 1 /* Version */
1690 + 1 /* Cipher code */
1691 + 1 /* S2K specifier */
1692 + 1 /* Hash identifier */
1693 + ECRYPTFS_SALT_SIZE /* Salt */
1694 + 1 /* Hash iterations */
1695 + key_rec->enc_key_size); /* Encrypted key size */
1696 if (max_packet_size > (*remaining_bytes)) {
1697 printk(KERN_ERR "Packet too large; need up to [%d] bytes, but "
1698 "there are only [%d] available\n", max_packet_size,
1699 (*remaining_bytes));
1700 rc = -EINVAL;
1701 goto out;
1702 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001703 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001704 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1705 * to get the number of octets in the actual Tag 3 packet */
1706 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrow237fead2006-10-04 02:16:22 -07001707 &packet_size_length);
1708 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001709 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1710 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001711 goto out;
1712 }
1713 (*packet_size) += packet_size_length;
1714 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001715 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1716 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001717 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1718 if (cipher_code == 0) {
1719 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1720 "cipher [%s]\n", crypt_stat->cipher);
1721 rc = -EINVAL;
1722 goto out;
1723 }
1724 dest[(*packet_size)++] = cipher_code;
1725 dest[(*packet_size)++] = 0x03; /* S2K */
1726 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1727 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1728 ECRYPTFS_SALT_SIZE);
1729 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1730 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001731 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1732 key_rec->enc_key_size);
1733 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001734out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001735 if (rc)
1736 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001737 else
1738 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001739 return rc;
1740}
1741
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001742struct kmem_cache *ecryptfs_key_record_cache;
1743
Michael Halcrow237fead2006-10-04 02:16:22 -07001744/**
1745 * ecryptfs_generate_key_packet_set
1746 * @dest: Virtual address from which to write the key record set
1747 * @crypt_stat: The cryptographic context from which the
1748 * authentication tokens will be retrieved
1749 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1750 * for the global parameters
1751 * @len: The amount written
1752 * @max: The maximum amount of data allowed to be written
1753 *
1754 * Generates a key packet set and writes it to the virtual address
1755 * passed in.
1756 *
1757 * Returns zero on success; non-zero on error.
1758 */
1759int
1760ecryptfs_generate_key_packet_set(char *dest_base,
1761 struct ecryptfs_crypt_stat *crypt_stat,
1762 struct dentry *ecryptfs_dentry, size_t *len,
1763 size_t max)
1764{
Michael Halcrow237fead2006-10-04 02:16:22 -07001765 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001766 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001767 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1768 &ecryptfs_superblock_to_private(
1769 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1770 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001771 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001772 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001773 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001774
1775 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001776 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001777 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1778 if (!key_rec) {
1779 rc = -ENOMEM;
1780 goto out;
1781 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001782 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1783 crypt_stat_list) {
1784 memset(key_rec, 0, sizeof(*key_rec));
1785 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1786 mount_crypt_stat,
1787 key_sig->keysig);
1788 if (rc) {
1789 printk(KERN_ERR "Error attempting to get the global "
1790 "auth_tok; rc = [%d]\n", rc);
1791 goto out_free;
1792 }
1793 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1794 printk(KERN_WARNING
1795 "Skipping invalid auth tok with sig = [%s]\n",
1796 global_auth_tok->sig);
1797 continue;
1798 }
1799 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001800 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1801 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001802 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001803 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001804 &written);
1805 if (rc) {
1806 ecryptfs_printk(KERN_WARNING, "Error "
1807 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001808 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001809 }
1810 (*len) += written;
1811 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001812 rc = write_tag_11_packet((dest_base + (*len)), &max,
1813 key_rec->sig,
1814 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001815 if (rc) {
1816 ecryptfs_printk(KERN_ERR, "Error writing "
1817 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001818 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001819 }
1820 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001821 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1822 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001823 &max, auth_tok,
1824 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001825 if (rc) {
1826 ecryptfs_printk(KERN_WARNING, "Error "
1827 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001828 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001829 }
1830 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001831 } else {
1832 ecryptfs_printk(KERN_WARNING, "Unsupported "
1833 "authentication token type\n");
1834 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001835 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001836 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001837 }
1838 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001839 dest_base[(*len)] = 0x00;
1840 } else {
1841 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1842 rc = -EIO;
1843 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001844out_free:
1845 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001846out:
1847 if (rc)
1848 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001849 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001850 return rc;
1851}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001852
1853struct kmem_cache *ecryptfs_key_sig_cache;
1854
1855int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1856{
1857 struct ecryptfs_key_sig *new_key_sig;
1858 int rc = 0;
1859
1860 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1861 if (!new_key_sig) {
1862 rc = -ENOMEM;
1863 printk(KERN_ERR
1864 "Error allocating from ecryptfs_key_sig_cache\n");
1865 goto out;
1866 }
1867 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1868 mutex_lock(&crypt_stat->keysig_list_mutex);
1869 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1870 mutex_unlock(&crypt_stat->keysig_list_mutex);
1871out:
1872 return rc;
1873}
1874
1875struct kmem_cache *ecryptfs_global_auth_tok_cache;
1876
1877int
1878ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1879 char *sig)
1880{
1881 struct ecryptfs_global_auth_tok *new_auth_tok;
1882 int rc = 0;
1883
1884 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1885 GFP_KERNEL);
1886 if (!new_auth_tok) {
1887 rc = -ENOMEM;
1888 printk(KERN_ERR "Error allocating from "
1889 "ecryptfs_global_auth_tok_cache\n");
1890 goto out;
1891 }
1892 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1893 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1894 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1895 list_add(&new_auth_tok->mount_crypt_stat_list,
1896 &mount_crypt_stat->global_auth_tok_list);
1897 mount_crypt_stat->num_global_auth_toks++;
1898 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1899out:
1900 return rc;
1901}
1902