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