KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.security.*;
14 import java.security.cert.*;
15 import java.security.cert.Certificate JavaDoc;
16 import java.text.DateFormat JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Date JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21
22 import org.eclipse.osgi.util.NLS;
23 import org.eclipse.update.core.*;
24 import org.eclipse.update.internal.core.Messages;
25
26 /**
27  * Result of the service
28  */

29 public class JarVerificationResult implements IVerificationResult {
30
31
32     private int resultCode;
33     private int verificationCode;
34     private Exception JavaDoc resultException;
35     private List JavaDoc /*of Certificates[] */
36     certificates;
37     private CertificatePair[] rootCertificates;
38     private CertificatePair foundCertificate; // certificate found in one keystore
39

40     private String JavaDoc signerInfo;
41     private String JavaDoc verifierInfo;
42     private ContentReference contentReference;
43     private IFeature feature;
44     private boolean featureVerification;
45     private boolean alreadySeen;
46
47     public JarVerificationResult() {
48     }
49     
50     /*
51      *
52      */

53     public int getResultCode() {
54         return resultCode;
55     }
56     
57     /*
58      *
59      */

60     public Exception JavaDoc getVerificationException() {
61         return resultException;
62     }
63     
64     /*
65      *
66      */

67     public void setResultCode(int newResultCode) {
68         resultCode = newResultCode;
69     }
70     
71     /*
72      *
73      */

74     public void setResultException(Exception JavaDoc newResultException) {
75         resultException = newResultException;
76     }
77     
78     /*
79      *
80      */

81     public int getVerificationCode() {
82         return verificationCode;
83     }
84
85     /*
86      *
87      */

88     public void setVerificationCode(int verificationCode) {
89         this.verificationCode = verificationCode;
90     }
91
92     /*
93      * adds an array of Certificates to the list
94      * force recomputation of root cert
95      */

96     public void addCertificates(Certificate JavaDoc[] certs) {
97         if (certificates == null)
98             certificates = new ArrayList JavaDoc();
99         certificates.add(certs);
100         rootCertificates = null;
101     }
102
103     /*
104      * Returns the list of root certificates
105      * The list of certificates we received is an array of certificates
106      * we have to determine
107      * 1) how many chain do we have (a chain stops when verifier of a cert is
108      * not the signer of the next cert in the list
109      * 2) build a cert with the leaf signer and the root verifier for each chain
110      */

111     public CertificatePair[] getRootCertificates() {
112         if (rootCertificates == null) {
113             rootCertificates = new CertificatePair[0];
114             List JavaDoc rootCertificatesList = new ArrayList JavaDoc();
115             if (certificates != null && certificates.size() > 0) {
116                 Iterator JavaDoc iter = certificates.iterator();
117                 while (iter.hasNext()) {
118
119                     Certificate JavaDoc[] certs = (Certificate JavaDoc[]) 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                         // add the latest one
139
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     /*
159      *
160      */

161     private CertificatePair getFoundCertificate() {
162         return foundCertificate;
163     }
164
165     /*
166      *
167      */

168     public void setFoundCertificate(CertificatePair foundCertificate) {
169         this.foundCertificate = foundCertificate;
170     }
171
172
173     /*
174      * Initializes the signerInfo and the VerifierInfo from the Certificate Pair
175      */

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 JavaDoc strb = new StringBuffer JavaDoc();
192         strb.append(issuerString(certIssuer.getSubjectDN()));
193         strb.append("\r\n"); //$NON-NLS-1$
194
strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String JavaDoc[] { 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 JavaDoc();
199             strb.append(issuerString(certIssuer.getIssuerDN()));
200             strb.append("\r\n"); //$NON-NLS-1$
201
strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String JavaDoc[] { dateString(certRoot.getNotBefore()), dateString(certRoot.getNotAfter()) })));
202             strb.append(checkValidity(certRoot));
203             verifierInfo = strb.toString();
204         }
205
206     }
207
208     /*
209      * Returns a String to show if the certificate is valid
210      */

211     private String JavaDoc checkValidity(X509Certificate cert) {
212
213         try {
214             cert.checkValidity();
215         } catch (CertificateExpiredException e) {
216             return ("\r\n" + Messages.JarVerificationResult_ExpiredCertificate); //$NON-NLS-1$
217
} catch (CertificateNotYetValidException e) {
218             return ("\r\n" + Messages.JarVerificationResult_CertificateNotYetValid); //$NON-NLS-1$
219
}
220         return ("\r\n" + Messages.JarVerificationResult_CertificateValid); //$NON-NLS-1$
221
}
222
223     /*
224      * Returns the label String from a X50name
225      */

226     private String JavaDoc issuerString(Principal principal) {
227 // 19902
228
// try {
229
// if (principal instanceof X500Name) {
230
// StringBuffer buf = new StringBuffer();
231
// X500Name name = (X500Name) principal;
232
// buf.append((name.getDNQualifier() != null) ? name.getDNQualifier() + ", " : "");
233
// buf.append(name.getCommonName());
234
// buf.append((name.getOrganizationalUnit() != null) ? ", " + name.getOrganizationalUnit() : "");
235
// buf.append((name.getOrganization() != null) ? ", " + name.getOrganization() : "");
236
// buf.append((name.getLocality() != null) ? ", " + name.getLocality() : "");
237
// buf.append((name.getCountry() != null) ? ", " + name.getCountry() : "");
238
// return new String(buf);
239
// }
240
// } catch (Exception e) {
241
// UpdateCore.warn("Error parsing X500 Certificate",e);
242
// }
243
return principal.toString();
244     }
245
246     /*
247      *
248      */

249     private String JavaDoc dateString(Date JavaDoc date) {
250         return DateFormat.getDateInstance().format(date);
251     }
252
253     /*
254      *
255      */

256     public String JavaDoc getSignerInfo() {
257         if (signerInfo==null) initializeCertificates();
258         return signerInfo;
259     }
260
261     /*
262      *
263      */

264     public String JavaDoc getVerifierInfo() {
265         if (signerInfo==null) initializeCertificates();
266         return verifierInfo;
267     }
268
269     /*
270      *
271      */

272     public ContentReference getContentReference() {
273         return contentReference;
274     }
275
276     /*
277      *
278      */

279     public void setContentReference(ContentReference ref) {
280         this.contentReference = ref;
281     }
282
283
284     /*
285      *
286      */

287     public IFeature getFeature() {
288         return feature;
289     }
290
291     /*
292      *
293      */

294     public void setFeature(IFeature feature) {
295         this.feature = feature;
296     }
297
298     /*
299      *
300      */

301     public String JavaDoc getText() {
302         return null;
303     }
304
305
306     /*
307      *
308      */

309     public boolean isFeatureVerification() {
310         return featureVerification;
311     }
312     
313     /*
314      *
315      */

316     public void isFeatureVerification(boolean featureVerification) {
317         this.featureVerification = featureVerification;
318     }
319
320     /*
321      *
322      */

323     public boolean alreadySeen() {
324         return alreadySeen;
325     }
326
327     /*
328      *
329      */

330     public boolean alreadySeen(boolean seen) {
331         return this.alreadySeen = seen;
332     }
333
334 }
335
Popular Tags