keystore.c 58 KB

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