blob: 09e2340ab783efa5c7f5689e3e5022f96edbf134 [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
5 * file.
6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -080010 * Trevor S. Highland <trevor.highland@gmail.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/string.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070029#include <linux/syscalls.h>
30#include <linux/pagemap.h>
31#include <linux/key.h>
32#include <linux/random.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
37/**
38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and
40 * return an error code.
41 */
Michael Halcrowcd9d67d2007-10-16 01:28:04 -070042static int process_request_key_err(long err_code)
Michael Halcrow237fead2006-10-04 02:16:22 -070043{
44 int rc = 0;
45
46 switch (err_code) {
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
Michael Halcrowcd9d67d2007-10-16 01:28:04 -0700399static int
400ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
401{
402 int rc = 0;
403
404 (*sig) = NULL;
405 switch (auth_tok->token_type) {
406 case ECRYPTFS_PASSWORD:
407 (*sig) = auth_tok->token.password.signature;
408 break;
409 case ECRYPTFS_PRIVATE_KEY:
410 (*sig) = auth_tok->token.private_key.signature;
411 break;
412 default:
413 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
414 auth_tok->token_type);
415 rc = -EINVAL;
416 }
417 return rc;
418}
419
Michael Halcrowdddfa462007-02-12 00:53:44 -0800420/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700421 * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok.
422 * @auth_tok: The key authentication token used to decrypt the session key
423 * @crypt_stat: The cryptographic context
Michael Halcrowdddfa462007-02-12 00:53:44 -0800424 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700425 * Returns zero on success; non-zero error otherwise.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800426 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700427static int
428decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
429 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -0800430{
431 u16 cipher_code = 0;
432 struct ecryptfs_msg_ctx *msg_ctx;
433 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700434 char *auth_tok_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800435 char *netlink_message;
436 size_t netlink_message_length;
437 int rc;
438
Michael Halcrowf4aad162007-10-16 01:27:53 -0700439 if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) {
440 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
441 auth_tok->token_type);
442 goto out;
443 }
444 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800445 &netlink_message, &netlink_message_length);
446 if (rc) {
447 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
448 goto out;
449 }
450 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
451 netlink_message_length, &msg_ctx);
452 if (rc) {
453 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
454 goto out;
455 }
456 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
457 if (rc) {
458 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
459 "from the user space daemon\n");
460 rc = -EIO;
461 goto out;
462 }
463 rc = parse_tag_65_packet(&(auth_tok->session_key),
464 &cipher_code, msg);
465 if (rc) {
466 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
467 rc);
468 goto out;
469 }
470 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
471 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
472 auth_tok->session_key.decrypted_key_size);
473 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
474 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
475 if (rc) {
476 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
477 cipher_code)
478 goto out;
479 }
480 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
481 if (ecryptfs_verbosity > 0) {
482 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
483 ecryptfs_dump_hex(crypt_stat->key,
484 crypt_stat->key_size);
485 }
486out:
487 if (msg)
488 kfree(msg);
489 return rc;
490}
491
492static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
493{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800494 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700495 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800496
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700497 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
498 auth_tok_list_head, list) {
499 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800500 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
501 auth_tok_list_item);
502 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800503}
504
505struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
506
Michael Halcrowdddfa462007-02-12 00:53:44 -0800507/**
508 * parse_tag_1_packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700509 * @crypt_stat: The cryptographic context to modify based on packet contents
Michael Halcrowdddfa462007-02-12 00:53:44 -0800510 * @data: The raw bytes of the packet.
511 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700512 * a new authentication token will be placed at the
513 * end of this list for this packet.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800514 * @new_auth_tok: Pointer to a pointer to memory that this function
515 * allocates; sets the memory address of the pointer to
516 * NULL on error. This object is added to the
517 * auth_tok_list.
518 * @packet_size: This function writes the size of the parsed packet
519 * into this memory location; zero on error.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700520 * @max_packet_size: The maximum allowable packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800521 *
522 * Returns zero on success; non-zero on error.
523 */
524static int
525parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
526 unsigned char *data, struct list_head *auth_tok_list,
527 struct ecryptfs_auth_tok **new_auth_tok,
528 size_t *packet_size, size_t max_packet_size)
529{
530 size_t body_size;
531 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
532 size_t length_size;
533 int rc = 0;
534
535 (*packet_size) = 0;
536 (*new_auth_tok) = NULL;
Michael Halcrow13218172007-10-16 01:27:56 -0700537 /**
538 * This format is inspired by OpenPGP; see RFC 2440
539 * packet tag 1
540 *
541 * Tag 1 identifier (1 byte)
542 * Max Tag 1 packet size (max 3 bytes)
543 * Version (1 byte)
544 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
545 * Cipher identifier (1 byte)
546 * Encrypted key size (arbitrary)
547 *
548 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800549 */
Michael Halcrow13218172007-10-16 01:27:56 -0700550 if (unlikely(max_packet_size < 12)) {
551 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800552 rc = -EINVAL;
553 goto out;
554 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800555 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow13218172007-10-16 01:27:56 -0700556 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
557 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800558 rc = -EINVAL;
559 goto out;
560 }
561 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
562 * at end of function upon failure */
563 auth_tok_list_item =
Michael Halcrow13218172007-10-16 01:27:56 -0700564 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
565 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800566 if (!auth_tok_list_item) {
Michael Halcrow13218172007-10-16 01:27:56 -0700567 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800568 rc = -ENOMEM;
569 goto out;
570 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800571 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow13218172007-10-16 01:27:56 -0700572 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
573 &length_size))) {
574 printk(KERN_WARNING "Error parsing packet length; "
575 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800576 goto out_free;
577 }
Michael Halcrow13218172007-10-16 01:27:56 -0700578 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700579 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800580 rc = -EINVAL;
581 goto out_free;
582 }
583 (*packet_size) += length_size;
584 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700585 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800586 rc = -EINVAL;
587 goto out_free;
588 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800589 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700590 printk(KERN_WARNING "Unknown version number [%d]\n",
591 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800592 rc = -EINVAL;
593 goto out_free;
594 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800595 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
596 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
597 *packet_size += ECRYPTFS_SIG_SIZE;
598 /* This byte is skipped because the kernel does not need to
599 * know which public key encryption algorithm was used */
600 (*packet_size)++;
601 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow13218172007-10-16 01:27:56 -0700602 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800603 if ((*new_auth_tok)->session_key.encrypted_key_size
604 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow13218172007-10-16 01:27:56 -0700605 printk(KERN_WARNING "Tag 1 packet contains key larger "
606 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800607 rc = -EINVAL;
608 goto out;
609 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800610 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow13218172007-10-16 01:27:56 -0700611 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800612 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
613 (*new_auth_tok)->session_key.flags &=
614 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
615 (*new_auth_tok)->session_key.flags |=
616 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
617 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow13218172007-10-16 01:27:56 -0700618 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800619 (*new_auth_tok)->session_key.flags &=
620 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
621 (*new_auth_tok)->session_key.flags &=
622 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800623 list_add(&auth_tok_list_item->list, auth_tok_list);
624 goto out;
625out_free:
626 (*new_auth_tok) = NULL;
627 memset(auth_tok_list_item, 0,
628 sizeof(struct ecryptfs_auth_tok_list_item));
629 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
630 auth_tok_list_item);
631out:
632 if (rc)
633 (*packet_size) = 0;
634 return rc;
635}
636
Michael Halcrow237fead2006-10-04 02:16:22 -0700637/**
638 * parse_tag_3_packet
639 * @crypt_stat: The cryptographic context to modify based on packet
640 * contents.
641 * @data: The raw bytes of the packet.
642 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
643 * a new authentication token will be placed at the end
644 * of this list for this packet.
645 * @new_auth_tok: Pointer to a pointer to memory that this function
646 * allocates; sets the memory address of the pointer to
647 * NULL on error. This object is added to the
648 * auth_tok_list.
649 * @packet_size: This function writes the size of the parsed packet
650 * into this memory location; zero on error.
651 * @max_packet_size: maximum number of bytes to parse
652 *
653 * Returns zero on success; non-zero on error.
654 */
655static int
656parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
657 unsigned char *data, struct list_head *auth_tok_list,
658 struct ecryptfs_auth_tok **new_auth_tok,
659 size_t *packet_size, size_t max_packet_size)
660{
Michael Halcrow237fead2006-10-04 02:16:22 -0700661 size_t body_size;
662 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
663 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800664 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700665
666 (*packet_size) = 0;
667 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700668 /**
669 *This format is inspired by OpenPGP; see RFC 2440
670 * packet tag 3
671 *
672 * Tag 3 identifier (1 byte)
673 * Max Tag 3 packet size (max 3 bytes)
674 * Version (1 byte)
675 * Cipher code (1 byte)
676 * S2K specifier (1 byte)
677 * Hash identifier (1 byte)
678 * Salt (ECRYPTFS_SALT_SIZE)
679 * Hash iterations (1 byte)
680 * Encrypted key (arbitrary)
681 *
682 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -0700683 */
Michael Halcrowc59becf2007-10-16 01:27:56 -0700684 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
685 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700686 rc = -EINVAL;
687 goto out;
688 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700689 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700690 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
691 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700692 rc = -EINVAL;
693 goto out;
694 }
695 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
696 * at end of function upon failure */
697 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800698 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700699 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700700 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700701 rc = -ENOMEM;
702 goto out;
703 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700704 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700705 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
706 &length_size))) {
707 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
708 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700709 goto out_free;
710 }
Michael Halcrowc59becf2007-10-16 01:27:56 -0700711 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700712 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700713 rc = -EINVAL;
714 goto out_free;
715 }
716 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700717 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700718 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700719 rc = -EINVAL;
720 goto out_free;
721 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -0700723 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Michael Halcrow237fead2006-10-04 02:16:22 -0700724 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700725 printk(KERN_WARNING "Unknown version number [%d]\n",
726 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -0700727 rc = -EINVAL;
728 goto out_free;
729 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
731 (u16)data[(*packet_size)]);
732 /* A little extra work to differentiate among the AES key
733 * sizes; see RFC2440 */
734 switch(data[(*packet_size)++]) {
735 case RFC2440_CIPHER_AES_192:
736 crypt_stat->key_size = 24;
737 break;
738 default:
739 crypt_stat->key_size =
740 (*new_auth_tok)->session_key.encrypted_key_size;
741 }
742 ecryptfs_init_crypt_ctx(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700743 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700744 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700745 rc = -ENOSYS;
746 goto out_free;
747 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -0700749 switch (data[(*packet_size)++]) {
750 case 0x01: /* See RFC2440 for these numbers and their mappings */
751 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 memcpy((*new_auth_tok)->token.password.salt,
753 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
754 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700755 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700756 (*new_auth_tok)->token.password.hash_iterations =
757 ((u32) 16 + (data[(*packet_size)] & 15))
758 << ((data[(*packet_size)] >> 4) + 6);
759 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700760 /* Friendly reminder:
761 * (*new_auth_tok)->session_key.encrypted_key_size =
762 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -0700763 memcpy((*new_auth_tok)->session_key.encrypted_key,
764 &data[(*packet_size)],
765 (*new_auth_tok)->session_key.encrypted_key_size);
766 (*packet_size) +=
767 (*new_auth_tok)->session_key.encrypted_key_size;
768 (*new_auth_tok)->session_key.flags &=
769 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
770 (*new_auth_tok)->session_key.flags |=
771 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700772 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700773 break;
774 default:
775 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
776 "[%d]\n", data[(*packet_size) - 1]);
777 rc = -ENOSYS;
778 goto out_free;
779 }
780 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
781 /* TODO: Parametarize; we might actually want userspace to
782 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800783 (*new_auth_tok)->session_key.flags &=
784 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
785 (*new_auth_tok)->session_key.flags &=
786 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700787 list_add(&auth_tok_list_item->list, auth_tok_list);
788 goto out;
789out_free:
790 (*new_auth_tok) = NULL;
791 memset(auth_tok_list_item, 0,
792 sizeof(struct ecryptfs_auth_tok_list_item));
793 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
794 auth_tok_list_item);
795out:
796 if (rc)
797 (*packet_size) = 0;
798 return rc;
799}
800
801/**
802 * parse_tag_11_packet
803 * @data: The raw bytes of the packet
804 * @contents: This function writes the data contents of the literal
805 * packet into this memory location
806 * @max_contents_bytes: The maximum number of bytes that this function
807 * is allowed to write into contents
808 * @tag_11_contents_size: This function writes the size of the parsed
809 * contents into this memory location; zero on
810 * error
811 * @packet_size: This function writes the size of the parsed packet
812 * into this memory location; zero on error
813 * @max_packet_size: maximum number of bytes to parse
814 *
815 * Returns zero on success; non-zero on error.
816 */
817static int
818parse_tag_11_packet(unsigned char *data, unsigned char *contents,
819 size_t max_contents_bytes, size_t *tag_11_contents_size,
820 size_t *packet_size, size_t max_packet_size)
821{
Michael Halcrow237fead2006-10-04 02:16:22 -0700822 size_t body_size;
823 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800824 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700825
826 (*packet_size) = 0;
827 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -0700828 /* This format is inspired by OpenPGP; see RFC 2440
829 * packet tag 11
830 *
831 * Tag 11 identifier (1 byte)
832 * Max Tag 11 packet size (max 3 bytes)
833 * Binary format specifier (1 byte)
834 * Filename length (1 byte)
835 * Filename ("_CONSOLE") (8 bytes)
836 * Modification date (4 bytes)
837 * Literal data (arbitrary)
838 *
839 * We need at least 16 bytes of data for the packet to even be
840 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -0700841 */
Michael Halcrowf6481042007-10-16 01:27:57 -0700842 if (max_packet_size < 16) {
843 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700844 rc = -EINVAL;
845 goto out;
846 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700847 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700848 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700849 rc = -EINVAL;
850 goto out;
851 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700852 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
853 &length_size))) {
854 printk(KERN_WARNING "Invalid tag 11 packet format\n");
855 goto out;
856 }
857 if (body_size < 14) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700858 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowf6481042007-10-16 01:27:57 -0700859 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700860 goto out;
861 }
862 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -0700863 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -0700864 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700865 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700866 rc = -EINVAL;
867 goto out;
868 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700869 if (data[(*packet_size)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700870 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700871 rc = -EINVAL;
872 goto out;
873 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700874 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700875 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700876 rc = -EINVAL;
877 goto out;
878 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700879 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -0700880 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
881 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700882out:
883 if (rc) {
884 (*packet_size) = 0;
885 (*tag_11_contents_size) = 0;
886 }
887 return rc;
888}
889
Michael Halcrowf4aad162007-10-16 01:27:53 -0700890static int
891ecryptfs_find_global_auth_tok_for_sig(
892 struct ecryptfs_global_auth_tok **global_auth_tok,
893 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
894{
895 struct ecryptfs_global_auth_tok *walker;
896 int rc = 0;
897
898 (*global_auth_tok) = NULL;
899 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
900 list_for_each_entry(walker,
901 &mount_crypt_stat->global_auth_tok_list,
902 mount_crypt_stat_list) {
903 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
904 (*global_auth_tok) = walker;
905 goto out;
906 }
907 }
908 rc = -EINVAL;
909out:
910 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
911 return rc;
912}
913
Michael Halcrow237fead2006-10-04 02:16:22 -0700914/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700915 * ecryptfs_verify_version
916 * @version: The version number to confirm
917 *
918 * Returns zero on good version; non-zero otherwise
919 */
920static int ecryptfs_verify_version(u16 version)
921{
922 int rc = 0;
923 unsigned char major;
924 unsigned char minor;
925
926 major = ((version >> 8) & 0xFF);
927 minor = (version & 0xFF);
928 if (major != ECRYPTFS_VERSION_MAJOR) {
929 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
930 "Expected [%d]; got [%d]\n",
931 ECRYPTFS_VERSION_MAJOR, major);
932 rc = -EINVAL;
933 goto out;
934 }
935 if (minor != ECRYPTFS_VERSION_MINOR) {
936 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
937 "Expected [%d]; got [%d]\n",
938 ECRYPTFS_VERSION_MINOR, minor);
939 rc = -EINVAL;
940 goto out;
941 }
942out:
943 return rc;
944}
945
946int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
947 struct ecryptfs_auth_tok **auth_tok,
948 char *sig)
949{
950 int rc = 0;
951
952 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
953 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
954 printk(KERN_ERR "Could not find key with description: [%s]\n",
955 sig);
956 process_request_key_err(PTR_ERR(*auth_tok_key));
957 rc = -EINVAL;
958 goto out;
959 }
960 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
961 if (ecryptfs_verify_version((*auth_tok)->version)) {
962 printk(KERN_ERR
963 "Data structure version mismatch. "
964 "Userspace tools must match eCryptfs "
965 "kernel module with major version [%d] "
966 "and minor version [%d]\n",
967 ECRYPTFS_VERSION_MAJOR,
968 ECRYPTFS_VERSION_MINOR);
969 rc = -EINVAL;
970 goto out;
971 }
972 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
973 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
974 printk(KERN_ERR "Invalid auth_tok structure "
975 "returned from key query\n");
976 rc = -EINVAL;
977 goto out;
978 }
979out:
980 return rc;
981}
982
983/**
984 * ecryptfs_find_auth_tok_for_sig
985 * @auth_tok: Set to the matching auth_tok; NULL if not found
986 * @crypt_stat: inode crypt_stat crypto context
987 * @sig: Sig of auth_tok to find
988 *
989 * For now, this function simply looks at the registered auth_tok's
990 * linked off the mount_crypt_stat, so all the auth_toks that can be
991 * used must be registered at mount time. This function could
992 * potentially try a lot harder to find auth_tok's (e.g., by calling
993 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
994 * that static registration of auth_tok's will no longer be necessary.
995 *
996 * Returns zero on no error; non-zero on error
997 */
998static int
999ecryptfs_find_auth_tok_for_sig(
1000 struct ecryptfs_auth_tok **auth_tok,
1001 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1002{
1003 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1004 crypt_stat->mount_crypt_stat;
1005 struct ecryptfs_global_auth_tok *global_auth_tok;
1006 int rc = 0;
1007
1008 (*auth_tok) = NULL;
1009 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1010 mount_crypt_stat, sig)) {
1011 struct key *auth_tok_key;
1012
1013 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1014 sig);
1015 } else
1016 (*auth_tok) = global_auth_tok->global_auth_tok;
1017 return rc;
1018}
1019
1020/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001021 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1022 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1023 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001024 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001025 * Returns zero on success; non-zero error otherwise
Michael Halcrow237fead2006-10-04 02:16:22 -07001026 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001027static int
1028decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1029 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001030{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001031 struct scatterlist dst_sg;
1032 struct scatterlist src_sg;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001033 struct mutex *tfm_mutex;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001034 struct blkcipher_desc desc = {
1035 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1036 };
1037 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001038
Michael Halcrowf4aad162007-10-16 01:27:53 -07001039 if (unlikely(ecryptfs_verbosity > 0)) {
1040 ecryptfs_printk(
1041 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1042 auth_tok->token.password.session_key_encryption_key_bytes);
1043 ecryptfs_dump_hex(
1044 auth_tok->token.password.session_key_encryption_key,
1045 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001046 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001047 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1048 crypt_stat->cipher);
1049 if (unlikely(rc)) {
1050 printk(KERN_ERR "Internal error whilst attempting to get "
1051 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1052 crypt_stat->cipher, rc);
1053 goto out;
1054 }
1055 if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1056 auth_tok->session_key.encrypted_key_size,
1057 &src_sg, 1)) != 1) {
1058 printk(KERN_ERR "Internal error whilst attempting to convert "
1059 "auth_tok->session_key.encrypted_key to scatterlist; "
1060 "expected rc = 1; got rc = [%d]. "
1061 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1062 auth_tok->session_key.encrypted_key_size);
1063 goto out;
1064 }
1065 auth_tok->session_key.decrypted_key_size =
1066 auth_tok->session_key.encrypted_key_size;
1067 if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1068 auth_tok->session_key.decrypted_key_size,
1069 &dst_sg, 1)) != 1) {
1070 printk(KERN_ERR "Internal error whilst attempting to convert "
1071 "auth_tok->session_key.decrypted_key to scatterlist; "
1072 "expected rc = 1; got rc = [%d]\n", rc);
1073 goto out;
1074 }
1075 mutex_lock(tfm_mutex);
1076 rc = crypto_blkcipher_setkey(
1077 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1078 crypt_stat->key_size);
1079 if (unlikely(rc < 0)) {
1080 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001081 printk(KERN_ERR "Error setting key for crypto context\n");
1082 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001083 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001084 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001085 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001086 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001087 mutex_unlock(tfm_mutex);
1088 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001089 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001090 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001091 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001092 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1093 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1094 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001095 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001096 if (unlikely(ecryptfs_verbosity > 0)) {
1097 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1098 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001099 ecryptfs_dump_hex(crypt_stat->key,
1100 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001101 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001102out:
1103 return rc;
1104}
1105
1106/**
1107 * ecryptfs_parse_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001108 * @crypt_stat: The cryptographic context
1109 * @src: Virtual address of region of memory containing the packets
1110 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
Michael Halcrow237fead2006-10-04 02:16:22 -07001111 *
1112 * Get crypt_stat to have the file's session key if the requisite key
1113 * is available to decrypt the session key.
1114 *
1115 * Returns Zero if a valid authentication token was retrieved and
1116 * processed; negative value for file not encrypted or for error
1117 * conditions.
1118 */
1119int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1120 unsigned char *src,
1121 struct dentry *ecryptfs_dentry)
1122{
1123 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001124 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001125 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001126 struct list_head auth_tok_list;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001127 struct ecryptfs_auth_tok *matching_auth_tok;
1128 struct ecryptfs_auth_tok *candidate_auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001129 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001130 size_t packet_size;
1131 struct ecryptfs_auth_tok *new_auth_tok;
1132 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001133 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001134 size_t tag_11_contents_size;
1135 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001136 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001137
1138 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001139 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001140 * added the our &auth_tok_list */
1141 next_packet_is_auth_tok_packet = 1;
1142 while (next_packet_is_auth_tok_packet) {
1143 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1144
1145 switch (src[i]) {
1146 case ECRYPTFS_TAG_3_PACKET_TYPE:
1147 rc = parse_tag_3_packet(crypt_stat,
1148 (unsigned char *)&src[i],
1149 &auth_tok_list, &new_auth_tok,
1150 &packet_size, max_packet_size);
1151 if (rc) {
1152 ecryptfs_printk(KERN_ERR, "Error parsing "
1153 "tag 3 packet\n");
1154 rc = -EIO;
1155 goto out_wipe_list;
1156 }
1157 i += packet_size;
1158 rc = parse_tag_11_packet((unsigned char *)&src[i],
1159 sig_tmp_space,
1160 ECRYPTFS_SIG_SIZE,
1161 &tag_11_contents_size,
1162 &tag_11_packet_size,
1163 max_packet_size);
1164 if (rc) {
1165 ecryptfs_printk(KERN_ERR, "No valid "
1166 "(ecryptfs-specific) literal "
1167 "packet containing "
1168 "authentication token "
1169 "signature found after "
1170 "tag 3 packet\n");
1171 rc = -EIO;
1172 goto out_wipe_list;
1173 }
1174 i += tag_11_packet_size;
1175 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1176 ecryptfs_printk(KERN_ERR, "Expected "
1177 "signature of size [%d]; "
1178 "read size [%d]\n",
1179 ECRYPTFS_SIG_SIZE,
1180 tag_11_contents_size);
1181 rc = -EIO;
1182 goto out_wipe_list;
1183 }
1184 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1185 sig_tmp_space, tag_11_contents_size);
1186 new_auth_tok->token.password.signature[
1187 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001188 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001189 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001190 case ECRYPTFS_TAG_1_PACKET_TYPE:
1191 rc = parse_tag_1_packet(crypt_stat,
1192 (unsigned char *)&src[i],
1193 &auth_tok_list, &new_auth_tok,
1194 &packet_size, max_packet_size);
1195 if (rc) {
1196 ecryptfs_printk(KERN_ERR, "Error parsing "
1197 "tag 1 packet\n");
1198 rc = -EIO;
1199 goto out_wipe_list;
1200 }
1201 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001202 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001203 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001204 case ECRYPTFS_TAG_11_PACKET_TYPE:
1205 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1206 "(Tag 11 not allowed by itself)\n");
1207 rc = -EIO;
1208 goto out_wipe_list;
1209 break;
1210 default:
1211 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1212 "[%d] of the file header; hex value of "
1213 "character is [0x%.2x]\n", i, src[i]);
1214 next_packet_is_auth_tok_packet = 0;
1215 }
1216 }
1217 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001218 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1219 "eCryptfs file; this is not supported in this version "
1220 "of the eCryptfs kernel module\n");
1221 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001222 goto out;
1223 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001224 /* auth_tok_list contains the set of authentication tokens
1225 * parsed from the metadata. We need to find a matching
1226 * authentication token that has the secret component(s)
1227 * necessary to decrypt the EFEK in the auth_tok parsed from
1228 * the metadata. There may be several potential matches, but
1229 * just one will be sufficient to decrypt to get the FEK. */
1230find_next_matching_auth_tok:
1231 found_auth_tok = 0;
1232 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001233 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1234 if (unlikely(ecryptfs_verbosity > 0)) {
1235 ecryptfs_printk(KERN_DEBUG,
1236 "Considering cadidate auth tok:\n");
1237 ecryptfs_dump_auth_tok(candidate_auth_tok);
1238 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001239 if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1240 candidate_auth_tok))) {
1241 printk(KERN_ERR
1242 "Unrecognized candidate auth tok type: [%d]\n",
1243 candidate_auth_tok->token_type);
1244 rc = -EINVAL;
1245 goto out_wipe_list;
1246 }
1247 if ((rc = ecryptfs_find_auth_tok_for_sig(
1248 &matching_auth_tok, crypt_stat,
1249 candidate_auth_tok_sig)))
1250 rc = 0;
1251 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001252 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001253 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001254 }
1255 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001256 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001257 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1258 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001259 rc = -EIO;
1260 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001261 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001262found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001263 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001264 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001265 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001266 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001267 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001268 crypt_stat);
1269 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001270 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001271 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001272 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001273 rc = decrypt_passphrase_encrypted_session_key(
1274 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001275 }
1276 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001277 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1278
1279 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1280 "session key for authentication token with sig "
1281 "[%.*s]; rc = [%d]. Removing auth tok "
1282 "candidate from the list and searching for "
1283 "the next match.\n", candidate_auth_tok_sig,
1284 ECRYPTFS_SIG_SIZE_HEX, rc);
1285 list_for_each_entry_safe(auth_tok_list_item,
1286 auth_tok_list_item_tmp,
1287 &auth_tok_list, list) {
1288 if (candidate_auth_tok
1289 == &auth_tok_list_item->auth_tok) {
1290 list_del(&auth_tok_list_item->list);
1291 kmem_cache_free(
1292 ecryptfs_auth_tok_list_item_cache,
1293 auth_tok_list_item);
1294 goto find_next_matching_auth_tok;
1295 }
1296 }
1297 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001298 }
1299 rc = ecryptfs_compute_root_iv(crypt_stat);
1300 if (rc) {
1301 ecryptfs_printk(KERN_ERR, "Error computing "
1302 "the root IV\n");
1303 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001304 }
1305 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1306 if (rc) {
1307 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1308 "context for cipher [%s]; rc = [%d]\n",
1309 crypt_stat->cipher, rc);
1310 }
1311out_wipe_list:
1312 wipe_auth_tok_list(&auth_tok_list);
1313out:
1314 return rc;
1315}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001316
Michael Halcrowdddfa462007-02-12 00:53:44 -08001317static int
1318pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1319 struct ecryptfs_crypt_stat *crypt_stat,
1320 struct ecryptfs_key_record *key_rec)
1321{
1322 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1323 char *netlink_payload;
1324 size_t netlink_payload_length;
1325 struct ecryptfs_message *msg;
1326 int rc;
1327
1328 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1329 ecryptfs_code_for_cipher_string(crypt_stat),
1330 crypt_stat, &netlink_payload,
1331 &netlink_payload_length);
1332 if (rc) {
1333 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1334 goto out;
1335 }
1336 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1337 netlink_payload_length, &msg_ctx);
1338 if (rc) {
1339 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1340 goto out;
1341 }
1342 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1343 if (rc) {
1344 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1345 "from the user space daemon\n");
1346 rc = -EIO;
1347 goto out;
1348 }
1349 rc = parse_tag_67_packet(key_rec, msg);
1350 if (rc)
1351 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1352 kfree(msg);
1353out:
1354 if (netlink_payload)
1355 kfree(netlink_payload);
1356 return rc;
1357}
1358/**
1359 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1360 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001361 * @remaining_bytes: Maximum number of bytes that can be writtn
1362 * @auth_tok: The authentication token used for generating the tag 1 packet
1363 * @crypt_stat: The cryptographic context
1364 * @key_rec: The key record struct for the tag 1 packet
Michael Halcrowdddfa462007-02-12 00:53:44 -08001365 * @packet_size: This function will write the number of bytes that end
1366 * up constituting the packet; set to zero on error
1367 *
1368 * Returns zero on success; non-zero on error.
1369 */
1370static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001371write_tag_1_packet(char *dest, size_t *remaining_bytes,
1372 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001373 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001374 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1375{
1376 size_t i;
1377 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001378 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001379 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001380 int rc = 0;
1381
1382 (*packet_size) = 0;
1383 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1384 ECRYPTFS_SIG_SIZE);
1385 encrypted_session_key_valid = 0;
1386 for (i = 0; i < crypt_stat->key_size; i++)
1387 encrypted_session_key_valid |=
1388 auth_tok->session_key.encrypted_key[i];
1389 if (encrypted_session_key_valid) {
1390 memcpy(key_rec->enc_key,
1391 auth_tok->session_key.encrypted_key,
1392 auth_tok->session_key.encrypted_key_size);
1393 goto encrypted_session_key_set;
1394 }
1395 if (auth_tok->session_key.encrypted_key_size == 0)
1396 auth_tok->session_key.encrypted_key_size =
1397 auth_tok->token.private_key.key_size;
1398 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1399 if (rc) {
1400 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1401 "via a pki");
1402 goto out;
1403 }
1404 if (ecryptfs_verbosity > 0) {
1405 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1406 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1407 }
1408encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001409 /* This format is inspired by OpenPGP; see RFC 2440
1410 * packet tag 1 */
1411 max_packet_size = (1 /* Tag 1 identifier */
1412 + 3 /* Max Tag 1 packet size */
1413 + 1 /* Version */
1414 + ECRYPTFS_SIG_SIZE /* Key identifier */
1415 + 1 /* Cipher identifier */
1416 + key_rec->enc_key_size); /* Encrypted key size */
1417 if (max_packet_size > (*remaining_bytes)) {
1418 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001419 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001420 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001421 rc = -EINVAL;
1422 goto out;
1423 }
1424 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001425 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001426 &packet_size_length);
1427 if (rc) {
1428 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1429 "header; cannot generate packet length\n");
1430 goto out;
1431 }
1432 (*packet_size) += packet_size_length;
1433 dest[(*packet_size)++] = 0x03; /* version 3 */
1434 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1435 (*packet_size) += ECRYPTFS_SIG_SIZE;
1436 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1437 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1438 key_rec->enc_key_size);
1439 (*packet_size) += key_rec->enc_key_size;
1440out:
1441 if (rc)
1442 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001443 else
1444 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001445 return rc;
1446}
Michael Halcrow237fead2006-10-04 02:16:22 -07001447
1448/**
1449 * write_tag_11_packet
1450 * @dest: Target into which Tag 11 packet is to be written
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001451 * @remaining_bytes: Maximum packet length
Michael Halcrow237fead2006-10-04 02:16:22 -07001452 * @contents: Byte array of contents to copy in
1453 * @contents_length: Number of bytes in contents
1454 * @packet_length: Length of the Tag 11 packet written; zero on error
1455 *
1456 * Returns zero on success; non-zero on error.
1457 */
1458static int
Andrew Morton81acbcd2007-10-16 01:27:59 -07001459write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
Michael Halcrow146a4602007-10-16 01:27:58 -07001460 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07001461{
Michael Halcrow237fead2006-10-04 02:16:22 -07001462 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001463 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001464 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001465
1466 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001467 /* This format is inspired by OpenPGP; see RFC 2440
1468 * packet tag 11 */
1469 max_packet_size = (1 /* Tag 11 identifier */
1470 + 3 /* Max Tag 11 packet size */
1471 + 1 /* Binary format specifier */
1472 + 1 /* Filename length */
1473 + 8 /* Filename ("_CONSOLE") */
1474 + 4 /* Modification date */
1475 + contents_length); /* Literal data */
1476 if (max_packet_size > (*remaining_bytes)) {
1477 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001478 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrow146a4602007-10-16 01:27:58 -07001479 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07001480 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001481 goto out;
1482 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001483 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001484 rc = write_packet_length(&dest[(*packet_length)],
Michael Halcrow146a4602007-10-16 01:27:58 -07001485 (max_packet_size - 4), &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001486 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07001487 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1488 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001489 goto out;
1490 }
1491 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001492 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07001493 dest[(*packet_length)++] = 8;
1494 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1495 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07001496 memset(&dest[(*packet_length)], 0x00, 4);
1497 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07001498 memcpy(&dest[(*packet_length)], contents, contents_length);
1499 (*packet_length) += contents_length;
1500 out:
1501 if (rc)
1502 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001503 else
1504 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001505 return rc;
1506}
1507
1508/**
1509 * write_tag_3_packet
1510 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001511 * @remaining_bytes: Maximum number of bytes that can be written
Michael Halcrow237fead2006-10-04 02:16:22 -07001512 * @auth_tok: Authentication token
1513 * @crypt_stat: The cryptographic context
1514 * @key_rec: encrypted key
1515 * @packet_size: This function will write the number of bytes that end
1516 * up constituting the packet; set to zero on error
1517 *
1518 * Returns zero on success; non-zero on error.
1519 */
1520static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001521write_tag_3_packet(char *dest, size_t *remaining_bytes,
1522 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001523 struct ecryptfs_crypt_stat *crypt_stat,
1524 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1525{
Michael Halcrow237fead2006-10-04 02:16:22 -07001526 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001527 size_t encrypted_session_key_valid = 0;
1528 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001529 struct scatterlist dst_sg;
1530 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001531 struct mutex *tfm_mutex = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001532 size_t cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001533 size_t packet_size_length;
1534 size_t max_packet_size;
1535 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1536 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001537 struct blkcipher_desc desc = {
1538 .tfm = NULL,
1539 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1540 };
1541 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001542
1543 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001544 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001545 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001546 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1547 crypt_stat->cipher);
1548 if (unlikely(rc)) {
1549 printk(KERN_ERR "Internal error whilst attempting to get "
1550 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1551 crypt_stat->cipher, rc);
1552 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001553 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001554 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1555 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1556
1557 printk(KERN_WARNING "No key size specified at mount; "
1558 "defaulting to [%d]\n", alg->max_keysize);
1559 mount_crypt_stat->global_default_cipher_key_size =
1560 alg->max_keysize;
1561 }
1562 if (crypt_stat->key_size == 0)
1563 crypt_stat->key_size =
1564 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001565 if (auth_tok->session_key.encrypted_key_size == 0)
1566 auth_tok->session_key.encrypted_key_size =
1567 crypt_stat->key_size;
1568 if (crypt_stat->key_size == 24
1569 && strcmp("aes", crypt_stat->cipher) == 0) {
1570 memset((crypt_stat->key + 24), 0, 8);
1571 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001572 } else
1573 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001574 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001575 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001576 encrypted_session_key_valid = 0;
1577 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1578 encrypted_session_key_valid |=
1579 auth_tok->session_key.encrypted_key[i];
1580 if (encrypted_session_key_valid) {
1581 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1582 "using auth_tok->session_key.encrypted_key, "
1583 "where key_rec->enc_key_size = [%d]\n",
1584 key_rec->enc_key_size);
1585 memcpy(key_rec->enc_key,
1586 auth_tok->session_key.encrypted_key,
1587 key_rec->enc_key_size);
1588 goto encrypted_session_key_set;
1589 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001590 if (auth_tok->token.password.flags &
1591 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001592 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1593 "session key encryption key of size [%d]\n",
1594 auth_tok->token.password.
1595 session_key_encryption_key_bytes);
1596 memcpy(session_key_encryption_key,
1597 auth_tok->token.password.session_key_encryption_key,
1598 crypt_stat->key_size);
1599 ecryptfs_printk(KERN_DEBUG,
1600 "Cached session key " "encryption key: \n");
1601 if (ecryptfs_verbosity > 0)
1602 ecryptfs_dump_hex(session_key_encryption_key, 16);
1603 }
1604 if (unlikely(ecryptfs_verbosity > 0)) {
1605 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1606 ecryptfs_dump_hex(session_key_encryption_key, 16);
1607 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001608 if ((rc = virt_to_scatterlist(crypt_stat->key,
1609 key_rec->enc_key_size, &src_sg, 1))
1610 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001611 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001612 "for crypt_stat session key; expected rc = 1; "
1613 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1614 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001615 rc = -ENOMEM;
1616 goto out;
1617 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001618 if ((rc = virt_to_scatterlist(key_rec->enc_key,
1619 key_rec->enc_key_size, &dst_sg, 1))
1620 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001621 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001622 "for crypt_stat encrypted session key; "
1623 "expected rc = 1; got rc = [%d]. "
1624 "key_rec->enc_key_size = [%d]\n", rc,
1625 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001626 rc = -ENOMEM;
1627 goto out;
1628 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001629 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001630 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1631 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001632 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001633 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001634 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001635 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001636 goto out;
1637 }
1638 rc = 0;
1639 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1640 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001641 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001642 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001643 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001644 if (rc) {
1645 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1646 goto out;
1647 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001648 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001649 if (ecryptfs_verbosity > 0) {
1650 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1651 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001652 ecryptfs_dump_hex(key_rec->enc_key,
1653 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001654 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001655encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001656 /* This format is inspired by OpenPGP; see RFC 2440
1657 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001658 max_packet_size = (1 /* Tag 3 identifier */
1659 + 3 /* Max Tag 3 packet size */
1660 + 1 /* Version */
1661 + 1 /* Cipher code */
1662 + 1 /* S2K specifier */
1663 + 1 /* Hash identifier */
1664 + ECRYPTFS_SALT_SIZE /* Salt */
1665 + 1 /* Hash iterations */
1666 + key_rec->enc_key_size); /* Encrypted key size */
1667 if (max_packet_size > (*remaining_bytes)) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001668 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
1669 "there are only [%td] available\n", max_packet_size,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001670 (*remaining_bytes));
1671 rc = -EINVAL;
1672 goto out;
1673 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001674 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001675 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1676 * to get the number of octets in the actual Tag 3 packet */
1677 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrow237fead2006-10-04 02:16:22 -07001678 &packet_size_length);
1679 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001680 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1681 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001682 goto out;
1683 }
1684 (*packet_size) += packet_size_length;
1685 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001686 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1687 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001688 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1689 if (cipher_code == 0) {
1690 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1691 "cipher [%s]\n", crypt_stat->cipher);
1692 rc = -EINVAL;
1693 goto out;
1694 }
1695 dest[(*packet_size)++] = cipher_code;
1696 dest[(*packet_size)++] = 0x03; /* S2K */
1697 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1698 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1699 ECRYPTFS_SALT_SIZE);
1700 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1701 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001702 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1703 key_rec->enc_key_size);
1704 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001705out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001706 if (rc)
1707 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001708 else
1709 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001710 return rc;
1711}
1712
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001713struct kmem_cache *ecryptfs_key_record_cache;
1714
Michael Halcrow237fead2006-10-04 02:16:22 -07001715/**
1716 * ecryptfs_generate_key_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001717 * @dest_base: Virtual address from which to write the key record set
Michael Halcrow237fead2006-10-04 02:16:22 -07001718 * @crypt_stat: The cryptographic context from which the
1719 * authentication tokens will be retrieved
1720 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1721 * for the global parameters
1722 * @len: The amount written
1723 * @max: The maximum amount of data allowed to be written
1724 *
1725 * Generates a key packet set and writes it to the virtual address
1726 * passed in.
1727 *
1728 * Returns zero on success; non-zero on error.
1729 */
1730int
1731ecryptfs_generate_key_packet_set(char *dest_base,
1732 struct ecryptfs_crypt_stat *crypt_stat,
1733 struct dentry *ecryptfs_dentry, size_t *len,
1734 size_t max)
1735{
Michael Halcrow237fead2006-10-04 02:16:22 -07001736 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001737 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001738 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1739 &ecryptfs_superblock_to_private(
1740 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1741 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001742 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001743 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001744 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001745
1746 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001747 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001748 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1749 if (!key_rec) {
1750 rc = -ENOMEM;
1751 goto out;
1752 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001753 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1754 crypt_stat_list) {
1755 memset(key_rec, 0, sizeof(*key_rec));
1756 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1757 mount_crypt_stat,
1758 key_sig->keysig);
1759 if (rc) {
1760 printk(KERN_ERR "Error attempting to get the global "
1761 "auth_tok; rc = [%d]\n", rc);
1762 goto out_free;
1763 }
1764 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1765 printk(KERN_WARNING
1766 "Skipping invalid auth tok with sig = [%s]\n",
1767 global_auth_tok->sig);
1768 continue;
1769 }
1770 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001771 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1772 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001773 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001774 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001775 &written);
1776 if (rc) {
1777 ecryptfs_printk(KERN_WARNING, "Error "
1778 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001779 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001780 }
1781 (*len) += written;
1782 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001783 rc = write_tag_11_packet((dest_base + (*len)), &max,
1784 key_rec->sig,
1785 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001786 if (rc) {
1787 ecryptfs_printk(KERN_ERR, "Error writing "
1788 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001789 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001790 }
1791 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001792 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1793 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001794 &max, auth_tok,
1795 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001796 if (rc) {
1797 ecryptfs_printk(KERN_WARNING, "Error "
1798 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001799 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001800 }
1801 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001802 } else {
1803 ecryptfs_printk(KERN_WARNING, "Unsupported "
1804 "authentication token type\n");
1805 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001806 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001807 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001808 }
1809 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001810 dest_base[(*len)] = 0x00;
1811 } else {
1812 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1813 rc = -EIO;
1814 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001815out_free:
1816 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001817out:
1818 if (rc)
1819 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001820 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001821 return rc;
1822}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001823
1824struct kmem_cache *ecryptfs_key_sig_cache;
1825
1826int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1827{
1828 struct ecryptfs_key_sig *new_key_sig;
1829 int rc = 0;
1830
1831 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1832 if (!new_key_sig) {
1833 rc = -ENOMEM;
1834 printk(KERN_ERR
1835 "Error allocating from ecryptfs_key_sig_cache\n");
1836 goto out;
1837 }
1838 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1839 mutex_lock(&crypt_stat->keysig_list_mutex);
1840 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1841 mutex_unlock(&crypt_stat->keysig_list_mutex);
1842out:
1843 return rc;
1844}
1845
1846struct kmem_cache *ecryptfs_global_auth_tok_cache;
1847
1848int
1849ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1850 char *sig)
1851{
1852 struct ecryptfs_global_auth_tok *new_auth_tok;
1853 int rc = 0;
1854
1855 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1856 GFP_KERNEL);
1857 if (!new_auth_tok) {
1858 rc = -ENOMEM;
1859 printk(KERN_ERR "Error allocating from "
1860 "ecryptfs_global_auth_tok_cache\n");
1861 goto out;
1862 }
1863 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1864 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1865 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1866 list_add(&new_auth_tok->mount_crypt_stat_list,
1867 &mount_crypt_stat->global_auth_tok_list);
1868 mount_crypt_stat->num_global_auth_toks++;
1869 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1870out:
1871 return rc;
1872}
1873