Cipherpack v1.2.0-dirty
A Cryprographic Stream Processor
Cipherpack.cxx
Go to the documentation of this file.
1/*
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25#include "org_cipherpack_Cipherpack.h"
26#include "org_cipherpack_Cipherpack_HashUtil.h"
27
28// #define VERBOSE_ON 1
29#include <jau/debug.hpp>
30
32
33#include "CipherpackHelper.hpp"
34
35jobject Java_org_cipherpack_Cipherpack_encryptThenSignImpl1(JNIEnv *env, jclass jclazz,
36 jobject jccfg, jobject jenc_pub_keys,
37 jstring jsign_sec_key_fname, jobject jpassphrase,
38 jobject jsource_feed,
39 jstring jtarget_path, jstring jsubject,
40 jstring jplaintext_version,
41 jstring jplaintext_version_parent,
42 jobject cpListener,
43 jstring jplaintext_hash_algo,
44 jstring jdestination_fname)
45{
46 try {
47 jau::jni::shared_ptr_ref<jau::io::ByteInStream> refSource(env, jsource_feed); // hold until done
48 jau::jni::shared_ptr_ref<cipherpack::CipherpackListener> refListener(env, cpListener); // hold until done
49
51 std::vector<std::string> enc_pub_keys = jau::jni::convert_jlist_string_to_vector(env, jenc_pub_keys);
52 std::string sign_sec_key_fname = jau::jni::from_jstring_to_string(env, jsign_sec_key_fname);
53 jau::io::secure_string passphrase = nullptr != jpassphrase ? jau::jni::from_jbytebuffer_to_sstring(env, jpassphrase) : jau::io::secure_string();
54 std::string target_path = jau::jni::from_jstring_to_string(env, jtarget_path);
55 std::string subject = jau::jni::from_jstring_to_string(env, jsubject);
56 std::string plaintext_version = jau::jni::from_jstring_to_string(env, jplaintext_version);
57 std::string plaintext_version_parent = jau::jni::from_jstring_to_string(env, jplaintext_version_parent);
58 std::string plaintext_hash_algo = jau::jni::from_jstring_to_string(env, jplaintext_hash_algo);
59 std::string destination_fname = nullptr != jdestination_fname ? jau::jni::from_jstring_to_string(env, jdestination_fname) : "";
60
61 cipherpack::PackHeader ph = encryptThenSign(ccfg, enc_pub_keys, sign_sec_key_fname, passphrase, *refSource,
62 target_path, subject, plaintext_version, plaintext_version_parent,
63 refListener.shared_ptr(), plaintext_hash_algo, destination_fname);
64 jau::jni::java_exception_check_and_throw(env, E_FILE_LINE);
65
66 jobject jph = jcipherpack::to_jPackHeader(env, ph);
67
68 return jph;
69 } catch(...) {
71 }
72 return nullptr;
73}
74
76 jobject jsign_pub_keys,
77 jstring jdec_sec_key_fname, jobject jpassphrase,
78 jobject jsource_feed,
79 jobject cpListener,
80 jstring jplaintext_hash_algo,
81 jstring jdestination_fname)
82{
83 try {
84 jau::jni::shared_ptr_ref<jau::io::ByteInStream> refSource(env, jsource_feed); // hold until done
85 jau::jni::shared_ptr_ref<cipherpack::CipherpackListener> refListener(env, cpListener); // hold until done
86
87 std::vector<std::string> sign_pub_keys = jau::jni::convert_jlist_string_to_vector(env, jsign_pub_keys);
88 std::string dec_sec_key_fname = jau::jni::from_jstring_to_string(env, jdec_sec_key_fname);
89 jau::io::secure_string passphrase = nullptr != jpassphrase ? jau::jni::from_jbytebuffer_to_sstring(env, jpassphrase) : jau::io::secure_string();
90 std::string plaintext_hash_algo = jau::jni::from_jstring_to_string(env, jplaintext_hash_algo);
91 std::string destination_fname = nullptr != jdestination_fname ? jau::jni::from_jstring_to_string(env, jdestination_fname) : "";
92
93 cipherpack::PackHeader ph = checkSignThenDecrypt(sign_pub_keys, dec_sec_key_fname, passphrase, *refSource,
94 refListener.shared_ptr(), plaintext_hash_algo, destination_fname);
95 jau::jni::java_exception_check_and_throw(env, E_FILE_LINE);
96
97 jobject jph = jcipherpack::to_jPackHeader(env, ph);
98
99 return jph;
100 } catch(...) {
102 }
103 return nullptr;
104}
105
106jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl1(JNIEnv *env, jclass jclazz, jstring jalgo, jobject jsource_feed) {
107 try {
108 jau::jni::shared_ptr_ref<jau::io::ByteInStream> refSource(env, jsource_feed); // hold until done
109 std::string algo = jau::jni::from_jstring_to_string(env, jalgo);
110
111 std::unique_ptr<std::vector<uint8_t>> hash = cipherpack::hash_util::calc(algo, *refSource);
112 if( nullptr == hash ) {
113 return nullptr;
114 }
115 jbyteArray jhash = jau::jni::convert_bytes_to_jbytearray(env, *hash);
116 return jhash;
117 } catch(...) {
119 }
120 return nullptr;
121}
122
123jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl2(JNIEnv *env, jclass jclazz, jstring jalgo, jstring jpath_or_uri, jlongArray jbytes_hashed, jlong jtimeoutMS) {
124 try {
125 std::string algo = jau::jni::from_jstring_to_string(env, jalgo);
126 std::string path_or_uri = jau::jni::from_jstring_to_string(env, jpath_or_uri);
127 const jau::fraction_i64 timeout = (int64_t)jtimeoutMS * 1_ms;
128
129 if( nullptr == jbytes_hashed ) {
130 throw jau::IllegalArgumentException("bytes_hashed null", E_FILE_LINE);
131 }
132 const size_t bh_size = env->GetArrayLength(jbytes_hashed);
133 if( 1 > bh_size ) {
134 throw jau::IllegalArgumentException("bytes_hashed array size "+std::to_string(bh_size)+" < 1", E_FILE_LINE);
135 }
136 jau::jni::JNICriticalArray<uint64_t, jlongArray> criticalArray(env); // RAII - release
137 uint64_t * bh_ptr = criticalArray.get(jbytes_hashed, criticalArray.Mode::UPDATE_AND_RELEASE);
138 if( nullptr == bh_ptr ) {
139 throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
140 }
141
142 std::unique_ptr<std::vector<uint8_t>> hash = cipherpack::hash_util::calc(algo, path_or_uri, *bh_ptr, timeout);
143 if( nullptr == hash ) {
144 return nullptr;
145 }
146 jbyteArray jhash = jau::jni::convert_bytes_to_jbytearray(env, *hash);
147 return jhash;
148 } catch(...) {
150 }
151 return nullptr;
152}
jobject Java_org_cipherpack_Cipherpack_checkSignThenDecrypt1(JNIEnv *env, jclass jclazz, jobject jsign_pub_keys, jstring jdec_sec_key_fname, jobject jpassphrase, jobject jsource_feed, jobject cpListener, jstring jplaintext_hash_algo, jstring jdestination_fname)
Definition: Cipherpack.cxx:75
jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl2(JNIEnv *env, jclass jclazz, jstring jalgo, jstring jpath_or_uri, jlongArray jbytes_hashed, jlong jtimeoutMS)
Definition: Cipherpack.cxx:123
jobject Java_org_cipherpack_Cipherpack_encryptThenSignImpl1(JNIEnv *env, jclass jclazz, jobject jccfg, jobject jenc_pub_keys, jstring jsign_sec_key_fname, jobject jpassphrase, jobject jsource_feed, jstring jtarget_path, jstring jsubject, jstring jplaintext_version, jstring jplaintext_version_parent, jobject cpListener, jstring jplaintext_hash_algo, jstring jdestination_fname)
Definition: Cipherpack.cxx:35
jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl1(JNIEnv *env, jclass jclazz, jstring jalgo, jobject jsource_feed)
Definition: Cipherpack.cxx:106
Cipherpack header less encrypted keys or signatures as described in Cipherpack Data Stream.
Definition: cipherpack.hpp:275
static std::string to_string(const std::vector< uint8_t > &v)
Definition: crypto1.cpp:72
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...
Definition: crypto1.cpp:518
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...
Definition: crypto1.cpp:1134
#define rethrow_and_raise_java_exception(E)
Re-throw current exception and raise respective java exception using any matching function above.
Definition: helper_base.hpp:45
std::unique_ptr< std::vector< uint8_t > > calc(const std::string_view &algo, jau::io::ByteInStream &source) noexcept
Return the calculated hash value using given algo name and byte input stream.
Definition: crypto0.cpp:386
jobject to_jPackHeader(JNIEnv *env, const cipherpack::PackHeader &ph)
cipherpack::CryptoConfig to_CryptoConfig(JNIEnv *env, jobject jccfg)
CryptoConfig, contains crypto algorithms settings given at encryption wired via the Cipherpack Data S...
Definition: cipherpack.hpp:205