From 078b6a5e3686686d4d463a1cfa861921f239e0c9 Mon Sep 17 00:00:00 2001 From: Olexandr Date: Thu, 12 Oct 2023 11:49:49 +0300 Subject: [PATCH] add support of p7s files --- README.rst | 3 ++- filetype/types/__init__.py | 1 + filetype/types/document.py | 28 ++++++++++++++++++++++++++++ tests/fixtures/p7s_der.p7s | Bin 0 -> 298 bytes tests/test_types.py | 6 ++++++ 5 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 tests/fixtures/p7s_der.p7s 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 0000000000000000000000000000000000000000..d234a49f60e4bebd125e58531486c1602885fa43 GIT binary patch literal 298 zcmXqLVpL<})N1o+`_9YA&a|M3QQV-3QJ9I5(U8G_8zIAJ*!a(&@i!AAlYyFnkfDG9 z9~*Nh3$rl0b7E0WP_QGDfSXD7@dk#lJ%X3>^o|H@-T=~_(B{FI%FM#VxWLdrA8r^Y zi=h&W!p1|>+)U?O_By>_Q~mrY9&^t!?U?!h#hF#}XYNTX=;T_3WIH1_OJhSx%+%^S z<6BQ|FI;q^a@K+S_6IC?Z4)uF@LifAE`4m?-KxxGOSA2CmxsJFT~zpdg3RKH5B?uK z`<3}>`-d|}omeJs+En#q_WfFu>l0oD+Di+`)qYTk;s3ijOwOYwVf|zGYghC{RgL~7 fJ2*T#82Xb}Y|XKE-Q{QAe|uN&cihoTd%iRPw^eq! literal 0 HcmV?d00001 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')