Loading...
Searching...
No Matches
gpgme.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020, Michael Raitza. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
44#pragma once
45
46#include <system.hh>
47
48#include "utils.h"
49
50#include <streambuf>
51#include <istream>
52
53#include <gpgme++/data.h>
54
55namespace ledger {
56
57 class data_streambuffer_t : public std::streambuf {
58 public:
59 GpgME::Data& data;
60
61 /* Size of cbuf */
62 const unsigned int bufsize;
63
64 /* Backing character buffer */
65 std::unique_ptr<char[]> cbuf;
66
67 explicit data_streambuffer_t(GpgME::Data& _data);
68
70
71 protected:
72 virtual std::streambuf::pos_type seekpos(std::streambuf::pos_type sp, std::ios_base::openmode which);
73 virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off,
74 std::ios_base::seekdir dir,
75 std::ios_base::openmode which);
76 };
77
78 class decrypted_stream_t : public std::istream {
79 public:
80 std::shared_ptr<GpgME::Data> dec_d;
81 std::FILE * file;
82
83 /* Establishes an istream decrypting a file pointed to by FILENAME.
84
85 Decryption is performed at object creation and only the decrypted Data
86 buffer is retained as the backing store for the stream.
87
88 Expects the input file to be unencrypted or encrypted in CMS or PGP
89 format (includes asymmetrically and symmetrically encrypted content).
90
91 Calls open_file(), setup_cipher_buffer() and decrypt() and throws
92 exceptions noted in there on error. */
94
95 /* Established an istream serving the decrypted content in DEC_D.
96
97 Make sure DEC_D is properly rewound. (Which it is not after decrypting.)
98
99 Expects DEC_D was created by actually decrypting input data (usually a
100 FILE object). Otherwise GpgME just hands over the reference from the
101 buffer holding the "encrypted" input to DEC_D. Then, you must keep the
102 original object around for the lifetime of this stream. */
103 decrypted_stream_t(std::shared_ptr<GpgME::Data> dec_d);
104
106
107 /* Opens file pointed to by FILENAME.
108
109 Opens the file using fopen() in "rb" mode.
110
111 Throws a runtime error when the file cannot be opened for reading. */
112 static std::FILE * open_file(const path& filename);
113
114 /* Returns a Data buffer connected to an open FILE object.
115
116 Throws a runtime error when the content is neither PGPEncrypted,
117 CMSEncrypted or Unknown, or when the buffer cannot be established. */
118 static std::shared_ptr<GpgME::Data> setup_cipher_buffer(std::FILE * f);
119
120 /* Returns a Data buffer of the plain text. Decrypts cipher text by
121 establishing a proper decryption context, first. .
122
123 Returns the input Data buffer when the encryption type is Unknown, which
124 is considered unencrypted input.
125
126 Throws a runtime error when the decryption fails or when the cipher text
127 is neither PGPEncrypted nor CMSEncrypted. */
128 static std::shared_ptr<GpgME::Data> decrypt(std::shared_ptr<GpgME::Data> enc_d);
129
130 /* Returns an istream, which is either a decrypted_stream_t, given the file
131 is encrypted, or an ifstream object.
132
133 Use this to create the istream! The decrypted_stream_t is perfectly
134 capable reading unencrypted data, but the file size and data pointers no
135 longer match with a standard ifstream. */
136 static std::istream* open_stream(const path& filename);
137 };
138
139} // namespace ledger
General utility facilities used by Ledger.
boost::filesystem::path path
Definition utils.h:68
T & downcast(U &object)
Definition utils.h:468
const unsigned int bufsize
Definition gpgme.h:62
virtual int_type underflow()
virtual std::streambuf::pos_type seekoff(std::streambuf::off_type off, std::ios_base::seekdir dir, std::ios_base::openmode which)
GpgME::Data & data
Definition gpgme.h:59
data_streambuffer_t(GpgME::Data &_data)
std::unique_ptr< char[]> cbuf
Definition gpgme.h:65
virtual std::streambuf::pos_type seekpos(std::streambuf::pos_type sp, std::ios_base::openmode which)
decrypted_stream_t(std::shared_ptr< GpgME::Data > dec_d)
decrypted_stream_t(path &filename)
static std::shared_ptr< GpgME::Data > decrypt(std::shared_ptr< GpgME::Data > enc_d)
static std::FILE * open_file(const path &filename)
static std::shared_ptr< GpgME::Data > setup_cipher_buffer(std::FILE *f)
static std::istream * open_stream(const path &filename)
std::shared_ptr< GpgME::Data > dec_d
Definition gpgme.h:80