bbottema/simple-java-mail

Added missing support for S/MIME envloped signing

cgruber0 opened this issue · 6 comments

Hi,

I have troubles getting the content of an email that is encrypted and signed (see attachment). The email itself only contains one smime.p7m attachment.

I use following code:

FileInputStream f = new FileInputStream(new File("email.eml"));

FileInputStream key = new FileInputStream(new File("keystore.p12"));
FileInputStream key2 = new FileInputStream(new File("keystore.p12"));

SmimeKeyStore smimeKeyStore = new SmimeKeyStore(key, "pw".toCharArray());
String alias = smimeKeyStore.getPrivateKeyAliases().iterator().next();

Pkcs12Config yourPkcs12Config = Pkcs12Config.builder()
        .pkcs12Store(key2) // path, File or InputStream
        .storePassword("pw")
        .keyAlias(alias)
        .keyPassword("pw")
        .build();

Email mergedEmail = EmailConverter.emlToEmail(f, yourPkcs12Config);
List<AttachmentResource> list = mergedEmail.getDecryptedAttachments();

for (AttachmentResource r : list) {
    play.Logger.info(r.readAllData());
}

But the output of r.readAllData() gives me binary data only.

When I open the email in Thunderbird it is decrypted correctly, it contains text only.

Can you please help me to get the actual content?
email.txt

Hi, my apologies for the late reply, but I'm currently unable to look into this as I have recently become father to our second child. Takes rather lot of attention I'm finding out. On top of that, the topic here is rather complicated and needs some proper debugging and research.

I'm always open to suggestions and pull requests though. Sorry I don't have better news for the moment...

When I open this file in Thunderbird, I don't see any attachments...

image

If you look at the content of the file (email.txt) you can see a Content-Disposition: attachment; filename="smime.p7m" part.

That's the actual content (= text) of the email. But it's encrypted.

Thunderbird is able to decrypt the email without problems and shows the content (private key provided). The library is not.

You can understand the problem now?

Unless you provide the private key along with the encrypted email, I can't do a root-cause analysis. The sample emails included in Simple Java Mail's tests all work fine, so I have no clue what can go wrong.

Can you provide the private key, perhaps email it to me directly so I can analyse (I won't publish it in the GIT repo). That would be of great help.

I have been provided with a matching keystore and have been analysing the issue. It turns out that S/MIME support was incomplete for the backwards compatible legacy signing protocol: Content-Type: application/x-pkcs7-mime; name=smime.p7m; smime-type=signed-data. Nowadays signing is done using certificate added to an attachment, but legacy style is that the entire attachment content is wrapped similarly to how encrypted content still is wrapped (enveloped content).

I've fixed the support (that was pretty complex!), but I've got it working nicely now. Wrapping up on the code quality and then I'll be shipping out a new release...

Fix released in 6.4.1. Again, greatly appreciate your trust, I could not have analysed and fixed this without it!