src/java.base/share/classes/sun/security/pkcs12/PKCS12KeyStore.java
author weijun
Thu, 19 Jul 2018 00:14:40 +0800
changeset 51142 69dc9ea17b33
parent 51063 038688fa32d0
child 52996 2457d862a646
permissions -rw-r--r--
8202837: PBES2 AlgorithmId encoding error in PKCS12 KeyStore Reviewed-by: xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
     2
 * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.security.pkcs12;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    29
import java.security.AccessController;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.MessageDigest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.Key;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    33
import java.security.KeyFactory;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    34
import java.security.KeyStore;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    35
import java.security.KeyStoreSpi;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    36
import java.security.KeyStoreException;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    37
import java.security.PKCS12Attribute;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.security.PrivateKey;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    39
import java.security.PrivilegedAction;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    40
import java.security.UnrecoverableEntryException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import java.security.UnrecoverableKeyException;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    42
import java.security.SecureRandom;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    43
import java.security.Security;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import java.security.cert.CertificateFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import java.security.cert.CertificateException;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    48
import java.security.spec.AlgorithmParameterSpec;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
    49
import java.security.spec.InvalidParameterSpecException;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    50
import java.security.spec.KeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
import java.security.spec.PKCS8EncodedKeySpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
import java.security.AlgorithmParameters;
32634
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31695
diff changeset
    55
import java.security.InvalidAlgorithmParameterException;
614f8e5859aa 8134232: KeyStore.load() throws an IOException with a wrong cause in case of wrong password
asmotrak
parents: 31695
diff changeset
    56
import java.security.InvalidKeyException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
import javax.crypto.spec.PBEParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
import javax.crypto.spec.PBEKeySpec;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
    59
import javax.crypto.spec.SecretKeySpec;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
import javax.crypto.SecretKeyFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
import javax.crypto.SecretKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
import javax.crypto.Mac;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    64
import javax.security.auth.DestroyFailedException;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
import javax.security.auth.x500.X500Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
    67
import sun.security.util.Debug;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
import sun.security.util.DerInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
import sun.security.util.DerOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
import sun.security.util.DerValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
import sun.security.util.ObjectIdentifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
import sun.security.pkcs.ContentInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
import sun.security.x509.AlgorithmId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
import sun.security.pkcs.EncryptedPrivateKeyInfo;
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    75
import sun.security.provider.JavaKeyStore.JKS;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
    76
import sun.security.util.KeyStoreDelegator;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * This class provides the keystore implementation referred to as "PKCS12".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * Implements the PKCS#12 PFX protected using the Password privacy mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 * The contents are protected using Password integrity mode.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Currently we support following PBE algorithms:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 *  - pbeWithSHAAnd3KeyTripleDESCBC to encrypt private keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *  - pbeWithSHAAnd40BitRC2CBC to encrypt certificates
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * Supported encryption of various implementations :
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * Software and mode.     Certificate encryption  Private key encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 * ---------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * MSIE4 (domestic            40 bit RC2.            40 bit RC2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * and xport versions)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * PKCS#12 export.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * MSIE4, 5 (domestic         40 bit RC2,            40 bit RC2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
 * and export versions)       3 key triple DES       3 key triple DES
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * PKCS#12 import.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * MSIE5                      40 bit RC2             3 key triple DES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * PKCS#12 export.                                   with SHA1 (168 bits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 * Netscape Communicator      40 bit RC2             3 key triple DES,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 * (domestic and export                              with SHA1 (168 bits)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 * versions) PKCS#12 export
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 * Netscape Communicator      40 bit ciphers only    All.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 * (export version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
 * PKCS#12 import.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 * Netscape Communicator      All.                   All.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 * (domestic or fortified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 * version) PKCS#12 import.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * OpenSSL PKCS#12 code.      All.                   All.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 * ---------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   118
 * NOTE: PKCS12 KeyStore supports PrivateKeyEntry and TrustedCertficateEntry.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * PKCS#12 is mainly used to deliver private keys with their associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 * certificate chain and aliases. In a PKCS12 keystore, entries are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * identified by the alias, and a localKeyId is required to match the
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   122
 * private key with the certificate. Trusted certificate entries are identified
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   123
 * by the presence of an trustedKeyUsage attribute.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * @author Seema Malkani
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * @author Jeff Nisewanger
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
 * @see KeyProtector
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 * @see java.security.KeyStoreSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
 * @see KeyTool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
public final class PKCS12KeyStore extends KeyStoreSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   137
    // special PKCS12 keystore that supports PKCS12 and JKS file formats
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   138
    public static final class DualFormatPKCS12 extends KeyStoreDelegator {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   139
        public DualFormatPKCS12() {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   140
            super("PKCS12", PKCS12KeyStore.class, "JKS", JKS.class);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   141
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   142
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
   143
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    public static final int VERSION_3 = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   146
    private static final String[] KEY_PROTECTION_ALGORITHM = {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   147
        "keystore.pkcs12.keyProtectionAlgorithm",
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   148
        "keystore.PKCS12.keyProtectionAlgorithm"
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   149
    };
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   150
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   151
    private static final int MAX_ITERATION_COUNT = 5000000;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   152
    private static final int PBE_ITERATION_COUNT = 50000; // default
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   153
    private static final int MAC_ITERATION_COUNT = 100000; // default
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   154
    private static final int SALT_LEN = 20;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   155
15308
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   156
    // friendlyName, localKeyId, trustedKeyUsage
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   157
    private static final String[] CORE_ATTRIBUTES = {
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   158
        "1.2.840.113549.1.9.20",
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   159
        "1.2.840.113549.1.9.21",
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   160
        "2.16.840.1.113894.746875.1.1"
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   161
    };
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
   162
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   163
    private static final Debug debug = Debug.getInstance("pkcs12");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   164
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   165
    private static final int[] keyBag  = {1, 2, 840, 113549, 1, 12, 10, 1, 2};
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   166
    private static final int[] certBag = {1, 2, 840, 113549, 1, 12, 10, 1, 3};
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   167
    private static final int[] secretBag = {1, 2, 840, 113549, 1, 12, 10, 1, 5};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   169
    private static final int[] pkcs9Name  = {1, 2, 840, 113549, 1, 9, 20};
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   170
    private static final int[] pkcs9KeyId = {1, 2, 840, 113549, 1, 9, 21};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   172
    private static final int[] pkcs9certType = {1, 2, 840, 113549, 1, 9, 22, 1};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   174
    private static final int[] pbeWithSHAAnd40BitRC2CBC =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                                        {1, 2, 840, 113549, 1, 12, 1, 6};
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   176
    private static final int[] pbeWithSHAAnd3KeyTripleDESCBC =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                                        {1, 2, 840, 113549, 1, 12, 1, 3};
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   178
    private static final int[] pbes2 = {1, 2, 840, 113549, 1, 5, 13};
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   179
    // TODO: temporary Oracle OID
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   180
    /*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   181
     * { joint-iso-itu-t(2) country(16) us(840) organization(1) oracle(113894)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   182
     *   jdk(746875) crypto(1) id-at-trustedKeyUsage(1) }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   183
     */
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   184
    private static final int[] TrustedKeyUsage =
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   185
                                        {2, 16, 840, 1, 113894, 746875, 1, 1};
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   186
    private static final int[] AnyExtendedKeyUsage = {2, 5, 29, 37, 0};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private static ObjectIdentifier PKCS8ShroudedKeyBag_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private static ObjectIdentifier CertBag_OID;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   190
    private static ObjectIdentifier SecretBag_OID;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private static ObjectIdentifier PKCS9FriendlyName_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private static ObjectIdentifier PKCS9LocalKeyId_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    private static ObjectIdentifier PKCS9CertType_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    private static ObjectIdentifier pbeWithSHAAnd40BitRC2CBC_OID;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    private static ObjectIdentifier pbeWithSHAAnd3KeyTripleDESCBC_OID;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   196
    private static ObjectIdentifier pbes2_OID;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   197
    private static ObjectIdentifier TrustedKeyUsage_OID;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   198
    private static ObjectIdentifier[] AnyUsage;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    private int counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    // private key count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    // Note: This is a workaround to allow null localKeyID attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    // in pkcs12 with one private key entry and associated cert-chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private int privateKeyCount = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   207
    // secret key count
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   208
    private int secretKeyCount = 0;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   209
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   210
    // certificate count
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   211
    private int certificateCount = 0;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   212
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private SecureRandom random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            PKCS8ShroudedKeyBag_OID = new ObjectIdentifier(keyBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            CertBag_OID = new ObjectIdentifier(certBag);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   220
            SecretBag_OID = new ObjectIdentifier(secretBag);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            PKCS9FriendlyName_OID = new ObjectIdentifier(pkcs9Name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            PKCS9LocalKeyId_OID = new ObjectIdentifier(pkcs9KeyId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            PKCS9CertType_OID = new ObjectIdentifier(pkcs9certType);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            pbeWithSHAAnd40BitRC2CBC_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                        new ObjectIdentifier(pbeWithSHAAnd40BitRC2CBC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            pbeWithSHAAnd3KeyTripleDESCBC_OID =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                        new ObjectIdentifier(pbeWithSHAAnd3KeyTripleDESCBC);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   228
            pbes2_OID = new ObjectIdentifier(pbes2);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   229
            TrustedKeyUsage_OID = new ObjectIdentifier(TrustedKeyUsage);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   230
            AnyUsage = new ObjectIdentifier[]{
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   231
                new ObjectIdentifier(AnyExtendedKeyUsage)};
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            // should not happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   237
    // A keystore entry and associated attributes
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   238
    private static class Entry {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        Date date; // the creation date of this entry
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   240
        String alias;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   241
        byte[] keyId;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   242
        Set<KeyStore.Entry.Attribute> attributes;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   243
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   244
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   245
    // A key entry
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   246
    private static class KeyEntry extends Entry {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   247
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   248
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   249
    // A private key entry and its supporting certificate chain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   250
    private static class PrivateKeyEntry extends KeyEntry {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        byte[] protectedPrivKey;
31538
0981099a3e54 8130022: Use Java-style array declarations consistently
igerasim
parents: 31426
diff changeset
   252
        Certificate[] chain;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   253
    };
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   254
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   255
    // A secret key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   256
    private static class SecretKeyEntry extends KeyEntry {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   257
        byte[] protectedSecretKey;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   260
    // A certificate entry
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   261
    private static class CertEntry extends Entry {
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   262
        final X509Certificate cert;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   263
        ObjectIdentifier[] trustedKeyUsage;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   264
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   265
        CertEntry(X509Certificate cert, byte[] keyId, String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   266
            this(cert, keyId, alias, null, null);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   267
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   268
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   269
        CertEntry(X509Certificate cert, byte[] keyId, String alias,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   270
                ObjectIdentifier[] trustedKeyUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   271
                Set<? extends KeyStore.Entry.Attribute> attributes) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   272
            this.date = new Date();
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   273
            this.cert = cert;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            this.keyId = keyId;
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   275
            this.alias = alias;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   276
            this.trustedKeyUsage = trustedKeyUsage;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   277
            this.attributes = new HashSet<>();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   278
            if (attributes != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   279
                this.attributes.addAll(attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   280
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   285
     * Retries an action with password "\0" if "" fails.
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   286
     * @param <T> the return type
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   287
     */
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   288
    @FunctionalInterface
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   289
    private interface RetryWithZero<T> {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   290
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   291
        T tryOnce(char[] password) throws Exception;
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   292
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   293
        static <S> S run(RetryWithZero<S> f, char[] password) throws Exception {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   294
            try {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   295
                return f.tryOnce(password);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   296
            } catch (Exception e) {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   297
                if (password.length == 0) {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   298
                    // Retry using an empty password with a NUL terminator.
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   299
                    if (debug != null) {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   300
                        debug.println("Retry with a NUL password");
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   301
                    }
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   302
                    return f.tryOnce(new char[1]);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   303
                }
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   304
                throw e;
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   305
            }
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   306
        }
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   307
    }
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   308
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   309
    /**
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   310
     * Private keys and certificates are stored in a map.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   311
     * Map entries are keyed by alias names.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     */
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   313
    private Map<String, Entry> entries =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   314
        Collections.synchronizedMap(new LinkedHashMap<String, Entry>());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
    private ArrayList<KeyEntry> keyList = new ArrayList<KeyEntry>();
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   317
    private LinkedHashMap<X500Principal, X509Certificate> certsMap =
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   318
            new LinkedHashMap<X500Principal, X509Certificate>();
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   319
    private ArrayList<CertEntry> certEntries = new ArrayList<CertEntry>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * Returns the key associated with the given alias, using the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * password to recover it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
     * @param password the password for recovering the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
     * @return the requested key, or null if the given alias does not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * or does not identify a <i>key entry</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     * key cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @exception UnrecoverableKeyException if the key cannot be recovered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * (e.g., the given password is wrong).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    public Key engineGetKey(String alias, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        throws NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   339
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        Key key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   342
        if (entry == null || (!(entry instanceof KeyEntry))) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   346
        // get the encoded private key or secret key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   347
        byte[] encrBytes = null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   348
        if (entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   349
            encrBytes = ((PrivateKeyEntry) entry).protectedPrivKey;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   350
        } else if (entry instanceof SecretKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   351
            encrBytes = ((SecretKeyEntry) entry).protectedSecretKey;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   352
        } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   353
            throw new UnrecoverableKeyException("Error locating key");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   354
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        byte[] encryptedKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        AlgorithmParameters algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        ObjectIdentifier algOid;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   359
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            // get the encrypted private key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
            EncryptedPrivateKeyInfo encrInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                        new EncryptedPrivateKeyInfo(encrBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            encryptedKey = encrInfo.getEncryptedData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            // parse Algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
            DerValue val = new DerValue(encrInfo.getAlgorithm().encode());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
            DerInputStream in = val.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
            algOid = in.getOID();
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   370
            algParams = parseAlgParameters(algOid, in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        } catch (IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
            UnrecoverableKeyException uke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                new UnrecoverableKeyException("Private key not stored as "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                                 + "PKCS#8 EncryptedPrivateKeyInfo: " + ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            uke.initCause(ioe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            throw uke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   380
       try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   381
            PBEParameterSpec pbeSpec;
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   382
            int ic;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   383
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   384
            if (algParams != null) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   385
                try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   386
                    pbeSpec =
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   387
                        algParams.getParameterSpec(PBEParameterSpec.class);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   388
                } catch (InvalidParameterSpecException ipse) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   389
                    throw new IOException("Invalid PBE algorithm parameters");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   390
                }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   391
                ic = pbeSpec.getIterationCount();
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   392
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   393
                if (ic > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   394
                    throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   395
                }
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   396
            } else {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   397
                ic = 0;
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   398
            }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   399
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   400
            key = RetryWithZero.run(pass -> {
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   401
                // Use JCE
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   402
                SecretKey skey = getPBEKey(pass);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   403
                Cipher cipher = Cipher.getInstance(
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   404
                        mapPBEParamsToAlgorithm(algOid, algParams));
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   405
                cipher.init(Cipher.DECRYPT_MODE, skey, algParams);
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   406
                byte[] keyInfo = cipher.doFinal(encryptedKey);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   407
                /*
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   408
                 * Parse the key algorithm and then use a JCA key factory
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   409
                 * to re-create the key.
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   410
                 */
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   411
                DerValue val = new DerValue(keyInfo);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   412
                DerInputStream in = val.toDerInputStream();
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   413
                int i = in.getInteger();
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   414
                DerValue[] value = in.getSequence(2);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   415
                AlgorithmId algId = new AlgorithmId(value[0].getOID());
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   416
                String keyAlgo = algId.getName();
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   417
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   418
                // decode private key
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   419
                if (entry instanceof PrivateKeyEntry) {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   420
                    KeyFactory kfac = KeyFactory.getInstance(keyAlgo);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   421
                    PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(keyInfo);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   422
                    Key tmp = kfac.generatePrivate(kspec);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   423
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   424
                    if (debug != null) {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   425
                        debug.println("Retrieved a protected private key at alias" +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   426
                                " '" + alias + "' (" +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   427
                                new AlgorithmId(algOid).getName() +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   428
                                " iterations: " + ic + ")");
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   429
                    }
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   430
                    return tmp;
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   431
                    // decode secret key
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   432
                } else {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   433
                    byte[] keyBytes = in.getOctetString();
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   434
                    SecretKeySpec secretKeySpec =
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   435
                            new SecretKeySpec(keyBytes, keyAlgo);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   436
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   437
                    // Special handling required for PBE: needs a PBEKeySpec
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   438
                    Key tmp;
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   439
                    if (keyAlgo.startsWith("PBE")) {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   440
                        SecretKeyFactory sKeyFactory =
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   441
                                SecretKeyFactory.getInstance(keyAlgo);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   442
                        KeySpec pbeKeySpec =
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   443
                                sKeyFactory.getKeySpec(secretKeySpec, PBEKeySpec.class);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   444
                        tmp = sKeyFactory.generateSecret(pbeKeySpec);
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   445
                    } else {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   446
                        tmp = secretKeySpec;
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   447
                    }
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   448
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   449
                    if (debug != null) {
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   450
                        debug.println("Retrieved a protected secret key at alias " +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   451
                                "'" + alias + "' (" +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   452
                                new AlgorithmId(algOid).getName() +
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   453
                                " iterations: " + ic + ")");
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   454
                    }
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   455
                    return tmp;
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
   456
                }
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
   457
            }, password);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
            UnrecoverableKeyException uke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
                new UnrecoverableKeyException("Get Key failed: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
                                        e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
            uke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            throw uke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * Returns the certificate chain associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     * @return the certificate chain (ordered with the user's certificate first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * and the root certificate authority last), or null if the given alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * does not exist or does not contain a certificate chain (i.e., the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * alias identifies either a <i>trusted certificate entry</i> or a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * <i>key entry</i> without a certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    public Certificate[] engineGetCertificateChain(String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   481
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   482
        if (entry != null && entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   483
            if (((PrivateKeyEntry) entry).chain == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            } else {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   486
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   487
                if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   488
                    debug.println("Retrieved a " +
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   489
                        ((PrivateKeyEntry) entry).chain.length +
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   490
                        "-certificate chain at alias '" + alias + "'");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   491
                }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   492
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   493
                return ((PrivateKeyEntry) entry).chain.clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     * Returns the certificate associated with the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * <p>If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * <i>trusted certificate entry</i>, the certificate associated with that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * entry is returned. If the given alias name identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * <i>key entry</i>, the first element of the certificate chain of that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     * entry is returned, or null if that entry does not have a certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
     * chain.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
     * @return the certificate, or null if the given alias does not exist or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
     * does not contain a certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    public Certificate engineGetCertificate(String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   516
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   517
        if (entry == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   518
            return null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   519
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   520
        if (entry instanceof CertEntry &&
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   521
            ((CertEntry) entry).trustedKeyUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   522
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   523
            if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   524
                if (Arrays.equals(AnyUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   525
                    ((CertEntry) entry).trustedKeyUsage)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   526
                    debug.println("Retrieved a certificate at alias '" + alias +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   527
                        "' (trusted for any purpose)");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   528
                } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   529
                    debug.println("Retrieved a certificate at alias '" + alias +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   530
                        "' (trusted for limited purposes)");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   531
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   532
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   533
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   534
            return ((CertEntry) entry).cert;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   535
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   536
        } else if (entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   537
            if (((PrivateKeyEntry) entry).chain == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
            } else {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   540
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   541
                if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   542
                    debug.println("Retrieved a certificate at alias '" + alias +
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   543
                        "'");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   544
                }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   545
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   546
                return ((PrivateKeyEntry) entry).chain[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
            }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   548
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     * Returns the creation date of the entry identified by the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
     * @return the creation date of this entry, or null if the given alias does
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * not exist
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    public Date engineGetCreationDate(String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   563
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
        if (entry != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return new Date(entry.date.getTime());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * Assigns the given key to the given alias, protecting it with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * <p>If the given key is of type <code>java.security.PrivateKey</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * it must be accompanied by a certificate chain certifying the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     * corresponding public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @param key the key to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * @param password the password to protect the key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * key (only required if the given key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * @exception KeyStoreException if the given key cannot be protected, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * this operation fails for some other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
    public synchronized void engineSetKeyEntry(String alias, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
                        char[] password, Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   597
        KeyStore.PasswordProtection passwordProtection =
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   598
            new KeyStore.PasswordProtection(password);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   599
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   600
        try {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   601
            setKeyEntry(alias, key, passwordProtection, chain, null);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   602
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   603
        } finally {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   604
            try {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   605
                passwordProtection.destroy();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   606
            } catch (DestroyFailedException dfe) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   607
                // ignore
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   608
            }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   609
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   610
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   611
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   612
    /*
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   613
     * Sets a key entry (with attributes, when present)
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   614
     */
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   615
    private void setKeyEntry(String alias, Key key,
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   616
        KeyStore.PasswordProtection passwordProtection, Certificate[] chain,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   617
        Set<KeyStore.Entry.Attribute> attributes)
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   618
            throws KeyStoreException
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   619
    {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
        try {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   621
            Entry entry;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            if (key instanceof PrivateKey) {
40276
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   624
                // Check that all the certs are X.509 certs
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   625
                checkX509Certs(chain);
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   626
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   627
                PrivateKeyEntry keyEntry = new PrivateKeyEntry();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   628
                keyEntry.date = new Date();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   629
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                if ((key.getFormat().equals("PKCS#8")) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                    (key.getFormat().equals("PKCS8"))) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   632
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   633
                    if (debug != null) {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   634
                        debug.println(
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   635
                            "Setting a protected private key at alias '" +
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   636
                            alias + "'");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   637
                        }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   638
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   639
                    // Encrypt the private key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   640
                    keyEntry.protectedPrivKey =
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   641
                        encryptPrivateKey(key.getEncoded(), passwordProtection);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
                    throw new KeyStoreException("Private key is not encoded" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
                                "as PKCS#8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
                }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   646
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   647
                // clone the chain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   648
                if (chain != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   649
                    // validate cert-chain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   650
                    if ((chain.length > 1) && (!validateChain(chain)))
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   651
                       throw new KeyStoreException("Certificate chain is " +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   652
                                                "not valid");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   653
                    keyEntry.chain = chain.clone();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   654
                    certificateCount += chain.length;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   655
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   656
                    if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   657
                        debug.println("Setting a " + chain.length +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   658
                            "-certificate chain at alias '" + alias + "'");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   659
                    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   660
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   661
                privateKeyCount++;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   662
                entry = keyEntry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   663
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   664
            } else if (key instanceof SecretKey) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   665
                SecretKeyEntry keyEntry = new SecretKeyEntry();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   666
                keyEntry.date = new Date();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   667
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   668
                // Encode secret key in a PKCS#8
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   669
                DerOutputStream pkcs8 = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   670
                DerOutputStream secretKeyInfo = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   671
                secretKeyInfo.putInteger(0);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   672
                AlgorithmId algId = AlgorithmId.get(key.getAlgorithm());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   673
                algId.encode(secretKeyInfo);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   674
                secretKeyInfo.putOctetString(key.getEncoded());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   675
                pkcs8.write(DerValue.tag_Sequence, secretKeyInfo);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   676
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   677
                // Encrypt the secret key (using same PBE as for private keys)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   678
                keyEntry.protectedSecretKey =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   679
                    encryptPrivateKey(pkcs8.toByteArray(), passwordProtection);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   680
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   681
                if (debug != null) {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   682
                    debug.println("Setting a protected secret key at alias '" +
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   683
                        alias + "'");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   684
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   685
                secretKeyCount++;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   686
                entry = keyEntry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   687
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
            } else {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   689
                throw new KeyStoreException("Unsupported Key type");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   692
            entry.attributes = new HashSet<>();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   693
            if (attributes != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   694
                entry.attributes.addAll(attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
            // set the keyId to current date
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
            entry.keyId = ("Time " + (entry.date).getTime()).getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
            // set the alias
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
   699
            entry.alias = alias.toLowerCase(Locale.ENGLISH);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            // add the entry
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
   701
            entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   702
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        } catch (Exception nsae) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   704
            throw new KeyStoreException("Key protection " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   705
                       " algorithm not found: " + nsae, nsae);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
     * Assigns the given key (that has already been protected) to the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
     * alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
     * <p>If the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
     * <code>java.security.PrivateKey</code>, it must be accompanied by a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * certificate chain certifying the corresponding public key. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * underlying keystore implementation is of type <code>jks</code>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     * <code>key</code> must be encoded as an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * <p>If the given alias already exists, the keystore information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * associated with it is overridden by the given key (and possibly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     * certificate chain).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * @param key the key (in protected format) to be associated with the alias
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     * @param chain the certificate chain for the corresponding public
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * key (only useful if the protected key is of type
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * <code>java.security.PrivateKey</code>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     * @exception KeyStoreException if this operation fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
    public synchronized void engineSetKeyEntry(String alias, byte[] key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
                                  Certificate[] chain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
    {
40276
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   736
        // Check that all the certs are X.509 certs
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   737
        checkX509Certs(chain);
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
   738
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   739
        // Private key must be encoded as EncryptedPrivateKeyInfo
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        // as defined in PKCS#8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
            new EncryptedPrivateKeyInfo(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
        } catch (IOException ioe) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   744
            throw new KeyStoreException("Private key is not stored"
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   745
                    + " as PKCS#8 EncryptedPrivateKeyInfo: " + ioe, ioe);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   748
        PrivateKeyEntry entry = new PrivateKeyEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        entry.date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   751
        if (debug != null) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   752
            debug.println("Setting a protected private key at alias '" +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   753
                alias + "'");
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   754
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   755
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   756
        try {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   757
            // set the keyId to current date
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   758
            entry.keyId = ("Time " + (entry.date).getTime()).getBytes("UTF8");
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   759
        } catch (UnsupportedEncodingException ex) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   760
            // Won't happen
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   761
        }
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   762
        // set the alias
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
   763
        entry.alias = alias.toLowerCase(Locale.ENGLISH);
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
   764
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        entry.protectedPrivKey = key.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (chain != null) {
29909
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
   767
            // validate cert-chain
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
   768
            if ((chain.length > 1) && (!validateChain(chain))) {
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
   769
                throw new KeyStoreException("Certificate chain is "
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
   770
                        + "not valid");
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
   771
            }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   772
            entry.chain = chain.clone();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   773
            certificateCount += chain.length;
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   774
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   775
            if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   776
                debug.println("Setting a " + entry.chain.length +
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   777
                    "-certificate chain at alias '" + alias + "'");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   778
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        // add the entry
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   782
        privateKeyCount++;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
   783
        entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
     * Generate random salt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    private byte[] getSalt()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
        // Generate a random salt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
        byte[] salt = new byte[SALT_LEN];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (random == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
           random = new SecureRandom();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        random.nextBytes(salt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        return salt;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
     * Generate PBE Algorithm Parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
     */
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   804
    private AlgorithmParameters getPBEAlgorithmParameters(String algorithm)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
        AlgorithmParameters algParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
        // create PBE parameters from salt and iteration count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
        PBEParameterSpec paramSpec =
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   811
                new PBEParameterSpec(getSalt(), PBE_ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
           algParams = AlgorithmParameters.getInstance(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
           algParams.init(paramSpec);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        } catch (Exception e) {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   816
           throw new IOException("getPBEAlgorithmParameters failed: " +
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   817
                                 e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
        return algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * parse Algorithm Parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     */
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   825
    private AlgorithmParameters parseAlgParameters(ObjectIdentifier algorithm,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   826
        DerInputStream in) throws IOException
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        AlgorithmParameters algParams = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            DerValue params;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            if (in.available() == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
                params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                params = in.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                if (params.tag == DerValue.tag_Null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
                   params = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
            if (params != null) {
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
   840
                if (algorithm.equals(pbes2_OID)) {
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   841
                    algParams = AlgorithmParameters.getInstance("PBES2");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   842
                } else {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   843
                    algParams = AlgorithmParameters.getInstance("PBE");
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   844
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
                algParams.init(params.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   848
           throw new IOException("parseAlgParameters failed: " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   849
                                 e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
        return algParams;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * Generate PBE key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
    private SecretKey getPBEKey(char[] password) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        SecretKey skey = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            PBEKeySpec keySpec = new PBEKeySpec(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            skey = skFac.generateSecret(keySpec);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   865
            keySpec.clearPassword();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   867
           throw new IOException("getSecretKey failed: " +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
   868
                                 e.getMessage(), e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        return skey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * Encrypt private key using Password-based encryption (PBE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * as defined in PKCS#5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     *
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   877
     * NOTE: By default, pbeWithSHAAnd3-KeyTripleDES-CBC algorithmID is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *       used to derive the key and IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     * @return encrypted private key encoded as EncryptedPrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     */
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   882
    private byte[] encryptPrivateKey(byte[] data,
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   883
        KeyStore.PasswordProtection passwordProtection)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
        throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
        byte[] key = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        try {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   889
            String algorithm;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   890
            AlgorithmParameters algParams;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   891
            AlgorithmId algid;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   892
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   893
            // Initialize PBE algorithm and parameters
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   894
            algorithm = passwordProtection.getProtectionAlgorithm();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   895
            if (algorithm != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   896
                AlgorithmParameterSpec algParamSpec =
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   897
                    passwordProtection.getProtectionParameters();
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   898
                if (algParamSpec != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   899
                    algParams = AlgorithmParameters.getInstance(algorithm);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   900
                    algParams.init(algParamSpec);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   901
                } else {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   902
                    algParams = getPBEAlgorithmParameters(algorithm);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   903
                }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   904
            } else {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   905
                // Check default key protection algorithm for PKCS12 keystores
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   906
                algorithm = AccessController.doPrivileged(
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   907
                    new PrivilegedAction<String>() {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   908
                        public String run() {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   909
                            String prop =
15299
879d8cfab8e6 8006813: Compilation error in PKCS12KeyStore.java
mullan
parents: 15298
diff changeset
   910
                                Security.getProperty(
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   911
                                    KEY_PROTECTION_ALGORITHM[0]);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   912
                            if (prop == null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   913
                                prop = Security.getProperty(
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   914
                                    KEY_PROTECTION_ALGORITHM[1]);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   915
                            }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   916
                            return prop;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   917
                        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   918
                    });
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   919
                if (algorithm == null || algorithm.isEmpty()) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   920
                    algorithm = "PBEWithSHA1AndDESede";
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   921
                }
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
   922
                algParams = getPBEAlgorithmParameters(algorithm);
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   923
            }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   924
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   925
            ObjectIdentifier pbeOID = mapPBEAlgorithmToOID(algorithm);
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   926
            if (pbeOID == null) {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   927
                    throw new IOException("PBE algorithm '" + algorithm +
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   928
                        " 'is not supported for key entry protection");
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   929
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
            // Use JCE
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   932
            SecretKey skey = getPBEKey(passwordProtection.getPassword());
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   933
            Cipher cipher = Cipher.getInstance(algorithm);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            byte[] encryptedKey = cipher.doFinal(data);
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   936
            algid = new AlgorithmId(pbeOID, cipher.getParameters());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   938
            if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   939
                debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   940
                    ")");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   941
            }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   942
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            // wrap encrypted private key in EncryptedPrivateKeyInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
            // as defined in PKCS#8
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
            EncryptedPrivateKeyInfo encrInfo =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
                new EncryptedPrivateKeyInfo(algid, encryptedKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            key = encrInfo.getEncoded();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            UnrecoverableKeyException uke =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
                new UnrecoverableKeyException("Encrypt Private Key failed: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
                                                + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
            uke.initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
            throw uke;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
        return key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   959
    /*
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   960
     * Map a PBE algorithm name onto its object identifier
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   961
     */
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   962
    private static ObjectIdentifier mapPBEAlgorithmToOID(String algorithm)
15301
215128369cab 8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents: 15299
diff changeset
   963
        throws NoSuchAlgorithmException {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   964
        // Check for PBES2 algorithms
25402
0c24d9aa8fb9 7065233: To interpret case-insensitive string locale independently
juh
parents: 19210
diff changeset
   965
        if (algorithm.toLowerCase(Locale.ENGLISH).startsWith("pbewithhmacsha")) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   966
            return pbes2_OID;
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   967
        }
15301
215128369cab 8006855: PKCS12 test failures due to unsupported algorithm
vinnie
parents: 15299
diff changeset
   968
        return AlgorithmId.get(algorithm).getOID();
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   969
    }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
   970
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   971
    /*
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   972
     * Map a PBE algorithm parameters onto its algorithm name
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   973
     */
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   974
    private static String mapPBEParamsToAlgorithm(ObjectIdentifier algorithm,
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   975
        AlgorithmParameters algParams) throws NoSuchAlgorithmException {
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   976
        // Check for PBES2 algorithms
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
   977
        if (algorithm.equals(pbes2_OID) && algParams != null) {
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   978
            return algParams.toString();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   979
        }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   980
        return algorithm.toString();
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   981
    }
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
   982
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     * Assigns the given certificate to the given alias.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
     * <p>If the given alias already exists in this keystore and identifies a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
     * <i>trusted certificate entry</i>, the certificate associated with it is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
     * overridden by the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
     * @param cert the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
     * @exception KeyStoreException if the given alias already exists and does
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   994
     * not identify a <i>trusted certificate entry</i>, or this operation fails
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
   995
     * for some other reason.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
    public synchronized void engineSetCertificateEntry(String alias,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        Certificate cert) throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
    {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1000
        setCertEntry(alias, cert, null);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1001
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1002
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1003
    /*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1004
     * Sets a trusted cert entry (with attributes, when present)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1005
     */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1006
    private void setCertEntry(String alias, Certificate cert,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1007
        Set<KeyStore.Entry.Attribute> attributes) throws KeyStoreException {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1008
40276
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1009
        // Check that the cert is an X.509 cert
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1010
        if (cert != null && (!(cert instanceof X509Certificate))) {
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1011
            throw new KeyStoreException(
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1012
                "Only X.509 certificates are supported - rejecting class: " +
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1013
                cert.getClass().getName());
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1014
        }
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1015
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1016
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1017
        if (entry != null && entry instanceof KeyEntry) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            throw new KeyStoreException("Cannot overwrite own certificate");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1019
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1020
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1021
        CertEntry certEntry =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1022
            new CertEntry((X509Certificate) cert, null, alias, AnyUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1023
                attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1024
        certificateCount++;
43543
36f684103d94 8173956: KeyStore regression due to default keystore being changed to PKCS12
vinnie
parents: 40276
diff changeset
  1025
        entries.put(alias.toLowerCase(Locale.ENGLISH), certEntry);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1026
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1027
        if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1028
            debug.println("Setting a trusted certificate at alias '" + alias +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1029
                "'");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1030
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
     * Deletes the entry identified by the given alias from this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
     * @exception KeyStoreException if the entry cannot be removed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
    public synchronized void engineDeleteEntry(String alias)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
        throws KeyStoreException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
    {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1043
        if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1044
            debug.println("Removing entry at alias '" + alias + "'");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1045
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1046
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1047
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1048
        if (entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1049
            PrivateKeyEntry keyEntry = (PrivateKeyEntry) entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1050
            if (keyEntry.chain != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1051
                certificateCount -= keyEntry.chain.length;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1052
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1053
            privateKeyCount--;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1054
        } else if (entry instanceof CertEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1055
            certificateCount--;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1056
        } else if (entry instanceof SecretKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1057
            secretKeyCount--;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1058
        }
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1059
        entries.remove(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
     * Lists all the alias names of this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
     * @return enumeration of the alias names
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
    public Enumeration<String> engineAliases() {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1068
        return Collections.enumeration(entries.keySet());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
     * Checks if the given alias exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
     * @param alias the alias name
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
     * @return true if the alias exists, false otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    public boolean engineContainsAlias(String alias) {
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  1079
        return entries.containsKey(alias.toLowerCase(Locale.ENGLISH));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
     * Retrieves the number of entries in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
     * @return the number of entries in this keystore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
    public int engineSize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
        return entries.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
     * <i>key entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
     * <i>key entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
    public boolean engineIsKeyEntry(String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1099
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1100
        if (entry != null && entry instanceof KeyEntry) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
     * Returns true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
     * <i>trusted certificate entry</i>, and false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
     * @return true if the entry identified by the given alias is a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
     * <i>trusted certificate entry</i>, false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    public boolean engineIsCertificateEntry(String alias) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1115
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1116
        if (entry != null && entry instanceof CertEntry &&
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1117
            ((CertEntry) entry).trustedKeyUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1118
            return true;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1119
        } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1120
            return false;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1121
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
    /**
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1125
     * Determines if the keystore {@code Entry} for the specified
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1126
     * {@code alias} is an instance or subclass of the specified
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1127
     * {@code entryClass}.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1128
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1129
     * @param alias the alias name
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1130
     * @param entryClass the entry class
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1131
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1132
     * @return true if the keystore {@code Entry} for the specified
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1133
     *          {@code alias} is an instance or subclass of the
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1134
     *          specified {@code entryClass}, false otherwise
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1135
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1136
     * @since 1.5
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1137
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1138
    @Override
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1139
    public boolean
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1140
        engineEntryInstanceOf(String alias,
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1141
                              Class<? extends KeyStore.Entry> entryClass)
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1142
    {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1143
        if (entryClass == KeyStore.TrustedCertificateEntry.class) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1144
            return engineIsCertificateEntry(alias);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1145
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1146
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1147
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1148
        if (entryClass == KeyStore.PrivateKeyEntry.class) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1149
            return (entry != null && entry instanceof PrivateKeyEntry);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1150
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1151
        if (entryClass == KeyStore.SecretKeyEntry.class) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1152
            return (entry != null && entry instanceof SecretKeyEntry);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1153
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1154
        return false;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1155
    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1156
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1157
    /**
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
     * Returns the (alias) name of the first keystore entry whose certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
     * matches the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
     * <p>This method attempts to match the given certificate with each
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
     * keystore entry. If the entry being considered
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
     * is a <i>trusted certificate entry</i>, the given certificate is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
     * compared to that entry's certificate. If the entry being considered is
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
     * a <i>key entry</i>, the given certificate is compared to the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
     * element of that entry's certificate chain (if a chain exists).
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
     * @param cert the certificate to match with.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
     * @return the (alias) name of the first entry with matching certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
     * or null if no such entry exists in this keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    public String engineGetCertificateAlias(Certificate cert) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        Certificate certElem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1176
        for (Enumeration<String> e = engineAliases(); e.hasMoreElements(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            String alias = e.nextElement();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1178
            Entry entry = entries.get(alias);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1179
            if (entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1180
                if (((PrivateKeyEntry) entry).chain != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1181
                    certElem = ((PrivateKeyEntry) entry).chain[0];
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1182
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1183
            } else if (entry instanceof CertEntry &&
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1184
                    ((CertEntry) entry).trustedKeyUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1185
                certElem = ((CertEntry) entry).cert;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1186
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1187
                continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
            }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  1189
            if (certElem != null && certElem.equals(cert)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
                return alias;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
     * Stores this keystore to the given output stream, and protects its
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
     * integrity with the given password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
     * @param stream the output stream to which this keystore is written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
     * @param password the password to generate the keystore integrity check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
     * @exception IOException if there was an I/O problem with data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
     * @exception NoSuchAlgorithmException if the appropriate data integrity
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
     * algorithm could not be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
     * @exception CertificateException if any of the certificates included in
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
     * the keystore data could not be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
    public synchronized void engineStore(OutputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        // password is mandatory when storing
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
        if (password == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
           throw new IllegalArgumentException("password can't be null");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
        // -- Create PFX
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        DerOutputStream pfx = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        // PFX version (always write the latest version)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        DerOutputStream version = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
        version.putInteger(VERSION_3);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
        byte[] pfxVersion = version.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        pfx.write(pfxVersion);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        // -- Create AuthSafe
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
        DerOutputStream authSafe = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        // -- Create ContentInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
        DerOutputStream authSafeContentInfo = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
        // -- create safeContent Data ContentInfo
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1233
        if (privateKeyCount > 0 || secretKeyCount > 0) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1234
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1235
            if (debug != null) {
15538
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  1236
                debug.println("Storing " + (privateKeyCount + secretKeyCount) +
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  1237
                    " protected key(s) in a PKCS#7 data");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1238
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1239
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1240
            byte[] safeContentData = createSafeContent();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1241
            ContentInfo dataContentInfo = new ContentInfo(safeContentData);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1242
            dataContentInfo.encode(authSafeContentInfo);
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1243
        }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1244
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
        // -- create EncryptedContentInfo
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1246
        if (certificateCount > 0) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1247
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1248
            if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1249
                debug.println("Storing " + certificateCount +
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  1250
                    " certificate(s) in a PKCS#7 encryptedData");
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1251
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1252
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1253
            byte[] encrData = createEncryptedData(password);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1254
            ContentInfo encrContentInfo =
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                new ContentInfo(ContentInfo.ENCRYPTED_DATA_OID,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                                new DerValue(encrData));
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1257
            encrContentInfo.encode(authSafeContentInfo);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1258
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        // wrap as SequenceOf ContentInfos
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
        DerOutputStream cInfo = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
        cInfo.write(DerValue.tag_SequenceOf, authSafeContentInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
        byte[] authenticatedSafe = cInfo.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
        // Create Encapsulated ContentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        ContentInfo contentInfo = new ContentInfo(authenticatedSafe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
        contentInfo.encode(authSafe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        byte[] authSafeData = authSafe.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
        pfx.write(authSafeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
        // -- MAC
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
        byte[] macData = calculateMac(password, authenticatedSafe);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
        pfx.write(macData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
        // write PFX to output stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
        DerOutputStream pfxout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
        pfxout.write(DerValue.tag_Sequence, pfx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
        byte[] pfxData = pfxout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
        stream.write(pfxData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
        stream.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1283
    /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1284
     * Gets a <code>KeyStore.Entry</code> for the specified alias
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1285
     * with the specified protection parameter.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1286
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1287
     * @param alias get the <code>KeyStore.Entry</code> for this alias
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1288
     * @param protParam the <code>ProtectionParameter</code>
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1289
     *          used to protect the <code>Entry</code>,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1290
     *          which may be <code>null</code>
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1291
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1292
     * @return the <code>KeyStore.Entry</code> for the specified alias,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1293
     *          or <code>null</code> if there is no such entry
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1294
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1295
     * @exception KeyStoreException if the operation failed
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1296
     * @exception NoSuchAlgorithmException if the algorithm for recovering the
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1297
     *          entry cannot be found
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1298
     * @exception UnrecoverableEntryException if the specified
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1299
     *          <code>protParam</code> were insufficient or invalid
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1300
     * @exception UnrecoverableKeyException if the entry is a
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1301
     *          <code>PrivateKeyEntry</code> or <code>SecretKeyEntry</code>
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1302
     *          and the specified <code>protParam</code> does not contain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1303
     *          the information needed to recover the key (e.g. wrong password)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1304
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1305
     * @since 1.5
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1306
     */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1307
    @Override
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1308
    public KeyStore.Entry engineGetEntry(String alias,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1309
                        KeyStore.ProtectionParameter protParam)
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1310
                throws KeyStoreException, NoSuchAlgorithmException,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1311
                UnrecoverableEntryException {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1312
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1313
        if (!engineContainsAlias(alias)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1314
            return null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1315
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1316
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1317
        Entry entry = entries.get(alias.toLowerCase(Locale.ENGLISH));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1318
        if (protParam == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1319
            if (engineIsCertificateEntry(alias)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1320
                if (entry instanceof CertEntry &&
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1321
                    ((CertEntry) entry).trustedKeyUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1322
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1323
                    if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1324
                        debug.println("Retrieved a trusted certificate at " +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1325
                            "alias '" + alias + "'");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1326
                    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1327
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1328
                    return new KeyStore.TrustedCertificateEntry(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1329
                        ((CertEntry)entry).cert, getAttributes(entry));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1330
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1331
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1332
                throw new UnrecoverableKeyException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1333
                        ("requested entry requires a password");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1334
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1335
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1336
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1337
        if (protParam instanceof KeyStore.PasswordProtection) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1338
            if (engineIsCertificateEntry(alias)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1339
                throw new UnsupportedOperationException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1340
                    ("trusted certificate entries are not password-protected");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1341
            } else if (engineIsKeyEntry(alias)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1342
                KeyStore.PasswordProtection pp =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1343
                        (KeyStore.PasswordProtection)protParam;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1344
                char[] password = pp.getPassword();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1345
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1346
                Key key = engineGetKey(alias, password);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1347
                if (key instanceof PrivateKey) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1348
                    Certificate[] chain = engineGetCertificateChain(alias);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1349
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1350
                    return new KeyStore.PrivateKeyEntry((PrivateKey)key, chain,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1351
                        getAttributes(entry));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1352
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1353
                } else if (key instanceof SecretKey) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1354
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1355
                    return new KeyStore.SecretKeyEntry((SecretKey)key,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1356
                        getAttributes(entry));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1357
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1358
            } else if (!engineIsKeyEntry(alias)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1359
                throw new UnsupportedOperationException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1360
                    ("untrusted certificate entries are not " +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1361
                        "password-protected");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1362
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1363
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1364
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1365
        throw new UnsupportedOperationException();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1366
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1367
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1368
    /**
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1369
     * Saves a <code>KeyStore.Entry</code> under the specified alias.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1370
     * The specified protection parameter is used to protect the
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1371
     * <code>Entry</code>.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1372
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1373
     * <p> If an entry already exists for the specified alias,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1374
     * it is overridden.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1375
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1376
     * @param alias save the <code>KeyStore.Entry</code> under this alias
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1377
     * @param entry the <code>Entry</code> to save
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1378
     * @param protParam the <code>ProtectionParameter</code>
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1379
     *          used to protect the <code>Entry</code>,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1380
     *          which may be <code>null</code>
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1381
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1382
     * @exception KeyStoreException if this operation fails
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1383
     *
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1384
     * @since 1.5
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1385
     */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1386
    @Override
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1387
    public synchronized void engineSetEntry(String alias, KeyStore.Entry entry,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1388
        KeyStore.ProtectionParameter protParam) throws KeyStoreException {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1389
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1390
        // get password
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1391
        if (protParam != null &&
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1392
            !(protParam instanceof KeyStore.PasswordProtection)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1393
            throw new KeyStoreException("unsupported protection parameter");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1394
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1395
        KeyStore.PasswordProtection pProtect = null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1396
        if (protParam != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1397
            pProtect = (KeyStore.PasswordProtection)protParam;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1398
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1399
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1400
        // set entry
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1401
        if (entry instanceof KeyStore.TrustedCertificateEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1402
            if (protParam != null && pProtect.getPassword() != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1403
                // pre-1.5 style setCertificateEntry did not allow password
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1404
                throw new KeyStoreException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1405
                    ("trusted certificate entries are not password-protected");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1406
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1407
                KeyStore.TrustedCertificateEntry tce =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1408
                        (KeyStore.TrustedCertificateEntry)entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1409
                setCertEntry(alias, tce.getTrustedCertificate(),
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1410
                    tce.getAttributes());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1411
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1412
                return;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1413
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1414
        } else if (entry instanceof KeyStore.PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1415
            if (pProtect == null || pProtect.getPassword() == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1416
                // pre-1.5 style setKeyEntry required password
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1417
                throw new KeyStoreException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1418
                    ("non-null password required to create PrivateKeyEntry");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1419
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1420
                KeyStore.PrivateKeyEntry pke = (KeyStore.PrivateKeyEntry)entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1421
                setKeyEntry(alias, pke.getPrivateKey(), pProtect,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1422
                    pke.getCertificateChain(), pke.getAttributes());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1423
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1424
                return;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1425
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1426
        } else if (entry instanceof KeyStore.SecretKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1427
            if (pProtect == null || pProtect.getPassword() == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1428
                // pre-1.5 style setKeyEntry required password
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1429
                throw new KeyStoreException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1430
                    ("non-null password required to create SecretKeyEntry");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1431
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1432
                KeyStore.SecretKeyEntry ske = (KeyStore.SecretKeyEntry)entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1433
                setKeyEntry(alias, ske.getSecretKey(), pProtect,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1434
                    (Certificate[])null, ske.getAttributes());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1435
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1436
                return;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1437
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1438
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1439
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1440
        throw new KeyStoreException
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1441
                ("unsupported entry type: " + entry.getClass().getName());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1442
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1443
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1444
    /*
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1445
     * Assemble the entry attributes
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1446
     */
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1447
    private Set<KeyStore.Entry.Attribute> getAttributes(Entry entry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1448
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1449
        if (entry.attributes == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1450
            entry.attributes = new HashSet<>();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1451
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1452
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1453
        // friendlyName
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1454
        entry.attributes.add(new PKCS12Attribute(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1455
            PKCS9FriendlyName_OID.toString(), entry.alias));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1456
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1457
        // localKeyID
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1458
        byte[] keyIdValue = entry.keyId;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1459
        if (keyIdValue != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1460
            entry.attributes.add(new PKCS12Attribute(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1461
                PKCS9LocalKeyId_OID.toString(), Debug.toString(keyIdValue)));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1462
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1463
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1464
        // trustedKeyUsage
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1465
        if (entry instanceof CertEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1466
            ObjectIdentifier[] trustedKeyUsageValue =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1467
                ((CertEntry) entry).trustedKeyUsage;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1468
            if (trustedKeyUsageValue != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1469
                if (trustedKeyUsageValue.length == 1) { // omit brackets
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1470
                    entry.attributes.add(new PKCS12Attribute(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1471
                        TrustedKeyUsage_OID.toString(),
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1472
                        trustedKeyUsageValue[0].toString()));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1473
                } else { // multi-valued
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1474
                    entry.attributes.add(new PKCS12Attribute(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1475
                        TrustedKeyUsage_OID.toString(),
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1476
                        Arrays.toString(trustedKeyUsageValue)));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1477
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1478
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1479
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1480
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1481
        return entry.attributes;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1482
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1483
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     * Generate Hash.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    private byte[] generateHash(byte[] data) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        byte[] digest = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
            MessageDigest md = MessageDigest.getInstance("SHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
            md.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
            digest = md.digest();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1495
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
  1496
            throw new IOException("generateHash failed: " + e, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
        return digest;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
     * Calculate MAC using HMAC algorithm (required for password integrity)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     * Hash-based MAC algorithm combines secret key with message digest to
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
     * create a message authentication code (MAC)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1508
    private byte[] calculateMac(char[] passwd, byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1510
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
        byte[] mData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
        String algName = "SHA1";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
            // Generate a random salt.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
            byte[] salt = getSalt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
            // generate MAC (MAC key is generated within JCE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
            Mac m = Mac.getInstance("HmacPBESHA1");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
            PBEParameterSpec params =
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  1521
                        new PBEParameterSpec(salt, MAC_ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
            SecretKey key = getPBEKey(passwd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
            m.init(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
            m.update(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
            byte[] macResult = m.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
            // encode as MacData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
            MacData macData = new MacData(algName, macResult, salt,
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  1529
                                                MAC_ITERATION_COUNT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
            DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
            bytes.write(macData.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
            mData = bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
  1534
            throw new IOException("calculateMac failed: " + e, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
        return mData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
     * Validate Certificate Chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    private boolean validateChain(Certificate[] certChain)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
        for (int i = 0; i < certChain.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
            X500Principal issuerDN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
                ((X509Certificate)certChain[i]).getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
            X500Principal subjectDN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
                ((X509Certificate)certChain[i+1]).getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
            if (!(issuerDN.equals(subjectDN)))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
        }
29909
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1553
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1554
        // Check for loops in the chain. If there are repeated certs,
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1555
        // the Set of certs in the chain will contain fewer certs than
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1556
        // the chain
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1557
        Set<Certificate> set = new HashSet<>(Arrays.asList(certChain));
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  1558
        return set.size() == certChain.length;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
40276
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1561
    /*
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1562
     * Check that all the certificates are X.509 certificates
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1563
     */
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1564
    private static void checkX509Certs(Certificate[] certs)
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1565
            throws KeyStoreException {
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1566
        if (certs != null) {
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1567
            for (Certificate cert : certs) {
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1568
                if (!(cert instanceof X509Certificate)) {
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1569
                    throw new KeyStoreException(
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1570
                        "Only X.509 certificates are supported - " +
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1571
                        "rejecting class: " + cert.getClass().getName());
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1572
                }
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1573
            }
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1574
        }
2217f830ca7b 8163503: PKCS12 keystore cannot store non-X.509 certificates
vinnie
parents: 35958
diff changeset
  1575
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
    /*
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1578
     * Create PKCS#12 Attributes, friendlyName, localKeyId and trustedKeyUsage.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
     * Although attributes are optional, they could be required.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
     * For e.g. localKeyId attribute is required to match the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
     * private key with the associated end-entity certificate.
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1583
     * The trustedKeyUsage attribute is used to denote a trusted certificate.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
     * PKCS8ShroudedKeyBags include unique localKeyID and friendlyName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
     * CertBags may or may not include attributes depending on the type
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
     * of Certificate. In end-entity certificates, localKeyID should be
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
     * unique, and the corresponding private key should have the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
     * localKeyID. For trusted CA certs in the cert-chain, localKeyID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
     * attribute is not required, hence most vendors don't include it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
     * NSS/Netscape require it to be unique or null, where as IE/OpenSSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
     * ignore it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
     * Here is a list of pkcs12 attribute values in CertBags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
     * PKCS12 Attribute       NSS/Netscape    IE     OpenSSL    J2SE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
     * --------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
     * LocalKeyId
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
     * (In EE cert only,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
     *  NULL in CA certs)      true          true     true      true
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
     * friendlyName            unique        same/    same/     unique
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
     *                                       unique   unique/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
     *                                                null
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1605
     * trustedKeyUsage         -             -        -         true
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
     * Note: OpenSSL adds friendlyName for end-entity cert only, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
     * removes the localKeyID and friendlyName for CA certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
     * If the CertBag did not have a friendlyName, most vendors will
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
     * add it, and assign it to the DN of the cert.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
     */
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1612
    private byte[] getBagAttributes(String alias, byte[] keyId,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1613
        Set<KeyStore.Entry.Attribute> attributes) throws IOException {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1614
        return getBagAttributes(alias, keyId, null, attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1615
    }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1616
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1617
    private byte[] getBagAttributes(String alias, byte[] keyId,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1618
        ObjectIdentifier[] trustedUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1619
        Set<KeyStore.Entry.Attribute> attributes) throws IOException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
        byte[] localKeyID = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
        byte[] friendlyName = null;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1623
        byte[] trustedKeyUsage = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1625
        // return null if all three attributes are null
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1626
        if ((alias == null) && (keyId == null) && (trustedKeyUsage == null)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
        // SafeBag Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1631
        DerOutputStream bagAttrs = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
        // Encode the friendlyname oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
        if (alias != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
            DerOutputStream bagAttr1 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
            bagAttr1.putOID(PKCS9FriendlyName_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
            DerOutputStream bagAttrContent1 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
            DerOutputStream bagAttrValue1 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
            bagAttrContent1.putBMPString(alias);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
            bagAttr1.write(DerValue.tag_Set, bagAttrContent1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
            bagAttrValue1.write(DerValue.tag_Sequence, bagAttr1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1642
            friendlyName = bagAttrValue1.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1643
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1644
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1645
        // Encode the localkeyId oid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1646
        if (keyId != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1647
            DerOutputStream bagAttr2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1648
            bagAttr2.putOID(PKCS9LocalKeyId_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1649
            DerOutputStream bagAttrContent2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1650
            DerOutputStream bagAttrValue2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1651
            bagAttrContent2.putOctetString(keyId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1652
            bagAttr2.write(DerValue.tag_Set, bagAttrContent2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1653
            bagAttrValue2.write(DerValue.tag_Sequence, bagAttr2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1654
            localKeyID = bagAttrValue2.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1655
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1656
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1657
        // Encode the trustedKeyUsage oid.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1658
        if (trustedUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1659
            DerOutputStream bagAttr3 = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1660
            bagAttr3.putOID(TrustedKeyUsage_OID);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1661
            DerOutputStream bagAttrContent3 = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1662
            DerOutputStream bagAttrValue3 = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1663
            for (ObjectIdentifier usage : trustedUsage) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1664
                bagAttrContent3.putOID(usage);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1665
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1666
            bagAttr3.write(DerValue.tag_Set, bagAttrContent3);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1667
            bagAttrValue3.write(DerValue.tag_Sequence, bagAttr3);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1668
            trustedKeyUsage = bagAttrValue3.toByteArray();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1669
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1670
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1671
        DerOutputStream attrs = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1672
        if (friendlyName != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1673
            attrs.write(friendlyName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1674
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1675
        if (localKeyID != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1676
            attrs.write(localKeyID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1677
        }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1678
        if (trustedKeyUsage != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1679
            attrs.write(trustedKeyUsage);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1680
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1681
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1682
        if (attributes != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1683
            for (KeyStore.Entry.Attribute attribute : attributes) {
15308
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1684
                String attributeName = attribute.getName();
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1685
                // skip friendlyName, localKeyId and trustedKeyUsage
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1686
                if (CORE_ATTRIBUTES[0].equals(attributeName) ||
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1687
                    CORE_ATTRIBUTES[1].equals(attributeName) ||
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1688
                    CORE_ATTRIBUTES[2].equals(attributeName)) {
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1689
                    continue;
55742a890b6c 8006951: Avoid storing duplicate PKCS12 attributes
vinnie
parents: 15307
diff changeset
  1690
                }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1691
                attrs.write(((PKCS12Attribute) attribute).getEncoded());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1692
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1693
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1694
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1695
        bagAttrs.write(DerValue.tag_Set, attrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1696
        return bagAttrs.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1697
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1698
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1699
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1700
     * Create EncryptedData content type, that contains EncryptedContentInfo.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1701
     * Includes certificates in individual SafeBags of type CertBag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1702
     * Each CertBag may include pkcs12 attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1703
     * (see comments in getBagAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1705
    private byte[] createEncryptedData(char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1706
        throws CertificateException, IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1707
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1708
        DerOutputStream out = new DerOutputStream();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1709
        for (Enumeration<String> e = engineAliases(); e.hasMoreElements(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1710
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1711
            String alias = e.nextElement();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1712
            Entry entry = entries.get(alias);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1713
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1714
            // certificate chain
30368
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1715
            Certificate[] certs;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1716
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1717
            if (entry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1718
                PrivateKeyEntry keyEntry = (PrivateKeyEntry) entry;
30368
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1719
                if (keyEntry.chain != null) {
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1720
                    certs = keyEntry.chain;
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1721
                } else {
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1722
                    certs = new Certificate[0];
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1723
                }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1724
            } else if (entry instanceof CertEntry) {
30368
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1725
                certs = new Certificate[]{((CertEntry) entry).cert};
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1726
            } else {
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1727
                certs = new Certificate[0];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1728
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1729
30368
60f02327d396 8079129: NullPointerException in PKCS#12 Keystore in PKCS12KeyStore.java
vinnie
parents: 29909
diff changeset
  1730
            for (int i = 0; i < certs.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1731
                // create SafeBag of Type CertBag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1732
                DerOutputStream safeBag = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1733
                safeBag.putOID(CertBag_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1734
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1735
                // create a CertBag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1736
                DerOutputStream certBag = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1737
                certBag.putOID(PKCS9CertType_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1738
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1739
                // write encoded certs in a context-specific tag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1740
                DerOutputStream certValue = new DerOutputStream();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1741
                X509Certificate cert = (X509Certificate) certs[i];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1742
                certValue.putOctetString(cert.getEncoded());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1743
                certBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1744
                                        true, (byte) 0), certValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1745
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1746
                // wrap CertBag in a Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1747
                DerOutputStream certout = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1748
                certout.write(DerValue.tag_Sequence, certBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1749
                byte[] certBagValue = certout.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1750
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1751
                // Wrap the CertBag encoding in a context-specific tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1752
                DerOutputStream bagValue = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1753
                bagValue.write(certBagValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1754
                // write SafeBag Value
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1755
                safeBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1756
                                true, (byte) 0), bagValue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1757
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1758
                // write SafeBag Attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1759
                // All Certs should have a unique friendlyName.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1760
                // This change is made to meet NSS requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1761
                byte[] bagAttrs = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1762
                if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1763
                    // Only End-Entity Cert should have a localKeyId.
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1764
                    if (entry instanceof KeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1765
                        KeyEntry keyEntry = (KeyEntry) entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1766
                        bagAttrs =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1767
                            getBagAttributes(keyEntry.alias, keyEntry.keyId,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1768
                                keyEntry.attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1769
                    } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1770
                        CertEntry certEntry = (CertEntry) entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1771
                        bagAttrs =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1772
                            getBagAttributes(certEntry.alias, certEntry.keyId,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1773
                                certEntry.trustedKeyUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1774
                                certEntry.attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1775
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1776
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1777
                    // Trusted root CA certs and Intermediate CA certs do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1778
                    // need to have a localKeyId, and hence localKeyId is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1779
                    // This change is made to meet NSS/Netscape requirements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1780
                    // NSS pkcs12 library requires trusted CA certs in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1781
                    // certificate chain to have unique or null localKeyID.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1782
                    // However, IE/OpenSSL do not impose this restriction.
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  1783
                    bagAttrs = getBagAttributes(
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1784
                            cert.getSubjectX500Principal().getName(), null,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1785
                            entry.attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1786
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1787
                if (bagAttrs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1788
                    safeBag.write(bagAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1789
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1790
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1791
                // wrap as Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1792
                out.write(DerValue.tag_Sequence, safeBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1793
            } // for cert-chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1794
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1795
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1796
        // wrap as SequenceOf SafeBag
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1797
        DerOutputStream safeBagValue = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1798
        safeBagValue.write(DerValue.tag_SequenceOf, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1799
        byte[] safeBagData = safeBagValue.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1800
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1801
        // encrypt the content (EncryptedContentInfo)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1802
        byte[] encrContentInfo = encryptContent(safeBagData, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1803
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1804
        // -- SEQUENCE of EncryptedData
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1805
        DerOutputStream encrData = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1806
        DerOutputStream encrDataContent = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1807
        encrData.putInteger(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1808
        encrData.write(encrContentInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1809
        encrDataContent.write(DerValue.tag_Sequence, encrData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1810
        return encrDataContent.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1811
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1812
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1813
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1814
     * Create SafeContent Data content type.
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1815
     * Includes encrypted secret key in a SafeBag of type SecretBag.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1816
     * Includes encrypted private key in a SafeBag of type PKCS8ShroudedKeyBag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1817
     * Each PKCS8ShroudedKeyBag includes pkcs12 attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1818
     * (see comments in getBagAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1819
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1820
    private byte[] createSafeContent()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1821
        throws CertificateException, IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1822
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1823
        DerOutputStream out = new DerOutputStream();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1824
        for (Enumeration<String> e = engineAliases(); e.hasMoreElements(); ) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1825
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1826
            String alias = e.nextElement();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1827
            Entry entry = entries.get(alias);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1828
            if (entry == null || (!(entry instanceof KeyEntry))) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1829
                continue;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1830
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1831
            DerOutputStream safeBag = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1832
            KeyEntry keyEntry = (KeyEntry) entry;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1833
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1834
            // DER encode the private key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1835
            if (keyEntry instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1836
                // Create SafeBag of type pkcs8ShroudedKeyBag
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1837
                safeBag.putOID(PKCS8ShroudedKeyBag_OID);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1838
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1839
                // get the encrypted private key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1840
                byte[] encrBytes = ((PrivateKeyEntry)keyEntry).protectedPrivKey;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1841
                EncryptedPrivateKeyInfo encrInfo = null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1842
                try {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1843
                    encrInfo = new EncryptedPrivateKeyInfo(encrBytes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1844
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1845
                } catch (IOException ioe) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1846
                    throw new IOException("Private key not stored as "
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1847
                            + "PKCS#8 EncryptedPrivateKeyInfo"
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1848
                            + ioe.getMessage());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1849
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1850
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1851
                // Wrap the EncryptedPrivateKeyInfo in a context-specific tag.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1852
                DerOutputStream bagValue = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1853
                bagValue.write(encrInfo.getEncoded());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1854
                safeBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1855
                                true, (byte) 0), bagValue);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1856
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1857
            // DER encode the secret key
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1858
            } else if (keyEntry instanceof SecretKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1859
                // Create SafeBag of type SecretBag
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1860
                safeBag.putOID(SecretBag_OID);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1861
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1862
                // Create a SecretBag
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1863
                DerOutputStream secretBag = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1864
                secretBag.putOID(PKCS8ShroudedKeyBag_OID);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1865
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1866
                // Write secret key in a context-specific tag
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1867
                DerOutputStream secretKeyValue = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1868
                secretKeyValue.putOctetString(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1869
                    ((SecretKeyEntry) keyEntry).protectedSecretKey);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1870
                secretBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1871
                                        true, (byte) 0), secretKeyValue);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1872
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1873
                // Wrap SecretBag in a Sequence
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1874
                DerOutputStream secretBagSeq = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1875
                secretBagSeq.write(DerValue.tag_Sequence, secretBag);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1876
                byte[] secretBagValue = secretBagSeq.toByteArray();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1877
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1878
                // Wrap the secret bag in a context-specific tag.
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1879
                DerOutputStream bagValue = new DerOutputStream();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1880
                bagValue.write(secretBagValue);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1881
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1882
                // Write SafeBag value
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1883
                safeBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1884
                                    true, (byte) 0), bagValue);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1885
            } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1886
                continue; // skip this entry
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1887
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1888
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1889
            // write SafeBag Attributes
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1890
            byte[] bagAttrs =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  1891
                getBagAttributes(alias, entry.keyId, entry.attributes);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1892
            safeBag.write(bagAttrs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1893
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1894
            // wrap as Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1895
            out.write(DerValue.tag_Sequence, safeBag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1896
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1897
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1898
        // wrap as Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1899
        DerOutputStream safeBagValue = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1900
        safeBagValue.write(DerValue.tag_Sequence, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1901
        return safeBagValue.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1902
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1903
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1904
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1905
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1906
     * Encrypt the contents using Password-based (PBE) encryption
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1907
     * as defined in PKCS #5.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1908
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1909
     * NOTE: Currently pbeWithSHAAnd40BiteRC2-CBC algorithmID is used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1910
     *       to derive the key and IV.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1911
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1912
     * @return encrypted contents encoded as EncryptedContentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1913
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1914
    private byte[] encryptContent(byte[] data, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1915
        throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1916
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1917
        byte[] encryptedData = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1918
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1919
        // create AlgorithmParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1920
        AlgorithmParameters algParams =
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  1921
                getPBEAlgorithmParameters("PBEWithSHA1AndRC2_40");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1922
        DerOutputStream bytes = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1923
        AlgorithmId algId =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1924
                new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1925
        algId.encode(bytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1926
        byte[] encodedAlgId = bytes.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1927
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1928
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1929
            // Use JCE
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1930
            SecretKey skey = getPBEKey(password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1931
            Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1932
            cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1933
            encryptedData = cipher.doFinal(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1934
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1935
            if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1936
                debug.println("  (Cipher algorithm: " + cipher.getAlgorithm() +
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1937
                    ")");
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1938
            }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  1939
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1940
        } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
  1941
            throw new IOException("Failed to encrypt" +
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
  1942
                    " safe contents entry: " + e, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1943
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1944
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1945
        // create EncryptedContentInfo
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1946
        DerOutputStream bytes2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1947
        bytes2.putOID(ContentInfo.DATA_OID);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1948
        bytes2.write(encodedAlgId);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1949
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1950
        // Wrap encrypted data in a context-specific tag.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1951
        DerOutputStream tmpout2 = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1952
        tmpout2.putOctetString(encryptedData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1953
        bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1954
                                        false, (byte)0), tmpout2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1955
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1956
        // wrap EncryptedContentInfo in a Sequence
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1957
        DerOutputStream out = new DerOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1958
        out.write(DerValue.tag_Sequence, bytes2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1959
        return out.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1960
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1961
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1962
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1963
     * Loads the keystore from the given input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1964
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1965
     * <p>If a password is given, it is used to check the integrity of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1966
     * keystore data. Otherwise, the integrity of the keystore is not checked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1967
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1968
     * @param stream the input stream from which the keystore is loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1969
     * @param password the (optional) password used to check the integrity of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1970
     * the keystore.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1971
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1972
     * @exception IOException if there is an I/O or format problem with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1973
     * keystore data
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1974
     * @exception NoSuchAlgorithmException if the algorithm used to check
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1975
     * the integrity of the keystore cannot be found
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1976
     * @exception CertificateException if any of the certificates in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1977
     * keystore could not be loaded
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1978
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1979
    public synchronized void engineLoad(InputStream stream, char[] password)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1980
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1981
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1982
        DataInputStream dis;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1983
        CertificateFactory cf = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1984
        ByteArrayInputStream bais = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1985
        byte[] encoded = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1986
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1987
        if (stream == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1988
           return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1989
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1990
        // reset the counter
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1991
        counter = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1992
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1993
        DerValue val = new DerValue(stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1994
        DerInputStream s = val.toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1995
        int version = s.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1996
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1997
        if (version != VERSION_3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1998
           throw new IOException("PKCS12 keystore not in version 3 format");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1999
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2000
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2001
        entries.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2002
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2003
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2004
         * Read the authSafe.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2005
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2006
        byte[] authSafeData;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2007
        ContentInfo authSafe = new ContentInfo(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2008
        ObjectIdentifier contentType = authSafe.getContentType();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2009
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2010
        if (contentType.equals(ContentInfo.DATA_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2011
           authSafeData = authSafe.getData();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2012
        } else /* signed data */ {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2013
           throw new IOException("public key protected PKCS12 not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2014
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2015
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2016
        DerInputStream as = new DerInputStream(authSafeData);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2017
        DerValue[] safeContentsArray = as.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2018
        int count = safeContentsArray.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2019
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2020
        // reset the counters at the start
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2021
        privateKeyCount = 0;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2022
        secretKeyCount = 0;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2023
        certificateCount = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2024
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2025
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2026
         * Spin over the ContentInfos.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2027
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2028
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2029
            ContentInfo safeContents;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2030
            DerInputStream sci;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2031
            byte[] eAlgId = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2033
            sci = new DerInputStream(safeContentsArray[i].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2034
            safeContents = new ContentInfo(sci);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2035
            contentType = safeContents.getContentType();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2036
            if (contentType.equals(ContentInfo.DATA_OID)) {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2037
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2038
                if (debug != null) {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2039
                    debug.println("Loading PKCS#7 data");
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2040
                }
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2041
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2042
                loadSafeContents(new DerInputStream(safeContents.getData()));
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2043
            } else if (contentType.equals(ContentInfo.ENCRYPTED_DATA_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2044
                if (password == null) {
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2045
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2046
                    if (debug != null) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2047
                        debug.println("Warning: skipping PKCS#7 encryptedData" +
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2048
                            " - no password was supplied");
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2049
                    }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2050
                    continue;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2051
                }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2052
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2053
                DerInputStream edi =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2054
                                safeContents.getContent().toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2055
                int edVersion = edi.getInteger();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2056
                DerValue[] seq = edi.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2057
                ObjectIdentifier edContentType = seq[0].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2058
                eAlgId = seq[1].toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2059
                if (!seq[2].isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2060
                   throw new IOException("encrypted content not present!");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2061
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2062
                byte newTag = DerValue.tag_OctetString;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2063
                if (seq[2].isConstructed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2064
                   newTag |= 0x20;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2065
                seq[2].resetTag(newTag);
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2066
                byte[] rawData = seq[2].getOctetString();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2068
                // parse Algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2069
                DerInputStream in = seq[1].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2070
                ObjectIdentifier algOid = in.getOID();
15661
282a9cfb26ca 8007934: algorithm parameters for PBE Scheme 2 not decoded correctly in PKCS12 keystore
vinnie
parents: 15538
diff changeset
  2071
                AlgorithmParameters algParams = parseAlgParameters(algOid, in);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2072
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2073
                PBEParameterSpec pbeSpec;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2074
                int ic = 0;
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2075
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2076
                if (algParams != null) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2077
                    try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2078
                        pbeSpec =
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2079
                            algParams.getParameterSpec(PBEParameterSpec.class);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2080
                    } catch (InvalidParameterSpecException ipse) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2081
                        throw new IOException(
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2082
                            "Invalid PBE algorithm parameters");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2083
                    }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2084
                    ic = pbeSpec.getIterationCount();
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2085
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2086
                    if (ic > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2087
                        throw new IOException("PBE iteration count too large");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2088
                    }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2089
                }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2090
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2091
                if (debug != null) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2092
                    debug.println("Loading PKCS#7 encryptedData " +
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2093
                        "(" + new AlgorithmId(algOid).getName() +
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2094
                        " iterations: " + ic + ")");
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2095
                }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2096
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2097
                try {
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2098
                    RetryWithZero.run(pass -> {
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10369
diff changeset
  2099
                        // Use JCE
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2100
                        SecretKey skey = getPBEKey(pass);
51142
69dc9ea17b33 8202837: PBES2 AlgorithmId encoding error in PKCS12 KeyStore
weijun
parents: 51063
diff changeset
  2101
                        Cipher cipher = Cipher.getInstance(
69dc9ea17b33 8202837: PBES2 AlgorithmId encoding error in PKCS12 KeyStore
weijun
parents: 51063
diff changeset
  2102
                                mapPBEParamsToAlgorithm(algOid, algParams));
11835
c9e7cfc908b3 6879539: enable empty password support for pkcs12 keystore
weijun
parents: 10369
diff changeset
  2103
                        cipher.init(Cipher.DECRYPT_MODE, skey, algParams);
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2104
                        loadSafeContents(new DerInputStream(cipher.doFinal(rawData)));
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2105
                        return null;
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2106
                    }, password);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2107
                } catch (Exception e) {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2108
                    throw new IOException("keystore password was incorrect",
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2109
                            new UnrecoverableKeyException(
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2110
                                    "failed to decrypt safe contents entry: " + e));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2111
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2112
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2113
                throw new IOException("public key protected PKCS12" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2114
                                        " not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2115
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2117
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2118
        // The MacData is optional.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2119
        if (password != null && s.available() > 0) {
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2120
            MacData macData = new MacData(s);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2121
            int ic = macData.getIterations();
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2122
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2123
            try {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2124
                if (ic > MAX_ITERATION_COUNT) {
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2125
                    throw new InvalidAlgorithmParameterException(
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2126
                        "MAC iteration count too large: " + ic);
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2127
                }
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2128
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  2129
                String algName =
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  2130
                        macData.getDigestAlgName().toUpperCase(Locale.ENGLISH);
13361
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 11835
diff changeset
  2131
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 11835
diff changeset
  2132
                // Change SHA-1 to SHA1
bda5c2354fc6 7180907: Jarsigner -verify fails if rsa file used sha-256 with authenticated attributes
weijun
parents: 11835
diff changeset
  2133
                algName = algName.replace("-", "");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2134
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2135
                // generate MAC (MAC key is created within JCE)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2136
                Mac m = Mac.getInstance("HmacPBE" + algName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2137
                PBEParameterSpec params =
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2138
                        new PBEParameterSpec(macData.getSalt(), ic);
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2139
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2140
                RetryWithZero.run(pass -> {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2141
                    SecretKey key = getPBEKey(pass);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2142
                    m.init(key, params);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2143
                    m.update(authSafeData);
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2144
                    byte[] macResult = m.doFinal();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2145
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2146
                    if (debug != null) {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2147
                        debug.println("Checking keystore integrity " +
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2148
                                "(" + m.getAlgorithm() + " iterations: " + ic + ")");
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2149
                    }
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2150
50778
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2151
                    if (!MessageDigest.isEqual(macData.getDigest(), macResult)) {
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2152
                        throw new UnrecoverableKeyException("Failed PKCS12" +
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2153
                                " integrity checking");
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2154
                    }
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2155
                    return (Void)null;
bba1deda9216 8202299: Java Keystore fails to load PKCS12/PFX certificates created in WindowsServer2016
weijun
parents: 47417
diff changeset
  2156
                }, password);
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2157
            } catch (Exception e) {
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 5973
diff changeset
  2158
                throw new IOException("Integrity check failed: " + e, e);
47417
5984d1c9d03d 8181692: Update storage implementations
vinnie
parents: 47216
diff changeset
  2159
            }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2161
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2162
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2163
         * Match up private keys with certificate chains.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2164
         */
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2165
        PrivateKeyEntry[] list =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2166
            keyList.toArray(new PrivateKeyEntry[keyList.size()]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2167
        for (int m = 0; m < list.length; m++) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2168
            PrivateKeyEntry entry = list[m];
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2169
            if (entry.keyId != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2170
                ArrayList<X509Certificate> chain =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2171
                                new ArrayList<X509Certificate>();
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2172
                X509Certificate cert = findMatchedCertificate(entry);
29909
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2173
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2174
                mainloop:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2175
                while (cert != null) {
29909
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2176
                    // Check for loops in the certificate chain
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2177
                    if (!chain.isEmpty()) {
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2178
                        for (X509Certificate chainCert : chain) {
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2179
                            if (cert.equals(chainCert)) {
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2180
                                if (debug != null) {
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2181
                                    debug.println("Loop detected in " +
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2182
                                        "certificate chain. Skip adding " +
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2183
                                        "repeated cert to chain. Subject: " +
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2184
                                        cert.getSubjectX500Principal()
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2185
                                            .toString());
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2186
                                }
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2187
                                break mainloop;
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2188
                            }
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2189
                        }
388fe481deeb 8066479: Better certificate chain validation
juh
parents: 28243
diff changeset
  2190
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2191
                    chain.add(cert);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2192
                    X500Principal issuerDN = cert.getIssuerX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2193
                    if (issuerDN.equals(cert.getSubjectX500Principal())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2194
                        break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2195
                    }
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2196
                    cert = certsMap.get(issuerDN);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2197
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2198
                /* Update existing KeyEntry in entries table */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2199
                if (chain.size() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2200
                    entry.chain = chain.toArray(new Certificate[chain.size()]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2201
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2202
        }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2203
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2204
        if (debug != null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2205
            if (privateKeyCount > 0) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2206
                debug.println("Loaded " + privateKeyCount +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2207
                    " protected private key(s)");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2208
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2209
            if (secretKeyCount > 0) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2210
                debug.println("Loaded " + secretKeyCount +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2211
                    " protected secret key(s)");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2212
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2213
            if (certificateCount > 0) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2214
                debug.println("Loaded " + certificateCount +
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2215
                    " certificate(s)");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2216
            }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2217
        }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2218
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2219
        certEntries.clear();
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2220
        certsMap.clear();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2221
        keyList.clear();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2222
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2223
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2224
    /**
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2225
     * Locates a matched CertEntry from certEntries, and returns its cert.
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2226
     * @param entry the KeyEntry to match
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2227
     * @return a certificate, null if not found
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2228
     */
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2229
    private X509Certificate findMatchedCertificate(PrivateKeyEntry entry) {
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2230
        CertEntry keyIdMatch = null;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2231
        CertEntry aliasMatch = null;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2232
        for (CertEntry ce: certEntries) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2233
            if (Arrays.equals(entry.keyId, ce.keyId)) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2234
                keyIdMatch = ce;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2235
                if (entry.alias.equalsIgnoreCase(ce.alias)) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2236
                    // Full match!
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2237
                    return ce.cert;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2238
                }
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2239
            } else if (entry.alias.equalsIgnoreCase(ce.alias)) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2240
                aliasMatch = ce;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2241
            }
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2242
        }
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2243
        // keyId match first, for compatibility
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2244
        if (keyIdMatch != null) return keyIdMatch.cert;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2245
        else if (aliasMatch != null) return aliasMatch.cert;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2246
        else return null;
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2247
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2248
51063
038688fa32d0 8206189: sun/security/pkcs12/EmptyPassword.java fails with Sequence tag error
weijun
parents: 50778
diff changeset
  2249
    private void loadSafeContents(DerInputStream stream)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2250
        throws IOException, NoSuchAlgorithmException, CertificateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2251
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2252
        DerValue[] safeBags = stream.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2253
        int count = safeBags.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2254
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2255
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2256
         * Spin over the SafeBags.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2257
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2258
        for (int i = 0; i < count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2259
            ObjectIdentifier bagId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2260
            DerInputStream sbi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2261
            DerValue bagValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2262
            Object bagItem = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2263
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2264
            sbi = safeBags[i].toDerInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2265
            bagId = sbi.getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2266
            bagValue = sbi.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2267
            if (!bagValue.isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2268
                throw new IOException("unsupported PKCS12 bag value type "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2269
                                        + bagValue.tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2270
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2271
            bagValue = bagValue.data.getDerValue();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2272
            if (bagId.equals(PKCS8ShroudedKeyBag_OID)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2273
                PrivateKeyEntry kEntry = new PrivateKeyEntry();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2274
                kEntry.protectedPrivKey = bagValue.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2275
                bagItem = kEntry;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2276
                privateKeyCount++;
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2277
            } else if (bagId.equals(CertBag_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2278
                DerInputStream cs = new DerInputStream(bagValue.toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2279
                DerValue[] certValues = cs.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2280
                ObjectIdentifier certId = certValues[0].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2281
                if (!certValues[1].isContextSpecific((byte)0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2282
                    throw new IOException("unsupported PKCS12 cert value type "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2283
                                        + certValues[1].tag);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2284
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2285
                DerValue certValue = certValues[1].data.getDerValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2286
                CertificateFactory cf = CertificateFactory.getInstance("X509");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2287
                X509Certificate cert;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2288
                cert = (X509Certificate)cf.generateCertificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2289
                        (new ByteArrayInputStream(certValue.getOctetString()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2290
                bagItem = cert;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2291
                certificateCount++;
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2292
            } else if (bagId.equals(SecretBag_OID)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2293
                DerInputStream ss = new DerInputStream(bagValue.toByteArray());
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2294
                DerValue[] secretValues = ss.getSequence(2);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2295
                ObjectIdentifier secretId = secretValues[0].getOID();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2296
                if (!secretValues[1].isContextSpecific((byte)0)) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2297
                    throw new IOException(
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2298
                        "unsupported PKCS12 secret value type "
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2299
                                        + secretValues[1].tag);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2300
                }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2301
                DerValue secretValue = secretValues[1].data.getDerValue();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2302
                SecretKeyEntry kEntry = new SecretKeyEntry();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2303
                kEntry.protectedSecretKey = secretValue.getOctetString();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2304
                bagItem = kEntry;
15538
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  2305
                secretKeyCount++;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2306
            } else {
15297
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2307
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2308
                if (debug != null) {
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2309
                    debug.println("Unsupported PKCS12 bag type: " + bagId);
eb3d7b36b4c4 8006591: Protect keystore entries using stronger PBE algorithms
vinnie
parents: 14342
diff changeset
  2310
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2311
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2312
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2313
            DerValue[] attrSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2314
            try {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2315
                attrSet = sbi.getSet(3);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2316
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2317
                // entry does not have attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2318
                // Note: CA certs can have no attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2319
                // OpenSSL generates pkcs12 with no attr for CA certs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2320
                attrSet = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2321
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2322
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2323
            String alias = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2324
            byte[] keyId = null;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2325
            ObjectIdentifier[] trustedKeyUsage = null;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2326
            Set<PKCS12Attribute> attributes = new HashSet<>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2327
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2328
            if (attrSet != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2329
                for (int j = 0; j < attrSet.length; j++) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2330
                    byte[] encoded = attrSet[j].toByteArray();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2331
                    DerInputStream as = new DerInputStream(encoded);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2332
                    DerValue[] attrSeq = as.getSequence(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2333
                    ObjectIdentifier attrId = attrSeq[0].getOID();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2334
                    DerInputStream vs =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2335
                        new DerInputStream(attrSeq[1].toByteArray());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2336
                    DerValue[] valSet;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2337
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2338
                        valSet = vs.getSet(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2339
                    } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2340
                        throw new IOException("Attribute " + attrId +
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2341
                                " should have a value " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2342
                    }
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2343
                    if (attrId.equals(PKCS9FriendlyName_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2344
                        alias = valSet[0].getBMPString();
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2345
                    } else if (attrId.equals(PKCS9LocalKeyId_OID)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2346
                        keyId = valSet[0].getOctetString();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2347
                    } else if
31426
9cd672654f97 8022444: Remove sun.security.util.ObjectIdentifier.equals(ObjectIdentifier other) method
juh
parents: 30368
diff changeset
  2348
                        (attrId.equals(TrustedKeyUsage_OID)) {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2349
                        trustedKeyUsage = new ObjectIdentifier[valSet.length];
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2350
                        for (int k = 0; k < valSet.length; k++) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2351
                            trustedKeyUsage[k] = valSet[k].getOID();
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2352
                        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2353
                    } else {
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2354
                        attributes.add(new PKCS12Attribute(encoded));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2355
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2356
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2357
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2358
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2359
            /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2360
             * As per PKCS12 v1.0 friendlyname (alias) and localKeyId (keyId)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2361
             * are optional PKCS12 bagAttributes. But entries in the keyStore
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2362
             * are identified by their alias. Hence we need to have an
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2363
             * Unfriendlyname in the alias, if alias is null. The keyId
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2364
             * attribute is required to match the private key with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2365
             * certificate. If we get a bagItem of type KeyEntry with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2366
             * null keyId, we should skip it entirely.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2367
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2368
            if (bagItem instanceof KeyEntry) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2369
                KeyEntry entry = (KeyEntry)bagItem;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2370
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2371
                if (bagItem instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2372
                    if (keyId == null) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2373
                       // Insert a localKeyID for the privateKey
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2374
                       // Note: This is a workaround to allow null localKeyID
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2375
                       // attribute in pkcs12 with one private key entry and
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2376
                       // associated cert-chain
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2377
                       if (privateKeyCount == 1) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2378
                            keyId = "01".getBytes("UTF8");
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2379
                       } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2380
                            continue;
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2381
                       }
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2382
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2383
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2384
                entry.keyId = keyId;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2385
                // restore date if it exists
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2386
                String keyIdStr = new String(keyId, "UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2387
                Date date = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2388
                if (keyIdStr.startsWith("Time ")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2389
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2390
                        date = new Date(
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2391
                                Long.parseLong(keyIdStr.substring(5)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2392
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2393
                        date = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2394
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2395
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2396
                if (date == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2397
                    date = new Date();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2399
                entry.date = date;
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2400
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2401
                if (bagItem instanceof PrivateKeyEntry) {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2402
                    keyList.add((PrivateKeyEntry) entry);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2403
                }
15538
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  2404
                if (entry.attributes == null) {
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  2405
                    entry.attributes = new HashSet<>();
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  2406
                }
02e547c0b530 8007483: attributes are ignored when loading keys from a PKCS12 keystore
vinnie
parents: 15308
diff changeset
  2407
                entry.attributes.addAll(attributes);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2408
                if (alias == null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2409
                   alias = getUnfriendlyName();
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2410
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2411
                entry.alias = alias;
10369
e9d2e59e53f0 7059542: JNDI name operations should be locale independent
xuelei
parents: 10336
diff changeset
  2412
                entries.put(alias.toLowerCase(Locale.ENGLISH), entry);
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2413
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2414
            } else if (bagItem instanceof X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2415
                X509Certificate cert = (X509Certificate)bagItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2416
                // Insert a localKeyID for the corresponding cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2417
                // Note: This is a workaround to allow null localKeyID
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2418
                // attribute in pkcs12 with one private key entry and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2419
                // associated cert-chain
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2420
                if ((keyId == null) && (privateKeyCount == 1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2421
                    // insert localKeyID only for EE cert or self-signed cert
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2422
                    if (i == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2423
                        keyId = "01".getBytes("UTF8");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2424
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2425
                }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2426
                // Trusted certificate
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2427
                if (trustedKeyUsage != null) {
15307
6c19bd915338 8006946: PKCS12 test failure due to incorrect alias name
vinnie
parents: 15301
diff changeset
  2428
                    if (alias == null) {
6c19bd915338 8006946: PKCS12 test failure due to incorrect alias name
vinnie
parents: 15301
diff changeset
  2429
                        alias = getUnfriendlyName();
6c19bd915338 8006946: PKCS12 test failure due to incorrect alias name
vinnie
parents: 15301
diff changeset
  2430
                    }
15298
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2431
                    CertEntry certEntry =
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2432
                        new CertEntry(cert, keyId, alias, trustedKeyUsage,
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2433
                            attributes);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2434
                    entries.put(alias.toLowerCase(Locale.ENGLISH), certEntry);
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2435
                } else {
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2436
                    certEntries.add(new CertEntry(cert, keyId, alias));
06867bfe82ac 8005408: KeyStore API enhancements
vinnie
parents: 15297
diff changeset
  2437
                }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2438
                X500Principal subjectDN = cert.getSubjectX500Principal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2439
                if (subjectDN != null) {
5973
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2440
                    if (!certsMap.containsKey(subjectDN)) {
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2441
                        certsMap.put(subjectDN, cert);
b48e1702532e 6958026: Problem with PKCS12 keystore
weijun
parents: 5506
diff changeset
  2442
                    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2443
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2446
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2447
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2448
    private String getUnfriendlyName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2449
        counter++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2450
        return (String.valueOf(counter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2451
    }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2452
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2453
    /*
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2454
     * PKCS12 permitted first 24 bytes:
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2455
     *
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2456
     * 30 82 -- -- 02 01 03 30 82 -- -- 06 09 2A 86 48 86 F7 0D 01 07 01 A0 8-
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2457
     * 30 -- 02 01 03 30 -- 06 09 2A 86 48 86 F7 0D 01 07 01 A0 -- 04 -- -- --
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2458
     * 30 81 -- 02 01 03 30 81 -- 06 09 2A 86 48 86 F7 0D 01 07 01 A0 81 -- 04
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2459
     * 30 82 -- -- 02 01 03 30 81 -- 06 09 2A 86 48 86 F7 0D 01 07 01 A0 81 --
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2460
     * 30 83 -- -- -- 02 01 03 30 82 -- -- 06 09 2A 86 48 86 F7 0D 01 07 01 A0
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2461
     * 30 83 -- -- -- 02 01 03 30 83 -- -- -- 06 09 2A 86 48 86 F7 0D 01 07 01
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2462
     * 30 84 -- -- -- -- 02 01 03 30 83 -- -- -- 06 09 2A 86 48 86 F7 0D 01 07
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2463
     * 30 84 -- -- -- -- 02 01 03 30 84 -- -- -- -- 06 09 2A 86 48 86 F7 0D 01
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2464
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2465
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2466
    private static final long[][] PKCS12_HEADER_PATTERNS = {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2467
        { 0x3082000002010330L, 0x82000006092A8648L, 0x86F70D010701A080L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2468
        { 0x3000020103300006L, 0x092A864886F70D01L, 0x0701A00004000000L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2469
        { 0x3081000201033081L, 0x0006092A864886F7L, 0x0D010701A0810004L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2470
        { 0x3082000002010330L, 0x810006092A864886L, 0xF70D010701A08100L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2471
        { 0x3083000000020103L, 0x3082000006092A86L, 0x4886F70D010701A0L },
45636
87a2aeead10e 8181978: Keystore probing mechanism fails for large PKCS12 keystores
vinnie
parents: 43543
diff changeset
  2472
        { 0x3083000000020103L, 0x308300000006092AL, 0x864886F70D010701L },
87a2aeead10e 8181978: Keystore probing mechanism fails for large PKCS12 keystores
vinnie
parents: 43543
diff changeset
  2473
        { 0x3084000000000201L, 0x0330830000000609L, 0x2A864886F70D0107L },
87a2aeead10e 8181978: Keystore probing mechanism fails for large PKCS12 keystores
vinnie
parents: 43543
diff changeset
  2474
        { 0x3084000000000201L, 0x0330840000000006L, 0x092A864886F70D01L }
28243
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2475
    };
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2476
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2477
    private static final long[][] PKCS12_HEADER_MASKS = {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2478
        { 0xFFFF0000FFFFFFFFL, 0xFF0000FFFFFFFFFFL, 0xFFFFFFFFFFFFFFF0L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2479
        { 0xFF00FFFFFFFF00FFL, 0xFFFFFFFFFFFFFFFFL, 0xFFFFFF00FF000000L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2480
        { 0xFFFF00FFFFFFFFFFL, 0x00FFFFFFFFFFFFFFL, 0xFFFFFFFFFFFF00FFL },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2481
        { 0xFFFF0000FFFFFFFFL, 0xFF00FFFFFFFFFFFFL, 0xFFFFFFFFFFFFFF00L },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2482
        { 0xFFFF000000FFFFFFL, 0xFFFF0000FFFFFFFFL, 0xFFFFFFFFFFFFFFFFL },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2483
        { 0xFFFF000000FFFFFFL, 0xFFFF000000FFFFFFL, 0xFFFFFFFFFFFFFFFFL },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2484
        { 0xFFFF00000000FFFFL, 0xFFFFFF000000FFFFL, 0xFFFFFFFFFFFFFFFFL },
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2485
        { 0xFFFF00000000FFFFL, 0xFFFFFF00000000FFL, 0xFFFFFFFFFFFFFFFFL }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2486
    };
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2487
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2488
    /**
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2489
     * 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
  2490
     * PKCS12 keystore encoding.
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2491
     */
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2492
    @Override
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2493
    public boolean engineProbe(InputStream stream) throws IOException {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2494
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2495
        DataInputStream dataStream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2496
        if (stream instanceof DataInputStream) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2497
            dataStream = (DataInputStream)stream;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2498
        } else {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2499
            dataStream = new DataInputStream(stream);
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2500
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2501
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2502
        long firstPeek = dataStream.readLong();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2503
        long nextPeek = dataStream.readLong();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2504
        long finalPeek = dataStream.readLong();
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2505
        boolean result = false;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2506
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2507
        for (int i = 0; i < PKCS12_HEADER_PATTERNS.length; i++) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2508
            if (PKCS12_HEADER_PATTERNS[i][0] ==
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2509
                    (firstPeek & PKCS12_HEADER_MASKS[i][0]) &&
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2510
                (PKCS12_HEADER_PATTERNS[i][1] ==
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2511
                    (nextPeek & PKCS12_HEADER_MASKS[i][1])) &&
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2512
                (PKCS12_HEADER_PATTERNS[i][2] ==
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2513
                    (finalPeek & PKCS12_HEADER_MASKS[i][2]))) {
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2514
                result = true;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2515
                break;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2516
            }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2517
        }
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2518
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2519
        return result;
47080f9ae750 8044445: JEP 229: Create PKCS12 Keystores by Default
vinnie
parents: 25859
diff changeset
  2520
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  2521
}