Path: blob/master/test/jdk/sun/security/pkcs11/Provider/MultipleLogins.java
51710 views
/*1* Copyright (c) 2003, 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.KeyStore;34import java.security.Provider;35import java.security.Security;36import java.util.Iterator;37import java.util.ServiceConfigurationError;38import java.util.ServiceLoader;3940import jdk.test.lib.util.ForceGC;4142public class MultipleLogins {43private static final String KS_TYPE = "PKCS11";44private static final int NUM_PROVIDERS = 20;45private static final SunPKCS11[] providers = new SunPKCS11[NUM_PROVIDERS];464748public static void main(String[] args) throws Exception {49for (int i =0; i < NUM_PROVIDERS; i++) {50String nssConfig = PKCS11Test.getNssConfig();51if (nssConfig == null) {52// No test framework support yet. Ignore53System.out.println("No NSS config found. Skipping.");54return;55}56providers[i] =57(SunPKCS11)PKCS11Test.newPKCS11Provider()58.configure(nssConfig);59Security.addProvider(providers[i]);60test(providers[i]);61}6263WeakReference<SunPKCS11>[] weakRef = new WeakReference[NUM_PROVIDERS];64for (int i =0; i < NUM_PROVIDERS; i++) {65weakRef[i] = new WeakReference<>(providers[i]);66providers[i].logout();6768if (i == 0) {69// one provider stays for use with clean up thread70continue;71}7273try {74providers[i].login(new Subject(), new PasswordCallbackHandler());75throw new RuntimeException("Expected LoginException");76} catch (LoginException le) {77// expected78}7980Security.removeProvider(providers[i].getName());81providers[i] = null;8283ForceGC gc = new ForceGC();84int finalI = i;85gc.await(() -> weakRef[finalI].get() == null);86if (!weakRef[i].refersTo(null)) {87throw new RuntimeException("Expected SunPKCS11 Provider to be GC'ed..");88}89}90}9192private static void test(SunPKCS11 p) throws Exception {93KeyStore ks = KeyStore.getInstance(KS_TYPE, p);9495p.setCallbackHandler(new PasswordCallbackHandler());96try {97ks.load(null, (char[]) null);98} catch (IOException e) {99if (!e.getMessage().contains("load failed")) {100// we expect the keystore load to fail101throw new RuntimeException("unexpected exception", e);102}103}104105p.logout();106107try {108ks.load(null, (char[]) null);109} catch (IOException e) {110if (e.getCause() instanceof LoginException &&111e.getCause().getMessage().contains("No token present")) {112// expected113} else {114throw new RuntimeException("Token was present", e);115}116}117}118119public static class PasswordCallbackHandler implements CallbackHandler {120public void handle(Callback[] callbacks)121throws IOException, UnsupportedCallbackException {122if (!(callbacks[0] instanceof PasswordCallback)) {123throw new UnsupportedCallbackException(callbacks[0]);124}125PasswordCallback pc = (PasswordCallback)callbacks[0];126pc.setPassword(null);127}128}129}130131132