jdk/src/jdk.security.auth/share/classes/com/sun/security/auth/module/KeyStoreLoginModule.java
author alanb
Thu, 01 Dec 2016 08:57:53 +0000
changeset 42338 a60f280f803c
parent 36511 9d0388c6b336
child 43243 a48dab17a356
permissions -rw-r--r--
8169069: Module system implementation refresh (11/2016) Reviewed-by: plevart, chegar, psandoz, mchung, alanb, dfuchs, naoto, coffeys, weijun Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, mark.reinhold@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 20742
diff changeset
     2
 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.net.MalformedURLException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.net.URL;
16503
03d7a6155092 8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents: 10336
diff changeset
    33
import java.security.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.cert.*;
16503
03d7a6155092 8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents: 10336
diff changeset
    35
import java.security.cert.Certificate;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.cert.X509Certificate;
16503
03d7a6155092 8009970: Several LoginModule classes need extra permission to load AuthResources
weijun
parents: 10336
diff changeset
    37
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import javax.security.auth.Destroyable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.security.auth.DestroyFailedException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.security.auth.Subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.security.auth.x500.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.security.auth.callback.Callback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.callback.CallbackHandler;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.security.auth.callback.ConfirmationCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.callback.NameCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.callback.PasswordCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import javax.security.auth.callback.TextOutputCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import javax.security.auth.callback.UnsupportedCallbackException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import javax.security.auth.login.FailedLoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import javax.security.auth.login.LoginException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import javax.security.auth.spi.LoginModule;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.util.Password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Provides a JAAS login module that prompts for a key store alias and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * populates the subject with the alias's principal and credentials. Stores
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    58
 * an {@code X500Principal} for the subject distinguished name of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * first certificate in the alias's credentials in the subject's principals,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * the alias's certificate path in the subject's public credentials, and a
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    61
 * {@code X500PrivateCredential} whose certificate is the first
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * certificate in the alias's certificate path and whose private key is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * alias's private key in the subject's private credentials. <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * Recognizes the following options in the configuration file:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    68
 * <dt> {@code keyStoreURL} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * <dd> A URL that specifies the location of the key store.  Defaults to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *      a URL pointing to the .keystore file in the directory specified by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    71
 *      {@code user.home} system property.  The input stream from this
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    72
 *      URL is passed to the {@code KeyStore.load} method.
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    73
 *      "NONE" may be specified if a {@code null} stream must be
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    74
 *      passed to the {@code KeyStore.load} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *      "NONE" should be specified if the KeyStore resides
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *      on a hardware token device, for example.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    78
 * <dt> {@code keyStoreType} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * <dd> The key store type.  If not specified, defaults to the result of
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    80
 *      calling {@code KeyStore.getDefaultType()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *      If the type is "PKCS11", then keyStoreURL must be "NONE"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *      and privateKeyPasswordURL must not be specified.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    84
 * <dt> {@code keyStoreProvider} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * <dd> The key store provider.  If not specified, uses the standard search
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *      order to find the provider. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    88
 * <dt> {@code keyStoreAlias} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * <dd> The alias in the key store to login as.  Required when no callback
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 *      handler is provided.  No default value. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    92
 * <dt> {@code keyStorePasswordURL} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * <dd> A URL that specifies the location of the key store password.  Required
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *      when no callback handler is provided and
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    95
 *      {@code protected} is false.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 *      No default value. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    98
 * <dt> {@code privateKeyPasswordURL} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <dd> A URL that specifies the location of the specific private key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *      needed to access the private key for this alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 *      The keystore password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      is used if this value is needed and not specified. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   104
 * <dt> {@code protected} </dt>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * <dd> This value should be set to "true" if the KeyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      has a separate, protected authentication path
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *      (for example, a dedicated PIN-pad attached to a smart card).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *      Defaults to "false". If "true" keyStorePasswordURL and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 *      privateKeyPasswordURL must not be specified.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
public class KeyStoreLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   115
    private static final ResourceBundle rb =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   116
        ResourceBundle.getBundle("sun.security.util.AuthResources");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /* -- Fields -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    private static final int UNINITIALIZED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final int INITIALIZED = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    private static final int AUTHENTICATED = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    private static final int LOGGED_IN = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final int PROTECTED_PATH = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final int TOKEN = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    private static final int NORMAL = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    private static final String NONE = "NONE";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    private static final String P11KEYSTORE = "PKCS11";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    private static final TextOutputCallback bannerCallback =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                new TextOutputCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                        (TextOutputCallback.INFORMATION,
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   135
                        rb.getString("Please.enter.keystore.information"));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private final ConfirmationCallback confirmationCallback =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                new ConfirmationCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                        (ConfirmationCallback.INFORMATION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                        ConfirmationCallback.OK_CANCEL_OPTION,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                        ConfirmationCallback.OK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    private CallbackHandler callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   144
    private Map<String, Object> sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    private char[] keyStorePassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    private char[] privateKeyPassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    private KeyStore keyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private String keyStoreURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    private String keyStoreType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    private String keyStoreProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    private String keyStoreAlias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private String keyStorePasswordURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    private String privateKeyPasswordURL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private boolean debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
    private javax.security.auth.x500.X500Principal principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private Certificate[] fromKeyStore;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    private java.security.cert.CertPath certP = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    private X500PrivateCredential privateCredential;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    private int status = UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private boolean nullStream = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private boolean token = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private boolean protectedPath = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /* -- Methods -- */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    /**
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   170
     * Initialize this {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   172
     * @param subject the {@code Subject} to be authenticated.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   174
     * @param callbackHandler a {@code CallbackHandler} for communicating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     *                  with the end user (prompting for usernames and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *                  passwords, for example),
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   177
     *                  which may be {@code null}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   179
     * @param sharedState shared {@code LoginModule} state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * @param options options specified in the login
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   182
     *                  {@code Configuration} for this particular
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   183
     *                  {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   185
    // Unchecked warning from (Map<String, Object>)sharedState is safe
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   186
    // since javax.security.auth.login.LoginContext passes a raw HashMap.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   187
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    public void initialize(Subject subject,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                           CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                           Map<String,?> sharedState,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                           Map<String,?> options)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        this.callbackHandler = callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   195
        this.sharedState = (Map<String, Object>)sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        processOptions();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        status = INITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    private void processOptions() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        keyStoreURL = (String) options.get("keyStoreURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        if (keyStoreURL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            keyStoreURL =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                "file:" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                System.getProperty("user.home").replace(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    File.separatorChar, '/') +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                '/' + ".keystore";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        } else if (NONE.equals(keyStoreURL)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            nullStream = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        keyStoreType = (String) options.get("keyStoreType");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if (keyStoreType == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            keyStoreType = KeyStore.getDefaultType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        if (P11KEYSTORE.equalsIgnoreCase(keyStoreType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            token = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        keyStoreProvider = (String) options.get("keyStoreProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        keyStoreAlias = (String) options.get("keyStoreAlias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        keyStorePasswordURL = (String) options.get("keyStorePasswordURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        privateKeyPasswordURL = (String) options.get("privateKeyPasswordURL");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        protectedPath = "true".equalsIgnoreCase((String)options.get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                                        ("protected"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        debug = "true".equalsIgnoreCase((String) options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            debugPrint(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            debugPrint("keyStoreURL=" + keyStoreURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            debugPrint("keyStoreType=" + keyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            debugPrint("keyStoreProvider=" + keyStoreProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            debugPrint("keyStoreAlias=" + keyStoreAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            debugPrint("keyStorePasswordURL=" + keyStorePasswordURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            debugPrint("privateKeyPasswordURL=" + privateKeyPasswordURL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            debugPrint("protectedPath=" + protectedPath);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
            debugPrint(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * Authenticate the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * <p> Get the Keystore alias and relevant passwords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * Retrieve the alias's principal and credentials from the Keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   252
     * @exception FailedLoginException if the authentication fails.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   254
     * @return true in all cases (this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *          should not be ignored).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
        switch (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        case UNINITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            throw new LoginException("The login module is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        case INITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        case AUTHENTICATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
            if (token && !nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                        ("if keyStoreType is " + P11KEYSTORE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
                        " then keyStoreURL must be " + NONE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            if (token && privateKeyPasswordURL != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                        ("if keyStoreType is " + P11KEYSTORE +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                        " then privateKeyPasswordURL must not be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            if (protectedPath &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                (keyStorePasswordURL != null ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        privateKeyPasswordURL != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        ("if protected is true then keyStorePasswordURL and " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                        "privateKeyPasswordURL must not be specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // get relevant alias and password info
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            if (protectedPath) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
                getAliasAndPasswords(PROTECTED_PATH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            } else if (token) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                getAliasAndPasswords(TOKEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                getAliasAndPasswords(NORMAL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            // log into KeyStore to retrieve data,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // then clear passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                getKeyStoreInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                if (privateKeyPassword != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                    privateKeyPassword != keyStorePassword) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    Arrays.fill(privateKeyPassword, '\0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    privateKeyPassword = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (keyStorePassword != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    Arrays.fill(keyStorePassword, '\0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    keyStorePassword = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            status = AUTHENTICATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        case LOGGED_IN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    /** Get the alias and passwords to use for looking up in the KeyStore. */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9035
diff changeset
   320
    @SuppressWarnings("fallthrough")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private void getAliasAndPasswords(int env) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
        if (callbackHandler == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            // No callback handler - check for alias and password options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
            switch (env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            case PROTECTED_PATH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                checkAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
            case TOKEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                checkAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                checkStorePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            case NORMAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
                checkAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                checkStorePass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                checkKeyPass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            // Callback handler available - prompt for alias and passwords
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            NameCallback aliasCallback;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            if (keyStoreAlias == null || keyStoreAlias.length() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                aliasCallback = new NameCallback(
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   348
                                        rb.getString("Keystore.alias."));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                aliasCallback =
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   351
                    new NameCallback(rb.getString("Keystore.alias."),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                     keyStoreAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            PasswordCallback storePassCallback = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            PasswordCallback keyPassCallback = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            switch (env) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            case PROTECTED_PATH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            case NORMAL:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                keyPassCallback = new PasswordCallback
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   363
                    (rb.getString("Private.key.password.optional."), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                // fall thru
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            case TOKEN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                storePassCallback = new PasswordCallback
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   367
                    (rb.getString("Keystore.password."), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
            prompt(aliasCallback, storePassCallback, keyPassCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            debugPrint("alias=" + keyStoreAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    private void checkAlias() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        if (keyStoreAlias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                ("Need to specify an alias option to use " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                "KeyStoreLoginModule non-interactively.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    private void checkStorePass() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        if (keyStorePasswordURL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                ("Need to specify keyStorePasswordURL option to use " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                "KeyStoreLoginModule non-interactively.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            in = new URL(keyStorePasswordURL).openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
            keyStorePassword = Password.readPassword(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                ("Problem accessing keystore password \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
                keyStorePasswordURL + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
                    LoginException le = new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
                        "Problem closing the keystore password stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
                    le.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
                    throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private void checkKeyPass() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        if (privateKeyPasswordURL == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            privateKeyPassword = keyStorePassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
                in = new URL(privateKeyPasswordURL).openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                privateKeyPassword = Password.readPassword(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
                        ("Problem accessing private key password \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                        privateKeyPasswordURL + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
                    } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                        LoginException le = new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                            "Problem closing the private key password stream");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
                        le.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                        throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    private void prompt(NameCallback aliasCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
                        PasswordCallback storePassCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
                        PasswordCallback keyPassCallback)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
                throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        if (storePassCallback == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            // only prompt for alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                callbackHandler.handle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
                    new Callback[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                        bannerCallback, aliasCallback, confirmationCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                        ("Problem retrieving keystore alias");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                throw new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    "Error: " + e.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    " is not available to retrieve authentication " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                    " information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            int confirmationResult = confirmationCallback.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            if (confirmationResult == ConfirmationCallback.CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                throw new LoginException("Login cancelled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            saveAlias(aliasCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        } else if (keyPassCallback == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // prompt for alias and key store password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                callbackHandler.handle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
                    new Callback[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
                        bannerCallback, aliasCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
                        storePassCallback, confirmationCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                        ("Problem retrieving keystore alias and password");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
                throw new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                    "Error: " + e.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    " is not available to retrieve authentication " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                    " information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            int confirmationResult = confirmationCallback.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
            if (confirmationResult == ConfirmationCallback.CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                throw new LoginException("Login cancelled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            saveAlias(aliasCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
            saveStorePass(storePassCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
            // prompt for alias, key store password, and key password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                callbackHandler.handle(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                    new Callback[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                        bannerCallback, aliasCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                        storePassCallback, keyPassCallback,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
                        confirmationCallback
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
                    });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                        ("Problem retrieving keystore alias and passwords");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            } catch (UnsupportedCallbackException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
                throw new LoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
                    "Error: " + e.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                    " is not available to retrieve authentication " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
                    " information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            int confirmationResult = confirmationCallback.getSelectedIndex();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            if (confirmationResult == ConfirmationCallback.CANCEL) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                throw new LoginException("Login cancelled");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            saveAlias(aliasCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            saveStorePass(storePassCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
            saveKeyPass(keyPassCallback);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
    private void saveAlias(NameCallback cb) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
        keyStoreAlias = cb.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    private void saveStorePass(PasswordCallback c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        keyStorePassword = c.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        if (keyStorePassword == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            /* Treat a NULL password as an empty password */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            keyStorePassword = new char[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        c.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    private void saveKeyPass(PasswordCallback c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        privateKeyPassword = c.getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        if (privateKeyPassword == null || privateKeyPassword.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
             * Use keystore password if no private key password is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
             * specified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            privateKeyPassword = keyStorePassword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        c.clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
    /** Get the credentials from the KeyStore. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    private void getKeyStoreInfo() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        /* Get KeyStore instance */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
            if (keyStoreProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                keyStore = KeyStore.getInstance(keyStoreType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                keyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                    KeyStore.getInstance(keyStoreType, keyStoreProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                ("The specified keystore type was not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
        } catch (NoSuchProviderException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                ("The specified keystore provider was not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        /* Load KeyStore contents from file */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        InputStream in = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
            if (nullStream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                // if using protected auth path, keyStorePassword will be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                keyStore.load(null, keyStorePassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                in = new URL(keyStoreURL).openStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                keyStore.load(in, keyStorePassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
        } catch (MalformedURLException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                                ("Incorrect keyStoreURL option");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                                ("Error initializing keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                ("Error initializing keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (in != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                    in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                    LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                ("Error initializing keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                    le.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                    throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        /* Get certificate chain and create a certificate path */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
            fromKeyStore =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                keyStore.getCertificateChain(keyStoreAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            if (fromKeyStore == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                || fromKeyStore.length == 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                || !(fromKeyStore[0] instanceof X509Certificate))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                throw new FailedLoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                    "Unable to find X.509 certificate chain in keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
            } else {
7970
af1579474d16 7008728: diamond conversion of basic security, permissions, authentication
smarks
parents: 7179
diff changeset
   642
                LinkedList<Certificate> certList = new LinkedList<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                for (int i=0; i < fromKeyStore.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                    certList.add(fromKeyStore[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                CertificateFactory certF=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                    CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                certP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                    certF.generateCertPath(certList);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            LoginException le = new LoginException("Error using keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
            LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
                ("Error: X.509 Certificate type unavailable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
            le.initCause(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        /* Get principal and keys */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            X509Certificate certificate = (X509Certificate)fromKeyStore[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            principal = new javax.security.auth.x500.X500Principal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                (certificate.getSubjectDN().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
            // if token, privateKeyPassword will be null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            Key privateKey = keyStore.getKey(keyStoreAlias, privateKeyPassword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            if (privateKey == null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
                || !(privateKey instanceof PrivateKey))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                throw new FailedLoginException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                    "Unable to recover key from keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            privateCredential = new X500PrivateCredential(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                certificate, (PrivateKey) privateKey, keyStoreAlias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } catch (KeyStoreException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            LoginException le = new LoginException("Error using keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
            LoginException le = new LoginException("Error using keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            le.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        } catch (UnrecoverableKeyException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            FailedLoginException fle = new FailedLoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                                ("Unable to recover key from keystore");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            fle.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            throw fle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
            debugPrint("principal=" + principal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
                       "\n certificate="
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                       + privateCredential.getCertificate() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
                       "\n alias =" + privateCredential.getAlias());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * Abstract method to commit the authentication process (phase 2).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
     * succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   711
     * {@code login} method), then this method associates a
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   712
     * {@code X500Principal} for the subject distinguished name of the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * first certificate in the alias's credentials in the subject's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * principals,the alias's certificate path in the subject's public
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   715
     * credentials, and a {@code X500PrivateCredential} whose certificate
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * is the first  certificate in the alias's certificate path and whose
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * private key is the alias's private key in the subject's private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * credentials.  If this LoginModule's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @return true if this LoginModule's own login and commit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     *          attempts succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
        switch (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
        case UNINITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            throw new LoginException("The login module is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
        case INITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
            logoutInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
            throw new LoginException("Authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        case AUTHENTICATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
            if (commitInternal()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                logoutInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                throw new LoginException("Unable to retrieve certificates");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        case LOGGED_IN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    private boolean commitInternal() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        /* If the subject is not readonly add to the principal and credentials
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
         * set; otherwise just return true
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
            throw new LoginException ("Subject is set readonly");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            subject.getPrincipals().add(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            subject.getPublicCredentials().add(certP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            subject.getPrivateCredentials().add(privateCredential);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
            status = LOGGED_IN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    /**
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   764
     * This method is called if the LoginContext's
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
     * overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     * did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   771
     * {@code login} and {@code commit} methods),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
     * then this method cleans up any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * <p> If the loaded KeyStore's provider extends
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   775
     * {@code java.security.AuthProvider},
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   776
     * then the provider's {@code logout} method is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
     * @exception LoginException if the abort fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
        switch (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        case UNINITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        case INITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        case AUTHENTICATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            logoutInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        case LOGGED_IN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            logoutInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
     * Logout a user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * <p> This method removes the Principals, public credentials and the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   803
     * private credentials that were added by the {@code commit} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * <p> If the loaded KeyStore's provider extends
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   806
     * {@code java.security.AuthProvider},
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   807
     * then the provider's {@code logout} method is invoked.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * @exception LoginException if the logout fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   811
     * @return true in all cases since this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            debugPrint("Entering logout " + status);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        switch (status) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        case UNINITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
                ("The login module is not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        case INITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        case AUTHENTICATED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
           // impossible for LoginModule to be in AUTHENTICATED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
           // state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
           // assert status != AUTHENTICATED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        case LOGGED_IN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            logoutInternal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    private void logoutInternal() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            debugPrint("Entering logoutInternal");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        // assumption is that KeyStore.load did a login -
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        // perform explicit logout if possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        LoginException logoutException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        Provider provider = keyStore.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
        if (provider instanceof AuthProvider) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            AuthProvider ap = (AuthProvider)provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
                ap.logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                    debugPrint("logged out of KeyStore AuthProvider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                // save but continue below
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                logoutException = le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // attempt to destroy the private credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            // even if the Subject is read-only
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            certP = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            status = INITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            // destroy the private credential
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            Iterator<Object> it = subject.getPrivateCredentials().iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
            while (it.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                Object obj = it.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
                if (privateCredential.equals(obj)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                    privateCredential = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                        ((Destroyable)obj).destroy();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
                        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                            debugPrint("Destroyed private credential, " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                                       obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    } catch (DestroyFailedException dfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                        LoginException le = new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
                            ("Unable to destroy private credential, "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
                             + obj.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                        le.initCause(dfe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
                        throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
            // throw an exception because we can not remove
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            // the principal and public credential from this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
            // read-only Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                ("Unable to remove Principal ("
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                 + "X500Principal "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                 + ") and public credential (certificatepath) "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                 + "from read-only Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        if (principal != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
            subject.getPrincipals().remove(principal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
            principal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        if (certP != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
            subject.getPublicCredentials().remove(certP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
            certP = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        if (privateCredential != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
            subject.getPrivateCredentials().remove(privateCredential);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
            privateCredential = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        // throw pending logout exception if there is one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        if (logoutException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
            throw logoutException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        status = INITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    private void debugPrint(String message) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
        // we should switch to logging API
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        if (message == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            System.err.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            System.err.println("Debug KeyStoreLoginModule: " + message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
}