KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > upgrade > certconversion > JKStoJKSConversionModule


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.tools.upgrade.certconversion;
25
26 import java.io.*;
27 import java.util.*;
28 import java.util.logging.*;
29 import java.security.*;
30 import java.security.cert.*;
31 import com.sun.enterprise.tools.upgrade.logging.*;
32 import com.sun.enterprise.util.i18n.StringManager;
33 import com.sun.enterprise.tools.upgrade.common.*;
34
35 /**
36  *
37  * author : Servesh Singh
38  *
39  */

40
41 public class JKStoJKSConversionModule implements BaseModule{
42     
43     private static Logger _logger = LogService.getLogger(LogService.UPGRADE_LOGGER);
44     private StringManager sm;
45     private String JavaDoc jksPath;
46     private String JavaDoc sourceJksPath;
47     private String JavaDoc trustJksPath;
48     private String JavaDoc sourceTrustJksPath;
49     private String JavaDoc jksKeyStorePassword;
50     private String JavaDoc jksCAkeyStorePassword;
51     private boolean certificateError = false;
52     private CommonInfoModel commonInfo;
53     
54     public JKStoJKSConversionModule(){
55         sm = StringManager.getManager(LogService.UPGRADE_CERTCONVERSION_LOGGER);
56     }
57     
58     public boolean upgrade(CommonInfoModel commonInfo){
59         try {
60             String JavaDoc currentDomain = commonInfo.getCurrentDomain();
61             if(!(commonInfo.getDomainOptionList().contains(currentDomain)))
62                 return true;
63             this.jksPath=commonInfo.getTargetJKSKeyStorePath();
64             this.sourceJksPath=commonInfo.getSourceJKSKeyStorePath();
65             this.trustJksPath = commonInfo.getTargetTrustedJKSKeyStorePath();
66             this.sourceTrustJksPath = commonInfo.getSourceTrustedJKSKeyStorePath();
67             this.jksKeyStorePassword=commonInfo.getJksKeystorePassword();
68             this.jksCAkeyStorePassword=commonInfo.getJksCAKeystorePassword();
69             this.commonInfo = commonInfo;
70             _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.start_certificate_migration",currentDomain));
71             doBackup(commonInfo);
72             copyKeyPairs(sourceJksPath,jksPath);
73             copyCACertificates(sourceTrustJksPath,trustJksPath);
74             _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.finished_certificate_migration",currentDomain));
75         }catch(CertificateException ce) {
76             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_migrate_certificates",ce));
77             UpdateProgressManager.getProgressManager().setContinueUpgrade(false);
78             return false;
79         }
80         return true;
81     }
82     
83     private void doBackup(CommonInfoModel commonInfo) throws CertificateException {
84         doCACertificateBackup();
85         doKeyPairBackup();
86     }
87     
88     private void doCACertificateBackup() throws CertificateException{
89         copyCACertificates(trustJksPath, trustJksPath+".back");
90     /*FileInputStream in = null;
91     FileOutputStream out = null;
92     try {
93         KeyStore keystoreSource = KeyStore.getInstance(KeyStore.getDefaultType());
94         KeyStore keystoreTarget = KeyStore.getInstance(KeyStore.getDefaultType());
95         in = new FileInputStream(new File(trustJksPath));
96         keystoreSource.load(in,jksCAkeyStorePassword.toCharArray());
97         keystoreTarget.load(null, jksCAkeyStorePassword.toCharArray());
98         java.util.Enumeration en = keystoreSource.aliases();
99         for(; en.hasMoreElements(); ){
100             String alias = (String) en.nextElement();
101             java.security.cert.Certificate cert = keystoreSource.getCertificate(alias);
102             keystoreTarget.setCertificateEntry(alias,cert);
103         }
104         out = new FileOutputStream(trustJksPath+".back");
105         keystoreTarget.store(out, jksCAkeyStorePassword.toCharArray());
106     } catch (java.security.cert.CertificateException e) {
107         _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
108     } catch (NoSuchAlgorithmException e) {
109          _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
110     } catch (FileNotFoundException e) {
111          _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
112               // Keystore does not exist
113     } catch (KeyStoreException e) {
114          _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
115     } catch (IOException e) {
116          _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
117     }catch(Exception e) {
118          _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
119     }finally {
120         try {
121             if(in!=null)
122                 in.close();
123             if(out!=null)
124                 out.close();
125         }catch(Exception ex){}
126     } */

127     }
128     
129     private void copyCACertificates(String JavaDoc sourceTrustJksPath, String JavaDoc targetTrustJksPath) throws CertificateException {
130         FileInputStream in = null;
131         FileOutputStream out = null;
132         try {
133             KeyStore keystoreSource = KeyStore.getInstance(KeyStore.getDefaultType());
134             KeyStore keystoreTarget = KeyStore.getInstance(KeyStore.getDefaultType());
135             in = new FileInputStream(new File(sourceTrustJksPath));
136             keystoreSource.load(in,jksCAkeyStorePassword.toCharArray());
137             keystoreTarget.load(null, jksCAkeyStorePassword.toCharArray());
138             java.util.Enumeration JavaDoc en = keystoreSource.aliases();
139             for(; en.hasMoreElements(); ){
140                 String JavaDoc alias = (String JavaDoc) en.nextElement();
141                 java.security.cert.Certificate JavaDoc cert = keystoreSource.getCertificate(alias);
142                 keystoreTarget.setCertificateEntry(alias,cert);
143                 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.ca_transferred",alias));
144                 
145             }
146             out = new FileOutputStream(targetTrustJksPath);
147             keystoreTarget.store(out, jksCAkeyStorePassword.toCharArray());
148         } catch (java.security.cert.CertificateException JavaDoc e) {
149             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
150             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
151         } catch (NoSuchAlgorithmException e) {
152             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
153             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
154         } catch (FileNotFoundException e) {
155             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
156             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
157             // Keystore does not exist
158
} catch (KeyStoreException e) {
159             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
160             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
161         } catch (IOException e) {
162             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
163             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
164         }catch(Exception JavaDoc e) {
165             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
166             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
167         }finally {
168             try {
169                 if(in!=null)
170                     in.close();
171                 if(out!=null)
172                     out.close();
173             }catch(Exception JavaDoc ex){}
174         }
175     }
176     
177     private void doKeyPairBackup() throws CertificateException{
178         copyKeyPairs(jksPath, jksPath+".back") ;
179     }
180     
181     private void copyKeyPairs(String JavaDoc sourceJksPath, String JavaDoc targetJksPath) throws CertificateException{
182         FileInputStream in = null;
183         FileOutputStream out = null;
184         try {
185             KeyStore keystoreSource = KeyStore.getInstance(KeyStore.getDefaultType());
186             KeyStore keystoreTarget = KeyStore.getInstance(KeyStore.getDefaultType());
187             in = new FileInputStream(new File(sourceJksPath));
188             keystoreSource.load(in,jksKeyStorePassword.toCharArray());
189             keystoreTarget.load(null, jksKeyStorePassword.toCharArray());
190             java.util.Enumeration JavaDoc en = keystoreSource.aliases();
191             for(; en.hasMoreElements(); ){
192                 String JavaDoc alias = (String JavaDoc) en.nextElement();
193                 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.processing_keypair",alias));
194                 Key key = keystoreSource.getKey(alias, jksKeyStorePassword.toCharArray());
195                 java.security.cert.Certificate JavaDoc[] cert = keystoreSource.getCertificateChain(alias);
196                 keystoreTarget.setKeyEntry(alias, key, jksKeyStorePassword.toCharArray(), cert);
197                 _logger.log(Level.INFO,sm.getString("enterprise.tools.upgrade.certconversion.alias_transferred",alias));
198             }
199             out = new FileOutputStream(targetJksPath);
200             keystoreTarget.store(out, jksKeyStorePassword.toCharArray());
201         } catch (java.security.cert.CertificateException JavaDoc e) {
202             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
203             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
204         } catch (NoSuchAlgorithmException e) {
205             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
206             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
207         } catch (FileNotFoundException e) {
208             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
209             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
210             // Keystore does not exist
211
} catch (KeyStoreException e) {
212             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
213             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
214         } catch (IOException e) {
215             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
216             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
217         }catch(Exception JavaDoc e) {
218             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.unknownError"),e);
219             throw new CertificateException(sm.getString("enterprise.tools.upgrade.unknownError"));
220         }finally {
221             try {
222                 if(in!=null)
223                     in.close();
224                 if(out!=null)
225                     out.close();
226             }catch(Exception JavaDoc ex){}
227         }
228     }
229     
230     public void recovery(CommonInfoModel commonInfo) {
231         File keypairKeyStoreOriginalFile = new File(jksPath);
232         File keypairKeyStoreBackupFile = new File(jksPath+".back");
233         File trustedKeyStoreOriginalFile = new File(trustJksPath);
234         File trustedKeyStoreBackupFile = new File(trustJksPath+".back");
235         new File(jksPath+".back1").delete();
236         new File(trustJksPath+".back1").delete();
237         boolean success = keypairKeyStoreOriginalFile.renameTo(new File(jksPath+".back1"));
238         if(!success) {
239             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates"));
240             return;
241         }
242         success = trustedKeyStoreOriginalFile.renameTo(new File(trustJksPath+".back1"));
243         if(!success) {
244             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates"));
245             return;
246         }
247         keypairKeyStoreOriginalFile.delete();
248         trustedKeyStoreOriginalFile.delete();
249         success = keypairKeyStoreBackupFile.renameTo(keypairKeyStoreOriginalFile);
250         if(!success) {
251             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates"));
252             File keypairKeyStoreanotherBackupFile = new File(jksPath+".back1");
253             File trustedKeyStoreanotherBackupFile = new File(trustJksPath+".back1");
254             keypairKeyStoreanotherBackupFile.renameTo(keypairKeyStoreOriginalFile);
255             trustedKeyStoreanotherBackupFile.renameTo(trustedKeyStoreOriginalFile);
256             return;
257         }
258         success = trustedKeyStoreBackupFile.renameTo(trustedKeyStoreOriginalFile);
259         if(!success) {
260             _logger.log(Level.WARNING,sm.getString("enterprise.tools.upgrade.certconversion.could_not_recover_certificates"));
261             File keypairKeyStoreanotherBackupFile = new File(jksPath+".back1");
262             File trustedKeyStoreanotherBackupFile = new File(trustJksPath+".back1");
263             keypairKeyStoreanotherBackupFile.renameTo(keypairKeyStoreOriginalFile);
264             trustedKeyStoreanotherBackupFile.renameTo(trustedKeyStoreOriginalFile);
265             return;
266         }
267         new File(jksPath+".back1").delete();
268         new File(trustJksPath+".back1").delete();
269     }
270     
271     
272     public static void main(String JavaDoc[] args){
273         CommonInfoModel commonInfo = new CommonInfoModel();
274         commonInfo.setSourceInstallDir(args[0]);
275         commonInfo.setTargetInstallDir(args[1]);
276         commonInfo.setCertDbPassword(args[2]);
277         commonInfo.setJksKeystorePassword(args[3]);
278         JKStoJKSConversionModule convModule = new JKStoJKSConversionModule();
279         convModule.upgrade(commonInfo);
280     }
281     
282     public String JavaDoc getName() {
283         return sm.getString("enterprise.tools.upgrade.certconversion.moduleName");
284     }
285     
286 }
287
288
Popular Tags