1 package org.sapia.util.license; 2 3 import java.io.IOException ; 4 import java.io.Serializable ; 5 import java.security.PrivateKey ; 6 import java.security.PublicKey ; 7 import java.security.Signature ; 8 9 28 public class LicenseRecord implements Serializable { 29 30 static final long serialVersionUID = 1L; 31 32 private RecData _data; 33 private Object _vendorId; 34 private transient boolean _init, _valid; 35 private transient PublicKey _key; 36 private boolean _activated; 37 private long _id; 38 39 LicenseRecord(Object vendorId, License license, byte[] sig, byte[] publicKey, long id){ 40 _vendorId = vendorId; 41 _data = new RecData(sig, publicKey, license); 42 _id = id; 43 } 44 45 62 public boolean isValid(Object context, SecurityFactory fac) throws IOException , Exception { 63 if(!_init){ 64 Signature algo = fac.newSignature("SHA/DSA"); 65 _key = fac.generatePublic(_data._key); 66 algo.initVerify(_key); 67 byte[] toVerify = _data._license.getBytes(); 68 algo.update(toVerify); 69 _valid = algo.verify(_data._sig); 70 _init = true; 71 } 72 if(!_valid){ 73 return false; 74 } 75 return _valid = _data._license.isValid(context); 76 77 } 78 79 85 public void activate(Object context){ 86 if(!_activated){ 87 _data._license.activate(context); 88 _activated = true; 89 } 90 } 91 92 106 public synchronized boolean update(LicenseRecord newRecord, Object context){ 107 if(_id >= newRecord._id){ 108 return false; 109 } 110 if(newRecord._vendorId == null || !newRecord._vendorId.equals(_vendorId)){ 111 return false; 112 } 113 _data = newRecord._data; 114 _id = newRecord._id; 115 _init = false; 116 _data._license.activate(context); 117 return true; 118 } 119 120 134 public static LicenseRecord newInstance(Object vendorId, 135 License license, 136 PublicKey pub, 137 PrivateKey priv, 138 SecurityFactory factory, 139 LicenseIdFactory idFactory) throws IOException , Exception { 140 Signature algo = factory.newSignature("SHA/DSA"); 141 algo.initSign(priv); 142 byte[] toSign = license.getBytes(); 143 algo.update(toSign); 144 byte[] sig = algo.sign(); 145 return new LicenseRecord(vendorId, license, sig, pub.getEncoded(), idFactory.newInstance()); 146 } 147 148 synchronized void registerNewLicense(Object vendorId, 149 License license, 150 PublicKey pub, 151 PrivateKey priv, 152 SecurityFactory factory, 153 LicenseIdFactory idFactory) throws IOException , Exception { 154 LicenseRecord rec = LicenseRecord.newInstance(vendorId, license, pub, priv, factory, idFactory); 155 if(_key == null){ 156 _key = factory.generatePublic(_data._key); 157 } 158 if(!_key.equals(pub)){ 159 throw new IllegalArgumentException ("A license must be updated with existing public/private keys"); 160 } 161 _data = rec._data; 162 _id = rec._id; 163 _init = false; 164 } 165 166 static final class RecData implements Serializable { 167 168 static final long serialVersionUID = 1L; 169 170 private byte[] _sig; 171 private byte[] _key; 172 private License _license; 173 174 public RecData(byte[] sig, byte[] key, License license){ 175 _sig = sig; 176 _key = key; 177 _license = license; 178 } 179 180 } 181 } 182 183 | Popular Tags |