/core/src/main/java/org/gridgain/grid/kernal/controllers/license/GridLicenseUtil.java

https://github.com/simonetripodi/gridgain · Java · 233 lines · 117 code · 31 blank · 85 comment · 19 complexity · eb1aa691fd5d4cdff8a4a5ae52bbfbb7 MD5 · raw file

  1. // Copyright (C) GridGain Systems, Inc. Licensed under GPLv3, http://www.gnu.org/licenses/gpl.html
  2. /* _________ _____ __________________ _____
  3. * __ ____/___________(_)______ /__ ____/______ ____(_)_______
  4. * _ / __ __ ___/__ / _ __ / _ / __ _ __ `/__ / __ __ \
  5. * / /_/ / _ / _ / / /_/ / / /_/ / / /_/ / _ / _ / / /
  6. * \____/ /_/ /_/ \_,__/ \____/ \__,_/ /_/ /_/ /_/
  7. */
  8. package org.gridgain.grid.kernal.controllers.license;
  9. import org.gridgain.grid.*;
  10. import org.gridgain.grid.typedef.*;
  11. import org.gridgain.grid.typedef.internal.*;
  12. import java.io.*;
  13. import java.security.*;
  14. /**
  15. * GridGain license util.
  16. *
  17. * @author 2005-2011 Copyright (C) GridGain Systems, Inc.
  18. * @version 3.5.0c.22092011
  19. */
  20. public class GridLicenseUtil {
  21. /** Date format pattern. */
  22. private static final String DATE_PTRN = "MM/dd/yyyy";
  23. /** Signature algorithm. */
  24. private static final String SIGN_ALG = "SHA1withDSA";
  25. /** Security provider. */
  26. private static final String PRV = "SUN";
  27. /**
  28. * Ensures singleton.
  29. */
  30. private GridLicenseUtil() {
  31. // No-op.
  32. }
  33. /**
  34. * Gets license version 1 data for sign.
  35. *
  36. * @param lic GridGain license version 1.
  37. * @return Data for sign.
  38. */
  39. public static byte[] getDataV1ForSign(GridLicenseV1 lic) {
  40. assert lic != null;
  41. try {
  42. return new SB()
  43. .a(lic.getVersion())
  44. .a(lic.getId())
  45. .a(lic.getIssueDate() != null ? U.format(lic.getIssueDate(), DATE_PTRN) : "")
  46. .a(lic.getIssueOrganization())
  47. .a(lic.getUserOrganization())
  48. .a(lic.getUserWww())
  49. .a(lic.getUserEmail())
  50. .a(lic.getType())
  51. .a(lic.getExpireDate() != null ? U.format(lic.getExpireDate(), DATE_PTRN) : "")
  52. .a(lic.getMeteringKey1())
  53. .a(lic.getMeteringKey2())
  54. .a(lic.getMaxCpus())
  55. .a(lic.getMaxNodes())
  56. .toString()
  57. .getBytes("UTF-8");
  58. }
  59. catch (UnsupportedEncodingException e) {
  60. throw new GridRuntimeException(e);
  61. }
  62. }
  63. /**
  64. * Gets license version 2 data for sign.
  65. *
  66. * @param lic GridGain license version 2.
  67. * @return Data for sign.
  68. */
  69. public static byte[] getDataV2ForSign(GridLicenseV2 lic) {
  70. assert lic != null;
  71. try {
  72. return new SB()
  73. .a(lic.getVersion())
  74. .a(lic.getId())
  75. .a(lic.getIssueDate() != null ? U.format(lic.getIssueDate(), DATE_PTRN) : "")
  76. .a(lic.getIssueOrganization())
  77. .a(lic.getUserOrganization())
  78. .a(lic.getUserWww())
  79. .a(lic.getUserEmail())
  80. .a(lic.getUserName())
  81. .a(lic.getLicenseNote())
  82. .a(lic.getVersionRegexp())
  83. .a(lic.getType())
  84. .a(lic.getExpireDate() != null ? U.format(lic.getExpireDate(), DATE_PTRN) : "")
  85. .a(lic.getMeteringKey1())
  86. .a(lic.getMeteringKey2())
  87. .a(lic.getMaxCpus())
  88. .a(lic.getMaxComputers())
  89. .a(lic.getMaxNodes())
  90. .a(lic.getMaxUpTime())
  91. .toString()
  92. .getBytes("UTF-8");
  93. }
  94. catch (UnsupportedEncodingException e) {
  95. throw new GridRuntimeException(e);
  96. }
  97. }
  98. /**
  99. * Verifies license version 1 signature.
  100. *
  101. * @param key Public key.
  102. * @param lic License version 1.
  103. * @return {@code true} if signature was verified, {@code false} - otherwise.
  104. * @throws GeneralSecurityException If any exception occurs while verifying.
  105. */
  106. public static boolean verifySignatureV1(PublicKey key, GridLicenseV1 lic) throws GeneralSecurityException {
  107. assert key != null;
  108. assert lic != null;
  109. byte[] data = getDataV1ForSign(lic);
  110. byte[] sign = U.hexString2ByteArray(lic.getSignature());
  111. return verifySignature(SIGN_ALG, PRV, key, data, sign);
  112. }
  113. /**
  114. * Verifies license version 2 signature.
  115. *
  116. * @param key Public key.
  117. * @param lic License version 2.
  118. * @return {@code true} if signature was verified, {@code false} - otherwise.
  119. * @throws GeneralSecurityException If any exception occurs while verifying.
  120. */
  121. public static boolean verifySignatureV2(PublicKey key, GridLicenseV2 lic) throws GeneralSecurityException {
  122. assert key != null;
  123. assert lic != null;
  124. byte[] data = getDataV2ForSign(lic);
  125. byte[] sign = U.hexString2ByteArray(lic.getSignature());
  126. return verifySignature(SIGN_ALG, PRV, key, data, sign);
  127. }
  128. /**
  129. * Verifies signature for data.
  130. *
  131. * @param alg Algorithm.
  132. * @param prv Provider.
  133. * @param key Public key.
  134. * @param data Data.
  135. * @param sign Signature.
  136. * @return {@code true} if signature was verified, {@code false} - otherwise.
  137. * @throws GeneralSecurityException If any exception occurs while verifying.
  138. */
  139. public static boolean verifySignature(String alg, String prv, PublicKey key, byte[] data, byte[] sign)
  140. throws GeneralSecurityException {
  141. assert !F.isEmpty(alg);
  142. assert !F.isEmpty(prv);
  143. assert key != null;
  144. assert data != null;
  145. assert sign != null;
  146. Signature sign0 = Signature.getInstance(alg, prv);
  147. sign0.initVerify(key);
  148. sign0.update(data);
  149. return sign0.verify(sign);
  150. }
  151. /**
  152. * Create signature for license version 1.
  153. *
  154. * @param key Private key.
  155. * @param lic License version 1.
  156. * @return Signature.
  157. * @throws GeneralSecurityException If any exception occurs while signature creation.
  158. */
  159. public static String createSignatureV1(PrivateKey key, GridLicenseV1 lic) throws GeneralSecurityException {
  160. assert key != null;
  161. assert lic != null;
  162. byte[] data = getDataV1ForSign(lic);
  163. byte[] sign = createSignature(SIGN_ALG, PRV, key, data);
  164. return U.byteArray2HexString(sign);
  165. }
  166. /**
  167. * Create signature for license version 2.
  168. *
  169. * @param key Private key.
  170. * @param lic License version 2.
  171. * @return Signature.
  172. * @throws GeneralSecurityException If any exception occurs while signature creation.
  173. */
  174. public static String createSignatureV2(PrivateKey key, GridLicenseV2 lic) throws GeneralSecurityException {
  175. assert key != null;
  176. assert lic != null;
  177. byte[] data = getDataV2ForSign(lic);
  178. byte[] sign = createSignature(SIGN_ALG, PRV, key, data);
  179. return U.byteArray2HexString(sign);
  180. }
  181. /**
  182. * Creates signature for data.
  183. *
  184. * @param alg Algorithm.
  185. * @param prv Provider.
  186. * @param key Private key.
  187. * @param data Data.
  188. * @return Signature.
  189. * @throws GeneralSecurityException If any exception occurs while signature creation.
  190. */
  191. public static byte[] createSignature(String alg, String prv, PrivateKey key, byte[] data)
  192. throws GeneralSecurityException {
  193. assert !F.isEmpty(alg);
  194. assert !F.isEmpty(prv);
  195. assert key != null;
  196. assert data != null;
  197. Signature sign = Signature.getInstance(alg, prv);
  198. sign.initSign(key);
  199. sign.update(data);
  200. return sign.sign();
  201. }
  202. }