blob: a6cbfc16d8a43522314872a87b14c283bf4cc036 [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 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -070074 * Returns zero on success; non-zero on error
Michael Halcrow237fead2006-10-04 02:16:22 -070075 */
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
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700109 * @dest: The byte array target into which to write the length. Must
110 * have at least 5 bytes allocated.
Michael Halcrow237fead2006-10-04 02:16:22 -0700111 * @size: The length to write.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700112 * @packet_size_length: The number of bytes used to encode the packet
113 * length is written to this address.
Michael Halcrow237fead2006-10-04 02:16:22 -0700114 *
115 * Returns zero on success; non-zero on error.
116 */
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/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700400 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
401 * @auth_tok: The key authentication token used to decrypt the session key
402 * @crypt_stat: The cryptographic context
Michael Halcrowdddfa462007-02-12 00:53:44 -0800403 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700404 * Returns zero on success; non-zero error otherwise.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800405 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700406static int
407decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
408 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -0800409{
410 u16 cipher_code = 0;
411 struct ecryptfs_msg_ctx *msg_ctx;
412 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700413 char *auth_tok_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800414 char *netlink_message;
415 size_t netlink_message_length;
416 int rc;
417
Michael Halcrowf4aad162007-10-16 01:27:53 -0700418 if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) {
419 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
420 auth_tok->token_type);
421 goto out;
422 }
423 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800424 &netlink_message, &netlink_message_length);
425 if (rc) {
426 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
427 goto out;
428 }
429 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
430 netlink_message_length, &msg_ctx);
431 if (rc) {
432 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
433 goto out;
434 }
435 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
436 if (rc) {
437 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
438 "from the user space daemon\n");
439 rc = -EIO;
440 goto out;
441 }
442 rc = parse_tag_65_packet(&(auth_tok->session_key),
443 &cipher_code, msg);
444 if (rc) {
445 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
446 rc);
447 goto out;
448 }
449 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
450 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
451 auth_tok->session_key.decrypted_key_size);
452 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
453 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
454 if (rc) {
455 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
456 cipher_code)
457 goto out;
458 }
459 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
460 if (ecryptfs_verbosity > 0) {
461 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
462 ecryptfs_dump_hex(crypt_stat->key,
463 crypt_stat->key_size);
464 }
465out:
466 if (msg)
467 kfree(msg);
468 return rc;
469}
470
471static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
472{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800473 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700474 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800475
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700476 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
477 auth_tok_list_head, list) {
478 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800479 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
480 auth_tok_list_item);
481 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800482}
483
484struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
485
Michael Halcrowdddfa462007-02-12 00:53:44 -0800486/**
487 * parse_tag_1_packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700488 * @crypt_stat: The cryptographic context to modify based on packet contents
Michael Halcrowdddfa462007-02-12 00:53:44 -0800489 * @data: The raw bytes of the packet.
490 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700491 * a new authentication token will be placed at the
492 * end of this list for this packet.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800493 * @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.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700499 * @max_packet_size: The maximum allowable packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800500 *
501 * Returns zero on success; non-zero on error.
502 */
503static int
504parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
505 unsigned char *data, struct list_head *auth_tok_list,
506 struct ecryptfs_auth_tok **new_auth_tok,
507 size_t *packet_size, size_t max_packet_size)
508{
509 size_t body_size;
510 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
511 size_t length_size;
512 int rc = 0;
513
514 (*packet_size) = 0;
515 (*new_auth_tok) = NULL;
Michael Halcrow13218172007-10-16 01:27:56 -0700516 /**
517 * This format is inspired by OpenPGP; see RFC 2440
518 * packet tag 1
519 *
520 * Tag 1 identifier (1 byte)
521 * Max Tag 1 packet size (max 3 bytes)
522 * Version (1 byte)
523 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
524 * Cipher identifier (1 byte)
525 * Encrypted key size (arbitrary)
526 *
527 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800528 */
Michael Halcrow13218172007-10-16 01:27:56 -0700529 if (unlikely(max_packet_size < 12)) {
530 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800531 rc = -EINVAL;
532 goto out;
533 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800534 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow13218172007-10-16 01:27:56 -0700535 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
536 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800537 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 =
Michael Halcrow13218172007-10-16 01:27:56 -0700543 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
544 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800545 if (!auth_tok_list_item) {
Michael Halcrow13218172007-10-16 01:27:56 -0700546 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800547 rc = -ENOMEM;
548 goto out;
549 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800550 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow13218172007-10-16 01:27:56 -0700551 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
552 &length_size))) {
553 printk(KERN_WARNING "Error parsing packet length; "
554 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800555 goto out_free;
556 }
Michael Halcrow13218172007-10-16 01:27:56 -0700557 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700558 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800559 rc = -EINVAL;
560 goto out_free;
561 }
562 (*packet_size) += length_size;
563 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700564 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800565 rc = -EINVAL;
566 goto out_free;
567 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800568 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700569 printk(KERN_WARNING "Unknown version number [%d]\n",
570 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800571 rc = -EINVAL;
572 goto out_free;
573 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800574 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
575 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
576 *packet_size += ECRYPTFS_SIG_SIZE;
577 /* This byte is skipped because the kernel does not need to
578 * know which public key encryption algorithm was used */
579 (*packet_size)++;
580 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow13218172007-10-16 01:27:56 -0700581 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800582 if ((*new_auth_tok)->session_key.encrypted_key_size
583 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow13218172007-10-16 01:27:56 -0700584 printk(KERN_WARNING "Tag 1 packet contains key larger "
585 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800586 rc = -EINVAL;
587 goto out;
588 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800589 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow13218172007-10-16 01:27:56 -0700590 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800591 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
592 (*new_auth_tok)->session_key.flags &=
593 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
594 (*new_auth_tok)->session_key.flags |=
595 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
596 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow13218172007-10-16 01:27:56 -0700597 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800598 (*new_auth_tok)->session_key.flags &=
599 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
600 (*new_auth_tok)->session_key.flags &=
601 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800602 list_add(&auth_tok_list_item->list, auth_tok_list);
603 goto out;
604out_free:
605 (*new_auth_tok) = NULL;
606 memset(auth_tok_list_item, 0,
607 sizeof(struct ecryptfs_auth_tok_list_item));
608 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
609 auth_tok_list_item);
610out:
611 if (rc)
612 (*packet_size) = 0;
613 return rc;
614}
615
Michael Halcrow237fead2006-10-04 02:16:22 -0700616/**
617 * parse_tag_3_packet
618 * @crypt_stat: The cryptographic context to modify based on packet
619 * contents.
620 * @data: The raw bytes of the packet.
621 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
622 * a new authentication token will be placed at the end
623 * of this list for this packet.
624 * @new_auth_tok: Pointer to a pointer to memory that this function
625 * allocates; sets the memory address of the pointer to
626 * NULL on error. This object is added to the
627 * auth_tok_list.
628 * @packet_size: This function writes the size of the parsed packet
629 * into this memory location; zero on error.
630 * @max_packet_size: maximum number of bytes to parse
631 *
632 * Returns zero on success; non-zero on error.
633 */
634static int
635parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
636 unsigned char *data, struct list_head *auth_tok_list,
637 struct ecryptfs_auth_tok **new_auth_tok,
638 size_t *packet_size, size_t max_packet_size)
639{
Michael Halcrow237fead2006-10-04 02:16:22 -0700640 size_t body_size;
641 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
642 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800643 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700644
645 (*packet_size) = 0;
646 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700647 /**
648 *This format is inspired by OpenPGP; see RFC 2440
649 * packet tag 3
650 *
651 * Tag 3 identifier (1 byte)
652 * Max Tag 3 packet size (max 3 bytes)
653 * Version (1 byte)
654 * Cipher code (1 byte)
655 * S2K specifier (1 byte)
656 * Hash identifier (1 byte)
657 * Salt (ECRYPTFS_SALT_SIZE)
658 * Hash iterations (1 byte)
659 * Encrypted key (arbitrary)
660 *
661 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -0700662 */
Michael Halcrowc59becf2007-10-16 01:27:56 -0700663 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
664 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700665 rc = -EINVAL;
666 goto out;
667 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700668 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700669 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
670 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700671 rc = -EINVAL;
672 goto out;
673 }
674 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
675 * at end of function upon failure */
676 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800677 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700678 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700679 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700680 rc = -ENOMEM;
681 goto out;
682 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700683 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700684 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
685 &length_size))) {
686 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
687 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700688 goto out_free;
689 }
Michael Halcrowc59becf2007-10-16 01:27:56 -0700690 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700691 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700692 rc = -EINVAL;
693 goto out_free;
694 }
695 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700696 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700697 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700698 rc = -EINVAL;
699 goto out_free;
700 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700701 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -0700702 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Michael Halcrow237fead2006-10-04 02:16:22 -0700703 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700704 printk(KERN_WARNING "Unknown version number [%d]\n",
705 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -0700706 rc = -EINVAL;
707 goto out_free;
708 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700709 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
710 (u16)data[(*packet_size)]);
711 /* A little extra work to differentiate among the AES key
712 * sizes; see RFC2440 */
713 switch(data[(*packet_size)++]) {
714 case RFC2440_CIPHER_AES_192:
715 crypt_stat->key_size = 24;
716 break;
717 default:
718 crypt_stat->key_size =
719 (*new_auth_tok)->session_key.encrypted_key_size;
720 }
721 ecryptfs_init_crypt_ctx(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700723 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700724 rc = -ENOSYS;
725 goto out_free;
726 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700727 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -0700728 switch (data[(*packet_size)++]) {
729 case 0x01: /* See RFC2440 for these numbers and their mappings */
730 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700731 memcpy((*new_auth_tok)->token.password.salt,
732 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
733 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700734 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700735 (*new_auth_tok)->token.password.hash_iterations =
736 ((u32) 16 + (data[(*packet_size)] & 15))
737 << ((data[(*packet_size)] >> 4) + 6);
738 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700739 /* Friendly reminder:
740 * (*new_auth_tok)->session_key.encrypted_key_size =
741 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -0700742 memcpy((*new_auth_tok)->session_key.encrypted_key,
743 &data[(*packet_size)],
744 (*new_auth_tok)->session_key.encrypted_key_size);
745 (*packet_size) +=
746 (*new_auth_tok)->session_key.encrypted_key_size;
747 (*new_auth_tok)->session_key.flags &=
748 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
749 (*new_auth_tok)->session_key.flags |=
750 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700751 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 break;
753 default:
754 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
755 "[%d]\n", data[(*packet_size) - 1]);
756 rc = -ENOSYS;
757 goto out_free;
758 }
759 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
760 /* TODO: Parametarize; we might actually want userspace to
761 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800762 (*new_auth_tok)->session_key.flags &=
763 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
764 (*new_auth_tok)->session_key.flags &=
765 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700766 list_add(&auth_tok_list_item->list, auth_tok_list);
767 goto out;
768out_free:
769 (*new_auth_tok) = NULL;
770 memset(auth_tok_list_item, 0,
771 sizeof(struct ecryptfs_auth_tok_list_item));
772 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
773 auth_tok_list_item);
774out:
775 if (rc)
776 (*packet_size) = 0;
777 return rc;
778}
779
780/**
781 * parse_tag_11_packet
782 * @data: The raw bytes of the packet
783 * @contents: This function writes the data contents of the literal
784 * packet into this memory location
785 * @max_contents_bytes: The maximum number of bytes that this function
786 * is allowed to write into contents
787 * @tag_11_contents_size: This function writes the size of the parsed
788 * contents into this memory location; zero on
789 * error
790 * @packet_size: This function writes the size of the parsed packet
791 * into this memory location; zero on error
792 * @max_packet_size: maximum number of bytes to parse
793 *
794 * Returns zero on success; non-zero on error.
795 */
796static int
797parse_tag_11_packet(unsigned char *data, unsigned char *contents,
798 size_t max_contents_bytes, size_t *tag_11_contents_size,
799 size_t *packet_size, size_t max_packet_size)
800{
Michael Halcrow237fead2006-10-04 02:16:22 -0700801 size_t body_size;
802 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800803 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700804
805 (*packet_size) = 0;
806 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -0700807 /* This format is inspired by OpenPGP; see RFC 2440
808 * packet tag 11
809 *
810 * Tag 11 identifier (1 byte)
811 * Max Tag 11 packet size (max 3 bytes)
812 * Binary format specifier (1 byte)
813 * Filename length (1 byte)
814 * Filename ("_CONSOLE") (8 bytes)
815 * Modification date (4 bytes)
816 * Literal data (arbitrary)
817 *
818 * We need at least 16 bytes of data for the packet to even be
819 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -0700820 */
Michael Halcrowf6481042007-10-16 01:27:57 -0700821 if (max_packet_size < 16) {
822 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700823 rc = -EINVAL;
824 goto out;
825 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700826 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700827 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700828 rc = -EINVAL;
829 goto out;
830 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700831 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
832 &length_size))) {
833 printk(KERN_WARNING "Invalid tag 11 packet format\n");
834 goto out;
835 }
836 if (body_size < 14) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700837 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowf6481042007-10-16 01:27:57 -0700838 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700839 goto out;
840 }
841 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -0700842 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -0700843 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700844 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700845 rc = -EINVAL;
846 goto out;
847 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700848 if (data[(*packet_size)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700849 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700850 rc = -EINVAL;
851 goto out;
852 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700853 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700854 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700855 rc = -EINVAL;
856 goto out;
857 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700858 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -0700859 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
860 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700861out:
862 if (rc) {
863 (*packet_size) = 0;
864 (*tag_11_contents_size) = 0;
865 }
866 return rc;
867}
868
Michael Halcrowf4aad162007-10-16 01:27:53 -0700869static int
870ecryptfs_find_global_auth_tok_for_sig(
871 struct ecryptfs_global_auth_tok **global_auth_tok,
872 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
873{
874 struct ecryptfs_global_auth_tok *walker;
875 int rc = 0;
876
877 (*global_auth_tok) = NULL;
878 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
879 list_for_each_entry(walker,
880 &mount_crypt_stat->global_auth_tok_list,
881 mount_crypt_stat_list) {
882 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
883 (*global_auth_tok) = walker;
884 goto out;
885 }
886 }
887 rc = -EINVAL;
888out:
889 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
890 return rc;
891}
892
Michael Halcrow237fead2006-10-04 02:16:22 -0700893/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700894 * ecryptfs_verify_version
895 * @version: The version number to confirm
896 *
897 * Returns zero on good version; non-zero otherwise
898 */
899static int ecryptfs_verify_version(u16 version)
900{
901 int rc = 0;
902 unsigned char major;
903 unsigned char minor;
904
905 major = ((version >> 8) & 0xFF);
906 minor = (version & 0xFF);
907 if (major != ECRYPTFS_VERSION_MAJOR) {
908 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
909 "Expected [%d]; got [%d]\n",
910 ECRYPTFS_VERSION_MAJOR, major);
911 rc = -EINVAL;
912 goto out;
913 }
914 if (minor != ECRYPTFS_VERSION_MINOR) {
915 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
916 "Expected [%d]; got [%d]\n",
917 ECRYPTFS_VERSION_MINOR, minor);
918 rc = -EINVAL;
919 goto out;
920 }
921out:
922 return rc;
923}
924
925int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
926 struct ecryptfs_auth_tok **auth_tok,
927 char *sig)
928{
929 int rc = 0;
930
931 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
932 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
933 printk(KERN_ERR "Could not find key with description: [%s]\n",
934 sig);
935 process_request_key_err(PTR_ERR(*auth_tok_key));
936 rc = -EINVAL;
937 goto out;
938 }
939 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
940 if (ecryptfs_verify_version((*auth_tok)->version)) {
941 printk(KERN_ERR
942 "Data structure version mismatch. "
943 "Userspace tools must match eCryptfs "
944 "kernel module with major version [%d] "
945 "and minor version [%d]\n",
946 ECRYPTFS_VERSION_MAJOR,
947 ECRYPTFS_VERSION_MINOR);
948 rc = -EINVAL;
949 goto out;
950 }
951 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
952 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
953 printk(KERN_ERR "Invalid auth_tok structure "
954 "returned from key query\n");
955 rc = -EINVAL;
956 goto out;
957 }
958out:
959 return rc;
960}
961
962/**
963 * ecryptfs_find_auth_tok_for_sig
964 * @auth_tok: Set to the matching auth_tok; NULL if not found
965 * @crypt_stat: inode crypt_stat crypto context
966 * @sig: Sig of auth_tok to find
967 *
968 * For now, this function simply looks at the registered auth_tok's
969 * linked off the mount_crypt_stat, so all the auth_toks that can be
970 * used must be registered at mount time. This function could
971 * potentially try a lot harder to find auth_tok's (e.g., by calling
972 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
973 * that static registration of auth_tok's will no longer be necessary.
974 *
975 * Returns zero on no error; non-zero on error
976 */
977static int
978ecryptfs_find_auth_tok_for_sig(
979 struct ecryptfs_auth_tok **auth_tok,
980 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
981{
982 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
983 crypt_stat->mount_crypt_stat;
984 struct ecryptfs_global_auth_tok *global_auth_tok;
985 int rc = 0;
986
987 (*auth_tok) = NULL;
988 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
989 mount_crypt_stat, sig)) {
990 struct key *auth_tok_key;
991
992 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
993 sig);
994 } else
995 (*auth_tok) = global_auth_tok->global_auth_tok;
996 return rc;
997}
998
999/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001000 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1001 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1002 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001003 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001004 * Returns zero on success; non-zero error otherwise
Michael Halcrow237fead2006-10-04 02:16:22 -07001005 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001006static int
1007decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1008 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001009{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001010 struct scatterlist dst_sg;
1011 struct scatterlist src_sg;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001012 struct mutex *tfm_mutex;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001013 struct blkcipher_desc desc = {
1014 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1015 };
1016 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001017
Michael Halcrowf4aad162007-10-16 01:27:53 -07001018 if (unlikely(ecryptfs_verbosity > 0)) {
1019 ecryptfs_printk(
1020 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1021 auth_tok->token.password.session_key_encryption_key_bytes);
1022 ecryptfs_dump_hex(
1023 auth_tok->token.password.session_key_encryption_key,
1024 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001025 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001026 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1027 crypt_stat->cipher);
1028 if (unlikely(rc)) {
1029 printk(KERN_ERR "Internal error whilst attempting to get "
1030 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1031 crypt_stat->cipher, rc);
1032 goto out;
1033 }
1034 if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1035 auth_tok->session_key.encrypted_key_size,
1036 &src_sg, 1)) != 1) {
1037 printk(KERN_ERR "Internal error whilst attempting to convert "
1038 "auth_tok->session_key.encrypted_key to scatterlist; "
1039 "expected rc = 1; got rc = [%d]. "
1040 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1041 auth_tok->session_key.encrypted_key_size);
1042 goto out;
1043 }
1044 auth_tok->session_key.decrypted_key_size =
1045 auth_tok->session_key.encrypted_key_size;
1046 if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1047 auth_tok->session_key.decrypted_key_size,
1048 &dst_sg, 1)) != 1) {
1049 printk(KERN_ERR "Internal error whilst attempting to convert "
1050 "auth_tok->session_key.decrypted_key to scatterlist; "
1051 "expected rc = 1; got rc = [%d]\n", rc);
1052 goto out;
1053 }
1054 mutex_lock(tfm_mutex);
1055 rc = crypto_blkcipher_setkey(
1056 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1057 crypt_stat->key_size);
1058 if (unlikely(rc < 0)) {
1059 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001060 printk(KERN_ERR "Error setting key for crypto context\n");
1061 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001062 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001063 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001064 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001065 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001066 mutex_unlock(tfm_mutex);
1067 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001068 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001069 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001070 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001071 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1072 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1073 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001074 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001075 if (unlikely(ecryptfs_verbosity > 0)) {
1076 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1077 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001078 ecryptfs_dump_hex(crypt_stat->key,
1079 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001080 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001081out:
1082 return rc;
1083}
1084
Michael Halcrowf4aad162007-10-16 01:27:53 -07001085int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1086{
1087 int rc = 0;
1088
1089 (*sig) = NULL;
1090 switch (auth_tok->token_type) {
1091 case ECRYPTFS_PASSWORD:
1092 (*sig) = auth_tok->token.password.signature;
1093 break;
1094 case ECRYPTFS_PRIVATE_KEY:
1095 (*sig) = auth_tok->token.private_key.signature;
1096 break;
1097 default:
1098 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1099 auth_tok->token_type);
1100 rc = -EINVAL;
1101 }
1102 return rc;
1103}
1104
Michael Halcrow237fead2006-10-04 02:16:22 -07001105/**
1106 * ecryptfs_parse_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001107 * @crypt_stat: The cryptographic context
1108 * @src: Virtual address of region of memory containing the packets
1109 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
Michael Halcrow237fead2006-10-04 02:16:22 -07001110 *
1111 * Get crypt_stat to have the file's session key if the requisite key
1112 * is available to decrypt the session key.
1113 *
1114 * Returns Zero if a valid authentication token was retrieved and
1115 * processed; negative value for file not encrypted or for error
1116 * conditions.
1117 */
1118int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1119 unsigned char *src,
1120 struct dentry *ecryptfs_dentry)
1121{
1122 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001123 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001124 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001125 struct list_head auth_tok_list;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001126 struct ecryptfs_auth_tok *matching_auth_tok;
1127 struct ecryptfs_auth_tok *candidate_auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001128 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001129 size_t packet_size;
1130 struct ecryptfs_auth_tok *new_auth_tok;
1131 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001132 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001133 size_t tag_11_contents_size;
1134 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001135 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001136
1137 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001138 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001139 * added the our &auth_tok_list */
1140 next_packet_is_auth_tok_packet = 1;
1141 while (next_packet_is_auth_tok_packet) {
1142 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1143
1144 switch (src[i]) {
1145 case ECRYPTFS_TAG_3_PACKET_TYPE:
1146 rc = parse_tag_3_packet(crypt_stat,
1147 (unsigned char *)&src[i],
1148 &auth_tok_list, &new_auth_tok,
1149 &packet_size, max_packet_size);
1150 if (rc) {
1151 ecryptfs_printk(KERN_ERR, "Error parsing "
1152 "tag 3 packet\n");
1153 rc = -EIO;
1154 goto out_wipe_list;
1155 }
1156 i += packet_size;
1157 rc = parse_tag_11_packet((unsigned char *)&src[i],
1158 sig_tmp_space,
1159 ECRYPTFS_SIG_SIZE,
1160 &tag_11_contents_size,
1161 &tag_11_packet_size,
1162 max_packet_size);
1163 if (rc) {
1164 ecryptfs_printk(KERN_ERR, "No valid "
1165 "(ecryptfs-specific) literal "
1166 "packet containing "
1167 "authentication token "
1168 "signature found after "
1169 "tag 3 packet\n");
1170 rc = -EIO;
1171 goto out_wipe_list;
1172 }
1173 i += tag_11_packet_size;
1174 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1175 ecryptfs_printk(KERN_ERR, "Expected "
1176 "signature of size [%d]; "
1177 "read size [%d]\n",
1178 ECRYPTFS_SIG_SIZE,
1179 tag_11_contents_size);
1180 rc = -EIO;
1181 goto out_wipe_list;
1182 }
1183 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1184 sig_tmp_space, tag_11_contents_size);
1185 new_auth_tok->token.password.signature[
1186 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001187 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001188 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001189 case ECRYPTFS_TAG_1_PACKET_TYPE:
1190 rc = parse_tag_1_packet(crypt_stat,
1191 (unsigned char *)&src[i],
1192 &auth_tok_list, &new_auth_tok,
1193 &packet_size, max_packet_size);
1194 if (rc) {
1195 ecryptfs_printk(KERN_ERR, "Error parsing "
1196 "tag 1 packet\n");
1197 rc = -EIO;
1198 goto out_wipe_list;
1199 }
1200 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001201 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001202 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001203 case ECRYPTFS_TAG_11_PACKET_TYPE:
1204 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1205 "(Tag 11 not allowed by itself)\n");
1206 rc = -EIO;
1207 goto out_wipe_list;
1208 break;
1209 default:
1210 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1211 "[%d] of the file header; hex value of "
1212 "character is [0x%.2x]\n", i, src[i]);
1213 next_packet_is_auth_tok_packet = 0;
1214 }
1215 }
1216 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001217 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1218 "eCryptfs file; this is not supported in this version "
1219 "of the eCryptfs kernel module\n");
1220 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001221 goto out;
1222 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001223 /* auth_tok_list contains the set of authentication tokens
1224 * parsed from the metadata. We need to find a matching
1225 * authentication token that has the secret component(s)
1226 * necessary to decrypt the EFEK in the auth_tok parsed from
1227 * the metadata. There may be several potential matches, but
1228 * just one will be sufficient to decrypt to get the FEK. */
1229find_next_matching_auth_tok:
1230 found_auth_tok = 0;
1231 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001232 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1233 if (unlikely(ecryptfs_verbosity > 0)) {
1234 ecryptfs_printk(KERN_DEBUG,
1235 "Considering cadidate auth tok:\n");
1236 ecryptfs_dump_auth_tok(candidate_auth_tok);
1237 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001238 if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1239 candidate_auth_tok))) {
1240 printk(KERN_ERR
1241 "Unrecognized candidate auth tok type: [%d]\n",
1242 candidate_auth_tok->token_type);
1243 rc = -EINVAL;
1244 goto out_wipe_list;
1245 }
1246 if ((rc = ecryptfs_find_auth_tok_for_sig(
1247 &matching_auth_tok, crypt_stat,
1248 candidate_auth_tok_sig)))
1249 rc = 0;
1250 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001251 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001252 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001253 }
1254 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001255 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001256 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1257 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001258 rc = -EIO;
1259 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001260 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001261found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001262 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001263 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001264 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001265 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001266 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001267 crypt_stat);
1268 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001269 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001270 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001271 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001272 rc = decrypt_passphrase_encrypted_session_key(
1273 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001274 }
1275 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001276 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1277
1278 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1279 "session key for authentication token with sig "
1280 "[%.*s]; rc = [%d]. Removing auth tok "
1281 "candidate from the list and searching for "
1282 "the next match.\n", candidate_auth_tok_sig,
1283 ECRYPTFS_SIG_SIZE_HEX, rc);
1284 list_for_each_entry_safe(auth_tok_list_item,
1285 auth_tok_list_item_tmp,
1286 &auth_tok_list, list) {
1287 if (candidate_auth_tok
1288 == &auth_tok_list_item->auth_tok) {
1289 list_del(&auth_tok_list_item->list);
1290 kmem_cache_free(
1291 ecryptfs_auth_tok_list_item_cache,
1292 auth_tok_list_item);
1293 goto find_next_matching_auth_tok;
1294 }
1295 }
1296 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001297 }
1298 rc = ecryptfs_compute_root_iv(crypt_stat);
1299 if (rc) {
1300 ecryptfs_printk(KERN_ERR, "Error computing "
1301 "the root IV\n");
1302 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001303 }
1304 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1305 if (rc) {
1306 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1307 "context for cipher [%s]; rc = [%d]\n",
1308 crypt_stat->cipher, rc);
1309 }
1310out_wipe_list:
1311 wipe_auth_tok_list(&auth_tok_list);
1312out:
1313 return rc;
1314}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001315
Michael Halcrowdddfa462007-02-12 00:53:44 -08001316static int
1317pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1318 struct ecryptfs_crypt_stat *crypt_stat,
1319 struct ecryptfs_key_record *key_rec)
1320{
1321 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1322 char *netlink_payload;
1323 size_t netlink_payload_length;
1324 struct ecryptfs_message *msg;
1325 int rc;
1326
1327 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1328 ecryptfs_code_for_cipher_string(crypt_stat),
1329 crypt_stat, &netlink_payload,
1330 &netlink_payload_length);
1331 if (rc) {
1332 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1333 goto out;
1334 }
1335 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1336 netlink_payload_length, &msg_ctx);
1337 if (rc) {
1338 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1339 goto out;
1340 }
1341 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1342 if (rc) {
1343 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1344 "from the user space daemon\n");
1345 rc = -EIO;
1346 goto out;
1347 }
1348 rc = parse_tag_67_packet(key_rec, msg);
1349 if (rc)
1350 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1351 kfree(msg);
1352out:
1353 if (netlink_payload)
1354 kfree(netlink_payload);
1355 return rc;
1356}
1357/**
1358 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1359 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001360 * @remaining_bytes: Maximum number of bytes that can be writtn
1361 * @auth_tok: The authentication token used for generating the tag 1 packet
1362 * @crypt_stat: The cryptographic context
1363 * @key_rec: The key record struct for the tag 1 packet
Michael Halcrowdddfa462007-02-12 00:53:44 -08001364 * @packet_size: This function will write the number of bytes that end
1365 * up constituting the packet; set to zero on error
1366 *
1367 * Returns zero on success; non-zero on error.
1368 */
1369static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001370write_tag_1_packet(char *dest, size_t *remaining_bytes,
1371 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001372 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001373 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1374{
1375 size_t i;
1376 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001377 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001378 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001379 int rc = 0;
1380
1381 (*packet_size) = 0;
1382 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1383 ECRYPTFS_SIG_SIZE);
1384 encrypted_session_key_valid = 0;
1385 for (i = 0; i < crypt_stat->key_size; i++)
1386 encrypted_session_key_valid |=
1387 auth_tok->session_key.encrypted_key[i];
1388 if (encrypted_session_key_valid) {
1389 memcpy(key_rec->enc_key,
1390 auth_tok->session_key.encrypted_key,
1391 auth_tok->session_key.encrypted_key_size);
1392 goto encrypted_session_key_set;
1393 }
1394 if (auth_tok->session_key.encrypted_key_size == 0)
1395 auth_tok->session_key.encrypted_key_size =
1396 auth_tok->token.private_key.key_size;
1397 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1398 if (rc) {
1399 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1400 "via a pki");
1401 goto out;
1402 }
1403 if (ecryptfs_verbosity > 0) {
1404 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1405 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1406 }
1407encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001408 /* This format is inspired by OpenPGP; see RFC 2440
1409 * packet tag 1 */
1410 max_packet_size = (1 /* Tag 1 identifier */
1411 + 3 /* Max Tag 1 packet size */
1412 + 1 /* Version */
1413 + ECRYPTFS_SIG_SIZE /* Key identifier */
1414 + 1 /* Cipher identifier */
1415 + key_rec->enc_key_size); /* Encrypted key size */
1416 if (max_packet_size > (*remaining_bytes)) {
1417 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001418 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001419 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001420 rc = -EINVAL;
1421 goto out;
1422 }
1423 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001424 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001425 &packet_size_length);
1426 if (rc) {
1427 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1428 "header; cannot generate packet length\n");
1429 goto out;
1430 }
1431 (*packet_size) += packet_size_length;
1432 dest[(*packet_size)++] = 0x03; /* version 3 */
1433 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1434 (*packet_size) += ECRYPTFS_SIG_SIZE;
1435 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1436 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1437 key_rec->enc_key_size);
1438 (*packet_size) += key_rec->enc_key_size;
1439out:
1440 if (rc)
1441 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001442 else
1443 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001444 return rc;
1445}
Michael Halcrow237fead2006-10-04 02:16:22 -07001446
1447/**
1448 * write_tag_11_packet
1449 * @dest: Target into which Tag 11 packet is to be written
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001450 * @remaining_bytes: Maximum packet length
Michael Halcrow237fead2006-10-04 02:16:22 -07001451 * @contents: Byte array of contents to copy in
1452 * @contents_length: Number of bytes in contents
1453 * @packet_length: Length of the Tag 11 packet written; zero on error
1454 *
1455 * Returns zero on success; non-zero on error.
1456 */
1457static int
Andrew Morton81acbcd2007-10-16 01:27:59 -07001458write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
Michael Halcrow146a4602007-10-16 01:27:58 -07001459 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07001460{
Michael Halcrow237fead2006-10-04 02:16:22 -07001461 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001462 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001463 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001464
1465 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001466 /* This format is inspired by OpenPGP; see RFC 2440
1467 * packet tag 11 */
1468 max_packet_size = (1 /* Tag 11 identifier */
1469 + 3 /* Max Tag 11 packet size */
1470 + 1 /* Binary format specifier */
1471 + 1 /* Filename length */
1472 + 8 /* Filename ("_CONSOLE") */
1473 + 4 /* Modification date */
1474 + contents_length); /* Literal data */
1475 if (max_packet_size > (*remaining_bytes)) {
1476 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001477 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrow146a4602007-10-16 01:27:58 -07001478 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07001479 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001480 goto out;
1481 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001482 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001483 rc = write_packet_length(&dest[(*packet_length)],
Michael Halcrow146a4602007-10-16 01:27:58 -07001484 (max_packet_size - 4), &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001485 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07001486 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1487 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001488 goto out;
1489 }
1490 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001491 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07001492 dest[(*packet_length)++] = 8;
1493 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1494 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07001495 memset(&dest[(*packet_length)], 0x00, 4);
1496 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07001497 memcpy(&dest[(*packet_length)], contents, contents_length);
1498 (*packet_length) += contents_length;
1499 out:
1500 if (rc)
1501 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001502 else
1503 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001504 return rc;
1505}
1506
1507/**
1508 * write_tag_3_packet
1509 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001510 * @remaining_bytes: Maximum number of bytes that can be written
Michael Halcrow237fead2006-10-04 02:16:22 -07001511 * @auth_tok: Authentication token
1512 * @crypt_stat: The cryptographic context
1513 * @key_rec: encrypted key
1514 * @packet_size: This function will write the number of bytes that end
1515 * up constituting the packet; set to zero on error
1516 *
1517 * Returns zero on success; non-zero on error.
1518 */
1519static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001520write_tag_3_packet(char *dest, size_t *remaining_bytes,
1521 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001522 struct ecryptfs_crypt_stat *crypt_stat,
1523 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1524{
Michael Halcrow237fead2006-10-04 02:16:22 -07001525 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001526 size_t encrypted_session_key_valid = 0;
1527 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001528 struct scatterlist dst_sg;
1529 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001530 struct mutex *tfm_mutex = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001531 size_t cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001532 size_t packet_size_length;
1533 size_t max_packet_size;
1534 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1535 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001536 struct blkcipher_desc desc = {
1537 .tfm = NULL,
1538 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1539 };
1540 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001541
1542 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001543 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001544 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001545 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1546 crypt_stat->cipher);
1547 if (unlikely(rc)) {
1548 printk(KERN_ERR "Internal error whilst attempting to get "
1549 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1550 crypt_stat->cipher, rc);
1551 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001552 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001553 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1554 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1555
1556 printk(KERN_WARNING "No key size specified at mount; "
1557 "defaulting to [%d]\n", alg->max_keysize);
1558 mount_crypt_stat->global_default_cipher_key_size =
1559 alg->max_keysize;
1560 }
1561 if (crypt_stat->key_size == 0)
1562 crypt_stat->key_size =
1563 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001564 if (auth_tok->session_key.encrypted_key_size == 0)
1565 auth_tok->session_key.encrypted_key_size =
1566 crypt_stat->key_size;
1567 if (crypt_stat->key_size == 24
1568 && strcmp("aes", crypt_stat->cipher) == 0) {
1569 memset((crypt_stat->key + 24), 0, 8);
1570 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001571 } else
1572 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001573 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001574 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001575 encrypted_session_key_valid = 0;
1576 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1577 encrypted_session_key_valid |=
1578 auth_tok->session_key.encrypted_key[i];
1579 if (encrypted_session_key_valid) {
1580 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1581 "using auth_tok->session_key.encrypted_key, "
1582 "where key_rec->enc_key_size = [%d]\n",
1583 key_rec->enc_key_size);
1584 memcpy(key_rec->enc_key,
1585 auth_tok->session_key.encrypted_key,
1586 key_rec->enc_key_size);
1587 goto encrypted_session_key_set;
1588 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001589 if (auth_tok->token.password.flags &
1590 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001591 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1592 "session key encryption key of size [%d]\n",
1593 auth_tok->token.password.
1594 session_key_encryption_key_bytes);
1595 memcpy(session_key_encryption_key,
1596 auth_tok->token.password.session_key_encryption_key,
1597 crypt_stat->key_size);
1598 ecryptfs_printk(KERN_DEBUG,
1599 "Cached session key " "encryption key: \n");
1600 if (ecryptfs_verbosity > 0)
1601 ecryptfs_dump_hex(session_key_encryption_key, 16);
1602 }
1603 if (unlikely(ecryptfs_verbosity > 0)) {
1604 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1605 ecryptfs_dump_hex(session_key_encryption_key, 16);
1606 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001607 if ((rc = virt_to_scatterlist(crypt_stat->key,
1608 key_rec->enc_key_size, &src_sg, 1))
1609 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001610 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001611 "for crypt_stat session key; expected rc = 1; "
1612 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1613 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001614 rc = -ENOMEM;
1615 goto out;
1616 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001617 if ((rc = virt_to_scatterlist(key_rec->enc_key,
1618 key_rec->enc_key_size, &dst_sg, 1))
1619 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001620 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001621 "for crypt_stat encrypted session key; "
1622 "expected rc = 1; got rc = [%d]. "
1623 "key_rec->enc_key_size = [%d]\n", rc,
1624 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001625 rc = -ENOMEM;
1626 goto out;
1627 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001628 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001629 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1630 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001631 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001632 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001633 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001634 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001635 goto out;
1636 }
1637 rc = 0;
1638 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1639 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001640 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001641 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001642 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001643 if (rc) {
1644 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1645 goto out;
1646 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001647 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001648 if (ecryptfs_verbosity > 0) {
1649 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1650 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001651 ecryptfs_dump_hex(key_rec->enc_key,
1652 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001653 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001654encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001655 /* This format is inspired by OpenPGP; see RFC 2440
1656 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001657 max_packet_size = (1 /* Tag 3 identifier */
1658 + 3 /* Max Tag 3 packet size */
1659 + 1 /* Version */
1660 + 1 /* Cipher code */
1661 + 1 /* S2K specifier */
1662 + 1 /* Hash identifier */
1663 + ECRYPTFS_SALT_SIZE /* Salt */
1664 + 1 /* Hash iterations */
1665 + key_rec->enc_key_size); /* Encrypted key size */
1666 if (max_packet_size > (*remaining_bytes)) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001667 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
1668 "there are only [%td] available\n", max_packet_size,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001669 (*remaining_bytes));
1670 rc = -EINVAL;
1671 goto out;
1672 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001673 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001674 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1675 * to get the number of octets in the actual Tag 3 packet */
1676 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrow237fead2006-10-04 02:16:22 -07001677 &packet_size_length);
1678 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001679 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1680 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001681 goto out;
1682 }
1683 (*packet_size) += packet_size_length;
1684 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001685 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1686 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001687 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1688 if (cipher_code == 0) {
1689 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1690 "cipher [%s]\n", crypt_stat->cipher);
1691 rc = -EINVAL;
1692 goto out;
1693 }
1694 dest[(*packet_size)++] = cipher_code;
1695 dest[(*packet_size)++] = 0x03; /* S2K */
1696 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1697 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1698 ECRYPTFS_SALT_SIZE);
1699 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1700 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001701 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1702 key_rec->enc_key_size);
1703 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001704out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001705 if (rc)
1706 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001707 else
1708 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001709 return rc;
1710}
1711
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001712struct kmem_cache *ecryptfs_key_record_cache;
1713
Michael Halcrow237fead2006-10-04 02:16:22 -07001714/**
1715 * ecryptfs_generate_key_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001716 * @dest_base: Virtual address from which to write the key record set
Michael Halcrow237fead2006-10-04 02:16:22 -07001717 * @crypt_stat: The cryptographic context from which the
1718 * authentication tokens will be retrieved
1719 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1720 * for the global parameters
1721 * @len: The amount written
1722 * @max: The maximum amount of data allowed to be written
1723 *
1724 * Generates a key packet set and writes it to the virtual address
1725 * passed in.
1726 *
1727 * Returns zero on success; non-zero on error.
1728 */
1729int
1730ecryptfs_generate_key_packet_set(char *dest_base,
1731 struct ecryptfs_crypt_stat *crypt_stat,
1732 struct dentry *ecryptfs_dentry, size_t *len,
1733 size_t max)
1734{
Michael Halcrow237fead2006-10-04 02:16:22 -07001735 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001736 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001737 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1738 &ecryptfs_superblock_to_private(
1739 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1740 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001741 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001742 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001743 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001744
1745 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001746 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001747 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1748 if (!key_rec) {
1749 rc = -ENOMEM;
1750 goto out;
1751 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001752 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1753 crypt_stat_list) {
1754 memset(key_rec, 0, sizeof(*key_rec));
1755 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1756 mount_crypt_stat,
1757 key_sig->keysig);
1758 if (rc) {
1759 printk(KERN_ERR "Error attempting to get the global "
1760 "auth_tok; rc = [%d]\n", rc);
1761 goto out_free;
1762 }
1763 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1764 printk(KERN_WARNING
1765 "Skipping invalid auth tok with sig = [%s]\n",
1766 global_auth_tok->sig);
1767 continue;
1768 }
1769 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001770 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1771 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001772 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001773 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001774 &written);
1775 if (rc) {
1776 ecryptfs_printk(KERN_WARNING, "Error "
1777 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001778 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001779 }
1780 (*len) += written;
1781 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001782 rc = write_tag_11_packet((dest_base + (*len)), &max,
1783 key_rec->sig,
1784 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001785 if (rc) {
1786 ecryptfs_printk(KERN_ERR, "Error writing "
1787 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001788 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001789 }
1790 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001791 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1792 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001793 &max, auth_tok,
1794 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001795 if (rc) {
1796 ecryptfs_printk(KERN_WARNING, "Error "
1797 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001798 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001799 }
1800 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001801 } else {
1802 ecryptfs_printk(KERN_WARNING, "Unsupported "
1803 "authentication token type\n");
1804 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001805 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001806 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001807 }
1808 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001809 dest_base[(*len)] = 0x00;
1810 } else {
1811 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1812 rc = -EIO;
1813 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001814out_free:
1815 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001816out:
1817 if (rc)
1818 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001819 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001820 return rc;
1821}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001822
1823struct kmem_cache *ecryptfs_key_sig_cache;
1824
1825int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1826{
1827 struct ecryptfs_key_sig *new_key_sig;
1828 int rc = 0;
1829
1830 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1831 if (!new_key_sig) {
1832 rc = -ENOMEM;
1833 printk(KERN_ERR
1834 "Error allocating from ecryptfs_key_sig_cache\n");
1835 goto out;
1836 }
1837 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1838 mutex_lock(&crypt_stat->keysig_list_mutex);
1839 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1840 mutex_unlock(&crypt_stat->keysig_list_mutex);
1841out:
1842 return rc;
1843}
1844
1845struct kmem_cache *ecryptfs_global_auth_tok_cache;
1846
1847int
1848ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1849 char *sig)
1850{
1851 struct ecryptfs_global_auth_tok *new_auth_tok;
1852 int rc = 0;
1853
1854 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1855 GFP_KERNEL);
1856 if (!new_auth_tok) {
1857 rc = -ENOMEM;
1858 printk(KERN_ERR "Error allocating from "
1859 "ecryptfs_global_auth_tok_cache\n");
1860 goto out;
1861 }
1862 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1863 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1864 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1865 list_add(&new_auth_tok->mount_crypt_stat_list,
1866 &mount_crypt_stat->global_auth_tok_list);
1867 mount_crypt_stat->num_global_auth_toks++;
1868 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1869out:
1870 return rc;
1871}
1872