jdk/src/windows/classes/sun/security/mscapi/KeyStore.java
author vinnie
Thu, 21 Apr 2011 19:05:29 +0100
changeset 9508 310b4f6c8e61
parent 5506 202f599c92aa
child 10336 0bb1999251f8
permissions -rw-r--r--
6732372: Some MSCAPI native methods not returning correct exceptions. Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
     2
 * Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.mscapi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.math.BigInteger;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.AccessController;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    34
import java.security.InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.KeyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.SecurityPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.CertificateEncodingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.interfaces.RSAPrivateCrtKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.util.ArrayList;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.util.Collection;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import java.util.Date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
import java.util.Iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.util.UUID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
import sun.security.action.GetPropertyAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * Implementation of key store for Windows using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
abstract class KeyStore extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final class MY extends KeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        public MY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            super("MY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    public static final class ROOT extends KeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        public ROOT() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            super("ROOT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    class KeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        private Key privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        private X509Certificate certChain[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        private String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        KeyEntry(Key key, X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            this(null, key, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        KeyEntry(String alias, Key key, X509Certificate[] chain) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            this.privateKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            this.certChain = chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
             * The default alias for both entry types is derived from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
             * hash value intrinsic to the first certificate in the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
             if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                 this.alias = Integer.toString(chain[0].hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                 this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         * Gets the alias for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        String getAlias()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         * Sets the alias for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        void setAlias(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            // TODO - set friendly name prop in cert store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
         * Gets the private key for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        Key getPrivateKey()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         * Sets the private key for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        void setPrivateKey(RSAPrivateCrtKey key)
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   127
            throws InvalidKeyException, KeyStoreException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            byte[] modulusBytes = key.getModulus().toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Adjust key length due to sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            int keyBitLength = (modulusBytes[0] == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                ? (modulusBytes.length - 1) * 8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                : modulusBytes.length * 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            byte[] keyBlob = generatePrivateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                keyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                modulusBytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                key.getPublicExponent().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                key.getPrivateExponent().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                key.getPrimeP().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                key.getPrimeQ().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                key.getPrimeExponentP().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                key.getPrimeExponentQ().toByteArray(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                key.getCrtCoefficient().toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            privateKey = storePrivateKey(keyBlob,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                "{" + UUID.randomUUID().toString() + "}", keyBitLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
         * Gets the certificate chain for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        X509Certificate[] getCertificateChain()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            return certChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
         * Sets the certificate chain for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        void setCertificateChain(X509Certificate[] chain)
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   163
            throws CertificateException, KeyStoreException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                byte[] encoding = chain[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                if (i == 0 && privateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                    storeCertificate(getName(), alias, encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                        encoding.length, privateKey.getHCryptProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                        privateKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                    storeCertificate(getName(), alias, encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                        encoding.length, 0L, 0L); // no private key to attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            certChain = chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * An X.509 certificate factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * Used to create an X.509 certificate from its DER-encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private CertificateFactory certificateFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Compatibility mode: for applications that assume keystores are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * stream-based this mode tolerates (but ignores) a non-null stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * or password parameter when passed to the load or store methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        "sun.security.mscapi.keyStoreCompatibilityMode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private final boolean keyStoreCompatibilityMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * The keystore entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private Collection<KeyEntry> entries = new ArrayList<KeyEntry>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * The keystore name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * Case is not significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    private final String storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    KeyStore(String storeName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        // Get the compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        String prop =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            AccessController.doPrivileged(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                new GetPropertyAction(KEYSTORE_COMPATIBILITY_MODE_PROP));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        if ("false".equalsIgnoreCase(prop)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            keyStoreCompatibilityMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            keyStoreCompatibilityMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        this.storeName = storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Returns the key associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * a password must be supplied. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * <code>password</code>.  The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * and reject a non-null <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * or does not identify a <i>key entry</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     * key cannot be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * or if compatibility mode is disabled and <code>password</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @exception UnrecoverableKeyException if the key cannot be recovered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    public java.security.Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            throw new UnrecoverableKeyException("Password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        if (engineIsKeyEntry(alias) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            if (alias.equals(entry.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                return entry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
     * does not exist or does not contain a certificate chain (i.e., the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
     * alias identifies either a <i>trusted certificate entry</i> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * <i>key entry</i> without a certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    public Certificate[] engineGetCertificateChain(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            if (alias.equals(entry.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
                X509Certificate[] certChain = entry.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                return certChain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * <p>If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * <i>trusted certificate entry</i>, the certificate associated with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * entry is returned. If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <i>key entry</i>, the first element of the certificate chain of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * entry is returned, or null if that entry does not have a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    public Certificate engineGetCertificate(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if (alias.equals(entry.getAlias()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                X509Certificate[] certChain = entry.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                return certChain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * Stores the given private key and associated certificate chain in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * <p>The given java.security.PrivateKey <code>key</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * associated with it is overridden by the given key and certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * chain. Otherwise, a new entry is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * a password must be supplied. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * <code>password</code>.  The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
     * and reject a non-null <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * @param key the private key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *        key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     *        <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * @exception KeyStoreException if the given key is not a private key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * cannot be protected, or if compatibility mode is disabled and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>password</code> is non-null, or if this operation fails for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    public void engineSetKeyEntry(String alias, java.security.Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        char[] password, Certificate[] chain) throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            throw new KeyStoreException("Password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            KeyEntry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
            for (KeyEntry e : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                if (alias.equals(e.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    entry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            if (! found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
                entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                    //TODO new KeyEntry(alias, key, (X509Certificate[]) chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
                    new KeyEntry(alias, null, (X509Certificate[]) chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                entries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            entry.setAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            try {
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   411
                entry.setPrivateKey((RSAPrivateCrtKey) key);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
                entry.setCertificateChain((X509Certificate[]) chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
                throw new KeyStoreException(ce);
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   416
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   417
            } catch (InvalidKeyException ike) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   418
                throw new KeyStoreException(ike);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
                "Cannot assign the key to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * certificate chain certifying the corresponding public key. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * underlying keystore implementation is of type <code>jks</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * <code>key</code> must be encoded as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    public void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
            "Cannot assign the encoded key to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * <p>If the given alias already exists in this keystore and identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * <i>trusted certificate entry</i>, the certificate associated with it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @exception KeyStoreException if the given alias already exists and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * not identify a <i>trusted certificate entry</i>, or this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
    public void engineSetCertificateEntry(String alias, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        if (cert instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
            // TODO - build CryptoAPI chain?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            X509Certificate[] chain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                new X509Certificate[]{ (X509Certificate) cert };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
            KeyEntry entry = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
            for (KeyEntry e : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
                if (alias.equals(e.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
                    entry = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            if (! found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                    new KeyEntry(alias, null, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                entries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
            if (entry.getPrivateKey() == null) { // trusted-cert entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
                entry.setAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
                    entry.setCertificateChain(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                    throw new KeyStoreException(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                "Cannot assign the certificate to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (alias.equals(entry.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
                // Get end-entity certificate and remove from system cert store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                X509Certificate[] certChain = entry.getCertificateChain();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                if (certChain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                        byte[] encoding = certChain[0].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                        removeCertificate(getName(), alias, encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
                            encoding.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   545
                    } catch (CertificateException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                        throw new KeyStoreException("Cannot remove entry: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                            e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                Key privateKey = entry.getPrivateKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                if (privateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                    destroyKeyContainer(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
                        Key.getContainerName(privateKey.getHCryptProvider()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                entries.remove(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
    public Enumeration engineAliases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
        final Iterator iter = entries.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        return new Enumeration()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            public boolean hasMoreElements()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                return iter.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
            public Object nextElement()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                KeyEntry entry = (KeyEntry) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                return entry.getAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public boolean engineContainsAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
        for (Enumeration enumerator = engineAliases();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
            enumerator.hasMoreElements();)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
            String a = (String) enumerator.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
            if (a.equals(alias))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
    public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * <i>key entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     * <i>key entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    public boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
            if (alias.equals(entry.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                return entry.getPrivateKey() != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * <i>trusted certificate entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     * <i>trusted certificate entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public boolean engineIsCertificateEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
            if (alias.equals(entry.getAlias())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                return entry.getPrivateKey() == null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * keystore entry. If the entry being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * is a <i>trusted certificate entry</i>, the given certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * compared to that entry's certificate. If the entry being considered is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * a <i>key entry</i>, the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * element of that entry's certificate chain (if a chain exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @return the (alias) name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
    public String engineGetCertificateAlias(Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        for (KeyEntry entry : entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            if (entry.certChain != null && entry.certChain[0].equals(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                return entry.getAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     * engineStore is currently a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
     * Entries are stored during engineSetEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
     * keystores are stream-based. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
     * <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     * and reject a non-null <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
     * @param stream the output stream, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
     * @exception IOException if compatibility mode is disabled and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * parameter is non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
    public void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        if (stream != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
            throw new IOException("Keystore output stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            throw new IOException("Keystore password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * Loads the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * keystores are stream-based. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * and reject a non-null <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param stream the input stream, which should be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param password the password, which should be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * keystore data. Or if compatibility mode is disabled and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * parameter is non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     * @exception SecurityException if the security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     *  pass, where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     *  this provider's <code>getName</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    public void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        if (stream != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw new IOException("Keystore input stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            throw new IOException("Keystore password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
         * Use the same security check as AuthProvider.login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            sm.checkPermission(new SecurityPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                "authProvider.SunMSCAPI"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        // Clear all key entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   762
        try {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   763
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   764
            // Load keys and/or certificate chains
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   765
            loadKeysOrCertificateChains(getName(), entries);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   766
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   767
        } catch (KeyStoreException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   768
            throw new IOException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   769
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Generates a certificate chain from the collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     * certificates and stores the result into a key entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
    private void generateCertificateChain(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
        Collection certCollection, Collection<KeyEntry> entries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
            X509Certificate[] certChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                new X509Certificate[certCollection.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            for (Iterator iter=certCollection.iterator(); iter.hasNext(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
                certChain[i] = (X509Certificate) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
            KeyEntry entry = new KeyEntry(alias, null, certChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            // Add cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            entries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
        catch (Throwable e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            // Ignore the exception and skip this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            // TODO - throw CertificateException?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     * Generates RSA key and certificate chain from the private key handle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
     * collection of certificates and stores the result into key entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    private void generateRSAKeyAndCertificateChain(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        long hCryptProv, long hCryptKey, int keyLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        Collection certCollection, Collection<KeyEntry> entries)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
            X509Certificate[] certChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                new X509Certificate[certCollection.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
            int i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            for (Iterator iter=certCollection.iterator(); iter.hasNext(); i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                certChain[i] = (X509Certificate) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
            KeyEntry entry = new KeyEntry(alias, new RSAPrivateKey(hCryptProv,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                hCryptKey, keyLength), certChain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
            // Add cert chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            entries.add(entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
        catch (Throwable e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
            // Ignore the exception and skip this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            // TODO - throw CertificateException?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * Generates certificates from byte data and stores into cert collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     * @param data Byte data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @param certCollection Collection of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    private void generateCertificate(byte[] data, Collection certCollection)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
        try
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            // Obtain certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            if (certificateFactory == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                certificateFactory = CertificateFactory.getInstance("X.509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            // Generate certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
            Collection c = certificateFactory.generateCertificates(bis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            certCollection.addAll(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        catch (CertificateException e)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
            // Ignore the exception and skip this certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            // TODO - throw CertificateException?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        catch (Throwable te)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            // Ignore the exception and skip this certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            // TODO - throw CertificateException?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
     * Returns the name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    private String getName()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        return storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * Load keys and/or certificates from keystore into Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     * @param name Name of keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @param entries Collection of key/certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    private native void loadKeysOrCertificateChains(String name,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   882
        Collection<KeyEntry> entries) throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * Stores a DER-encoded certificate into the certificate store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     * @param name Name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @param alias Name of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     * @param encoding DER-encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
    private native void storeCertificate(String name, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        byte[] encoding, int encodingLength, long hCryptProvider,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   893
        long hCryptKey) throws CertificateException, KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
     * Removes the certificate from the certificate store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
     * @param name Name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * @param alias Name of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     * @param encoding DER-encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
    private native void removeCertificate(String name, String alias,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   903
        byte[] encoding, int encodingLength)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   904
            throws CertificateException, KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
     * Destroys the key container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
     * @param keyContainerName The name of the key container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
     */
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   911
    private native void destroyKeyContainer(String keyContainerName)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   912
        throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * Generates a private-key BLOB from a key's components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
    private native byte[] generatePrivateKeyBlob(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        int keyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
        byte[] modulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
        byte[] publicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
        byte[] privateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
        byte[] primeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        byte[] primeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
        byte[] exponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        byte[] exponentQ,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   926
        byte[] crtCoefficient) throws InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
    private native RSAPrivateKey storePrivateKey(byte[] keyBlob,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   929
        String keyContainerName, int keySize) throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
}