KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > security > license > License


1 /*
2  * Created on Sep 14, 2003
3  *
4  * To change the template for this generated file go to
5  * Window>Preferences>Java>Code Generation>Code and Comments
6  */

7 package org.jahia.security.license;
8
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.security.KeyStore JavaDoc;
12 import java.security.PrivateKey JavaDoc;
13 import java.security.PublicKey JavaDoc;
14 import java.security.Signature JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.Properties JavaDoc;
19
20 import org.apache.commons.codec.binary.Base64;
21 import org.jahia.utils.xml.XmlWriter;
22 import org.jahia.resourcebundle.ResourceMessage;
23
24 /**
25  * @author loom
26  *
27  * To change the template for this generated type comment go to
28  * Window>Preferences>Java>Code Generation>Code and Comments
29  */

30 public class License {
31
32     private static org.apache.log4j.Logger logger =
33         org.apache.log4j.Logger.getLogger(License.class);
34
35     private String JavaDoc certAlias;
36     private String JavaDoc signatureString;
37     private String JavaDoc componentName;
38     private String JavaDoc licensee;
39     private ArrayList JavaDoc limits;
40     private Properties JavaDoc properties;
41
42     /**
43      * @param componentName
44      * @param licensee
45      * @param signatureString
46      * @param certAlias
47      * @param limits
48      * @param properties
49      */

50     public License(
51         String JavaDoc componentName,
52         String JavaDoc licensee,
53         String JavaDoc signatureString,
54         String JavaDoc certAlias,
55         ArrayList JavaDoc limits,
56         Properties JavaDoc properties) {
57         this.componentName = componentName;
58         this.licensee = licensee;
59         this.signatureString = signatureString;
60         this.certAlias = certAlias;
61         this.limits = limits;
62         this.properties = properties;
63     }
64
65     /**
66      * @return
67      */

68     public String JavaDoc getSignatureString() {
69         return signatureString;
70     }
71
72     /**
73      * @return
74      */

75     public String JavaDoc getComponentName() {
76         return componentName;
77     }
78
79     /**
80      * @return
81      */

82     public String JavaDoc getLicensee() {
83         return licensee;
84     }
85
86     /**
87      * @return
88      */

89     public ArrayList JavaDoc getLimits() {
90         return limits;
91     }
92
93     /**
94      * @return
95      */

96     public String JavaDoc getCertAlias() {
97         return certAlias;
98     }
99
100     public boolean verifySignature(
101         InputStream JavaDoc keystoreIn,
102         String JavaDoc keystorePassword) {
103
104         // now let's verify the signature...
105

106         try {
107             KeyStore JavaDoc ks = KeyStore.getInstance("JKS");
108             ks.load(keystoreIn, keystorePassword.toCharArray()
109             /* keystore password */
110             );
111             java.security.cert.Certificate JavaDoc cert =
112                 ks.getCertificate(certAlias);
113             PublicKey JavaDoc publicKey = cert.getPublicKey();
114
115             Base64 base64 = new Base64();
116             byte[] base64SigToVerify = signatureString.getBytes("ISO-8859-1");
117             byte[] sigToVerify = base64.decode(base64SigToVerify);
118
119             Signature JavaDoc sig = Signature.getInstance("SHA1withDSA");
120             sig.initVerify(publicKey);
121
122             byte[] buffer = toSignatureData().getBytes("ISO-8859-1");
123             logger.debug("buffer length=" + buffer.length);
124
125             sig.update(buffer, 0, buffer.length);
126
127             return sig.verify(sigToVerify);
128         } catch (Throwable JavaDoc t) {
129             logger.error("Error while verifying signature:", t);
130         }
131         return false;
132     }
133
134     public void updateSignature(
135         InputStream JavaDoc keystoreIn,
136         String JavaDoc keystorePassword,
137         String JavaDoc privateKeyAlias,
138         String JavaDoc privateKeyPassword,
139         String JavaDoc certAlias) {
140
141         this.certAlias = certAlias;
142
143         try {
144             KeyStore JavaDoc ks = KeyStore.getInstance("JKS");
145             ks.load(keystoreIn, keystorePassword.toCharArray());
146
147             PrivateKey JavaDoc privateKey =
148                 (PrivateKey JavaDoc) ks.getKey(
149                     privateKeyAlias,
150                     privateKeyPassword.toCharArray());
151
152             Signature JavaDoc dsa = Signature.getInstance("SHA1withDSA", "SUN");
153
154             dsa.initSign(privateKey);
155
156             byte[] buffer = toSignatureData().getBytes("ISO-8859-1");
157             logger.debug("buffer length=" + buffer.length);
158
159             dsa.update(buffer, 0, buffer.length);
160             byte[] realSig = dsa.sign();
161
162             Base64 base64 = new Base64();
163
164             byte[] base64Signature = base64.encode(realSig);
165             signatureString = new String JavaDoc(base64Signature, "ISO-8859-1");
166
167         } catch (Throwable JavaDoc t) {
168             logger.error("Error while updating signature:", t);
169         }
170
171     }
172
173     public void toXML(XmlWriter xmlWriter) throws IOException JavaDoc {
174         xmlWriter.writeEntity("license");
175         xmlWriter.writeAttribute("component", componentName);
176         xmlWriter.writeAttribute("licensee", licensee);
177         xmlWriter.writeAttribute("signature", signatureString);
178         xmlWriter.writeAttribute("certAlias", certAlias);
179         Iterator JavaDoc limitIter = limits.iterator();
180         while (limitIter.hasNext()) {
181             Limit curLimit = (Limit) limitIter.next();
182             curLimit.toXML(xmlWriter);
183         }
184         Enumeration JavaDoc propertyNameEnum = properties.propertyNames();
185         while (propertyNameEnum.hasMoreElements()) {
186             String JavaDoc curPropertyName = (String JavaDoc) propertyNameEnum.nextElement();
187             xmlWriter.writeEntity("property");
188             xmlWriter.writeAttribute("name", curPropertyName);
189             xmlWriter.writeAttribute("value", properties.getProperty(curPropertyName));
190             xmlWriter.endEntity();
191         }
192         xmlWriter.endEntity();
193     }
194
195     public boolean checkLimits() {
196         Iterator JavaDoc limitIter = limits.iterator();
197         while (limitIter.hasNext()) {
198             Limit curLimit = (Limit) limitIter.next();
199             if (!curLimit.check()) {
200                 return false;
201             }
202         }
203         return true;
204     }
205
206     public ResourceMessage[] getErrorMessages() {
207
208         ArrayList JavaDoc errorMessages = new ArrayList JavaDoc();
209         Iterator JavaDoc limitIter = limits.iterator();
210         while (limitIter.hasNext()) {
211             Limit curLimit = (Limit) limitIter.next();
212             ResourceMessage curErrorMessage = curLimit.getErrorMessage();
213             if (curErrorMessage != null) {
214                 errorMessages.add(curErrorMessage);
215             }
216         }
217         return (ResourceMessage[]) errorMessages.toArray(new ResourceMessage[errorMessages.size()]);
218
219     }
220
221     public Limit getLimit(String JavaDoc name) {
222         Iterator JavaDoc limitIter = limits.iterator();
223         while (limitIter.hasNext()) {
224             Limit curLimit = (Limit) limitIter.next();
225             if (curLimit.getName().equals(name)) {
226                 return curLimit;
227             }
228         }
229         return null;
230     }
231
232     protected String JavaDoc toSignatureData() {
233         StringBuffer JavaDoc signDataBuf = new StringBuffer JavaDoc();
234         signDataBuf.append(componentName);
235         signDataBuf.append("\n");
236         signDataBuf.append(licensee);
237         signDataBuf.append("\n");
238         signDataBuf.append(certAlias);
239         signDataBuf.append("\n");
240         Iterator JavaDoc limitIter = limits.iterator();
241         while (limitIter.hasNext()) {
242             Limit curLimit = (Limit) limitIter.next();
243             signDataBuf.append(curLimit.toSignatureData());
244             signDataBuf.append("\n");
245         }
246         Enumeration JavaDoc propertyNameEnum = properties.propertyNames();
247         while (propertyNameEnum.hasMoreElements()) {
248             String JavaDoc curPropertyName = (String JavaDoc) propertyNameEnum.nextElement();
249             signDataBuf.append(curPropertyName);
250             signDataBuf.append("\n");
251             signDataBuf.append(properties.getProperty(curPropertyName));
252             signDataBuf.append("\n");
253         }
254         return signDataBuf.toString();
255     }
256   public void setLimits(ArrayList JavaDoc limits) {
257     this.limits = limits;
258   }
259   public void setProperties(Properties JavaDoc properties) {
260     this.properties = properties;
261   }
262
263 }
264
Popular Tags