99
1010from typing import Sequence
1111
12- from pyasn1 .codec .der .decoder import decode
13- from pyasn1 .type .char import IA5String
14- from pyasn1 .type .univ import ObjectIdentifier
15- from pyasn1_modules .rfc2459 import GeneralNames
16-
17- from .exceptions import CertificateError
12+ from .cryptography import extract_patterns as _cryptography_extract_patterns
1813from .hazmat import (
1914 DNS_ID ,
2015 CertificatePattern ,
21- DNSPattern ,
2216 IPAddress_ID ,
23- IPAddressPattern ,
24- SRVPattern ,
25- URIPattern ,
2617 verify_service_identity ,
2718)
2819
@@ -105,9 +96,6 @@ def verify_ip_address(connection: Connection, ip_address: str) -> None:
10596 )
10697
10798
108- ID_ON_DNS_SRV = ObjectIdentifier ("1.3.6.1.5.5.7.8.7" ) # id_on_dnsSRV
109-
110-
11199def extract_patterns (cert : X509 ) -> Sequence [CertificatePattern ]:
112100 """
113101 Extract all valid ID patterns from a certificate for service verification.
@@ -121,43 +109,7 @@ def extract_patterns(cert: X509) -> Sequence[CertificatePattern]:
121109 .. versionchanged:: 23.1.0
122110 ``commonName`` is not used as a fallback anymore.
123111 """
124- ids : list [CertificatePattern ] = []
125- for i in range (cert .get_extension_count ()):
126- ext = cert .get_extension (i )
127- if ext .get_short_name () == b"subjectAltName" :
128- names , _ = decode (ext .get_data (), asn1Spec = GeneralNames ())
129- for n in names :
130- name_string = n .getName ()
131- if name_string == "dNSName" :
132- ids .append (
133- DNSPattern .from_bytes (n .getComponent ().asOctets ())
134- )
135- elif name_string == "iPAddress" :
136- ids .append (
137- IPAddressPattern .from_bytes (
138- n .getComponent ().asOctets ()
139- )
140- )
141- elif name_string == "uniformResourceIdentifier" :
142- ids .append (
143- URIPattern .from_bytes (n .getComponent ().asOctets ())
144- )
145- elif name_string == "otherName" :
146- comp = n .getComponent ()
147- oid = comp .getComponentByPosition (0 )
148- if oid == ID_ON_DNS_SRV :
149- srv , _ = decode (comp .getComponentByPosition (1 ))
150- if isinstance (srv , IA5String ):
151- ids .append (SRVPattern .from_bytes (srv .asOctets ()))
152- else : # pragma: no cover
153- msg = "Unexpected certificate content."
154- raise CertificateError (msg )
155- else : # pragma: no cover
156- pass
157- else : # pragma: no cover
158- pass
159-
160- return ids
112+ return _cryptography_extract_patterns (cert .to_cryptography ())
161113
162114
163115def extract_ids (cert : X509 ) -> Sequence [CertificatePattern ]:
0 commit comments