Path: blob/master/test/jdk/sun/security/pkcs11/Provider/Login.java
51712 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*/2223/* @test24* @bug 485042325* @summary login facilities for hardware tokens26* @library /test/lib ..27* @run testng/othervm -Djava.security.manager=allow Login28*/2930import org.testng.annotations.BeforeClass;31import org.testng.annotations.Test;3233import java.io.*;34import java.nio.file.Path;35import java.security.*;36import javax.security.auth.callback.*;3738import javax.security.auth.Subject;39import javax.security.auth.login.FailedLoginException;4041public class Login extends PKCS11Test {4243private static final String KS_TYPE = "PKCS11";44private static char[] password;4546@BeforeClass47public void setUp() throws Exception {48copyNssCertKeyToClassesDir();49setCommonSystemProps();50System.setProperty("CUSTOM_P11_CONFIG",51Path.of(BASE).resolve("Login-nss.txt").toString());52}5354@Test55public void testLogin() throws Exception {56String[] args = new String[]{ "sm", "Login.policy"};57main(new Login(), args);58}5960public void main(Provider p) throws Exception {6162int testnum = 1;6364KeyStore ks = KeyStore.getInstance(KS_TYPE, p);6566// check instance67if (ks.getProvider() instanceof AuthProvider ap) {68System.out.println("keystore provider instance of AuthProvider");69System.out.println("test " + testnum++ + " passed");70} else {71throw new SecurityException("did not get AuthProvider KeyStore");72}7374try {7576// test app-provided callback77System.out.println("*** enter [foo] as the password ***");78password = new char[] { 'f', 'o', 'o' };7980ap.login(new Subject(), new PasswordCallbackHandler());81ap.logout();82throw new SecurityException("test failed, expected LoginException");83} catch (FailedLoginException fle) {84System.out.println("test " + testnum++ + " passed");85}8687try {8889// test default callback90System.out.println("*** enter [foo] as the password ***");91password = new char[] { 'f', 'o', 'o' };9293Security.setProperty("auth.login.defaultCallbackHandler",94"Login$PasswordCallbackHandler");95ap.login(new Subject(), null);96ap.logout();97throw new SecurityException("test failed, expected LoginException");98} catch (FailedLoginException fle) {99System.out.println("test " + testnum++ + " passed");100}101102// test provider-set callback103System.out.println("*** enter test12 (correct) password ***");104password = new char[] { 't', 'e', 's', 't', '1', '2' };105106Security.setProperty("auth.login.defaultCallbackHandler", "");107ap.setCallbackHandler(new PasswordCallbackHandler());108ap.login(new Subject(), null);109System.out.println("test " + testnum++ + " passed");110111// test user already logged in112ap.setCallbackHandler(null);113ap.login(new Subject(), null);114System.out.println("test " + testnum++ + " passed");115116// logout117ap.logout();118119// call KeyStore.load with a NULL password, and get prompted for PIN120ap.setCallbackHandler(new PasswordCallbackHandler());121ks.load(null, (char[])null);122System.out.println("test " + testnum++ + " passed");123}124125public static class PasswordCallbackHandler implements CallbackHandler {126public void handle(Callback[] callbacks)127throws IOException, UnsupportedCallbackException {128if (!(callbacks[0] instanceof PasswordCallback pc)) {129throw new UnsupportedCallbackException(callbacks[0]);130}131pc.setPassword(Login.password);132}133}134}135136137