Source code: com/sourcetap/license/LicenseVerifier.java
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 SourceTap - www.sourcetap.com
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21 * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24 package com.sourcetap.license;
25
26 import java.io.*;
27 import java.security.PublicKey;
28
29 /**
30 * Verify a License Key using the public key to decrypt it
31 *
32 * @author Steve Fowler
33 * @version $Revision$
34 */
35 public class LicenseVerifier {
36
37 private Boolean valid = new Boolean(false);
38
39 public LicenseVerifier(){}
40
41 /**
42 * decrypt the encoded license key and return the corresponding License Object
43 * @param licenseEncoded am encrypted license key as generated by the Registration Server
44 * @param publicKeyFile the name of the file containing the public key
45 * @return the License Object corresponding to the supplied licenseKey
46 * @throws LicenseException
47 */
48 public static License validateLicense(String licenseEncoded, String publicKeyFile)
49 throws LicenseException
50 {
51 String licenseKey = "";
52 String signature = "";
53
54 try
55 {
56 EncryptionUtil signer = new EncryptionUtil();
57
58 // the encoded license key consists of the base64 encoded license key and the signature
59 // generated by the private key, separated by a #
60 String tokens[] = licenseEncoded.split("#");
61 if ( tokens.length != 2 )
62 throw new LicenseException("LicenseKey is invalid");
63 licenseKey = Base64Coder.decode( tokens[0] );
64 signature = tokens[1];
65
66 if ( !signer.verify( licenseKey, signature, publicKeyFile ) )
67 throw new LicenseException("License cannot be verified.");
68
69 License lic = new License(licenseKey);
70
71 return lic;
72 }
73 catch ( Exception e)
74 {
75 e.printStackTrace();
76 throw new LicenseException( "LicenseKey is invalid: " + e.getMessage() + ":");
77
78 }
79 }
80
81 private static String readSignature(String signatureFile) throws IOException {
82 FileInputStream sigfis = new FileInputStream(signatureFile);
83 byte[] sigToVerify = new byte[sigfis.available()];
84 sigfis.read(sigToVerify);
85 sigfis.close();
86 return new String(sigToVerify);
87 }
88
89 /**
90 * read a Public Key from a file
91 * @param URI the name of the file containing the public key
92 * @return the public key
93 * @throws LicenseException
94 */
95 public static PublicKey readPublicKey( String URI ) throws LicenseException
96 {
97 try
98 {
99 EncryptionUtil keyUtil = new EncryptionUtil();
100 PublicKey publicKey = keyUtil.readPublicKey( URI );
101 return publicKey;
102 }
103 catch (Exception e)
104 {
105 throw new LicenseException("Unable to read Key File:" + e.getMessage());
106 }
107 }
108
109 }