Commit | Line | Data |
---|---|---|
237fead6 MH |
1 | /** |
2 | * eCryptfs: Linux filesystem encryption layer | |
3 | * In-kernel key management code. Includes functions to parse and | |
4 | * write authentication token-related packets with the underlying | |
5 | * file. | |
6 | * | |
7 | * Copyright (C) 2004-2006 International Business Machines Corp. | |
8 | * Author(s): Michael A. Halcrow <mhalcrow@us.ibm.com> | |
9 | * Michael C. Thompson <mcthomps@us.ibm.com> | |
dddfa461 | 10 | * Trevor S. Highland <trevor.highland@gmail.com> |
237fead6 MH |
11 | * |
12 | * This program is free software; you can redistribute it and/or | |
13 | * modify it under the terms of the GNU General Public License as | |
14 | * published by the Free Software Foundation; either version 2 of the | |
15 | * License, or (at your option) any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, but | |
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
20 | * General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; if not, write to the Free Software | |
24 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
25 | * 02111-1307, USA. | |
26 | */ | |
27 | ||
28 | #include <linux/string.h> | |
237fead6 MH |
29 | #include <linux/syscalls.h> |
30 | #include <linux/pagemap.h> | |
31 | #include <linux/key.h> | |
32 | #include <linux/random.h> | |
33 | #include <linux/crypto.h> | |
34 | #include <linux/scatterlist.h> | |
35 | #include "ecryptfs_kernel.h" | |
36 | ||
37 | /** | |
38 | * request_key returned an error instead of a valid key address; | |
39 | * determine the type of error, make appropriate log entries, and | |
40 | * return an error code. | |
41 | */ | |
cd9d67df | 42 | static int process_request_key_err(long err_code) |
237fead6 MH |
43 | { |
44 | int rc = 0; | |
45 | ||
46 | switch (err_code) { | |
982363c9 | 47 | case -ENOKEY: |
237fead6 MH |
48 | ecryptfs_printk(KERN_WARNING, "No key\n"); |
49 | rc = -ENOENT; | |
50 | break; | |
982363c9 | 51 | case -EKEYEXPIRED: |
237fead6 MH |
52 | ecryptfs_printk(KERN_WARNING, "Key expired\n"); |
53 | rc = -ETIME; | |
54 | break; | |
982363c9 | 55 | case -EKEYREVOKED: |
237fead6 MH |
56 | ecryptfs_printk(KERN_WARNING, "Key revoked\n"); |
57 | rc = -EINVAL; | |
58 | break; | |
59 | default: | |
60 | ecryptfs_printk(KERN_WARNING, "Unknown error code: " | |
61 | "[0x%.16x]\n", err_code); | |
62 | rc = -EINVAL; | |
63 | } | |
64 | return rc; | |
65 | } | |
66 | ||
237fead6 | 67 | /** |
f66e883e | 68 | * ecryptfs_parse_packet_length |
237fead6 MH |
69 | * @data: Pointer to memory containing length at offset |
70 | * @size: This function writes the decoded size to this memory | |
71 | * address; zero on error | |
72 | * @length_size: The number of bytes occupied by the encoded length | |
73 | * | |
22e78faf | 74 | * Returns zero on success; non-zero on error |
237fead6 | 75 | */ |
f66e883e MH |
76 | int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, |
77 | size_t *length_size) | |
237fead6 MH |
78 | { |
79 | int rc = 0; | |
80 | ||
81 | (*length_size) = 0; | |
82 | (*size) = 0; | |
83 | if (data[0] < 192) { | |
84 | /* One-byte length */ | |
dddfa461 | 85 | (*size) = (unsigned char)data[0]; |
237fead6 MH |
86 | (*length_size) = 1; |
87 | } else if (data[0] < 224) { | |
88 | /* Two-byte length */ | |
dddfa461 MH |
89 | (*size) = (((unsigned char)(data[0]) - 192) * 256); |
90 | (*size) += ((unsigned char)(data[1]) + 192); | |
237fead6 MH |
91 | (*length_size) = 2; |
92 | } else if (data[0] == 255) { | |
93 | /* Five-byte length; we're not supposed to see this */ | |
94 | ecryptfs_printk(KERN_ERR, "Five-byte packet length not " | |
95 | "supported\n"); | |
96 | rc = -EINVAL; | |
97 | goto out; | |
98 | } else { | |
99 | ecryptfs_printk(KERN_ERR, "Error parsing packet length\n"); | |
100 | rc = -EINVAL; | |
101 | goto out; | |
102 | } | |
103 | out: | |
104 | return rc; | |
105 | } | |
106 | ||
107 | /** | |
f66e883e | 108 | * ecryptfs_write_packet_length |
22e78faf MH |
109 | * @dest: The byte array target into which to write the length. Must |
110 | * have at least 5 bytes allocated. | |
237fead6 | 111 | * @size: The length to write. |
22e78faf MH |
112 | * @packet_size_length: The number of bytes used to encode the packet |
113 | * length is written to this address. | |
237fead6 MH |
114 | * |
115 | * Returns zero on success; non-zero on error. | |
116 | */ | |
f66e883e MH |
117 | int ecryptfs_write_packet_length(char *dest, size_t size, |
118 | size_t *packet_size_length) | |
237fead6 MH |
119 | { |
120 | int rc = 0; | |
121 | ||
122 | if (size < 192) { | |
123 | dest[0] = size; | |
124 | (*packet_size_length) = 1; | |
125 | } else if (size < 65536) { | |
126 | dest[0] = (((size - 192) / 256) + 192); | |
127 | dest[1] = ((size - 192) % 256); | |
128 | (*packet_size_length) = 2; | |
129 | } else { | |
130 | rc = -EINVAL; | |
131 | ecryptfs_printk(KERN_WARNING, | |
132 | "Unsupported packet size: [%d]\n", size); | |
133 | } | |
134 | return rc; | |
135 | } | |
136 | ||
dddfa461 MH |
137 | static int |
138 | write_tag_64_packet(char *signature, struct ecryptfs_session_key *session_key, | |
139 | char **packet, size_t *packet_len) | |
140 | { | |
141 | size_t i = 0; | |
142 | size_t data_len; | |
143 | size_t packet_size_len; | |
144 | char *message; | |
145 | int rc; | |
146 | ||
147 | /* | |
148 | * ***** TAG 64 Packet Format ***** | |
149 | * | Content Type | 1 byte | | |
150 | * | Key Identifier Size | 1 or 2 bytes | | |
151 | * | Key Identifier | arbitrary | | |
152 | * | Encrypted File Encryption Key Size | 1 or 2 bytes | | |
153 | * | Encrypted File Encryption Key | arbitrary | | |
154 | */ | |
155 | data_len = (5 + ECRYPTFS_SIG_SIZE_HEX | |
156 | + session_key->encrypted_key_size); | |
157 | *packet = kmalloc(data_len, GFP_KERNEL); | |
158 | message = *packet; | |
159 | if (!message) { | |
160 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | |
161 | rc = -ENOMEM; | |
162 | goto out; | |
163 | } | |
164 | message[i++] = ECRYPTFS_TAG_64_PACKET_TYPE; | |
f66e883e MH |
165 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
166 | &packet_size_len); | |
dddfa461 MH |
167 | if (rc) { |
168 | ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " | |
169 | "header; cannot generate packet length\n"); | |
170 | goto out; | |
171 | } | |
172 | i += packet_size_len; | |
173 | memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); | |
174 | i += ECRYPTFS_SIG_SIZE_HEX; | |
f66e883e MH |
175 | rc = ecryptfs_write_packet_length(&message[i], |
176 | session_key->encrypted_key_size, | |
177 | &packet_size_len); | |
dddfa461 MH |
178 | if (rc) { |
179 | ecryptfs_printk(KERN_ERR, "Error generating tag 64 packet " | |
180 | "header; cannot generate packet length\n"); | |
181 | goto out; | |
182 | } | |
183 | i += packet_size_len; | |
184 | memcpy(&message[i], session_key->encrypted_key, | |
185 | session_key->encrypted_key_size); | |
186 | i += session_key->encrypted_key_size; | |
187 | *packet_len = i; | |
188 | out: | |
189 | return rc; | |
190 | } | |
191 | ||
192 | static int | |
19e66a67 | 193 | parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, |
dddfa461 MH |
194 | struct ecryptfs_message *msg) |
195 | { | |
196 | size_t i = 0; | |
197 | char *data; | |
198 | size_t data_len; | |
199 | size_t m_size; | |
200 | size_t message_len; | |
201 | u16 checksum = 0; | |
202 | u16 expected_checksum = 0; | |
203 | int rc; | |
204 | ||
205 | /* | |
206 | * ***** TAG 65 Packet Format ***** | |
207 | * | Content Type | 1 byte | | |
208 | * | Status Indicator | 1 byte | | |
209 | * | File Encryption Key Size | 1 or 2 bytes | | |
210 | * | File Encryption Key | arbitrary | | |
211 | */ | |
212 | message_len = msg->data_len; | |
213 | data = msg->data; | |
214 | if (message_len < 4) { | |
215 | rc = -EIO; | |
216 | goto out; | |
217 | } | |
218 | if (data[i++] != ECRYPTFS_TAG_65_PACKET_TYPE) { | |
219 | ecryptfs_printk(KERN_ERR, "Type should be ECRYPTFS_TAG_65\n"); | |
220 | rc = -EIO; | |
221 | goto out; | |
222 | } | |
223 | if (data[i++]) { | |
224 | ecryptfs_printk(KERN_ERR, "Status indicator has non-zero value " | |
225 | "[%d]\n", data[i-1]); | |
226 | rc = -EIO; | |
227 | goto out; | |
228 | } | |
f66e883e | 229 | rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); |
dddfa461 MH |
230 | if (rc) { |
231 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | |
232 | "rc = [%d]\n", rc); | |
233 | goto out; | |
234 | } | |
235 | i += data_len; | |
236 | if (message_len < (i + m_size)) { | |
624ae528 TH |
237 | ecryptfs_printk(KERN_ERR, "The message received from ecryptfsd " |
238 | "is shorter than expected\n"); | |
dddfa461 MH |
239 | rc = -EIO; |
240 | goto out; | |
241 | } | |
242 | if (m_size < 3) { | |
243 | ecryptfs_printk(KERN_ERR, | |
244 | "The decrypted key is not long enough to " | |
245 | "include a cipher code and checksum\n"); | |
246 | rc = -EIO; | |
247 | goto out; | |
248 | } | |
249 | *cipher_code = data[i++]; | |
250 | /* The decrypted key includes 1 byte cipher code and 2 byte checksum */ | |
251 | session_key->decrypted_key_size = m_size - 3; | |
252 | if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) { | |
253 | ecryptfs_printk(KERN_ERR, "key_size [%d] larger than " | |
254 | "the maximum key size [%d]\n", | |
255 | session_key->decrypted_key_size, | |
256 | ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); | |
257 | rc = -EIO; | |
258 | goto out; | |
259 | } | |
260 | memcpy(session_key->decrypted_key, &data[i], | |
261 | session_key->decrypted_key_size); | |
262 | i += session_key->decrypted_key_size; | |
263 | expected_checksum += (unsigned char)(data[i++]) << 8; | |
264 | expected_checksum += (unsigned char)(data[i++]); | |
265 | for (i = 0; i < session_key->decrypted_key_size; i++) | |
266 | checksum += session_key->decrypted_key[i]; | |
267 | if (expected_checksum != checksum) { | |
268 | ecryptfs_printk(KERN_ERR, "Invalid checksum for file " | |
269 | "encryption key; expected [%x]; calculated " | |
270 | "[%x]\n", expected_checksum, checksum); | |
271 | rc = -EIO; | |
272 | } | |
273 | out: | |
274 | return rc; | |
275 | } | |
276 | ||
277 | ||
278 | static int | |
19e66a67 | 279 | write_tag_66_packet(char *signature, u8 cipher_code, |
dddfa461 MH |
280 | struct ecryptfs_crypt_stat *crypt_stat, char **packet, |
281 | size_t *packet_len) | |
282 | { | |
283 | size_t i = 0; | |
284 | size_t j; | |
285 | size_t data_len; | |
286 | size_t checksum = 0; | |
287 | size_t packet_size_len; | |
288 | char *message; | |
289 | int rc; | |
290 | ||
291 | /* | |
292 | * ***** TAG 66 Packet Format ***** | |
293 | * | Content Type | 1 byte | | |
294 | * | Key Identifier Size | 1 or 2 bytes | | |
295 | * | Key Identifier | arbitrary | | |
296 | * | File Encryption Key Size | 1 or 2 bytes | | |
297 | * | File Encryption Key | arbitrary | | |
298 | */ | |
299 | data_len = (5 + ECRYPTFS_SIG_SIZE_HEX + crypt_stat->key_size); | |
300 | *packet = kmalloc(data_len, GFP_KERNEL); | |
301 | message = *packet; | |
302 | if (!message) { | |
303 | ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n"); | |
304 | rc = -ENOMEM; | |
305 | goto out; | |
306 | } | |
307 | message[i++] = ECRYPTFS_TAG_66_PACKET_TYPE; | |
f66e883e MH |
308 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
309 | &packet_size_len); | |
dddfa461 MH |
310 | if (rc) { |
311 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " | |
312 | "header; cannot generate packet length\n"); | |
313 | goto out; | |
314 | } | |
315 | i += packet_size_len; | |
316 | memcpy(&message[i], signature, ECRYPTFS_SIG_SIZE_HEX); | |
317 | i += ECRYPTFS_SIG_SIZE_HEX; | |
318 | /* The encrypted key includes 1 byte cipher code and 2 byte checksum */ | |
f66e883e MH |
319 | rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, |
320 | &packet_size_len); | |
dddfa461 MH |
321 | if (rc) { |
322 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet " | |
323 | "header; cannot generate packet length\n"); | |
324 | goto out; | |
325 | } | |
326 | i += packet_size_len; | |
327 | message[i++] = cipher_code; | |
328 | memcpy(&message[i], crypt_stat->key, crypt_stat->key_size); | |
329 | i += crypt_stat->key_size; | |
330 | for (j = 0; j < crypt_stat->key_size; j++) | |
331 | checksum += crypt_stat->key[j]; | |
332 | message[i++] = (checksum / 256) % 256; | |
333 | message[i++] = (checksum % 256); | |
334 | *packet_len = i; | |
335 | out: | |
336 | return rc; | |
337 | } | |
338 | ||
339 | static int | |
340 | parse_tag_67_packet(struct ecryptfs_key_record *key_rec, | |
341 | struct ecryptfs_message *msg) | |
342 | { | |
343 | size_t i = 0; | |
344 | char *data; | |
345 | size_t data_len; | |
346 | size_t message_len; | |
347 | int rc; | |
348 | ||
349 | /* | |
350 | * ***** TAG 65 Packet Format ***** | |
351 | * | Content Type | 1 byte | | |
352 | * | Status Indicator | 1 byte | | |
353 | * | Encrypted File Encryption Key Size | 1 or 2 bytes | | |
354 | * | Encrypted File Encryption Key | arbitrary | | |
355 | */ | |
356 | message_len = msg->data_len; | |
357 | data = msg->data; | |
358 | /* verify that everything through the encrypted FEK size is present */ | |
359 | if (message_len < 4) { | |
360 | rc = -EIO; | |
f66e883e MH |
361 | printk(KERN_ERR "%s: message_len is [%Zd]; minimum acceptable " |
362 | "message length is [%d]\n", __func__, message_len, 4); | |
dddfa461 MH |
363 | goto out; |
364 | } | |
365 | if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { | |
dddfa461 | 366 | rc = -EIO; |
f66e883e MH |
367 | printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", |
368 | __func__); | |
dddfa461 MH |
369 | goto out; |
370 | } | |
371 | if (data[i++]) { | |
dddfa461 | 372 | rc = -EIO; |
f66e883e MH |
373 | printk(KERN_ERR "%s: Status indicator has non zero " |
374 | "value [%d]\n", __func__, data[i-1]); | |
375 | ||
dddfa461 MH |
376 | goto out; |
377 | } | |
f66e883e MH |
378 | rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, |
379 | &data_len); | |
dddfa461 MH |
380 | if (rc) { |
381 | ecryptfs_printk(KERN_WARNING, "Error parsing packet length; " | |
382 | "rc = [%d]\n", rc); | |
383 | goto out; | |
384 | } | |
385 | i += data_len; | |
386 | if (message_len < (i + key_rec->enc_key_size)) { | |
dddfa461 | 387 | rc = -EIO; |
f66e883e MH |
388 | printk(KERN_ERR "%s: message_len [%Zd]; max len is [%Zd]\n", |
389 | __func__, message_len, (i + key_rec->enc_key_size)); | |
dddfa461 MH |
390 | goto out; |
391 | } | |
392 | if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
dddfa461 | 393 | rc = -EIO; |
f66e883e MH |
394 | printk(KERN_ERR "%s: Encrypted key_size [%Zd] larger than " |
395 | "the maximum key size [%d]\n", __func__, | |
396 | key_rec->enc_key_size, | |
397 | ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES); | |
dddfa461 MH |
398 | goto out; |
399 | } | |
400 | memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); | |
401 | out: | |
402 | return rc; | |
403 | } | |
404 | ||
cd9d67df MH |
405 | static int |
406 | ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok) | |
407 | { | |
408 | int rc = 0; | |
409 | ||
410 | (*sig) = NULL; | |
411 | switch (auth_tok->token_type) { | |
412 | case ECRYPTFS_PASSWORD: | |
413 | (*sig) = auth_tok->token.password.signature; | |
414 | break; | |
415 | case ECRYPTFS_PRIVATE_KEY: | |
416 | (*sig) = auth_tok->token.private_key.signature; | |
417 | break; | |
418 | default: | |
419 | printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n", | |
420 | auth_tok->token_type); | |
421 | rc = -EINVAL; | |
422 | } | |
423 | return rc; | |
424 | } | |
425 | ||
dddfa461 | 426 | /** |
22e78faf MH |
427 | * decrypt_pki_encrypted_session_key - Decrypt the session key with the given auth_tok. |
428 | * @auth_tok: The key authentication token used to decrypt the session key | |
429 | * @crypt_stat: The cryptographic context | |
dddfa461 | 430 | * |
22e78faf | 431 | * Returns zero on success; non-zero error otherwise. |
dddfa461 | 432 | */ |
f4aad16a MH |
433 | static int |
434 | decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
435 | struct ecryptfs_crypt_stat *crypt_stat) | |
dddfa461 | 436 | { |
19e66a67 | 437 | u8 cipher_code = 0; |
dddfa461 MH |
438 | struct ecryptfs_msg_ctx *msg_ctx; |
439 | struct ecryptfs_message *msg = NULL; | |
f4aad16a | 440 | char *auth_tok_sig; |
624ae528 TH |
441 | char *payload; |
442 | size_t payload_len; | |
dddfa461 MH |
443 | int rc; |
444 | ||
5dda6992 MH |
445 | rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); |
446 | if (rc) { | |
f4aad16a MH |
447 | printk(KERN_ERR "Unrecognized auth tok type: [%d]\n", |
448 | auth_tok->token_type); | |
449 | goto out; | |
450 | } | |
451 | rc = write_tag_64_packet(auth_tok_sig, &(auth_tok->session_key), | |
624ae528 | 452 | &payload, &payload_len); |
dddfa461 | 453 | if (rc) { |
f66e883e | 454 | ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); |
dddfa461 MH |
455 | goto out; |
456 | } | |
624ae528 | 457 | rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); |
dddfa461 | 458 | if (rc) { |
624ae528 TH |
459 | ecryptfs_printk(KERN_ERR, "Error sending message to " |
460 | "ecryptfsd\n"); | |
dddfa461 MH |
461 | goto out; |
462 | } | |
463 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); | |
464 | if (rc) { | |
465 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 65 packet " | |
466 | "from the user space daemon\n"); | |
467 | rc = -EIO; | |
468 | goto out; | |
469 | } | |
470 | rc = parse_tag_65_packet(&(auth_tok->session_key), | |
471 | &cipher_code, msg); | |
472 | if (rc) { | |
473 | printk(KERN_ERR "Failed to parse tag 65 packet; rc = [%d]\n", | |
474 | rc); | |
475 | goto out; | |
476 | } | |
477 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
478 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | |
479 | auth_tok->session_key.decrypted_key_size); | |
480 | crypt_stat->key_size = auth_tok->session_key.decrypted_key_size; | |
481 | rc = ecryptfs_cipher_code_to_string(crypt_stat->cipher, cipher_code); | |
482 | if (rc) { | |
483 | ecryptfs_printk(KERN_ERR, "Cipher code [%d] is invalid\n", | |
484 | cipher_code) | |
485 | goto out; | |
486 | } | |
487 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; | |
488 | if (ecryptfs_verbosity > 0) { | |
489 | ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n"); | |
490 | ecryptfs_dump_hex(crypt_stat->key, | |
491 | crypt_stat->key_size); | |
492 | } | |
493 | out: | |
494 | if (msg) | |
495 | kfree(msg); | |
496 | return rc; | |
497 | } | |
498 | ||
499 | static void wipe_auth_tok_list(struct list_head *auth_tok_list_head) | |
500 | { | |
dddfa461 | 501 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
e0869cc1 | 502 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
dddfa461 | 503 | |
e0869cc1 MH |
504 | list_for_each_entry_safe(auth_tok_list_item, auth_tok_list_item_tmp, |
505 | auth_tok_list_head, list) { | |
506 | list_del(&auth_tok_list_item->list); | |
dddfa461 MH |
507 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, |
508 | auth_tok_list_item); | |
509 | } | |
dddfa461 MH |
510 | } |
511 | ||
512 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; | |
513 | ||
dddfa461 MH |
514 | /** |
515 | * parse_tag_1_packet | |
22e78faf | 516 | * @crypt_stat: The cryptographic context to modify based on packet contents |
dddfa461 MH |
517 | * @data: The raw bytes of the packet. |
518 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; | |
22e78faf MH |
519 | * a new authentication token will be placed at the |
520 | * end of this list for this packet. | |
dddfa461 MH |
521 | * @new_auth_tok: Pointer to a pointer to memory that this function |
522 | * allocates; sets the memory address of the pointer to | |
523 | * NULL on error. This object is added to the | |
524 | * auth_tok_list. | |
525 | * @packet_size: This function writes the size of the parsed packet | |
526 | * into this memory location; zero on error. | |
22e78faf | 527 | * @max_packet_size: The maximum allowable packet size |
dddfa461 MH |
528 | * |
529 | * Returns zero on success; non-zero on error. | |
530 | */ | |
531 | static int | |
532 | parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat, | |
533 | unsigned char *data, struct list_head *auth_tok_list, | |
534 | struct ecryptfs_auth_tok **new_auth_tok, | |
535 | size_t *packet_size, size_t max_packet_size) | |
536 | { | |
537 | size_t body_size; | |
538 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | |
539 | size_t length_size; | |
540 | int rc = 0; | |
541 | ||
542 | (*packet_size) = 0; | |
543 | (*new_auth_tok) = NULL; | |
13218179 MH |
544 | /** |
545 | * This format is inspired by OpenPGP; see RFC 2440 | |
546 | * packet tag 1 | |
547 | * | |
548 | * Tag 1 identifier (1 byte) | |
549 | * Max Tag 1 packet size (max 3 bytes) | |
550 | * Version (1 byte) | |
551 | * Key identifier (8 bytes; ECRYPTFS_SIG_SIZE) | |
552 | * Cipher identifier (1 byte) | |
553 | * Encrypted key size (arbitrary) | |
554 | * | |
555 | * 12 bytes minimum packet size | |
dddfa461 | 556 | */ |
13218179 MH |
557 | if (unlikely(max_packet_size < 12)) { |
558 | printk(KERN_ERR "Invalid max packet size; must be >=12\n"); | |
dddfa461 MH |
559 | rc = -EINVAL; |
560 | goto out; | |
561 | } | |
dddfa461 | 562 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { |
13218179 MH |
563 | printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", |
564 | ECRYPTFS_TAG_1_PACKET_TYPE); | |
dddfa461 MH |
565 | rc = -EINVAL; |
566 | goto out; | |
567 | } | |
568 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or | |
569 | * at end of function upon failure */ | |
570 | auth_tok_list_item = | |
13218179 MH |
571 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, |
572 | GFP_KERNEL); | |
dddfa461 | 573 | if (!auth_tok_list_item) { |
13218179 | 574 | printk(KERN_ERR "Unable to allocate memory\n"); |
dddfa461 MH |
575 | rc = -ENOMEM; |
576 | goto out; | |
577 | } | |
dddfa461 | 578 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
f66e883e MH |
579 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
580 | &length_size); | |
5dda6992 | 581 | if (rc) { |
13218179 MH |
582 | printk(KERN_WARNING "Error parsing packet length; " |
583 | "rc = [%d]\n", rc); | |
dddfa461 MH |
584 | goto out_free; |
585 | } | |
13218179 | 586 | if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { |
81acbcd6 | 587 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
dddfa461 MH |
588 | rc = -EINVAL; |
589 | goto out_free; | |
590 | } | |
591 | (*packet_size) += length_size; | |
592 | if (unlikely((*packet_size) + body_size > max_packet_size)) { | |
13218179 | 593 | printk(KERN_WARNING "Packet size exceeds max\n"); |
dddfa461 MH |
594 | rc = -EINVAL; |
595 | goto out_free; | |
596 | } | |
dddfa461 | 597 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
13218179 MH |
598 | printk(KERN_WARNING "Unknown version number [%d]\n", |
599 | data[(*packet_size) - 1]); | |
dddfa461 MH |
600 | rc = -EINVAL; |
601 | goto out_free; | |
602 | } | |
dddfa461 MH |
603 | ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature, |
604 | &data[(*packet_size)], ECRYPTFS_SIG_SIZE); | |
605 | *packet_size += ECRYPTFS_SIG_SIZE; | |
606 | /* This byte is skipped because the kernel does not need to | |
607 | * know which public key encryption algorithm was used */ | |
608 | (*packet_size)++; | |
609 | (*new_auth_tok)->session_key.encrypted_key_size = | |
13218179 | 610 | body_size - (ECRYPTFS_SIG_SIZE + 2); |
dddfa461 MH |
611 | if ((*new_auth_tok)->session_key.encrypted_key_size |
612 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { | |
13218179 MH |
613 | printk(KERN_WARNING "Tag 1 packet contains key larger " |
614 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); | |
dddfa461 MH |
615 | rc = -EINVAL; |
616 | goto out; | |
617 | } | |
dddfa461 | 618 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
13218179 | 619 | &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); |
dddfa461 MH |
620 | (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size; |
621 | (*new_auth_tok)->session_key.flags &= | |
622 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
623 | (*new_auth_tok)->session_key.flags |= | |
624 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | |
625 | (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY; | |
13218179 | 626 | (*new_auth_tok)->flags = 0; |
e2bd99ec MH |
627 | (*new_auth_tok)->session_key.flags &= |
628 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); | |
629 | (*new_auth_tok)->session_key.flags &= | |
630 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); | |
dddfa461 MH |
631 | list_add(&auth_tok_list_item->list, auth_tok_list); |
632 | goto out; | |
633 | out_free: | |
634 | (*new_auth_tok) = NULL; | |
635 | memset(auth_tok_list_item, 0, | |
636 | sizeof(struct ecryptfs_auth_tok_list_item)); | |
637 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, | |
638 | auth_tok_list_item); | |
639 | out: | |
640 | if (rc) | |
641 | (*packet_size) = 0; | |
642 | return rc; | |
643 | } | |
644 | ||
237fead6 MH |
645 | /** |
646 | * parse_tag_3_packet | |
647 | * @crypt_stat: The cryptographic context to modify based on packet | |
648 | * contents. | |
649 | * @data: The raw bytes of the packet. | |
650 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; | |
651 | * a new authentication token will be placed at the end | |
652 | * of this list for this packet. | |
653 | * @new_auth_tok: Pointer to a pointer to memory that this function | |
654 | * allocates; sets the memory address of the pointer to | |
655 | * NULL on error. This object is added to the | |
656 | * auth_tok_list. | |
657 | * @packet_size: This function writes the size of the parsed packet | |
658 | * into this memory location; zero on error. | |
659 | * @max_packet_size: maximum number of bytes to parse | |
660 | * | |
661 | * Returns zero on success; non-zero on error. | |
662 | */ | |
663 | static int | |
664 | parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat, | |
665 | unsigned char *data, struct list_head *auth_tok_list, | |
666 | struct ecryptfs_auth_tok **new_auth_tok, | |
667 | size_t *packet_size, size_t max_packet_size) | |
668 | { | |
237fead6 MH |
669 | size_t body_size; |
670 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; | |
671 | size_t length_size; | |
dddfa461 | 672 | int rc = 0; |
237fead6 MH |
673 | |
674 | (*packet_size) = 0; | |
675 | (*new_auth_tok) = NULL; | |
c59becfc MH |
676 | /** |
677 | *This format is inspired by OpenPGP; see RFC 2440 | |
678 | * packet tag 3 | |
679 | * | |
680 | * Tag 3 identifier (1 byte) | |
681 | * Max Tag 3 packet size (max 3 bytes) | |
682 | * Version (1 byte) | |
683 | * Cipher code (1 byte) | |
684 | * S2K specifier (1 byte) | |
685 | * Hash identifier (1 byte) | |
686 | * Salt (ECRYPTFS_SALT_SIZE) | |
687 | * Hash iterations (1 byte) | |
688 | * Encrypted key (arbitrary) | |
689 | * | |
690 | * (ECRYPTFS_SALT_SIZE + 7) minimum packet size | |
237fead6 | 691 | */ |
c59becfc MH |
692 | if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { |
693 | printk(KERN_ERR "Max packet size too large\n"); | |
237fead6 MH |
694 | rc = -EINVAL; |
695 | goto out; | |
696 | } | |
237fead6 | 697 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { |
c59becfc MH |
698 | printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", |
699 | ECRYPTFS_TAG_3_PACKET_TYPE); | |
237fead6 MH |
700 | rc = -EINVAL; |
701 | goto out; | |
702 | } | |
703 | /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or | |
704 | * at end of function upon failure */ | |
705 | auth_tok_list_item = | |
c3762229 | 706 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); |
237fead6 | 707 | if (!auth_tok_list_item) { |
c59becfc | 708 | printk(KERN_ERR "Unable to allocate memory\n"); |
237fead6 MH |
709 | rc = -ENOMEM; |
710 | goto out; | |
711 | } | |
237fead6 | 712 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
f66e883e MH |
713 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
714 | &length_size); | |
5dda6992 | 715 | if (rc) { |
c59becfc MH |
716 | printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", |
717 | rc); | |
237fead6 MH |
718 | goto out_free; |
719 | } | |
c59becfc | 720 | if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { |
81acbcd6 | 721 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
237fead6 MH |
722 | rc = -EINVAL; |
723 | goto out_free; | |
724 | } | |
725 | (*packet_size) += length_size; | |
237fead6 | 726 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
c59becfc | 727 | printk(KERN_ERR "Packet size exceeds max\n"); |
237fead6 MH |
728 | rc = -EINVAL; |
729 | goto out_free; | |
730 | } | |
237fead6 | 731 | (*new_auth_tok)->session_key.encrypted_key_size = |
c59becfc | 732 | (body_size - (ECRYPTFS_SALT_SIZE + 5)); |
237fead6 | 733 | if (unlikely(data[(*packet_size)++] != 0x04)) { |
c59becfc MH |
734 | printk(KERN_WARNING "Unknown version number [%d]\n", |
735 | data[(*packet_size) - 1]); | |
237fead6 MH |
736 | rc = -EINVAL; |
737 | goto out_free; | |
738 | } | |
237fead6 MH |
739 | ecryptfs_cipher_code_to_string(crypt_stat->cipher, |
740 | (u16)data[(*packet_size)]); | |
741 | /* A little extra work to differentiate among the AES key | |
742 | * sizes; see RFC2440 */ | |
743 | switch(data[(*packet_size)++]) { | |
744 | case RFC2440_CIPHER_AES_192: | |
745 | crypt_stat->key_size = 24; | |
746 | break; | |
747 | default: | |
748 | crypt_stat->key_size = | |
749 | (*new_auth_tok)->session_key.encrypted_key_size; | |
750 | } | |
751 | ecryptfs_init_crypt_ctx(crypt_stat); | |
237fead6 | 752 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
c59becfc | 753 | printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); |
237fead6 MH |
754 | rc = -ENOSYS; |
755 | goto out_free; | |
756 | } | |
237fead6 | 757 | /* TODO: finish the hash mapping */ |
237fead6 MH |
758 | switch (data[(*packet_size)++]) { |
759 | case 0x01: /* See RFC2440 for these numbers and their mappings */ | |
760 | /* Choose MD5 */ | |
237fead6 MH |
761 | memcpy((*new_auth_tok)->token.password.salt, |
762 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); | |
763 | (*packet_size) += ECRYPTFS_SALT_SIZE; | |
237fead6 | 764 | /* This conversion was taken straight from RFC2440 */ |
237fead6 MH |
765 | (*new_auth_tok)->token.password.hash_iterations = |
766 | ((u32) 16 + (data[(*packet_size)] & 15)) | |
767 | << ((data[(*packet_size)] >> 4) + 6); | |
768 | (*packet_size)++; | |
c59becfc MH |
769 | /* Friendly reminder: |
770 | * (*new_auth_tok)->session_key.encrypted_key_size = | |
771 | * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ | |
237fead6 MH |
772 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
773 | &data[(*packet_size)], | |
774 | (*new_auth_tok)->session_key.encrypted_key_size); | |
775 | (*packet_size) += | |
776 | (*new_auth_tok)->session_key.encrypted_key_size; | |
777 | (*new_auth_tok)->session_key.flags &= | |
778 | ~ECRYPTFS_CONTAINS_DECRYPTED_KEY; | |
779 | (*new_auth_tok)->session_key.flags |= | |
780 | ECRYPTFS_CONTAINS_ENCRYPTED_KEY; | |
c59becfc | 781 | (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ |
237fead6 MH |
782 | break; |
783 | default: | |
784 | ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: " | |
785 | "[%d]\n", data[(*packet_size) - 1]); | |
786 | rc = -ENOSYS; | |
787 | goto out_free; | |
788 | } | |
789 | (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD; | |
790 | /* TODO: Parametarize; we might actually want userspace to | |
791 | * decrypt the session key. */ | |
e2bd99ec MH |
792 | (*new_auth_tok)->session_key.flags &= |
793 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT); | |
794 | (*new_auth_tok)->session_key.flags &= | |
795 | ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT); | |
237fead6 MH |
796 | list_add(&auth_tok_list_item->list, auth_tok_list); |
797 | goto out; | |
798 | out_free: | |
799 | (*new_auth_tok) = NULL; | |
800 | memset(auth_tok_list_item, 0, | |
801 | sizeof(struct ecryptfs_auth_tok_list_item)); | |
802 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, | |
803 | auth_tok_list_item); | |
804 | out: | |
805 | if (rc) | |
806 | (*packet_size) = 0; | |
807 | return rc; | |
808 | } | |
809 | ||
810 | /** | |
811 | * parse_tag_11_packet | |
812 | * @data: The raw bytes of the packet | |
813 | * @contents: This function writes the data contents of the literal | |
814 | * packet into this memory location | |
815 | * @max_contents_bytes: The maximum number of bytes that this function | |
816 | * is allowed to write into contents | |
817 | * @tag_11_contents_size: This function writes the size of the parsed | |
818 | * contents into this memory location; zero on | |
819 | * error | |
820 | * @packet_size: This function writes the size of the parsed packet | |
821 | * into this memory location; zero on error | |
822 | * @max_packet_size: maximum number of bytes to parse | |
823 | * | |
824 | * Returns zero on success; non-zero on error. | |
825 | */ | |
826 | static int | |
827 | parse_tag_11_packet(unsigned char *data, unsigned char *contents, | |
828 | size_t max_contents_bytes, size_t *tag_11_contents_size, | |
829 | size_t *packet_size, size_t max_packet_size) | |
830 | { | |
237fead6 MH |
831 | size_t body_size; |
832 | size_t length_size; | |
dddfa461 | 833 | int rc = 0; |
237fead6 MH |
834 | |
835 | (*packet_size) = 0; | |
836 | (*tag_11_contents_size) = 0; | |
f648104a MH |
837 | /* This format is inspired by OpenPGP; see RFC 2440 |
838 | * packet tag 11 | |
839 | * | |
840 | * Tag 11 identifier (1 byte) | |
841 | * Max Tag 11 packet size (max 3 bytes) | |
842 | * Binary format specifier (1 byte) | |
843 | * Filename length (1 byte) | |
844 | * Filename ("_CONSOLE") (8 bytes) | |
845 | * Modification date (4 bytes) | |
846 | * Literal data (arbitrary) | |
847 | * | |
848 | * We need at least 16 bytes of data for the packet to even be | |
849 | * valid. | |
237fead6 | 850 | */ |
f648104a MH |
851 | if (max_packet_size < 16) { |
852 | printk(KERN_ERR "Maximum packet size too small\n"); | |
237fead6 MH |
853 | rc = -EINVAL; |
854 | goto out; | |
855 | } | |
237fead6 | 856 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { |
f648104a | 857 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
237fead6 MH |
858 | rc = -EINVAL; |
859 | goto out; | |
860 | } | |
f66e883e MH |
861 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
862 | &length_size); | |
5dda6992 | 863 | if (rc) { |
f648104a | 864 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
237fead6 MH |
865 | goto out; |
866 | } | |
f648104a | 867 | if (body_size < 14) { |
81acbcd6 | 868 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
237fead6 MH |
869 | rc = -EINVAL; |
870 | goto out; | |
871 | } | |
f648104a MH |
872 | (*packet_size) += length_size; |
873 | (*tag_11_contents_size) = (body_size - 14); | |
237fead6 | 874 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { |
f648104a | 875 | printk(KERN_ERR "Packet size exceeds max\n"); |
237fead6 MH |
876 | rc = -EINVAL; |
877 | goto out; | |
878 | } | |
237fead6 | 879 | if (data[(*packet_size)++] != 0x62) { |
f648104a | 880 | printk(KERN_WARNING "Unrecognizable packet\n"); |
237fead6 MH |
881 | rc = -EINVAL; |
882 | goto out; | |
883 | } | |
237fead6 | 884 | if (data[(*packet_size)++] != 0x08) { |
f648104a | 885 | printk(KERN_WARNING "Unrecognizable packet\n"); |
237fead6 MH |
886 | rc = -EINVAL; |
887 | goto out; | |
888 | } | |
f648104a | 889 | (*packet_size) += 12; /* Ignore filename and modification date */ |
237fead6 MH |
890 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); |
891 | (*packet_size) += (*tag_11_contents_size); | |
237fead6 MH |
892 | out: |
893 | if (rc) { | |
894 | (*packet_size) = 0; | |
895 | (*tag_11_contents_size) = 0; | |
896 | } | |
897 | return rc; | |
898 | } | |
899 | ||
f4aad16a MH |
900 | static int |
901 | ecryptfs_find_global_auth_tok_for_sig( | |
902 | struct ecryptfs_global_auth_tok **global_auth_tok, | |
903 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig) | |
904 | { | |
905 | struct ecryptfs_global_auth_tok *walker; | |
906 | int rc = 0; | |
907 | ||
908 | (*global_auth_tok) = NULL; | |
909 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
910 | list_for_each_entry(walker, | |
911 | &mount_crypt_stat->global_auth_tok_list, | |
912 | mount_crypt_stat_list) { | |
913 | if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) { | |
914 | (*global_auth_tok) = walker; | |
915 | goto out; | |
916 | } | |
917 | } | |
918 | rc = -EINVAL; | |
919 | out: | |
920 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
921 | return rc; | |
922 | } | |
923 | ||
237fead6 | 924 | /** |
f4aad16a MH |
925 | * ecryptfs_verify_version |
926 | * @version: The version number to confirm | |
927 | * | |
928 | * Returns zero on good version; non-zero otherwise | |
929 | */ | |
930 | static int ecryptfs_verify_version(u16 version) | |
931 | { | |
932 | int rc = 0; | |
933 | unsigned char major; | |
934 | unsigned char minor; | |
935 | ||
936 | major = ((version >> 8) & 0xFF); | |
937 | minor = (version & 0xFF); | |
938 | if (major != ECRYPTFS_VERSION_MAJOR) { | |
939 | ecryptfs_printk(KERN_ERR, "Major version number mismatch. " | |
940 | "Expected [%d]; got [%d]\n", | |
941 | ECRYPTFS_VERSION_MAJOR, major); | |
942 | rc = -EINVAL; | |
943 | goto out; | |
944 | } | |
945 | if (minor != ECRYPTFS_VERSION_MINOR) { | |
946 | ecryptfs_printk(KERN_ERR, "Minor version number mismatch. " | |
947 | "Expected [%d]; got [%d]\n", | |
948 | ECRYPTFS_VERSION_MINOR, minor); | |
949 | rc = -EINVAL; | |
950 | goto out; | |
951 | } | |
952 | out: | |
953 | return rc; | |
954 | } | |
955 | ||
956 | int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key, | |
957 | struct ecryptfs_auth_tok **auth_tok, | |
958 | char *sig) | |
959 | { | |
960 | int rc = 0; | |
961 | ||
962 | (*auth_tok_key) = request_key(&key_type_user, sig, NULL); | |
963 | if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) { | |
964 | printk(KERN_ERR "Could not find key with description: [%s]\n", | |
965 | sig); | |
982363c9 | 966 | rc = process_request_key_err(PTR_ERR(*auth_tok_key)); |
f4aad16a MH |
967 | goto out; |
968 | } | |
969 | (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); | |
970 | if (ecryptfs_verify_version((*auth_tok)->version)) { | |
971 | printk(KERN_ERR | |
972 | "Data structure version mismatch. " | |
973 | "Userspace tools must match eCryptfs " | |
974 | "kernel module with major version [%d] " | |
975 | "and minor version [%d]\n", | |
976 | ECRYPTFS_VERSION_MAJOR, | |
977 | ECRYPTFS_VERSION_MINOR); | |
978 | rc = -EINVAL; | |
979 | goto out; | |
980 | } | |
981 | if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD | |
982 | && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { | |
983 | printk(KERN_ERR "Invalid auth_tok structure " | |
984 | "returned from key query\n"); | |
985 | rc = -EINVAL; | |
986 | goto out; | |
987 | } | |
988 | out: | |
989 | return rc; | |
990 | } | |
991 | ||
992 | /** | |
993 | * ecryptfs_find_auth_tok_for_sig | |
994 | * @auth_tok: Set to the matching auth_tok; NULL if not found | |
995 | * @crypt_stat: inode crypt_stat crypto context | |
996 | * @sig: Sig of auth_tok to find | |
997 | * | |
998 | * For now, this function simply looks at the registered auth_tok's | |
999 | * linked off the mount_crypt_stat, so all the auth_toks that can be | |
1000 | * used must be registered at mount time. This function could | |
1001 | * potentially try a lot harder to find auth_tok's (e.g., by calling | |
1002 | * out to ecryptfsd to dynamically retrieve an auth_tok object) so | |
1003 | * that static registration of auth_tok's will no longer be necessary. | |
1004 | * | |
1005 | * Returns zero on no error; non-zero on error | |
1006 | */ | |
1007 | static int | |
1008 | ecryptfs_find_auth_tok_for_sig( | |
1009 | struct ecryptfs_auth_tok **auth_tok, | |
1010 | struct ecryptfs_crypt_stat *crypt_stat, char *sig) | |
1011 | { | |
1012 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | |
1013 | crypt_stat->mount_crypt_stat; | |
1014 | struct ecryptfs_global_auth_tok *global_auth_tok; | |
1015 | int rc = 0; | |
1016 | ||
1017 | (*auth_tok) = NULL; | |
1018 | if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | |
1019 | mount_crypt_stat, sig)) { | |
1020 | struct key *auth_tok_key; | |
1021 | ||
1022 | rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, | |
1023 | sig); | |
1024 | } else | |
1025 | (*auth_tok) = global_auth_tok->global_auth_tok; | |
1026 | return rc; | |
1027 | } | |
1028 | ||
1029 | /** | |
22e78faf MH |
1030 | * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. |
1031 | * @auth_tok: The passphrase authentication token to use to encrypt the FEK | |
1032 | * @crypt_stat: The cryptographic context | |
237fead6 | 1033 | * |
22e78faf | 1034 | * Returns zero on success; non-zero error otherwise |
237fead6 | 1035 | */ |
f4aad16a MH |
1036 | static int |
1037 | decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1038 | struct ecryptfs_crypt_stat *crypt_stat) | |
237fead6 | 1039 | { |
ac97b9f9 MH |
1040 | struct scatterlist dst_sg[2]; |
1041 | struct scatterlist src_sg[2]; | |
dd8e2902 | 1042 | struct mutex *tfm_mutex; |
8bba066f MH |
1043 | struct blkcipher_desc desc = { |
1044 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | |
1045 | }; | |
1046 | int rc = 0; | |
237fead6 | 1047 | |
f4aad16a MH |
1048 | if (unlikely(ecryptfs_verbosity > 0)) { |
1049 | ecryptfs_printk( | |
1050 | KERN_DEBUG, "Session key encryption key (size [%d]):\n", | |
1051 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1052 | ecryptfs_dump_hex( | |
1053 | auth_tok->token.password.session_key_encryption_key, | |
1054 | auth_tok->token.password.session_key_encryption_key_bytes); | |
1055 | } | |
1056 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, | |
1057 | crypt_stat->cipher); | |
1058 | if (unlikely(rc)) { | |
1059 | printk(KERN_ERR "Internal error whilst attempting to get " | |
1060 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
1061 | crypt_stat->cipher, rc); | |
1062 | goto out; | |
237fead6 | 1063 | } |
5dda6992 MH |
1064 | rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, |
1065 | auth_tok->session_key.encrypted_key_size, | |
ac97b9f9 MH |
1066 | src_sg, 2); |
1067 | if (rc < 1 || rc > 2) { | |
f4aad16a MH |
1068 | printk(KERN_ERR "Internal error whilst attempting to convert " |
1069 | "auth_tok->session_key.encrypted_key to scatterlist; " | |
1070 | "expected rc = 1; got rc = [%d]. " | |
1071 | "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, | |
1072 | auth_tok->session_key.encrypted_key_size); | |
1073 | goto out; | |
1074 | } | |
1075 | auth_tok->session_key.decrypted_key_size = | |
1076 | auth_tok->session_key.encrypted_key_size; | |
5dda6992 MH |
1077 | rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, |
1078 | auth_tok->session_key.decrypted_key_size, | |
ac97b9f9 MH |
1079 | dst_sg, 2); |
1080 | if (rc < 1 || rc > 2) { | |
f4aad16a MH |
1081 | printk(KERN_ERR "Internal error whilst attempting to convert " |
1082 | "auth_tok->session_key.decrypted_key to scatterlist; " | |
1083 | "expected rc = 1; got rc = [%d]\n", rc); | |
1084 | goto out; | |
1085 | } | |
1086 | mutex_lock(tfm_mutex); | |
1087 | rc = crypto_blkcipher_setkey( | |
1088 | desc.tfm, auth_tok->token.password.session_key_encryption_key, | |
1089 | crypt_stat->key_size); | |
1090 | if (unlikely(rc < 0)) { | |
1091 | mutex_unlock(tfm_mutex); | |
e5d9cbde MH |
1092 | printk(KERN_ERR "Error setting key for crypto context\n"); |
1093 | rc = -EINVAL; | |
f4aad16a | 1094 | goto out; |
237fead6 | 1095 | } |
ac97b9f9 | 1096 | rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg, |
8bba066f | 1097 | auth_tok->session_key.encrypted_key_size); |
f4aad16a MH |
1098 | mutex_unlock(tfm_mutex); |
1099 | if (unlikely(rc)) { | |
8bba066f | 1100 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); |
f4aad16a | 1101 | goto out; |
8bba066f | 1102 | } |
237fead6 MH |
1103 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
1104 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, | |
1105 | auth_tok->session_key.decrypted_key_size); | |
e2bd99ec | 1106 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
f4aad16a MH |
1107 | if (unlikely(ecryptfs_verbosity > 0)) { |
1108 | ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", | |
1109 | crypt_stat->key_size); | |
237fead6 MH |
1110 | ecryptfs_dump_hex(crypt_stat->key, |
1111 | crypt_stat->key_size); | |
f4aad16a | 1112 | } |
237fead6 MH |
1113 | out: |
1114 | return rc; | |
1115 | } | |
1116 | ||
1117 | /** | |
1118 | * ecryptfs_parse_packet_set | |
22e78faf MH |
1119 | * @crypt_stat: The cryptographic context |
1120 | * @src: Virtual address of region of memory containing the packets | |
1121 | * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set | |
237fead6 MH |
1122 | * |
1123 | * Get crypt_stat to have the file's session key if the requisite key | |
1124 | * is available to decrypt the session key. | |
1125 | * | |
1126 | * Returns Zero if a valid authentication token was retrieved and | |
1127 | * processed; negative value for file not encrypted or for error | |
1128 | * conditions. | |
1129 | */ | |
1130 | int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, | |
1131 | unsigned char *src, | |
1132 | struct dentry *ecryptfs_dentry) | |
1133 | { | |
1134 | size_t i = 0; | |
f4aad16a | 1135 | size_t found_auth_tok; |
237fead6 | 1136 | size_t next_packet_is_auth_tok_packet; |
237fead6 | 1137 | struct list_head auth_tok_list; |
dd8e2902 MH |
1138 | struct ecryptfs_auth_tok *matching_auth_tok; |
1139 | struct ecryptfs_auth_tok *candidate_auth_tok; | |
f4aad16a | 1140 | char *candidate_auth_tok_sig; |
237fead6 MH |
1141 | size_t packet_size; |
1142 | struct ecryptfs_auth_tok *new_auth_tok; | |
1143 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; | |
f4aad16a | 1144 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
237fead6 MH |
1145 | size_t tag_11_contents_size; |
1146 | size_t tag_11_packet_size; | |
dddfa461 | 1147 | int rc = 0; |
237fead6 MH |
1148 | |
1149 | INIT_LIST_HEAD(&auth_tok_list); | |
f4aad16a | 1150 | /* Parse the header to find as many packets as we can; these will be |
237fead6 MH |
1151 | * added the our &auth_tok_list */ |
1152 | next_packet_is_auth_tok_packet = 1; | |
1153 | while (next_packet_is_auth_tok_packet) { | |
1154 | size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); | |
1155 | ||
1156 | switch (src[i]) { | |
1157 | case ECRYPTFS_TAG_3_PACKET_TYPE: | |
1158 | rc = parse_tag_3_packet(crypt_stat, | |
1159 | (unsigned char *)&src[i], | |
1160 | &auth_tok_list, &new_auth_tok, | |
1161 | &packet_size, max_packet_size); | |
1162 | if (rc) { | |
1163 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1164 | "tag 3 packet\n"); | |
1165 | rc = -EIO; | |
1166 | goto out_wipe_list; | |
1167 | } | |
1168 | i += packet_size; | |
1169 | rc = parse_tag_11_packet((unsigned char *)&src[i], | |
1170 | sig_tmp_space, | |
1171 | ECRYPTFS_SIG_SIZE, | |
1172 | &tag_11_contents_size, | |
1173 | &tag_11_packet_size, | |
1174 | max_packet_size); | |
1175 | if (rc) { | |
1176 | ecryptfs_printk(KERN_ERR, "No valid " | |
1177 | "(ecryptfs-specific) literal " | |
1178 | "packet containing " | |
1179 | "authentication token " | |
1180 | "signature found after " | |
1181 | "tag 3 packet\n"); | |
1182 | rc = -EIO; | |
1183 | goto out_wipe_list; | |
1184 | } | |
1185 | i += tag_11_packet_size; | |
1186 | if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { | |
1187 | ecryptfs_printk(KERN_ERR, "Expected " | |
1188 | "signature of size [%d]; " | |
1189 | "read size [%d]\n", | |
1190 | ECRYPTFS_SIG_SIZE, | |
1191 | tag_11_contents_size); | |
1192 | rc = -EIO; | |
1193 | goto out_wipe_list; | |
1194 | } | |
1195 | ecryptfs_to_hex(new_auth_tok->token.password.signature, | |
1196 | sig_tmp_space, tag_11_contents_size); | |
1197 | new_auth_tok->token.password.signature[ | |
1198 | ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; | |
e2bd99ec | 1199 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
237fead6 | 1200 | break; |
dddfa461 MH |
1201 | case ECRYPTFS_TAG_1_PACKET_TYPE: |
1202 | rc = parse_tag_1_packet(crypt_stat, | |
1203 | (unsigned char *)&src[i], | |
1204 | &auth_tok_list, &new_auth_tok, | |
1205 | &packet_size, max_packet_size); | |
1206 | if (rc) { | |
1207 | ecryptfs_printk(KERN_ERR, "Error parsing " | |
1208 | "tag 1 packet\n"); | |
1209 | rc = -EIO; | |
1210 | goto out_wipe_list; | |
1211 | } | |
1212 | i += packet_size; | |
e2bd99ec | 1213 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
dddfa461 | 1214 | break; |
237fead6 MH |
1215 | case ECRYPTFS_TAG_11_PACKET_TYPE: |
1216 | ecryptfs_printk(KERN_WARNING, "Invalid packet set " | |
1217 | "(Tag 11 not allowed by itself)\n"); | |
1218 | rc = -EIO; | |
1219 | goto out_wipe_list; | |
1220 | break; | |
1221 | default: | |
1222 | ecryptfs_printk(KERN_DEBUG, "No packet at offset " | |
1223 | "[%d] of the file header; hex value of " | |
1224 | "character is [0x%.2x]\n", i, src[i]); | |
1225 | next_packet_is_auth_tok_packet = 0; | |
1226 | } | |
1227 | } | |
1228 | if (list_empty(&auth_tok_list)) { | |
f4aad16a MH |
1229 | printk(KERN_ERR "The lower file appears to be a non-encrypted " |
1230 | "eCryptfs file; this is not supported in this version " | |
1231 | "of the eCryptfs kernel module\n"); | |
1232 | rc = -EINVAL; | |
237fead6 MH |
1233 | goto out; |
1234 | } | |
f4aad16a MH |
1235 | /* auth_tok_list contains the set of authentication tokens |
1236 | * parsed from the metadata. We need to find a matching | |
1237 | * authentication token that has the secret component(s) | |
1238 | * necessary to decrypt the EFEK in the auth_tok parsed from | |
1239 | * the metadata. There may be several potential matches, but | |
1240 | * just one will be sufficient to decrypt to get the FEK. */ | |
1241 | find_next_matching_auth_tok: | |
1242 | found_auth_tok = 0; | |
1243 | list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { | |
237fead6 MH |
1244 | candidate_auth_tok = &auth_tok_list_item->auth_tok; |
1245 | if (unlikely(ecryptfs_verbosity > 0)) { | |
1246 | ecryptfs_printk(KERN_DEBUG, | |
1247 | "Considering cadidate auth tok:\n"); | |
1248 | ecryptfs_dump_auth_tok(candidate_auth_tok); | |
1249 | } | |
5dda6992 MH |
1250 | rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, |
1251 | candidate_auth_tok); | |
1252 | if (rc) { | |
f4aad16a MH |
1253 | printk(KERN_ERR |
1254 | "Unrecognized candidate auth tok type: [%d]\n", | |
1255 | candidate_auth_tok->token_type); | |
1256 | rc = -EINVAL; | |
1257 | goto out_wipe_list; | |
1258 | } | |
5dda6992 MH |
1259 | ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat, |
1260 | candidate_auth_tok_sig); | |
f4aad16a | 1261 | if (matching_auth_tok) { |
dddfa461 | 1262 | found_auth_tok = 1; |
f4aad16a | 1263 | goto found_matching_auth_tok; |
237fead6 MH |
1264 | } |
1265 | } | |
237fead6 | 1266 | if (!found_auth_tok) { |
f4aad16a MH |
1267 | ecryptfs_printk(KERN_ERR, "Could not find a usable " |
1268 | "authentication token\n"); | |
237fead6 MH |
1269 | rc = -EIO; |
1270 | goto out_wipe_list; | |
dddfa461 | 1271 | } |
f4aad16a | 1272 | found_matching_auth_tok: |
e2bd99ec | 1273 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
dddfa461 | 1274 | memcpy(&(candidate_auth_tok->token.private_key), |
f4aad16a | 1275 | &(matching_auth_tok->token.private_key), |
dddfa461 | 1276 | sizeof(struct ecryptfs_private_key)); |
f4aad16a | 1277 | rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, |
dddfa461 MH |
1278 | crypt_stat); |
1279 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { | |
237fead6 | 1280 | memcpy(&(candidate_auth_tok->token.password), |
f4aad16a | 1281 | &(matching_auth_tok->token.password), |
237fead6 | 1282 | sizeof(struct ecryptfs_password)); |
f4aad16a MH |
1283 | rc = decrypt_passphrase_encrypted_session_key( |
1284 | candidate_auth_tok, crypt_stat); | |
dddfa461 MH |
1285 | } |
1286 | if (rc) { | |
f4aad16a MH |
1287 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
1288 | ||
1289 | ecryptfs_printk(KERN_WARNING, "Error decrypting the " | |
1290 | "session key for authentication token with sig " | |
1291 | "[%.*s]; rc = [%d]. Removing auth tok " | |
1292 | "candidate from the list and searching for " | |
1293 | "the next match.\n", candidate_auth_tok_sig, | |
1294 | ECRYPTFS_SIG_SIZE_HEX, rc); | |
1295 | list_for_each_entry_safe(auth_tok_list_item, | |
1296 | auth_tok_list_item_tmp, | |
1297 | &auth_tok_list, list) { | |
1298 | if (candidate_auth_tok | |
1299 | == &auth_tok_list_item->auth_tok) { | |
1300 | list_del(&auth_tok_list_item->list); | |
1301 | kmem_cache_free( | |
1302 | ecryptfs_auth_tok_list_item_cache, | |
1303 | auth_tok_list_item); | |
1304 | goto find_next_matching_auth_tok; | |
1305 | } | |
1306 | } | |
1307 | BUG(); | |
dddfa461 MH |
1308 | } |
1309 | rc = ecryptfs_compute_root_iv(crypt_stat); | |
1310 | if (rc) { | |
1311 | ecryptfs_printk(KERN_ERR, "Error computing " | |
1312 | "the root IV\n"); | |
1313 | goto out_wipe_list; | |
237fead6 MH |
1314 | } |
1315 | rc = ecryptfs_init_crypt_ctx(crypt_stat); | |
1316 | if (rc) { | |
1317 | ecryptfs_printk(KERN_ERR, "Error initializing crypto " | |
1318 | "context for cipher [%s]; rc = [%d]\n", | |
1319 | crypt_stat->cipher, rc); | |
1320 | } | |
1321 | out_wipe_list: | |
1322 | wipe_auth_tok_list(&auth_tok_list); | |
1323 | out: | |
1324 | return rc; | |
1325 | } | |
f4aad16a | 1326 | |
dddfa461 MH |
1327 | static int |
1328 | pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, | |
1329 | struct ecryptfs_crypt_stat *crypt_stat, | |
1330 | struct ecryptfs_key_record *key_rec) | |
1331 | { | |
1332 | struct ecryptfs_msg_ctx *msg_ctx = NULL; | |
624ae528 TH |
1333 | char *payload = NULL; |
1334 | size_t payload_len; | |
dddfa461 MH |
1335 | struct ecryptfs_message *msg; |
1336 | int rc; | |
1337 | ||
1338 | rc = write_tag_66_packet(auth_tok->token.private_key.signature, | |
1339 | ecryptfs_code_for_cipher_string(crypt_stat), | |
624ae528 | 1340 | crypt_stat, &payload, &payload_len); |
dddfa461 MH |
1341 | if (rc) { |
1342 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); | |
1343 | goto out; | |
1344 | } | |
624ae528 | 1345 | rc = ecryptfs_send_message(payload, payload_len, &msg_ctx); |
dddfa461 | 1346 | if (rc) { |
624ae528 TH |
1347 | ecryptfs_printk(KERN_ERR, "Error sending message to " |
1348 | "ecryptfsd\n"); | |
dddfa461 MH |
1349 | goto out; |
1350 | } | |
1351 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); | |
1352 | if (rc) { | |
1353 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " | |
1354 | "from the user space daemon\n"); | |
1355 | rc = -EIO; | |
1356 | goto out; | |
1357 | } | |
1358 | rc = parse_tag_67_packet(key_rec, msg); | |
1359 | if (rc) | |
1360 | ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); | |
1361 | kfree(msg); | |
1362 | out: | |
624ae528 | 1363 | kfree(payload); |
dddfa461 MH |
1364 | return rc; |
1365 | } | |
1366 | /** | |
1367 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet | |
1368 | * @dest: Buffer into which to write the packet | |
22e78faf MH |
1369 | * @remaining_bytes: Maximum number of bytes that can be writtn |
1370 | * @auth_tok: The authentication token used for generating the tag 1 packet | |
1371 | * @crypt_stat: The cryptographic context | |
1372 | * @key_rec: The key record struct for the tag 1 packet | |
dddfa461 MH |
1373 | * @packet_size: This function will write the number of bytes that end |
1374 | * up constituting the packet; set to zero on error | |
1375 | * | |
1376 | * Returns zero on success; non-zero on error. | |
1377 | */ | |
1378 | static int | |
f4aad16a MH |
1379 | write_tag_1_packet(char *dest, size_t *remaining_bytes, |
1380 | struct ecryptfs_auth_tok *auth_tok, | |
dddfa461 | 1381 | struct ecryptfs_crypt_stat *crypt_stat, |
dddfa461 MH |
1382 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
1383 | { | |
1384 | size_t i; | |
1385 | size_t encrypted_session_key_valid = 0; | |
dddfa461 | 1386 | size_t packet_size_length; |
f4aad16a | 1387 | size_t max_packet_size; |
dddfa461 MH |
1388 | int rc = 0; |
1389 | ||
1390 | (*packet_size) = 0; | |
1391 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, | |
1392 | ECRYPTFS_SIG_SIZE); | |
1393 | encrypted_session_key_valid = 0; | |
1394 | for (i = 0; i < crypt_stat->key_size; i++) | |
1395 | encrypted_session_key_valid |= | |
1396 | auth_tok->session_key.encrypted_key[i]; | |
1397 | if (encrypted_session_key_valid) { | |
1398 | memcpy(key_rec->enc_key, | |
1399 | auth_tok->session_key.encrypted_key, | |
1400 | auth_tok->session_key.encrypted_key_size); | |
1401 | goto encrypted_session_key_set; | |
1402 | } | |
1403 | if (auth_tok->session_key.encrypted_key_size == 0) | |
1404 | auth_tok->session_key.encrypted_key_size = | |
1405 | auth_tok->token.private_key.key_size; | |
1406 | rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); | |
1407 | if (rc) { | |
f66e883e MH |
1408 | printk(KERN_ERR "Failed to encrypt session key via a key " |
1409 | "module; rc = [%d]\n", rc); | |
dddfa461 MH |
1410 | goto out; |
1411 | } | |
1412 | if (ecryptfs_verbosity > 0) { | |
1413 | ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); | |
1414 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); | |
1415 | } | |
1416 | encrypted_session_key_set: | |
f4aad16a MH |
1417 | /* This format is inspired by OpenPGP; see RFC 2440 |
1418 | * packet tag 1 */ | |
1419 | max_packet_size = (1 /* Tag 1 identifier */ | |
1420 | + 3 /* Max Tag 1 packet size */ | |
1421 | + 1 /* Version */ | |
1422 | + ECRYPTFS_SIG_SIZE /* Key identifier */ | |
1423 | + 1 /* Cipher identifier */ | |
1424 | + key_rec->enc_key_size); /* Encrypted key size */ | |
1425 | if (max_packet_size > (*remaining_bytes)) { | |
1426 | printk(KERN_ERR "Packet length larger than maximum allowable; " | |
81acbcd6 | 1427 | "need up to [%td] bytes, but there are only [%td] " |
f4aad16a | 1428 | "available\n", max_packet_size, (*remaining_bytes)); |
dddfa461 MH |
1429 | rc = -EINVAL; |
1430 | goto out; | |
1431 | } | |
1432 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; | |
f66e883e MH |
1433 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
1434 | (max_packet_size - 4), | |
1435 | &packet_size_length); | |
dddfa461 MH |
1436 | if (rc) { |
1437 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " | |
1438 | "header; cannot generate packet length\n"); | |
1439 | goto out; | |
1440 | } | |
1441 | (*packet_size) += packet_size_length; | |
1442 | dest[(*packet_size)++] = 0x03; /* version 3 */ | |
1443 | memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); | |
1444 | (*packet_size) += ECRYPTFS_SIG_SIZE; | |
1445 | dest[(*packet_size)++] = RFC2440_CIPHER_RSA; | |
1446 | memcpy(&dest[(*packet_size)], key_rec->enc_key, | |
1447 | key_rec->enc_key_size); | |
1448 | (*packet_size) += key_rec->enc_key_size; | |
1449 | out: | |
1450 | if (rc) | |
1451 | (*packet_size) = 0; | |
f4aad16a MH |
1452 | else |
1453 | (*remaining_bytes) -= (*packet_size); | |
dddfa461 MH |
1454 | return rc; |
1455 | } | |
237fead6 MH |
1456 | |
1457 | /** | |
1458 | * write_tag_11_packet | |
1459 | * @dest: Target into which Tag 11 packet is to be written | |
22e78faf | 1460 | * @remaining_bytes: Maximum packet length |
237fead6 MH |
1461 | * @contents: Byte array of contents to copy in |
1462 | * @contents_length: Number of bytes in contents | |
1463 | * @packet_length: Length of the Tag 11 packet written; zero on error | |
1464 | * | |
1465 | * Returns zero on success; non-zero on error. | |
1466 | */ | |
1467 | static int | |
81acbcd6 | 1468 | write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, |
146a4606 | 1469 | size_t contents_length, size_t *packet_length) |
237fead6 | 1470 | { |
237fead6 | 1471 | size_t packet_size_length; |
146a4606 | 1472 | size_t max_packet_size; |
dddfa461 | 1473 | int rc = 0; |
237fead6 MH |
1474 | |
1475 | (*packet_length) = 0; | |
146a4606 MH |
1476 | /* This format is inspired by OpenPGP; see RFC 2440 |
1477 | * packet tag 11 */ | |
1478 | max_packet_size = (1 /* Tag 11 identifier */ | |
1479 | + 3 /* Max Tag 11 packet size */ | |
1480 | + 1 /* Binary format specifier */ | |
1481 | + 1 /* Filename length */ | |
1482 | + 8 /* Filename ("_CONSOLE") */ | |
1483 | + 4 /* Modification date */ | |
1484 | + contents_length); /* Literal data */ | |
1485 | if (max_packet_size > (*remaining_bytes)) { | |
1486 | printk(KERN_ERR "Packet length larger than maximum allowable; " | |
81acbcd6 | 1487 | "need up to [%td] bytes, but there are only [%td] " |
146a4606 | 1488 | "available\n", max_packet_size, (*remaining_bytes)); |
237fead6 | 1489 | rc = -EINVAL; |
237fead6 MH |
1490 | goto out; |
1491 | } | |
237fead6 | 1492 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; |
f66e883e MH |
1493 | rc = ecryptfs_write_packet_length(&dest[(*packet_length)], |
1494 | (max_packet_size - 4), | |
1495 | &packet_size_length); | |
237fead6 | 1496 | if (rc) { |
146a4606 MH |
1497 | printk(KERN_ERR "Error generating tag 11 packet header; cannot " |
1498 | "generate packet length. rc = [%d]\n", rc); | |
237fead6 MH |
1499 | goto out; |
1500 | } | |
1501 | (*packet_length) += packet_size_length; | |
146a4606 | 1502 | dest[(*packet_length)++] = 0x62; /* binary data format specifier */ |
237fead6 MH |
1503 | dest[(*packet_length)++] = 8; |
1504 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); | |
1505 | (*packet_length) += 8; | |
237fead6 MH |
1506 | memset(&dest[(*packet_length)], 0x00, 4); |
1507 | (*packet_length) += 4; | |
237fead6 MH |
1508 | memcpy(&dest[(*packet_length)], contents, contents_length); |
1509 | (*packet_length) += contents_length; | |
1510 | out: | |
1511 | if (rc) | |
1512 | (*packet_length) = 0; | |
146a4606 MH |
1513 | else |
1514 | (*remaining_bytes) -= (*packet_length); | |
237fead6 MH |
1515 | return rc; |
1516 | } | |
1517 | ||
1518 | /** | |
1519 | * write_tag_3_packet | |
1520 | * @dest: Buffer into which to write the packet | |
22e78faf | 1521 | * @remaining_bytes: Maximum number of bytes that can be written |
237fead6 MH |
1522 | * @auth_tok: Authentication token |
1523 | * @crypt_stat: The cryptographic context | |
1524 | * @key_rec: encrypted key | |
1525 | * @packet_size: This function will write the number of bytes that end | |
1526 | * up constituting the packet; set to zero on error | |
1527 | * | |
1528 | * Returns zero on success; non-zero on error. | |
1529 | */ | |
1530 | static int | |
f4aad16a MH |
1531 | write_tag_3_packet(char *dest, size_t *remaining_bytes, |
1532 | struct ecryptfs_auth_tok *auth_tok, | |
237fead6 MH |
1533 | struct ecryptfs_crypt_stat *crypt_stat, |
1534 | struct ecryptfs_key_record *key_rec, size_t *packet_size) | |
1535 | { | |
237fead6 | 1536 | size_t i; |
237fead6 MH |
1537 | size_t encrypted_session_key_valid = 0; |
1538 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; | |
ac97b9f9 MH |
1539 | struct scatterlist dst_sg[2]; |
1540 | struct scatterlist src_sg[2]; | |
237fead6 | 1541 | struct mutex *tfm_mutex = NULL; |
19e66a67 | 1542 | u8 cipher_code; |
f4aad16a MH |
1543 | size_t packet_size_length; |
1544 | size_t max_packet_size; | |
1545 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = | |
1546 | crypt_stat->mount_crypt_stat; | |
8bba066f MH |
1547 | struct blkcipher_desc desc = { |
1548 | .tfm = NULL, | |
1549 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP | |
1550 | }; | |
1551 | int rc = 0; | |
237fead6 MH |
1552 | |
1553 | (*packet_size) = 0; | |
dddfa461 | 1554 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, |
237fead6 | 1555 | ECRYPTFS_SIG_SIZE); |
f4aad16a MH |
1556 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
1557 | crypt_stat->cipher); | |
1558 | if (unlikely(rc)) { | |
1559 | printk(KERN_ERR "Internal error whilst attempting to get " | |
1560 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", | |
1561 | crypt_stat->cipher, rc); | |
1562 | goto out; | |
1563 | } | |
1564 | if (mount_crypt_stat->global_default_cipher_key_size == 0) { | |
1565 | struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); | |
1566 | ||
1567 | printk(KERN_WARNING "No key size specified at mount; " | |
1568 | "defaulting to [%d]\n", alg->max_keysize); | |
1569 | mount_crypt_stat->global_default_cipher_key_size = | |
1570 | alg->max_keysize; | |
237fead6 | 1571 | } |
f4aad16a MH |
1572 | if (crypt_stat->key_size == 0) |
1573 | crypt_stat->key_size = | |
1574 | mount_crypt_stat->global_default_cipher_key_size; | |
237fead6 MH |
1575 | if (auth_tok->session_key.encrypted_key_size == 0) |
1576 | auth_tok->session_key.encrypted_key_size = | |
1577 | crypt_stat->key_size; | |
1578 | if (crypt_stat->key_size == 24 | |
1579 | && strcmp("aes", crypt_stat->cipher) == 0) { | |
1580 | memset((crypt_stat->key + 24), 0, 8); | |
1581 | auth_tok->session_key.encrypted_key_size = 32; | |
f4aad16a MH |
1582 | } else |
1583 | auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; | |
dddfa461 | 1584 | key_rec->enc_key_size = |
237fead6 | 1585 | auth_tok->session_key.encrypted_key_size; |
f4aad16a MH |
1586 | encrypted_session_key_valid = 0; |
1587 | for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) | |
1588 | encrypted_session_key_valid |= | |
1589 | auth_tok->session_key.encrypted_key[i]; | |
1590 | if (encrypted_session_key_valid) { | |
1591 | ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " | |
1592 | "using auth_tok->session_key.encrypted_key, " | |
1593 | "where key_rec->enc_key_size = [%d]\n", | |
1594 | key_rec->enc_key_size); | |
1595 | memcpy(key_rec->enc_key, | |
1596 | auth_tok->session_key.encrypted_key, | |
1597 | key_rec->enc_key_size); | |
1598 | goto encrypted_session_key_set; | |
1599 | } | |
dddfa461 MH |
1600 | if (auth_tok->token.password.flags & |
1601 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { | |
237fead6 MH |
1602 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " |
1603 | "session key encryption key of size [%d]\n", | |
1604 | auth_tok->token.password. | |
1605 | session_key_encryption_key_bytes); | |
1606 | memcpy(session_key_encryption_key, | |
1607 | auth_tok->token.password.session_key_encryption_key, | |
1608 | crypt_stat->key_size); | |
1609 | ecryptfs_printk(KERN_DEBUG, | |
1610 | "Cached session key " "encryption key: \n"); | |
1611 | if (ecryptfs_verbosity > 0) | |
1612 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
1613 | } | |
1614 | if (unlikely(ecryptfs_verbosity > 0)) { | |
1615 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); | |
1616 | ecryptfs_dump_hex(session_key_encryption_key, 16); | |
1617 | } | |
5dda6992 | 1618 | rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, |
ac97b9f9 MH |
1619 | src_sg, 2); |
1620 | if (rc < 1 || rc > 2) { | |
237fead6 | 1621 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a MH |
1622 | "for crypt_stat session key; expected rc = 1; " |
1623 | "got rc = [%d]. key_rec->enc_key_size = [%d]\n", | |
1624 | rc, key_rec->enc_key_size); | |
237fead6 MH |
1625 | rc = -ENOMEM; |
1626 | goto out; | |
1627 | } | |
5dda6992 | 1628 | rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, |
ac97b9f9 MH |
1629 | dst_sg, 2); |
1630 | if (rc < 1 || rc > 2) { | |
237fead6 | 1631 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
f4aad16a MH |
1632 | "for crypt_stat encrypted session key; " |
1633 | "expected rc = 1; got rc = [%d]. " | |
1634 | "key_rec->enc_key_size = [%d]\n", rc, | |
1635 | key_rec->enc_key_size); | |
237fead6 MH |
1636 | rc = -ENOMEM; |
1637 | goto out; | |
1638 | } | |
f4aad16a | 1639 | mutex_lock(tfm_mutex); |
8bba066f MH |
1640 | rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, |
1641 | crypt_stat->key_size); | |
237fead6 | 1642 | if (rc < 0) { |
f4aad16a | 1643 | mutex_unlock(tfm_mutex); |
237fead6 | 1644 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " |
8bba066f | 1645 | "context; rc = [%d]\n", rc); |
237fead6 MH |
1646 | goto out; |
1647 | } | |
1648 | rc = 0; | |
1649 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", | |
1650 | crypt_stat->key_size); | |
ac97b9f9 | 1651 | rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg, |
8bba066f | 1652 | (*key_rec).enc_key_size); |
f4aad16a | 1653 | mutex_unlock(tfm_mutex); |
8bba066f MH |
1654 | if (rc) { |
1655 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); | |
1656 | goto out; | |
1657 | } | |
237fead6 | 1658 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); |
f4aad16a MH |
1659 | if (ecryptfs_verbosity > 0) { |
1660 | ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", | |
1661 | key_rec->enc_key_size); | |
dddfa461 MH |
1662 | ecryptfs_dump_hex(key_rec->enc_key, |
1663 | key_rec->enc_key_size); | |
237fead6 | 1664 | } |
f4aad16a MH |
1665 | encrypted_session_key_set: |
1666 | /* This format is inspired by OpenPGP; see RFC 2440 | |
1667 | * packet tag 3 */ | |
1668 | max_packet_size = (1 /* Tag 3 identifier */ | |
1669 | + 3 /* Max Tag 3 packet size */ | |
1670 | + 1 /* Version */ | |
1671 | + 1 /* Cipher code */ | |
1672 | + 1 /* S2K specifier */ | |
1673 | + 1 /* Hash identifier */ | |
1674 | + ECRYPTFS_SALT_SIZE /* Salt */ | |
1675 | + 1 /* Hash iterations */ | |
1676 | + key_rec->enc_key_size); /* Encrypted key size */ | |
1677 | if (max_packet_size > (*remaining_bytes)) { | |
81acbcd6 AM |
1678 | printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " |
1679 | "there are only [%td] available\n", max_packet_size, | |
f4aad16a | 1680 | (*remaining_bytes)); |
237fead6 MH |
1681 | rc = -EINVAL; |
1682 | goto out; | |
1683 | } | |
237fead6 | 1684 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; |
f4aad16a MH |
1685 | /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) |
1686 | * to get the number of octets in the actual Tag 3 packet */ | |
f66e883e MH |
1687 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
1688 | (max_packet_size - 4), | |
1689 | &packet_size_length); | |
237fead6 | 1690 | if (rc) { |
f4aad16a MH |
1691 | printk(KERN_ERR "Error generating tag 3 packet header; cannot " |
1692 | "generate packet length. rc = [%d]\n", rc); | |
237fead6 MH |
1693 | goto out; |
1694 | } | |
1695 | (*packet_size) += packet_size_length; | |
1696 | dest[(*packet_size)++] = 0x04; /* version 4 */ | |
f4aad16a MH |
1697 | /* TODO: Break from RFC2440 so that arbitrary ciphers can be |
1698 | * specified with strings */ | |
237fead6 MH |
1699 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); |
1700 | if (cipher_code == 0) { | |
1701 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " | |
1702 | "cipher [%s]\n", crypt_stat->cipher); | |
1703 | rc = -EINVAL; | |
1704 | goto out; | |
1705 | } | |
1706 | dest[(*packet_size)++] = cipher_code; | |
1707 | dest[(*packet_size)++] = 0x03; /* S2K */ | |
1708 | dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ | |
1709 | memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, | |
1710 | ECRYPTFS_SALT_SIZE); | |
1711 | (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ | |
1712 | dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ | |
dddfa461 MH |
1713 | memcpy(&dest[(*packet_size)], key_rec->enc_key, |
1714 | key_rec->enc_key_size); | |
1715 | (*packet_size) += key_rec->enc_key_size; | |
237fead6 | 1716 | out: |
237fead6 MH |
1717 | if (rc) |
1718 | (*packet_size) = 0; | |
f4aad16a MH |
1719 | else |
1720 | (*remaining_bytes) -= (*packet_size); | |
237fead6 MH |
1721 | return rc; |
1722 | } | |
1723 | ||
eb95e7ff MH |
1724 | struct kmem_cache *ecryptfs_key_record_cache; |
1725 | ||
237fead6 MH |
1726 | /** |
1727 | * ecryptfs_generate_key_packet_set | |
22e78faf | 1728 | * @dest_base: Virtual address from which to write the key record set |
237fead6 MH |
1729 | * @crypt_stat: The cryptographic context from which the |
1730 | * authentication tokens will be retrieved | |
1731 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat | |
1732 | * for the global parameters | |
1733 | * @len: The amount written | |
1734 | * @max: The maximum amount of data allowed to be written | |
1735 | * | |
1736 | * Generates a key packet set and writes it to the virtual address | |
1737 | * passed in. | |
1738 | * | |
1739 | * Returns zero on success; non-zero on error. | |
1740 | */ | |
1741 | int | |
1742 | ecryptfs_generate_key_packet_set(char *dest_base, | |
1743 | struct ecryptfs_crypt_stat *crypt_stat, | |
1744 | struct dentry *ecryptfs_dentry, size_t *len, | |
1745 | size_t max) | |
1746 | { | |
237fead6 | 1747 | struct ecryptfs_auth_tok *auth_tok; |
f4aad16a | 1748 | struct ecryptfs_global_auth_tok *global_auth_tok; |
237fead6 MH |
1749 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
1750 | &ecryptfs_superblock_to_private( | |
1751 | ecryptfs_dentry->d_sb)->mount_crypt_stat; | |
1752 | size_t written; | |
eb95e7ff | 1753 | struct ecryptfs_key_record *key_rec; |
f4aad16a | 1754 | struct ecryptfs_key_sig *key_sig; |
dddfa461 | 1755 | int rc = 0; |
237fead6 MH |
1756 | |
1757 | (*len) = 0; | |
f4aad16a | 1758 | mutex_lock(&crypt_stat->keysig_list_mutex); |
eb95e7ff MH |
1759 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); |
1760 | if (!key_rec) { | |
1761 | rc = -ENOMEM; | |
1762 | goto out; | |
1763 | } | |
f4aad16a MH |
1764 | list_for_each_entry(key_sig, &crypt_stat->keysig_list, |
1765 | crypt_stat_list) { | |
1766 | memset(key_rec, 0, sizeof(*key_rec)); | |
1767 | rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, | |
1768 | mount_crypt_stat, | |
1769 | key_sig->keysig); | |
1770 | if (rc) { | |
1771 | printk(KERN_ERR "Error attempting to get the global " | |
1772 | "auth_tok; rc = [%d]\n", rc); | |
1773 | goto out_free; | |
1774 | } | |
1775 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { | |
1776 | printk(KERN_WARNING | |
1777 | "Skipping invalid auth tok with sig = [%s]\n", | |
1778 | global_auth_tok->sig); | |
1779 | continue; | |
1780 | } | |
1781 | auth_tok = global_auth_tok->global_auth_tok; | |
237fead6 MH |
1782 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { |
1783 | rc = write_tag_3_packet((dest_base + (*len)), | |
f4aad16a | 1784 | &max, auth_tok, |
eb95e7ff | 1785 | crypt_stat, key_rec, |
237fead6 MH |
1786 | &written); |
1787 | if (rc) { | |
1788 | ecryptfs_printk(KERN_WARNING, "Error " | |
1789 | "writing tag 3 packet\n"); | |
eb95e7ff | 1790 | goto out_free; |
237fead6 MH |
1791 | } |
1792 | (*len) += written; | |
1793 | /* Write auth tok signature packet */ | |
f4aad16a MH |
1794 | rc = write_tag_11_packet((dest_base + (*len)), &max, |
1795 | key_rec->sig, | |
1796 | ECRYPTFS_SIG_SIZE, &written); | |
237fead6 MH |
1797 | if (rc) { |
1798 | ecryptfs_printk(KERN_ERR, "Error writing " | |
1799 | "auth tok signature packet\n"); | |
eb95e7ff | 1800 | goto out_free; |
237fead6 MH |
1801 | } |
1802 | (*len) += written; | |
dddfa461 MH |
1803 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
1804 | rc = write_tag_1_packet(dest_base + (*len), | |
f4aad16a MH |
1805 | &max, auth_tok, |
1806 | crypt_stat, key_rec, &written); | |
dddfa461 MH |
1807 | if (rc) { |
1808 | ecryptfs_printk(KERN_WARNING, "Error " | |
1809 | "writing tag 1 packet\n"); | |
eb95e7ff | 1810 | goto out_free; |
dddfa461 MH |
1811 | } |
1812 | (*len) += written; | |
237fead6 MH |
1813 | } else { |
1814 | ecryptfs_printk(KERN_WARNING, "Unsupported " | |
1815 | "authentication token type\n"); | |
1816 | rc = -EINVAL; | |
eb95e7ff | 1817 | goto out_free; |
237fead6 | 1818 | } |
f4aad16a MH |
1819 | } |
1820 | if (likely(max > 0)) { | |
237fead6 MH |
1821 | dest_base[(*len)] = 0x00; |
1822 | } else { | |
1823 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); | |
1824 | rc = -EIO; | |
1825 | } | |
eb95e7ff MH |
1826 | out_free: |
1827 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); | |
237fead6 MH |
1828 | out: |
1829 | if (rc) | |
1830 | (*len) = 0; | |
f4aad16a MH |
1831 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
1832 | return rc; | |
1833 | } | |
1834 | ||
1835 | struct kmem_cache *ecryptfs_key_sig_cache; | |
1836 | ||
1837 | int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) | |
1838 | { | |
1839 | struct ecryptfs_key_sig *new_key_sig; | |
1840 | int rc = 0; | |
1841 | ||
1842 | new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); | |
1843 | if (!new_key_sig) { | |
1844 | rc = -ENOMEM; | |
1845 | printk(KERN_ERR | |
1846 | "Error allocating from ecryptfs_key_sig_cache\n"); | |
1847 | goto out; | |
1848 | } | |
1849 | memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
1850 | mutex_lock(&crypt_stat->keysig_list_mutex); | |
1851 | list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); | |
1852 | mutex_unlock(&crypt_stat->keysig_list_mutex); | |
1853 | out: | |
237fead6 MH |
1854 | return rc; |
1855 | } | |
f4aad16a MH |
1856 | |
1857 | struct kmem_cache *ecryptfs_global_auth_tok_cache; | |
1858 | ||
1859 | int | |
1860 | ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, | |
1861 | char *sig) | |
1862 | { | |
1863 | struct ecryptfs_global_auth_tok *new_auth_tok; | |
1864 | int rc = 0; | |
1865 | ||
459e2164 | 1866 | new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, |
f4aad16a MH |
1867 | GFP_KERNEL); |
1868 | if (!new_auth_tok) { | |
1869 | rc = -ENOMEM; | |
1870 | printk(KERN_ERR "Error allocating from " | |
1871 | "ecryptfs_global_auth_tok_cache\n"); | |
1872 | goto out; | |
1873 | } | |
1874 | memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); | |
1875 | new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; | |
1876 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
1877 | list_add(&new_auth_tok->mount_crypt_stat_list, | |
1878 | &mount_crypt_stat->global_auth_tok_list); | |
1879 | mount_crypt_stat->num_global_auth_toks++; | |
1880 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); | |
1881 | out: | |
1882 | return rc; | |
1883 | } | |
1884 |