-
Notifications
You must be signed in to change notification settings - Fork 456
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#544] Add IP Address SAN to Certificates Information
- Loading branch information
1 parent
37760ef
commit 463c084
Showing
6 changed files
with
77 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,57 @@ | ||
from dataclasses import dataclass | ||
from hashlib import sha256 | ||
from typing import List, cast | ||
|
||
from cryptography import x509 | ||
from cryptography.hazmat.primitives.serialization import Encoding, PublicFormat | ||
from cryptography.x509 import ExtensionOID, DNSName, ExtensionNotFound, NameOID, DuplicateExtension | ||
|
||
|
||
def extract_dns_subject_alternative_names(certificate: x509.Certificate) -> List[str]: | ||
"""Retrieve all the DNS entries of the Subject Alternative Name extension.""" | ||
subj_alt_names: List[str] = [] | ||
from cryptography.x509 import ( | ||
ExtensionOID, | ||
DNSName, | ||
ExtensionNotFound, | ||
NameOID, | ||
DuplicateExtension, | ||
IPAddress, | ||
Certificate, | ||
SubjectAlternativeName, | ||
Name, | ||
) | ||
|
||
|
||
@dataclass(frozen=True) | ||
class SubjectAlternativeNameExtension: | ||
dns_names: List[str] | ||
ip_addresses: List[str] | ||
|
||
|
||
def parse_subject_alternative_name_extension(certificate: Certificate) -> SubjectAlternativeNameExtension: | ||
try: | ||
san_ext = certificate.extensions.get_extension_for_oid(ExtensionOID.SUBJECT_ALTERNATIVE_NAME) | ||
san_ext_value = cast(x509.SubjectAlternativeName, san_ext.value) | ||
subj_alt_names = san_ext_value.get_values_for_type(DNSName) | ||
san_ext_value = cast(SubjectAlternativeName, san_ext.value) | ||
except ExtensionNotFound: | ||
pass | ||
return SubjectAlternativeNameExtension(dns_names=[], ip_addresses=[]) | ||
except DuplicateExtension: | ||
# Fix for https://github.com/nabla-c0d3/sslyze/issues/420 | ||
# Not sure how browsers behave in this case but having a duplicate extension makes the certificate invalid | ||
# so we just return no SANs (likely to make hostname validation fail, which is fine) | ||
pass | ||
return SubjectAlternativeNameExtension(dns_names=[], ip_addresses=[]) | ||
|
||
dns_names = [] | ||
ip_addresses = [] | ||
for san_value in san_ext_value: | ||
if isinstance(san_value, IPAddress): | ||
ip_addresses.append(str(san_value.value)) | ||
elif isinstance(san_value, DNSName): | ||
dns_names.append(san_value.value) | ||
else: | ||
pass | ||
|
||
return subj_alt_names | ||
return SubjectAlternativeNameExtension(dns_names=dns_names, ip_addresses=ip_addresses) | ||
|
||
|
||
def get_common_names(name_field: x509.Name) -> List[str]: | ||
def get_common_names(name_field: Name) -> List[str]: | ||
return [cn.value for cn in name_field.get_attributes_for_oid(NameOID.COMMON_NAME)] # type: ignore | ||
|
||
|
||
def get_public_key_sha256(certificate: x509.Certificate) -> bytes: | ||
def get_public_key_sha256(certificate: Certificate) -> bytes: | ||
pub_bytes = certificate.public_key().public_bytes(encoding=Encoding.DER, format=PublicFormat.SubjectPublicKeyInfo) | ||
digest = sha256(pub_bytes).digest() | ||
return digest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters