Skip to content

add support of p7s files #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^
Expand All @@ -160,7 +161,7 @@ Font
- **otf** - ``application/font-sfnt``

Application
^^^^^^^^^^^
^^^^^^^^^^^

- **wasm** - ``application/wasm``

Expand Down
1 change: 1 addition & 0 deletions filetype/types/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
document.Ppt(),
document.Pptx(),
document.Odp(),
document.Pkcs7()
)


Expand Down
28 changes: 28 additions & 0 deletions filetype/types/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is parsed ASN1 file which may be a different length. Check possibly samples here: https://lapo.it/asn1js/
full OID may be checked too for x30:x82:
contentType ContentType OBJECT IDENTIFIER 1.2.840.113549.1.7.2 signedData (PKCS #7)

if buf.startswith(match):
if buf[i+2:].startswith(signed_data_match):
return True
return False
Binary file added tests/fixtures/p7s_der.p7s
Binary file not shown.
6 changes: 6 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')