Class License
- java.lang.Object
-
- javax0.license3j.License
-
public class License extends java.lang.Object
A license describes the rights that a certain user has. The rights are represented byFeature
s. Each feature has a name, type and a value. The license is essentially the set of features.As examples features can be license expiration date and time, number of users allowed to use the software, name of rights and so on.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
License.Create
Inner class containing factory methods to create a license object from various sources.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(byte[] signature)
Add the signature to the license.Feature
add(Feature feature)
Add a feature to the license.java.util.UUID
fingerprint()
Get the fingerprint of a license.Feature
get(java.lang.String name)
Get a feature of a given name from the license ornull
if there is no feature for the name in the license.java.util.UUID
getLicenseId()
Get the license identifier.byte[]
getSignature()
Get the signature of the license.boolean
isExpired()
Checks the expiration date of the license and returnstrue
if the license has expired.boolean
isOK(byte[] key)
SeeisOK(PublicKey)
.boolean
isOK(java.security.PublicKey key)
Returns true if the license is signed and the authenticity of the signature can be checked successfully using the key.byte[]
serialized()
Get the license serialized (not standard Java serialized!!!) as a byte array.void
setExpiry(java.util.Date expiryDate)
Set the expiration date of the license.java.util.UUID
setLicenseId()
Generates a new license id.void
setLicenseId(java.util.UUID licenseId)
Set the UUID of a license.void
sign(java.security.PrivateKey key, java.lang.String digest)
Sign the license.java.lang.String
toString()
Converts a license to string.byte[]
unsigned()
Get the license as a byte[] without the signature key.
-
-
-
Constructor Detail
-
License
public License()
-
License
protected License(License license)
-
-
Method Detail
-
get
public Feature get(java.lang.String name)
Get a feature of a given name from the license ornull
if there is no feature for the name in the license.- Parameters:
name
- the name of the feature we want to retrieve- Returns:
- the feature object
-
isExpired
public boolean isExpired()
Checks the expiration date of the license and returnstrue
if the license has expired.The expiration date is stored in the license feature
expiryDate
. A license is expired if the current date is after the specifiedexpiryDate
. At the given date (ms precision) the license is still valid.The method does not check that the license is properly signed or not. That has to be checked using a separate call to the underlying license.
- Returns:
true
if the license has expired.
-
setExpiry
public void setExpiry(java.util.Date expiryDate)
Set the expiration date of the license.- Parameters:
expiryDate
- the date when the license expires
-
sign
public void sign(java.security.PrivateKey key, java.lang.String digest) throws java.security.NoSuchAlgorithmException, javax.crypto.NoSuchPaddingException, java.security.InvalidKeyException, javax.crypto.BadPaddingException, javax.crypto.IllegalBlockSizeException
Sign the license.The license is signed the following way:
- Add the digest algorithm string to the license as a feature. The feature name is
signatureDigest
(name is defined in the constantDIGEST_KEY
in this class). - The license is converted to binary format
- A digest is created from the binary license using the message digest algorithm named by the
digest
parameter - The digest is encrypted using the key (which also has the information about the algorithm).
- The encrypted digest is added to the license as a new
BINARY
feature as signature.
- Parameters:
key
- the private key to be used to create the signaturedigest
- the name of the digest algorithm- Throws:
java.security.NoSuchAlgorithmException
- this exception comes from the underlying encryption libraryjavax.crypto.NoSuchPaddingException
- this exception comes from the underlying encryption libraryjava.security.InvalidKeyException
- this exception comes from the underlying encryption libraryjavax.crypto.BadPaddingException
- this exception comes from the underlying encryption libraryjavax.crypto.IllegalBlockSizeException
- this exception comes from the underlying encryption library
- Add the digest algorithm string to the license as a feature. The feature name is
-
isOK
public boolean isOK(byte[] key)
SeeisOK(PublicKey)
.- Parameters:
key
- serialized encryption key to check the authenticity of the license signature- Returns:
- see
isOK(PublicKey)
-
isOK
public boolean isOK(java.security.PublicKey key)
Returns true if the license is signed and the authenticity of the signature can be checked successfully using the key.- Parameters:
key
- encryption key to check the authenticity of the license signature- Returns:
true
if the license was properly signed and is intact. In any other cases it returnsfalse
.
-
add
public Feature add(Feature feature)
Add a feature to the license. Note that adding a feature to a license renders the license signature invalid. Adding the feature does not remove the signature features though.The method throws exception in case the feature is the license signature and the type is not
BINARY
.- Parameters:
feature
- is added to the license- Returns:
- the previous feature of the same name but presumably different type and value or
null
in case there was no previous feature in the license of the same name.
-
toString
public java.lang.String toString()
Converts a license to string. The string contains all the features and also the order of the features are guaranteed to be always the same.Every feature is formatted as
name:TYPE=value
when the value is multiline then it is converted to be multiline. Multiline strings are represented as usually in unix, with the HERE_STRING. The value in this case starts right after the
=
character with<<
characters and a string to the end of the line that does not appear as a single line inside the string value. This value signals the end of the string and all other lines before it is part of the multiline string. For example:feature name : STRING =<<END this is a multi-line string END
In this example the string
END
singals the end of the string and the lines between are part of the strings. A feature string is also converted to multi-line representation if it happens to start with the characters<<
.The generated string can be used as argument to
License.Create.from(String)
.- Overrides:
toString
in classjava.lang.Object
- Returns:
- the converted license as a string
-
setLicenseId
public java.util.UUID setLicenseId()
Generates a new license id.This ID is also stored in the license thus there is no need to create a feature and add it to the license.
Generating UUID can be handy when you want to identify each license individually. For example you want to store revocation information about each license. The url to check the revocation may contain the
${licenseId}
place holder that will be replaced by the actual uuid stored in the license.- Returns:
- the generated identifier.
-
getLicenseId
public java.util.UUID getLicenseId()
Get the license identifier. The identifier of the license is a random UUID (128 bit random value) that can optionally be set and can be used to identify the license. This Id can be used as a reference to the license in databases, URLs. An example use it to upload a simple file to a publicly reachable server with the name of the license UUID and that it can be retrieved via a URL containing the UUID. The licensed program downloads the file (presumably zero length) and in case the response code is 200 OK then it assumes that the license is OK. If the server is not reachable or for some other reason it cannot reach the file it may assume that this is a technical glitch and go on working for a while, however if the response is a definitive 404 it means that the file was removes that means the license was revoked.- Returns:
- the UUID former identifier of the license or null in case the license does not contain an ID.
-
fingerprint
public java.util.UUID fingerprint()
Get the fingerprint of a license. The fingerprint is a 128bit value calculated from the license itself using the MD5 algorithm. The fingerprint is represented as a UUID. The fingerprint is never stored in the license as a feature. If the fingerprint is stored in the license as a feature then the fingerprint of the license changes so the stored fingerprint will not be the fingerprint of the license any more.The calculation of the fingerprint ignores the signature of the license as well as the feature that stores the name of the signature digest algorithm (usually "SHA-512"). This is to ensure that even if you change the signature on the license using a different digest algorithm: fingerprint will not change. The fingerprint is relevant to the core content of the license.
Since the fingerprint is calculated from the binary representation of the license using the MD5 algorithm it is likely that each license will have different fingerprint. This fingerprint can be used to identify licenses similarly like the license id (see
setLicenseId(UUID)
,getLicenseId()
). The fingerprint can be used even when the license does not have random ID stored in the license.- Returns:
- the calculated fingerprint of the license
-
setLicenseId
public void setLicenseId(java.util.UUID licenseId)
Set the UUID of a license. Note that this UUID can be generated calling the methodsetLicenseId()
, which method automatically calls this method setting the generated UUID to be the identifier of the license.- Parameters:
licenseId
- the uuid that was generated somewhere.
-
serialized
public byte[] serialized()
Get the license serialized (not standard Java serialized!!!) as a byte array. This method is used to save the license inBINARY
format but not to sign the license. The returned byte array contains all the features including the signature of the license.- Returns:
- the license in binary format as a byte array
-
unsigned
public byte[] unsigned()
Get the license as a byte[] without the signature key. This byte array is used to create the signature of the license. Obviously, the signature itself cannot be part of the signed part of the license.- Returns:
- the byte array containing the license without the signature. Note that the message digest algorithm used during the signature creation of the license and stored as a feature in the license is also signed.
-
add
public void add(byte[] signature)
Add the signature to the license.- Parameters:
signature
- the signature itself
-
getSignature
public byte[] getSignature()
Get the signature of the license. The signature of a license is stored in the license as aBINARY
feature. This method retrieves the feature and then it retrieves the value of the feature and returns the raw value.- Returns:
- the electronic signature attached to the license
-
-