KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > update > internal > verifier > CertVerificationResult


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.verifier;
12
13 import java.security.*;
14 import java.security.cert.*;
15 import java.text.DateFormat JavaDoc;
16 import java.util.Date JavaDoc;
17
18 import org.eclipse.osgi.internal.provisional.verifier.CertificateChain;
19 import org.eclipse.osgi.util.NLS;
20 import org.eclipse.update.core.*;
21 import org.eclipse.update.internal.core.Messages;
22
23 /**
24  * Result of the service
25  */

26 public class CertVerificationResult implements IVerificationResult {
27
28
29     private int resultCode;
30     private int verificationCode;
31     private Exception JavaDoc resultException;
32
33     private CertificateChain[] chains;
34     private CertificateChain foundChain; // certificate found in one keystore
35

36     private String JavaDoc signerInfo;
37     private String JavaDoc verifierInfo;
38     private ContentReference contentReference;
39     private IFeature feature;
40     private boolean featureVerification;
41     private boolean alreadySeen;
42
43     public CertVerificationResult() {
44     }
45     
46     /*
47      *
48      */

49     public int getResultCode() {
50         return resultCode;
51     }
52     
53     /*
54      *
55      */

56     public Exception JavaDoc getVerificationException() {
57         return resultException;
58     }
59     
60     /*
61      *
62      */

63     public void setResultCode(int newResultCode) {
64         resultCode = newResultCode;
65     }
66     
67     /*
68      *
69      */

70     public void setResultException(Exception JavaDoc newResultException) {
71         resultException = newResultException;
72     }
73     
74     /*
75      *
76      */

77     public int getVerificationCode() {
78         return verificationCode;
79     }
80
81     /*
82      *
83      */

84     public void setVerificationCode(int verificationCode) {
85         this.verificationCode = verificationCode;
86     }
87
88     void setChains(CertificateChain[] chains) {
89         this.chains = chains;
90     }
91
92     public CertificateChain[] getchains() {
93         return chains;
94     }
95
96     /*
97      *
98      */

99     private CertificateChain getFoundChain() {
100         return foundChain;
101     }
102
103     /*
104      *
105      */

106     public void setFoundChain(CertificateChain foundChain) {
107         this.foundChain = foundChain;
108     }
109
110
111     /*
112      * Initializes the signerInfo and the VerifierInfo from the Certificate Pair
113      */

114     private void initializeCertificates(){
115         X509Certificate certRoot = null;
116         X509Certificate certIssuer = null;
117         CertificateChain trustedChain;
118         if (getFoundChain() == null) {
119             CertificateChain[] certs = getchains();
120             if (certs.length == 0)
121                 return;
122             trustedChain = certs[0];
123         } else {
124             trustedChain = getFoundChain();
125         }
126         certRoot = (X509Certificate) trustedChain.getRoot();
127         certIssuer = (X509Certificate) trustedChain.getSigner();
128
129         StringBuffer JavaDoc strb = new StringBuffer JavaDoc();
130         strb.append(issuerString(certIssuer.getSubjectDN()));
131         strb.append("\r\n"); //$NON-NLS-1$
132
strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String JavaDoc[] { dateString(certIssuer.getNotBefore()), dateString(certIssuer.getNotAfter()) })));
133         strb.append(checkValidity(certIssuer));
134         signerInfo = strb.toString();
135         if (certIssuer != null && !certIssuer.equals(certRoot)) {
136             strb = new StringBuffer JavaDoc();
137             strb.append(issuerString(certIssuer.getIssuerDN()));
138             strb.append("\r\n"); //$NON-NLS-1$
139
strb.append(NLS.bind(Messages.JarVerificationResult_ValidBetween, (new String JavaDoc[] { dateString(certRoot.getNotBefore()), dateString(certRoot.getNotAfter()) })));
140             strb.append(checkValidity(certRoot));
141             verifierInfo = strb.toString();
142         }
143
144     }
145
146     /*
147      * Returns a String to show if the certificate is valid
148      */

149     private String JavaDoc checkValidity(X509Certificate cert) {
150
151         try {
152             cert.checkValidity();
153         } catch (CertificateExpiredException e) {
154             return ("\r\n" + Messages.JarVerificationResult_ExpiredCertificate); //$NON-NLS-1$
155
} catch (CertificateNotYetValidException e) {
156             return ("\r\n" + Messages.JarVerificationResult_CertificateNotYetValid); //$NON-NLS-1$
157
}
158         return ("\r\n" + Messages.JarVerificationResult_CertificateValid); //$NON-NLS-1$
159
}
160
161     /*
162      * Returns the label String from a X50name
163      */

164     private String JavaDoc issuerString(Principal principal) {
165 // 19902
166
// try {
167
// if (principal instanceof X500Name) {
168
// StringBuffer buf = new StringBuffer();
169
// X500Name name = (X500Name) principal;
170
// buf.append((name.getDNQualifier() != null) ? name.getDNQualifier() + ", " : "");
171
// buf.append(name.getCommonName());
172
// buf.append((name.getOrganizationalUnit() != null) ? ", " + name.getOrganizationalUnit() : "");
173
// buf.append((name.getOrganization() != null) ? ", " + name.getOrganization() : "");
174
// buf.append((name.getLocality() != null) ? ", " + name.getLocality() : "");
175
// buf.append((name.getCountry() != null) ? ", " + name.getCountry() : "");
176
// return new String(buf);
177
// }
178
// } catch (Exception e) {
179
// UpdateCore.warn("Error parsing X500 Certificate",e);
180
// }
181
return principal.toString();
182     }
183
184     /*
185      *
186      */

187     private String JavaDoc dateString(Date JavaDoc date) {
188         return DateFormat.getDateInstance().format(date);
189     }
190
191     /*
192      *
193      */

194     public String JavaDoc getSignerInfo() {
195         if (signerInfo==null) initializeCertificates();
196         return signerInfo;
197     }
198
199     /*
200      *
201      */

202     public String JavaDoc getVerifierInfo() {
203         if (signerInfo==null) initializeCertificates();
204         return verifierInfo;
205     }
206
207     /*
208      *
209      */

210     public ContentReference getContentReference() {
211         return contentReference;
212     }
213
214     /*
215      *
216      */

217     public void setContentReference(ContentReference ref) {
218         this.contentReference = ref;
219     }
220
221
222     /*
223      *
224      */

225     public IFeature getFeature() {
226         return feature;
227     }
228
229     /*
230      *
231      */

232     public void setFeature(IFeature feature) {
233         this.feature = feature;
234     }
235
236     /*
237      *
238      */

239     public String JavaDoc getText() {
240         return null;
241     }
242
243
244     /*
245      *
246      */

247     public boolean isFeatureVerification() {
248         return featureVerification;
249     }
250     
251     /*
252      *
253      */

254     public void isFeatureVerification(boolean featureVerification) {
255         this.featureVerification = featureVerification;
256     }
257
258     /*
259      *
260      */

261     public boolean alreadySeen() {
262         return alreadySeen;
263     }
264
265     /*
266      *
267      */

268     public boolean alreadySeen(boolean seen) {
269         return this.alreadySeen = seen;
270     }
271
272 }
273
Popular Tags