public class CertificateRequest extends java.lang.Object implements java.io.Serializable, CertRequest
PKCS #10: Certification Request Syntax Version 1.5 (RFC 2314) defines
a certification request to consist of a version number, the subject´s distinguished name
(where subject denotes the entity claiming for being certified), the subject´s
public key (of type subjectPublicKeyInfo including a BIT-STRING
representation of the public key together with an identification of the public-key
algorithm being used) and an optional set of attributes providing some additional
information of the subject entity, all together forming a
CertificationRequestInfo value which is signed by the certificate
requesting entity using some particular signature algorithm for attesting to being
owner of the public key:
CertificationRequest ::= SEQUENCE {
certificationRequestInfo CertificationRequestInfo,
signatureAlgorithm SignatureAlgorithmIdentifier,
signature Signature }
where:
CertificationRequestInfo ::= SEQUENCE {
version Version, -- INTEGER version number (0 for this version)
subject Name, -- distinguished name of the entity claiming for certification
subjectPublicKeyInfo SubjectPublicKeyInfo, -- information about the subject´s public key
attributes [0] IMPLICIT Attributes -- additional information about the subject
}
For creating a certification request to be sent to some certification authority, an application shall use a proper constructor - for instance by directly supplying the subjects´s public key and distinguished name - and subsequently sign the request thereby identifying the signature algorithm and supplying the private key used for signing, e.g:
CertificateRequest cert_request = new CertificateRequest(public_key, subject_name); cert_request.sign(AlgorithmID.sha1WithRSAEncryption, private_key);The certification authority responses a certification request by re-sending a message (e.g. PKCS#7 message) containing a X.509 public-key certificate or a PKCS#6 extended certificate created from the data received with the certification request. However, before actually fulfilling the request, the certifcation authority will proove the correctness of the entity´s digital signature according to its certification policy.
For verifying a self-signed certificate request an entity may use the verify
method:
if (cert_request.verify()) {
// do something useful
}
X509Certificate,
PublicKeyInfo,
Name,
AlgorithmID,
Serialized Form| Constructor and Description |
|---|
CertificateRequest(byte[] arr)
Creates a CertificateRequest form a byte array.
|
CertificateRequest(java.io.InputStream is)
Creates a CertificateRequest form an input stream.
|
CertificateRequest(java.security.PublicKey publicKey,
Name subject)
Creates a new CertificateRequest from a PublicKeyInfo and a Name.
|
| Modifier and Type | Method and Description |
|---|---|
void |
addAttribute(Attribute attribute)
Adds one Atribute to this CertificateRequest.
|
Attribute[] |
getAttributes()
Gets the Atributes of this CertificateRequest.
|
Attribute[] |
getAttributes(ObjectID type)
Gets all the Atributes matching to a specific type (object identifier).
|
byte[] |
getFingerprint()
Returns the fingerprint of this certificate request.
|
byte[] |
getFingerprint(java.lang.String digestAlgorithm)
Returns the fingerprint of this certificate request calculated with the given
hash algorithm.
|
java.security.PublicKey |
getPublicKey()
Returns the public key of this certificate request.
|
AlgorithmID |
getSignatureAlgorithmID()
Returns the signature algorithm of this certificate request.
|
Name |
getSubject()
Returns the subject of this certificate request.
|
int |
getVersion()
Returns the version number of this certificate request.
|
void |
setAttributes(Attribute[] attributes)
Sets the Atributes for this CertificateRequest.
|
void |
sign(AlgorithmID signatureAlgorithm,
java.security.PrivateKey issuerPrivateKey)
Signs the certificate request with the private key of the subject.
|
byte[] |
toByteArray()
Returns the certificate request in a byte array in DER format.
|
java.lang.String |
toString()
Returns a string that represents the contents of the certificate request.
|
boolean |
verify()
Verifies the self signed certificate request.
|
void |
writeTo(java.io.OutputStream os)
Writes this certificate request to the given output stream.
|
public CertificateRequest(java.io.InputStream is)
throws java.io.IOException,
PKCSParsingException
From the request derived from the input stream an ASN.1 object is created and
subsequently parsed for the inherent CertificationRequestInfo
to obtain the version number, the subject´s name and public key information,
and any supplied attributes.
is - the input stream from where the certificate request shall be readjava.io.IOException - if an I/O error occursPKCSParsingException - if the certificate request cannot be parsedpublic CertificateRequest(byte[] arr)
throws PKCSParsingException
From the request derived from the byte array an ASN.1 object is created and
subsequently parsed for the inherent CertificationRequestInfo
to obtain the version number, the subject´s name and public key information,
and any supplied attributes. This constructor only may be used for creating
a certification request from an already existing certification request, supplied
as DER encoded ASN.1 object that may have been created by calling the
toByteArray method.
arr - the byte array containing the certificate requestPKCSParsingException - if the certificate request cannot be parsedpublic CertificateRequest(java.security.PublicKey publicKey,
Name subject)
throws java.security.InvalidKeyException
The Name is of ASN.1 type Name specifying a DistinguishedName
within the X.500 directory information tree. It may be created and supplied
with proper informations (relative distinguished names) as follows:
Name subject = new Name(); subject.addRDN(ObjectID.country, "AT"); subject.addRDN(ObjectID.locality, "Graz"); subject.addRDN(ObjectID.organization ,"TU Graz"); subject.addRDN(ObjectID.organizationalUnit ,"IAIK"); subject.addRDN(ObjectID.commonName ,"TestUser");
publicKey - the public key of the subjectsubject - the subject of the certificate request as distinguished namejava.security.InvalidKeyException - if the public key has an invalid encodingNamepublic void addAttribute(Attribute attribute)
attribute - the Attribute to addpublic void setAttributes(Attribute[] attributes)
iaik.asn1.structures.Attribute objects. To, for instance, add the PKCS#9 attribute
challengePassword, use:
CertificateRequest request = new CertificateRequest(pubKey, subject);
PrintableString password = new PrintableString("iaik");
Attribute cPAttribute =
new Attribute(ObjectID.challengePassword, new ASN1Object[] { password });
Attribute[] attributes = new Attribute[] { cPAttribute };
request.setAttributes(attributes);
Note that the attributes have to be set before the request is
signed with the subject´s private key!attributes - the Attribute to setpublic Attribute[] getAttributes()
null is returned.
Otherwise the atributes are returned as an array of
iaik.asn1.structures.Attribute
objects:
Attribute[] attributes = request.getAttributes();
if (attributes != null) {
for (int i = 0; i < attributes.length; i++) {
Attribute attr = attributes[i];
System.out.println(attr.getType());
ASN1Object[] asn1Obj = attr.getValue();
for (int j = 0; j < asn1Obj.length; j++) {
System.out.println(asn1Obj[j].toString());
}
}
}
null if there are no attributes includedpublic Attribute[] getAttributes(ObjectID type)
null is returned.
Otherwise the matching atributes are returned as an array of
iaik.asn1.structures.Attribute
objects. The following sample queries if the challangePassword attribute
is included:
Attribute[] attributes = request.getAttributes(ObjectID.challengePassword);
if (attributes != null) {
//expected only one:
Attribute attr = attributes[0];
System.out.println(attr.getType());
ASN1Object[] asn1Obj = attr.getValue();
//again, only one expected:
System.out.println(asn1Obj[0].getValue());
}
null if there are no attributes of the given type includedpublic void sign(AlgorithmID signatureAlgorithm, java.security.PrivateKey issuerPrivateKey) throws java.security.SignatureException, java.security.InvalidKeyException, java.security.NoSuchAlgorithmException
signatureAlgorithm - the AlgorithmID of the signature algorithmissuerPrivateKey - the private key of the subjectjava.security.SignatureException - if the signature could not be createdjava.security.InvalidKeyException - if the format of the key is wrongjava.security.NoSuchAlgorithmException - if there is no implementation for the specified signature algorithmpublic boolean verify()
throws java.security.SignatureException
verify in interface CertRequesttrue if the cert request is OK, false if notjava.security.SignatureException - if the certificate request could not be verifiedpublic byte[] toByteArray()
The DER format (Distinguished Encoding Rules) defines a binary representation of an abstract ASN.1 datastructure.
public void writeTo(java.io.OutputStream os)
throws java.io.IOException
os - the output stream to which the request shall be writtenjava.io.IOException - if an I/O error occurspublic int getVersion()
public AlgorithmID getSignatureAlgorithmID()
public Name getSubject()
Namepublic java.security.PublicKey getPublicKey()
getPublicKey in interface CertRequestpublic byte[] getFingerprint()
public byte[] getFingerprint(java.lang.String digestAlgorithm)
throws java.security.NoSuchAlgorithmException
digestAlgorithm - the digest algorithm to be usedjava.security.NoSuchAlgorithmException - if the requested algorithm is not supportedpublic java.lang.String toString()
toString in class java.lang.Object