Skip to content

Commit

Permalink
Method for extracting the added alias
Browse files Browse the repository at this point in the history
  • Loading branch information
viniciusxyz committed Jan 13, 2025
1 parent 3fd6481 commit 2a8e576
Showing 1 changed file with 19 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.netty.handler.ssl.SslProvider;

import javax.net.ssl.KeyManagerFactory;
import java.security.Key;
import java.security.KeyStore;
import java.util.Optional;

Expand Down Expand Up @@ -79,7 +80,25 @@ public static KeyManagerFactory storeToFactory(@NonNull SslConfiguration ssl, @N
if (keyPassword == null && pwd.isPresent()) {
keyPassword = pwd.get().toCharArray();
}
if (keyStore != null && ssl.getKey().getAlias().isPresent()) {
keyStore = extractKeystoreAlias(keyStore, ssl.getKey().getAlias().get(), keyPassword);
}
keyManagerFactory.init(keyStore, keyPassword);
return keyManagerFactory;
}

private static KeyStore extractKeystoreAlias(@NonNull KeyStore rootKeystore, @NonNull String alias, @Nullable char[] password) throws Exception {
if (!rootKeystore.containsAlias(alias)) {
throw new IllegalArgumentException("Alias " + alias + " not found in keystore.");
}
Key key = rootKeystore.getKey(alias, password);
if (key == null) {
throw new IllegalStateException("There are no keys associated with the alias " + alias);
}
java.security.cert.Certificate[] certChain = rootKeystore.getCertificateChain(alias);
KeyStore aliasKeystore = KeyStore.getInstance(rootKeystore.getType());
aliasKeystore.load(null, null);
aliasKeystore.setKeyEntry(alias, key, password, certChain);
return aliasKeystore;
}
}

0 comments on commit 2a8e576

Please sign in to comment.