Cipherpack v1.2.0-dirty
A Cryprographic Stream Processor
PackHeader.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 */
24package org.cipherpack;
25
26import java.time.Instant;
27import java.time.ZoneOffset;
28import java.time.ZonedDateTime;
29import java.util.ArrayList;
30import java.util.List;
31
32import org.jau.util.BasicTypes;
33
34/**
35 * Cipherpack header less encrypted keys or signatures as described in @ref cipherpack_stream "Cipherpack Data Stream"
36 *
37 * @see @ref cipherpack_overview "Cipherpack Overview"
38 * @see @ref cipherpack_stream "Cipherpack Data Stream"
39 */
40public class PackHeader {
41 /** Designated target path for this plaintext message, see @ref cipherpack_stream "Cipherpack Data Stream". */
42 public final String target_path;
43
44 /** Plaintext message size in bytes, zero if not determined yet. See @ref cipherpack_stream "Cipherpack Data Stream". */
45 public final long plaintext_size;
46
47 /** Creation time since Unix epoch, second component, see @ref cipherpack_stream "Cipherpack Data Stream". */
48 public final long ts_creation_sec;
49
50 /** Creation time since Unix epoch, nanosecond component, see @ref cipherpack_stream "Cipherpack Data Stream". */
51 public final long ts_creation_nsec;
52
53 /** Designated subject of message, see @ref cipherpack_stream "Cipherpack Data Stream". */
54 public final String subject;
55
56 /** Version of this plaintext message, user semantic, see @ref cipherpack_stream "Cipherpack Data Stream". */
57 public final String plaintext_version;
58
59 /** Version of this plaintext message's preceding message, user semantic, see @ref cipherpack_stream "Cipherpack Data Stream". */
60 public final String plaintext_version_parent;
61
63
64 /** Sender's public-key fingerprint used to sign, see @ref cipherpack_stream "Cipherpack Data Stream".. */
65 public final byte[] sender_fingerprint;
66
67 /** List of receiver's public-keys fingerprints used to encrypt the symmetric-key, see @ref cipherpack_stream "Cipherpack Data Stream". */
68 public final List<byte[]> recevr_fingerprints;
69
70 /** Index of the matching receiver's public-key fingerprint used to decrypt the symmetric-key, see @ref cipherpack_stream "Cipherpack Data Stream", -1 if not found or not decrypting. */
71 public final int used_recevr_key_idx;
72
73 /**
74 * Optional hash algorithm for the plaintext message, produced for convenience and not wired.
75 *
76 * If not used, {@link #plaintext_hash_algo} is empty.
77 */
78 public final String plaintext_hash_algo;
79
80 /**
81 * Optional hash value of the plaintext message, produced for convenience and not wired.
82 *
83 * If not used, i.e. {@link #plaintext_hash_algo} is empty, array has zero size.
84 */
85 public final byte[] plaintext_hash;
86
87 /** True if packet is valid, otherwise false. */
88 public final boolean valid;
89
90 PackHeader() {
91 this.target_path = "";
92 this.plaintext_size = 0;
93 this.ts_creation_sec = 0;
94 this.ts_creation_nsec = 0;
95 this.subject = "";
96 this.plaintext_version = "0";
97 this.plaintext_version_parent = "0";
98 this.crypto_cfg = new CryptoConfig();
99 this.sender_fingerprint = new byte[0];
100 this.recevr_fingerprints = new ArrayList<byte[]>();
101 this.used_recevr_key_idx = -1;
102 this.plaintext_hash_algo = "";
103 this.plaintext_hash = new byte[0];
104 this.valid = false;
105 }
106
107 PackHeader(final String target_path_,
108 final long plaintext_size_,
109 final long ts_creation_sec_,
110 final long ts_creation_nsec_,
111 final String subject_,
112 final String pversion, final String pversion_parent,
113 final CryptoConfig crypto_cfg_,
114 final byte[] sender_key_fingerprint_,
115 final List<byte[]> recevr_fingerprint_,
116 final int used_recevr_key_idx_,
117 final String plaintext_hash_algo_,
118 final byte[] plaintext_hash_,
119 final boolean valid_) {
120 this.target_path = target_path_;
121 this.plaintext_size = plaintext_size_;
122 this.ts_creation_sec = ts_creation_sec_;
123 this.ts_creation_nsec = ts_creation_nsec_;
124 this.subject = subject_;
125 this.plaintext_version = pversion;
126 this.plaintext_version_parent = pversion_parent;
127 this.crypto_cfg = crypto_cfg_;
128 this.sender_fingerprint = sender_key_fingerprint_;
129 this.recevr_fingerprints = recevr_fingerprint_;
130 this.used_recevr_key_idx = used_recevr_key_idx_;
131 this.plaintext_hash_algo = plaintext_hash_algo_;
132 this.plaintext_hash = plaintext_hash_;
133 this.valid = valid_;
134 }
135
136 /**
137 * Return a string representation
138 * @param show_crypto_algos pass true if used crypto algos shall be shown, otherwise suppressed (default).
139 * @param force_all_fingerprints if true always show all getTermKeysFingerprint(), otherwise show only the getTermKeysFingerprint() if >= 0 (default).
140 * @return string representation
141 */
142 public String toString(final boolean show_crypto_algos, final boolean force_all_fingerprints) {
143 final String crypto_str = show_crypto_algos ? ", "+crypto_cfg.toString() : "";
144
145 final StringBuilder recevr_fingerprint = new StringBuilder();
146 {
147 if( 0 <= used_recevr_key_idx ) {
148 final byte[] fp = recevr_fingerprints.get(used_recevr_key_idx);
149 recevr_fingerprint.append( "dec '").append(BasicTypes.bytesHexString(fp, 0, fp.length, true /* lsb */)).append("', ");
150 }
151 if( force_all_fingerprints || 0 > used_recevr_key_idx ) {
152 recevr_fingerprint.append("enc[");
153 int i = 0;
154 for(final byte[] tkf : recevr_fingerprints) {
155 if( 0 < i ) {
156 recevr_fingerprint.append(", ");
157 }
158 recevr_fingerprint.append("'").append(BasicTypes.bytesHexString(tkf, 0, tkf.length, true /* lsb */)).append("'");
159 ++i;
160 }
161 recevr_fingerprint.append("]");
162 }
163 }
164 final ZonedDateTime utc_creation = Instant.ofEpochSecond(ts_creation_sec, ts_creation_nsec).atZone(ZoneOffset.UTC);
165 final String res = "Header[valid "+valid+
166 ", file[target_path '"+target_path+"', plaintext_size "+String.format("%,d", plaintext_size)+
167 "], creation "+utc_creation.toString()+" , subject '"+subject+"', "+
168 " version['"+plaintext_version+
169 "', parent '"+plaintext_version_parent+"']"+crypto_str+
170 ", fingerprints[sender '"+BasicTypes.bytesHexString(sender_fingerprint, 0, sender_fingerprint.length, true /* lsb */)+
171 "', recevr["+recevr_fingerprint+
172 "]], phash['"+plaintext_hash_algo+"', sz "+plaintext_hash.length+"]]";
173 return res;
174 }
175
176 public final boolean isValid() { return valid; }
177
178 /**
179 * Return a string representation
180 * @return string representation
181 */
182 @Override
183 public String toString() {
184 return toString(false, false);
185 }
186}
CryptoConfig, contains crypto algorithms settings given at encryption wired via the Cipherpack Data S...
Cipherpack header less encrypted keys or signatures as described in Cipherpack Data Stream.
Definition: PackHeader.java:40
final long ts_creation_sec
Creation time since Unix epoch, second component, see Cipherpack Data Stream.
Definition: PackHeader.java:48
final CryptoConfig crypto_cfg
Definition: PackHeader.java:62
String toString(final boolean show_crypto_algos, final boolean force_all_fingerprints)
Return a string representation.
final boolean isValid()
final long plaintext_size
Plaintext message size in bytes, zero if not determined yet.
Definition: PackHeader.java:45
final byte[] sender_fingerprint
Sender's public-key fingerprint used to sign, see Cipherpack Data Stream.
Definition: PackHeader.java:65
final String plaintext_version
Version of this plaintext message, user semantic, see Cipherpack Data Stream.
Definition: PackHeader.java:57
final String plaintext_version_parent
Version of this plaintext message's preceding message, user semantic, see Cipherpack Data Stream.
Definition: PackHeader.java:60
final String target_path
Designated target path for this plaintext message, see Cipherpack Data Stream.
Definition: PackHeader.java:42
final boolean valid
True if packet is valid, otherwise false.
Definition: PackHeader.java:88
String toString()
Return a string representation.
final List< byte[]> recevr_fingerprints
List of receiver's public-keys fingerprints used to encrypt the symmetric-key, see Cipherpack Data St...
Definition: PackHeader.java:68
final String plaintext_hash_algo
Optional hash algorithm for the plaintext message, produced for convenience and not wired.
Definition: PackHeader.java:78
final byte[] plaintext_hash
Optional hash value of the plaintext message, produced for convenience and not wired.
Definition: PackHeader.java:85
final String subject
Designated subject of message, see Cipherpack Data Stream.
Definition: PackHeader.java:54
final long ts_creation_nsec
Creation time since Unix epoch, nanosecond component, see Cipherpack Data Stream.
Definition: PackHeader.java:51
final int used_recevr_key_idx
Index of the matching receiver's public-key fingerprint used to decrypt the symmetric-key,...
Definition: PackHeader.java:71