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