Skip to content

Commit 41d23d2

Browse files
authored
TlsManager (helidon-io#7291)
1 parent 707a03b commit 41d23d2

65 files changed

Lines changed: 3469 additions & 489 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bom/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -833,6 +833,11 @@
833833
<artifactId>helidon-integrations-oci-metrics-cdi</artifactId>
834834
<version>${helidon.version}</version>
835835
</dependency>
836+
<dependency>
837+
<groupId>io.helidon.integrations.oci</groupId>
838+
<artifactId>helidon-integrations-oci-tls-certificates</artifactId>
839+
<version>${helidon.version}</version>
840+
</dependency>
836841
<dependency>
837842
<groupId>io.helidon.integrations.vault</groupId>
838843
<artifactId>helidon-integrations-vault</artifactId>

common/key-util/src/main/java/io/helidon/common/pki/PemReader.java

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2022 Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2023 Oracle and/or its affiliates.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -47,7 +47,7 @@
4747
/**
4848
* Reads a PEM file and converts it into a list of DERs so that they are imported into a {@link java.security.KeyStore} easily.
4949
*/
50-
final class PemReader {
50+
public final class PemReader {
5151
private static final System.Logger LOGGER = System.getLogger(PemReader.class.getName());
5252

5353
private static final Pattern CERT_PATTERN = Pattern.compile(
@@ -69,6 +69,54 @@ final class PemReader {
6969
private PemReader() {
7070
}
7171

72+
/**
73+
* Reads a certificate-based input stream and converts it to a list of {@link X509Certificate}s.
74+
*
75+
* @param certStream cert input stream
76+
* @return list of certificates
77+
*/
78+
public static List<X509Certificate> readCertificates(InputStream certStream) {
79+
CertificateFactory cf;
80+
try {
81+
cf = CertificateFactory.getInstance("X.509");
82+
} catch (CertificateException e) {
83+
throw new PkiException("Failed to create certificate factory", e);
84+
}
85+
String content;
86+
try {
87+
content = readContent(certStream);
88+
} catch (IOException e) {
89+
throw new PkiException("Failed to read certificate input stream", e);
90+
} finally {
91+
safeClose(certStream);
92+
}
93+
94+
List<X509Certificate> certs = new ArrayList<>();
95+
Matcher m = CERT_PATTERN.matcher(content);
96+
int start = 0;
97+
while (true) {
98+
if (!m.find(start)) {
99+
break;
100+
}
101+
102+
byte[] base64 = m.group(1).getBytes(StandardCharsets.US_ASCII);
103+
byte[] der = Base64.getMimeDecoder().decode(base64);
104+
try {
105+
certs.add((X509Certificate) cf.generateCertificate(new ByteArrayInputStream(der)));
106+
} catch (Exception e) {
107+
throw new PkiException("Failed to read certificate from bytes", e);
108+
}
109+
110+
start = m.end();
111+
}
112+
113+
if (certs.isEmpty()) {
114+
throw new PkiException("Found no certificates in input stream");
115+
}
116+
117+
return certs;
118+
}
119+
72120
static PublicKey readPublicKey(InputStream input) {
73121
byte[] pkBytes = readPublicKeyBytes(input);
74122

@@ -143,56 +191,13 @@ private static PrivateKey dsaPrivateKey(KeySpec keySpec) {
143191
}
144192

145193
private static PrivateKey rsaPrivateKey(KeySpec keySpec) {
146-
147194
try {
148195
return KeyFactory.getInstance("RSA").generatePrivate(keySpec);
149196
} catch (Exception e) {
150197
throw new PkiException("Failed to get RSA private key", e);
151198
}
152199
}
153200

154-
static List<X509Certificate> readCertificates(InputStream certStream) {
155-
CertificateFactory cf;
156-
try {
157-
cf = CertificateFactory.getInstance("X.509");
158-
} catch (CertificateException e) {
159-
throw new PkiException("Failed to create certificate factory", e);
160-
}
161-
String content;
162-
try {
163-
content = readContent(certStream);
164-
} catch (IOException e) {
165-
throw new PkiException("Failed to read certificate input stream", e);
166-
} finally {
167-
safeClose(certStream);
168-
}
169-
170-
List<X509Certificate> certs = new ArrayList<>();
171-
Matcher m = CERT_PATTERN.matcher(content);
172-
int start = 0;
173-
while (true) {
174-
if (!m.find(start)) {
175-
break;
176-
}
177-
178-
byte[] base64 = m.group(1).getBytes(StandardCharsets.US_ASCII);
179-
byte[] der = Base64.getMimeDecoder().decode(base64);
180-
try {
181-
certs.add((X509Certificate) cf.generateCertificate(new ByteArrayInputStream(der)));
182-
} catch (Exception e) {
183-
throw new PkiException("Failed to read certificate from bytes", e);
184-
}
185-
186-
start = m.end();
187-
}
188-
189-
if (certs.isEmpty()) {
190-
throw new PkiException("Found no certificates in input stream");
191-
}
192-
193-
return certs;
194-
}
195-
196201
private static KeySpec generateKeySpec(byte[] keyBytes, char[] password) {
197202
if (password == null) {
198203
return new PKCS8EncodedKeySpec(keyBytes);

common/tls/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@
6161
<artifactId>helidon-builder-api</artifactId>
6262
<optional>true</optional>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.junit.jupiter</groupId>
66+
<artifactId>junit-jupiter-api</artifactId>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.hamcrest</groupId>
71+
<artifactId>hamcrest-all</artifactId>
72+
<scope>test</scope>
73+
</dependency>
6474
</dependencies>
6575

6676
<build>

0 commit comments

Comments
 (0)