diff --git a/README.rst b/README.rst index 1011f72..55cefdf 100644 --- a/README.rst +++ b/README.rst @@ -150,6 +150,7 @@ Document - **ppt** - ``application/vnd.ms-powerpoint`` - **pptx** - ``application/vnd.openxmlformats-officedocument.presentationml.presentation`` - **odp** - ``application/vnd.oasis.opendocument.presentation`` +- **p7s** - ``application/pkcs7-signed-data`` Font ^^^^ @@ -160,7 +161,7 @@ Font - **otf** - ``application/font-sfnt`` Application -^^^^^^^^^^^ +^^^^^^^^^^^ - **wasm** - ``application/wasm`` diff --git a/filetype/types/__init__.py b/filetype/types/__init__.py index 7d4daed..451d899 100644 --- a/filetype/types/__init__.py +++ b/filetype/types/__init__.py @@ -111,6 +111,7 @@ document.Ppt(), document.Pptx(), document.Odp(), + document.Pkcs7() ) diff --git a/filetype/types/document.py b/filetype/types/document.py index 653eaf1..082c158 100644 --- a/filetype/types/document.py +++ b/filetype/types/document.py @@ -258,3 +258,31 @@ class Odp(OpenDocument): def __init__(self): super(Odp, self).__init__(mime=Odp.MIME, extension=Odp.EXTENSION) + + +class Pkcs7(Type): + """ + Implements signed document in .p7s format type matcher + """ + MIME = "application/pkcs7-signed-data" + EXTENSION = "p7s" + + def __init__(self): + super(Pkcs7, self).__init__(mime=Pkcs7.MIME, extension=Pkcs7.EXTENSION) + + def match(self, buf): + if buf.startswith(b"-----BEGIN PKCS7"): + return True + if len(buf) < 20: + return False + + start_header = [ + bytearray([0x30, 0x80]), bytearray([0x30, 0x81]), bytearray([0x30, 0x82]), + bytearray([0x30, 0x83]), bytearray([0x30, 0x84]) + ] + signed_data_match = bytearray([0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07]) + for i, match in enumerate(start_header): + if buf.startswith(match): + if buf[i+2:].startswith(signed_data_match): + return True + return False diff --git a/tests/fixtures/p7s_der.p7s b/tests/fixtures/p7s_der.p7s new file mode 100644 index 0000000..d234a49 Binary files /dev/null and b/tests/fixtures/p7s_der.p7s differ diff --git a/tests/test_types.py b/tests/test_types.py index 6b1031f..b070fd6 100644 --- a/tests/test_types.py +++ b/tests/test_types.py @@ -143,3 +143,9 @@ def test_guess_odp(self): self.assertTrue(kind is not None) self.assertEqual(kind.mime, 'application/vnd.oasis.opendocument.presentation') self.assertEqual(kind.extension, 'odp') + + def test_guess_p7s(self): + kind = filetype.guess(FIXTURES + '/p7s_der.p7s') + self.assertTrue(kind is not None) + self.assertEqual(kind.mime, 'application/pkcs7-signed-data') + self.assertEqual(kind.extension, 'p7s')