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