/** * Basic constructor - specify the contents of the PKIArchiveControl structure. * * @param privateKeyInfo the private key to be archived. * @param generalName the general name to be associated with the private key. */ public PKIArchiveControlBuilder(PrivateKeyInfo privateKeyInfo, GeneralName generalName) { EncKeyWithID encKeyWithID = new EncKeyWithID(privateKeyInfo, generalName); try { this.keyContent = new CMSProcessableByteArray(CRMFObjectIdentifiers.id_ct_encKeyWithID, encKeyWithID.getEncoded()); } catch (IOException e) { throw new IllegalStateException("unable to encode key and general name info"); } this.envGen = new CMSEnvelopedDataGenerator(); }
public void testBasicMessageWithArchiveControl() throws Exception { KeyPairGenerator kGen = KeyPairGenerator.getInstance("RSA", BC); kGen.initialize(512); KeyPair kp = kGen.generateKeyPair(); X509Certificate cert = makeV1Certificate(kp, "CN=Test", kp, "CN=Test"); JcaCertificateRequestMessageBuilder certReqBuild = new JcaCertificateRequestMessageBuilder(BigInteger.ONE); certReqBuild.setSubject(new X500Principal("CN=Test")) .setPublicKey(kp.getPublic()); certReqBuild.addControl(new JcaPKIArchiveControlBuilder(kp.getPrivate(), new X500Principal("CN=Test")) .addRecipientGenerator(new JceKeyTransRecipientInfoGenerator(cert).setProvider(BC)) .build(new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(CMSEnvelopedDataGenerator.AES128_CBC)).setProvider(BC).build())); JcaCertificateRequestMessage certReqMsg = new JcaCertificateRequestMessage(certReqBuild.build()); assertEquals(new X500Principal("CN=Test"), certReqMsg.getSubjectX500Principal()); assertEquals(kp.getPublic(), certReqMsg.getPublicKey()); PKIArchiveControl archiveControl = (PKIArchiveControl)certReqMsg.getControl(CRMFObjectIdentifiers.id_regCtrl_pkiArchiveOptions); assertEquals(PKIArchiveControl.encryptedPrivKey, archiveControl.getArchiveType()); assertTrue(archiveControl.isEnvelopedData()); RecipientInformationStore recips = archiveControl.getEnvelopedData().getRecipientInfos(); RecipientId recipientId = new JceKeyTransRecipientId(cert); RecipientInformation recipientInformation = recips.get(recipientId); assertNotNull(recipientInformation); EncKeyWithID encKeyWithID = EncKeyWithID.getInstance(recipientInformation.getContent(new JceKeyTransEnvelopedRecipient(kp.getPrivate()).setProvider(BC))); assertTrue(encKeyWithID.hasIdentifier()); assertFalse(encKeyWithID.isIdentifierUTF8String()); assertEquals(new GeneralName(X500Name.getInstance(new X500Principal("CN=Test").getEncoded())), encKeyWithID.getIdentifier()); assertTrue(Arrays.areEqual(kp.getPrivate().getEncoded(), encKeyWithID.getPrivateKey().getEncoded())); }
public void testBasicMessageWithArchiveControl() throws Exception { KeyPairGenerator kGen = KeyPairGenerator.getInstance("RSA", BC); kGen.initialize(512); KeyPair kp = kGen.generateKeyPair(); X509Certificate cert = makeV1Certificate(kp, "CN=Test", kp, "CN=Test"); JcaCertificateRequestMessageBuilder certReqBuild = new JcaCertificateRequestMessageBuilder(BigInteger.ONE); certReqBuild.setPublicKey(kp.getPublic()) .setSubject(new X500Name("CN=Test")); certReqBuild.addControl(new JcaPKIArchiveControlBuilder(kp.getPrivate(), new X500Name("CN=Test")) .addRecipientGenerator(new JceKeyTransRecipientInfoGenerator(cert).setProvider(BC)) .build(new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(CMSEnvelopedDataGenerator.AES128_CBC)).setProvider(BC).build())); JcaCertificateRequestMessage certReqMsg = new JcaCertificateRequestMessage(certReqBuild.build()); assertEquals(new X500Name("CN=Test"), certReqMsg.getCertTemplate().getSubject()); assertEquals(kp.getPublic(), certReqMsg.getPublicKey()); PKIArchiveControl archiveControl = (PKIArchiveControl)certReqMsg.getControl(CRMFObjectIdentifiers.id_regCtrl_pkiArchiveOptions); assertEquals(PKIArchiveControl.encryptedPrivKey, archiveControl.getArchiveType()); assertTrue(archiveControl.isEnvelopedData()); RecipientInformationStore recips = archiveControl.getEnvelopedData().getRecipientInfos(); RecipientId recipientId = new JceKeyTransRecipientId(cert); RecipientInformation recipientInformation = recips.get(recipientId); assertNotNull(recipientInformation); EncKeyWithID encKeyWithID = EncKeyWithID.getInstance(recipientInformation.getContent(new JceKeyTransEnvelopedRecipient(kp.getPrivate()).setProvider(BC))); assertTrue(encKeyWithID.hasIdentifier()); assertFalse(encKeyWithID.isIdentifierUTF8String()); assertEquals(new GeneralName(X500Name.getInstance(new X500Name("CN=Test").getEncoded())), encKeyWithID.getIdentifier()); assertTrue(Arrays.areEqual(kp.getPrivate().getEncoded(), encKeyWithID.getPrivateKey().getEncoded())); }