keystore.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058
  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>
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  24. * 02111-1307, USA.
  25. */
  26. #include <linux/string.h>
  27. #include <linux/sched.h>
  28. #include <linux/syscalls.h>
  29. #include <linux/pagemap.h>
  30. #include <linux/key.h>
  31. #include <linux/random.h>
  32. #include <linux/crypto.h>
  33. #include <linux/scatterlist.h>
  34. #include "ecryptfs_kernel.h"
  35. /**
  36. * request_key returned an error instead of a valid key address;
  37. * determine the type of error, make appropriate log entries, and
  38. * return an error code.
  39. */
  40. int process_request_key_err(long err_code)
  41. {
  42. int rc = 0;
  43. switch (err_code) {
  44. case ENOKEY:
  45. ecryptfs_printk(KERN_WARNING, "No key\n");
  46. rc = -ENOENT;
  47. break;
  48. case EKEYEXPIRED:
  49. ecryptfs_printk(KERN_WARNING, "Key expired\n");
  50. rc = -ETIME;
  51. break;
  52. case EKEYREVOKED:
  53. ecryptfs_printk(KERN_WARNING, "Key revoked\n");
  54. rc = -EINVAL;
  55. break;
  56. default:
  57. ecryptfs_printk(KERN_WARNING, "Unknown error code: "
  58. "[0x%.16x]\n", err_code);
  59. rc = -EINVAL;
  60. }
  61. return rc;
  62. }
  63. static void wipe_auth_tok_list(struct list_head *auth_tok_list_head)
  64. {
  65. struct list_head *walker;
  66. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  67. walker = auth_tok_list_head->next;
  68. while (walker != auth_tok_list_head) {
  69. auth_tok_list_item =
  70. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  71. list);
  72. walker = auth_tok_list_item->list.next;
  73. memset(auth_tok_list_item, 0,
  74. sizeof(struct ecryptfs_auth_tok_list_item));
  75. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  76. auth_tok_list_item);
  77. }
  78. }
  79. struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  80. /**
  81. * parse_packet_length
  82. * @data: Pointer to memory containing length at offset
  83. * @size: This function writes the decoded size to this memory
  84. * address; zero on error
  85. * @length_size: The number of bytes occupied by the encoded length
  86. *
  87. * Returns Zero on success
  88. */
  89. static int parse_packet_length(unsigned char *data, size_t *size,
  90. size_t *length_size)
  91. {
  92. int rc = 0;
  93. (*length_size) = 0;
  94. (*size) = 0;
  95. if (data[0] < 192) {
  96. /* One-byte length */
  97. (*size) = data[0];
  98. (*length_size) = 1;
  99. } else if (data[0] < 224) {
  100. /* Two-byte length */
  101. (*size) = ((data[0] - 192) * 256);
  102. (*size) += (data[1] + 192);
  103. (*length_size) = 2;
  104. } else if (data[0] == 255) {
  105. /* Five-byte length; we're not supposed to see this */
  106. ecryptfs_printk(KERN_ERR, "Five-byte packet length not "
  107. "supported\n");
  108. rc = -EINVAL;
  109. goto out;
  110. } else {
  111. ecryptfs_printk(KERN_ERR, "Error parsing packet length\n");
  112. rc = -EINVAL;
  113. goto out;
  114. }
  115. out:
  116. return rc;
  117. }
  118. /**
  119. * write_packet_length
  120. * @dest: The byte array target into which to write the
  121. * length. Must have at least 5 bytes allocated.
  122. * @size: The length to write.
  123. * @packet_size_length: The number of bytes used to encode the
  124. * packet length is written to this address.
  125. *
  126. * Returns zero on success; non-zero on error.
  127. */
  128. static int write_packet_length(char *dest, size_t size,
  129. size_t *packet_size_length)
  130. {
  131. int rc = 0;
  132. if (size < 192) {
  133. dest[0] = size;
  134. (*packet_size_length) = 1;
  135. } else if (size < 65536) {
  136. dest[0] = (((size - 192) / 256) + 192);
  137. dest[1] = ((size - 192) % 256);
  138. (*packet_size_length) = 2;
  139. } else {
  140. rc = -EINVAL;
  141. ecryptfs_printk(KERN_WARNING,
  142. "Unsupported packet size: [%d]\n", size);
  143. }
  144. return rc;
  145. }
  146. /**
  147. * parse_tag_3_packet
  148. * @crypt_stat: The cryptographic context to modify based on packet
  149. * contents.
  150. * @data: The raw bytes of the packet.
  151. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  152. * a new authentication token will be placed at the end
  153. * of this list for this packet.
  154. * @new_auth_tok: Pointer to a pointer to memory that this function
  155. * allocates; sets the memory address of the pointer to
  156. * NULL on error. This object is added to the
  157. * auth_tok_list.
  158. * @packet_size: This function writes the size of the parsed packet
  159. * into this memory location; zero on error.
  160. * @max_packet_size: maximum number of bytes to parse
  161. *
  162. * Returns zero on success; non-zero on error.
  163. */
  164. static int
  165. parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  166. unsigned char *data, struct list_head *auth_tok_list,
  167. struct ecryptfs_auth_tok **new_auth_tok,
  168. size_t *packet_size, size_t max_packet_size)
  169. {
  170. int rc = 0;
  171. size_t body_size;
  172. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  173. size_t length_size;
  174. (*packet_size) = 0;
  175. (*new_auth_tok) = NULL;
  176. /* we check that:
  177. * one byte for the Tag 3 ID flag
  178. * two bytes for the body size
  179. * do not exceed the maximum_packet_size
  180. */
  181. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  182. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  183. rc = -EINVAL;
  184. goto out;
  185. }
  186. /* check for Tag 3 identifyer - one byte */
  187. if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
  188. ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
  189. ECRYPTFS_TAG_3_PACKET_TYPE);
  190. rc = -EINVAL;
  191. goto out;
  192. }
  193. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  194. * at end of function upon failure */
  195. auth_tok_list_item =
  196. kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache, SLAB_KERNEL);
  197. if (!auth_tok_list_item) {
  198. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  199. rc = -ENOMEM;
  200. goto out;
  201. }
  202. memset(auth_tok_list_item, 0,
  203. sizeof(struct ecryptfs_auth_tok_list_item));
  204. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  205. /* check for body size - one to two bytes */
  206. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  207. &length_size);
  208. if (rc) {
  209. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  210. "rc = [%d]\n", rc);
  211. goto out_free;
  212. }
  213. if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
  214. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  215. body_size);
  216. rc = -EINVAL;
  217. goto out_free;
  218. }
  219. (*packet_size) += length_size;
  220. /* now we know the length of the remainting Tag 3 packet size:
  221. * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
  222. * number of hash iterations
  223. * ECRYPTFS_SALT_SIZE bytes for salt
  224. * body_size bytes minus the stuff above is the encrypted key size
  225. */
  226. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  227. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  228. rc = -EINVAL;
  229. goto out_free;
  230. }
  231. /* There are 5 characters of additional information in the
  232. * packet */
  233. (*new_auth_tok)->session_key.encrypted_key_size =
  234. body_size - (0x05 + ECRYPTFS_SALT_SIZE);
  235. ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
  236. (*new_auth_tok)->session_key.encrypted_key_size);
  237. /* Version 4 (from RFC2440) - one byte */
  238. if (unlikely(data[(*packet_size)++] != 0x04)) {
  239. ecryptfs_printk(KERN_DEBUG, "Unknown version number "
  240. "[%d]\n", data[(*packet_size) - 1]);
  241. rc = -EINVAL;
  242. goto out_free;
  243. }
  244. /* cipher - one byte */
  245. ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  246. (u16)data[(*packet_size)]);
  247. /* A little extra work to differentiate among the AES key
  248. * sizes; see RFC2440 */
  249. switch(data[(*packet_size)++]) {
  250. case RFC2440_CIPHER_AES_192:
  251. crypt_stat->key_size = 24;
  252. break;
  253. default:
  254. crypt_stat->key_size =
  255. (*new_auth_tok)->session_key.encrypted_key_size;
  256. }
  257. ecryptfs_init_crypt_ctx(crypt_stat);
  258. /* S2K identifier 3 (from RFC2440) */
  259. if (unlikely(data[(*packet_size)++] != 0x03)) {
  260. ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
  261. "supported\n");
  262. rc = -ENOSYS;
  263. goto out_free;
  264. }
  265. /* TODO: finish the hash mapping */
  266. /* hash algorithm - one byte */
  267. switch (data[(*packet_size)++]) {
  268. case 0x01: /* See RFC2440 for these numbers and their mappings */
  269. /* Choose MD5 */
  270. /* salt - ECRYPTFS_SALT_SIZE bytes */
  271. memcpy((*new_auth_tok)->token.password.salt,
  272. &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  273. (*packet_size) += ECRYPTFS_SALT_SIZE;
  274. /* This conversion was taken straight from RFC2440 */
  275. /* number of hash iterations - one byte */
  276. (*new_auth_tok)->token.password.hash_iterations =
  277. ((u32) 16 + (data[(*packet_size)] & 15))
  278. << ((data[(*packet_size)] >> 4) + 6);
  279. (*packet_size)++;
  280. /* encrypted session key -
  281. * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
  282. memcpy((*new_auth_tok)->session_key.encrypted_key,
  283. &data[(*packet_size)],
  284. (*new_auth_tok)->session_key.encrypted_key_size);
  285. (*packet_size) +=
  286. (*new_auth_tok)->session_key.encrypted_key_size;
  287. (*new_auth_tok)->session_key.flags &=
  288. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  289. (*new_auth_tok)->session_key.flags |=
  290. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  291. (*new_auth_tok)->token.password.hash_algo = 0x01;
  292. break;
  293. default:
  294. ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  295. "[%d]\n", data[(*packet_size) - 1]);
  296. rc = -ENOSYS;
  297. goto out_free;
  298. }
  299. (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  300. /* TODO: Parametarize; we might actually want userspace to
  301. * decrypt the session key. */
  302. ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
  303. ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  304. ECRYPTFS_CLEAR_FLAG((*new_auth_tok)->session_key.flags,
  305. ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  306. list_add(&auth_tok_list_item->list, auth_tok_list);
  307. goto out;
  308. out_free:
  309. (*new_auth_tok) = NULL;
  310. memset(auth_tok_list_item, 0,
  311. sizeof(struct ecryptfs_auth_tok_list_item));
  312. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  313. auth_tok_list_item);
  314. out:
  315. if (rc)
  316. (*packet_size) = 0;
  317. return rc;
  318. }
  319. /**
  320. * parse_tag_11_packet
  321. * @data: The raw bytes of the packet
  322. * @contents: This function writes the data contents of the literal
  323. * packet into this memory location
  324. * @max_contents_bytes: The maximum number of bytes that this function
  325. * is allowed to write into contents
  326. * @tag_11_contents_size: This function writes the size of the parsed
  327. * contents into this memory location; zero on
  328. * error
  329. * @packet_size: This function writes the size of the parsed packet
  330. * into this memory location; zero on error
  331. * @max_packet_size: maximum number of bytes to parse
  332. *
  333. * Returns zero on success; non-zero on error.
  334. */
  335. static int
  336. parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  337. size_t max_contents_bytes, size_t *tag_11_contents_size,
  338. size_t *packet_size, size_t max_packet_size)
  339. {
  340. int rc = 0;
  341. size_t body_size;
  342. size_t length_size;
  343. (*packet_size) = 0;
  344. (*tag_11_contents_size) = 0;
  345. /* check that:
  346. * one byte for the Tag 11 ID flag
  347. * two bytes for the Tag 11 length
  348. * do not exceed the maximum_packet_size
  349. */
  350. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  351. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  352. rc = -EINVAL;
  353. goto out;
  354. }
  355. /* check for Tag 11 identifyer - one byte */
  356. if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
  357. ecryptfs_printk(KERN_WARNING,
  358. "Invalid tag 11 packet format\n");
  359. rc = -EINVAL;
  360. goto out;
  361. }
  362. /* get Tag 11 content length - one or two bytes */
  363. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  364. &length_size);
  365. if (rc) {
  366. ecryptfs_printk(KERN_WARNING,
  367. "Invalid tag 11 packet format\n");
  368. goto out;
  369. }
  370. (*packet_size) += length_size;
  371. if (body_size < 13) {
  372. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  373. body_size);
  374. rc = -EINVAL;
  375. goto out;
  376. }
  377. /* We have 13 bytes of surrounding packet values */
  378. (*tag_11_contents_size) = (body_size - 13);
  379. /* now we know the length of the remainting Tag 11 packet size:
  380. * 14 fix bytes for: special flag one, special flag two,
  381. * 12 skipped bytes
  382. * body_size bytes minus the stuff above is the Tag 11 content
  383. */
  384. /* FIXME why is the body size one byte smaller than the actual
  385. * size of the body?
  386. * this seems to be an error here as well as in
  387. * write_tag_11_packet() */
  388. if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
  389. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  390. rc = -EINVAL;
  391. goto out;
  392. }
  393. /* special flag one - one byte */
  394. if (data[(*packet_size)++] != 0x62) {
  395. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  396. rc = -EINVAL;
  397. goto out;
  398. }
  399. /* special flag two - one byte */
  400. if (data[(*packet_size)++] != 0x08) {
  401. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  402. rc = -EINVAL;
  403. goto out;
  404. }
  405. /* skip the next 12 bytes */
  406. (*packet_size) += 12; /* We don't care about the filename or
  407. * the timestamp */
  408. /* get the Tag 11 contents - tag_11_contents_size bytes */
  409. memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  410. (*packet_size) += (*tag_11_contents_size);
  411. out:
  412. if (rc) {
  413. (*packet_size) = 0;
  414. (*tag_11_contents_size) = 0;
  415. }
  416. return rc;
  417. }
  418. /**
  419. * decrypt_session_key - Decrypt the session key with the given auth_tok.
  420. *
  421. * Returns Zero on success; non-zero error otherwise.
  422. */
  423. static int decrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
  424. struct ecryptfs_crypt_stat *crypt_stat)
  425. {
  426. int rc = 0;
  427. struct ecryptfs_password *password_s_ptr;
  428. struct crypto_tfm *tfm = NULL;
  429. struct scatterlist src_sg[2], dst_sg[2];
  430. struct mutex *tfm_mutex = NULL;
  431. /* TODO: Use virt_to_scatterlist for these */
  432. char *encrypted_session_key;
  433. char *session_key;
  434. password_s_ptr = &auth_tok->token.password;
  435. if (ECRYPTFS_CHECK_FLAG(password_s_ptr->flags,
  436. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET))
  437. ecryptfs_printk(KERN_DEBUG, "Session key encryption key "
  438. "set; skipping key generation\n");
  439. ecryptfs_printk(KERN_DEBUG, "Session key encryption key (size [%d])"
  440. ":\n",
  441. password_s_ptr->session_key_encryption_key_bytes);
  442. if (ecryptfs_verbosity > 0)
  443. ecryptfs_dump_hex(password_s_ptr->session_key_encryption_key,
  444. password_s_ptr->
  445. session_key_encryption_key_bytes);
  446. if (!strcmp(crypt_stat->cipher,
  447. crypt_stat->mount_crypt_stat->global_default_cipher_name)
  448. && crypt_stat->mount_crypt_stat->global_key_tfm) {
  449. tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
  450. tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
  451. } else {
  452. tfm = crypto_alloc_tfm(crypt_stat->cipher,
  453. CRYPTO_TFM_REQ_WEAK_KEY);
  454. if (!tfm) {
  455. printk(KERN_ERR "Error allocating crypto context\n");
  456. rc = -ENOMEM;
  457. goto out;
  458. }
  459. }
  460. if (tfm_mutex)
  461. mutex_lock(tfm_mutex);
  462. rc = crypto_cipher_setkey(tfm,
  463. password_s_ptr->session_key_encryption_key,
  464. crypt_stat->key_size);
  465. if (rc < 0) {
  466. printk(KERN_ERR "Error setting key for crypto context\n");
  467. rc = -EINVAL;
  468. goto out_free_tfm;
  469. }
  470. /* TODO: virt_to_scatterlist */
  471. encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
  472. if (!encrypted_session_key) {
  473. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  474. rc = -ENOMEM;
  475. goto out_free_tfm;
  476. }
  477. session_key = (char *)__get_free_page(GFP_KERNEL);
  478. if (!session_key) {
  479. kfree(encrypted_session_key);
  480. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  481. rc = -ENOMEM;
  482. goto out_free_tfm;
  483. }
  484. memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
  485. auth_tok->session_key.encrypted_key_size);
  486. src_sg[0].page = virt_to_page(encrypted_session_key);
  487. src_sg[0].offset = 0;
  488. BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
  489. src_sg[0].length = auth_tok->session_key.encrypted_key_size;
  490. dst_sg[0].page = virt_to_page(session_key);
  491. dst_sg[0].offset = 0;
  492. auth_tok->session_key.decrypted_key_size =
  493. auth_tok->session_key.encrypted_key_size;
  494. dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
  495. /* TODO: Handle error condition */
  496. crypto_cipher_decrypt(tfm, dst_sg, src_sg,
  497. auth_tok->session_key.encrypted_key_size);
  498. auth_tok->session_key.decrypted_key_size =
  499. auth_tok->session_key.encrypted_key_size;
  500. memcpy(auth_tok->session_key.decrypted_key, session_key,
  501. auth_tok->session_key.decrypted_key_size);
  502. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  503. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  504. auth_tok->session_key.decrypted_key_size);
  505. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  506. ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
  507. if (ecryptfs_verbosity > 0)
  508. ecryptfs_dump_hex(crypt_stat->key,
  509. crypt_stat->key_size);
  510. memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
  511. free_page((unsigned long)encrypted_session_key);
  512. memset(session_key, 0, PAGE_CACHE_SIZE);
  513. free_page((unsigned long)session_key);
  514. out_free_tfm:
  515. if (tfm_mutex)
  516. mutex_unlock(tfm_mutex);
  517. else
  518. crypto_free_tfm(tfm);
  519. out:
  520. return rc;
  521. }
  522. /**
  523. * ecryptfs_parse_packet_set
  524. * @dest: The header page in memory
  525. * @version: Version of file format, to guide parsing behavior
  526. *
  527. * Get crypt_stat to have the file's session key if the requisite key
  528. * is available to decrypt the session key.
  529. *
  530. * Returns Zero if a valid authentication token was retrieved and
  531. * processed; negative value for file not encrypted or for error
  532. * conditions.
  533. */
  534. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  535. unsigned char *src,
  536. struct dentry *ecryptfs_dentry)
  537. {
  538. size_t i = 0;
  539. int rc = 0;
  540. size_t found_auth_tok = 0;
  541. size_t next_packet_is_auth_tok_packet;
  542. char sig[ECRYPTFS_SIG_SIZE_HEX];
  543. struct list_head auth_tok_list;
  544. struct list_head *walker;
  545. struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
  546. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  547. &ecryptfs_superblock_to_private(
  548. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  549. struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
  550. size_t packet_size;
  551. struct ecryptfs_auth_tok *new_auth_tok;
  552. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  553. size_t tag_11_contents_size;
  554. size_t tag_11_packet_size;
  555. INIT_LIST_HEAD(&auth_tok_list);
  556. /* Parse the header to find as many packets as we can, these will be
  557. * added the our &auth_tok_list */
  558. next_packet_is_auth_tok_packet = 1;
  559. while (next_packet_is_auth_tok_packet) {
  560. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  561. switch (src[i]) {
  562. case ECRYPTFS_TAG_3_PACKET_TYPE:
  563. rc = parse_tag_3_packet(crypt_stat,
  564. (unsigned char *)&src[i],
  565. &auth_tok_list, &new_auth_tok,
  566. &packet_size, max_packet_size);
  567. if (rc) {
  568. ecryptfs_printk(KERN_ERR, "Error parsing "
  569. "tag 3 packet\n");
  570. rc = -EIO;
  571. goto out_wipe_list;
  572. }
  573. i += packet_size;
  574. rc = parse_tag_11_packet((unsigned char *)&src[i],
  575. sig_tmp_space,
  576. ECRYPTFS_SIG_SIZE,
  577. &tag_11_contents_size,
  578. &tag_11_packet_size,
  579. max_packet_size);
  580. if (rc) {
  581. ecryptfs_printk(KERN_ERR, "No valid "
  582. "(ecryptfs-specific) literal "
  583. "packet containing "
  584. "authentication token "
  585. "signature found after "
  586. "tag 3 packet\n");
  587. rc = -EIO;
  588. goto out_wipe_list;
  589. }
  590. i += tag_11_packet_size;
  591. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  592. ecryptfs_printk(KERN_ERR, "Expected "
  593. "signature of size [%d]; "
  594. "read size [%d]\n",
  595. ECRYPTFS_SIG_SIZE,
  596. tag_11_contents_size);
  597. rc = -EIO;
  598. goto out_wipe_list;
  599. }
  600. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  601. sig_tmp_space, tag_11_contents_size);
  602. new_auth_tok->token.password.signature[
  603. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  604. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  605. ECRYPTFS_ENCRYPTED);
  606. break;
  607. case ECRYPTFS_TAG_11_PACKET_TYPE:
  608. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  609. "(Tag 11 not allowed by itself)\n");
  610. rc = -EIO;
  611. goto out_wipe_list;
  612. break;
  613. default:
  614. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  615. "[%d] of the file header; hex value of "
  616. "character is [0x%.2x]\n", i, src[i]);
  617. next_packet_is_auth_tok_packet = 0;
  618. }
  619. }
  620. if (list_empty(&auth_tok_list)) {
  621. rc = -EINVAL; /* Do not support non-encrypted files in
  622. * the 0.1 release */
  623. goto out;
  624. }
  625. /* If we have a global auth tok, then we should try to use
  626. * it */
  627. if (mount_crypt_stat->global_auth_tok) {
  628. memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
  629. ECRYPTFS_SIG_SIZE_HEX);
  630. chosen_auth_tok = mount_crypt_stat->global_auth_tok;
  631. } else
  632. BUG(); /* We should always have a global auth tok in
  633. * the 0.1 release */
  634. /* Scan list to see if our chosen_auth_tok works */
  635. list_for_each(walker, &auth_tok_list) {
  636. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  637. auth_tok_list_item =
  638. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  639. list);
  640. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  641. if (unlikely(ecryptfs_verbosity > 0)) {
  642. ecryptfs_printk(KERN_DEBUG,
  643. "Considering cadidate auth tok:\n");
  644. ecryptfs_dump_auth_tok(candidate_auth_tok);
  645. }
  646. /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
  647. if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
  648. && !strncmp(candidate_auth_tok->token.password.signature,
  649. sig, ECRYPTFS_SIG_SIZE_HEX)) {
  650. found_auth_tok = 1;
  651. goto leave_list;
  652. /* TODO: Transfer the common salt into the
  653. * crypt_stat salt */
  654. }
  655. }
  656. leave_list:
  657. if (!found_auth_tok) {
  658. ecryptfs_printk(KERN_ERR, "Could not find authentication "
  659. "token on temporary list for sig [%.*s]\n",
  660. ECRYPTFS_SIG_SIZE_HEX, sig);
  661. rc = -EIO;
  662. goto out_wipe_list;
  663. } else {
  664. memcpy(&(candidate_auth_tok->token.password),
  665. &(chosen_auth_tok->token.password),
  666. sizeof(struct ecryptfs_password));
  667. rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
  668. if (rc) {
  669. ecryptfs_printk(KERN_ERR, "Error decrypting the "
  670. "session key\n");
  671. goto out_wipe_list;
  672. }
  673. rc = ecryptfs_compute_root_iv(crypt_stat);
  674. if (rc) {
  675. ecryptfs_printk(KERN_ERR, "Error computing "
  676. "the root IV\n");
  677. goto out_wipe_list;
  678. }
  679. }
  680. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  681. if (rc) {
  682. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  683. "context for cipher [%s]; rc = [%d]\n",
  684. crypt_stat->cipher, rc);
  685. }
  686. out_wipe_list:
  687. wipe_auth_tok_list(&auth_tok_list);
  688. out:
  689. return rc;
  690. }
  691. /**
  692. * write_tag_11_packet
  693. * @dest: Target into which Tag 11 packet is to be written
  694. * @max: Maximum packet length
  695. * @contents: Byte array of contents to copy in
  696. * @contents_length: Number of bytes in contents
  697. * @packet_length: Length of the Tag 11 packet written; zero on error
  698. *
  699. * Returns zero on success; non-zero on error.
  700. */
  701. static int
  702. write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
  703. size_t *packet_length)
  704. {
  705. int rc = 0;
  706. size_t packet_size_length;
  707. (*packet_length) = 0;
  708. if ((13 + contents_length) > max) {
  709. rc = -EINVAL;
  710. ecryptfs_printk(KERN_ERR, "Packet length larger than "
  711. "maximum allowable\n");
  712. goto out;
  713. }
  714. /* General packet header */
  715. /* Packet tag */
  716. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  717. /* Packet length */
  718. rc = write_packet_length(&dest[(*packet_length)],
  719. (13 + contents_length), &packet_size_length);
  720. if (rc) {
  721. ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
  722. "header; cannot generate packet length\n");
  723. goto out;
  724. }
  725. (*packet_length) += packet_size_length;
  726. /* Tag 11 specific */
  727. /* One-octet field that describes how the data is formatted */
  728. dest[(*packet_length)++] = 0x62; /* binary data */
  729. /* One-octet filename length followed by filename */
  730. dest[(*packet_length)++] = 8;
  731. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  732. (*packet_length) += 8;
  733. /* Four-octet number indicating modification date */
  734. memset(&dest[(*packet_length)], 0x00, 4);
  735. (*packet_length) += 4;
  736. /* Remainder is literal data */
  737. memcpy(&dest[(*packet_length)], contents, contents_length);
  738. (*packet_length) += contents_length;
  739. out:
  740. if (rc)
  741. (*packet_length) = 0;
  742. return rc;
  743. }
  744. /**
  745. * write_tag_3_packet
  746. * @dest: Buffer into which to write the packet
  747. * @max: Maximum number of bytes that can be written
  748. * @auth_tok: Authentication token
  749. * @crypt_stat: The cryptographic context
  750. * @key_rec: encrypted key
  751. * @packet_size: This function will write the number of bytes that end
  752. * up constituting the packet; set to zero on error
  753. *
  754. * Returns zero on success; non-zero on error.
  755. */
  756. static int
  757. write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
  758. struct ecryptfs_crypt_stat *crypt_stat,
  759. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  760. {
  761. int rc = 0;
  762. size_t i;
  763. size_t signature_is_valid = 0;
  764. size_t encrypted_session_key_valid = 0;
  765. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  766. struct scatterlist dest_sg[2];
  767. struct scatterlist src_sg[2];
  768. struct crypto_tfm *tfm = NULL;
  769. struct mutex *tfm_mutex = NULL;
  770. size_t key_rec_size;
  771. size_t packet_size_length;
  772. size_t cipher_code;
  773. (*packet_size) = 0;
  774. /* Check for a valid signature on the auth_tok */
  775. for (i = 0; i < ECRYPTFS_SIG_SIZE_HEX; i++)
  776. signature_is_valid |= auth_tok->token.password.signature[i];
  777. if (!signature_is_valid)
  778. BUG();
  779. ecryptfs_from_hex((*key_rec).sig, auth_tok->token.password.signature,
  780. ECRYPTFS_SIG_SIZE);
  781. encrypted_session_key_valid = 0;
  782. for (i = 0; i < crypt_stat->key_size; i++)
  783. encrypted_session_key_valid |=
  784. auth_tok->session_key.encrypted_key[i];
  785. if (encrypted_session_key_valid) {
  786. memcpy((*key_rec).enc_key,
  787. auth_tok->session_key.encrypted_key,
  788. auth_tok->session_key.encrypted_key_size);
  789. goto encrypted_session_key_set;
  790. }
  791. if (auth_tok->session_key.encrypted_key_size == 0)
  792. auth_tok->session_key.encrypted_key_size =
  793. crypt_stat->key_size;
  794. if (crypt_stat->key_size == 24
  795. && strcmp("aes", crypt_stat->cipher) == 0) {
  796. memset((crypt_stat->key + 24), 0, 8);
  797. auth_tok->session_key.encrypted_key_size = 32;
  798. }
  799. (*key_rec).enc_key_size =
  800. auth_tok->session_key.encrypted_key_size;
  801. if (ECRYPTFS_CHECK_FLAG(auth_tok->token.password.flags,
  802. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET)) {
  803. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  804. "session key encryption key of size [%d]\n",
  805. auth_tok->token.password.
  806. session_key_encryption_key_bytes);
  807. memcpy(session_key_encryption_key,
  808. auth_tok->token.password.session_key_encryption_key,
  809. crypt_stat->key_size);
  810. ecryptfs_printk(KERN_DEBUG,
  811. "Cached session key " "encryption key: \n");
  812. if (ecryptfs_verbosity > 0)
  813. ecryptfs_dump_hex(session_key_encryption_key, 16);
  814. }
  815. if (unlikely(ecryptfs_verbosity > 0)) {
  816. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  817. ecryptfs_dump_hex(session_key_encryption_key, 16);
  818. }
  819. rc = virt_to_scatterlist(crypt_stat->key,
  820. (*key_rec).enc_key_size, src_sg, 2);
  821. if (!rc) {
  822. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  823. "for crypt_stat session key\n");
  824. rc = -ENOMEM;
  825. goto out;
  826. }
  827. rc = virt_to_scatterlist((*key_rec).enc_key,
  828. (*key_rec).enc_key_size, dest_sg, 2);
  829. if (!rc) {
  830. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  831. "for crypt_stat encrypted session key\n");
  832. rc = -ENOMEM;
  833. goto out;
  834. }
  835. if (!strcmp(crypt_stat->cipher,
  836. crypt_stat->mount_crypt_stat->global_default_cipher_name)
  837. && crypt_stat->mount_crypt_stat->global_key_tfm) {
  838. tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
  839. tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
  840. } else
  841. tfm = crypto_alloc_tfm(crypt_stat->cipher, 0);
  842. if (!tfm) {
  843. ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
  844. "context for cipher [%s]\n",
  845. crypt_stat->cipher);
  846. rc = -EINVAL;
  847. goto out;
  848. }
  849. if (tfm_mutex)
  850. mutex_lock(tfm_mutex);
  851. rc = crypto_cipher_setkey(tfm, session_key_encryption_key,
  852. crypt_stat->key_size);
  853. if (rc < 0) {
  854. if (tfm_mutex)
  855. mutex_unlock(tfm_mutex);
  856. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  857. "context\n");
  858. goto out;
  859. }
  860. rc = 0;
  861. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  862. crypt_stat->key_size);
  863. crypto_cipher_encrypt(tfm, dest_sg, src_sg,
  864. (*key_rec).enc_key_size);
  865. if (tfm_mutex)
  866. mutex_unlock(tfm_mutex);
  867. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  868. if (ecryptfs_verbosity > 0)
  869. ecryptfs_dump_hex((*key_rec).enc_key,
  870. (*key_rec).enc_key_size);
  871. encrypted_session_key_set:
  872. /* Now we have a valid key_rec. Append it to the
  873. * key_rec set. */
  874. key_rec_size = (sizeof(struct ecryptfs_key_record)
  875. - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
  876. + ((*key_rec).enc_key_size));
  877. /* TODO: Include a packet size limit as a parameter to this
  878. * function once we have multi-packet headers (for versions
  879. * later than 0.1 */
  880. if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
  881. ecryptfs_printk(KERN_ERR, "Keyset too large\n");
  882. rc = -EINVAL;
  883. goto out;
  884. }
  885. /* TODO: Packet size limit */
  886. /* We have 5 bytes of surrounding packet data */
  887. if ((0x05 + ECRYPTFS_SALT_SIZE
  888. + (*key_rec).enc_key_size) >= max) {
  889. ecryptfs_printk(KERN_ERR, "Authentication token is too "
  890. "large\n");
  891. rc = -EINVAL;
  892. goto out;
  893. }
  894. /* This format is inspired by OpenPGP; see RFC 2440
  895. * packet tag 3 */
  896. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  897. /* ver+cipher+s2k+hash+salt+iter+enc_key */
  898. rc = write_packet_length(&dest[(*packet_size)],
  899. (0x05 + ECRYPTFS_SALT_SIZE
  900. + (*key_rec).enc_key_size),
  901. &packet_size_length);
  902. if (rc) {
  903. ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
  904. "header; cannot generate packet length\n");
  905. goto out;
  906. }
  907. (*packet_size) += packet_size_length;
  908. dest[(*packet_size)++] = 0x04; /* version 4 */
  909. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  910. if (cipher_code == 0) {
  911. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  912. "cipher [%s]\n", crypt_stat->cipher);
  913. rc = -EINVAL;
  914. goto out;
  915. }
  916. dest[(*packet_size)++] = cipher_code;
  917. dest[(*packet_size)++] = 0x03; /* S2K */
  918. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  919. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  920. ECRYPTFS_SALT_SIZE);
  921. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  922. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  923. memcpy(&dest[(*packet_size)], (*key_rec).enc_key,
  924. (*key_rec).enc_key_size);
  925. (*packet_size) += (*key_rec).enc_key_size;
  926. out:
  927. if (tfm && !tfm_mutex)
  928. crypto_free_tfm(tfm);
  929. if (rc)
  930. (*packet_size) = 0;
  931. return rc;
  932. }
  933. /**
  934. * ecryptfs_generate_key_packet_set
  935. * @dest: Virtual address from which to write the key record set
  936. * @crypt_stat: The cryptographic context from which the
  937. * authentication tokens will be retrieved
  938. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  939. * for the global parameters
  940. * @len: The amount written
  941. * @max: The maximum amount of data allowed to be written
  942. *
  943. * Generates a key packet set and writes it to the virtual address
  944. * passed in.
  945. *
  946. * Returns zero on success; non-zero on error.
  947. */
  948. int
  949. ecryptfs_generate_key_packet_set(char *dest_base,
  950. struct ecryptfs_crypt_stat *crypt_stat,
  951. struct dentry *ecryptfs_dentry, size_t *len,
  952. size_t max)
  953. {
  954. int rc = 0;
  955. struct ecryptfs_auth_tok *auth_tok;
  956. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  957. &ecryptfs_superblock_to_private(
  958. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  959. size_t written;
  960. struct ecryptfs_key_record key_rec;
  961. (*len) = 0;
  962. if (mount_crypt_stat->global_auth_tok) {
  963. auth_tok = mount_crypt_stat->global_auth_tok;
  964. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  965. rc = write_tag_3_packet((dest_base + (*len)),
  966. max, auth_tok,
  967. crypt_stat, &key_rec,
  968. &written);
  969. if (rc) {
  970. ecryptfs_printk(KERN_WARNING, "Error "
  971. "writing tag 3 packet\n");
  972. goto out;
  973. }
  974. (*len) += written;
  975. /* Write auth tok signature packet */
  976. rc = write_tag_11_packet(
  977. (dest_base + (*len)),
  978. (max - (*len)),
  979. key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
  980. if (rc) {
  981. ecryptfs_printk(KERN_ERR, "Error writing "
  982. "auth tok signature packet\n");
  983. goto out;
  984. }
  985. (*len) += written;
  986. } else {
  987. ecryptfs_printk(KERN_WARNING, "Unsupported "
  988. "authentication token type\n");
  989. rc = -EINVAL;
  990. goto out;
  991. }
  992. if (rc) {
  993. ecryptfs_printk(KERN_WARNING, "Error writing "
  994. "authentication token packet with sig "
  995. "= [%s]\n",
  996. mount_crypt_stat->global_auth_tok_sig);
  997. rc = -EIO;
  998. goto out;
  999. }
  1000. } else
  1001. BUG();
  1002. if (likely((max - (*len)) > 0)) {
  1003. dest_base[(*len)] = 0x00;
  1004. } else {
  1005. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1006. rc = -EIO;
  1007. }
  1008. out:
  1009. if (rc)
  1010. (*len) = 0;
  1011. return rc;
  1012. }