]>
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 | ||
3095e8e3 HX |
28 | #include <crypto/hash.h> |
29 | #include <crypto/skcipher.h> | |
237fead6 | 30 | #include <linux/string.h> |
237fead6 MH |
31 | #include <linux/pagemap.h> |
32 | #include <linux/key.h> | |
33 | #include <linux/random.h> | |
237fead6 | 34 | #include <linux/scatterlist.h> |
5a0e3ad6 | 35 | #include <linux/slab.h> |
237fead6 MH |
36 | #include "ecryptfs_kernel.h" |
37 | ||
38 | /** | |
39 | * request_key returned an error instead of a valid key address; | |
40 | * determine the type of error, make appropriate log entries, and | |
41 | * return an error code. | |
42 | */ | |
cd9d67df | 43 | static int process_request_key_err(long err_code) |
237fead6 MH |
44 | { |
45 | int rc = 0; | |
46 | ||
47 | switch (err_code) { | |
982363c9 | 48 | case -ENOKEY: |
237fead6 MH |
49 | ecryptfs_printk(KERN_WARNING, "No key\n"); |
50 | rc = -ENOENT; | |
51 | break; | |
982363c9 | 52 | case -EKEYEXPIRED: |
237fead6 MH |
53 | ecryptfs_printk(KERN_WARNING, "Key expired\n"); |
54 | rc = -ETIME; | |
55 | break; | |
982363c9 | 56 | case -EKEYREVOKED: |
237fead6 MH |
57 | ecryptfs_printk(KERN_WARNING, "Key revoked\n"); |
58 | rc = -EINVAL; | |
59 | break; | |
60 | default: | |
61 | ecryptfs_printk(KERN_WARNING, "Unknown error code: " | |
888d57bb | 62 | "[0x%.16lx]\n", err_code); |
237fead6 MH |
63 | rc = -EINVAL; |
64 | } | |
65 | return rc; | |
66 | } | |
67 | ||
0e1fc5ef RS |
68 | static int process_find_global_auth_tok_for_sig_err(int err_code) |
69 | { | |
70 | int rc = err_code; | |
71 | ||
72 | switch (err_code) { | |
73 | case -ENOENT: | |
74 | ecryptfs_printk(KERN_WARNING, "Missing auth tok\n"); | |
75 | break; | |
76 | case -EINVAL: | |
77 | ecryptfs_printk(KERN_WARNING, "Invalid auth tok\n"); | |
78 | break; | |
79 | default: | |
80 | rc = process_request_key_err(err_code); | |
81 | break; | |
82 | } | |
83 | return rc; | |
84 | } | |
85 | ||
237fead6 | 86 | /** |
f66e883e | 87 | * ecryptfs_parse_packet_length |
237fead6 MH |
88 | * @data: Pointer to memory containing length at offset |
89 | * @size: This function writes the decoded size to this memory | |
90 | * address; zero on error | |
91 | * @length_size: The number of bytes occupied by the encoded length | |
92 | * | |
22e78faf | 93 | * Returns zero on success; non-zero on error |
237fead6 | 94 | */ |
f66e883e MH |
95 | int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, |
96 | size_t *length_size) | |
237fead6 MH |
97 | { |
98 | int rc = 0; | |
99 | ||
100 | (*length_size) = 0; | |
101 | (*size) = 0; | |
102 | if (data[0] < 192) { | |
103 | /* One-byte length */ | |
831115af | 104 | (*size) = data[0]; |
237fead6 MH |
105 | (*length_size) = 1; |
106 | } else if (data[0] < 224) { | |
107 | /* Two-byte length */ | |
831115af TH |
108 | (*size) = (data[0] - 192) * 256; |
109 | (*size) += data[1] + 192; | |
237fead6 MH |
110 | (*length_size) = 2; |
111 | } else if (data[0] == 255) { | |
48399c0b | 112 | /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ |
237fead6 MH |
113 | ecryptfs_printk(KERN_ERR, "Five-byte packet length not " |
114 | "supported\n"); | |
115 | rc = -EINVAL; | |
116 | goto out; | |
117 | } else { | |
118 | ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); | |
119 | rc = -EINVAL; | |
120 | goto out; | |
121 | } | |
122 | out: | |
123 | return rc; | |
124 | } | |
125 | ||
126 | /** | |
f66e883e | 127 | * ecryptfs_write_packet_length |
22e78faf | 128 | * @dest: The byte array target into which to write the length. Must |
48399c0b | 129 | * have at least ECRYPTFS_MAX_PKT_LEN_SIZE bytes allocated. |
237fead6 | 130 | * @size: The length to write. |
22e78faf MH |
131 | * @packet_size_length: The number of bytes used to encode the packet |
132 | * length is written to this address. | |
237fead6 MH |
133 | * |
134 | * Returns zero on success; non-zero on error. | |
135 | */ | |
f66e883e MH |
136 | int ecryptfs_write_packet_length(char *dest, size_t size, |
137 | size_t *packet_size_length) | |
237fead6 MH |
138 | { |
139 | int rc = 0; | |
140 | ||
141 | if (size < 192) { | |
142 | dest[0] = size; | |
143 | (*packet_size_length) = 1; | |
144 | } else if (size < 65536) { | |
145 | dest[0] = (((size - 192) / 256) + 192); | |
146 | dest[1] = ((size - 192) % 256); | |
147 | (*packet_size_length) = 2; | |
148 | } else { | |
48399c0b | 149 | /* If support is added, adjust ECRYPTFS_MAX_PKT_LEN_SIZE */ |
237fead6 MH |
150 | rc = -EINVAL; |
151 | ecryptfs_printk(KERN_WARNING, | |
f24b3887 | 152 | "Unsupported packet size: [%zd]\n", size); |
237fead6 MH |
153 | } |
154 | return rc; | |
155 | } | |
156 | ||
dddfa461 MH |
157 | static int |
158 | write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, | |
159 | char **packet, size_t *packet_len) | |
160 | { | |
161 | size_t i = 0; | |
162 | size_t data_len; | |
163 | size_t packet_size_len; | |
164 | char *message; | |
165 | int rc; | |
166 | ||
167 | /* | |
168 | * ***** TAG 64 Packet Format ***** | |
169 | * | Content Type | 1 byte | | |
170 | * | Key Identifier Size | 1 or 2 bytes | | |
171 | * | Key Identifier | arbitrary | | |
172 | * | Encrypted File Encryption Key Size | 1 or 2 bytes | | |
173 | * | Encrypted File Encryption Key | arbitrary | | |
174 | */ | |
175 | data_len = (5 + ECRYPTFS_SIG_SIZE_HEX | |
176 | + session_key->encrypted_key_size); | |
177 | *packet = kmalloc(data_len, GFP_KERNEL); | |
178 | message = *packet; | |
179 | if (!message) { | |
180 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | |
181 | rc = -ENOMEM; | |
182 | goto out; | |
183 | } | |
184 | message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; | |
f66e883e MH |
185 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
186 | &packet_size_len); | |
dddfa461 MH |
187 | if (rc) { |
188 | ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " | |
189 | "header; cannot generate packet length\n"); | |
190 | goto out; | |
191 | } | |
192 | i += packet_size_len; | |
193 | memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); | |
194 | i += ECRYPTFS_SIG_SIZE_HEX; | |
f66e883e MH |
195 | rc = ecryptfs_write_packet_length(&message[i], |
196 | session_key->encrypted_key_size, | |
197 | &packet_size_len); | |
dddfa461 MH |
198 | if (rc) { |
199 | ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " | |
200 | "header; cannot generate packet length\n"); | |
201 | goto out; | |
202 | } | |
203 | i += packet_size_len; | |
204 | memcpy(&message[i], session_key->encrypted_key, | |
205 | session_key->encrypted_key_size); | |
206 | i += session_key->encrypted_key_size; | |
207 | *packet_len = i; | |
208 | out: | |
209 | return rc; | |
210 | } | |
211 | ||
212 | static int | |
19e66a67 | 213 | parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, |
dddfa461 MH |
214 | struct ecryptfs_message *msg) |
215 | { | |
216 | size_t i = 0; | |
217 | char *data; | |
218 | size_t data_len; | |
219 | size_t m_size; | |
220 | size_t message_len; | |
221 | u16 checksum = 0; | |
222 | u16 expected_checksum = 0; | |
223 | int rc; | |
224 | ||
225 | /* | |
226 | * ***** TAG 65 Packet Format ***** | |
227 | * | Content Type | 1 byte | | |
228 | * | Status Indicator | 1 byte | | |
229 | * | File Encryption Key Size | 1 or 2 bytes | | |
230 | * | File Encryption Key | arbitrary | | |
231 | */ | |
232 | message_len = msg->data_len; | |
233 | data = msg->data; | |
234 | if (message_len < 4) { | |
235 | rc = -EIO; | |
236 | goto out; | |
237 | } | |
238 | if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { | |
239 | ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); | |
240 | rc = -EIO; | |
241 | goto out; | |
242 | } | |
243 | if (data[i++]) { | |
244 | ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " | |
245 | "[%d]\n", data[i-1]); | |
246 | rc = -EIO; | |
247 | goto out; | |
248 | } | |
f66e883e | 249 | rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); |
dddfa461 MH |
250 | if (rc) { |
251 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | |
252 | "rc = [%d]\n", rc); | |
253 | goto out; | |
254 | } | |
255 | i += data_len; | |
256 | if (message_len < (i + m_size)) { | |
624ae528 TH |
257 | ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " |
258 | "is shorter than expected\n"); | |
dddfa461 MH |
259 | rc = -EIO; |
260 | goto out; | |
261 | } | |
262 | if (m_size < 3) { | |
263 | ecryptfs_printk(KERN_ERR, | |
264 | "The decrypted key is not long enough to " | |
265 | "include a cipher code and checksum\n"); | |
266 | rc = -EIO; | |
267 | goto out; | |
268 | } | |
269 | *cipher_code = data[i++]; | |
270 | /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ | |
271 | session_key->decrypted_key_size = m_size - 3; | |
272 | if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { | |
273 | ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " | |
274 | "the maximum key size [%d]\n", | |
275 | session_key->decrypted_key_size, | |
276 | ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); | |
277 | rc = -EIO; | |
278 | goto out; | |
279 | } | |
280 | memcpy(session_key->decrypted_key, &data[i], | |
281 | session_key->decrypted_key_size); | |
282 | i += session_key->decrypted_key_size; | |
283 | expected_checksum += (unsigned char)(data[i++]) << 8; | |
284 | expected_checksum += (unsigned char)(data[i++]); | |
285 | for (i = 0; i < session_key->decrypted_key_size; i++) | |
286 | checksum += session_key->decrypted_key[i]; | |
287 | if (expected_checksum != checksum) { | |
288 | ecryptfs_printk(KERN_ERR, "Invalid checksum for file " | |
289 | "encryption key; expected [%x]; calculated " | |
290 | "[%x]\n", expected_checksum, checksum); | |
291 | rc = -EIO; | |
292 | } | |
293 | out: | |
294 | return rc; | |
295 | } | |
296 | ||
297 | ||
298 | static int | |
19e66a67 | 299 | write_tag_66_packet(char *signature, u8 cipher_code, |
dddfa461 MH |
300 | struct ecryptfs_crypt_stat *crypt_stat, char **packet, |
301 | size_t *packet_len) | |
302 | { | |
303 | size_t i = 0; | |
304 | size_t j; | |
305 | size_t data_len; | |
306 | size_t checksum = 0; | |
307 | size_t packet_size_len; | |
308 | char *message; | |
309 | int rc; | |
310 | ||
311 | /* | |
312 | * ***** TAG 66 Packet Format ***** | |
313 | * | Content Type | 1 byte | | |
314 | * | Key Identifier Size | 1 or 2 bytes | | |
315 | * | Key Identifier | arbitrary | | |
316 | * | File Encryption Key Size | 1 or 2 bytes | | |
317 | * | File Encryption Key | arbitrary | | |
318 | */ | |
319 | data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); | |
320 | *packet = kmalloc(data_len, GFP_KERNEL); | |
321 | message = *packet; | |
322 | if (!message) { | |
323 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | |
324 | rc = -ENOMEM; | |
325 | goto out; | |
326 | } | |
327 | message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; | |
f66e883e MH |
328 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
329 | &packet_size_len); | |
dddfa461 MH |
330 | if (rc) { |
331 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " | |
332 | "header; cannot generate packet length\n"); | |
333 | goto out; | |
334 | } | |
335 | i += packet_size_len; | |
336 | memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); | |
337 | i += ECRYPTFS_SIG_SIZE_HEX; | |
338 | /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ | |
f66e883e MH |
339 | rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, |
340 | &packet_size_len); | |
dddfa461 MH |
341 | if (rc) { |
342 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " | |
343 | "header; cannot generate packet length\n"); | |
344 | goto out; | |
345 | } | |
346 | i += packet_size_len; | |
347 | message[i++] = cipher_code; | |
348 | memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); | |
349 | i += crypt_stat->key_size; | |
350 | for (j = 0; j < crypt_stat->key_size; j++) | |
351 | checksum += crypt_stat->key[j]; | |
352 | message[i++] = (checksum / 256) % 256; | |
353 | message[i++] = (checksum % 256); | |
354 | *packet_len = i; | |
355 | out: | |
356 | return rc; | |
357 | } | |
358 | ||
359 | static int | |
360 | parse_tag_67_packet(struct ecryptfs_key_record *key_rec, | |
361 | struct ecryptfs_message *msg) | |
362 | { | |
363 | size_t i = 0; | |
364 | char *data; | |
365 | size_t data_len; | |
366 | size_t message_len; | |
367 | int rc; | |
368 | ||
369 | /* | |
370 | * ***** TAG 65 Packet Format ***** | |
371 | * | Content Type | 1 byte | | |
372 | * | Status Indicator | 1 byte | | |
373 | * | Encrypted File Encryption Key Size | 1 or 2 bytes | | |
374 | * | Encrypted File Encryption Key | arbitrary | | |
375 | */ | |
376 | message_len = msg->data_len; | |
377 | data = msg->data; | |
378 | /* verify that everything through the encrypted FEK size is present */ | |
379 | if (message_len < 4) { | |
380 | rc = -EIO; | |
df261c52 | 381 | printk(KERN_ERR "%s: message_len is [%zd]; minimum acceptable " |
f66e883e | 382 | "message length is [%d]\n", __func__, message_len, 4); |
dddfa461 MH |
383 | goto out; |
384 | } | |
385 | if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { | |
dddfa461 | 386 | rc = -EIO; |
f66e883e MH |
387 | printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", |
388 | __func__); | |
dddfa461 MH |
389 | goto out; |
390 | } | |
391 | if (data[i++]) { | |
dddfa461 | 392 | rc = -EIO; |
f66e883e MH |
393 | printk(KERN_ERR "%s: Status indicator has non zero " |
394 | "value [%d]\n", __func__, data[i-1]); | |
395 | ||
dddfa461 MH |
396 | goto out; |
397 | } | |
f66e883e MH |
398 | rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, |
399 | &data_len); | |
dddfa461 MH |
400 | if (rc) { |
401 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | |
402 | "rc = [%d]\n", rc); | |
403 | goto out; | |
404 | } | |
405 | i += data_len; | |
406 | if (message_len < (i + key_rec->enc_key_size)) { | |
dddfa461 | 407 | rc = -EIO; |
df261c52 | 408 | printk(KERN_ERR "%s: message_len [%zd]; max len is [%zd]\n", |
f66e883e | 409 | __func__, message_len, (i + key_rec->enc_key_size)); |
dddfa461 MH |
410 | goto out; |
411 | } | |
412 | if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
dddfa461 | 413 | rc = -EIO; |
df261c52 | 414 | printk(KERN_ERR "%s: Encrypted key_size [%zd] larger than " |
f66e883e MH |
415 | "the maximum key size [%d]\n", __func__, |
416 | key_rec->enc_key_size, | |
417 | ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); | |
dddfa461 MH |
418 | goto out; |
419 | } | |
420 | memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); | |
421 | out: | |
422 | return rc; | |
423 | } | |
424 | ||
0e1fc5ef RS |
425 | /** |
426 | * ecryptfs_verify_version | |
427 | * @version: The version number to confirm | |
428 | * | |
429 | * Returns zero on good version; non-zero otherwise | |
430 | */ | |
431 | static int ecryptfs_verify_version(u16 version) | |
432 | { | |
433 | int rc = 0; | |
434 | unsigned char major; | |
435 | unsigned char minor; | |
436 | ||
437 | major = ((version >> 8) & 0xFF); | |
438 | minor = (version & 0xFF); | |
439 | if (major != ECRYPTFS_VERSION_MAJOR) { | |
440 | ecryptfs_printk(KERN_ERR, "Major version number mismatch. " | |
441 | "Expected [%d]; got [%d]\n", | |
442 | ECRYPTFS_VERSION_MAJOR, major); | |
443 | rc = -EINVAL; | |
444 | goto out; | |
445 | } | |
446 | if (minor != ECRYPTFS_VERSION_MINOR) { | |
447 | ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " | |
448 | "Expected [%d]; got [%d]\n", | |
449 | ECRYPTFS_VERSION_MINOR, minor); | |
450 | rc = -EINVAL; | |
451 | goto out; | |
452 | } | |
453 | out: | |
454 | return rc; | |
455 | } | |
456 | ||
457 | /** | |
458 | * ecryptfs_verify_auth_tok_from_key | |
459 | * @auth_tok_key: key containing the authentication token | |
460 | * @auth_tok: authentication token | |
461 | * | |
462 | * Returns zero on valid auth tok; -EINVAL otherwise | |
463 | */ | |
464 | static int | |
465 | ecryptfs_verify_auth_tok_from_key(struct key *auth_tok_key, | |
466 | struct ecryptfs_auth_tok **auth_tok) | |
467 | { | |
468 | int rc = 0; | |
469 | ||
470 | (*auth_tok) = ecryptfs_get_key_payload_data(auth_tok_key); | |
471 | if (ecryptfs_verify_version((*auth_tok)->version)) { | |
472 | printk(KERN_ERR "Data structure version mismatch. Userspace " | |
473 | "tools must match eCryptfs kernel module with major " | |
474 | "version [%d] and minor version [%d]\n", | |
475 | ECRYPTFS_VERSION_MAJOR, ECRYPTFS_VERSION_MINOR); | |
476 | rc = -EINVAL; | |
477 | goto out; | |
478 | } | |
479 | if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD | |
480 | && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { | |
481 | printk(KERN_ERR "Invalid auth_tok structure " | |
482 | "returned from key query\n"); | |
483 | rc = -EINVAL; | |
484 | goto out; | |
485 | } | |
486 | out: | |
487 | return rc; | |
488 | } | |
489 | ||
9c79f34f MH |
490 | static int |
491 | ecryptfs_find_global_auth_tok_for_sig( | |
0e1fc5ef RS |
492 | struct key **auth_tok_key, |
493 | struct ecryptfs_auth_tok **auth_tok, | |
9c79f34f MH |
494 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) |
495 | { | |
496 | struct ecryptfs_global_auth_tok *walker; | |
497 | int rc = 0; | |
498 | ||
0e1fc5ef RS |
499 | (*auth_tok_key) = NULL; |
500 | (*auth_tok) = NULL; | |
9c79f34f MH |
501 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
502 | list_for_each_entry(walker, | |
503 | &mount_crypt_stat->global_auth_tok_list, | |
504 | mount_crypt_stat_list) { | |
0e1fc5ef RS |
505 | if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX)) |
506 | continue; | |
507 | ||
508 | if (walker->flags & ECRYPTFS_AUTH_TOK_INVALID) { | |
509 | rc = -EINVAL; | |
9c79f34f MH |
510 | goto out; |
511 | } | |
0e1fc5ef RS |
512 | |
513 | rc = key_validate(walker->global_auth_tok_key); | |
514 | if (rc) { | |
515 | if (rc == -EKEYEXPIRED) | |
516 | goto out; | |
517 | goto out_invalid_auth_tok; | |
518 | } | |
519 | ||
b5695d04 | 520 | down_write(&(walker->global_auth_tok_key->sem)); |
0e1fc5ef RS |
521 | rc = ecryptfs_verify_auth_tok_from_key( |
522 | walker->global_auth_tok_key, auth_tok); | |
523 | if (rc) | |
b5695d04 | 524 | goto out_invalid_auth_tok_unlock; |
0e1fc5ef RS |
525 | |
526 | (*auth_tok_key) = walker->global_auth_tok_key; | |
527 | key_get(*auth_tok_key); | |
528 | goto out; | |
9c79f34f | 529 | } |
0e1fc5ef RS |
530 | rc = -ENOENT; |
531 | goto out; | |
b5695d04 RS |
532 | out_invalid_auth_tok_unlock: |
533 | up_write(&(walker->global_auth_tok_key->sem)); | |
0e1fc5ef RS |
534 | out_invalid_auth_tok: |
535 | printk(KERN_WARNING "Invalidating auth tok with sig = [%s]\n", sig); | |
536 | walker->flags |= ECRYPTFS_AUTH_TOK_INVALID; | |
537 | key_put(walker->global_auth_tok_key); | |
538 | walker->global_auth_tok_key = NULL; | |
9c79f34f MH |
539 | out: |
540 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
541 | return rc; | |
542 | } | |
543 | ||
544 | /** | |
545 | * ecryptfs_find_auth_tok_for_sig | |
546 | * @auth_tok: Set to the matching auth_tok; NULL if not found | |
547 | * @crypt_stat: inode crypt_stat crypto context | |
548 | * @sig: Sig of auth_tok to find | |
549 | * | |
550 | * For now, this function simply looks at the registered auth_tok's | |
551 | * linked off the mount_crypt_stat, so all the auth_toks that can be | |
552 | * used must be registered at mount time. This function could | |
553 | * potentially try a lot harder to find auth_tok's (e.g., by calling | |
554 | * out to ecryptfsd to dynamically retrieve an auth_tok object) so | |
555 | * that static registration of auth_tok's will no longer be necessary. | |
556 | * | |
557 | * Returns zero on no error; non-zero on error | |
558 | */ | |
559 | static int | |
560 | ecryptfs_find_auth_tok_for_sig( | |
aee683b9 | 561 | struct key **auth_tok_key, |
9c79f34f MH |
562 | struct ecryptfs_auth_tok **auth_tok, |
563 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
564 | char *sig) | |
565 | { | |
9c79f34f MH |
566 | int rc = 0; |
567 | ||
0e1fc5ef RS |
568 | rc = ecryptfs_find_global_auth_tok_for_sig(auth_tok_key, auth_tok, |
569 | mount_crypt_stat, sig); | |
570 | if (rc == -ENOENT) { | |
f16feb51 RS |
571 | /* if the flag ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY is set in the |
572 | * mount_crypt_stat structure, we prevent to use auth toks that | |
573 | * are not inserted through the ecryptfs_add_global_auth_tok | |
574 | * function. | |
575 | */ | |
576 | if (mount_crypt_stat->flags | |
577 | & ECRYPTFS_GLOBAL_MOUNT_AUTH_TOK_ONLY) | |
578 | return -EINVAL; | |
579 | ||
aee683b9 | 580 | rc = ecryptfs_keyring_auth_tok_for_sig(auth_tok_key, auth_tok, |
9c79f34f | 581 | sig); |
0e1fc5ef | 582 | } |
9c79f34f MH |
583 | return rc; |
584 | } | |
585 | ||
586 | /** | |
587 | * write_tag_70_packet can gobble a lot of stack space. We stuff most | |
588 | * of the function's parameters in a kmalloc'd struct to help reduce | |
589 | * eCryptfs' overall stack usage. | |
590 | */ | |
591 | struct ecryptfs_write_tag_70_packet_silly_stack { | |
592 | u8 cipher_code; | |
593 | size_t max_packet_size; | |
594 | size_t packet_size_len; | |
595 | size_t block_aligned_filename_size; | |
596 | size_t block_size; | |
597 | size_t i; | |
598 | size_t j; | |
599 | size_t num_rand_bytes; | |
600 | struct mutex *tfm_mutex; | |
601 | char *block_aligned_filename; | |
602 | struct ecryptfs_auth_tok *auth_tok; | |
8d08dab7 TH |
603 | struct scatterlist src_sg[2]; |
604 | struct scatterlist dst_sg[2]; | |
3095e8e3 HX |
605 | struct crypto_skcipher *skcipher_tfm; |
606 | struct skcipher_request *skcipher_req; | |
9c79f34f MH |
607 | char iv[ECRYPTFS_MAX_IV_BYTES]; |
608 | char hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; | |
609 | char tmp_hash[ECRYPTFS_TAG_70_DIGEST_SIZE]; | |
3095e8e3 HX |
610 | struct crypto_shash *hash_tfm; |
611 | struct shash_desc *hash_desc; | |
9c79f34f MH |
612 | }; |
613 | ||
614 | /** | |
615 | * write_tag_70_packet - Write encrypted filename (EFN) packet against FNEK | |
616 | * @filename: NULL-terminated filename string | |
617 | * | |
618 | * This is the simplest mechanism for achieving filename encryption in | |
619 | * eCryptfs. It encrypts the given filename with the mount-wide | |
620 | * filename encryption key (FNEK) and stores it in a packet to @dest, | |
621 | * which the callee will encode and write directly into the dentry | |
622 | * name. | |
623 | */ | |
624 | int | |
625 | ecryptfs_write_tag_70_packet(char *dest, size_t *remaining_bytes, | |
626 | size_t *packet_size, | |
627 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
628 | char *filename, size_t filename_size) | |
629 | { | |
630 | struct ecryptfs_write_tag_70_packet_silly_stack *s; | |
aee683b9 | 631 | struct key *auth_tok_key = NULL; |
9c79f34f MH |
632 | int rc = 0; |
633 | ||
3095e8e3 | 634 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
9c79f34f MH |
635 | if (!s) { |
636 | printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " | |
df261c52 | 637 | "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); |
f137f150 | 638 | rc = -ENOMEM; |
9c79f34f MH |
639 | goto out; |
640 | } | |
9c79f34f | 641 | (*packet_size) = 0; |
950983fc RS |
642 | rc = ecryptfs_find_auth_tok_for_sig( |
643 | &auth_tok_key, | |
644 | &s->auth_tok, mount_crypt_stat, | |
645 | mount_crypt_stat->global_default_fnek_sig); | |
646 | if (rc) { | |
647 | printk(KERN_ERR "%s: Error attempting to find auth tok for " | |
648 | "fnek sig [%s]; rc = [%d]\n", __func__, | |
649 | mount_crypt_stat->global_default_fnek_sig, rc); | |
650 | goto out; | |
651 | } | |
9c79f34f | 652 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name( |
3095e8e3 | 653 | &s->skcipher_tfm, |
9c79f34f MH |
654 | &s->tfm_mutex, mount_crypt_stat->global_default_fn_cipher_name); |
655 | if (unlikely(rc)) { | |
656 | printk(KERN_ERR "Internal error whilst attempting to get " | |
657 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
658 | mount_crypt_stat->global_default_fn_cipher_name, rc); | |
659 | goto out; | |
660 | } | |
661 | mutex_lock(s->tfm_mutex); | |
3095e8e3 | 662 | s->block_size = crypto_skcipher_blocksize(s->skcipher_tfm); |
9c79f34f MH |
663 | /* Plus one for the \0 separator between the random prefix |
664 | * and the plaintext filename */ | |
665 | s->num_rand_bytes = (ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES + 1); | |
666 | s->block_aligned_filename_size = (s->num_rand_bytes + filename_size); | |
667 | if ((s->block_aligned_filename_size % s->block_size) != 0) { | |
668 | s->num_rand_bytes += (s->block_size | |
669 | - (s->block_aligned_filename_size | |
670 | % s->block_size)); | |
671 | s->block_aligned_filename_size = (s->num_rand_bytes | |
672 | + filename_size); | |
673 | } | |
674 | /* Octet 0: Tag 70 identifier | |
675 | * Octets 1-N1: Tag 70 packet size (includes cipher identifier | |
676 | * and block-aligned encrypted filename size) | |
677 | * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) | |
678 | * Octet N2-N3: Cipher identifier (1 octet) | |
679 | * Octets N3-N4: Block-aligned encrypted filename | |
680 | * - Consists of a minimum number of random characters, a \0 | |
681 | * separator, and then the filename */ | |
4a26620d | 682 | s->max_packet_size = (ECRYPTFS_TAG_70_MAX_METADATA_SIZE |
9c79f34f MH |
683 | + s->block_aligned_filename_size); |
684 | if (dest == NULL) { | |
685 | (*packet_size) = s->max_packet_size; | |
686 | goto out_unlock; | |
687 | } | |
688 | if (s->max_packet_size > (*remaining_bytes)) { | |
a8f12864 MH |
689 | printk(KERN_WARNING "%s: Require [%zd] bytes to write; only " |
690 | "[%zd] available\n", __func__, s->max_packet_size, | |
9c79f34f MH |
691 | (*remaining_bytes)); |
692 | rc = -EINVAL; | |
693 | goto out_unlock; | |
694 | } | |
3095e8e3 HX |
695 | |
696 | s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); | |
697 | if (!s->skcipher_req) { | |
698 | printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " | |
699 | "skcipher_request_alloc for %s\n", __func__, | |
700 | crypto_skcipher_driver_name(s->skcipher_tfm)); | |
701 | rc = -ENOMEM; | |
702 | goto out_unlock; | |
703 | } | |
704 | ||
705 | skcipher_request_set_callback(s->skcipher_req, | |
706 | CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); | |
707 | ||
9c79f34f MH |
708 | s->block_aligned_filename = kzalloc(s->block_aligned_filename_size, |
709 | GFP_KERNEL); | |
710 | if (!s->block_aligned_filename) { | |
711 | printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " | |
df261c52 | 712 | "kzalloc [%zd] bytes\n", __func__, |
9c79f34f MH |
713 | s->block_aligned_filename_size); |
714 | rc = -ENOMEM; | |
715 | goto out_unlock; | |
716 | } | |
9c79f34f MH |
717 | dest[s->i++] = ECRYPTFS_TAG_70_PACKET_TYPE; |
718 | rc = ecryptfs_write_packet_length(&dest[s->i], | |
719 | (ECRYPTFS_SIG_SIZE | |
720 | + 1 /* Cipher code */ | |
721 | + s->block_aligned_filename_size), | |
722 | &s->packet_size_len); | |
723 | if (rc) { | |
724 | printk(KERN_ERR "%s: Error generating tag 70 packet " | |
725 | "header; cannot generate packet length; rc = [%d]\n", | |
726 | __func__, rc); | |
727 | goto out_free_unlock; | |
728 | } | |
729 | s->i += s->packet_size_len; | |
730 | ecryptfs_from_hex(&dest[s->i], | |
731 | mount_crypt_stat->global_default_fnek_sig, | |
732 | ECRYPTFS_SIG_SIZE); | |
733 | s->i += ECRYPTFS_SIG_SIZE; | |
734 | s->cipher_code = ecryptfs_code_for_cipher_string( | |
735 | mount_crypt_stat->global_default_fn_cipher_name, | |
736 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
737 | if (s->cipher_code == 0) { | |
738 | printk(KERN_WARNING "%s: Unable to generate code for " | |
a8f12864 | 739 | "cipher [%s] with key bytes [%zd]\n", __func__, |
9c79f34f MH |
740 | mount_crypt_stat->global_default_fn_cipher_name, |
741 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
742 | rc = -EINVAL; | |
743 | goto out_free_unlock; | |
744 | } | |
745 | dest[s->i++] = s->cipher_code; | |
9c79f34f MH |
746 | /* TODO: Support other key modules than passphrase for |
747 | * filename encryption */ | |
df6ad33b TH |
748 | if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { |
749 | rc = -EOPNOTSUPP; | |
750 | printk(KERN_INFO "%s: Filename encryption only supports " | |
751 | "password tokens\n", __func__); | |
752 | goto out_free_unlock; | |
753 | } | |
3095e8e3 HX |
754 | s->hash_tfm = crypto_alloc_shash(ECRYPTFS_TAG_70_DIGEST, 0, 0); |
755 | if (IS_ERR(s->hash_tfm)) { | |
756 | rc = PTR_ERR(s->hash_tfm); | |
9c79f34f MH |
757 | printk(KERN_ERR "%s: Error attempting to " |
758 | "allocate hash crypto context; rc = [%d]\n", | |
759 | __func__, rc); | |
760 | goto out_free_unlock; | |
761 | } | |
3095e8e3 HX |
762 | |
763 | s->hash_desc = kmalloc(sizeof(*s->hash_desc) + | |
764 | crypto_shash_descsize(s->hash_tfm), GFP_KERNEL); | |
765 | if (!s->hash_desc) { | |
766 | printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " | |
767 | "kmalloc [%zd] bytes\n", __func__, | |
768 | sizeof(*s->hash_desc) + | |
769 | crypto_shash_descsize(s->hash_tfm)); | |
770 | rc = -ENOMEM; | |
9c79f34f MH |
771 | goto out_release_free_unlock; |
772 | } | |
3095e8e3 HX |
773 | |
774 | s->hash_desc->tfm = s->hash_tfm; | |
775 | s->hash_desc->flags = CRYPTO_TFM_REQ_MAY_SLEEP; | |
776 | ||
777 | rc = crypto_shash_digest(s->hash_desc, | |
778 | (u8 *)s->auth_tok->token.password.session_key_encryption_key, | |
779 | s->auth_tok->token.password.session_key_encryption_key_bytes, | |
780 | s->hash); | |
9c79f34f MH |
781 | if (rc) { |
782 | printk(KERN_ERR | |
3095e8e3 | 783 | "%s: Error computing crypto hash; rc = [%d]\n", |
9c79f34f MH |
784 | __func__, rc); |
785 | goto out_release_free_unlock; | |
786 | } | |
787 | for (s->j = 0; s->j < (s->num_rand_bytes - 1); s->j++) { | |
788 | s->block_aligned_filename[s->j] = | |
789 | s->hash[(s->j % ECRYPTFS_TAG_70_DIGEST_SIZE)]; | |
790 | if ((s->j % ECRYPTFS_TAG_70_DIGEST_SIZE) | |
791 | == (ECRYPTFS_TAG_70_DIGEST_SIZE - 1)) { | |
3095e8e3 HX |
792 | rc = crypto_shash_digest(s->hash_desc, (u8 *)s->hash, |
793 | ECRYPTFS_TAG_70_DIGEST_SIZE, | |
794 | s->tmp_hash); | |
9c79f34f MH |
795 | if (rc) { |
796 | printk(KERN_ERR | |
3095e8e3 | 797 | "%s: Error computing crypto hash; " |
9c79f34f MH |
798 | "rc = [%d]\n", __func__, rc); |
799 | goto out_release_free_unlock; | |
800 | } | |
801 | memcpy(s->hash, s->tmp_hash, | |
802 | ECRYPTFS_TAG_70_DIGEST_SIZE); | |
803 | } | |
804 | if (s->block_aligned_filename[s->j] == '\0') | |
805 | s->block_aligned_filename[s->j] = ECRYPTFS_NON_NULL; | |
806 | } | |
807 | memcpy(&s->block_aligned_filename[s->num_rand_bytes], filename, | |
808 | filename_size); | |
809 | rc = virt_to_scatterlist(s->block_aligned_filename, | |
8d08dab7 TH |
810 | s->block_aligned_filename_size, s->src_sg, 2); |
811 | if (rc < 1) { | |
9c79f34f | 812 | printk(KERN_ERR "%s: Internal error whilst attempting to " |
8d08dab7 | 813 | "convert filename memory to scatterlist; rc = [%d]. " |
a8f12864 | 814 | "block_aligned_filename_size = [%zd]\n", __func__, rc, |
9c79f34f MH |
815 | s->block_aligned_filename_size); |
816 | goto out_release_free_unlock; | |
817 | } | |
818 | rc = virt_to_scatterlist(&dest[s->i], s->block_aligned_filename_size, | |
8d08dab7 TH |
819 | s->dst_sg, 2); |
820 | if (rc < 1) { | |
9c79f34f MH |
821 | printk(KERN_ERR "%s: Internal error whilst attempting to " |
822 | "convert encrypted filename memory to scatterlist; " | |
8d08dab7 TH |
823 | "rc = [%d]. block_aligned_filename_size = [%zd]\n", |
824 | __func__, rc, s->block_aligned_filename_size); | |
9c79f34f MH |
825 | goto out_release_free_unlock; |
826 | } | |
827 | /* The characters in the first block effectively do the job | |
828 | * of the IV here, so we just use 0's for the IV. Note the | |
829 | * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES | |
830 | * >= ECRYPTFS_MAX_IV_BYTES. */ | |
3095e8e3 HX |
831 | rc = crypto_skcipher_setkey( |
832 | s->skcipher_tfm, | |
9c79f34f MH |
833 | s->auth_tok->token.password.session_key_encryption_key, |
834 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
835 | if (rc < 0) { | |
836 | printk(KERN_ERR "%s: Error setting key for crypto context; " | |
837 | "rc = [%d]. s->auth_tok->token.password.session_key_" | |
838 | "encryption_key = [0x%p]; mount_crypt_stat->" | |
df261c52 | 839 | "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, |
9c79f34f MH |
840 | rc, |
841 | s->auth_tok->token.password.session_key_encryption_key, | |
842 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
843 | goto out_release_free_unlock; | |
844 | } | |
3095e8e3 HX |
845 | skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, |
846 | s->block_aligned_filename_size, s->iv); | |
847 | rc = crypto_skcipher_encrypt(s->skcipher_req); | |
9c79f34f MH |
848 | if (rc) { |
849 | printk(KERN_ERR "%s: Error attempting to encrypt filename; " | |
850 | "rc = [%d]\n", __func__, rc); | |
851 | goto out_release_free_unlock; | |
852 | } | |
853 | s->i += s->block_aligned_filename_size; | |
854 | (*packet_size) = s->i; | |
855 | (*remaining_bytes) -= (*packet_size); | |
856 | out_release_free_unlock: | |
3095e8e3 | 857 | crypto_free_shash(s->hash_tfm); |
9c79f34f | 858 | out_free_unlock: |
00fcf2cb | 859 | kzfree(s->block_aligned_filename); |
9c79f34f MH |
860 | out_unlock: |
861 | mutex_unlock(s->tfm_mutex); | |
862 | out: | |
b5695d04 RS |
863 | if (auth_tok_key) { |
864 | up_write(&(auth_tok_key->sem)); | |
aee683b9 | 865 | key_put(auth_tok_key); |
b5695d04 | 866 | } |
3095e8e3 HX |
867 | skcipher_request_free(s->skcipher_req); |
868 | kzfree(s->hash_desc); | |
9c79f34f MH |
869 | kfree(s); |
870 | return rc; | |
871 | } | |
872 | ||
873 | struct ecryptfs_parse_tag_70_packet_silly_stack { | |
874 | u8 cipher_code; | |
875 | size_t max_packet_size; | |
876 | size_t packet_size_len; | |
877 | size_t parsed_tag_70_packet_size; | |
878 | size_t block_aligned_filename_size; | |
879 | size_t block_size; | |
880 | size_t i; | |
881 | struct mutex *tfm_mutex; | |
882 | char *decrypted_filename; | |
883 | struct ecryptfs_auth_tok *auth_tok; | |
8d08dab7 TH |
884 | struct scatterlist src_sg[2]; |
885 | struct scatterlist dst_sg[2]; | |
3095e8e3 HX |
886 | struct crypto_skcipher *skcipher_tfm; |
887 | struct skcipher_request *skcipher_req; | |
9c79f34f MH |
888 | char fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX + 1]; |
889 | char iv[ECRYPTFS_MAX_IV_BYTES]; | |
2a559a8b | 890 | char cipher_string[ECRYPTFS_MAX_CIPHER_NAME_SIZE + 1]; |
9c79f34f MH |
891 | }; |
892 | ||
893 | /** | |
894 | * parse_tag_70_packet - Parse and process FNEK-encrypted passphrase packet | |
895 | * @filename: This function kmalloc's the memory for the filename | |
7d8bc2be MH |
896 | * @filename_size: This function sets this to the amount of memory |
897 | * kmalloc'd for the filename | |
898 | * @packet_size: This function sets this to the the number of octets | |
899 | * in the packet parsed | |
900 | * @mount_crypt_stat: The mount-wide cryptographic context | |
901 | * @data: The memory location containing the start of the tag 70 | |
902 | * packet | |
903 | * @max_packet_size: The maximum legal size of the packet to be parsed | |
904 | * from @data | |
905 | * | |
906 | * Returns zero on success; non-zero otherwise | |
9c79f34f MH |
907 | */ |
908 | int | |
909 | ecryptfs_parse_tag_70_packet(char **filename, size_t *filename_size, | |
910 | size_t *packet_size, | |
911 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
912 | char *data, size_t max_packet_size) | |
913 | { | |
914 | struct ecryptfs_parse_tag_70_packet_silly_stack *s; | |
aee683b9 | 915 | struct key *auth_tok_key = NULL; |
9c79f34f MH |
916 | int rc = 0; |
917 | ||
918 | (*packet_size) = 0; | |
919 | (*filename_size) = 0; | |
920 | (*filename) = NULL; | |
3095e8e3 | 921 | s = kzalloc(sizeof(*s), GFP_KERNEL); |
9c79f34f MH |
922 | if (!s) { |
923 | printk(KERN_ERR "%s: Out of memory whilst trying to kmalloc " | |
a8f12864 | 924 | "[%zd] bytes of kernel memory\n", __func__, sizeof(*s)); |
f137f150 | 925 | rc = -ENOMEM; |
9c79f34f MH |
926 | goto out; |
927 | } | |
4a26620d | 928 | if (max_packet_size < ECRYPTFS_TAG_70_MIN_METADATA_SIZE) { |
df261c52 | 929 | printk(KERN_WARNING "%s: max_packet_size is [%zd]; it must be " |
9c79f34f | 930 | "at least [%d]\n", __func__, max_packet_size, |
4a26620d | 931 | ECRYPTFS_TAG_70_MIN_METADATA_SIZE); |
9c79f34f MH |
932 | rc = -EINVAL; |
933 | goto out; | |
934 | } | |
935 | /* Octet 0: Tag 70 identifier | |
936 | * Octets 1-N1: Tag 70 packet size (includes cipher identifier | |
937 | * and block-aligned encrypted filename size) | |
938 | * Octets N1-N2: FNEK sig (ECRYPTFS_SIG_SIZE) | |
939 | * Octet N2-N3: Cipher identifier (1 octet) | |
940 | * Octets N3-N4: Block-aligned encrypted filename | |
941 | * - Consists of a minimum number of random numbers, a \0 | |
942 | * separator, and then the filename */ | |
943 | if (data[(*packet_size)++] != ECRYPTFS_TAG_70_PACKET_TYPE) { | |
944 | printk(KERN_WARNING "%s: Invalid packet tag [0x%.2x]; must be " | |
945 | "tag [0x%.2x]\n", __func__, | |
946 | data[((*packet_size) - 1)], ECRYPTFS_TAG_70_PACKET_TYPE); | |
947 | rc = -EINVAL; | |
948 | goto out; | |
949 | } | |
950 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], | |
951 | &s->parsed_tag_70_packet_size, | |
952 | &s->packet_size_len); | |
953 | if (rc) { | |
954 | printk(KERN_WARNING "%s: Error parsing packet length; " | |
955 | "rc = [%d]\n", __func__, rc); | |
956 | goto out; | |
957 | } | |
958 | s->block_aligned_filename_size = (s->parsed_tag_70_packet_size | |
959 | - ECRYPTFS_SIG_SIZE - 1); | |
960 | if ((1 + s->packet_size_len + s->parsed_tag_70_packet_size) | |
961 | > max_packet_size) { | |
a8f12864 MH |
962 | printk(KERN_WARNING "%s: max_packet_size is [%zd]; real packet " |
963 | "size is [%zd]\n", __func__, max_packet_size, | |
9c79f34f MH |
964 | (1 + s->packet_size_len + 1 |
965 | + s->block_aligned_filename_size)); | |
966 | rc = -EINVAL; | |
967 | goto out; | |
968 | } | |
969 | (*packet_size) += s->packet_size_len; | |
970 | ecryptfs_to_hex(s->fnek_sig_hex, &data[(*packet_size)], | |
971 | ECRYPTFS_SIG_SIZE); | |
972 | s->fnek_sig_hex[ECRYPTFS_SIG_SIZE_HEX] = '\0'; | |
973 | (*packet_size) += ECRYPTFS_SIG_SIZE; | |
974 | s->cipher_code = data[(*packet_size)++]; | |
975 | rc = ecryptfs_cipher_code_to_string(s->cipher_string, s->cipher_code); | |
976 | if (rc) { | |
977 | printk(KERN_WARNING "%s: Cipher code [%d] is invalid\n", | |
978 | __func__, s->cipher_code); | |
979 | goto out; | |
980 | } | |
950983fc RS |
981 | rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, |
982 | &s->auth_tok, mount_crypt_stat, | |
983 | s->fnek_sig_hex); | |
984 | if (rc) { | |
985 | printk(KERN_ERR "%s: Error attempting to find auth tok for " | |
986 | "fnek sig [%s]; rc = [%d]\n", __func__, s->fnek_sig_hex, | |
987 | rc); | |
988 | goto out; | |
989 | } | |
3095e8e3 | 990 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&s->skcipher_tfm, |
9c79f34f MH |
991 | &s->tfm_mutex, |
992 | s->cipher_string); | |
993 | if (unlikely(rc)) { | |
994 | printk(KERN_ERR "Internal error whilst attempting to get " | |
995 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
996 | s->cipher_string, rc); | |
997 | goto out; | |
998 | } | |
999 | mutex_lock(s->tfm_mutex); | |
1000 | rc = virt_to_scatterlist(&data[(*packet_size)], | |
8d08dab7 TH |
1001 | s->block_aligned_filename_size, s->src_sg, 2); |
1002 | if (rc < 1) { | |
9c79f34f MH |
1003 | printk(KERN_ERR "%s: Internal error whilst attempting to " |
1004 | "convert encrypted filename memory to scatterlist; " | |
8d08dab7 TH |
1005 | "rc = [%d]. block_aligned_filename_size = [%zd]\n", |
1006 | __func__, rc, s->block_aligned_filename_size); | |
9c79f34f MH |
1007 | goto out_unlock; |
1008 | } | |
1009 | (*packet_size) += s->block_aligned_filename_size; | |
1010 | s->decrypted_filename = kmalloc(s->block_aligned_filename_size, | |
1011 | GFP_KERNEL); | |
1012 | if (!s->decrypted_filename) { | |
1013 | printk(KERN_ERR "%s: Out of memory whilst attempting to " | |
a8f12864 | 1014 | "kmalloc [%zd] bytes\n", __func__, |
9c79f34f MH |
1015 | s->block_aligned_filename_size); |
1016 | rc = -ENOMEM; | |
1017 | goto out_unlock; | |
1018 | } | |
1019 | rc = virt_to_scatterlist(s->decrypted_filename, | |
8d08dab7 TH |
1020 | s->block_aligned_filename_size, s->dst_sg, 2); |
1021 | if (rc < 1) { | |
9c79f34f MH |
1022 | printk(KERN_ERR "%s: Internal error whilst attempting to " |
1023 | "convert decrypted filename memory to scatterlist; " | |
8d08dab7 TH |
1024 | "rc = [%d]. block_aligned_filename_size = [%zd]\n", |
1025 | __func__, rc, s->block_aligned_filename_size); | |
9c79f34f MH |
1026 | goto out_free_unlock; |
1027 | } | |
3095e8e3 HX |
1028 | |
1029 | s->skcipher_req = skcipher_request_alloc(s->skcipher_tfm, GFP_KERNEL); | |
1030 | if (!s->skcipher_req) { | |
1031 | printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " | |
1032 | "skcipher_request_alloc for %s\n", __func__, | |
1033 | crypto_skcipher_driver_name(s->skcipher_tfm)); | |
1034 | rc = -ENOMEM; | |
1035 | goto out_free_unlock; | |
1036 | } | |
1037 | ||
1038 | skcipher_request_set_callback(s->skcipher_req, | |
1039 | CRYPTO_TFM_REQ_MAY_SLEEP, NULL, NULL); | |
1040 | ||
9c79f34f MH |
1041 | /* The characters in the first block effectively do the job of |
1042 | * the IV here, so we just use 0's for the IV. Note the | |
1043 | * constraint that ECRYPTFS_FILENAME_MIN_RANDOM_PREPEND_BYTES | |
1044 | * >= ECRYPTFS_MAX_IV_BYTES. */ | |
9c79f34f MH |
1045 | /* TODO: Support other key modules than passphrase for |
1046 | * filename encryption */ | |
df6ad33b TH |
1047 | if (s->auth_tok->token_type != ECRYPTFS_PASSWORD) { |
1048 | rc = -EOPNOTSUPP; | |
1049 | printk(KERN_INFO "%s: Filename encryption only supports " | |
1050 | "password tokens\n", __func__); | |
1051 | goto out_free_unlock; | |
1052 | } | |
3095e8e3 HX |
1053 | rc = crypto_skcipher_setkey( |
1054 | s->skcipher_tfm, | |
9c79f34f MH |
1055 | s->auth_tok->token.password.session_key_encryption_key, |
1056 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
1057 | if (rc < 0) { | |
1058 | printk(KERN_ERR "%s: Error setting key for crypto context; " | |
1059 | "rc = [%d]. s->auth_tok->token.password.session_key_" | |
1060 | "encryption_key = [0x%p]; mount_crypt_stat->" | |
df261c52 | 1061 | "global_default_fn_cipher_key_bytes = [%zd]\n", __func__, |
9c79f34f MH |
1062 | rc, |
1063 | s->auth_tok->token.password.session_key_encryption_key, | |
1064 | mount_crypt_stat->global_default_fn_cipher_key_bytes); | |
1065 | goto out_free_unlock; | |
1066 | } | |
3095e8e3 HX |
1067 | skcipher_request_set_crypt(s->skcipher_req, s->src_sg, s->dst_sg, |
1068 | s->block_aligned_filename_size, s->iv); | |
1069 | rc = crypto_skcipher_decrypt(s->skcipher_req); | |
9c79f34f MH |
1070 | if (rc) { |
1071 | printk(KERN_ERR "%s: Error attempting to decrypt filename; " | |
1072 | "rc = [%d]\n", __func__, rc); | |
1073 | goto out_free_unlock; | |
1074 | } | |
9c79f34f MH |
1075 | while (s->decrypted_filename[s->i] != '\0' |
1076 | && s->i < s->block_aligned_filename_size) | |
1077 | s->i++; | |
1078 | if (s->i == s->block_aligned_filename_size) { | |
1079 | printk(KERN_WARNING "%s: Invalid tag 70 packet; could not " | |
1080 | "find valid separator between random characters and " | |
1081 | "the filename\n", __func__); | |
1082 | rc = -EINVAL; | |
1083 | goto out_free_unlock; | |
1084 | } | |
1085 | s->i++; | |
1086 | (*filename_size) = (s->block_aligned_filename_size - s->i); | |
1087 | if (!((*filename_size) > 0 && (*filename_size < PATH_MAX))) { | |
df261c52 | 1088 | printk(KERN_WARNING "%s: Filename size is [%zd], which is " |
9c79f34f MH |
1089 | "invalid\n", __func__, (*filename_size)); |
1090 | rc = -EINVAL; | |
1091 | goto out_free_unlock; | |
1092 | } | |
1093 | (*filename) = kmalloc(((*filename_size) + 1), GFP_KERNEL); | |
1094 | if (!(*filename)) { | |
1095 | printk(KERN_ERR "%s: Out of memory whilst attempting to " | |
a8f12864 | 1096 | "kmalloc [%zd] bytes\n", __func__, |
9c79f34f MH |
1097 | ((*filename_size) + 1)); |
1098 | rc = -ENOMEM; | |
1099 | goto out_free_unlock; | |
1100 | } | |
1101 | memcpy((*filename), &s->decrypted_filename[s->i], (*filename_size)); | |
1102 | (*filename)[(*filename_size)] = '\0'; | |
1103 | out_free_unlock: | |
1104 | kfree(s->decrypted_filename); | |
1105 | out_unlock: | |
1106 | mutex_unlock(s->tfm_mutex); | |
1107 | out: | |
1108 | if (rc) { | |
1109 | (*packet_size) = 0; | |
1110 | (*filename_size) = 0; | |
1111 | (*filename) = NULL; | |
1112 | } | |
b5695d04 RS |
1113 | if (auth_tok_key) { |
1114 | up_write(&(auth_tok_key->sem)); | |
aee683b9 | 1115 | key_put(auth_tok_key); |
b5695d04 | 1116 | } |
3095e8e3 | 1117 | skcipher_request_free(s->skcipher_req); |
9c79f34f MH |
1118 | kfree(s); |
1119 | return rc; | |
1120 | } | |
1121 | ||
cd9d67df MH |
1122 | static int |
1123 | ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) | |
1124 | { | |
1125 | int rc = 0; | |
1126 | ||
1127 | (*sig) = NULL; | |
1128 | switch (auth_tok->token_type) { | |
1129 | case ECRYPTFS_PASSWORD: | |
1130 | (*sig) = auth_tok->token.password.signature; | |
1131 | break; | |
1132 | case ECRYPTFS_PRIVATE_KEY: | |
1133 | (*sig) = auth_tok->token.private_key.signature; | |
1134 | break; | |
1135 | default: | |
1136 | printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", | |
1137 | auth_tok->token_type); | |
1138 | rc = -EINVAL; | |
1139 | } | |
1140 | return rc; | |
1141 | } | |
1142 | ||
dddfa461 | 1143 | /** |
22e78faf MH |
1144 | * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. |
1145 | * @auth_tok: The key authentication token used to decrypt the session key | |
1146 | * @crypt_stat: The cryptographic context | |
dddfa461 | 1147 | * |
22e78faf | 1148 | * Returns zero on success; non-zero error otherwise. |
dddfa461 | 1149 | */ |
f4aad16a MH |
1150 | static int |
1151 | decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1152 | struct ecryptfs_crypt_stat *crypt_stat) | |
dddfa461 | 1153 | { |
19e66a67 | 1154 | u8 cipher_code = 0; |
dddfa461 MH |
1155 | struct ecryptfs_msg_ctx *msg_ctx; |
1156 | struct ecryptfs_message *msg = NULL; | |
f4aad16a | 1157 | char *auth_tok_sig; |
3edc8376 | 1158 | char *payload = NULL; |
fa519964 | 1159 | size_t payload_len = 0; |
dddfa461 MH |
1160 | int rc; |
1161 | ||
5dda6992 MH |
1162 | rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); |
1163 | if (rc) { | |
f4aad16a MH |
1164 | printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", |
1165 | auth_tok->token_type); | |
1166 | goto out; | |
1167 | } | |
1168 | rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), | |
624ae528 | 1169 | &payload, &payload_len); |
dddfa461 | 1170 | if (rc) { |
f66e883e | 1171 | ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); |
dddfa461 MH |
1172 | goto out; |
1173 | } | |
624ae528 | 1174 | rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); |
dddfa461 | 1175 | if (rc) { |
624ae528 | 1176 | ecryptfs_printk(KERN_ERR, "Error sending message to " |
290502be | 1177 | "ecryptfsd: %d\n", rc); |
dddfa461 MH |
1178 | goto out; |
1179 | } | |
1180 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); | |
1181 | if (rc) { | |
1182 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " | |
1183 | "from the user space daemon\n"); | |
1184 | rc = -EIO; | |
1185 | goto out; | |
1186 | } | |
1187 | rc = parse_tag_65_packet(&(auth_tok->session_key), | |
1188 | &cipher_code, msg); | |
1189 | if (rc) { | |
1190 | printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", | |
1191 | rc); | |
1192 | goto out; | |
1193 | } | |
1194 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
1195 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | |
1196 | auth_tok->session_key.decrypted_key_size); | |
1197 | crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; | |
1198 | rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); | |
1199 | if (rc) { | |
1200 | ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", | |
1201 | cipher_code) | |
1202 | goto out; | |
1203 | } | |
1204 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; | |
1205 | if (ecryptfs_verbosity > 0) { | |
1206 | ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); | |
1207 | ecryptfs_dump_hex(crypt_stat->key, | |
1208 | crypt_stat->key_size); | |
1209 | } | |
1210 | out: | |
3a467418 | 1211 | kfree(msg); |
3edc8376 | 1212 | kfree(payload); |
dddfa461 MH |
1213 | return rc; |
1214 | } | |
1215 | ||
1216 | static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) | |
1217 | { | |
dddfa461 | 1218 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
e0869cc1 | 1219 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
dddfa461 | 1220 | |
e0869cc1 MH |
1221 | list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, |
1222 | auth_tok_list_head, list) { | |
1223 | list_del(&auth_tok_list_item->list); | |
dddfa461 MH |
1224 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, |
1225 | auth_tok_list_item); | |
1226 | } | |
dddfa461 MH |
1227 | } |
1228 | ||
1229 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; | |
1230 | ||
dddfa461 MH |
1231 | /** |
1232 | * parse_tag_1_packet | |
22e78faf | 1233 | * @crypt_stat: The cryptographic context to modify based on packet contents |
dddfa461 MH |
1234 | * @data: The raw bytes of the packet. |
1235 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; | |
22e78faf MH |
1236 | * a new authentication token will be placed at the |
1237 | * end of this list for this packet. | |
dddfa461 MH |
1238 | * @new_auth_tok: Pointer to a pointer to memory that this function |
1239 | * allocates; sets the memory address of the pointer to | |
1240 | * NULL on error. This object is added to the | |
1241 | * auth_tok_list. | |
1242 | * @packet_size: This function writes the size of the parsed packet | |
1243 | * into this memory location; zero on error. | |
22e78faf | 1244 | * @max_packet_size: The maximum allowable packet size |
dddfa461 MH |
1245 | * |
1246 | * Returns zero on success; non-zero on error. | |
1247 | */ | |
1248 | static int | |
1249 | parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, | |
1250 | unsigned char *data, struct list_head *auth_tok_list, | |
1251 | struct ecryptfs_auth_tok **new_auth_tok, | |
1252 | size_t *packet_size, size_t max_packet_size) | |
1253 | { | |
1254 | size_t body_size; | |
1255 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | |
1256 | size_t length_size; | |
1257 | int rc = 0; | |
1258 | ||
1259 | (*packet_size) = 0; | |
1260 | (*new_auth_tok) = NULL; | |
13218179 MH |
1261 | /** |
1262 | * This format is inspired by OpenPGP; see RFC 2440 | |
1263 | * packet tag 1 | |
1264 | * | |
1265 | * Tag 1 identifier (1 byte) | |
1266 | * Max Tag 1 packet size (max 3 bytes) | |
1267 | * Version (1 byte) | |
1268 | * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) | |
1269 | * Cipher identifier (1 byte) | |
1270 | * Encrypted key size (arbitrary) | |
1271 | * | |
1272 | * 12 bytes minimum packet size | |
dddfa461 | 1273 | */ |
13218179 MH |
1274 | if (unlikely(max_packet_size < 12)) { |
1275 | printk(KERN_ERR "Invalid max packet size; must be >=12\n"); | |
dddfa461 MH |
1276 | rc = -EINVAL; |
1277 | goto out; | |
1278 | } | |
dddfa461 | 1279 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { |
13218179 MH |
1280 | printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", |
1281 | ECRYPTFS_TAG_1_PACKET_TYPE); | |
dddfa461 MH |
1282 | rc = -EINVAL; |
1283 | goto out; | |
1284 | } | |
1285 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or | |
1286 | * at end of function upon failure */ | |
1287 | auth_tok_list_item = | |
13218179 MH |
1288 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, |
1289 | GFP_KERNEL); | |
dddfa461 | 1290 | if (!auth_tok_list_item) { |
13218179 | 1291 | printk(KERN_ERR "Unable to allocate memory\n"); |
dddfa461 MH |
1292 | rc = -ENOMEM; |
1293 | goto out; | |
1294 | } | |
dddfa461 | 1295 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
f66e883e MH |
1296 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
1297 | &length_size); | |
5dda6992 | 1298 | if (rc) { |
13218179 MH |
1299 | printk(KERN_WARNING "Error parsing packet length; " |
1300 | "rc = [%d]\n", rc); | |
dddfa461 MH |
1301 | goto out_free; |
1302 | } | |
13218179 | 1303 | if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { |
81acbcd6 | 1304 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
dddfa461 MH |
1305 | rc = -EINVAL; |
1306 | goto out_free; | |
1307 | } | |
1308 | (*packet_size) += length_size; | |
1309 | if (unlikely((*packet_size) + body_size > max_packet_size)) { | |
13218179 | 1310 | printk(KERN_WARNING "Packet size exceeds max\n"); |
dddfa461 MH |
1311 | rc = -EINVAL; |
1312 | goto out_free; | |
1313 | } | |
dddfa461 | 1314 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
13218179 MH |
1315 | printk(KERN_WARNING "Unknown version number [%d]\n", |
1316 | data[(*packet_size) - 1]); | |
dddfa461 MH |
1317 | rc = -EINVAL; |
1318 | goto out_free; | |
1319 | } | |
dddfa461 MH |
1320 | ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, |
1321 | &data[(*packet_size)], ECRYPTFS_SIG_SIZE); | |
1322 | *packet_size += ECRYPTFS_SIG_SIZE; | |
1323 | /* This byte is skipped because the kernel does not need to | |
1324 | * know which public key encryption algorithm was used */ | |
1325 | (*packet_size)++; | |
1326 | (*new_auth_tok)->session_key.encrypted_key_size = | |
13218179 | 1327 | body_size - (ECRYPTFS_SIG_SIZE + 2); |
dddfa461 MH |
1328 | if ((*new_auth_tok)->session_key.encrypted_key_size |
1329 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
13218179 MH |
1330 | printk(KERN_WARNING "Tag 1 packet contains key larger " |
1331 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); | |
dddfa461 MH |
1332 | rc = -EINVAL; |
1333 | goto out; | |
1334 | } | |
dddfa461 | 1335 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
13218179 | 1336 | &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); |
dddfa461 MH |
1337 | (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; |
1338 | (*new_auth_tok)->session_key.flags &= | |
1339 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
1340 | (*new_auth_tok)->session_key.flags |= | |
1341 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | |
1342 | (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; | |
13218179 | 1343 | (*new_auth_tok)->flags = 0; |
e2bd99ec MH |
1344 | (*new_auth_tok)->session_key.flags &= |
1345 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); | |
1346 | (*new_auth_tok)->session_key.flags &= | |
1347 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); | |
dddfa461 MH |
1348 | list_add(&auth_tok_list_item->list, auth_tok_list); |
1349 | goto out; | |
1350 | out_free: | |
1351 | (*new_auth_tok) = NULL; | |
1352 | memset(auth_tok_list_item, 0, | |
1353 | sizeof(struct ecryptfs_auth_tok_list_item)); | |
1354 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, | |
1355 | auth_tok_list_item); | |
1356 | out: | |
1357 | if (rc) | |
1358 | (*packet_size) = 0; | |
1359 | return rc; | |
1360 | } | |
1361 | ||
237fead6 MH |
1362 | /** |
1363 | * parse_tag_3_packet | |
1364 | * @crypt_stat: The cryptographic context to modify based on packet | |
1365 | * contents. | |
1366 | * @data: The raw bytes of the packet. | |
1367 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; | |
1368 | * a new authentication token will be placed at the end | |
1369 | * of this list for this packet. | |
1370 | * @new_auth_tok: Pointer to a pointer to memory that this function | |
1371 | * allocates; sets the memory address of the pointer to | |
1372 | * NULL on error. This object is added to the | |
1373 | * auth_tok_list. | |
1374 | * @packet_size: This function writes the size of the parsed packet | |
1375 | * into this memory location; zero on error. | |
1376 | * @max_packet_size: maximum number of bytes to parse | |
1377 | * | |
1378 | * Returns zero on success; non-zero on error. | |
1379 | */ | |
1380 | static int | |
1381 | parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |
1382 | unsigned char *data, struct list_head *auth_tok_list, | |
1383 | struct ecryptfs_auth_tok **new_auth_tok, | |
1384 | size_t *packet_size, size_t max_packet_size) | |
1385 | { | |
237fead6 MH |
1386 | size_t body_size; |
1387 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | |
1388 | size_t length_size; | |
dddfa461 | 1389 | int rc = 0; |
237fead6 MH |
1390 | |
1391 | (*packet_size) = 0; | |
1392 | (*new_auth_tok) = NULL; | |
c59becfc MH |
1393 | /** |
1394 | *This format is inspired by OpenPGP; see RFC 2440 | |
1395 | * packet tag 3 | |
1396 | * | |
1397 | * Tag 3 identifier (1 byte) | |
1398 | * Max Tag 3 packet size (max 3 bytes) | |
1399 | * Version (1 byte) | |
1400 | * Cipher code (1 byte) | |
1401 | * S2K specifier (1 byte) | |
1402 | * Hash identifier (1 byte) | |
1403 | * Salt (ECRYPTFS_SALT_SIZE) | |
1404 | * Hash iterations (1 byte) | |
1405 | * Encrypted key (arbitrary) | |
1406 | * | |
1407 | * (ECRYPTFS_SALT_SIZE + 7) minimum packet size | |
237fead6 | 1408 | */ |
c59becfc MH |
1409 | if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { |
1410 | printk(KERN_ERR "Max packet size too large\n"); | |
237fead6 MH |
1411 | rc = -EINVAL; |
1412 | goto out; | |
1413 | } | |
237fead6 | 1414 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { |
c59becfc MH |
1415 | printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", |
1416 | ECRYPTFS_TAG_3_PACKET_TYPE); | |
237fead6 MH |
1417 | rc = -EINVAL; |
1418 | goto out; | |
1419 | } | |
1420 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or | |
1421 | * at end of function upon failure */ | |
1422 | auth_tok_list_item = | |
c3762229 | 1423 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); |
237fead6 | 1424 | if (!auth_tok_list_item) { |
c59becfc | 1425 | printk(KERN_ERR "Unable to allocate memory\n"); |
237fead6 MH |
1426 | rc = -ENOMEM; |
1427 | goto out; | |
1428 | } | |
237fead6 | 1429 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
f66e883e MH |
1430 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
1431 | &length_size); | |
5dda6992 | 1432 | if (rc) { |
c59becfc MH |
1433 | printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", |
1434 | rc); | |
237fead6 MH |
1435 | goto out_free; |
1436 | } | |
c59becfc | 1437 | if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { |
81acbcd6 | 1438 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
237fead6 MH |
1439 | rc = -EINVAL; |
1440 | goto out_free; | |
1441 | } | |
1442 | (*packet_size) += length_size; | |
237fead6 | 1443 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
c59becfc | 1444 | printk(KERN_ERR "Packet size exceeds max\n"); |
237fead6 MH |
1445 | rc = -EINVAL; |
1446 | goto out_free; | |
1447 | } | |
237fead6 | 1448 | (*new_auth_tok)->session_key.encrypted_key_size = |
c59becfc | 1449 | (body_size - (ECRYPTFS_SALT_SIZE + 5)); |
f151cd2c RCV |
1450 | if ((*new_auth_tok)->session_key.encrypted_key_size |
1451 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
1452 | printk(KERN_WARNING "Tag 3 packet contains key larger " | |
1453 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES\n"); | |
1454 | rc = -EINVAL; | |
1455 | goto out_free; | |
1456 | } | |
237fead6 | 1457 | if (unlikely(data[(*packet_size)++] != 0x04)) { |
c59becfc MH |
1458 | printk(KERN_WARNING "Unknown version number [%d]\n", |
1459 | data[(*packet_size) - 1]); | |
237fead6 MH |
1460 | rc = -EINVAL; |
1461 | goto out_free; | |
1462 | } | |
b0105eae TH |
1463 | rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, |
1464 | (u16)data[(*packet_size)]); | |
1465 | if (rc) | |
1466 | goto out_free; | |
237fead6 MH |
1467 | /* A little extra work to differentiate among the AES key |
1468 | * sizes; see RFC2440 */ | |
1469 | switch(data[(*packet_size)++]) { | |
1470 | case RFC2440_CIPHER_AES_192: | |
1471 | crypt_stat->key_size = 24; | |
1472 | break; | |
1473 | default: | |
1474 | crypt_stat->key_size = | |
1475 | (*new_auth_tok)->session_key.encrypted_key_size; | |
1476 | } | |
b0105eae TH |
1477 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
1478 | if (rc) | |
1479 | goto out_free; | |
237fead6 | 1480 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
c59becfc | 1481 | printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); |
237fead6 MH |
1482 | rc = -ENOSYS; |
1483 | goto out_free; | |
1484 | } | |
237fead6 | 1485 | /* TODO: finish the hash mapping */ |
237fead6 MH |
1486 | switch (data[(*packet_size)++]) { |
1487 | case 0x01: /* See RFC2440 for these numbers and their mappings */ | |
1488 | /* Choose MD5 */ | |
237fead6 MH |
1489 | memcpy((*new_auth_tok)->token.password.salt, |
1490 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); | |
1491 | (*packet_size) += ECRYPTFS_SALT_SIZE; | |
237fead6 | 1492 | /* This conversion was taken straight from RFC2440 */ |
237fead6 MH |
1493 | (*new_auth_tok)->token.password.hash_iterations = |
1494 | ((u32) 16 + (data[(*packet_size)] & 15)) | |
1495 | << ((data[(*packet_size)] >> 4) + 6); | |
1496 | (*packet_size)++; | |
c59becfc MH |
1497 | /* Friendly reminder: |
1498 | * (*new_auth_tok)->session_key.encrypted_key_size = | |
1499 | * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ | |
237fead6 MH |
1500 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
1501 | &data[(*packet_size)], | |
1502 | (*new_auth_tok)->session_key.encrypted_key_size); | |
1503 | (*packet_size) += | |
1504 | (*new_auth_tok)->session_key.encrypted_key_size; | |
1505 | (*new_auth_tok)->session_key.flags &= | |
1506 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
1507 | (*new_auth_tok)->session_key.flags |= | |
1508 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | |
c59becfc | 1509 | (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ |
237fead6 MH |
1510 | break; |
1511 | default: | |
1512 | ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " | |
1513 | "[%d]\n", data[(*packet_size) - 1]); | |
1514 | rc = -ENOSYS; | |
1515 | goto out_free; | |
1516 | } | |
1517 | (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; | |
1518 | /* TODO: Parametarize; we might actually want userspace to | |
1519 | * decrypt the session key. */ | |
e2bd99ec MH |
1520 | (*new_auth_tok)->session_key.flags &= |
1521 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); | |
1522 | (*new_auth_tok)->session_key.flags &= | |
1523 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); | |
237fead6 MH |
1524 | list_add(&auth_tok_list_item->list, auth_tok_list); |
1525 | goto out; | |
1526 | out_free: | |
1527 | (*new_auth_tok) = NULL; | |
1528 | memset(auth_tok_list_item, 0, | |
1529 | sizeof(struct ecryptfs_auth_tok_list_item)); | |
1530 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, | |
1531 | auth_tok_list_item); | |
1532 | out: | |
1533 | if (rc) | |
1534 | (*packet_size) = 0; | |
1535 | return rc; | |
1536 | } | |
1537 | ||
1538 | /** | |
1539 | * parse_tag_11_packet | |
1540 | * @data: The raw bytes of the packet | |
1541 | * @contents: This function writes the data contents of the literal | |
1542 | * packet into this memory location | |
1543 | * @max_contents_bytes: The maximum number of bytes that this function | |
1544 | * is allowed to write into contents | |
1545 | * @tag_11_contents_size: This function writes the size of the parsed | |
1546 | * contents into this memory location; zero on | |
1547 | * error | |
1548 | * @packet_size: This function writes the size of the parsed packet | |
1549 | * into this memory location; zero on error | |
1550 | * @max_packet_size: maximum number of bytes to parse | |
1551 | * | |
1552 | * Returns zero on success; non-zero on error. | |
1553 | */ | |
1554 | static int | |
1555 | parse_tag_11_packet(unsigned char *data, unsigned char *contents, | |
1556 | size_t max_contents_bytes, size_t *tag_11_contents_size, | |
1557 | size_t *packet_size, size_t max_packet_size) | |
1558 | { | |
237fead6 MH |
1559 | size_t body_size; |
1560 | size_t length_size; | |
dddfa461 | 1561 | int rc = 0; |
237fead6 MH |
1562 | |
1563 | (*packet_size) = 0; | |
1564 | (*tag_11_contents_size) = 0; | |
f648104a MH |
1565 | /* This format is inspired by OpenPGP; see RFC 2440 |
1566 | * packet tag 11 | |
1567 | * | |
1568 | * Tag 11 identifier (1 byte) | |
1569 | * Max Tag 11 packet size (max 3 bytes) | |
1570 | * Binary format specifier (1 byte) | |
1571 | * Filename length (1 byte) | |
1572 | * Filename ("_CONSOLE") (8 bytes) | |
1573 | * Modification date (4 bytes) | |
1574 | * Literal data (arbitrary) | |
1575 | * | |
1576 | * We need at least 16 bytes of data for the packet to even be | |
1577 | * valid. | |
237fead6 | 1578 | */ |
f648104a MH |
1579 | if (max_packet_size < 16) { |
1580 | printk(KERN_ERR "Maximum packet size too small\n"); | |
237fead6 MH |
1581 | rc = -EINVAL; |
1582 | goto out; | |
1583 | } | |
237fead6 | 1584 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { |
f648104a | 1585 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
237fead6 MH |
1586 | rc = -EINVAL; |
1587 | goto out; | |
1588 | } | |
f66e883e MH |
1589 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
1590 | &length_size); | |
5dda6992 | 1591 | if (rc) { |
f648104a | 1592 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
237fead6 MH |
1593 | goto out; |
1594 | } | |
f648104a | 1595 | if (body_size < 14) { |
81acbcd6 | 1596 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
237fead6 MH |
1597 | rc = -EINVAL; |
1598 | goto out; | |
1599 | } | |
f648104a MH |
1600 | (*packet_size) += length_size; |
1601 | (*tag_11_contents_size) = (body_size - 14); | |
237fead6 | 1602 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { |
f648104a | 1603 | printk(KERN_ERR "Packet size exceeds max\n"); |
237fead6 MH |
1604 | rc = -EINVAL; |
1605 | goto out; | |
1606 | } | |
6352a293 TH |
1607 | if (unlikely((*tag_11_contents_size) > max_contents_bytes)) { |
1608 | printk(KERN_ERR "Literal data section in tag 11 packet exceeds " | |
1609 | "expected size\n"); | |
1610 | rc = -EINVAL; | |
1611 | goto out; | |
1612 | } | |
237fead6 | 1613 | if (data[(*packet_size)++] != 0x62) { |
f648104a | 1614 | printk(KERN_WARNING "Unrecognizable packet\n"); |
237fead6 MH |
1615 | rc = -EINVAL; |
1616 | goto out; | |
1617 | } | |
237fead6 | 1618 | if (data[(*packet_size)++] != 0x08) { |
f648104a | 1619 | printk(KERN_WARNING "Unrecognizable packet\n"); |
237fead6 MH |
1620 | rc = -EINVAL; |
1621 | goto out; | |
1622 | } | |
f648104a | 1623 | (*packet_size) += 12; /* Ignore filename and modification date */ |
237fead6 MH |
1624 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); |
1625 | (*packet_size) += (*tag_11_contents_size); | |
237fead6 MH |
1626 | out: |
1627 | if (rc) { | |
1628 | (*packet_size) = 0; | |
1629 | (*tag_11_contents_size) = 0; | |
1630 | } | |
1631 | return rc; | |
1632 | } | |
1633 | ||
f4aad16a MH |
1634 | int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, |
1635 | struct ecryptfs_auth_tok **auth_tok, | |
1636 | char *sig) | |
1637 | { | |
1638 | int rc = 0; | |
1639 | ||
1640 | (*auth_tok_key) = request_key(&key_type_user, sig, NULL); | |
1641 | if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { | |
1252cc3b RS |
1642 | (*auth_tok_key) = ecryptfs_get_encrypted_key(sig); |
1643 | if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { | |
1644 | printk(KERN_ERR "Could not find key with description: [%s]\n", | |
1645 | sig); | |
1646 | rc = process_request_key_err(PTR_ERR(*auth_tok_key)); | |
1647 | (*auth_tok_key) = NULL; | |
1648 | goto out; | |
1649 | } | |
f4aad16a | 1650 | } |
b5695d04 | 1651 | down_write(&(*auth_tok_key)->sem); |
0e1fc5ef | 1652 | rc = ecryptfs_verify_auth_tok_from_key(*auth_tok_key, auth_tok); |
aee683b9 | 1653 | if (rc) { |
b5695d04 | 1654 | up_write(&(*auth_tok_key)->sem); |
aee683b9 RS |
1655 | key_put(*auth_tok_key); |
1656 | (*auth_tok_key) = NULL; | |
0e1fc5ef | 1657 | goto out; |
f4aad16a MH |
1658 | } |
1659 | out: | |
1660 | return rc; | |
1661 | } | |
1662 | ||
f4aad16a | 1663 | /** |
22e78faf MH |
1664 | * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. |
1665 | * @auth_tok: The passphrase authentication token to use to encrypt the FEK | |
1666 | * @crypt_stat: The cryptographic context | |
237fead6 | 1667 | * |
22e78faf | 1668 | * Returns zero on success; non-zero error otherwise |
237fead6 | 1669 | */ |
f4aad16a MH |
1670 | static int |
1671 | decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1672 | struct ecryptfs_crypt_stat *crypt_stat) | |
237fead6 | 1673 | { |
ac97b9f9 MH |
1674 | struct scatterlist dst_sg[2]; |
1675 | struct scatterlist src_sg[2]; | |
dd8e2902 | 1676 | struct mutex *tfm_mutex; |
3095e8e3 HX |
1677 | struct crypto_skcipher *tfm; |
1678 | struct skcipher_request *req = NULL; | |
8bba066f | 1679 | int rc = 0; |
237fead6 | 1680 | |
f4aad16a MH |
1681 | if (unlikely(ecryptfs_verbosity > 0)) { |
1682 | ecryptfs_printk( | |
1683 | KERN_DEBUG, "Session key encryption key (size [%d]):\n", | |
1684 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1685 | ecryptfs_dump_hex( | |
1686 | auth_tok->token.password.session_key_encryption_key, | |
1687 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1688 | } | |
3095e8e3 | 1689 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, |
f4aad16a MH |
1690 | crypt_stat->cipher); |
1691 | if (unlikely(rc)) { | |
1692 | printk(KERN_ERR "Internal error whilst attempting to get " | |
1693 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
1694 | crypt_stat->cipher, rc); | |
1695 | goto out; | |
237fead6 | 1696 | } |
5dda6992 MH |
1697 | rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, |
1698 | auth_tok->session_key.encrypted_key_size, | |
ac97b9f9 MH |
1699 | src_sg, 2); |
1700 | if (rc < 1 || rc > 2) { | |
f4aad16a MH |
1701 | printk(KERN_ERR "Internal error whilst attempting to convert " |
1702 | "auth_tok->session_key.encrypted_key to scatterlist; " | |
1703 | "expected rc = 1; got rc = [%d]. " | |
1704 | "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, | |
1705 | auth_tok->session_key.encrypted_key_size); | |
1706 | goto out; | |
1707 | } | |
1708 | auth_tok->session_key.decrypted_key_size = | |
1709 | auth_tok->session_key.encrypted_key_size; | |
5dda6992 MH |
1710 | rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, |
1711 | auth_tok->session_key.decrypted_key_size, | |
ac97b9f9 MH |
1712 | dst_sg, 2); |
1713 | if (rc < 1 || rc > 2) { | |
f4aad16a MH |
1714 | printk(KERN_ERR "Internal error whilst attempting to convert " |
1715 | "auth_tok->session_key.decrypted_key to scatterlist; " | |
1716 | "expected rc = 1; got rc = [%d]\n", rc); | |
1717 | goto out; | |
1718 | } | |
1719 | mutex_lock(tfm_mutex); | |
3095e8e3 HX |
1720 | req = skcipher_request_alloc(tfm, GFP_KERNEL); |
1721 | if (!req) { | |
1722 | mutex_unlock(tfm_mutex); | |
1723 | printk(KERN_ERR "%s: Out of kernel memory whilst attempting to " | |
1724 | "skcipher_request_alloc for %s\n", __func__, | |
1725 | crypto_skcipher_driver_name(tfm)); | |
1726 | rc = -ENOMEM; | |
1727 | goto out; | |
1728 | } | |
1729 | ||
1730 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, | |
1731 | NULL, NULL); | |
1732 | rc = crypto_skcipher_setkey( | |
1733 | tfm, auth_tok->token.password.session_key_encryption_key, | |
f4aad16a MH |
1734 | crypt_stat->key_size); |
1735 | if (unlikely(rc < 0)) { | |
1736 | mutex_unlock(tfm_mutex); | |
e5d9cbde MH |
1737 | printk(KERN_ERR "Error setting key for crypto context\n"); |
1738 | rc = -EINVAL; | |
f4aad16a | 1739 | goto out; |
237fead6 | 1740 | } |
3095e8e3 HX |
1741 | skcipher_request_set_crypt(req, src_sg, dst_sg, |
1742 | auth_tok->session_key.encrypted_key_size, | |
1743 | NULL); | |
1744 | rc = crypto_skcipher_decrypt(req); | |
f4aad16a MH |
1745 | mutex_unlock(tfm_mutex); |
1746 | if (unlikely(rc)) { | |
8bba066f | 1747 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); |
f4aad16a | 1748 | goto out; |
8bba066f | 1749 | } |
237fead6 MH |
1750 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
1751 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | |
1752 | auth_tok->session_key.decrypted_key_size); | |
e2bd99ec | 1753 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
f4aad16a | 1754 | if (unlikely(ecryptfs_verbosity > 0)) { |
f24b3887 | 1755 | ecryptfs_printk(KERN_DEBUG, "FEK of size [%zd]:\n", |
f4aad16a | 1756 | crypt_stat->key_size); |
237fead6 MH |
1757 | ecryptfs_dump_hex(crypt_stat->key, |
1758 | crypt_stat->key_size); | |
f4aad16a | 1759 | } |
237fead6 | 1760 | out: |
3095e8e3 | 1761 | skcipher_request_free(req); |
237fead6 MH |
1762 | return rc; |
1763 | } | |
1764 | ||
1765 | /** | |
1766 | * ecryptfs_parse_packet_set | |
22e78faf MH |
1767 | * @crypt_stat: The cryptographic context |
1768 | * @src: Virtual address of region of memory containing the packets | |
1769 | * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set | |
237fead6 MH |
1770 | * |
1771 | * Get crypt_stat to have the file's session key if the requisite key | |
1772 | * is available to decrypt the session key. | |
1773 | * | |
1774 | * Returns Zero if a valid authentication token was retrieved and | |
1775 | * processed; negative value for file not encrypted or for error | |
1776 | * conditions. | |
1777 | */ | |
1778 | int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, | |
1779 | unsigned char *src, | |
1780 | struct dentry *ecryptfs_dentry) | |
1781 | { | |
1782 | size_t i = 0; | |
f4aad16a | 1783 | size_t found_auth_tok; |
237fead6 | 1784 | size_t next_packet_is_auth_tok_packet; |
237fead6 | 1785 | struct list_head auth_tok_list; |
dd8e2902 MH |
1786 | struct ecryptfs_auth_tok *matching_auth_tok; |
1787 | struct ecryptfs_auth_tok *candidate_auth_tok; | |
f4aad16a | 1788 | char *candidate_auth_tok_sig; |
237fead6 MH |
1789 | size_t packet_size; |
1790 | struct ecryptfs_auth_tok *new_auth_tok; | |
1791 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; | |
f4aad16a | 1792 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
237fead6 MH |
1793 | size_t tag_11_contents_size; |
1794 | size_t tag_11_packet_size; | |
aee683b9 | 1795 | struct key *auth_tok_key = NULL; |
dddfa461 | 1796 | int rc = 0; |
237fead6 MH |
1797 | |
1798 | INIT_LIST_HEAD(&auth_tok_list); | |
f4aad16a | 1799 | /* Parse the header to find as many packets as we can; these will be |
237fead6 MH |
1800 | * added the our &auth_tok_list */ |
1801 | next_packet_is_auth_tok_packet = 1; | |
1802 | while (next_packet_is_auth_tok_packet) { | |
1803 | size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); | |
1804 | ||
1805 | switch (src[i]) { | |
1806 | case ECRYPTFS_TAG_3_PACKET_TYPE: | |
1807 | rc = parse_tag_3_packet(crypt_stat, | |
1808 | (unsigned char *)&src[i], | |
1809 | &auth_tok_list, &new_auth_tok, | |
1810 | &packet_size, max_packet_size); | |
1811 | if (rc) { | |
1812 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1813 | "tag 3 packet\n"); | |
1814 | rc = -EIO; | |
1815 | goto out_wipe_list; | |
1816 | } | |
1817 | i += packet_size; | |
1818 | rc = parse_tag_11_packet((unsigned char *)&src[i], | |
1819 | sig_tmp_space, | |
1820 | ECRYPTFS_SIG_SIZE, | |
1821 | &tag_11_contents_size, | |
1822 | &tag_11_packet_size, | |
1823 | max_packet_size); | |
1824 | if (rc) { | |
1825 | ecryptfs_printk(KERN_ERR, "No valid " | |
1826 | "(ecryptfs-specific) literal " | |
1827 | "packet containing " | |
1828 | "authentication token " | |
1829 | "signature found after " | |
1830 | "tag 3 packet\n"); | |
1831 | rc = -EIO; | |
1832 | goto out_wipe_list; | |
1833 | } | |
1834 | i += tag_11_packet_size; | |
1835 | if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { | |
1836 | ecryptfs_printk(KERN_ERR, "Expected " | |
1837 | "signature of size [%d]; " | |
f24b3887 | 1838 | "read size [%zd]\n", |
237fead6 MH |
1839 | ECRYPTFS_SIG_SIZE, |
1840 | tag_11_contents_size); | |
1841 | rc = -EIO; | |
1842 | goto out_wipe_list; | |
1843 | } | |
1844 | ecryptfs_to_hex(new_auth_tok->token.password.signature, | |
1845 | sig_tmp_space, tag_11_contents_size); | |
1846 | new_auth_tok->token.password.signature[ | |
1847 | ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; | |
e2bd99ec | 1848 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
237fead6 | 1849 | break; |
dddfa461 MH |
1850 | case ECRYPTFS_TAG_1_PACKET_TYPE: |
1851 | rc = parse_tag_1_packet(crypt_stat, | |
1852 | (unsigned char *)&src[i], | |
1853 | &auth_tok_list, &new_auth_tok, | |
1854 | &packet_size, max_packet_size); | |
1855 | if (rc) { | |
1856 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1857 | "tag 1 packet\n"); | |
1858 | rc = -EIO; | |
1859 | goto out_wipe_list; | |
1860 | } | |
1861 | i += packet_size; | |
e2bd99ec | 1862 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
dddfa461 | 1863 | break; |
237fead6 MH |
1864 | case ECRYPTFS_TAG_11_PACKET_TYPE: |
1865 | ecryptfs_printk(KERN_WARNING, "Invalid packet set " | |
1866 | "(Tag 11 not allowed by itself)\n"); | |
1867 | rc = -EIO; | |
1868 | goto out_wipe_list; | |
237fead6 | 1869 | default: |
f24b3887 TH |
1870 | ecryptfs_printk(KERN_DEBUG, "No packet at offset [%zd] " |
1871 | "of the file header; hex value of " | |
237fead6 MH |
1872 | "character is [0x%.2x]\n", i, src[i]); |
1873 | next_packet_is_auth_tok_packet = 0; | |
1874 | } | |
1875 | } | |
1876 | if (list_empty(&auth_tok_list)) { | |
f4aad16a MH |
1877 | printk(KERN_ERR "The lower file appears to be a non-encrypted " |
1878 | "eCryptfs file; this is not supported in this version " | |
1879 | "of the eCryptfs kernel module\n"); | |
1880 | rc = -EINVAL; | |
237fead6 MH |
1881 | goto out; |
1882 | } | |
f4aad16a MH |
1883 | /* auth_tok_list contains the set of authentication tokens |
1884 | * parsed from the metadata. We need to find a matching | |
1885 | * authentication token that has the secret component(s) | |
1886 | * necessary to decrypt the EFEK in the auth_tok parsed from | |
1887 | * the metadata. There may be several potential matches, but | |
1888 | * just one will be sufficient to decrypt to get the FEK. */ | |
1889 | find_next_matching_auth_tok: | |
1890 | found_auth_tok = 0; | |
1891 | list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { | |
237fead6 MH |
1892 | candidate_auth_tok = &auth_tok_list_item->auth_tok; |
1893 | if (unlikely(ecryptfs_verbosity > 0)) { | |
1894 | ecryptfs_printk(KERN_DEBUG, | |
1895 | "Considering cadidate auth tok:\n"); | |
1896 | ecryptfs_dump_auth_tok(candidate_auth_tok); | |
1897 | } | |
5dda6992 MH |
1898 | rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, |
1899 | candidate_auth_tok); | |
1900 | if (rc) { | |
f4aad16a MH |
1901 | printk(KERN_ERR |
1902 | "Unrecognized candidate auth tok type: [%d]\n", | |
1903 | candidate_auth_tok->token_type); | |
1904 | rc = -EINVAL; | |
1905 | goto out_wipe_list; | |
1906 | } | |
39fac853 | 1907 | rc = ecryptfs_find_auth_tok_for_sig(&auth_tok_key, |
aee683b9 | 1908 | &matching_auth_tok, |
9c79f34f | 1909 | crypt_stat->mount_crypt_stat, |
5dda6992 | 1910 | candidate_auth_tok_sig); |
39fac853 | 1911 | if (!rc) { |
dddfa461 | 1912 | found_auth_tok = 1; |
f4aad16a | 1913 | goto found_matching_auth_tok; |
237fead6 MH |
1914 | } |
1915 | } | |
237fead6 | 1916 | if (!found_auth_tok) { |
f4aad16a MH |
1917 | ecryptfs_printk(KERN_ERR, "Could not find a usable " |
1918 | "authentication token\n"); | |
237fead6 MH |
1919 | rc = -EIO; |
1920 | goto out_wipe_list; | |
dddfa461 | 1921 | } |
f4aad16a | 1922 | found_matching_auth_tok: |
e2bd99ec | 1923 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
dddfa461 | 1924 | memcpy(&(candidate_auth_tok->token.private_key), |
f4aad16a | 1925 | &(matching_auth_tok->token.private_key), |
dddfa461 | 1926 | sizeof(struct ecryptfs_private_key)); |
b2987a5e TH |
1927 | up_write(&(auth_tok_key->sem)); |
1928 | key_put(auth_tok_key); | |
f4aad16a | 1929 | rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, |
dddfa461 MH |
1930 | crypt_stat); |
1931 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { | |
237fead6 | 1932 | memcpy(&(candidate_auth_tok->token.password), |
f4aad16a | 1933 | &(matching_auth_tok->token.password), |
237fead6 | 1934 | sizeof(struct ecryptfs_password)); |
b2987a5e TH |
1935 | up_write(&(auth_tok_key->sem)); |
1936 | key_put(auth_tok_key); | |
f4aad16a MH |
1937 | rc = decrypt_passphrase_encrypted_session_key( |
1938 | candidate_auth_tok, crypt_stat); | |
b2987a5e TH |
1939 | } else { |
1940 | up_write(&(auth_tok_key->sem)); | |
1941 | key_put(auth_tok_key); | |
1942 | rc = -EINVAL; | |
dddfa461 MH |
1943 | } |
1944 | if (rc) { | |
f4aad16a MH |
1945 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
1946 | ||
1947 | ecryptfs_printk(KERN_WARNING, "Error decrypting the " | |
1948 | "session key for authentication token with sig " | |
1949 | "[%.*s]; rc = [%d]. Removing auth tok " | |
1950 | "candidate from the list and searching for " | |
888d57bb JP |
1951 | "the next match.\n", ECRYPTFS_SIG_SIZE_HEX, |
1952 | candidate_auth_tok_sig, rc); | |
f4aad16a MH |
1953 | list_for_each_entry_safe(auth_tok_list_item, |
1954 | auth_tok_list_item_tmp, | |
1955 | &auth_tok_list, list) { | |
1956 | if (candidate_auth_tok | |
1957 | == &auth_tok_list_item->auth_tok) { | |
1958 | list_del(&auth_tok_list_item->list); | |
1959 | kmem_cache_free( | |
1960 | ecryptfs_auth_tok_list_item_cache, | |
1961 | auth_tok_list_item); | |
1962 | goto find_next_matching_auth_tok; | |
1963 | } | |
1964 | } | |
1965 | BUG(); | |
dddfa461 MH |
1966 | } |
1967 | rc = ecryptfs_compute_root_iv(crypt_stat); | |
1968 | if (rc) { | |
1969 | ecryptfs_printk(KERN_ERR, "Error computing " | |
1970 | "the root IV\n"); | |
1971 | goto out_wipe_list; | |
237fead6 MH |
1972 | } |
1973 | rc = ecryptfs_init_crypt_ctx(crypt_stat); | |
1974 | if (rc) { | |
1975 | ecryptfs_printk(KERN_ERR, "Error initializing crypto " | |
1976 | "context for cipher [%s]; rc = [%d]\n", | |
1977 | crypt_stat->cipher, rc); | |
1978 | } | |
1979 | out_wipe_list: | |
1980 | wipe_auth_tok_list(&auth_tok_list); | |
1981 | out: | |
1982 | return rc; | |
1983 | } | |
f4aad16a | 1984 | |
dddfa461 | 1985 | static int |
b2987a5e TH |
1986 | pki_encrypt_session_key(struct key *auth_tok_key, |
1987 | struct ecryptfs_auth_tok *auth_tok, | |
dddfa461 MH |
1988 | struct ecryptfs_crypt_stat *crypt_stat, |
1989 | struct ecryptfs_key_record *key_rec) | |
1990 | { | |
1991 | struct ecryptfs_msg_ctx *msg_ctx = NULL; | |
624ae528 | 1992 | char *payload = NULL; |
99b373ff | 1993 | size_t payload_len = 0; |
dddfa461 MH |
1994 | struct ecryptfs_message *msg; |
1995 | int rc; | |
1996 | ||
1997 | rc = write_tag_66_packet(auth_tok->token.private_key.signature, | |
9c79f34f MH |
1998 | ecryptfs_code_for_cipher_string( |
1999 | crypt_stat->cipher, | |
2000 | crypt_stat->key_size), | |
624ae528 | 2001 | crypt_stat, &payload, &payload_len); |
b2987a5e TH |
2002 | up_write(&(auth_tok_key->sem)); |
2003 | key_put(auth_tok_key); | |
dddfa461 MH |
2004 | if (rc) { |
2005 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); | |
2006 | goto out; | |
2007 | } | |
624ae528 | 2008 | rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); |
dddfa461 | 2009 | if (rc) { |
624ae528 | 2010 | ecryptfs_printk(KERN_ERR, "Error sending message to " |
290502be | 2011 | "ecryptfsd: %d\n", rc); |
dddfa461 MH |
2012 | goto out; |
2013 | } | |
2014 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); | |
2015 | if (rc) { | |
2016 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " | |
2017 | "from the user space daemon\n"); | |
2018 | rc = -EIO; | |
2019 | goto out; | |
2020 | } | |
2021 | rc = parse_tag_67_packet(key_rec, msg); | |
2022 | if (rc) | |
2023 | ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); | |
2024 | kfree(msg); | |
2025 | out: | |
624ae528 | 2026 | kfree(payload); |
dddfa461 MH |
2027 | return rc; |
2028 | } | |
2029 | /** | |
2030 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet | |
2031 | * @dest: Buffer into which to write the packet | |
22e78faf | 2032 | * @remaining_bytes: Maximum number of bytes that can be writtn |
b2987a5e TH |
2033 | * @auth_tok_key: The authentication token key to unlock and put when done with |
2034 | * @auth_tok | |
22e78faf MH |
2035 | * @auth_tok: The authentication token used for generating the tag 1 packet |
2036 | * @crypt_stat: The cryptographic context | |
2037 | * @key_rec: The key record struct for the tag 1 packet | |
dddfa461 MH |
2038 | * @packet_size: This function will write the number of bytes that end |
2039 | * up constituting the packet; set to zero on error | |
2040 | * | |
2041 | * Returns zero on success; non-zero on error. | |
2042 | */ | |
2043 | static int | |
f4aad16a | 2044 | write_tag_1_packet(char *dest, size_t *remaining_bytes, |
b2987a5e | 2045 | struct key *auth_tok_key, struct ecryptfs_auth_tok *auth_tok, |
dddfa461 | 2046 | struct ecryptfs_crypt_stat *crypt_stat, |
dddfa461 MH |
2047 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
2048 | { | |
2049 | size_t i; | |
2050 | size_t encrypted_session_key_valid = 0; | |
dddfa461 | 2051 | size_t packet_size_length; |
f4aad16a | 2052 | size_t max_packet_size; |
dddfa461 MH |
2053 | int rc = 0; |
2054 | ||
2055 | (*packet_size) = 0; | |
2056 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, | |
2057 | ECRYPTFS_SIG_SIZE); | |
2058 | encrypted_session_key_valid = 0; | |
2059 | for (i = 0; i < crypt_stat->key_size; i++) | |
2060 | encrypted_session_key_valid |= | |
2061 | auth_tok->session_key.encrypted_key[i]; | |
2062 | if (encrypted_session_key_valid) { | |
2063 | memcpy(key_rec->enc_key, | |
2064 | auth_tok->session_key.encrypted_key, | |
2065 | auth_tok->session_key.encrypted_key_size); | |
b2987a5e TH |
2066 | up_write(&(auth_tok_key->sem)); |
2067 | key_put(auth_tok_key); | |
dddfa461 MH |
2068 | goto encrypted_session_key_set; |
2069 | } | |
2070 | if (auth_tok->session_key.encrypted_key_size == 0) | |
2071 | auth_tok->session_key.encrypted_key_size = | |
2072 | auth_tok->token.private_key.key_size; | |
b2987a5e TH |
2073 | rc = pki_encrypt_session_key(auth_tok_key, auth_tok, crypt_stat, |
2074 | key_rec); | |
dddfa461 | 2075 | if (rc) { |
f66e883e MH |
2076 | printk(KERN_ERR "Failed to encrypt session key via a key " |
2077 | "module; rc = [%d]\n", rc); | |
dddfa461 MH |
2078 | goto out; |
2079 | } | |
2080 | if (ecryptfs_verbosity > 0) { | |
2081 | ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); | |
2082 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); | |
2083 | } | |
2084 | encrypted_session_key_set: | |
f4aad16a MH |
2085 | /* This format is inspired by OpenPGP; see RFC 2440 |
2086 | * packet tag 1 */ | |
2087 | max_packet_size = (1 /* Tag 1 identifier */ | |
2088 | + 3 /* Max Tag 1 packet size */ | |
2089 | + 1 /* Version */ | |
2090 | + ECRYPTFS_SIG_SIZE /* Key identifier */ | |
2091 | + 1 /* Cipher identifier */ | |
2092 | + key_rec->enc_key_size); /* Encrypted key size */ | |
2093 | if (max_packet_size > (*remaining_bytes)) { | |
2094 | printk(KERN_ERR "Packet length larger than maximum allowable; " | |
81acbcd6 | 2095 | "need up to [%td] bytes, but there are only [%td] " |
f4aad16a | 2096 | "available\n", max_packet_size, (*remaining_bytes)); |
dddfa461 MH |
2097 | rc = -EINVAL; |
2098 | goto out; | |
2099 | } | |
2100 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; | |
f66e883e MH |
2101 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
2102 | (max_packet_size - 4), | |
2103 | &packet_size_length); | |
dddfa461 MH |
2104 | if (rc) { |
2105 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " | |
2106 | "header; cannot generate packet length\n"); | |
2107 | goto out; | |
2108 | } | |
2109 | (*packet_size) += packet_size_length; | |
2110 | dest[(*packet_size)++] = 0x03; /* version 3 */ | |
2111 | memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); | |
2112 | (*packet_size) += ECRYPTFS_SIG_SIZE; | |
2113 | dest[(*packet_size)++] = RFC2440_CIPHER_RSA; | |
2114 | memcpy(&dest[(*packet_size)], key_rec->enc_key, | |
2115 | key_rec->enc_key_size); | |
2116 | (*packet_size) += key_rec->enc_key_size; | |
2117 | out: | |
2118 | if (rc) | |
2119 | (*packet_size) = 0; | |
f4aad16a MH |
2120 | else |
2121 | (*remaining_bytes) -= (*packet_size); | |
dddfa461 MH |
2122 | return rc; |
2123 | } | |
237fead6 MH |
2124 | |
2125 | /** | |
2126 | * write_tag_11_packet | |
2127 | * @dest: Target into which Tag 11 packet is to be written | |
22e78faf | 2128 | * @remaining_bytes: Maximum packet length |
237fead6 MH |
2129 | * @contents: Byte array of contents to copy in |
2130 | * @contents_length: Number of bytes in contents | |
2131 | * @packet_length: Length of the Tag 11 packet written; zero on error | |
2132 | * | |
2133 | * Returns zero on success; non-zero on error. | |
2134 | */ | |
2135 | static int | |
81acbcd6 | 2136 | write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, |
146a4606 | 2137 | size_t contents_length, size_t *packet_length) |
237fead6 | 2138 | { |
237fead6 | 2139 | size_t packet_size_length; |
146a4606 | 2140 | size_t max_packet_size; |
dddfa461 | 2141 | int rc = 0; |
237fead6 MH |
2142 | |
2143 | (*packet_length) = 0; | |
146a4606 MH |
2144 | /* This format is inspired by OpenPGP; see RFC 2440 |
2145 | * packet tag 11 */ | |
2146 | max_packet_size = (1 /* Tag 11 identifier */ | |
2147 | + 3 /* Max Tag 11 packet size */ | |
2148 | + 1 /* Binary format specifier */ | |
2149 | + 1 /* Filename length */ | |
2150 | + 8 /* Filename ("_CONSOLE") */ | |
2151 | + 4 /* Modification date */ | |
2152 | + contents_length); /* Literal data */ | |
2153 | if (max_packet_size > (*remaining_bytes)) { | |
2154 | printk(KERN_ERR "Packet length larger than maximum allowable; " | |
81acbcd6 | 2155 | "need up to [%td] bytes, but there are only [%td] " |
146a4606 | 2156 | "available\n", max_packet_size, (*remaining_bytes)); |
237fead6 | 2157 | rc = -EINVAL; |
237fead6 MH |
2158 | goto out; |
2159 | } | |
237fead6 | 2160 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; |
f66e883e MH |
2161 | rc = ecryptfs_write_packet_length(&dest[(*packet_length)], |
2162 | (max_packet_size - 4), | |
2163 | &packet_size_length); | |
237fead6 | 2164 | if (rc) { |
146a4606 MH |
2165 | printk(KERN_ERR "Error generating tag 11 packet header; cannot " |
2166 | "generate packet length. rc = [%d]\n", rc); | |
237fead6 MH |
2167 | goto out; |
2168 | } | |
2169 | (*packet_length) += packet_size_length; | |
146a4606 | 2170 | dest[(*packet_length)++] = 0x62; /* binary data format specifier */ |
237fead6 MH |
2171 | dest[(*packet_length)++] = 8; |
2172 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); | |
2173 | (*packet_length) += 8; | |
237fead6 MH |
2174 | memset(&dest[(*packet_length)], 0x00, 4); |
2175 | (*packet_length) += 4; | |
237fead6 MH |
2176 | memcpy(&dest[(*packet_length)], contents, contents_length); |
2177 | (*packet_length) += contents_length; | |
2178 | out: | |
2179 | if (rc) | |
2180 | (*packet_length) = 0; | |
146a4606 MH |
2181 | else |
2182 | (*remaining_bytes) -= (*packet_length); | |
237fead6 MH |
2183 | return rc; |
2184 | } | |
2185 | ||
2186 | /** | |
2187 | * write_tag_3_packet | |
2188 | * @dest: Buffer into which to write the packet | |
22e78faf | 2189 | * @remaining_bytes: Maximum number of bytes that can be written |
237fead6 MH |
2190 | * @auth_tok: Authentication token |
2191 | * @crypt_stat: The cryptographic context | |
2192 | * @key_rec: encrypted key | |
2193 | * @packet_size: This function will write the number of bytes that end | |
2194 | * up constituting the packet; set to zero on error | |
2195 | * | |
2196 | * Returns zero on success; non-zero on error. | |
2197 | */ | |
2198 | static int | |
f4aad16a MH |
2199 | write_tag_3_packet(char *dest, size_t *remaining_bytes, |
2200 | struct ecryptfs_auth_tok *auth_tok, | |
237fead6 MH |
2201 | struct ecryptfs_crypt_stat *crypt_stat, |
2202 | struct ecryptfs_key_record *key_rec, size_t *packet_size) | |
2203 | { | |
237fead6 | 2204 | size_t i; |
237fead6 MH |
2205 | size_t encrypted_session_key_valid = 0; |
2206 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; | |
ac97b9f9 MH |
2207 | struct scatterlist dst_sg[2]; |
2208 | struct scatterlist src_sg[2]; | |
237fead6 | 2209 | struct mutex *tfm_mutex = NULL; |
19e66a67 | 2210 | u8 cipher_code; |
f4aad16a MH |
2211 | size_t packet_size_length; |
2212 | size_t max_packet_size; | |
2213 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | |
2214 | crypt_stat->mount_crypt_stat; | |
3095e8e3 HX |
2215 | struct crypto_skcipher *tfm; |
2216 | struct skcipher_request *req; | |
8bba066f | 2217 | int rc = 0; |
237fead6 MH |
2218 | |
2219 | (*packet_size) = 0; | |
dddfa461 | 2220 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, |
237fead6 | 2221 | ECRYPTFS_SIG_SIZE); |
3095e8e3 | 2222 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&tfm, &tfm_mutex, |
f4aad16a MH |
2223 | crypt_stat->cipher); |
2224 | if (unlikely(rc)) { | |
2225 | printk(KERN_ERR "Internal error whilst attempting to get " | |
2226 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
2227 | crypt_stat->cipher, rc); | |
2228 | goto out; | |
2229 | } | |
2230 | if (mount_crypt_stat->global_default_cipher_key_size == 0) { | |
f4aad16a | 2231 | printk(KERN_WARNING "No key size specified at mount; " |
3095e8e3 HX |
2232 | "defaulting to [%d]\n", |
2233 | crypto_skcipher_default_keysize(tfm)); | |
f4aad16a | 2234 | mount_crypt_stat->global_default_cipher_key_size = |
3095e8e3 | 2235 | crypto_skcipher_default_keysize(tfm); |
237fead6 | 2236 | } |
f4aad16a MH |
2237 | if (crypt_stat->key_size == 0) |
2238 | crypt_stat->key_size = | |
2239 | mount_crypt_stat->global_default_cipher_key_size; | |
237fead6 MH |
2240 | if (auth_tok->session_key.encrypted_key_size == 0) |
2241 | auth_tok->session_key.encrypted_key_size = | |
2242 | crypt_stat->key_size; | |
2243 | if (crypt_stat->key_size == 24 | |
2244 | && strcmp("aes", crypt_stat->cipher) == 0) { | |
2245 | memset((crypt_stat->key + 24), 0, 8); | |
2246 | auth_tok->session_key.encrypted_key_size = 32; | |
f4aad16a MH |
2247 | } else |
2248 | auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; | |
dddfa461 | 2249 | key_rec->enc_key_size = |
237fead6 | 2250 | auth_tok->session_key.encrypted_key_size; |
f4aad16a MH |
2251 | encrypted_session_key_valid = 0; |
2252 | for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) | |
2253 | encrypted_session_key_valid |= | |
2254 | auth_tok->session_key.encrypted_key[i]; | |
2255 | if (encrypted_session_key_valid) { | |
2256 | ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " | |
2257 | "using auth_tok->session_key.encrypted_key, " | |
f24b3887 | 2258 | "where key_rec->enc_key_size = [%zd]\n", |
f4aad16a MH |
2259 | key_rec->enc_key_size); |
2260 | memcpy(key_rec->enc_key, | |
2261 | auth_tok->session_key.encrypted_key, | |
2262 | key_rec->enc_key_size); | |
2263 | goto encrypted_session_key_set; | |
2264 | } | |
dddfa461 MH |
2265 | if (auth_tok->token.password.flags & |
2266 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { | |
237fead6 MH |
2267 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " |
2268 | "session key encryption key of size [%d]\n", | |
2269 | auth_tok->token.password. | |
2270 | session_key_encryption_key_bytes); | |
2271 | memcpy(session_key_encryption_key, | |
2272 | auth_tok->token.password.session_key_encryption_key, | |
2273 | crypt_stat->key_size); | |
2274 | ecryptfs_printk(KERN_DEBUG, | |
df2e301f | 2275 | "Cached session key encryption key:\n"); |
237fead6 MH |
2276 | if (ecryptfs_verbosity > 0) |
2277 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
2278 | } | |
2279 | if (unlikely(ecryptfs_verbosity > 0)) { | |
2280 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); | |
2281 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
2282 | } | |
5dda6992 | 2283 | rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, |
ac97b9f9 MH |
2284 | src_sg, 2); |
2285 | if (rc < 1 || rc > 2) { | |
237fead6 | 2286 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a | 2287 | "for crypt_stat session key; expected rc = 1; " |
f24b3887 | 2288 | "got rc = [%d]. key_rec->enc_key_size = [%zd]\n", |
f4aad16a | 2289 | rc, key_rec->enc_key_size); |
237fead6 MH |
2290 | rc = -ENOMEM; |
2291 | goto out; | |
2292 | } | |
5dda6992 | 2293 | rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, |
ac97b9f9 MH |
2294 | dst_sg, 2); |
2295 | if (rc < 1 || rc > 2) { | |
237fead6 | 2296 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a MH |
2297 | "for crypt_stat encrypted session key; " |
2298 | "expected rc = 1; got rc = [%d]. " | |
f24b3887 | 2299 | "key_rec->enc_key_size = [%zd]\n", rc, |
f4aad16a | 2300 | key_rec->enc_key_size); |
237fead6 MH |
2301 | rc = -ENOMEM; |
2302 | goto out; | |
2303 | } | |
f4aad16a | 2304 | mutex_lock(tfm_mutex); |
3095e8e3 HX |
2305 | rc = crypto_skcipher_setkey(tfm, session_key_encryption_key, |
2306 | crypt_stat->key_size); | |
237fead6 | 2307 | if (rc < 0) { |
f4aad16a | 2308 | mutex_unlock(tfm_mutex); |
237fead6 | 2309 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " |
8bba066f | 2310 | "context; rc = [%d]\n", rc); |
237fead6 MH |
2311 | goto out; |
2312 | } | |
3095e8e3 HX |
2313 | |
2314 | req = skcipher_request_alloc(tfm, GFP_KERNEL); | |
2315 | if (!req) { | |
2316 | mutex_unlock(tfm_mutex); | |
2317 | ecryptfs_printk(KERN_ERR, "Out of kernel memory whilst " | |
2318 | "attempting to skcipher_request_alloc for " | |
2319 | "%s\n", crypto_skcipher_driver_name(tfm)); | |
2320 | rc = -ENOMEM; | |
2321 | goto out; | |
2322 | } | |
2323 | ||
2324 | skcipher_request_set_callback(req, CRYPTO_TFM_REQ_MAY_SLEEP, | |
2325 | NULL, NULL); | |
2326 | ||
237fead6 | 2327 | rc = 0; |
f24b3887 | 2328 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%zd] bytes of the key\n", |
237fead6 | 2329 | crypt_stat->key_size); |
3095e8e3 HX |
2330 | skcipher_request_set_crypt(req, src_sg, dst_sg, |
2331 | (*key_rec).enc_key_size, NULL); | |
2332 | rc = crypto_skcipher_encrypt(req); | |
f4aad16a | 2333 | mutex_unlock(tfm_mutex); |
3095e8e3 | 2334 | skcipher_request_free(req); |
8bba066f MH |
2335 | if (rc) { |
2336 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); | |
2337 | goto out; | |
2338 | } | |
237fead6 | 2339 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); |
f4aad16a | 2340 | if (ecryptfs_verbosity > 0) { |
f24b3887 | 2341 | ecryptfs_printk(KERN_DEBUG, "EFEK of size [%zd]:\n", |
f4aad16a | 2342 | key_rec->enc_key_size); |
dddfa461 MH |
2343 | ecryptfs_dump_hex(key_rec->enc_key, |
2344 | key_rec->enc_key_size); | |
237fead6 | 2345 | } |
f4aad16a MH |
2346 | encrypted_session_key_set: |
2347 | /* This format is inspired by OpenPGP; see RFC 2440 | |
2348 | * packet tag 3 */ | |
2349 | max_packet_size = (1 /* Tag 3 identifier */ | |
2350 | + 3 /* Max Tag 3 packet size */ | |
2351 | + 1 /* Version */ | |
2352 | + 1 /* Cipher code */ | |
2353 | + 1 /* S2K specifier */ | |
2354 | + 1 /* Hash identifier */ | |
2355 | + ECRYPTFS_SALT_SIZE /* Salt */ | |
2356 | + 1 /* Hash iterations */ | |
2357 | + key_rec->enc_key_size); /* Encrypted key size */ | |
2358 | if (max_packet_size > (*remaining_bytes)) { | |
81acbcd6 AM |
2359 | printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " |
2360 | "there are only [%td] available\n", max_packet_size, | |
f4aad16a | 2361 | (*remaining_bytes)); |
237fead6 MH |
2362 | rc = -EINVAL; |
2363 | goto out; | |
2364 | } | |
237fead6 | 2365 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; |
f4aad16a MH |
2366 | /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) |
2367 | * to get the number of octets in the actual Tag 3 packet */ | |
f66e883e MH |
2368 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
2369 | (max_packet_size - 4), | |
2370 | &packet_size_length); | |
237fead6 | 2371 | if (rc) { |
f4aad16a MH |
2372 | printk(KERN_ERR "Error generating tag 3 packet header; cannot " |
2373 | "generate packet length. rc = [%d]\n", rc); | |
237fead6 MH |
2374 | goto out; |
2375 | } | |
2376 | (*packet_size) += packet_size_length; | |
2377 | dest[(*packet_size)++] = 0x04; /* version 4 */ | |
f4aad16a MH |
2378 | /* TODO: Break from RFC2440 so that arbitrary ciphers can be |
2379 | * specified with strings */ | |
9c79f34f MH |
2380 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat->cipher, |
2381 | crypt_stat->key_size); | |
237fead6 MH |
2382 | if (cipher_code == 0) { |
2383 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " | |
2384 | "cipher [%s]\n", crypt_stat->cipher); | |
2385 | rc = -EINVAL; | |
2386 | goto out; | |
2387 | } | |
2388 | dest[(*packet_size)++] = cipher_code; | |
2389 | dest[(*packet_size)++] = 0x03; /* S2K */ | |
2390 | dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ | |
2391 | memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, | |
2392 | ECRYPTFS_SALT_SIZE); | |
2393 | (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ | |
2394 | dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ | |
dddfa461 MH |
2395 | memcpy(&dest[(*packet_size)], key_rec->enc_key, |
2396 | key_rec->enc_key_size); | |
2397 | (*packet_size) += key_rec->enc_key_size; | |
237fead6 | 2398 | out: |
237fead6 MH |
2399 | if (rc) |
2400 | (*packet_size) = 0; | |
f4aad16a MH |
2401 | else |
2402 | (*remaining_bytes) -= (*packet_size); | |
237fead6 MH |
2403 | return rc; |
2404 | } | |
2405 | ||
eb95e7ff MH |
2406 | struct kmem_cache *ecryptfs_key_record_cache; |
2407 | ||
237fead6 MH |
2408 | /** |
2409 | * ecryptfs_generate_key_packet_set | |
22e78faf | 2410 | * @dest_base: Virtual address from which to write the key record set |
237fead6 MH |
2411 | * @crypt_stat: The cryptographic context from which the |
2412 | * authentication tokens will be retrieved | |
2413 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat | |
2414 | * for the global parameters | |
2415 | * @len: The amount written | |
2416 | * @max: The maximum amount of data allowed to be written | |
2417 | * | |
2418 | * Generates a key packet set and writes it to the virtual address | |
2419 | * passed in. | |
2420 | * | |
2421 | * Returns zero on success; non-zero on error. | |
2422 | */ | |
2423 | int | |
2424 | ecryptfs_generate_key_packet_set(char *dest_base, | |
2425 | struct ecryptfs_crypt_stat *crypt_stat, | |
2426 | struct dentry *ecryptfs_dentry, size_t *len, | |
2427 | size_t max) | |
2428 | { | |
237fead6 | 2429 | struct ecryptfs_auth_tok *auth_tok; |
0e1fc5ef | 2430 | struct key *auth_tok_key = NULL; |
237fead6 MH |
2431 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
2432 | &ecryptfs_superblock_to_private( | |
2433 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | |
2434 | size_t written; | |
eb95e7ff | 2435 | struct ecryptfs_key_record *key_rec; |
f4aad16a | 2436 | struct ecryptfs_key_sig *key_sig; |
dddfa461 | 2437 | int rc = 0; |
237fead6 MH |
2438 | |
2439 | (*len) = 0; | |
f4aad16a | 2440 | mutex_lock(&crypt_stat->keysig_list_mutex); |
eb95e7ff MH |
2441 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); |
2442 | if (!key_rec) { | |
2443 | rc = -ENOMEM; | |
2444 | goto out; | |
2445 | } | |
f4aad16a MH |
2446 | list_for_each_entry(key_sig, &crypt_stat->keysig_list, |
2447 | crypt_stat_list) { | |
2448 | memset(key_rec, 0, sizeof(*key_rec)); | |
0e1fc5ef RS |
2449 | rc = ecryptfs_find_global_auth_tok_for_sig(&auth_tok_key, |
2450 | &auth_tok, | |
f4aad16a MH |
2451 | mount_crypt_stat, |
2452 | key_sig->keysig); | |
2453 | if (rc) { | |
0e1fc5ef RS |
2454 | printk(KERN_WARNING "Unable to retrieve auth tok with " |
2455 | "sig = [%s]\n", key_sig->keysig); | |
2456 | rc = process_find_global_auth_tok_for_sig_err(rc); | |
f4aad16a MH |
2457 | goto out_free; |
2458 | } | |
237fead6 MH |
2459 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { |
2460 | rc = write_tag_3_packet((dest_base + (*len)), | |
f4aad16a | 2461 | &max, auth_tok, |
eb95e7ff | 2462 | crypt_stat, key_rec, |
237fead6 | 2463 | &written); |
b2987a5e TH |
2464 | up_write(&(auth_tok_key->sem)); |
2465 | key_put(auth_tok_key); | |
237fead6 MH |
2466 | if (rc) { |
2467 | ecryptfs_printk(KERN_WARNING, "Error " | |
2468 | "writing tag 3 packet\n"); | |
eb95e7ff | 2469 | goto out_free; |
237fead6 MH |
2470 | } |
2471 | (*len) += written; | |
2472 | /* Write auth tok signature packet */ | |
f4aad16a MH |
2473 | rc = write_tag_11_packet((dest_base + (*len)), &max, |
2474 | key_rec->sig, | |
2475 | ECRYPTFS_SIG_SIZE, &written); | |
237fead6 MH |
2476 | if (rc) { |
2477 | ecryptfs_printk(KERN_ERR, "Error writing " | |
2478 | "auth tok signature packet\n"); | |
eb95e7ff | 2479 | goto out_free; |
237fead6 MH |
2480 | } |
2481 | (*len) += written; | |
dddfa461 | 2482 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
b2987a5e TH |
2483 | rc = write_tag_1_packet(dest_base + (*len), &max, |
2484 | auth_tok_key, auth_tok, | |
f4aad16a | 2485 | crypt_stat, key_rec, &written); |
dddfa461 MH |
2486 | if (rc) { |
2487 | ecryptfs_printk(KERN_WARNING, "Error " | |
2488 | "writing tag 1 packet\n"); | |
eb95e7ff | 2489 | goto out_free; |
dddfa461 MH |
2490 | } |
2491 | (*len) += written; | |
237fead6 | 2492 | } else { |
b2987a5e TH |
2493 | up_write(&(auth_tok_key->sem)); |
2494 | key_put(auth_tok_key); | |
237fead6 MH |
2495 | ecryptfs_printk(KERN_WARNING, "Unsupported " |
2496 | "authentication token type\n"); | |
2497 | rc = -EINVAL; | |
eb95e7ff | 2498 | goto out_free; |
237fead6 | 2499 | } |
f4aad16a MH |
2500 | } |
2501 | if (likely(max > 0)) { | |
237fead6 MH |
2502 | dest_base[(*len)] = 0x00; |
2503 | } else { | |
2504 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); | |
2505 | rc = -EIO; | |
2506 | } | |
eb95e7ff MH |
2507 | out_free: |
2508 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); | |
237fead6 MH |
2509 | out: |
2510 | if (rc) | |
2511 | (*len) = 0; | |
f4aad16a MH |
2512 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
2513 | return rc; | |
2514 | } | |
2515 | ||
2516 | struct kmem_cache *ecryptfs_key_sig_cache; | |
2517 | ||
2518 | int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) | |
2519 | { | |
2520 | struct ecryptfs_key_sig *new_key_sig; | |
f4aad16a MH |
2521 | |
2522 | new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); | |
2523 | if (!new_key_sig) { | |
f4aad16a MH |
2524 | printk(KERN_ERR |
2525 | "Error allocating from ecryptfs_key_sig_cache\n"); | |
aa06117f | 2526 | return -ENOMEM; |
f4aad16a MH |
2527 | } |
2528 | memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
7762e230 | 2529 | new_key_sig->keysig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; |
aa06117f | 2530 | /* Caller must hold keysig_list_mutex */ |
f4aad16a | 2531 | list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); |
aa06117f RD |
2532 | |
2533 | return 0; | |
237fead6 | 2534 | } |
f4aad16a MH |
2535 | |
2536 | struct kmem_cache *ecryptfs_global_auth_tok_cache; | |
2537 | ||
2538 | int | |
2539 | ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
84814d64 | 2540 | char *sig, u32 global_auth_tok_flags) |
f4aad16a MH |
2541 | { |
2542 | struct ecryptfs_global_auth_tok *new_auth_tok; | |
2543 | int rc = 0; | |
2544 | ||
459e2164 | 2545 | new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, |
f4aad16a MH |
2546 | GFP_KERNEL); |
2547 | if (!new_auth_tok) { | |
2548 | rc = -ENOMEM; | |
2549 | printk(KERN_ERR "Error allocating from " | |
2550 | "ecryptfs_global_auth_tok_cache\n"); | |
2551 | goto out; | |
2552 | } | |
2553 | memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
84814d64 | 2554 | new_auth_tok->flags = global_auth_tok_flags; |
f4aad16a MH |
2555 | new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; |
2556 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
2557 | list_add(&new_auth_tok->mount_crypt_stat_list, | |
2558 | &mount_crypt_stat->global_auth_tok_list); | |
f4aad16a MH |
2559 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
2560 | out: | |
2561 | return rc; | |
2562 | } | |
2563 |