1 /*
   2  * Copyright (c) 2002, 2018, 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 jdk.internal.access;
  27 
  28 import javax.crypto.SealedObject;
  29 import java.io.ObjectInputFilter;
  30 import java.lang.module.ModuleDescriptor;
  31 import java.util.ResourceBundle;
  32 import java.util.jar.JarFile;
  33 import java.io.Console;
  34 import java.io.FileDescriptor;
  35 import java.io.FilePermission;
  36 import java.io.ObjectInputStream;
  37 import java.io.RandomAccessFile;
  38 import java.security.ProtectionDomain;
  39 import jdk.internal.misc.Unsafe;
  40 
  41 /** A repository of "shared secrets", which are a mechanism for
  42     calling implementation-private methods in another package without
  43     using reflection. A package-private class implements a public
  44     interface and provides the ability to call package-private methods
  45     within that package; the object implementing that interface is
  46     provided through a third package to which access is restricted.
  47     This framework avoids the primary disadvantage of using reflection
  48     for this purpose, namely the loss of compile-time checking. */
  49 
  50 public class SharedSecrets {
  51     private static final Unsafe unsafe = Unsafe.getUnsafe();
  52     private static JavaUtilJarAccess javaUtilJarAccess;
  53     private static JavaLangAccess javaLangAccess;
  54     private static JavaLangModuleAccess javaLangModuleAccess;
  55     private static JavaLangInvokeAccess javaLangInvokeAccess;
  56     private static JavaLangRefAccess javaLangRefAccess;
  57     private static JavaIOAccess javaIOAccess;
  58     private static JavaNetInetAddressAccess javaNetInetAddressAccess;
  59     private static JavaNetHttpCookieAccess javaNetHttpCookieAccess;
  60     private static JavaNetSocketAccess javaNetSocketAccess;
  61     private static JavaNetUriAccess javaNetUriAccess;
  62     private static JavaNetURLAccess javaNetURLAccess;
  63     private static JavaNetURLClassLoaderAccess javaNetURLClassLoaderAccess;
  64     private static JavaNioAccess javaNioAccess;
  65     private static JavaIOFileDescriptorAccess javaIOFileDescriptorAccess;
  66     private static JavaIOFilePermissionAccess javaIOFilePermissionAccess;
  67     private static JavaSecurityAccess javaSecurityAccess;
  68     private static JavaUtilZipFileAccess javaUtilZipFileAccess;
  69     private static JavaUtilResourceBundleAccess javaUtilResourceBundleAccess;
  70     private static JavaAWTAccess javaAWTAccess;
  71     private static JavaAWTFontAccess javaAWTFontAccess;
  72     private static JavaBeansAccess javaBeansAccess;
  73     private static JavaObjectInputStreamAccess javaObjectInputStreamAccess;
  74     private static JavaObjectInputFilterAccess javaObjectInputFilterAccess;
  75     private static JavaIORandomAccessFileAccess javaIORandomAccessFileAccess;
  76     private static JavaxCryptoSealedObjectAccess javaxCryptoSealedObjectAccess;
  77 
  78     public static JavaUtilJarAccess javaUtilJarAccess() {
  79         if (javaUtilJarAccess == null) {
  80             // Ensure JarFile is initialized; we know that that class
  81             // provides the shared secret
  82             unsafe.ensureClassInitialized(JarFile.class);
  83         }
  84         return javaUtilJarAccess;
  85     }
  86 
  87     public static void setJavaUtilJarAccess(JavaUtilJarAccess access) {
  88         javaUtilJarAccess = access;
  89     }
  90 
  91     public static void setJavaLangAccess(JavaLangAccess jla) {
  92         javaLangAccess = jla;
  93     }
  94 
  95     public static JavaLangAccess getJavaLangAccess() {
  96         return javaLangAccess;
  97     }
  98 
  99     public static void setJavaLangInvokeAccess(JavaLangInvokeAccess jlia) {
 100         javaLangInvokeAccess = jlia;
 101     }
 102 
 103     public static JavaLangInvokeAccess getJavaLangInvokeAccess() {
 104         if (javaLangInvokeAccess == null) {
 105             try {
 106                 Class<?> c = Class.forName("java.lang.invoke.MethodHandleImpl");
 107                 unsafe.ensureClassInitialized(c);
 108             } catch (ClassNotFoundException e) {};
 109         }
 110         return javaLangInvokeAccess;
 111     }
 112 
 113     public static void setJavaLangModuleAccess(JavaLangModuleAccess jlrma) {
 114         javaLangModuleAccess = jlrma;
 115     }
 116 
 117     public static JavaLangModuleAccess getJavaLangModuleAccess() {
 118         if (javaLangModuleAccess == null) {
 119             unsafe.ensureClassInitialized(ModuleDescriptor.class);
 120         }
 121         return javaLangModuleAccess;
 122     }
 123 
 124     public static void setJavaLangRefAccess(JavaLangRefAccess jlra) {
 125         javaLangRefAccess = jlra;
 126     }
 127 
 128     public static JavaLangRefAccess getJavaLangRefAccess() {
 129         return javaLangRefAccess;
 130     }
 131 
 132     public static void setJavaNetUriAccess(JavaNetUriAccess jnua) {
 133         javaNetUriAccess = jnua;
 134     }
 135 
 136     public static JavaNetUriAccess getJavaNetUriAccess() {
 137         if (javaNetUriAccess == null)
 138             unsafe.ensureClassInitialized(java.net.URI.class);
 139         return javaNetUriAccess;
 140     }
 141 
 142     public static void setJavaNetURLAccess(JavaNetURLAccess jnua) {
 143         javaNetURLAccess = jnua;
 144     }
 145 
 146     public static JavaNetURLAccess getJavaNetURLAccess() {
 147         if (javaNetURLAccess == null)
 148             unsafe.ensureClassInitialized(java.net.URL.class);
 149         return javaNetURLAccess;
 150     }
 151 
 152     public static void setJavaNetURLClassLoaderAccess(JavaNetURLClassLoaderAccess jnua) {
 153         javaNetURLClassLoaderAccess = jnua;
 154     }
 155 
 156     public static JavaNetURLClassLoaderAccess getJavaNetURLClassLoaderAccess() {
 157         if (javaNetURLClassLoaderAccess == null)
 158             unsafe.ensureClassInitialized(java.net.URLClassLoader.class);
 159         return javaNetURLClassLoaderAccess;
 160     }
 161 
 162     public static void setJavaNetInetAddressAccess(JavaNetInetAddressAccess jna) {
 163         javaNetInetAddressAccess = jna;
 164     }
 165 
 166     public static JavaNetInetAddressAccess getJavaNetInetAddressAccess() {
 167         if (javaNetInetAddressAccess == null)
 168             unsafe.ensureClassInitialized(java.net.InetAddress.class);
 169         return javaNetInetAddressAccess;
 170     }
 171 
 172     public static void setJavaNetHttpCookieAccess(JavaNetHttpCookieAccess a) {
 173         javaNetHttpCookieAccess = a;
 174     }
 175 
 176     public static JavaNetHttpCookieAccess getJavaNetHttpCookieAccess() {
 177         if (javaNetHttpCookieAccess == null)
 178             unsafe.ensureClassInitialized(java.net.HttpCookie.class);
 179         return javaNetHttpCookieAccess;
 180     }
 181 
 182     public static void setJavaNetSocketAccess(JavaNetSocketAccess jnsa) {
 183         javaNetSocketAccess = jnsa;
 184     }
 185 
 186     public static JavaNetSocketAccess getJavaNetSocketAccess() {
 187         if (javaNetSocketAccess == null)
 188             unsafe.ensureClassInitialized(java.net.ServerSocket.class);
 189         return javaNetSocketAccess;
 190     }
 191 
 192     public static void setJavaNioAccess(JavaNioAccess jna) {
 193         javaNioAccess = jna;
 194     }
 195 
 196     public static JavaNioAccess getJavaNioAccess() {
 197         if (javaNioAccess == null) {
 198             // Ensure java.nio.Buffer is initialized, which provides the
 199             // shared secret.
 200             unsafe.ensureClassInitialized(java.nio.Buffer.class);
 201         }
 202         return javaNioAccess;
 203     }
 204 
 205     public static void setJavaIOAccess(JavaIOAccess jia) {
 206         javaIOAccess = jia;
 207     }
 208 
 209     public static JavaIOAccess getJavaIOAccess() {
 210         if (javaIOAccess == null) {
 211             unsafe.ensureClassInitialized(Console.class);
 212         }
 213         return javaIOAccess;
 214     }
 215 
 216     public static void setJavaIOFileDescriptorAccess(JavaIOFileDescriptorAccess jiofda) {
 217         javaIOFileDescriptorAccess = jiofda;
 218     }
 219 
 220     public static JavaIOFilePermissionAccess getJavaIOFilePermissionAccess() {
 221         if (javaIOFilePermissionAccess == null)
 222             unsafe.ensureClassInitialized(FilePermission.class);
 223 
 224         return javaIOFilePermissionAccess;
 225     }
 226 
 227     public static void setJavaIOFilePermissionAccess(JavaIOFilePermissionAccess jiofpa) {
 228         javaIOFilePermissionAccess = jiofpa;
 229     }
 230 
 231     public static JavaIOFileDescriptorAccess getJavaIOFileDescriptorAccess() {
 232         if (javaIOFileDescriptorAccess == null)
 233             unsafe.ensureClassInitialized(FileDescriptor.class);
 234 
 235         return javaIOFileDescriptorAccess;
 236     }
 237 
 238     public static void setJavaSecurityAccess(JavaSecurityAccess jsa) {
 239         javaSecurityAccess = jsa;
 240     }
 241 
 242     public static JavaSecurityAccess getJavaSecurityAccess() {
 243         if (javaSecurityAccess == null) {
 244             unsafe.ensureClassInitialized(ProtectionDomain.class);
 245         }
 246         return javaSecurityAccess;
 247     }
 248 
 249     public static JavaUtilZipFileAccess getJavaUtilZipFileAccess() {
 250         if (javaUtilZipFileAccess == null)
 251             unsafe.ensureClassInitialized(java.util.zip.ZipFile.class);
 252         return javaUtilZipFileAccess;
 253     }
 254 
 255     public static void setJavaUtilZipFileAccess(JavaUtilZipFileAccess access) {
 256         javaUtilZipFileAccess = access;
 257     }
 258 
 259     public static void setJavaAWTAccess(JavaAWTAccess jaa) {
 260         javaAWTAccess = jaa;
 261     }
 262 
 263     public static JavaAWTAccess getJavaAWTAccess() {
 264         // this may return null in which case calling code needs to
 265         // provision for.
 266         return javaAWTAccess;
 267     }
 268 
 269     public static void setJavaAWTFontAccess(JavaAWTFontAccess jafa) {
 270         javaAWTFontAccess = jafa;
 271     }
 272 
 273     public static JavaAWTFontAccess getJavaAWTFontAccess() {
 274         // this may return null in which case calling code needs to
 275         // provision for.
 276         return javaAWTFontAccess;
 277     }
 278 
 279     public static JavaBeansAccess getJavaBeansAccess() {
 280         return javaBeansAccess;
 281     }
 282 
 283     public static void setJavaBeansAccess(JavaBeansAccess access) {
 284         javaBeansAccess = access;
 285     }
 286 
 287     public static JavaUtilResourceBundleAccess getJavaUtilResourceBundleAccess() {
 288         if (javaUtilResourceBundleAccess == null)
 289             unsafe.ensureClassInitialized(ResourceBundle.class);
 290         return javaUtilResourceBundleAccess;
 291     }
 292 
 293     public static void setJavaUtilResourceBundleAccess(JavaUtilResourceBundleAccess access) {
 294         javaUtilResourceBundleAccess = access;
 295     }
 296 
 297     public static JavaObjectInputStreamAccess getJavaObjectInputStreamAccess() {
 298         if (javaObjectInputStreamAccess == null) {
 299             unsafe.ensureClassInitialized(ObjectInputStream.class);
 300         }
 301         return javaObjectInputStreamAccess;
 302     }
 303 
 304     public static void setJavaObjectInputStreamAccess(JavaObjectInputStreamAccess access) {
 305         javaObjectInputStreamAccess = access;
 306     }
 307 
 308     public static JavaObjectInputFilterAccess getJavaObjectInputFilterAccess() {
 309         if (javaObjectInputFilterAccess == null) {
 310             unsafe.ensureClassInitialized(ObjectInputFilter.Config.class);
 311         }
 312         return javaObjectInputFilterAccess;
 313     }
 314 
 315     public static void setJavaObjectInputFilterAccess(JavaObjectInputFilterAccess access) {
 316         javaObjectInputFilterAccess = access;
 317     }
 318 
 319     public static void setJavaIORandomAccessFileAccess(JavaIORandomAccessFileAccess jirafa) {
 320         javaIORandomAccessFileAccess = jirafa;
 321     }
 322 
 323     public static JavaIORandomAccessFileAccess getJavaIORandomAccessFileAccess() {
 324         if (javaIORandomAccessFileAccess == null) {
 325             unsafe.ensureClassInitialized(RandomAccessFile.class);
 326         }
 327         return javaIORandomAccessFileAccess;
 328     }
 329 
 330     public static void setJavaxCryptoSealedObjectAccess(JavaxCryptoSealedObjectAccess jcsoa) {
 331         javaxCryptoSealedObjectAccess = jcsoa;
 332     }
 333 
 334     public static JavaxCryptoSealedObjectAccess getJavaxCryptoSealedObjectAccess() {
 335         if (javaxCryptoSealedObjectAccess == null) {
 336             unsafe.ensureClassInitialized(SealedObject.class);
 337         }
 338         return javaxCryptoSealedObjectAccess;
 339     }
 340 }