keystore.c 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884
  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[2];
  1001. struct scatterlist src_sg[2];
  1002. struct mutex *tfm_mutex;
  1003. struct blkcipher_desc desc = {
  1004. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1005. };
  1006. int rc = 0;
  1007. if (unlikely(ecryptfs_verbosity > 0)) {
  1008. ecryptfs_printk(
  1009. KERN_DEBUG, "Session key encryption key (size [%d]):\n",
  1010. auth_tok->token.password.session_key_encryption_key_bytes);
  1011. ecryptfs_dump_hex(
  1012. auth_tok->token.password.session_key_encryption_key,
  1013. auth_tok->token.password.session_key_encryption_key_bytes);
  1014. }
  1015. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1016. crypt_stat->cipher);
  1017. if (unlikely(rc)) {
  1018. printk(KERN_ERR "Internal error whilst attempting to get "
  1019. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1020. crypt_stat->cipher, rc);
  1021. goto out;
  1022. }
  1023. rc = virt_to_scatterlist(auth_tok->session_key.encrypted_key,
  1024. auth_tok->session_key.encrypted_key_size,
  1025. src_sg, 2);
  1026. if (rc < 1 || rc > 2) {
  1027. printk(KERN_ERR "Internal error whilst attempting to convert "
  1028. "auth_tok->session_key.encrypted_key to scatterlist; "
  1029. "expected rc = 1; got rc = [%d]. "
  1030. "auth_tok->session_key.encrypted_key_size = [%d]\n", rc,
  1031. auth_tok->session_key.encrypted_key_size);
  1032. goto out;
  1033. }
  1034. auth_tok->session_key.decrypted_key_size =
  1035. auth_tok->session_key.encrypted_key_size;
  1036. rc = virt_to_scatterlist(auth_tok->session_key.decrypted_key,
  1037. auth_tok->session_key.decrypted_key_size,
  1038. dst_sg, 2);
  1039. if (rc < 1 || rc > 2) {
  1040. printk(KERN_ERR "Internal error whilst attempting to convert "
  1041. "auth_tok->session_key.decrypted_key to scatterlist; "
  1042. "expected rc = 1; got rc = [%d]\n", rc);
  1043. goto out;
  1044. }
  1045. mutex_lock(tfm_mutex);
  1046. rc = crypto_blkcipher_setkey(
  1047. desc.tfm, auth_tok->token.password.session_key_encryption_key,
  1048. crypt_stat->key_size);
  1049. if (unlikely(rc < 0)) {
  1050. mutex_unlock(tfm_mutex);
  1051. printk(KERN_ERR "Error setting key for crypto context\n");
  1052. rc = -EINVAL;
  1053. goto out;
  1054. }
  1055. rc = crypto_blkcipher_decrypt(&desc, dst_sg, src_sg,
  1056. auth_tok->session_key.encrypted_key_size);
  1057. mutex_unlock(tfm_mutex);
  1058. if (unlikely(rc)) {
  1059. printk(KERN_ERR "Error decrypting; rc = [%d]\n", rc);
  1060. goto out;
  1061. }
  1062. auth_tok->session_key.flags |= ECRYPTFS_CONTAINS_DECRYPTED_KEY;
  1063. memcpy(crypt_stat->key, auth_tok->session_key.decrypted_key,
  1064. auth_tok->session_key.decrypted_key_size);
  1065. crypt_stat->flags |= ECRYPTFS_KEY_VALID;
  1066. if (unlikely(ecryptfs_verbosity > 0)) {
  1067. ecryptfs_printk(KERN_DEBUG, "FEK of size [%d]:\n",
  1068. crypt_stat->key_size);
  1069. ecryptfs_dump_hex(crypt_stat->key,
  1070. crypt_stat->key_size);
  1071. }
  1072. out:
  1073. return rc;
  1074. }
  1075. /**
  1076. * ecryptfs_parse_packet_set
  1077. * @crypt_stat: The cryptographic context
  1078. * @src: Virtual address of region of memory containing the packets
  1079. * @ecryptfs_dentry: The eCryptfs dentry associated with the packet set
  1080. *
  1081. * Get crypt_stat to have the file's session key if the requisite key
  1082. * is available to decrypt the session key.
  1083. *
  1084. * Returns Zero if a valid authentication token was retrieved and
  1085. * processed; negative value for file not encrypted or for error
  1086. * conditions.
  1087. */
  1088. int ecryptfs_parse_packet_set(struct ecryptfs_crypt_stat *crypt_stat,
  1089. unsigned char *src,
  1090. struct dentry *ecryptfs_dentry)
  1091. {
  1092. size_t i = 0;
  1093. size_t found_auth_tok;
  1094. size_t next_packet_is_auth_tok_packet;
  1095. struct list_head auth_tok_list;
  1096. struct ecryptfs_auth_tok *matching_auth_tok;
  1097. struct ecryptfs_auth_tok *candidate_auth_tok;
  1098. char *candidate_auth_tok_sig;
  1099. size_t packet_size;
  1100. struct ecryptfs_auth_tok *new_auth_tok;
  1101. unsigned char sig_tmp_space[ECRYPTFS_SIG_SIZE];
  1102. struct ecryptfs_auth_tok_list_item *auth_tok_list_item;
  1103. size_t tag_11_contents_size;
  1104. size_t tag_11_packet_size;
  1105. int rc = 0;
  1106. INIT_LIST_HEAD(&auth_tok_list);
  1107. /* Parse the header to find as many packets as we can; these will be
  1108. * added the our &auth_tok_list */
  1109. next_packet_is_auth_tok_packet = 1;
  1110. while (next_packet_is_auth_tok_packet) {
  1111. size_t max_packet_size = ((PAGE_CACHE_SIZE - 8) - i);
  1112. switch (src[i]) {
  1113. case ECRYPTFS_TAG_3_PACKET_TYPE:
  1114. rc = parse_tag_3_packet(crypt_stat,
  1115. (unsigned char *)&src[i],
  1116. &auth_tok_list, &new_auth_tok,
  1117. &packet_size, max_packet_size);
  1118. if (rc) {
  1119. ecryptfs_printk(KERN_ERR, "Error parsing "
  1120. "tag 3 packet\n");
  1121. rc = -EIO;
  1122. goto out_wipe_list;
  1123. }
  1124. i += packet_size;
  1125. rc = parse_tag_11_packet((unsigned char *)&src[i],
  1126. sig_tmp_space,
  1127. ECRYPTFS_SIG_SIZE,
  1128. &tag_11_contents_size,
  1129. &tag_11_packet_size,
  1130. max_packet_size);
  1131. if (rc) {
  1132. ecryptfs_printk(KERN_ERR, "No valid "
  1133. "(ecryptfs-specific) literal "
  1134. "packet containing "
  1135. "authentication token "
  1136. "signature found after "
  1137. "tag 3 packet\n");
  1138. rc = -EIO;
  1139. goto out_wipe_list;
  1140. }
  1141. i += tag_11_packet_size;
  1142. if (ECRYPTFS_SIG_SIZE != tag_11_contents_size) {
  1143. ecryptfs_printk(KERN_ERR, "Expected "
  1144. "signature of size [%d]; "
  1145. "read size [%d]\n",
  1146. ECRYPTFS_SIG_SIZE,
  1147. tag_11_contents_size);
  1148. rc = -EIO;
  1149. goto out_wipe_list;
  1150. }
  1151. ecryptfs_to_hex(new_auth_tok->token.password.signature,
  1152. sig_tmp_space, tag_11_contents_size);
  1153. new_auth_tok->token.password.signature[
  1154. ECRYPTFS_PASSWORD_SIG_SIZE] = '\0';
  1155. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1156. break;
  1157. case ECRYPTFS_TAG_1_PACKET_TYPE:
  1158. rc = parse_tag_1_packet(crypt_stat,
  1159. (unsigned char *)&src[i],
  1160. &auth_tok_list, &new_auth_tok,
  1161. &packet_size, max_packet_size);
  1162. if (rc) {
  1163. ecryptfs_printk(KERN_ERR, "Error parsing "
  1164. "tag 1 packet\n");
  1165. rc = -EIO;
  1166. goto out_wipe_list;
  1167. }
  1168. i += packet_size;
  1169. crypt_stat->flags |= ECRYPTFS_ENCRYPTED;
  1170. break;
  1171. case ECRYPTFS_TAG_11_PACKET_TYPE:
  1172. ecryptfs_printk(KERN_WARNING, "Invalid packet set "
  1173. "(Tag 11 not allowed by itself)\n");
  1174. rc = -EIO;
  1175. goto out_wipe_list;
  1176. break;
  1177. default:
  1178. ecryptfs_printk(KERN_DEBUG, "No packet at offset "
  1179. "[%d] of the file header; hex value of "
  1180. "character is [0x%.2x]\n", i, src[i]);
  1181. next_packet_is_auth_tok_packet = 0;
  1182. }
  1183. }
  1184. if (list_empty(&auth_tok_list)) {
  1185. printk(KERN_ERR "The lower file appears to be a non-encrypted "
  1186. "eCryptfs file; this is not supported in this version "
  1187. "of the eCryptfs kernel module\n");
  1188. rc = -EINVAL;
  1189. goto out;
  1190. }
  1191. /* auth_tok_list contains the set of authentication tokens
  1192. * parsed from the metadata. We need to find a matching
  1193. * authentication token that has the secret component(s)
  1194. * necessary to decrypt the EFEK in the auth_tok parsed from
  1195. * the metadata. There may be several potential matches, but
  1196. * just one will be sufficient to decrypt to get the FEK. */
  1197. find_next_matching_auth_tok:
  1198. found_auth_tok = 0;
  1199. list_for_each_entry(auth_tok_list_item, &auth_tok_list, list) {
  1200. candidate_auth_tok = &auth_tok_list_item->auth_tok;
  1201. if (unlikely(ecryptfs_verbosity > 0)) {
  1202. ecryptfs_printk(KERN_DEBUG,
  1203. "Considering cadidate auth tok:\n");
  1204. ecryptfs_dump_auth_tok(candidate_auth_tok);
  1205. }
  1206. rc = ecryptfs_get_auth_tok_sig(&candidate_auth_tok_sig,
  1207. candidate_auth_tok);
  1208. if (rc) {
  1209. printk(KERN_ERR
  1210. "Unrecognized candidate auth tok type: [%d]\n",
  1211. candidate_auth_tok->token_type);
  1212. rc = -EINVAL;
  1213. goto out_wipe_list;
  1214. }
  1215. ecryptfs_find_auth_tok_for_sig(&matching_auth_tok, crypt_stat,
  1216. candidate_auth_tok_sig);
  1217. if (matching_auth_tok) {
  1218. found_auth_tok = 1;
  1219. goto found_matching_auth_tok;
  1220. }
  1221. }
  1222. if (!found_auth_tok) {
  1223. ecryptfs_printk(KERN_ERR, "Could not find a usable "
  1224. "authentication token\n");
  1225. rc = -EIO;
  1226. goto out_wipe_list;
  1227. }
  1228. found_matching_auth_tok:
  1229. if (candidate_auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1230. memcpy(&(candidate_auth_tok->token.private_key),
  1231. &(matching_auth_tok->token.private_key),
  1232. sizeof(struct ecryptfs_private_key));
  1233. rc = decrypt_pki_encrypted_session_key(candidate_auth_tok,
  1234. crypt_stat);
  1235. } else if (candidate_auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1236. memcpy(&(candidate_auth_tok->token.password),
  1237. &(matching_auth_tok->token.password),
  1238. sizeof(struct ecryptfs_password));
  1239. rc = decrypt_passphrase_encrypted_session_key(
  1240. candidate_auth_tok, crypt_stat);
  1241. }
  1242. if (rc) {
  1243. struct ecryptfs_auth_tok_list_item *auth_tok_list_item_tmp;
  1244. ecryptfs_printk(KERN_WARNING, "Error decrypting the "
  1245. "session key for authentication token with sig "
  1246. "[%.*s]; rc = [%d]. Removing auth tok "
  1247. "candidate from the list and searching for "
  1248. "the next match.\n", candidate_auth_tok_sig,
  1249. ECRYPTFS_SIG_SIZE_HEX, rc);
  1250. list_for_each_entry_safe(auth_tok_list_item,
  1251. auth_tok_list_item_tmp,
  1252. &auth_tok_list, list) {
  1253. if (candidate_auth_tok
  1254. == &auth_tok_list_item->auth_tok) {
  1255. list_del(&auth_tok_list_item->list);
  1256. kmem_cache_free(
  1257. ecryptfs_auth_tok_list_item_cache,
  1258. auth_tok_list_item);
  1259. goto find_next_matching_auth_tok;
  1260. }
  1261. }
  1262. BUG();
  1263. }
  1264. rc = ecryptfs_compute_root_iv(crypt_stat);
  1265. if (rc) {
  1266. ecryptfs_printk(KERN_ERR, "Error computing "
  1267. "the root IV\n");
  1268. goto out_wipe_list;
  1269. }
  1270. rc = ecryptfs_init_crypt_ctx(crypt_stat);
  1271. if (rc) {
  1272. ecryptfs_printk(KERN_ERR, "Error initializing crypto "
  1273. "context for cipher [%s]; rc = [%d]\n",
  1274. crypt_stat->cipher, rc);
  1275. }
  1276. out_wipe_list:
  1277. wipe_auth_tok_list(&auth_tok_list);
  1278. out:
  1279. return rc;
  1280. }
  1281. static int
  1282. pki_encrypt_session_key(struct ecryptfs_auth_tok *auth_tok,
  1283. struct ecryptfs_crypt_stat *crypt_stat,
  1284. struct ecryptfs_key_record *key_rec)
  1285. {
  1286. struct ecryptfs_msg_ctx *msg_ctx = NULL;
  1287. char *payload = NULL;
  1288. size_t payload_len;
  1289. struct ecryptfs_message *msg;
  1290. int rc;
  1291. rc = write_tag_66_packet(auth_tok->token.private_key.signature,
  1292. ecryptfs_code_for_cipher_string(crypt_stat),
  1293. crypt_stat, &payload, &payload_len);
  1294. if (rc) {
  1295. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
  1296. goto out;
  1297. }
  1298. rc = ecryptfs_send_message(payload, payload_len, &msg_ctx);
  1299. if (rc) {
  1300. ecryptfs_printk(KERN_ERR, "Error sending message to "
  1301. "ecryptfsd\n");
  1302. goto out;
  1303. }
  1304. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1305. if (rc) {
  1306. ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  1307. "from the user space daemon\n");
  1308. rc = -EIO;
  1309. goto out;
  1310. }
  1311. rc = parse_tag_67_packet(key_rec, msg);
  1312. if (rc)
  1313. ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
  1314. kfree(msg);
  1315. out:
  1316. kfree(payload);
  1317. return rc;
  1318. }
  1319. /**
  1320. * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
  1321. * @dest: Buffer into which to write the packet
  1322. * @remaining_bytes: Maximum number of bytes that can be writtn
  1323. * @auth_tok: The authentication token used for generating the tag 1 packet
  1324. * @crypt_stat: The cryptographic context
  1325. * @key_rec: The key record struct for the tag 1 packet
  1326. * @packet_size: This function will write the number of bytes that end
  1327. * up constituting the packet; set to zero on error
  1328. *
  1329. * Returns zero on success; non-zero on error.
  1330. */
  1331. static int
  1332. write_tag_1_packet(char *dest, size_t *remaining_bytes,
  1333. struct ecryptfs_auth_tok *auth_tok,
  1334. struct ecryptfs_crypt_stat *crypt_stat,
  1335. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1336. {
  1337. size_t i;
  1338. size_t encrypted_session_key_valid = 0;
  1339. size_t packet_size_length;
  1340. size_t max_packet_size;
  1341. int rc = 0;
  1342. (*packet_size) = 0;
  1343. ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  1344. ECRYPTFS_SIG_SIZE);
  1345. encrypted_session_key_valid = 0;
  1346. for (i = 0; i < crypt_stat->key_size; i++)
  1347. encrypted_session_key_valid |=
  1348. auth_tok->session_key.encrypted_key[i];
  1349. if (encrypted_session_key_valid) {
  1350. memcpy(key_rec->enc_key,
  1351. auth_tok->session_key.encrypted_key,
  1352. auth_tok->session_key.encrypted_key_size);
  1353. goto encrypted_session_key_set;
  1354. }
  1355. if (auth_tok->session_key.encrypted_key_size == 0)
  1356. auth_tok->session_key.encrypted_key_size =
  1357. auth_tok->token.private_key.key_size;
  1358. rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
  1359. if (rc) {
  1360. printk(KERN_ERR "Failed to encrypt session key via a key "
  1361. "module; rc = [%d]\n", rc);
  1362. goto out;
  1363. }
  1364. if (ecryptfs_verbosity > 0) {
  1365. ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
  1366. ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  1367. }
  1368. encrypted_session_key_set:
  1369. /* This format is inspired by OpenPGP; see RFC 2440
  1370. * packet tag 1 */
  1371. max_packet_size = (1 /* Tag 1 identifier */
  1372. + 3 /* Max Tag 1 packet size */
  1373. + 1 /* Version */
  1374. + ECRYPTFS_SIG_SIZE /* Key identifier */
  1375. + 1 /* Cipher identifier */
  1376. + key_rec->enc_key_size); /* Encrypted key size */
  1377. if (max_packet_size > (*remaining_bytes)) {
  1378. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1379. "need up to [%td] bytes, but there are only [%td] "
  1380. "available\n", max_packet_size, (*remaining_bytes));
  1381. rc = -EINVAL;
  1382. goto out;
  1383. }
  1384. dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
  1385. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  1386. (max_packet_size - 4),
  1387. &packet_size_length);
  1388. if (rc) {
  1389. ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  1390. "header; cannot generate packet length\n");
  1391. goto out;
  1392. }
  1393. (*packet_size) += packet_size_length;
  1394. dest[(*packet_size)++] = 0x03; /* version 3 */
  1395. memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  1396. (*packet_size) += ECRYPTFS_SIG_SIZE;
  1397. dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  1398. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1399. key_rec->enc_key_size);
  1400. (*packet_size) += key_rec->enc_key_size;
  1401. out:
  1402. if (rc)
  1403. (*packet_size) = 0;
  1404. else
  1405. (*remaining_bytes) -= (*packet_size);
  1406. return rc;
  1407. }
  1408. /**
  1409. * write_tag_11_packet
  1410. * @dest: Target into which Tag 11 packet is to be written
  1411. * @remaining_bytes: Maximum packet length
  1412. * @contents: Byte array of contents to copy in
  1413. * @contents_length: Number of bytes in contents
  1414. * @packet_length: Length of the Tag 11 packet written; zero on error
  1415. *
  1416. * Returns zero on success; non-zero on error.
  1417. */
  1418. static int
  1419. write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
  1420. size_t contents_length, size_t *packet_length)
  1421. {
  1422. size_t packet_size_length;
  1423. size_t max_packet_size;
  1424. int rc = 0;
  1425. (*packet_length) = 0;
  1426. /* This format is inspired by OpenPGP; see RFC 2440
  1427. * packet tag 11 */
  1428. max_packet_size = (1 /* Tag 11 identifier */
  1429. + 3 /* Max Tag 11 packet size */
  1430. + 1 /* Binary format specifier */
  1431. + 1 /* Filename length */
  1432. + 8 /* Filename ("_CONSOLE") */
  1433. + 4 /* Modification date */
  1434. + contents_length); /* Literal data */
  1435. if (max_packet_size > (*remaining_bytes)) {
  1436. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1437. "need up to [%td] bytes, but there are only [%td] "
  1438. "available\n", max_packet_size, (*remaining_bytes));
  1439. rc = -EINVAL;
  1440. goto out;
  1441. }
  1442. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  1443. rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
  1444. (max_packet_size - 4),
  1445. &packet_size_length);
  1446. if (rc) {
  1447. printk(KERN_ERR "Error generating tag 11 packet header; cannot "
  1448. "generate packet length. rc = [%d]\n", rc);
  1449. goto out;
  1450. }
  1451. (*packet_length) += packet_size_length;
  1452. dest[(*packet_length)++] = 0x62; /* binary data format specifier */
  1453. dest[(*packet_length)++] = 8;
  1454. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  1455. (*packet_length) += 8;
  1456. memset(&dest[(*packet_length)], 0x00, 4);
  1457. (*packet_length) += 4;
  1458. memcpy(&dest[(*packet_length)], contents, contents_length);
  1459. (*packet_length) += contents_length;
  1460. out:
  1461. if (rc)
  1462. (*packet_length) = 0;
  1463. else
  1464. (*remaining_bytes) -= (*packet_length);
  1465. return rc;
  1466. }
  1467. /**
  1468. * write_tag_3_packet
  1469. * @dest: Buffer into which to write the packet
  1470. * @remaining_bytes: Maximum number of bytes that can be written
  1471. * @auth_tok: Authentication token
  1472. * @crypt_stat: The cryptographic context
  1473. * @key_rec: encrypted key
  1474. * @packet_size: This function will write the number of bytes that end
  1475. * up constituting the packet; set to zero on error
  1476. *
  1477. * Returns zero on success; non-zero on error.
  1478. */
  1479. static int
  1480. write_tag_3_packet(char *dest, size_t *remaining_bytes,
  1481. struct ecryptfs_auth_tok *auth_tok,
  1482. struct ecryptfs_crypt_stat *crypt_stat,
  1483. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1484. {
  1485. size_t i;
  1486. size_t encrypted_session_key_valid = 0;
  1487. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  1488. struct scatterlist dst_sg[2];
  1489. struct scatterlist src_sg[2];
  1490. struct mutex *tfm_mutex = NULL;
  1491. u8 cipher_code;
  1492. size_t packet_size_length;
  1493. size_t max_packet_size;
  1494. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1495. crypt_stat->mount_crypt_stat;
  1496. struct blkcipher_desc desc = {
  1497. .tfm = NULL,
  1498. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1499. };
  1500. int rc = 0;
  1501. (*packet_size) = 0;
  1502. ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
  1503. ECRYPTFS_SIG_SIZE);
  1504. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1505. crypt_stat->cipher);
  1506. if (unlikely(rc)) {
  1507. printk(KERN_ERR "Internal error whilst attempting to get "
  1508. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1509. crypt_stat->cipher, rc);
  1510. goto out;
  1511. }
  1512. if (mount_crypt_stat->global_default_cipher_key_size == 0) {
  1513. struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
  1514. printk(KERN_WARNING "No key size specified at mount; "
  1515. "defaulting to [%d]\n", alg->max_keysize);
  1516. mount_crypt_stat->global_default_cipher_key_size =
  1517. alg->max_keysize;
  1518. }
  1519. if (crypt_stat->key_size == 0)
  1520. crypt_stat->key_size =
  1521. mount_crypt_stat->global_default_cipher_key_size;
  1522. if (auth_tok->session_key.encrypted_key_size == 0)
  1523. auth_tok->session_key.encrypted_key_size =
  1524. crypt_stat->key_size;
  1525. if (crypt_stat->key_size == 24
  1526. && strcmp("aes", crypt_stat->cipher) == 0) {
  1527. memset((crypt_stat->key + 24), 0, 8);
  1528. auth_tok->session_key.encrypted_key_size = 32;
  1529. } else
  1530. auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
  1531. key_rec->enc_key_size =
  1532. auth_tok->session_key.encrypted_key_size;
  1533. encrypted_session_key_valid = 0;
  1534. for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  1535. encrypted_session_key_valid |=
  1536. auth_tok->session_key.encrypted_key[i];
  1537. if (encrypted_session_key_valid) {
  1538. ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  1539. "using auth_tok->session_key.encrypted_key, "
  1540. "where key_rec->enc_key_size = [%d]\n",
  1541. key_rec->enc_key_size);
  1542. memcpy(key_rec->enc_key,
  1543. auth_tok->session_key.encrypted_key,
  1544. key_rec->enc_key_size);
  1545. goto encrypted_session_key_set;
  1546. }
  1547. if (auth_tok->token.password.flags &
  1548. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
  1549. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  1550. "session key encryption key of size [%d]\n",
  1551. auth_tok->token.password.
  1552. session_key_encryption_key_bytes);
  1553. memcpy(session_key_encryption_key,
  1554. auth_tok->token.password.session_key_encryption_key,
  1555. crypt_stat->key_size);
  1556. ecryptfs_printk(KERN_DEBUG,
  1557. "Cached session key " "encryption key: \n");
  1558. if (ecryptfs_verbosity > 0)
  1559. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1560. }
  1561. if (unlikely(ecryptfs_verbosity > 0)) {
  1562. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  1563. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1564. }
  1565. rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
  1566. src_sg, 2);
  1567. if (rc < 1 || rc > 2) {
  1568. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1569. "for crypt_stat session key; expected rc = 1; "
  1570. "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
  1571. rc, key_rec->enc_key_size);
  1572. rc = -ENOMEM;
  1573. goto out;
  1574. }
  1575. rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
  1576. dst_sg, 2);
  1577. if (rc < 1 || rc > 2) {
  1578. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1579. "for crypt_stat encrypted session key; "
  1580. "expected rc = 1; got rc = [%d]. "
  1581. "key_rec->enc_key_size = [%d]\n", rc,
  1582. key_rec->enc_key_size);
  1583. rc = -ENOMEM;
  1584. goto out;
  1585. }
  1586. mutex_lock(tfm_mutex);
  1587. rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
  1588. crypt_stat->key_size);
  1589. if (rc < 0) {
  1590. mutex_unlock(tfm_mutex);
  1591. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  1592. "context; rc = [%d]\n", rc);
  1593. goto out;
  1594. }
  1595. rc = 0;
  1596. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  1597. crypt_stat->key_size);
  1598. rc = crypto_blkcipher_encrypt(&desc, dst_sg, src_sg,
  1599. (*key_rec).enc_key_size);
  1600. mutex_unlock(tfm_mutex);
  1601. if (rc) {
  1602. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  1603. goto out;
  1604. }
  1605. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  1606. if (ecryptfs_verbosity > 0) {
  1607. ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
  1608. key_rec->enc_key_size);
  1609. ecryptfs_dump_hex(key_rec->enc_key,
  1610. key_rec->enc_key_size);
  1611. }
  1612. encrypted_session_key_set:
  1613. /* This format is inspired by OpenPGP; see RFC 2440
  1614. * packet tag 3 */
  1615. max_packet_size = (1 /* Tag 3 identifier */
  1616. + 3 /* Max Tag 3 packet size */
  1617. + 1 /* Version */
  1618. + 1 /* Cipher code */
  1619. + 1 /* S2K specifier */
  1620. + 1 /* Hash identifier */
  1621. + ECRYPTFS_SALT_SIZE /* Salt */
  1622. + 1 /* Hash iterations */
  1623. + key_rec->enc_key_size); /* Encrypted key size */
  1624. if (max_packet_size > (*remaining_bytes)) {
  1625. printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
  1626. "there are only [%td] available\n", max_packet_size,
  1627. (*remaining_bytes));
  1628. rc = -EINVAL;
  1629. goto out;
  1630. }
  1631. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  1632. /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  1633. * to get the number of octets in the actual Tag 3 packet */
  1634. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  1635. (max_packet_size - 4),
  1636. &packet_size_length);
  1637. if (rc) {
  1638. printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  1639. "generate packet length. rc = [%d]\n", rc);
  1640. goto out;
  1641. }
  1642. (*packet_size) += packet_size_length;
  1643. dest[(*packet_size)++] = 0x04; /* version 4 */
  1644. /* TODO: Break from RFC2440 so that arbitrary ciphers can be
  1645. * specified with strings */
  1646. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  1647. if (cipher_code == 0) {
  1648. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  1649. "cipher [%s]\n", crypt_stat->cipher);
  1650. rc = -EINVAL;
  1651. goto out;
  1652. }
  1653. dest[(*packet_size)++] = cipher_code;
  1654. dest[(*packet_size)++] = 0x03; /* S2K */
  1655. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  1656. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  1657. ECRYPTFS_SALT_SIZE);
  1658. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  1659. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  1660. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1661. key_rec->enc_key_size);
  1662. (*packet_size) += key_rec->enc_key_size;
  1663. out:
  1664. if (rc)
  1665. (*packet_size) = 0;
  1666. else
  1667. (*remaining_bytes) -= (*packet_size);
  1668. return rc;
  1669. }
  1670. struct kmem_cache *ecryptfs_key_record_cache;
  1671. /**
  1672. * ecryptfs_generate_key_packet_set
  1673. * @dest_base: Virtual address from which to write the key record set
  1674. * @crypt_stat: The cryptographic context from which the
  1675. * authentication tokens will be retrieved
  1676. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  1677. * for the global parameters
  1678. * @len: The amount written
  1679. * @max: The maximum amount of data allowed to be written
  1680. *
  1681. * Generates a key packet set and writes it to the virtual address
  1682. * passed in.
  1683. *
  1684. * Returns zero on success; non-zero on error.
  1685. */
  1686. int
  1687. ecryptfs_generate_key_packet_set(char *dest_base,
  1688. struct ecryptfs_crypt_stat *crypt_stat,
  1689. struct dentry *ecryptfs_dentry, size_t *len,
  1690. size_t max)
  1691. {
  1692. struct ecryptfs_auth_tok *auth_tok;
  1693. struct ecryptfs_global_auth_tok *global_auth_tok;
  1694. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1695. &ecryptfs_superblock_to_private(
  1696. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1697. size_t written;
  1698. struct ecryptfs_key_record *key_rec;
  1699. struct ecryptfs_key_sig *key_sig;
  1700. int rc = 0;
  1701. (*len) = 0;
  1702. mutex_lock(&crypt_stat->keysig_list_mutex);
  1703. key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  1704. if (!key_rec) {
  1705. rc = -ENOMEM;
  1706. goto out;
  1707. }
  1708. list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  1709. crypt_stat_list) {
  1710. memset(key_rec, 0, sizeof(*key_rec));
  1711. rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
  1712. mount_crypt_stat,
  1713. key_sig->keysig);
  1714. if (rc) {
  1715. printk(KERN_ERR "Error attempting to get the global "
  1716. "auth_tok; rc = [%d]\n", rc);
  1717. goto out_free;
  1718. }
  1719. if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  1720. printk(KERN_WARNING
  1721. "Skipping invalid auth tok with sig = [%s]\n",
  1722. global_auth_tok->sig);
  1723. continue;
  1724. }
  1725. auth_tok = global_auth_tok->global_auth_tok;
  1726. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1727. rc = write_tag_3_packet((dest_base + (*len)),
  1728. &max, auth_tok,
  1729. crypt_stat, key_rec,
  1730. &written);
  1731. if (rc) {
  1732. ecryptfs_printk(KERN_WARNING, "Error "
  1733. "writing tag 3 packet\n");
  1734. goto out_free;
  1735. }
  1736. (*len) += written;
  1737. /* Write auth tok signature packet */
  1738. rc = write_tag_11_packet((dest_base + (*len)), &max,
  1739. key_rec->sig,
  1740. ECRYPTFS_SIG_SIZE, &written);
  1741. if (rc) {
  1742. ecryptfs_printk(KERN_ERR, "Error writing "
  1743. "auth tok signature packet\n");
  1744. goto out_free;
  1745. }
  1746. (*len) += written;
  1747. } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1748. rc = write_tag_1_packet(dest_base + (*len),
  1749. &max, auth_tok,
  1750. crypt_stat, key_rec, &written);
  1751. if (rc) {
  1752. ecryptfs_printk(KERN_WARNING, "Error "
  1753. "writing tag 1 packet\n");
  1754. goto out_free;
  1755. }
  1756. (*len) += written;
  1757. } else {
  1758. ecryptfs_printk(KERN_WARNING, "Unsupported "
  1759. "authentication token type\n");
  1760. rc = -EINVAL;
  1761. goto out_free;
  1762. }
  1763. }
  1764. if (likely(max > 0)) {
  1765. dest_base[(*len)] = 0x00;
  1766. } else {
  1767. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1768. rc = -EIO;
  1769. }
  1770. out_free:
  1771. kmem_cache_free(ecryptfs_key_record_cache, key_rec);
  1772. out:
  1773. if (rc)
  1774. (*len) = 0;
  1775. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1776. return rc;
  1777. }
  1778. struct kmem_cache *ecryptfs_key_sig_cache;
  1779. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  1780. {
  1781. struct ecryptfs_key_sig *new_key_sig;
  1782. int rc = 0;
  1783. new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  1784. if (!new_key_sig) {
  1785. rc = -ENOMEM;
  1786. printk(KERN_ERR
  1787. "Error allocating from ecryptfs_key_sig_cache\n");
  1788. goto out;
  1789. }
  1790. memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1791. mutex_lock(&crypt_stat->keysig_list_mutex);
  1792. list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
  1793. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1794. out:
  1795. return rc;
  1796. }
  1797. struct kmem_cache *ecryptfs_global_auth_tok_cache;
  1798. int
  1799. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  1800. char *sig)
  1801. {
  1802. struct ecryptfs_global_auth_tok *new_auth_tok;
  1803. int rc = 0;
  1804. new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
  1805. GFP_KERNEL);
  1806. if (!new_auth_tok) {
  1807. rc = -ENOMEM;
  1808. printk(KERN_ERR "Error allocating from "
  1809. "ecryptfs_global_auth_tok_cache\n");
  1810. goto out;
  1811. }
  1812. memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1813. new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  1814. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1815. list_add(&new_auth_tok->mount_crypt_stat_list,
  1816. &mount_crypt_stat->global_auth_tok_list);
  1817. mount_crypt_stat->num_global_auth_toks++;
  1818. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1819. out:
  1820. return rc;
  1821. }