public class BasicOCSPResponse extends Response
BasicOCSPResponse.
When sending an OCSPResponse in
response to an OCSPRequest, a successful
response may include ResponseBytes
giving more detailed information about the status of the certificates
asked for. ResponseBytes may include Response information of any type, identified by its object identifier
(see RFC 2560):
OCSPResponse ::= SEQUENCE {
responseStatus OCSPResponseStatus,
responseBytes [0] EXPLICIT ResponseBytes OPTIONAL }
OCSPResponseStatus ::= ENUMERATED {
successful (0), --Response has valid confirmations
malformedRequest (1), --Illegal confirmation request
internalError (2), --Internal error in issuer
tryLater (3), --Try again later
--(4) is not used
sigRequired (5), --Must sign the request
unauthorized (6) --Request unauthorized
}
ResponseBytes ::= SEQUENCE {
responseType OBJECT IDENTIFIER,
response OCTET STRING }
This class implements the only one response type predefined by
RFC 2560,
BasicOCSResponse which shall be supported by all
OCSP clients and servers:
BasicOCSPResponse ::= SEQUENCE {
tbsResponseData ResponseData,
signatureAlgorithm AlgorithmIdentifier,
signature BIT STRING,
certs [0] EXPLICIT SEQUENCE OF Certificate OPTIONAL }
ResponseData ::= SEQUENCE {
version [0] EXPLICIT Version DEFAULT v1,
responderID ResponderID,
producedAt GeneralizedTime,
responses SEQUENCE OF SingleResponse,
responseExtensions [1] EXPLICIT Extensions OPTIONAL }
After creating a BasicOCSPResponse and
setting ResponderID and
producedAt date, add single responses for any single
request included in the OCSPRequest
received, e.g.:
// the ocsp request received
OCSPrequest ocspRequest = ...;
Request[] requests = ocspRequest.getRequestList();
SingleResponse[] singleResponses = new SingleResponse[requests.length];
// create a single response for any request included:
for (int i = 0; i < requests.length; i++) {
...
singleResponses[i] = ...;
...
}
// create the BasicOCSPResponse:
BasicOCSPresponse basicOCSPResponse = new BasicOCSPResponse();
// set the ResponderID:
ResponderID responderID = ...;
basicOCSPResponse.setResponderID(responderID);
// set the producedAt date:
basicOCSPResponse.setProducedAt(new Date());
// add the single responses:
basicOCSPResponse.setSingleResponses(singleResponses);
Before signing the BasicOCSPResponse
extensions may be added and the certificates of the signer may be
included to help the OCSP requestor to verify the signature,
e.g.:
// the certificates of the responder X509Certificate[] responderCerts = ...; // the private key of the responder, used for signing: PrivateKey responderKey = ...; // set the certificates: basicOCSPResponse.setCertificates(responderCerts); // sign the response: ocspResponse.sign(AlgorithmID.sha1WithRSAEncryption, responderKey);Finally the BasicOCSPResponse has to be included into an
OCSPResponse message for being sent
back to the requestor:
OCSPResponse ocspResponse = new OCSPResponse(basicOCSPResponse); OutputStream os = ...; // encode the OCSP response ocspResponse.writeTo(os);
A requestor receiving an ocsp response, checks the response
status and -- if successful -- gets
the BasicOCSPResponse included:
// the stream supplying the encoded OCSP response:
InputStream is = ...;
OCSPResponse ocspResponse = new OCSPResponse(is);
// get the response status:
int responseStatus = ocspResponse.getResponseStatus();
if (responseStatus != OCSPResponse.successful) {
System.out.println("Not successful; got response status: " +
ocspResponse.getResponseStatusName());
...
} else {
// get the basicOCSPResponse
BasicOCSPResponse basicOCSPResponse =
(BasicOCSPResponse)ocspResponse.getResponse();
After verifying the signature of the responder
the basicOCSPResponse may be searched for the single response given for a particular certificate
request sent to the server:
// verify the response
try {
if (basicOCSPResponse.containsCertificates()) {
X509Certificate signerCert = basicOCSPResponse.verify();
System.out.println("Signature ok from response signer " + signerCert.getSubjectDN());
} else {
basicOCSPResponse.verify(responderCerts[0].getPublicKey());
System.out.println("Signature ok!");
}
} catch (SignatureException ex) {
System.out.println("Signature verification error!!!");
}
// is there a single response for our request?
SingleResponse singleResponse = null;
try {
singleResponse = basicOCSPResponse.getSingleResponse(CertID);
} catch (OCSPException ex) {
singleResponse = basicOCSPResponse.getSingleResponse(targetCerts[0],
targetCerts[1], null);
}
if (singleResponse != null) {
System.out.println("Status information got for cert: ");
System.out.println(singleResponse.getCertStatus());
...
} else {
System.out.println("No response got!");
OCSPRequest,
Request,
OCSPResponse,
ResponseBytes,
SingleResponse,
CertID,
CertStatus| Modifier and Type | Field and Description |
|---|---|
static ObjectID |
responseType
The response type of the BasicOCSPResponse.
|
| Constructor and Description |
|---|
BasicOCSPResponse()
Default constructor for creating a new empty BasicOCSPResponse.
|
BasicOCSPResponse(byte[] array)
Creates a BasicOCSPResponse form a PEM or DER byte array.
|
BasicOCSPResponse(java.io.InputStream is)
Creates a BasicOCSPResponse from an input stream.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addExtension(V3Extension e)
Adds the given extension.
|
boolean |
containsCertificates()
Checks if certificates are included.
|
int |
countExtensions()
Returns the number of extensions included in this basic ocsp response.
|
int |
countSingleResponses()
Returns the number of single responses included.
|
void |
decode(ASN1Object obj)
Decodes a BasicOCSPResponse from an ASN1Object.
|
void |
decode(byte[] enc)
Decodes a BasicOCSPResponse from an byte array.
|
void |
decode(java.io.InputStream is)
Decodes a BasicOCSPResponse from an InputStream.
|
CertificateResponse |
getCertificateResponse(CertID certid)
Searches for the certificate response corresponding to the certificate
identified by the given CertID.
|
CertificateResponse |
getCertificateResponse(X509Certificate targetCert,
X509Certificate issuerCert)
Searches this BasicOCSPReponse for status information about the certificate
identified by the given certificate information.
|
X509Certificate[] |
getCertificates()
Returns the signer certificates that may be included in this response.
|
byte[] |
getEncoded()
Returns this BasicOCSPResponse as DER encoded ASN.1 data structure
|
V3Extension |
getExtension(ObjectID oid)
Returns a specific extension, identified by its object identifier.
|
byte[] |
getNonce()
A convenience method for getting the value of the Nonce extension,
if included in this response.
|
java.util.Date |
getProducedAt()
Returns the
producedAt date of this BasicOCSPResponse. |
ResponderID |
getResponderID()
Returns the responderID.
|
ObjectID |
getResponseType()
Returns the response type identifying this
BasicOCSPResponse
The corresponding OID string is "1.3.6.1.5.5.7.1.11.1". |
byte[] |
getSignature()
Returns the signature of this BasicOCSPResponse.
|
AlgorithmID |
getSignatureAlgorithm()
Returns the signature algorithm of this BasicOCSPResponse.
|
SingleResponse |
getSingleResponse(CertID certid)
Searches for the single response corresponding to the certificate
identified by the given CertID.
|
SingleResponse |
getSingleResponse(X509Certificate targetCert,
X509Certificate issuerCert)
Searches this BasicOCSPReponse for status information about the certificate
identified by the given certificate information.
|
SingleResponse[] |
getSingleResponses()
Returns all single responses included in this BasicOCSPResponse.
|
byte[] |
getTBSResponseData()
Returns the DER encoded
TBSResponseData ASN.1 data structure
specifying response data to be signed. |
int |
getVersion()
Returns the version number of this BasicOCSPResponse as
int. |
boolean |
hasExtensions()
Checks, if there are any extensions included in this basic ocsp response.
|
boolean |
hasUnsupportedCriticalExtension()
Returns true if there are unsupported critical extensions.
|
java.util.Enumeration |
listExtensions()
Returns an enumeration of all extensions included in this basic ocsp response.
|
void |
removeAllExtensions()
Removes all extensions from this basic ocsp response.
|
boolean |
removeExtension(ObjectID oid)
Removes the extension specified by its object identifier.
|
void |
setCertificates(X509Certificate[] signerCerts)
Sets the certificates to be included into this BasicOCSPResponse.
|
void |
setNonce(byte[] nonce)
A convenience method for setting the value of the Nonce extension.
|
void |
setProducedAt(java.util.Date producedAtDate)
Sets the
producedAt date of this certificate. |
void |
setResponderID(ResponderID responderID)
Sets the responderID.
|
void |
setSignature(AlgorithmID signatureAlg,
byte[] signature)
Sets the signature value of this BasicOCSPResponse.
|
void |
setSingleResponses(SingleResponse[] singleResponses)
Sets the single responses of this BasicOCSPResponse.
|
void |
sign(AlgorithmID signatureAlg,
java.security.PrivateKey issuerPK)
Signs the BasicOCSPResponse with the private key of the issuer.
|
void |
sign(AlgorithmID signatureAlg,
java.security.PrivateKey issuerPK,
java.lang.String provider)
Signs the BasicOCSPResponse with the private key of the issuer.
|
ASN1Object |
toASN1Object()
Returns the BasicOCSPResponse as an ASN1Object.
|
java.lang.String |
toString()
Returns a string that represents the contents of this BasicOCSPResponse.
|
java.lang.String |
toString(boolean detailed)
Returns a string that represents the contents of this BasicOCSPResponse.
|
X509Certificate |
verify()
Verifies this BasicOCSPResponse using the included signer certificates.
|
void |
verify(java.security.PublicKey key)
Uses the given public key to verify this BasicOCSPResponse.
|
void |
verify(java.security.PublicKey key,
java.lang.String sigProvider)
Uses the given public key to verify this BasicOCSPResponse.
|
void |
writeTo(java.io.OutputStream os)
Writes this BasicOCSPResponse DER encoded to the given output stream.
|
public static final ObjectID responseType
public BasicOCSPResponse()
Any value may be set using the corrseponding set<Value>
method.
The version number per default is set to 0 indicating a
v1 response.
public BasicOCSPResponse(java.io.InputStream is)
throws java.io.IOException,
CodingException
The supplied BasicOCSPResponse can be in PEM or DER format.
This constructor reads a BasicOCSPResponse previously written with
method writeTo(OutputStream).
For instance:
InputStream is = ...; BasicOCSPResponse response = new BasicOCSPResponse(is); is.close();
is - InputStream from which to create the BasicOCSPResponsejava.io.IOException - if the response could not be readCodingException - if there is a problem with the responsepublic BasicOCSPResponse(byte[] array)
throws CodingException
This constructor may be used for parsing an already exisiting
BasicOCSPResponse ASN.1 object, supplied as DER encoded
byte array, which may have been created by calling method
getEncoded.
array - the byte array containing the DER encoded responseCodingException - if the response cannot be decodedpublic void decode(ASN1Object obj) throws CodingException
The given ASN1Object represents an already existing BasicOCSPResponse
which may have been created by calling the toASN1Object method.
obj - the ASN1Object which representing the responseCodingException - if there is a problem when parsing the responsepublic void decode(byte[] enc)
throws CodingException
decode in class Responseenc - the byte array from where the response should be readCodingException - if an decoding/parsing error occurspublic void decode(java.io.InputStream is)
throws java.io.IOException
is - the InputStream from where the response should be readjava.io.IOException - if something is wrong with the InputStreampublic void sign(AlgorithmID signatureAlg, java.security.PrivateKey issuerPK) throws OCSPException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
signatureAlg - the AlgorithmID of the signature algorithmissuerPK - the private key of the issuerOCSPException - if the response could not be signedjava.security.InvalidKeyException - if the format of the key is wrongjava.security.NoSuchAlgorithmException - if there is no implementation for the
specified algorithmpublic void sign(AlgorithmID signatureAlg, java.security.PrivateKey issuerPK, java.lang.String provider) throws OCSPException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
signatureAlg - the AlgorithmID of the signature algorithmissuerPK - the private key of the issuerprovider - the name of the provider supplying the Signature engine
to be usedOCSPException - if the response could not be signedjava.security.InvalidKeyException - if the format of the key is wrongjava.security.NoSuchAlgorithmException - if there is no implementation for the
specified algorithmpublic void setSignature(AlgorithmID signatureAlg, byte[] signature) throws OCSPException
This method provides an alternative way to method sign for "signing" this
basic OCSP response with a precalculated signature value.
If using this method please make sure that the signature
value provided actually has beeb calculated over the
TBS responseData.
signatureAlg - the AlgorithmID of the signature algorithmsignature - the (precalculated) signature valueOCSPException - if the response could not be signedpublic byte[] getEncoded()
getEncoded in class Responsepublic int getVersion()
int.
Default version: v1.
ASN.1 definition:
Version ::= INTEGER { v1(0) }
int,
1 for v1.public ObjectID getResponseType()
BasicOCSPResponse
The corresponding OID string is "1.3.6.1.5.5.7.1.11.1".getResponseType in class ResponseBasicOCSPResponsepublic ResponderID getResponderID()
public SingleResponse[] getSingleResponses()
public int countSingleResponses()
public SingleResponse getSingleResponse(X509Certificate targetCert, X509Certificate issuerCert) throws OCSPException
This method only calls method getCertificateResponse and casts the result
to SingleResponse.
Each particular single response included is expected to be
identified by its CertID identifying
the corresponding certificate.
CertID ::= SEQUENCE {
hashAlgorithm AlgorithmIdentifier,
issuerNameHash OCTET STRING, -- Hash of Issuer's DN
issuerKeyHash OCTET STRING, -- Hash of Issuers public key
serialNumber CertificateSerialNumber }
When searching an OCSP response for a SingleResponse the search has to be done by checking the CertID identifiers of the single responses included.
If method getSingleResponse
does not find a single response for a given CertID thereby throwing an OCSPException there maybe single
responses using different hash algorithms as the one queried for.
In this case this method can be used for stepping
through the single responses
included and using their CertID types for searching for a single
response for the cert identified by the given certificate data. t.
Assumimg, for instance, that you have used method getSingleResponse for asking if
a response for your request is included. The search has stopped by
throwing an OCSPException indicating that no single response for
your CertID is included
targetCert - the target cert, if requiredissuerCert - the cert of the target cert issuer, if requirednull if no single response for the certificate
in mind is includedOCSPException - if some processing error occurs, e.g. if the CertID's
hash algorithm is not supported by the installed providerspublic SingleResponse getSingleResponse(CertID certid) throws OCSPException
This method only calls method getCertificateResponse and casts the result
to SingleResponse.
Each single response included in this BasicOCSPResponse is
identified by its CertID ID.
This method steps through all the single responses included in this
BasicOCSPResponse and compares their CertIDs with the given CertID.
certid - the CertID of the certificate for which status information
shall be obtainednull
if no single response for the certificate in mind is includedOCSPException - if a single response for the given CertID
cannot be found, but single responses include
hash algorithms different to the given
certID´s one; in this case you may try method
getSingleResponse to use the CertID
types of the single responses includedpublic CertificateResponse getCertificateResponse(X509Certificate targetCert, X509Certificate issuerCert) throws OCSPException
This method overrides the corresponding method of the parent Response class.
Each particular certificate response included is expected to be
identified by its CertID
When searching an OCSP response for a CertificateResponse the search has to be done by checking the CertID identifiers of the certificate responses included.
If method getCertificateResponse
does not find a certificate response for a given CertID thereby throwing an OCSPException.
targetCert and issuerCert are required;
generalName is ignored. From the given certs,
a certID is created and checked for equality with the certID of the
reqCert of the particular certificate response.
Note that any reqCert type can be created if target cert and issuer cert a set.
Assumimg, for instance, that you have used method getCertificateResponse for asking if
a response for your request is included. The search has stopped by
throwing an OCSPException indicating that no certificate response for
your CertID is included, but there are certificate responses present having
a different CertID type.
getCertificateResponse in class ResponsetargetCert - the target cert, if requiredissuerCert - the cert of the target cert issuer, if requirednull if no certificate response for the certificate
in mind is includedOCSPException - if some processing error occurs, e.g. if the CertID's
hash algorithm is not supported by the installed providerspublic CertificateResponse getCertificateResponse(CertID certid) throws OCSPException
This method overrides the corresponding method of the parent Response class.
Each single response included in this BasicOCSPResponse is
identified by its CertID ID.
This method steps through all the single responses included in this
BasicOCSPResponse and compares their CertIDs with the given CertID.
getCertificateResponse in class Responsecertid - the CertID of the certificate for which status information
shall be obtainednull
if no certificate response for the certificate in mind is includedOCSPException - if a single response for the given CertID
cannot be found, but single responses include
hash algorithms different to the given
certID´s one; in this case you may try method
getSingleResponse to use the CertID
types of the single responses includedpublic java.util.Date getProducedAt()
producedAt date of this BasicOCSPResponse.
The producedAt value denotes the time at which the
response was produced and can be set using the setProducedAt method.
null
if the producedAt date has yet not been setpublic byte[] getTBSResponseData()
throws CodingException
TBSResponseData ASN.1 data structure
specifying response data to be signed.
ResponseData ::= SEQUENCE {
version [0] EXPLICIT Version DEFAULT v1,
responderID ResponderID,
producedAt GeneralizedTime,
responses SEQUENCE OF SingleResponse,
responseExtensions [1] EXPLICIT Extensions OPTIONAL }
TBSResponseData as DER encoded ASN.1 structureCodingException - if an encoding error occurspublic byte[] getSignature()
public AlgorithmID getSignatureAlgorithm()
AlgorithmIDpublic void verify(java.security.PublicKey key)
throws java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException,
java.security.SignatureException
key - the public key (of the issuer) to verify the responsejava.security.NoSuchAlgorithmException - if there is no implementation for the algorithm that has been
used to sign this responsejava.security.InvalidKeyException - if the format of the public key is wrongjava.security.SignatureException - if the signature does not verifypublic void verify(java.security.PublicKey key,
java.lang.String sigProvider)
throws java.security.NoSuchAlgorithmException,
java.security.InvalidKeyException,
java.security.SignatureException
key - the public key (of the issuer) to verify the responsesigProvider - the crypto provider supplying the Signature engine
to be usedjava.security.NoSuchAlgorithmException - if there is no implementation for the algorithm that has been
used to sign this responsejava.security.InvalidKeyException - if the format of the public key is wrongjava.security.SignatureException - if the signature does not verifypublic X509Certificate verify() throws java.security.NoSuchAlgorithmException, java.security.InvalidKeyException, java.security.SignatureException, OCSPException
This method only can be used for verifying this response if
signer certificates are included. If so, this method assumes
that all certificates included belong to same chain. It tries
to sort the chain to get the signer certificate public key
for verifying the response. If no certificates are included
or the chain cannot be sorted, an OCSPException is thrown. In
this case you may use method verify
for verifying the response with the right public key supplied by
other means.
java.security.NoSuchAlgorithmException - if there is no implementation for the algorithm that has been
used to sign this responsejava.security.InvalidKeyException - if the format of the public key is wrongjava.security.SignatureException - if the signature does not verifyOCSPException - if no certs are included or the signer cert cannot
be found in the certificate list includedpublic boolean containsCertificates()
true if certificates are included, false
otherwisepublic ASN1Object toASN1Object()
public void writeTo(java.io.OutputStream os)
throws java.io.IOException
os - the output stream where the response shall be written tojava.io.IOException - if an I/O error occurspublic void setResponderID(ResponderID responderID)
responderID - the responderID identifying the responder by name or keypublic void setProducedAt(java.util.Date producedAtDate)
producedAt date of this certificate.
For instance:
GregorianCalendar date = (GregorianCalendar)Calendar.getInstance(); response.setProducedAt(date.getTime());
producedAtDate - the Date at which this response is producedpublic void setSingleResponses(SingleResponse[] singleResponses)
singleResponses - the single responses to be setpublic void setCertificates(X509Certificate[] signerCerts)
signerCerts - the certificates of the signer to be includedpublic X509Certificate[] getCertificates()
null otherwisepublic void addExtension(V3Extension e) throws X509ExtensionException
The extension to be added shall be an implemented
V3Extension.
If an extension with the same object ID already exists, it is replaced.
For reading back some extension use the getExtension(ObjectID) method.
e - the extension to be addedX509ExtensionException - if the extension cannot be addedpublic boolean removeExtension(ObjectID oid)
oid - the object ID of the extension to removetrue if the extension has been successfully removed,
false otherwisepublic void removeAllExtensions()
public java.util.Enumeration listExtensions()
The returned enumeration may contain unknown extensions (instances of
UnknownExtension
if there are any extensions included in basic response, for which there
exists no registered implementation, and it may contain error extensions
(instances of ErrorExtension) indicating extensions which cannot be
parsed properly because of some kind of error.
null if there are no
extensions present at allpublic boolean hasExtensions()
true if there are extensions, false if notpublic boolean hasUnsupportedCriticalExtension()
public int countExtensions()
public V3Extension getExtension(ObjectID oid) throws X509ExtensionInitException
If the extension cannot be initialized for some reason, an
X509ExtensionInitException is thrown. If the requested extension is
an unknown extension, which is not supported by a registered
implementation, this method creates and returns an UnknownExtension which may be queried for
obtaining as much information as possible about the unknown extension.
oid - the object ID of the extensionnull if the requested
extension is not presentX509ExtensionInitException - if the extension can not be initializedpublic void setNonce(byte[] nonce)
throws X509ExtensionException
This method provides an convenient alternative to method addExtension for including the Nonce extension in this
response. From the given nonce value a Nonce extension object
is created an added to the list of response extensions.
The Nonce extension can be used for cryptographically binding a request and a response to prevent replay attacks.
nonce - the nonce valueX509ExtensionException - if the Nonce extension cannot be createdpublic byte[] getNonce()
throws X509ExtensionInitException
This method provides an convenient alternative to method getExtension for getting the value of the
Nonce extension, if included
in this response.
The Nonce extension can be used for cryptographically binding a request and a response to prevent replay attacks.
nullX509ExtensionInitException - if the Nonce extension cannot be
initialized from its encodingpublic java.lang.String toString()
public java.lang.String toString(boolean detailed)
detailed - whether or not to give detailed information about the
included single responses and extensions