Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/security/pkcs11/Provider/Login.java
38855 views
/*1* Copyright (c) 2003, 2004, 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 java.io.*;24import java.util.*;25import java.lang.reflect.*;26import java.security.*;27import javax.security.auth.callback.*;2829import javax.security.auth.Subject;30import javax.security.auth.login.FailedLoginException;3132public class Login extends PKCS11Test {3334private static final String KS_TYPE = "PKCS11";35private static char[] password;3637public static void main(String[] args) throws Exception {38main(new Login());39}4041public void main(Provider p) throws Exception {4243int testnum = 1;4445KeyStore ks = KeyStore.getInstance(KS_TYPE, p);4647// check instance48if (ks.getProvider() instanceof java.security.AuthProvider) {49System.out.println("keystore provider instance of AuthProvider");50System.out.println("test " + testnum++ + " passed");51} else {52throw new SecurityException("did not get AuthProvider KeyStore");53}5455AuthProvider ap = (AuthProvider)ks.getProvider();56try {5758// test app-provided callback59System.out.println("*** enter [foo] as the password ***");60password = new char[] { 'f', 'o', 'o' };6162ap.login(new Subject(), new PasswordCallbackHandler());63ap.logout();64throw new SecurityException("test failed, expected LoginException");65} catch (FailedLoginException fle) {66System.out.println("test " + testnum++ + " passed");67}6869try {7071// test default callback72System.out.println("*** enter [foo] as the password ***");73password = new char[] { 'f', 'o', 'o' };7475Security.setProperty("auth.login.defaultCallbackHandler",76"Login$PasswordCallbackHandler");77ap.login(new Subject(), null);78ap.logout();79throw new SecurityException("test failed, expected LoginException");80} catch (FailedLoginException fle) {81System.out.println("test " + testnum++ + " passed");82}8384// test provider-set callback85System.out.println("*** enter test12 (correct) password ***");86password = new char[] { 't', 'e', 's', 't', '1', '2' };8788Security.setProperty("auth.login.defaultCallbackHandler", "");89ap.setCallbackHandler90(new com.sun.security.auth.callback.DialogCallbackHandler());91ap.setCallbackHandler(new PasswordCallbackHandler());92ap.login(new Subject(), null);93System.out.println("test " + testnum++ + " passed");9495// test user already logged in96ap.setCallbackHandler(null);97ap.login(new Subject(), null);98System.out.println("test " + testnum++ + " passed");99100// logout101ap.logout();102103// call KeyStore.load with a NULL password, and get prompted for PIN104ap.setCallbackHandler(new PasswordCallbackHandler());105ks.load(null, (char[])null);106System.out.println("test " + testnum++ + " passed");107}108109public static class PasswordCallbackHandler implements CallbackHandler {110public void handle(Callback[] callbacks)111throws IOException, UnsupportedCallbackException {112if (!(callbacks[0] instanceof PasswordCallback)) {113throw new UnsupportedCallbackException(callbacks[0]);114}115PasswordCallback pc = (PasswordCallback)callbacks[0];116pc.setPassword(Login.password);117}118}119}120121122