26#ifndef JAU_CIPHERPACK_HPP_
27#define JAU_CIPHERPACK_HPP_
37#include <jau/basic_types.hpp>
38#include <jau/file_util.hpp>
39#include <jau/byte_stream.hpp>
40#include <jau/io_util.hpp>
41#include <jau/environment.hpp>
42#include <jau/java_uplink.hpp>
141 #define JAVA_MAIN_PACKAGE "org/cipherpack/"
166 template<
typename T>
using secure_vector = std::vector<T, Botan::secure_allocator<T>>;
174 jau::io::ByteInStream&
in;
179 [[nodiscard]]
size_t read(uint8_t out[],
size_t length)
override
180 {
return in.read(out, length); }
183 {
return in.available(n); }
185 [[nodiscard]]
size_t peek(uint8_t out[],
size_t length,
size_t peek_offset)
const override
186 {
return in.peek(out, length, peek_offset); }
189 {
return !
in.good(); }
191 std::string
id()
const override
195 {
return static_cast<size_t>(
in.tellg() ); }
238 const std::string& pk_fingerprt_hash_algo_,
239 const std::string& pk_enc_padding_algo_,
240 const std::string& pk_enc_hash_algo_,
241 const std::string& pk_sign_algo_,
242 const std::string& sym_enc_algo_,
243 const size_t sym_enc_nonce_bytes_) noexcept
253 bool valid() const noexcept;
261 constexpr static const size_t buffer_size = 16384;
277 std::string target_path_;
278 uint64_t plaintext_size_;
279 jau::fraction_timespec ts_creation_;
280 std::string subject_;
281 std::string plaintext_version_;
282 std::string plaintext_version_parent_;
284 std::vector<uint8_t> sender_fingerprint_;
285 std::vector<std::vector<uint8_t>> recevr_fingerprints_;
286 ssize_t used_recevr_key_idx_;
287 std::string plaintext_hash_algo_;
288 std::vector<uint8_t> plaintext_hash_;
294 : target_path_(
"none"),
296 ts_creation_( jau::getWallClockTime() ),
298 plaintext_version_(),
299 plaintext_version_parent_(),
301 sender_fingerprint_(),
302 recevr_fingerprints_(),
303 used_recevr_key_idx_(-1),
304 plaintext_hash_algo_(),
311 : target_path_(
"none"),
313 ts_creation_( ts_creation ),
315 plaintext_version_(),
316 plaintext_version_parent_(),
318 sender_fingerprint_(),
319 recevr_fingerprints_(),
320 used_recevr_key_idx_(-1),
321 plaintext_hash_algo_(),
328 const uint64_t& _plaintext_size,
329 const jau::fraction_timespec& _ts_creation,
330 const std::string& _subject,
331 const std::string& _pversion,
const std::string& _pversion_parent,
333 const std::vector<uint8_t>& _sender_fingerprint,
334 const std::vector<std::vector<uint8_t>>& _recevr_fingerprints,
335 const size_t _used_recevr_key_idx,
337 : target_path_(_target_path),
338 plaintext_size_(_plaintext_size),
339 ts_creation_(_ts_creation),
341 plaintext_version_(_pversion), plaintext_version_parent_(_pversion_parent),
342 crypto_cfg_(_crypto_cfg),
343 sender_fingerprint_(_sender_fingerprint),
344 recevr_fingerprints_(_recevr_fingerprints),
345 used_recevr_key_idx_(_used_recevr_key_idx),
346 plaintext_hash_algo_(),
352 const std::string&
target_path() const noexcept {
return target_path_; }
360 constexpr const jau::fraction_timespec&
creation_time() const noexcept {
return ts_creation_; }
363 constexpr const std::string&
subject() const noexcept {
return subject_; }
408 const std::vector<uint8_t>&
plaintext_hash() const noexcept {
return plaintext_hash_; }
416 plaintext_hash_algo_ = algo;
417 plaintext_hash_ = hash;
426 std::string
to_string(
const bool show_crypto_algos=
false,
const bool force_all_fingerprints=
false) const noexcept;
428 void setValid(const
bool v) { valid_ = v; }
429 bool isValid() const noexcept {
return valid_; }
433 std::shared_ptr<Botan::Public_Key>
load_public_key(
const std::string& pubkey_fname);
434 std::shared_ptr<Botan::Private_Key>
load_private_key(
const std::string& privatekey_fname,
const jau::io::secure_string& passphrase);
485 virtual bool notifyProgress(
const bool decrypt_mode,
const uint64_t plaintext_size,
const uint64_t bytes_processed)
noexcept {
487 (void)plaintext_size;
488 (void)bytes_processed;
537 std::string
toString() const noexcept
override {
return "CipherpackListener["+jau::to_hexstring(
this)+
"]"; }
553 {
return this == &rhs; }
556 {
return !(*
this == rhs); }
607 const std::vector<std::
string>& enc_pub_keys,
608 const std::
string& sign_sec_key_fname, const jau::io::secure_string& passphrase,
609 jau::io::ByteInStream& source,
610 const std::
string& target_path, const std::
string& subject,
611 const std::
string& plaintext_version,
612 const std::
string& plaintext_version_parent,
614 const std::string_view& plaintext_hash_algo,
615 const std::
string destination_fname = "");
640 const std::
string& dec_sec_key_fname, const jau::io::secure_string& passphrase,
641 jau::io::ByteInStream& source,
643 const std::string_view& plaintext_hash_algo,
644 const std::
string destination_fname = "");
650 namespace hash_util {
652 std::string
file_suffix(
const std::string& algo)
noexcept;
673 bool append_to_file(
const std::string& out_file,
const std::string& hashed_file,
const std::string_view& hash_algo,
const std::vector<uint8_t>& hash_value)
noexcept;
681 std::unique_ptr<std::vector<uint8_t>>
calc(
const std::string_view& algo, jau::io::ByteInStream& source)
noexcept;
691 std::unique_ptr<std::vector<uint8_t>>
calc(
const std::string_view& algo,
const std::string& path_or_uri, uint64_t& bytes_hashed, jau::fraction_i64 timeout=20_s)
noexcept;
Listener for events occurring while processing a cipherpack message via encryptThenSign() and checkSi...
static std::string java_class() noexcept
virtual bool contentProcessed(const bool decrypt_mode, const content_type ctype, cipherpack::secure_vector< uint8_t > &data, const bool is_final) noexcept
User callback to receive the actual processed content, either the generated cipherpack or plaintext c...
virtual bool notifyProgress(const bool decrypt_mode, const uint64_t plaintext_size, const uint64_t bytes_processed) noexcept
User notification about content streaming progress.
std::string toString() const noexcept override
bool operator!=(const CipherpackListener &rhs) const noexcept
std::string get_java_class() const noexcept override
virtual bool getSendContent(const bool decrypt_mode) const noexcept
User provided information whether process shall send the processed content via contentProcessed() or ...
virtual void notifyEnd(const bool decrypt_mode, const PackHeader &header) noexcept
User notification of successful completion.
~CipherpackListener() noexcept override
virtual bool operator==(const CipherpackListener &rhs) const noexcept
Default comparison operator, merely testing for same memory reference.
virtual void notifyError(const bool decrypt_mode, const PackHeader &header, const std::string &msg) noexcept
User notification about an error via text message and preliminary PackHeader.
virtual bool notifyHeader(const bool decrypt_mode, const PackHeader &header) noexcept
User notification of preliminary PackHeader w/o optional hash of the plaintext message.
static const std::string package_magic
Package magic CIPHERPACK_0004.
This class represents an abstract data source object.
std::string id() const override
bool end_of_data() const override
size_t peek(uint8_t out[], size_t length, size_t peek_offset) const override
WrappingDataSource(jau::io::ByteInStream &in_)
size_t read(uint8_t out[], size_t length) override
jau::io::ByteInStream & in
bool check_available(size_t n) override
size_t get_bytes_read() const override
void print_info() noexcept
static environment & get() noexcept
std::shared_ptr< Botan::Public_Key > load_public_key(const std::string &pubkey_fname)
std::shared_ptr< CipherpackListener > CipherpackListenerRef
std::shared_ptr< Botan::Private_Key > load_private_key(const std::string &privatekey_fname, const jau::io::secure_string &passphrase)
std::vector< T, Botan::secure_allocator< T > > secure_vector
PackHeader encryptThenSign(const CryptoConfig &crypto_cfg, const std::vector< std::string > &enc_pub_keys, const std::string &sign_sec_key_fname, const jau::io::secure_string &passphrase, jau::io::ByteInStream &source, const std::string &target_path, const std::string &subject, const std::string &plaintext_version, const std::string &plaintext_version_parent, CipherpackListenerRef listener, const std::string_view &plaintext_hash_algo, const std::string destination_fname="")
Encrypt then sign the source producing a cipherpack stream passed to the CipherpackListener if opt-in...
std::string_view default_hash_algo() noexcept
Name of default hash algo for the plaintext message, e.g.
PackHeader checkSignThenDecrypt(const std::vector< std::string > &sign_pub_keys, const std::string &dec_sec_key_fname, const jau::io::secure_string &passphrase, jau::io::ByteInStream &source, CipherpackListenerRef listener, const std::string_view &plaintext_hash_algo, const std::string destination_fname="")
Verify signature then decrypt the source passing to the CipherpackListener if opt-in and also optiona...
#define JAVA_MAIN_PACKAGE
std::unique_ptr< std::vector< uint8_t > > calc(const std::string_view &algo, const std::string &path_or_uri, uint64_t &bytes_hashed, jau::fraction_i64 timeout=20_s) noexcept
Return the calculated hash value using given algo name and the bytes of a single file or all files if...
std::string file_suffix(const std::string &algo) noexcept
Return a lower-case file suffix used to store a sha256sum compatible hash signature w/o dot and w/o d...
bool append_to_file(const std::string &out_file, const std::string &hashed_file, const std::string_view &hash_algo, const std::vector< uint8_t > &hash_value) noexcept
Append the hash signature to the text file out_file.
CryptoConfig, contains crypto algorithms settings given at encryption wired via the Cipherpack Data S...
size_t sym_enc_nonce_bytes
bool valid() const noexcept
CryptoConfig(const std::string &pk_type_, const std::string &pk_fingerprt_hash_algo_, const std::string &pk_enc_padding_algo_, const std::string &pk_enc_hash_algo_, const std::string &pk_sign_algo_, const std::string &sym_enc_algo_, const size_t sym_enc_nonce_bytes_) noexcept
static CryptoConfig getDefault() noexcept
Returns default CryptoConfig.
std::string pk_enc_padding_algo
std::string pk_fingerprt_hash_algo
std::string pk_enc_hash_algo
std::string to_string() const noexcept