public abstract class AbstractCertificate extends java.lang.Object implements ICertificate
AES_ENCRYPTION_MODE, CERTIFICATE_TYPE, DIGEST_TYPE, HMAC_ALGORITHM, RSA_ENCRYPTION_MODE
Constructor and Description |
---|
AbstractCertificate(byte[] certificateBytes,
byte[] privateKey)
Creates a new certificate abstraction, needs the bytes of the certificate.
|
Modifier and Type | Method and Description |
---|---|
static AbstractCertificate |
createCertificate(byte[] certificate)
Creates a new certificate based on the given byte-array (which contains only the public key in DER format).
|
static AbstractCertificate |
createCertificate(byte[] certificate,
byte[] key)
Creates a new certificate based on the given byte-array (which contains only the public key in DER format)
and the certificates key (also as byte-array in DER format).
|
byte[] |
decrypt(byte[] encryptedMessage)
Decrypts a message, so that the plain text is accessible.
|
byte[] |
encrypt(byte[] message)
Encrypts a message, so that it can only be read by the owner of the corresponding private key of this certificate.
|
abstract boolean |
equals(java.lang.Object obj)
Every certificate type has to implement a comparison operator.
|
java.util.Date |
getEndDate()
Returns the time after that the certificate is invalid.
|
byte[] |
getFingerprint()
Gets the SHA-1 fingerprint of the certificate.
|
abstract java.lang.String |
getIssuer()
Get the issuer of the certificate
|
abstract IPublicKey |
getPublicKey()
Get the public key of the certificate
|
abstract byte[] |
getSignature()
Get the signature of the issuer of this certificate.
|
abstract byte[] |
getSignedPart()
Returns the part of the certificate, that was signed by the issuer.
|
java.util.Date |
getStartDate()
Returns the time from which the certificate is valid.
|
abstract java.lang.String |
getSubject()
Get the subject of the certificate.
|
int |
hashCode()
Returns a hash code for the certificate.
|
boolean |
hasPrivateKey()
Can be used to check if the device owns the corresponding private key of this certificate.
|
boolean |
isValidRegardingStartAndEndDate()
Validates the start and end date of a certificate.
|
byte[] |
sign(byte[] toSign)
Signs a given byte array.
|
byte[][] |
toByteArray()
Get the certificate as byte array (DER encoded), contains also the private key as byte array, if it does exist.
|
java.lang.String |
toString()
Convert the bytes of the certificate to a BASE64-encoded string.
|
static boolean |
verifyCACertificateSignature(AbstractCertificate caCertificate)
Verifies a given CA root certificate.
|
static boolean |
verifyCertificate(AbstractCertificate[] certificate,
IKeyStore keystore)
Verifies a given certificate and returns true if the certificate can be trusted, because it is either
Is stored in the given keystore
One of the given intermediate certificates is stored in the keystore
Its given root certificate is stored in the keystore.
|
boolean |
verifyCertificate(IKeyStore keystore)
Verify if the certificate is trusted or issued by a trusted CA.
|
boolean |
verifySignature(byte[] message,
byte[] signature)
Verifies a signature which was created with the private key of this certificate (only needs the public key for verification).
|
public AbstractCertificate(byte[] certificateBytes, byte[] privateKey) throws java.io.IOException
certificateBytes
- The bytes of the certificate (so it can be accessed properly)java.io.IOException
public abstract IPublicKey getPublicKey()
getPublicKey
in interface ICertificate
public abstract java.lang.String getSubject()
getSubject
in interface ICertificate
public abstract java.lang.String getIssuer()
getIssuer
in interface ICertificate
public boolean verifyCertificate(IKeyStore keystore)
verifyCertificate
in interface ICertificate
- public final byte[] getFingerprint()
getFingerprint
in interface ICertificate
java.io.IOException
- This exception is thrown if the certificate is stored in a file and cannot properly accessed.public byte[] sign(byte[] toSign)
sign
in interface ICertificate
toSign
- The byte array which should be hashed and signedpublic boolean verifySignature(byte[] message, byte[] signature)
verifySignature
in interface ICertificate
message
- The message which was signedsignature
- The signature of the messagepublic byte[] encrypt(byte[] message)
encrypt
in interface ICertificate
message
- The message which should be encryptedpublic byte[] decrypt(byte[] encryptedMessage)
decrypt
in interface ICertificate
encryptedMessage
- A message which was encrypted using the public key of this certificatejava.io.IOException
public boolean hasPrivateKey()
hasPrivateKey
in interface ICertificate
public java.lang.String toString()
toString
in interface ICertificate
toString
in class java.lang.Object
public byte[][] toByteArray()
toByteArray
in interface ICertificate
public abstract boolean equals(java.lang.Object obj)
equals
in interface ICertificate
equals
in class java.lang.Object
obj
- The object of the type CertificateAbstraction that should be compared to the current certificateObject.equals(java.lang.Object)
public abstract byte[] getSignedPart()
getSignedPart
in interface ICertificate
public abstract byte[] getSignature()
getSignature
in interface ICertificate
getSignedPart()
public static boolean verifyCACertificateSignature(AbstractCertificate caCertificate)
caCertificate
- A byte array containing a CA root certificate.public final int hashCode()
hashCode
in interface ICertificate
hashCode
in class java.lang.Object
public final java.util.Date getStartDate()
getStartDate
in interface ICertificate
getEndDate()
public final java.util.Date getEndDate()
getEndDate
in interface ICertificate
getStartDate()
public static AbstractCertificate createCertificate(byte[] certificate) throws java.io.IOException
certificate
- The certificate in DER format.java.io.IOException
- If the byte-array is malformed or this type of certificate is not supported.public static AbstractCertificate createCertificate(byte[] certificate, byte[] key) throws java.io.IOException
certificate
- The certificate in DER format.key
- The key of the certificate in DER format. Can be null if the key is not known.java.io.IOException
- If one of the byte-arrays is malformed or this type of certificate/key is not supported.public final boolean isValidRegardingStartAndEndDate()
isValidRegardingStartAndEndDate
in interface ICertificate
public static boolean verifyCertificate(AbstractCertificate[] certificate, IKeyStore keystore)
certificate
- A chain of certificates that should be verified. The first certificate
should point to the actual certificate that shall be validated, the remaining certificates
should be the path to the top of the hierarchy. Given that the certificates of the hierarchy
are installed in the key store, they do not have to be specified as parameter in order to
successfully validate the certificate.keystore
- The keystore that should be used for validation.