1 11 package org.eclipse.update.internal.security; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.InputStreamReader ; 17 import java.io.Reader ; 18 import java.net.MalformedURLException ; 19 import java.net.URL ; 20 import java.security.Security ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 25 import org.eclipse.update.internal.core.UpdateCore; 26 import org.eclipse.update.internal.core.connection.ConnectionFactory; 27 31 public class KeyStores { 32 33 34 37 private static final String JAVA_POLICY_URL = "policy.url."; 39 42 private static final String DEFAULT_KEYSTORE_TYPE = "keystore.type"; 44 48 private List listOfKeyStores; 49 50 53 private Iterator iterator; 54 57 public KeyStores() { 58 super(); 59 initializeDefaultKeyStores(); 60 } 61 64 private Iterator getIterator() { 65 if (iterator == null) 66 iterator = listOfKeyStores.iterator(); 67 return iterator; 68 } 69 72 public boolean hasNext() { 73 return getIterator().hasNext(); 74 } 75 80 private void initializeDefaultKeyStores() { 81 82 listOfKeyStores = new ArrayList (5); 83 84 try { 86 URL url = new URL ("file", null, 0, System.getProperty("java.home") + File.separator + "lib" + File.separator + "security" + File.separator + "cacerts"); listOfKeyStores.add(new KeystoreHandle(url,Security.getProperty(DEFAULT_KEYSTORE_TYPE))); 88 } 89 catch (MalformedURLException e) { 90 } 92 93 try { 95 URL url = new URL ("file", null, 0, System.getProperty("user.home") + File.separator + ".keystore"); listOfKeyStores.add(new KeystoreHandle(url,Security.getProperty(DEFAULT_KEYSTORE_TYPE))); 97 } 98 catch (MalformedURLException e) { 99 } 101 102 int index = 1; 104 String java_policy = Security.getProperty(JAVA_POLICY_URL+index); 105 while (java_policy!=null){ 106 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 120 public KeystoreHandle next() { 121 return (KeystoreHandle) getIterator().next(); 122 } 123 124 127 private KeystoreHandle getKeystoreFromLocation(String location){ 128 129 InputStream in = null; 130 char[] buff = new char[4096]; 131 132 133 int indexOf$ = location.indexOf("${"); int indexOfCurly = location.indexOf('}',indexOf$); 135 if (indexOf$!=-1 && indexOfCurly!=-1){ 136 String prop = System.getProperty(location.substring(indexOf$+2,indexOfCurly)); 137 String location2 = location.substring(0,indexOf$); 138 location2 += prop; 139 location2 += location.substring(indexOfCurly+1); 140 location = location2; 141 } 142 143 144 try { 145 URL url = new URL (location); 146 in = ConnectionFactory.get(url).getInputStream(); 147 Reader reader = new InputStreamReader (in); 148 int result = reader.read(buff); 149 StringBuffer contentBuff = new StringBuffer (); 150 while (result!=-1){ 151 contentBuff.append(buff,0,result); 152 result = reader.read(buff); 153 } 154 155 if (contentBuff.length()>0){ 156 String content = new String (contentBuff); 157 int indexOfKeystore = content.indexOf("keystore"); if (indexOfKeystore != -1){ 159 int indexOfSemiColumn = content.indexOf(';',indexOfKeystore); 160 return getKeystoreFromString(content.substring(indexOfKeystore,indexOfSemiColumn),url); 161 } 162 } 163 } catch (MalformedURLException e){ 164 log(e); 165 } catch (IOException e){ 166 log(e); 169 } finally { 170 if (in!=null){ 171 try { 172 in.close(); 173 } catch (IOException e){} 174 } 175 } 176 return null; 177 } 178 179 182 private KeystoreHandle getKeystoreFromString(String content,URL rootURL){ 183 KeystoreHandle handle = null; 184 String 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 url = null; 198 try { 199 url = new URL (content.substring(indexOfSpace,secondSpace)); 200 } catch (MalformedURLException e){ 201 log(e); 202 try { 204 url = new URL (rootURL,content.substring(indexOfSpace,secondSpace)); 205 } catch (MalformedURLException 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 e){ 217 UpdateCore.warn("Cannot retrieve a KeyStore",e); } 219 } 220 | Popular Tags |