forked from verhas/License3j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.html
181 lines (143 loc) · 13.2 KB
/
sample.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
| Generated by Apache Maven Doxia at Sep 2, 2012
| Rendered using Apache Maven Fluido Skin 1.2.1
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>License3j - Sample use of license3j</title>
<link rel="stylesheet" href="./css/apache-maven-fluido.min.css" />
<link rel="stylesheet" href="./css/site.css" />
<link rel="stylesheet" href="./css/print.css" media="print" />
<script type="text/javascript" src="./js/apache-maven-fluido.min.js"></script>
<meta name="Date-Revision-yyyymmdd" content="20120902" />
<meta http-equiv="Content-Language" content="en" />
</head>
<body class="topBarDisabled">
<div class="container-fluid">
<div id="banner">
<div class="pull-left">
<a href="http://www.verhas.com/" id="bannerLeft">
<img src="http://www.verhas.com/images/logo.jpg" alt="License3j"/>
</a>
</div>
<div class="pull-right"> </div>
<div class="clear"><hr/></div>
</div>
<div id="breadcrumbs">
<ul class="breadcrumb">
<li id="publishDate">Last Published: 2012-09-02</li>
<li class="divider">|</li> <li id="projectVersion">Version: 1.0.4</li>
</ul>
</div>
<div class="row-fluid">
<div id="leftColumn" class="span3">
<div class="well sidebar-nav">
<h3>Verhas & Verhas Software Craftsmen</h3>
<ul>
<li class="none">
<a href="http://www.verhas.com" class="externalLink" title="Company Home page">Company Home page</a>
</li>
</ul>
<h3>License3j Application Documentation</h3>
<ul>
<li class="none">
<a href="home.html" title="Home Page">Home Page</a>
</li>
<li class="none">
<a href="apidocs/index.html" title="Api Reference">Api Reference</a>
</li>
</ul>
<h3>Project Documentation</h3>
<ul>
<li class="collapsed">
<a href="project-info.html" title="Project Information">Project Information</a>
</li>
<li class="collapsed">
<a href="project-reports.html" title="Project Reports">Project Reports</a>
</li>
</ul>
<hr class="divider" />
<div id="poweredBy">
<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<div class="g-plusone" data-href="http://nexus.sonatype.org/oss-repository-hosting.html/license3j" data-size="tall" ></div>
<div class="clear"></div>
<div class="clear"></div>
<div class="clear"></div>
<a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
<img class="poweredBy" alt="Built by Maven" src="./images/logos/maven-feather.png" />
</a>
</div>
</div>
</div>
<div id="bodyColumn" class="span9" >
<div class="section"><h2>Sample use of license3j<a name="Sample_use_of_license3j"></a></h2><p>This document describes via a simple example how to use license3j in your application to check the validity of a license. We assume that you are familiar up to some extent with public key cryptography and you read the introduction of license3j as well as the <a href="./tuto.html">document</a> that describes how to create key rings and how to encode license.</p><p>When the program is loaded you should load the encoded license programmatically (this is the only point where you can not use GPG and this is where you get license3j handy).</p><p>License3j can use external key ring file that is installed along with the Java file or a key ring file installed into the JAR file (as a resource) or you can programmatically load the license string into the java code by any means you want to program.</p><p>The example will show you the code of Groowiki license checking. Note that this part of groowiki is not open source. This is in the edition part and even exposing the code that checks the license do not decrease the license enforcement capabilities. The code presented here may not be the last version of Groowiki, nevertheless it still works as a demonstration example. Also note that some of the code (checking for <tt>null</tt> arguments, catching exceptions) was removed to ease readability of the examples.</p><p>The class that checks the license of Groowiki has a field:</p><div><pre> import com.verhas.licensor.License;
. . .
private License lic = null;</pre></div><p>The public method <tt>checkLicense()</tt> uses this field:</p><div><pre> 1. public void checkLicense() {
2. String licenseFileName = config.getString("edition.license.file");
3. lic = new License();
4. lic.loadKeyRingFromResource("pubring.gpg", digest);
5. lic.setLicenseEncodedFromFile(licenseFileName);
6. checkDateAndVersionValidity();
7. edition = lic.getFeature("edition");
8. }</pre></div><p>The line 2 gets the license file name. Line 3 generates a new license. This class is provided by license3j. It should be used to load the key ring and to set the encoded license file. Loading the key ring the call to method <tt>loadKeyRingFromResource()</tt> has two arguments.</p><div><pre> 4. lic.loadKeyRingFromResource("pubring.gpg", digest);</pre></div><ul><li>the name of the resource file that contains the key ring</li><li>the digest of the key ring</li></ul><p>The key ring is a binary file. It is usually named <tt>pubring.gpg</tt> in your key management. You copy this file into the Java project so that it will be available as a resource and thus <tt>loadKeyRingFromResource()</tt> can load it. (Never copy your secring into your java project!) To load this safely you have to provide a second argument, a <tt>byte []</tt> byte array that should contain the digest of the key ring. To get this array you can use <tt>license3j.bat</tt>. If you type</p><div><pre> license3j decode --license-file=lic.txt --keyring-file=pubring.gpg</pre></div><p>it will not only print the decoded open form of the license but also the digest of the key ring file in a format that can be copy/paste put into your Java code:</p><div><pre> ---KEY RING DIGEST START
byte [] digest = new byte[] {
(byte)0x83,
(byte)0x41, (byte)0xD1, (byte)0x19, (byte)0x92, (byte)0x7B, (byte)0xC0, (byte)0x91, (byte)0x8D,
(byte)0x46, (byte)0x99, (byte)0xFF, (byte)0x29, (byte)0x24, (byte)0x26, (byte)0x22, (byte)0x80,
(byte)0x0F, (byte)0x89, (byte)0xEB, (byte)0x1C, (byte)0x03, (byte)0x10, (byte)0x23, (byte)0x3A,
(byte)0x54, (byte)0xA3, (byte)0x8C, (byte)0x4A, (byte)0x61, (byte)0x61, (byte)0xB3,
};
---KEY RING DIGEST END</pre></div><p>If you are debugging, not serious or for some other reason do not want this extra security check been performed then you can pass <tt>null</tt> as second argument to method <tt>loadKeyRingFromResource()</tt>. For more information why you need to include your digest into the code and pass it to the key ring loading have a look at the page <a href="./fraud.html">./fraud.html</a>.</p><p>If you look at the API you can see that there are other versions of this method, namely the overloaded <tt>loadKeyRing()</tt> with different parameters that you can use to load the key ring from a file. All these methods check the digest of the file or bypass the check if you provide <tt>null</tt> instead of the digest.</p><p>The line 5 is used to load the license file itself:</p><div><pre> 5. lic.setLicenseEncodedFromFile(licenseFileName);</pre></div><p>The license file is encoded but this is a text file to ease the transfer via email. For example a license file may look like this:</p><div><pre> -----BEGIN PGP MESSAGE-----
Version: BCPG v1.43
owJ4nJvAy8zAxFh5I1Td/rnvDMbTBzyTxHMyk1PzilPdMnNS/RJzU3U90/Pyi1JT
vIrl5ujqKuRkFpdk5qUrFBTlF6QWlWSmFivo6vJyAdnF+Xm2AaklqUUKYalFGYnF
vFypKZklmUDRxOICoIqM/LxUW21jMw1jA01LYwMzCwNToJLcxMwc2wKQNocysDa9
5PxcXi4gUZCYV2lbVlaczMuVWFAAdFUi2LD0ovz88szsTF6uotSc1MTiVF2gvmKQ
jKGegZ4BL1dZYk5mim5KYkmqrZGBgaWugYWusQFCNYZEZnFxKYqwua6RJS9XJ5Mu
CwMjE4M4KxPI6wIy7kCLw4EWu6Tm5nunVjJwcQrAAu5WswDD5C2TS0pyrr0OatXi
Vvi2iElDPGXa/FCts/ZBZ1+IWzwyl46y+fbn3YJZ921CX2/vNVi9QNn6xkQLgxUK
Tf3JudM/3tvKteBKsMKdk66nJgV9qd5xJmSNb2Js/Nbpx9fEche3WHzsOWYU+twk
5lHGt55Y+cPzlSZOcPecHBrrdfO7p1HH7Xo+pv+emWZ319r/WXQ1SfRIpaLPfb0+
Viul7lbuWV+e3aox/L9vmxf3AauXB11sLje+PHNv7aUv28Xf9C9wvrfsq/Zt9vZD
N3iiW07p529leNfJaG6z+tikN1p2E/czSSYl/p8f/8JEhnnWxbldUX0sa2/cVWFU
EOCyYGTezbjrXNvLJ1Z9vR/l2N2zZ7YkXF0pJH9yT2hAqLa/ZVX/pv2XHn4sWsLn
mriBSd6367JhaP4JJ00L69ZH/Tf2zFa7ti5ajX0ti8u9jvf/Jm680FrW8vjLxsdv
lL/sjNtQtGTlb79gprQE960WCftNXgntXlf0P9Flxt6Jttvsi2Y9aD1y+JIP2/V3
edWOJyYVerf/vPmu+IjuMduyH/88Yr84P/iu8a7v2medirP95tqmM9+fXHRx4WVl
Zc6FZ5/e+RdYz7FnW8DMGjOT5uNyN//vPlDyWsP6ek719d1HFuxwCQu64Xhx022h
Q245ZglvTodtk71xkqOr12WzbAtHn6wOQ+vlZImw3U4bFVedbH1Y77M+2vTBJeO1
TgBy3m93
=DOIM
-----END PGP MESSAGE-----</pre></div><p>The name of this file has to be passed to the method <tt>setLicenseEncodedFromFile()</tt> but again there are many variations of this method you can use to move the license information into the memory of JVM. If you consult the JavaDoc API you will see that you can specify the license string itself as an argument to method <tt>setLicenseEncoded()</tt> or load the license string from a resource or from an input stream. At the end all these methods do the same: the license text will be loaded into <tt>License</tt> object and it will be decoded. If the file can not be found or the license can not be decoded then the method will throw some exception. This can be <tt>IOException</tt> or some exception from the underlying crypto API.</p><p>The line 6 checks the date and the validity of the license. This is a code specific tot he application and is not provided by the license3j library. You have total freedom to check the validity of a license. You can use dates, like issue date, validity date, validity end date, user count and so on. They are license <i>features</i> that you can get via the method <tt>getFeature</tt> providing the key to the feature, like in the line 7:</p><div><pre> 6. checkDateAndVersionValidity();
7. edition = lic.getFeature("edition");</pre></div><p>As a reference and to futher help your education we present here an abbreviated version of the method <tt>checkDateAndVersionValidity()</tt>:</p><div><pre> 1. protected void checkDateAndVersionValidity() {
2. String issueDate = lic.getFeature("issue-date");
3. String today = getTodayString();
4. LicenseDate todayLD = new LicenseDate(today);
5. if (!todayLD.isLaterThan(issueDate)) {
6. throw new IllegalArgumentException(
7. "Issue date is too late, probably tampered system time");
8. }
9. String validDate = lic.getFeature("valid-date");
10. if (validDate != null) {
11. LicenseDate validDateLD = new LicenseDate(validDate);
12. if (todayLD.isLaterThan(validDateLD)) {
13. throw new IllegalArgumentException("License expired.");
14. }
15. }
. . .
16. }</pre></div><p>The class <tt>LicenseDate</tt> is a proprietary class of Groowiki that provides date comparision for dates provided in the format <tt>YYYY-MM-DD</tt> which is exclusively used for Groowiki licenses.</p><p>More or less this is all that license3j provides and that you should use. The details of license handling above features are up to other packages.</p><p>You may be interested in <a href="./macaddr-binding.html">how to bind</a> a license to the Ethernet address.</p></div>
</div>
</div>
<hr/>
<footer>
<div class="container-fluid">
<div class="row span16">Copyright © 2012.
All Rights Reserved.
</div>
</div>
</footer>
</body>
</html>