keystore.c 53 KB

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