+
+ /* No signer */
+ Dmsg1(100, "Could not find a valid public key for signature on %s\n", jcr->last_fname);
+ crypto_digest_free(digest);
+ return false;
+}
+
+/*
+ * In the context of jcr, write data to bfd.
+ * We write buflen bytes in buf at addr. addr is updated in place.
+ * The flags specify whether to use sparse files or compression.
+ * Return value is the number of bytes written, or -1 on errors.
+ */
+int32_t extract_data(JCR *jcr, BFILE *bfd, POOLMEM *buf, int32_t buflen,
+ uint64_t *addr, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
+{
+ int stat;
+ char *wbuf; /* write buffer */
+ uint32_t wsize; /* write size */
+ uint32_t rsize; /* read size */
+ char ec1[50]; /* Buffer printing huge values */
+ const uint8_t *cipher_input; /* Decryption input */
+ uint32_t cipher_input_len; /* Decryption input length */
+ uint32_t decrypted_len = 0; /* Decryption output length */
+
+ if (flags & FO_SPARSE) {
+ ser_declare;
+ uint64_t faddr;
+ char ec1[50];
+ wbuf = buf + SPARSE_FADDR_SIZE;
+ rsize = buflen - SPARSE_FADDR_SIZE;
+ ser_begin(buf, SPARSE_FADDR_SIZE);
+ unser_uint64(faddr);
+ if (*addr != faddr) {
+ *addr = faddr;
+ if (blseek(bfd, (off_t)*addr, SEEK_SET) < 0) {
+ berrno be;
+ Jmsg3(jcr, M_ERROR, 0, _("Seek to %s error on %s: ERR=%s\n"),
+ edit_uint64(*addr, ec1), jcr->last_fname,
+ be.strerror(bfd->berrno));
+ return -1;
+ }
+ }
+ } else {
+ wbuf = buf;
+ rsize = buflen;
+ }
+ wsize = rsize;
+ cipher_input = (uint8_t *)wbuf;
+ cipher_input_len = (uint32_t)wsize;
+
+ if (flags & FO_GZIP) {
+#ifdef HAVE_LIBZ
+ uLong compress_len;
+ /*
+ * NOTE! We only use uLong and Byte because they are
+ * needed by the zlib routines, they should not otherwise
+ * be used in Bacula.
+ */
+ compress_len = jcr->compress_buf_size;
+ Dmsg2(100, "Comp_len=%d msglen=%d\n", compress_len, wsize);
+ if ((stat=uncompress((Byte *)jcr->compress_buf, &compress_len,
+ (const Byte *)wbuf, (uLong)rsize)) != Z_OK) {
+ Qmsg(jcr, M_ERROR, 0, _("Uncompression error on file %s. ERR=%s\n"),
+ jcr->last_fname, zlib_strerror(stat));
+ return -1;
+ }
+ wbuf = jcr->compress_buf;
+ wsize = compress_len;
+ cipher_input = (uint8_t *)jcr->compress_buf; /* decrypt decompressed data */
+ cipher_input_len = compress_len;
+ Dmsg2(100, "Write uncompressed %d bytes, total before write=%s\n", compress_len, edit_uint64(jcr->JobBytes, ec1));
+#else
+ Qmsg(jcr, M_ERROR, 0, _("GZIP data stream found, but GZIP not configured!\n"));
+ return -1;
+#endif
+ } else {
+ Dmsg2(30, "Write %u bytes, total before write=%s\n", wsize, edit_uint64(jcr->JobBytes, ec1));
+ }
+
+ if (flags & FO_ENCRYPT) {
+ ASSERT(cipher);
+
+ /*
+ * Grow the crypto buffer, if necessary.
+ * crypto_cipher_update() will process only whole blocks,
+ * buffering the remaining input.
+ */
+ jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, cipher_input_len + cipher_block_size);
+
+
+ /* Encrypt the input block */
+ if (!crypto_cipher_update(cipher, cipher_input, cipher_input_len, (uint8_t *)jcr->crypto_buf, &decrypted_len)) {
+ /* Decryption failed. Shouldn't happen. */
+ Jmsg(jcr, M_FATAL, 0, _("Decryption error\n"));
+ return -1;
+ }
+
+ if (decrypted_len == 0) {
+ /* No full block of data available, write more data */
+ goto ok;
+ }
+
+ Dmsg2(400, "decrypted len=%d undecrypted len=%d\n",
+ decrypted_len, cipher_input_len);
+ wsize = decrypted_len;
+ wbuf = jcr->crypto_buf; /* Decrypted, possibly decompressed output here. */
+ }
+
+
+ if (flags & FO_WIN32DECOMP) {
+ if (!processWin32BackupAPIBlock(bfd, wbuf, wsize)) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return -1;
+ }
+ } else if (bwrite(bfd, wbuf, wsize) != (ssize_t)wsize) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return -1;
+ }
+
+ if (decrypted_len && decrypted_len > wsize) {
+ /* If more than wsize is output, it was previously buffered
+ * and reported, and should not be reported again */
+ wsize = wsize - decrypted_len;
+ }
+
+ok:
+ jcr->JobBytes += wsize;
+ jcr->ReadBytes += rsize;
+ *addr += wsize;
+
+ return wsize;
+}
+
+/*
+ * In the context of jcr, flush any remaining data from the cipher context,
+ * writing it to bfd.
+ * Return value is true on success, false on failure.
+ */
+bool flush_cipher(JCR *jcr, BFILE *bfd, int flags, CIPHER_CONTEXT *cipher, uint32_t cipher_block_size)
+{
+ uint32_t decrypted_len;
+
+ /* Write out the remaining block and free the cipher context */
+ jcr->crypto_buf = check_pool_memory_size(jcr->crypto_buf, cipher_block_size);
+
+ if (!crypto_cipher_finalize(cipher, (uint8_t *)jcr->crypto_buf, &decrypted_len)) {
+ /* Writing out the final, buffered block failed. Shouldn't happen. */
+ Jmsg1(jcr, M_FATAL, 0, _("Decryption error for %s\n"), jcr->last_fname);
+ }
+
+ if (flags & FO_WIN32DECOMP) {
+ if (!processWin32BackupAPIBlock(bfd, jcr->crypto_buf, decrypted_len)) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error in Win32 Block Decomposition on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return false;
+ }
+ } else if (bwrite(bfd, jcr->crypto_buf, decrypted_len) != (ssize_t)decrypted_len) {
+ berrno be;
+ Jmsg2(jcr, M_ERROR, 0, _("Write error on %s: %s\n"),
+ jcr->last_fname, be.strerror(bfd->berrno));
+ return false;
+ }
+
+ return true;