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