src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/CKeyStore.java
author henryjen
Thu, 13 Dec 2018 11:47:35 -0800
changeset 53333 fd6de53a0d6e
parent 53330 src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/KeyStore.java@e8bae92beee3
parent 53007 src/jdk.crypto.mscapi/windows/classes/sun/security/mscapi/KeyStore.java@e2798bf6318a
child 53684 3f054fd85646
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
     2
 * Copyright (c) 2005, 2018, 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.security.AccessController;
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
    33
import java.security.InvalidKeyException;
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    34
import java.security.Key;
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;
23582
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 10336
diff changeset
    37
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.SecurityPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import java.security.cert.CertificateException;
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;
31264
896105040033 8023546: sun/security/mscapi/ShortRSAKey1024.sh fails intermittently
weijun
parents: 25859
diff changeset
    46
import java.util.*;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * Implementation of key store for Windows using the Microsoft Crypto API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    53
abstract class CKeyStore extends KeyStoreSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    55
    public static final class MY extends CKeyStore {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        public MY() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            super("MY");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    61
    public static final class ROOT extends CKeyStore {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        public ROOT() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            super("ROOT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    67
    class KeyEntry {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    68
        private CKey privateKey;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31264
diff changeset
    69
        private X509Certificate[] certChain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        private String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    72
        KeyEntry(CKey key, X509Certificate[] chain) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            this(null, key, chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    76
        KeyEntry(String alias, CKey key, X509Certificate[] chain) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            this.privateKey = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            this.certChain = chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
             * The default alias for both entry types is derived from a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
             * hash value intrinsic to the first certificate in the chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
             if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                 this.alias = Integer.toString(chain[0].hashCode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
             } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                 this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
             }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
         * Gets the alias for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
         */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
    93
        String getAlias() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            return 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
         * Sets the alias for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
         */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   100
        void setAlias(String alias) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            // TODO - set friendly name prop in cert store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            this.alias = alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
         * Gets the private key for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
         */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   108
        CKey getPrivateKey() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
         * Sets the private key for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
         */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   115
        void setRSAPrivateKey(Key k)
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   116
            throws InvalidKeyException, KeyStoreException {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   117
            RSAPrivateCrtKey key = (RSAPrivateCrtKey) k;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            byte[] modulusBytes = key.getModulus().toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            // Adjust key length due to sign bit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            int keyBitLength = (modulusBytes[0] == 0)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   122
                    ? (modulusBytes.length - 1) * 8
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   123
                    : modulusBytes.length * 8;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   125
            byte[] keyBlob = generateRSAPrivateKeyBlob(
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   126
                    keyBitLength,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   127
                    modulusBytes,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   128
                    key.getPublicExponent().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   129
                    key.getPrivateExponent().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   130
                    key.getPrimeP().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   131
                    key.getPrimeQ().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   132
                    key.getPrimeExponentP().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   133
                    key.getPrimeExponentQ().toByteArray(),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   134
                    key.getCrtCoefficient().toByteArray());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   136
            privateKey = storePrivateKey("RSA", Objects.requireNonNull(keyBlob),
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   137
                    "{" + UUID.randomUUID().toString() + "}", keyBitLength);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
         * Gets the certificate chain for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
         */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   143
        X509Certificate[] getCertificateChain() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return certChain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
         * Sets the certificate chain for the keystore entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        void setCertificateChain(X509Certificate[] chain)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   151
                throws CertificateException, KeyStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            for (int i = 0; i < chain.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                byte[] encoding = chain[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                if (i == 0 && privateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    storeCertificate(getName(), alias, encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                        encoding.length, privateKey.getHCryptProvider(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                        privateKey.getHCryptKey());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    storeCertificate(getName(), alias, encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                        encoding.length, 0L, 0L); // no private key to attach
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            certChain = chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        }
40389
c6df8bba0b71 8163896: Finalizing one key of a KeyPair invalidates the other key
igerasim
parents: 36759
diff changeset
   166
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * An X.509 certificate factory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * Used to create an X.509 certificate from its DER-encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    private CertificateFactory certificateFactory = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * Compatibility mode: for applications that assume keystores are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * stream-based this mode tolerates (but ignores) a non-null stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * or password parameter when passed to the load or store methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final String KEYSTORE_COMPATIBILITY_MODE_PROP =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        "sun.security.mscapi.keyStoreCompatibilityMode";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private final boolean keyStoreCompatibilityMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * The keystore entries.
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   186
     * Keys in the map are unique aliases (thus can differ from
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   187
     * KeyEntry.getAlias())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   189
    private Map<String,KeyEntry> entries = new HashMap<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * The keystore name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Case is not significant.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private final String storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   197
    CKeyStore(String storeName) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        // Get the compatibility mode
23582
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 10336
diff changeset
   199
        String prop = AccessController.doPrivileged(
d5fa3327ab3a 8038177: Eliminate unnecessary dependency to sun.security.action
mchung
parents: 10336
diff changeset
   200
            (PrivilegedAction<String>) () -> System.getProperty(KEYSTORE_COMPATIBILITY_MODE_PROP));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        if ("false".equalsIgnoreCase(prop)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            keyStoreCompatibilityMode = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            keyStoreCompatibilityMode = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        this.storeName = storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * Returns the key associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * a password must be supplied. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     * <code>password</code>.  The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * and reject a non-null <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * or does not identify a <i>key entry</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * key cannot be found,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * or if compatibility mode is disabled and <code>password</code> is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * @exception UnrecoverableKeyException if the key cannot be recovered.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    public java.security.Key engineGetKey(String alias, char[] password)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   235
        throws NoSuchAlgorithmException, UnrecoverableKeyException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
            throw new UnrecoverableKeyException("Password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        if (engineIsKeyEntry(alias) == false)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   247
        KeyEntry entry = entries.get(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   248
        return (entry == null)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   249
                ? null
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   250
                : entry.getPrivateKey();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * does not exist or does not contain a certificate chain (i.e., the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * alias identifies either a <i>trusted certificate entry</i> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * <i>key entry</i> without a certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   264
    public Certificate[] engineGetCertificateChain(String alias) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        if (alias == null) {
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
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   269
        KeyEntry entry = entries.get(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   270
        X509Certificate[] certChain = (entry == null)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   271
                ? null
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   272
                : entry.getCertificateChain();
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   273
        return (certChain == null)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   274
                ? null
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   275
                : certChain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * <p>If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     * <i>trusted certificate entry</i>, the certificate associated with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * entry is returned. If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * <i>key entry</i>, the first element of the certificate chain of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * entry is returned, or null if that entry does not have a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   293
    public Certificate engineGetCertificate(String alias) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   298
        KeyEntry entry = entries.get(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   299
        X509Certificate[] certChain = (entry == null)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   300
                ? null
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   301
                : entry.getCertificateChain();
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   302
        return (certChain == null || certChain.length == 0)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   303
                ? null
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   304
                : certChain[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
    public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        return new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * Stores the given private key and associated certificate chain in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     * keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * <p>The given java.security.PrivateKey <code>key</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     * be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * associated with it is overridden by the given key and certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * chain. Otherwise, a new entry is created.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * a password must be supplied. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <code>password</code>.  The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * and reject a non-null <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param key the private key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     *        key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *        <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * @exception KeyStoreException if the given key is not a private key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * cannot be protected, or if compatibility mode is disabled and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * <code>password</code> is non-null, or if this operation fails for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    public void engineSetKeyEntry(String alias, java.security.Key key,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   356
            char[] password, Certificate[] chain) throws KeyStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            throw new KeyStoreException("Password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
        if (key instanceof RSAPrivateCrtKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   367
            KeyEntry entry = entries.get(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
34334
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   369
            X509Certificate[] xchain;
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   370
            if (chain != null) {
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   371
                if (chain instanceof X509Certificate[]) {
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   372
                    xchain = (X509Certificate[]) chain;
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   373
                } else {
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   374
                    xchain = new X509Certificate[chain.length];
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   375
                    System.arraycopy(chain, 0, xchain, 0, chain.length);
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   376
                }
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   377
            } else {
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   378
                xchain = null;
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   379
            }
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   380
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   381
            if (entry == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                entry =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    //TODO new KeyEntry(alias, key, (X509Certificate[]) chain);
34334
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   384
                    new KeyEntry(alias, null, xchain);
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   385
                storeWithUniqueAlias(alias, entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            entry.setAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
            try {
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   391
                entry.setRSAPrivateKey(key);
34334
512c85086978 8143913: MSCAPI keystore should accept Certificate[] in setEntry()
weijun
parents: 33868
diff changeset
   392
                entry.setCertificateChain(xchain);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
            } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                throw new KeyStoreException(ce);
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   396
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   397
            } catch (InvalidKeyException ike) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   398
                throw new KeyStoreException(ike);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
                "Cannot assign the key to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * certificate chain certifying the corresponding public key. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * underlying keystore implementation is of type <code>jks</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * <code>key</code> must be encoded as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    public void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
                                  Certificate[] chain)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   432
            throws KeyStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
            "Cannot assign the encoded key to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * <p>If the given alias already exists in this keystore and identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * <i>trusted certificate entry</i>, the certificate associated with it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * @exception KeyStoreException if the given alias already exists and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * not identify a <i>trusted certificate entry</i>, or this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    public void engineSetCertificateEntry(String alias, Certificate cert)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   452
            throws KeyStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        if (cert instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            // TODO - build CryptoAPI chain?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            X509Certificate[] chain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                new X509Certificate[]{ (X509Certificate) cert };
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   462
            KeyEntry entry = entries.get(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   464
            if (entry == null) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   465
                entry =
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   466
                    new KeyEntry(alias, null, chain);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   467
                storeWithUniqueAlias(alias, entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            if (entry.getPrivateKey() == null) { // trusted-cert entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                entry.setAlias(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    entry.setCertificateChain(chain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                } catch (CertificateException ce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                    throw new KeyStoreException(ce);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
            throw new UnsupportedOperationException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
                "Cannot assign the certificate to the given alias.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   494
    public void engineDeleteEntry(String alias) throws KeyStoreException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            throw new KeyStoreException("alias must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   499
        KeyEntry entry = entries.remove(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   500
        if (entry != null) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   501
            // Get end-entity certificate and remove from system cert store
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   502
            X509Certificate[] certChain = entry.getCertificateChain();
45193
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents: 40389
diff changeset
   503
            if (certChain != null && certChain.length > 0) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   505
                try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   507
                    byte[] encoding = certChain[0].getEncoded();
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   508
                    removeCertificate(getName(), entry.getAlias(), encoding,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                            encoding.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   511
                } catch (CertificateException e) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   512
                    throw new KeyStoreException("Cannot remove entry: ", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                }
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   514
            }
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   515
            CKey privateKey = entry.getPrivateKey();
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   516
            if (privateKey != null) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   517
                destroyKeyContainer(
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   518
                    CKey.getContainerName(privateKey.getHCryptProvider()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   528
    public Enumeration<String> engineAliases() {
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   529
        final Iterator<String> iter = entries.keySet().iterator();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   531
        return new Enumeration<String>() {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   532
            public boolean hasMoreElements() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                return iter.hasNext();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   536
            public String nextElement() {
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   537
                return iter.next();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
    public boolean engineContainsAlias(String alias) {
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   550
        return entries.containsKey(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
    public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
        return entries.size();
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
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <i>key entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * <i>key entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    public boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
        if (alias == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   575
        KeyEntry entry = entries.get(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   576
        return entry != null && entry.getPrivateKey() != null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * <i>trusted certificate entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * <i>trusted certificate entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     */
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   586
    public boolean engineIsCertificateEntry(String alias) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   587
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   588
        if (alias == null) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   589
            return false;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   592
        KeyEntry entry = entries.get(alias);
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   593
        return entry != null && entry.getPrivateKey() == null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * keystore entry. If the entry being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * is a <i>trusted certificate entry</i>, the given certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * compared to that entry's certificate. If the entry being considered is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * a <i>key entry</i>, the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     * element of that entry's certificate chain (if a chain exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * @return the (alias) name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     */
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   612
    public String engineGetCertificateAlias(Certificate cert) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   613
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   614
        for (Map.Entry<String,KeyEntry> mapEntry : entries.entrySet()) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   615
            KeyEntry entry = mapEntry.getValue();
45193
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents: 40389
diff changeset
   616
            if (entry.certChain != null &&
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents: 40389
diff changeset
   617
                entry.certChain.length > 0 &&
a4c62ccf8688 8172244: AIOOBE in KeyStore.getCertificateAlias on Windows
apetcher
parents: 40389
diff changeset
   618
                entry.certChain[0].equals(cert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                return entry.getAlias();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * engineStore is currently a no-op.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * Entries are stored during engineSetEntry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     * keystores are stream-based. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     * and reject a non-null <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     * @param stream the output stream, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @param password the password, which should be <code>null</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @exception IOException if compatibility mode is disabled and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * parameter is non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    public void engineStore(OutputStream stream, char[] password)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   646
            throws IOException, NoSuchAlgorithmException, CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
        if (stream != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            throw new IOException("Keystore output stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            throw new IOException("Keystore password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * Loads the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * A compatibility mode is supported for applications that assume
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * keystores are stream-based. It permits (but ignores) a non-null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * The mode is enabled by default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     * Set the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * <code>sun.security.mscapi.keyStoreCompatibilityMode</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * system property to <code>false</code> to disable compatibility mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * and reject a non-null <code>stream</code> or <code>password</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param stream the input stream, which should be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * @param password the password, which should be <code>null</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * keystore data. Or if compatibility mode is disabled and either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * parameter is non-null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
     * @exception SecurityException if the security check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
     *  <code>SecurityPermission("authProvider.<i>name</i>")</code> does not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
     *  pass, where <i>name</i> is the value returned by
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     *  this provider's <code>getName</code> method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public void engineLoad(InputStream stream, char[] password)
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   684
            throws IOException, NoSuchAlgorithmException, CertificateException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (stream != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            throw new IOException("Keystore input stream must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
        if (password != null && !keyStoreCompatibilityMode) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            throw new IOException("Keystore password must be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
         * Use the same security check as AuthProvider.login
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        SecurityManager sm = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
        if (sm != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            sm.checkPermission(new SecurityPermission(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                "authProvider.SunMSCAPI"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        // Clear all key entries
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   705
        try {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   706
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   707
            // Load keys and/or certificate chains
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   708
            loadKeysOrCertificateChains(getName());
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   709
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   710
        } catch (KeyStoreException e) {
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   711
            throw new IOException(e);
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   712
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
    /**
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   716
     * Stores the given entry into the map, making sure
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   717
     * the alias, used as the key is unique.
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   718
     * If the same alias already exists, it tries to append
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   719
     * a suffix  (1), (2), etc to it until it finds a unique
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   720
     * value.
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   721
     */
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   722
    private void storeWithUniqueAlias(String alias, KeyEntry entry) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   723
        String uniqAlias = alias;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   724
        int uniqNum = 1;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   725
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   726
        while (true) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   727
            if (entries.putIfAbsent(uniqAlias, entry) == null) {
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   728
                break;
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   729
            }
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   730
            uniqAlias = alias + " (" + (uniqNum++) + ")";
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   731
        }
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   732
    }
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   733
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   734
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   735
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * Generates a certificate chain from the collection of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     * certificates and stores the result into a key entry.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   738
     * <p>
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   739
     * This method is called by native codes in security.cpp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    private void generateCertificateChain(String alias,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   742
            Collection<? extends Certificate> certCollection) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   743
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
            X509Certificate[] certChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                new X509Certificate[certCollection.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
            int i = 0;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   748
            for (Iterator<? extends Certificate> iter =
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   749
                    certCollection.iterator(); iter.hasNext(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                certChain[i] = (X509Certificate) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   753
            storeWithUniqueAlias(alias,
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   754
                    new KeyEntry(alias, null, certChain));
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   755
        } catch (Throwable e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
            // Ignore the exception and skip this entry
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   757
            // If e is thrown, remember to deal with it in
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   758
            // native code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
    /**
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   763
     * Generates key and certificate chain from the private key handle,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
     * collection of certificates and stores the result into key entries.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   765
     * <p>
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   766
     * This method is called by native codes in security.cpp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
     */
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   768
    private void generateKeyAndCertificateChain(boolean isRSA, String alias,
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   769
            long hCryptProv, long hCryptKey, int keyLength,
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   770
            Collection<? extends Certificate> certCollection) {
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   771
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            X509Certificate[] certChain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                new X509Certificate[certCollection.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
            int i = 0;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   776
            for (Iterator<? extends Certificate> iter =
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   777
                    certCollection.iterator(); iter.hasNext(); i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                certChain[i] = (X509Certificate) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
            }
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   780
            storeWithUniqueAlias(alias, new KeyEntry(alias,
53007
e2798bf6318a 8213010: Supporting keys created with certmgr.exe
weijun
parents: 53006
diff changeset
   781
                    CPrivateKey.of(isRSA ? "RSA" : "EC", hCryptProv, hCryptKey, keyLength),
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   782
                    certChain));
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   783
        } catch (Throwable e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
            // Ignore the exception and skip this entry
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   785
            // If e is thrown, remember to deal with it in
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   786
            // native code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * Generates certificates from byte data and stores into cert collection.
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   792
     * <p>
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   793
     * This method is called by native codes in security.cpp.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
     * @param data Byte data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
     * @param certCollection Collection of certificates.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   798
    private void generateCertificate(byte[] data,
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   799
        Collection<Certificate> certCollection) {
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   800
        try {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            ByteArrayInputStream bis = new ByteArrayInputStream(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
            // Obtain certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
            if (certificateFactory == null) {
33868
9c1bde39fe18 8139436: sun.security.mscapi.KeyStore might load incomplete data
clanger
parents: 31538
diff changeset
   805
                certificateFactory = CertificateFactory.getInstance("X.509", "SUN");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
            // Generate certificate
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   809
            Collection<? extends Certificate> c =
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 9508
diff changeset
   810
                    certificateFactory.generateCertificates(bis);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            certCollection.addAll(c);
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   812
        } catch (CertificateException e) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
            // Ignore the exception and skip this certificate
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   814
            // If e is thrown, remember to deal with it in
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   815
            // native code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
        catch (Throwable te)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            // Ignore the exception and skip this certificate
53330
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   820
            // If e is thrown, remember to deal with it in
e8bae92beee3 8210870: Libsunmscapi improved interactions
weijun
parents: 47216
diff changeset
   821
            // native code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * Returns the name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   828
    private String getName() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        return storeName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
     * Load keys and/or certificates from keystore into Collection.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
     * @param name Name of keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     */
36759
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   837
    private native void loadKeysOrCertificateChains(String name)
07dc1868fd1e 6483657: MSCAPI provider does not create unique alias names
igerasim
parents: 34334
diff changeset
   838
            throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * Stores a DER-encoded certificate into the certificate store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @param name Name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * @param alias Name of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * @param encoding DER-encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
    private native void storeCertificate(String name, String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
        byte[] encoding, int encodingLength, long hCryptProvider,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   849
        long hCryptKey) throws CertificateException, KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     * Removes the certificate from the certificate store
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * @param name Name of the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * @param alias Name of the certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * @param encoding DER-encoded certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    private native void removeCertificate(String name, String alias,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   859
        byte[] encoding, int encodingLength)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   860
            throws CertificateException, KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * Destroys the key container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
     * @param keyContainerName The name of the key container.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
     */
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   867
    private native void destroyKeyContainer(String keyContainerName)
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   868
        throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * Generates a private-key BLOB from a key's components.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     */
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   873
    private native byte[] generateRSAPrivateKeyBlob(
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        int keyBitLength,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
        byte[] modulus,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
        byte[] publicExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        byte[] privateExponent,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        byte[] primeP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
        byte[] primeQ,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
        byte[] exponentP,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
        byte[] exponentQ,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   882
        byte[] crtCoefficient) throws InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
53006
4debb3321e65 8213009: Refactoring existing SunMSCAPI classes
weijun
parents: 47216
diff changeset
   884
    private native CPrivateKey storePrivateKey(String alg, byte[] keyBlob,
9508
310b4f6c8e61 6732372: Some MSCAPI native methods not returning correct exceptions.
vinnie
parents: 5506
diff changeset
   885
        String keyContainerName, int keySize) throws KeyStoreException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
}