|
| 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.List; |
| 32 | +import java.util.Properties; |
| 33 | +import java.util.Set; |
| 34 | +import java.util.concurrent.ConcurrentHashMap; |
| 35 | + |
| 36 | +import org.graalvm.collections.EconomicMap; |
| 37 | +import org.graalvm.collections.EconomicSet; |
| 38 | +import org.graalvm.nativeimage.ImageSingletons; |
| 39 | +import org.graalvm.nativeimage.Platform; |
| 40 | +import org.graalvm.nativeimage.Platforms; |
| 41 | + |
| 42 | +import com.oracle.svm.core.util.ImageHeapMap; |
| 43 | +import com.oracle.svm.core.util.VMError; |
| 44 | + |
| 45 | +import jdk.graal.compiler.api.replacements.Fold; |
| 46 | +import sun.security.util.Debug; |
| 47 | + |
| 48 | +/** |
| 49 | + * The class that holds various build-time and run-time structures necessary for security providers, |
| 50 | + * but only in case they are initialized at run time (see the <a href= |
| 51 | + * "../../../../../../../../../../../../docs/reference-manual/native-image/JCASecurityServices.md"> |
| 52 | + * JCA Security Services documentation</a> for details). |
| 53 | + */ |
| 54 | +public final class SecurityProvidersSupport { |
| 55 | + /** |
| 56 | + * A set of providers to be loaded using the service-loading technique at runtime, but not |
| 57 | + * discoverable at build-time when processing services in the feature (see |
| 58 | + * ServiceLoaderFeature#handleServiceClassIsReachable). This occurs when the user does not |
| 59 | + * explicitly request a provider, but the provider is discovered via static analysis from a |
| 60 | + * JCA-compliant security service used by the user's code (see |
| 61 | + * SecurityServicesFeature#registerServiceReachabilityHandlers). |
| 62 | + */ |
| 63 | + @Platforms(Platform.HOSTED_ONLY.class)// |
| 64 | + private final Set<String> markedAsNotLoaded = ConcurrentHashMap.newKeySet(); |
| 65 | + |
| 66 | + /** Set of fully qualified provider names, required for runtime resource access. */ |
| 67 | + private final EconomicSet<String> userRequestedSecurityProviders = EconomicSet.create(); |
| 68 | + |
| 69 | + /** |
| 70 | + * A map of providers, identified by their names (see {@link Provider#getName()}), and the |
| 71 | + * results of their verification (see javax.crypto.JceSecurity#getVerificationResult). This |
| 72 | + * structure is used instead of the (see javax.crypto.JceSecurity#verifyingProviders) map to |
| 73 | + * avoid keeping provider objects in the image heap. |
| 74 | + */ |
| 75 | + private final EconomicMap<String, Object> verifiedSecurityProviders = ImageHeapMap.create("verifiedSecurityProviders"); |
| 76 | + |
| 77 | + private Properties savedInitialSecurityProperties; |
| 78 | + |
| 79 | + private Constructor<?> sunECConstructor; |
| 80 | + |
| 81 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 82 | + public SecurityProvidersSupport(List<String> userRequestedSecurityProviders) { |
| 83 | + this.userRequestedSecurityProviders.addAll(userRequestedSecurityProviders); |
| 84 | + } |
| 85 | + |
| 86 | + @Fold |
| 87 | + public static SecurityProvidersSupport singleton() { |
| 88 | + return ImageSingletons.lookup(SecurityProvidersSupport.class); |
| 89 | + } |
| 90 | + |
| 91 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 92 | + public void addVerifiedSecurityProvider(String key, Object verificationResult) { |
| 93 | + verifiedSecurityProviders.put(key, verificationResult); |
| 94 | + } |
| 95 | + |
| 96 | + public Object getSecurityProviderVerificationResult(String key) { |
| 97 | + return verifiedSecurityProviders.get(key); |
| 98 | + } |
| 99 | + |
| 100 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 101 | + public void markSecurityProviderAsNotLoaded(String provider) { |
| 102 | + markedAsNotLoaded.add(provider); |
| 103 | + } |
| 104 | + |
| 105 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 106 | + public boolean isSecurityProviderNotLoaded(String provider) { |
| 107 | + return markedAsNotLoaded.contains(provider); |
| 108 | + } |
| 109 | + |
| 110 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 111 | + public boolean isUserRequestedSecurityProvider(String provider) { |
| 112 | + return userRequestedSecurityProviders.contains(provider); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns {@code true} if the provider, identified by either its name (e.g., SUN) or fully |
| 117 | + * qualified name (e.g., sun.security.provider.Sun), is either user-requested or reachable via a |
| 118 | + * security service. |
| 119 | + */ |
| 120 | + public boolean isSecurityProviderRequested(String providerName, String providerFQName) { |
| 121 | + return verifiedSecurityProviders.containsKey(providerName) || userRequestedSecurityProviders.contains(providerFQName); |
| 122 | + } |
| 123 | + |
| 124 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 125 | + public void setSunECConstructor(Constructor<?> sunECConstructor) { |
| 126 | + this.sunECConstructor = sunECConstructor; |
| 127 | + } |
| 128 | + |
| 129 | + public Provider allocateSunECProvider() { |
| 130 | + try { |
| 131 | + return (Provider) sunECConstructor.newInstance(); |
| 132 | + } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) { |
| 133 | + throw VMError.shouldNotReachHere("The SunEC constructor is not present."); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Platforms(Platform.HOSTED_ONLY.class) |
| 138 | + public void setSavedInitialSecurityProperties(Properties savedSecurityProperties) { |
| 139 | + this.savedInitialSecurityProperties = savedSecurityProperties; |
| 140 | + } |
| 141 | + |
| 142 | + public Properties getSavedInitialSecurityProperties() { |
| 143 | + return savedInitialSecurityProperties; |
| 144 | + } |
| 145 | + |
| 146 | + public Provider loadBuiltInProvider(String provName, Debug debug) { |
| 147 | + return switch (provName) { |
| 148 | + case "SUN", "sun.security.provider.Sun" -> |
| 149 | + isSecurityProviderRequested("SUN", "sun.security.provider.Sun") ? new sun.security.provider.Sun() : null; |
| 150 | + case "SunRsaSign", "sun.security.rsa.SunRsaSign" -> |
| 151 | + isSecurityProviderRequested("SunRsaSign", "sun.security.rsa.SunRsaSign") ? new sun.security.rsa.SunRsaSign() : null; |
| 152 | + case "SunJCE", "com.sun.crypto.provider.SunJCE" -> |
| 153 | + isSecurityProviderRequested("SunJCE", "com.sun.crypto.provider.SunJCE") ? new com.sun.crypto.provider.SunJCE() : null; |
| 154 | + case "SunJSSE" -> |
| 155 | + isSecurityProviderRequested("SunJSSE", "sun.security.ssl.SunJSSE") ? new sun.security.ssl.SunJSSE() : null; |
| 156 | + case "SunEC" -> |
| 157 | + isSecurityProviderRequested("SunEC", "sun.security.ec.SunEC") ? allocateSunECProvider() : null; |
| 158 | + case "Apple", "apple.security.AppleProvider" -> { |
| 159 | + try { |
| 160 | + Class<?> c = Class.forName("apple.security.AppleProvider"); |
| 161 | + if (Provider.class.isAssignableFrom(c)) { |
| 162 | + yield (Provider) c.getDeclaredConstructor().newInstance(); |
| 163 | + } |
| 164 | + } catch (Exception ex) { |
| 165 | + if (debug != null) { |
| 166 | + debug.println("Error loading provider Apple"); |
| 167 | + ex.printStackTrace(); |
| 168 | + } |
| 169 | + } |
| 170 | + yield null; |
| 171 | + } |
| 172 | + default -> null; |
| 173 | + }; |
| 174 | + } |
| 175 | + |
| 176 | + public static boolean isBuiltInProvider(String provName) { |
| 177 | + return switch (provName) { |
| 178 | + case "SUN", "sun.security.provider.Sun", |
| 179 | + "SunRsaSign", "sun.security.rsa.SunRsaSign", |
| 180 | + "SunJCE", "com.sun.crypto.provider.SunJCE", |
| 181 | + "SunJSSE", |
| 182 | + "SunEC", |
| 183 | + "Apple", "apple.security.AppleProvider" -> |
| 184 | + true; |
| 185 | + default -> false; |
| 186 | + }; |
| 187 | + } |
| 188 | +} |
0 commit comments