keystore.c 58 KB

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