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 | */ |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 42 | static int process_request_key_err(long err_code) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | /** |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 68 | * ecryptfs_parse_packet_length |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 69 | * @data: Pointer to memory containing length at offset |
| 70 | * @size: This function writes the decoded size to this memory |
| 71 | * address; zero on error |
| 72 | * @length_size: The number of bytes occupied by the encoded length |
| 73 | * |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 74 | * Returns zero on success; non-zero on error |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 75 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 76 | int ecryptfs_parse_packet_length(unsigned char *data, size_t *size, |
| 77 | size_t *length_size) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | /** |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 108 | * ecryptfs_write_packet_length |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 109 | * @dest: The byte array target into which to write the length. Must |
| 110 | * have at least 5 bytes allocated. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 111 | * @size: The length to write. |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 112 | * @packet_size_length: The number of bytes used to encode the packet |
| 113 | * length is written to this address. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 114 | * |
| 115 | * Returns zero on success; non-zero on error. |
| 116 | */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 117 | int ecryptfs_write_packet_length(char *dest, size_t size, |
| 118 | size_t *packet_size_length) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 165 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
| 166 | &packet_size_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 175 | rc = ecryptfs_write_packet_length(&message[i], |
| 176 | session_key->encrypted_key_size, |
| 177 | &packet_size_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 193 | parse_tag_65_packet(struct ecryptfs_session_key *session_key, u8 *cipher_code, |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 229 | rc = ecryptfs_parse_packet_length(&data[i], &m_size, &data_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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)) { |
| 237 | ecryptfs_printk(KERN_ERR, "The received netlink message is " |
| 238 | "shorter than expected\n"); |
| 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 |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 279 | write_tag_66_packet(char *signature, u8 cipher_code, |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 308 | rc = ecryptfs_write_packet_length(&message[i], ECRYPTFS_SIG_SIZE_HEX, |
| 309 | &packet_size_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 319 | rc = ecryptfs_write_packet_length(&message[i], crypt_stat->key_size + 3, |
| 320 | &packet_size_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 361 | printk(KERN_ERR "%s: message_len is [%Zd]; minimum acceptable " |
| 362 | "message length is [%d]\n", __func__, message_len, 4); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 363 | goto out; |
| 364 | } |
| 365 | if (data[i++] != ECRYPTFS_TAG_67_PACKET_TYPE) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 366 | rc = -EIO; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 367 | printk(KERN_ERR "%s: Type should be ECRYPTFS_TAG_67\n", |
| 368 | __func__); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 369 | goto out; |
| 370 | } |
| 371 | if (data[i++]) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 372 | rc = -EIO; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 373 | printk(KERN_ERR "%s: Status indicator has non zero " |
| 374 | "value [%d]\n", __func__, data[i-1]); |
| 375 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 376 | goto out; |
| 377 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 378 | rc = ecryptfs_parse_packet_length(&data[i], &key_rec->enc_key_size, |
| 379 | &data_len); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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)) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 387 | rc = -EIO; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 388 | printk(KERN_ERR "%s: message_len [%Zd]; max len is [%Zd]\n", |
| 389 | __func__, message_len, (i + key_rec->enc_key_size)); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 390 | goto out; |
| 391 | } |
| 392 | if (key_rec->enc_key_size > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 393 | rc = -EIO; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 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); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 398 | goto out; |
| 399 | } |
| 400 | memcpy(key_rec->enc_key, &data[i], key_rec->enc_key_size); |
| 401 | out: |
| 402 | return rc; |
| 403 | } |
| 404 | |
Michael Halcrow | cd9d67d | 2007-10-16 01:28:04 -0700 | [diff] [blame] | 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 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 426 | /** |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 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 |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 430 | * |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 431 | * Returns zero on success; non-zero error otherwise. |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 432 | */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 433 | static int |
| 434 | decrypt_pki_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, |
| 435 | struct ecryptfs_crypt_stat *crypt_stat) |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 436 | { |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 437 | u8 cipher_code = 0; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 438 | struct ecryptfs_msg_ctx *msg_ctx; |
| 439 | struct ecryptfs_message *msg = NULL; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 440 | char *auth_tok_sig; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 441 | char *netlink_message; |
| 442 | size_t netlink_message_length; |
| 443 | int rc; |
| 444 | |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 445 | rc = ecryptfs_get_auth_tok_sig(&auth_tok_sig, auth_tok); |
| 446 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 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), |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 452 | &netlink_message, &netlink_message_length); |
| 453 | if (rc) { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 454 | ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n"); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 455 | goto out; |
| 456 | } |
| 457 | rc = ecryptfs_send_message(ecryptfs_transport, netlink_message, |
| 458 | netlink_message_length, &msg_ctx); |
| 459 | if (rc) { |
| 460 | ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); |
| 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 | { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 501 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
Michael Halcrow | e0869cc | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 502 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 503 | |
Michael Halcrow | e0869cc | 2007-10-16 01:27:55 -0700 | [diff] [blame] | 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); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 507 | kmem_cache_free(ecryptfs_auth_tok_list_item_cache, |
| 508 | auth_tok_list_item); |
| 509 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 510 | } |
| 511 | |
| 512 | struct kmem_cache *ecryptfs_auth_tok_list_item_cache; |
| 513 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 514 | /** |
| 515 | * parse_tag_1_packet |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 516 | * @crypt_stat: The cryptographic context to modify based on packet contents |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 517 | * @data: The raw bytes of the packet. |
| 518 | * @auth_tok_list: eCryptfs parses packets into authentication tokens; |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 519 | * a new authentication token will be placed at the |
| 520 | * end of this list for this packet. |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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. |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 527 | * @max_packet_size: The maximum allowable packet size |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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; |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 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 |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 556 | */ |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 557 | if (unlikely(max_packet_size < 12)) { |
| 558 | printk(KERN_ERR "Invalid max packet size; must be >=12\n"); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 559 | rc = -EINVAL; |
| 560 | goto out; |
| 561 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 562 | if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 563 | printk(KERN_ERR "Enter w/ first byte != 0x%.2x\n", |
| 564 | ECRYPTFS_TAG_1_PACKET_TYPE); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 = |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 571 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, |
| 572 | GFP_KERNEL); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 573 | if (!auth_tok_list_item) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 574 | printk(KERN_ERR "Unable to allocate memory\n"); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 575 | rc = -ENOMEM; |
| 576 | goto out; |
| 577 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 578 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 579 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
| 580 | &length_size); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 581 | if (rc) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 582 | printk(KERN_WARNING "Error parsing packet length; " |
| 583 | "rc = [%d]\n", rc); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 584 | goto out_free; |
| 585 | } |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 586 | if (unlikely(body_size < (ECRYPTFS_SIG_SIZE + 2))) { |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 587 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 588 | rc = -EINVAL; |
| 589 | goto out_free; |
| 590 | } |
| 591 | (*packet_size) += length_size; |
| 592 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 593 | printk(KERN_WARNING "Packet size exceeds max\n"); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 594 | rc = -EINVAL; |
| 595 | goto out_free; |
| 596 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 597 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 598 | printk(KERN_WARNING "Unknown version number [%d]\n", |
| 599 | data[(*packet_size) - 1]); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 600 | rc = -EINVAL; |
| 601 | goto out_free; |
| 602 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 = |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 610 | body_size - (ECRYPTFS_SIG_SIZE + 2); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 611 | if ((*new_auth_tok)->session_key.encrypted_key_size |
| 612 | > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) { |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 613 | printk(KERN_WARNING "Tag 1 packet contains key larger " |
| 614 | "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES"); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 615 | rc = -EINVAL; |
| 616 | goto out; |
| 617 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 618 | memcpy((*new_auth_tok)->session_key.encrypted_key, |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 619 | &data[(*packet_size)], (body_size - (ECRYPTFS_SIG_SIZE + 2))); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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; |
Michael Halcrow | 13218179 | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 626 | (*new_auth_tok)->flags = 0; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 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); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 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 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 669 | size_t body_size; |
| 670 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
| 671 | size_t length_size; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 672 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 673 | |
| 674 | (*packet_size) = 0; |
| 675 | (*new_auth_tok) = NULL; |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 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 |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 691 | */ |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 692 | if (max_packet_size < (ECRYPTFS_SALT_SIZE + 7)) { |
| 693 | printk(KERN_ERR "Max packet size too large\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 694 | rc = -EINVAL; |
| 695 | goto out; |
| 696 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 697 | if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 698 | printk(KERN_ERR "First byte != 0x%.2x; invalid packet\n", |
| 699 | ECRYPTFS_TAG_3_PACKET_TYPE); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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 = |
Robert P. J. Day | c376222 | 2007-02-10 01:45:03 -0800 | [diff] [blame] | 706 | kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 707 | if (!auth_tok_list_item) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 708 | printk(KERN_ERR "Unable to allocate memory\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 709 | rc = -ENOMEM; |
| 710 | goto out; |
| 711 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 712 | (*new_auth_tok) = &auth_tok_list_item->auth_tok; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 713 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
| 714 | &length_size); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 715 | if (rc) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 716 | printk(KERN_WARNING "Error parsing packet length; rc = [%d]\n", |
| 717 | rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 718 | goto out_free; |
| 719 | } |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 720 | if (unlikely(body_size < (ECRYPTFS_SALT_SIZE + 5))) { |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 721 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 722 | rc = -EINVAL; |
| 723 | goto out_free; |
| 724 | } |
| 725 | (*packet_size) += length_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 726 | if (unlikely((*packet_size) + body_size > max_packet_size)) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 727 | printk(KERN_ERR "Packet size exceeds max\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 728 | rc = -EINVAL; |
| 729 | goto out_free; |
| 730 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 731 | (*new_auth_tok)->session_key.encrypted_key_size = |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 732 | (body_size - (ECRYPTFS_SALT_SIZE + 5)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 733 | if (unlikely(data[(*packet_size)++] != 0x04)) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 734 | printk(KERN_WARNING "Unknown version number [%d]\n", |
| 735 | data[(*packet_size) - 1]); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 736 | rc = -EINVAL; |
| 737 | goto out_free; |
| 738 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 752 | if (unlikely(data[(*packet_size)++] != 0x03)) { |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 753 | printk(KERN_WARNING "Only S2K ID 3 is currently supported\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 754 | rc = -ENOSYS; |
| 755 | goto out_free; |
| 756 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 757 | /* TODO: finish the hash mapping */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 758 | switch (data[(*packet_size)++]) { |
| 759 | case 0x01: /* See RFC2440 for these numbers and their mappings */ |
| 760 | /* Choose MD5 */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 761 | memcpy((*new_auth_tok)->token.password.salt, |
| 762 | &data[(*packet_size)], ECRYPTFS_SALT_SIZE); |
| 763 | (*packet_size) += ECRYPTFS_SALT_SIZE; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 764 | /* This conversion was taken straight from RFC2440 */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 765 | (*new_auth_tok)->token.password.hash_iterations = |
| 766 | ((u32) 16 + (data[(*packet_size)] & 15)) |
| 767 | << ((data[(*packet_size)] >> 4) + 6); |
| 768 | (*packet_size)++; |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 769 | /* Friendly reminder: |
| 770 | * (*new_auth_tok)->session_key.encrypted_key_size = |
| 771 | * (body_size - (ECRYPTFS_SALT_SIZE + 5)); */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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; |
Michael Halcrow | c59becf | 2007-10-16 01:27:56 -0700 | [diff] [blame] | 781 | (*new_auth_tok)->token.password.hash_algo = 0x01; /* MD5 */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 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; |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 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. |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 850 | */ |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 851 | if (max_packet_size < 16) { |
| 852 | printk(KERN_ERR "Maximum packet size too small\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 853 | rc = -EINVAL; |
| 854 | goto out; |
| 855 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 856 | if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) { |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 857 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 858 | rc = -EINVAL; |
| 859 | goto out; |
| 860 | } |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 861 | rc = ecryptfs_parse_packet_length(&data[(*packet_size)], &body_size, |
| 862 | &length_size); |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 863 | if (rc) { |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 864 | printk(KERN_WARNING "Invalid tag 11 packet format\n"); |
| 865 | goto out; |
| 866 | } |
| 867 | if (body_size < 14) { |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 868 | printk(KERN_WARNING "Invalid body size ([%td])\n", body_size); |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 869 | rc = -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 870 | goto out; |
| 871 | } |
| 872 | (*packet_size) += length_size; |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 873 | (*tag_11_contents_size) = (body_size - 14); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 874 | if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) { |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 875 | printk(KERN_ERR "Packet size exceeds max\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 876 | rc = -EINVAL; |
| 877 | goto out; |
| 878 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 879 | if (data[(*packet_size)++] != 0x62) { |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 880 | printk(KERN_WARNING "Unrecognizable packet\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 881 | rc = -EINVAL; |
| 882 | goto out; |
| 883 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 884 | if (data[(*packet_size)++] != 0x08) { |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 885 | printk(KERN_WARNING "Unrecognizable packet\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 886 | rc = -EINVAL; |
| 887 | goto out; |
| 888 | } |
Michael Halcrow | f648104 | 2007-10-16 01:27:57 -0700 | [diff] [blame] | 889 | (*packet_size) += 12; /* Ignore filename and modification date */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 890 | memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size)); |
| 891 | (*packet_size) += (*tag_11_contents_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 892 | out: |
| 893 | if (rc) { |
| 894 | (*packet_size) = 0; |
| 895 | (*tag_11_contents_size) = 0; |
| 896 | } |
| 897 | return rc; |
| 898 | } |
| 899 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 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 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 924 | /** |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 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); |
| 966 | process_request_key_err(PTR_ERR(*auth_tok_key)); |
| 967 | rc = -EINVAL; |
| 968 | goto out; |
| 969 | } |
| 970 | (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key); |
| 971 | if (ecryptfs_verify_version((*auth_tok)->version)) { |
| 972 | printk(KERN_ERR |
| 973 | "Data structure version mismatch. " |
| 974 | "Userspace tools must match eCryptfs " |
| 975 | "kernel module with major version [%d] " |
| 976 | "and minor version [%d]\n", |
| 977 | ECRYPTFS_VERSION_MAJOR, |
| 978 | ECRYPTFS_VERSION_MINOR); |
| 979 | rc = -EINVAL; |
| 980 | goto out; |
| 981 | } |
| 982 | if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD |
| 983 | && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) { |
| 984 | printk(KERN_ERR "Invalid auth_tok structure " |
| 985 | "returned from key query\n"); |
| 986 | rc = -EINVAL; |
| 987 | goto out; |
| 988 | } |
| 989 | out: |
| 990 | return rc; |
| 991 | } |
| 992 | |
| 993 | /** |
| 994 | * ecryptfs_find_auth_tok_for_sig |
| 995 | * @auth_tok: Set to the matching auth_tok; NULL if not found |
| 996 | * @crypt_stat: inode crypt_stat crypto context |
| 997 | * @sig: Sig of auth_tok to find |
| 998 | * |
| 999 | * For now, this function simply looks at the registered auth_tok's |
| 1000 | * linked off the mount_crypt_stat, so all the auth_toks that can be |
| 1001 | * used must be registered at mount time. This function could |
| 1002 | * potentially try a lot harder to find auth_tok's (e.g., by calling |
| 1003 | * out to ecryptfsd to dynamically retrieve an auth_tok object) so |
| 1004 | * that static registration of auth_tok's will no longer be necessary. |
| 1005 | * |
| 1006 | * Returns zero on no error; non-zero on error |
| 1007 | */ |
| 1008 | static int |
| 1009 | ecryptfs_find_auth_tok_for_sig( |
| 1010 | struct ecryptfs_auth_tok **auth_tok, |
| 1011 | struct ecryptfs_crypt_stat *crypt_stat, char *sig) |
| 1012 | { |
| 1013 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1014 | crypt_stat->mount_crypt_stat; |
| 1015 | struct ecryptfs_global_auth_tok *global_auth_tok; |
| 1016 | int rc = 0; |
| 1017 | |
| 1018 | (*auth_tok) = NULL; |
| 1019 | if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, |
| 1020 | mount_crypt_stat, sig)) { |
| 1021 | struct key *auth_tok_key; |
| 1022 | |
| 1023 | rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok, |
| 1024 | sig); |
| 1025 | } else |
| 1026 | (*auth_tok) = global_auth_tok->global_auth_tok; |
| 1027 | return rc; |
| 1028 | } |
| 1029 | |
| 1030 | /** |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1031 | * decrypt_passphrase_encrypted_session_key - Decrypt the session key with the given auth_tok. |
| 1032 | * @auth_tok: The passphrase authentication token to use to encrypt the FEK |
| 1033 | * @crypt_stat: The cryptographic context |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1034 | * |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1035 | * Returns zero on success; non-zero error otherwise |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1036 | */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1037 | static int |
| 1038 | decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok, |
| 1039 | struct ecryptfs_crypt_stat *crypt_stat) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1040 | { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1041 | struct scatterlist dst_sg; |
| 1042 | struct scatterlist src_sg; |
Michael Halcrow | dd8e290 | 2007-10-16 01:28:03 -0700 | [diff] [blame] | 1043 | struct mutex *tfm_mutex; |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1044 | struct blkcipher_desc desc = { |
| 1045 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 1046 | }; |
| 1047 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1048 | |
Jens Axboe | 60c74f8 | 2007-10-22 19:43:30 +0200 | [diff] [blame] | 1049 | sg_init_table(&dst_sg, 1); |
| 1050 | sg_init_table(&src_sg, 1); |
| 1051 | |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1052 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1053 | ecryptfs_printk( |
| 1054 | KERN_DEBUG, "Session key encryption key (size [%d]):\n", |
| 1055 | auth_tok->token.password.session_key_encryption_key_bytes); |
| 1056 | ecryptfs_dump_hex( |
| 1057 | auth_tok->token.password.session_key_encryption_key, |
| 1058 | auth_tok->token.password.session_key_encryption_key_bytes); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1059 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1060 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
| 1061 | crypt_stat->cipher); |
| 1062 | if (unlikely(rc)) { |
| 1063 | printk(KERN_ERR "Internal error whilst attempting to get " |
| 1064 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", |
| 1065 | crypt_stat->cipher, rc); |
| 1066 | goto out; |
| 1067 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1068 | rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key, |
| 1069 | auth_tok->session_key.encrypted_key_size, |
| 1070 | &src_sg, 1); |
| 1071 | if (rc != 1) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1072 | printk(KERN_ERR "Internal error whilst attempting to convert " |
| 1073 | "auth_tok->session_key.encrypted_key to scatterlist; " |
| 1074 | "expected rc = 1; got rc = [%d]. " |
| 1075 | "auth_tok->session_key.encrypted_key_size = [%d]\n", rc, |
| 1076 | auth_tok->session_key.encrypted_key_size); |
| 1077 | goto out; |
| 1078 | } |
| 1079 | auth_tok->session_key.decrypted_key_size = |
| 1080 | auth_tok->session_key.encrypted_key_size; |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1081 | rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key, |
| 1082 | auth_tok->session_key.decrypted_key_size, |
| 1083 | &dst_sg, 1); |
| 1084 | if (rc != 1) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1085 | printk(KERN_ERR "Internal error whilst attempting to convert " |
| 1086 | "auth_tok->session_key.decrypted_key to scatterlist; " |
| 1087 | "expected rc = 1; got rc = [%d]\n", rc); |
| 1088 | goto out; |
| 1089 | } |
| 1090 | mutex_lock(tfm_mutex); |
| 1091 | rc = crypto_blkcipher_setkey( |
| 1092 | desc.tfm, auth_tok->token.password.session_key_encryption_key, |
| 1093 | crypt_stat->key_size); |
| 1094 | if (unlikely(rc < 0)) { |
| 1095 | mutex_unlock(tfm_mutex); |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1096 | printk(KERN_ERR "Error setting key for crypto context\n"); |
| 1097 | rc = -EINVAL; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1098 | goto out; |
Michael Halcrow | e5d9cbd | 2006-10-30 22:07:16 -0800 | [diff] [blame] | 1099 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1100 | rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg, |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1101 | auth_tok->session_key.encrypted_key_size); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1102 | mutex_unlock(tfm_mutex); |
| 1103 | if (unlikely(rc)) { |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1104 | printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1105 | goto out; |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1106 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1107 | auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY; |
| 1108 | memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key, |
| 1109 | auth_tok->session_key.decrypted_key_size); |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1110 | crypt_stat->flags |= ECRYPTFS_KEY_VALID; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1111 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1112 | ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n", |
| 1113 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1114 | ecryptfs_dump_hex(crypt_stat->key, |
| 1115 | crypt_stat->key_size); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1116 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1117 | out: |
| 1118 | return rc; |
| 1119 | } |
| 1120 | |
| 1121 | /** |
| 1122 | * ecryptfs_parse_packet_set |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1123 | * @crypt_stat: The cryptographic context |
| 1124 | * @src: Virtual address of region of memory containing the packets |
| 1125 | * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1126 | * |
| 1127 | * Get crypt_stat to have the file's session key if the requisite key |
| 1128 | * is available to decrypt the session key. |
| 1129 | * |
| 1130 | * Returns Zero if a valid authentication token was retrieved and |
| 1131 | * processed; negative value for file not encrypted or for error |
| 1132 | * conditions. |
| 1133 | */ |
| 1134 | int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat, |
| 1135 | unsigned char *src, |
| 1136 | struct dentry *ecryptfs_dentry) |
| 1137 | { |
| 1138 | size_t i = 0; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1139 | size_t found_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1140 | size_t next_packet_is_auth_tok_packet; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1141 | struct list_head auth_tok_list; |
Michael Halcrow | dd8e290 | 2007-10-16 01:28:03 -0700 | [diff] [blame] | 1142 | struct ecryptfs_auth_tok *matching_auth_tok; |
| 1143 | struct ecryptfs_auth_tok *candidate_auth_tok; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1144 | char *candidate_auth_tok_sig; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1145 | size_t packet_size; |
| 1146 | struct ecryptfs_auth_tok *new_auth_tok; |
| 1147 | unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE]; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1148 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1149 | size_t tag_11_contents_size; |
| 1150 | size_t tag_11_packet_size; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1151 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1152 | |
| 1153 | INIT_LIST_HEAD(&auth_tok_list); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1154 | /* 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] | 1155 | * added the our &auth_tok_list */ |
| 1156 | next_packet_is_auth_tok_packet = 1; |
| 1157 | while (next_packet_is_auth_tok_packet) { |
| 1158 | size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i); |
| 1159 | |
| 1160 | switch (src[i]) { |
| 1161 | case ECRYPTFS_TAG_3_PACKET_TYPE: |
| 1162 | rc = parse_tag_3_packet(crypt_stat, |
| 1163 | (unsigned char *)&src[i], |
| 1164 | &auth_tok_list, &new_auth_tok, |
| 1165 | &packet_size, max_packet_size); |
| 1166 | if (rc) { |
| 1167 | ecryptfs_printk(KERN_ERR, "Error parsing " |
| 1168 | "tag 3 packet\n"); |
| 1169 | rc = -EIO; |
| 1170 | goto out_wipe_list; |
| 1171 | } |
| 1172 | i += packet_size; |
| 1173 | rc = parse_tag_11_packet((unsigned char *)&src[i], |
| 1174 | sig_tmp_space, |
| 1175 | ECRYPTFS_SIG_SIZE, |
| 1176 | &tag_11_contents_size, |
| 1177 | &tag_11_packet_size, |
| 1178 | max_packet_size); |
| 1179 | if (rc) { |
| 1180 | ecryptfs_printk(KERN_ERR, "No valid " |
| 1181 | "(ecryptfs-specific) literal " |
| 1182 | "packet containing " |
| 1183 | "authentication token " |
| 1184 | "signature found after " |
| 1185 | "tag 3 packet\n"); |
| 1186 | rc = -EIO; |
| 1187 | goto out_wipe_list; |
| 1188 | } |
| 1189 | i += tag_11_packet_size; |
| 1190 | if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) { |
| 1191 | ecryptfs_printk(KERN_ERR, "Expected " |
| 1192 | "signature of size [%d]; " |
| 1193 | "read size [%d]\n", |
| 1194 | ECRYPTFS_SIG_SIZE, |
| 1195 | tag_11_contents_size); |
| 1196 | rc = -EIO; |
| 1197 | goto out_wipe_list; |
| 1198 | } |
| 1199 | ecryptfs_to_hex(new_auth_tok->token.password.signature, |
| 1200 | sig_tmp_space, tag_11_contents_size); |
| 1201 | new_auth_tok->token.password.signature[ |
| 1202 | ECRYPTFS_PASSWORD_SIG_SIZE] = '\0'; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1203 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1204 | break; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1205 | case ECRYPTFS_TAG_1_PACKET_TYPE: |
| 1206 | rc = parse_tag_1_packet(crypt_stat, |
| 1207 | (unsigned char *)&src[i], |
| 1208 | &auth_tok_list, &new_auth_tok, |
| 1209 | &packet_size, max_packet_size); |
| 1210 | if (rc) { |
| 1211 | ecryptfs_printk(KERN_ERR, "Error parsing " |
| 1212 | "tag 1 packet\n"); |
| 1213 | rc = -EIO; |
| 1214 | goto out_wipe_list; |
| 1215 | } |
| 1216 | i += packet_size; |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1217 | crypt_stat->flags |= ECRYPTFS_ENCRYPTED; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1218 | break; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1219 | case ECRYPTFS_TAG_11_PACKET_TYPE: |
| 1220 | ecryptfs_printk(KERN_WARNING, "Invalid packet set " |
| 1221 | "(Tag 11 not allowed by itself)\n"); |
| 1222 | rc = -EIO; |
| 1223 | goto out_wipe_list; |
| 1224 | break; |
| 1225 | default: |
| 1226 | ecryptfs_printk(KERN_DEBUG, "No packet at offset " |
| 1227 | "[%d] of the file header; hex value of " |
| 1228 | "character is [0x%.2x]\n", i, src[i]); |
| 1229 | next_packet_is_auth_tok_packet = 0; |
| 1230 | } |
| 1231 | } |
| 1232 | if (list_empty(&auth_tok_list)) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1233 | printk(KERN_ERR "The lower file appears to be a non-encrypted " |
| 1234 | "eCryptfs file; this is not supported in this version " |
| 1235 | "of the eCryptfs kernel module\n"); |
| 1236 | rc = -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1237 | goto out; |
| 1238 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1239 | /* auth_tok_list contains the set of authentication tokens |
| 1240 | * parsed from the metadata. We need to find a matching |
| 1241 | * authentication token that has the secret component(s) |
| 1242 | * necessary to decrypt the EFEK in the auth_tok parsed from |
| 1243 | * the metadata. There may be several potential matches, but |
| 1244 | * just one will be sufficient to decrypt to get the FEK. */ |
| 1245 | find_next_matching_auth_tok: |
| 1246 | found_auth_tok = 0; |
| 1247 | list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1248 | candidate_auth_tok = &auth_tok_list_item->auth_tok; |
| 1249 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1250 | ecryptfs_printk(KERN_DEBUG, |
| 1251 | "Considering cadidate auth tok:\n"); |
| 1252 | ecryptfs_dump_auth_tok(candidate_auth_tok); |
| 1253 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1254 | rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig, |
| 1255 | candidate_auth_tok); |
| 1256 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1257 | printk(KERN_ERR |
| 1258 | "Unrecognized candidate auth tok type: [%d]\n", |
| 1259 | candidate_auth_tok->token_type); |
| 1260 | rc = -EINVAL; |
| 1261 | goto out_wipe_list; |
| 1262 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1263 | ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat, |
| 1264 | candidate_auth_tok_sig); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1265 | if (matching_auth_tok) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1266 | found_auth_tok = 1; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1267 | goto found_matching_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1268 | } |
| 1269 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1270 | if (!found_auth_tok) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1271 | ecryptfs_printk(KERN_ERR, "Could not find a usable " |
| 1272 | "authentication token\n"); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1273 | rc = -EIO; |
| 1274 | goto out_wipe_list; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1275 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1276 | found_matching_auth_tok: |
Michael Halcrow | e2bd99e | 2007-02-12 00:53:49 -0800 | [diff] [blame] | 1277 | if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1278 | memcpy(&(candidate_auth_tok->token.private_key), |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1279 | &(matching_auth_tok->token.private_key), |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1280 | sizeof(struct ecryptfs_private_key)); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1281 | rc = decrypt_pki_encrypted_session_key(candidate_auth_tok, |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1282 | crypt_stat); |
| 1283 | } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1284 | memcpy(&(candidate_auth_tok->token.password), |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1285 | &(matching_auth_tok->token.password), |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1286 | sizeof(struct ecryptfs_password)); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1287 | rc = decrypt_passphrase_encrypted_session_key( |
| 1288 | candidate_auth_tok, crypt_stat); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1289 | } |
| 1290 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1291 | struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp; |
| 1292 | |
| 1293 | ecryptfs_printk(KERN_WARNING, "Error decrypting the " |
| 1294 | "session key for authentication token with sig " |
| 1295 | "[%.*s]; rc = [%d]. Removing auth tok " |
| 1296 | "candidate from the list and searching for " |
| 1297 | "the next match.\n", candidate_auth_tok_sig, |
| 1298 | ECRYPTFS_SIG_SIZE_HEX, rc); |
| 1299 | list_for_each_entry_safe(auth_tok_list_item, |
| 1300 | auth_tok_list_item_tmp, |
| 1301 | &auth_tok_list, list) { |
| 1302 | if (candidate_auth_tok |
| 1303 | == &auth_tok_list_item->auth_tok) { |
| 1304 | list_del(&auth_tok_list_item->list); |
| 1305 | kmem_cache_free( |
| 1306 | ecryptfs_auth_tok_list_item_cache, |
| 1307 | auth_tok_list_item); |
| 1308 | goto find_next_matching_auth_tok; |
| 1309 | } |
| 1310 | } |
| 1311 | BUG(); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1312 | } |
| 1313 | rc = ecryptfs_compute_root_iv(crypt_stat); |
| 1314 | if (rc) { |
| 1315 | ecryptfs_printk(KERN_ERR, "Error computing " |
| 1316 | "the root IV\n"); |
| 1317 | goto out_wipe_list; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1318 | } |
| 1319 | rc = ecryptfs_init_crypt_ctx(crypt_stat); |
| 1320 | if (rc) { |
| 1321 | ecryptfs_printk(KERN_ERR, "Error initializing crypto " |
| 1322 | "context for cipher [%s]; rc = [%d]\n", |
| 1323 | crypt_stat->cipher, rc); |
| 1324 | } |
| 1325 | out_wipe_list: |
| 1326 | wipe_auth_tok_list(&auth_tok_list); |
| 1327 | out: |
| 1328 | return rc; |
| 1329 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1330 | |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1331 | static int |
| 1332 | pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok, |
| 1333 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1334 | struct ecryptfs_key_record *key_rec) |
| 1335 | { |
| 1336 | struct ecryptfs_msg_ctx *msg_ctx = NULL; |
| 1337 | char *netlink_payload; |
| 1338 | size_t netlink_payload_length; |
| 1339 | struct ecryptfs_message *msg; |
| 1340 | int rc; |
| 1341 | |
| 1342 | rc = write_tag_66_packet(auth_tok->token.private_key.signature, |
| 1343 | ecryptfs_code_for_cipher_string(crypt_stat), |
| 1344 | crypt_stat, &netlink_payload, |
| 1345 | &netlink_payload_length); |
| 1346 | if (rc) { |
| 1347 | ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n"); |
| 1348 | goto out; |
| 1349 | } |
| 1350 | rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload, |
| 1351 | netlink_payload_length, &msg_ctx); |
| 1352 | if (rc) { |
| 1353 | ecryptfs_printk(KERN_ERR, "Error sending netlink message\n"); |
| 1354 | goto out; |
| 1355 | } |
| 1356 | rc = ecryptfs_wait_for_response(msg_ctx, &msg); |
| 1357 | if (rc) { |
| 1358 | ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet " |
| 1359 | "from the user space daemon\n"); |
| 1360 | rc = -EIO; |
| 1361 | goto out; |
| 1362 | } |
| 1363 | rc = parse_tag_67_packet(key_rec, msg); |
| 1364 | if (rc) |
| 1365 | ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n"); |
| 1366 | kfree(msg); |
| 1367 | out: |
| 1368 | if (netlink_payload) |
| 1369 | kfree(netlink_payload); |
| 1370 | return rc; |
| 1371 | } |
| 1372 | /** |
| 1373 | * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet |
| 1374 | * @dest: Buffer into which to write the packet |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1375 | * @remaining_bytes: Maximum number of bytes that can be writtn |
| 1376 | * @auth_tok: The authentication token used for generating the tag 1 packet |
| 1377 | * @crypt_stat: The cryptographic context |
| 1378 | * @key_rec: The key record struct for the tag 1 packet |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1379 | * @packet_size: This function will write the number of bytes that end |
| 1380 | * up constituting the packet; set to zero on error |
| 1381 | * |
| 1382 | * Returns zero on success; non-zero on error. |
| 1383 | */ |
| 1384 | static int |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1385 | write_tag_1_packet(char *dest, size_t *remaining_bytes, |
| 1386 | struct ecryptfs_auth_tok *auth_tok, |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1387 | struct ecryptfs_crypt_stat *crypt_stat, |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1388 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
| 1389 | { |
| 1390 | size_t i; |
| 1391 | size_t encrypted_session_key_valid = 0; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1392 | size_t packet_size_length; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1393 | size_t max_packet_size; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1394 | int rc = 0; |
| 1395 | |
| 1396 | (*packet_size) = 0; |
| 1397 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature, |
| 1398 | ECRYPTFS_SIG_SIZE); |
| 1399 | encrypted_session_key_valid = 0; |
| 1400 | for (i = 0; i < crypt_stat->key_size; i++) |
| 1401 | encrypted_session_key_valid |= |
| 1402 | auth_tok->session_key.encrypted_key[i]; |
| 1403 | if (encrypted_session_key_valid) { |
| 1404 | memcpy(key_rec->enc_key, |
| 1405 | auth_tok->session_key.encrypted_key, |
| 1406 | auth_tok->session_key.encrypted_key_size); |
| 1407 | goto encrypted_session_key_set; |
| 1408 | } |
| 1409 | if (auth_tok->session_key.encrypted_key_size == 0) |
| 1410 | auth_tok->session_key.encrypted_key_size = |
| 1411 | auth_tok->token.private_key.key_size; |
| 1412 | rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec); |
| 1413 | if (rc) { |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 1414 | printk(KERN_ERR "Failed to encrypt session key via a key " |
| 1415 | "module; rc = [%d]\n", rc); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1416 | goto out; |
| 1417 | } |
| 1418 | if (ecryptfs_verbosity > 0) { |
| 1419 | ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n"); |
| 1420 | ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size); |
| 1421 | } |
| 1422 | encrypted_session_key_set: |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1423 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1424 | * packet tag 1 */ |
| 1425 | max_packet_size = (1 /* Tag 1 identifier */ |
| 1426 | + 3 /* Max Tag 1 packet size */ |
| 1427 | + 1 /* Version */ |
| 1428 | + ECRYPTFS_SIG_SIZE /* Key identifier */ |
| 1429 | + 1 /* Cipher identifier */ |
| 1430 | + key_rec->enc_key_size); /* Encrypted key size */ |
| 1431 | if (max_packet_size > (*remaining_bytes)) { |
| 1432 | printk(KERN_ERR "Packet length larger than maximum allowable; " |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 1433 | "need up to [%td] bytes, but there are only [%td] " |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1434 | "available\n", max_packet_size, (*remaining_bytes)); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1435 | rc = -EINVAL; |
| 1436 | goto out; |
| 1437 | } |
| 1438 | dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 1439 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
| 1440 | (max_packet_size - 4), |
| 1441 | &packet_size_length); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1442 | if (rc) { |
| 1443 | ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet " |
| 1444 | "header; cannot generate packet length\n"); |
| 1445 | goto out; |
| 1446 | } |
| 1447 | (*packet_size) += packet_size_length; |
| 1448 | dest[(*packet_size)++] = 0x03; /* version 3 */ |
| 1449 | memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE); |
| 1450 | (*packet_size) += ECRYPTFS_SIG_SIZE; |
| 1451 | dest[(*packet_size)++] = RFC2440_CIPHER_RSA; |
| 1452 | memcpy(&dest[(*packet_size)], key_rec->enc_key, |
| 1453 | key_rec->enc_key_size); |
| 1454 | (*packet_size) += key_rec->enc_key_size; |
| 1455 | out: |
| 1456 | if (rc) |
| 1457 | (*packet_size) = 0; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1458 | else |
| 1459 | (*remaining_bytes) -= (*packet_size); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1460 | return rc; |
| 1461 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1462 | |
| 1463 | /** |
| 1464 | * write_tag_11_packet |
| 1465 | * @dest: Target into which Tag 11 packet is to be written |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1466 | * @remaining_bytes: Maximum packet length |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1467 | * @contents: Byte array of contents to copy in |
| 1468 | * @contents_length: Number of bytes in contents |
| 1469 | * @packet_length: Length of the Tag 11 packet written; zero on error |
| 1470 | * |
| 1471 | * Returns zero on success; non-zero on error. |
| 1472 | */ |
| 1473 | static int |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 1474 | write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents, |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1475 | size_t contents_length, size_t *packet_length) |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1476 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1477 | size_t packet_size_length; |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1478 | size_t max_packet_size; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1479 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1480 | |
| 1481 | (*packet_length) = 0; |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1482 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1483 | * packet tag 11 */ |
| 1484 | max_packet_size = (1 /* Tag 11 identifier */ |
| 1485 | + 3 /* Max Tag 11 packet size */ |
| 1486 | + 1 /* Binary format specifier */ |
| 1487 | + 1 /* Filename length */ |
| 1488 | + 8 /* Filename ("_CONSOLE") */ |
| 1489 | + 4 /* Modification date */ |
| 1490 | + contents_length); /* Literal data */ |
| 1491 | if (max_packet_size > (*remaining_bytes)) { |
| 1492 | printk(KERN_ERR "Packet length larger than maximum allowable; " |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 1493 | "need up to [%td] bytes, but there are only [%td] " |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1494 | "available\n", max_packet_size, (*remaining_bytes)); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1495 | rc = -EINVAL; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1496 | goto out; |
| 1497 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1498 | dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE; |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 1499 | rc = ecryptfs_write_packet_length(&dest[(*packet_length)], |
| 1500 | (max_packet_size - 4), |
| 1501 | &packet_size_length); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1502 | if (rc) { |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1503 | printk(KERN_ERR "Error generating tag 11 packet header; cannot " |
| 1504 | "generate packet length. rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1505 | goto out; |
| 1506 | } |
| 1507 | (*packet_length) += packet_size_length; |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1508 | dest[(*packet_length)++] = 0x62; /* binary data format specifier */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1509 | dest[(*packet_length)++] = 8; |
| 1510 | memcpy(&dest[(*packet_length)], "_CONSOLE", 8); |
| 1511 | (*packet_length) += 8; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1512 | memset(&dest[(*packet_length)], 0x00, 4); |
| 1513 | (*packet_length) += 4; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1514 | memcpy(&dest[(*packet_length)], contents, contents_length); |
| 1515 | (*packet_length) += contents_length; |
| 1516 | out: |
| 1517 | if (rc) |
| 1518 | (*packet_length) = 0; |
Michael Halcrow | 146a460 | 2007-10-16 01:27:58 -0700 | [diff] [blame] | 1519 | else |
| 1520 | (*remaining_bytes) -= (*packet_length); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1521 | return rc; |
| 1522 | } |
| 1523 | |
| 1524 | /** |
| 1525 | * write_tag_3_packet |
| 1526 | * @dest: Buffer into which to write the packet |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1527 | * @remaining_bytes: Maximum number of bytes that can be written |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1528 | * @auth_tok: Authentication token |
| 1529 | * @crypt_stat: The cryptographic context |
| 1530 | * @key_rec: encrypted key |
| 1531 | * @packet_size: This function will write the number of bytes that end |
| 1532 | * up constituting the packet; set to zero on error |
| 1533 | * |
| 1534 | * Returns zero on success; non-zero on error. |
| 1535 | */ |
| 1536 | static int |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1537 | write_tag_3_packet(char *dest, size_t *remaining_bytes, |
| 1538 | struct ecryptfs_auth_tok *auth_tok, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1539 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1540 | struct ecryptfs_key_record *key_rec, size_t *packet_size) |
| 1541 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1542 | size_t i; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1543 | size_t encrypted_session_key_valid = 0; |
| 1544 | char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES]; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1545 | struct scatterlist dst_sg; |
| 1546 | struct scatterlist src_sg; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1547 | struct mutex *tfm_mutex = NULL; |
Trevor Highland | 19e66a6 | 2008-02-06 01:38:36 -0800 | [diff] [blame] | 1548 | u8 cipher_code; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1549 | size_t packet_size_length; |
| 1550 | size_t max_packet_size; |
| 1551 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1552 | crypt_stat->mount_crypt_stat; |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1553 | struct blkcipher_desc desc = { |
| 1554 | .tfm = NULL, |
| 1555 | .flags = CRYPTO_TFM_REQ_MAY_SLEEP |
| 1556 | }; |
| 1557 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1558 | |
| 1559 | (*packet_size) = 0; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1560 | ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1561 | ECRYPTFS_SIG_SIZE); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1562 | rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex, |
| 1563 | crypt_stat->cipher); |
| 1564 | if (unlikely(rc)) { |
| 1565 | printk(KERN_ERR "Internal error whilst attempting to get " |
| 1566 | "tfm and mutex for cipher name [%s]; rc = [%d]\n", |
| 1567 | crypt_stat->cipher, rc); |
| 1568 | goto out; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1569 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1570 | if (mount_crypt_stat->global_default_cipher_key_size == 0) { |
| 1571 | struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm); |
| 1572 | |
| 1573 | printk(KERN_WARNING "No key size specified at mount; " |
| 1574 | "defaulting to [%d]\n", alg->max_keysize); |
| 1575 | mount_crypt_stat->global_default_cipher_key_size = |
| 1576 | alg->max_keysize; |
| 1577 | } |
| 1578 | if (crypt_stat->key_size == 0) |
| 1579 | crypt_stat->key_size = |
| 1580 | mount_crypt_stat->global_default_cipher_key_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1581 | if (auth_tok->session_key.encrypted_key_size == 0) |
| 1582 | auth_tok->session_key.encrypted_key_size = |
| 1583 | crypt_stat->key_size; |
| 1584 | if (crypt_stat->key_size == 24 |
| 1585 | && strcmp("aes", crypt_stat->cipher) == 0) { |
| 1586 | memset((crypt_stat->key + 24), 0, 8); |
| 1587 | auth_tok->session_key.encrypted_key_size = 32; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1588 | } else |
| 1589 | auth_tok->session_key.encrypted_key_size = crypt_stat->key_size; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1590 | key_rec->enc_key_size = |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1591 | auth_tok->session_key.encrypted_key_size; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1592 | encrypted_session_key_valid = 0; |
| 1593 | for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++) |
| 1594 | encrypted_session_key_valid |= |
| 1595 | auth_tok->session_key.encrypted_key[i]; |
| 1596 | if (encrypted_session_key_valid) { |
| 1597 | ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; " |
| 1598 | "using auth_tok->session_key.encrypted_key, " |
| 1599 | "where key_rec->enc_key_size = [%d]\n", |
| 1600 | key_rec->enc_key_size); |
| 1601 | memcpy(key_rec->enc_key, |
| 1602 | auth_tok->session_key.encrypted_key, |
| 1603 | key_rec->enc_key_size); |
| 1604 | goto encrypted_session_key_set; |
| 1605 | } |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1606 | if (auth_tok->token.password.flags & |
| 1607 | ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1608 | ecryptfs_printk(KERN_DEBUG, "Using previously generated " |
| 1609 | "session key encryption key of size [%d]\n", |
| 1610 | auth_tok->token.password. |
| 1611 | session_key_encryption_key_bytes); |
| 1612 | memcpy(session_key_encryption_key, |
| 1613 | auth_tok->token.password.session_key_encryption_key, |
| 1614 | crypt_stat->key_size); |
| 1615 | ecryptfs_printk(KERN_DEBUG, |
| 1616 | "Cached session key " "encryption key: \n"); |
| 1617 | if (ecryptfs_verbosity > 0) |
| 1618 | ecryptfs_dump_hex(session_key_encryption_key, 16); |
| 1619 | } |
| 1620 | if (unlikely(ecryptfs_verbosity > 0)) { |
| 1621 | ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n"); |
| 1622 | ecryptfs_dump_hex(session_key_encryption_key, 16); |
| 1623 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1624 | rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size, |
| 1625 | &src_sg, 1); |
| 1626 | if (rc != 1) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1627 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1628 | "for crypt_stat session key; expected rc = 1; " |
| 1629 | "got rc = [%d]. key_rec->enc_key_size = [%d]\n", |
| 1630 | rc, key_rec->enc_key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1631 | rc = -ENOMEM; |
| 1632 | goto out; |
| 1633 | } |
Michael Halcrow | 5dda699 | 2007-10-16 01:28:06 -0700 | [diff] [blame] | 1634 | rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size, |
| 1635 | &dst_sg, 1); |
| 1636 | if (rc != 1) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1637 | ecryptfs_printk(KERN_ERR, "Error generating scatterlist " |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1638 | "for crypt_stat encrypted session key; " |
| 1639 | "expected rc = 1; got rc = [%d]. " |
| 1640 | "key_rec->enc_key_size = [%d]\n", rc, |
| 1641 | key_rec->enc_key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1642 | rc = -ENOMEM; |
| 1643 | goto out; |
| 1644 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1645 | mutex_lock(tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1646 | rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key, |
| 1647 | crypt_stat->key_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1648 | if (rc < 0) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1649 | mutex_unlock(tfm_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1650 | ecryptfs_printk(KERN_ERR, "Error setting key for crypto " |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1651 | "context; rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1652 | goto out; |
| 1653 | } |
| 1654 | rc = 0; |
| 1655 | ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n", |
| 1656 | crypt_stat->key_size); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1657 | rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg, |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1658 | (*key_rec).enc_key_size); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1659 | mutex_unlock(tfm_mutex); |
Michael Halcrow | 8bba066 | 2006-10-30 22:07:18 -0800 | [diff] [blame] | 1660 | if (rc) { |
| 1661 | printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc); |
| 1662 | goto out; |
| 1663 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1664 | ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n"); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1665 | if (ecryptfs_verbosity > 0) { |
| 1666 | ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n", |
| 1667 | key_rec->enc_key_size); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1668 | ecryptfs_dump_hex(key_rec->enc_key, |
| 1669 | key_rec->enc_key_size); |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1670 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1671 | encrypted_session_key_set: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1672 | /* This format is inspired by OpenPGP; see RFC 2440 |
| 1673 | * packet tag 3 */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1674 | max_packet_size = (1 /* Tag 3 identifier */ |
| 1675 | + 3 /* Max Tag 3 packet size */ |
| 1676 | + 1 /* Version */ |
| 1677 | + 1 /* Cipher code */ |
| 1678 | + 1 /* S2K specifier */ |
| 1679 | + 1 /* Hash identifier */ |
| 1680 | + ECRYPTFS_SALT_SIZE /* Salt */ |
| 1681 | + 1 /* Hash iterations */ |
| 1682 | + key_rec->enc_key_size); /* Encrypted key size */ |
| 1683 | if (max_packet_size > (*remaining_bytes)) { |
Andrew Morton | 81acbcd | 2007-10-16 01:27:59 -0700 | [diff] [blame] | 1684 | printk(KERN_ERR "Packet too large; need up to [%td] bytes, but " |
| 1685 | "there are only [%td] available\n", max_packet_size, |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1686 | (*remaining_bytes)); |
| 1687 | rc = -EINVAL; |
| 1688 | goto out; |
| 1689 | } |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1690 | dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1691 | /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3) |
| 1692 | * to get the number of octets in the actual Tag 3 packet */ |
Michael Halcrow | f66e883 | 2008-04-29 00:59:51 -0700 | [diff] [blame] | 1693 | rc = ecryptfs_write_packet_length(&dest[(*packet_size)], |
| 1694 | (max_packet_size - 4), |
| 1695 | &packet_size_length); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1696 | if (rc) { |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1697 | printk(KERN_ERR "Error generating tag 3 packet header; cannot " |
| 1698 | "generate packet length. rc = [%d]\n", rc); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1699 | goto out; |
| 1700 | } |
| 1701 | (*packet_size) += packet_size_length; |
| 1702 | dest[(*packet_size)++] = 0x04; /* version 4 */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1703 | /* TODO: Break from RFC2440 so that arbitrary ciphers can be |
| 1704 | * specified with strings */ |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1705 | cipher_code = ecryptfs_code_for_cipher_string(crypt_stat); |
| 1706 | if (cipher_code == 0) { |
| 1707 | ecryptfs_printk(KERN_WARNING, "Unable to generate code for " |
| 1708 | "cipher [%s]\n", crypt_stat->cipher); |
| 1709 | rc = -EINVAL; |
| 1710 | goto out; |
| 1711 | } |
| 1712 | dest[(*packet_size)++] = cipher_code; |
| 1713 | dest[(*packet_size)++] = 0x03; /* S2K */ |
| 1714 | dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */ |
| 1715 | memcpy(&dest[(*packet_size)], auth_tok->token.password.salt, |
| 1716 | ECRYPTFS_SALT_SIZE); |
| 1717 | (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */ |
| 1718 | dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */ |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1719 | memcpy(&dest[(*packet_size)], key_rec->enc_key, |
| 1720 | key_rec->enc_key_size); |
| 1721 | (*packet_size) += key_rec->enc_key_size; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1722 | out: |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1723 | if (rc) |
| 1724 | (*packet_size) = 0; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1725 | else |
| 1726 | (*remaining_bytes) -= (*packet_size); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1727 | return rc; |
| 1728 | } |
| 1729 | |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1730 | struct kmem_cache *ecryptfs_key_record_cache; |
| 1731 | |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1732 | /** |
| 1733 | * ecryptfs_generate_key_packet_set |
Michael Halcrow | 22e78fa | 2007-10-16 01:28:02 -0700 | [diff] [blame] | 1734 | * @dest_base: Virtual address from which to write the key record set |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1735 | * @crypt_stat: The cryptographic context from which the |
| 1736 | * authentication tokens will be retrieved |
| 1737 | * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat |
| 1738 | * for the global parameters |
| 1739 | * @len: The amount written |
| 1740 | * @max: The maximum amount of data allowed to be written |
| 1741 | * |
| 1742 | * Generates a key packet set and writes it to the virtual address |
| 1743 | * passed in. |
| 1744 | * |
| 1745 | * Returns zero on success; non-zero on error. |
| 1746 | */ |
| 1747 | int |
| 1748 | ecryptfs_generate_key_packet_set(char *dest_base, |
| 1749 | struct ecryptfs_crypt_stat *crypt_stat, |
| 1750 | struct dentry *ecryptfs_dentry, size_t *len, |
| 1751 | size_t max) |
| 1752 | { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1753 | struct ecryptfs_auth_tok *auth_tok; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1754 | struct ecryptfs_global_auth_tok *global_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1755 | struct ecryptfs_mount_crypt_stat *mount_crypt_stat = |
| 1756 | &ecryptfs_superblock_to_private( |
| 1757 | ecryptfs_dentry->d_sb)->mount_crypt_stat; |
| 1758 | size_t written; |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1759 | struct ecryptfs_key_record *key_rec; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1760 | struct ecryptfs_key_sig *key_sig; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1761 | int rc = 0; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1762 | |
| 1763 | (*len) = 0; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1764 | mutex_lock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1765 | key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL); |
| 1766 | if (!key_rec) { |
| 1767 | rc = -ENOMEM; |
| 1768 | goto out; |
| 1769 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1770 | list_for_each_entry(key_sig, &crypt_stat->keysig_list, |
| 1771 | crypt_stat_list) { |
| 1772 | memset(key_rec, 0, sizeof(*key_rec)); |
| 1773 | rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok, |
| 1774 | mount_crypt_stat, |
| 1775 | key_sig->keysig); |
| 1776 | if (rc) { |
| 1777 | printk(KERN_ERR "Error attempting to get the global " |
| 1778 | "auth_tok; rc = [%d]\n", rc); |
| 1779 | goto out_free; |
| 1780 | } |
| 1781 | if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) { |
| 1782 | printk(KERN_WARNING |
| 1783 | "Skipping invalid auth tok with sig = [%s]\n", |
| 1784 | global_auth_tok->sig); |
| 1785 | continue; |
| 1786 | } |
| 1787 | auth_tok = global_auth_tok->global_auth_tok; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1788 | if (auth_tok->token_type == ECRYPTFS_PASSWORD) { |
| 1789 | rc = write_tag_3_packet((dest_base + (*len)), |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1790 | &max, auth_tok, |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1791 | crypt_stat, key_rec, |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1792 | &written); |
| 1793 | if (rc) { |
| 1794 | ecryptfs_printk(KERN_WARNING, "Error " |
| 1795 | "writing tag 3 packet\n"); |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1796 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1797 | } |
| 1798 | (*len) += written; |
| 1799 | /* Write auth tok signature packet */ |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1800 | rc = write_tag_11_packet((dest_base + (*len)), &max, |
| 1801 | key_rec->sig, |
| 1802 | ECRYPTFS_SIG_SIZE, &written); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1803 | if (rc) { |
| 1804 | ecryptfs_printk(KERN_ERR, "Error writing " |
| 1805 | "auth tok signature packet\n"); |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1806 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1807 | } |
| 1808 | (*len) += written; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1809 | } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) { |
| 1810 | rc = write_tag_1_packet(dest_base + (*len), |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1811 | &max, auth_tok, |
| 1812 | crypt_stat, key_rec, &written); |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1813 | if (rc) { |
| 1814 | ecryptfs_printk(KERN_WARNING, "Error " |
| 1815 | "writing tag 1 packet\n"); |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1816 | goto out_free; |
Michael Halcrow | dddfa46 | 2007-02-12 00:53:44 -0800 | [diff] [blame] | 1817 | } |
| 1818 | (*len) += written; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1819 | } else { |
| 1820 | ecryptfs_printk(KERN_WARNING, "Unsupported " |
| 1821 | "authentication token type\n"); |
| 1822 | rc = -EINVAL; |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1823 | goto out_free; |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1824 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1825 | } |
| 1826 | if (likely(max > 0)) { |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1827 | dest_base[(*len)] = 0x00; |
| 1828 | } else { |
| 1829 | ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n"); |
| 1830 | rc = -EIO; |
| 1831 | } |
Michael Halcrow | eb95e7f | 2007-02-16 01:28:40 -0800 | [diff] [blame] | 1832 | out_free: |
| 1833 | kmem_cache_free(ecryptfs_key_record_cache, key_rec); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1834 | out: |
| 1835 | if (rc) |
| 1836 | (*len) = 0; |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1837 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
Michael Halcrow | 237fead | 2006-10-04 02:16:22 -0700 | [diff] [blame] | 1838 | return rc; |
| 1839 | } |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1840 | |
| 1841 | struct kmem_cache *ecryptfs_key_sig_cache; |
| 1842 | |
| 1843 | int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig) |
| 1844 | { |
| 1845 | struct ecryptfs_key_sig *new_key_sig; |
| 1846 | int rc = 0; |
| 1847 | |
| 1848 | new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL); |
| 1849 | if (!new_key_sig) { |
| 1850 | rc = -ENOMEM; |
| 1851 | printk(KERN_ERR |
| 1852 | "Error allocating from ecryptfs_key_sig_cache\n"); |
| 1853 | goto out; |
| 1854 | } |
| 1855 | memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX); |
| 1856 | mutex_lock(&crypt_stat->keysig_list_mutex); |
| 1857 | list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list); |
| 1858 | mutex_unlock(&crypt_stat->keysig_list_mutex); |
| 1859 | out: |
| 1860 | return rc; |
| 1861 | } |
| 1862 | |
| 1863 | struct kmem_cache *ecryptfs_global_auth_tok_cache; |
| 1864 | |
| 1865 | int |
| 1866 | ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat, |
| 1867 | char *sig) |
| 1868 | { |
| 1869 | struct ecryptfs_global_auth_tok *new_auth_tok; |
| 1870 | int rc = 0; |
| 1871 | |
Eric Sandeen | 459e216 | 2007-12-17 16:19:52 -0800 | [diff] [blame] | 1872 | new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache, |
Michael Halcrow | f4aad16 | 2007-10-16 01:27:53 -0700 | [diff] [blame] | 1873 | GFP_KERNEL); |
| 1874 | if (!new_auth_tok) { |
| 1875 | rc = -ENOMEM; |
| 1876 | printk(KERN_ERR "Error allocating from " |
| 1877 | "ecryptfs_global_auth_tok_cache\n"); |
| 1878 | goto out; |
| 1879 | } |
| 1880 | memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX); |
| 1881 | new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0'; |
| 1882 | mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 1883 | list_add(&new_auth_tok->mount_crypt_stat_list, |
| 1884 | &mount_crypt_stat->global_auth_tok_list); |
| 1885 | mount_crypt_stat->num_global_auth_toks++; |
| 1886 | mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex); |
| 1887 | out: |
| 1888 | return rc; |
| 1889 | } |
| 1890 | |