1 7 package org.jahia.security.license; 8 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.security.KeyStore ; 12 import java.security.PrivateKey ; 13 import java.security.PublicKey ; 14 import java.security.Signature ; 15 import java.util.ArrayList ; 16 import java.util.Enumeration ; 17 import java.util.Iterator ; 18 import java.util.Properties ; 19 20 import org.apache.commons.codec.binary.Base64; 21 import org.jahia.utils.xml.XmlWriter; 22 import org.jahia.resourcebundle.ResourceMessage; 23 24 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 certAlias; 36 private String signatureString; 37 private String componentName; 38 private String licensee; 39 private ArrayList limits; 40 private Properties properties; 41 42 50 public License( 51 String componentName, 52 String licensee, 53 String signatureString, 54 String certAlias, 55 ArrayList limits, 56 Properties 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 68 public String getSignatureString() { 69 return signatureString; 70 } 71 72 75 public String getComponentName() { 76 return componentName; 77 } 78 79 82 public String getLicensee() { 83 return licensee; 84 } 85 86 89 public ArrayList getLimits() { 90 return limits; 91 } 92 93 96 public String getCertAlias() { 97 return certAlias; 98 } 99 100 public boolean verifySignature( 101 InputStream keystoreIn, 102 String keystorePassword) { 103 104 106 try { 107 KeyStore ks = KeyStore.getInstance("JKS"); 108 ks.load(keystoreIn, keystorePassword.toCharArray() 109 110 ); 111 java.security.cert.Certificate cert = 112 ks.getCertificate(certAlias); 113 PublicKey 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 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 t) { 129 logger.error("Error while verifying signature:", t); 130 } 131 return false; 132 } 133 134 public void updateSignature( 135 InputStream keystoreIn, 136 String keystorePassword, 137 String privateKeyAlias, 138 String privateKeyPassword, 139 String certAlias) { 140 141 this.certAlias = certAlias; 142 143 try { 144 KeyStore ks = KeyStore.getInstance("JKS"); 145 ks.load(keystoreIn, keystorePassword.toCharArray()); 146 147 PrivateKey privateKey = 148 (PrivateKey ) ks.getKey( 149 privateKeyAlias, 150 privateKeyPassword.toCharArray()); 151 152 Signature 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 (base64Signature, "ISO-8859-1"); 166 167 } catch (Throwable t) { 168 logger.error("Error while updating signature:", t); 169 } 170 171 } 172 173 public void toXML(XmlWriter xmlWriter) throws IOException { 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 limitIter = limits.iterator(); 180 while (limitIter.hasNext()) { 181 Limit curLimit = (Limit) limitIter.next(); 182 curLimit.toXML(xmlWriter); 183 } 184 Enumeration propertyNameEnum = properties.propertyNames(); 185 while (propertyNameEnum.hasMoreElements()) { 186 String curPropertyName = (String ) 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 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 errorMessages = new ArrayList (); 209 Iterator 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 name) { 222 Iterator 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 toSignatureData() { 233 StringBuffer signDataBuf = new StringBuffer (); 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 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 propertyNameEnum = properties.propertyNames(); 247 while (propertyNameEnum.hasMoreElements()) { 248 String curPropertyName = (String ) 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 limits) { 257 this.limits = limits; 258 } 259 public void setProperties(Properties properties) { 260 this.properties = properties; 261 } 262 263 } 264 | Popular Tags |