1 11 12 package org.eclipse.osgi.internal.verifier; 13 14 import java.io.*; 15 import java.net.*; 16 import java.security.*; 17 import java.security.cert.Certificate ; 18 import java.util.*; 19 import org.eclipse.osgi.framework.internal.core.FrameworkProperties; 20 import org.eclipse.osgi.framework.log.FrameworkLogEntry; 21 22 26 public class KeyStores { 27 30 private static final String JAVA_POLICY_URL = "policy.url."; 34 private static final String DEFAULT_KEYSTORE_TYPE = "keystore.type"; 38 private List keyStores; 39 40 43 public KeyStores() { 44 super(); 45 initializeDefaultKeyStores(); 46 } 47 48 private void processKeyStore(String urlSpec, String type, URL rootURL) { 49 if (type == null) 50 type = KeyStore.getDefaultType(); 51 InputStream in = null; 52 try { 53 URL url; 54 try { 55 url = new URL(urlSpec); 56 } catch (MalformedURLException mue) { 57 url = new URL(rootURL, urlSpec); 58 } 59 KeyStore ks = KeyStore.getInstance(type); 60 try { 61 in = url.openStream(); 62 } catch (IOException ioe) { 63 } 65 if (in != null) { 66 ks.load(in, null); 67 keyStores.add(ks); 68 } 69 } catch (Exception e) { 70 SignedBundleHook.log(e.getMessage(), FrameworkLogEntry.WARNING, e); 71 } finally { 72 if (in != null) 73 try { 74 in.close(); 75 } catch (IOException e){ 76 } 78 } 79 } 80 81 85 private void initializeDefaultKeyStores() { 86 keyStores = new ArrayList(5); 87 String defaultType = Security.getProperty(DEFAULT_KEYSTORE_TYPE); 89 String urlSpec = "file:" + FrameworkProperties.getProperty("java.home") + File.separator + "lib" + File.separator + "security" + File.separator + "cacerts"; processKeyStore(urlSpec, defaultType, null); 91 92 urlSpec = "file:" + FrameworkProperties.getProperty("user.home") + File.separator + ".keystore"; processKeyStore(urlSpec, defaultType, null); 95 96 urlSpec = FrameworkProperties.getProperty("osgi.framework.keystore"); if (urlSpec != null) 99 processKeyStore(urlSpec, defaultType, null); 100 101 int index = 1; 103 String java_policy = Security.getProperty(JAVA_POLICY_URL + index); 104 while (java_policy != null) { 105 processKeystoreFromLocation(java_policy); 108 index++; 109 java_policy = Security.getProperty(JAVA_POLICY_URL + index); 110 } 111 } 112 113 116 private void processKeystoreFromLocation(String location) { 117 InputStream in = null; 118 char[] buff = new char[4096]; 119 int indexOf$ = location.indexOf("${"); int indexOfCurly = location.indexOf('}', indexOf$); 121 if (indexOf$ != -1 && indexOfCurly != -1) { 122 String prop = FrameworkProperties.getProperty(location.substring(indexOf$ + 2, indexOfCurly)); 123 String location2 = location.substring(0, indexOf$); 124 location2 += prop; 125 location2 += location.substring(indexOfCurly + 1); 126 location = location2; 127 } 128 try { 129 URL url = new URL(location); 130 in = url.openStream(); 132 Reader reader = new InputStreamReader(in); 133 int result = reader.read(buff); 134 StringBuffer contentBuff = new StringBuffer (); 135 while (result != -1) { 136 contentBuff.append(buff, 0, result); 137 result = reader.read(buff); 138 } 139 if (contentBuff.length() > 0) { 140 String content = new String (contentBuff.toString()); 141 int indexOfKeystore = content.indexOf("keystore"); if (indexOfKeystore != -1) { 143 int indexOfSemiColumn = content.indexOf(';', indexOfKeystore); 144 processKeystoreFromString(content.substring(indexOfKeystore, indexOfSemiColumn), url); 145 return; 146 } 147 } 148 } catch (MalformedURLException e) { 149 SignedBundleHook.log(e.getMessage(), FrameworkLogEntry.WARNING, e); 150 } catch (IOException e) { 151 } finally { 153 if (in != null) { 154 try { 155 in.close(); 156 } catch (IOException e) { 157 } 159 } 160 } 161 } 162 163 166 private void processKeystoreFromString(String content, URL rootURL) { 167 String keyStoreType = null; 168 int indexOfSpace = content.indexOf(' '); 169 if (indexOfSpace == -1) 170 return; 171 int secondSpace = content.lastIndexOf(','); 172 if (secondSpace == -1) { 173 secondSpace = content.length(); 174 } else { 175 keyStoreType = content.substring(secondSpace + 1, content.length()).trim(); 176 } 177 processKeyStore(content.substring(indexOfSpace, secondSpace), keyStoreType, rootURL); 178 } 179 180 public boolean isTrusted(Certificate cert) { 181 Iterator it = keyStores.iterator(); 182 while (it.hasNext()) { 183 KeyStore ks = (KeyStore) it.next(); 184 try { 185 if (ks.getCertificateAlias(cert) != null) { 186 return true; 187 } 188 } catch (KeyStoreException e) { 189 SignedBundleHook.log(e.getMessage(), FrameworkLogEntry.WARNING, e); 190 } 191 } 192 return false; 193 } 194 } | Popular Tags |