J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2003-2004 Sun Microsystems, Inc. All Rights Reserved. |
| 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
| 19 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 20 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 21 | * have any questions. |
| 22 | */ |
| 23 | |
| 24 | import java.io.*; |
| 25 | import java.util.*; |
| 26 | import java.lang.reflect.*; |
| 27 | import java.security.*; |
| 28 | import javax.security.auth.callback.*; |
| 29 | |
| 30 | import javax.security.auth.Subject; |
| 31 | import javax.security.auth.login.FailedLoginException; |
| 32 | |
| 33 | public class Login extends PKCS11Test { |
| 34 | |
| 35 | private static final String KS_TYPE = "PKCS11"; |
| 36 | private static char[] password; |
| 37 | |
| 38 | public static void main(String[] args) throws Exception { |
| 39 | main(new Login()); |
| 40 | } |
| 41 | |
| 42 | public void main(Provider p) throws Exception { |
| 43 | |
| 44 | int testnum = 1; |
| 45 | |
| 46 | KeyStore ks = KeyStore.getInstance(KS_TYPE, p); |
| 47 | |
| 48 | // check instance |
| 49 | if (ks.getProvider() instanceof java.security.AuthProvider) { |
| 50 | System.out.println("keystore provider instance of AuthProvider"); |
| 51 | System.out.println("test " + testnum++ + " passed"); |
| 52 | } else { |
| 53 | throw new SecurityException("did not get AuthProvider KeyStore"); |
| 54 | } |
| 55 | |
| 56 | AuthProvider ap = (AuthProvider)ks.getProvider(); |
| 57 | try { |
| 58 | |
| 59 | // test app-provided callback |
| 60 | System.out.println("*** enter [foo] as the password ***"); |
| 61 | password = new char[] { 'f', 'o', 'o' }; |
| 62 | |
| 63 | ap.login(new Subject(), new PasswordCallbackHandler()); |
| 64 | ap.logout(); |
| 65 | throw new SecurityException("test failed, expected LoginException"); |
| 66 | } catch (FailedLoginException fle) { |
| 67 | System.out.println("test " + testnum++ + " passed"); |
| 68 | } |
| 69 | |
| 70 | try { |
| 71 | |
| 72 | // test default callback |
| 73 | System.out.println("*** enter [foo] as the password ***"); |
| 74 | password = new char[] { 'f', 'o', 'o' }; |
| 75 | |
| 76 | Security.setProperty("auth.login.defaultCallbackHandler", |
| 77 | "Login$PasswordCallbackHandler"); |
| 78 | ap.login(new Subject(), null); |
| 79 | ap.logout(); |
| 80 | throw new SecurityException("test failed, expected LoginException"); |
| 81 | } catch (FailedLoginException fle) { |
| 82 | System.out.println("test " + testnum++ + " passed"); |
| 83 | } |
| 84 | |
| 85 | // test provider-set callback |
| 86 | System.out.println("*** enter test12 (correct) password ***"); |
| 87 | password = new char[] { 't', 'e', 's', 't', '1', '2' }; |
| 88 | |
| 89 | Security.setProperty("auth.login.defaultCallbackHandler", ""); |
| 90 | ap.setCallbackHandler |
| 91 | (new com.sun.security.auth.callback.DialogCallbackHandler()); |
| 92 | ap.setCallbackHandler(new PasswordCallbackHandler()); |
| 93 | ap.login(new Subject(), null); |
| 94 | System.out.println("test " + testnum++ + " passed"); |
| 95 | |
| 96 | // test user already logged in |
| 97 | ap.setCallbackHandler(null); |
| 98 | ap.login(new Subject(), null); |
| 99 | System.out.println("test " + testnum++ + " passed"); |
| 100 | |
| 101 | // logout |
| 102 | ap.logout(); |
| 103 | |
| 104 | // call KeyStore.load with a NULL password, and get prompted for PIN |
| 105 | ap.setCallbackHandler(new PasswordCallbackHandler()); |
| 106 | ks.load(null, (char[])null); |
| 107 | System.out.println("test " + testnum++ + " passed"); |
| 108 | } |
| 109 | |
| 110 | public static class PasswordCallbackHandler implements CallbackHandler { |
| 111 | public void handle(Callback[] callbacks) |
| 112 | throws IOException, UnsupportedCallbackException { |
| 113 | if (!(callbacks[0] instanceof PasswordCallback)) { |
| 114 | throw new UnsupportedCallbackException(callbacks[0]); |
| 115 | } |
| 116 | PasswordCallback pc = (PasswordCallback)callbacks[0]; |
| 117 | pc.setPassword(Login.password); |
| 118 | } |
| 119 | } |
| 120 | } |