001// Generated by delombok at Sat Feb 25 17:58:34 UTC 2023
002package io.cui.tools.net.ssl;
003
004import java.io.Serializable;
005import java.util.Base64;
006import lombok.NonNull;
007
008/**
009 * Runtime representation of key-material. The actual key is provided as byte[]. An optional
010 * keyPassword is available as well.
011 *
012 * @author Oliver Wolff
013 */
014public final class KeyMaterialHolder implements Serializable {
015    private static final long serialVersionUID = -3125499798220509939L;
016    @NonNull
017    private final byte[] keyMaterial;
018    /**
019     * (Optional) password for the key, or in case of {@link KeyHolderType#KEY_STORE} for the store.
020     */
021    private final String keyPassword;
022    /**
023     * Optional: An arbitrary name for displaying the key in an ui or logging context.
024     */
025    private final String name;
026    /**
027     * Optional: additional information transporting some context-information.
028     */
029    private final String description;
030    /**
031     * Optional: An alias name for a given key.
032     */
033    private final String keyAlias;
034    // owolff: False positive: This is input for @Builder, no
035    // constant, especially not public
036    @SuppressWarnings("squid:S1170")
037    private final KeyHolderType keyHolderType;
038    // owolff: False positive: This is input for @Builder, no
039    // constant, especially not public
040    @SuppressWarnings("squid:S1170")
041    private final KeyAlgorithm keyAlgorithm;
042
043    /**
044     * @return NPE-safe char-array representation of {@link #getKeyPassword()}. If keyPassword is
045     *         {@code null} or empty it returns an empty char[], never {@code null}
046     */
047    public char[] getKeyPasswordAsCharArray() {
048        return KeyStoreProvider.toCharArray(keyPassword);
049    }
050
051    /**
052     * @param serializedKeyMaterial the Base64 encoded key material
053     *
054     * @return Raw i.e. original key material
055     * @throws IllegalArgumentException if serializedKeyMaterial is not Base64 encoded
056     */
057    public static byte[] deserializeKeyMaterial(final String serializedKeyMaterial) {
058        return Base64.getDecoder().decode(serializedKeyMaterial);
059    }
060
061    /**
062     * @param keyMaterial Raw i.e. original key material
063     *
064     * @return Base64 encoded key material
065     */
066    public static String serializeKeyMaterial(final byte[] keyMaterial) {
067        return Base64.getEncoder().encodeToString(keyMaterial);
068    }
069
070    @java.lang.SuppressWarnings("all")
071    @lombok.Generated
072    private static KeyHolderType $default$keyHolderType() {
073        return KeyHolderType.SINGLE_KEY;
074    }
075
076    @java.lang.SuppressWarnings("all")
077    @lombok.Generated
078    private static KeyAlgorithm $default$keyAlgorithm() {
079        return KeyAlgorithm.UNDEFINED;
080    }
081
082    @java.lang.SuppressWarnings("all")
083    @lombok.Generated
084    KeyMaterialHolder(@NonNull final byte[] keyMaterial, final String keyPassword, final String name, final String description, final String keyAlias, final KeyHolderType keyHolderType, final KeyAlgorithm keyAlgorithm) {
085        if (keyMaterial == null) {
086            throw new java.lang.NullPointerException("keyMaterial is marked non-null but is null");
087        }
088        this.keyMaterial = keyMaterial;
089        this.keyPassword = keyPassword;
090        this.name = name;
091        this.description = description;
092        this.keyAlias = keyAlias;
093        this.keyHolderType = keyHolderType;
094        this.keyAlgorithm = keyAlgorithm;
095    }
096
097
098    @java.lang.SuppressWarnings("all")
099    @lombok.Generated
100    public static class KeyMaterialHolderBuilder {
101        @java.lang.SuppressWarnings("all")
102        @lombok.Generated
103        private byte[] keyMaterial;
104        @java.lang.SuppressWarnings("all")
105        @lombok.Generated
106        private String keyPassword;
107        @java.lang.SuppressWarnings("all")
108        @lombok.Generated
109        private String name;
110        @java.lang.SuppressWarnings("all")
111        @lombok.Generated
112        private String description;
113        @java.lang.SuppressWarnings("all")
114        @lombok.Generated
115        private String keyAlias;
116        @java.lang.SuppressWarnings("all")
117        @lombok.Generated
118        private boolean keyHolderType$set;
119        @java.lang.SuppressWarnings("all")
120        @lombok.Generated
121        private KeyHolderType keyHolderType$value;
122        @java.lang.SuppressWarnings("all")
123        @lombok.Generated
124        private boolean keyAlgorithm$set;
125        @java.lang.SuppressWarnings("all")
126        @lombok.Generated
127        private KeyAlgorithm keyAlgorithm$value;
128
129        @java.lang.SuppressWarnings("all")
130        @lombok.Generated
131        KeyMaterialHolderBuilder() {
132        }
133
134        /**
135         * @return {@code this}.
136         */
137        @java.lang.SuppressWarnings("all")
138        @lombok.Generated
139        public KeyMaterialHolder.KeyMaterialHolderBuilder keyMaterial(@NonNull final byte[] keyMaterial) {
140            if (keyMaterial == null) {
141                throw new java.lang.NullPointerException("keyMaterial is marked non-null but is null");
142            }
143            this.keyMaterial = keyMaterial;
144            return this;
145        }
146
147        /**
148         * (Optional) password for the key, or in case of {@link KeyHolderType#KEY_STORE} for the store.
149         * @return {@code this}.
150         */
151        @java.lang.SuppressWarnings("all")
152        @lombok.Generated
153        public KeyMaterialHolder.KeyMaterialHolderBuilder keyPassword(final String keyPassword) {
154            this.keyPassword = keyPassword;
155            return this;
156        }
157
158        /**
159         * Optional: An arbitrary name for displaying the key in an ui or logging context.
160         * @return {@code this}.
161         */
162        @java.lang.SuppressWarnings("all")
163        @lombok.Generated
164        public KeyMaterialHolder.KeyMaterialHolderBuilder name(final String name) {
165            this.name = name;
166            return this;
167        }
168
169        /**
170         * Optional: additional information transporting some context-information.
171         * @return {@code this}.
172         */
173        @java.lang.SuppressWarnings("all")
174        @lombok.Generated
175        public KeyMaterialHolder.KeyMaterialHolderBuilder description(final String description) {
176            this.description = description;
177            return this;
178        }
179
180        /**
181         * Optional: An alias name for a given key.
182         * @return {@code this}.
183         */
184        @java.lang.SuppressWarnings("all")
185        @lombok.Generated
186        public KeyMaterialHolder.KeyMaterialHolderBuilder keyAlias(final String keyAlias) {
187            this.keyAlias = keyAlias;
188            return this;
189        }
190
191        /**
192         * @return {@code this}.
193         */
194        @java.lang.SuppressWarnings("all")
195        @lombok.Generated
196        public KeyMaterialHolder.KeyMaterialHolderBuilder keyHolderType(final KeyHolderType keyHolderType) {
197            this.keyHolderType$value = keyHolderType;
198            keyHolderType$set = true;
199            return this;
200        }
201
202        /**
203         * @return {@code this}.
204         */
205        @java.lang.SuppressWarnings("all")
206        @lombok.Generated
207        public KeyMaterialHolder.KeyMaterialHolderBuilder keyAlgorithm(final KeyAlgorithm keyAlgorithm) {
208            this.keyAlgorithm$value = keyAlgorithm;
209            keyAlgorithm$set = true;
210            return this;
211        }
212
213        @java.lang.SuppressWarnings("all")
214        @lombok.Generated
215        public KeyMaterialHolder build() {
216            KeyHolderType keyHolderType$value = this.keyHolderType$value;
217            if (!this.keyHolderType$set) keyHolderType$value = KeyMaterialHolder.$default$keyHolderType();
218            KeyAlgorithm keyAlgorithm$value = this.keyAlgorithm$value;
219            if (!this.keyAlgorithm$set) keyAlgorithm$value = KeyMaterialHolder.$default$keyAlgorithm();
220            return new KeyMaterialHolder(this.keyMaterial, this.keyPassword, this.name, this.description, this.keyAlias, keyHolderType$value, keyAlgorithm$value);
221        }
222
223        @java.lang.Override
224        @java.lang.SuppressWarnings("all")
225        @lombok.Generated
226        public java.lang.String toString() {
227            return "KeyMaterialHolder.KeyMaterialHolderBuilder(keyMaterial=" + java.util.Arrays.toString(this.keyMaterial) + ", keyPassword=" + this.keyPassword + ", name=" + this.name + ", description=" + this.description + ", keyAlias=" + this.keyAlias + ", keyHolderType$value=" + this.keyHolderType$value + ", keyAlgorithm$value=" + this.keyAlgorithm$value + ")";
228        }
229    }
230
231    @java.lang.SuppressWarnings("all")
232    @lombok.Generated
233    public static KeyMaterialHolder.KeyMaterialHolderBuilder builder() {
234        return new KeyMaterialHolder.KeyMaterialHolderBuilder();
235    }
236
237    @java.lang.Override
238    @java.lang.SuppressWarnings("all")
239    @lombok.Generated
240    public boolean equals(final java.lang.Object o) {
241        if (o == this) return true;
242        if (!(o instanceof KeyMaterialHolder)) return false;
243        final KeyMaterialHolder other = (KeyMaterialHolder) o;
244        final java.lang.Object this$name = this.name;
245        final java.lang.Object other$name = other.name;
246        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
247        final java.lang.Object this$description = this.description;
248        final java.lang.Object other$description = other.description;
249        if (this$description == null ? other$description != null : !this$description.equals(other$description)) return false;
250        final java.lang.Object this$keyAlias = this.keyAlias;
251        final java.lang.Object other$keyAlias = other.keyAlias;
252        if (this$keyAlias == null ? other$keyAlias != null : !this$keyAlias.equals(other$keyAlias)) return false;
253        final java.lang.Object this$keyHolderType = this.keyHolderType;
254        final java.lang.Object other$keyHolderType = other.keyHolderType;
255        if (this$keyHolderType == null ? other$keyHolderType != null : !this$keyHolderType.equals(other$keyHolderType)) return false;
256        final java.lang.Object this$keyAlgorithm = this.keyAlgorithm;
257        final java.lang.Object other$keyAlgorithm = other.keyAlgorithm;
258        if (this$keyAlgorithm == null ? other$keyAlgorithm != null : !this$keyAlgorithm.equals(other$keyAlgorithm)) return false;
259        return true;
260    }
261
262    @java.lang.Override
263    @java.lang.SuppressWarnings("all")
264    @lombok.Generated
265    public int hashCode() {
266        final int PRIME = 59;
267        int result = 1;
268        final java.lang.Object $name = this.name;
269        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
270        final java.lang.Object $description = this.description;
271        result = result * PRIME + ($description == null ? 43 : $description.hashCode());
272        final java.lang.Object $keyAlias = this.keyAlias;
273        result = result * PRIME + ($keyAlias == null ? 43 : $keyAlias.hashCode());
274        final java.lang.Object $keyHolderType = this.keyHolderType;
275        result = result * PRIME + ($keyHolderType == null ? 43 : $keyHolderType.hashCode());
276        final java.lang.Object $keyAlgorithm = this.keyAlgorithm;
277        result = result * PRIME + ($keyAlgorithm == null ? 43 : $keyAlgorithm.hashCode());
278        return result;
279    }
280
281    @java.lang.Override
282    @java.lang.SuppressWarnings("all")
283    @lombok.Generated
284    public java.lang.String toString() {
285        return "KeyMaterialHolder(name=" + this.name + ", description=" + this.description + ", keyAlias=" + this.keyAlias + ", keyHolderType=" + this.keyHolderType + ", keyAlgorithm=" + this.keyAlgorithm + ")";
286    }
287
288    @NonNull
289    @java.lang.SuppressWarnings("all")
290    @lombok.Generated
291    public byte[] getKeyMaterial() {
292        return this.keyMaterial;
293    }
294
295    /**
296     * (Optional) password for the key, or in case of {@link KeyHolderType#KEY_STORE} for the store.
297     */
298    @java.lang.SuppressWarnings("all")
299    @lombok.Generated
300    public String getKeyPassword() {
301        return this.keyPassword;
302    }
303
304    /**
305     * Optional: An arbitrary name for displaying the key in an ui or logging context.
306     */
307    @java.lang.SuppressWarnings("all")
308    @lombok.Generated
309    public String getName() {
310        return this.name;
311    }
312
313    /**
314     * Optional: additional information transporting some context-information.
315     */
316    @java.lang.SuppressWarnings("all")
317    @lombok.Generated
318    public String getDescription() {
319        return this.description;
320    }
321
322    /**
323     * Optional: An alias name for a given key.
324     */
325    @java.lang.SuppressWarnings("all")
326    @lombok.Generated
327    public String getKeyAlias() {
328        return this.keyAlias;
329    }
330
331    @java.lang.SuppressWarnings("all")
332    @lombok.Generated
333    public KeyHolderType getKeyHolderType() {
334        return this.keyHolderType;
335    }
336
337    @java.lang.SuppressWarnings("all")
338    @lombok.Generated
339    public KeyAlgorithm getKeyAlgorithm() {
340        return this.keyAlgorithm;
341    }
342}