KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > common > crypto > KeyTool


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.common.crypto;
20
21 import java.io.File JavaDoc;
22 import java.io.FileInputStream JavaDoc;
23 import java.io.FileOutputStream JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27
28 public class KeyTool
29 {
30     private static String JavaDoc getKeytoolFullPath()
31     {
32         String JavaDoc javaHome = System.getProperty("java.home");
33         String JavaDoc keyTool = javaHome + "/bin/keytool";
34         
35         return keyTool.replace('\\', '/');
36     }
37     
38     public static String JavaDoc sixCharsMin(String JavaDoc base)
39     {
40         String JavaDoc my = base;
41         while(my.length() < 6)
42             my += base;
43         
44         return my;
45     }
46     
47     public static String JavaDoc createPrivateStore(String JavaDoc name, String JavaDoc keyPasswd)
48     throws IOException JavaDoc, InterruptedException JavaDoc
49     {
50         //create temp store
51
File JavaDoc privateStore = File.createTempFile("private", null);
52         privateStore.delete();
53         
54         // execute keytool
55
String JavaDoc[] cmd = {
56                 getKeytoolFullPath(),
57                 "-genkey",
58                 "-alias",
59                 name,
60                 "-keystore",
61                 privateStore.getPath(),
62                 "-storetype",
63                 "JKS",
64                 "-keyalg",
65                 "rsa",
66                 "-dname",
67                 "CN=" + name + ", OU=, O=, L=, S=, C=",
68                 "-storepass",
69                 sixCharsMin(name),
70                 "-keypass",
71                 sixCharsMin(keyPasswd)};
72         
73         Process JavaDoc p = Runtime.getRuntime().exec(cmd);
74         int errno = p.waitFor();
75         if(errno != 0)
76             throw new IOException JavaDoc("Unable to generate private store : errno=" + errno);
77         
78         //read temp store
79
InputStream JavaDoc input = new FileInputStream JavaDoc(privateStore);
80         byte[] data = new byte[input.available()];
81         input.read(data);
82         input.close();
83         
84         //delete temp file & return base64
85
privateStore.delete();
86         return Base64.encode(data);
87     }
88     
89     public static String JavaDoc reencodePrivateStore(String JavaDoc privateKey, String JavaDoc name, String JavaDoc oldPasswd, String JavaDoc newPasswd)
90     throws IOException JavaDoc, InterruptedException JavaDoc
91     {
92         //temp private store
93
File JavaDoc privateStore = File.createTempFile("private", null);
94         OutputStream JavaDoc output = new FileOutputStream JavaDoc(privateStore);
95         output.write(Base64.decode(privateKey));
96
97         //change password
98
String JavaDoc[] cmd = {
99                 getKeytoolFullPath(),
100                 "-keypasswd",
101                 "-alias",
102                 name,
103                 "-keystore",
104                 privateStore.getPath(),
105                 "-keypass",
106                 sixCharsMin(oldPasswd),
107                 "-new",
108                 sixCharsMin(newPasswd)};
109         Process JavaDoc p = Runtime.getRuntime().exec(cmd);
110         int errno = p.waitFor();
111         if(errno != 0)
112             throw new IOException JavaDoc("Unable to change key password : errno=" + errno);
113         
114         //read temp store
115
InputStream JavaDoc input = new FileInputStream JavaDoc(privateStore);
116         byte[] data = new byte[input.available()];
117         input.read(data);
118         input.close();
119         
120         //delete temp file & return base64
121
privateStore.delete();
122         return Base64.encode(data);
123     }
124     
125     public static String JavaDoc createPublicStore(String JavaDoc privateKey, String JavaDoc name)
126     throws IOException JavaDoc, InterruptedException JavaDoc
127     {
128         //temp private store
129
File JavaDoc privateStore = File.createTempFile("private", null);
130         OutputStream JavaDoc output = new FileOutputStream JavaDoc(privateStore);
131         output.write(Base64.decode(privateKey));
132         
133         //export certificate : execute keytool
134
File JavaDoc certificate = File.createTempFile("x509", null);
135         String JavaDoc[] certCmd = {
136                 getKeytoolFullPath(),
137                 "-export",
138                 "-alias",
139                 name,
140                 "-keystore",
141                 privateStore.getPath(),
142                 "-file",
143                 certificate.getPath(),
144                 "-storepass",
145                 sixCharsMin(name)};
146         
147         Process JavaDoc p = Runtime.getRuntime().exec(certCmd);
148         int errno = p.waitFor();
149         if(errno != 0)
150             throw new IOException JavaDoc("Unable to extract certificate : errno=" + errno);
151         
152         
153         //generate public store
154
File JavaDoc publicStore = File.createTempFile("public", null);
155         publicStore.delete();
156         String JavaDoc[] cmd = {
157                 getKeytoolFullPath(),
158                 "-import",
159                 "-noprompt",
160                 "-alias",
161                 name,
162                 "-keystore",
163                 publicStore.getPath(),
164                 "-file",
165                 certificate.getPath(),
166                 "-storepass",
167                 sixCharsMin(name)};
168         
169         p = Runtime.getRuntime().exec(cmd);
170         errno = p.waitFor();
171         if(errno != 0)
172             throw new IOException JavaDoc("Unable to extract public key : errno=" + errno);
173
174         //read temp store
175
InputStream JavaDoc input = new FileInputStream JavaDoc(publicStore);
176         byte[] data = new byte[input.available()];
177         input.read(data);
178         input.close();
179         
180         //delete temp files & return base64
181
privateStore.delete();
182         certificate.delete();
183         publicStore.delete();
184         return Base64.encode(data);
185     }
186     
187     public static void main(String JavaDoc[] args)
188     throws IOException JavaDoc, InterruptedException JavaDoc
189     {
190         String JavaDoc priv = createPrivateStore("admin", "admin");
191         String JavaDoc pub = createPublicStore(priv, "admin");
192         System.out.println("priv : " + priv.length());
193         System.out.println("pub : " + pub.length());
194     }
195 }
Popular Tags