private void verifyParserAlgorithm(String algorithmOid, MimeBodyPart msg) throws Exception {
    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));

    //
    // generate a MimeBodyPart object which encapsulates the content
    // we want encrypted.
    //

    MimeBodyPart mp =
        gen.generate(
            msg,
            new JceCMSContentEncryptorBuilder(new ASN1ObjectIdentifier(algorithmOid))
                .setProvider(BC)
                .build());
    SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(mp);
    RecipientId recId = getRecipientId(_reciCert);

    RecipientInformationStore recipients = m.getRecipientInfos();
    RecipientInformation recipient = recipients.get(recId);

    MimeBodyPart res =
        SMIMEUtil.toMimeBodyPart(
            recipient.getContent(
                new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));

    verifyMessageBytes(msg, res);
  }
示例#2
0
  private MimeBodyPart createEncryptedEnvelope(
      MimeBodyPart bodyPart, Collection<X509Certificate> encryptingCertificates) {
    if (bodyPart == null || encryptingCertificates == null || encryptingCertificates.size() == 0) {
      throw new IllegalArgumentException();
    }

    if (LOGGER.isDebugEnabled()) {
      writePreEncypt(EntitySerializer.Default.serializeToBytes(bodyPart));
    }

    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    for (X509Certificate cert : encryptingCertificates) gen.addKeyTransRecipient(cert);

    MimeBodyPart retVal = null;

    try {
      retVal =
          gen.generate(
              bodyPart,
              toEncyAlgorithmOid(this.m_encryptionAlgorithm),
              CryptoExtensions.getJCEProviderName());
    } catch (Exception e) {
      throw new MimeException(MimeError.Unexpected, e);
    }

    return retVal;
  }
  public void testTwoRecipients() throws Exception {
    MimeBodyPart _msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");

    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));
    gen.addRecipientInfoGenerator(
        new JceKeyTransRecipientInfoGenerator(_reciCert2).setProvider(BC));

    //
    // generate a MimeBodyPart object which encapsulates the content
    // we want encrypted.
    //
    MimeBodyPart mp =
        gen.generate(
            _msg,
            new JceCMSContentEncryptorBuilder(CMSAlgorithm.RC2_CBC, 40).setProvider(BC).build());

    SMIMEEnvelopedParser m = new SMIMEEnvelopedParser(mp);

    RecipientId recId = getRecipientId(_reciCert2);

    RecipientInformationStore recipients = m.getRecipientInfos();
    RecipientInformation recipient = recipients.get(recId);

    FileBackedMimeBodyPart res =
        SMIMEUtil.toMimeBodyPart(
            recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(_reciKP2.getPrivate()).setProvider(BC)));

    verifyMessageBytes(_msg, res);

    m = new SMIMEEnvelopedParser(mp);

    res.dispose();

    recId = getRecipientId(_reciCert);

    recipients = m.getRecipientInfos();
    recipient = recipients.get(recId);

    res =
        SMIMEUtil.toMimeBodyPart(
            recipient.getContentStream(
                new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));

    verifyMessageBytes(_msg, res);

    res.dispose();
  }
  public void testSubKeyId() throws Exception {
    MimeBodyPart msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");

    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    //
    // create a subject key id - this has to be done the same way as
    // it is done in the certificate associated with the private key
    //
    MessageDigest dig = MessageDigest.getInstance("SHA1", BC);
    dig.update(
        SubjectPublicKeyInfo.getInstance(_reciCert.getPublicKey().getEncoded())
            .getPublicKeyData()
            .getBytes());

    gen.addRecipientInfoGenerator(
        new JceKeyTransRecipientInfoGenerator(dig.digest(), _reciCert.getPublicKey())
            .setProvider(BC));

    //
    // generate a MimeBodyPart object which encapsulates the content
    // we want encrypted.
    //

    MimeBodyPart mp =
        gen.generate(
            msg,
            new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(BC).build());

    SMIMEEnveloped m = new SMIMEEnveloped(mp);

    dig.update(
        SubjectPublicKeyInfo.getInstance(_reciCert.getPublicKey().getEncoded())
            .getPublicKeyData()
            .getBytes());

    RecipientId recId = new KeyTransRecipientId(dig.digest());

    RecipientInformationStore recipients = m.getRecipientInfos();
    RecipientInformation recipient = recipients.get(recId);

    MimeBodyPart res =
        SMIMEUtil.toMimeBodyPart(
            recipient.getContent(
                new JceKeyTransEnvelopedRecipient(_reciKP.getPrivate()).setProvider(BC)));

    verifyMessageBytes(msg, res);
  }
  public void testHeaders() throws Exception {
    MimeBodyPart msg = SMIMETestUtil.makeMimeBodyPart("WallaWallaWashington");

    SMIMEEnvelopedGenerator gen = new SMIMEEnvelopedGenerator();

    gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(_reciCert).setProvider(BC));

    //
    // generate a MimeBodyPart object which encapsulates the content
    // we want encrypted.
    //

    MimeBodyPart mp =
        gen.generate(
            msg,
            new JceCMSContentEncryptorBuilder(CMSAlgorithm.DES_EDE3_CBC).setProvider(BC).build());

    assertEquals(
        "application/pkcs7-mime; name=\"smime.p7m\"; smime-type=enveloped-data",
        mp.getHeader("Content-Type")[0]);
    assertEquals("attachment; filename=\"smime.p7m\"", mp.getHeader("Content-Disposition")[0]);
    assertEquals("S/MIME Encrypted Message", mp.getHeader("Content-Description")[0]);
  }