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