Get SignedContent from P7M

2.3k Views Asked by At

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?

0

There are 0 best solutions below