keystore.c 57 KB

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