1 11 package org.eclipse.update.internal.security; 12 13 import java.security.*; 14 import java.security.cert.*; 15 import java.security.cert.Certificate ; 16 import java.text.DateFormat ; 17 import java.util.ArrayList ; 18 import java.util.Date ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 22 import org.eclipse.osgi.util.NLS; 23 import org.eclipse.update.core.*; 24 import org.eclipse.update.internal.core.Messages; 25 26 29 public class JarVerificationResult implements IVerificationResult { 30 31 32 private int resultCode; 33 private int verificationCode; 34 private Exception resultException; 35 private List 36 certificates; 37 private CertificatePair[] rootCertificates; 38 private CertificatePair foundCertificate; 40 private String signerInfo; 41 private String verifierInfo; 42 private ContentReference contentReference; 43 private IFeature feature; 44 private boolean featureVerification; 45 private boolean alreadySeen; 46 47 public JarVerificationResult() { 48 } 49 50 53 public int getResultCode() { 54 return resultCode; 55 } 56 57 60 public Exception getVerificationException() { 61 return resultException; 62 } 63 64 67 public void setResultCode(int newResultCode) { 68 resultCode = newResultCode; 69 } 70 71 74 public void setResultException(Exception newResultException) { 75 resultException = newResultException; 76 } 77 78 81 public int getVerificationCode() { 82 return verificationCode; 83 } 84 85 88 public void setVerificationCode(int verificationCode) { 89 this.verificationCode = verificationCode; 90 } 91 92 96 public void addCertificates(Certificate [] certs) { 97 if (certificates == null) 98 certificates = new ArrayList (); 99 certificates.add(certs); 100 rootCertificates = null; 101 } 102 103 111 public CertificatePair[] getRootCertificates() { 112 if (rootCertificates == null) { 113 rootCertificates = new CertificatePair[0]; 114 List rootCertificatesList = new ArrayList (); 115 if (certificates != null && certificates.size() > 0) { 116 Iterator iter = certificates.iterator(); 117 while (iter.hasNext()) { 118 119 Certificate [] certs = (Certificate []) iter.next(); 120 if (certs != null && certs.length > 0) { 121 122 CertificatePair pair = new CertificatePair(); 123 pair.setIssuer(certs[0]); 124 125 for (int i = 0; i < certs.length - 1; i++) { 126 X509Certificate x509certRoot = (X509Certificate) certs[i]; 127 X509Certificate x509certIssuer = (X509Certificate) certs[i+1]; 128 if (!x509certRoot.getIssuerDN().equals(x509certIssuer.getSubjectDN())) { 129 pair.setRoot(x509certRoot); 130 if (!rootCertificatesList.contains(pair)) { 131 rootCertificatesList.add(pair); 132 } 133 pair = new CertificatePair(); 134 pair.setIssuer(x509certIssuer); 135 } 136 } 137 138 if (pair != null) { 140 pair.setRoot(certs[certs.length - 1]); 141 if (!rootCertificatesList.contains(pair)) { 142 rootCertificatesList.add(pair); 143 } 144 } 145 } 146 } 147 148 } 149 150 if (rootCertificatesList.size() > 0) { 151 rootCertificates = new CertificatePair[rootCertificatesList.size()]; 152 rootCertificatesList.toArray(rootCertificates); 153 } 154 } 155 return rootCertificates; 156 } 157 158 161 private CertificatePair getFoundCertificate() { 162 return foundCertificate; 163 } 164 165 168 public void setFoundCertificate(CertificatePair foundCertificate) { 169 this.foundCertificate = foundCertificate; 170 } 171 172 173 176 private void initializeCertificates(){ 177 X509Certificate certRoot = null; 178 X509Certificate certIssuer = null; 179 CertificatePair trustedCertificate; 180 if (getFoundCertificate() == null) { 181 CertificatePair[] certs = getRootCertificates(); 182 if (certs.length == 0) 183 return; 184 trustedCertificate = certs[0]; 185 } else { 186 trustedCertificate = getFoundCertificate(); 187 } 188 certRoot = (X509Certificate) trustedCertificate.getRoot(); 189 certIssuer = (X509Certificate) trustedCertificate.getIssuer(); 190 191 StringBuffer strb = new StringBuffer (); 192 strb.append(issuerString(certIssuer.getSubjectDN())); 193 strb.append("\r\n"); strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String [] { dateString(certIssuer.getNotBefore()), dateString(certIssuer.getNotAfter()) }))); 195 strb.append(checkValidity(certIssuer)); 196 signerInfo = strb.toString(); 197 if (certIssuer != null && !certIssuer.equals(certRoot)) { 198 strb = new StringBuffer (); 199 strb.append(issuerString(certIssuer.getIssuerDN())); 200 strb.append("\r\n"); strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String [] { dateString(certRoot.getNotBefore()), dateString(certRoot.getNotAfter()) }))); 202 strb.append(checkValidity(certRoot)); 203 verifierInfo = strb.toString(); 204 } 205 206 } 207 208 211 private String checkValidity(X509Certificate cert) { 212 213 try { 214 cert.checkValidity(); 215 } catch (CertificateExpiredException e) { 216 return ("\r\n" + Messages.JarVerificationResult_ExpiredCertificate); } catch (CertificateNotYetValidException e) { 218 return ("\r\n" + Messages.JarVerificationResult_CertificateNotYetValid); } 220 return ("\r\n" + Messages.JarVerificationResult_CertificateValid); } 222 223 226 private String issuerString(Principal principal) { 227 return principal.toString(); 244 } 245 246 249 private String dateString(Date date) { 250 return DateFormat.getDateInstance().format(date); 251 } 252 253 256 public String getSignerInfo() { 257 if (signerInfo==null) initializeCertificates(); 258 return signerInfo; 259 } 260 261 264 public String getVerifierInfo() { 265 if (signerInfo==null) initializeCertificates(); 266 return verifierInfo; 267 } 268 269 272 public ContentReference getContentReference() { 273 return contentReference; 274 } 275 276 279 public void setContentReference(ContentReference ref) { 280 this.contentReference = ref; 281 } 282 283 284 287 public IFeature getFeature() { 288 return feature; 289 } 290 291 294 public void setFeature(IFeature feature) { 295 this.feature = feature; 296 } 297 298 301 public String getText() { 302 return null; 303 } 304 305 306 309 public boolean isFeatureVerification() { 310 return featureVerification; 311 } 312 313 316 public void isFeatureVerification(boolean featureVerification) { 317 this.featureVerification = featureVerification; 318 } 319 320 323 public boolean alreadySeen() { 324 return alreadySeen; 325 } 326 327 330 public boolean alreadySeen(boolean seen) { 331 return this.alreadySeen = seen; 332 } 333 334 } 335 | Popular Tags |