001: //PKCS12Tool.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2006
007: //
008: //This file ist contributed by Martin Thelian
009: //
010: //last change: $LastChangedDate: 2006-05-12 16:35:56 +0200 (Fr, 12 Mai 2006) $ by $LastChangedBy: theli $
011: //Revision: $LastChangedRevision: 2086 $
012: //
013: //This program is free software; you can redistribute it and/or modify
014: //it under the terms of the GNU General Public License as published by
015: //the Free Software Foundation; either version 2 of the License, or
016: //(at your option) any later version.
017: //
018: //This program is distributed in the hope that it will be useful,
019: //but WITHOUT ANY WARRANTY; without even the implied warranty of
020: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
021: //GNU General Public License for more details.
022: //
023: //You should have received a copy of the GNU General Public License
024: //along with this program; if not, write to the Free Software
025: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
026: //
027: //Using this software in any meaning (reading, learning, copying, compiling,
028: //running) means that you agree that the Author(s) is (are) not responsible
029: //for cost, loss of data or any harm that may be caused directly or indirectly
030: //by usage of this softare or this documentation. The usage of this software
031: //is on your own risk. The installation and usage (starting/running) of this
032: //software may allow other people or application to access your computer and
033: //any attached devices and is highly dependent on the configuration of the
034: //software which must be done by the user of the software; the author(s) is
035: //(are) also not responsible for proper configuration and usage of the
036: //software, even if provoked by documentation provided together with
037: //the software.
038: //
039: //Any changes to this file according to the GPL as documented in the file
040: //gpl.txt aside this file in the shipment you received can be done to the
041: //lines that follows this copyright notice here, but changes must not be
042: //done inside the copyright notive above. A re-distribution must contain
043: //the intact and unchanged copyright notice.
044: //Contributions and changes to the program code must be marked as such.
045:
046: package de.anomic.tools;
047:
048: import java.io.File;
049: import java.io.FileInputStream;
050: import java.io.FileNotFoundException;
051: import java.io.FileOutputStream;
052: import java.io.IOException;
053: import java.security.Key;
054: import java.security.KeyStore;
055: import java.security.KeyStoreException;
056: import java.security.NoSuchAlgorithmException;
057: import java.security.UnrecoverableKeyException;
058: import java.security.cert.Certificate;
059: import java.security.cert.CertificateException;
060: import java.util.Enumeration;
061:
062: public class PKCS12Tool {
063:
064: private KeyStore kspkcs12;
065: private String kspkcs12Pass;
066:
067: public PKCS12Tool(String pkcs12FileName, String pkcs12Pwd)
068: throws KeyStoreException, NoSuchAlgorithmException,
069: CertificateException, FileNotFoundException, IOException {
070: if (pkcs12FileName == null)
071: throw new NullPointerException();
072: this .kspkcs12Pass = pkcs12Pwd;
073:
074: // creating PKCS12 keystore
075: this .kspkcs12 = KeyStore.getInstance("PKCS12");
076:
077: // load pkcs12 file into keystore object
078: FileInputStream fileIn = new FileInputStream(pkcs12FileName);
079: this .kspkcs12.load(fileIn, (pkcs12Pwd != null) ? pkcs12Pwd
080: .toCharArray() : null);
081:
082: // close stream
083: fileIn.close();
084: }
085:
086: public Enumeration<String> aliases() throws KeyStoreException {
087: return this .kspkcs12.aliases();
088: }
089:
090: public void printAliases() throws KeyStoreException {
091: Enumeration<String> aliases = aliases();
092: while (aliases.hasMoreElements()) {
093: System.out.println(aliases.nextElement());
094: }
095: }
096:
097: public void importToJKS(String jksName, String jksPassword)
098: throws KeyStoreException, NoSuchAlgorithmException,
099: CertificateException, IOException,
100: UnrecoverableKeyException {
101: // creating java keystore
102: KeyStore jks = KeyStore.getInstance("JKS");
103:
104: // loading keystore from file
105: FileInputStream jksFileIn = null;
106: File jksFile = new File(jksName);
107:
108: if (jksFile.exists()) {
109: System.err.println("Loading java keystore from file '"
110: + jksFile + "'");
111: jksFileIn = new FileInputStream(jksFile);
112: } else {
113: System.err.println("Creating new java keystore '" + jksFile
114: + "'");
115: }
116: jks.load(jksFileIn, (jksPassword != null) ? jksPassword
117: .toCharArray() : null);
118: if (jksFileIn != null)
119: jksFileIn.close();
120:
121: Enumeration<String> pkcs12Aliases = aliases();
122: while (pkcs12Aliases.hasMoreElements()) {
123: String strAlias = (String) pkcs12Aliases.nextElement();
124: System.err.println("Importing Alias '" + strAlias + "'");
125:
126: if (this .kspkcs12.isKeyEntry(strAlias)) {
127: System.err.println("- Alias has key");
128: Key key = this .kspkcs12.getKey(strAlias,
129: (this .kspkcs12Pass != null) ? this .kspkcs12Pass
130: .toCharArray() : null);
131: System.err.println("- Alias key imported");
132:
133: Certificate[] chain = this .kspkcs12
134: .getCertificateChain(strAlias);
135: System.err.println("- Alias certificate chain size: "
136: + chain.length);
137:
138: jks.setKeyEntry(strAlias, key,
139: (jksPassword != null) ? jksPassword
140: .toCharArray() : null, chain);
141: }
142: }
143:
144: // storing jdk into file
145: System.err.print("Storing java keystore");
146: FileOutputStream jksFileOut = new FileOutputStream(jksName);
147: jks.store(jksFileOut, (jksPassword != null) ? jksPassword
148: .toCharArray() : null);
149: jksFileOut.close();
150: System.err.print("Import finished.");
151: }
152:
153: /**
154: * @param args
155: */
156: public static void main(String[] args) throws Exception {
157: PKCS12Tool pkcs12 = new PKCS12Tool("c:/temp/keystore.pkcs12",
158: "test");
159: //pkcs12.printAliases();
160: pkcs12.importToJKS("c:/temp/jks.ks", "test");
161: }
162:
163: }
|