keystore.c 59 KB

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