TrueLicense XML 1.33

de.schlichtherle.xml
Class GenericCertificate

java.lang.Object
  extended by de.schlichtherle.xml.GenericCertificate
All Implemented Interfaces:
XMLConstants, Serializable

public final class GenericCertificate
extends Object
implements Serializable, XMLConstants

This non-visual JavaBean is a factory for authenticated runtime objects whose integrity cannot be compromised without being detected. The idea and the design of this class is inspired by both SignedObject and Certificate.

More specifically, a GenericCertificate contains an XML string encoded representation of an arbitrary object in the "encoded" property and a Base64 immutable string representation of the object's corresponding digital signature in the "signature" property. The selection of this representation form and the design of this class as a plain JavaBean allows its instances to be serialized using either this package's PersistenceService, JDK's XMLEncoder, or the vanilla ObjectOutputStream.

For an object to be successfully digitally signed, it must support serialization via JDK's XMLEncoder, for which this package provides the class PersistenceService. This easy-to-use class allows you to provide custom PersistenceDelegate instances for the serialisation of any classes which do not implement the JavaBean design pattern and are not supported by XMLEncoder as a default.

Whenever an instance of this GenericCertificate class is created, you can arbitrarily set and get its "encoded" and "signature" properties, allowing you to provide even custom deserialisation methods other than this class already provides via the aforementioned classes. However, once this instance is used to either sign or verify another object it gets locked, allowing subsequent read access to its properties only.

The underlying signing algorithm is designated by the Signature object passed to the sign and the verify methods.

A typical usage for signing is the following: <pre> GenericCertificate cert = new GenericCertificate(); Signature signingEngine = Signature.getInstance(algorithm, provider); try { cert.sign(myObject, signingKey, signingEngine); } catch (PropertyVetoException signingVetoed) { // ... } catch (PersistenceServiceException serialisationFailed) { // ... } catch (InvalidKeyException invalidKey) { // ... } catch (SignatureException signingEngineBroken) { // ... } </pre> A typical usage for verification is the following (having received GenericCertificate cert): <pre> Signature verificationEngine = Signature.getInstance(algorithm, provider); try { cert.verify(publicKey, verificationEngine)); } catch (PropertyVetoException verificationVetoed) { // ... } catch (InvalidKeyException invalidKey) { // ... } catch (SignatureException verificationEngineBroken) { // ... } catch (GenericCertificateException integrityCompromised) { // ... } Object myObject = cert.getContent(); </pre> Several points are worth noting:

Potential applications of GenericCertificate include:

This class is thread-safe.

Author:
Christian Schlichtherle
See Also:
Signature, SignedObject, Certificate, Serialized Form

Field Summary
private static String BASE64_CHARSET
           
private  String encoded
          Holds value of property encoded.
private  boolean locked
          Holds value of property locked - is not serializable!!!
private  PropertyChangeSupport propertyChangeSupport
          Utility field used by bound properties.
private static long serialVersionUID
           
private  String signature
          Holds value of property signature.
private static String SIGNATURE_ENCODING
           
private  String signatureAlgorithm
          Holds value of property signatureAlgorithm.
private  String signatureEncoding
          Holds value of property signatureEncoding.
private  VetoableChangeSupport vetoableChangeSupport
          Utility field used by constrained properties.
 
Fields inherited from interface de.schlichtherle.xml.XMLConstants
DEFAULT_BUFSIZE, XML_CHARSET
 
Constructor Summary
GenericCertificate()
          Constructs a new generic certificate.
GenericCertificate(GenericCertificate cert)
          Copy constructor for the given generic certificate.
 
Method Summary
private static boolean equals(Object a, Object b)
           
 Object getContent()
          Returns a clone of the certificate's content as it was signed or verified before.
 String getEncoded()
          The value of the property encoded.
 String getSignature()
          Getter for the property signature.
 String getSignatureAlgorithm()
          Getter for the property signatureAlgorithm.
 String getSignatureEncoding()
          Getter for the property signatureEncoding.
 boolean isLocked()
          Returns the value of the property locked.
 void setEncoded(String encoded)
          Setter for the bound property encoded.
 void setSignature(String signature)
          Setter for the bound property signature.
 void setSignatureAlgorithm(String signatureAlgorithm)
          Setter for the bound property signatureAlgorithm.
 void sign(Object content, PrivateKey signingKey, Signature signingEngine)
          Encodes and signs the given content in this certificate and locks it.
 void verify(PublicKey verificationKey, Signature verificationEngine)
          Verifies the digital signature of the encoded content in this certificate and locks it.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BASE64_CHARSET

private static final String BASE64_CHARSET
See Also:
Constant Field Values

encoded

private String encoded
Holds value of property encoded.


locked

private transient volatile boolean locked
Holds value of property locked - is not serializable!!!


propertyChangeSupport

private transient PropertyChangeSupport propertyChangeSupport
Utility field used by bound properties.


serialVersionUID

private static final long serialVersionUID
See Also:
Constant Field Values

signature

private String signature
Holds value of property signature.


SIGNATURE_ENCODING

private static final String SIGNATURE_ENCODING
See Also:
Constant Field Values

signatureAlgorithm

private String signatureAlgorithm
Holds value of property signatureAlgorithm.


signatureEncoding

private String signatureEncoding
Holds value of property signatureEncoding.


vetoableChangeSupport

private transient VetoableChangeSupport vetoableChangeSupport
Utility field used by constrained properties.

Constructor Detail

GenericCertificate

public GenericCertificate()
Constructs a new generic certificate.


GenericCertificate

public GenericCertificate(GenericCertificate cert)
Copy constructor for the given generic certificate. Note that the new certificate is unlocked and does not have any event listeners.

Parameters:
cert - the generic certificate to copy.
Method Detail

equals

private static boolean equals(Object a,
                              Object b)

getContent

public Object getContent()
                  throws GenericCertificateNotLockedException,
                         PersistenceServiceException
Returns a clone of the certificate's content as it was signed or verified before. You should save the returned object for later use as each call to this method is pretty expensive in terms of runtime and memory. This method may return null if this has been signed before.

Returns:
A clone of the certificate's content as it was signed or verified before.
Throws:
GenericCertificateNotLockedException - If no content has been signed or verified before. Note that this is ultimately a RuntimeException.
PersistenceServiceException - If the signed object cannot get reinstantiated from its XML representation for some reason. This may happen for example if the signed object was created by a more recent version of its class which contains additional properties which are not supported by earlier versions.

getEncoded

public String getEncoded()
The value of the property encoded. The default is null.

Returns:
The value of the property encoded.

getSignature

public String getSignature()
Getter for the property signature. The default is null.

Returns:
Value of property signature.

getSignatureAlgorithm

public String getSignatureAlgorithm()
Getter for the property signatureAlgorithm. The default is null.

Returns:
The signature algorithm.

getSignatureEncoding

public String getSignatureEncoding()
Getter for the property signatureEncoding. The default is null.

Returns:
The character encoding of the signature string.

isLocked

public boolean isLocked()
Returns the value of the property locked. If true, an object was successfully signed or verified before and a clone can be safely retrieved using getContent().

Returns:
The value of the property locked.

setEncoded

public void setEncoded(String encoded)
                throws GenericCertificateIsLockedException
Setter for the bound property encoded.

Parameters:
encoded - The new encoded representation of the signed object - may be null.
Throws:
GenericCertificateIsLockedException - If this certificate is already locked by signing or verifying it before. Note that this is actually a subclass of PropertyVetoException.

setSignature

public void setSignature(String signature)
                  throws GenericCertificateIsLockedException
Setter for the bound property signature.

Parameters:
signature - The signature encoded as a string - may be null.
Throws:
GenericCertificateIsLockedException - If this certificate is already locked by signing or verifying it before. Note that this is actually a subclass of PropertyVetoException.

setSignatureAlgorithm

public void setSignatureAlgorithm(String signatureAlgorithm)
                           throws GenericCertificateIsLockedException
Setter for the bound property signatureAlgorithm.

Parameters:
signatureAlgorithm - The string identifying the signature algorithm - may be null.
Throws:
GenericCertificateIsLockedException - If this certificate is already locked by signing or verifying it before. Note that this is actually a subclass of PropertyVetoException.

sign

public void sign(Object content,
                 PrivateKey signingKey,
                 Signature signingEngine)
          throws NullPointerException,
                 GenericCertificateIsLockedException,
                 PropertyVetoException,
                 PersistenceServiceException,
                 InvalidKeyException
Encodes and signs the given content in this certificate and locks it.

Please note the following:

Parameters:
content - The object to sign. This must either be a JavaBean or an instance of any other class which is supported by {@link PersistenceService} - maybe null.
signingKey - The private key for signing - may not be null.
signingEngine - The signature signing engine - may not be null.
Throws:
NullPointerException - If the preconditions for the parameters do not hold.
GenericCertificateIsLockedException - If this certificate is already locked by signing or verifying it before. Note that this is actually a subclass of PropertyVetoException.
PropertyVetoException - If locking the certifificate (and thus signing the object) is vetoed by any listener.
PersistenceServiceException - If the object cannot be serialised.
InvalidKeyException - If the verification key is invalid.

verify

public void verify(PublicKey verificationKey,
                   Signature verificationEngine)
            throws NullPointerException,
                   GenericCertificateIsLockedException,
                   PropertyVetoException,
                   InvalidKeyException,
                   SignatureException,
                   GenericCertificateIntegrityException
Verifies the digital signature of the encoded content in this certificate and locks it.

Please note the following:

Parameters:
verificationKey - The public key for verification - may not be null.
verificationEngine - The signature verification engine - may not be null.
Throws:
NullPointerException - If the preconditions for the parameters do not hold.
GenericCertificateIsLockedException - If this certificate is already locked by signing or verifying it before. Note that this is actually a subclass of PropertyVetoException.
PropertyVetoException - If locking the certifificate (and thus verifying the object) is vetoed by any listener.
InvalidKeyException - If the verification key is invalid.
SignatureException - If signature verification failed.
GenericCertificateIntegrityException - If the integrity of this certificate has been compromised.

TrueLicense XML 1.33

Copyright © 2005-2015 Schlichtherle IT Services. All Rights Reserved.