Skip to content

Commit

Permalink
Version 0.48. See CHANGELOG.md
Browse files Browse the repository at this point in the history
  • Loading branch information
Git committed Aug 13, 2023
1 parent c765eb0 commit 47adb5b
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions certificate/getCertificateChain.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,16 @@

import ssl
import socket
from cryptography import x509
from cryptography.x509.oid import ExtensionOID
from cryptography.hazmat.primitives import hashes, serialization

import requests
import sys
import re
import hashlib

from cryptography import x509
from cryptography.x509.oid import ExtensionOID
from cryptography.hazmat.primitives import hashes, serialization
from typing import Optional


class getCertificateChain:
"""
Expand All @@ -23,7 +24,9 @@ class getCertificateChain:
later use.
"""
@staticmethod
def loadRootCACertChain(__filename: str) -> dict:
def loadRootCACertChain(
__filename: str
) -> dict:
"""
Load the Root CA Chain in a structured format.
caRootStore = {
Expand Down Expand Up @@ -75,7 +78,10 @@ def loadRootCACertChain(__filename: str) -> dict:
sys.exit(1)

@staticmethod
def getCertificate(__hostname: str, __port: int) -> x509.Certificate:
def getCertificate(
__hostname: str,
__port: int
) -> x509.Certificate:
"""Retrieves the certificate from the website."""
try:
"""
Expand All @@ -84,10 +90,9 @@ def getCertificate(__hostname: str, __port: int) -> x509.Certificate:
"""
sslContext = ssl._create_unverified_context()

with socket.create_connection((__hostname, __port)) as sock:
with sslContext.wrap_socket(sock, server_hostname=__hostname) as sslSocket:
# Get the certificate from the connection, convert it to PEM format.
sslCertificate = ssl.DER_cert_to_PEM_cert(sslSocket.getpeercert(True))
with socket.create_connection((__hostname, __port)) as sock, sslContext.wrap_socket(sock, server_hostname=__hostname) as sslSocket:
# Get the certificate from the connection, convert it to PEM format.
sslCertificate = ssl.DER_cert_to_PEM_cert(sslSocket.getpeercert(True))

# Load the PEM formatted file.
sslCertificate = x509.load_pem_x509_certificate(sslCertificate.encode('ascii'))
Expand All @@ -100,7 +105,9 @@ def getCertificate(__hostname: str, __port: int) -> x509.Certificate:
return sslCertificate

@staticmethod
def getCertificateFromUri(__uri: str) -> str:
def getCertificateFromUri(
__uri: str
) -> str:
"""Gets the certificate from a URI.
By default, we're expecting to find nothing. Therefore certI = None.
If we find something, we'll update certI accordingly.
Expand All @@ -125,7 +132,7 @@ def getCertificateFromUri(__uri: str) -> str:
return certI

@staticmethod
def returnCertAKI(__sslCertificate: x509.Certificate) -> x509.extensions.Extension:
def returnCertAKI(__sslCertificate: x509.Certificate) -> Optional[x509.extensions.Extension]:
"""Returns the AKI of the certificate."""
try:
certAKI = __sslCertificate.extensions.get_extension_for_oid(ExtensionOID.AUTHORITY_KEY_IDENTIFIER)
Expand All @@ -134,14 +141,14 @@ def returnCertAKI(__sslCertificate: x509.Certificate) -> x509.extensions.Extensi
return certAKI

@staticmethod
def returnCertSKI(__sslCertificate):
def returnCertSKI(__sslCertificate: x509.Certificate) -> x509.extensions.Extension:
"""Returns the SKI of the certificate."""
certSKI = __sslCertificate.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_KEY_IDENTIFIER)

return certSKI

@staticmethod
def returnCertAIA(__sslCertificate):
def returnCertAIA(__sslCertificate: x509.Certificate) -> Optional[x509.extensions.Extension]:
"""Returns the AIA of the certificate. If not defined, then return None."""
try:
certAIA = __sslCertificate.extensions.get_extension_for_oid(ExtensionOID.AUTHORITY_INFORMATION_ACCESS)
Expand Down

0 comments on commit 47adb5b

Please sign in to comment.