keystore.c 58 KB

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