keystore.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  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 (password_s_ptr->session_key_encryption_key_bytes
  461. < crypto_tfm_alg_min_keysize(tfm)) {
  462. printk(KERN_WARNING "Session key encryption key is [%d] bytes; "
  463. "minimum keysize for selected cipher is [%d] bytes.\n",
  464. password_s_ptr->session_key_encryption_key_bytes,
  465. crypto_tfm_alg_min_keysize(tfm));
  466. rc = -EINVAL;
  467. goto out;
  468. }
  469. if (tfm_mutex)
  470. mutex_lock(tfm_mutex);
  471. crypto_cipher_setkey(tfm, password_s_ptr->session_key_encryption_key,
  472. crypt_stat->key_size);
  473. /* TODO: virt_to_scatterlist */
  474. encrypted_session_key = (char *)__get_free_page(GFP_KERNEL);
  475. if (!encrypted_session_key) {
  476. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  477. rc = -ENOMEM;
  478. goto out_free_tfm;
  479. }
  480. session_key = (char *)__get_free_page(GFP_KERNEL);
  481. if (!session_key) {
  482. kfree(encrypted_session_key);
  483. ecryptfs_printk(KERN_ERR, "Out of memory\n");
  484. rc = -ENOMEM;
  485. goto out_free_tfm;
  486. }
  487. memcpy(encrypted_session_key, auth_tok->session_key.encrypted_key,
  488. auth_tok->session_key.encrypted_key_size);
  489. src_sg[0].page = virt_to_page(encrypted_session_key);
  490. src_sg[0].offset = 0;
  491. BUG_ON(auth_tok->session_key.encrypted_key_size > PAGE_CACHE_SIZE);
  492. src_sg[0].length = auth_tok->session_key.encrypted_key_size;
  493. dst_sg[0].page = virt_to_page(session_key);
  494. dst_sg[0].offset = 0;
  495. auth_tok->session_key.decrypted_key_size =
  496. auth_tok->session_key.encrypted_key_size;
  497. dst_sg[0].length = auth_tok->session_key.encrypted_key_size;
  498. /* TODO: Handle error condition */
  499. crypto_cipher_decrypt(tfm, dst_sg, src_sg,
  500. auth_tok->session_key.encrypted_key_size);
  501. auth_tok->session_key.decrypted_key_size =
  502. auth_tok->session_key.encrypted_key_size;
  503. memcpy(auth_tok->session_key.decrypted_key, session_key,
  504. auth_tok->session_key.decrypted_key_size);
  505. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  506. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  507. auth_tok->session_key.decrypted_key_size);
  508. ECRYPTFS_SET_FLAG(crypt_stat->flags, ECRYPTFS_KEY_VALID);
  509. ecryptfs_printk(KERN_DEBUG, "Decrypted session key:\n");
  510. if (ecryptfs_verbosity > 0)
  511. ecryptfs_dump_hex(crypt_stat->key,
  512. crypt_stat->key_size);
  513. memset(encrypted_session_key, 0, PAGE_CACHE_SIZE);
  514. free_page((unsigned long)encrypted_session_key);
  515. memset(session_key, 0, PAGE_CACHE_SIZE);
  516. free_page((unsigned long)session_key);
  517. out_free_tfm:
  518. if (tfm_mutex)
  519. mutex_unlock(tfm_mutex);
  520. else
  521. crypto_free_tfm(tfm);
  522. out:
  523. return rc;
  524. }
  525. /**
  526. * ecryptfs_parse_packet_set
  527. * @dest: The header page in memory
  528. * @version: Version of file format, to guide parsing behavior
  529. *
  530. * Get crypt_stat to have the file's session key if the requisite key
  531. * is available to decrypt the session key.
  532. *
  533. * Returns Zero if a valid authentication token was retrieved and
  534. * processed; negative value for file not encrypted or for error
  535. * conditions.
  536. */
  537. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  538. unsigned char *src,
  539. struct dentry *ecryptfs_dentry)
  540. {
  541. size_t i = 0;
  542. int rc = 0;
  543. size_t found_auth_tok = 0;
  544. size_t next_packet_is_auth_tok_packet;
  545. char sig[ECRYPTFS_SIG_SIZE_HEX];
  546. struct list_head auth_tok_list;
  547. struct list_head *walker;
  548. struct ecryptfs_auth_tok *chosen_auth_tok = NULL;
  549. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  550. &ecryptfs_superblock_to_private(
  551. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  552. struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
  553. size_t packet_size;
  554. struct ecryptfs_auth_tok *new_auth_tok;
  555. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  556. size_t tag_11_contents_size;
  557. size_t tag_11_packet_size;
  558. INIT_LIST_HEAD(&auth_tok_list);
  559. /* Parse the header to find as many packets as we can, these will be
  560. * added the our &auth_tok_list */
  561. next_packet_is_auth_tok_packet = 1;
  562. while (next_packet_is_auth_tok_packet) {
  563. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  564. switch (src[i]) {
  565. case ECRYPTFS_TAG_3_PACKET_TYPE:
  566. rc = parse_tag_3_packet(crypt_stat,
  567. (unsigned char *)&src[i],
  568. &auth_tok_list, &new_auth_tok,
  569. &packet_size, max_packet_size);
  570. if (rc) {
  571. ecryptfs_printk(KERN_ERR, "Error parsing "
  572. "tag 3 packet\n");
  573. rc = -EIO;
  574. goto out_wipe_list;
  575. }
  576. i += packet_size;
  577. rc = parse_tag_11_packet((unsigned char *)&src[i],
  578. sig_tmp_space,
  579. ECRYPTFS_SIG_SIZE,
  580. &tag_11_contents_size,
  581. &tag_11_packet_size,
  582. max_packet_size);
  583. if (rc) {
  584. ecryptfs_printk(KERN_ERR, "No valid "
  585. "(ecryptfs-specific) literal "
  586. "packet containing "
  587. "authentication token "
  588. "signature found after "
  589. "tag 3 packet\n");
  590. rc = -EIO;
  591. goto out_wipe_list;
  592. }
  593. i += tag_11_packet_size;
  594. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  595. ecryptfs_printk(KERN_ERR, "Expected "
  596. "signature of size [%d]; "
  597. "read size [%d]\n",
  598. ECRYPTFS_SIG_SIZE,
  599. tag_11_contents_size);
  600. rc = -EIO;
  601. goto out_wipe_list;
  602. }
  603. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  604. sig_tmp_space, tag_11_contents_size);
  605. new_auth_tok->token.password.signature[
  606. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  607. ECRYPTFS_SET_FLAG(crypt_stat->flags,
  608. ECRYPTFS_ENCRYPTED);
  609. break;
  610. case ECRYPTFS_TAG_11_PACKET_TYPE:
  611. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  612. "(Tag 11 not allowed by itself)\n");
  613. rc = -EIO;
  614. goto out_wipe_list;
  615. break;
  616. default:
  617. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  618. "[%d] of the file header; hex value of "
  619. "character is [0x%.2x]\n", i, src[i]);
  620. next_packet_is_auth_tok_packet = 0;
  621. }
  622. }
  623. if (list_empty(&auth_tok_list)) {
  624. rc = -EINVAL; /* Do not support non-encrypted files in
  625. * the 0.1 release */
  626. goto out;
  627. }
  628. /* If we have a global auth tok, then we should try to use
  629. * it */
  630. if (mount_crypt_stat->global_auth_tok) {
  631. memcpy(sig, mount_crypt_stat->global_auth_tok_sig,
  632. ECRYPTFS_SIG_SIZE_HEX);
  633. chosen_auth_tok = mount_crypt_stat->global_auth_tok;
  634. } else
  635. BUG(); /* We should always have a global auth tok in
  636. * the 0.1 release */
  637. /* Scan list to see if our chosen_auth_tok works */
  638. list_for_each(walker, &auth_tok_list) {
  639. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  640. auth_tok_list_item =
  641. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  642. list);
  643. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  644. if (unlikely(ecryptfs_verbosity > 0)) {
  645. ecryptfs_printk(KERN_DEBUG,
  646. "Considering cadidate auth tok:\n");
  647. ecryptfs_dump_auth_tok(candidate_auth_tok);
  648. }
  649. /* TODO: Replace ECRYPTFS_SIG_SIZE_HEX w/ dynamic value */
  650. if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD
  651. && !strncmp(candidate_auth_tok->token.password.signature,
  652. sig, ECRYPTFS_SIG_SIZE_HEX)) {
  653. found_auth_tok = 1;
  654. goto leave_list;
  655. /* TODO: Transfer the common salt into the
  656. * crypt_stat salt */
  657. }
  658. }
  659. leave_list:
  660. if (!found_auth_tok) {
  661. ecryptfs_printk(KERN_ERR, "Could not find authentication "
  662. "token on temporary list for sig [%.*s]\n",
  663. ECRYPTFS_SIG_SIZE_HEX, sig);
  664. rc = -EIO;
  665. goto out_wipe_list;
  666. } else {
  667. memcpy(&(candidate_auth_tok->token.password),
  668. &(chosen_auth_tok->token.password),
  669. sizeof(struct ecryptfs_password));
  670. rc = decrypt_session_key(candidate_auth_tok, crypt_stat);
  671. if (rc) {
  672. ecryptfs_printk(KERN_ERR, "Error decrypting the "
  673. "session key\n");
  674. goto out_wipe_list;
  675. }
  676. rc = ecryptfs_compute_root_iv(crypt_stat);
  677. if (rc) {
  678. ecryptfs_printk(KERN_ERR, "Error computing "
  679. "the root IV\n");
  680. goto out_wipe_list;
  681. }
  682. }
  683. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  684. if (rc) {
  685. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  686. "context for cipher [%s]; rc = [%d]\n",
  687. crypt_stat->cipher, rc);
  688. }
  689. out_wipe_list:
  690. wipe_auth_tok_list(&auth_tok_list);
  691. out:
  692. return rc;
  693. }
  694. /**
  695. * write_tag_11_packet
  696. * @dest: Target into which Tag 11 packet is to be written
  697. * @max: Maximum packet length
  698. * @contents: Byte array of contents to copy in
  699. * @contents_length: Number of bytes in contents
  700. * @packet_length: Length of the Tag 11 packet written; zero on error
  701. *
  702. * Returns zero on success; non-zero on error.
  703. */
  704. static int
  705. write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
  706. size_t *packet_length)
  707. {
  708. int rc = 0;
  709. size_t packet_size_length;
  710. (*packet_length) = 0;
  711. if ((13 + contents_length) > max) {
  712. rc = -EINVAL;
  713. ecryptfs_printk(KERN_ERR, "Packet length larger than "
  714. "maximum allowable\n");
  715. goto out;
  716. }
  717. /* General packet header */
  718. /* Packet tag */
  719. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  720. /* Packet length */
  721. rc = write_packet_length(&dest[(*packet_length)],
  722. (13 + contents_length), &packet_size_length);
  723. if (rc) {
  724. ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
  725. "header; cannot generate packet length\n");
  726. goto out;
  727. }
  728. (*packet_length) += packet_size_length;
  729. /* Tag 11 specific */
  730. /* One-octet field that describes how the data is formatted */
  731. dest[(*packet_length)++] = 0x62; /* binary data */
  732. /* One-octet filename length followed by filename */
  733. dest[(*packet_length)++] = 8;
  734. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  735. (*packet_length) += 8;
  736. /* Four-octet number indicating modification date */
  737. memset(&dest[(*packet_length)], 0x00, 4);
  738. (*packet_length) += 4;
  739. /* Remainder is literal data */
  740. memcpy(&dest[(*packet_length)], contents, contents_length);
  741. (*packet_length) += contents_length;
  742. out:
  743. if (rc)
  744. (*packet_length) = 0;
  745. return rc;
  746. }
  747. /**
  748. * write_tag_3_packet
  749. * @dest: Buffer into which to write the packet
  750. * @max: Maximum number of bytes that can be written
  751. * @auth_tok: Authentication token
  752. * @crypt_stat: The cryptographic context
  753. * @key_rec: encrypted key
  754. * @packet_size: This function will write the number of bytes that end
  755. * up constituting the packet; set to zero on error
  756. *
  757. * Returns zero on success; non-zero on error.
  758. */
  759. static int
  760. write_tag_3_packet(char *dest, size_t max, struct ecryptfs_auth_tok *auth_tok,
  761. struct ecryptfs_crypt_stat *crypt_stat,
  762. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  763. {
  764. int rc = 0;
  765. size_t i;
  766. size_t signature_is_valid = 0;
  767. size_t encrypted_session_key_valid = 0;
  768. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  769. struct scatterlist dest_sg[2];
  770. struct scatterlist src_sg[2];
  771. struct crypto_tfm *tfm = NULL;
  772. struct mutex *tfm_mutex = NULL;
  773. size_t key_rec_size;
  774. size_t packet_size_length;
  775. size_t cipher_code;
  776. (*packet_size) = 0;
  777. /* Check for a valid signature on the auth_tok */
  778. for (i = 0; i < ECRYPTFS_SIG_SIZE_HEX; i++)
  779. signature_is_valid |= auth_tok->token.password.signature[i];
  780. if (!signature_is_valid)
  781. BUG();
  782. ecryptfs_from_hex((*key_rec).sig, auth_tok->token.password.signature,
  783. ECRYPTFS_SIG_SIZE);
  784. encrypted_session_key_valid = 0;
  785. for (i = 0; i < crypt_stat->key_size; i++)
  786. encrypted_session_key_valid |=
  787. auth_tok->session_key.encrypted_key[i];
  788. if (encrypted_session_key_valid) {
  789. memcpy((*key_rec).enc_key,
  790. auth_tok->session_key.encrypted_key,
  791. auth_tok->session_key.encrypted_key_size);
  792. goto encrypted_session_key_set;
  793. }
  794. if (auth_tok->session_key.encrypted_key_size == 0)
  795. auth_tok->session_key.encrypted_key_size =
  796. crypt_stat->key_size;
  797. if (crypt_stat->key_size == 24
  798. && strcmp("aes", crypt_stat->cipher) == 0) {
  799. memset((crypt_stat->key + 24), 0, 8);
  800. auth_tok->session_key.encrypted_key_size = 32;
  801. }
  802. (*key_rec).enc_key_size =
  803. auth_tok->session_key.encrypted_key_size;
  804. if (ECRYPTFS_CHECK_FLAG(auth_tok->token.password.flags,
  805. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET)) {
  806. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  807. "session key encryption key of size [%d]\n",
  808. auth_tok->token.password.
  809. session_key_encryption_key_bytes);
  810. memcpy(session_key_encryption_key,
  811. auth_tok->token.password.session_key_encryption_key,
  812. crypt_stat->key_size);
  813. ecryptfs_printk(KERN_DEBUG,
  814. "Cached session key " "encryption key: \n");
  815. if (ecryptfs_verbosity > 0)
  816. ecryptfs_dump_hex(session_key_encryption_key, 16);
  817. }
  818. if (unlikely(ecryptfs_verbosity > 0)) {
  819. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  820. ecryptfs_dump_hex(session_key_encryption_key, 16);
  821. }
  822. rc = virt_to_scatterlist(crypt_stat->key,
  823. (*key_rec).enc_key_size, src_sg, 2);
  824. if (!rc) {
  825. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  826. "for crypt_stat session key\n");
  827. rc = -ENOMEM;
  828. goto out;
  829. }
  830. rc = virt_to_scatterlist((*key_rec).enc_key,
  831. (*key_rec).enc_key_size, dest_sg, 2);
  832. if (!rc) {
  833. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  834. "for crypt_stat encrypted session key\n");
  835. rc = -ENOMEM;
  836. goto out;
  837. }
  838. if (!strcmp(crypt_stat->cipher,
  839. crypt_stat->mount_crypt_stat->global_default_cipher_name)
  840. && crypt_stat->mount_crypt_stat->global_key_tfm) {
  841. tfm = crypt_stat->mount_crypt_stat->global_key_tfm;
  842. tfm_mutex = &crypt_stat->mount_crypt_stat->global_key_tfm_mutex;
  843. } else
  844. tfm = crypto_alloc_tfm(crypt_stat->cipher, 0);
  845. if (!tfm) {
  846. ecryptfs_printk(KERN_ERR, "Could not initialize crypto "
  847. "context for cipher [%s]\n",
  848. crypt_stat->cipher);
  849. rc = -EINVAL;
  850. goto out;
  851. }
  852. if (tfm_mutex)
  853. mutex_lock(tfm_mutex);
  854. rc = crypto_cipher_setkey(tfm, session_key_encryption_key,
  855. crypt_stat->key_size);
  856. if (rc < 0) {
  857. if (tfm_mutex)
  858. mutex_unlock(tfm_mutex);
  859. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  860. "context\n");
  861. goto out;
  862. }
  863. rc = 0;
  864. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  865. crypt_stat->key_size);
  866. crypto_cipher_encrypt(tfm, dest_sg, src_sg,
  867. (*key_rec).enc_key_size);
  868. if (tfm_mutex)
  869. mutex_unlock(tfm_mutex);
  870. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  871. if (ecryptfs_verbosity > 0)
  872. ecryptfs_dump_hex((*key_rec).enc_key,
  873. (*key_rec).enc_key_size);
  874. encrypted_session_key_set:
  875. /* Now we have a valid key_rec. Append it to the
  876. * key_rec set. */
  877. key_rec_size = (sizeof(struct ecryptfs_key_record)
  878. - ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES
  879. + ((*key_rec).enc_key_size));
  880. /* TODO: Include a packet size limit as a parameter to this
  881. * function once we have multi-packet headers (for versions
  882. * later than 0.1 */
  883. if (key_rec_size >= ECRYPTFS_MAX_KEYSET_SIZE) {
  884. ecryptfs_printk(KERN_ERR, "Keyset too large\n");
  885. rc = -EINVAL;
  886. goto out;
  887. }
  888. /* TODO: Packet size limit */
  889. /* We have 5 bytes of surrounding packet data */
  890. if ((0x05 + ECRYPTFS_SALT_SIZE
  891. + (*key_rec).enc_key_size) >= max) {
  892. ecryptfs_printk(KERN_ERR, "Authentication token is too "
  893. "large\n");
  894. rc = -EINVAL;
  895. goto out;
  896. }
  897. /* This format is inspired by OpenPGP; see RFC 2440
  898. * packet tag 3 */
  899. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  900. /* ver+cipher+s2k+hash+salt+iter+enc_key */
  901. rc = write_packet_length(&dest[(*packet_size)],
  902. (0x05 + ECRYPTFS_SALT_SIZE
  903. + (*key_rec).enc_key_size),
  904. &packet_size_length);
  905. if (rc) {
  906. ecryptfs_printk(KERN_ERR, "Error generating tag 3 packet "
  907. "header; cannot generate packet length\n");
  908. goto out;
  909. }
  910. (*packet_size) += packet_size_length;
  911. dest[(*packet_size)++] = 0x04; /* version 4 */
  912. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  913. if (cipher_code == 0) {
  914. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  915. "cipher [%s]\n", crypt_stat->cipher);
  916. rc = -EINVAL;
  917. goto out;
  918. }
  919. dest[(*packet_size)++] = cipher_code;
  920. dest[(*packet_size)++] = 0x03; /* S2K */
  921. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  922. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  923. ECRYPTFS_SALT_SIZE);
  924. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  925. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  926. memcpy(&dest[(*packet_size)], (*key_rec).enc_key,
  927. (*key_rec).enc_key_size);
  928. (*packet_size) += (*key_rec).enc_key_size;
  929. out:
  930. if (tfm && !tfm_mutex)
  931. crypto_free_tfm(tfm);
  932. if (rc)
  933. (*packet_size) = 0;
  934. return rc;
  935. }
  936. /**
  937. * ecryptfs_generate_key_packet_set
  938. * @dest: Virtual address from which to write the key record set
  939. * @crypt_stat: The cryptographic context from which the
  940. * authentication tokens will be retrieved
  941. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  942. * for the global parameters
  943. * @len: The amount written
  944. * @max: The maximum amount of data allowed to be written
  945. *
  946. * Generates a key packet set and writes it to the virtual address
  947. * passed in.
  948. *
  949. * Returns zero on success; non-zero on error.
  950. */
  951. int
  952. ecryptfs_generate_key_packet_set(char *dest_base,
  953. struct ecryptfs_crypt_stat *crypt_stat,
  954. struct dentry *ecryptfs_dentry, size_t *len,
  955. size_t max)
  956. {
  957. int rc = 0;
  958. struct ecryptfs_auth_tok *auth_tok;
  959. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  960. &ecryptfs_superblock_to_private(
  961. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  962. size_t written;
  963. struct ecryptfs_key_record key_rec;
  964. (*len) = 0;
  965. if (mount_crypt_stat->global_auth_tok) {
  966. auth_tok = mount_crypt_stat->global_auth_tok;
  967. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  968. rc = write_tag_3_packet((dest_base + (*len)),
  969. max, auth_tok,
  970. crypt_stat, &key_rec,
  971. &written);
  972. if (rc) {
  973. ecryptfs_printk(KERN_WARNING, "Error "
  974. "writing tag 3 packet\n");
  975. goto out;
  976. }
  977. (*len) += written;
  978. /* Write auth tok signature packet */
  979. rc = write_tag_11_packet(
  980. (dest_base + (*len)),
  981. (max - (*len)),
  982. key_rec.sig, ECRYPTFS_SIG_SIZE, &written);
  983. if (rc) {
  984. ecryptfs_printk(KERN_ERR, "Error writing "
  985. "auth tok signature packet\n");
  986. goto out;
  987. }
  988. (*len) += written;
  989. } else {
  990. ecryptfs_printk(KERN_WARNING, "Unsupported "
  991. "authentication token type\n");
  992. rc = -EINVAL;
  993. goto out;
  994. }
  995. if (rc) {
  996. ecryptfs_printk(KERN_WARNING, "Error writing "
  997. "authentication token packet with sig "
  998. "= [%s]\n",
  999. mount_crypt_stat->global_auth_tok_sig);
  1000. rc = -EIO;
  1001. goto out;
  1002. }
  1003. } else
  1004. BUG();
  1005. if (likely((max - (*len)) > 0)) {
  1006. dest_base[(*len)] = 0x00;
  1007. } else {
  1008. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1009. rc = -EIO;
  1010. }
  1011. out:
  1012. if (rc)
  1013. (*len) = 0;
  1014. return rc;
  1015. }