Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<bouncycastle.version>1.82</bouncycastle.version>
<bouncycastle.version>1.83</bouncycastle.version>
<commons-compress.version>1.28.0</commons-compress.version>
<commons.io.version>2.21.0</commons.io.version>
<jaxb-api.version>4.0.4</jaxb-api.version>
<jaxb-core.version>4.0.6</jaxb-core.version>
<jaxb-impl.version>4.0.6</jaxb-impl.version>
<opennlp-tools.version>1.9.3</opennlp-tools.version>
<pdfbox.version>3.0.6</pdfbox.version>
<poi.version>5.5.0</poi.version>
<poi.version>5.5.1</poi.version>
<rtfparserkit.version>1.16.0</rtfparserkit.version>
<slf4j.version>2.0.17</slf4j.version>
<zip4j.version>2.11.5</zip4j.version>
<!-- testing -->
<hamcrest.version>3.0</hamcrest.version>
<jmock-junit5.version>2.13.1</jmock-junit5.version>
<junit.version>5.11.4</junit.version>
<log4j.version>2.25.2</log4j.version>
<log4j.version>2.25.3</log4j.version>
</properties>

<build>
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/jadice/filetype/matchers/PKCS7Matcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.jadice.filetype.matchers;

import java.io.IOException;
import java.security.Security;
import java.util.Base64;

import org.bouncycastle.cms.CMSSignedData;
import org.jadice.filetype.Context;
import org.jadice.filetype.io.SeekableInputStream;

public class PKCS7Matcher extends Matcher {

@Override
public boolean matches(final Context context) throws IOException {
SeekableInputStream sis = context.getStream();
return isPKCS7(sis, context.getStatedExtension());
}

public static boolean isPKCS7(SeekableInputStream sis, String extension) throws IOException {
byte[] buffer = new byte[5];
sis.readFully(buffer);
try {
if (looksLikeCms(buffer) || (extension != null && ("ps7".equals(extension) || extension.startsWith("p7")))) {
return readAsCMS(sis);
}
} catch (Exception e) {
// ignore – detection should be resilient
}
return false;
}

private static boolean looksLikeCms(byte[] data) {
// ASN.1 DER SEQUENCE
return data.length > 2 && (data[0] & 0xFF) == 0x30;
}

private static boolean readAsCMS(SeekableInputStream sis) throws Exception {
Security.addProvider(
new org.bouncycastle.jce.provider.BouncyCastleProvider());
sis.seek(0);
final byte[] data = sis.readAllBytes();
CMSSignedData cms;
try {
// first try to decode in case it is base64-encoded
byte[] decodedBytes = Base64.getDecoder().decode(data);
cms = new CMSSignedData(decodedBytes);
} catch (IllegalArgumentException e) {
cms = new CMSSignedData(data);
}
return cms.getSignedContent() != null;
}
}

23 changes: 22 additions & 1 deletion src/main/resources/magic.xml
Original file line number Diff line number Diff line change
Expand Up @@ -761,6 +761,28 @@
<description>X-Rechnung type CreditNote</description>
<match-xml-metadata namespaceUri="urn:oasis:names:specification:ubl:schema:xsd:CreditNote-" rootElementName="CreditNote">x_rechnung</match-xml-metadata>
</type>
<type>
<description>E-Rezept</description>
<match-xml-metadata namespaceUri="http://hl7.org/fhir" rootElementName="Bundle">e_rezept</match-xml-metadata>
</type>
</type>

<type>
<mime-type>application/json</mime-type>
<extension>json</extension>
<description>JSON</description>
<match-regexp offset="0" range="1" >^(\{|\[)</match-regexp>
<type>
<description>E-Rezept</description>
<match-regexp offset="0" range="100" multiline="true">resourceType</match-regexp>
</type>
</type>

<type>
<mime-type>application/pkcs7-mime</mime-type>
<extension>p7s</extension>
<description>PKCS #7</description>
<match-custom>org.jadice.filetype.matchers.PKCS7Matcher</match-custom>
</type>

<type>
Expand All @@ -770,7 +792,6 @@
<match-regexp offset="4">ftypheif</match-regexp>
</type>


<type>
<mime-type>image/heic</mime-type>
<extension>heic</extension>
Expand Down
Loading
Loading