Example usage for org.bouncycastle.asn1.pkcs CertificationRequestInfo getEncoded

List of usage examples for org.bouncycastle.asn1.pkcs CertificationRequestInfo getEncoded

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.pkcs CertificationRequestInfo getEncoded.

Prototype

public byte[] getEncoded() throws IOException 

Source Link

Document

Return the default BER or DER encoding for this object.

Usage

From source file:org.clever.Common.XMPPCommunicator.ScepRequest.java

License:Open Source License

public CertificationRequest createCsr(X500Principal subject, PublicKey pubKey, PrivateKey priKey,
        char[] password) throws GeneralSecurityException, IOException {
    AlgorithmIdentifier sha1withRsa = new AlgorithmIdentifier(PKCSObjectIdentifiers.sha1WithRSAEncryption);

    ASN1Set cpSet = new DERSet(new DERPrintableString(new String(password)));
    Attribute challengePassword = new Attribute(PKCSObjectIdentifiers.pkcs_9_at_challengePassword, cpSet);
    ASN1Set attrs = new DERSet(challengePassword);

    SubjectPublicKeyInfo pkInfo = new SubjectPublicKeyInfo(
            (ASN1Sequence) ASN1Object.fromByteArray(pubKey.getEncoded()));

    Properties ht = new Properties();
    ht.put(X509Principal.CN, this.hostname);
    ht.put(X509Principal.C, this.C);
    ht.put(X509Principal.O, this.O);
    ht.put(X509Principal.OU, this.OU);
    ht.put(X509Principal.EmailAddress, this.hostname + "@" + this.domain);
    X509Name nn = new X509Name(ht);

    X509Name name = new X509Name(subject.toString());

    CertificationRequestInfo requestInfo = new CertificationRequestInfo(nn, pkInfo, attrs);

    Signature signer = Signature.getInstance("SHA1withRSA");
    signer.initSign(priKey);//from w  w  w  . ja  va  2  s  .c o  m
    signer.update(requestInfo.getEncoded());
    byte[] signatureBytes = signer.sign();
    DERBitString signature = new DERBitString(signatureBytes);

    return new CertificationRequest(requestInfo, sha1withRsa, signature);
}