0N/A/*
2549N/A * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.security.pkcs12;
0N/A
0N/Aimport java.io.*;
0N/Aimport java.security.MessageDigest;
0N/Aimport java.security.NoSuchAlgorithmException;
0N/Aimport java.security.Key;
0N/Aimport java.security.KeyFactory;
0N/Aimport java.security.PrivateKey;
0N/Aimport java.security.KeyStoreSpi;
0N/Aimport java.security.KeyStoreException;
0N/Aimport java.security.UnrecoverableKeyException;
0N/Aimport java.security.SecureRandom;
0N/Aimport java.security.cert.Certificate;
0N/Aimport java.security.cert.CertificateFactory;
0N/Aimport java.security.cert.X509Certificate;
0N/Aimport java.security.cert.CertificateException;
0N/Aimport java.security.spec.PKCS8EncodedKeySpec;
0N/Aimport java.util.*;
0N/Aimport java.math.*;
0N/A
0N/Aimport java.security.AlgorithmParameters;
0N/Aimport java.security.spec.AlgorithmParameterSpec;
0N/Aimport javax.crypto.spec.PBEParameterSpec;
0N/Aimport javax.crypto.spec.PBEKeySpec;
0N/Aimport javax.crypto.spec.SecretKeySpec;
0N/Aimport javax.crypto.SecretKeyFactory;
0N/Aimport javax.crypto.SecretKey;
0N/Aimport javax.crypto.Cipher;
0N/Aimport javax.crypto.Mac;
0N/Aimport javax.security.auth.x500.X500Principal;
0N/A
0N/Aimport sun.security.util.DerInputStream;
0N/Aimport sun.security.util.DerOutputStream;
0N/Aimport sun.security.util.DerValue;
0N/Aimport sun.security.util.ObjectIdentifier;
0N/Aimport sun.security.pkcs.ContentInfo;
0N/Aimport sun.security.x509.AlgorithmId;
0N/Aimport sun.security.pkcs.EncryptedPrivateKeyInfo;
0N/A
0N/A
0N/A/**
0N/A * This class provides the keystore implementation referred to as "PKCS12".
0N/A * Implements the PKCS#12 PFX protected using the Password privacy mode.
0N/A * The contents are protected using Password integrity mode.
0N/A *
0N/A * Currently we support following PBE algorithms:
0N/A * - pbeWithSHAAnd3KeyTripleDESCBC to encrypt private keys
0N/A * - pbeWithSHAAnd40BitRC2CBC to encrypt certificates
0N/A *
0N/A * Supported encryption of various implementations :
0N/A *
0N/A * Software and mode. Certificate encryption Private key encryption
0N/A * ---------------------------------------------------------------------
0N/A * MSIE4 (domestic 40 bit RC2. 40 bit RC2
0N/A * and xport versions)
0N/A * PKCS#12 export.
0N/A *
0N/A * MSIE4, 5 (domestic 40 bit RC2, 40 bit RC2,
0N/A * and export versions) 3 key triple DES 3 key triple DES
0N/A * PKCS#12 import.
0N/A *
0N/A * MSIE5 40 bit RC2 3 key triple DES,
0N/A * PKCS#12 export. with SHA1 (168 bits)
0N/A *
0N/A * Netscape Communicator 40 bit RC2 3 key triple DES,
0N/A * (domestic and export with SHA1 (168 bits)
0N/A * versions) PKCS#12 export
0N/A *
0N/A * Netscape Communicator 40 bit ciphers only All.
0N/A * (export version)
0N/A * PKCS#12 import.
0N/A *
0N/A * Netscape Communicator All. All.
0N/A * (domestic or fortified
0N/A * version) PKCS#12 import.
0N/A *
0N/A * OpenSSL PKCS#12 code. All. All.
0N/A * ---------------------------------------------------------------------
0N/A *
0N/A * NOTE: Currently PKCS12 KeyStore does not support TrustedCertEntries.
0N/A * PKCS#12 is mainly used to deliver private keys with their associated
0N/A * certificate chain and aliases. In a PKCS12 keystore, entries are
0N/A * identified by the alias, and a localKeyId is required to match the
0N/A * private key with the certificate.
0N/A *
0N/A * @author Seema Malkani
0N/A * @author Jeff Nisewanger
0N/A * @author Jan Luehe
0N/A *
0N/A * @see KeyProtector
0N/A * @see java.security.KeyStoreSpi
0N/A * @see KeyTool
0N/A *
0N/A *
0N/A */
0N/Apublic final class PKCS12KeyStore extends KeyStoreSpi {
0N/A
0N/A public static final int VERSION_3 = 3;
0N/A
0N/A private static final int keyBag[] = {1, 2, 840, 113549, 1, 12, 10, 1, 2};
0N/A private static final int certBag[] = {1, 2, 840, 113549, 1, 12, 10, 1, 3};
0N/A
0N/A private static final int pkcs9Name[] = {1, 2, 840, 113549, 1, 9, 20};
0N/A private static final int pkcs9KeyId[] = {1, 2, 840, 113549, 1, 9, 21};
0N/A
0N/A private static final int pkcs9certType[] = {1, 2, 840, 113549, 1, 9, 22, 1};
0N/A
0N/A private static final int pbeWithSHAAnd40BitRC2CBC[] =
0N/A {1, 2, 840, 113549, 1, 12, 1, 6};
0N/A private static final int pbeWithSHAAnd3KeyTripleDESCBC[] =
0N/A {1, 2, 840, 113549, 1, 12, 1, 3};
0N/A
0N/A private static ObjectIdentifier PKCS8ShroudedKeyBag_OID;
0N/A private static ObjectIdentifier CertBag_OID;
0N/A private static ObjectIdentifier PKCS9FriendlyName_OID;
0N/A private static ObjectIdentifier PKCS9LocalKeyId_OID;
0N/A private static ObjectIdentifier PKCS9CertType_OID;
0N/A private static ObjectIdentifier pbeWithSHAAnd40BitRC2CBC_OID;
0N/A private static ObjectIdentifier pbeWithSHAAnd3KeyTripleDESCBC_OID;
0N/A
0N/A private int counter = 0;
0N/A private static final int iterationCount = 1024;
0N/A private static final int SALT_LEN = 20;
0N/A
0N/A // private key count
0N/A // Note: This is a workaround to allow null localKeyID attribute
0N/A // in pkcs12 with one private key entry and associated cert-chain
0N/A private int privateKeyCount = 0;
0N/A
0N/A // the source of randomness
0N/A private SecureRandom random;
0N/A
0N/A static {
0N/A try {
0N/A PKCS8ShroudedKeyBag_OID = new ObjectIdentifier(keyBag);
0N/A CertBag_OID = new ObjectIdentifier(certBag);
0N/A PKCS9FriendlyName_OID = new ObjectIdentifier(pkcs9Name);
0N/A PKCS9LocalKeyId_OID = new ObjectIdentifier(pkcs9KeyId);
0N/A PKCS9CertType_OID = new ObjectIdentifier(pkcs9certType);
0N/A pbeWithSHAAnd40BitRC2CBC_OID =
0N/A new ObjectIdentifier(pbeWithSHAAnd40BitRC2CBC);
0N/A pbeWithSHAAnd3KeyTripleDESCBC_OID =
0N/A new ObjectIdentifier(pbeWithSHAAnd3KeyTripleDESCBC);
0N/A } catch (IOException ioe) {
0N/A // should not happen
0N/A }
0N/A }
0N/A
0N/A // Private keys and their supporting certificate chains
0N/A private static class KeyEntry {
0N/A Date date; // the creation date of this entry
0N/A byte[] protectedPrivKey;
0N/A Certificate chain[];
0N/A byte[] keyId;
0N/A String alias;
0N/A };
0N/A
2549N/A // A certificate with its PKCS #9 attributes
2549N/A private static class CertEntry {
2549N/A final X509Certificate cert;
2549N/A final byte[] keyId;
2549N/A final String alias;
2549N/A CertEntry(X509Certificate cert, byte[] keyId, String alias) {
2549N/A this.cert = cert;
0N/A this.keyId = keyId;
2549N/A this.alias = alias;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Private keys and certificates are stored in a hashtable.
0N/A * Hash entries are keyed by alias names.
0N/A */
0N/A private Hashtable<String, KeyEntry> entries =
0N/A new Hashtable<String, KeyEntry>();
0N/A
0N/A private ArrayList<KeyEntry> keyList = new ArrayList<KeyEntry>();
2549N/A private LinkedHashMap<X500Principal, X509Certificate> certsMap =
2549N/A new LinkedHashMap<X500Principal, X509Certificate>();
2549N/A private ArrayList<CertEntry> certEntries = new ArrayList<CertEntry>();
0N/A
0N/A /**
0N/A * Returns the key associated with the given alias, using the given
0N/A * password to recover it.
0N/A *
0N/A * @param alias the alias name
0N/A * @param password the password for recovering the key
0N/A *
0N/A * @return the requested key, or null if the given alias does not exist
0N/A * or does not identify a <i>key entry</i>.
0N/A *
0N/A * @exception NoSuchAlgorithmException if the algorithm for recovering the
0N/A * key cannot be found
0N/A * @exception UnrecoverableKeyException if the key cannot be recovered
0N/A * (e.g., the given password is wrong).
0N/A */
0N/A public Key engineGetKey(String alias, char[] password)
0N/A throws NoSuchAlgorithmException, UnrecoverableKeyException
0N/A {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A Key key = null;
0N/A
0N/A if (entry == null) {
0N/A return null;
0N/A }
0N/A
0N/A // get the encoded private key
0N/A byte[] encrBytes = entry.protectedPrivKey;
0N/A
0N/A byte[] encryptedKey;
0N/A AlgorithmParameters algParams;
0N/A ObjectIdentifier algOid;
0N/A try {
0N/A // get the encrypted private key
0N/A EncryptedPrivateKeyInfo encrInfo =
0N/A new EncryptedPrivateKeyInfo(encrBytes);
0N/A encryptedKey = encrInfo.getEncryptedData();
0N/A
0N/A // parse Algorithm parameters
0N/A DerValue val = new DerValue(encrInfo.getAlgorithm().encode());
0N/A DerInputStream in = val.toDerInputStream();
0N/A algOid = in.getOID();
0N/A algParams = parseAlgParameters(in);
0N/A
0N/A } catch (IOException ioe) {
0N/A UnrecoverableKeyException uke =
0N/A new UnrecoverableKeyException("Private key not stored as "
0N/A + "PKCS#8 EncryptedPrivateKeyInfo: " + ioe);
0N/A uke.initCause(ioe);
0N/A throw uke;
0N/A }
0N/A
0N/A try {
4784N/A byte[] privateKeyInfo;
4784N/A while (true) {
4784N/A try {
4784N/A // Use JCE
4784N/A SecretKey skey = getPBEKey(password);
4784N/A Cipher cipher = Cipher.getInstance(algOid.toString());
4784N/A cipher.init(Cipher.DECRYPT_MODE, skey, algParams);
4784N/A privateKeyInfo = cipher.doFinal(encryptedKey);
4784N/A break;
4784N/A } catch (Exception e) {
4784N/A if (password.length == 0) {
4784N/A // Retry using an empty password
4784N/A // without a NULL terminator.
4784N/A password = new char[1];
4784N/A continue;
4784N/A }
4784N/A throw e;
4784N/A }
4784N/A }
0N/A
0N/A PKCS8EncodedKeySpec kspec = new PKCS8EncodedKeySpec(privateKeyInfo);
0N/A
0N/A /*
0N/A * Parse the key algorithm and then use a JCA key factory
0N/A * to create the private key.
0N/A */
0N/A DerValue val = new DerValue(privateKeyInfo);
0N/A DerInputStream in = val.toDerInputStream();
0N/A int i = in.getInteger();
0N/A DerValue[] value = in.getSequence(2);
0N/A AlgorithmId algId = new AlgorithmId(value[0].getOID());
0N/A String algName = algId.getName();
0N/A
0N/A KeyFactory kfac = KeyFactory.getInstance(algName);
0N/A key = kfac.generatePrivate(kspec);
0N/A } catch (Exception e) {
0N/A UnrecoverableKeyException uke =
0N/A new UnrecoverableKeyException("Get Key failed: " +
0N/A e.getMessage());
0N/A uke.initCause(e);
0N/A throw uke;
0N/A }
0N/A return key;
0N/A }
0N/A
0N/A /**
0N/A * Returns the certificate chain associated with the given alias.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the certificate chain (ordered with the user's certificate first
0N/A * and the root certificate authority last), or null if the given alias
0N/A * does not exist or does not contain a certificate chain (i.e., the given
0N/A * alias identifies either a <i>trusted certificate entry</i> or a
0N/A * <i>key entry</i> without a certificate chain).
0N/A */
0N/A public Certificate[] engineGetCertificateChain(String alias) {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A if (entry != null) {
0N/A if (entry.chain == null) {
0N/A return null;
0N/A } else {
0N/A return entry.chain.clone();
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the certificate associated with the given alias.
0N/A *
0N/A * <p>If the given alias name identifies a
0N/A * <i>trusted certificate entry</i>, the certificate associated with that
0N/A * entry is returned. If the given alias name identifies a
0N/A * <i>key entry</i>, the first element of the certificate chain of that
0N/A * entry is returned, or null if that entry does not have a certificate
0N/A * chain.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the certificate, or null if the given alias does not exist or
0N/A * does not contain a certificate.
0N/A */
0N/A public Certificate engineGetCertificate(String alias) {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A if (entry != null) {
0N/A if (entry.chain == null) {
0N/A return null;
0N/A } else {
0N/A return entry.chain[0];
0N/A }
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns the creation date of the entry identified by the given alias.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return the creation date of this entry, or null if the given alias does
0N/A * not exist
0N/A */
0N/A public Date engineGetCreationDate(String alias) {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A if (entry != null) {
0N/A return new Date(entry.date.getTime());
0N/A } else {
0N/A return null;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Assigns the given key to the given alias, protecting it with the given
0N/A * password.
0N/A *
0N/A * <p>If the given key is of type <code>java.security.PrivateKey</code>,
0N/A * it must be accompanied by a certificate chain certifying the
0N/A * corresponding public key.
0N/A *
0N/A * <p>If the given alias already exists, the keystore information
0N/A * associated with it is overridden by the given key (and possibly
0N/A * certificate chain).
0N/A *
0N/A * @param alias the alias name
0N/A * @param key the key to be associated with the alias
0N/A * @param password the password to protect the key
0N/A * @param chain the certificate chain for the corresponding public
0N/A * key (only required if the given key is of type
0N/A * <code>java.security.PrivateKey</code>).
0N/A *
0N/A * @exception KeyStoreException if the given key cannot be protected, or
0N/A * this operation fails for some other reason
0N/A */
0N/A public synchronized void engineSetKeyEntry(String alias, Key key,
0N/A char[] password, Certificate[] chain)
0N/A throws KeyStoreException
0N/A {
0N/A try {
0N/A KeyEntry entry = new KeyEntry();
0N/A entry.date = new Date();
0N/A
0N/A if (key instanceof PrivateKey) {
0N/A if ((key.getFormat().equals("PKCS#8")) ||
0N/A (key.getFormat().equals("PKCS8"))) {
0N/A // Encrypt the private key
0N/A entry.protectedPrivKey =
0N/A encryptPrivateKey(key.getEncoded(), password);
0N/A } else {
0N/A throw new KeyStoreException("Private key is not encoded" +
0N/A "as PKCS#8");
0N/A }
0N/A } else {
0N/A throw new KeyStoreException("Key is not a PrivateKey");
0N/A }
0N/A
0N/A // clone the chain
0N/A if (chain != null) {
0N/A // validate cert-chain
0N/A if ((chain.length > 1) && (!validateChain(chain)))
0N/A throw new KeyStoreException("Certificate chain is " +
0N/A "not validate");
0N/A entry.chain = chain.clone();
0N/A }
0N/A
0N/A // set the keyId to current date
0N/A entry.keyId = ("Time " + (entry.date).getTime()).getBytes("UTF8");
0N/A // set the alias
0N/A entry.alias = alias.toLowerCase();
0N/A
0N/A // add the entry
0N/A entries.put(alias.toLowerCase(), entry);
0N/A } catch (Exception nsae) {
0N/A KeyStoreException ke = new KeyStoreException("Key protection " +
0N/A " algorithm not found: " + nsae);
0N/A ke.initCause(nsae);
0N/A throw ke;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Assigns the given key (that has already been protected) to the given
0N/A * alias.
0N/A *
0N/A * <p>If the protected key is of type
0N/A * <code>java.security.PrivateKey</code>, it must be accompanied by a
0N/A * certificate chain certifying the corresponding public key. If the
0N/A * underlying keystore implementation is of type <code>jks</code>,
0N/A * <code>key</code> must be encoded as an
0N/A * <code>EncryptedPrivateKeyInfo</code> as defined in the PKCS #8 standard.
0N/A *
0N/A * <p>If the given alias already exists, the keystore information
0N/A * associated with it is overridden by the given key (and possibly
0N/A * certificate chain).
0N/A *
0N/A * @param alias the alias name
0N/A * @param key the key (in protected format) to be associated with the alias
0N/A * @param chain the certificate chain for the corresponding public
0N/A * key (only useful if the protected key is of type
0N/A * <code>java.security.PrivateKey</code>).
0N/A *
0N/A * @exception KeyStoreException if this operation fails.
0N/A */
0N/A public synchronized void engineSetKeyEntry(String alias, byte[] key,
0N/A Certificate[] chain)
0N/A throws KeyStoreException
0N/A {
0N/A // key must be encoded as EncryptedPrivateKeyInfo
0N/A // as defined in PKCS#8
0N/A try {
0N/A new EncryptedPrivateKeyInfo(key);
0N/A } catch (IOException ioe) {
0N/A KeyStoreException ke = new KeyStoreException("Private key is not"
0N/A + " stored as PKCS#8 EncryptedPrivateKeyInfo: " + ioe);
0N/A ke.initCause(ioe);
0N/A throw ke;
0N/A }
0N/A
0N/A KeyEntry entry = new KeyEntry();
0N/A entry.date = new Date();
0N/A
2549N/A try {
2549N/A // set the keyId to current date
2549N/A entry.keyId = ("Time " + (entry.date).getTime()).getBytes("UTF8");
2549N/A } catch (UnsupportedEncodingException ex) {
2549N/A // Won't happen
2549N/A }
2549N/A // set the alias
2549N/A entry.alias = alias.toLowerCase();
2549N/A
0N/A entry.protectedPrivKey = key.clone();
0N/A if (chain != null) {
0N/A entry.chain = chain.clone();
0N/A }
0N/A
0N/A // add the entry
0N/A entries.put(alias.toLowerCase(), entry);
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Generate random salt
0N/A */
0N/A private byte[] getSalt()
0N/A {
0N/A // Generate a random salt.
0N/A byte[] salt = new byte[SALT_LEN];
0N/A if (random == null) {
0N/A random = new SecureRandom();
0N/A }
0N/A random.nextBytes(salt);
0N/A return salt;
0N/A }
0N/A
0N/A /*
0N/A * Generate PBE Algorithm Parameters
0N/A */
0N/A private AlgorithmParameters getAlgorithmParameters(String algorithm)
0N/A throws IOException
0N/A {
0N/A AlgorithmParameters algParams = null;
0N/A
0N/A // create PBE parameters from salt and iteration count
0N/A PBEParameterSpec paramSpec =
0N/A new PBEParameterSpec(getSalt(), iterationCount);
0N/A try {
0N/A algParams = AlgorithmParameters.getInstance(algorithm);
0N/A algParams.init(paramSpec);
0N/A } catch (Exception e) {
0N/A IOException ioe =
0N/A new IOException("getAlgorithmParameters failed: " +
0N/A e.getMessage());
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A return algParams;
0N/A }
0N/A
0N/A /*
0N/A * parse Algorithm Parameters
0N/A */
0N/A private AlgorithmParameters parseAlgParameters(DerInputStream in)
0N/A throws IOException
0N/A {
0N/A AlgorithmParameters algParams = null;
0N/A try {
0N/A DerValue params;
0N/A if (in.available() == 0) {
0N/A params = null;
0N/A } else {
0N/A params = in.getDerValue();
0N/A if (params.tag == DerValue.tag_Null) {
0N/A params = null;
0N/A }
0N/A }
0N/A if (params != null) {
0N/A algParams = AlgorithmParameters.getInstance("PBE");
0N/A algParams.init(params.toByteArray());
0N/A }
0N/A } catch (Exception e) {
0N/A IOException ioe =
0N/A new IOException("parseAlgParameters failed: " +
0N/A e.getMessage());
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A return algParams;
0N/A }
0N/A
0N/A /*
0N/A * Generate PBE key
0N/A */
0N/A private SecretKey getPBEKey(char[] password) throws IOException
0N/A {
0N/A SecretKey skey = null;
0N/A
0N/A try {
0N/A PBEKeySpec keySpec = new PBEKeySpec(password);
0N/A SecretKeyFactory skFac = SecretKeyFactory.getInstance("PBE");
0N/A skey = skFac.generateSecret(keySpec);
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException("getSecretKey failed: " +
0N/A e.getMessage());
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A return skey;
0N/A }
0N/A
0N/A /*
0N/A * Encrypt private key using Password-based encryption (PBE)
0N/A * as defined in PKCS#5.
0N/A *
0N/A * NOTE: Currently pbeWithSHAAnd3-KeyTripleDES-CBC algorithmID is
0N/A * used to derive the key and IV.
0N/A *
0N/A * @return encrypted private key encoded as EncryptedPrivateKeyInfo
0N/A */
0N/A private byte[] encryptPrivateKey(byte[] data, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, UnrecoverableKeyException
0N/A {
0N/A byte[] key = null;
0N/A
0N/A try {
0N/A // create AlgorithmParameters
0N/A AlgorithmParameters algParams =
0N/A getAlgorithmParameters("PBEWithSHA1AndDESede");
0N/A
0N/A // Use JCE
0N/A SecretKey skey = getPBEKey(password);
0N/A Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
0N/A cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
0N/A byte[] encryptedKey = cipher.doFinal(data);
0N/A
0N/A // wrap encrypted private key in EncryptedPrivateKeyInfo
0N/A // as defined in PKCS#8
0N/A AlgorithmId algid =
0N/A new AlgorithmId(pbeWithSHAAnd3KeyTripleDESCBC_OID, algParams);
0N/A EncryptedPrivateKeyInfo encrInfo =
0N/A new EncryptedPrivateKeyInfo(algid, encryptedKey);
0N/A key = encrInfo.getEncoded();
0N/A } catch (Exception e) {
0N/A UnrecoverableKeyException uke =
0N/A new UnrecoverableKeyException("Encrypt Private Key failed: "
0N/A + e.getMessage());
0N/A uke.initCause(e);
0N/A throw uke;
0N/A }
0N/A
0N/A return key;
0N/A }
0N/A
0N/A /**
0N/A * Assigns the given certificate to the given alias.
0N/A *
0N/A * <p>If the given alias already exists in this keystore and identifies a
0N/A * <i>trusted certificate entry</i>, the certificate associated with it is
0N/A * overridden by the given certificate.
0N/A *
0N/A * @param alias the alias name
0N/A * @param cert the certificate
0N/A *
0N/A * @exception KeyStoreException if the given alias already exists and does
0N/A * identify a <i>key entry</i>, or on an attempt to create a
0N/A * <i>trusted cert entry</i> which is currently not supported.
0N/A */
0N/A public synchronized void engineSetCertificateEntry(String alias,
0N/A Certificate cert) throws KeyStoreException
0N/A {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A if (entry != null) {
0N/A throw new KeyStoreException("Cannot overwrite own certificate");
0N/A } else
0N/A throw new KeyStoreException("TrustedCertEntry not supported");
0N/A }
0N/A
0N/A /**
0N/A * Deletes the entry identified by the given alias from this keystore.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @exception KeyStoreException if the entry cannot be removed.
0N/A */
0N/A public synchronized void engineDeleteEntry(String alias)
0N/A throws KeyStoreException
0N/A {
0N/A entries.remove(alias.toLowerCase());
0N/A }
0N/A
0N/A /**
0N/A * Lists all the alias names of this keystore.
0N/A *
0N/A * @return enumeration of the alias names
0N/A */
0N/A public Enumeration<String> engineAliases() {
0N/A return entries.keys();
0N/A }
0N/A
0N/A /**
0N/A * Checks if the given alias exists in this keystore.
0N/A *
0N/A * @param alias the alias name
0N/A *
0N/A * @return true if the alias exists, false otherwise
0N/A */
0N/A public boolean engineContainsAlias(String alias) {
0N/A return entries.containsKey(alias.toLowerCase());
0N/A }
0N/A
0N/A /**
0N/A * Retrieves the number of entries in this keystore.
0N/A *
0N/A * @return the number of entries in this keystore
0N/A */
0N/A public int engineSize() {
0N/A return entries.size();
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the entry identified by the given alias is a
0N/A * <i>key entry</i>, and false otherwise.
0N/A *
0N/A * @return true if the entry identified by the given alias is a
0N/A * <i>key entry</i>, false otherwise.
0N/A */
0N/A public boolean engineIsKeyEntry(String alias) {
0N/A KeyEntry entry = entries.get(alias.toLowerCase());
0N/A if (entry != null) {
0N/A return true;
0N/A } else {
0N/A return false;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Returns true if the entry identified by the given alias is a
0N/A * <i>trusted certificate entry</i>, and false otherwise.
0N/A *
0N/A * @return true if the entry identified by the given alias is a
0N/A * <i>trusted certificate entry</i>, false otherwise.
0N/A */
0N/A public boolean engineIsCertificateEntry(String alias) {
0N/A // TrustedCertEntry is not supported
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Returns the (alias) name of the first keystore entry whose certificate
0N/A * matches the given certificate.
0N/A *
0N/A * <p>This method attempts to match the given certificate with each
0N/A * keystore entry. If the entry being considered
0N/A * is a <i>trusted certificate entry</i>, the given certificate is
0N/A * compared to that entry's certificate. If the entry being considered is
0N/A * a <i>key entry</i>, the given certificate is compared to the first
0N/A * element of that entry's certificate chain (if a chain exists).
0N/A *
0N/A * @param cert the certificate to match with.
0N/A *
0N/A * @return the (alias) name of the first entry with matching certificate,
0N/A * or null if no such entry exists in this keystore.
0N/A */
0N/A public String engineGetCertificateAlias(Certificate cert) {
0N/A Certificate certElem = null;
0N/A
0N/A for (Enumeration<String> e = entries.keys(); e.hasMoreElements(); ) {
0N/A String alias = e.nextElement();
0N/A KeyEntry entry = entries.get(alias);
0N/A if (entry.chain != null) {
0N/A certElem = entry.chain[0];
0N/A }
0N/A if (certElem.equals(cert)) {
0N/A return alias;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Stores this keystore to the given output stream, and protects its
0N/A * integrity with the given password.
0N/A *
0N/A * @param stream the output stream to which this keystore is written.
0N/A * @param password the password to generate the keystore integrity check
0N/A *
0N/A * @exception IOException if there was an I/O problem with data
0N/A * @exception NoSuchAlgorithmException if the appropriate data integrity
0N/A * algorithm could not be found
0N/A * @exception CertificateException if any of the certificates included in
0N/A * the keystore data could not be stored
0N/A */
0N/A public synchronized void engineStore(OutputStream stream, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, CertificateException
0N/A {
0N/A // password is mandatory when storing
0N/A if (password == null) {
0N/A throw new IllegalArgumentException("password can't be null");
0N/A }
0N/A
0N/A // -- Create PFX
0N/A DerOutputStream pfx = new DerOutputStream();
0N/A
0N/A // PFX version (always write the latest version)
0N/A DerOutputStream version = new DerOutputStream();
0N/A version.putInteger(VERSION_3);
0N/A byte[] pfxVersion = version.toByteArray();
0N/A pfx.write(pfxVersion);
0N/A
0N/A // -- Create AuthSafe
0N/A DerOutputStream authSafe = new DerOutputStream();
0N/A
0N/A // -- Create ContentInfos
0N/A DerOutputStream authSafeContentInfo = new DerOutputStream();
0N/A
0N/A // -- create safeContent Data ContentInfo
0N/A byte[] safeContentData = createSafeContent();
0N/A ContentInfo dataContentInfo = new ContentInfo(safeContentData);
0N/A dataContentInfo.encode(authSafeContentInfo);
0N/A
0N/A // -- create EncryptedContentInfo
0N/A byte[] encrData = createEncryptedData(password);
0N/A ContentInfo encrContentInfo =
0N/A new ContentInfo(ContentInfo.ENCRYPTED_DATA_OID,
0N/A new DerValue(encrData));
0N/A encrContentInfo.encode(authSafeContentInfo);
0N/A
0N/A // wrap as SequenceOf ContentInfos
0N/A DerOutputStream cInfo = new DerOutputStream();
0N/A cInfo.write(DerValue.tag_SequenceOf, authSafeContentInfo);
0N/A byte[] authenticatedSafe = cInfo.toByteArray();
0N/A
0N/A // Create Encapsulated ContentInfo
0N/A ContentInfo contentInfo = new ContentInfo(authenticatedSafe);
0N/A contentInfo.encode(authSafe);
0N/A byte[] authSafeData = authSafe.toByteArray();
0N/A pfx.write(authSafeData);
0N/A
0N/A // -- MAC
0N/A byte[] macData = calculateMac(password, authenticatedSafe);
0N/A pfx.write(macData);
0N/A
0N/A // write PFX to output stream
0N/A DerOutputStream pfxout = new DerOutputStream();
0N/A pfxout.write(DerValue.tag_Sequence, pfx);
0N/A byte[] pfxData = pfxout.toByteArray();
0N/A stream.write(pfxData);
0N/A stream.flush();
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Generate Hash.
0N/A */
0N/A private byte[] generateHash(byte[] data) throws IOException
0N/A {
0N/A byte[] digest = null;
0N/A
0N/A try {
0N/A MessageDigest md = MessageDigest.getInstance("SHA1");
0N/A md.update(data);
0N/A digest = md.digest();
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException("generateHash failed: " + e);
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A return digest;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Calculate MAC using HMAC algorithm (required for password integrity)
0N/A *
0N/A * Hash-based MAC algorithm combines secret key with message digest to
0N/A * create a message authentication code (MAC)
0N/A */
0N/A private byte[] calculateMac(char[] passwd, byte[] data)
0N/A throws IOException
0N/A {
0N/A byte[] mData = null;
0N/A String algName = "SHA1";
0N/A
0N/A try {
0N/A // Generate a random salt.
0N/A byte[] salt = getSalt();
0N/A
0N/A // generate MAC (MAC key is generated within JCE)
0N/A Mac m = Mac.getInstance("HmacPBESHA1");
0N/A PBEParameterSpec params =
0N/A new PBEParameterSpec(salt, iterationCount);
0N/A SecretKey key = getPBEKey(passwd);
0N/A m.init(key, params);
0N/A m.update(data);
0N/A byte[] macResult = m.doFinal();
0N/A
0N/A // encode as MacData
0N/A MacData macData = new MacData(algName, macResult, salt,
0N/A iterationCount);
0N/A DerOutputStream bytes = new DerOutputStream();
0N/A bytes.write(macData.getEncoded());
0N/A mData = bytes.toByteArray();
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException("calculateMac failed: " + e);
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A return mData;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Validate Certificate Chain
0N/A */
0N/A private boolean validateChain(Certificate[] certChain)
0N/A {
0N/A for (int i = 0; i < certChain.length-1; i++) {
0N/A X500Principal issuerDN =
0N/A ((X509Certificate)certChain[i]).getIssuerX500Principal();
0N/A X500Principal subjectDN =
0N/A ((X509Certificate)certChain[i+1]).getSubjectX500Principal();
0N/A if (!(issuerDN.equals(subjectDN)))
0N/A return false;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Create PKCS#12 Attributes, friendlyName and localKeyId.
0N/A *
0N/A * Although attributes are optional, they could be required.
0N/A * For e.g. localKeyId attribute is required to match the
0N/A * private key with the associated end-entity certificate.
0N/A *
0N/A * PKCS8ShroudedKeyBags include unique localKeyID and friendlyName.
0N/A * CertBags may or may not include attributes depending on the type
0N/A * of Certificate. In end-entity certificates, localKeyID should be
0N/A * unique, and the corresponding private key should have the same
0N/A * localKeyID. For trusted CA certs in the cert-chain, localKeyID
0N/A * attribute is not required, hence most vendors don't include it.
0N/A * NSS/Netscape require it to be unique or null, where as IE/OpenSSL
0N/A * ignore it.
0N/A *
0N/A * Here is a list of pkcs12 attribute values in CertBags.
0N/A *
0N/A * PKCS12 Attribute NSS/Netscape IE OpenSSL J2SE
0N/A * --------------------------------------------------------------
0N/A * LocalKeyId
0N/A * (In EE cert only,
0N/A * NULL in CA certs) true true true true
0N/A *
0N/A * friendlyName unique same/ same/ unique
0N/A * unique unique/
0N/A * null
0N/A *
0N/A * Note: OpenSSL adds friendlyName for end-entity cert only, and
0N/A * removes the localKeyID and friendlyName for CA certs.
0N/A * If the CertBag did not have a friendlyName, most vendors will
0N/A * add it, and assign it to the DN of the cert.
0N/A */
0N/A private byte[] getBagAttributes(String alias, byte[] keyId)
0N/A throws IOException {
0N/A
0N/A byte[] localKeyID = null;
0N/A byte[] friendlyName = null;
0N/A
0N/A // return null if both attributes are null
0N/A if ((alias == null) && (keyId == null)) {
0N/A return null;
0N/A }
0N/A
0N/A // SafeBag Attributes
0N/A DerOutputStream bagAttrs = new DerOutputStream();
0N/A
0N/A // Encode the friendlyname oid.
0N/A if (alias != null) {
0N/A DerOutputStream bagAttr1 = new DerOutputStream();
0N/A bagAttr1.putOID(PKCS9FriendlyName_OID);
0N/A DerOutputStream bagAttrContent1 = new DerOutputStream();
0N/A DerOutputStream bagAttrValue1 = new DerOutputStream();
0N/A bagAttrContent1.putBMPString(alias);
0N/A bagAttr1.write(DerValue.tag_Set, bagAttrContent1);
0N/A bagAttrValue1.write(DerValue.tag_Sequence, bagAttr1);
0N/A friendlyName = bagAttrValue1.toByteArray();
0N/A }
0N/A
0N/A // Encode the localkeyId oid.
0N/A if (keyId != null) {
0N/A DerOutputStream bagAttr2 = new DerOutputStream();
0N/A bagAttr2.putOID(PKCS9LocalKeyId_OID);
0N/A DerOutputStream bagAttrContent2 = new DerOutputStream();
0N/A DerOutputStream bagAttrValue2 = new DerOutputStream();
0N/A bagAttrContent2.putOctetString(keyId);
0N/A bagAttr2.write(DerValue.tag_Set, bagAttrContent2);
0N/A bagAttrValue2.write(DerValue.tag_Sequence, bagAttr2);
0N/A localKeyID = bagAttrValue2.toByteArray();
0N/A }
0N/A
0N/A DerOutputStream attrs = new DerOutputStream();
0N/A if (friendlyName != null) {
0N/A attrs.write(friendlyName);
0N/A }
0N/A if (localKeyID != null) {
0N/A attrs.write(localKeyID);
0N/A }
0N/A bagAttrs.write(DerValue.tag_Set, attrs);
0N/A return bagAttrs.toByteArray();
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Create EncryptedData content type, that contains EncryptedContentInfo.
0N/A * Includes certificates in individual SafeBags of type CertBag.
0N/A * Each CertBag may include pkcs12 attributes
0N/A * (see comments in getBagAttributes)
0N/A */
0N/A private byte[] createEncryptedData(char[] password)
0N/A throws CertificateException, IOException
0N/A {
0N/A DerOutputStream out = new DerOutputStream();
0N/A for (Enumeration<String> e = entries.keys(); e.hasMoreElements(); ) {
0N/A
0N/A String alias = e.nextElement();
0N/A KeyEntry entry = entries.get(alias);
0N/A
0N/A // certificate chain
0N/A int chainLen;
0N/A if (entry.chain == null) {
0N/A chainLen = 0;
0N/A } else {
0N/A chainLen = entry.chain.length;
0N/A }
0N/A
0N/A for (int i = 0; i < chainLen; i++) {
0N/A // create SafeBag of Type CertBag
0N/A DerOutputStream safeBag = new DerOutputStream();
0N/A safeBag.putOID(CertBag_OID);
0N/A
0N/A // create a CertBag
0N/A DerOutputStream certBag = new DerOutputStream();
0N/A certBag.putOID(PKCS9CertType_OID);
0N/A
0N/A // write encoded certs in a context-specific tag
0N/A DerOutputStream certValue = new DerOutputStream();
0N/A X509Certificate cert = (X509Certificate)entry.chain[i];
0N/A certValue.putOctetString(cert.getEncoded());
0N/A certBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte) 0), certValue);
0N/A
0N/A // wrap CertBag in a Sequence
0N/A DerOutputStream certout = new DerOutputStream();
0N/A certout.write(DerValue.tag_Sequence, certBag);
0N/A byte[] certBagValue = certout.toByteArray();
0N/A
0N/A // Wrap the CertBag encoding in a context-specific tag.
0N/A DerOutputStream bagValue = new DerOutputStream();
0N/A bagValue.write(certBagValue);
0N/A // write SafeBag Value
0N/A safeBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte) 0), bagValue);
0N/A
0N/A // write SafeBag Attributes
0N/A // All Certs should have a unique friendlyName.
0N/A // This change is made to meet NSS requirements.
0N/A byte[] bagAttrs = null;
0N/A if (i == 0) {
0N/A // Only End-Entity Cert should have a localKeyId.
2549N/A bagAttrs = getBagAttributes(entry.alias, entry.keyId);
0N/A } else {
0N/A // Trusted root CA certs and Intermediate CA certs do not
0N/A // need to have a localKeyId, and hence localKeyId is null
0N/A // This change is made to meet NSS/Netscape requirements.
0N/A // NSS pkcs12 library requires trusted CA certs in the
0N/A // certificate chain to have unique or null localKeyID.
0N/A // However, IE/OpenSSL do not impose this restriction.
2549N/A bagAttrs = getBagAttributes(
2549N/A cert.getSubjectX500Principal().getName(), null);
0N/A }
0N/A if (bagAttrs != null) {
0N/A safeBag.write(bagAttrs);
0N/A }
0N/A
0N/A // wrap as Sequence
0N/A out.write(DerValue.tag_Sequence, safeBag);
0N/A } // for cert-chain
0N/A }
0N/A
0N/A // wrap as SequenceOf SafeBag
0N/A DerOutputStream safeBagValue = new DerOutputStream();
0N/A safeBagValue.write(DerValue.tag_SequenceOf, out);
0N/A byte[] safeBagData = safeBagValue.toByteArray();
0N/A
0N/A // encrypt the content (EncryptedContentInfo)
0N/A byte[] encrContentInfo = encryptContent(safeBagData, password);
0N/A
0N/A // -- SEQUENCE of EncryptedData
0N/A DerOutputStream encrData = new DerOutputStream();
0N/A DerOutputStream encrDataContent = new DerOutputStream();
0N/A encrData.putInteger(0);
0N/A encrData.write(encrContentInfo);
0N/A encrDataContent.write(DerValue.tag_Sequence, encrData);
0N/A return encrDataContent.toByteArray();
0N/A }
0N/A
0N/A /*
0N/A * Create SafeContent Data content type.
0N/A * Includes encrypted private key in a SafeBag of type PKCS8ShroudedKeyBag.
0N/A * Each PKCS8ShroudedKeyBag includes pkcs12 attributes
0N/A * (see comments in getBagAttributes)
0N/A */
0N/A private byte[] createSafeContent()
0N/A throws CertificateException, IOException {
0N/A
0N/A DerOutputStream out = new DerOutputStream();
0N/A for (Enumeration<String> e = entries.keys(); e.hasMoreElements(); ) {
0N/A
0N/A String alias = e.nextElement();
0N/A KeyEntry entry = entries.get(alias);
0N/A
0N/A // Create SafeBag of type pkcs8ShroudedKeyBag
0N/A DerOutputStream safeBag = new DerOutputStream();
0N/A safeBag.putOID(PKCS8ShroudedKeyBag_OID);
0N/A
0N/A // get the encrypted private key
0N/A byte[] encrBytes = entry.protectedPrivKey;
0N/A EncryptedPrivateKeyInfo encrInfo = null;
0N/A try {
0N/A encrInfo = new EncryptedPrivateKeyInfo(encrBytes);
0N/A } catch (IOException ioe) {
0N/A throw new IOException("Private key not stored as "
0N/A + "PKCS#8 EncryptedPrivateKeyInfo" + ioe.getMessage());
0N/A }
0N/A
0N/A // Wrap the EncryptedPrivateKeyInfo in a context-specific tag.
0N/A DerOutputStream bagValue = new DerOutputStream();
0N/A bagValue.write(encrInfo.getEncoded());
0N/A safeBag.write(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A true, (byte) 0), bagValue);
0N/A
0N/A // write SafeBag Attributes
0N/A byte[] bagAttrs = getBagAttributes(alias, entry.keyId);
0N/A safeBag.write(bagAttrs);
0N/A
0N/A // wrap as Sequence
0N/A out.write(DerValue.tag_Sequence, safeBag);
0N/A }
0N/A
0N/A // wrap as Sequence
0N/A DerOutputStream safeBagValue = new DerOutputStream();
0N/A safeBagValue.write(DerValue.tag_Sequence, out);
0N/A return safeBagValue.toByteArray();
0N/A }
0N/A
0N/A
0N/A /*
0N/A * Encrypt the contents using Password-based (PBE) encryption
0N/A * as defined in PKCS #5.
0N/A *
0N/A * NOTE: Currently pbeWithSHAAnd40BiteRC2-CBC algorithmID is used
0N/A * to derive the key and IV.
0N/A *
0N/A * @return encrypted contents encoded as EncryptedContentInfo
0N/A */
0N/A private byte[] encryptContent(byte[] data, char[] password)
0N/A throws IOException {
0N/A
0N/A byte[] encryptedData = null;
0N/A
0N/A // create AlgorithmParameters
0N/A AlgorithmParameters algParams =
0N/A getAlgorithmParameters("PBEWithSHA1AndRC2_40");
0N/A DerOutputStream bytes = new DerOutputStream();
0N/A AlgorithmId algId =
0N/A new AlgorithmId(pbeWithSHAAnd40BitRC2CBC_OID, algParams);
0N/A algId.encode(bytes);
0N/A byte[] encodedAlgId = bytes.toByteArray();
0N/A
0N/A try {
0N/A // Use JCE
0N/A SecretKey skey = getPBEKey(password);
0N/A Cipher cipher = Cipher.getInstance("PBEWithSHA1AndRC2_40");
0N/A cipher.init(Cipher.ENCRYPT_MODE, skey, algParams);
0N/A encryptedData = cipher.doFinal(data);
0N/A
0N/A } catch (Exception e) {
0N/A IOException ioe = new IOException("Failed to encrypt" +
0N/A " safe contents entry: " + e);
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A
0N/A // create EncryptedContentInfo
0N/A DerOutputStream bytes2 = new DerOutputStream();
0N/A bytes2.putOID(ContentInfo.DATA_OID);
0N/A bytes2.write(encodedAlgId);
0N/A
0N/A // Wrap encrypted data in a context-specific tag.
0N/A DerOutputStream tmpout2 = new DerOutputStream();
0N/A tmpout2.putOctetString(encryptedData);
0N/A bytes2.writeImplicit(DerValue.createTag(DerValue.TAG_CONTEXT,
0N/A false, (byte)0), tmpout2);
0N/A
0N/A // wrap EncryptedContentInfo in a Sequence
0N/A DerOutputStream out = new DerOutputStream();
0N/A out.write(DerValue.tag_Sequence, bytes2);
0N/A return out.toByteArray();
0N/A }
0N/A
0N/A /**
0N/A * Loads the keystore from the given input stream.
0N/A *
0N/A * <p>If a password is given, it is used to check the integrity of the
0N/A * keystore data. Otherwise, the integrity of the keystore is not checked.
0N/A *
0N/A * @param stream the input stream from which the keystore is loaded
0N/A * @param password the (optional) password used to check the integrity of
0N/A * the keystore.
0N/A *
0N/A * @exception IOException if there is an I/O or format problem with the
0N/A * keystore data
0N/A * @exception NoSuchAlgorithmException if the algorithm used to check
0N/A * the integrity of the keystore cannot be found
0N/A * @exception CertificateException if any of the certificates in the
0N/A * keystore could not be loaded
0N/A */
0N/A public synchronized void engineLoad(InputStream stream, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, CertificateException
0N/A {
0N/A DataInputStream dis;
0N/A CertificateFactory cf = null;
0N/A ByteArrayInputStream bais = null;
0N/A byte[] encoded = null;
0N/A
0N/A if (stream == null)
0N/A return;
0N/A
0N/A // reset the counter
0N/A counter = 0;
0N/A
0N/A DerValue val = new DerValue(stream);
0N/A DerInputStream s = val.toDerInputStream();
0N/A int version = s.getInteger();
0N/A
0N/A if (version != VERSION_3) {
0N/A throw new IOException("PKCS12 keystore not in version 3 format");
0N/A }
0N/A
0N/A entries.clear();
0N/A
0N/A /*
0N/A * Read the authSafe.
0N/A */
0N/A byte[] authSafeData;
0N/A ContentInfo authSafe = new ContentInfo(s);
0N/A ObjectIdentifier contentType = authSafe.getContentType();
0N/A
0N/A if (contentType.equals(ContentInfo.DATA_OID)) {
0N/A authSafeData = authSafe.getData();
0N/A } else /* signed data */ {
0N/A throw new IOException("public key protected PKCS12 not supported");
0N/A }
0N/A
0N/A DerInputStream as = new DerInputStream(authSafeData);
0N/A DerValue[] safeContentsArray = as.getSequence(2);
0N/A int count = safeContentsArray.length;
0N/A
0N/A // reset the count at the start
0N/A privateKeyCount = 0;
0N/A
0N/A /*
0N/A * Spin over the ContentInfos.
0N/A */
0N/A for (int i = 0; i < count; i++) {
0N/A byte[] safeContentsData;
0N/A ContentInfo safeContents;
0N/A DerInputStream sci;
0N/A byte[] eAlgId = null;
0N/A
0N/A sci = new DerInputStream(safeContentsArray[i].toByteArray());
0N/A safeContents = new ContentInfo(sci);
0N/A contentType = safeContents.getContentType();
0N/A safeContentsData = null;
0N/A if (contentType.equals(ContentInfo.DATA_OID)) {
0N/A safeContentsData = safeContents.getData();
0N/A } else if (contentType.equals(ContentInfo.ENCRYPTED_DATA_OID)) {
0N/A if (password == null) {
0N/A continue;
0N/A }
0N/A DerInputStream edi =
0N/A safeContents.getContent().toDerInputStream();
0N/A int edVersion = edi.getInteger();
0N/A DerValue[] seq = edi.getSequence(2);
0N/A ObjectIdentifier edContentType = seq[0].getOID();
0N/A eAlgId = seq[1].toByteArray();
0N/A if (!seq[2].isContextSpecific((byte)0)) {
0N/A throw new IOException("encrypted content not present!");
0N/A }
0N/A byte newTag = DerValue.tag_OctetString;
0N/A if (seq[2].isConstructed())
0N/A newTag |= 0x20;
0N/A seq[2].resetTag(newTag);
0N/A safeContentsData = seq[2].getOctetString();
0N/A
0N/A // parse Algorithm parameters
0N/A DerInputStream in = seq[1].toDerInputStream();
0N/A ObjectIdentifier algOid = in.getOID();
0N/A AlgorithmParameters algParams = parseAlgParameters(in);
0N/A
4784N/A while (true) {
4784N/A try {
4784N/A // Use JCE
4784N/A SecretKey skey = getPBEKey(password);
4784N/A Cipher cipher = Cipher.getInstance(algOid.toString());
4784N/A cipher.init(Cipher.DECRYPT_MODE, skey, algParams);
4784N/A safeContentsData = cipher.doFinal(safeContentsData);
4784N/A break;
4784N/A } catch (Exception e) {
4784N/A if (password.length == 0) {
4784N/A // Retry using an empty password
4784N/A // without a NULL terminator.
4784N/A password = new char[1];
4784N/A continue;
4784N/A }
4784N/A throw new IOException(
4784N/A "failed to decrypt safe contents entry: " + e, e);
4784N/A }
0N/A }
0N/A } else {
0N/A throw new IOException("public key protected PKCS12" +
0N/A " not supported");
0N/A }
0N/A DerInputStream sc = new DerInputStream(safeContentsData);
0N/A loadSafeContents(sc, password);
0N/A }
0N/A
0N/A // The MacData is optional.
0N/A if (password != null && s.available() > 0) {
0N/A MacData macData = new MacData(s);
0N/A try {
0N/A String algName = macData.getDigestAlgName().toUpperCase();
0N/A if (algName.equals("SHA") ||
0N/A algName.equals("SHA1") ||
0N/A algName.equals("SHA-1")) {
0N/A algName = "SHA1";
0N/A }
0N/A
0N/A // generate MAC (MAC key is created within JCE)
0N/A Mac m = Mac.getInstance("HmacPBE" + algName);
0N/A PBEParameterSpec params =
0N/A new PBEParameterSpec(macData.getSalt(),
0N/A macData.getIterations());
0N/A SecretKey key = getPBEKey(password);
0N/A m.init(key, params);
0N/A m.update(authSafeData);
0N/A byte[] macResult = m.doFinal();
0N/A
0N/A if (!Arrays.equals(macData.getDigest(), macResult)) {
0N/A throw new SecurityException("Failed PKCS12" +
0N/A " integrity checking");
0N/A }
0N/A } catch (Exception e) {
0N/A IOException ioe =
0N/A new IOException("Integrity check failed: " + e);
0N/A ioe.initCause(e);
0N/A throw ioe;
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * Match up private keys with certificate chains.
0N/A */
0N/A KeyEntry[] list = keyList.toArray(new KeyEntry[keyList.size()]);
0N/A for (int m = 0; m < list.length; m++) {
0N/A KeyEntry entry = list[m];
0N/A if (entry.keyId != null) {
0N/A ArrayList<X509Certificate> chain =
0N/A new ArrayList<X509Certificate>();
2549N/A X509Certificate cert = findMatchedCertificate(entry);
0N/A while (cert != null) {
0N/A chain.add(cert);
0N/A X500Principal issuerDN = cert.getIssuerX500Principal();
0N/A if (issuerDN.equals(cert.getSubjectX500Principal())) {
0N/A break;
0N/A }
2549N/A cert = certsMap.get(issuerDN);
0N/A }
0N/A /* Update existing KeyEntry in entries table */
0N/A if (chain.size() > 0)
0N/A entry.chain = chain.toArray(new Certificate[chain.size()]);
0N/A }
0N/A }
2549N/A certEntries.clear();
2549N/A certsMap.clear();
0N/A keyList.clear();
0N/A }
0N/A
2549N/A /**
2549N/A * Locates a matched CertEntry from certEntries, and returns its cert.
2549N/A * @param entry the KeyEntry to match
2549N/A * @return a certificate, null if not found
2549N/A */
2549N/A private X509Certificate findMatchedCertificate(KeyEntry entry) {
2549N/A CertEntry keyIdMatch = null;
2549N/A CertEntry aliasMatch = null;
2549N/A for (CertEntry ce: certEntries) {
2549N/A if (Arrays.equals(entry.keyId, ce.keyId)) {
2549N/A keyIdMatch = ce;
2549N/A if (entry.alias.equalsIgnoreCase(ce.alias)) {
2549N/A // Full match!
2549N/A return ce.cert;
2549N/A }
2549N/A } else if (entry.alias.equalsIgnoreCase(ce.alias)) {
2549N/A aliasMatch = ce;
2549N/A }
2549N/A }
2549N/A // keyId match first, for compatibility
2549N/A if (keyIdMatch != null) return keyIdMatch.cert;
2549N/A else if (aliasMatch != null) return aliasMatch.cert;
2549N/A else return null;
2549N/A }
0N/A
0N/A private void loadSafeContents(DerInputStream stream, char[] password)
0N/A throws IOException, NoSuchAlgorithmException, CertificateException
0N/A {
0N/A DerValue[] safeBags = stream.getSequence(2);
0N/A int count = safeBags.length;
0N/A
0N/A /*
0N/A * Spin over the SafeBags.
0N/A */
0N/A for (int i = 0; i < count; i++) {
0N/A ObjectIdentifier bagId;
0N/A DerInputStream sbi;
0N/A DerValue bagValue;
0N/A Object bagItem = null;
0N/A
0N/A sbi = safeBags[i].toDerInputStream();
0N/A bagId = sbi.getOID();
0N/A bagValue = sbi.getDerValue();
0N/A if (!bagValue.isContextSpecific((byte)0)) {
0N/A throw new IOException("unsupported PKCS12 bag value type "
0N/A + bagValue.tag);
0N/A }
0N/A bagValue = bagValue.data.getDerValue();
0N/A if (bagId.equals(PKCS8ShroudedKeyBag_OID)) {
0N/A KeyEntry kEntry = new KeyEntry();
0N/A kEntry.protectedPrivKey = bagValue.toByteArray();
0N/A bagItem = kEntry;
0N/A privateKeyCount++;
0N/A } else if (bagId.equals(CertBag_OID)) {
0N/A DerInputStream cs = new DerInputStream(bagValue.toByteArray());
0N/A DerValue[] certValues = cs.getSequence(2);
0N/A ObjectIdentifier certId = certValues[0].getOID();
0N/A if (!certValues[1].isContextSpecific((byte)0)) {
0N/A throw new IOException("unsupported PKCS12 cert value type "
0N/A + certValues[1].tag);
0N/A }
0N/A DerValue certValue = certValues[1].data.getDerValue();
0N/A CertificateFactory cf = CertificateFactory.getInstance("X509");
0N/A X509Certificate cert;
0N/A cert = (X509Certificate)cf.generateCertificate
0N/A (new ByteArrayInputStream(certValue.getOctetString()));
0N/A bagItem = cert;
0N/A } else {
0N/A // log error message for "unsupported PKCS12 bag type"
0N/A }
0N/A
0N/A DerValue[] attrSet;
0N/A try {
0N/A attrSet = sbi.getSet(2);
0N/A } catch (IOException e) {
0N/A // entry does not have attributes
0N/A // Note: CA certs can have no attributes
0N/A // OpenSSL generates pkcs12 with no attr for CA certs.
0N/A attrSet = null;
0N/A }
0N/A
0N/A String alias = null;
0N/A byte[] keyId = null;
0N/A
0N/A if (attrSet != null) {
0N/A for (int j = 0; j < attrSet.length; j++) {
0N/A DerInputStream as =
0N/A new DerInputStream(attrSet[j].toByteArray());
0N/A DerValue[] attrSeq = as.getSequence(2);
0N/A ObjectIdentifier attrId = attrSeq[0].getOID();
0N/A DerInputStream vs =
0N/A new DerInputStream(attrSeq[1].toByteArray());
0N/A DerValue[] valSet;
0N/A try {
0N/A valSet = vs.getSet(1);
0N/A } catch (IOException e) {
0N/A throw new IOException("Attribute " + attrId +
0N/A " should have a value " + e.getMessage());
0N/A }
0N/A if (attrId.equals(PKCS9FriendlyName_OID)) {
0N/A alias = valSet[0].getBMPString();
0N/A } else if (attrId.equals(PKCS9LocalKeyId_OID)) {
0N/A keyId = valSet[0].getOctetString();
0N/A } else {
0N/A // log error message for "unknown attr"
0N/A }
0N/A }
0N/A }
0N/A
0N/A /*
0N/A * As per PKCS12 v1.0 friendlyname (alias) and localKeyId (keyId)
0N/A * are optional PKCS12 bagAttributes. But entries in the keyStore
0N/A * are identified by their alias. Hence we need to have an
0N/A * Unfriendlyname in the alias, if alias is null. The keyId
0N/A * attribute is required to match the private key with the
0N/A * certificate. If we get a bagItem of type KeyEntry with a
0N/A * null keyId, we should skip it entirely.
0N/A */
0N/A if (bagItem instanceof KeyEntry) {
0N/A KeyEntry entry = (KeyEntry)bagItem;
0N/A if (keyId == null) {
0N/A // Insert a localKeyID for the privateKey
0N/A // Note: This is a workaround to allow null localKeyID
0N/A // attribute in pkcs12 with one private key entry and
0N/A // associated cert-chain
0N/A if (privateKeyCount == 1) {
0N/A keyId = "01".getBytes("UTF8");
0N/A } else {
0N/A continue;
0N/A }
0N/A }
0N/A entry.keyId = keyId;
0N/A // restore date if it exists
0N/A String keyIdStr = new String(keyId, "UTF8");
0N/A Date date = null;
0N/A if (keyIdStr.startsWith("Time ")) {
0N/A try {
0N/A date = new Date(
0N/A Long.parseLong(keyIdStr.substring(5)));
0N/A } catch (Exception e) {
0N/A date = null;
0N/A }
0N/A }
0N/A if (date == null) {
0N/A date = new Date();
0N/A }
0N/A entry.date = date;
0N/A keyList.add(entry);
0N/A if (alias == null)
0N/A alias = getUnfriendlyName();
0N/A entry.alias = alias;
0N/A entries.put(alias.toLowerCase(), entry);
0N/A } else if (bagItem instanceof X509Certificate) {
0N/A X509Certificate cert = (X509Certificate)bagItem;
0N/A // Insert a localKeyID for the corresponding cert
0N/A // Note: This is a workaround to allow null localKeyID
0N/A // attribute in pkcs12 with one private key entry and
0N/A // associated cert-chain
0N/A if ((keyId == null) && (privateKeyCount == 1)) {
0N/A // insert localKeyID only for EE cert or self-signed cert
0N/A if (i == 0) {
0N/A keyId = "01".getBytes("UTF8");
0N/A }
0N/A }
2549N/A certEntries.add(new CertEntry(cert, keyId, alias));
0N/A X500Principal subjectDN = cert.getSubjectX500Principal();
0N/A if (subjectDN != null) {
2549N/A if (!certsMap.containsKey(subjectDN)) {
2549N/A certsMap.put(subjectDN, cert);
2549N/A }
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A private String getUnfriendlyName() {
0N/A counter++;
0N/A return (String.valueOf(counter));
0N/A }
0N/A}