Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
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> | |
dddfa461 | 10 | * Trevor S. Highland <trevor.highland@gmail.com> |
237fead6 MH |
11 | * |
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> | |
237fead6 MH |
29 | #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 | */ | |
42 | int 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 | ||
237fead6 MH |
67 | /** |
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 | */ | |
76 | static 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 */ | |
dddfa461 | 85 | (*size) = (unsigned char)data[0]; |
237fead6 MH |
86 | (*length_size) = 1; |
87 | } else if (data[0] < 224) { | |
88 | /* Two-byte length */ | |
dddfa461 MH |
89 | (*size) = (((unsigned char)(data[0]) - 192) * 256); |
90 | (*size) += ((unsigned char)(data[1]) + 192); | |
237fead6 MH |
91 | (*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 | } | |
103 | out: | |
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 | */ | |
117 | static 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 | ||
dddfa461 MH |
137 | static int |
138 | write_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; | |
187 | out: | |
188 | return rc; | |
189 | } | |
190 | ||
191 | static int | |
192 | parse_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 | } | |
272 | out: | |
273 | return rc; | |
274 | } | |
275 | ||
276 | ||
277 | static int | |
278 | write_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; | |
334 | out: | |
335 | return rc; | |
336 | } | |
337 | ||
338 | static int | |
339 | parse_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); | |
395 | out: | |
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 | */ | |
f4aad16a MH |
405 | static int |
406 | decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
407 | struct ecryptfs_crypt_stat *crypt_stat) | |
dddfa461 MH |
408 | { |
409 | u16 cipher_code = 0; | |
410 | struct ecryptfs_msg_ctx *msg_ctx; | |
411 | struct ecryptfs_message *msg = NULL; | |
f4aad16a | 412 | char *auth_tok_sig; |
dddfa461 MH |
413 | char *netlink_message; |
414 | size_t netlink_message_length; | |
415 | int rc; | |
416 | ||
f4aad16a MH |
417 | 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), | |
dddfa461 MH |
423 | &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 | } | |
464 | out: | |
465 | if (msg) | |
466 | kfree(msg); | |
467 | return rc; | |
468 | } | |
469 | ||
470 | static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) | |
471 | { | |
dddfa461 | 472 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
e0869cc1 | 473 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
dddfa461 | 474 | |
e0869cc1 MH |
475 | 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); | |
dddfa461 MH |
478 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, |
479 | auth_tok_list_item); | |
480 | } | |
dddfa461 MH |
481 | } |
482 | ||
483 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; | |
484 | ||
dddfa461 MH |
485 | /** |
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 | */ | |
502 | static int | |
503 | parse_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; | |
13218179 MH |
515 | /** |
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 | |
dddfa461 | 527 | */ |
13218179 MH |
528 | if (unlikely(max_packet_size < 12)) { |
529 | printk(KERN_ERR "Invalid max packet size; must be >=12\n"); | |
dddfa461 MH |
530 | rc = -EINVAL; |
531 | goto out; | |
532 | } | |
dddfa461 | 533 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { |
13218179 MH |
534 | printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", |
535 | ECRYPTFS_TAG_1_PACKET_TYPE); | |
dddfa461 MH |
536 | 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 = | |
13218179 MH |
542 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, |
543 | GFP_KERNEL); | |
dddfa461 | 544 | if (!auth_tok_list_item) { |
13218179 | 545 | printk(KERN_ERR "Unable to allocate memory\n"); |
dddfa461 MH |
546 | rc = -ENOMEM; |
547 | goto out; | |
548 | } | |
dddfa461 | 549 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
13218179 MH |
550 | 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); | |
dddfa461 MH |
554 | goto out_free; |
555 | } | |
13218179 MH |
556 | if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { |
557 | printk(KERN_WARNING "Invalid body size ([%d])\n", body_size); | |
dddfa461 MH |
558 | rc = -EINVAL; |
559 | goto out_free; | |
560 | } | |
561 | (*packet_size) += length_size; | |
562 | if (unlikely((*packet_size) + body_size > max_packet_size)) { | |
13218179 | 563 | printk(KERN_WARNING "Packet size exceeds max\n"); |
dddfa461 MH |
564 | rc = -EINVAL; |
565 | goto out_free; | |
566 | } | |
dddfa461 | 567 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
13218179 MH |
568 | printk(KERN_WARNING "Unknown version number [%d]\n", |
569 | data[(*packet_size) - 1]); | |
dddfa461 MH |
570 | rc = -EINVAL; |
571 | goto out_free; | |
572 | } | |
dddfa461 MH |
573 | 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 = | |
13218179 | 580 | body_size - (ECRYPTFS_SIG_SIZE + 2); |
dddfa461 MH |
581 | if ((*new_auth_tok)->session_key.encrypted_key_size |
582 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
13218179 MH |
583 | printk(KERN_WARNING "Tag 1 packet contains key larger " |
584 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); | |
dddfa461 MH |
585 | rc = -EINVAL; |
586 | goto out; | |
587 | } | |
dddfa461 | 588 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
13218179 | 589 | &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); |
dddfa461 MH |
590 | (*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; | |
13218179 | 596 | (*new_auth_tok)->flags = 0; |
e2bd99ec MH |
597 | (*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); | |
dddfa461 MH |
601 | list_add(&auth_tok_list_item->list, auth_tok_list); |
602 | goto out; | |
603 | out_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); | |
609 | out: | |
610 | if (rc) | |
611 | (*packet_size) = 0; | |
612 | return rc; | |
613 | } | |
614 | ||
237fead6 MH |
615 | /** |
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 | */ | |
633 | static int | |
634 | parse_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 | { | |
237fead6 MH |
639 | size_t body_size; |
640 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | |
641 | size_t length_size; | |
dddfa461 | 642 | int rc = 0; |
237fead6 MH |
643 | |
644 | (*packet_size) = 0; | |
645 | (*new_auth_tok) = NULL; | |
c59becfc MH |
646 | /** |
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 | |
237fead6 | 661 | */ |
c59becfc MH |
662 | if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { |
663 | printk(KERN_ERR "Max packet size too large\n"); | |
237fead6 MH |
664 | rc = -EINVAL; |
665 | goto out; | |
666 | } | |
237fead6 | 667 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { |
c59becfc MH |
668 | printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", |
669 | ECRYPTFS_TAG_3_PACKET_TYPE); | |
237fead6 MH |
670 | 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 = | |
c3762229 | 676 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); |
237fead6 | 677 | if (!auth_tok_list_item) { |
c59becfc | 678 | printk(KERN_ERR "Unable to allocate memory\n"); |
237fead6 MH |
679 | rc = -ENOMEM; |
680 | goto out; | |
681 | } | |
237fead6 | 682 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
c59becfc MH |
683 | 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); | |
237fead6 MH |
687 | goto out_free; |
688 | } | |
c59becfc MH |
689 | if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { |
690 | printk(KERN_WARNING "Invalid body size ([%d])\n", body_size); | |
237fead6 MH |
691 | rc = -EINVAL; |
692 | goto out_free; | |
693 | } | |
694 | (*packet_size) += length_size; | |
237fead6 | 695 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
c59becfc | 696 | printk(KERN_ERR "Packet size exceeds max\n"); |
237fead6 MH |
697 | rc = -EINVAL; |
698 | goto out_free; | |
699 | } | |
237fead6 | 700 | (*new_auth_tok)->session_key.encrypted_key_size = |
c59becfc | 701 | (body_size - (ECRYPTFS_SALT_SIZE + 5)); |
237fead6 | 702 | if (unlikely(data[(*packet_size)++] != 0x04)) { |
c59becfc MH |
703 | printk(KERN_WARNING "Unknown version number [%d]\n", |
704 | data[(*packet_size) - 1]); | |
237fead6 MH |
705 | rc = -EINVAL; |
706 | goto out_free; | |
707 | } | |
237fead6 MH |
708 | 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); | |
237fead6 | 721 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
c59becfc | 722 | printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); |
237fead6 MH |
723 | rc = -ENOSYS; |
724 | goto out_free; | |
725 | } | |
237fead6 | 726 | /* TODO: finish the hash mapping */ |
237fead6 MH |
727 | switch (data[(*packet_size)++]) { |
728 | case 0x01: /* See RFC2440 for these numbers and their mappings */ | |
729 | /* Choose MD5 */ | |
237fead6 MH |
730 | memcpy((*new_auth_tok)->token.password.salt, |
731 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); | |
732 | (*packet_size) += ECRYPTFS_SALT_SIZE; | |
237fead6 | 733 | /* This conversion was taken straight from RFC2440 */ |
237fead6 MH |
734 | (*new_auth_tok)->token.password.hash_iterations = |
735 | ((u32) 16 + (data[(*packet_size)] & 15)) | |
736 | << ((data[(*packet_size)] >> 4) + 6); | |
737 | (*packet_size)++; | |
c59becfc MH |
738 | /* Friendly reminder: |
739 | * (*new_auth_tok)->session_key.encrypted_key_size = | |
740 | * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ | |
237fead6 MH |
741 | 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; | |
c59becfc | 750 | (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ |
237fead6 MH |
751 | 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. */ | |
e2bd99ec MH |
761 | (*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); | |
237fead6 MH |
765 | list_add(&auth_tok_list_item->list, auth_tok_list); |
766 | goto out; | |
767 | out_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); | |
773 | out: | |
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 | */ | |
795 | static int | |
796 | parse_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 | { | |
237fead6 MH |
800 | size_t body_size; |
801 | size_t length_size; | |
dddfa461 | 802 | int rc = 0; |
237fead6 MH |
803 | |
804 | (*packet_size) = 0; | |
805 | (*tag_11_contents_size) = 0; | |
806 | ||
807 | /* check that: | |
808 | * one byte for the Tag 11 ID flag | |
809 | * two bytes for the Tag 11 length | |
810 | * do not exceed the maximum_packet_size | |
811 | */ | |
812 | if (unlikely((*packet_size) + 3 > max_packet_size)) { | |
813 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | |
814 | rc = -EINVAL; | |
815 | goto out; | |
816 | } | |
817 | ||
818 | /* check for Tag 11 identifyer - one byte */ | |
819 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { | |
820 | ecryptfs_printk(KERN_WARNING, | |
821 | "Invalid tag 11 packet format\n"); | |
822 | rc = -EINVAL; | |
823 | goto out; | |
824 | } | |
825 | ||
826 | /* get Tag 11 content length - one or two bytes */ | |
827 | rc = parse_packet_length(&data[(*packet_size)], &body_size, | |
828 | &length_size); | |
829 | if (rc) { | |
830 | ecryptfs_printk(KERN_WARNING, | |
831 | "Invalid tag 11 packet format\n"); | |
832 | goto out; | |
833 | } | |
834 | (*packet_size) += length_size; | |
835 | ||
836 | if (body_size < 13) { | |
837 | ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n", | |
838 | body_size); | |
839 | rc = -EINVAL; | |
840 | goto out; | |
841 | } | |
842 | /* We have 13 bytes of surrounding packet values */ | |
843 | (*tag_11_contents_size) = (body_size - 13); | |
844 | ||
845 | /* now we know the length of the remainting Tag 11 packet size: | |
846 | * 14 fix bytes for: special flag one, special flag two, | |
847 | * 12 skipped bytes | |
848 | * body_size bytes minus the stuff above is the Tag 11 content | |
849 | */ | |
850 | /* FIXME why is the body size one byte smaller than the actual | |
851 | * size of the body? | |
852 | * this seems to be an error here as well as in | |
853 | * write_tag_11_packet() */ | |
854 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { | |
855 | ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n"); | |
856 | rc = -EINVAL; | |
857 | goto out; | |
858 | } | |
859 | ||
860 | /* special flag one - one byte */ | |
861 | if (data[(*packet_size)++] != 0x62) { | |
862 | ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); | |
863 | rc = -EINVAL; | |
864 | goto out; | |
865 | } | |
866 | ||
867 | /* special flag two - one byte */ | |
868 | if (data[(*packet_size)++] != 0x08) { | |
869 | ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n"); | |
870 | rc = -EINVAL; | |
871 | goto out; | |
872 | } | |
873 | ||
874 | /* skip the next 12 bytes */ | |
875 | (*packet_size) += 12; /* We don't care about the filename or | |
876 | * the timestamp */ | |
877 | ||
878 | /* get the Tag 11 contents - tag_11_contents_size bytes */ | |
879 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); | |
880 | (*packet_size) += (*tag_11_contents_size); | |
881 | ||
882 | out: | |
883 | if (rc) { | |
884 | (*packet_size) = 0; | |
885 | (*tag_11_contents_size) = 0; | |
886 | } | |
887 | return rc; | |
888 | } | |
889 | ||
f4aad16a MH |
890 | static int |
891 | ecryptfs_find_global_auth_tok_for_sig( | |
892 | struct ecryptfs_global_auth_tok **global_auth_tok, | |
893 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) | |
894 | { | |
895 | struct ecryptfs_global_auth_tok *walker; | |
896 | int rc = 0; | |
897 | ||
898 | (*global_auth_tok) = NULL; | |
899 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
900 | list_for_each_entry(walker, | |
901 | &mount_crypt_stat->global_auth_tok_list, | |
902 | mount_crypt_stat_list) { | |
903 | if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { | |
904 | (*global_auth_tok) = walker; | |
905 | goto out; | |
906 | } | |
907 | } | |
908 | rc = -EINVAL; | |
909 | out: | |
910 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
911 | return rc; | |
912 | } | |
913 | ||
237fead6 | 914 | /** |
f4aad16a MH |
915 | * ecryptfs_verify_version |
916 | * @version: The version number to confirm | |
917 | * | |
918 | * Returns zero on good version; non-zero otherwise | |
919 | */ | |
920 | static int ecryptfs_verify_version(u16 version) | |
921 | { | |
922 | int rc = 0; | |
923 | unsigned char major; | |
924 | unsigned char minor; | |
925 | ||
926 | major = ((version >> 8) & 0xFF); | |
927 | minor = (version & 0xFF); | |
928 | if (major != ECRYPTFS_VERSION_MAJOR) { | |
929 | ecryptfs_printk(KERN_ERR, "Major version number mismatch. " | |
930 | "Expected [%d]; got [%d]\n", | |
931 | ECRYPTFS_VERSION_MAJOR, major); | |
932 | rc = -EINVAL; | |
933 | goto out; | |
934 | } | |
935 | if (minor != ECRYPTFS_VERSION_MINOR) { | |
936 | ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " | |
937 | "Expected [%d]; got [%d]\n", | |
938 | ECRYPTFS_VERSION_MINOR, minor); | |
939 | rc = -EINVAL; | |
940 | goto out; | |
941 | } | |
942 | out: | |
943 | return rc; | |
944 | } | |
945 | ||
946 | int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, | |
947 | struct ecryptfs_auth_tok **auth_tok, | |
948 | char *sig) | |
949 | { | |
950 | int rc = 0; | |
951 | ||
952 | (*auth_tok_key) = request_key(&key_type_user, sig, NULL); | |
953 | if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { | |
954 | printk(KERN_ERR "Could not find key with description: [%s]\n", | |
955 | sig); | |
956 | process_request_key_err(PTR_ERR(*auth_tok_key)); | |
957 | rc = -EINVAL; | |
958 | goto out; | |
959 | } | |
960 | (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); | |
961 | if (ecryptfs_verify_version((*auth_tok)->version)) { | |
962 | printk(KERN_ERR | |
963 | "Data structure version mismatch. " | |
964 | "Userspace tools must match eCryptfs " | |
965 | "kernel module with major version [%d] " | |
966 | "and minor version [%d]\n", | |
967 | ECRYPTFS_VERSION_MAJOR, | |
968 | ECRYPTFS_VERSION_MINOR); | |
969 | rc = -EINVAL; | |
970 | goto out; | |
971 | } | |
972 | if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD | |
973 | && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { | |
974 | printk(KERN_ERR "Invalid auth_tok structure " | |
975 | "returned from key query\n"); | |
976 | rc = -EINVAL; | |
977 | goto out; | |
978 | } | |
979 | out: | |
980 | return rc; | |
981 | } | |
982 | ||
983 | /** | |
984 | * ecryptfs_find_auth_tok_for_sig | |
985 | * @auth_tok: Set to the matching auth_tok; NULL if not found | |
986 | * @crypt_stat: inode crypt_stat crypto context | |
987 | * @sig: Sig of auth_tok to find | |
988 | * | |
989 | * For now, this function simply looks at the registered auth_tok's | |
990 | * linked off the mount_crypt_stat, so all the auth_toks that can be | |
991 | * used must be registered at mount time. This function could | |
992 | * potentially try a lot harder to find auth_tok's (e.g., by calling | |
993 | * out to ecryptfsd to dynamically retrieve an auth_tok object) so | |
994 | * that static registration of auth_tok's will no longer be necessary. | |
995 | * | |
996 | * Returns zero on no error; non-zero on error | |
997 | */ | |
998 | static int | |
999 | ecryptfs_find_auth_tok_for_sig( | |
1000 | struct ecryptfs_auth_tok **auth_tok, | |
1001 | struct ecryptfs_crypt_stat *crypt_stat, char *sig) | |
1002 | { | |
1003 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | |
1004 | crypt_stat->mount_crypt_stat; | |
1005 | struct ecryptfs_global_auth_tok *global_auth_tok; | |
1006 | int rc = 0; | |
1007 | ||
1008 | (*auth_tok) = NULL; | |
1009 | if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | |
1010 | mount_crypt_stat, sig)) { | |
1011 | struct key *auth_tok_key; | |
1012 | ||
1013 | rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, | |
1014 | sig); | |
1015 | } else | |
1016 | (*auth_tok) = global_auth_tok->global_auth_tok; | |
1017 | return rc; | |
1018 | } | |
1019 | ||
1020 | /** | |
1021 | * decrypt_passphrase_encrypted_session_key - Decrypt the session key | |
1022 | * with the given auth_tok. | |
237fead6 MH |
1023 | * |
1024 | * Returns Zero on success; non-zero error otherwise. | |
1025 | */ | |
f4aad16a MH |
1026 | static int |
1027 | decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1028 | struct ecryptfs_crypt_stat *crypt_stat) | |
237fead6 | 1029 | { |
f4aad16a MH |
1030 | struct scatterlist dst_sg; |
1031 | struct scatterlist src_sg; | |
237fead6 | 1032 | struct mutex *tfm_mutex = NULL; |
8bba066f MH |
1033 | struct blkcipher_desc desc = { |
1034 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | |
1035 | }; | |
1036 | int rc = 0; | |
237fead6 | 1037 | |
f4aad16a MH |
1038 | if (unlikely(ecryptfs_verbosity > 0)) { |
1039 | ecryptfs_printk( | |
1040 | KERN_DEBUG, "Session key encryption key (size [%d]):\n", | |
1041 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1042 | ecryptfs_dump_hex( | |
1043 | auth_tok->token.password.session_key_encryption_key, | |
1044 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1045 | } | |
1046 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, | |
1047 | crypt_stat->cipher); | |
1048 | if (unlikely(rc)) { | |
1049 | printk(KERN_ERR "Internal error whilst attempting to get " | |
1050 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
1051 | crypt_stat->cipher, rc); | |
1052 | goto out; | |
237fead6 | 1053 | } |
f4aad16a MH |
1054 | if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, |
1055 | auth_tok->session_key.encrypted_key_size, | |
1056 | &src_sg, 1)) != 1) { | |
1057 | printk(KERN_ERR "Internal error whilst attempting to convert " | |
1058 | "auth_tok->session_key.encrypted_key to scatterlist; " | |
1059 | "expected rc = 1; got rc = [%d]. " | |
1060 | "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, | |
1061 | auth_tok->session_key.encrypted_key_size); | |
1062 | goto out; | |
1063 | } | |
1064 | auth_tok->session_key.decrypted_key_size = | |
1065 | auth_tok->session_key.encrypted_key_size; | |
1066 | if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, | |
1067 | auth_tok->session_key.decrypted_key_size, | |
1068 | &dst_sg, 1)) != 1) { | |
1069 | printk(KERN_ERR "Internal error whilst attempting to convert " | |
1070 | "auth_tok->session_key.decrypted_key to scatterlist; " | |
1071 | "expected rc = 1; got rc = [%d]\n", rc); | |
1072 | goto out; | |
1073 | } | |
1074 | mutex_lock(tfm_mutex); | |
1075 | rc = crypto_blkcipher_setkey( | |
1076 | desc.tfm, auth_tok->token.password.session_key_encryption_key, | |
1077 | crypt_stat->key_size); | |
1078 | if (unlikely(rc < 0)) { | |
1079 | mutex_unlock(tfm_mutex); | |
e5d9cbde MH |
1080 | printk(KERN_ERR "Error setting key for crypto context\n"); |
1081 | rc = -EINVAL; | |
f4aad16a | 1082 | goto out; |
237fead6 | 1083 | } |
f4aad16a | 1084 | rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, |
8bba066f | 1085 | auth_tok->session_key.encrypted_key_size); |
f4aad16a MH |
1086 | mutex_unlock(tfm_mutex); |
1087 | if (unlikely(rc)) { | |
8bba066f | 1088 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); |
f4aad16a | 1089 | goto out; |
8bba066f | 1090 | } |
237fead6 MH |
1091 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
1092 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | |
1093 | auth_tok->session_key.decrypted_key_size); | |
e2bd99ec | 1094 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
f4aad16a MH |
1095 | if (unlikely(ecryptfs_verbosity > 0)) { |
1096 | ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", | |
1097 | crypt_stat->key_size); | |
237fead6 MH |
1098 | ecryptfs_dump_hex(crypt_stat->key, |
1099 | crypt_stat->key_size); | |
f4aad16a | 1100 | } |
237fead6 MH |
1101 | out: |
1102 | return rc; | |
1103 | } | |
1104 | ||
f4aad16a MH |
1105 | int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) |
1106 | { | |
1107 | int rc = 0; | |
1108 | ||
1109 | (*sig) = NULL; | |
1110 | switch (auth_tok->token_type) { | |
1111 | case ECRYPTFS_PASSWORD: | |
1112 | (*sig) = auth_tok->token.password.signature; | |
1113 | break; | |
1114 | case ECRYPTFS_PRIVATE_KEY: | |
1115 | (*sig) = auth_tok->token.private_key.signature; | |
1116 | break; | |
1117 | default: | |
1118 | printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", | |
1119 | auth_tok->token_type); | |
1120 | rc = -EINVAL; | |
1121 | } | |
1122 | return rc; | |
1123 | } | |
1124 | ||
237fead6 MH |
1125 | /** |
1126 | * ecryptfs_parse_packet_set | |
1127 | * @dest: The header page in memory | |
1128 | * @version: Version of file format, to guide parsing behavior | |
1129 | * | |
1130 | * Get crypt_stat to have the file's session key if the requisite key | |
1131 | * is available to decrypt the session key. | |
1132 | * | |
1133 | * Returns Zero if a valid authentication token was retrieved and | |
1134 | * processed; negative value for file not encrypted or for error | |
1135 | * conditions. | |
1136 | */ | |
1137 | int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, | |
1138 | unsigned char *src, | |
1139 | struct dentry *ecryptfs_dentry) | |
1140 | { | |
1141 | size_t i = 0; | |
f4aad16a | 1142 | size_t found_auth_tok; |
237fead6 | 1143 | size_t next_packet_is_auth_tok_packet; |
237fead6 | 1144 | struct list_head auth_tok_list; |
f4aad16a | 1145 | struct ecryptfs_auth_tok *matching_auth_tok = NULL; |
237fead6 | 1146 | struct ecryptfs_auth_tok *candidate_auth_tok = NULL; |
f4aad16a | 1147 | char *candidate_auth_tok_sig; |
237fead6 MH |
1148 | size_t packet_size; |
1149 | struct ecryptfs_auth_tok *new_auth_tok; | |
1150 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; | |
f4aad16a | 1151 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
237fead6 MH |
1152 | size_t tag_11_contents_size; |
1153 | size_t tag_11_packet_size; | |
dddfa461 | 1154 | int rc = 0; |
237fead6 MH |
1155 | |
1156 | INIT_LIST_HEAD(&auth_tok_list); | |
f4aad16a | 1157 | /* Parse the header to find as many packets as we can; these will be |
237fead6 MH |
1158 | * added the our &auth_tok_list */ |
1159 | next_packet_is_auth_tok_packet = 1; | |
1160 | while (next_packet_is_auth_tok_packet) { | |
1161 | size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); | |
1162 | ||
1163 | switch (src[i]) { | |
1164 | case ECRYPTFS_TAG_3_PACKET_TYPE: | |
1165 | rc = parse_tag_3_packet(crypt_stat, | |
1166 | (unsigned char *)&src[i], | |
1167 | &auth_tok_list, &new_auth_tok, | |
1168 | &packet_size, max_packet_size); | |
1169 | if (rc) { | |
1170 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1171 | "tag 3 packet\n"); | |
1172 | rc = -EIO; | |
1173 | goto out_wipe_list; | |
1174 | } | |
1175 | i += packet_size; | |
1176 | rc = parse_tag_11_packet((unsigned char *)&src[i], | |
1177 | sig_tmp_space, | |
1178 | ECRYPTFS_SIG_SIZE, | |
1179 | &tag_11_contents_size, | |
1180 | &tag_11_packet_size, | |
1181 | max_packet_size); | |
1182 | if (rc) { | |
1183 | ecryptfs_printk(KERN_ERR, "No valid " | |
1184 | "(ecryptfs-specific) literal " | |
1185 | "packet containing " | |
1186 | "authentication token " | |
1187 | "signature found after " | |
1188 | "tag 3 packet\n"); | |
1189 | rc = -EIO; | |
1190 | goto out_wipe_list; | |
1191 | } | |
1192 | i += tag_11_packet_size; | |
1193 | if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { | |
1194 | ecryptfs_printk(KERN_ERR, "Expected " | |
1195 | "signature of size [%d]; " | |
1196 | "read size [%d]\n", | |
1197 | ECRYPTFS_SIG_SIZE, | |
1198 | tag_11_contents_size); | |
1199 | rc = -EIO; | |
1200 | goto out_wipe_list; | |
1201 | } | |
1202 | ecryptfs_to_hex(new_auth_tok->token.password.signature, | |
1203 | sig_tmp_space, tag_11_contents_size); | |
1204 | new_auth_tok->token.password.signature[ | |
1205 | ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; | |
e2bd99ec | 1206 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
237fead6 | 1207 | break; |
dddfa461 MH |
1208 | case ECRYPTFS_TAG_1_PACKET_TYPE: |
1209 | rc = parse_tag_1_packet(crypt_stat, | |
1210 | (unsigned char *)&src[i], | |
1211 | &auth_tok_list, &new_auth_tok, | |
1212 | &packet_size, max_packet_size); | |
1213 | if (rc) { | |
1214 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1215 | "tag 1 packet\n"); | |
1216 | rc = -EIO; | |
1217 | goto out_wipe_list; | |
1218 | } | |
1219 | i += packet_size; | |
e2bd99ec | 1220 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
dddfa461 | 1221 | break; |
237fead6 MH |
1222 | case ECRYPTFS_TAG_11_PACKET_TYPE: |
1223 | ecryptfs_printk(KERN_WARNING, "Invalid packet set " | |
1224 | "(Tag 11 not allowed by itself)\n"); | |
1225 | rc = -EIO; | |
1226 | goto out_wipe_list; | |
1227 | break; | |
1228 | default: | |
1229 | ecryptfs_printk(KERN_DEBUG, "No packet at offset " | |
1230 | "[%d] of the file header; hex value of " | |
1231 | "character is [0x%.2x]\n", i, src[i]); | |
1232 | next_packet_is_auth_tok_packet = 0; | |
1233 | } | |
1234 | } | |
1235 | if (list_empty(&auth_tok_list)) { | |
f4aad16a MH |
1236 | printk(KERN_ERR "The lower file appears to be a non-encrypted " |
1237 | "eCryptfs file; this is not supported in this version " | |
1238 | "of the eCryptfs kernel module\n"); | |
1239 | rc = -EINVAL; | |
237fead6 MH |
1240 | goto out; |
1241 | } | |
f4aad16a MH |
1242 | /* auth_tok_list contains the set of authentication tokens |
1243 | * parsed from the metadata. We need to find a matching | |
1244 | * authentication token that has the secret component(s) | |
1245 | * necessary to decrypt the EFEK in the auth_tok parsed from | |
1246 | * the metadata. There may be several potential matches, but | |
1247 | * just one will be sufficient to decrypt to get the FEK. */ | |
1248 | find_next_matching_auth_tok: | |
1249 | found_auth_tok = 0; | |
1250 | list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { | |
237fead6 MH |
1251 | candidate_auth_tok = &auth_tok_list_item->auth_tok; |
1252 | if (unlikely(ecryptfs_verbosity > 0)) { | |
1253 | ecryptfs_printk(KERN_DEBUG, | |
1254 | "Considering cadidate auth tok:\n"); | |
1255 | ecryptfs_dump_auth_tok(candidate_auth_tok); | |
1256 | } | |
f4aad16a MH |
1257 | if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, |
1258 | candidate_auth_tok))) { | |
1259 | printk(KERN_ERR | |
1260 | "Unrecognized candidate auth tok type: [%d]\n", | |
1261 | candidate_auth_tok->token_type); | |
1262 | rc = -EINVAL; | |
1263 | goto out_wipe_list; | |
1264 | } | |
1265 | if ((rc = ecryptfs_find_auth_tok_for_sig( | |
1266 | &matching_auth_tok, crypt_stat, | |
1267 | candidate_auth_tok_sig))) | |
1268 | rc = 0; | |
1269 | if (matching_auth_tok) { | |
dddfa461 | 1270 | found_auth_tok = 1; |
f4aad16a | 1271 | goto found_matching_auth_tok; |
237fead6 MH |
1272 | } |
1273 | } | |
237fead6 | 1274 | if (!found_auth_tok) { |
f4aad16a MH |
1275 | ecryptfs_printk(KERN_ERR, "Could not find a usable " |
1276 | "authentication token\n"); | |
237fead6 MH |
1277 | rc = -EIO; |
1278 | goto out_wipe_list; | |
dddfa461 | 1279 | } |
f4aad16a | 1280 | found_matching_auth_tok: |
e2bd99ec | 1281 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
dddfa461 | 1282 | memcpy(&(candidate_auth_tok->token.private_key), |
f4aad16a | 1283 | &(matching_auth_tok->token.private_key), |
dddfa461 | 1284 | sizeof(struct ecryptfs_private_key)); |
f4aad16a | 1285 | rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, |
dddfa461 MH |
1286 | crypt_stat); |
1287 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { | |
237fead6 | 1288 | memcpy(&(candidate_auth_tok->token.password), |
f4aad16a | 1289 | &(matching_auth_tok->token.password), |
237fead6 | 1290 | sizeof(struct ecryptfs_password)); |
f4aad16a MH |
1291 | rc = decrypt_passphrase_encrypted_session_key( |
1292 | candidate_auth_tok, crypt_stat); | |
dddfa461 MH |
1293 | } |
1294 | if (rc) { | |
f4aad16a MH |
1295 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
1296 | ||
1297 | ecryptfs_printk(KERN_WARNING, "Error decrypting the " | |
1298 | "session key for authentication token with sig " | |
1299 | "[%.*s]; rc = [%d]. Removing auth tok " | |
1300 | "candidate from the list and searching for " | |
1301 | "the next match.\n", candidate_auth_tok_sig, | |
1302 | ECRYPTFS_SIG_SIZE_HEX, rc); | |
1303 | list_for_each_entry_safe(auth_tok_list_item, | |
1304 | auth_tok_list_item_tmp, | |
1305 | &auth_tok_list, list) { | |
1306 | if (candidate_auth_tok | |
1307 | == &auth_tok_list_item->auth_tok) { | |
1308 | list_del(&auth_tok_list_item->list); | |
1309 | kmem_cache_free( | |
1310 | ecryptfs_auth_tok_list_item_cache, | |
1311 | auth_tok_list_item); | |
1312 | goto find_next_matching_auth_tok; | |
1313 | } | |
1314 | } | |
1315 | BUG(); | |
dddfa461 MH |
1316 | } |
1317 | rc = ecryptfs_compute_root_iv(crypt_stat); | |
1318 | if (rc) { | |
1319 | ecryptfs_printk(KERN_ERR, "Error computing " | |
1320 | "the root IV\n"); | |
1321 | goto out_wipe_list; | |
237fead6 MH |
1322 | } |
1323 | rc = ecryptfs_init_crypt_ctx(crypt_stat); | |
1324 | if (rc) { | |
1325 | ecryptfs_printk(KERN_ERR, "Error initializing crypto " | |
1326 | "context for cipher [%s]; rc = [%d]\n", | |
1327 | crypt_stat->cipher, rc); | |
1328 | } | |
1329 | out_wipe_list: | |
1330 | wipe_auth_tok_list(&auth_tok_list); | |
1331 | out: | |
1332 | return rc; | |
1333 | } | |
f4aad16a | 1334 | |
dddfa461 MH |
1335 | static int |
1336 | pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1337 | struct ecryptfs_crypt_stat *crypt_stat, | |
1338 | struct ecryptfs_key_record *key_rec) | |
1339 | { | |
1340 | struct ecryptfs_msg_ctx *msg_ctx = NULL; | |
1341 | char *netlink_payload; | |
1342 | size_t netlink_payload_length; | |
1343 | struct ecryptfs_message *msg; | |
1344 | int rc; | |
1345 | ||
1346 | rc = write_tag_66_packet(auth_tok->token.private_key.signature, | |
1347 | ecryptfs_code_for_cipher_string(crypt_stat), | |
1348 | crypt_stat, &netlink_payload, | |
1349 | &netlink_payload_length); | |
1350 | if (rc) { | |
1351 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); | |
1352 | goto out; | |
1353 | } | |
1354 | rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload, | |
1355 | netlink_payload_length, &msg_ctx); | |
1356 | if (rc) { | |
1357 | ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); | |
1358 | goto out; | |
1359 | } | |
1360 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); | |
1361 | if (rc) { | |
1362 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " | |
1363 | "from the user space daemon\n"); | |
1364 | rc = -EIO; | |
1365 | goto out; | |
1366 | } | |
1367 | rc = parse_tag_67_packet(key_rec, msg); | |
1368 | if (rc) | |
1369 | ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); | |
1370 | kfree(msg); | |
1371 | out: | |
1372 | if (netlink_payload) | |
1373 | kfree(netlink_payload); | |
1374 | return rc; | |
1375 | } | |
1376 | /** | |
1377 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet | |
1378 | * @dest: Buffer into which to write the packet | |
1379 | * @max: Maximum number of bytes that can be writtn | |
1380 | * @packet_size: This function will write the number of bytes that end | |
1381 | * up constituting the packet; set to zero on error | |
1382 | * | |
1383 | * Returns zero on success; non-zero on error. | |
1384 | */ | |
1385 | static int | |
f4aad16a MH |
1386 | write_tag_1_packet(char *dest, size_t *remaining_bytes, |
1387 | struct ecryptfs_auth_tok *auth_tok, | |
dddfa461 | 1388 | struct ecryptfs_crypt_stat *crypt_stat, |
dddfa461 MH |
1389 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
1390 | { | |
1391 | size_t i; | |
1392 | size_t encrypted_session_key_valid = 0; | |
dddfa461 | 1393 | size_t packet_size_length; |
f4aad16a | 1394 | size_t max_packet_size; |
dddfa461 MH |
1395 | int rc = 0; |
1396 | ||
1397 | (*packet_size) = 0; | |
1398 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, | |
1399 | ECRYPTFS_SIG_SIZE); | |
1400 | encrypted_session_key_valid = 0; | |
1401 | for (i = 0; i < crypt_stat->key_size; i++) | |
1402 | encrypted_session_key_valid |= | |
1403 | auth_tok->session_key.encrypted_key[i]; | |
1404 | if (encrypted_session_key_valid) { | |
1405 | memcpy(key_rec->enc_key, | |
1406 | auth_tok->session_key.encrypted_key, | |
1407 | auth_tok->session_key.encrypted_key_size); | |
1408 | goto encrypted_session_key_set; | |
1409 | } | |
1410 | if (auth_tok->session_key.encrypted_key_size == 0) | |
1411 | auth_tok->session_key.encrypted_key_size = | |
1412 | auth_tok->token.private_key.key_size; | |
1413 | rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); | |
1414 | if (rc) { | |
1415 | ecryptfs_printk(KERN_ERR, "Failed to encrypt session key " | |
1416 | "via a pki"); | |
1417 | goto out; | |
1418 | } | |
1419 | if (ecryptfs_verbosity > 0) { | |
1420 | ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); | |
1421 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); | |
1422 | } | |
1423 | encrypted_session_key_set: | |
f4aad16a MH |
1424 | /* This format is inspired by OpenPGP; see RFC 2440 |
1425 | * packet tag 1 */ | |
1426 | max_packet_size = (1 /* Tag 1 identifier */ | |
1427 | + 3 /* Max Tag 1 packet size */ | |
1428 | + 1 /* Version */ | |
1429 | + ECRYPTFS_SIG_SIZE /* Key identifier */ | |
1430 | + 1 /* Cipher identifier */ | |
1431 | + key_rec->enc_key_size); /* Encrypted key size */ | |
1432 | if (max_packet_size > (*remaining_bytes)) { | |
1433 | printk(KERN_ERR "Packet length larger than maximum allowable; " | |
1434 | "need up to [%d] bytes, but there are only [%d] " | |
1435 | "available\n", max_packet_size, (*remaining_bytes)); | |
dddfa461 MH |
1436 | rc = -EINVAL; |
1437 | goto out; | |
1438 | } | |
1439 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; | |
f4aad16a | 1440 | rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), |
dddfa461 MH |
1441 | &packet_size_length); |
1442 | if (rc) { | |
1443 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " | |
1444 | "header; cannot generate packet length\n"); | |
1445 | goto out; | |
1446 | } | |
1447 | (*packet_size) += packet_size_length; | |
1448 | dest[(*packet_size)++] = 0x03; /* version 3 */ | |
1449 | memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); | |
1450 | (*packet_size) += ECRYPTFS_SIG_SIZE; | |
1451 | dest[(*packet_size)++] = RFC2440_CIPHER_RSA; | |
1452 | memcpy(&dest[(*packet_size)], key_rec->enc_key, | |
1453 | key_rec->enc_key_size); | |
1454 | (*packet_size) += key_rec->enc_key_size; | |
1455 | out: | |
1456 | if (rc) | |
1457 | (*packet_size) = 0; | |
f4aad16a MH |
1458 | else |
1459 | (*remaining_bytes) -= (*packet_size); | |
dddfa461 MH |
1460 | return rc; |
1461 | } | |
237fead6 MH |
1462 | |
1463 | /** | |
1464 | * write_tag_11_packet | |
1465 | * @dest: Target into which Tag 11 packet is to be written | |
1466 | * @max: Maximum packet length | |
1467 | * @contents: Byte array of contents to copy in | |
1468 | * @contents_length: Number of bytes in contents | |
1469 | * @packet_length: Length of the Tag 11 packet written; zero on error | |
1470 | * | |
1471 | * Returns zero on success; non-zero on error. | |
1472 | */ | |
1473 | static int | |
1474 | write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length, | |
1475 | size_t *packet_length) | |
1476 | { | |
237fead6 | 1477 | size_t packet_size_length; |
dddfa461 | 1478 | int rc = 0; |
237fead6 MH |
1479 | |
1480 | (*packet_length) = 0; | |
1481 | if ((13 + contents_length) > max) { | |
1482 | rc = -EINVAL; | |
1483 | ecryptfs_printk(KERN_ERR, "Packet length larger than " | |
1484 | "maximum allowable\n"); | |
1485 | goto out; | |
1486 | } | |
1487 | /* General packet header */ | |
1488 | /* Packet tag */ | |
1489 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; | |
1490 | /* Packet length */ | |
1491 | rc = write_packet_length(&dest[(*packet_length)], | |
1492 | (13 + contents_length), &packet_size_length); | |
1493 | if (rc) { | |
1494 | ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet " | |
1495 | "header; cannot generate packet length\n"); | |
1496 | goto out; | |
1497 | } | |
1498 | (*packet_length) += packet_size_length; | |
1499 | /* Tag 11 specific */ | |
1500 | /* One-octet field that describes how the data is formatted */ | |
1501 | dest[(*packet_length)++] = 0x62; /* binary data */ | |
1502 | /* One-octet filename length followed by filename */ | |
1503 | dest[(*packet_length)++] = 8; | |
1504 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); | |
1505 | (*packet_length) += 8; | |
1506 | /* Four-octet number indicating modification date */ | |
1507 | memset(&dest[(*packet_length)], 0x00, 4); | |
1508 | (*packet_length) += 4; | |
1509 | /* Remainder is literal data */ | |
1510 | memcpy(&dest[(*packet_length)], contents, contents_length); | |
1511 | (*packet_length) += contents_length; | |
1512 | out: | |
1513 | if (rc) | |
1514 | (*packet_length) = 0; | |
1515 | return rc; | |
1516 | } | |
1517 | ||
1518 | /** | |
1519 | * write_tag_3_packet | |
1520 | * @dest: Buffer into which to write the packet | |
1521 | * @max: Maximum number of bytes that can be written | |
1522 | * @auth_tok: Authentication token | |
1523 | * @crypt_stat: The cryptographic context | |
1524 | * @key_rec: encrypted key | |
1525 | * @packet_size: This function will write the number of bytes that end | |
1526 | * up constituting the packet; set to zero on error | |
1527 | * | |
1528 | * Returns zero on success; non-zero on error. | |
1529 | */ | |
1530 | static int | |
f4aad16a MH |
1531 | write_tag_3_packet(char *dest, size_t *remaining_bytes, |
1532 | struct ecryptfs_auth_tok *auth_tok, | |
237fead6 MH |
1533 | struct ecryptfs_crypt_stat *crypt_stat, |
1534 | struct ecryptfs_key_record *key_rec, size_t *packet_size) | |
1535 | { | |
237fead6 | 1536 | size_t i; |
237fead6 MH |
1537 | size_t encrypted_session_key_valid = 0; |
1538 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; | |
f4aad16a MH |
1539 | struct scatterlist dst_sg; |
1540 | struct scatterlist src_sg; | |
237fead6 | 1541 | struct mutex *tfm_mutex = NULL; |
237fead6 | 1542 | size_t cipher_code; |
f4aad16a MH |
1543 | size_t packet_size_length; |
1544 | size_t max_packet_size; | |
1545 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | |
1546 | crypt_stat->mount_crypt_stat; | |
8bba066f MH |
1547 | struct blkcipher_desc desc = { |
1548 | .tfm = NULL, | |
1549 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | |
1550 | }; | |
1551 | int rc = 0; | |
237fead6 MH |
1552 | |
1553 | (*packet_size) = 0; | |
dddfa461 | 1554 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, |
237fead6 | 1555 | ECRYPTFS_SIG_SIZE); |
f4aad16a MH |
1556 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
1557 | crypt_stat->cipher); | |
1558 | if (unlikely(rc)) { | |
1559 | printk(KERN_ERR "Internal error whilst attempting to get " | |
1560 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
1561 | crypt_stat->cipher, rc); | |
1562 | goto out; | |
1563 | } | |
1564 | if (mount_crypt_stat->global_default_cipher_key_size == 0) { | |
1565 | struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); | |
1566 | ||
1567 | printk(KERN_WARNING "No key size specified at mount; " | |
1568 | "defaulting to [%d]\n", alg->max_keysize); | |
1569 | mount_crypt_stat->global_default_cipher_key_size = | |
1570 | alg->max_keysize; | |
237fead6 | 1571 | } |
f4aad16a MH |
1572 | if (crypt_stat->key_size == 0) |
1573 | crypt_stat->key_size = | |
1574 | mount_crypt_stat->global_default_cipher_key_size; | |
237fead6 MH |
1575 | if (auth_tok->session_key.encrypted_key_size == 0) |
1576 | auth_tok->session_key.encrypted_key_size = | |
1577 | crypt_stat->key_size; | |
1578 | if (crypt_stat->key_size == 24 | |
1579 | && strcmp("aes", crypt_stat->cipher) == 0) { | |
1580 | memset((crypt_stat->key + 24), 0, 8); | |
1581 | auth_tok->session_key.encrypted_key_size = 32; | |
f4aad16a MH |
1582 | } else |
1583 | auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; | |
dddfa461 | 1584 | key_rec->enc_key_size = |
237fead6 | 1585 | auth_tok->session_key.encrypted_key_size; |
f4aad16a MH |
1586 | encrypted_session_key_valid = 0; |
1587 | for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) | |
1588 | encrypted_session_key_valid |= | |
1589 | auth_tok->session_key.encrypted_key[i]; | |
1590 | if (encrypted_session_key_valid) { | |
1591 | ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " | |
1592 | "using auth_tok->session_key.encrypted_key, " | |
1593 | "where key_rec->enc_key_size = [%d]\n", | |
1594 | key_rec->enc_key_size); | |
1595 | memcpy(key_rec->enc_key, | |
1596 | auth_tok->session_key.encrypted_key, | |
1597 | key_rec->enc_key_size); | |
1598 | goto encrypted_session_key_set; | |
1599 | } | |
dddfa461 MH |
1600 | if (auth_tok->token.password.flags & |
1601 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { | |
237fead6 MH |
1602 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " |
1603 | "session key encryption key of size [%d]\n", | |
1604 | auth_tok->token.password. | |
1605 | session_key_encryption_key_bytes); | |
1606 | memcpy(session_key_encryption_key, | |
1607 | auth_tok->token.password.session_key_encryption_key, | |
1608 | crypt_stat->key_size); | |
1609 | ecryptfs_printk(KERN_DEBUG, | |
1610 | "Cached session key " "encryption key: \n"); | |
1611 | if (ecryptfs_verbosity > 0) | |
1612 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
1613 | } | |
1614 | if (unlikely(ecryptfs_verbosity > 0)) { | |
1615 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); | |
1616 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
1617 | } | |
f4aad16a MH |
1618 | if ((rc = virt_to_scatterlist(crypt_stat->key, |
1619 | key_rec->enc_key_size, &src_sg, 1)) | |
1620 | != 1) { | |
237fead6 | 1621 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a MH |
1622 | "for crypt_stat session key; expected rc = 1; " |
1623 | "got rc = [%d]. key_rec->enc_key_size = [%d]\n", | |
1624 | rc, key_rec->enc_key_size); | |
237fead6 MH |
1625 | rc = -ENOMEM; |
1626 | goto out; | |
1627 | } | |
f4aad16a MH |
1628 | if ((rc = virt_to_scatterlist(key_rec->enc_key, |
1629 | key_rec->enc_key_size, &dst_sg, 1)) | |
1630 | != 1) { | |
237fead6 | 1631 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a MH |
1632 | "for crypt_stat encrypted session key; " |
1633 | "expected rc = 1; got rc = [%d]. " | |
1634 | "key_rec->enc_key_size = [%d]\n", rc, | |
1635 | key_rec->enc_key_size); | |
237fead6 MH |
1636 | rc = -ENOMEM; |
1637 | goto out; | |
1638 | } | |
f4aad16a | 1639 | mutex_lock(tfm_mutex); |
8bba066f MH |
1640 | rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, |
1641 | crypt_stat->key_size); | |
237fead6 | 1642 | if (rc < 0) { |
f4aad16a | 1643 | mutex_unlock(tfm_mutex); |
237fead6 | 1644 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " |
8bba066f | 1645 | "context; rc = [%d]\n", rc); |
237fead6 MH |
1646 | goto out; |
1647 | } | |
1648 | rc = 0; | |
1649 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", | |
1650 | crypt_stat->key_size); | |
f4aad16a | 1651 | rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, |
8bba066f | 1652 | (*key_rec).enc_key_size); |
f4aad16a | 1653 | mutex_unlock(tfm_mutex); |
8bba066f MH |
1654 | if (rc) { |
1655 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); | |
1656 | goto out; | |
1657 | } | |
237fead6 | 1658 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); |
f4aad16a MH |
1659 | if (ecryptfs_verbosity > 0) { |
1660 | ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", | |
1661 | key_rec->enc_key_size); | |
dddfa461 MH |
1662 | ecryptfs_dump_hex(key_rec->enc_key, |
1663 | key_rec->enc_key_size); | |
237fead6 | 1664 | } |
f4aad16a MH |
1665 | encrypted_session_key_set: |
1666 | /* This format is inspired by OpenPGP; see RFC 2440 | |
1667 | * packet tag 3 */ | |
1668 | max_packet_size = (1 /* Tag 3 identifier */ | |
1669 | + 3 /* Max Tag 3 packet size */ | |
1670 | + 1 /* Version */ | |
1671 | + 1 /* Cipher code */ | |
1672 | + 1 /* S2K specifier */ | |
1673 | + 1 /* Hash identifier */ | |
1674 | + ECRYPTFS_SALT_SIZE /* Salt */ | |
1675 | + 1 /* Hash iterations */ | |
1676 | + key_rec->enc_key_size); /* Encrypted key size */ | |
1677 | if (max_packet_size > (*remaining_bytes)) { | |
1678 | printk(KERN_ERR "Packet too large; need up to [%d] bytes, but " | |
1679 | "there are only [%d] available\n", max_packet_size, | |
1680 | (*remaining_bytes)); | |
237fead6 MH |
1681 | rc = -EINVAL; |
1682 | goto out; | |
1683 | } | |
237fead6 | 1684 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; |
f4aad16a MH |
1685 | /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) |
1686 | * to get the number of octets in the actual Tag 3 packet */ | |
1687 | rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4), | |
237fead6 MH |
1688 | &packet_size_length); |
1689 | if (rc) { | |
f4aad16a MH |
1690 | printk(KERN_ERR "Error generating tag 3 packet header; cannot " |
1691 | "generate packet length. rc = [%d]\n", rc); | |
237fead6 MH |
1692 | goto out; |
1693 | } | |
1694 | (*packet_size) += packet_size_length; | |
1695 | dest[(*packet_size)++] = 0x04; /* version 4 */ | |
f4aad16a MH |
1696 | /* TODO: Break from RFC2440 so that arbitrary ciphers can be |
1697 | * specified with strings */ | |
237fead6 MH |
1698 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); |
1699 | if (cipher_code == 0) { | |
1700 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " | |
1701 | "cipher [%s]\n", crypt_stat->cipher); | |
1702 | rc = -EINVAL; | |
1703 | goto out; | |
1704 | } | |
1705 | dest[(*packet_size)++] = cipher_code; | |
1706 | dest[(*packet_size)++] = 0x03; /* S2K */ | |
1707 | dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ | |
1708 | memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, | |
1709 | ECRYPTFS_SALT_SIZE); | |
1710 | (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ | |
1711 | dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ | |
dddfa461 MH |
1712 | memcpy(&dest[(*packet_size)], key_rec->enc_key, |
1713 | key_rec->enc_key_size); | |
1714 | (*packet_size) += key_rec->enc_key_size; | |
237fead6 | 1715 | out: |
237fead6 MH |
1716 | if (rc) |
1717 | (*packet_size) = 0; | |
f4aad16a MH |
1718 | else |
1719 | (*remaining_bytes) -= (*packet_size); | |
237fead6 MH |
1720 | return rc; |
1721 | } | |
1722 | ||
eb95e7ff MH |
1723 | struct kmem_cache *ecryptfs_key_record_cache; |
1724 | ||
237fead6 MH |
1725 | /** |
1726 | * ecryptfs_generate_key_packet_set | |
1727 | * @dest: Virtual address from which to write the key record set | |
1728 | * @crypt_stat: The cryptographic context from which the | |
1729 | * authentication tokens will be retrieved | |
1730 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat | |
1731 | * for the global parameters | |
1732 | * @len: The amount written | |
1733 | * @max: The maximum amount of data allowed to be written | |
1734 | * | |
1735 | * Generates a key packet set and writes it to the virtual address | |
1736 | * passed in. | |
1737 | * | |
1738 | * Returns zero on success; non-zero on error. | |
1739 | */ | |
1740 | int | |
1741 | ecryptfs_generate_key_packet_set(char *dest_base, | |
1742 | struct ecryptfs_crypt_stat *crypt_stat, | |
1743 | struct dentry *ecryptfs_dentry, size_t *len, | |
1744 | size_t max) | |
1745 | { | |
237fead6 | 1746 | struct ecryptfs_auth_tok *auth_tok; |
f4aad16a | 1747 | struct ecryptfs_global_auth_tok *global_auth_tok; |
237fead6 MH |
1748 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
1749 | &ecryptfs_superblock_to_private( | |
1750 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | |
1751 | size_t written; | |
eb95e7ff | 1752 | struct ecryptfs_key_record *key_rec; |
f4aad16a | 1753 | struct ecryptfs_key_sig *key_sig; |
dddfa461 | 1754 | int rc = 0; |
237fead6 MH |
1755 | |
1756 | (*len) = 0; | |
f4aad16a | 1757 | mutex_lock(&crypt_stat->keysig_list_mutex); |
eb95e7ff MH |
1758 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); |
1759 | if (!key_rec) { | |
1760 | rc = -ENOMEM; | |
1761 | goto out; | |
1762 | } | |
f4aad16a MH |
1763 | list_for_each_entry(key_sig, &crypt_stat->keysig_list, |
1764 | crypt_stat_list) { | |
1765 | memset(key_rec, 0, sizeof(*key_rec)); | |
1766 | rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | |
1767 | mount_crypt_stat, | |
1768 | key_sig->keysig); | |
1769 | if (rc) { | |
1770 | printk(KERN_ERR "Error attempting to get the global " | |
1771 | "auth_tok; rc = [%d]\n", rc); | |
1772 | goto out_free; | |
1773 | } | |
1774 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { | |
1775 | printk(KERN_WARNING | |
1776 | "Skipping invalid auth tok with sig = [%s]\n", | |
1777 | global_auth_tok->sig); | |
1778 | continue; | |
1779 | } | |
1780 | auth_tok = global_auth_tok->global_auth_tok; | |
237fead6 MH |
1781 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { |
1782 | rc = write_tag_3_packet((dest_base + (*len)), | |
f4aad16a | 1783 | &max, auth_tok, |
eb95e7ff | 1784 | crypt_stat, key_rec, |
237fead6 MH |
1785 | &written); |
1786 | if (rc) { | |
1787 | ecryptfs_printk(KERN_WARNING, "Error " | |
1788 | "writing tag 3 packet\n"); | |
eb95e7ff | 1789 | goto out_free; |
237fead6 MH |
1790 | } |
1791 | (*len) += written; | |
1792 | /* Write auth tok signature packet */ | |
f4aad16a MH |
1793 | rc = write_tag_11_packet((dest_base + (*len)), &max, |
1794 | key_rec->sig, | |
1795 | ECRYPTFS_SIG_SIZE, &written); | |
237fead6 MH |
1796 | if (rc) { |
1797 | ecryptfs_printk(KERN_ERR, "Error writing " | |
1798 | "auth tok signature packet\n"); | |
eb95e7ff | 1799 | goto out_free; |
237fead6 MH |
1800 | } |
1801 | (*len) += written; | |
dddfa461 MH |
1802 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
1803 | rc = write_tag_1_packet(dest_base + (*len), | |
f4aad16a MH |
1804 | &max, auth_tok, |
1805 | crypt_stat, key_rec, &written); | |
dddfa461 MH |
1806 | if (rc) { |
1807 | ecryptfs_printk(KERN_WARNING, "Error " | |
1808 | "writing tag 1 packet\n"); | |
eb95e7ff | 1809 | goto out_free; |
dddfa461 MH |
1810 | } |
1811 | (*len) += written; | |
237fead6 MH |
1812 | } else { |
1813 | ecryptfs_printk(KERN_WARNING, "Unsupported " | |
1814 | "authentication token type\n"); | |
1815 | rc = -EINVAL; | |
eb95e7ff | 1816 | goto out_free; |
237fead6 | 1817 | } |
f4aad16a MH |
1818 | } |
1819 | if (likely(max > 0)) { | |
237fead6 MH |
1820 | dest_base[(*len)] = 0x00; |
1821 | } else { | |
1822 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); | |
1823 | rc = -EIO; | |
1824 | } | |
eb95e7ff MH |
1825 | out_free: |
1826 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); | |
237fead6 MH |
1827 | out: |
1828 | if (rc) | |
1829 | (*len) = 0; | |
f4aad16a MH |
1830 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
1831 | return rc; | |
1832 | } | |
1833 | ||
1834 | struct kmem_cache *ecryptfs_key_sig_cache; | |
1835 | ||
1836 | int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) | |
1837 | { | |
1838 | struct ecryptfs_key_sig *new_key_sig; | |
1839 | int rc = 0; | |
1840 | ||
1841 | new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); | |
1842 | if (!new_key_sig) { | |
1843 | rc = -ENOMEM; | |
1844 | printk(KERN_ERR | |
1845 | "Error allocating from ecryptfs_key_sig_cache\n"); | |
1846 | goto out; | |
1847 | } | |
1848 | memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
1849 | mutex_lock(&crypt_stat->keysig_list_mutex); | |
1850 | list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); | |
1851 | mutex_unlock(&crypt_stat->keysig_list_mutex); | |
1852 | out: | |
237fead6 MH |
1853 | return rc; |
1854 | } | |
f4aad16a MH |
1855 | |
1856 | struct kmem_cache *ecryptfs_global_auth_tok_cache; | |
1857 | ||
1858 | int | |
1859 | ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
1860 | char *sig) | |
1861 | { | |
1862 | struct ecryptfs_global_auth_tok *new_auth_tok; | |
1863 | int rc = 0; | |
1864 | ||
1865 | new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache, | |
1866 | GFP_KERNEL); | |
1867 | if (!new_auth_tok) { | |
1868 | rc = -ENOMEM; | |
1869 | printk(KERN_ERR "Error allocating from " | |
1870 | "ecryptfs_global_auth_tok_cache\n"); | |
1871 | goto out; | |
1872 | } | |
1873 | memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
1874 | new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; | |
1875 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
1876 | list_add(&new_auth_tok->mount_crypt_stat_list, | |
1877 | &mount_crypt_stat->global_auth_tok_list); | |
1878 | mount_crypt_stat->num_global_auth_toks++; | |
1879 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
1880 | out: | |
1881 | return rc; | |
1882 | } | |
1883 |