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