Skip to content

Conversation

@sourcery-ai
Copy link

@sourcery-ai sourcery-ai bot commented Jul 30, 2023

Branch main refactored by Sourcery.

If you're happy with these changes, merge this Pull Request using the Squash and merge strategy.

See our documentation here.

Run Sourcery locally

Reduce the feedback loop during development by using the Sourcery editor plugin:

Review changes via command line

To manually merge these changes, make sure you're on the main branch, then run:

git fetch origin sourcery/main
git merge --ff-only FETCH_HEAD
git reset HEAD^

Help us improve this pull request!

@sourcery-ai sourcery-ai bot requested a review from TheScriptGuy July 30, 2023 05:22
Comment on lines -27 to +29
parser = argparse.ArgumentParser(description='Get Certificate Chain v' + scriptVersion)
parser = argparse.ArgumentParser(
description=f'Get Certificate Chain v{scriptVersion}'
)
Copy link
Author

Choose a reason for hiding this comment

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

Function parseArguments refactored with the following changes:

Comment on lines -139 to +141
# Make sure the filename string is lower case
newNormalizedName = ''.join(commonName).lower()

# Return newNormalizedName
return newNormalizedName
return ''.join(commonName).lower()
Copy link
Author

Choose a reason for hiding this comment

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

Function normalizeSubject refactored with the following changes:

This removes the following comments ( why? ):

# Make sure the filename string is lower case
# Return newNormalizedName

return certSKI
return __sslCertificate.extensions.get_extension_for_oid(
ExtensionOID.SUBJECT_KEY_IDENTIFIER
)
Copy link
Author

Choose a reason for hiding this comment

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

Function returnCertSKI refactored with the following changes:

Comment on lines -236 to +239
dataAIA = [x for x in certValue or []]
for item in dataAIA:
if item.access_method._name == "caIssuers":
aiaUriList.append(item.access_location._value)

dataAIA = list(certValue or [])
aiaUriList.extend(
item.access_location._value
for item in dataAIA
if item.access_method._name == "caIssuers"
)
Copy link
Author

Choose a reason for hiding this comment

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

Function returnCertAIAList refactored with the following changes:

Comment on lines -253 to +309
if __depth <= maxDepth:
# Retrive the AKI from the certificate.
certAKI = returnCertAKI(__sslCertificate)
# Retrieve the SKI from the certificate.
certSKI = returnCertSKI(__sslCertificate)
if __depth > maxDepth:
return
# Retrive the AKI from the certificate.
certAKI = returnCertAKI(__sslCertificate)
# Retrieve the SKI from the certificate.
certSKI = returnCertSKI(__sslCertificate)

# Sometimes the AKI can be none. Lets handle this accordingly.
if certAKI is not None:
certAKIValue = certAKI._value.key_identifier
else:
certAKIValue = None

# Get the value of the SKI from certSKI
certSKIValue = certSKI._value.digest

# Sometimes the AKI can be none. Lets handle this accordingly.
if certAKIValue is not None:
aiaUriList = returnCertAIAList(__sslCertificate)
if aiaUriList != []:
# Iterate through the aiaUriList list.
for item in aiaUriList:
# get the certificate for the item element.
nextCert = getCertificateFromUri(item)

# If the certificate is not none (great), append it to the certChain, increase the __depth and run the walkTheChain subroutine again.
if nextCert is not None:
certChain.append(nextCert)
__depth += 1
walkTheChain(nextCert, __depth)
else:
print("Could not retrieve certificate.")
sys.exit(1)
else:
"""Now we have to go on a hunt to find the root from a standard root store."""
print("Certificate didn't have AIA...ruh roh.")

# Load the Root CA Cert Chain.
caRootStore = loadRootCACertChain("cacert.pem")

# Assume we cannot find a Root CA
rootCACN = None

# Iterate through the caRootStore object.
for rootCA in caRootStore:
try:
rootCACertificatePEM = caRootStore[rootCA]
rootCACertificate = x509.load_pem_x509_certificate(rootCACertificatePEM.encode('ascii'))
rootCASKI = returnCertSKI(rootCACertificate)
rootCASKI_Value = rootCASKI._value.digest
if rootCASKI_Value == certAKIValue:
rootCACN = rootCA
print(f"Root CA Found - {rootCACN}")
certChain.append(rootCACertificate)
break
except x509.extensions.ExtensionNotFound:
# Apparently some Root CA's don't have a SKI?
pass

if rootCACN is None:
print("ERROR - Root CA NOT found.")
certAKIValue = certAKI._value.key_identifier if certAKI is not None else None
# Get the value of the SKI from certSKI
certSKIValue = certSKI._value.digest

# Sometimes the AKI can be none. Lets handle this accordingly.
if certAKIValue is not None:
aiaUriList = returnCertAIAList(__sslCertificate)
if aiaUriList != []:
# Iterate through the aiaUriList list.
for item in aiaUriList:
# get the certificate for the item element.
nextCert = getCertificateFromUri(item)

# If the certificate is not none (great), append it to the certChain, increase the __depth and run the walkTheChain subroutine again.
if nextCert is not None:
certChain.append(nextCert)
__depth += 1
walkTheChain(nextCert, __depth)
else:
print("Could not retrieve certificate.")
sys.exit(1)
else:
"""Now we have to go on a hunt to find the root from a standard root store."""
print("Certificate didn't have AIA...ruh roh.")

# Load the Root CA Cert Chain.
caRootStore = loadRootCACertChain("cacert.pem")

# Assume we cannot find a Root CA
rootCACN = None

# Iterate through the caRootStore object.
for rootCA in caRootStore:
try:
rootCACertificatePEM = caRootStore[rootCA]
rootCACertificate = x509.load_pem_x509_certificate(rootCACertificatePEM.encode('ascii'))
rootCASKI = returnCertSKI(rootCACertificate)
rootCASKI_Value = rootCASKI._value.digest
if rootCASKI_Value == certAKIValue:
rootCACN = rootCA
print(f"Root CA Found - {rootCACN}")
certChain.append(rootCACertificate)
break
except x509.extensions.ExtensionNotFound:
# Apparently some Root CA's don't have a SKI?
pass

if rootCACN is None:
print("ERROR - Root CA NOT found.")
sys.exit(1)
Copy link
Author

Choose a reason for hiding this comment

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

Function walkTheChain refactored with the following changes:

Comment on lines -337 to +333
sslCertificateFilename = str(len(__certificateChain) - 1 - counter) + '-' + normalizedSubject + '.crt'
sslCertificateFilename = f'{str(len(__certificateChain) - 1 - counter)}-{normalizedSubject}.crt'
Copy link
Author

Choose a reason for hiding this comment

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

Function writeChainToFile refactored with the following changes:

Comment on lines -347 to +348
# If the ':' is in the hostname argument, then we'll assume it's meant to be a port following the ':'.
if ":" in args.hostname:
tmpLine = args.hostname.split(':')
hostnameQuery = {"hostname": tmpLine[0], "port": int(tmpLine[1])}

else:
if ":" not in args.hostname:
# If no ':' is found, then set default port 443.
hostnameQuery = {"hostname": args.hostname, "port": 443}
return {"hostname": args.hostname, "port": 443}

return hostnameQuery
tmpLine = args.hostname.split(':')
return {"hostname": tmpLine[0], "port": int(tmpLine[1])}
Copy link
Author

Choose a reason for hiding this comment

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

Function checkHostname refactored with the following changes:

This removes the following comments ( why? ):

# If the ':' is in the hostname argument, then we'll assume it's meant to be a port following the ':'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant