Cipherpack v1.3.0-3-ga29431a
A Cryprographic Stream Processor
Loading...
Searching...
No Matches
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/basic_types.hpp>
30#include <jau/debug.hpp>
31
33
34#include "CipherpackHelper.hpp"
35
36jobject Java_org_cipherpack_Cipherpack_encryptThenSignImpl1(JNIEnv *env, jclass /*jclazz*/,
37 jobject jccfg, jobject jenc_pub_keys,
38 jstring jsign_sec_key_fname, jobject jpassphrase,
39 jobject jsource_feed,
40 jstring jtarget_path, jstring jsubject,
41 jstring jplaintext_version,
42 jstring jplaintext_version_parent,
43 jobject cpListener,
44 jstring jplaintext_hash_algo,
45 jstring jdestination_fname)
46{
47 try {
48 jau::jni::shared_ptr_ref<jau::io::ByteStream> refSource(env, jsource_feed); // hold until done
49 jau::jni::shared_ptr_ref<cipherpack::CipherpackListener> refListener(env, cpListener); // hold until done
50
52 std::vector<std::string> enc_pub_keys = jau::jni::convert_jlist_string_to_vector(env, jenc_pub_keys);
53 std::string sign_sec_key_fname = jau::jni::from_jstring_to_string(env, jsign_sec_key_fname);
54 jau::io::secure_string passphrase = nullptr != jpassphrase ? jau::jni::from_jbytebuffer_to_sstring(env, jpassphrase) : jau::io::secure_string();
55 std::string target_path = jau::jni::from_jstring_to_string(env, jtarget_path);
56 std::string subject = jau::jni::from_jstring_to_string(env, jsubject);
57 std::string plaintext_version = jau::jni::from_jstring_to_string(env, jplaintext_version);
58 std::string plaintext_version_parent = jau::jni::from_jstring_to_string(env, jplaintext_version_parent);
59 std::string plaintext_hash_algo = jau::jni::from_jstring_to_string(env, jplaintext_hash_algo);
60 std::string destination_fname = nullptr != jdestination_fname ? jau::jni::from_jstring_to_string(env, jdestination_fname) : "";
61
62 cipherpack::PackHeader ph = encryptThenSign(ccfg, enc_pub_keys, sign_sec_key_fname, passphrase, *refSource,
63 target_path, subject, plaintext_version, plaintext_version_parent,
64 refListener.shared_ptr(), plaintext_hash_algo, destination_fname);
65 jau::jni::java_exception_check_and_throw(env, E_FILE_LINE);
66
67 jobject jph = jcipherpack::to_jPackHeader(env, ph);
68
69 return jph;
70 } catch(...) {
72 }
73 return nullptr;
74}
75
76jobject Java_org_cipherpack_Cipherpack_checkSignThenDecrypt1(JNIEnv *env, jclass /*jclazz*/,
77 jobject jsign_pub_keys,
78 jstring jdec_sec_key_fname, jobject jpassphrase,
79 jobject jsource_feed,
80 jobject cpListener,
81 jstring jplaintext_hash_algo,
82 jstring jdestination_fname)
83{
84 try {
85 jau::jni::shared_ptr_ref<jau::io::ByteStream> refSource(env, jsource_feed); // hold until done
86 jau::jni::shared_ptr_ref<cipherpack::CipherpackListener> refListener(env, cpListener); // hold until done
87
88 std::vector<std::string> sign_pub_keys = jau::jni::convert_jlist_string_to_vector(env, jsign_pub_keys);
89 std::string dec_sec_key_fname = jau::jni::from_jstring_to_string(env, jdec_sec_key_fname);
90 jau::io::secure_string passphrase = nullptr != jpassphrase ? jau::jni::from_jbytebuffer_to_sstring(env, jpassphrase) : jau::io::secure_string();
91 std::string plaintext_hash_algo = jau::jni::from_jstring_to_string(env, jplaintext_hash_algo);
92 std::string destination_fname = nullptr != jdestination_fname ? jau::jni::from_jstring_to_string(env, jdestination_fname) : "";
93
94 cipherpack::PackHeader ph = checkSignThenDecrypt(sign_pub_keys, dec_sec_key_fname, passphrase, *refSource,
95 refListener.shared_ptr(), plaintext_hash_algo, destination_fname);
96 jau::jni::java_exception_check_and_throw(env, E_FILE_LINE);
97
98 jobject jph = jcipherpack::to_jPackHeader(env, ph);
99
100 return jph;
101 } catch(...) {
103 }
104 return nullptr;
105}
106
107jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl1(JNIEnv *env, jclass /*jclazz*/, jstring jalgo, jobject jsource_feed) {
108 try {
109 jau::jni::shared_ptr_ref<jau::io::ByteStream> refSource(env, jsource_feed); // hold until done
110 std::string algo = jau::jni::from_jstring_to_string(env, jalgo);
111
112 std::unique_ptr<std::vector<uint8_t>> hash = cipherpack::hash_util::calc(algo, *refSource);
113 if( nullptr == hash ) {
114 return nullptr;
115 }
116 jbyteArray jhash = jau::jni::convert_bytes_to_jbytearray(env, *hash);
117 return jhash;
118 } catch(...) {
120 }
121 return nullptr;
122}
123
124jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl2(JNIEnv *env, jclass /*jclazz*/, jstring jalgo, jstring jpath_or_uri, jlongArray jbytes_hashed, jlong jtimeoutMS) {
125 try {
126 std::string algo = jau::jni::from_jstring_to_string(env, jalgo);
127 std::string path_or_uri = jau::jni::from_jstring_to_string(env, jpath_or_uri);
128 const jau::fraction_i64 timeout = (int64_t)jtimeoutMS * 1_ms;
129
130 if( nullptr == jbytes_hashed ) {
131 throw jau::IllegalArgumentError("bytes_hashed null", E_FILE_LINE);
132 }
133 const size_t bh_size = env->GetArrayLength(jbytes_hashed);
134 if( 1 > bh_size ) {
135 throw jau::IllegalArgumentError("bytes_hashed array size "+std::to_string(bh_size)+" < 1", E_FILE_LINE);
136 }
137 jau::jni::JNICriticalArray<uint64_t, jlongArray> criticalArray(env); // RAII - release
138 uint64_t * bh_ptr = criticalArray.get(jbytes_hashed, criticalArray.Mode::UPDATE_AND_RELEASE);
139 if( nullptr == bh_ptr ) {
140 throw jau::InternalError("GetPrimitiveArrayCritical(address byte array) is null", E_FILE_LINE);
141 }
142
143 std::unique_ptr<std::vector<uint8_t>> hash = cipherpack::hash_util::calc(algo, path_or_uri, *bh_ptr, timeout);
144 if( nullptr == hash ) {
145 return nullptr;
146 }
147 jbyteArray jhash = jau::jni::convert_bytes_to_jbytearray(env, *hash);
148 return jhash;
149 } catch(...) {
151 }
152 return nullptr;
153}
jobject Java_org_cipherpack_Cipherpack_encryptThenSignImpl1(JNIEnv *env, jclass, 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)
jobject Java_org_cipherpack_Cipherpack_checkSignThenDecrypt1(JNIEnv *env, jclass, jobject jsign_pub_keys, jstring jdec_sec_key_fname, jobject jpassphrase, jobject jsource_feed, jobject cpListener, jstring jplaintext_hash_algo, jstring jdestination_fname)
jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl2(JNIEnv *env, jclass, jstring jalgo, jstring jpath_or_uri, jlongArray jbytes_hashed, jlong jtimeoutMS)
jbyteArray Java_org_cipherpack_Cipherpack_00024HashUtil_calcImpl1(JNIEnv *env, jclass, jstring jalgo, jobject jsource_feed)
Cipherpack header less encrypted keys or signatures as described in Cipherpack Data Stream.
#define rethrow_and_raise_java_exception(E)
Re-throw current exception and raise respective java exception using any matching function above.
std::unique_ptr< std::vector< uint8_t > > calc(const std::string_view &algo, jau::io::ByteStream &source) noexcept
Return the calculated hash value using given algo name and byte input stream.
Definition crypto0.cpp:388
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...