Path: blob/master/test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java
66646 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.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 sun.security.pkcs11.SunPKCS11;2425import javax.security.auth.Subject;26import javax.security.auth.callback.Callback;27import javax.security.auth.callback.CallbackHandler;28import javax.security.auth.callback.PasswordCallback;29import javax.security.auth.callback.UnsupportedCallbackException;30import javax.security.auth.login.LoginException;31import java.io.IOException;32import java.lang.ref.WeakReference;33import java.security.*;34import java.util.Iterator;35import java.util.PropertyPermission;36import java.util.ServiceConfigurationError;37import java.util.ServiceLoader;3839import jdk.test.lib.util.ForceGC;4041public class MultipleLogins {42private static final String KS_TYPE = "PKCS11";43private static final int NUM_PROVIDERS = 20;44private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];45static final Policy DEFAULT_POLICY = Policy.getPolicy();4647public static void main(String[] args) throws Exception {48String nssConfig = PKCS11Test.getNssConfig();49if (nssConfig == null) {50// No test framework support yet. Ignore51System.out.println("No NSS config found. Skipping.");52return;53}5455for (int i =0; i < NUM_PROVIDERS; i++) {56// loop to set up test without security manger57providers[i] = (SunPKCS11)PKCS11Test.newPKCS11Provider();58}5960if (args.length > 0) {61Policy.setPolicy(new SimplePolicy());62System.setSecurityManager(new SecurityManager());63}6465for (int i =0; i < NUM_PROVIDERS; i++) {66providers[i] = (SunPKCS11)providers[i].configure(nssConfig);67Security.addProvider(providers[i]);68test(providers[i]);69}7071WeakReference<SunPKCS11>[] weakRef = new WeakReference[NUM_PROVIDERS];72for (int i =0; i < NUM_PROVIDERS; i++) {73weakRef[i] = new WeakReference<>(providers[i]);74providers[i].logout();7576if (i == 0) {77// one provider stays for use with clean up thread78continue;79}8081try {82providers[i].login(new Subject(), new PasswordCallbackHandler());83throw new RuntimeException("Expected LoginException");84} catch (LoginException le) {85// expected86}8788Security.removeProvider(providers[i].getName());89providers[i] = null;9091ForceGC gc = new ForceGC();92int finalI = i;93gc.await(() -> weakRef[finalI].get() == null);94if (!weakRef[i].refersTo(null)) {95throw new RuntimeException("Expected SunPKCS11 Provider to be GC'ed..");96}97}98}99100private static void test(SunPKCS11 p) throws Exception {101KeyStore ks = KeyStore.getInstance(KS_TYPE, p);102p.setCallbackHandler(new PasswordCallbackHandler());103try {104ks.load(null, (char[]) null);105} catch (IOException e) {106if (!e.getMessage().contains("load failed")) {107// we expect the keystore load to fail108throw new RuntimeException("unexpected exception", e);109}110}111112p.logout();113114try {115ks.load(null, (char[]) null);116} catch (IOException e) {117if (e.getCause() instanceof LoginException &&118e.getCause().getMessage().contains("No token present")) {119// expected120} else {121throw new RuntimeException("Token was present", e);122}123}124}125126static final class SimplePolicy extends Policy {127128final Permissions perms = new Permissions();129SimplePolicy() {130perms.add(new PropertyPermission("*", "read, write"));131perms.add(new SecurityPermission("authProvider.*"));132perms.add(new SecurityPermission("insertProvider.*"));133perms.add(new SecurityPermission("removeProvider.*"));134}135136@Override137public boolean implies(ProtectionDomain domain, Permission permission) {138return perms.implies(permission) ||139DEFAULT_POLICY.implies(domain, permission);140}141}142143public static class PasswordCallbackHandler implements CallbackHandler {144public void handle(Callback[] callbacks)145throws IOException, UnsupportedCallbackException {146if (!(callbacks[0] instanceof PasswordCallback)) {147throw new UnsupportedCallbackException(callbacks[0]);148}149PasswordCallback pc = (PasswordCallback)callbacks[0];150pc.setPassword(null);151}152}153}154155156