src/java.base/share/classes/sun/security/provider/JavaKeyStore.java
author weijun
Wed, 01 Aug 2018 13:35:08 +0800
changeset 51272 9d92ff04a29c
parent 47216 71c04702a3d5
child 51293 53c3b460503c
permissions -rw-r--r--
8208602: Cannot read PEM X.509 cert if there is whitespace after the header or footer Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 1997, 2014, 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: 4989
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: 4989
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: 4989
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4989
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4989
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.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.*;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.pkcs.EncryptedPrivateKeyInfo;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    36
import sun.security.pkcs12.PKCS12KeyStore;
32933
83d50f1247bb 8138978: Examine usages of sun.misc.IOUtils
chegar
parents: 31538
diff changeset
    37
import sun.security.util.IOUtils;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    38
import sun.security.util.KeyStoreDelegator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * This class provides the keystore implementation referred to as "JKS".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * @author David Brownell
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * @see KeyProtector
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @see java.security.KeyStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @see KeyTool
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    54
public abstract class JavaKeyStore extends KeyStoreSpi {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // regular JKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    public static final class JKS extends JavaKeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        String convertAlias(String alias) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 7668
diff changeset
    59
            return alias.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // special JKS that uses case sensitive aliases
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public static final class CaseExactJKS extends JavaKeyStore {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        String convertAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
            return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    70
    // special JKS that supports JKS and PKCS12 file formats
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    71
    public static final class DualFormatJKS extends KeyStoreDelegator {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    72
        public DualFormatJKS() {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    73
            super("JKS", JKS.class, "PKCS12", PKCS12KeyStore.class);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    74
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    75
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    76
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    private static final int MAGIC = 0xfeedfeed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static final int VERSION_1 = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final int VERSION_2 = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    // Private keys and their supporting certificate chains
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    private static class KeyEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        Date date; // the creation date of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        byte[] protectedPrivKey;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 28243
diff changeset
    85
        Certificate[] chain;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    // Trusted certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    private static class TrustedCertEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        Date date; // the creation date of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        Certificate cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Private keys and certificates are stored in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Hash entries are keyed by alias names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private final Hashtable<String, Object> entries;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    JavaKeyStore() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        entries = new Hashtable<String, Object>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    // convert an alias to internal form, overridden in subclasses:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    // lower case for regular JKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    // original string for CaseExactJKS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    abstract String convertAlias(String alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * password to recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param password the password for recovering the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * or does not identify a <i>key entry</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * (e.g., the given password is wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    public Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (entry == null || !(entry instanceof KeyEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            throw new UnrecoverableKeyException("Password must not be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        KeyProtector keyProtector = new KeyProtector(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        byte[] encrBytes = ((KeyEntry)entry).protectedPrivKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        EncryptedPrivateKeyInfo encrInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        byte[] plain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            encrInfo = new EncryptedPrivateKeyInfo(encrBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throw new UnrecoverableKeyException("Private key not stored as "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                                                + "PKCS #8 "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                                                + "EncryptedPrivateKeyInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return keyProtector.recover(encrInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * does not exist or does not contain a certificate chain (i.e., the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * alias identifies either a <i>trusted certificate entry</i> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * <i>key entry</i> without a certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public Certificate[] engineGetCertificateChain(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        if (entry != null && entry instanceof KeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            if (((KeyEntry)entry).chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                return ((KeyEntry)entry).chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * <p>If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * <i>trusted certificate entry</i>, the certificate associated with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * entry is returned. If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * <i>key entry</i>, the first element of the certificate chain of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * entry is returned, or null if that entry does not have a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    public Certificate engineGetCertificate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
                return ((TrustedCertEntry)entry).cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                if (((KeyEntry)entry).chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                    return ((KeyEntry)entry).chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                return new Date(((TrustedCertEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                return new Date(((KeyEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * Assigns the given private key to the given alias, protecting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * it with the given password as defined in PKCS8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * <p>The given java.security.PrivateKey <code>key</code> must
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * associated with it is overridden by the given key and certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @param key the private key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * @exception KeyStoreException if the given key is not a private key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * cannot be protected, or this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public void engineSetKeyEntry(String alias, Key key, char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        KeyProtector keyProtector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
        if (!(key instanceof java.security.PrivateKey)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            throw new KeyStoreException("Cannot store non-PrivateKeys");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                KeyEntry entry = new KeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                entry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                // Protect the encoding of the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
                keyProtector = new KeyProtector(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                entry.protectedPrivKey = keyProtector.protect(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
                // clone the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                if ((chain != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    (chain.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    entry.chain = chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    entry.chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                entries.put(convertAlias(alias), entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        } catch (NoSuchAlgorithmException nsae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            throw new KeyStoreException("Key protection algorithm not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            keyProtector = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * certificate chain certifying the corresponding public key. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     * underlying keystore implementation is of type <code>jks</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <code>key</code> must be encoded as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    public void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            // key must be encoded as EncryptedPrivateKeyInfo as defined in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
            // PKCS#8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                new EncryptedPrivateKeyInfo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                throw new KeyStoreException("key is not encoded as "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                                            + "EncryptedPrivateKeyInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            KeyEntry entry = new KeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            entry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            entry.protectedPrivKey = key.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            if ((chain != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                (chain.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                entry.chain = chain.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                entry.chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
            entries.put(convertAlias(alias), entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <p>If the given alias already exists in this keystore and identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * <i>trusted certificate entry</i>, the certificate associated with it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * overridden by the given certificate.
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 cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception KeyStoreException if the given alias already exists and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * not identify a <i>trusted certificate entry</i>, or this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    public void engineSetCertificateEntry(String alias, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            if ((entry != null) && (entry instanceof KeyEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                throw new KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                    ("Cannot overwrite own certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            TrustedCertEntry trustedCertEntry = new TrustedCertEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            trustedCertEntry.cert = cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            trustedCertEntry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            entries.put(convertAlias(alias), trustedCertEntry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    public void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            entries.remove(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
    public Enumeration<String> engineAliases() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        return entries.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    public boolean engineContainsAlias(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        return entries.containsKey(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * <i>key entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * <i>key entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
    public boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if ((entry != null) && (entry instanceof KeyEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <i>trusted certificate entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * <i>trusted certificate entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
    public boolean engineIsCertificateEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
        Object entry = entries.get(convertAlias(alias));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        if ((entry != null) && (entry instanceof TrustedCertEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * keystore entry. If the entry being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * is a <i>trusted certificate entry</i>, the given certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * compared to that entry's certificate. If the entry being considered is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * a <i>key entry</i>, the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * element of that entry's certificate chain (if a chain exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * @return the (alias) name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    public String engineGetCertificateAlias(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        Certificate certElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        for (Enumeration<String> e = entries.keys(); e.hasMoreElements(); ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            Object entry = entries.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                certElem = ((TrustedCertEntry)entry).cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
            } else if (((KeyEntry)entry).chain != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
                certElem = ((KeyEntry)entry).chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            if (certElem.equals(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
                return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return null;
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
     * Stores this keystore to the given output stream, and protects its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     * integrity with the given password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param stream the output stream to which this keystore is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @param password the password to generate the keystore integrity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    public void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             * KEYSTORE FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             * Magic number (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
             * Version of this file format (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             * Count (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             * followed by "count" instances of either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             *      tag=1 (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             *      alias (UTF string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             *      timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             *      encrypted private-key info according to PKCS #8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             *          (integer length followed by encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             *      cert chain (integer count, then certs; for each cert,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             *          integer length followed by encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
             *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
             * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
             *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
             *      tag=2 (big-endian integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
             *      alias (UTF string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
             *      timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
             *      cert (integer length followed by encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             * ended by a keyed SHA1 hash (bytes only) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             *     { password + whitener + preceding body }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            // password is mandatory when storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
            byte[] encoded; // the certificate encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            MessageDigest md = getPreKeyedHash(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
            DataOutputStream dos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                = new DataOutputStream(new DigestOutputStream(stream, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            dos.writeInt(MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            // always write the latest version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            dos.writeInt(VERSION_2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            dos.writeInt(entries.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            for (Enumeration<String> e = entries.keys(); e.hasMoreElements();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                String alias = e.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
                Object entry = entries.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
                if (entry instanceof KeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                    // Store this entry as a KeyEntry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                    dos.writeInt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                    // Write the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
                    dos.writeUTF(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                    // Write the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                    dos.writeLong(((KeyEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    // Write the protected private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                    dos.writeInt(((KeyEntry)entry).protectedPrivKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    dos.write(((KeyEntry)entry).protectedPrivKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                    // Write the certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                    int chainLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                    if (((KeyEntry)entry).chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        chainLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        chainLen = ((KeyEntry)entry).chain.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
                    dos.writeInt(chainLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                    for (int i = 0; i < chainLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        encoded = ((KeyEntry)entry).chain[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
                        dos.writeUTF(((KeyEntry)entry).chain[i].getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        dos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        dos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                    // Store this entry as a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                    dos.writeInt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                    // Write the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                    dos.writeUTF(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                    // Write the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                    dos.writeLong(((TrustedCertEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                    // Write the trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                    encoded = ((TrustedCertEntry)entry).cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                    dos.writeUTF(((TrustedCertEntry)entry).cert.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                    dos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
                    dos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
             * Write the keyed hash which is used to detect tampering with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
             * the keystore (such as deleting or modifying key or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
             * certificate entries).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
             */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 28243
diff changeset
   607
            byte[] digest = md.digest();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            dos.write(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
            dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * Loads the keystore from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     * <p>If a password is given, it is used to check the integrity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
     * keystore data. Otherwise, the integrity of the keystore is not checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
     * @param stream the input stream from which the keystore is loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
     * @param password the (optional) password used to check the integrity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
     * the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
     * keystore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
    public void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
            DataInputStream dis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
            MessageDigest md = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
            CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
            Hashtable<String, CertificateFactory> cfs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
            byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
            if (stream == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                md = getPreKeyedHash(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                dis = new DataInputStream(new DigestInputStream(stream, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                dis = new DataInputStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
            // Body format: see store method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
            int xMagic = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
            int xVersion = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
            if (xMagic!=MAGIC ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
                (xVersion!=VERSION_1 && xVersion!=VERSION_2)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
                throw new IOException("Invalid keystore format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
            if (xVersion == VERSION_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
                cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
                // version 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                cfs = new Hashtable<String, CertificateFactory>(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
            int count = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
                int tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
                String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
                tag = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
                if (tag == 1) { // private key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
                    KeyEntry entry = new KeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
                    // Read the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                    alias = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
                    // Read the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                    entry.date = new Date(dis.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                    // Read the private key
4989
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   689
                    entry.protectedPrivKey =
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   690
                            IOUtils.readFully(dis, dis.readInt(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
                    // Read the certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
                    int numOfCerts = dis.readInt();
4989
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   694
                    if (numOfCerts > 0) {
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   695
                        List<Certificate> certs = new ArrayList<>(
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   696
                                numOfCerts > 10 ? 10 : numOfCerts);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   697
                        for (int j = 0; j < numOfCerts; j++) {
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   698
                            if (xVersion == 2) {
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   699
                                // read the certificate type, and instantiate a
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   700
                                // certificate factory of that type (reuse
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   701
                                // existing factory if possible)
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   702
                                String certType = dis.readUTF();
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   703
                                if (cfs.containsKey(certType)) {
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   704
                                    // reuse certificate factory
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   705
                                    cf = cfs.get(certType);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   706
                                } else {
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   707
                                    // create new certificate factory
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   708
                                    cf = CertificateFactory.getInstance(certType);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   709
                                    // store the certificate factory so we can
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   710
                                    // reuse it later
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   711
                                    cfs.put(certType, cf);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   712
                                }
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   713
                            }
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   714
                            // instantiate the certificate
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   715
                            encoded = IOUtils.readFully(dis, dis.readInt(), true);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   716
                            bais = new ByteArrayInputStream(encoded);
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   717
                            certs.add(cf.generateCertificate(bais));
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   718
                            bais.close();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
                        }
4989
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   720
                        // We can be sure now that numOfCerts of certs are read
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   721
                        entry.chain = certs.toArray(new Certificate[numOfCerts]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    // Add the entry to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
                    entries.put(alias, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
                } else if (tag == 2) { // trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
                    TrustedCertEntry entry = new TrustedCertEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                    // Read the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
                    alias = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                    // Read the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
                    entry.date = new Date(dis.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                    // Read the trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                    if (xVersion == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        // read the certificate type, and instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                        // certificate factory of that type (reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        // existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        String certType = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
                        if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                            // reuse certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                            cf = cfs.get(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                            // create new certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                            cf = CertificateFactory.getInstance(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                            // store the certificate factory so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                            // reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                    }
4989
1abad76dc481 6880321: sun.security.provider.JavaKeyStore abuse of OOM Exception handling
weijun
parents: 2
diff changeset
   754
                    encoded = IOUtils.readFully(dis, dis.readInt(), true);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                    bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                    entry.cert = cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                    bais.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                    // Add the entry to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                    entries.put(alias, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    throw new IOException("Unrecognized keystore entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
             * If a password has been provided, we check the keyed digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
             * at the end. If this check fails, the store has been tampered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
             * with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            if (password != null) {
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 28243
diff changeset
   773
                byte[] computed = md.digest();
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 28243
diff changeset
   774
                byte[] actual = new byte[computed.length];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                dis.readFully(actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                for (int i = 0; i < computed.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                    if (computed[i] != actual[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                        Throwable t = new UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            ("Password verification failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                        throw (IOException)new IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                            ("Keystore was tampered with, or "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
                            + "password was incorrect").initCause(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
     * To guard against tampering with the keystore, we append a keyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
     * hash with a bit of whitener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    private MessageDigest getPreKeyedHash(char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        throws NoSuchAlgorithmException, UnsupportedEncodingException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
        byte[] passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        for (i=0; i<passwdBytes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
            passwdBytes[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        md.update("Mighty Aphrodite".getBytes("UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
        return md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   810
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   811
    /**
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   812
     * Probe the first few bytes of the keystore data stream for a valid
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   813
     * JKS keystore encoding.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   814
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   815
    @Override
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   816
    public boolean engineProbe(InputStream stream) throws IOException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   817
        DataInputStream dataStream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   818
        if (stream instanceof DataInputStream) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   819
            dataStream = (DataInputStream)stream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   820
        } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   821
            dataStream = new DataInputStream(stream);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   822
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   823
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   824
        return MAGIC == dataStream.readInt();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   825
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
}