|
| 1 | +/* |
| 2 | + * Copyright (c) 2021, Red Hat, Inc. |
| 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. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.security.AllPermission; |
| 25 | +import java.security.KeyFactory; |
| 26 | +import java.security.KeyPair; |
| 27 | +import java.security.KeyPairGenerator; |
| 28 | +import java.security.Permission; |
| 29 | +import java.security.PermissionCollection; |
| 30 | +import java.security.Permissions; |
| 31 | +import java.security.Policy; |
| 32 | +import java.security.ProtectionDomain; |
| 33 | +import java.security.Provider; |
| 34 | +import java.security.Security; |
| 35 | +import java.security.spec.X509EncodedKeySpec; |
| 36 | + |
| 37 | +/* |
| 38 | + * @test |
| 39 | + * @bug 8259319 |
| 40 | + * @library /test/lib .. |
| 41 | + * @run main/othervm IllegalPackageAccess |
| 42 | + */ |
| 43 | + |
| 44 | +public class IllegalPackageAccess extends PKCS11Test { |
| 45 | + |
| 46 | + private static Policy policy = Policy.getPolicy(); |
| 47 | + private static RuntimePermission accessPerm = |
| 48 | + new RuntimePermission("accessClassInPackage.com.sun.crypto.provider"); |
| 49 | + |
| 50 | + private static class MyPolicy extends Policy { |
| 51 | + @Override |
| 52 | + public PermissionCollection getPermissions(ProtectionDomain domain) { |
| 53 | + PermissionCollection perms = new Permissions(); |
| 54 | + perms.add(new AllPermission()); |
| 55 | + return perms; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public boolean implies(ProtectionDomain domain, Permission permission) { |
| 60 | + if (permission.equals(accessPerm)) { |
| 61 | + return policy.implies(domain, permission); |
| 62 | + } |
| 63 | + return super.implies(domain, permission); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + public static void main(String[] args) throws Exception { |
| 68 | + main(new IllegalPackageAccess(), args); |
| 69 | + System.out.println("TEST PASS - OK"); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void main(Provider p) throws Exception { |
| 74 | + Policy.setPolicy(new MyPolicy()); |
| 75 | + System.setSecurityManager(new SecurityManager()); |
| 76 | + |
| 77 | + // Remove all security providers so a fallback scheme |
| 78 | + // that creates class instances is forced. |
| 79 | + for (Provider provider : Security.getProviders()) { |
| 80 | + Security.removeProvider(provider.getName()); |
| 81 | + } |
| 82 | + |
| 83 | + KeyPair kp = KeyPairGenerator.getInstance("DH", p) |
| 84 | + .generateKeyPair(); |
| 85 | + byte[] encPubKey = kp.getPublic().getEncoded(); |
| 86 | + KeyFactory kf = KeyFactory.getInstance("DH", p); |
| 87 | + |
| 88 | + // Requires access to a SunJCE class that parses |
| 89 | + // the encoded key. |
| 90 | + kf.generatePublic(new X509EncodedKeySpec(encPubKey)); |
| 91 | + |
| 92 | + System.setSecurityManager(null); |
| 93 | + Policy.setPolicy(policy); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments