Skip to content
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
rsa_public_from_der_certificate PEP8 fixes
- Use double quotes consistently
- Moved camel casing to lower_case_with_underscores casing
  • Loading branch information
Alex Moneger committed Oct 5, 2016
commit 1a4bda1cd03c488fc837fb98c9d815819cbb9720
30 changes: 15 additions & 15 deletions scapy_ssl_tls/ssl_tls_keystore.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,50 @@


def rsa_public_from_der_certificate(certificate):
# Extract subjectPublicKeyInfo field from X.509 certificate (see RFC3280)
# Extract subject_public_key_info field from X.509 certificate (see RFC3280)
try:
# try to extract pubkey from scapy.layers.x509 X509Cert type in case
# der_certificate is of type X509Cert
# Note: der_certificate may not be of type X509Cert if it wasn't
# received completely, in that case, we'll try to extract it anyway
# using the old method.
# TODO: get rid of the old method and always expect X509Cert obj ?
'''
"""
Rebuild ASN1 SubjectPublicKeyInfo since X509Cert does not provide the full struct

ASN1F_SEQUENCE(
ASN1F_SEQUENCE(ASN1F_OID("pubkey_algo","1.2.840.113549.1.1.1"),
ASN1F_field("pk_value",ASN1_NULL(0))),
ASN1F_BIT_STRING("pubkey","")
),
'''
subjectPublicKeyInfo = ASN1_SEQUENCE([ASN1_SEQUENCE([certificate.pubkey_algo,
certificate.pk_value]),
certificate.pubkey, ])
return RSA.importKey(str(subjectPublicKeyInfo))
"""
subject_public_key_info = ASN1_SEQUENCE([ASN1_SEQUENCE([certificate.pubkey_algo, certificate.pk_value]),
certificate.pubkey])
return RSA.importKey(str(subject_public_key_info))
except AttributeError:
pass

# Fallback method, may pot. allow to extract pubkey from incomplete der streams
cert = DerSequence()
cert.decode(certificate)

tbsCertificate = DerSequence()
tbsCertificate.decode(cert[0]) # first DER SEQUENCE
tbs_certificate = DerSequence()
tbs_certificate.decode(cert[0]) # first DER SEQUENCE

# search for pubkey OID: rsaEncryption: "1.2.840.113549.1.1.1"
# hex: 06 09 2A 86 48 86 F7 0D 01 01 01
subjectPublicKeyInfo=None
for seq in tbsCertificate:
if not isinstance(seq,basestring): continue # skip numerics and non sequence stuff
subject_public_key_info = None
for seq in tbs_certificate:
if not isinstance(seq, basestring):
continue # skip numerics and non sequence stuff
if "\x2A\x86\x48\x86\xF7\x0D\x01\x01\x01" in seq:
subjectPublicKeyInfo=seq
subject_public_key_info = seq

if not subjectPublicKeyInfo:
if subject_public_key_info is None:
raise ValueError("could not find OID rsaEncryption 1.2.840.113549.1.1.1 in certificate")

# Initialize RSA key
return RSA.importKey(subjectPublicKeyInfo)
return RSA.importKey(subject_public_key_info)


def rsa_public_from_pem_certificate(certificate):
Expand Down