001: /*
002: * Lucane - a collaborative platform
003: * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package org.lucane.common.crypto;
020:
021: import java.io.File;
022: import java.io.FileInputStream;
023: import java.io.FileOutputStream;
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.io.OutputStream;
027:
028: public class KeyTool {
029: private static String getKeytoolFullPath() {
030: String javaHome = System.getProperty("java.home");
031: String keyTool = javaHome + "/bin/keytool";
032:
033: return keyTool.replace('\\', '/');
034: }
035:
036: public static String sixCharsMin(String base) {
037: String my = base;
038: while (my.length() < 6)
039: my += base;
040:
041: return my;
042: }
043:
044: public static String createPrivateStore(String name,
045: String keyPasswd) throws IOException, InterruptedException {
046: //create temp store
047: File privateStore = File.createTempFile("private", null);
048: privateStore.delete();
049:
050: // execute keytool
051: String[] cmd = { getKeytoolFullPath(), "-genkey", "-alias",
052: name, "-keystore", privateStore.getPath(),
053: "-storetype", "JKS", "-keyalg", "rsa", "-dname",
054: "CN=" + name + ", OU=, O=, L=, S=, C=", "-storepass",
055: sixCharsMin(name), "-keypass", sixCharsMin(keyPasswd) };
056:
057: Process p = Runtime.getRuntime().exec(cmd);
058: int errno = p.waitFor();
059: if (errno != 0)
060: throw new IOException(
061: "Unable to generate private store : errno=" + errno);
062:
063: //read temp store
064: InputStream input = new FileInputStream(privateStore);
065: byte[] data = new byte[input.available()];
066: input.read(data);
067: input.close();
068:
069: //delete temp file & return base64
070: privateStore.delete();
071: return Base64.encode(data);
072: }
073:
074: public static String reencodePrivateStore(String privateKey,
075: String name, String oldPasswd, String newPasswd)
076: throws IOException, InterruptedException {
077: //temp private store
078: File privateStore = File.createTempFile("private", null);
079: OutputStream output = new FileOutputStream(privateStore);
080: output.write(Base64.decode(privateKey));
081:
082: //change password
083: String[] cmd = { getKeytoolFullPath(), "-keypasswd", "-alias",
084: name, "-keystore", privateStore.getPath(), "-keypass",
085: sixCharsMin(oldPasswd), "-new", sixCharsMin(newPasswd) };
086: Process p = Runtime.getRuntime().exec(cmd);
087: int errno = p.waitFor();
088: if (errno != 0)
089: throw new IOException(
090: "Unable to change key password : errno=" + errno);
091:
092: //read temp store
093: InputStream input = new FileInputStream(privateStore);
094: byte[] data = new byte[input.available()];
095: input.read(data);
096: input.close();
097:
098: //delete temp file & return base64
099: privateStore.delete();
100: return Base64.encode(data);
101: }
102:
103: public static String createPublicStore(String privateKey,
104: String name) throws IOException, InterruptedException {
105: //temp private store
106: File privateStore = File.createTempFile("private", null);
107: OutputStream output = new FileOutputStream(privateStore);
108: output.write(Base64.decode(privateKey));
109:
110: //export certificate : execute keytool
111: File certificate = File.createTempFile("x509", null);
112: String[] certCmd = { getKeytoolFullPath(), "-export", "-alias",
113: name, "-keystore", privateStore.getPath(), "-file",
114: certificate.getPath(), "-storepass", sixCharsMin(name) };
115:
116: Process p = Runtime.getRuntime().exec(certCmd);
117: int errno = p.waitFor();
118: if (errno != 0)
119: throw new IOException(
120: "Unable to extract certificate : errno=" + errno);
121:
122: //generate public store
123: File publicStore = File.createTempFile("public", null);
124: publicStore.delete();
125: String[] cmd = { getKeytoolFullPath(), "-import", "-noprompt",
126: "-alias", name, "-keystore", publicStore.getPath(),
127: "-file", certificate.getPath(), "-storepass",
128: sixCharsMin(name) };
129:
130: p = Runtime.getRuntime().exec(cmd);
131: errno = p.waitFor();
132: if (errno != 0)
133: throw new IOException(
134: "Unable to extract public key : errno=" + errno);
135:
136: //read temp store
137: InputStream input = new FileInputStream(publicStore);
138: byte[] data = new byte[input.available()];
139: input.read(data);
140: input.close();
141:
142: //delete temp files & return base64
143: privateStore.delete();
144: certificate.delete();
145: publicStore.delete();
146: return Base64.encode(data);
147: }
148:
149: public static void main(String[] args) throws IOException,
150: InterruptedException {
151: String priv = createPrivateStore("admin", "admin");
152: String pub = createPublicStore(priv, "admin");
153: System.out.println("priv : " + priv.length());
154: System.out.println("pub : " + pub.length());
155: }
156: }
|