Path: blob/master/test/jdk/sun/security/pkcs11/KeyAgreement/IllegalPackageAccess.java
41155 views
/*1* Copyright (c) 2021, Red Hat, Inc.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.security.AllPermission;24import java.security.KeyFactory;25import java.security.KeyPair;26import java.security.KeyPairGenerator;27import java.security.Permission;28import java.security.PermissionCollection;29import java.security.Permissions;30import java.security.Policy;31import java.security.ProtectionDomain;32import java.security.Provider;33import java.security.Security;34import java.security.spec.X509EncodedKeySpec;3536/*37* @test38* @bug 825931939* @library /test/lib ..40* @run main/othervm -Djava.security.manager=allow IllegalPackageAccess41*/4243public class IllegalPackageAccess extends PKCS11Test {4445private static Policy policy = Policy.getPolicy();46private static RuntimePermission accessPerm =47new RuntimePermission("accessClassInPackage.com.sun.crypto.provider");4849private static class MyPolicy extends Policy {50@Override51public PermissionCollection getPermissions(ProtectionDomain domain) {52PermissionCollection perms = new Permissions();53perms.add(new AllPermission());54return perms;55}5657@Override58public boolean implies(ProtectionDomain domain, Permission permission) {59if (permission.equals(accessPerm)) {60return policy.implies(domain, permission);61}62return super.implies(domain, permission);63}64}6566public static void main(String[] args) throws Exception {67main(new IllegalPackageAccess(), args);68System.out.println("TEST PASS - OK");69}7071@Override72public void main(Provider p) throws Exception {73Policy.setPolicy(null);74Policy.setPolicy(new MyPolicy());75System.setSecurityManager(new SecurityManager());7677// Remove all security providers so a fallback scheme78// that creates class instances is forced.79for (Provider provider : Security.getProviders()) {80Security.removeProvider(provider.getName());81}8283KeyPair kp = KeyPairGenerator.getInstance("DH", p)84.generateKeyPair();85byte[] encPubKey = kp.getPublic().getEncoded();86KeyFactory kf = KeyFactory.getInstance("DH", p);8788// Requires access to a SunJCE class that parses89// the encoded key.90kf.generatePublic(new X509EncodedKeySpec(encPubKey));9192System.setSecurityManager(null);93Policy.setPolicy(policy);94}9596}979899