3

I am using java (jdk 1.7) and bouncycastle library to get the content of a p7m signed file.

To the build path I added the following files:

bcpkix-jdk15on-160.jar
commons-io-2.1.jar
log4j-1.2.17.jar
bcprov-ext-jdk15on-160.jar
bcprov-jdk15on-160.jar

This is the code I am using:

public static void main(String[] args) throws Exception {
    File f = new File("E:\\test\\file.xml.p7m");

    logger.debug(f.getAbsolutePath());
    byte[] content = org.apache.commons.io.FileUtils.readFileToByteArray(f);

    byte[] contentUnsigned = removeP7MCodes(f.getName(), content);
    if (contentUnsigned != null)
        logger.debug(new String(contentUnsigned));
}

public static byte[] removeP7MCodes(final String fileName, final byte[] p7bytes) {
    try {

        if (p7bytes == null)
            return p7bytes;

        if (!fileName.toUpperCase().endsWith(".P7M")) {
            return p7bytes;
        }

        // This is where I get the exception
        CMSSignedData cms = new CMSSignedData(p7bytes); 

        if (cms.getSignedContent() == null) {
            logger.error("Unable to find signed Content during decoding from P7M for file: " + fileName);               
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        cms.getSignedContent().write(out);

        return out.toByteArray();
    } catch (CMSException e) {
        logger.error("CMSException from P7M for file: " + fileName, e);
    } catch (IOException e) {
        logger.error("IOException from P7M for file: " + fileName, e);
    }
    return null;
}

For most of the p7m I tested it works without errors/problems..

There are some p7m files that I cannot decode and I get the following error (I tried verifying them with online tools and the files are genuine):

11:01:30 DEBUG [DecoderTester:45] - E:\test\file.xml.p7m
11:01:30 ERROR [DecoderTester:75] - CMSException from P7M for file: file.xml.p7m
org.bouncycastle.cms.CMSException: Malformed content.
    at org.bouncycastle.cms.CMSUtils.readContentInfo(Unknown Source)
    at org.bouncycastle.cms.CMSUtils.readContentInfo(Unknown Source)
    at org.bouncycastle.cms.CMSSignedData.<init>(Unknown Source)
    at com.decoder.DecoderTester.removeP7MCodes(DecoderTester.java:63)
    at com.decoder.DecoderTester.main(DecoderTester.java:48)
Caused by: java.lang.IllegalArgumentException: unknown object in getInstance: org.bouncycastle.asn1.DERApplicationSpecific
    at org.bouncycastle.asn1.ASN1Sequence.getInstance(Unknown Source)
    at org.bouncycastle.asn1.cms.ContentInfo.getInstance(Unknown Source)
    ... 5 more

I tried to remove one libs between the following two: bcprov-ext-jdk15on-160.jar and bcprov-jdk15on-160.jar but I got the same error.

Do you have any ideas?

Marcx
  • 6,806
  • 5
  • 46
  • 69
  • 3
    did you check that file.xml.p7m content is binary and not base64? – pedrofb Jan 10 '19 at 12:06
  • 1
    Thank you.. I solved the issue adding a base64 decode before creating the instance for `CMSSignedData`... I added `try { p7bytes = org.bouncycastle.util.encoders.Base64.decode(p7bytes); } catch (Exception e) { logger.debug("File P7m non in base64, tengo content standard" + e.getMessage()); }` not sure if there is a better way to do that. – Marcx Jan 10 '19 at 12:33
  • 1
    With that code, I think that's enough. Check this in case you like this option more https://stackoverflow.com/questions/8571501/how-to-check-whether-a-string-is-base64-encoded-or-not – pedrofb Jan 10 '19 at 13:27

0 Answers0