keystore.c 53 KB

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