diff --git a/jdk/src/java.base/share/classes/java/security/AuthProvider.java b/jdk/src/java.base/share/classes/java/security/AuthProvider.java index 571a1fe085f..efa21641cb3 100644 --- a/jdk/src/java.base/share/classes/java/security/AuthProvider.java +++ b/jdk/src/java.base/share/classes/java/security/AuthProvider.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2003, 2015, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -79,8 +79,10 @@ public abstract class AuthProvider extends Provider { * this provider to obtain authentication information * from the caller, which may be {@code null} * - * @exception LoginException if the login operation fails - * @exception SecurityException if the caller does not pass a + * @throws IllegalStateException if the provider requires configuration + * and {@link configure} has not been called + * @throws LoginException if the login operation fails + * @throws SecurityException if the caller does not pass a * security check for * {@code SecurityPermission("authProvider.name")}, * where {@code name} is the value returned by @@ -92,8 +94,10 @@ public abstract class AuthProvider extends Provider { /** * Log out from this provider. * - * @exception LoginException if the logout operation fails - * @exception SecurityException if the caller does not pass a + * @throws IllegalStateException if the provider requires configuration + * and {@link configure} has not been called + * @throws LoginException if the logout operation fails + * @throws SecurityException if the caller does not pass a * security check for * {@code SecurityPermission("authProvider.name")}, * where {@code name} is the value returned by @@ -118,7 +122,9 @@ public abstract class AuthProvider extends Provider { * @param handler a {@code CallbackHandler} for obtaining * authentication information, which may be {@code null} * - * @exception SecurityException if the caller does not pass a + * @throws IllegalStateException if the provider requires configuration + * and {@link configure} has not been called + * @throws SecurityException if the caller does not pass a * security check for * {@code SecurityPermission("authProvider.name")}, * where {@code name} is the value returned by diff --git a/jdk/src/java.base/share/classes/java/security/Provider.java b/jdk/src/java.base/share/classes/java/security/Provider.java index a38fbdcfaf0..e192d23cbbe 100644 --- a/jdk/src/java.base/share/classes/java/security/Provider.java +++ b/jdk/src/java.base/share/classes/java/security/Provider.java @@ -187,12 +187,29 @@ public abstract class Provider extends Properties { * is invalid. * @return a provider configured with the supplied configuration argument. * - * @since 1.9 + * @since 9 */ public Provider configure(String configArg) { throw new UnsupportedOperationException("configure is not supported"); } + /** + * Check if this provider instance has been configured. + * + * @implSpec + * The default implementation returns true. + * Subclasses should override this method if the provider instance requires + * an explicit {@code configure} call after being constructed. + * + * @return true if no further configuration is needed, false otherwise. + * + * @since 9 + */ + public boolean isConfigured() { + return true; + } + + /** * Returns the name of this provider. * diff --git a/jdk/src/java.base/share/classes/java/security/SecurityPermission.java b/jdk/src/java.base/share/classes/java/security/SecurityPermission.java index 60d1d785860..8234624d26e 100644 --- a/jdk/src/java.base/share/classes/java/security/SecurityPermission.java +++ b/jdk/src/java.base/share/classes/java/security/SecurityPermission.java @@ -53,6 +53,17 @@ import java.util.StringTokenizer; * * * + * authProvider.{provider name} + * Allow the named provider to be an AuthProvider for login and + * logout operations. + * This allows the named provider to perform login and logout + * operations. The named provider must extend {@code AuthProvider} + * and care must be taken to grant to a trusted provider since + * login operations involve sensitive authentication information + * such as PINs and passwords. + * + * + * * createAccessControlContext * Creation of an AccessControlContext * This allows someone to instantiate an AccessControlContext diff --git a/jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/SunPKCS11.java b/jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/SunPKCS11.java index 8eacc210afe..11016eb42ae 100644 --- a/jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/SunPKCS11.java +++ b/jdk/src/jdk.crypto.pkcs11/share/classes/sun/security/pkcs11/SunPKCS11.java @@ -106,9 +106,9 @@ public final class SunPKCS11 extends AuthProvider { public Provider configure(String configArg) throws InvalidParameterException { final String newConfigName = checkNull(configArg); try { - return AccessController.doPrivileged(new PrivilegedExceptionAction() { + return AccessController.doPrivileged(new PrivilegedExceptionAction<>() { @Override - public Provider run() throws Exception { + public SunPKCS11 run() throws Exception { return new SunPKCS11(new Config(newConfigName)); } }); @@ -119,6 +119,11 @@ public final class SunPKCS11 extends AuthProvider { } } + @Override + public boolean isConfigured() { + return (config != null); + } + private static T checkNull(T obj) { if (obj == null) { throw new NullPointerException(); @@ -1142,8 +1147,10 @@ public final class SunPKCS11 extends AuthProvider { * @param handler the CallbackHandler used by * this provider to communicate with the caller * - * @exception LoginException if the login operation fails - * @exception SecurityException if the does not pass a security check for + * @throws IllegalStateException if the provider requires configuration + * and Provider.configure has not been called + * @throws LoginException if the login operation fails + * @throws SecurityException if the does not pass a security check for * SecurityPermission("authProvider.name"), * where name is the value returned by * this provider's getName method @@ -1151,8 +1158,11 @@ public final class SunPKCS11 extends AuthProvider { public void login(Subject subject, CallbackHandler handler) throws LoginException { - // security check + if (!isConfigured()) { + throw new IllegalStateException("Configuration is required"); + } + // security check SecurityManager sm = System.getSecurityManager(); if (sm != null) { if (debug != null) { @@ -1271,16 +1281,21 @@ public final class SunPKCS11 extends AuthProvider { /** * Log out from this provider * - * @exception LoginException if the logout operation fails - * @exception SecurityException if the does not pass a security check for + * @throws IllegalStateException if the provider requires configuration + * and Provider.configure has not been called + * @throws LoginException if the logout operation fails + * @throws SecurityException if the does not pass a security check for * SecurityPermission("authProvider.name"), * where name is the value returned by * this provider's getName method */ public void logout() throws LoginException { - // security check + if (!isConfigured()) { + throw new IllegalStateException("Configuration is required"); + } + // security check SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission @@ -1353,7 +1368,9 @@ public final class SunPKCS11 extends AuthProvider { * @param handler a CallbackHandler for obtaining * authentication information, which may be null * - * @exception SecurityException if the caller does not pass a + * @throws IllegalStateException if the provider requires configuration + * and Provider.configure has not been called + * @throws SecurityException if the caller does not pass a * security check for * SecurityPermission("authProvider.name"), * where name is the value returned by @@ -1361,8 +1378,11 @@ public final class SunPKCS11 extends AuthProvider { */ public void setCallbackHandler(CallbackHandler handler) { - // security check + if (!isConfigured()) { + throw new IllegalStateException("Configuration is required"); + } + // security check SecurityManager sm = System.getSecurityManager(); if (sm != null) { sm.checkPermission diff --git a/jdk/test/sun/security/pkcs11/Provider/LoginISE.java b/jdk/test/sun/security/pkcs11/Provider/LoginISE.java new file mode 100644 index 00000000000..5027770c5e6 --- /dev/null +++ b/jdk/test/sun/security/pkcs11/Provider/LoginISE.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.*; +import java.util.*; +import java.security.*; +import javax.security.auth.callback.*; + +/** + * @test + * @bug 8130648 + * @summary make sure IllegalStateException is thrown for uninitialized + * SunPKCS11 provider instance + */ +public class LoginISE { + + public static void main(String[] args) throws Exception { + + Provider p = Security.getProvider("SunPKCS11"); + if (p == null) { + System.out.println("No un-initialized PKCS11 provider available; skip"); + return; + } + if (!(p instanceof AuthProvider)) { + throw new RuntimeException("Error: expect AuthProvider!"); + } + AuthProvider ap = (AuthProvider) p; + if (ap.isConfigured()) { + throw new RuntimeException("Fail: isConfigured() should return false"); + } + try { + ap.login(null, null); + throw new RuntimeException("Fail: expected ISE not thrown!"); + } catch (IllegalStateException ise) { + System.out.println("Expected ISE thrown for login call"); + } + try { + ap.logout(); + throw new RuntimeException("Fail: expected ISE not thrown!"); + } catch (IllegalStateException ise) { + System.out.println("Expected ISE thrown for logout call"); + } + try { + ap.setCallbackHandler(new PasswordCallbackHandler()); + throw new RuntimeException("Fail: expected ISE not thrown!"); + } catch (IllegalStateException ise) { + System.out.println("Expected ISE thrown for logout call"); + } + + System.out.println("Test Passed"); + } + + public static class PasswordCallbackHandler implements CallbackHandler { + public void handle(Callback[] callbacks) + throws IOException, UnsupportedCallbackException { + } + } +}