1 11 package org.eclipse.update.internal.verifier; 12 13 import java.io.File ; 14 import java.io.IOException ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.jar.JarFile ; 19 import java.util.zip.ZipException ; 20 21 import org.eclipse.core.runtime.CoreException; 22 import org.eclipse.core.runtime.IProgressMonitor; 23 import org.eclipse.osgi.internal.provisional.verifier.*; 24 import org.eclipse.osgi.util.NLS; 25 import org.eclipse.update.core.*; 26 import org.eclipse.update.internal.core.Messages; 27 import org.eclipse.update.internal.core.UpdateCore; 28 29 36 37 public class CertVerifier extends Verifier { 38 39 private CertVerificationResult result; 40 private boolean acceptUnsignedFiles; 41 private IProgressMonitor monitor; 42 private File jarFile; 43 private CertificateVerifierFactory factory; 44 private List trustedCertificates; 45 46 49 public CertVerifier(CertificateVerifierFactory factory) { 50 this.factory = factory; 51 initialize(); 52 } 53 54 55 58 private void initialize() { 59 result = null; 60 acceptUnsignedFiles = false; 61 } 62 63 66 private void init(IFeature feature, ContentReference contentRef) throws CoreException { 67 jarFile = null; 68 if (contentRef instanceof JarContentReference) { 69 JarContentReference jarReference = (JarContentReference) contentRef; 70 try { 71 jarFile = jarReference.asFile(); 72 if (UpdateCore.DEBUG && UpdateCore.DEBUG_SHOW_INSTALL) 73 UpdateCore.debug("Attempting to read JAR file:"+jarFile); 75 if (!jarFile.exists()) throw new IOException (); 77 JarFile jar = new JarFile (jarFile); 78 if (jar !=null){ 79 try { 80 jar.close(); 81 } catch (IOException ex) { 82 } 84 } 85 } catch (ZipException e){ 86 throw Utilities.newCoreException(NLS.bind(Messages.JarVerifier_InvalidJar, (new String [] { jarReference.toString() })), e); 87 } catch (IOException e) { 88 throw Utilities.newCoreException(NLS.bind(Messages.JarVerifier_UnableToAccessJar, (new String [] { jarReference.toString() })), e); 89 } 90 } 91 92 result = new CertVerificationResult(); 93 result.setVerificationCode(IVerificationResult.UNKNOWN_ERROR); 94 result.setResultException(null); 95 result.setFeature(feature); 96 result.setContentReference(contentRef); 97 } 98 99 102 private void setMonitor(IProgressMonitor newMonitor) { 103 monitor = newMonitor; 104 } 105 106 109 public IVerificationResult verify( 110 IFeature feature, 111 ContentReference reference, 112 boolean isFeatureVerification, 113 InstallMonitor monitor) 114 throws CoreException { 115 116 if (reference == null) 117 return result; 118 119 if (getParent() != null) { 121 IVerificationResult vr = 122 getParent().verify(feature, reference, isFeatureVerification, monitor); 123 if (vr.getVerificationCode() != IVerificationResult.TYPE_ENTRY_UNRECOGNIZED) 124 return vr; 125 } 126 127 setMonitor(monitor); 129 init(feature, reference); 130 result.isFeatureVerification(isFeatureVerification); 131 132 if (jarFile!=null) { 133 result = verify(jarFile.getAbsolutePath(), reference.getIdentifier()); 134 } else { 135 result.setVerificationCode(IVerificationResult.TYPE_ENTRY_UNRECOGNIZED); 136 } 137 138 return result; 139 } 140 141 144 private CertVerificationResult verify(String file, String identifier) { 145 146 try { 147 CertificateVerifier verifier = factory.getVerifier(new File (file)); 148 verifyIntegrity(verifier, identifier); 150 151 result.alreadySeen(alreadyValidated()); 153 154 if (result.getVerificationCode() 156 == IVerificationResult.TYPE_ENTRY_NOT_SIGNED) { 157 acceptUnsignedFiles = true; 158 } 159 160 } catch (Exception e) { 161 result.setVerificationCode(IVerificationResult.UNKNOWN_ERROR); 162 result.setResultException(e); 163 } 164 165 if (monitor != null) { 166 monitor.worked(1); 167 if (monitor.isCanceled()) { 168 result.setVerificationCode(IVerificationResult.VERIFICATION_CANCELLED); 169 } 170 } 171 172 return result; 173 } 174 175 178 private void verifyIntegrity(CertificateVerifier verifier, String identifier) { 179 try { 180 if (verifier.isSigned()) { 181 if (verifier.verifyContent().length == 0) { 183 CertificateChain[] roots = verifier.getChains(); 184 result.setChains(roots); 185 for (int i = 0; i < roots.length; i++) 186 if (roots[i].isTrusted()) { 187 result.setFoundChain(roots[i]); 188 result.setVerificationCode(IVerificationResult.TYPE_ENTRY_SIGNED_RECOGNIZED); 189 break; 190 } 191 if (result.getVerificationCode() != IVerificationResult.TYPE_ENTRY_SIGNED_RECOGNIZED) 192 result.setVerificationCode(IVerificationResult.TYPE_ENTRY_SIGNED_UNRECOGNIZED); 193 } else 194 result.setVerificationCode(IVerificationResult.TYPE_ENTRY_CORRUPTED); 195 } else { 196 result.setVerificationCode(IVerificationResult.TYPE_ENTRY_NOT_SIGNED); 197 return; 198 } 199 } catch (Exception e) { 200 result.setVerificationCode(IVerificationResult.UNKNOWN_ERROR); 201 result.setResultException(e); 202 } 203 } 204 205 208 private boolean alreadyValidated() { 209 int verifyCode = result.getVerificationCode(); 210 if (verifyCode == IVerificationResult.TYPE_ENTRY_NOT_SIGNED) 211 return (acceptUnsignedFiles); 212 if (verifyCode == IVerificationResult.UNKNOWN_ERROR) 213 return false; 214 if (result.getchains() != null) { Iterator iter = getTrustedCertificates().iterator(); 216 CertificateChain[] roots = result.getchains(); 217 218 while (iter.hasNext()) { 220 CertificateChain chain = (CertificateChain) iter.next(); 221 for (int i = 0; i < roots.length; i++) 222 if (chain.equals(roots[i])) 223 return true; 224 } 225 226 for (int i = 0; i < roots.length; i++) { 228 addTrustedCertificate(roots[i]); 229 } 230 } 231 232 return false; 233 } 234 235 238 private void addTrustedCertificate(CertificateChain chain) { 239 if (trustedCertificates == null) 240 trustedCertificates = new ArrayList (); 241 if (chain != null) 242 trustedCertificates.add(chain); 243 } 244 245 248 private List getTrustedCertificates() { 249 if (trustedCertificates == null) 250 trustedCertificates = new ArrayList (); 251 return trustedCertificates; 252 } 253 254 257 public void setParent(IVerifier parentVerifier) { 258 super.setParent(parentVerifier); 259 initialize(); 260 } 261 262 } 263 | Popular Tags |