Search code examples
bouncycastlepkcs#7

Bouncycastle - how to distinguish attached from detached signature file programmatically


I am working on an application that is used to sign/verify files according to pkcs7. I am using bouncycastle. The problem is that whenever i pass in (to verify!) a file containing a signature i cannot find a way to distinguish whether the file contains both signature and signed data or just a signature. The point is to ask the user to select a second file if the first one contains only signature /and display appropriate error/. Is there any way around this problem?


Solution

  • To construct the CMSSignedData (the first time, before you know whether it has encapsulated content), just use the CMSSignedData(byte[]) constructor, where byte[] is the full contents of the file.

    Once you have the CMSSignedData instance, then getSignedContent() simply returns null if the content was not encapsulated.

    Once you have the basics working, if you are dealing with very large files, you may want to look at CMSSignedDataParser as a more advanced option that will avoid reading in the entire file.