Roberto Sánchez
2014-02-21 8cc95fdfbe8146af8d5d4bcac7f7b9e3e2eb95c6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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 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 <roberto.sanchez@curisit.net>
 */
public class LicenseGenerator {
   @SuppressWarnings("unused")
   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, int maxUsers, Date expirationDate) 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.setExpirationDate(expirationDate);
       license.setMaxUsers(maxUsers);
       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"));
   }
}