Example usage for org.bouncycastle.asn1.crmf EncKeyWithID getPrivateKey

List of usage examples for org.bouncycastle.asn1.crmf EncKeyWithID getPrivateKey

Introduction

In this page you can find the example usage for org.bouncycastle.asn1.crmf EncKeyWithID getPrivateKey.

Prototype

public PrivateKeyInfo getPrivateKey() 

Source Link

Usage

From source file:org.cryptable.pki.communication.PKICMPMessagesTest.java

License:Open Source License

/**
 * Check the private key archive control in the certification request
 *
 * @throws OperatorCreationException/*from  w ww.  ja va  2s . c o  m*/
 * @throws PKICMPMessageException
 * @throws CertificateEncodingException
 * @throws IOException
 * @throws CRMFException
 * @throws CMPException
 * @throws CMSException
 */
@Test
public void testCertificationWithPrivateKeyControl()
        throws OperatorCreationException, PKICMPMessageException, CertificateException, IOException,
        CRMFException, CMPException, CMSException, InvalidKeySpecException, NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchFieldException, IllegalAccessException, CRLException {
    String distinguishedName = pki.getTestUser1Cert().getSubjectX500Principal().getName();

    KeyPair keyPair = new KeyPair(pki.getTestUser1Cert().getPublicKey(), pki.getTestUser1CertPrivateKey());

    PKICMPMessages pkiMessages = new PKICMPMessages();
    pkiMessages.setPkiKeyStore(pkiKeyStoreRA);
    byte[] result = pkiMessages.createCertificateMessageWithLocalKey(distinguishedName, keyPair);

    ASN1InputStream asn1InputStream = new ASN1InputStream(result);
    ASN1Primitive asn1Primitive = asn1InputStream.readObject();
    PKIMessage pkiMessage = PKIMessage.getInstance(asn1Primitive);

    CertReqMsg[] certReqMsgs = CertReqMessages.getInstance(pkiMessage.getBody().getContent())
            .toCertReqMsgArray();
    AttributeTypeAndValue[] attributeTypeAndValues = certReqMsgs[0].getCertReq().getControls()
            .toAttributeTypeAndValueArray();
    GeneratePKI genPKI = new GeneratePKI();
    genPKI.createPKI();

    boolean bFound = false;
    for (AttributeTypeAndValue attributeTypeAndValue : attributeTypeAndValues) {
        if (attributeTypeAndValue.getType().equals(CRMFObjectIdentifiers.id_regCtrl_pkiArchiveOptions)) {
            PKIArchiveControl pkiArchiveControl = new PKIArchiveControl(
                    PKIArchiveOptions.getInstance(attributeTypeAndValue.getValue()));

            // Decrypt data
            CMSEnvelopedDataParser cmsEnvelopedDataParser = new CMSEnvelopedDataParser(
                    pkiArchiveControl.getEnvelopedData().getEncoded());
            RecipientInformationStore recipients = cmsEnvelopedDataParser.getRecipientInfos();
            Collection c = recipients.getRecipients();
            Iterator it = c.iterator();

            if (it.hasNext()) {
                RecipientInformation recipient = (RecipientInformation) it.next();
                byte[] recdata = recipient
                        .getContent(new JceKeyTransEnvelopedRecipient(genPKI.getSubCACertPrivateKey())
                                .setProvider(pkiKeyStoreRA.getProvider()));
                ASN1InputStream tstAsn1InputStream = new ASN1InputStream(recdata);
                ASN1Primitive tstAsn1Primitive = tstAsn1InputStream.readObject();
                EncKeyWithID encKeyWithID = EncKeyWithID.getInstance(tstAsn1Primitive);
                Assert.assertArrayEquals(keyPair.getPrivate().getEncoded(),
                        encKeyWithID.getPrivateKey().getEncoded());
                Assert.assertTrue(encKeyWithID.hasIdentifier());
                GeneralName identifier = GeneralName.getInstance(encKeyWithID.getIdentifier());
                Assert.assertEquals(genPKI.getTestUser1Cert().getSubjectDN().getName(),
                        identifier.getName().toString());
                bFound = true;
            }
        }
    }

    Assert.assertTrue(bFound);

}