keystore.c 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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 list_head *walker;
  454. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  455. walker = auth_tok_list_head->next;
  456. while (walker != auth_tok_list_head) {
  457. auth_tok_list_item =
  458. list_entry(walker, struct ecryptfs_auth_tok_list_item,
  459. list);
  460. walker = auth_tok_list_item->list.next;
  461. memset(auth_tok_list_item, 0,
  462. sizeof(struct ecryptfs_auth_tok_list_item));
  463. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  464. auth_tok_list_item);
  465. }
  466. auth_tok_list_head->next = NULL;
  467. }
  468. struct kmem_cache *ecryptfs_auth_tok_list_item_cache;
  469. /**
  470. * parse_tag_1_packet
  471. * @crypt_stat: The cryptographic context to modify based on packet
  472. * contents.
  473. * @data: The raw bytes of the packet.
  474. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  475. * a new authentication token will be placed at the end
  476. * of this list for this packet.
  477. * @new_auth_tok: Pointer to a pointer to memory that this function
  478. * allocates; sets the memory address of the pointer to
  479. * NULL on error. This object is added to the
  480. * auth_tok_list.
  481. * @packet_size: This function writes the size of the parsed packet
  482. * into this memory location; zero on error.
  483. *
  484. * Returns zero on success; non-zero on error.
  485. */
  486. static int
  487. parse_tag_1_packet(struct ecryptfs_crypt_stat *crypt_stat,
  488. unsigned char *data, struct list_head *auth_tok_list,
  489. struct ecryptfs_auth_tok **new_auth_tok,
  490. size_t *packet_size, size_t max_packet_size)
  491. {
  492. size_t body_size;
  493. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  494. size_t length_size;
  495. int rc = 0;
  496. (*packet_size) = 0;
  497. (*new_auth_tok) = NULL;
  498. /* we check that:
  499. * one byte for the Tag 1 ID flag
  500. * two bytes for the body size
  501. * do not exceed the maximum_packet_size
  502. */
  503. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  504. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  505. rc = -EINVAL;
  506. goto out;
  507. }
  508. /* check for Tag 1 identifier - one byte */
  509. if (data[(*packet_size)++] != ECRYPTFS_TAG_1_PACKET_TYPE) {
  510. ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
  511. ECRYPTFS_TAG_1_PACKET_TYPE);
  512. rc = -EINVAL;
  513. goto out;
  514. }
  515. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  516. * at end of function upon failure */
  517. auth_tok_list_item =
  518. kmem_cache_alloc(ecryptfs_auth_tok_list_item_cache,
  519. GFP_KERNEL);
  520. if (!auth_tok_list_item) {
  521. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  522. rc = -ENOMEM;
  523. goto out;
  524. }
  525. memset(auth_tok_list_item, 0,
  526. sizeof(struct ecryptfs_auth_tok_list_item));
  527. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  528. /* check for body size - one to two bytes
  529. *
  530. * ***** TAG 1 Packet Format *****
  531. * | version number | 1 byte |
  532. * | key ID | 8 bytes |
  533. * | public key algorithm | 1 byte |
  534. * | encrypted session key | arbitrary |
  535. */
  536. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  537. &length_size);
  538. if (rc) {
  539. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  540. "rc = [%d]\n", rc);
  541. goto out_free;
  542. }
  543. if (unlikely(body_size < (0x02 + ECRYPTFS_SIG_SIZE))) {
  544. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  545. body_size);
  546. rc = -EINVAL;
  547. goto out_free;
  548. }
  549. (*packet_size) += length_size;
  550. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  551. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  552. rc = -EINVAL;
  553. goto out_free;
  554. }
  555. /* Version 3 (from RFC2440) - one byte */
  556. if (unlikely(data[(*packet_size)++] != 0x03)) {
  557. ecryptfs_printk(KERN_DEBUG, "Unknown version number "
  558. "[%d]\n", data[(*packet_size) - 1]);
  559. rc = -EINVAL;
  560. goto out_free;
  561. }
  562. /* Read Signature */
  563. ecryptfs_to_hex((*new_auth_tok)->token.private_key.signature,
  564. &data[(*packet_size)], ECRYPTFS_SIG_SIZE);
  565. *packet_size += ECRYPTFS_SIG_SIZE;
  566. /* This byte is skipped because the kernel does not need to
  567. * know which public key encryption algorithm was used */
  568. (*packet_size)++;
  569. (*new_auth_tok)->session_key.encrypted_key_size =
  570. body_size - (0x02 + ECRYPTFS_SIG_SIZE);
  571. if ((*new_auth_tok)->session_key.encrypted_key_size
  572. > ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES) {
  573. ecryptfs_printk(KERN_ERR, "Tag 1 packet contains key larger "
  574. "than ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES");
  575. rc = -EINVAL;
  576. goto out;
  577. }
  578. ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
  579. (*new_auth_tok)->session_key.encrypted_key_size);
  580. memcpy((*new_auth_tok)->session_key.encrypted_key,
  581. &data[(*packet_size)], (body_size - 0x02 - ECRYPTFS_SIG_SIZE));
  582. (*packet_size) += (*new_auth_tok)->session_key.encrypted_key_size;
  583. (*new_auth_tok)->session_key.flags &=
  584. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  585. (*new_auth_tok)->session_key.flags |=
  586. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  587. (*new_auth_tok)->token_type = ECRYPTFS_PRIVATE_KEY;
  588. (*new_auth_tok)->flags |= ECRYPTFS_PRIVATE_KEY;
  589. /* TODO: Why are we setting this flag here? Don't we want the
  590. * userspace to decrypt the session key? */
  591. (*new_auth_tok)->session_key.flags &=
  592. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  593. (*new_auth_tok)->session_key.flags &=
  594. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  595. list_add(&auth_tok_list_item->list, auth_tok_list);
  596. goto out;
  597. out_free:
  598. (*new_auth_tok) = NULL;
  599. memset(auth_tok_list_item, 0,
  600. sizeof(struct ecryptfs_auth_tok_list_item));
  601. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  602. auth_tok_list_item);
  603. out:
  604. if (rc)
  605. (*packet_size) = 0;
  606. return rc;
  607. }
  608. /**
  609. * parse_tag_3_packet
  610. * @crypt_stat: The cryptographic context to modify based on packet
  611. * contents.
  612. * @data: The raw bytes of the packet.
  613. * @auth_tok_list: eCryptfs parses packets into authentication tokens;
  614. * a new authentication token will be placed at the end
  615. * of this list for this packet.
  616. * @new_auth_tok: Pointer to a pointer to memory that this function
  617. * allocates; sets the memory address of the pointer to
  618. * NULL on error. This object is added to the
  619. * auth_tok_list.
  620. * @packet_size: This function writes the size of the parsed packet
  621. * into this memory location; zero on error.
  622. * @max_packet_size: maximum number of bytes to parse
  623. *
  624. * Returns zero on success; non-zero on error.
  625. */
  626. static int
  627. parse_tag_3_packet(struct ecryptfs_crypt_stat *crypt_stat,
  628. unsigned char *data, struct list_head *auth_tok_list,
  629. struct ecryptfs_auth_tok **new_auth_tok,
  630. size_t *packet_size, size_t max_packet_size)
  631. {
  632. size_t body_size;
  633. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  634. size_t length_size;
  635. int rc = 0;
  636. (*packet_size) = 0;
  637. (*new_auth_tok) = NULL;
  638. /* we check that:
  639. * one byte for the Tag 3 ID flag
  640. * two bytes for the body size
  641. * do not exceed the maximum_packet_size
  642. */
  643. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  644. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  645. rc = -EINVAL;
  646. goto out;
  647. }
  648. /* check for Tag 3 identifyer - one byte */
  649. if (data[(*packet_size)++] != ECRYPTFS_TAG_3_PACKET_TYPE) {
  650. ecryptfs_printk(KERN_ERR, "Enter w/ first byte != 0x%.2x\n",
  651. ECRYPTFS_TAG_3_PACKET_TYPE);
  652. rc = -EINVAL;
  653. goto out;
  654. }
  655. /* Released: wipe_auth_tok_list called in ecryptfs_parse_packet_set or
  656. * at end of function upon failure */
  657. auth_tok_list_item =
  658. kmem_cache_zalloc(ecryptfs_auth_tok_list_item_cache, GFP_KERNEL);
  659. if (!auth_tok_list_item) {
  660. ecryptfs_printk(KERN_ERR, "Unable to allocate memory\n");
  661. rc = -ENOMEM;
  662. goto out;
  663. }
  664. (*new_auth_tok) = &auth_tok_list_item->auth_tok;
  665. /* check for body size - one to two bytes */
  666. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  667. &length_size);
  668. if (rc) {
  669. ecryptfs_printk(KERN_WARNING, "Error parsing packet length; "
  670. "rc = [%d]\n", rc);
  671. goto out_free;
  672. }
  673. if (unlikely(body_size < (0x05 + ECRYPTFS_SALT_SIZE))) {
  674. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  675. body_size);
  676. rc = -EINVAL;
  677. goto out_free;
  678. }
  679. (*packet_size) += length_size;
  680. /* now we know the length of the remainting Tag 3 packet size:
  681. * 5 fix bytes for: version string, cipher, S2K ID, hash algo,
  682. * number of hash iterations
  683. * ECRYPTFS_SALT_SIZE bytes for salt
  684. * body_size bytes minus the stuff above is the encrypted key size
  685. */
  686. if (unlikely((*packet_size) + body_size > max_packet_size)) {
  687. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  688. rc = -EINVAL;
  689. goto out_free;
  690. }
  691. /* There are 5 characters of additional information in the
  692. * packet */
  693. (*new_auth_tok)->session_key.encrypted_key_size =
  694. body_size - (0x05 + ECRYPTFS_SALT_SIZE);
  695. ecryptfs_printk(KERN_DEBUG, "Encrypted key size = [%d]\n",
  696. (*new_auth_tok)->session_key.encrypted_key_size);
  697. /* Version 4 (from RFC2440) - one byte */
  698. if (unlikely(data[(*packet_size)++] != 0x04)) {
  699. ecryptfs_printk(KERN_DEBUG, "Unknown version number "
  700. "[%d]\n", data[(*packet_size) - 1]);
  701. rc = -EINVAL;
  702. goto out_free;
  703. }
  704. /* cipher - one byte */
  705. ecryptfs_cipher_code_to_string(crypt_stat->cipher,
  706. (u16)data[(*packet_size)]);
  707. /* A little extra work to differentiate among the AES key
  708. * sizes; see RFC2440 */
  709. switch(data[(*packet_size)++]) {
  710. case RFC2440_CIPHER_AES_192:
  711. crypt_stat->key_size = 24;
  712. break;
  713. default:
  714. crypt_stat->key_size =
  715. (*new_auth_tok)->session_key.encrypted_key_size;
  716. }
  717. ecryptfs_init_crypt_ctx(crypt_stat);
  718. /* S2K identifier 3 (from RFC2440) */
  719. if (unlikely(data[(*packet_size)++] != 0x03)) {
  720. ecryptfs_printk(KERN_ERR, "Only S2K ID 3 is currently "
  721. "supported\n");
  722. rc = -ENOSYS;
  723. goto out_free;
  724. }
  725. /* TODO: finish the hash mapping */
  726. /* hash algorithm - one byte */
  727. switch (data[(*packet_size)++]) {
  728. case 0x01: /* See RFC2440 for these numbers and their mappings */
  729. /* Choose MD5 */
  730. /* salt - ECRYPTFS_SALT_SIZE bytes */
  731. memcpy((*new_auth_tok)->token.password.salt,
  732. &data[(*packet_size)], ECRYPTFS_SALT_SIZE);
  733. (*packet_size) += ECRYPTFS_SALT_SIZE;
  734. /* This conversion was taken straight from RFC2440 */
  735. /* number of hash iterations - one byte */
  736. (*new_auth_tok)->token.password.hash_iterations =
  737. ((u32) 16 + (data[(*packet_size)] & 15))
  738. << ((data[(*packet_size)] >> 4) + 6);
  739. (*packet_size)++;
  740. /* encrypted session key -
  741. * (body_size-5-ECRYPTFS_SALT_SIZE) bytes */
  742. memcpy((*new_auth_tok)->session_key.encrypted_key,
  743. &data[(*packet_size)],
  744. (*new_auth_tok)->session_key.encrypted_key_size);
  745. (*packet_size) +=
  746. (*new_auth_tok)->session_key.encrypted_key_size;
  747. (*new_auth_tok)->session_key.flags &=
  748. ~ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  749. (*new_auth_tok)->session_key.flags |=
  750. ECRYPTFS_CONTAINS_ENCRYPTED_KEY;
  751. (*new_auth_tok)->token.password.hash_algo = 0x01;
  752. break;
  753. default:
  754. ecryptfs_printk(KERN_ERR, "Unsupported hash algorithm: "
  755. "[%d]\n", data[(*packet_size) - 1]);
  756. rc = -ENOSYS;
  757. goto out_free;
  758. }
  759. (*new_auth_tok)->token_type = ECRYPTFS_PASSWORD;
  760. /* TODO: Parametarize; we might actually want userspace to
  761. * decrypt the session key. */
  762. (*new_auth_tok)->session_key.flags &=
  763. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_DECRYPT);
  764. (*new_auth_tok)->session_key.flags &=
  765. ~(ECRYPTFS_USERSPACE_SHOULD_TRY_TO_ENCRYPT);
  766. list_add(&auth_tok_list_item->list, auth_tok_list);
  767. goto out;
  768. out_free:
  769. (*new_auth_tok) = NULL;
  770. memset(auth_tok_list_item, 0,
  771. sizeof(struct ecryptfs_auth_tok_list_item));
  772. kmem_cache_free(ecryptfs_auth_tok_list_item_cache,
  773. auth_tok_list_item);
  774. out:
  775. if (rc)
  776. (*packet_size) = 0;
  777. return rc;
  778. }
  779. /**
  780. * parse_tag_11_packet
  781. * @data: The raw bytes of the packet
  782. * @contents: This function writes the data contents of the literal
  783. * packet into this memory location
  784. * @max_contents_bytes: The maximum number of bytes that this function
  785. * is allowed to write into contents
  786. * @tag_11_contents_size: This function writes the size of the parsed
  787. * contents into this memory location; zero on
  788. * error
  789. * @packet_size: This function writes the size of the parsed packet
  790. * into this memory location; zero on error
  791. * @max_packet_size: maximum number of bytes to parse
  792. *
  793. * Returns zero on success; non-zero on error.
  794. */
  795. static int
  796. parse_tag_11_packet(unsigned char *data, unsigned char *contents,
  797. size_t max_contents_bytes, size_t *tag_11_contents_size,
  798. size_t *packet_size, size_t max_packet_size)
  799. {
  800. size_t body_size;
  801. size_t length_size;
  802. int rc = 0;
  803. (*packet_size) = 0;
  804. (*tag_11_contents_size) = 0;
  805. /* check that:
  806. * one byte for the Tag 11 ID flag
  807. * two bytes for the Tag 11 length
  808. * do not exceed the maximum_packet_size
  809. */
  810. if (unlikely((*packet_size) + 3 > max_packet_size)) {
  811. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  812. rc = -EINVAL;
  813. goto out;
  814. }
  815. /* check for Tag 11 identifyer - one byte */
  816. if (data[(*packet_size)++] != ECRYPTFS_TAG_11_PACKET_TYPE) {
  817. ecryptfs_printk(KERN_WARNING,
  818. "Invalid tag 11 packet format\n");
  819. rc = -EINVAL;
  820. goto out;
  821. }
  822. /* get Tag 11 content length - one or two bytes */
  823. rc = parse_packet_length(&data[(*packet_size)], &body_size,
  824. &length_size);
  825. if (rc) {
  826. ecryptfs_printk(KERN_WARNING,
  827. "Invalid tag 11 packet format\n");
  828. goto out;
  829. }
  830. (*packet_size) += length_size;
  831. if (body_size < 13) {
  832. ecryptfs_printk(KERN_WARNING, "Invalid body size ([%d])\n",
  833. body_size);
  834. rc = -EINVAL;
  835. goto out;
  836. }
  837. /* We have 13 bytes of surrounding packet values */
  838. (*tag_11_contents_size) = (body_size - 13);
  839. /* now we know the length of the remainting Tag 11 packet size:
  840. * 14 fix bytes for: special flag one, special flag two,
  841. * 12 skipped bytes
  842. * body_size bytes minus the stuff above is the Tag 11 content
  843. */
  844. /* FIXME why is the body size one byte smaller than the actual
  845. * size of the body?
  846. * this seems to be an error here as well as in
  847. * write_tag_11_packet() */
  848. if (unlikely((*packet_size) + body_size + 1 > max_packet_size)) {
  849. ecryptfs_printk(KERN_ERR, "Packet size exceeds max\n");
  850. rc = -EINVAL;
  851. goto out;
  852. }
  853. /* special flag one - one byte */
  854. if (data[(*packet_size)++] != 0x62) {
  855. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  856. rc = -EINVAL;
  857. goto out;
  858. }
  859. /* special flag two - one byte */
  860. if (data[(*packet_size)++] != 0x08) {
  861. ecryptfs_printk(KERN_WARNING, "Unrecognizable packet\n");
  862. rc = -EINVAL;
  863. goto out;
  864. }
  865. /* skip the next 12 bytes */
  866. (*packet_size) += 12; /* We don't care about the filename or
  867. * the timestamp */
  868. /* get the Tag 11 contents - tag_11_contents_size bytes */
  869. memcpy(contents, &data[(*packet_size)], (*tag_11_contents_size));
  870. (*packet_size) += (*tag_11_contents_size);
  871. out:
  872. if (rc) {
  873. (*packet_size) = 0;
  874. (*tag_11_contents_size) = 0;
  875. }
  876. return rc;
  877. }
  878. static int
  879. ecryptfs_find_global_auth_tok_for_sig(
  880. struct ecryptfs_global_auth_tok **global_auth_tok,
  881. struct ecryptfs_mount_crypt_stat *mount_crypt_stat, char *sig)
  882. {
  883. struct ecryptfs_global_auth_tok *walker;
  884. int rc = 0;
  885. (*global_auth_tok) = NULL;
  886. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  887. list_for_each_entry(walker,
  888. &mount_crypt_stat->global_auth_tok_list,
  889. mount_crypt_stat_list) {
  890. if (memcmp(walker->sig, sig, ECRYPTFS_SIG_SIZE_HEX) == 0) {
  891. (*global_auth_tok) = walker;
  892. goto out;
  893. }
  894. }
  895. rc = -EINVAL;
  896. out:
  897. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  898. return rc;
  899. }
  900. /**
  901. * ecryptfs_verify_version
  902. * @version: The version number to confirm
  903. *
  904. * Returns zero on good version; non-zero otherwise
  905. */
  906. static int ecryptfs_verify_version(u16 version)
  907. {
  908. int rc = 0;
  909. unsigned char major;
  910. unsigned char minor;
  911. major = ((version >> 8) & 0xFF);
  912. minor = (version & 0xFF);
  913. if (major != ECRYPTFS_VERSION_MAJOR) {
  914. ecryptfs_printk(KERN_ERR, "Major version number mismatch. "
  915. "Expected [%d]; got [%d]\n",
  916. ECRYPTFS_VERSION_MAJOR, major);
  917. rc = -EINVAL;
  918. goto out;
  919. }
  920. if (minor != ECRYPTFS_VERSION_MINOR) {
  921. ecryptfs_printk(KERN_ERR, "Minor version number mismatch. "
  922. "Expected [%d]; got [%d]\n",
  923. ECRYPTFS_VERSION_MINOR, minor);
  924. rc = -EINVAL;
  925. goto out;
  926. }
  927. out:
  928. return rc;
  929. }
  930. int ecryptfs_keyring_auth_tok_for_sig(struct key **auth_tok_key,
  931. struct ecryptfs_auth_tok **auth_tok,
  932. char *sig)
  933. {
  934. int rc = 0;
  935. (*auth_tok_key) = request_key(&key_type_user, sig, NULL);
  936. if (!(*auth_tok_key) || IS_ERR(*auth_tok_key)) {
  937. printk(KERN_ERR "Could not find key with description: [%s]\n",
  938. sig);
  939. process_request_key_err(PTR_ERR(*auth_tok_key));
  940. rc = -EINVAL;
  941. goto out;
  942. }
  943. (*auth_tok) = ecryptfs_get_key_payload_data(*auth_tok_key);
  944. if (ecryptfs_verify_version((*auth_tok)->version)) {
  945. printk(KERN_ERR
  946. "Data structure version mismatch. "
  947. "Userspace tools must match eCryptfs "
  948. "kernel module with major version [%d] "
  949. "and minor version [%d]\n",
  950. ECRYPTFS_VERSION_MAJOR,
  951. ECRYPTFS_VERSION_MINOR);
  952. rc = -EINVAL;
  953. goto out;
  954. }
  955. if ((*auth_tok)->token_type != ECRYPTFS_PASSWORD
  956. && (*auth_tok)->token_type != ECRYPTFS_PRIVATE_KEY) {
  957. printk(KERN_ERR "Invalid auth_tok structure "
  958. "returned from key query\n");
  959. rc = -EINVAL;
  960. goto out;
  961. }
  962. out:
  963. return rc;
  964. }
  965. /**
  966. * ecryptfs_find_auth_tok_for_sig
  967. * @auth_tok: Set to the matching auth_tok; NULL if not found
  968. * @crypt_stat: inode crypt_stat crypto context
  969. * @sig: Sig of auth_tok to find
  970. *
  971. * For now, this function simply looks at the registered auth_tok's
  972. * linked off the mount_crypt_stat, so all the auth_toks that can be
  973. * used must be registered at mount time. This function could
  974. * potentially try a lot harder to find auth_tok's (e.g., by calling
  975. * out to ecryptfsd to dynamically retrieve an auth_tok object) so
  976. * that static registration of auth_tok's will no longer be necessary.
  977. *
  978. * Returns zero on no error; non-zero on error
  979. */
  980. static int
  981. ecryptfs_find_auth_tok_for_sig(
  982. struct ecryptfs_auth_tok **auth_tok,
  983. struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  984. {
  985. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  986. crypt_stat->mount_crypt_stat;
  987. struct ecryptfs_global_auth_tok *global_auth_tok;
  988. int rc = 0;
  989. (*auth_tok) = NULL;
  990. if (ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
  991. mount_crypt_stat, sig)) {
  992. struct key *auth_tok_key;
  993. rc = ecryptfs_keyring_auth_tok_for_sig(&auth_tok_key, auth_tok,
  994. sig);
  995. } else
  996. (*auth_tok) = global_auth_tok->global_auth_tok;
  997. return rc;
  998. }
  999. /**
  1000. * decrypt_passphrase_encrypted_session_key - Decrypt the session key
  1001. * with the given auth_tok.
  1002. *
  1003. * Returns Zero on success; non-zero error otherwise.
  1004. */
  1005. static int
  1006. decrypt_passphrase_encrypted_session_key(struct ecryptfs_auth_tok *auth_tok,
  1007. struct ecryptfs_crypt_stat *crypt_stat)
  1008. {
  1009. struct scatterlist dst_sg;
  1010. struct scatterlist src_sg;
  1011. struct mutex *tfm_mutex = NULL;
  1012. struct blkcipher_desc desc = {
  1013. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1014. };
  1015. int rc = 0;
  1016. if (unlikely(ecryptfs_verbosity > 0)) {
  1017. ecryptfs_printk(
  1018. KERN_DEBUG, "Session key encryption key (size [%d]):\n",
  1019. auth_tok->token.password.session_key_encryption_key_bytes);
  1020. ecryptfs_dump_hex(
  1021. auth_tok->token.password.session_key_encryption_key,
  1022. auth_tok->token.password.session_key_encryption_key_bytes);
  1023. }
  1024. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1025. crypt_stat->cipher);
  1026. if (unlikely(rc)) {
  1027. printk(KERN_ERR "Internal error whilst attempting to get "
  1028. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1029. crypt_stat->cipher, rc);
  1030. goto out;
  1031. }
  1032. if ((rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
  1033. auth_tok->session_key.encrypted_key_size,
  1034. &src_sg, 1)) != 1) {
  1035. printk(KERN_ERR "Internal error whilst attempting to convert "
  1036. "auth_tok->session_key.encrypted_key to scatterlist; "
  1037. "expected rc = 1; got rc = [%d]. "
  1038. "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
  1039. auth_tok->session_key.encrypted_key_size);
  1040. goto out;
  1041. }
  1042. auth_tok->session_key.decrypted_key_size =
  1043. auth_tok->session_key.encrypted_key_size;
  1044. if ((rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
  1045. auth_tok->session_key.decrypted_key_size,
  1046. &dst_sg, 1)) != 1) {
  1047. printk(KERN_ERR "Internal error whilst attempting to convert "
  1048. "auth_tok->session_key.decrypted_key to scatterlist; "
  1049. "expected rc = 1; got rc = [%d]\n", rc);
  1050. goto out;
  1051. }
  1052. mutex_lock(tfm_mutex);
  1053. rc = crypto_blkcipher_setkey(
  1054. desc.tfm, auth_tok->token.password.session_key_encryption_key,
  1055. crypt_stat->key_size);
  1056. if (unlikely(rc < 0)) {
  1057. mutex_unlock(tfm_mutex);
  1058. printk(KERN_ERR "Error setting key for crypto context\n");
  1059. rc = -EINVAL;
  1060. goto out;
  1061. }
  1062. rc = crypto_blkcipher_decrypt(&desc, &dst_sg, &src_sg,
  1063. auth_tok->session_key.encrypted_key_size);
  1064. mutex_unlock(tfm_mutex);
  1065. if (unlikely(rc)) {
  1066. printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
  1067. goto out;
  1068. }
  1069. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1070. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1071. auth_tok->session_key.decrypted_key_size);
  1072. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1073. if (unlikely(ecryptfs_verbosity > 0)) {
  1074. ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
  1075. crypt_stat->key_size);
  1076. ecryptfs_dump_hex(crypt_stat->key,
  1077. crypt_stat->key_size);
  1078. }
  1079. out:
  1080. return rc;
  1081. }
  1082. int ecryptfs_get_auth_tok_sig(char **sig, struct ecryptfs_auth_tok *auth_tok)
  1083. {
  1084. int rc = 0;
  1085. (*sig) = NULL;
  1086. switch (auth_tok->token_type) {
  1087. case ECRYPTFS_PASSWORD:
  1088. (*sig) = auth_tok->token.password.signature;
  1089. break;
  1090. case ECRYPTFS_PRIVATE_KEY:
  1091. (*sig) = auth_tok->token.private_key.signature;
  1092. break;
  1093. default:
  1094. printk(KERN_ERR "Cannot get sig for auth_tok of type [%d]\n",
  1095. auth_tok->token_type);
  1096. rc = -EINVAL;
  1097. }
  1098. return rc;
  1099. }
  1100. /**
  1101. * ecryptfs_parse_packet_set
  1102. * @dest: The header page in memory
  1103. * @version: Version of file format, to guide parsing behavior
  1104. *
  1105. * Get crypt_stat to have the file's session key if the requisite key
  1106. * is available to decrypt the session key.
  1107. *
  1108. * Returns Zero if a valid authentication token was retrieved and
  1109. * processed; negative value for file not encrypted or for error
  1110. * conditions.
  1111. */
  1112. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  1113. unsigned char *src,
  1114. struct dentry *ecryptfs_dentry)
  1115. {
  1116. size_t i = 0;
  1117. size_t found_auth_tok;
  1118. size_t next_packet_is_auth_tok_packet;
  1119. struct list_head auth_tok_list;
  1120. struct ecryptfs_auth_tok *matching_auth_tok = NULL;
  1121. struct ecryptfs_auth_tok *candidate_auth_tok = NULL;
  1122. char *candidate_auth_tok_sig;
  1123. size_t packet_size;
  1124. struct ecryptfs_auth_tok *new_auth_tok;
  1125. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  1126. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1127. size_t tag_11_contents_size;
  1128. size_t tag_11_packet_size;
  1129. int rc = 0;
  1130. INIT_LIST_HEAD(&auth_tok_list);
  1131. /* Parse the header to find as many packets as we can; these will be
  1132. * added the our &auth_tok_list */
  1133. next_packet_is_auth_tok_packet = 1;
  1134. while (next_packet_is_auth_tok_packet) {
  1135. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  1136. switch (src[i]) {
  1137. case ECRYPTFS_TAG_3_PACKET_TYPE:
  1138. rc = parse_tag_3_packet(crypt_stat,
  1139. (unsigned char *)&src[i],
  1140. &auth_tok_list, &new_auth_tok,
  1141. &packet_size, max_packet_size);
  1142. if (rc) {
  1143. ecryptfs_printk(KERN_ERR, "Error parsing "
  1144. "tag 3 packet\n");
  1145. rc = -EIO;
  1146. goto out_wipe_list;
  1147. }
  1148. i += packet_size;
  1149. rc = parse_tag_11_packet((unsigned char *)&src[i],
  1150. sig_tmp_space,
  1151. ECRYPTFS_SIG_SIZE,
  1152. &tag_11_contents_size,
  1153. &tag_11_packet_size,
  1154. max_packet_size);
  1155. if (rc) {
  1156. ecryptfs_printk(KERN_ERR, "No valid "
  1157. "(ecryptfs-specific) literal "
  1158. "packet containing "
  1159. "authentication token "
  1160. "signature found after "
  1161. "tag 3 packet\n");
  1162. rc = -EIO;
  1163. goto out_wipe_list;
  1164. }
  1165. i += tag_11_packet_size;
  1166. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  1167. ecryptfs_printk(KERN_ERR, "Expected "
  1168. "signature of size [%d]; "
  1169. "read size [%d]\n",
  1170. ECRYPTFS_SIG_SIZE,
  1171. tag_11_contents_size);
  1172. rc = -EIO;
  1173. goto out_wipe_list;
  1174. }
  1175. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  1176. sig_tmp_space, tag_11_contents_size);
  1177. new_auth_tok->token.password.signature[
  1178. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  1179. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1180. break;
  1181. case ECRYPTFS_TAG_1_PACKET_TYPE:
  1182. rc = parse_tag_1_packet(crypt_stat,
  1183. (unsigned char *)&src[i],
  1184. &auth_tok_list, &new_auth_tok,
  1185. &packet_size, max_packet_size);
  1186. if (rc) {
  1187. ecryptfs_printk(KERN_ERR, "Error parsing "
  1188. "tag 1 packet\n");
  1189. rc = -EIO;
  1190. goto out_wipe_list;
  1191. }
  1192. i += packet_size;
  1193. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1194. break;
  1195. case ECRYPTFS_TAG_11_PACKET_TYPE:
  1196. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  1197. "(Tag 11 not allowed by itself)\n");
  1198. rc = -EIO;
  1199. goto out_wipe_list;
  1200. break;
  1201. default:
  1202. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  1203. "[%d] of the file header; hex value of "
  1204. "character is [0x%.2x]\n", i, src[i]);
  1205. next_packet_is_auth_tok_packet = 0;
  1206. }
  1207. }
  1208. if (list_empty(&auth_tok_list)) {
  1209. printk(KERN_ERR "The lower file appears to be a non-encrypted "
  1210. "eCryptfs file; this is not supported in this version "
  1211. "of the eCryptfs kernel module\n");
  1212. rc = -EINVAL;
  1213. goto out;
  1214. }
  1215. /* auth_tok_list contains the set of authentication tokens
  1216. * parsed from the metadata. We need to find a matching
  1217. * authentication token that has the secret component(s)
  1218. * necessary to decrypt the EFEK in the auth_tok parsed from
  1219. * the metadata. There may be several potential matches, but
  1220. * just one will be sufficient to decrypt to get the FEK. */
  1221. find_next_matching_auth_tok:
  1222. found_auth_tok = 0;
  1223. list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
  1224. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  1225. if (unlikely(ecryptfs_verbosity > 0)) {
  1226. ecryptfs_printk(KERN_DEBUG,
  1227. "Considering cadidate auth tok:\n");
  1228. ecryptfs_dump_auth_tok(candidate_auth_tok);
  1229. }
  1230. if ((rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
  1231. candidate_auth_tok))) {
  1232. printk(KERN_ERR
  1233. "Unrecognized candidate auth tok type: [%d]\n",
  1234. candidate_auth_tok->token_type);
  1235. rc = -EINVAL;
  1236. goto out_wipe_list;
  1237. }
  1238. if ((rc = ecryptfs_find_auth_tok_for_sig(
  1239. &matching_auth_tok, crypt_stat,
  1240. candidate_auth_tok_sig)))
  1241. rc = 0;
  1242. if (matching_auth_tok) {
  1243. found_auth_tok = 1;
  1244. goto found_matching_auth_tok;
  1245. }
  1246. }
  1247. if (!found_auth_tok) {
  1248. ecryptfs_printk(KERN_ERR, "Could not find a usable "
  1249. "authentication token\n");
  1250. rc = -EIO;
  1251. goto out_wipe_list;
  1252. }
  1253. found_matching_auth_tok:
  1254. if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1255. memcpy(&(candidate_auth_tok->token.private_key),
  1256. &(matching_auth_tok->token.private_key),
  1257. sizeof(struct ecryptfs_private_key));
  1258. rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
  1259. crypt_stat);
  1260. } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1261. memcpy(&(candidate_auth_tok->token.password),
  1262. &(matching_auth_tok->token.password),
  1263. sizeof(struct ecryptfs_password));
  1264. rc = decrypt_passphrase_encrypted_session_key(
  1265. candidate_auth_tok, crypt_stat);
  1266. }
  1267. if (rc) {
  1268. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1269. ecryptfs_printk(KERN_WARNING, "Error decrypting the "
  1270. "session key for authentication token with sig "
  1271. "[%.*s]; rc = [%d]. Removing auth tok "
  1272. "candidate from the list and searching for "
  1273. "the next match.\n", candidate_auth_tok_sig,
  1274. ECRYPTFS_SIG_SIZE_HEX, rc);
  1275. list_for_each_entry_safe(auth_tok_list_item,
  1276. auth_tok_list_item_tmp,
  1277. &auth_tok_list, list) {
  1278. if (candidate_auth_tok
  1279. == &auth_tok_list_item->auth_tok) {
  1280. list_del(&auth_tok_list_item->list);
  1281. kmem_cache_free(
  1282. ecryptfs_auth_tok_list_item_cache,
  1283. auth_tok_list_item);
  1284. goto find_next_matching_auth_tok;
  1285. }
  1286. }
  1287. BUG();
  1288. }
  1289. rc = ecryptfs_compute_root_iv(crypt_stat);
  1290. if (rc) {
  1291. ecryptfs_printk(KERN_ERR, "Error computing "
  1292. "the root IV\n");
  1293. goto out_wipe_list;
  1294. }
  1295. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  1296. if (rc) {
  1297. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  1298. "context for cipher [%s]; rc = [%d]\n",
  1299. crypt_stat->cipher, rc);
  1300. }
  1301. out_wipe_list:
  1302. wipe_auth_tok_list(&auth_tok_list);
  1303. out:
  1304. return rc;
  1305. }
  1306. static int
  1307. pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
  1308. struct ecryptfs_crypt_stat *crypt_stat,
  1309. struct ecryptfs_key_record *key_rec)
  1310. {
  1311. struct ecryptfs_msg_ctx *msg_ctx = NULL;
  1312. char *netlink_payload;
  1313. size_t netlink_payload_length;
  1314. struct ecryptfs_message *msg;
  1315. int rc;
  1316. rc = write_tag_66_packet(auth_tok->token.private_key.signature,
  1317. ecryptfs_code_for_cipher_string(crypt_stat),
  1318. crypt_stat, &netlink_payload,
  1319. &netlink_payload_length);
  1320. if (rc) {
  1321. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
  1322. goto out;
  1323. }
  1324. rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
  1325. netlink_payload_length, &msg_ctx);
  1326. if (rc) {
  1327. ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
  1328. goto out;
  1329. }
  1330. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1331. if (rc) {
  1332. ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  1333. "from the user space daemon\n");
  1334. rc = -EIO;
  1335. goto out;
  1336. }
  1337. rc = parse_tag_67_packet(key_rec, msg);
  1338. if (rc)
  1339. ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
  1340. kfree(msg);
  1341. out:
  1342. if (netlink_payload)
  1343. kfree(netlink_payload);
  1344. return rc;
  1345. }
  1346. /**
  1347. * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
  1348. * @dest: Buffer into which to write the packet
  1349. * @max: Maximum number of bytes that can be writtn
  1350. * @packet_size: This function will write the number of bytes that end
  1351. * up constituting the packet; set to zero on error
  1352. *
  1353. * Returns zero on success; non-zero on error.
  1354. */
  1355. static int
  1356. write_tag_1_packet(char *dest, size_t *remaining_bytes,
  1357. struct ecryptfs_auth_tok *auth_tok,
  1358. struct ecryptfs_crypt_stat *crypt_stat,
  1359. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1360. {
  1361. size_t i;
  1362. size_t encrypted_session_key_valid = 0;
  1363. size_t packet_size_length;
  1364. size_t max_packet_size;
  1365. int rc = 0;
  1366. (*packet_size) = 0;
  1367. ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  1368. ECRYPTFS_SIG_SIZE);
  1369. encrypted_session_key_valid = 0;
  1370. for (i = 0; i < crypt_stat->key_size; i++)
  1371. encrypted_session_key_valid |=
  1372. auth_tok->session_key.encrypted_key[i];
  1373. if (encrypted_session_key_valid) {
  1374. memcpy(key_rec->enc_key,
  1375. auth_tok->session_key.encrypted_key,
  1376. auth_tok->session_key.encrypted_key_size);
  1377. goto encrypted_session_key_set;
  1378. }
  1379. if (auth_tok->session_key.encrypted_key_size == 0)
  1380. auth_tok->session_key.encrypted_key_size =
  1381. auth_tok->token.private_key.key_size;
  1382. rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
  1383. if (rc) {
  1384. ecryptfs_printk(KERN_ERR, "Failed to encrypt session key "
  1385. "via a pki");
  1386. goto out;
  1387. }
  1388. if (ecryptfs_verbosity > 0) {
  1389. ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
  1390. ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  1391. }
  1392. encrypted_session_key_set:
  1393. /* This format is inspired by OpenPGP; see RFC 2440
  1394. * packet tag 1 */
  1395. max_packet_size = (1 /* Tag 1 identifier */
  1396. + 3 /* Max Tag 1 packet size */
  1397. + 1 /* Version */
  1398. + ECRYPTFS_SIG_SIZE /* Key identifier */
  1399. + 1 /* Cipher identifier */
  1400. + key_rec->enc_key_size); /* Encrypted key size */
  1401. if (max_packet_size > (*remaining_bytes)) {
  1402. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1403. "need up to [%d] bytes, but there are only [%d] "
  1404. "available\n", max_packet_size, (*remaining_bytes));
  1405. rc = -EINVAL;
  1406. goto out;
  1407. }
  1408. dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
  1409. rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
  1410. &packet_size_length);
  1411. if (rc) {
  1412. ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  1413. "header; cannot generate packet length\n");
  1414. goto out;
  1415. }
  1416. (*packet_size) += packet_size_length;
  1417. dest[(*packet_size)++] = 0x03; /* version 3 */
  1418. memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  1419. (*packet_size) += ECRYPTFS_SIG_SIZE;
  1420. dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  1421. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1422. key_rec->enc_key_size);
  1423. (*packet_size) += key_rec->enc_key_size;
  1424. out:
  1425. if (rc)
  1426. (*packet_size) = 0;
  1427. else
  1428. (*remaining_bytes) -= (*packet_size);
  1429. return rc;
  1430. }
  1431. /**
  1432. * write_tag_11_packet
  1433. * @dest: Target into which Tag 11 packet is to be written
  1434. * @max: Maximum packet length
  1435. * @contents: Byte array of contents to copy in
  1436. * @contents_length: Number of bytes in contents
  1437. * @packet_length: Length of the Tag 11 packet written; zero on error
  1438. *
  1439. * Returns zero on success; non-zero on error.
  1440. */
  1441. static int
  1442. write_tag_11_packet(char *dest, int max, char *contents, size_t contents_length,
  1443. size_t *packet_length)
  1444. {
  1445. size_t packet_size_length;
  1446. int rc = 0;
  1447. (*packet_length) = 0;
  1448. if ((13 + contents_length) > max) {
  1449. rc = -EINVAL;
  1450. ecryptfs_printk(KERN_ERR, "Packet length larger than "
  1451. "maximum allowable\n");
  1452. goto out;
  1453. }
  1454. /* General packet header */
  1455. /* Packet tag */
  1456. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  1457. /* Packet length */
  1458. rc = write_packet_length(&dest[(*packet_length)],
  1459. (13 + contents_length), &packet_size_length);
  1460. if (rc) {
  1461. ecryptfs_printk(KERN_ERR, "Error generating tag 11 packet "
  1462. "header; cannot generate packet length\n");
  1463. goto out;
  1464. }
  1465. (*packet_length) += packet_size_length;
  1466. /* Tag 11 specific */
  1467. /* One-octet field that describes how the data is formatted */
  1468. dest[(*packet_length)++] = 0x62; /* binary data */
  1469. /* One-octet filename length followed by filename */
  1470. dest[(*packet_length)++] = 8;
  1471. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  1472. (*packet_length) += 8;
  1473. /* Four-octet number indicating modification date */
  1474. memset(&dest[(*packet_length)], 0x00, 4);
  1475. (*packet_length) += 4;
  1476. /* Remainder is literal data */
  1477. memcpy(&dest[(*packet_length)], contents, contents_length);
  1478. (*packet_length) += contents_length;
  1479. out:
  1480. if (rc)
  1481. (*packet_length) = 0;
  1482. return rc;
  1483. }
  1484. /**
  1485. * write_tag_3_packet
  1486. * @dest: Buffer into which to write the packet
  1487. * @max: Maximum number of bytes that can be written
  1488. * @auth_tok: Authentication token
  1489. * @crypt_stat: The cryptographic context
  1490. * @key_rec: encrypted key
  1491. * @packet_size: This function will write the number of bytes that end
  1492. * up constituting the packet; set to zero on error
  1493. *
  1494. * Returns zero on success; non-zero on error.
  1495. */
  1496. static int
  1497. write_tag_3_packet(char *dest, size_t *remaining_bytes,
  1498. struct ecryptfs_auth_tok *auth_tok,
  1499. struct ecryptfs_crypt_stat *crypt_stat,
  1500. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1501. {
  1502. size_t i;
  1503. size_t encrypted_session_key_valid = 0;
  1504. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  1505. struct scatterlist dst_sg;
  1506. struct scatterlist src_sg;
  1507. struct mutex *tfm_mutex = NULL;
  1508. size_t cipher_code;
  1509. size_t packet_size_length;
  1510. size_t max_packet_size;
  1511. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1512. crypt_stat->mount_crypt_stat;
  1513. struct blkcipher_desc desc = {
  1514. .tfm = NULL,
  1515. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1516. };
  1517. int rc = 0;
  1518. (*packet_size) = 0;
  1519. ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
  1520. ECRYPTFS_SIG_SIZE);
  1521. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1522. crypt_stat->cipher);
  1523. if (unlikely(rc)) {
  1524. printk(KERN_ERR "Internal error whilst attempting to get "
  1525. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1526. crypt_stat->cipher, rc);
  1527. goto out;
  1528. }
  1529. if (mount_crypt_stat->global_default_cipher_key_size == 0) {
  1530. struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
  1531. printk(KERN_WARNING "No key size specified at mount; "
  1532. "defaulting to [%d]\n", alg->max_keysize);
  1533. mount_crypt_stat->global_default_cipher_key_size =
  1534. alg->max_keysize;
  1535. }
  1536. if (crypt_stat->key_size == 0)
  1537. crypt_stat->key_size =
  1538. mount_crypt_stat->global_default_cipher_key_size;
  1539. if (auth_tok->session_key.encrypted_key_size == 0)
  1540. auth_tok->session_key.encrypted_key_size =
  1541. crypt_stat->key_size;
  1542. if (crypt_stat->key_size == 24
  1543. && strcmp("aes", crypt_stat->cipher) == 0) {
  1544. memset((crypt_stat->key + 24), 0, 8);
  1545. auth_tok->session_key.encrypted_key_size = 32;
  1546. } else
  1547. auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
  1548. key_rec->enc_key_size =
  1549. auth_tok->session_key.encrypted_key_size;
  1550. encrypted_session_key_valid = 0;
  1551. for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  1552. encrypted_session_key_valid |=
  1553. auth_tok->session_key.encrypted_key[i];
  1554. if (encrypted_session_key_valid) {
  1555. ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  1556. "using auth_tok->session_key.encrypted_key, "
  1557. "where key_rec->enc_key_size = [%d]\n",
  1558. key_rec->enc_key_size);
  1559. memcpy(key_rec->enc_key,
  1560. auth_tok->session_key.encrypted_key,
  1561. key_rec->enc_key_size);
  1562. goto encrypted_session_key_set;
  1563. }
  1564. if (auth_tok->token.password.flags &
  1565. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
  1566. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  1567. "session key encryption key of size [%d]\n",
  1568. auth_tok->token.password.
  1569. session_key_encryption_key_bytes);
  1570. memcpy(session_key_encryption_key,
  1571. auth_tok->token.password.session_key_encryption_key,
  1572. crypt_stat->key_size);
  1573. ecryptfs_printk(KERN_DEBUG,
  1574. "Cached session key " "encryption key: \n");
  1575. if (ecryptfs_verbosity > 0)
  1576. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1577. }
  1578. if (unlikely(ecryptfs_verbosity > 0)) {
  1579. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  1580. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1581. }
  1582. if ((rc = virt_to_scatterlist(crypt_stat->key,
  1583. key_rec->enc_key_size, &src_sg, 1))
  1584. != 1) {
  1585. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1586. "for crypt_stat session key; expected rc = 1; "
  1587. "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
  1588. rc, key_rec->enc_key_size);
  1589. rc = -ENOMEM;
  1590. goto out;
  1591. }
  1592. if ((rc = virt_to_scatterlist(key_rec->enc_key,
  1593. key_rec->enc_key_size, &dst_sg, 1))
  1594. != 1) {
  1595. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1596. "for crypt_stat encrypted session key; "
  1597. "expected rc = 1; got rc = [%d]. "
  1598. "key_rec->enc_key_size = [%d]\n", rc,
  1599. key_rec->enc_key_size);
  1600. rc = -ENOMEM;
  1601. goto out;
  1602. }
  1603. mutex_lock(tfm_mutex);
  1604. rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
  1605. crypt_stat->key_size);
  1606. if (rc < 0) {
  1607. mutex_unlock(tfm_mutex);
  1608. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  1609. "context; rc = [%d]\n", rc);
  1610. goto out;
  1611. }
  1612. rc = 0;
  1613. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  1614. crypt_stat->key_size);
  1615. rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
  1616. (*key_rec).enc_key_size);
  1617. mutex_unlock(tfm_mutex);
  1618. if (rc) {
  1619. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  1620. goto out;
  1621. }
  1622. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  1623. if (ecryptfs_verbosity > 0) {
  1624. ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
  1625. key_rec->enc_key_size);
  1626. ecryptfs_dump_hex(key_rec->enc_key,
  1627. key_rec->enc_key_size);
  1628. }
  1629. encrypted_session_key_set:
  1630. /* This format is inspired by OpenPGP; see RFC 2440
  1631. * packet tag 3 */
  1632. max_packet_size = (1 /* Tag 3 identifier */
  1633. + 3 /* Max Tag 3 packet size */
  1634. + 1 /* Version */
  1635. + 1 /* Cipher code */
  1636. + 1 /* S2K specifier */
  1637. + 1 /* Hash identifier */
  1638. + ECRYPTFS_SALT_SIZE /* Salt */
  1639. + 1 /* Hash iterations */
  1640. + key_rec->enc_key_size); /* Encrypted key size */
  1641. if (max_packet_size > (*remaining_bytes)) {
  1642. printk(KERN_ERR "Packet too large; need up to [%d] bytes, but "
  1643. "there are only [%d] available\n", max_packet_size,
  1644. (*remaining_bytes));
  1645. rc = -EINVAL;
  1646. goto out;
  1647. }
  1648. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  1649. /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  1650. * to get the number of octets in the actual Tag 3 packet */
  1651. rc = write_packet_length(&dest[(*packet_size)], (max_packet_size - 4),
  1652. &packet_size_length);
  1653. if (rc) {
  1654. printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  1655. "generate packet length. rc = [%d]\n", rc);
  1656. goto out;
  1657. }
  1658. (*packet_size) += packet_size_length;
  1659. dest[(*packet_size)++] = 0x04; /* version 4 */
  1660. /* TODO: Break from RFC2440 so that arbitrary ciphers can be
  1661. * specified with strings */
  1662. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  1663. if (cipher_code == 0) {
  1664. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  1665. "cipher [%s]\n", crypt_stat->cipher);
  1666. rc = -EINVAL;
  1667. goto out;
  1668. }
  1669. dest[(*packet_size)++] = cipher_code;
  1670. dest[(*packet_size)++] = 0x03; /* S2K */
  1671. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  1672. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  1673. ECRYPTFS_SALT_SIZE);
  1674. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  1675. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  1676. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1677. key_rec->enc_key_size);
  1678. (*packet_size) += key_rec->enc_key_size;
  1679. out:
  1680. if (rc)
  1681. (*packet_size) = 0;
  1682. else
  1683. (*remaining_bytes) -= (*packet_size);
  1684. return rc;
  1685. }
  1686. struct kmem_cache *ecryptfs_key_record_cache;
  1687. /**
  1688. * ecryptfs_generate_key_packet_set
  1689. * @dest: Virtual address from which to write the key record set
  1690. * @crypt_stat: The cryptographic context from which the
  1691. * authentication tokens will be retrieved
  1692. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  1693. * for the global parameters
  1694. * @len: The amount written
  1695. * @max: The maximum amount of data allowed to be written
  1696. *
  1697. * Generates a key packet set and writes it to the virtual address
  1698. * passed in.
  1699. *
  1700. * Returns zero on success; non-zero on error.
  1701. */
  1702. int
  1703. ecryptfs_generate_key_packet_set(char *dest_base,
  1704. struct ecryptfs_crypt_stat *crypt_stat,
  1705. struct dentry *ecryptfs_dentry, size_t *len,
  1706. size_t max)
  1707. {
  1708. struct ecryptfs_auth_tok *auth_tok;
  1709. struct ecryptfs_global_auth_tok *global_auth_tok;
  1710. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1711. &ecryptfs_superblock_to_private(
  1712. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1713. size_t written;
  1714. struct ecryptfs_key_record *key_rec;
  1715. struct ecryptfs_key_sig *key_sig;
  1716. int rc = 0;
  1717. (*len) = 0;
  1718. mutex_lock(&crypt_stat->keysig_list_mutex);
  1719. key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  1720. if (!key_rec) {
  1721. rc = -ENOMEM;
  1722. goto out;
  1723. }
  1724. list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  1725. crypt_stat_list) {
  1726. memset(key_rec, 0, sizeof(*key_rec));
  1727. rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
  1728. mount_crypt_stat,
  1729. key_sig->keysig);
  1730. if (rc) {
  1731. printk(KERN_ERR "Error attempting to get the global "
  1732. "auth_tok; rc = [%d]\n", rc);
  1733. goto out_free;
  1734. }
  1735. if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  1736. printk(KERN_WARNING
  1737. "Skipping invalid auth tok with sig = [%s]\n",
  1738. global_auth_tok->sig);
  1739. continue;
  1740. }
  1741. auth_tok = global_auth_tok->global_auth_tok;
  1742. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1743. rc = write_tag_3_packet((dest_base + (*len)),
  1744. &max, auth_tok,
  1745. crypt_stat, key_rec,
  1746. &written);
  1747. if (rc) {
  1748. ecryptfs_printk(KERN_WARNING, "Error "
  1749. "writing tag 3 packet\n");
  1750. goto out_free;
  1751. }
  1752. (*len) += written;
  1753. /* Write auth tok signature packet */
  1754. rc = write_tag_11_packet((dest_base + (*len)), &max,
  1755. key_rec->sig,
  1756. ECRYPTFS_SIG_SIZE, &written);
  1757. if (rc) {
  1758. ecryptfs_printk(KERN_ERR, "Error writing "
  1759. "auth tok signature packet\n");
  1760. goto out_free;
  1761. }
  1762. (*len) += written;
  1763. } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1764. rc = write_tag_1_packet(dest_base + (*len),
  1765. &max, auth_tok,
  1766. crypt_stat, key_rec, &written);
  1767. if (rc) {
  1768. ecryptfs_printk(KERN_WARNING, "Error "
  1769. "writing tag 1 packet\n");
  1770. goto out_free;
  1771. }
  1772. (*len) += written;
  1773. } else {
  1774. ecryptfs_printk(KERN_WARNING, "Unsupported "
  1775. "authentication token type\n");
  1776. rc = -EINVAL;
  1777. goto out_free;
  1778. }
  1779. }
  1780. if (likely(max > 0)) {
  1781. dest_base[(*len)] = 0x00;
  1782. } else {
  1783. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1784. rc = -EIO;
  1785. }
  1786. out_free:
  1787. kmem_cache_free(ecryptfs_key_record_cache, key_rec);
  1788. out:
  1789. if (rc)
  1790. (*len) = 0;
  1791. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1792. return rc;
  1793. }
  1794. struct kmem_cache *ecryptfs_key_sig_cache;
  1795. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  1796. {
  1797. struct ecryptfs_key_sig *new_key_sig;
  1798. int rc = 0;
  1799. new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  1800. if (!new_key_sig) {
  1801. rc = -ENOMEM;
  1802. printk(KERN_ERR
  1803. "Error allocating from ecryptfs_key_sig_cache\n");
  1804. goto out;
  1805. }
  1806. memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1807. mutex_lock(&crypt_stat->keysig_list_mutex);
  1808. list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
  1809. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1810. out:
  1811. return rc;
  1812. }
  1813. struct kmem_cache *ecryptfs_global_auth_tok_cache;
  1814. int
  1815. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  1816. char *sig)
  1817. {
  1818. struct ecryptfs_global_auth_tok *new_auth_tok;
  1819. int rc = 0;
  1820. new_auth_tok = kmem_cache_alloc(ecryptfs_global_auth_tok_cache,
  1821. GFP_KERNEL);
  1822. if (!new_auth_tok) {
  1823. rc = -ENOMEM;
  1824. printk(KERN_ERR "Error allocating from "
  1825. "ecryptfs_global_auth_tok_cache\n");
  1826. goto out;
  1827. }
  1828. memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1829. new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  1830. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1831. list_add(&new_auth_tok->mount_crypt_stat_list,
  1832. &mount_crypt_stat->global_auth_tok_list);
  1833. mount_crypt_stat->num_global_auth_toks++;
  1834. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1835. out:
  1836. return rc;
  1837. }