blob: 190e2a01d8bdf18e409b92f9b3e8b327908ed00f [file] [log] [blame]
Michael Halcrow237fead2006-10-04 02:16:22 -07001/**
2 * eCryptfs: Linux filesystem encryption layer
3 * In-kernel key management code. Includes functions to parse and
4 * write authentication token-related packets with the underlying
5 * file.
6 *
7 * Copyright (C) 2004-2006 International Business Machines Corp.
8 * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com>
9 * Michael C. Thompson <mcthomps@us.ibm.com>
Michael Halcrowdddfa462007-02-12 00:53:44 -080010 * Trevor S. Highland <trevor.highland@gmail.com>
Michael Halcrow237fead2006-10-04 02:16:22 -070011 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 * 02111-1307, USA.
26 */
27
28#include <linux/string.h>
Michael Halcrow237fead2006-10-04 02:16:22 -070029#include <linux/syscalls.h>
30#include <linux/pagemap.h>
31#include <linux/key.h>
32#include <linux/random.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include "ecryptfs_kernel.h"
36
37/**
38 * request_key returned an error instead of a valid key address;
39 * determine the type of error, make appropriate log entries, and
40 * return an error code.
41 */
42int process_request_key_err(long err_code)
43{
44 int rc = 0;
45
46 switch (err_code) {
47 case ENOKEY:
48 ecryptfs_printk(KERN_WARNING, "No key\n");
49 rc = -ENOENT;
50 break;
51 case EKEYEXPIRED:
52 ecryptfs_printk(KERN_WARNING, "Key expired\n");
53 rc = -ETIME;
54 break;
55 case EKEYREVOKED:
56 ecryptfs_printk(KERN_WARNING, "Key revoked\n");
57 rc = -EINVAL;
58 break;
59 default:
60 ecryptfs_printk(KERN_WARNING, "Unknown error code: "
61 "[0x%.16x]\n", err_code);
62 rc = -EINVAL;
63 }
64 return rc;
65}
66
Michael Halcrow237fead2006-10-04 02:16:22 -070067/**
68 * parse_packet_length
69 * @data: Pointer to memory containing length at offset
70 * @size: This function writes the decoded size to this memory
71 * address; zero on error
72 * @length_size: The number of bytes occupied by the encoded length
73 *
74 * Returns Zero on success
75 */
76static int parse_packet_length(unsigned char *data, size_t *size,
77 size_t *length_size)
78{
79 int rc = 0;
80
81 (*length_size) = 0;
82 (*size) = 0;
83 if (data[0] < 192) {
84 /* One-byte length */
Michael Halcrowdddfa462007-02-12 00:53:44 -080085 (*size) = (unsigned char)data[0];
Michael Halcrow237fead2006-10-04 02:16:22 -070086 (*length_size) = 1;
87 } else if (data[0] < 224) {
88 /* Two-byte length */
Michael Halcrowdddfa462007-02-12 00:53:44 -080089 (*size) = (((unsigned char)(data[0]) - 192) * 256);
90 (*size) += ((unsigned char)(data[1]) + 192);
Michael Halcrow237fead2006-10-04 02:16:22 -070091 (*length_size) = 2;
92 } else if (data[0] == 255) {
93 /* Five-byte length; we're not supposed to see this */
94 ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
95 "supported\n");
96 rc = -EINVAL;
97 goto out;
98 } else {
99 ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
100 rc = -EINVAL;
101 goto out;
102 }
103out:
104 return rc;
105}
106
107/**
108 * write_packet_length
109 * @dest: The byte array target into which to write the
110 * length. Must have at least 5 bytes allocated.
111 * @size: The length to write.
112 * @packet_size_length: The number of bytes used to encode the
113 * packet length is written to this address.
114 *
115 * Returns zero on success; non-zero on error.
116 */
117static int write_packet_length(char *dest, size_t size,
118 size_t *packet_size_length)
119{
120 int rc = 0;
121
122 if (size < 192) {
123 dest[0] = size;
124 (*packet_size_length) = 1;
125 } else if (size < 65536) {
126 dest[0] = (((size - 192) / 256) + 192);
127 dest[1] = ((size - 192) % 256);
128 (*packet_size_length) = 2;
129 } else {
130 rc = -EINVAL;
131 ecryptfs_printk(KERN_WARNING,
132 "Unsupported packet size: [%d]\n", size);
133 }
134 return rc;
135}
136
Michael Halcrowdddfa462007-02-12 00:53:44 -0800137static int
138write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key,
139 char **packet, size_t *packet_len)
140{
141 size_t i = 0;
142 size_t data_len;
143 size_t packet_size_len;
144 char *message;
145 int rc;
146
147 /*
148 * ***** TAG 64 Packet Format *****
149 * | Content Type | 1 byte |
150 * | Key Identifier Size | 1 or 2 bytes |
151 * | Key Identifier | arbitrary |
152 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
153 * | Encrypted File Encryption Key | arbitrary |
154 */
155 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX
156 + session_key->encrypted_key_size);
157 *packet = kmalloc(data_len, GFP_KERNEL);
158 message = *packet;
159 if (!message) {
160 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
161 rc = -ENOMEM;
162 goto out;
163 }
164 message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE;
165 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
166 &packet_size_len);
167 if (rc) {
168 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
169 "header; cannot generate packet length\n");
170 goto out;
171 }
172 i += packet_size_len;
173 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
174 i += ECRYPTFS_SIG_SIZE_HEX;
175 rc = write_packet_length(&message[i], session_key->encrypted_key_size,
176 &packet_size_len);
177 if (rc) {
178 ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet "
179 "header; cannot generate packet length\n");
180 goto out;
181 }
182 i += packet_size_len;
183 memcpy(&message[i], session_key->encrypted_key,
184 session_key->encrypted_key_size);
185 i += session_key->encrypted_key_size;
186 *packet_len = i;
187out:
188 return rc;
189}
190
191static int
192parse_tag_65_packet(struct ecryptfs_session_key *session_key, u16 *cipher_code,
193 struct ecryptfs_message *msg)
194{
195 size_t i = 0;
196 char *data;
197 size_t data_len;
198 size_t m_size;
199 size_t message_len;
200 u16 checksum = 0;
201 u16 expected_checksum = 0;
202 int rc;
203
204 /*
205 * ***** TAG 65 Packet Format *****
206 * | Content Type | 1 byte |
207 * | Status Indicator | 1 byte |
208 * | File Encryption Key Size | 1 or 2 bytes |
209 * | File Encryption Key | arbitrary |
210 */
211 message_len = msg->data_len;
212 data = msg->data;
213 if (message_len < 4) {
214 rc = -EIO;
215 goto out;
216 }
217 if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) {
218 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n");
219 rc = -EIO;
220 goto out;
221 }
222 if (data[i++]) {
223 ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value "
224 "[%d]\n", data[i-1]);
225 rc = -EIO;
226 goto out;
227 }
228 rc = parse_packet_length(&data[i], &m_size, &data_len);
229 if (rc) {
230 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
231 "rc = [%d]\n", rc);
232 goto out;
233 }
234 i += data_len;
235 if (message_len < (i + m_size)) {
236 ecryptfs_printk(KERN_ERR, "The received netlink message is "
237 "shorter than expected\n");
238 rc = -EIO;
239 goto out;
240 }
241 if (m_size < 3) {
242 ecryptfs_printk(KERN_ERR,
243 "The decrypted key is not long enough to "
244 "include a cipher code and checksum\n");
245 rc = -EIO;
246 goto out;
247 }
248 *cipher_code = data[i++];
249 /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
250 session_key->decrypted_key_size = m_size - 3;
251 if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
252 ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
253 "the maximum key size [%d]\n",
254 session_key->decrypted_key_size,
255 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
256 rc = -EIO;
257 goto out;
258 }
259 memcpy(session_key->decrypted_key, &data[i],
260 session_key->decrypted_key_size);
261 i += session_key->decrypted_key_size;
262 expected_checksum += (unsigned char)(data[i++]) << 8;
263 expected_checksum += (unsigned char)(data[i++]);
264 for (i = 0; i < session_key->decrypted_key_size; i++)
265 checksum += session_key->decrypted_key[i];
266 if (expected_checksum != checksum) {
267 ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
268 "encryption key; expected [%x]; calculated "
269 "[%x]\n", expected_checksum, checksum);
270 rc = -EIO;
271 }
272out:
273 return rc;
274}
275
276
277static int
278write_tag_66_packet(char *signature, size_t cipher_code,
279 struct ecryptfs_crypt_stat *crypt_stat, char **packet,
280 size_t *packet_len)
281{
282 size_t i = 0;
283 size_t j;
284 size_t data_len;
285 size_t checksum = 0;
286 size_t packet_size_len;
287 char *message;
288 int rc;
289
290 /*
291 * ***** TAG 66 Packet Format *****
292 * | Content Type | 1 byte |
293 * | Key Identifier Size | 1 or 2 bytes |
294 * | Key Identifier | arbitrary |
295 * | File Encryption Key Size | 1 or 2 bytes |
296 * | File Encryption Key | arbitrary |
297 */
298 data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size);
299 *packet = kmalloc(data_len, GFP_KERNEL);
300 message = *packet;
301 if (!message) {
302 ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
303 rc = -ENOMEM;
304 goto out;
305 }
306 message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE;
307 rc = write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX,
308 &packet_size_len);
309 if (rc) {
310 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
311 "header; cannot generate packet length\n");
312 goto out;
313 }
314 i += packet_size_len;
315 memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX);
316 i += ECRYPTFS_SIG_SIZE_HEX;
317 /* The encrypted key includes 1 byte cipher code and 2 byte checksum */
318 rc = write_packet_length(&message[i], crypt_stat->key_size + 3,
319 &packet_size_len);
320 if (rc) {
321 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet "
322 "header; cannot generate packet length\n");
323 goto out;
324 }
325 i += packet_size_len;
326 message[i++] = cipher_code;
327 memcpy(&message[i], crypt_stat->key, crypt_stat->key_size);
328 i += crypt_stat->key_size;
329 for (j = 0; j < crypt_stat->key_size; j++)
330 checksum += crypt_stat->key[j];
331 message[i++] = (checksum / 256) % 256;
332 message[i++] = (checksum % 256);
333 *packet_len = i;
334out:
335 return rc;
336}
337
338static int
339parse_tag_67_packet(struct ecryptfs_key_record *key_rec,
340 struct ecryptfs_message *msg)
341{
342 size_t i = 0;
343 char *data;
344 size_t data_len;
345 size_t message_len;
346 int rc;
347
348 /*
349 * ***** TAG 65 Packet Format *****
350 * | Content Type | 1 byte |
351 * | Status Indicator | 1 byte |
352 * | Encrypted File Encryption Key Size | 1 or 2 bytes |
353 * | Encrypted File Encryption Key | arbitrary |
354 */
355 message_len = msg->data_len;
356 data = msg->data;
357 /* verify that everything through the encrypted FEK size is present */
358 if (message_len < 4) {
359 rc = -EIO;
360 goto out;
361 }
362 if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) {
363 ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_67\n");
364 rc = -EIO;
365 goto out;
366 }
367 if (data[i++]) {
368 ecryptfs_printk(KERN_ERR, "Status indicator has non zero value"
369 " [%d]\n", data[i-1]);
370 rc = -EIO;
371 goto out;
372 }
373 rc = parse_packet_length(&data[i], &key_rec->enc_key_size, &data_len);
374 if (rc) {
375 ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
376 "rc = [%d]\n", rc);
377 goto out;
378 }
379 i += data_len;
380 if (message_len < (i + key_rec->enc_key_size)) {
381 ecryptfs_printk(KERN_ERR, "message_len [%d]; max len is [%d]\n",
382 message_len, (i + key_rec->enc_key_size));
383 rc = -EIO;
384 goto out;
385 }
386 if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
387 ecryptfs_printk(KERN_ERR, "Encrypted key_size [%d] larger than "
388 "the maximum key size [%d]\n",
389 key_rec->enc_key_size,
390 ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
391 rc = -EIO;
392 goto out;
393 }
394 memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size);
395out:
396 return rc;
397}
398
399/**
400 * decrypt_pki_encrypted_session_key - Decrypt the session key with
401 * the given auth_tok.
402 *
403 * Returns Zero on success; non-zero error otherwise.
404 */
Michael Halcrowf4aad162007-10-16 01:27:53 -0700405static int
406decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
407 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrowdddfa462007-02-12 00:53:44 -0800408{
409 u16 cipher_code = 0;
410 struct ecryptfs_msg_ctx *msg_ctx;
411 struct ecryptfs_message *msg = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -0700412 char *auth_tok_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800413 char *netlink_message;
414 size_t netlink_message_length;
415 int rc;
416
Michael Halcrowf4aad162007-10-16 01:27:53 -0700417 if ((rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok))) {
418 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
419 auth_tok->token_type);
420 goto out;
421 }
422 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800423 &netlink_message, &netlink_message_length);
424 if (rc) {
425 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
426 goto out;
427 }
428 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
429 netlink_message_length, &msg_ctx);
430 if (rc) {
431 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
432 goto out;
433 }
434 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
435 if (rc) {
436 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
437 "from the user space daemon\n");
438 rc = -EIO;
439 goto out;
440 }
441 rc = parse_tag_65_packet(&(auth_tok->session_key),
442 &cipher_code, msg);
443 if (rc) {
444 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
445 rc);
446 goto out;
447 }
448 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
449 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
450 auth_tok->session_key.decrypted_key_size);
451 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
452 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
453 if (rc) {
454 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
455 cipher_code)
456 goto out;
457 }
458 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
459 if (ecryptfs_verbosity > 0) {
460 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
461 ecryptfs_dump_hex(crypt_stat->key,
462 crypt_stat->key_size);
463 }
464out:
465 if (msg)
466 kfree(msg);
467 return rc;
468}
469
470static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
471{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800472 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700473 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800474
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700475 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
476 auth_tok_list_head, list) {
477 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800478 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
479 auth_tok_list_item);
480 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800481}
482
483struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
484
Michael Halcrowdddfa462007-02-12 00:53:44 -0800485/**
486 * parse_tag_1_packet
487 * @crypt_stat: The cryptographic context to modify based on packet
488 * contents.
489 * @data: The raw bytes of the packet.
490 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
491 * a new authentication token will be placed at the end
492 * of this list for this packet.
493 * @new_auth_tok: Pointer to a pointer to memory that this function
494 * allocates; sets the memory address of the pointer to
495 * NULL on error. This object is added to the
496 * auth_tok_list.
497 * @packet_size: This function writes the size of the parsed packet
498 * into this memory location; zero on error.
499 *
500 * Returns zero on success; non-zero on error.
501 */
502static int
503parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
504 unsigned char *data, struct list_head *auth_tok_list,
505 struct ecryptfs_auth_tok **new_auth_tok,
506 size_t *packet_size, size_t max_packet_size)
507{
508 size_t body_size;
509 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
510 size_t length_size;
511 int rc = 0;
512
513 (*packet_size) = 0;
514 (*new_auth_tok) = NULL;
Michael Halcrow13218172007-10-16 01:27:56 -0700515 /**
516 * This format is inspired by OpenPGP; see RFC 2440
517 * packet tag 1
518 *
519 * Tag 1 identifier (1 byte)
520 * Max Tag 1 packet size (max 3 bytes)
521 * Version (1 byte)
522 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
523 * Cipher identifier (1 byte)
524 * Encrypted key size (arbitrary)
525 *
526 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800527 */
Michael Halcrow13218172007-10-16 01:27:56 -0700528 if (unlikely(max_packet_size < 12)) {
529 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800530 rc = -EINVAL;
531 goto out;
532 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800533 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow13218172007-10-16 01:27:56 -0700534 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
535 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800536 rc = -EINVAL;
537 goto out;
538 }
539 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
540 * at end of function upon failure */
541 auth_tok_list_item =
Michael Halcrow13218172007-10-16 01:27:56 -0700542 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
543 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800544 if (!auth_tok_list_item) {
Michael Halcrow13218172007-10-16 01:27:56 -0700545 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800546 rc = -ENOMEM;
547 goto out;
548 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800549 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow13218172007-10-16 01:27:56 -0700550 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
551 &length_size))) {
552 printk(KERN_WARNING "Error parsing packet length; "
553 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800554 goto out_free;
555 }
Michael Halcrow13218172007-10-16 01:27:56 -0700556 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
557 printk(KERN_WARNING "Invalid body size ([%d])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800558 rc = -EINVAL;
559 goto out_free;
560 }
561 (*packet_size) += length_size;
562 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700563 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800564 rc = -EINVAL;
565 goto out_free;
566 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800567 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700568 printk(KERN_WARNING "Unknown version number [%d]\n",
569 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800570 rc = -EINVAL;
571 goto out_free;
572 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800573 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
574 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
575 *packet_size += ECRYPTFS_SIG_SIZE;
576 /* This byte is skipped because the kernel does not need to
577 * know which public key encryption algorithm was used */
578 (*packet_size)++;
579 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow13218172007-10-16 01:27:56 -0700580 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800581 if ((*new_auth_tok)->session_key.encrypted_key_size
582 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow13218172007-10-16 01:27:56 -0700583 printk(KERN_WARNING "Tag 1 packet contains key larger "
584 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800585 rc = -EINVAL;
586 goto out;
587 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800588 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow13218172007-10-16 01:27:56 -0700589 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800590 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
591 (*new_auth_tok)->session_key.flags &=
592 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
593 (*new_auth_tok)->session_key.flags |=
594 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
595 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow13218172007-10-16 01:27:56 -0700596 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800597 (*new_auth_tok)->session_key.flags &=
598 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
599 (*new_auth_tok)->session_key.flags &=
600 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800601 list_add(&auth_tok_list_item->list, auth_tok_list);
602 goto out;
603out_free:
604 (*new_auth_tok) = NULL;
605 memset(auth_tok_list_item, 0,
606 sizeof(struct ecryptfs_auth_tok_list_item));
607 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
608 auth_tok_list_item);
609out:
610 if (rc)
611 (*packet_size) = 0;
612 return rc;
613}
614
Michael Halcrow237fead2006-10-04 02:16:22 -0700615/**
616 * parse_tag_3_packet
617 * @crypt_stat: The cryptographic context to modify based on packet
618 * contents.
619 * @data: The raw bytes of the packet.
620 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
621 * a new authentication token will be placed at the end
622 * of this list for this packet.
623 * @new_auth_tok: Pointer to a pointer to memory that this function
624 * allocates; sets the memory address of the pointer to
625 * NULL on error. This object is added to the
626 * auth_tok_list.
627 * @packet_size: This function writes the size of the parsed packet
628 * into this memory location; zero on error.
629 * @max_packet_size: maximum number of bytes to parse
630 *
631 * Returns zero on success; non-zero on error.
632 */
633static int
634parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
635 unsigned char *data, struct list_head *auth_tok_list,
636 struct ecryptfs_auth_tok **new_auth_tok,
637 size_t *packet_size, size_t max_packet_size)
638{
Michael Halcrow237fead2006-10-04 02:16:22 -0700639 size_t body_size;
640 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
641 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800642 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700643
644 (*packet_size) = 0;
645 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700646 /**
647 *This format is inspired by OpenPGP; see RFC 2440
648 * packet tag 3
649 *
650 * Tag 3 identifier (1 byte)
651 * Max Tag 3 packet size (max 3 bytes)
652 * Version (1 byte)
653 * Cipher code (1 byte)
654 * S2K specifier (1 byte)
655 * Hash identifier (1 byte)
656 * Salt (ECRYPTFS_SALT_SIZE)
657 * Hash iterations (1 byte)
658 * Encrypted key (arbitrary)
659 *
660 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -0700661 */
Michael Halcrowc59becf2007-10-16 01:27:56 -0700662 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
663 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700664 rc = -EINVAL;
665 goto out;
666 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700667 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700668 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
669 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700670 rc = -EINVAL;
671 goto out;
672 }
673 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
674 * at end of function upon failure */
675 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800676 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700677 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700678 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700679 rc = -ENOMEM;
680 goto out;
681 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700682 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700683 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
684 &length_size))) {
685 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
686 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700687 goto out_free;
688 }
Michael Halcrowc59becf2007-10-16 01:27:56 -0700689 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
690 printk(KERN_WARNING "Invalid body size ([%d])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700691 rc = -EINVAL;
692 goto out_free;
693 }
694 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700695 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700696 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700697 rc = -EINVAL;
698 goto out_free;
699 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700700 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -0700701 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Michael Halcrow237fead2006-10-04 02:16:22 -0700702 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700703 printk(KERN_WARNING "Unknown version number [%d]\n",
704 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -0700705 rc = -EINVAL;
706 goto out_free;
707 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700708 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
709 (u16)data[(*packet_size)]);
710 /* A little extra work to differentiate among the AES key
711 * sizes; see RFC2440 */
712 switch(data[(*packet_size)++]) {
713 case RFC2440_CIPHER_AES_192:
714 crypt_stat->key_size = 24;
715 break;
716 default:
717 crypt_stat->key_size =
718 (*new_auth_tok)->session_key.encrypted_key_size;
719 }
720 ecryptfs_init_crypt_ctx(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700721 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700722 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700723 rc = -ENOSYS;
724 goto out_free;
725 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700726 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -0700727 switch (data[(*packet_size)++]) {
728 case 0x01: /* See RFC2440 for these numbers and their mappings */
729 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 memcpy((*new_auth_tok)->token.password.salt,
731 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
732 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700733 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700734 (*new_auth_tok)->token.password.hash_iterations =
735 ((u32) 16 + (data[(*packet_size)] & 15))
736 << ((data[(*packet_size)] >> 4) + 6);
737 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700738 /* Friendly reminder:
739 * (*new_auth_tok)->session_key.encrypted_key_size =
740 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -0700741 memcpy((*new_auth_tok)->session_key.encrypted_key,
742 &data[(*packet_size)],
743 (*new_auth_tok)->session_key.encrypted_key_size);
744 (*packet_size) +=
745 (*new_auth_tok)->session_key.encrypted_key_size;
746 (*new_auth_tok)->session_key.flags &=
747 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
748 (*new_auth_tok)->session_key.flags |=
749 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700750 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700751 break;
752 default:
753 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
754 "[%d]\n", data[(*packet_size) - 1]);
755 rc = -ENOSYS;
756 goto out_free;
757 }
758 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
759 /* TODO: Parametarize; we might actually want userspace to
760 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800761 (*new_auth_tok)->session_key.flags &=
762 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
763 (*new_auth_tok)->session_key.flags &=
764 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700765 list_add(&auth_tok_list_item->list, auth_tok_list);
766 goto out;
767out_free:
768 (*new_auth_tok) = NULL;
769 memset(auth_tok_list_item, 0,
770 sizeof(struct ecryptfs_auth_tok_list_item));
771 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
772 auth_tok_list_item);
773out:
774 if (rc)
775 (*packet_size) = 0;
776 return rc;
777}
778
779/**
780 * parse_tag_11_packet
781 * @data: The raw bytes of the packet
782 * @contents: This function writes the data contents of the literal
783 * packet into this memory location
784 * @max_contents_bytes: The maximum number of bytes that this function
785 * is allowed to write into contents
786 * @tag_11_contents_size: This function writes the size of the parsed
787 * contents into this memory location; zero on
788 * error
789 * @packet_size: This function writes the size of the parsed packet
790 * into this memory location; zero on error
791 * @max_packet_size: maximum number of bytes to parse
792 *
793 * Returns zero on success; non-zero on error.
794 */
795static int
796parse_tag_11_packet(unsigned char *data, unsigned char *contents,
797 size_t max_contents_bytes, size_t *tag_11_contents_size,
798 size_t *packet_size, size_t max_packet_size)
799{
Michael Halcrow237fead2006-10-04 02:16:22 -0700800 size_t body_size;
801 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800802 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700803
804 (*packet_size) = 0;
805 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -0700806 /* This format is inspired by OpenPGP; see RFC 2440
807 * packet tag 11
808 *
809 * Tag 11 identifier (1 byte)
810 * Max Tag 11 packet size (max 3 bytes)
811 * Binary format specifier (1 byte)
812 * Filename length (1 byte)
813 * Filename ("_CONSOLE") (8 bytes)
814 * Modification date (4 bytes)
815 * Literal data (arbitrary)
816 *
817 * We need at least 16 bytes of data for the packet to even be
818 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -0700819 */
Michael Halcrowf6481042007-10-16 01:27:57 -0700820 if (max_packet_size < 16) {
821 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700822 rc = -EINVAL;
823 goto out;
824 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700825 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700826 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700827 rc = -EINVAL;
828 goto out;
829 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700830 if ((rc = parse_packet_length(&data[(*packet_size)], &body_size,
831 &length_size))) {
832 printk(KERN_WARNING "Invalid tag 11 packet format\n");
833 goto out;
834 }
835 if (body_size < 14) {
836 printk(KERN_WARNING "Invalid body size ([%d])\n", body_size);
837 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700838 goto out;
839 }
840 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -0700841 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -0700842 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700843 printk(KERN_ERR "Packet size exceeds max\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)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700848 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700849 rc = -EINVAL;
850 goto out;
851 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700852 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700853 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700854 rc = -EINVAL;
855 goto out;
856 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700857 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -0700858 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
859 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700860out:
861 if (rc) {
862 (*packet_size) = 0;
863 (*tag_11_contents_size) = 0;
864 }
865 return rc;
866}
867
Michael Halcrowf4aad162007-10-16 01:27:53 -0700868static int
869ecryptfs_find_global_auth_tok_for_sig(
870 struct ecryptfs_global_auth_tok **global_auth_tok,
871 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
872{
873 struct ecryptfs_global_auth_tok *walker;
874 int rc = 0;
875
876 (*global_auth_tok) = NULL;
877 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
878 list_for_each_entry(walker,
879 &mount_crypt_stat->global_auth_tok_list,
880 mount_crypt_stat_list) {
881 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
882 (*global_auth_tok) = walker;
883 goto out;
884 }
885 }
886 rc = -EINVAL;
887out:
888 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
889 return rc;
890}
891
Michael Halcrow237fead2006-10-04 02:16:22 -0700892/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700893 * ecryptfs_verify_version
894 * @version: The version number to confirm
895 *
896 * Returns zero on good version; non-zero otherwise
897 */
898static int ecryptfs_verify_version(u16 version)
899{
900 int rc = 0;
901 unsigned char major;
902 unsigned char minor;
903
904 major = ((version >> 8) & 0xFF);
905 minor = (version & 0xFF);
906 if (major != ECRYPTFS_VERSION_MAJOR) {
907 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
908 "Expected [%d]; got [%d]\n",
909 ECRYPTFS_VERSION_MAJOR, major);
910 rc = -EINVAL;
911 goto out;
912 }
913 if (minor != ECRYPTFS_VERSION_MINOR) {
914 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
915 "Expected [%d]; got [%d]\n",
916 ECRYPTFS_VERSION_MINOR, minor);
917 rc = -EINVAL;
918 goto out;
919 }
920out:
921 return rc;
922}
923
924int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
925 struct ecryptfs_auth_tok **auth_tok,
926 char *sig)
927{
928 int rc = 0;
929
930 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
931 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
932 printk(KERN_ERR "Could not find key with description: [%s]\n",
933 sig);
934 process_request_key_err(PTR_ERR(*auth_tok_key));
935 rc = -EINVAL;
936 goto out;
937 }
938 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
939 if (ecryptfs_verify_version((*auth_tok)->version)) {
940 printk(KERN_ERR
941 "Data structure version mismatch. "
942 "Userspace tools must match eCryptfs "
943 "kernel module with major version [%d] "
944 "and minor version [%d]\n",
945 ECRYPTFS_VERSION_MAJOR,
946 ECRYPTFS_VERSION_MINOR);
947 rc = -EINVAL;
948 goto out;
949 }
950 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
951 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
952 printk(KERN_ERR "Invalid auth_tok structure "
953 "returned from key query\n");
954 rc = -EINVAL;
955 goto out;
956 }
957out:
958 return rc;
959}
960
961/**
962 * ecryptfs_find_auth_tok_for_sig
963 * @auth_tok: Set to the matching auth_tok; NULL if not found
964 * @crypt_stat: inode crypt_stat crypto context
965 * @sig: Sig of auth_tok to find
966 *
967 * For now, this function simply looks at the registered auth_tok's
968 * linked off the mount_crypt_stat, so all the auth_toks that can be
969 * used must be registered at mount time. This function could
970 * potentially try a lot harder to find auth_tok's (e.g., by calling
971 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
972 * that static registration of auth_tok's will no longer be necessary.
973 *
974 * Returns zero on no error; non-zero on error
975 */
976static int
977ecryptfs_find_auth_tok_for_sig(
978 struct ecryptfs_auth_tok **auth_tok,
979 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
980{
981 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
982 crypt_stat->mount_crypt_stat;
983 struct ecryptfs_global_auth_tok *global_auth_tok;
984 int rc = 0;
985
986 (*auth_tok) = NULL;
987 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
988 mount_crypt_stat, sig)) {
989 struct key *auth_tok_key;
990
991 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
992 sig);
993 } else
994 (*auth_tok) = global_auth_tok->global_auth_tok;
995 return rc;
996}
997
998/**
999 * decrypt_passphrase_encrypted_session_key - Decrypt the session key
1000 * with the given auth_tok.
Michael Halcrow237fead2006-10-04 02:16:22 -07001001 *
1002 * Returns Zero on success; non-zero error otherwise.
1003 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001004static int
1005decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1006 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001007{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001008 struct scatterlist dst_sg;
1009 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001010 struct mutex *tfm_mutex = NULL;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001011 struct blkcipher_desc desc = {
1012 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1013 };
1014 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001015
Michael Halcrowf4aad162007-10-16 01:27:53 -07001016 if (unlikely(ecryptfs_verbosity > 0)) {
1017 ecryptfs_printk(
1018 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1019 auth_tok->token.password.session_key_encryption_key_bytes);
1020 ecryptfs_dump_hex(
1021 auth_tok->token.password.session_key_encryption_key,
1022 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001023 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001024 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1025 crypt_stat->cipher);
1026 if (unlikely(rc)) {
1027 printk(KERN_ERR "Internal error whilst attempting to get "
1028 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1029 crypt_stat->cipher, rc);
1030 goto out;
1031 }
1032 if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1033 auth_tok->session_key.encrypted_key_size,
1034 &src_sg, 1)) != 1) {
1035 printk(KERN_ERR "Internal error whilst attempting to convert "
1036 "auth_tok->session_key.encrypted_key to scatterlist; "
1037 "expected rc = 1; got rc = [%d]. "
1038 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1039 auth_tok->session_key.encrypted_key_size);
1040 goto out;
1041 }
1042 auth_tok->session_key.decrypted_key_size =
1043 auth_tok->session_key.encrypted_key_size;
1044 if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1045 auth_tok->session_key.decrypted_key_size,
1046 &dst_sg, 1)) != 1) {
1047 printk(KERN_ERR "Internal error whilst attempting to convert "
1048 "auth_tok->session_key.decrypted_key to scatterlist; "
1049 "expected rc = 1; got rc = [%d]\n", rc);
1050 goto out;
1051 }
1052 mutex_lock(tfm_mutex);
1053 rc = crypto_blkcipher_setkey(
1054 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1055 crypt_stat->key_size);
1056 if (unlikely(rc < 0)) {
1057 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001058 printk(KERN_ERR "Error setting key for crypto context\n");
1059 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001060 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001061 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001062 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001063 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001064 mutex_unlock(tfm_mutex);
1065 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001066 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001067 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001068 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001069 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1070 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1071 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001072 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001073 if (unlikely(ecryptfs_verbosity > 0)) {
1074 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1075 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001076 ecryptfs_dump_hex(crypt_stat->key,
1077 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001078 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001079out:
1080 return rc;
1081}
1082
Michael Halcrowf4aad162007-10-16 01:27:53 -07001083int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
1084{
1085 int rc = 0;
1086
1087 (*sig) = NULL;
1088 switch (auth_tok->token_type) {
1089 case ECRYPTFS_PASSWORD:
1090 (*sig) = auth_tok->token.password.signature;
1091 break;
1092 case ECRYPTFS_PRIVATE_KEY:
1093 (*sig) = auth_tok->token.private_key.signature;
1094 break;
1095 default:
1096 printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
1097 auth_tok->token_type);
1098 rc = -EINVAL;
1099 }
1100 return rc;
1101}
1102
Michael Halcrow237fead2006-10-04 02:16:22 -07001103/**
1104 * ecryptfs_parse_packet_set
1105 * @dest: The header page in memory
1106 * @version: Version of file format, to guide parsing behavior
1107 *
1108 * Get crypt_stat to have the file's session key if the requisite key
1109 * is available to decrypt the session key.
1110 *
1111 * Returns Zero if a valid authentication token was retrieved and
1112 * processed; negative value for file not encrypted or for error
1113 * conditions.
1114 */
1115int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1116 unsigned char *src,
1117 struct dentry *ecryptfs_dentry)
1118{
1119 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001120 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001121 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001122 struct list_head auth_tok_list;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001123 struct ecryptfs_auth_tok *matching_auth_tok = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001124 struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001125 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001126 size_t packet_size;
1127 struct ecryptfs_auth_tok *new_auth_tok;
1128 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001129 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001130 size_t tag_11_contents_size;
1131 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001132 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001133
1134 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001135 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001136 * added the our &auth_tok_list */
1137 next_packet_is_auth_tok_packet = 1;
1138 while (next_packet_is_auth_tok_packet) {
1139 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1140
1141 switch (src[i]) {
1142 case ECRYPTFS_TAG_3_PACKET_TYPE:
1143 rc = parse_tag_3_packet(crypt_stat,
1144 (unsigned char *)&src[i],
1145 &auth_tok_list, &new_auth_tok,
1146 &packet_size, max_packet_size);
1147 if (rc) {
1148 ecryptfs_printk(KERN_ERR, "Error parsing "
1149 "tag 3 packet\n");
1150 rc = -EIO;
1151 goto out_wipe_list;
1152 }
1153 i += packet_size;
1154 rc = parse_tag_11_packet((unsigned char *)&src[i],
1155 sig_tmp_space,
1156 ECRYPTFS_SIG_SIZE,
1157 &tag_11_contents_size,
1158 &tag_11_packet_size,
1159 max_packet_size);
1160 if (rc) {
1161 ecryptfs_printk(KERN_ERR, "No valid "
1162 "(ecryptfs-specific) literal "
1163 "packet containing "
1164 "authentication token "
1165 "signature found after "
1166 "tag 3 packet\n");
1167 rc = -EIO;
1168 goto out_wipe_list;
1169 }
1170 i += tag_11_packet_size;
1171 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1172 ecryptfs_printk(KERN_ERR, "Expected "
1173 "signature of size [%d]; "
1174 "read size [%d]\n",
1175 ECRYPTFS_SIG_SIZE,
1176 tag_11_contents_size);
1177 rc = -EIO;
1178 goto out_wipe_list;
1179 }
1180 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1181 sig_tmp_space, tag_11_contents_size);
1182 new_auth_tok->token.password.signature[
1183 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001184 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001185 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001186 case ECRYPTFS_TAG_1_PACKET_TYPE:
1187 rc = parse_tag_1_packet(crypt_stat,
1188 (unsigned char *)&src[i],
1189 &auth_tok_list, &new_auth_tok,
1190 &packet_size, max_packet_size);
1191 if (rc) {
1192 ecryptfs_printk(KERN_ERR, "Error parsing "
1193 "tag 1 packet\n");
1194 rc = -EIO;
1195 goto out_wipe_list;
1196 }
1197 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001198 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001199 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001200 case ECRYPTFS_TAG_11_PACKET_TYPE:
1201 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1202 "(Tag 11 not allowed by itself)\n");
1203 rc = -EIO;
1204 goto out_wipe_list;
1205 break;
1206 default:
1207 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1208 "[%d] of the file header; hex value of "
1209 "character is [0x%.2x]\n", i, src[i]);
1210 next_packet_is_auth_tok_packet = 0;
1211 }
1212 }
1213 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001214 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1215 "eCryptfs file; this is not supported in this version "
1216 "of the eCryptfs kernel module\n");
1217 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001218 goto out;
1219 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001220 /* auth_tok_list contains the set of authentication tokens
1221 * parsed from the metadata. We need to find a matching
1222 * authentication token that has the secret component(s)
1223 * necessary to decrypt the EFEK in the auth_tok parsed from
1224 * the metadata. There may be several potential matches, but
1225 * just one will be sufficient to decrypt to get the FEK. */
1226find_next_matching_auth_tok:
1227 found_auth_tok = 0;
1228 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001229 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1230 if (unlikely(ecryptfs_verbosity > 0)) {
1231 ecryptfs_printk(KERN_DEBUG,
1232 "Considering cadidate auth tok:\n");
1233 ecryptfs_dump_auth_tok(candidate_auth_tok);
1234 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001235 if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1236 candidate_auth_tok))) {
1237 printk(KERN_ERR
1238 "Unrecognized candidate auth tok type: [%d]\n",
1239 candidate_auth_tok->token_type);
1240 rc = -EINVAL;
1241 goto out_wipe_list;
1242 }
1243 if ((rc = ecryptfs_find_auth_tok_for_sig(
1244 &matching_auth_tok, crypt_stat,
1245 candidate_auth_tok_sig)))
1246 rc = 0;
1247 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001248 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001249 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001250 }
1251 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001252 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001253 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1254 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001255 rc = -EIO;
1256 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001257 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001258found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001259 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001260 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001261 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001262 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001263 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001264 crypt_stat);
1265 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001266 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001267 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001268 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001269 rc = decrypt_passphrase_encrypted_session_key(
1270 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001271 }
1272 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001273 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1274
1275 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1276 "session key for authentication token with sig "
1277 "[%.*s]; rc = [%d]. Removing auth tok "
1278 "candidate from the list and searching for "
1279 "the next match.\n", candidate_auth_tok_sig,
1280 ECRYPTFS_SIG_SIZE_HEX, rc);
1281 list_for_each_entry_safe(auth_tok_list_item,
1282 auth_tok_list_item_tmp,
1283 &auth_tok_list, list) {
1284 if (candidate_auth_tok
1285 == &auth_tok_list_item->auth_tok) {
1286 list_del(&auth_tok_list_item->list);
1287 kmem_cache_free(
1288 ecryptfs_auth_tok_list_item_cache,
1289 auth_tok_list_item);
1290 goto find_next_matching_auth_tok;
1291 }
1292 }
1293 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001294 }
1295 rc = ecryptfs_compute_root_iv(crypt_stat);
1296 if (rc) {
1297 ecryptfs_printk(KERN_ERR, "Error computing "
1298 "the root IV\n");
1299 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001300 }
1301 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1302 if (rc) {
1303 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1304 "context for cipher [%s]; rc = [%d]\n",
1305 crypt_stat->cipher, rc);
1306 }
1307out_wipe_list:
1308 wipe_auth_tok_list(&auth_tok_list);
1309out:
1310 return rc;
1311}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001312
Michael Halcrowdddfa462007-02-12 00:53:44 -08001313static int
1314pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1315 struct ecryptfs_crypt_stat *crypt_stat,
1316 struct ecryptfs_key_record *key_rec)
1317{
1318 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1319 char *netlink_payload;
1320 size_t netlink_payload_length;
1321 struct ecryptfs_message *msg;
1322 int rc;
1323
1324 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1325 ecryptfs_code_for_cipher_string(crypt_stat),
1326 crypt_stat, &netlink_payload,
1327 &netlink_payload_length);
1328 if (rc) {
1329 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1330 goto out;
1331 }
1332 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1333 netlink_payload_length, &msg_ctx);
1334 if (rc) {
1335 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1336 goto out;
1337 }
1338 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1339 if (rc) {
1340 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1341 "from the user space daemon\n");
1342 rc = -EIO;
1343 goto out;
1344 }
1345 rc = parse_tag_67_packet(key_rec, msg);
1346 if (rc)
1347 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1348 kfree(msg);
1349out:
1350 if (netlink_payload)
1351 kfree(netlink_payload);
1352 return rc;
1353}
1354/**
1355 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1356 * @dest: Buffer into which to write the packet
1357 * @max: Maximum number of bytes that can be writtn
1358 * @packet_size: This function will write the number of bytes that end
1359 * up constituting the packet; set to zero on error
1360 *
1361 * Returns zero on success; non-zero on error.
1362 */
1363static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001364write_tag_1_packet(char *dest, size_t *remaining_bytes,
1365 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001366 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001367 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1368{
1369 size_t i;
1370 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001371 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001372 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001373 int rc = 0;
1374
1375 (*packet_size) = 0;
1376 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1377 ECRYPTFS_SIG_SIZE);
1378 encrypted_session_key_valid = 0;
1379 for (i = 0; i < crypt_stat->key_size; i++)
1380 encrypted_session_key_valid |=
1381 auth_tok->session_key.encrypted_key[i];
1382 if (encrypted_session_key_valid) {
1383 memcpy(key_rec->enc_key,
1384 auth_tok->session_key.encrypted_key,
1385 auth_tok->session_key.encrypted_key_size);
1386 goto encrypted_session_key_set;
1387 }
1388 if (auth_tok->session_key.encrypted_key_size == 0)
1389 auth_tok->session_key.encrypted_key_size =
1390 auth_tok->token.private_key.key_size;
1391 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1392 if (rc) {
1393 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1394 "via a pki");
1395 goto out;
1396 }
1397 if (ecryptfs_verbosity > 0) {
1398 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1399 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1400 }
1401encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001402 /* This format is inspired by OpenPGP; see RFC 2440
1403 * packet tag 1 */
1404 max_packet_size = (1 /* Tag 1 identifier */
1405 + 3 /* Max Tag 1 packet size */
1406 + 1 /* Version */
1407 + ECRYPTFS_SIG_SIZE /* Key identifier */
1408 + 1 /* Cipher identifier */
1409 + key_rec->enc_key_size); /* Encrypted key size */
1410 if (max_packet_size > (*remaining_bytes)) {
1411 printk(KERN_ERR "Packet length larger than maximum allowable; "
1412 "need up to [%d] bytes, but there are only [%d] "
1413 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001414 rc = -EINVAL;
1415 goto out;
1416 }
1417 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001418 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001419 &packet_size_length);
1420 if (rc) {
1421 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1422 "header; cannot generate packet length\n");
1423 goto out;
1424 }
1425 (*packet_size) += packet_size_length;
1426 dest[(*packet_size)++] = 0x03; /* version 3 */
1427 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1428 (*packet_size) += ECRYPTFS_SIG_SIZE;
1429 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1430 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1431 key_rec->enc_key_size);
1432 (*packet_size) += key_rec->enc_key_size;
1433out:
1434 if (rc)
1435 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001436 else
1437 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001438 return rc;
1439}
Michael Halcrow237fead2006-10-04 02:16:22 -07001440
1441/**
1442 * write_tag_11_packet
1443 * @dest: Target into which Tag 11 packet is to be written
1444 * @max: Maximum packet length
1445 * @contents: Byte array of contents to copy in
1446 * @contents_length: Number of bytes in contents
1447 * @packet_length: Length of the Tag 11 packet written; zero on error
1448 *
1449 * Returns zero on success; non-zero on error.
1450 */
1451static int
Michael Halcrow146a4602007-10-16 01:27:58 -07001452write_tag_11_packet(char *dest, int *remaining_bytes, char *contents,
1453 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07001454{
Michael Halcrow237fead2006-10-04 02:16:22 -07001455 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001456 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001457 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001458
1459 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001460 /* This format is inspired by OpenPGP; see RFC 2440
1461 * packet tag 11 */
1462 max_packet_size = (1 /* Tag 11 identifier */
1463 + 3 /* Max Tag 11 packet size */
1464 + 1 /* Binary format specifier */
1465 + 1 /* Filename length */
1466 + 8 /* Filename ("_CONSOLE") */
1467 + 4 /* Modification date */
1468 + contents_length); /* Literal data */
1469 if (max_packet_size > (*remaining_bytes)) {
1470 printk(KERN_ERR "Packet length larger than maximum allowable; "
1471 "need up to [%d] bytes, but there are only [%d] "
1472 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07001473 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001474 goto out;
1475 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001476 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001477 rc = write_packet_length(&dest[(*packet_length)],
Michael Halcrow146a4602007-10-16 01:27:58 -07001478 (max_packet_size - 4), &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001479 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07001480 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1481 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001482 goto out;
1483 }
1484 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001485 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07001486 dest[(*packet_length)++] = 8;
1487 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1488 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07001489 memset(&dest[(*packet_length)], 0x00, 4);
1490 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07001491 memcpy(&dest[(*packet_length)], contents, contents_length);
1492 (*packet_length) += contents_length;
1493 out:
1494 if (rc)
1495 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001496 else
1497 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001498 return rc;
1499}
1500
1501/**
1502 * write_tag_3_packet
1503 * @dest: Buffer into which to write the packet
1504 * @max: Maximum number of bytes that can be written
1505 * @auth_tok: Authentication token
1506 * @crypt_stat: The cryptographic context
1507 * @key_rec: encrypted key
1508 * @packet_size: This function will write the number of bytes that end
1509 * up constituting the packet; set to zero on error
1510 *
1511 * Returns zero on success; non-zero on error.
1512 */
1513static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001514write_tag_3_packet(char *dest, size_t *remaining_bytes,
1515 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001516 struct ecryptfs_crypt_stat *crypt_stat,
1517 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1518{
Michael Halcrow237fead2006-10-04 02:16:22 -07001519 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001520 size_t encrypted_session_key_valid = 0;
1521 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001522 struct scatterlist dst_sg;
1523 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001524 struct mutex *tfm_mutex = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001525 size_t cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001526 size_t packet_size_length;
1527 size_t max_packet_size;
1528 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1529 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001530 struct blkcipher_desc desc = {
1531 .tfm = NULL,
1532 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1533 };
1534 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001535
1536 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001537 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001538 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001539 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1540 crypt_stat->cipher);
1541 if (unlikely(rc)) {
1542 printk(KERN_ERR "Internal error whilst attempting to get "
1543 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1544 crypt_stat->cipher, rc);
1545 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001546 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001547 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1548 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1549
1550 printk(KERN_WARNING "No key size specified at mount; "
1551 "defaulting to [%d]\n", alg->max_keysize);
1552 mount_crypt_stat->global_default_cipher_key_size =
1553 alg->max_keysize;
1554 }
1555 if (crypt_stat->key_size == 0)
1556 crypt_stat->key_size =
1557 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001558 if (auth_tok->session_key.encrypted_key_size == 0)
1559 auth_tok->session_key.encrypted_key_size =
1560 crypt_stat->key_size;
1561 if (crypt_stat->key_size == 24
1562 && strcmp("aes", crypt_stat->cipher) == 0) {
1563 memset((crypt_stat->key + 24), 0, 8);
1564 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001565 } else
1566 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001567 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001568 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001569 encrypted_session_key_valid = 0;
1570 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1571 encrypted_session_key_valid |=
1572 auth_tok->session_key.encrypted_key[i];
1573 if (encrypted_session_key_valid) {
1574 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1575 "using auth_tok->session_key.encrypted_key, "
1576 "where key_rec->enc_key_size = [%d]\n",
1577 key_rec->enc_key_size);
1578 memcpy(key_rec->enc_key,
1579 auth_tok->session_key.encrypted_key,
1580 key_rec->enc_key_size);
1581 goto encrypted_session_key_set;
1582 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001583 if (auth_tok->token.password.flags &
1584 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001585 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1586 "session key encryption key of size [%d]\n",
1587 auth_tok->token.password.
1588 session_key_encryption_key_bytes);
1589 memcpy(session_key_encryption_key,
1590 auth_tok->token.password.session_key_encryption_key,
1591 crypt_stat->key_size);
1592 ecryptfs_printk(KERN_DEBUG,
1593 "Cached session key " "encryption key: \n");
1594 if (ecryptfs_verbosity > 0)
1595 ecryptfs_dump_hex(session_key_encryption_key, 16);
1596 }
1597 if (unlikely(ecryptfs_verbosity > 0)) {
1598 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1599 ecryptfs_dump_hex(session_key_encryption_key, 16);
1600 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001601 if ((rc = virt_to_scatterlist(crypt_stat->key,
1602 key_rec->enc_key_size, &src_sg, 1))
1603 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001604 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001605 "for crypt_stat session key; expected rc = 1; "
1606 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1607 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001608 rc = -ENOMEM;
1609 goto out;
1610 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001611 if ((rc = virt_to_scatterlist(key_rec->enc_key,
1612 key_rec->enc_key_size, &dst_sg, 1))
1613 != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001614 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001615 "for crypt_stat encrypted session key; "
1616 "expected rc = 1; got rc = [%d]. "
1617 "key_rec->enc_key_size = [%d]\n", rc,
1618 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001619 rc = -ENOMEM;
1620 goto out;
1621 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001622 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001623 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1624 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001625 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001626 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001627 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001628 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001629 goto out;
1630 }
1631 rc = 0;
1632 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1633 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001634 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001635 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001636 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001637 if (rc) {
1638 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1639 goto out;
1640 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001641 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001642 if (ecryptfs_verbosity > 0) {
1643 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1644 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001645 ecryptfs_dump_hex(key_rec->enc_key,
1646 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001647 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001648encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001649 /* This format is inspired by OpenPGP; see RFC 2440
1650 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001651 max_packet_size = (1 /* Tag 3 identifier */
1652 + 3 /* Max Tag 3 packet size */
1653 + 1 /* Version */
1654 + 1 /* Cipher code */
1655 + 1 /* S2K specifier */
1656 + 1 /* Hash identifier */
1657 + ECRYPTFS_SALT_SIZE /* Salt */
1658 + 1 /* Hash iterations */
1659 + key_rec->enc_key_size); /* Encrypted key size */
1660 if (max_packet_size > (*remaining_bytes)) {
1661 printk(KERN_ERR "Packet too large; need up to [%d] bytes, but "
1662 "there are only [%d] available\n", max_packet_size,
1663 (*remaining_bytes));
1664 rc = -EINVAL;
1665 goto out;
1666 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001667 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001668 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1669 * to get the number of octets in the actual Tag 3 packet */
1670 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrow237fead2006-10-04 02:16:22 -07001671 &packet_size_length);
1672 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001673 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1674 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001675 goto out;
1676 }
1677 (*packet_size) += packet_size_length;
1678 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001679 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1680 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001681 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1682 if (cipher_code == 0) {
1683 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1684 "cipher [%s]\n", crypt_stat->cipher);
1685 rc = -EINVAL;
1686 goto out;
1687 }
1688 dest[(*packet_size)++] = cipher_code;
1689 dest[(*packet_size)++] = 0x03; /* S2K */
1690 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1691 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1692 ECRYPTFS_SALT_SIZE);
1693 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1694 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001695 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1696 key_rec->enc_key_size);
1697 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001698out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001699 if (rc)
1700 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001701 else
1702 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001703 return rc;
1704}
1705
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001706struct kmem_cache *ecryptfs_key_record_cache;
1707
Michael Halcrow237fead2006-10-04 02:16:22 -07001708/**
1709 * ecryptfs_generate_key_packet_set
1710 * @dest: Virtual address from which to write the key record set
1711 * @crypt_stat: The cryptographic context from which the
1712 * authentication tokens will be retrieved
1713 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1714 * for the global parameters
1715 * @len: The amount written
1716 * @max: The maximum amount of data allowed to be written
1717 *
1718 * Generates a key packet set and writes it to the virtual address
1719 * passed in.
1720 *
1721 * Returns zero on success; non-zero on error.
1722 */
1723int
1724ecryptfs_generate_key_packet_set(char *dest_base,
1725 struct ecryptfs_crypt_stat *crypt_stat,
1726 struct dentry *ecryptfs_dentry, size_t *len,
1727 size_t max)
1728{
Michael Halcrow237fead2006-10-04 02:16:22 -07001729 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001730 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001731 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1732 &ecryptfs_superblock_to_private(
1733 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1734 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001735 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001736 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001737 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001738
1739 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001740 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001741 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1742 if (!key_rec) {
1743 rc = -ENOMEM;
1744 goto out;
1745 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001746 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1747 crypt_stat_list) {
1748 memset(key_rec, 0, sizeof(*key_rec));
1749 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1750 mount_crypt_stat,
1751 key_sig->keysig);
1752 if (rc) {
1753 printk(KERN_ERR "Error attempting to get the global "
1754 "auth_tok; rc = [%d]\n", rc);
1755 goto out_free;
1756 }
1757 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1758 printk(KERN_WARNING
1759 "Skipping invalid auth tok with sig = [%s]\n",
1760 global_auth_tok->sig);
1761 continue;
1762 }
1763 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001764 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1765 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001766 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001767 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001768 &written);
1769 if (rc) {
1770 ecryptfs_printk(KERN_WARNING, "Error "
1771 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001772 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001773 }
1774 (*len) += written;
1775 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001776 rc = write_tag_11_packet((dest_base + (*len)), &max,
1777 key_rec->sig,
1778 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001779 if (rc) {
1780 ecryptfs_printk(KERN_ERR, "Error writing "
1781 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001782 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001783 }
1784 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001785 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1786 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001787 &max, auth_tok,
1788 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001789 if (rc) {
1790 ecryptfs_printk(KERN_WARNING, "Error "
1791 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001792 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001793 }
1794 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001795 } else {
1796 ecryptfs_printk(KERN_WARNING, "Unsupported "
1797 "authentication token type\n");
1798 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001799 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001800 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001801 }
1802 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001803 dest_base[(*len)] = 0x00;
1804 } else {
1805 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1806 rc = -EIO;
1807 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001808out_free:
1809 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001810out:
1811 if (rc)
1812 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001813 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001814 return rc;
1815}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001816
1817struct kmem_cache *ecryptfs_key_sig_cache;
1818
1819int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1820{
1821 struct ecryptfs_key_sig *new_key_sig;
1822 int rc = 0;
1823
1824 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1825 if (!new_key_sig) {
1826 rc = -ENOMEM;
1827 printk(KERN_ERR
1828 "Error allocating from ecryptfs_key_sig_cache\n");
1829 goto out;
1830 }
1831 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1832 mutex_lock(&crypt_stat->keysig_list_mutex);
1833 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1834 mutex_unlock(&crypt_stat->keysig_list_mutex);
1835out:
1836 return rc;
1837}
1838
1839struct kmem_cache *ecryptfs_global_auth_tok_cache;
1840
1841int
1842ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1843 char *sig)
1844{
1845 struct ecryptfs_global_auth_tok *new_auth_tok;
1846 int rc = 0;
1847
1848 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1849 GFP_KERNEL);
1850 if (!new_auth_tok) {
1851 rc = -ENOMEM;
1852 printk(KERN_ERR "Error allocating from "
1853 "ecryptfs_global_auth_tok_cache\n");
1854 goto out;
1855 }
1856 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1857 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1858 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1859 list_add(&new_auth_tok->mount_crypt_stat_list,
1860 &mount_crypt_stat->global_auth_tok_list);
1861 mount_crypt_stat->num_global_auth_toks++;
1862 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1863out:
1864 return rc;
1865}
1866