keystore.c 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889
  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 received netlink message is "
  227. "shorter than expected\n");
  228. rc = -EIO;
  229. goto out;
  230. }
  231. if (m_size < 3) {
  232. ecryptfs_printk(KERN_ERR,
  233. "The decrypted key is not long enough to "
  234. "include a cipher code and checksum\n");
  235. rc = -EIO;
  236. goto out;
  237. }
  238. *cipher_code = data[i++];
  239. /* The decrypted key includes 1 byte cipher code and 2 byte checksum */
  240. session_key->decrypted_key_size = m_size - 3;
  241. if (session_key->decrypted_key_size > ECRYPTFS_MAX_KEY_BYTES) {
  242. ecryptfs_printk(KERN_ERR, "key_size [%d] larger than "
  243. "the maximum key size [%d]\n",
  244. session_key->decrypted_key_size,
  245. ECRYPTFS_MAX_ENCRYPTED_KEY_BYTES);
  246. rc = -EIO;
  247. goto out;
  248. }
  249. memcpy(session_key->decrypted_key, &data[i],
  250. session_key->decrypted_key_size);
  251. i += session_key->decrypted_key_size;
  252. expected_checksum += (unsigned char)(data[i++]) << 8;
  253. expected_checksum += (unsigned char)(data[i++]);
  254. for (i = 0; i < session_key->decrypted_key_size; i++)
  255. checksum += session_key->decrypted_key[i];
  256. if (expected_checksum != checksum) {
  257. ecryptfs_printk(KERN_ERR, "Invalid checksum for file "
  258. "encryption key; expected [%x]; calculated "
  259. "[%x]\n", expected_checksum, checksum);
  260. rc = -EIO;
  261. }
  262. out:
  263. return rc;
  264. }
  265. static int
  266. write_tag_66_packet(char *signature, 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 *netlink_message;
  422. size_t netlink_message_length;
  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. &netlink_message, &netlink_message_length);
  432. if (rc) {
  433. ecryptfs_printk(KERN_ERR, "Failed to write tag 64 packet\n");
  434. goto out;
  435. }
  436. rc = ecryptfs_send_message(ecryptfs_transport, netlink_message,
  437. netlink_message_length, &msg_ctx);
  438. if (rc) {
  439. ecryptfs_printk(KERN_ERR, "Error sending netlink message\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 *netlink_payload;
  1290. size_t netlink_payload_length;
  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, &netlink_payload,
  1296. &netlink_payload_length);
  1297. if (rc) {
  1298. ecryptfs_printk(KERN_ERR, "Error generating tag 66 packet\n");
  1299. goto out;
  1300. }
  1301. rc = ecryptfs_send_message(ecryptfs_transport, netlink_payload,
  1302. netlink_payload_length, &msg_ctx);
  1303. if (rc) {
  1304. ecryptfs_printk(KERN_ERR, "Error sending netlink message\n");
  1305. goto out;
  1306. }
  1307. rc = ecryptfs_wait_for_response(msg_ctx, &msg);
  1308. if (rc) {
  1309. ecryptfs_printk(KERN_ERR, "Failed to receive tag 67 packet "
  1310. "from the user space daemon\n");
  1311. rc = -EIO;
  1312. goto out;
  1313. }
  1314. rc = parse_tag_67_packet(key_rec, msg);
  1315. if (rc)
  1316. ecryptfs_printk(KERN_ERR, "Error parsing tag 67 packet\n");
  1317. kfree(msg);
  1318. out:
  1319. if (netlink_payload)
  1320. kfree(netlink_payload);
  1321. return rc;
  1322. }
  1323. /**
  1324. * write_tag_1_packet - Write an RFC2440-compatible tag 1 (public key) packet
  1325. * @dest: Buffer into which to write the packet
  1326. * @remaining_bytes: Maximum number of bytes that can be writtn
  1327. * @auth_tok: The authentication token used for generating the tag 1 packet
  1328. * @crypt_stat: The cryptographic context
  1329. * @key_rec: The key record struct for the tag 1 packet
  1330. * @packet_size: This function will write the number of bytes that end
  1331. * up constituting the packet; set to zero on error
  1332. *
  1333. * Returns zero on success; non-zero on error.
  1334. */
  1335. static int
  1336. write_tag_1_packet(char *dest, size_t *remaining_bytes,
  1337. struct ecryptfs_auth_tok *auth_tok,
  1338. struct ecryptfs_crypt_stat *crypt_stat,
  1339. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1340. {
  1341. size_t i;
  1342. size_t encrypted_session_key_valid = 0;
  1343. size_t packet_size_length;
  1344. size_t max_packet_size;
  1345. int rc = 0;
  1346. (*packet_size) = 0;
  1347. ecryptfs_from_hex(key_rec->sig, auth_tok->token.private_key.signature,
  1348. ECRYPTFS_SIG_SIZE);
  1349. encrypted_session_key_valid = 0;
  1350. for (i = 0; i < crypt_stat->key_size; i++)
  1351. encrypted_session_key_valid |=
  1352. auth_tok->session_key.encrypted_key[i];
  1353. if (encrypted_session_key_valid) {
  1354. memcpy(key_rec->enc_key,
  1355. auth_tok->session_key.encrypted_key,
  1356. auth_tok->session_key.encrypted_key_size);
  1357. goto encrypted_session_key_set;
  1358. }
  1359. if (auth_tok->session_key.encrypted_key_size == 0)
  1360. auth_tok->session_key.encrypted_key_size =
  1361. auth_tok->token.private_key.key_size;
  1362. rc = pki_encrypt_session_key(auth_tok, crypt_stat, key_rec);
  1363. if (rc) {
  1364. printk(KERN_ERR "Failed to encrypt session key via a key "
  1365. "module; rc = [%d]\n", rc);
  1366. goto out;
  1367. }
  1368. if (ecryptfs_verbosity > 0) {
  1369. ecryptfs_printk(KERN_DEBUG, "Encrypted key:\n");
  1370. ecryptfs_dump_hex(key_rec->enc_key, key_rec->enc_key_size);
  1371. }
  1372. encrypted_session_key_set:
  1373. /* This format is inspired by OpenPGP; see RFC 2440
  1374. * packet tag 1 */
  1375. max_packet_size = (1 /* Tag 1 identifier */
  1376. + 3 /* Max Tag 1 packet size */
  1377. + 1 /* Version */
  1378. + ECRYPTFS_SIG_SIZE /* Key identifier */
  1379. + 1 /* Cipher identifier */
  1380. + key_rec->enc_key_size); /* Encrypted key size */
  1381. if (max_packet_size > (*remaining_bytes)) {
  1382. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1383. "need up to [%td] bytes, but there are only [%td] "
  1384. "available\n", max_packet_size, (*remaining_bytes));
  1385. rc = -EINVAL;
  1386. goto out;
  1387. }
  1388. dest[(*packet_size)++] = ECRYPTFS_TAG_1_PACKET_TYPE;
  1389. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  1390. (max_packet_size - 4),
  1391. &packet_size_length);
  1392. if (rc) {
  1393. ecryptfs_printk(KERN_ERR, "Error generating tag 1 packet "
  1394. "header; cannot generate packet length\n");
  1395. goto out;
  1396. }
  1397. (*packet_size) += packet_size_length;
  1398. dest[(*packet_size)++] = 0x03; /* version 3 */
  1399. memcpy(&dest[(*packet_size)], key_rec->sig, ECRYPTFS_SIG_SIZE);
  1400. (*packet_size) += ECRYPTFS_SIG_SIZE;
  1401. dest[(*packet_size)++] = RFC2440_CIPHER_RSA;
  1402. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1403. key_rec->enc_key_size);
  1404. (*packet_size) += key_rec->enc_key_size;
  1405. out:
  1406. if (rc)
  1407. (*packet_size) = 0;
  1408. else
  1409. (*remaining_bytes) -= (*packet_size);
  1410. return rc;
  1411. }
  1412. /**
  1413. * write_tag_11_packet
  1414. * @dest: Target into which Tag 11 packet is to be written
  1415. * @remaining_bytes: Maximum packet length
  1416. * @contents: Byte array of contents to copy in
  1417. * @contents_length: Number of bytes in contents
  1418. * @packet_length: Length of the Tag 11 packet written; zero on error
  1419. *
  1420. * Returns zero on success; non-zero on error.
  1421. */
  1422. static int
  1423. write_tag_11_packet(char *dest, size_t *remaining_bytes, char *contents,
  1424. size_t contents_length, size_t *packet_length)
  1425. {
  1426. size_t packet_size_length;
  1427. size_t max_packet_size;
  1428. int rc = 0;
  1429. (*packet_length) = 0;
  1430. /* This format is inspired by OpenPGP; see RFC 2440
  1431. * packet tag 11 */
  1432. max_packet_size = (1 /* Tag 11 identifier */
  1433. + 3 /* Max Tag 11 packet size */
  1434. + 1 /* Binary format specifier */
  1435. + 1 /* Filename length */
  1436. + 8 /* Filename ("_CONSOLE") */
  1437. + 4 /* Modification date */
  1438. + contents_length); /* Literal data */
  1439. if (max_packet_size > (*remaining_bytes)) {
  1440. printk(KERN_ERR "Packet length larger than maximum allowable; "
  1441. "need up to [%td] bytes, but there are only [%td] "
  1442. "available\n", max_packet_size, (*remaining_bytes));
  1443. rc = -EINVAL;
  1444. goto out;
  1445. }
  1446. dest[(*packet_length)++] = ECRYPTFS_TAG_11_PACKET_TYPE;
  1447. rc = ecryptfs_write_packet_length(&dest[(*packet_length)],
  1448. (max_packet_size - 4),
  1449. &packet_size_length);
  1450. if (rc) {
  1451. printk(KERN_ERR "Error generating tag 11 packet header; cannot "
  1452. "generate packet length. rc = [%d]\n", rc);
  1453. goto out;
  1454. }
  1455. (*packet_length) += packet_size_length;
  1456. dest[(*packet_length)++] = 0x62; /* binary data format specifier */
  1457. dest[(*packet_length)++] = 8;
  1458. memcpy(&dest[(*packet_length)], "_CONSOLE", 8);
  1459. (*packet_length) += 8;
  1460. memset(&dest[(*packet_length)], 0x00, 4);
  1461. (*packet_length) += 4;
  1462. memcpy(&dest[(*packet_length)], contents, contents_length);
  1463. (*packet_length) += contents_length;
  1464. out:
  1465. if (rc)
  1466. (*packet_length) = 0;
  1467. else
  1468. (*remaining_bytes) -= (*packet_length);
  1469. return rc;
  1470. }
  1471. /**
  1472. * write_tag_3_packet
  1473. * @dest: Buffer into which to write the packet
  1474. * @remaining_bytes: Maximum number of bytes that can be written
  1475. * @auth_tok: Authentication token
  1476. * @crypt_stat: The cryptographic context
  1477. * @key_rec: encrypted key
  1478. * @packet_size: This function will write the number of bytes that end
  1479. * up constituting the packet; set to zero on error
  1480. *
  1481. * Returns zero on success; non-zero on error.
  1482. */
  1483. static int
  1484. write_tag_3_packet(char *dest, size_t *remaining_bytes,
  1485. struct ecryptfs_auth_tok *auth_tok,
  1486. struct ecryptfs_crypt_stat *crypt_stat,
  1487. struct ecryptfs_key_record *key_rec, size_t *packet_size)
  1488. {
  1489. size_t i;
  1490. size_t encrypted_session_key_valid = 0;
  1491. char session_key_encryption_key[ECRYPTFS_MAX_KEY_BYTES];
  1492. struct scatterlist dst_sg;
  1493. struct scatterlist src_sg;
  1494. struct mutex *tfm_mutex = NULL;
  1495. u8 cipher_code;
  1496. size_t packet_size_length;
  1497. size_t max_packet_size;
  1498. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1499. crypt_stat->mount_crypt_stat;
  1500. struct blkcipher_desc desc = {
  1501. .tfm = NULL,
  1502. .flags = CRYPTO_TFM_REQ_MAY_SLEEP
  1503. };
  1504. int rc = 0;
  1505. (*packet_size) = 0;
  1506. ecryptfs_from_hex(key_rec->sig, auth_tok->token.password.signature,
  1507. ECRYPTFS_SIG_SIZE);
  1508. rc = ecryptfs_get_tfm_and_mutex_for_cipher_name(&desc.tfm, &tfm_mutex,
  1509. crypt_stat->cipher);
  1510. if (unlikely(rc)) {
  1511. printk(KERN_ERR "Internal error whilst attempting to get "
  1512. "tfm and mutex for cipher name [%s]; rc = [%d]\n",
  1513. crypt_stat->cipher, rc);
  1514. goto out;
  1515. }
  1516. if (mount_crypt_stat->global_default_cipher_key_size == 0) {
  1517. struct blkcipher_alg *alg = crypto_blkcipher_alg(desc.tfm);
  1518. printk(KERN_WARNING "No key size specified at mount; "
  1519. "defaulting to [%d]\n", alg->max_keysize);
  1520. mount_crypt_stat->global_default_cipher_key_size =
  1521. alg->max_keysize;
  1522. }
  1523. if (crypt_stat->key_size == 0)
  1524. crypt_stat->key_size =
  1525. mount_crypt_stat->global_default_cipher_key_size;
  1526. if (auth_tok->session_key.encrypted_key_size == 0)
  1527. auth_tok->session_key.encrypted_key_size =
  1528. crypt_stat->key_size;
  1529. if (crypt_stat->key_size == 24
  1530. && strcmp("aes", crypt_stat->cipher) == 0) {
  1531. memset((crypt_stat->key + 24), 0, 8);
  1532. auth_tok->session_key.encrypted_key_size = 32;
  1533. } else
  1534. auth_tok->session_key.encrypted_key_size = crypt_stat->key_size;
  1535. key_rec->enc_key_size =
  1536. auth_tok->session_key.encrypted_key_size;
  1537. encrypted_session_key_valid = 0;
  1538. for (i = 0; i < auth_tok->session_key.encrypted_key_size; i++)
  1539. encrypted_session_key_valid |=
  1540. auth_tok->session_key.encrypted_key[i];
  1541. if (encrypted_session_key_valid) {
  1542. ecryptfs_printk(KERN_DEBUG, "encrypted_session_key_valid != 0; "
  1543. "using auth_tok->session_key.encrypted_key, "
  1544. "where key_rec->enc_key_size = [%d]\n",
  1545. key_rec->enc_key_size);
  1546. memcpy(key_rec->enc_key,
  1547. auth_tok->session_key.encrypted_key,
  1548. key_rec->enc_key_size);
  1549. goto encrypted_session_key_set;
  1550. }
  1551. if (auth_tok->token.password.flags &
  1552. ECRYPTFS_SESSION_KEY_ENCRYPTION_KEY_SET) {
  1553. ecryptfs_printk(KERN_DEBUG, "Using previously generated "
  1554. "session key encryption key of size [%d]\n",
  1555. auth_tok->token.password.
  1556. session_key_encryption_key_bytes);
  1557. memcpy(session_key_encryption_key,
  1558. auth_tok->token.password.session_key_encryption_key,
  1559. crypt_stat->key_size);
  1560. ecryptfs_printk(KERN_DEBUG,
  1561. "Cached session key " "encryption key: \n");
  1562. if (ecryptfs_verbosity > 0)
  1563. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1564. }
  1565. if (unlikely(ecryptfs_verbosity > 0)) {
  1566. ecryptfs_printk(KERN_DEBUG, "Session key encryption key:\n");
  1567. ecryptfs_dump_hex(session_key_encryption_key, 16);
  1568. }
  1569. rc = virt_to_scatterlist(crypt_stat->key, key_rec->enc_key_size,
  1570. &src_sg, 1);
  1571. if (rc != 1) {
  1572. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1573. "for crypt_stat session key; expected rc = 1; "
  1574. "got rc = [%d]. key_rec->enc_key_size = [%d]\n",
  1575. rc, key_rec->enc_key_size);
  1576. rc = -ENOMEM;
  1577. goto out;
  1578. }
  1579. rc = virt_to_scatterlist(key_rec->enc_key, key_rec->enc_key_size,
  1580. &dst_sg, 1);
  1581. if (rc != 1) {
  1582. ecryptfs_printk(KERN_ERR, "Error generating scatterlist "
  1583. "for crypt_stat encrypted session key; "
  1584. "expected rc = 1; got rc = [%d]. "
  1585. "key_rec->enc_key_size = [%d]\n", rc,
  1586. key_rec->enc_key_size);
  1587. rc = -ENOMEM;
  1588. goto out;
  1589. }
  1590. mutex_lock(tfm_mutex);
  1591. rc = crypto_blkcipher_setkey(desc.tfm, session_key_encryption_key,
  1592. crypt_stat->key_size);
  1593. if (rc < 0) {
  1594. mutex_unlock(tfm_mutex);
  1595. ecryptfs_printk(KERN_ERR, "Error setting key for crypto "
  1596. "context; rc = [%d]\n", rc);
  1597. goto out;
  1598. }
  1599. rc = 0;
  1600. ecryptfs_printk(KERN_DEBUG, "Encrypting [%d] bytes of the key\n",
  1601. crypt_stat->key_size);
  1602. rc = crypto_blkcipher_encrypt(&desc, &dst_sg, &src_sg,
  1603. (*key_rec).enc_key_size);
  1604. mutex_unlock(tfm_mutex);
  1605. if (rc) {
  1606. printk(KERN_ERR "Error encrypting; rc = [%d]\n", rc);
  1607. goto out;
  1608. }
  1609. ecryptfs_printk(KERN_DEBUG, "This should be the encrypted key:\n");
  1610. if (ecryptfs_verbosity > 0) {
  1611. ecryptfs_printk(KERN_DEBUG, "EFEK of size [%d]:\n",
  1612. key_rec->enc_key_size);
  1613. ecryptfs_dump_hex(key_rec->enc_key,
  1614. key_rec->enc_key_size);
  1615. }
  1616. encrypted_session_key_set:
  1617. /* This format is inspired by OpenPGP; see RFC 2440
  1618. * packet tag 3 */
  1619. max_packet_size = (1 /* Tag 3 identifier */
  1620. + 3 /* Max Tag 3 packet size */
  1621. + 1 /* Version */
  1622. + 1 /* Cipher code */
  1623. + 1 /* S2K specifier */
  1624. + 1 /* Hash identifier */
  1625. + ECRYPTFS_SALT_SIZE /* Salt */
  1626. + 1 /* Hash iterations */
  1627. + key_rec->enc_key_size); /* Encrypted key size */
  1628. if (max_packet_size > (*remaining_bytes)) {
  1629. printk(KERN_ERR "Packet too large; need up to [%td] bytes, but "
  1630. "there are only [%td] available\n", max_packet_size,
  1631. (*remaining_bytes));
  1632. rc = -EINVAL;
  1633. goto out;
  1634. }
  1635. dest[(*packet_size)++] = ECRYPTFS_TAG_3_PACKET_TYPE;
  1636. /* Chop off the Tag 3 identifier(1) and Tag 3 packet size(3)
  1637. * to get the number of octets in the actual Tag 3 packet */
  1638. rc = ecryptfs_write_packet_length(&dest[(*packet_size)],
  1639. (max_packet_size - 4),
  1640. &packet_size_length);
  1641. if (rc) {
  1642. printk(KERN_ERR "Error generating tag 3 packet header; cannot "
  1643. "generate packet length. rc = [%d]\n", rc);
  1644. goto out;
  1645. }
  1646. (*packet_size) += packet_size_length;
  1647. dest[(*packet_size)++] = 0x04; /* version 4 */
  1648. /* TODO: Break from RFC2440 so that arbitrary ciphers can be
  1649. * specified with strings */
  1650. cipher_code = ecryptfs_code_for_cipher_string(crypt_stat);
  1651. if (cipher_code == 0) {
  1652. ecryptfs_printk(KERN_WARNING, "Unable to generate code for "
  1653. "cipher [%s]\n", crypt_stat->cipher);
  1654. rc = -EINVAL;
  1655. goto out;
  1656. }
  1657. dest[(*packet_size)++] = cipher_code;
  1658. dest[(*packet_size)++] = 0x03; /* S2K */
  1659. dest[(*packet_size)++] = 0x01; /* MD5 (TODO: parameterize) */
  1660. memcpy(&dest[(*packet_size)], auth_tok->token.password.salt,
  1661. ECRYPTFS_SALT_SIZE);
  1662. (*packet_size) += ECRYPTFS_SALT_SIZE; /* salt */
  1663. dest[(*packet_size)++] = 0x60; /* hash iterations (65536) */
  1664. memcpy(&dest[(*packet_size)], key_rec->enc_key,
  1665. key_rec->enc_key_size);
  1666. (*packet_size) += key_rec->enc_key_size;
  1667. out:
  1668. if (rc)
  1669. (*packet_size) = 0;
  1670. else
  1671. (*remaining_bytes) -= (*packet_size);
  1672. return rc;
  1673. }
  1674. struct kmem_cache *ecryptfs_key_record_cache;
  1675. /**
  1676. * ecryptfs_generate_key_packet_set
  1677. * @dest_base: Virtual address from which to write the key record set
  1678. * @crypt_stat: The cryptographic context from which the
  1679. * authentication tokens will be retrieved
  1680. * @ecryptfs_dentry: The dentry, used to retrieve the mount crypt stat
  1681. * for the global parameters
  1682. * @len: The amount written
  1683. * @max: The maximum amount of data allowed to be written
  1684. *
  1685. * Generates a key packet set and writes it to the virtual address
  1686. * passed in.
  1687. *
  1688. * Returns zero on success; non-zero on error.
  1689. */
  1690. int
  1691. ecryptfs_generate_key_packet_set(char *dest_base,
  1692. struct ecryptfs_crypt_stat *crypt_stat,
  1693. struct dentry *ecryptfs_dentry, size_t *len,
  1694. size_t max)
  1695. {
  1696. struct ecryptfs_auth_tok *auth_tok;
  1697. struct ecryptfs_global_auth_tok *global_auth_tok;
  1698. struct ecryptfs_mount_crypt_stat *mount_crypt_stat =
  1699. &ecryptfs_superblock_to_private(
  1700. ecryptfs_dentry->d_sb)->mount_crypt_stat;
  1701. size_t written;
  1702. struct ecryptfs_key_record *key_rec;
  1703. struct ecryptfs_key_sig *key_sig;
  1704. int rc = 0;
  1705. (*len) = 0;
  1706. mutex_lock(&crypt_stat->keysig_list_mutex);
  1707. key_rec = kmem_cache_alloc(ecryptfs_key_record_cache, GFP_KERNEL);
  1708. if (!key_rec) {
  1709. rc = -ENOMEM;
  1710. goto out;
  1711. }
  1712. list_for_each_entry(key_sig, &crypt_stat->keysig_list,
  1713. crypt_stat_list) {
  1714. memset(key_rec, 0, sizeof(*key_rec));
  1715. rc = ecryptfs_find_global_auth_tok_for_sig(&global_auth_tok,
  1716. mount_crypt_stat,
  1717. key_sig->keysig);
  1718. if (rc) {
  1719. printk(KERN_ERR "Error attempting to get the global "
  1720. "auth_tok; rc = [%d]\n", rc);
  1721. goto out_free;
  1722. }
  1723. if (global_auth_tok->flags & ECRYPTFS_AUTH_TOK_INVALID) {
  1724. printk(KERN_WARNING
  1725. "Skipping invalid auth tok with sig = [%s]\n",
  1726. global_auth_tok->sig);
  1727. continue;
  1728. }
  1729. auth_tok = global_auth_tok->global_auth_tok;
  1730. if (auth_tok->token_type == ECRYPTFS_PASSWORD) {
  1731. rc = write_tag_3_packet((dest_base + (*len)),
  1732. &max, auth_tok,
  1733. crypt_stat, key_rec,
  1734. &written);
  1735. if (rc) {
  1736. ecryptfs_printk(KERN_WARNING, "Error "
  1737. "writing tag 3 packet\n");
  1738. goto out_free;
  1739. }
  1740. (*len) += written;
  1741. /* Write auth tok signature packet */
  1742. rc = write_tag_11_packet((dest_base + (*len)), &max,
  1743. key_rec->sig,
  1744. ECRYPTFS_SIG_SIZE, &written);
  1745. if (rc) {
  1746. ecryptfs_printk(KERN_ERR, "Error writing "
  1747. "auth tok signature packet\n");
  1748. goto out_free;
  1749. }
  1750. (*len) += written;
  1751. } else if (auth_tok->token_type == ECRYPTFS_PRIVATE_KEY) {
  1752. rc = write_tag_1_packet(dest_base + (*len),
  1753. &max, auth_tok,
  1754. crypt_stat, key_rec, &written);
  1755. if (rc) {
  1756. ecryptfs_printk(KERN_WARNING, "Error "
  1757. "writing tag 1 packet\n");
  1758. goto out_free;
  1759. }
  1760. (*len) += written;
  1761. } else {
  1762. ecryptfs_printk(KERN_WARNING, "Unsupported "
  1763. "authentication token type\n");
  1764. rc = -EINVAL;
  1765. goto out_free;
  1766. }
  1767. }
  1768. if (likely(max > 0)) {
  1769. dest_base[(*len)] = 0x00;
  1770. } else {
  1771. ecryptfs_printk(KERN_ERR, "Error writing boundary byte\n");
  1772. rc = -EIO;
  1773. }
  1774. out_free:
  1775. kmem_cache_free(ecryptfs_key_record_cache, key_rec);
  1776. out:
  1777. if (rc)
  1778. (*len) = 0;
  1779. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1780. return rc;
  1781. }
  1782. struct kmem_cache *ecryptfs_key_sig_cache;
  1783. int ecryptfs_add_keysig(struct ecryptfs_crypt_stat *crypt_stat, char *sig)
  1784. {
  1785. struct ecryptfs_key_sig *new_key_sig;
  1786. int rc = 0;
  1787. new_key_sig = kmem_cache_alloc(ecryptfs_key_sig_cache, GFP_KERNEL);
  1788. if (!new_key_sig) {
  1789. rc = -ENOMEM;
  1790. printk(KERN_ERR
  1791. "Error allocating from ecryptfs_key_sig_cache\n");
  1792. goto out;
  1793. }
  1794. memcpy(new_key_sig->keysig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1795. mutex_lock(&crypt_stat->keysig_list_mutex);
  1796. list_add(&new_key_sig->crypt_stat_list, &crypt_stat->keysig_list);
  1797. mutex_unlock(&crypt_stat->keysig_list_mutex);
  1798. out:
  1799. return rc;
  1800. }
  1801. struct kmem_cache *ecryptfs_global_auth_tok_cache;
  1802. int
  1803. ecryptfs_add_global_auth_tok(struct ecryptfs_mount_crypt_stat *mount_crypt_stat,
  1804. char *sig)
  1805. {
  1806. struct ecryptfs_global_auth_tok *new_auth_tok;
  1807. int rc = 0;
  1808. new_auth_tok = kmem_cache_zalloc(ecryptfs_global_auth_tok_cache,
  1809. GFP_KERNEL);
  1810. if (!new_auth_tok) {
  1811. rc = -ENOMEM;
  1812. printk(KERN_ERR "Error allocating from "
  1813. "ecryptfs_global_auth_tok_cache\n");
  1814. goto out;
  1815. }
  1816. memcpy(new_auth_tok->sig, sig, ECRYPTFS_SIG_SIZE_HEX);
  1817. new_auth_tok->sig[ECRYPTFS_SIG_SIZE_HEX] = '\0';
  1818. mutex_lock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1819. list_add(&new_auth_tok->mount_crypt_stat_list,
  1820. &mount_crypt_stat->global_auth_tok_list);
  1821. mount_crypt_stat->num_global_auth_toks++;
  1822. mutex_unlock(&mount_crypt_stat->global_auth_tok_list_mutex);
  1823. out:
  1824. return rc;
  1825. }