src/java.base/share/classes/sun/security/provider/DomainKeyStore.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
child 59024 b046ba510bbc
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     1
/*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     4
 *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    10
 *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    15
 * accompanied this code).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    16
 *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    20
 *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    23
 * questions.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    24
 */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    25
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    26
package sun.security.provider;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    27
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    28
import java.io.*;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    29
import java.net.*;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    30
import java.security.*;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    31
import java.security.cert.Certificate;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    32
import java.security.cert.CertificateFactory;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    33
import java.security.cert.CertificateException;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    34
import java.util.*;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    35
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    36
import sun.security.pkcs.EncryptedPrivateKeyInfo;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    37
import sun.security.util.PolicyUtil;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    38
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    39
/**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    40
 * This class provides the domain keystore type identified as "DKS".
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    41
 * DKS presents a collection of separate keystores as a single logical keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    42
 * The collection of keystores is specified in a domain configuration file which
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
    43
 * is passed to DKS in a {@link DomainLoadStoreParameter}.
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    44
 * <p>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    45
 * The following properties are supported:
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    46
 * <dl>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    47
 * <dt> {@code keystoreType="<type>"} </dt>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    48
 *     <dd> The keystore type. </dd>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    49
 * <dt> {@code keystoreURI="<url>"} </dt>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    50
 *     <dd> The keystore location. </dd>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    51
 * <dt> {@code keystoreProviderName="<name>"} </dt>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    52
 *     <dd> The name of the keystore's JCE provider. </dd>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    53
 * <dt> {@code keystorePasswordEnv="<environment-variable>"} </dt>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    54
 *     <dd> The environment variable that stores a keystore password.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    55
 * <dt> {@code entryNameSeparator="<separator>"} </dt>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    56
 *     <dd> The separator between a keystore name prefix and an entry name.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    57
 *          When specified, it applies to all the entries in a domain.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    58
 *          Its default value is a space. </dd>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    59
 * </dl>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    60
 *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    61
 * @since 1.8
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    62
 */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    63
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    64
abstract class DomainKeyStore extends KeyStoreSpi {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    65
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    66
    // regular DKS
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    67
    public static final class DKS extends DomainKeyStore {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    68
        String convertAlias(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    69
            return alias.toLowerCase(Locale.ENGLISH);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    70
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    71
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    72
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    73
    // DKS property names
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    74
    private static final String ENTRY_NAME_SEPARATOR = "entrynameseparator";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    75
    private static final String KEYSTORE_PROVIDER_NAME = "keystoreprovidername";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    76
    private static final String KEYSTORE_TYPE = "keystoretype";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    77
    private static final String KEYSTORE_URI = "keystoreuri";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    78
    private static final String KEYSTORE_PASSWORD_ENV = "keystorepasswordenv";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    79
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    80
    // RegEx meta characters
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    81
    private static final String REGEX_META = ".$|()[{^?*+\\";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    82
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    83
    // Default prefix for keystores loaded-by-stream
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    84
    private static final String DEFAULT_STREAM_PREFIX = "iostream";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    85
    private int streamCounter = 1;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    86
    private String entryNameSeparator = " ";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    87
    private String entryNameSeparatorRegEx = " ";
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    88
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    89
    // Default keystore type
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    90
    private static final String DEFAULT_KEYSTORE_TYPE =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    91
        KeyStore.getDefaultType();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    92
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    93
    // Domain keystores
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    94
    private final Map<String, KeyStore> keystores = new HashMap<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    95
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    96
    DomainKeyStore() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    97
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    98
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
    99
    // convert an alias to internal form, overridden in subclasses:
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   100
    // lower case for regular DKS
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   101
    abstract String convertAlias(String alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   102
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   103
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   104
     * Returns the key associated with the given alias, using the given
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   105
     * password to recover it.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   106
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   107
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   108
     * @param password the password for recovering the key
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   109
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   110
     * @return the requested key, or null if the given alias does not exist
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   111
     * or does not identify a <i>key entry</i>.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   112
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   113
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   114
     * key cannot be found
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   115
     * @exception UnrecoverableKeyException if the key cannot be recovered
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   116
     * (e.g., the given password is wrong).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   117
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   118
    public Key engineGetKey(String alias, char[] password)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   119
        throws NoSuchAlgorithmException, UnrecoverableKeyException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   120
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   121
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   122
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   123
        Key key = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   124
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   125
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   126
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   127
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   128
                key = keystore.getKey(entryAlias, password);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   129
                if (key != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   130
                    break;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   131
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   132
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   133
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   134
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   135
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   136
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   137
        return key;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   138
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   139
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   140
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   141
     * Returns the certificate chain associated with the given alias.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   142
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   143
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   144
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   145
     * @return the certificate chain (ordered with the user's certificate first
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   146
     * and the root certificate authority last), or null if the given alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   147
     * does not exist or does not contain a certificate chain (i.e., the given
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   148
     * alias identifies either a <i>trusted certificate entry</i> or a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   149
     * <i>key entry</i> without a certificate chain).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   150
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   151
    public Certificate[] engineGetCertificateChain(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   152
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   153
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   154
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   155
        Certificate[] chain = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   156
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   157
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   158
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   159
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   160
                chain = keystore.getCertificateChain(entryAlias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   161
                if (chain != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   162
                    break;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   163
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   164
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   165
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   166
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   167
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   168
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   169
        return chain;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   170
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   171
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   172
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   173
     * Returns the certificate associated with the given alias.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   174
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   175
     * <p>If the given alias name identifies a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   176
     * <i>trusted certificate entry</i>, the certificate associated with that
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   177
     * entry is returned. If the given alias name identifies a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   178
     * <i>key entry</i>, the first element of the certificate chain of that
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   179
     * entry is returned, or null if that entry does not have a certificate
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   180
     * chain.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   181
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   182
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   183
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   184
     * @return the certificate, or null if the given alias does not exist or
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   185
     * does not contain a certificate.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   186
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   187
    public Certificate engineGetCertificate(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   188
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   189
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   190
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   191
        Certificate cert = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   192
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   193
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   194
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   195
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   196
                cert = keystore.getCertificate(entryAlias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   197
                if (cert != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   198
                    break;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   199
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   200
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   201
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   202
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   203
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   204
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   205
        return cert;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   206
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   207
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   208
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   209
     * Returns the creation date of the entry identified by the given alias.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   210
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   211
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   212
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   213
     * @return the creation date of this entry, or null if the given alias does
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   214
     * not exist
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   215
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   216
    public Date engineGetCreationDate(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   217
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   218
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   219
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   220
        Date date = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   221
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   222
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   223
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   224
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   225
                date = keystore.getCreationDate(entryAlias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   226
                if (date != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   227
                    break;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   228
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   229
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   230
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   231
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   232
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   233
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   234
        return date;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   235
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   236
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   237
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   238
     * Assigns the given private key to the given alias, protecting
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   239
     * it with the given password as defined in PKCS8.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   240
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   241
     * <p>The given java.security.PrivateKey <code>key</code> must
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   242
     * be accompanied by a certificate chain certifying the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   243
     * corresponding public key.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   244
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   245
     * <p>If the given alias already exists, the keystore information
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   246
     * associated with it is overridden by the given key and certificate
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   247
     * chain.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   248
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   249
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   250
     * @param key the private key to be associated with the alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   251
     * @param password the password to protect the key
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   252
     * @param chain the certificate chain for the corresponding public
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   253
     * key (only required if the given key is of type
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   254
     * <code>java.security.PrivateKey</code>).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   255
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   256
     * @exception KeyStoreException if the given key is not a private key,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   257
     * cannot be protected, or this operation fails for some other reason
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   258
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   259
    public void engineSetKeyEntry(String alias, Key key, char[] password,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   260
                                  Certificate[] chain)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   261
        throws KeyStoreException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   262
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   263
        AbstractMap.SimpleEntry<String,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   264
            AbstractMap.SimpleEntry<String, KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   265
                getKeystoreForWriting(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   266
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   267
        if (pair == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   268
            throw new KeyStoreException("Error setting key entry for '" +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   269
                alias + "'");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   270
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   271
        String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   272
        Map.Entry<String, KeyStore> keystore = pair.getValue();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   273
        keystore.getValue().setKeyEntry(entryAlias, key, password, chain);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   274
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   275
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   276
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   277
     * Assigns the given key (that has already been protected) to the given
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   278
     * alias.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   279
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   280
     * <p>If the protected key is of type
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   281
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   282
     * certificate chain certifying the corresponding public key. If the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   283
     * underlying keystore implementation is of type <code>jks</code>,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   284
     * <code>key</code> must be encoded as an
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   285
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   286
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   287
     * <p>If the given alias already exists, the keystore information
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   288
     * associated with it is overridden by the given key (and possibly
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   289
     * certificate chain).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   290
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   291
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   292
     * @param key the key (in protected format) to be associated with the alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   293
     * @param chain the certificate chain for the corresponding public
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   294
     * key (only useful if the protected key is of type
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   295
     * <code>java.security.PrivateKey</code>).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   296
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   297
     * @exception KeyStoreException if this operation fails.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   298
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   299
    public void engineSetKeyEntry(String alias, byte[] key,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   300
                                  Certificate[] chain)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   301
        throws KeyStoreException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   302
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   303
        AbstractMap.SimpleEntry<String,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   304
            AbstractMap.SimpleEntry<String, KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   305
                getKeystoreForWriting(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   306
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   307
        if (pair == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   308
            throw new KeyStoreException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   309
                "Error setting protected key entry for '" + alias + "'");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   310
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   311
        String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   312
        Map.Entry<String, KeyStore> keystore = pair.getValue();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   313
        keystore.getValue().setKeyEntry(entryAlias, key, chain);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   314
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   315
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   316
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   317
     * Assigns the given certificate to the given alias.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   318
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   319
     * <p>If the given alias already exists in this keystore and identifies a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   320
     * <i>trusted certificate entry</i>, the certificate associated with it is
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   321
     * overridden by the given certificate.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   322
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   323
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   324
     * @param cert the certificate
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   325
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   326
     * @exception KeyStoreException if the given alias already exists and does
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   327
     * not identify a <i>trusted certificate entry</i>, or this operation
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   328
     * fails for some other reason.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   329
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   330
    public void engineSetCertificateEntry(String alias, Certificate cert)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   331
        throws KeyStoreException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   332
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   333
        AbstractMap.SimpleEntry<String,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   334
            AbstractMap.SimpleEntry<String, KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   335
                getKeystoreForWriting(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   336
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   337
        if (pair == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   338
            throw new KeyStoreException("Error setting certificate entry for '"
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   339
                + alias + "'");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   340
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   341
        String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   342
        Map.Entry<String, KeyStore> keystore = pair.getValue();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   343
        keystore.getValue().setCertificateEntry(entryAlias, cert);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   344
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   345
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   346
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   347
     * Deletes the entry identified by the given alias from this keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   348
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   349
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   350
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   351
     * @exception KeyStoreException if the entry cannot be removed.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   352
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   353
    public void engineDeleteEntry(String alias) throws KeyStoreException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   354
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   355
        AbstractMap.SimpleEntry<String,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   356
            AbstractMap.SimpleEntry<String, KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   357
                getKeystoreForWriting(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   358
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   359
        if (pair == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   360
            throw new KeyStoreException("Error deleting entry for '" + alias +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   361
                "'");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   362
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   363
        String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   364
        Map.Entry<String, KeyStore> keystore = pair.getValue();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   365
        keystore.getValue().deleteEntry(entryAlias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   366
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   367
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   368
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   369
     * Lists all the alias names of this keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   370
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   371
     * @return enumeration of the alias names
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   372
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   373
    public Enumeration<String> engineAliases() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   374
        final Iterator<Map.Entry<String, KeyStore>> iterator =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   375
            keystores.entrySet().iterator();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   376
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   377
        return new Enumeration<String>() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   378
            private int index = 0;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   379
            private Map.Entry<String, KeyStore> keystoresEntry = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   380
            private String prefix = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   381
            private Enumeration<String> aliases = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   382
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   383
            public boolean hasMoreElements() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   384
                try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   385
                    if (aliases == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   386
                        if (iterator.hasNext()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   387
                            keystoresEntry = iterator.next();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   388
                            prefix = keystoresEntry.getKey() +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   389
                                entryNameSeparator;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   390
                            aliases = keystoresEntry.getValue().aliases();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   391
                        } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   392
                            return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   393
                        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   394
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   395
                    if (aliases.hasMoreElements()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   396
                        return true;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   397
                    } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   398
                        if (iterator.hasNext()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   399
                            keystoresEntry = iterator.next();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   400
                            prefix = keystoresEntry.getKey() +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   401
                                entryNameSeparator;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   402
                            aliases = keystoresEntry.getValue().aliases();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   403
                        } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   404
                            return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   405
                        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   406
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   407
                } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   408
                    return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   409
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   410
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   411
                return aliases.hasMoreElements();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   412
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   413
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   414
            public String nextElement() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   415
                if (hasMoreElements()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   416
                    return prefix + aliases.nextElement();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   417
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   418
                throw new NoSuchElementException();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   419
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   420
        };
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   421
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   422
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   423
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   424
     * Checks if the given alias exists in this keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   425
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   426
     * @param alias the alias name
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   427
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   428
     * @return true if the alias exists, false otherwise
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   429
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   430
    public boolean engineContainsAlias(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   431
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   432
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   433
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   434
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   435
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   436
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   437
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   438
                if (keystore.containsAlias(entryAlias)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   439
                    return true;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   440
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   441
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   442
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   443
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   444
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   445
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   446
        return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   447
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   448
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   449
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   450
     * Retrieves the number of entries in this keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   451
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   452
     * @return the number of entries in this keystore
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   453
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   454
    public int engineSize() {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   455
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   456
        int size = 0;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   457
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   458
            for (KeyStore keystore : keystores.values()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   459
                size += keystore.size();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   460
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   461
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   462
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   463
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   464
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   465
        return size;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   466
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   467
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   468
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   469
     * Returns true if the entry identified by the given alias is a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   470
     * <i>key entry</i>, and false otherwise.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   471
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   472
     * @return true if the entry identified by the given alias is a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   473
     * <i>key entry</i>, false otherwise.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   474
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   475
    public boolean engineIsKeyEntry(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   476
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   477
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   478
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   479
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   480
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   481
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   482
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   483
                if (keystore.isKeyEntry(entryAlias)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   484
                    return true;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   485
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   486
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   487
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   488
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   489
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   490
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   491
        return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   492
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   493
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   494
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   495
     * Returns true if the entry identified by the given alias is a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   496
     * <i>trusted certificate entry</i>, and false otherwise.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   497
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   498
     * @return true if the entry identified by the given alias is a
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   499
     * <i>trusted certificate entry</i>, false otherwise.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   500
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   501
    public boolean engineIsCertificateEntry(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   502
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   503
        AbstractMap.SimpleEntry<String, Collection<KeyStore>> pair =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   504
            getKeystoresForReading(alias);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   505
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   506
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   507
            String entryAlias = pair.getKey();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   508
            for (KeyStore keystore : pair.getValue()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   509
                if (keystore.isCertificateEntry(entryAlias)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   510
                    return true;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   511
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   512
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   513
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   514
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   515
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   516
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   517
        return false;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   518
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   519
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   520
    /*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   521
     * Returns a keystore entry alias and a list of target keystores.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   522
     * When the supplied alias prefix identifies a keystore then that single
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   523
     * keystore is returned. When no alias prefix is supplied then all the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   524
     * keystores are returned.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   525
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   526
    private AbstractMap.SimpleEntry<String, Collection<KeyStore>>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   527
        getKeystoresForReading(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   528
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   529
        String[] splits = alias.split(this.entryNameSeparatorRegEx, 2);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   530
        if (splits.length == 2) { // prefixed alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   531
            KeyStore keystore = keystores.get(splits[0]);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   532
            if (keystore != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   533
                return new AbstractMap.SimpleEntry<>(splits[1],
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   534
                    (Collection<KeyStore>) Collections.singleton(keystore));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   535
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   536
        } else if (splits.length == 1) { // unprefixed alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   537
            // Check all keystores for the first occurrence of the alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   538
            return new AbstractMap.SimpleEntry<>(alias, keystores.values());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   539
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   540
        return new AbstractMap.SimpleEntry<>("",
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   541
            (Collection<KeyStore>) Collections.<KeyStore>emptyList());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   542
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   543
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   544
    /*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   545
     * Returns a keystore entry alias and a single target keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   546
     * An alias prefix must be supplied.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   547
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   548
    private
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   549
    AbstractMap.SimpleEntry<String, AbstractMap.SimpleEntry<String, KeyStore>>
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   550
        getKeystoreForWriting(String alias) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   551
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   552
        String[] splits = alias.split(this.entryNameSeparator, 2);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   553
        if (splits.length == 2) { // prefixed alias
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   554
            KeyStore keystore = keystores.get(splits[0]);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   555
            if (keystore != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   556
                return new AbstractMap.SimpleEntry<>(splits[1],
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   557
                    new AbstractMap.SimpleEntry<>(splits[0], keystore));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   558
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   559
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   560
        return null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   561
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   562
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   563
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   564
     * Returns the (alias) name of the first keystore entry whose certificate
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   565
     * matches the given certificate.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   566
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   567
     * <p>This method attempts to match the given certificate with each
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   568
     * keystore entry. If the entry being considered
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   569
     * is a <i>trusted certificate entry</i>, the given certificate is
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   570
     * compared to that entry's certificate. If the entry being considered is
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   571
     * a <i>key entry</i>, the given certificate is compared to the first
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   572
     * element of that entry's certificate chain (if a chain exists).
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   573
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   574
     * @param cert the certificate to match with.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   575
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   576
     * @return the (alias) name of the first entry with matching certificate,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   577
     * or null if no such entry exists in this keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   578
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   579
    public String engineGetCertificateAlias(Certificate cert) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   580
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   581
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   582
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   583
            String alias = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   584
            for (KeyStore keystore : keystores.values()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   585
                if ((alias = keystore.getCertificateAlias(cert)) != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   586
                    break;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   587
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   588
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   589
            return alias;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   590
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   591
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   592
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   593
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   594
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   595
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   596
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   597
     * Stores this keystore to the given output stream, and protects its
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   598
     * integrity with the given password.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   599
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   600
     * @param stream the output stream to which this keystore is written.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   601
     * @param password the password to generate the keystore integrity check
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   602
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   603
     * @exception IOException if there was an I/O problem with data
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   604
     * @exception NoSuchAlgorithmException if the appropriate data integrity
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   605
     * algorithm could not be found
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   606
     * @exception CertificateException if any of the certificates included in
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   607
     * the keystore data could not be stored
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   608
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   609
    public void engineStore(OutputStream stream, char[] password)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   610
        throws IOException, NoSuchAlgorithmException, CertificateException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   611
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   612
        // Support storing to a stream only when a single keystore has been
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   613
        // configured
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   614
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   615
            if (keystores.size() == 1) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   616
                keystores.values().iterator().next().store(stream, password);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   617
                return;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   618
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   619
        } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   620
            throw new IllegalStateException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   621
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   622
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   623
        throw new UnsupportedOperationException(
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   624
            "This keystore must be stored using a DomainLoadStoreParameter");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   625
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   626
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   627
    @Override
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   628
    public void engineStore(KeyStore.LoadStoreParameter param)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   629
        throws IOException, NoSuchAlgorithmException, CertificateException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   630
    {
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   631
        if (param instanceof DomainLoadStoreParameter) {
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   632
            DomainLoadStoreParameter domainParameter =
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   633
                (DomainLoadStoreParameter) param;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   634
            List<KeyStoreBuilderComponents> builders = getBuilders(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   635
                domainParameter.getConfiguration(),
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   636
                    domainParameter.getProtectionParams());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   637
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   638
            for (KeyStoreBuilderComponents builder : builders) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   639
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   640
                try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   641
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   642
                    KeyStore.ProtectionParameter pp = builder.protection;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   643
                    if (!(pp instanceof KeyStore.PasswordProtection)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   644
                        throw new KeyStoreException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   645
                            new IllegalArgumentException("ProtectionParameter" +
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   646
                                " must be a KeyStore.PasswordProtection"));
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   647
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   648
                    char[] password =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   649
                        ((KeyStore.PasswordProtection) builder.protection)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   650
                            .getPassword();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   651
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   652
                    // Store the keystores
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   653
                    KeyStore keystore = keystores.get(builder.name);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   654
20777
ad30bd042ffd 8026301: DomainKeyStore doesn't cleanup correctly when storing to keystore
vinnie
parents: 20743
diff changeset
   655
                    try (FileOutputStream stream =
ad30bd042ffd 8026301: DomainKeyStore doesn't cleanup correctly when storing to keystore
vinnie
parents: 20743
diff changeset
   656
                        new FileOutputStream(builder.file)) {
ad30bd042ffd 8026301: DomainKeyStore doesn't cleanup correctly when storing to keystore
vinnie
parents: 20743
diff changeset
   657
ad30bd042ffd 8026301: DomainKeyStore doesn't cleanup correctly when storing to keystore
vinnie
parents: 20743
diff changeset
   658
                        keystore.store(stream, password);
ad30bd042ffd 8026301: DomainKeyStore doesn't cleanup correctly when storing to keystore
vinnie
parents: 20743
diff changeset
   659
                    }
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   660
                } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   661
                    throw new IOException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   662
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   663
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   664
        } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   665
            throw new UnsupportedOperationException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   666
                "This keystore must be stored using a " +
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   667
                "DomainLoadStoreParameter");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   668
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   669
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   670
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   671
    /**
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   672
     * Loads the keystore from the given input stream.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   673
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   674
     * <p>If a password is given, it is used to check the integrity of the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   675
     * keystore data. Otherwise, the integrity of the keystore is not checked.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   676
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   677
     * @param stream the input stream from which the keystore is loaded
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   678
     * @param password the (optional) password used to check the integrity of
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   679
     * the keystore.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   680
     *
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   681
     * @exception IOException if there is an I/O or format problem with the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   682
     * keystore data
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   683
     * @exception NoSuchAlgorithmException if the algorithm used to check
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   684
     * the integrity of the keystore cannot be found
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   685
     * @exception CertificateException if any of the certificates in the
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   686
     * keystore could not be loaded
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   687
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   688
    public void engineLoad(InputStream stream, char[] password)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   689
        throws IOException, NoSuchAlgorithmException, CertificateException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   690
    {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   691
        // Support loading from a stream only for a JKS or default type keystore
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   692
        try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   693
            KeyStore keystore = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   694
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   695
            try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   696
                keystore = KeyStore.getInstance("JKS");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   697
                keystore.load(stream, password);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   698
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   699
            } catch (Exception e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   700
                // Retry
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   701
                if (!"JKS".equalsIgnoreCase(DEFAULT_KEYSTORE_TYPE)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   702
                    keystore = KeyStore.getInstance(DEFAULT_KEYSTORE_TYPE);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   703
                    keystore.load(stream, password);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   704
                } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   705
                    throw e;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   706
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   707
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   708
            String keystoreName = DEFAULT_STREAM_PREFIX + streamCounter++;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   709
            keystores.put(keystoreName, keystore);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   710
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   711
        } catch (Exception e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   712
            throw new UnsupportedOperationException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   713
                "This keystore must be loaded using a " +
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   714
                "DomainLoadStoreParameter");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   715
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   716
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   717
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   718
    @Override
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   719
    public void engineLoad(KeyStore.LoadStoreParameter param)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   720
        throws IOException, NoSuchAlgorithmException, CertificateException
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   721
    {
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   722
        if (param instanceof DomainLoadStoreParameter) {
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   723
            DomainLoadStoreParameter domainParameter =
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   724
                (DomainLoadStoreParameter) param;
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   725
            List<KeyStoreBuilderComponents> builders = getBuilders(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   726
                domainParameter.getConfiguration(),
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   727
                    domainParameter.getProtectionParams());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   728
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   729
            for (KeyStoreBuilderComponents builder : builders) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   730
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   731
                try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   732
                    // Load the keystores (file-based and non-file-based)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   733
                    if (builder.file != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   734
                        keystores.put(builder.name,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   735
                            KeyStore.Builder.newInstance(builder.type,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   736
                                builder.provider, builder.file,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   737
                                builder.protection)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   738
                                    .getKeyStore());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   739
                    } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   740
                        keystores.put(builder.name,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   741
                            KeyStore.Builder.newInstance(builder.type,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   742
                                builder.provider, builder.protection)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   743
                                    .getKeyStore());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   744
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   745
                } catch (KeyStoreException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   746
                    throw new IOException(e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   747
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   748
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   749
        } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   750
            throw new UnsupportedOperationException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   751
                "This keystore must be loaded using a " +
20743
cd866b3875d8 8008171: Refactor KeyStore.DomainLoadStoreParameter as a standalone class
vinnie
parents: 15664
diff changeset
   752
                "DomainLoadStoreParameter");
15664
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   753
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   754
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   755
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   756
    /*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   757
     * Parse a keystore domain configuration file and associated collection
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   758
     * of keystore passwords to create a collection of KeyStore.Builder.
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   759
     */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   760
    private List<KeyStoreBuilderComponents> getBuilders(URI configuration,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   761
        Map<String, KeyStore.ProtectionParameter> passwords)
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   762
            throws IOException {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   763
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   764
        PolicyParser parser = new PolicyParser(true); // expand properties
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   765
        Collection<PolicyParser.DomainEntry> domains = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   766
        List<KeyStoreBuilderComponents> builders = new ArrayList<>();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   767
        String uriDomain = configuration.getFragment();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   768
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   769
        try (InputStreamReader configurationReader =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   770
            new InputStreamReader(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   771
                PolicyUtil.getInputStream(configuration.toURL()), "UTF-8")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   772
            parser.read(configurationReader);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   773
            domains = parser.getDomainEntries();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   774
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   775
        } catch (MalformedURLException mue) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   776
            throw new IOException(mue);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   777
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   778
        } catch (PolicyParser.ParsingException pe) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   779
            throw new IOException(pe);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   780
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   781
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   782
        for (PolicyParser.DomainEntry domain : domains) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   783
            Map<String, String> domainProperties = domain.getProperties();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   784
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   785
            if (uriDomain != null &&
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   786
                (!uriDomain.equalsIgnoreCase(domain.getName()))) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   787
                continue; // skip this domain
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   788
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   789
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   790
            if (domainProperties.containsKey(ENTRY_NAME_SEPARATOR)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   791
                this.entryNameSeparator =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   792
                    domainProperties.get(ENTRY_NAME_SEPARATOR);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   793
                // escape any regex meta characters
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   794
                char ch = 0;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   795
                StringBuilder s = new StringBuilder();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   796
                for (int i = 0; i < this.entryNameSeparator.length(); i++) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   797
                    ch = this.entryNameSeparator.charAt(i);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   798
                    if (REGEX_META.indexOf(ch) != -1) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   799
                        s.append('\\');
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   800
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   801
                    s.append(ch);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   802
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   803
                this.entryNameSeparatorRegEx = s.toString();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   804
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   805
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   806
            Collection<PolicyParser.KeyStoreEntry> keystores =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   807
                domain.getEntries();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   808
            for (PolicyParser.KeyStoreEntry keystore : keystores) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   809
                String keystoreName = keystore.getName();
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   810
                Map<String, String> properties =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   811
                    new HashMap<>(domainProperties);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   812
                properties.putAll(keystore.getProperties());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   813
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   814
                String keystoreType = DEFAULT_KEYSTORE_TYPE;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   815
                if (properties.containsKey(KEYSTORE_TYPE)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   816
                    keystoreType = properties.get(KEYSTORE_TYPE);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   817
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   818
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   819
                Provider keystoreProvider = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   820
                if (properties.containsKey(KEYSTORE_PROVIDER_NAME)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   821
                    String keystoreProviderName =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   822
                        properties.get(KEYSTORE_PROVIDER_NAME);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   823
                    keystoreProvider =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   824
                        Security.getProvider(keystoreProviderName);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   825
                    if (keystoreProvider == null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   826
                        throw new IOException("Error locating JCE provider: " +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   827
                            keystoreProviderName);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   828
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   829
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   830
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   831
                File keystoreFile = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   832
                if (properties.containsKey(KEYSTORE_URI)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   833
                    String uri = properties.get(KEYSTORE_URI);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   834
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   835
                    try {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   836
                        if (uri.startsWith("file://")) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   837
                            keystoreFile = new File(new URI(uri));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   838
                        } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   839
                            keystoreFile = new File(uri);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   840
                        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   841
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   842
                    } catch (URISyntaxException | IllegalArgumentException e) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   843
                        throw new IOException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   844
                            "Error processing keystore property: " +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   845
                                "keystoreURI=\"" + uri + "\"", e);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   846
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   847
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   848
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   849
                KeyStore.ProtectionParameter keystoreProtection = null;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   850
                if (passwords.containsKey(keystoreName)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   851
                    keystoreProtection = passwords.get(keystoreName);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   852
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   853
                } else if (properties.containsKey(KEYSTORE_PASSWORD_ENV)) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   854
                    String env = properties.get(KEYSTORE_PASSWORD_ENV);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   855
                    String pwd = System.getenv(env);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   856
                    if (pwd != null) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   857
                        keystoreProtection =
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   858
                            new KeyStore.PasswordProtection(pwd.toCharArray());
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   859
                    } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   860
                        throw new IOException(
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   861
                            "Error processing keystore property: " +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   862
                                "keystorePasswordEnv=\"" + env + "\"");
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   863
                    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   864
                } else {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   865
                    keystoreProtection = new KeyStore.PasswordProtection(null);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   866
                }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   867
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   868
                builders.add(new KeyStoreBuilderComponents(keystoreName,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   869
                    keystoreType, keystoreProvider, keystoreFile,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   870
                    keystoreProtection));
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   871
            }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   872
            break; // skip other domains
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   873
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   874
        if (builders.isEmpty()) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   875
            throw new IOException("Error locating domain configuration data " +
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   876
                "for: " + configuration);
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   877
        }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   878
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   879
        return builders;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   880
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   881
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   882
/*
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   883
 * Utility class that holds the components used to construct a KeyStore.Builder
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   884
 */
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   885
class KeyStoreBuilderComponents {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   886
    String name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   887
    String type;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   888
    Provider provider;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   889
    File file;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   890
    KeyStore.ProtectionParameter protection;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   891
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   892
    KeyStoreBuilderComponents(String name, String type, Provider provider,
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   893
        File file, KeyStore.ProtectionParameter protection) {
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   894
        this.name = name;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   895
        this.type = type;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   896
        this.provider = provider;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   897
        this.file = file;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   898
        this.protection = protection;
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   899
    }
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   900
}
e33b115f1981 8007755: Support the logical grouping of keystores
vinnie
parents:
diff changeset
   901
}