Skip to content

Commit

Permalink
Support for SunPKCS11 provider in native image
Browse files Browse the repository at this point in the history
  • Loading branch information
sberyozkin committed Apr 22, 2022
1 parent 3ac2669 commit 6d85f44
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,9 +121,10 @@ void produceJcaSecurityProviders(BuildProducer<JCAProviderBuildItem> jcaProvider
*/
@BuildStep
void registerJCAProvidersForReflection(BuildProducer<ReflectiveClassBuildItem> classes,
List<JCAProviderBuildItem> jcaProviders) throws IOException, URISyntaxException {
List<JCAProviderBuildItem> jcaProviders,
BuildProducer<NativeImageSecurityProviderBuildItem> additionalProviders) throws IOException, URISyntaxException {
for (JCAProviderBuildItem provider : jcaProviders) {
List<String> providerClasses = registerProvider(provider.getProviderName());
List<String> providerClasses = registerProvider(provider.getProviderName(), additionalProviders);
for (String className : providerClasses) {
classes.produce(new ReflectiveClassBuildItem(true, true, className));
log.debugf("Register JCA class: %s", className);
Expand Down Expand Up @@ -352,7 +353,8 @@ private <BI extends MultiBuildItem> Optional<BI> getOne(List<BI> items) {
* @param providerName - JCA provider name
* @return class names that make up the provider and its services
*/
private List<String> registerProvider(String providerName) {
private List<String> registerProvider(String providerName,
BuildProducer<NativeImageSecurityProviderBuildItem> additionalProviders) {
List<String> providerClasses = new ArrayList<>();
Provider provider = Security.getProvider(providerName);
if (provider != null) {
Expand All @@ -366,6 +368,11 @@ private List<String> registerProvider(String providerName) {
}
}
}

if (SecurityProviderUtils.SUN_PROVIDERS.containsKey(providerName)) {
additionalProviders.produce(
new NativeImageSecurityProviderBuildItem(SecurityProviderUtils.SUN_PROVIDERS.get(providerName)));
}
return providerClasses;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.lang.reflect.Constructor;
import java.security.Provider;
import java.security.Security;
import java.util.Map;

import io.quarkus.runtime.configuration.ConfigurationException;

Expand All @@ -18,10 +19,16 @@ public final class SecurityProviderUtils {
public static final String BOUNCYCASTLE_JSSE_PROVIDER_CLASS_NAME = "org.bouncycastle.jsse.provider.BouncyCastleJsseProvider";
public static final String BOUNCYCASTLE_FIPS_PROVIDER_CLASS_NAME = "org.bouncycastle.jcajce.provider.BouncyCastleFipsProvider";

public static final Map<String, String> SUN_PROVIDERS = Map.of("SunPKCS11", "sun.security.pkcs11.SunPKCS11");

private SecurityProviderUtils() {

}

public static void addProvider(String provider) {
addProvider(loadProvider(provider));
}

public static void addProvider(Provider provider) {
try {
if (Security.getProvider(provider.getName()) == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public class BouncyCastleEndpoint {
@Path("listProviders")
public String listProviders() {
return Arrays.asList(Security.getProviders()).stream()
.filter(p -> p.getName().equals("BC"))
.map(p -> p.getName()).collect(Collectors.joining());
.filter(p -> (p.getName().equals("BC") || p.getName().equals("SunPKCS11")))
.map(p -> p.getName()).collect(Collectors.joining(","));
}

@GET
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
quarkus.security.security-providers=BC
quarkus.security.security-providers=BC,SunPKCS11
quarkus.native.additional-build-args=-H:IncludeResources=.*\\.pem
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void testListProviders() {
.get("/jca/listProviders")
.then()
.statusCode(200)
.body(equalTo("BC"));
.body(equalTo("SunPKCS11,BC"));
}

@Test
Expand Down
1 change: 0 additions & 1 deletion integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,6 @@
<module>logging-panache</module>
<module>locales</module>
<module>redis-devservices</module>

<!-- gRPC tests -->
<module>grpc-tls</module>
<module>grpc-plain-text-gzip</module>
Expand Down

0 comments on commit 6d85f44

Please sign in to comment.