keystore.c 53 KB

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