package net.curisit.securis; import java.io.File; import java.io.IOException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.Signature; import java.security.SignatureException; import java.security.spec.InvalidKeySpecException; import java.text.MessageFormat; import java.util.Date; import java.util.Map; import net.curisit.securis.beans.LicenseBean; import net.curisit.securis.beans.RequestBean; import org.apache.commons.net.util.Base64; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * License generator and signer * * @author roberto */ public class LicenseGenerator { private static final Logger log = LoggerFactory.getLogger(LicenseGenerator.class); private static LicenseGenerator singleton = new LicenseGenerator(); private LicenseGenerator() { } public static LicenseGenerator getInstance() { return singleton; } /** * Generate a license bean with the specified data * * @param hw * @param customerCode * - e.g: "BP" * @param maxInstances * @param maxUsers * @param maxTimeThreshold * Max time between synchronizations expressed in days * @return * @throws SeCurisException */ public LicenseBean generateLicense(RequestBean req, Map metadata, Date expirationDate, String licenseCode) throws SeCurisException { log.info(MessageFormat.format("Generating license: MAC: {0}, Customer code: {1}, AppCode: {2}", req.getMacAddresses(), req.getCustomerCode(), req.getAppCode())); LicenseBean license = new LicenseBean(req); license.setLicenseCode(licenseCode); license.setExpirationDate(expirationDate); license.setMetadata(metadata); sign(license); return license; } /** * TODO: This method should be removed from client code. * * @param licBean * @return * @throws NoSuchAlgorithmException * @throws IOException * @throws InvalidKeySpecException * @throws InvalidKeyException * @throws SignatureException */ public String sign(LicenseBean licBean) throws SeCurisException { SignatureHelper sh = SignatureHelper.getInstance(); Signature signature; try { signature = Signature.getInstance(SignatureHelper.SIGNATURE_GENERATION_ALGORITHM); signature.initSign(sh.generatePrivateKey(new File("/Users/cproberto/Documents/wsPython/doky/tests/privkey.pkcs8"))); sh.prepareSignature(signature, licBean); byte[] signatureData = signature.sign(); licBean.setSignature(Base64.encodeBase64String(signatureData)); return licBean.getSignature(); } catch (NoSuchAlgorithmException e) { log.error("Error signing license for " + licBean, e); } catch (InvalidKeyException e) { log.error("Error signing license for " + licBean, e); } catch (InvalidKeySpecException e) { log.error("Error signing license for " + licBean, e); } catch (IOException e) { log.error("Error signing license for " + licBean, e); } catch (SignatureException e) { log.error("Error signing license for " + licBean, e); } throw new SeCurisException("License could not be generated"); } public static void main(String[] args) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException, InvalidKeyException, SignatureException { System.out.print("os.arch: " + System.getProperty("os.arch") + " " + System.getProperty("os.name")); } }