PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/g10/decrypt-data.c

https://gitlab.com/oyvholm/gnupg
C | 471 lines | 368 code | 46 blank | 57 comment | 110 complexity | be08fe3854275d684bdaafdfa276ba88 MD5 | raw file
  1. /* decrypt-data.c - Decrypt an encrypted data packet
  2. * Copyright (C) 1998, 1999, 2000, 2001, 2005,
  3. * 2006, 2009 Free Software Foundation, Inc.
  4. *
  5. * This file is part of GnuPG.
  6. *
  7. * GnuPG is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * GnuPG is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <config.h>
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include "gpg.h"
  26. #include "util.h"
  27. #include "packet.h"
  28. #include "options.h"
  29. #include "i18n.h"
  30. #include "status.h"
  31. static int mdc_decode_filter ( void *opaque, int control, IOBUF a,
  32. byte *buf, size_t *ret_len);
  33. static int decode_filter ( void *opaque, int control, IOBUF a,
  34. byte *buf, size_t *ret_len);
  35. typedef struct decode_filter_context_s
  36. {
  37. gcry_cipher_hd_t cipher_hd;
  38. gcry_md_hd_t mdc_hash;
  39. char defer[22];
  40. int defer_filled;
  41. int eof_seen;
  42. int refcount;
  43. int partial; /* Working on a partial length packet. */
  44. size_t length; /* If !partial: Remaining bytes in the packet. */
  45. } *decode_filter_ctx_t;
  46. /* Helper to release the decode context. */
  47. static void
  48. release_dfx_context (decode_filter_ctx_t dfx)
  49. {
  50. if (!dfx)
  51. return;
  52. assert (dfx->refcount);
  53. if ( !--dfx->refcount )
  54. {
  55. gcry_cipher_close (dfx->cipher_hd);
  56. dfx->cipher_hd = NULL;
  57. gcry_md_close (dfx->mdc_hash);
  58. dfx->mdc_hash = NULL;
  59. xfree (dfx);
  60. }
  61. }
  62. /****************
  63. * Decrypt the data, specified by ED with the key DEK.
  64. */
  65. int
  66. decrypt_data (ctrl_t ctrl, void *procctx, PKT_encrypted *ed, DEK *dek)
  67. {
  68. decode_filter_ctx_t dfx;
  69. byte *p;
  70. int rc=0, c, i;
  71. byte temp[32];
  72. unsigned blocksize;
  73. unsigned nprefix;
  74. dfx = xtrycalloc (1, sizeof *dfx);
  75. if (!dfx)
  76. return gpg_error_from_syserror ();
  77. dfx->refcount = 1;
  78. if ( opt.verbose && !dek->algo_info_printed )
  79. {
  80. if (!openpgp_cipher_test_algo (dek->algo))
  81. log_info (_("%s encrypted data\n"),
  82. openpgp_cipher_algo_name (dek->algo));
  83. else
  84. log_info (_("encrypted with unknown algorithm %d\n"), dek->algo );
  85. dek->algo_info_printed = 1;
  86. }
  87. {
  88. char buf[20];
  89. snprintf (buf, sizeof buf, "%d %d", ed->mdc_method, dek->algo);
  90. write_status_text (STATUS_DECRYPTION_INFO, buf);
  91. }
  92. if (opt.show_session_key)
  93. {
  94. char numbuf[25];
  95. char *hexbuf;
  96. snprintf (numbuf, sizeof numbuf, "%d:", dek->algo);
  97. hexbuf = bin2hex (dek->key, dek->keylen, NULL);
  98. if (!hexbuf)
  99. {
  100. rc = gpg_error_from_syserror ();
  101. goto leave;
  102. }
  103. log_info ("session key: '%s%s'\n", numbuf, hexbuf);
  104. write_status_strings (STATUS_SESSION_KEY, numbuf, hexbuf, NULL);
  105. xfree (hexbuf);
  106. }
  107. rc = openpgp_cipher_test_algo (dek->algo);
  108. if (rc)
  109. goto leave;
  110. blocksize = openpgp_cipher_get_algo_blklen (dek->algo);
  111. if ( !blocksize || blocksize > 16 )
  112. log_fatal ("unsupported blocksize %u\n", blocksize );
  113. nprefix = blocksize;
  114. if ( ed->len && ed->len < (nprefix+2) )
  115. {
  116. /* An invalid message. We can't check that during parsing
  117. because we may not know the used cipher then. */
  118. rc = gpg_error (GPG_ERR_INV_PACKET);
  119. goto leave;
  120. }
  121. if ( ed->mdc_method )
  122. {
  123. if (gcry_md_open (&dfx->mdc_hash, ed->mdc_method, 0 ))
  124. BUG ();
  125. if ( DBG_HASHING )
  126. gcry_md_debug (dfx->mdc_hash, "checkmdc");
  127. }
  128. rc = openpgp_cipher_open (&dfx->cipher_hd, dek->algo,
  129. GCRY_CIPHER_MODE_CFB,
  130. (GCRY_CIPHER_SECURE
  131. | ((ed->mdc_method || dek->algo >= 100)?
  132. 0 : GCRY_CIPHER_ENABLE_SYNC)));
  133. if (rc)
  134. {
  135. /* We should never get an error here cause we already checked
  136. * that the algorithm is available. */
  137. BUG();
  138. }
  139. /* log_hexdump( "thekey", dek->key, dek->keylen );*/
  140. rc = gcry_cipher_setkey (dfx->cipher_hd, dek->key, dek->keylen);
  141. if ( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
  142. {
  143. log_info(_("WARNING: message was encrypted with"
  144. " a weak key in the symmetric cipher.\n"));
  145. rc=0;
  146. }
  147. else if( rc )
  148. {
  149. log_error("key setup failed: %s\n", gpg_strerror (rc) );
  150. goto leave;
  151. }
  152. if (!ed->buf)
  153. {
  154. log_error(_("problem handling encrypted packet\n"));
  155. goto leave;
  156. }
  157. gcry_cipher_setiv (dfx->cipher_hd, NULL, 0);
  158. if ( ed->len )
  159. {
  160. for (i=0; i < (nprefix+2) && ed->len; i++, ed->len-- )
  161. {
  162. if ( (c=iobuf_get(ed->buf)) == -1 )
  163. break;
  164. else
  165. temp[i] = c;
  166. }
  167. }
  168. else
  169. {
  170. for (i=0; i < (nprefix+2); i++ )
  171. if ( (c=iobuf_get(ed->buf)) == -1 )
  172. break;
  173. else
  174. temp[i] = c;
  175. }
  176. gcry_cipher_decrypt (dfx->cipher_hd, temp, nprefix+2, NULL, 0);
  177. gcry_cipher_sync (dfx->cipher_hd);
  178. p = temp;
  179. /* log_hexdump( "prefix", temp, nprefix+2 ); */
  180. if (dek->symmetric
  181. && (p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1]) )
  182. {
  183. rc = gpg_error (GPG_ERR_BAD_KEY);
  184. goto leave;
  185. }
  186. if ( dfx->mdc_hash )
  187. gcry_md_write (dfx->mdc_hash, temp, nprefix+2);
  188. dfx->refcount++;
  189. dfx->partial = ed->is_partial;
  190. dfx->length = ed->len;
  191. if ( ed->mdc_method )
  192. iobuf_push_filter ( ed->buf, mdc_decode_filter, dfx );
  193. else
  194. iobuf_push_filter ( ed->buf, decode_filter, dfx );
  195. proc_packets (ctrl, procctx, ed->buf );
  196. ed->buf = NULL;
  197. if (dfx->eof_seen > 1 )
  198. rc = gpg_error (GPG_ERR_INV_PACKET);
  199. else if ( ed->mdc_method )
  200. {
  201. /* We used to let parse-packet.c handle the MDC packet but this
  202. turned out to be a problem with compressed packets: With old
  203. style packets there is no length information available and
  204. the decompressor uses an implicit end. However we can't know
  205. this implicit end beforehand (:-) and thus may feed the
  206. decompressor with more bytes than actually needed. It would
  207. be possible to unread the extra bytes but due to our weird
  208. iobuf system any unread is non reliable due to filters
  209. already popped off. The easy and sane solution is to care
  210. about the MDC packet only here and never pass it to the
  211. packet parser. Fortunatley the OpenPGP spec requires a
  212. strict format for the MDC packet so that we know that 22
  213. bytes are appended. */
  214. int datalen = gcry_md_get_algo_dlen (ed->mdc_method);
  215. assert (dfx->cipher_hd);
  216. assert (dfx->mdc_hash);
  217. gcry_cipher_decrypt (dfx->cipher_hd, dfx->defer, 22, NULL, 0);
  218. gcry_md_write (dfx->mdc_hash, dfx->defer, 2);
  219. gcry_md_final (dfx->mdc_hash);
  220. if ( dfx->defer[0] != '\xd3'
  221. || dfx->defer[1] != '\x14'
  222. || datalen != 20
  223. || memcmp (gcry_md_read (dfx->mdc_hash, 0), dfx->defer+2, datalen))
  224. rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
  225. /* log_printhex("MDC message:", dfx->defer, 22); */
  226. /* log_printhex("MDC calc:", gcry_md_read (dfx->mdc_hash,0), datalen); */
  227. }
  228. leave:
  229. release_dfx_context (dfx);
  230. return rc;
  231. }
  232. static int
  233. mdc_decode_filter (void *opaque, int control, IOBUF a,
  234. byte *buf, size_t *ret_len)
  235. {
  236. decode_filter_ctx_t dfx = opaque;
  237. size_t n, size = *ret_len;
  238. int rc = 0;
  239. int c;
  240. /* Note: We need to distinguish between a partial and a fixed length
  241. packet. The first is the usual case as created by GPG. However
  242. for short messages the format degrades to a fixed length packet
  243. and other implementations might use fixed length as well. Only
  244. looking for the EOF on fixed data works only if the encrypted
  245. packet is not followed by other data. This used to be a long
  246. standing bug which was fixed on 2009-10-02. */
  247. if ( control == IOBUFCTRL_UNDERFLOW && dfx->eof_seen )
  248. {
  249. *ret_len = 0;
  250. rc = -1;
  251. }
  252. else if( control == IOBUFCTRL_UNDERFLOW )
  253. {
  254. assert (a);
  255. assert (size > 44); /* Our code requires at least this size. */
  256. /* Get at least 22 bytes and put it ahead in the buffer. */
  257. if (dfx->partial)
  258. {
  259. for (n=22; n < 44; n++)
  260. {
  261. if ( (c = iobuf_get(a)) == -1 )
  262. break;
  263. buf[n] = c;
  264. }
  265. }
  266. else
  267. {
  268. for (n=22; n < 44 && dfx->length; n++, dfx->length--)
  269. {
  270. c = iobuf_get (a);
  271. if (c == -1)
  272. break; /* Premature EOF. */
  273. buf[n] = c;
  274. }
  275. }
  276. if (n == 44)
  277. {
  278. /* We have enough stuff - flush the deferred stuff. */
  279. if ( !dfx->defer_filled ) /* First time. */
  280. {
  281. memcpy (buf, buf+22, 22);
  282. n = 22;
  283. }
  284. else
  285. {
  286. memcpy (buf, dfx->defer, 22);
  287. }
  288. /* Fill up the buffer. */
  289. if (dfx->partial)
  290. {
  291. for (; n < size; n++ )
  292. {
  293. if ( (c = iobuf_get(a)) == -1 )
  294. {
  295. dfx->eof_seen = 1; /* Normal EOF. */
  296. break;
  297. }
  298. buf[n] = c;
  299. }
  300. }
  301. else
  302. {
  303. for (; n < size && dfx->length; n++, dfx->length--)
  304. {
  305. c = iobuf_get(a);
  306. if (c == -1)
  307. {
  308. dfx->eof_seen = 3; /* Premature EOF. */
  309. break;
  310. }
  311. buf[n] = c;
  312. }
  313. if (!dfx->length)
  314. dfx->eof_seen = 1; /* Normal EOF. */
  315. }
  316. /* Move the trailing 22 bytes back to the defer buffer. We
  317. have at least 44 bytes thus a memmove is not needed. */
  318. n -= 22;
  319. memcpy (dfx->defer, buf+n, 22 );
  320. dfx->defer_filled = 1;
  321. }
  322. else if ( !dfx->defer_filled ) /* EOF seen but empty defer buffer. */
  323. {
  324. /* This is bad because it means an incomplete hash. */
  325. n -= 22;
  326. memcpy (buf, buf+22, n );
  327. dfx->eof_seen = 2; /* EOF with incomplete hash. */
  328. }
  329. else /* EOF seen (i.e. read less than 22 bytes). */
  330. {
  331. memcpy (buf, dfx->defer, 22 );
  332. n -= 22;
  333. memcpy (dfx->defer, buf+n, 22 );
  334. dfx->eof_seen = 1; /* Normal EOF. */
  335. }
  336. if ( n )
  337. {
  338. if ( dfx->cipher_hd )
  339. gcry_cipher_decrypt (dfx->cipher_hd, buf, n, NULL, 0);
  340. if ( dfx->mdc_hash )
  341. gcry_md_write (dfx->mdc_hash, buf, n);
  342. }
  343. else
  344. {
  345. assert ( dfx->eof_seen );
  346. rc = -1; /* Return EOF. */
  347. }
  348. *ret_len = n;
  349. }
  350. else if ( control == IOBUFCTRL_FREE )
  351. {
  352. release_dfx_context (dfx);
  353. }
  354. else if ( control == IOBUFCTRL_DESC )
  355. {
  356. *(char**)buf = "mdc_decode_filter";
  357. }
  358. return rc;
  359. }
  360. static int
  361. decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
  362. {
  363. decode_filter_ctx_t fc = opaque;
  364. size_t size = *ret_len;
  365. size_t n;
  366. int c, rc = 0;
  367. if ( control == IOBUFCTRL_UNDERFLOW && fc->eof_seen )
  368. {
  369. *ret_len = 0;
  370. rc = -1;
  371. }
  372. else if ( control == IOBUFCTRL_UNDERFLOW )
  373. {
  374. assert(a);
  375. if (fc->partial)
  376. {
  377. for (n=0; n < size; n++ )
  378. {
  379. c = iobuf_get(a);
  380. if (c == -1)
  381. {
  382. fc->eof_seen = 1; /* Normal EOF. */
  383. break;
  384. }
  385. buf[n] = c;
  386. }
  387. }
  388. else
  389. {
  390. for (n=0; n < size && fc->length; n++, fc->length--)
  391. {
  392. c = iobuf_get(a);
  393. if (c == -1)
  394. {
  395. fc->eof_seen = 3; /* Premature EOF. */
  396. break;
  397. }
  398. buf[n] = c;
  399. }
  400. if (!fc->length)
  401. fc->eof_seen = 1; /* Normal EOF. */
  402. }
  403. if (n)
  404. {
  405. if (fc->cipher_hd)
  406. gcry_cipher_decrypt (fc->cipher_hd, buf, n, NULL, 0);
  407. }
  408. else
  409. {
  410. if (!fc->eof_seen)
  411. fc->eof_seen = 1;
  412. rc = -1; /* Return EOF. */
  413. }
  414. *ret_len = n;
  415. }
  416. else if ( control == IOBUFCTRL_FREE )
  417. {
  418. release_dfx_context (fc);
  419. }
  420. else if ( control == IOBUFCTRL_DESC )
  421. {
  422. *(char**)buf = "decode_filter";
  423. }
  424. return rc;
  425. }