Skip to content

Commit

Permalink
bugfix: add asn1crypto to requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
subho007 committed Mar 26, 2021
1 parent be2e0f2 commit 84410a8
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
21 changes: 15 additions & 6 deletions pyaxmlparser/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from builtins import str
from struct import unpack
from pyaxmlparser.utils import read, format_value
from pyaxmlparser.utils import read, format_value, get_certificate_name_string

from pyaxmlparser.arscparser import ARSCParser
from pyaxmlparser.axmlprinter import AXMLPrinter
Expand All @@ -20,10 +20,14 @@
import zipfile
import logging
import hashlib
import binascii

import lxml.sax
from xml.dom.pulldom import SAX2DOM

# Used for reading Certificates
from asn1crypto import cms, x509, keys


NS_ANDROID_URI = 'http://schemas.android.com/apk/res/android'
NS_ANDROID = '{{{}}}'.format(NS_ANDROID_URI) # Namespace as used by etree
Expand Down Expand Up @@ -217,7 +221,7 @@ class APK:

__no_magic = False

def __init__(self, filename, raw=False, magic_file=None, skip_analysis=False, testzip=False):
def __init__(self, filename, raw=False, skip_analysis=False, testzip=False):
"""
This class can access to all elements in an APK file
Expand All @@ -234,14 +238,10 @@ def __init__(self, filename, raw=False, magic_file=None, skip_analysis=False, te
:type filename: string
:type raw: boolean
:type magic_file: string
:type skip_analysis: boolean
:type testzip: boolean
"""
if magic_file:
log.warning("You set magic_file but this parameter is actually unused. You should remove it.")

self.filename = filename

self.xml = {}
Expand Down Expand Up @@ -646,6 +646,15 @@ def _get_file_magic_name(self, buffer):
# Magic is optional
import magic
except ImportError:
self.__no_magic = True
log.warning("No Magic library was found on your system.")
return default
except TypeError as e:
self.__no_magic = True
log.warning("It looks like you have the magic python package installed but not the magic library itself!")
log.warning("Error from magic library: %s", e)
log.warning("Please follow the installation instructions at https://github.com/ahupp/python-magic/#installation")
log.warning("You can also install the 'python-magic-bin' package on Windows and MacOS")
return default

try:
Expand Down
45 changes: 45 additions & 0 deletions pyaxmlparser/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from struct import unpack, pack

import lxml.sax
import asn1crypto


NS_ANDROID_URI = 'http://schemas.android.com/apk/res/android'
Expand Down Expand Up @@ -106,3 +107,47 @@ def read(filename, binary=True):
"""
with open(filename, 'rb' if binary else 'r') as f:
return f.read()


def get_certificate_name_string(name, short=False, delimiter=', '):
"""
Format the Name type of a X509 Certificate in a human readable form.
:param name: Name object to return the DN from
:param short: Use short form (default: False)
:param delimiter: Delimiter string or character between two parts (default: ', ')
:type name: dict or :class:`asn1crypto.x509.Name`
:type short: boolean
:type delimiter: str
:rtype: str
"""
if isinstance(name, asn1crypto.x509.Name):
name = name.native

# For the shortform, we have a lookup table
# See RFC4514 for more details
_ = {
'business_category': ("businessCategory", "businessCategory"),
'serial_number': ("serialNumber", "serialNumber"),
'country_name': ("C", "countryName"),
'postal_code': ("postalCode", "postalCode"),
'state_or_province_name': ("ST", "stateOrProvinceName"),
'locality_name': ("L", "localityName"),
'street_address': ("street", "streetAddress"),
'organization_name': ("O", "organizationName"),
'organizational_unit_name': ("OU", "organizationalUnitName"),
'title': ("title", "title"),
'common_name': ("CN", "commonName"),
'initials': ("initials", "initials"),
'generation_qualifier': ("generationQualifier", "generationQualifier"),
'surname': ("SN", "surname"),
'given_name': ("GN", "givenName"),
'name': ("name", "name"),
'pseudonym': ("pseudonym", "pseudonym"),
'dn_qualifier': ("dnQualifier", "dnQualifier"),
'telephone_number': ("telephoneNumber", "telephoneNumber"),
'email_address': ("E", "emailAddress"),
'domain_component': ("DC", "domainComponent"),
'name_distinguisher': ("nameDistinguisher", "nameDistinguisher"),
'organization_identifier': ("organizationIdentifier", "organizationIdentifier"),
}
return delimiter.join(["{}={}".format(_.get(attr, (attr, attr))[0 if short else 1], name[attr]) for attr in name])
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
lxml==4.6.2
click>=6.7
asn1crypto>=0.24.0

# testing
pytest==3.0.6
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
include_package_data=True,
zip_safe=False,
platforms='any',
install_requires=['lxml', 'click>=6.7'],
install_requires=['lxml', 'click>=6.7', 'asn1crypto>=0.24.0'],
entry_points='''
[console_scripts]
apkinfo = pyaxmlparser.cli:main
Expand Down

0 comments on commit 84410a8

Please sign in to comment.