jdk/src/java.base/share/classes/com/sun/crypto/provider/JceKeyStore.java
author vinnie
Tue, 23 Dec 2014 16:30:57 +0000
changeset 28243 47080f9ae750
parent 25859 3317bb8137f4
child 30033 b9c86c17164a
permissions -rw-r--r--
8044445: JEP 229: Create PKCS12 Keystores by Default Reviewed-by: mullan, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
     2
 * Copyright (c) 1998, 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: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.crypto.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.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.DigestInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.DigestOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.PrivateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.KeyStoreSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.KeyStoreException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.UnrecoverableKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.cert.CertificateException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.SealedObject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * This class provides the keystore implementation referred to as "jceks".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * This implementation strongly protects the keystore private keys using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * triple-DES, where the triple-DES encryption/decryption key is derived from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * the user's password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * The encrypted private keys are stored in the keystore in a standard format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * namely the <code>EncryptedPrivateKeyInfo</code> format defined in PKCS #8.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * @see java.security.KeyStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public final class JceKeyStore extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    private static final int JCEKS_MAGIC = 0xcececece;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final int JKS_MAGIC = 0xfeedfeed;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private static final int VERSION_1 = 0x01;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private static final int VERSION_2 = 0x02;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // Private key and supporting certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final class PrivateKeyEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        Date date; // the creation date of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        byte[] protectedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        Certificate chain[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static final class SecretKeyEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        Date date; // the creation date of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        SealedObject sealedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // Trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private static final class TrustedCertEntry {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        Date date; // the creation date of this entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        Certificate cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * Private keys and certificates are stored in a hashtable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * Hash entries are keyed by alias names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
    88
    private Hashtable<String, Object> entries = new Hashtable<String, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * password to recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param password the password for recovering the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * or does not identify a <i>key entry</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * (e.g., the given password is wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    public Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   110
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        if (!((entry instanceof PrivateKeyEntry) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
              (entry instanceof SecretKeyEntry))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        KeyProtector keyProtector = new KeyProtector(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        if (entry instanceof PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            byte[] encrBytes = ((PrivateKeyEntry)entry).protectedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            EncryptedPrivateKeyInfo encrInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                encrInfo = new EncryptedPrivateKeyInfo(encrBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                throw new UnrecoverableKeyException("Private key not stored "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                                    + "as PKCS #8 " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                                                    "EncryptedPrivateKeyInfo");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            key = keyProtector.recover(encrInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            key =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                keyProtector.unseal(((SecretKeyEntry)entry).sealedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * does not exist or does not contain a certificate chain (i.e., the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * alias identifies either a <i>trusted certificate entry</i> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * <i>key entry</i> without a certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    public Certificate[] engineGetCertificateChain(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        Certificate[] chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   153
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        if ((entry instanceof PrivateKeyEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            && (((PrivateKeyEntry)entry).chain != null)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   157
            chain = ((PrivateKeyEntry)entry).chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        return chain;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <p>If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     * <i>trusted certificate entry</i>, the certificate associated with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * entry is returned. If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * <i>key entry</i>, the first element of the certificate chain of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * entry is returned, or null if that entry does not have a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    public Certificate engineGetCertificate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        Certificate cert = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   181
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                cert = ((TrustedCertEntry)entry).cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } else if ((entry instanceof PrivateKeyEntry) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                       (((PrivateKeyEntry)entry).chain != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
                cert = ((PrivateKeyEntry)entry).chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        return cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    public Date engineGetCreationDate(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        Date date = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   206
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            // We have to create a new instance of java.util.Date because
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
            // dates are not immutable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                date = new Date(((TrustedCertEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            } else if (entry instanceof PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                date = new Date(((PrivateKeyEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                date = new Date(((SecretKeyEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        return date;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Assigns the given key to the given alias, protecting it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * <p>If the given key is of type <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param key the key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     * key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @exception KeyStoreException if the given key cannot be protected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    public void engineSetKeyEntry(String alias, Key key, char[] password,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                KeyProtector keyProtector = new KeyProtector(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                if (key instanceof PrivateKey) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    PrivateKeyEntry entry = new PrivateKeyEntry();
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 private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    entry.protectedKey = keyProtector.protect((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                    // clone the chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                    if ((chain != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                        (chain.length !=0)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   263
                        entry.chain = chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
                        entry.chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
                    // store the entry
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   269
                    entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
                    SecretKeyEntry entry = new SecretKeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
                    entry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
                    // seal and store the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
                    entry.sealedKey = keyProtector.seal(key);
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   277
                    entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                throw new KeyStoreException(e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        }
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
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    public void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            // We assume it's a private key, because there is no standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
            // (ASN.1) encoding format for wrapped secret keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            PrivateKeyEntry entry = new PrivateKeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            entry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   317
            entry.protectedKey = key.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            if ((chain != null) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                (chain.length != 0)) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   320
                entry.chain = chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                entry.chain = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   325
            entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        }
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
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * <p>If the given alias already exists in this keystore and identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * <i>trusted certificate entry</i>, the certificate associated with it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception KeyStoreException if the given alias already exists and does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * not identify a <i>trusted certificate entry</i>, or this operation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * fails for some other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    public void engineSetCertificateEntry(String alias, Certificate cert)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   348
            Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
            if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                if (entry instanceof PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                    throw new KeyStoreException("Cannot overwrite own "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                + "certificate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                } else if (entry instanceof SecretKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                    throw new KeyStoreException("Cannot overwrite secret key");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            TrustedCertEntry trustedCertEntry = new TrustedCertEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            trustedCertEntry.cert = cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            trustedCertEntry.date = new Date();
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   361
            entries.put(alias.toLowerCase(Locale.ENGLISH), trustedCertEntry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    public void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        synchronized(entries) {
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   376
            entries.remove(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   385
    public Enumeration<String> engineAliases() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        return entries.keys();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
    public boolean engineContainsAlias(String alias) {
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   397
        return entries.containsKey(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * <i>key entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * <i>key entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    public boolean engineIsKeyEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        boolean isKey = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   419
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        if ((entry instanceof PrivateKeyEntry)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            || (entry instanceof SecretKeyEntry)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            isKey = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        return isKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * <i>trusted certificate entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * <i>trusted certificate entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
    public boolean engineIsCertificateEntry(String alias) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        boolean isCert = false;
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 10336
diff changeset
   437
        Object entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            isCert = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
        return isCert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     * keystore entry. If the entry being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * is a <i>trusted certificate entry</i>, the given certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * compared to that entry's certificate. If the entry being considered is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * a <i>key entry</i>, the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * element of that entry's certificate chain (if a chain exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * @return the (alias) name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
    public String engineGetCertificateAlias(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        Certificate certElem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   463
        Enumeration<String> e = entries.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
        while (e.hasMoreElements()) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   465
            String alias = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            Object entry = entries.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
            if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
                certElem = ((TrustedCertEntry)entry).cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
            } else if ((entry instanceof PrivateKeyEntry) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                       (((PrivateKeyEntry)entry).chain != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                certElem = ((PrivateKeyEntry)entry).chain[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
            if (certElem.equals(cert)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
                return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * Stores this keystore to the given output stream, and protects its
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * integrity with the given password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
     * @param stream the output stream to which this keystore is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     * @param password the password to generate the keystore integrity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     * algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
    public void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
             * KEYSTORE FORMAT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
             * Magic number (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
             * Version of this file format (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
             * Count (big-endian integer),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
             * followed by "count" instances of either:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
             *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
             *      tag=1 (big-endian integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
             *      alias (UTF string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
             *      timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
             *      encrypted private-key info according to PKCS #8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
             *          (integer length followed by encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
             *      cert chain (integer count followed by certs;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
             *          for each cert: type UTF string, followed by integer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
             *              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 (type UTF string, followed by integer length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
             *          followed by encoding)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
             *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
             * or:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
             *     {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
             *      tag=3 (big-endian integer)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
             *      alias (UTF string)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
             *      timestamp
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
             *      sealed secret key (in serialized form)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
             *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
             *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
             * ended by a keyed SHA1 hash (bytes only) of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
             *     { password + whitener + preceding body }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
            // password is mandatory when storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
            if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
                throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            byte[] encoded; // the certificate encoding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            MessageDigest md = getPreKeyedHash(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            DataOutputStream dos
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                = new DataOutputStream(new DigestOutputStream(stream, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            // NOTE: don't pass dos to oos at this point or it'll corrupt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
            // the keystore!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
            ObjectOutputStream oos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
                dos.writeInt(JCEKS_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
                dos.writeInt(VERSION_2); // always write the latest version
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
                dos.writeInt(entries.size());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   561
                Enumeration<String> e = entries.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                while (e.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   564
                    String alias = e.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
                    Object entry = entries.get(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
                    if (entry instanceof PrivateKeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
                        PrivateKeyEntry pentry = (PrivateKeyEntry)entry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                        // write PrivateKeyEntry tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
                        dos.writeInt(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
                        // write the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
                        dos.writeUTF(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
                        // write the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
                        dos.writeLong(pentry.date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
                        // write the protected private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
                        dos.writeInt(pentry.protectedKey.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                        dos.write(pentry.protectedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
                        // write the certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
                        int chainLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
                        if (pentry.chain == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
                            chainLen = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
                        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
                            chainLen = pentry.chain.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
                        dos.writeInt(chainLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
                        for (int i = 0; i < chainLen; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
                            encoded = pentry.chain[i].getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                            dos.writeUTF(pentry.chain[i].getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
                            dos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
                            dos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
                    } else if (entry instanceof TrustedCertEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
                        // write TrustedCertEntry tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
                        dos.writeInt(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                        // write the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                        dos.writeUTF(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
                        // write the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
                        dos.writeLong(((TrustedCertEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                        // write the trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
                        encoded = ((TrustedCertEntry)entry).cert.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
                        dos.writeUTF(((TrustedCertEntry)entry).cert.getType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                        dos.writeInt(encoded.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                        dos.write(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
                        // write SecretKeyEntry tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
                        dos.writeInt(3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
                        // write the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
                        dos.writeUTF(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                        // write the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                        dos.writeLong(((SecretKeyEntry)entry).date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
                        // write the sealed key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
                        oos = new ObjectOutputStream(dos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
                        oos.writeObject(((SecretKeyEntry)entry).sealedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                        // NOTE: don't close oos here since we are still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                        // using dos!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                 * Write the keyed hash which is used to detect tampering with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                 * the keystore (such as deleting or modifying key or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                 * certificate entries).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
                byte digest[] = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                dos.write(digest);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                dos.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
            } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                if (oos != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
                    oos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
                    dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * Loads the keystore from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * <p>If a password is given, it is used to check the integrity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     * keystore data. Otherwise, the integrity of the keystore is not checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @param stream the input stream from which the keystore is loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     * @param password the (optional) password used to check the integrity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     * keystore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
        synchronized(entries) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
            DataInputStream dis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            MessageDigest md = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            CertificateFactory cf = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   678
            Hashtable<String, CertificateFactory> cfs = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
            ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            if (stream == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
            if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
                md = getPreKeyedHash(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
                dis = new DataInputStream(new DigestInputStream(stream, md));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
                dis = new DataInputStream(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            // NOTE: don't pass dis to ois at this point or it'll fail to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
            // the keystore!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
            ObjectInputStream ois = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
                // Body format: see store method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
                int xMagic = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
                int xVersion = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
                // Accept the following keystore implementations:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
                // - JCEKS (this implementation), versions 1 and 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
                // - JKS (Sun's keystore implementation in JDK 1.2),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
                //   versions 1 and 2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
                if (((xMagic != JCEKS_MAGIC) && (xMagic != JKS_MAGIC)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
                    ((xVersion != VERSION_1) && (xVersion != VERSION_2))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
                    throw new IOException("Invalid keystore format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                if (xVersion == VERSION_1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
                    cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
                    // version 2
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   714
                    cfs = new Hashtable<String, CertificateFactory>(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
                entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
                int count = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
                for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
                    int tag;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
                    String alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
                    tag = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
                    if (tag == 1) { // private-key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
                        PrivateKeyEntry entry = new PrivateKeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
                        // read the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
                        alias = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                        // read the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
                        entry.date = new Date(dis.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
                        // read the private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
                            entry.protectedKey = new byte[dis.readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                        } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
                            throw new IOException("Keysize too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
                        dis.readFully(entry.protectedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                        // read the certificate chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
                        int numOfCerts = dis.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
                            if (numOfCerts > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
                                entry.chain = new Certificate[numOfCerts];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
                        } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
                            throw new IOException("Too many certificates in "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
                                                  + "chain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                        for (int j = 0; j < numOfCerts; j++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
                            if (xVersion == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
                                // read the certificate type, and instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
                                // certificate factory of that type (reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                                // existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                                String certType = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
                                if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
                                // reuse certificate factory
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   762
                                    cf = cfs.get(certType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                // create new certificate factory
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   765
                                    cf = CertificateFactory.getInstance(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   766
                                        certType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
                                // store the certificate factory so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
                                // reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
                                    cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
                                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
                            // instantiate the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
                                encoded = new byte[dis.readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
                            } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
                                throw new IOException("Certificate too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                            dis.readFully(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                            bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                            entry.chain[j] = cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                        // Add the entry to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                        entries.put(alias, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
                    } else if (tag == 2) { // trusted certificate entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
                        TrustedCertEntry entry = new TrustedCertEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
                        // read the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
                        alias = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                        // read the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                        entry.date = new Date(dis.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                        // read the trusted certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
                        if (xVersion == 2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                            // read the certificate type, and instantiate a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                            // certificate factory of that type (reuse
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                            // existing factory if possible)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                            String certType = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                            if (cfs.containsKey(certType)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                                // reuse certificate factory
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7043
diff changeset
   804
                                cf = cfs.get(certType);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
                                // create new certificate factory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
                                cf = CertificateFactory.getInstance(certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                                // store the certificate factory so we can
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                                // reuse it later
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                                cfs.put(certType, cf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
                            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
                            encoded = new byte[dis.readInt()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
                        } catch (OutOfMemoryError e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
                            throw new IOException("Certificate too big");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
                        dis.readFully(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
                        bais = new ByteArrayInputStream(encoded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
                        entry.cert = cf.generateCertificate(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
                        // Add the entry to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
                        entries.put(alias, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
                    } else if (tag == 3) { // secret-key entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
                        SecretKeyEntry entry = new SecretKeyEntry();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
                        // read the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
                        alias = dis.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                        // read the (entry creation) date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
                        entry.date = new Date(dis.readLong());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                        // read the sealed key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                            ois = new ObjectInputStream(dis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                            entry.sealedKey = (SealedObject)ois.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                            // NOTE: don't close ois here since we are still
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
                            // using dis!!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
                        } catch (ClassNotFoundException cnfe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
                            throw new IOException(cnfe.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                        // Add the entry to the list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
                        entries.put(alias, entry);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
                        throw new IOException("Unrecognized keystore entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                 * If a password has been provided, we check the keyed digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
                 * at the end. If this check fails, the store has been tampered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
                 * with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
                if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
                    byte computed[], actual[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                    computed = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    actual = new byte[computed.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
                    dis.readFully(actual);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
                    for (int i = 0; i < computed.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
                        if (computed[i] != actual[i]) {
7043
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   865
                            throw new IOException(
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   866
                                "Keystore was tampered with, or "
5e2d1edeb2c7 6916074: Add support for TLS 1.2
xuelei
parents: 5506
diff changeset
   867
                                + "password was incorrect");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            }  finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
                if (ois != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
                    ois.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
                    dis.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * To guard against tampering with the keystore, we append a keyed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     * hash with a bit of whitener.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    private MessageDigest getPreKeyedHash(char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
    throws NoSuchAlgorithmException, UnsupportedEncodingException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        int i, j;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
        MessageDigest md = MessageDigest.getInstance("SHA");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
        byte[] passwdBytes = new byte[password.length * 2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
        for (i=0, j=0; i<password.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
            passwdBytes[j++] = (byte)(password[i] >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
            passwdBytes[j++] = (byte)password[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        md.update(passwdBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
        for (i=0; i<passwdBytes.length; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
            passwdBytes[i] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        md.update("Mighty Aphrodite".getBytes("UTF8"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        return md;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   901
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   902
    /**
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   903
     * 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
   904
     * JCEKS keystore encoding.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   905
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   906
    @Override
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   907
    public boolean engineProbe(InputStream stream) throws IOException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   908
        DataInputStream dataStream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   909
        if (stream instanceof DataInputStream) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   910
            dataStream = (DataInputStream)stream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   911
        } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   912
            dataStream = new DataInputStream(stream);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   913
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   914
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   915
        return JCEKS_MAGIC == dataStream.readInt();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   916
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
}