public void testProtectedMessage() throws Exception {
    KeyPairGenerator kGen = KeyPairGenerator.getInstance("RSA", BC);

    kGen.initialize(512);

    KeyPair kp = kGen.generateKeyPair();
    X509CertificateHolder cert = makeV3Certificate(kp, "CN=Test", kp, "CN=Test");

    GeneralName sender = new GeneralName(new X500Name("CN=Sender"));
    GeneralName recipient = new GeneralName(new X500Name("CN=Recip"));

    ContentSigner signer =
        new JcaContentSignerBuilder("MD5WithRSAEncryption").setProvider(BC).build(kp.getPrivate());
    ProtectedPKIMessage message =
        new ProtectedPKIMessageBuilder(sender, recipient)
            .setBody(
                new PKIBody(
                    PKIBody.TYPE_INIT_REP,
                    CertRepMessage.getInstance(new DERSequence(new DERSequence()))))
            .addCMPCertificate(cert)
            .build(signer);

    X509Certificate jcaCert =
        new JcaX509CertificateConverter()
            .setProvider(BC)
            .getCertificate(message.getCertificates()[0]);
    ContentVerifierProvider verifierProvider =
        new JcaContentVerifierProviderBuilder().setProvider(BC).build(jcaCert.getPublicKey());

    assertTrue(message.verify(verifierProvider));

    assertEquals(sender, message.getHeader().getSender());
    assertEquals(recipient, message.getHeader().getRecipient());
  }
  public void testMacProtectedMessage() throws Exception {
    KeyPairGenerator kGen = KeyPairGenerator.getInstance("RSA", BC);

    kGen.initialize(512);

    KeyPair kp = kGen.generateKeyPair();
    X509CertificateHolder cert = makeV3Certificate(kp, "CN=Test", kp, "CN=Test");

    GeneralName sender = new GeneralName(new X500Name("CN=Sender"));
    GeneralName recipient = new GeneralName(new X500Name("CN=Recip"));

    ProtectedPKIMessage message =
        new ProtectedPKIMessageBuilder(sender, recipient)
            .setBody(
                new PKIBody(
                    PKIBody.TYPE_INIT_REP,
                    CertRepMessage.getInstance(new DERSequence(new DERSequence()))))
            .addCMPCertificate(cert)
            .build(
                new PKMACBuilder(new JcePKMACValuesCalculator().setProvider(BC))
                    .build("secret".toCharArray()));

    PKMACBuilder pkMacBuilder = new PKMACBuilder(new JcePKMACValuesCalculator().setProvider(BC));

    assertTrue(message.verify(pkMacBuilder, "secret".toCharArray()));

    assertEquals(sender, message.getHeader().getSender());
    assertEquals(recipient, message.getHeader().getRecipient());
  }
  public void testSampleCr() throws Exception {
    PKIMessage msg = loadMessage("sample_cr.der");
    ProtectedPKIMessage procMsg = new ProtectedPKIMessage(new GeneralPKIMessage(msg));

    assertTrue(
        procMsg.verify(
            new PKMACBuilder(new JcePKMACValuesCalculator().setProvider(BC)),
            "TopSecret1234".toCharArray()));
  }
  public void testConfirmationMessage() throws Exception {
    KeyPairGenerator kGen = KeyPairGenerator.getInstance("RSA", BC);

    kGen.initialize(512);

    KeyPair kp = kGen.generateKeyPair();
    X509CertificateHolder cert = makeV3Certificate(kp, "CN=Test", kp, "CN=Test");

    GeneralName sender = new GeneralName(new X500Name("CN=Sender"));
    GeneralName recipient = new GeneralName(new X500Name("CN=Recip"));

    CertificateConfirmationContent content =
        new CertificateConfirmationContentBuilder()
            .addAcceptedCertificate(cert, BigInteger.valueOf(1))
            .build(new JcaDigestCalculatorProviderBuilder().build());

    ContentSigner signer =
        new JcaContentSignerBuilder("MD5WithRSAEncryption").setProvider(BC).build(kp.getPrivate());
    ProtectedPKIMessage message =
        new ProtectedPKIMessageBuilder(sender, recipient)
            .setBody(new PKIBody(PKIBody.TYPE_CERT_CONFIRM, content.toASN1Structure()))
            .addCMPCertificate(cert)
            .build(signer);

    X509Certificate jcaCert =
        new JcaX509CertificateConverter()
            .setProvider(BC)
            .getCertificate(message.getCertificates()[0]);
    ContentVerifierProvider verifierProvider =
        new JcaContentVerifierProviderBuilder().setProvider(BC).build(jcaCert.getPublicKey());

    assertTrue(message.verify(verifierProvider));

    assertEquals(sender, message.getHeader().getSender());
    assertEquals(recipient, message.getHeader().getRecipient());

    content =
        new CertificateConfirmationContent(
            CertConfirmContent.getInstance(message.getBody().getContent()));

    CertificateStatus[] statusList = content.getStatusMessages();

    assertEquals(1, statusList.length);
    assertTrue(
        statusList[0].isVerified(
            cert, new JcaDigestCalculatorProviderBuilder().setProvider(BC).build()));
  }