Cipherpack v1.2.0-dirty
A Cryprographic Stream Processor
Cipherpack.java
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
25import java.nio.ByteBuffer;
26import java.util.ArrayList;
27import java.util.List;
28
29import org.cipherpack.CPFactory;
30import org.cipherpack.CipherpackListener;
31import org.cipherpack.CryptoConfig;
32import org.cipherpack.PackHeader;
33import org.jau.io.ByteInStream;
34import org.jau.io.ByteInStreamUtil;
35import org.jau.io.PrintUtil;
36
37/**
38 * @deprecated The Java commandline toolkit is deprecated, please use the native commandline toolkit `cipherpack` from `commandline.cpp`.
39 */
40@Deprecated
41public class Cipherpack {
42
43 static void print_usage() {
44 PrintUtil.println(System.err, "Usage: pack [-epk <enc-pub-key>]+ -ssk <sign-sec-key> -sskp <sign-sec-key-passphrase> -in <input-source> -target_path <target-path-filename> "+
45 "-intention <string> -version <file-version-str> -version_parent <file-version-parent-str> -phash <payload-hash-algo> -out <output-filename>");
46 PrintUtil.println(System.err, "Usage: unpack [-spk <sign-pub-key>]+ -dsk <dec-sec-key> -dskp <dec-sec-key-passphrase> -in <input-source> -phash <payload-hash-algo> -out <output-filename>");
47 }
48
49 public static void main(final String[] args) throws InterruptedException {
50 final int argc = args.length;
51 for(int i=0; i< argc; i++) {
52 final String arg = args[i];
53 if( arg.equals("-cp_debug") && args.length > (i+1) ) {
54 System.setProperty("cipherpack.debug", args[++i]);
55 } else if( arg.equals("-cp_verbose") && args.length > (i+1) ) {
56 System.setProperty("cipherpack.verbose", args[++i]);
57 }
58 }
60
61 PrintUtil.println(System.err, "Cipherpack initialized!");
62 PrintUtil.println(System.err, "Cipherpack Native Version "+CPFactory.getNativeVersion()+" (API "+CPFactory.getNativeAPIVersion()+")");
63 PrintUtil.println(System.err, "Cipherpack Java Version "+CPFactory.getImplVersion()+" (API "+CPFactory.getAPIVersion()+")");
64
65 PrintUtil.fprintf_td(System.err, "Called with %d arguments: ", argc);
66 for(int i=0; i<argc; i++) {
67 PrintUtil.fprintf_td(System.err, "%s ", args[i]);
68 }
69 PrintUtil.fprintf_td(System.err, "\n");
70 int argi = 0;
71
72 if( 1 >= argc ) {
73 print_usage();
74 return;
75 }
76 final String command = args[++argi];
77
78 if( command.equals( "pack" ) ) {
79 final List<String> enc_pub_keys = new ArrayList<String>();
80 String sign_sec_key_fname = new String();
81 final ByteBuffer sign_sec_key_passphrase = null;
82 String source_name = new String();
83 String target_path = new String();
84 String intention = new String();
85 String payload_version = "0";
86 String payload_version_parent = "0";
87 String payload_hash_algo = org.cipherpack.Cipherpack.default_hash_algo();
88 String fname_output = new String();
89 for(int i=argi; i + 1 < argc; ++i) {
90 final String arg = args[i];
91
92 if( arg.equals("-epk") ) {
93 enc_pub_keys.add( args[++i] );
94 } else if( arg.equals("-ssk") ) {
95 sign_sec_key_fname = args[++i];
96 } else if( arg.equals("-sskp") ) {
97 // FIXME
98 // sign_sec_key_passphrase = args[++i];
99 } else if( arg.equals("-in") ) {
100 source_name = args[++i];
101 } else if( arg.equals("-target_path") ) {
102 target_path = args[++i];
103 } else if( arg.equals("-intention") ) {
104 intention = args[++i];
105 } else if( arg.equals("-version") ) {
106 payload_version = args[++i];
107 } else if( arg.equals("-version_parent") ) {
108 payload_version_parent = args[++i];
109 } else if( arg.equals("-phash") ) {
110 payload_hash_algo = args[++i];
111 } else if( arg.equals("-out") ) {
112 fname_output = args[++i];
113 }
114 }
115 if( 0 == enc_pub_keys.size() ||
116 sign_sec_key_fname.isEmpty() ||
117 source_name.isEmpty() ||
118 target_path.isEmpty() ||
119 fname_output.isEmpty() )
120 {
121 PrintUtil.fprintf_td(System.err, "Pack: Error: Arguments incomplete\n");
122 print_usage();
123 return;
124 }
125
126 final ByteInStream source = ByteInStreamUtil.to_ByteInStream(source_name); // 20_s default
127 final PackHeader ph = org.cipherpack.Cipherpack.encryptThenSign(
129 enc_pub_keys, sign_sec_key_fname, sign_sec_key_passphrase,
130 source,
131 target_path, intention,
132 payload_version, payload_version_parent,
133 new CipherpackListener(), payload_hash_algo, fname_output);
134
135 PrintUtil.fprintf_td(System.err, "Pack: Encrypted %s to %s\n", source_name, fname_output);
136 PrintUtil.fprintf_td(System.err, "Pack: %s\n", ph.toString(true, true));
137 return;
138 }
139 if( command == "unpack") {
140 final List<String> sign_pub_keys = new ArrayList<String>();
141 String dec_sec_key_fname = new String();
142 final ByteBuffer dec_sec_key_passphrase = null;
143 String source_name = new String();
144 String payload_hash_algo = org.cipherpack.Cipherpack.default_hash_algo();
145 String fname_output = new String();
146 for(int i=argi; i + 1 < argc; ++i) {
147 final String arg = args[i];
148
149 if( arg.equals("-spk") ) {
150 sign_pub_keys.add( args[++i] );
151 } else if( arg.equals("-dsk") ) {
152 dec_sec_key_fname = args[++i];
153 } else if( arg.equals("-dskp") ) {
154 // FIXME
155 // dec_sec_key_passphrase = args[++i];
156 } else if( arg.equals("-in") ) {
157 source_name = args[++i];
158 } else if( arg.equals("-phash") ) {
159 payload_hash_algo = args[++i];
160 } else if( arg.equals("-out") ) {
161 fname_output = args[++i];
162 }
163 }
164 if( 0 == sign_pub_keys.size() ||
165 dec_sec_key_fname.isEmpty() ||
166 source_name.isEmpty() ||
167 fname_output.isEmpty() )
168 {
169 PrintUtil.fprintf_td(System.err, "Unpack: Error: Arguments incomplete\n");
170 print_usage();
171 return;
172 }
173
174 final ByteInStream source = ByteInStreamUtil.to_ByteInStream(source_name); // 20_s default
175 final PackHeader ph = org.cipherpack.Cipherpack.checkSignThenDecrypt(
176 sign_pub_keys, dec_sec_key_fname, dec_sec_key_passphrase,
177 source,
178 new CipherpackListener(), payload_hash_algo, fname_output);
179
180 // dec_sec_key_passphrase.resize(0);
181 PrintUtil.fprintf_td(System.err, "Unpack: Decypted %s to %s\n", source_name, fname_output);
182 PrintUtil.fprintf_td(System.err, "Unpack: %s\n", ph.toString(true, true));
183 return;
184 }
185 PrintUtil.fprintf_td(System.err, "Pack: Error: Unknown command\n");
186 print_usage();
187 return;
188 }
189
190}
static void main(final String[] args)
Definition: Cipherpack.java:49
Cipherpack Factory, called by automatically to load the native library.
Definition: CPFactory.java:47
static native String getNativeVersion()
static native String getNativeAPIVersion()
static void checkInitialized()
Definition: CPFactory.java:269
static final String getImplVersion()
Manifest's Attributes.Name#IMPLEMENTATION_VERSION or null if not available.
Definition: CPFactory.java:58
static final String getAPIVersion()
Manifest's Attributes.Name#SPECIFICATION_VERSION or null if not available.
Definition: CPFactory.java:52
Listener for events occurring while processing a cipherpack message via encryptThenSign() and checkSi...
CryptoConfig, contains crypto algorithms settings given at encryption wired via the Cipherpack Data S...
static CryptoConfig getDefault()
Returns default CryptoConfig.
Cipherpack header less encrypted keys or signatures as described in Cipherpack Data Stream.
Definition: PackHeader.java:40
String toString(final boolean show_crypto_algos, final boolean force_all_fingerprints)
Return a string representation.