blob: 89d9710dd63dd2a091a1bacd8e78c536914c7a54 [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 Halcrow5dda6992007-10-16 01:28:06 -0700439 rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok);
440 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -0700441 printk(KERN_ERR "Unrecognized auth tok type: [%d]\n",
442 auth_tok->token_type);
443 goto out;
444 }
445 rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -0800446 &netlink_message, &netlink_message_length);
447 if (rc) {
448 ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet");
449 goto out;
450 }
451 rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
452 netlink_message_length, &msg_ctx);
453 if (rc) {
454 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
455 goto out;
456 }
457 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
458 if (rc) {
459 ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet "
460 "from the user space daemon\n");
461 rc = -EIO;
462 goto out;
463 }
464 rc = parse_tag_65_packet(&(auth_tok->session_key),
465 &cipher_code, msg);
466 if (rc) {
467 printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n",
468 rc);
469 goto out;
470 }
471 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
472 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
473 auth_tok->session_key.decrypted_key_size);
474 crypt_stat->key_size = auth_tok->session_key.decrypted_key_size;
475 rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code);
476 if (rc) {
477 ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n",
478 cipher_code)
479 goto out;
480 }
481 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
482 if (ecryptfs_verbosity > 0) {
483 ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
484 ecryptfs_dump_hex(crypt_stat->key,
485 crypt_stat->key_size);
486 }
487out:
488 if (msg)
489 kfree(msg);
490 return rc;
491}
492
493static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
494{
Michael Halcrowdddfa462007-02-12 00:53:44 -0800495 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700496 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800497
Michael Halcrowe0869cc2007-10-16 01:27:55 -0700498 list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp,
499 auth_tok_list_head, list) {
500 list_del(&auth_tok_list_item->list);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800501 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
502 auth_tok_list_item);
503 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800504}
505
506struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
507
Michael Halcrowdddfa462007-02-12 00:53:44 -0800508/**
509 * parse_tag_1_packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700510 * @crypt_stat: The cryptographic context to modify based on packet contents
Michael Halcrowdddfa462007-02-12 00:53:44 -0800511 * @data: The raw bytes of the packet.
512 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700513 * a new authentication token will be placed at the
514 * end of this list for this packet.
Michael Halcrowdddfa462007-02-12 00:53:44 -0800515 * @new_auth_tok: Pointer to a pointer to memory that this function
516 * allocates; sets the memory address of the pointer to
517 * NULL on error. This object is added to the
518 * auth_tok_list.
519 * @packet_size: This function writes the size of the parsed packet
520 * into this memory location; zero on error.
Michael Halcrow22e78fa2007-10-16 01:28:02 -0700521 * @max_packet_size: The maximum allowable packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800522 *
523 * Returns zero on success; non-zero on error.
524 */
525static int
526parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
527 unsigned char *data, struct list_head *auth_tok_list,
528 struct ecryptfs_auth_tok **new_auth_tok,
529 size_t *packet_size, size_t max_packet_size)
530{
531 size_t body_size;
532 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
533 size_t length_size;
534 int rc = 0;
535
536 (*packet_size) = 0;
537 (*new_auth_tok) = NULL;
Michael Halcrow13218172007-10-16 01:27:56 -0700538 /**
539 * This format is inspired by OpenPGP; see RFC 2440
540 * packet tag 1
541 *
542 * Tag 1 identifier (1 byte)
543 * Max Tag 1 packet size (max 3 bytes)
544 * Version (1 byte)
545 * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE)
546 * Cipher identifier (1 byte)
547 * Encrypted key size (arbitrary)
548 *
549 * 12 bytes minimum packet size
Michael Halcrowdddfa462007-02-12 00:53:44 -0800550 */
Michael Halcrow13218172007-10-16 01:27:56 -0700551 if (unlikely(max_packet_size < 12)) {
552 printk(KERN_ERR "Invalid max packet size; must be >=12\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800553 rc = -EINVAL;
554 goto out;
555 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800556 if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
Michael Halcrow13218172007-10-16 01:27:56 -0700557 printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n",
558 ECRYPTFS_TAG_1_PACKET_TYPE);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800559 rc = -EINVAL;
560 goto out;
561 }
562 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
563 * at end of function upon failure */
564 auth_tok_list_item =
Michael Halcrow13218172007-10-16 01:27:56 -0700565 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache,
566 GFP_KERNEL);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800567 if (!auth_tok_list_item) {
Michael Halcrow13218172007-10-16 01:27:56 -0700568 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800569 rc = -ENOMEM;
570 goto out;
571 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800572 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow5dda6992007-10-16 01:28:06 -0700573 rc = parse_packet_length(&data[(*packet_size)], &body_size,
574 &length_size);
575 if (rc) {
Michael Halcrow13218172007-10-16 01:27:56 -0700576 printk(KERN_WARNING "Error parsing packet length; "
577 "rc = [%d]\n", rc);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800578 goto out_free;
579 }
Michael Halcrow13218172007-10-16 01:27:56 -0700580 if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700581 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800582 rc = -EINVAL;
583 goto out_free;
584 }
585 (*packet_size) += length_size;
586 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700587 printk(KERN_WARNING "Packet size exceeds max\n");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800588 rc = -EINVAL;
589 goto out_free;
590 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800591 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrow13218172007-10-16 01:27:56 -0700592 printk(KERN_WARNING "Unknown version number [%d]\n",
593 data[(*packet_size) - 1]);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800594 rc = -EINVAL;
595 goto out_free;
596 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800597 ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
598 &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
599 *packet_size += ECRYPTFS_SIG_SIZE;
600 /* This byte is skipped because the kernel does not need to
601 * know which public key encryption algorithm was used */
602 (*packet_size)++;
603 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrow13218172007-10-16 01:27:56 -0700604 body_size - (ECRYPTFS_SIG_SIZE + 2);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800605 if ((*new_auth_tok)->session_key.encrypted_key_size
606 > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
Michael Halcrow13218172007-10-16 01:27:56 -0700607 printk(KERN_WARNING "Tag 1 packet contains key larger "
608 "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
Michael Halcrowdddfa462007-02-12 00:53:44 -0800609 rc = -EINVAL;
610 goto out;
611 }
Michael Halcrowdddfa462007-02-12 00:53:44 -0800612 memcpy((*new_auth_tok)->session_key.encrypted_key,
Michael Halcrow13218172007-10-16 01:27:56 -0700613 &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2)));
Michael Halcrowdddfa462007-02-12 00:53:44 -0800614 (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
615 (*new_auth_tok)->session_key.flags &=
616 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
617 (*new_auth_tok)->session_key.flags |=
618 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
619 (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
Michael Halcrow13218172007-10-16 01:27:56 -0700620 (*new_auth_tok)->flags = 0;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800621 (*new_auth_tok)->session_key.flags &=
622 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
623 (*new_auth_tok)->session_key.flags &=
624 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrowdddfa462007-02-12 00:53:44 -0800625 list_add(&auth_tok_list_item->list, auth_tok_list);
626 goto out;
627out_free:
628 (*new_auth_tok) = NULL;
629 memset(auth_tok_list_item, 0,
630 sizeof(struct ecryptfs_auth_tok_list_item));
631 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
632 auth_tok_list_item);
633out:
634 if (rc)
635 (*packet_size) = 0;
636 return rc;
637}
638
Michael Halcrow237fead2006-10-04 02:16:22 -0700639/**
640 * parse_tag_3_packet
641 * @crypt_stat: The cryptographic context to modify based on packet
642 * contents.
643 * @data: The raw bytes of the packet.
644 * @auth_tok_list: eCryptfs parses packets into authentication tokens;
645 * a new authentication token will be placed at the end
646 * of this list for this packet.
647 * @new_auth_tok: Pointer to a pointer to memory that this function
648 * allocates; sets the memory address of the pointer to
649 * NULL on error. This object is added to the
650 * auth_tok_list.
651 * @packet_size: This function writes the size of the parsed packet
652 * into this memory location; zero on error.
653 * @max_packet_size: maximum number of bytes to parse
654 *
655 * Returns zero on success; non-zero on error.
656 */
657static int
658parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
659 unsigned char *data, struct list_head *auth_tok_list,
660 struct ecryptfs_auth_tok **new_auth_tok,
661 size_t *packet_size, size_t max_packet_size)
662{
Michael Halcrow237fead2006-10-04 02:16:22 -0700663 size_t body_size;
664 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
665 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800666 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700667
668 (*packet_size) = 0;
669 (*new_auth_tok) = NULL;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700670 /**
671 *This format is inspired by OpenPGP; see RFC 2440
672 * packet tag 3
673 *
674 * Tag 3 identifier (1 byte)
675 * Max Tag 3 packet size (max 3 bytes)
676 * Version (1 byte)
677 * Cipher code (1 byte)
678 * S2K specifier (1 byte)
679 * Hash identifier (1 byte)
680 * Salt (ECRYPTFS_SALT_SIZE)
681 * Hash iterations (1 byte)
682 * Encrypted key (arbitrary)
683 *
684 * (ECRYPTFS_SALT_SIZE + 7) minimum packet size
Michael Halcrow237fead2006-10-04 02:16:22 -0700685 */
Michael Halcrowc59becf2007-10-16 01:27:56 -0700686 if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) {
687 printk(KERN_ERR "Max packet size too large\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700688 rc = -EINVAL;
689 goto out;
690 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700691 if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700692 printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n",
693 ECRYPTFS_TAG_3_PACKET_TYPE);
Michael Halcrow237fead2006-10-04 02:16:22 -0700694 rc = -EINVAL;
695 goto out;
696 }
697 /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
698 * at end of function upon failure */
699 auth_tok_list_item =
Robert P. J. Dayc3762222007-02-10 01:45:03 -0800700 kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
Michael Halcrow237fead2006-10-04 02:16:22 -0700701 if (!auth_tok_list_item) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700702 printk(KERN_ERR "Unable to allocate memory\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700703 rc = -ENOMEM;
704 goto out;
705 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700706 (*new_auth_tok) = &auth_tok_list_item->auth_tok;
Michael Halcrow5dda6992007-10-16 01:28:06 -0700707 rc = parse_packet_length(&data[(*packet_size)], &body_size,
708 &length_size);
709 if (rc) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700710 printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n",
711 rc);
Michael Halcrow237fead2006-10-04 02:16:22 -0700712 goto out_free;
713 }
Michael Halcrowc59becf2007-10-16 01:27:56 -0700714 if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700715 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700716 rc = -EINVAL;
717 goto out_free;
718 }
719 (*packet_size) += length_size;
Michael Halcrow237fead2006-10-04 02:16:22 -0700720 if (unlikely((*packet_size) + body_size > max_packet_size)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700721 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700722 rc = -EINVAL;
723 goto out_free;
724 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700725 (*new_auth_tok)->session_key.encrypted_key_size =
Michael Halcrowc59becf2007-10-16 01:27:56 -0700726 (body_size - (ECRYPTFS_SALT_SIZE + 5));
Michael Halcrow237fead2006-10-04 02:16:22 -0700727 if (unlikely(data[(*packet_size)++] != 0x04)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700728 printk(KERN_WARNING "Unknown version number [%d]\n",
729 data[(*packet_size) - 1]);
Michael Halcrow237fead2006-10-04 02:16:22 -0700730 rc = -EINVAL;
731 goto out_free;
732 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700733 ecryptfs_cipher_code_to_string(crypt_stat->cipher,
734 (u16)data[(*packet_size)]);
735 /* A little extra work to differentiate among the AES key
736 * sizes; see RFC2440 */
737 switch(data[(*packet_size)++]) {
738 case RFC2440_CIPHER_AES_192:
739 crypt_stat->key_size = 24;
740 break;
741 default:
742 crypt_stat->key_size =
743 (*new_auth_tok)->session_key.encrypted_key_size;
744 }
745 ecryptfs_init_crypt_ctx(crypt_stat);
Michael Halcrow237fead2006-10-04 02:16:22 -0700746 if (unlikely(data[(*packet_size)++] != 0x03)) {
Michael Halcrowc59becf2007-10-16 01:27:56 -0700747 printk(KERN_WARNING "Only S2K ID 3 is currently supported\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700748 rc = -ENOSYS;
749 goto out_free;
750 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700751 /* TODO: finish the hash mapping */
Michael Halcrow237fead2006-10-04 02:16:22 -0700752 switch (data[(*packet_size)++]) {
753 case 0x01: /* See RFC2440 for these numbers and their mappings */
754 /* Choose MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700755 memcpy((*new_auth_tok)->token.password.salt,
756 &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
757 (*packet_size) += ECRYPTFS_SALT_SIZE;
Michael Halcrow237fead2006-10-04 02:16:22 -0700758 /* This conversion was taken straight from RFC2440 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700759 (*new_auth_tok)->token.password.hash_iterations =
760 ((u32) 16 + (data[(*packet_size)] & 15))
761 << ((data[(*packet_size)] >> 4) + 6);
762 (*packet_size)++;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700763 /* Friendly reminder:
764 * (*new_auth_tok)->session_key.encrypted_key_size =
765 * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */
Michael Halcrow237fead2006-10-04 02:16:22 -0700766 memcpy((*new_auth_tok)->session_key.encrypted_key,
767 &data[(*packet_size)],
768 (*new_auth_tok)->session_key.encrypted_key_size);
769 (*packet_size) +=
770 (*new_auth_tok)->session_key.encrypted_key_size;
771 (*new_auth_tok)->session_key.flags &=
772 ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
773 (*new_auth_tok)->session_key.flags |=
774 ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
Michael Halcrowc59becf2007-10-16 01:27:56 -0700775 (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */
Michael Halcrow237fead2006-10-04 02:16:22 -0700776 break;
777 default:
778 ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
779 "[%d]\n", data[(*packet_size) - 1]);
780 rc = -ENOSYS;
781 goto out_free;
782 }
783 (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
784 /* TODO: Parametarize; we might actually want userspace to
785 * decrypt the session key. */
Michael Halcrowe2bd99e2007-02-12 00:53:49 -0800786 (*new_auth_tok)->session_key.flags &=
787 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
788 (*new_auth_tok)->session_key.flags &=
789 ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
Michael Halcrow237fead2006-10-04 02:16:22 -0700790 list_add(&auth_tok_list_item->list, auth_tok_list);
791 goto out;
792out_free:
793 (*new_auth_tok) = NULL;
794 memset(auth_tok_list_item, 0,
795 sizeof(struct ecryptfs_auth_tok_list_item));
796 kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
797 auth_tok_list_item);
798out:
799 if (rc)
800 (*packet_size) = 0;
801 return rc;
802}
803
804/**
805 * parse_tag_11_packet
806 * @data: The raw bytes of the packet
807 * @contents: This function writes the data contents of the literal
808 * packet into this memory location
809 * @max_contents_bytes: The maximum number of bytes that this function
810 * is allowed to write into contents
811 * @tag_11_contents_size: This function writes the size of the parsed
812 * contents into this memory location; zero on
813 * error
814 * @packet_size: This function writes the size of the parsed packet
815 * into this memory location; zero on error
816 * @max_packet_size: maximum number of bytes to parse
817 *
818 * Returns zero on success; non-zero on error.
819 */
820static int
821parse_tag_11_packet(unsigned char *data, unsigned char *contents,
822 size_t max_contents_bytes, size_t *tag_11_contents_size,
823 size_t *packet_size, size_t max_packet_size)
824{
Michael Halcrow237fead2006-10-04 02:16:22 -0700825 size_t body_size;
826 size_t length_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -0800827 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -0700828
829 (*packet_size) = 0;
830 (*tag_11_contents_size) = 0;
Michael Halcrowf6481042007-10-16 01:27:57 -0700831 /* This format is inspired by OpenPGP; see RFC 2440
832 * packet tag 11
833 *
834 * Tag 11 identifier (1 byte)
835 * Max Tag 11 packet size (max 3 bytes)
836 * Binary format specifier (1 byte)
837 * Filename length (1 byte)
838 * Filename ("_CONSOLE") (8 bytes)
839 * Modification date (4 bytes)
840 * Literal data (arbitrary)
841 *
842 * We need at least 16 bytes of data for the packet to even be
843 * valid.
Michael Halcrow237fead2006-10-04 02:16:22 -0700844 */
Michael Halcrowf6481042007-10-16 01:27:57 -0700845 if (max_packet_size < 16) {
846 printk(KERN_ERR "Maximum packet size too small\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700847 rc = -EINVAL;
848 goto out;
849 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700850 if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700851 printk(KERN_WARNING "Invalid tag 11 packet format\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700852 rc = -EINVAL;
853 goto out;
854 }
Michael Halcrow5dda6992007-10-16 01:28:06 -0700855 rc = parse_packet_length(&data[(*packet_size)], &body_size,
856 &length_size);
857 if (rc) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700858 printk(KERN_WARNING "Invalid tag 11 packet format\n");
859 goto out;
860 }
861 if (body_size < 14) {
Andrew Morton81acbcd2007-10-16 01:27:59 -0700862 printk(KERN_WARNING "Invalid body size ([%td])\n", body_size);
Michael Halcrowf6481042007-10-16 01:27:57 -0700863 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -0700864 goto out;
865 }
866 (*packet_size) += length_size;
Michael Halcrowf6481042007-10-16 01:27:57 -0700867 (*tag_11_contents_size) = (body_size - 14);
Michael Halcrow237fead2006-10-04 02:16:22 -0700868 if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700869 printk(KERN_ERR "Packet size exceeds max\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700870 rc = -EINVAL;
871 goto out;
872 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700873 if (data[(*packet_size)++] != 0x62) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700874 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700875 rc = -EINVAL;
876 goto out;
877 }
Michael Halcrow237fead2006-10-04 02:16:22 -0700878 if (data[(*packet_size)++] != 0x08) {
Michael Halcrowf6481042007-10-16 01:27:57 -0700879 printk(KERN_WARNING "Unrecognizable packet\n");
Michael Halcrow237fead2006-10-04 02:16:22 -0700880 rc = -EINVAL;
881 goto out;
882 }
Michael Halcrowf6481042007-10-16 01:27:57 -0700883 (*packet_size) += 12; /* Ignore filename and modification date */
Michael Halcrow237fead2006-10-04 02:16:22 -0700884 memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
885 (*packet_size) += (*tag_11_contents_size);
Michael Halcrow237fead2006-10-04 02:16:22 -0700886out:
887 if (rc) {
888 (*packet_size) = 0;
889 (*tag_11_contents_size) = 0;
890 }
891 return rc;
892}
893
Michael Halcrowf4aad162007-10-16 01:27:53 -0700894static int
895ecryptfs_find_global_auth_tok_for_sig(
896 struct ecryptfs_global_auth_tok **global_auth_tok,
897 struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
898{
899 struct ecryptfs_global_auth_tok *walker;
900 int rc = 0;
901
902 (*global_auth_tok) = NULL;
903 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
904 list_for_each_entry(walker,
905 &mount_crypt_stat->global_auth_tok_list,
906 mount_crypt_stat_list) {
907 if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
908 (*global_auth_tok) = walker;
909 goto out;
910 }
911 }
912 rc = -EINVAL;
913out:
914 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
915 return rc;
916}
917
Michael Halcrow237fead2006-10-04 02:16:22 -0700918/**
Michael Halcrowf4aad162007-10-16 01:27:53 -0700919 * ecryptfs_verify_version
920 * @version: The version number to confirm
921 *
922 * Returns zero on good version; non-zero otherwise
923 */
924static int ecryptfs_verify_version(u16 version)
925{
926 int rc = 0;
927 unsigned char major;
928 unsigned char minor;
929
930 major = ((version >> 8) & 0xFF);
931 minor = (version & 0xFF);
932 if (major != ECRYPTFS_VERSION_MAJOR) {
933 ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
934 "Expected [%d]; got [%d]\n",
935 ECRYPTFS_VERSION_MAJOR, major);
936 rc = -EINVAL;
937 goto out;
938 }
939 if (minor != ECRYPTFS_VERSION_MINOR) {
940 ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
941 "Expected [%d]; got [%d]\n",
942 ECRYPTFS_VERSION_MINOR, minor);
943 rc = -EINVAL;
944 goto out;
945 }
946out:
947 return rc;
948}
949
950int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
951 struct ecryptfs_auth_tok **auth_tok,
952 char *sig)
953{
954 int rc = 0;
955
956 (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
957 if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
958 printk(KERN_ERR "Could not find key with description: [%s]\n",
959 sig);
960 process_request_key_err(PTR_ERR(*auth_tok_key));
961 rc = -EINVAL;
962 goto out;
963 }
964 (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
965 if (ecryptfs_verify_version((*auth_tok)->version)) {
966 printk(KERN_ERR
967 "Data structure version mismatch. "
968 "Userspace tools must match eCryptfs "
969 "kernel module with major version [%d] "
970 "and minor version [%d]\n",
971 ECRYPTFS_VERSION_MAJOR,
972 ECRYPTFS_VERSION_MINOR);
973 rc = -EINVAL;
974 goto out;
975 }
976 if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
977 && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
978 printk(KERN_ERR "Invalid auth_tok structure "
979 "returned from key query\n");
980 rc = -EINVAL;
981 goto out;
982 }
983out:
984 return rc;
985}
986
987/**
988 * ecryptfs_find_auth_tok_for_sig
989 * @auth_tok: Set to the matching auth_tok; NULL if not found
990 * @crypt_stat: inode crypt_stat crypto context
991 * @sig: Sig of auth_tok to find
992 *
993 * For now, this function simply looks at the registered auth_tok's
994 * linked off the mount_crypt_stat, so all the auth_toks that can be
995 * used must be registered at mount time. This function could
996 * potentially try a lot harder to find auth_tok's (e.g., by calling
997 * out to ecryptfsd to dynamically retrieve an auth_tok object) so
998 * that static registration of auth_tok's will no longer be necessary.
999 *
1000 * Returns zero on no error; non-zero on error
1001 */
1002static int
1003ecryptfs_find_auth_tok_for_sig(
1004 struct ecryptfs_auth_tok **auth_tok,
1005 struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1006{
1007 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1008 crypt_stat->mount_crypt_stat;
1009 struct ecryptfs_global_auth_tok *global_auth_tok;
1010 int rc = 0;
1011
1012 (*auth_tok) = NULL;
1013 if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1014 mount_crypt_stat, sig)) {
1015 struct key *auth_tok_key;
1016
1017 rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
1018 sig);
1019 } else
1020 (*auth_tok) = global_auth_tok->global_auth_tok;
1021 return rc;
1022}
1023
1024/**
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001025 * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok.
1026 * @auth_tok: The passphrase authentication token to use to encrypt the FEK
1027 * @crypt_stat: The cryptographic context
Michael Halcrow237fead2006-10-04 02:16:22 -07001028 *
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001029 * Returns zero on success; non-zero error otherwise
Michael Halcrow237fead2006-10-04 02:16:22 -07001030 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001031static int
1032decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
1033 struct ecryptfs_crypt_stat *crypt_stat)
Michael Halcrow237fead2006-10-04 02:16:22 -07001034{
Michael Halcrowf4aad162007-10-16 01:27:53 -07001035 struct scatterlist dst_sg;
1036 struct scatterlist src_sg;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001037 struct mutex *tfm_mutex;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001038 struct blkcipher_desc desc = {
1039 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1040 };
1041 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001042
Michael Halcrowf4aad162007-10-16 01:27:53 -07001043 if (unlikely(ecryptfs_verbosity > 0)) {
1044 ecryptfs_printk(
1045 KERN_DEBUG, "Session key encryption key (size [%d]):\n",
1046 auth_tok->token.password.session_key_encryption_key_bytes);
1047 ecryptfs_dump_hex(
1048 auth_tok->token.password.session_key_encryption_key,
1049 auth_tok->token.password.session_key_encryption_key_bytes);
Michael Halcrow237fead2006-10-04 02:16:22 -07001050 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001051 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1052 crypt_stat->cipher);
1053 if (unlikely(rc)) {
1054 printk(KERN_ERR "Internal error whilst attempting to get "
1055 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1056 crypt_stat->cipher, rc);
1057 goto out;
1058 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001059 rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
1060 auth_tok->session_key.encrypted_key_size,
1061 &src_sg, 1);
1062 if (rc != 1) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001063 printk(KERN_ERR "Internal error whilst attempting to convert "
1064 "auth_tok->session_key.encrypted_key to scatterlist; "
1065 "expected rc = 1; got rc = [%d]. "
1066 "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
1067 auth_tok->session_key.encrypted_key_size);
1068 goto out;
1069 }
1070 auth_tok->session_key.decrypted_key_size =
1071 auth_tok->session_key.encrypted_key_size;
Michael Halcrow5dda6992007-10-16 01:28:06 -07001072 rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
1073 auth_tok->session_key.decrypted_key_size,
1074 &dst_sg, 1);
1075 if (rc != 1) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001076 printk(KERN_ERR "Internal error whilst attempting to convert "
1077 "auth_tok->session_key.decrypted_key to scatterlist; "
1078 "expected rc = 1; got rc = [%d]\n", rc);
1079 goto out;
1080 }
1081 mutex_lock(tfm_mutex);
1082 rc = crypto_blkcipher_setkey(
1083 desc.tfm, auth_tok->token.password.session_key_encryption_key,
1084 crypt_stat->key_size);
1085 if (unlikely(rc < 0)) {
1086 mutex_unlock(tfm_mutex);
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001087 printk(KERN_ERR "Error setting key for crypto context\n");
1088 rc = -EINVAL;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001089 goto out;
Michael Halcrowe5d9cbd2006-10-30 22:07:16 -08001090 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001091 rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001092 auth_tok->session_key.encrypted_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001093 mutex_unlock(tfm_mutex);
1094 if (unlikely(rc)) {
Michael Halcrow8bba0662006-10-30 22:07:18 -08001095 printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001096 goto out;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001097 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001098 auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
1099 memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
1100 auth_tok->session_key.decrypted_key_size);
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001101 crypt_stat->flags |= ECRYPTFS_KEY_VALID;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001102 if (unlikely(ecryptfs_verbosity > 0)) {
1103 ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
1104 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001105 ecryptfs_dump_hex(crypt_stat->key,
1106 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001107 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001108out:
1109 return rc;
1110}
1111
1112/**
1113 * ecryptfs_parse_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001114 * @crypt_stat: The cryptographic context
1115 * @src: Virtual address of region of memory containing the packets
1116 * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
Michael Halcrow237fead2006-10-04 02:16:22 -07001117 *
1118 * Get crypt_stat to have the file's session key if the requisite key
1119 * is available to decrypt the session key.
1120 *
1121 * Returns Zero if a valid authentication token was retrieved and
1122 * processed; negative value for file not encrypted or for error
1123 * conditions.
1124 */
1125int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
1126 unsigned char *src,
1127 struct dentry *ecryptfs_dentry)
1128{
1129 size_t i = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001130 size_t found_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001131 size_t next_packet_is_auth_tok_packet;
Michael Halcrow237fead2006-10-04 02:16:22 -07001132 struct list_head auth_tok_list;
Michael Halcrowdd8e2902007-10-16 01:28:03 -07001133 struct ecryptfs_auth_tok *matching_auth_tok;
1134 struct ecryptfs_auth_tok *candidate_auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001135 char *candidate_auth_tok_sig;
Michael Halcrow237fead2006-10-04 02:16:22 -07001136 size_t packet_size;
1137 struct ecryptfs_auth_tok *new_auth_tok;
1138 unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001139 struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
Michael Halcrow237fead2006-10-04 02:16:22 -07001140 size_t tag_11_contents_size;
1141 size_t tag_11_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001142 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001143
1144 INIT_LIST_HEAD(&auth_tok_list);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001145 /* Parse the header to find as many packets as we can; these will be
Michael Halcrow237fead2006-10-04 02:16:22 -07001146 * added the our &auth_tok_list */
1147 next_packet_is_auth_tok_packet = 1;
1148 while (next_packet_is_auth_tok_packet) {
1149 size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
1150
1151 switch (src[i]) {
1152 case ECRYPTFS_TAG_3_PACKET_TYPE:
1153 rc = parse_tag_3_packet(crypt_stat,
1154 (unsigned char *)&src[i],
1155 &auth_tok_list, &new_auth_tok,
1156 &packet_size, max_packet_size);
1157 if (rc) {
1158 ecryptfs_printk(KERN_ERR, "Error parsing "
1159 "tag 3 packet\n");
1160 rc = -EIO;
1161 goto out_wipe_list;
1162 }
1163 i += packet_size;
1164 rc = parse_tag_11_packet((unsigned char *)&src[i],
1165 sig_tmp_space,
1166 ECRYPTFS_SIG_SIZE,
1167 &tag_11_contents_size,
1168 &tag_11_packet_size,
1169 max_packet_size);
1170 if (rc) {
1171 ecryptfs_printk(KERN_ERR, "No valid "
1172 "(ecryptfs-specific) literal "
1173 "packet containing "
1174 "authentication token "
1175 "signature found after "
1176 "tag 3 packet\n");
1177 rc = -EIO;
1178 goto out_wipe_list;
1179 }
1180 i += tag_11_packet_size;
1181 if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
1182 ecryptfs_printk(KERN_ERR, "Expected "
1183 "signature of size [%d]; "
1184 "read size [%d]\n",
1185 ECRYPTFS_SIG_SIZE,
1186 tag_11_contents_size);
1187 rc = -EIO;
1188 goto out_wipe_list;
1189 }
1190 ecryptfs_to_hex(new_auth_tok->token.password.signature,
1191 sig_tmp_space, tag_11_contents_size);
1192 new_auth_tok->token.password.signature[
1193 ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001194 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrow237fead2006-10-04 02:16:22 -07001195 break;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001196 case ECRYPTFS_TAG_1_PACKET_TYPE:
1197 rc = parse_tag_1_packet(crypt_stat,
1198 (unsigned char *)&src[i],
1199 &auth_tok_list, &new_auth_tok,
1200 &packet_size, max_packet_size);
1201 if (rc) {
1202 ecryptfs_printk(KERN_ERR, "Error parsing "
1203 "tag 1 packet\n");
1204 rc = -EIO;
1205 goto out_wipe_list;
1206 }
1207 i += packet_size;
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001208 crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001209 break;
Michael Halcrow237fead2006-10-04 02:16:22 -07001210 case ECRYPTFS_TAG_11_PACKET_TYPE:
1211 ecryptfs_printk(KERN_WARNING, "Invalid packet set "
1212 "(Tag 11 not allowed by itself)\n");
1213 rc = -EIO;
1214 goto out_wipe_list;
1215 break;
1216 default:
1217 ecryptfs_printk(KERN_DEBUG, "No packet at offset "
1218 "[%d] of the file header; hex value of "
1219 "character is [0x%.2x]\n", i, src[i]);
1220 next_packet_is_auth_tok_packet = 0;
1221 }
1222 }
1223 if (list_empty(&auth_tok_list)) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001224 printk(KERN_ERR "The lower file appears to be a non-encrypted "
1225 "eCryptfs file; this is not supported in this version "
1226 "of the eCryptfs kernel module\n");
1227 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001228 goto out;
1229 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001230 /* auth_tok_list contains the set of authentication tokens
1231 * parsed from the metadata. We need to find a matching
1232 * authentication token that has the secret component(s)
1233 * necessary to decrypt the EFEK in the auth_tok parsed from
1234 * the metadata. There may be several potential matches, but
1235 * just one will be sufficient to decrypt to get the FEK. */
1236find_next_matching_auth_tok:
1237 found_auth_tok = 0;
1238 list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001239 candidate_auth_tok = &auth_tok_list_item->auth_tok;
1240 if (unlikely(ecryptfs_verbosity > 0)) {
1241 ecryptfs_printk(KERN_DEBUG,
1242 "Considering cadidate auth tok:\n");
1243 ecryptfs_dump_auth_tok(candidate_auth_tok);
1244 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001245 rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
1246 candidate_auth_tok);
1247 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001248 printk(KERN_ERR
1249 "Unrecognized candidate auth tok type: [%d]\n",
1250 candidate_auth_tok->token_type);
1251 rc = -EINVAL;
1252 goto out_wipe_list;
1253 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001254 ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat,
1255 candidate_auth_tok_sig);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001256 if (matching_auth_tok) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001257 found_auth_tok = 1;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001258 goto found_matching_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001259 }
1260 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001261 if (!found_auth_tok) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001262 ecryptfs_printk(KERN_ERR, "Could not find a usable "
1263 "authentication token\n");
Michael Halcrow237fead2006-10-04 02:16:22 -07001264 rc = -EIO;
1265 goto out_wipe_list;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001266 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001267found_matching_auth_tok:
Michael Halcrowe2bd99e2007-02-12 00:53:49 -08001268 if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
Michael Halcrowdddfa462007-02-12 00:53:44 -08001269 memcpy(&(candidate_auth_tok->token.private_key),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001270 &(matching_auth_tok->token.private_key),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001271 sizeof(struct ecryptfs_private_key));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001272 rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001273 crypt_stat);
1274 } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001275 memcpy(&(candidate_auth_tok->token.password),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001276 &(matching_auth_tok->token.password),
Michael Halcrow237fead2006-10-04 02:16:22 -07001277 sizeof(struct ecryptfs_password));
Michael Halcrowf4aad162007-10-16 01:27:53 -07001278 rc = decrypt_passphrase_encrypted_session_key(
1279 candidate_auth_tok, crypt_stat);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001280 }
1281 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001282 struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
1283
1284 ecryptfs_printk(KERN_WARNING, "Error decrypting the "
1285 "session key for authentication token with sig "
1286 "[%.*s]; rc = [%d]. Removing auth tok "
1287 "candidate from the list and searching for "
1288 "the next match.\n", candidate_auth_tok_sig,
1289 ECRYPTFS_SIG_SIZE_HEX, rc);
1290 list_for_each_entry_safe(auth_tok_list_item,
1291 auth_tok_list_item_tmp,
1292 &auth_tok_list, list) {
1293 if (candidate_auth_tok
1294 == &auth_tok_list_item->auth_tok) {
1295 list_del(&auth_tok_list_item->list);
1296 kmem_cache_free(
1297 ecryptfs_auth_tok_list_item_cache,
1298 auth_tok_list_item);
1299 goto find_next_matching_auth_tok;
1300 }
1301 }
1302 BUG();
Michael Halcrowdddfa462007-02-12 00:53:44 -08001303 }
1304 rc = ecryptfs_compute_root_iv(crypt_stat);
1305 if (rc) {
1306 ecryptfs_printk(KERN_ERR, "Error computing "
1307 "the root IV\n");
1308 goto out_wipe_list;
Michael Halcrow237fead2006-10-04 02:16:22 -07001309 }
1310 rc = ecryptfs_init_crypt_ctx(crypt_stat);
1311 if (rc) {
1312 ecryptfs_printk(KERN_ERR, "Error initializing crypto "
1313 "context for cipher [%s]; rc = [%d]\n",
1314 crypt_stat->cipher, rc);
1315 }
1316out_wipe_list:
1317 wipe_auth_tok_list(&auth_tok_list);
1318out:
1319 return rc;
1320}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001321
Michael Halcrowdddfa462007-02-12 00:53:44 -08001322static int
1323pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
1324 struct ecryptfs_crypt_stat *crypt_stat,
1325 struct ecryptfs_key_record *key_rec)
1326{
1327 struct ecryptfs_msg_ctx *msg_ctx = NULL;
1328 char *netlink_payload;
1329 size_t netlink_payload_length;
1330 struct ecryptfs_message *msg;
1331 int rc;
1332
1333 rc = write_tag_66_packet(auth_tok->token.private_key.signature,
1334 ecryptfs_code_for_cipher_string(crypt_stat),
1335 crypt_stat, &netlink_payload,
1336 &netlink_payload_length);
1337 if (rc) {
1338 ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
1339 goto out;
1340 }
1341 rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
1342 netlink_payload_length, &msg_ctx);
1343 if (rc) {
1344 ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
1345 goto out;
1346 }
1347 rc = ecryptfs_wait_for_response(msg_ctx, &msg);
1348 if (rc) {
1349 ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
1350 "from the user space daemon\n");
1351 rc = -EIO;
1352 goto out;
1353 }
1354 rc = parse_tag_67_packet(key_rec, msg);
1355 if (rc)
1356 ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
1357 kfree(msg);
1358out:
1359 if (netlink_payload)
1360 kfree(netlink_payload);
1361 return rc;
1362}
1363/**
1364 * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
1365 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001366 * @remaining_bytes: Maximum number of bytes that can be writtn
1367 * @auth_tok: The authentication token used for generating the tag 1 packet
1368 * @crypt_stat: The cryptographic context
1369 * @key_rec: The key record struct for the tag 1 packet
Michael Halcrowdddfa462007-02-12 00:53:44 -08001370 * @packet_size: This function will write the number of bytes that end
1371 * up constituting the packet; set to zero on error
1372 *
1373 * Returns zero on success; non-zero on error.
1374 */
1375static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001376write_tag_1_packet(char *dest, size_t *remaining_bytes,
1377 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001378 struct ecryptfs_crypt_stat *crypt_stat,
Michael Halcrowdddfa462007-02-12 00:53:44 -08001379 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1380{
1381 size_t i;
1382 size_t encrypted_session_key_valid = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001383 size_t packet_size_length;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001384 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001385 int rc = 0;
1386
1387 (*packet_size) = 0;
1388 ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
1389 ECRYPTFS_SIG_SIZE);
1390 encrypted_session_key_valid = 0;
1391 for (i = 0; i < crypt_stat->key_size; i++)
1392 encrypted_session_key_valid |=
1393 auth_tok->session_key.encrypted_key[i];
1394 if (encrypted_session_key_valid) {
1395 memcpy(key_rec->enc_key,
1396 auth_tok->session_key.encrypted_key,
1397 auth_tok->session_key.encrypted_key_size);
1398 goto encrypted_session_key_set;
1399 }
1400 if (auth_tok->session_key.encrypted_key_size == 0)
1401 auth_tok->session_key.encrypted_key_size =
1402 auth_tok->token.private_key.key_size;
1403 rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
1404 if (rc) {
1405 ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
1406 "via a pki");
1407 goto out;
1408 }
1409 if (ecryptfs_verbosity > 0) {
1410 ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
1411 ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
1412 }
1413encrypted_session_key_set:
Michael Halcrowf4aad162007-10-16 01:27:53 -07001414 /* This format is inspired by OpenPGP; see RFC 2440
1415 * packet tag 1 */
1416 max_packet_size = (1 /* Tag 1 identifier */
1417 + 3 /* Max Tag 1 packet size */
1418 + 1 /* Version */
1419 + ECRYPTFS_SIG_SIZE /* Key identifier */
1420 + 1 /* Cipher identifier */
1421 + key_rec->enc_key_size); /* Encrypted key size */
1422 if (max_packet_size > (*remaining_bytes)) {
1423 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001424 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001425 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrowdddfa462007-02-12 00:53:44 -08001426 rc = -EINVAL;
1427 goto out;
1428 }
1429 dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001430 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrowdddfa462007-02-12 00:53:44 -08001431 &packet_size_length);
1432 if (rc) {
1433 ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
1434 "header; cannot generate packet length\n");
1435 goto out;
1436 }
1437 (*packet_size) += packet_size_length;
1438 dest[(*packet_size)++] = 0x03; /* version 3 */
1439 memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
1440 (*packet_size) += ECRYPTFS_SIG_SIZE;
1441 dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
1442 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1443 key_rec->enc_key_size);
1444 (*packet_size) += key_rec->enc_key_size;
1445out:
1446 if (rc)
1447 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001448 else
1449 (*remaining_bytes) -= (*packet_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001450 return rc;
1451}
Michael Halcrow237fead2006-10-04 02:16:22 -07001452
1453/**
1454 * write_tag_11_packet
1455 * @dest: Target into which Tag 11 packet is to be written
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001456 * @remaining_bytes: Maximum packet length
Michael Halcrow237fead2006-10-04 02:16:22 -07001457 * @contents: Byte array of contents to copy in
1458 * @contents_length: Number of bytes in contents
1459 * @packet_length: Length of the Tag 11 packet written; zero on error
1460 *
1461 * Returns zero on success; non-zero on error.
1462 */
1463static int
Andrew Morton81acbcd2007-10-16 01:27:59 -07001464write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
Michael Halcrow146a4602007-10-16 01:27:58 -07001465 size_t contents_length, size_t *packet_length)
Michael Halcrow237fead2006-10-04 02:16:22 -07001466{
Michael Halcrow237fead2006-10-04 02:16:22 -07001467 size_t packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001468 size_t max_packet_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001469 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001470
1471 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001472 /* This format is inspired by OpenPGP; see RFC 2440
1473 * packet tag 11 */
1474 max_packet_size = (1 /* Tag 11 identifier */
1475 + 3 /* Max Tag 11 packet size */
1476 + 1 /* Binary format specifier */
1477 + 1 /* Filename length */
1478 + 8 /* Filename ("_CONSOLE") */
1479 + 4 /* Modification date */
1480 + contents_length); /* Literal data */
1481 if (max_packet_size > (*remaining_bytes)) {
1482 printk(KERN_ERR "Packet length larger than maximum allowable; "
Andrew Morton81acbcd2007-10-16 01:27:59 -07001483 "need up to [%td] bytes, but there are only [%td] "
Michael Halcrow146a4602007-10-16 01:27:58 -07001484 "available\n", max_packet_size, (*remaining_bytes));
Michael Halcrow237fead2006-10-04 02:16:22 -07001485 rc = -EINVAL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001486 goto out;
1487 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001488 dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
Michael Halcrow237fead2006-10-04 02:16:22 -07001489 rc = write_packet_length(&dest[(*packet_length)],
Michael Halcrow146a4602007-10-16 01:27:58 -07001490 (max_packet_size - 4), &packet_size_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001491 if (rc) {
Michael Halcrow146a4602007-10-16 01:27:58 -07001492 printk(KERN_ERR "Error generating tag 11 packet header; cannot "
1493 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001494 goto out;
1495 }
1496 (*packet_length) += packet_size_length;
Michael Halcrow146a4602007-10-16 01:27:58 -07001497 dest[(*packet_length)++] = 0x62; /* binary data format specifier */
Michael Halcrow237fead2006-10-04 02:16:22 -07001498 dest[(*packet_length)++] = 8;
1499 memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
1500 (*packet_length) += 8;
Michael Halcrow237fead2006-10-04 02:16:22 -07001501 memset(&dest[(*packet_length)], 0x00, 4);
1502 (*packet_length) += 4;
Michael Halcrow237fead2006-10-04 02:16:22 -07001503 memcpy(&dest[(*packet_length)], contents, contents_length);
1504 (*packet_length) += contents_length;
1505 out:
1506 if (rc)
1507 (*packet_length) = 0;
Michael Halcrow146a4602007-10-16 01:27:58 -07001508 else
1509 (*remaining_bytes) -= (*packet_length);
Michael Halcrow237fead2006-10-04 02:16:22 -07001510 return rc;
1511}
1512
1513/**
1514 * write_tag_3_packet
1515 * @dest: Buffer into which to write the packet
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001516 * @remaining_bytes: Maximum number of bytes that can be written
Michael Halcrow237fead2006-10-04 02:16:22 -07001517 * @auth_tok: Authentication token
1518 * @crypt_stat: The cryptographic context
1519 * @key_rec: encrypted key
1520 * @packet_size: This function will write the number of bytes that end
1521 * up constituting the packet; set to zero on error
1522 *
1523 * Returns zero on success; non-zero on error.
1524 */
1525static int
Michael Halcrowf4aad162007-10-16 01:27:53 -07001526write_tag_3_packet(char *dest, size_t *remaining_bytes,
1527 struct ecryptfs_auth_tok *auth_tok,
Michael Halcrow237fead2006-10-04 02:16:22 -07001528 struct ecryptfs_crypt_stat *crypt_stat,
1529 struct ecryptfs_key_record *key_rec, size_t *packet_size)
1530{
Michael Halcrow237fead2006-10-04 02:16:22 -07001531 size_t i;
Michael Halcrow237fead2006-10-04 02:16:22 -07001532 size_t encrypted_session_key_valid = 0;
1533 char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
Michael Halcrowf4aad162007-10-16 01:27:53 -07001534 struct scatterlist dst_sg;
1535 struct scatterlist src_sg;
Michael Halcrow237fead2006-10-04 02:16:22 -07001536 struct mutex *tfm_mutex = NULL;
Michael Halcrow237fead2006-10-04 02:16:22 -07001537 size_t cipher_code;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001538 size_t packet_size_length;
1539 size_t max_packet_size;
1540 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1541 crypt_stat->mount_crypt_stat;
Michael Halcrow8bba0662006-10-30 22:07:18 -08001542 struct blkcipher_desc desc = {
1543 .tfm = NULL,
1544 .flags = CRYPTO_TFM_REQ_MAY_SLEEP
1545 };
1546 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001547
1548 (*packet_size) = 0;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001549 ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
Michael Halcrow237fead2006-10-04 02:16:22 -07001550 ECRYPTFS_SIG_SIZE);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001551 rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
1552 crypt_stat->cipher);
1553 if (unlikely(rc)) {
1554 printk(KERN_ERR "Internal error whilst attempting to get "
1555 "tfm and mutex for cipher name [%s]; rc = [%d]\n",
1556 crypt_stat->cipher, rc);
1557 goto out;
Michael Halcrow237fead2006-10-04 02:16:22 -07001558 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001559 if (mount_crypt_stat->global_default_cipher_key_size == 0) {
1560 struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
1561
1562 printk(KERN_WARNING "No key size specified at mount; "
1563 "defaulting to [%d]\n", alg->max_keysize);
1564 mount_crypt_stat->global_default_cipher_key_size =
1565 alg->max_keysize;
1566 }
1567 if (crypt_stat->key_size == 0)
1568 crypt_stat->key_size =
1569 mount_crypt_stat->global_default_cipher_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001570 if (auth_tok->session_key.encrypted_key_size == 0)
1571 auth_tok->session_key.encrypted_key_size =
1572 crypt_stat->key_size;
1573 if (crypt_stat->key_size == 24
1574 && strcmp("aes", crypt_stat->cipher) == 0) {
1575 memset((crypt_stat->key + 24), 0, 8);
1576 auth_tok->session_key.encrypted_key_size = 32;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001577 } else
1578 auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001579 key_rec->enc_key_size =
Michael Halcrow237fead2006-10-04 02:16:22 -07001580 auth_tok->session_key.encrypted_key_size;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001581 encrypted_session_key_valid = 0;
1582 for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
1583 encrypted_session_key_valid |=
1584 auth_tok->session_key.encrypted_key[i];
1585 if (encrypted_session_key_valid) {
1586 ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
1587 "using auth_tok->session_key.encrypted_key, "
1588 "where key_rec->enc_key_size = [%d]\n",
1589 key_rec->enc_key_size);
1590 memcpy(key_rec->enc_key,
1591 auth_tok->session_key.encrypted_key,
1592 key_rec->enc_key_size);
1593 goto encrypted_session_key_set;
1594 }
Michael Halcrowdddfa462007-02-12 00:53:44 -08001595 if (auth_tok->token.password.flags &
1596 ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001597 ecryptfs_printk(KERN_DEBUG, "Using previously generated "
1598 "session key encryption key of size [%d]\n",
1599 auth_tok->token.password.
1600 session_key_encryption_key_bytes);
1601 memcpy(session_key_encryption_key,
1602 auth_tok->token.password.session_key_encryption_key,
1603 crypt_stat->key_size);
1604 ecryptfs_printk(KERN_DEBUG,
1605 "Cached session key " "encryption key: \n");
1606 if (ecryptfs_verbosity > 0)
1607 ecryptfs_dump_hex(session_key_encryption_key, 16);
1608 }
1609 if (unlikely(ecryptfs_verbosity > 0)) {
1610 ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
1611 ecryptfs_dump_hex(session_key_encryption_key, 16);
1612 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001613 rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
1614 &src_sg, 1);
1615 if (rc != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001616 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001617 "for crypt_stat session key; expected rc = 1; "
1618 "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
1619 rc, key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001620 rc = -ENOMEM;
1621 goto out;
1622 }
Michael Halcrow5dda6992007-10-16 01:28:06 -07001623 rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
1624 &dst_sg, 1);
1625 if (rc != 1) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001626 ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
Michael Halcrowf4aad162007-10-16 01:27:53 -07001627 "for crypt_stat encrypted session key; "
1628 "expected rc = 1; got rc = [%d]. "
1629 "key_rec->enc_key_size = [%d]\n", rc,
1630 key_rec->enc_key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001631 rc = -ENOMEM;
1632 goto out;
1633 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001634 mutex_lock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001635 rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
1636 crypt_stat->key_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001637 if (rc < 0) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001638 mutex_unlock(tfm_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001639 ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
Michael Halcrow8bba0662006-10-30 22:07:18 -08001640 "context; rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001641 goto out;
1642 }
1643 rc = 0;
1644 ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
1645 crypt_stat->key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001646 rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
Michael Halcrow8bba0662006-10-30 22:07:18 -08001647 (*key_rec).enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001648 mutex_unlock(tfm_mutex);
Michael Halcrow8bba0662006-10-30 22:07:18 -08001649 if (rc) {
1650 printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
1651 goto out;
1652 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001653 ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
Michael Halcrowf4aad162007-10-16 01:27:53 -07001654 if (ecryptfs_verbosity > 0) {
1655 ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
1656 key_rec->enc_key_size);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001657 ecryptfs_dump_hex(key_rec->enc_key,
1658 key_rec->enc_key_size);
Michael Halcrowf4aad162007-10-16 01:27:53 -07001659 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001660encrypted_session_key_set:
Michael Halcrow237fead2006-10-04 02:16:22 -07001661 /* This format is inspired by OpenPGP; see RFC 2440
1662 * packet tag 3 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001663 max_packet_size = (1 /* Tag 3 identifier */
1664 + 3 /* Max Tag 3 packet size */
1665 + 1 /* Version */
1666 + 1 /* Cipher code */
1667 + 1 /* S2K specifier */
1668 + 1 /* Hash identifier */
1669 + ECRYPTFS_SALT_SIZE /* Salt */
1670 + 1 /* Hash iterations */
1671 + key_rec->enc_key_size); /* Encrypted key size */
1672 if (max_packet_size > (*remaining_bytes)) {
Andrew Morton81acbcd2007-10-16 01:27:59 -07001673 printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
1674 "there are only [%td] available\n", max_packet_size,
Michael Halcrowf4aad162007-10-16 01:27:53 -07001675 (*remaining_bytes));
1676 rc = -EINVAL;
1677 goto out;
1678 }
Michael Halcrow237fead2006-10-04 02:16:22 -07001679 dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001680 /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
1681 * to get the number of octets in the actual Tag 3 packet */
1682 rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
Michael Halcrow237fead2006-10-04 02:16:22 -07001683 &packet_size_length);
1684 if (rc) {
Michael Halcrowf4aad162007-10-16 01:27:53 -07001685 printk(KERN_ERR "Error generating tag 3 packet header; cannot "
1686 "generate packet length. rc = [%d]\n", rc);
Michael Halcrow237fead2006-10-04 02:16:22 -07001687 goto out;
1688 }
1689 (*packet_size) += packet_size_length;
1690 dest[(*packet_size)++] = 0x04; /* version 4 */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001691 /* TODO: Break from RFC2440 so that arbitrary ciphers can be
1692 * specified with strings */
Michael Halcrow237fead2006-10-04 02:16:22 -07001693 cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
1694 if (cipher_code == 0) {
1695 ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
1696 "cipher [%s]\n", crypt_stat->cipher);
1697 rc = -EINVAL;
1698 goto out;
1699 }
1700 dest[(*packet_size)++] = cipher_code;
1701 dest[(*packet_size)++] = 0x03; /* S2K */
1702 dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
1703 memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
1704 ECRYPTFS_SALT_SIZE);
1705 (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
1706 dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
Michael Halcrowdddfa462007-02-12 00:53:44 -08001707 memcpy(&dest[(*packet_size)], key_rec->enc_key,
1708 key_rec->enc_key_size);
1709 (*packet_size) += key_rec->enc_key_size;
Michael Halcrow237fead2006-10-04 02:16:22 -07001710out:
Michael Halcrow237fead2006-10-04 02:16:22 -07001711 if (rc)
1712 (*packet_size) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001713 else
1714 (*remaining_bytes) -= (*packet_size);
Michael Halcrow237fead2006-10-04 02:16:22 -07001715 return rc;
1716}
1717
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001718struct kmem_cache *ecryptfs_key_record_cache;
1719
Michael Halcrow237fead2006-10-04 02:16:22 -07001720/**
1721 * ecryptfs_generate_key_packet_set
Michael Halcrow22e78fa2007-10-16 01:28:02 -07001722 * @dest_base: Virtual address from which to write the key record set
Michael Halcrow237fead2006-10-04 02:16:22 -07001723 * @crypt_stat: The cryptographic context from which the
1724 * authentication tokens will be retrieved
1725 * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
1726 * for the global parameters
1727 * @len: The amount written
1728 * @max: The maximum amount of data allowed to be written
1729 *
1730 * Generates a key packet set and writes it to the virtual address
1731 * passed in.
1732 *
1733 * Returns zero on success; non-zero on error.
1734 */
1735int
1736ecryptfs_generate_key_packet_set(char *dest_base,
1737 struct ecryptfs_crypt_stat *crypt_stat,
1738 struct dentry *ecryptfs_dentry, size_t *len,
1739 size_t max)
1740{
Michael Halcrow237fead2006-10-04 02:16:22 -07001741 struct ecryptfs_auth_tok *auth_tok;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001742 struct ecryptfs_global_auth_tok *global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001743 struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
1744 &ecryptfs_superblock_to_private(
1745 ecryptfs_dentry->d_sb)->mount_crypt_stat;
1746 size_t written;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001747 struct ecryptfs_key_record *key_rec;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001748 struct ecryptfs_key_sig *key_sig;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001749 int rc = 0;
Michael Halcrow237fead2006-10-04 02:16:22 -07001750
1751 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001752 mutex_lock(&crypt_stat->keysig_list_mutex);
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001753 key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
1754 if (!key_rec) {
1755 rc = -ENOMEM;
1756 goto out;
1757 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001758 list_for_each_entry(key_sig, &crypt_stat->keysig_list,
1759 crypt_stat_list) {
1760 memset(key_rec, 0, sizeof(*key_rec));
1761 rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
1762 mount_crypt_stat,
1763 key_sig->keysig);
1764 if (rc) {
1765 printk(KERN_ERR "Error attempting to get the global "
1766 "auth_tok; rc = [%d]\n", rc);
1767 goto out_free;
1768 }
1769 if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
1770 printk(KERN_WARNING
1771 "Skipping invalid auth tok with sig = [%s]\n",
1772 global_auth_tok->sig);
1773 continue;
1774 }
1775 auth_tok = global_auth_tok->global_auth_tok;
Michael Halcrow237fead2006-10-04 02:16:22 -07001776 if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
1777 rc = write_tag_3_packet((dest_base + (*len)),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001778 &max, auth_tok,
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001779 crypt_stat, key_rec,
Michael Halcrow237fead2006-10-04 02:16:22 -07001780 &written);
1781 if (rc) {
1782 ecryptfs_printk(KERN_WARNING, "Error "
1783 "writing tag 3 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001784 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001785 }
1786 (*len) += written;
1787 /* Write auth tok signature packet */
Michael Halcrowf4aad162007-10-16 01:27:53 -07001788 rc = write_tag_11_packet((dest_base + (*len)), &max,
1789 key_rec->sig,
1790 ECRYPTFS_SIG_SIZE, &written);
Michael Halcrow237fead2006-10-04 02:16:22 -07001791 if (rc) {
1792 ecryptfs_printk(KERN_ERR, "Error writing "
1793 "auth tok signature packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001794 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001795 }
1796 (*len) += written;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001797 } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
1798 rc = write_tag_1_packet(dest_base + (*len),
Michael Halcrowf4aad162007-10-16 01:27:53 -07001799 &max, auth_tok,
1800 crypt_stat, key_rec, &written);
Michael Halcrowdddfa462007-02-12 00:53:44 -08001801 if (rc) {
1802 ecryptfs_printk(KERN_WARNING, "Error "
1803 "writing tag 1 packet\n");
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001804 goto out_free;
Michael Halcrowdddfa462007-02-12 00:53:44 -08001805 }
1806 (*len) += written;
Michael Halcrow237fead2006-10-04 02:16:22 -07001807 } else {
1808 ecryptfs_printk(KERN_WARNING, "Unsupported "
1809 "authentication token type\n");
1810 rc = -EINVAL;
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001811 goto out_free;
Michael Halcrow237fead2006-10-04 02:16:22 -07001812 }
Michael Halcrowf4aad162007-10-16 01:27:53 -07001813 }
1814 if (likely(max > 0)) {
Michael Halcrow237fead2006-10-04 02:16:22 -07001815 dest_base[(*len)] = 0x00;
1816 } else {
1817 ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
1818 rc = -EIO;
1819 }
Michael Halcroweb95e7f2007-02-16 01:28:40 -08001820out_free:
1821 kmem_cache_free(ecryptfs_key_record_cache, key_rec);
Michael Halcrow237fead2006-10-04 02:16:22 -07001822out:
1823 if (rc)
1824 (*len) = 0;
Michael Halcrowf4aad162007-10-16 01:27:53 -07001825 mutex_unlock(&crypt_stat->keysig_list_mutex);
Michael Halcrow237fead2006-10-04 02:16:22 -07001826 return rc;
1827}
Michael Halcrowf4aad162007-10-16 01:27:53 -07001828
1829struct kmem_cache *ecryptfs_key_sig_cache;
1830
1831int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
1832{
1833 struct ecryptfs_key_sig *new_key_sig;
1834 int rc = 0;
1835
1836 new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
1837 if (!new_key_sig) {
1838 rc = -ENOMEM;
1839 printk(KERN_ERR
1840 "Error allocating from ecryptfs_key_sig_cache\n");
1841 goto out;
1842 }
1843 memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
1844 mutex_lock(&crypt_stat->keysig_list_mutex);
1845 list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
1846 mutex_unlock(&crypt_stat->keysig_list_mutex);
1847out:
1848 return rc;
1849}
1850
1851struct kmem_cache *ecryptfs_global_auth_tok_cache;
1852
1853int
1854ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
1855 char *sig)
1856{
1857 struct ecryptfs_global_auth_tok *new_auth_tok;
1858 int rc = 0;
1859
1860 new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
1861 GFP_KERNEL);
1862 if (!new_auth_tok) {
1863 rc = -ENOMEM;
1864 printk(KERN_ERR "Error allocating from "
1865 "ecryptfs_global_auth_tok_cache\n");
1866 goto out;
1867 }
1868 memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
1869 new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
1870 mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
1871 list_add(&new_auth_tok->mount_crypt_stat_list,
1872 &mount_crypt_stat->global_auth_tok_list);
1873 mount_crypt_stat->num_global_auth_toks++;
1874 mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
1875out:
1876 return rc;
1877}
1878