|
| 1 | +/** |
| 2 | + * Copyright (c) 2016-2023 Deephaven Data Labs and Patent Pending |
| 3 | + */ |
| 4 | +package io.deephaven.ssl.config; |
| 5 | + |
| 6 | +import io.deephaven.annotations.BuildableStyle; |
| 7 | +import org.immutables.value.Value.Immutable; |
| 8 | + |
| 9 | +import java.io.BufferedInputStream; |
| 10 | +import java.io.ByteArrayInputStream; |
| 11 | +import java.io.IOException; |
| 12 | +import java.io.InputStream; |
| 13 | +import java.nio.file.Files; |
| 14 | +import java.nio.file.Path; |
| 15 | +import java.security.cert.Certificate; |
| 16 | +import java.security.cert.CertificateException; |
| 17 | +import java.security.cert.CertificateFactory; |
| 18 | +import java.util.List; |
| 19 | + |
| 20 | +/** |
| 21 | + * A custom trust configuration provided via {@link Certificate}. This is not currently deserializable via JSON. |
| 22 | + */ |
| 23 | +@Immutable |
| 24 | +@BuildableStyle |
| 25 | +public abstract class TrustCustom extends TrustBase { |
| 26 | + private static final String X_509 = "X.509"; |
| 27 | + |
| 28 | + public static Builder builder() { |
| 29 | + return ImmutableTrustCustom.builder(); |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Creates a trust from the given {@code factory} and {@code in}. Equivalent to |
| 34 | + * {@code builder().addAllCertificates(factory.generateCertificates(in)).build()}. |
| 35 | + * |
| 36 | + * @param factory the certificate factory |
| 37 | + * @param in the input stream |
| 38 | + * @return the trust |
| 39 | + * @throws IOException if an IO exception occurs |
| 40 | + * @throws CertificateException on parsing errors |
| 41 | + */ |
| 42 | + public static TrustCustom of(CertificateFactory factory, InputStream in) throws IOException, CertificateException { |
| 43 | + return builder().addAllCertificates(factory.generateCertificates(in)).build(); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Creates a trust from the given {@code factory} and {@code path}. |
| 48 | + * |
| 49 | + * @param factory the certificate factory |
| 50 | + * @param path the path |
| 51 | + * @return the trust |
| 52 | + * @throws IOException if an IO exception occurs |
| 53 | + * @throws CertificateException on parsing errors |
| 54 | + */ |
| 55 | + public static TrustCustom of(CertificateFactory factory, Path path) throws IOException, CertificateException { |
| 56 | + try (final InputStream in = new BufferedInputStream(Files.newInputStream(path))) { |
| 57 | + return of(factory, in); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Creates a trust from the given {@code factory} and {@code in}. Equivalent to |
| 63 | + * {@code of(factory, new ByteArrayInputStream(in, offset, length))}. |
| 64 | + * |
| 65 | + * @param factory the certificate factory |
| 66 | + * @param in the input bytes |
| 67 | + * @param offset the input offset |
| 68 | + * @param length the input length |
| 69 | + * @return the trust |
| 70 | + * @throws IOException if an IO exception occurs |
| 71 | + * @throws CertificateException on parsing errors |
| 72 | + * @see ByteArrayInputStream#ByteArrayInputStream(byte[], int, int) |
| 73 | + * @see #of(CertificateFactory, InputStream). |
| 74 | + */ |
| 75 | + public static TrustCustom of(CertificateFactory factory, byte[] in, int offset, int length) |
| 76 | + throws IOException, CertificateException { |
| 77 | + return of(factory, new ByteArrayInputStream(in, offset, length)); |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Creates an X509 trust from the given {@code in}. Equivalent to |
| 82 | + * {@code of(CertificateFactory.getInstance("X.509"), in)}. |
| 83 | + * |
| 84 | + * @param in the input stream |
| 85 | + * @return the trust |
| 86 | + * @throws IOException if an IO exception occurs |
| 87 | + * @throws CertificateException on parsing errors |
| 88 | + * @see CertificateFactory#getInstance(String) |
| 89 | + * @see #of(CertificateFactory, InputStream) |
| 90 | + */ |
| 91 | + public static TrustCustom ofX509(InputStream in) throws CertificateException, IOException { |
| 92 | + return of(CertificateFactory.getInstance(X_509), in); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Creates an X509 trust from the given {@code path}. Equivalent to |
| 97 | + * {@code of(CertificateFactory.getInstance("X.509"), path)}. |
| 98 | + * |
| 99 | + * @param path the path |
| 100 | + * @return the trust |
| 101 | + * @throws IOException if an IO exception occurs |
| 102 | + * @throws CertificateException on parsing errors |
| 103 | + * @see CertificateFactory#getInstance(String) |
| 104 | + * @see #of(CertificateFactory, Path) |
| 105 | + */ |
| 106 | + public static TrustCustom ofX509(Path path) throws CertificateException, IOException { |
| 107 | + return of(CertificateFactory.getInstance(X_509), path); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Creates an X509 trust from the given {@code in}. Equivalent to |
| 112 | + * {@code of(CertificateFactory.getInstance("X.509"), in, offset, length)}. |
| 113 | + * |
| 114 | + * @param in the input bytes |
| 115 | + * @param offset the input offset |
| 116 | + * @param length the input length |
| 117 | + * @return the trust |
| 118 | + * @throws IOException if an IO exception occurs |
| 119 | + * @throws CertificateException on parsing errors |
| 120 | + * @see CertificateFactory#getInstance(String) |
| 121 | + * @see #of(CertificateFactory, byte[], int, int) |
| 122 | + */ |
| 123 | + public static TrustCustom ofX509(byte[] in, int offset, int length) throws CertificateException, IOException { |
| 124 | + return of(CertificateFactory.getInstance(X_509), in, offset, length); |
| 125 | + } |
| 126 | + |
| 127 | + // This is structurally more specific than what we could achieve with TrustList. There's potential in the future to |
| 128 | + // extend this with Function<KeyStore, CertPathTrustManagerParameters> if callers need more configurability. |
| 129 | + |
| 130 | + public abstract List<Certificate> certificates(); |
| 131 | + |
| 132 | + @Override |
| 133 | + public final <T> T walk(Visitor<T> visitor) { |
| 134 | + return visitor.visit(this); |
| 135 | + } |
| 136 | + |
| 137 | + public interface Builder { |
| 138 | + |
| 139 | + Builder addCertificates(Certificate element); |
| 140 | + |
| 141 | + Builder addCertificates(Certificate... elements); |
| 142 | + |
| 143 | + Builder addAllCertificates(Iterable<? extends Certificate> elements); |
| 144 | + |
| 145 | + TrustCustom build(); |
| 146 | + } |
| 147 | +} |
0 commit comments