1 /*
   2  * Copyright (c) 2003, 2004, Oracle and/or its affiliates. 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * 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(new PasswordCallbackHandler());
  91         ap.login(new Subject(), null);
  92         System.out.println("test " + testnum++ + " passed");
  93 
  94         // test user already logged in
  95         ap.setCallbackHandler(null);
  96         ap.login(new Subject(), null);
  97         System.out.println("test " + testnum++ + " passed");
  98 
  99         // logout
 100         ap.logout();
 101 
 102         // call KeyStore.load with a NULL password, and get prompted for PIN
 103         ap.setCallbackHandler(new PasswordCallbackHandler());
 104         ks.load(null, (char[])null);
 105         System.out.println("test " + testnum++ + " passed");
 106     }
 107 
 108     public static class PasswordCallbackHandler implements CallbackHandler {
 109         public void handle(Callback[] callbacks)
 110                 throws IOException, UnsupportedCallbackException {
 111             if (!(callbacks[0] instanceof PasswordCallback)) {
 112                 throw new UnsupportedCallbackException(callbacks[0]);
 113             }
 114             PasswordCallback pc = (PasswordCallback)callbacks[0];
 115             pc.setPassword(Login.password);
 116         }
 117     }
 118 }