4

One of the possible ways to get mime-type of the file is following:

File myFile = File("myDocument.pdf");
Path path = file.toPath();
String mimeType = Files.probeContentType(path);

It returns application/pdf. Everything is correct. But this way does not work with *.p7s files. It returns null instead of expected application/pkcs7-mime.

Maybe someone know where is the problem and what could be the solution?

tckmn
  • 57,719
  • 27
  • 114
  • 156
Martynas
  • 2,545
  • 7
  • 30
  • 36
  • The `FileTypeDetector` implementation(s) you are using are unable to determine the MIME type of the file. I believe the default `FileTypeDetector` is platform-specific. Which operating system(s) are you running your code on when you encounter this problem? – Robin Green Jan 11 '14 at 06:35
  • see also http://stackoverflow.com/questions/8488491/how-to-accurately-determine-mime-data-from-a-file – Robin Green Jan 11 '14 at 06:36
  • I'm using Windows 8.1. In your link I found Apache Tika and it determines mime-type accurately (also p7s). Thank you, Robin! – Martynas Jan 11 '14 at 13:22

2 Answers2

5

Java's FileTypeDetector does not support the detection of the p7s file format out of the box. So, unless some of the file type detecting libraries out there do support the p7s format you are pretty much on your own.

The p7s extension is defined by the SMIME standard as the file extension used for PKCS-7 cryptographic signatures in RFC 2315. It's mime content type is actually application/pkcs7-signature. Email clients that do not know how to handle these signatures allow the user to download them as a file.

The PKCS-7 syntax is described using ASN.1 in RFC 2315. The PKCS-7 signature is encoded using DER. Once you understand this, it is actually not that hard to verify if a p7s file is actually of the type pkcs7-signature.

In order to verify that p7s file is a pk7s-signature you can use an ASN.1 parser for Java like apache harmony to parse the file. Then you'll simply have to assert that the object identifier at the start of the ASN.1 sequence is of the type pkcs7-signedData (see chapter 14 of RFC 2315 for a definition of this object identifier). This detection method does not proof that the p7s file actually is a PKCS-7 signature, but the chances are extremely high.

OpenSSL has a built in ASN1 parser. You can use it to manually apply the above described detection method:

$ openssl asn1parse -inform DER -in smime.p7s -dump -i
    0:d=0  hl=4 l= 490 cons: SEQUENCE
    4:d=1  hl=2 l=   9 prim:  OBJECT            :pkcs7-signedData
                                                 ^^^^^^^^^^^^^^^^                                                                                       
                                                that's what I am talking about

Hope this helps. Good luck!

Community
  • 1
  • 1
Lodewijk Bogaards
  • 19,777
  • 3
  • 28
  • 52
-1

The best way, that I found is to use Apache Tika.

For more details see How to accurately determine mime data from a file?

Thank you Robin!

Community
  • 1
  • 1
Martynas
  • 2,545
  • 7
  • 30
  • 36