Skip to content

Commit 6fa2f31

Browse files
Initialize security providers at run time.
Add a test for security provider run time registration. Add entry to a CHANGELOG.md Parse java.security.properties file at run time.
1 parent 272457e commit 6fa2f31

File tree

9 files changed

+468
-289
lines changed

9 files changed

+468
-289
lines changed

substratevm/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This changelog summarizes major changes to GraalVM Native Image.
1616
1. `run-time-initialized-jdk` shifts away from build-time initialization of the JDK, instead initializing most of it at run time. This transition is gradual, with individual components of the JDK becoming run-time initialized in each release. This process should complete with JDK 29 when this option should not be needed anymore. Unless you store classes from the JDK in the image heap, this option should not affect you. In case this option breaks your build, follow the suggestions in the error messages.
1717
* (GR-63494) Recurring callback support is no longer enabled by default. If this feature is needed, please specify `-H:+SupportRecurringCallback` at image build-time.
1818
* (GR-60209) New syntax for configuration of the [Foreign Function & Memory API](https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/ForeignInterface.md)
19+
* (GR-57827) Move the initialization of security providers from build time to runtime.
1920

2021
## GraalVM for JDK 24 (Internal Version 24.2.0)
2122
* (GR-59717) Added `DuringSetupAccess.registerObjectReachabilityHandler` to allow registering a callback that is executed when an object of a specified type is marked as reachable during heap scanning.

substratevm/mx.substratevm/suite.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,12 @@
356356
"sun.reflect.generics.reflectiveObjects",
357357
"sun.reflect.generics.repository",
358358
"sun.reflect.generics.tree",
359+
"sun.security.rsa",
359360
"sun.security.jca",
360361
"sun.security.ssl",
361362
"sun.security.util",
363+
"sun.security.provider",
364+
"com.sun.crypto.provider",
362365
"sun.text.spi",
363366
"sun.util",
364367
"sun.util.locale",
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
/*
2+
* Copyright (c) 2024, 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package com.oracle.svm.core.jdk;
27+
28+
import java.lang.reflect.Constructor;
29+
import java.lang.reflect.InvocationTargetException;
30+
import java.security.Provider;
31+
import java.util.Collections;
32+
import java.util.HashMap;
33+
import java.util.HashSet;
34+
import java.util.List;
35+
import java.util.Map;
36+
import java.util.Properties;
37+
import java.util.Set;
38+
39+
import org.graalvm.nativeimage.ImageSingletons;
40+
import org.graalvm.nativeimage.Platform;
41+
import org.graalvm.nativeimage.Platforms;
42+
43+
import com.oracle.svm.core.util.VMError;
44+
45+
import jdk.graal.compiler.api.replacements.Fold;
46+
47+
/**
48+
* The class that holds various build-time and runtime structures necessary for security providers.
49+
*/
50+
public final class SecurityProvidersSupport {
51+
/**
52+
* A set of providers to be loaded using the service-loading technique at runtime, but not
53+
* discoverable at build-time when processing services in the feature (see
54+
* ServiceLoaderFeature#handleServiceClassIsReachable). This occurs when the user does not
55+
* explicitly request a provider, but the provider is discovered via static analysis from a
56+
* JCA-compliant security service used by the user's code (see
57+
* SecurityServicesFeature#registerServiceReachabilityHandlers).
58+
*/
59+
@Platforms(Platform.HOSTED_ONLY.class)//
60+
private final Set<String> markedAsNotLoaded = Collections.synchronizedSet(new HashSet<>());
61+
62+
/** Set of fully qualified provider names, required for runtime resource access. */
63+
private final Set<String> userRequestedSecurityProviders = Collections.synchronizedSet(new HashSet<>());
64+
65+
/**
66+
* A map of providers, identified by their names (see {@link Provider#getName()}), and the
67+
* results of their verification (see javax.crypto.JceSecurity#getVerificationResult). This
68+
* structure is used instead of the (see javax.crypto.JceSecurity#verifyingProviders) map to
69+
* avoid keeping provider objects in the image heap.
70+
*/
71+
private final Map<String, Object> verifiedSecurityProviders = Collections.synchronizedMap(new HashMap<>());
72+
73+
private Properties savedInitialSecurityProperties;
74+
75+
private Constructor<?> sunECConstructor;
76+
77+
@Platforms(Platform.HOSTED_ONLY.class)
78+
public SecurityProvidersSupport(List<String> userRequestedSecurityProviders) {
79+
this.userRequestedSecurityProviders.addAll(userRequestedSecurityProviders);
80+
}
81+
82+
@Fold
83+
public static SecurityProvidersSupport singleton() {
84+
return ImageSingletons.lookup(SecurityProvidersSupport.class);
85+
}
86+
87+
@Platforms(Platform.HOSTED_ONLY.class)
88+
public void addVerifiedSecurityProvider(String key, Object value) {
89+
verifiedSecurityProviders.put(key, value);
90+
}
91+
92+
public Object getSecurityProviderVerificationResult(String key) {
93+
return verifiedSecurityProviders.get(key);
94+
}
95+
96+
@Platforms(Platform.HOSTED_ONLY.class)
97+
public void markSecurityProviderAsNotLoaded(String provider) {
98+
markedAsNotLoaded.add(provider);
99+
}
100+
101+
@Platforms(Platform.HOSTED_ONLY.class)
102+
public boolean isSecurityProviderNotLoaded(String provider) {
103+
return markedAsNotLoaded.contains(provider);
104+
}
105+
106+
@Platforms(Platform.HOSTED_ONLY.class)
107+
public boolean isUserRequestedSecurityProvider(String provider) {
108+
return userRequestedSecurityProviders.contains(provider);
109+
}
110+
111+
/**
112+
* Returns {@code true} if the provider, identified by either its name (e.g., SUN) or fully
113+
* qualified name (e.g., sun.security.provider.Sun), is either user-requested or reachable via a
114+
* security service.
115+
*/
116+
public boolean isSecurityProviderExpected(String providerName, String providerFQName) {
117+
return verifiedSecurityProviders.containsKey(providerName) || userRequestedSecurityProviders.contains(providerFQName);
118+
}
119+
120+
@Platforms(Platform.HOSTED_ONLY.class)
121+
public void setSunECConstructor(Constructor<?> sunECConstructor) {
122+
this.sunECConstructor = sunECConstructor;
123+
}
124+
125+
public Provider allocateSunECProvider() {
126+
try {
127+
return (Provider) sunECConstructor.newInstance();
128+
} catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
129+
throw VMError.shouldNotReachHere("The SunEC constructor is not present.");
130+
}
131+
}
132+
133+
public void setSavedInitialSecurityProperties(Properties savedSecurityProperties) {
134+
this.savedInitialSecurityProperties = savedSecurityProperties;
135+
}
136+
137+
public Properties getSavedInitialSecurityProperties() {
138+
return savedInitialSecurityProperties;
139+
}
140+
}

0 commit comments

Comments
 (0)