KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > security > KeyStores


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.update.internal.security;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17 import java.io.Reader JavaDoc;
18 import java.net.MalformedURLException JavaDoc;
19 import java.net.URL JavaDoc;
20 import java.security.Security JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.eclipse.update.internal.core.UpdateCore;
26 import org.eclipse.update.internal.core.connection.ConnectionFactory;
27 /**
28  * Class to manage the different KeyStores we should
29  * check for certificates of Signed JAR
30  */

31 public class KeyStores {
32
33
34     /**
35      * java.policy files properties of the java.security file
36      */

37     private static final String JavaDoc JAVA_POLICY_URL = "policy.url."; //$NON-NLS-1$
38

39     /**
40      * Default keystore type in java.security file
41      */

42     private static final String JavaDoc DEFAULT_KEYSTORE_TYPE = "keystore.type"; //$NON-NLS-1$
43

44     /**
45      * List of KeystoreHandle pointing of valid KeyStores
46      * the URL of the KeystoreHandle is not tested yet...
47      */

48     private List JavaDoc /* of KeystoreHandle */ listOfKeyStores;
49
50     /**
51      * Iterator
52      */

53     private Iterator JavaDoc iterator;
54     /**
55      * KeyStores constructor comment.
56      */

57     public KeyStores() {
58         super();
59         initializeDefaultKeyStores();
60     }
61     /**
62      *
63      */

64     private Iterator JavaDoc getIterator() {
65         if (iterator == null)
66             iterator = listOfKeyStores.iterator();
67         return iterator;
68     }
69     /**
70      * returns trus if there is more Keystores in the list
71      */

72     public boolean hasNext() {
73         return getIterator().hasNext();
74     }
75     /**
76      * populate the list of Keystores
77      * should be done with Dialog with Cancel/Skip button if
78      * the connection to the URL is down...
79      */

80     private void initializeDefaultKeyStores() {
81
82         listOfKeyStores = new ArrayList JavaDoc(5);
83
84         // get JRE cacerts
85
try {
86             URL JavaDoc url = new URL JavaDoc("file", null, 0, System.getProperty("java.home") + File.separator + "lib" + File.separator + "security" + File.separator + "cacerts"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
87
listOfKeyStores.add(new KeystoreHandle(url,Security.getProperty(DEFAULT_KEYSTORE_TYPE)));
88         }
89         catch (MalformedURLException JavaDoc e) {
90             // should not happen, hardcoded...
91
}
92
93         // get java.home .keystore
94
try {
95             URL JavaDoc url = new URL JavaDoc("file", null, 0, System.getProperty("user.home") + File.separator + ".keystore"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
96
listOfKeyStores.add(new KeystoreHandle(url,Security.getProperty(DEFAULT_KEYSTORE_TYPE)));
97         }
98         catch (MalformedURLException JavaDoc e) {
99             // should not happen, hardcoded...
100
}
101
102         // get KeyStores from policy files...
103
int index = 1;
104         String JavaDoc java_policy = Security.getProperty(JAVA_POLICY_URL+index);
105         while (java_policy!=null){
106             // retrieve keystore url from java.policy
107
// also retrieve keystore type
108
KeystoreHandle keystore = getKeystoreFromLocation(java_policy);
109             if (keystore!=null){
110                 listOfKeyStores.add(keystore);
111             }
112             index++;
113             java_policy = Security.getProperty(JAVA_POLICY_URL+index);
114         }
115
116     }
117     /**
118      * returns the URL for the Next KeystoreHandle
119      */

120     public KeystoreHandle next() {
121         return (KeystoreHandle) getIterator().next();
122     }
123     
124     /**
125      * retrieve the keystore from java.policy file
126      */

127     private KeystoreHandle getKeystoreFromLocation(String JavaDoc location){
128         
129         InputStream JavaDoc in = null;
130         char[] buff = new char[4096];
131         
132         
133         int indexOf$ = location.indexOf("${"); //$NON-NLS-1$
134
int indexOfCurly = location.indexOf('}',indexOf$);
135         if (indexOf$!=-1 && indexOfCurly!=-1){
136             String JavaDoc prop = System.getProperty(location.substring(indexOf$+2,indexOfCurly));
137             String JavaDoc location2 = location.substring(0,indexOf$);
138             location2 += prop;
139             location2 += location.substring(indexOfCurly+1);
140             location = location2;
141         }
142         
143         
144         try {
145             URL JavaDoc url = new URL JavaDoc(location);
146             in = ConnectionFactory.get(url).getInputStream();
147             Reader JavaDoc reader = new InputStreamReader JavaDoc(in);
148             int result = reader.read(buff);
149             StringBuffer JavaDoc contentBuff = new StringBuffer JavaDoc();
150             while (result!=-1){
151                 contentBuff.append(buff,0,result);
152                 result = reader.read(buff);
153             }
154
155             if (contentBuff.length()>0){
156                 String JavaDoc content = new String JavaDoc(contentBuff);
157                 int indexOfKeystore = content.indexOf("keystore"); //$NON-NLS-1$
158
if (indexOfKeystore != -1){
159                     int indexOfSemiColumn = content.indexOf(';',indexOfKeystore);
160                     return getKeystoreFromString(content.substring(indexOfKeystore,indexOfSemiColumn),url);
161                 }
162             }
163         } catch (MalformedURLException JavaDoc e){
164             log(e);
165         } catch (IOException JavaDoc e){
166             // url.openStream, reader.read (x2)
167
// only log, the keystore may not exist
168
log(e);
169         } finally {
170             if (in!=null){
171                 try {
172                     in.close();
173                 } catch (IOException JavaDoc e){}
174             }
175         }
176         return null;
177     }
178     
179     /**
180      * retrieve the keystore from java.policy file
181      */

182     private KeystoreHandle getKeystoreFromString(String JavaDoc content,URL JavaDoc rootURL){
183         KeystoreHandle handle = null;
184         String JavaDoc keyStoreType = Security.getProperty(DEFAULT_KEYSTORE_TYPE);
185         
186         
187         int indexOfSpace = content.indexOf(' ');
188         if (indexOfSpace==-1) return null;
189         
190         int secondSpace = content.lastIndexOf(',');
191         if (secondSpace==-1) {
192             secondSpace = content.length();
193         } else {
194             keyStoreType = content.substring(secondSpace+1,content.length()).trim();
195         }
196         
197         URL JavaDoc url = null;
198         try {
199             url = new URL JavaDoc(content.substring(indexOfSpace,secondSpace));
200         } catch (MalformedURLException JavaDoc e){
201             log(e);
202             // the url maybe relative
203
try {
204             url = new URL JavaDoc(rootURL,content.substring(indexOfSpace,secondSpace));
205             } catch (MalformedURLException JavaDoc e1){
206                 log(e1);
207             }
208         }
209
210         if (url!=null)
211             handle = new KeystoreHandle(url,keyStoreType);
212             
213         return handle;
214     }
215     
216     private void log(Exception JavaDoc e){
217         UpdateCore.warn("Cannot retrieve a KeyStore",e); //$NON-NLS-1$
218
}
219 }
220
Popular Tags