Package HTTPSupport

Source Code of HTTPSupport.KeystoreConverter

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package HTTPSupport;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.cert.Certificate;


/**
* This tool copies a key from a PKCS12 keystore into a Java Keystore, which can be used by the JSSE library.
*
* - First, create a java keystore (you have to generate a dummy key to do this)
*     keytool -genkey -keystore keystore.jks -storepass <same password as PKCS12 keystore>
* - Then, remove the dummy key
*     keytool -delete -keystore keystore.jks -alias mykey
* - Next, update the constants below to reflect the locations of your keystores, the alias of the key to copy,
*   and the password of the keystores
* - Finally, execute the application
*/
public class KeystoreConverter {

    private final static String KEY_ALIAS = "1";
    private final static char[] KEY_PASSWORD = "luton123".toCharArray();
    private final static String PKCS12_KEYSTORE = "D:\\gmac\\eclipse\\workspace\\GMAC_POC\\etc\\server.p12";
    private final static String JKS_KEYSTORE = "D:\\gmac\\eclipse\\workspace\\GMAC_POC\\etc\\keystore.jks";

    public static void main(String[] args) throws Exception {
        KeyStore kspkcs12 = KeyStore.getInstance("PKCS12");
        kspkcs12.load(new FileInputStream(PKCS12_KEYSTORE), KEY_PASSWORD);
        Certificate certificateChain[] = kspkcs12.getCertificateChain(KEY_ALIAS);
        Key key = kspkcs12.getKey(KEY_ALIAS, KEY_PASSWORD);

        KeyStore ksjks=KeyStore.getInstance("JKS");
        ksjks.load(new FileInputStream(JKS_KEYSTORE), KEY_PASSWORD);

        ksjks.setKeyEntry(KEY_ALIAS, key, KEY_PASSWORD, certificateChain);

        ksjks.store(new FileOutputStream(JKS_KEYSTORE), KEY_PASSWORD);
    }

}
TOP

Related Classes of HTTPSupport.KeystoreConverter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.