12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093 |
- # This file is dual licensed under the terms of the Apache License, Version
- # 2.0, and the BSD License. See the LICENSE file in the root of this repository
- # for complete details.
- import abc
- import datetime
- import hashlib
- import ipaddress
- import typing
- from cryptography import utils
- from cryptography.hazmat.bindings._rust import asn1
- from cryptography.hazmat.primitives import constant_time, serialization
- from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey
- from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
- from cryptography.hazmat.primitives.asymmetric.types import PUBLIC_KEY_TYPES
- from cryptography.x509.certificate_transparency import (
- SignedCertificateTimestamp,
- )
- from cryptography.x509.general_name import (
- DNSName,
- DirectoryName,
- GeneralName,
- IPAddress,
- OtherName,
- RFC822Name,
- RegisteredID,
- UniformResourceIdentifier,
- _IPADDRESS_TYPES,
- )
- from cryptography.x509.name import Name, RelativeDistinguishedName
- from cryptography.x509.oid import (
- CRLEntryExtensionOID,
- ExtensionOID,
- OCSPExtensionOID,
- ObjectIdentifier,
- )
- ExtensionTypeVar = typing.TypeVar("ExtensionTypeVar", bound="ExtensionType")
- def _key_identifier_from_public_key(public_key: PUBLIC_KEY_TYPES) -> bytes:
- if isinstance(public_key, RSAPublicKey):
- data = public_key.public_bytes(
- serialization.Encoding.DER,
- serialization.PublicFormat.PKCS1,
- )
- elif isinstance(public_key, EllipticCurvePublicKey):
- data = public_key.public_bytes(
- serialization.Encoding.X962,
- serialization.PublicFormat.UncompressedPoint,
- )
- else:
- # This is a very slow way to do this.
- serialized = public_key.public_bytes(
- serialization.Encoding.DER,
- serialization.PublicFormat.SubjectPublicKeyInfo,
- )
- data = asn1.parse_spki_for_data(serialized)
- return hashlib.sha1(data).digest()
- def _make_sequence_methods(field_name: str):
- def len_method(self) -> int:
- return len(getattr(self, field_name))
- def iter_method(self):
- return iter(getattr(self, field_name))
- def getitem_method(self, idx):
- return getattr(self, field_name)[idx]
- return len_method, iter_method, getitem_method
- class DuplicateExtension(Exception):
- def __init__(self, msg: str, oid: ObjectIdentifier) -> None:
- super(DuplicateExtension, self).__init__(msg)
- self.oid = oid
- class ExtensionNotFound(Exception):
- def __init__(self, msg: str, oid: ObjectIdentifier) -> None:
- super(ExtensionNotFound, self).__init__(msg)
- self.oid = oid
- class ExtensionType(metaclass=abc.ABCMeta):
- oid: typing.ClassVar[ObjectIdentifier]
- class Extensions(object):
- def __init__(
- self, extensions: typing.Iterable["Extension[ExtensionType]"]
- ) -> None:
- self._extensions = list(extensions)
- def get_extension_for_oid(
- self, oid: ObjectIdentifier
- ) -> "Extension[ExtensionType]":
- for ext in self:
- if ext.oid == oid:
- return ext
- raise ExtensionNotFound("No {} extension was found".format(oid), oid)
- def get_extension_for_class(
- self, extclass: typing.Type[ExtensionTypeVar]
- ) -> "Extension[ExtensionTypeVar]":
- if extclass is UnrecognizedExtension:
- raise TypeError(
- "UnrecognizedExtension can't be used with "
- "get_extension_for_class because more than one instance of the"
- " class may be present."
- )
- for ext in self:
- if isinstance(ext.value, extclass):
- return ext
- raise ExtensionNotFound(
- "No {} extension was found".format(extclass), extclass.oid
- )
- __len__, __iter__, __getitem__ = _make_sequence_methods("_extensions")
- def __repr__(self) -> str:
- return "<Extensions({})>".format(self._extensions)
- class CRLNumber(ExtensionType):
- oid = ExtensionOID.CRL_NUMBER
- def __init__(self, crl_number: int) -> None:
- if not isinstance(crl_number, int):
- raise TypeError("crl_number must be an integer")
- self._crl_number = crl_number
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, CRLNumber):
- return NotImplemented
- return self.crl_number == other.crl_number
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.crl_number)
- def __repr__(self) -> str:
- return "<CRLNumber({})>".format(self.crl_number)
- @property
- def crl_number(self) -> int:
- return self._crl_number
- class AuthorityKeyIdentifier(ExtensionType):
- oid = ExtensionOID.AUTHORITY_KEY_IDENTIFIER
- def __init__(
- self,
- key_identifier: typing.Optional[bytes],
- authority_cert_issuer: typing.Optional[typing.Iterable[GeneralName]],
- authority_cert_serial_number: typing.Optional[int],
- ) -> None:
- if (authority_cert_issuer is None) != (
- authority_cert_serial_number is None
- ):
- raise ValueError(
- "authority_cert_issuer and authority_cert_serial_number "
- "must both be present or both None"
- )
- if authority_cert_issuer is not None:
- authority_cert_issuer = list(authority_cert_issuer)
- if not all(
- isinstance(x, GeneralName) for x in authority_cert_issuer
- ):
- raise TypeError(
- "authority_cert_issuer must be a list of GeneralName "
- "objects"
- )
- if authority_cert_serial_number is not None and not isinstance(
- authority_cert_serial_number, int
- ):
- raise TypeError("authority_cert_serial_number must be an integer")
- self._key_identifier = key_identifier
- self._authority_cert_issuer = authority_cert_issuer
- self._authority_cert_serial_number = authority_cert_serial_number
- @classmethod
- def from_issuer_public_key(
- cls, public_key: PUBLIC_KEY_TYPES
- ) -> "AuthorityKeyIdentifier":
- digest = _key_identifier_from_public_key(public_key)
- return cls(
- key_identifier=digest,
- authority_cert_issuer=None,
- authority_cert_serial_number=None,
- )
- @classmethod
- def from_issuer_subject_key_identifier(
- cls, ski: "SubjectKeyIdentifier"
- ) -> "AuthorityKeyIdentifier":
- return cls(
- key_identifier=ski.digest,
- authority_cert_issuer=None,
- authority_cert_serial_number=None,
- )
- def __repr__(self) -> str:
- return (
- "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, "
- "authority_cert_issuer={0.authority_cert_issuer}, "
- "authority_cert_serial_number={0.authority_cert_serial_number}"
- ")>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, AuthorityKeyIdentifier):
- return NotImplemented
- return (
- self.key_identifier == other.key_identifier
- and self.authority_cert_issuer == other.authority_cert_issuer
- and self.authority_cert_serial_number
- == other.authority_cert_serial_number
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- if self.authority_cert_issuer is None:
- aci = None
- else:
- aci = tuple(self.authority_cert_issuer)
- return hash(
- (self.key_identifier, aci, self.authority_cert_serial_number)
- )
- @property
- def key_identifier(self) -> typing.Optional[bytes]:
- return self._key_identifier
- @property
- def authority_cert_issuer(
- self,
- ) -> typing.Optional[typing.List[GeneralName]]:
- return self._authority_cert_issuer
- @property
- def authority_cert_serial_number(self) -> typing.Optional[int]:
- return self._authority_cert_serial_number
- class SubjectKeyIdentifier(ExtensionType):
- oid = ExtensionOID.SUBJECT_KEY_IDENTIFIER
- def __init__(self, digest: bytes) -> None:
- self._digest = digest
- @classmethod
- def from_public_key(
- cls, public_key: PUBLIC_KEY_TYPES
- ) -> "SubjectKeyIdentifier":
- return cls(_key_identifier_from_public_key(public_key))
- @property
- def digest(self) -> bytes:
- return self._digest
- @property
- def key_identifier(self) -> bytes:
- return self._digest
- def __repr__(self) -> str:
- return "<SubjectKeyIdentifier(digest={0!r})>".format(self.digest)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, SubjectKeyIdentifier):
- return NotImplemented
- return constant_time.bytes_eq(self.digest, other.digest)
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.digest)
- class AuthorityInformationAccess(ExtensionType):
- oid = ExtensionOID.AUTHORITY_INFORMATION_ACCESS
- def __init__(
- self, descriptions: typing.Iterable["AccessDescription"]
- ) -> None:
- descriptions = list(descriptions)
- if not all(isinstance(x, AccessDescription) for x in descriptions):
- raise TypeError(
- "Every item in the descriptions list must be an "
- "AccessDescription"
- )
- self._descriptions = descriptions
- __len__, __iter__, __getitem__ = _make_sequence_methods("_descriptions")
- def __repr__(self) -> str:
- return "<AuthorityInformationAccess({})>".format(self._descriptions)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, AuthorityInformationAccess):
- return NotImplemented
- return self._descriptions == other._descriptions
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._descriptions))
- class SubjectInformationAccess(ExtensionType):
- oid = ExtensionOID.SUBJECT_INFORMATION_ACCESS
- def __init__(
- self, descriptions: typing.Iterable["AccessDescription"]
- ) -> None:
- descriptions = list(descriptions)
- if not all(isinstance(x, AccessDescription) for x in descriptions):
- raise TypeError(
- "Every item in the descriptions list must be an "
- "AccessDescription"
- )
- self._descriptions = descriptions
- __len__, __iter__, __getitem__ = _make_sequence_methods("_descriptions")
- def __repr__(self) -> str:
- return "<SubjectInformationAccess({})>".format(self._descriptions)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, SubjectInformationAccess):
- return NotImplemented
- return self._descriptions == other._descriptions
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._descriptions))
- class AccessDescription(object):
- def __init__(
- self, access_method: ObjectIdentifier, access_location: GeneralName
- ) -> None:
- if not isinstance(access_method, ObjectIdentifier):
- raise TypeError("access_method must be an ObjectIdentifier")
- if not isinstance(access_location, GeneralName):
- raise TypeError("access_location must be a GeneralName")
- self._access_method = access_method
- self._access_location = access_location
- def __repr__(self) -> str:
- return (
- "<AccessDescription(access_method={0.access_method}, access_locati"
- "on={0.access_location})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, AccessDescription):
- return NotImplemented
- return (
- self.access_method == other.access_method
- and self.access_location == other.access_location
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.access_method, self.access_location))
- @property
- def access_method(self) -> ObjectIdentifier:
- return self._access_method
- @property
- def access_location(self) -> GeneralName:
- return self._access_location
- class BasicConstraints(ExtensionType):
- oid = ExtensionOID.BASIC_CONSTRAINTS
- def __init__(self, ca: bool, path_length: typing.Optional[int]) -> None:
- if not isinstance(ca, bool):
- raise TypeError("ca must be a boolean value")
- if path_length is not None and not ca:
- raise ValueError("path_length must be None when ca is False")
- if path_length is not None and (
- not isinstance(path_length, int) or path_length < 0
- ):
- raise TypeError(
- "path_length must be a non-negative integer or None"
- )
- self._ca = ca
- self._path_length = path_length
- @property
- def ca(self) -> bool:
- return self._ca
- @property
- def path_length(self) -> typing.Optional[int]:
- return self._path_length
- def __repr__(self) -> str:
- return (
- "<BasicConstraints(ca={0.ca}, " "path_length={0.path_length})>"
- ).format(self)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, BasicConstraints):
- return NotImplemented
- return self.ca == other.ca and self.path_length == other.path_length
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.ca, self.path_length))
- class DeltaCRLIndicator(ExtensionType):
- oid = ExtensionOID.DELTA_CRL_INDICATOR
- def __init__(self, crl_number: int) -> None:
- if not isinstance(crl_number, int):
- raise TypeError("crl_number must be an integer")
- self._crl_number = crl_number
- @property
- def crl_number(self) -> int:
- return self._crl_number
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, DeltaCRLIndicator):
- return NotImplemented
- return self.crl_number == other.crl_number
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.crl_number)
- def __repr__(self) -> str:
- return "<DeltaCRLIndicator(crl_number={0.crl_number})>".format(self)
- class CRLDistributionPoints(ExtensionType):
- oid = ExtensionOID.CRL_DISTRIBUTION_POINTS
- def __init__(
- self, distribution_points: typing.Iterable["DistributionPoint"]
- ) -> None:
- distribution_points = list(distribution_points)
- if not all(
- isinstance(x, DistributionPoint) for x in distribution_points
- ):
- raise TypeError(
- "distribution_points must be a list of DistributionPoint "
- "objects"
- )
- self._distribution_points = distribution_points
- __len__, __iter__, __getitem__ = _make_sequence_methods(
- "_distribution_points"
- )
- def __repr__(self) -> str:
- return "<CRLDistributionPoints({})>".format(self._distribution_points)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, CRLDistributionPoints):
- return NotImplemented
- return self._distribution_points == other._distribution_points
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._distribution_points))
- class FreshestCRL(ExtensionType):
- oid = ExtensionOID.FRESHEST_CRL
- def __init__(
- self, distribution_points: typing.Iterable["DistributionPoint"]
- ) -> None:
- distribution_points = list(distribution_points)
- if not all(
- isinstance(x, DistributionPoint) for x in distribution_points
- ):
- raise TypeError(
- "distribution_points must be a list of DistributionPoint "
- "objects"
- )
- self._distribution_points = distribution_points
- __len__, __iter__, __getitem__ = _make_sequence_methods(
- "_distribution_points"
- )
- def __repr__(self) -> str:
- return "<FreshestCRL({})>".format(self._distribution_points)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, FreshestCRL):
- return NotImplemented
- return self._distribution_points == other._distribution_points
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._distribution_points))
- class DistributionPoint(object):
- def __init__(
- self,
- full_name: typing.Optional[typing.Iterable[GeneralName]],
- relative_name: typing.Optional[RelativeDistinguishedName],
- reasons: typing.Optional[typing.FrozenSet["ReasonFlags"]],
- crl_issuer: typing.Optional[typing.Iterable[GeneralName]],
- ) -> None:
- if full_name and relative_name:
- raise ValueError(
- "You cannot provide both full_name and relative_name, at "
- "least one must be None."
- )
- if full_name is not None:
- full_name = list(full_name)
- if not all(isinstance(x, GeneralName) for x in full_name):
- raise TypeError(
- "full_name must be a list of GeneralName objects"
- )
- if relative_name:
- if not isinstance(relative_name, RelativeDistinguishedName):
- raise TypeError(
- "relative_name must be a RelativeDistinguishedName"
- )
- if crl_issuer is not None:
- crl_issuer = list(crl_issuer)
- if not all(isinstance(x, GeneralName) for x in crl_issuer):
- raise TypeError(
- "crl_issuer must be None or a list of general names"
- )
- if reasons and (
- not isinstance(reasons, frozenset)
- or not all(isinstance(x, ReasonFlags) for x in reasons)
- ):
- raise TypeError("reasons must be None or frozenset of ReasonFlags")
- if reasons and (
- ReasonFlags.unspecified in reasons
- or ReasonFlags.remove_from_crl in reasons
- ):
- raise ValueError(
- "unspecified and remove_from_crl are not valid reasons in a "
- "DistributionPoint"
- )
- if reasons and not crl_issuer and not (full_name or relative_name):
- raise ValueError(
- "You must supply crl_issuer, full_name, or relative_name when "
- "reasons is not None"
- )
- self._full_name = full_name
- self._relative_name = relative_name
- self._reasons = reasons
- self._crl_issuer = crl_issuer
- def __repr__(self) -> str:
- return (
- "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela"
- "tive_name}, reasons={0.reasons}, "
- "crl_issuer={0.crl_issuer})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, DistributionPoint):
- return NotImplemented
- return (
- self.full_name == other.full_name
- and self.relative_name == other.relative_name
- and self.reasons == other.reasons
- and self.crl_issuer == other.crl_issuer
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- if self.full_name is not None:
- fn: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
- self.full_name
- )
- else:
- fn = None
- if self.crl_issuer is not None:
- crl_issuer: typing.Optional[
- typing.Tuple[GeneralName, ...]
- ] = tuple(self.crl_issuer)
- else:
- crl_issuer = None
- return hash((fn, self.relative_name, self.reasons, crl_issuer))
- @property
- def full_name(self) -> typing.Optional[typing.List[GeneralName]]:
- return self._full_name
- @property
- def relative_name(self) -> typing.Optional[RelativeDistinguishedName]:
- return self._relative_name
- @property
- def reasons(self) -> typing.Optional[typing.FrozenSet["ReasonFlags"]]:
- return self._reasons
- @property
- def crl_issuer(self) -> typing.Optional[typing.List[GeneralName]]:
- return self._crl_issuer
- class ReasonFlags(utils.Enum):
- unspecified = "unspecified"
- key_compromise = "keyCompromise"
- ca_compromise = "cACompromise"
- affiliation_changed = "affiliationChanged"
- superseded = "superseded"
- cessation_of_operation = "cessationOfOperation"
- certificate_hold = "certificateHold"
- privilege_withdrawn = "privilegeWithdrawn"
- aa_compromise = "aACompromise"
- remove_from_crl = "removeFromCRL"
- # These are distribution point bit string mappings. Not to be confused with
- # CRLReason reason flags bit string mappings.
- # ReasonFlags ::= BIT STRING {
- # unused (0),
- # keyCompromise (1),
- # cACompromise (2),
- # affiliationChanged (3),
- # superseded (4),
- # cessationOfOperation (5),
- # certificateHold (6),
- # privilegeWithdrawn (7),
- # aACompromise (8) }
- _REASON_BIT_MAPPING = {
- 1: ReasonFlags.key_compromise,
- 2: ReasonFlags.ca_compromise,
- 3: ReasonFlags.affiliation_changed,
- 4: ReasonFlags.superseded,
- 5: ReasonFlags.cessation_of_operation,
- 6: ReasonFlags.certificate_hold,
- 7: ReasonFlags.privilege_withdrawn,
- 8: ReasonFlags.aa_compromise,
- }
- class PolicyConstraints(ExtensionType):
- oid = ExtensionOID.POLICY_CONSTRAINTS
- def __init__(
- self,
- require_explicit_policy: typing.Optional[int],
- inhibit_policy_mapping: typing.Optional[int],
- ) -> None:
- if require_explicit_policy is not None and not isinstance(
- require_explicit_policy, int
- ):
- raise TypeError(
- "require_explicit_policy must be a non-negative integer or "
- "None"
- )
- if inhibit_policy_mapping is not None and not isinstance(
- inhibit_policy_mapping, int
- ):
- raise TypeError(
- "inhibit_policy_mapping must be a non-negative integer or None"
- )
- if inhibit_policy_mapping is None and require_explicit_policy is None:
- raise ValueError(
- "At least one of require_explicit_policy and "
- "inhibit_policy_mapping must not be None"
- )
- self._require_explicit_policy = require_explicit_policy
- self._inhibit_policy_mapping = inhibit_policy_mapping
- def __repr__(self) -> str:
- return (
- "<PolicyConstraints(require_explicit_policy={0.require_explicit"
- "_policy}, inhibit_policy_mapping={0.inhibit_policy_"
- "mapping})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, PolicyConstraints):
- return NotImplemented
- return (
- self.require_explicit_policy == other.require_explicit_policy
- and self.inhibit_policy_mapping == other.inhibit_policy_mapping
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(
- (self.require_explicit_policy, self.inhibit_policy_mapping)
- )
- @property
- def require_explicit_policy(self) -> typing.Optional[int]:
- return self._require_explicit_policy
- @property
- def inhibit_policy_mapping(self) -> typing.Optional[int]:
- return self._inhibit_policy_mapping
- class CertificatePolicies(ExtensionType):
- oid = ExtensionOID.CERTIFICATE_POLICIES
- def __init__(self, policies: typing.Iterable["PolicyInformation"]) -> None:
- policies = list(policies)
- if not all(isinstance(x, PolicyInformation) for x in policies):
- raise TypeError(
- "Every item in the policies list must be a "
- "PolicyInformation"
- )
- self._policies = policies
- __len__, __iter__, __getitem__ = _make_sequence_methods("_policies")
- def __repr__(self) -> str:
- return "<CertificatePolicies({})>".format(self._policies)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, CertificatePolicies):
- return NotImplemented
- return self._policies == other._policies
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._policies))
- class PolicyInformation(object):
- def __init__(
- self,
- policy_identifier: ObjectIdentifier,
- policy_qualifiers: typing.Optional[
- typing.Iterable[typing.Union[str, "UserNotice"]]
- ],
- ) -> None:
- if not isinstance(policy_identifier, ObjectIdentifier):
- raise TypeError("policy_identifier must be an ObjectIdentifier")
- self._policy_identifier = policy_identifier
- if policy_qualifiers is not None:
- policy_qualifiers = list(policy_qualifiers)
- if not all(
- isinstance(x, (str, UserNotice)) for x in policy_qualifiers
- ):
- raise TypeError(
- "policy_qualifiers must be a list of strings and/or "
- "UserNotice objects or None"
- )
- self._policy_qualifiers = policy_qualifiers
- def __repr__(self) -> str:
- return (
- "<PolicyInformation(policy_identifier={0.policy_identifier}, polic"
- "y_qualifiers={0.policy_qualifiers})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, PolicyInformation):
- return NotImplemented
- return (
- self.policy_identifier == other.policy_identifier
- and self.policy_qualifiers == other.policy_qualifiers
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- if self.policy_qualifiers is not None:
- pq: typing.Optional[
- typing.Tuple[typing.Union[str, "UserNotice"], ...]
- ] = tuple(self.policy_qualifiers)
- else:
- pq = None
- return hash((self.policy_identifier, pq))
- @property
- def policy_identifier(self) -> ObjectIdentifier:
- return self._policy_identifier
- @property
- def policy_qualifiers(
- self,
- ) -> typing.Optional[typing.List[typing.Union[str, "UserNotice"]]]:
- return self._policy_qualifiers
- class UserNotice(object):
- def __init__(
- self,
- notice_reference: typing.Optional["NoticeReference"],
- explicit_text: typing.Optional[str],
- ) -> None:
- if notice_reference and not isinstance(
- notice_reference, NoticeReference
- ):
- raise TypeError(
- "notice_reference must be None or a NoticeReference"
- )
- self._notice_reference = notice_reference
- self._explicit_text = explicit_text
- def __repr__(self) -> str:
- return (
- "<UserNotice(notice_reference={0.notice_reference}, explicit_text="
- "{0.explicit_text!r})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, UserNotice):
- return NotImplemented
- return (
- self.notice_reference == other.notice_reference
- and self.explicit_text == other.explicit_text
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.notice_reference, self.explicit_text))
- @property
- def notice_reference(self) -> typing.Optional["NoticeReference"]:
- return self._notice_reference
- @property
- def explicit_text(self) -> typing.Optional[str]:
- return self._explicit_text
- class NoticeReference(object):
- def __init__(
- self,
- organization: typing.Optional[str],
- notice_numbers: typing.Iterable[int],
- ) -> None:
- self._organization = organization
- notice_numbers = list(notice_numbers)
- if not all(isinstance(x, int) for x in notice_numbers):
- raise TypeError("notice_numbers must be a list of integers")
- self._notice_numbers = notice_numbers
- def __repr__(self) -> str:
- return (
- "<NoticeReference(organization={0.organization!r}, notice_numbers="
- "{0.notice_numbers})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, NoticeReference):
- return NotImplemented
- return (
- self.organization == other.organization
- and self.notice_numbers == other.notice_numbers
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.organization, tuple(self.notice_numbers)))
- @property
- def organization(self) -> typing.Optional[str]:
- return self._organization
- @property
- def notice_numbers(self) -> typing.List[int]:
- return self._notice_numbers
- class ExtendedKeyUsage(ExtensionType):
- oid = ExtensionOID.EXTENDED_KEY_USAGE
- def __init__(self, usages: typing.Iterable[ObjectIdentifier]) -> None:
- usages = list(usages)
- if not all(isinstance(x, ObjectIdentifier) for x in usages):
- raise TypeError(
- "Every item in the usages list must be an ObjectIdentifier"
- )
- self._usages = usages
- __len__, __iter__, __getitem__ = _make_sequence_methods("_usages")
- def __repr__(self) -> str:
- return "<ExtendedKeyUsage({})>".format(self._usages)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, ExtendedKeyUsage):
- return NotImplemented
- return self._usages == other._usages
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._usages))
- class OCSPNoCheck(ExtensionType):
- oid = ExtensionOID.OCSP_NO_CHECK
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, OCSPNoCheck):
- return NotImplemented
- return True
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(OCSPNoCheck)
- def __repr__(self) -> str:
- return "<OCSPNoCheck()>"
- class PrecertPoison(ExtensionType):
- oid = ExtensionOID.PRECERT_POISON
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, PrecertPoison):
- return NotImplemented
- return True
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(PrecertPoison)
- def __repr__(self) -> str:
- return "<PrecertPoison()>"
- class TLSFeature(ExtensionType):
- oid = ExtensionOID.TLS_FEATURE
- def __init__(self, features: typing.Iterable["TLSFeatureType"]) -> None:
- features = list(features)
- if (
- not all(isinstance(x, TLSFeatureType) for x in features)
- or len(features) == 0
- ):
- raise TypeError(
- "features must be a list of elements from the TLSFeatureType "
- "enum"
- )
- self._features = features
- __len__, __iter__, __getitem__ = _make_sequence_methods("_features")
- def __repr__(self) -> str:
- return "<TLSFeature(features={0._features})>".format(self)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, TLSFeature):
- return NotImplemented
- return self._features == other._features
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._features))
- class TLSFeatureType(utils.Enum):
- # status_request is defined in RFC 6066 and is used for what is commonly
- # called OCSP Must-Staple when present in the TLS Feature extension in an
- # X.509 certificate.
- status_request = 5
- # status_request_v2 is defined in RFC 6961 and allows multiple OCSP
- # responses to be provided. It is not currently in use by clients or
- # servers.
- status_request_v2 = 17
- _TLS_FEATURE_TYPE_TO_ENUM = {x.value: x for x in TLSFeatureType}
- class InhibitAnyPolicy(ExtensionType):
- oid = ExtensionOID.INHIBIT_ANY_POLICY
- def __init__(self, skip_certs: int) -> None:
- if not isinstance(skip_certs, int):
- raise TypeError("skip_certs must be an integer")
- if skip_certs < 0:
- raise ValueError("skip_certs must be a non-negative integer")
- self._skip_certs = skip_certs
- def __repr__(self) -> str:
- return "<InhibitAnyPolicy(skip_certs={0.skip_certs})>".format(self)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, InhibitAnyPolicy):
- return NotImplemented
- return self.skip_certs == other.skip_certs
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.skip_certs)
- @property
- def skip_certs(self) -> int:
- return self._skip_certs
- class KeyUsage(ExtensionType):
- oid = ExtensionOID.KEY_USAGE
- def __init__(
- self,
- digital_signature: bool,
- content_commitment: bool,
- key_encipherment: bool,
- data_encipherment: bool,
- key_agreement: bool,
- key_cert_sign: bool,
- crl_sign: bool,
- encipher_only: bool,
- decipher_only: bool,
- ) -> None:
- if not key_agreement and (encipher_only or decipher_only):
- raise ValueError(
- "encipher_only and decipher_only can only be true when "
- "key_agreement is true"
- )
- self._digital_signature = digital_signature
- self._content_commitment = content_commitment
- self._key_encipherment = key_encipherment
- self._data_encipherment = data_encipherment
- self._key_agreement = key_agreement
- self._key_cert_sign = key_cert_sign
- self._crl_sign = crl_sign
- self._encipher_only = encipher_only
- self._decipher_only = decipher_only
- @property
- def digital_signature(self) -> bool:
- return self._digital_signature
- @property
- def content_commitment(self) -> bool:
- return self._content_commitment
- @property
- def key_encipherment(self) -> bool:
- return self._key_encipherment
- @property
- def data_encipherment(self) -> bool:
- return self._data_encipherment
- @property
- def key_agreement(self) -> bool:
- return self._key_agreement
- @property
- def key_cert_sign(self) -> bool:
- return self._key_cert_sign
- @property
- def crl_sign(self) -> bool:
- return self._crl_sign
- @property
- def encipher_only(self) -> bool:
- if not self.key_agreement:
- raise ValueError(
- "encipher_only is undefined unless key_agreement is true"
- )
- else:
- return self._encipher_only
- @property
- def decipher_only(self) -> bool:
- if not self.key_agreement:
- raise ValueError(
- "decipher_only is undefined unless key_agreement is true"
- )
- else:
- return self._decipher_only
- def __repr__(self) -> str:
- try:
- encipher_only = self.encipher_only
- decipher_only = self.decipher_only
- except ValueError:
- # Users found None confusing because even though encipher/decipher
- # have no meaning unless key_agreement is true, to construct an
- # instance of the class you still need to pass False.
- encipher_only = False
- decipher_only = False
- return (
- "<KeyUsage(digital_signature={0.digital_signature}, "
- "content_commitment={0.content_commitment}, "
- "key_encipherment={0.key_encipherment}, "
- "data_encipherment={0.data_encipherment}, "
- "key_agreement={0.key_agreement}, "
- "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, "
- "encipher_only={1}, decipher_only={2})>"
- ).format(self, encipher_only, decipher_only)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, KeyUsage):
- return NotImplemented
- return (
- self.digital_signature == other.digital_signature
- and self.content_commitment == other.content_commitment
- and self.key_encipherment == other.key_encipherment
- and self.data_encipherment == other.data_encipherment
- and self.key_agreement == other.key_agreement
- and self.key_cert_sign == other.key_cert_sign
- and self.crl_sign == other.crl_sign
- and self._encipher_only == other._encipher_only
- and self._decipher_only == other._decipher_only
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(
- (
- self.digital_signature,
- self.content_commitment,
- self.key_encipherment,
- self.data_encipherment,
- self.key_agreement,
- self.key_cert_sign,
- self.crl_sign,
- self._encipher_only,
- self._decipher_only,
- )
- )
- class NameConstraints(ExtensionType):
- oid = ExtensionOID.NAME_CONSTRAINTS
- def __init__(
- self,
- permitted_subtrees: typing.Optional[typing.Iterable[GeneralName]],
- excluded_subtrees: typing.Optional[typing.Iterable[GeneralName]],
- ) -> None:
- if permitted_subtrees is not None:
- permitted_subtrees = list(permitted_subtrees)
- if not all(isinstance(x, GeneralName) for x in permitted_subtrees):
- raise TypeError(
- "permitted_subtrees must be a list of GeneralName objects "
- "or None"
- )
- self._validate_ip_name(permitted_subtrees)
- if excluded_subtrees is not None:
- excluded_subtrees = list(excluded_subtrees)
- if not all(isinstance(x, GeneralName) for x in excluded_subtrees):
- raise TypeError(
- "excluded_subtrees must be a list of GeneralName objects "
- "or None"
- )
- self._validate_ip_name(excluded_subtrees)
- if permitted_subtrees is None and excluded_subtrees is None:
- raise ValueError(
- "At least one of permitted_subtrees and excluded_subtrees "
- "must not be None"
- )
- self._permitted_subtrees = permitted_subtrees
- self._excluded_subtrees = excluded_subtrees
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, NameConstraints):
- return NotImplemented
- return (
- self.excluded_subtrees == other.excluded_subtrees
- and self.permitted_subtrees == other.permitted_subtrees
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def _validate_ip_name(self, tree: typing.Iterable[GeneralName]) -> None:
- if any(
- isinstance(name, IPAddress)
- and not isinstance(
- name.value, (ipaddress.IPv4Network, ipaddress.IPv6Network)
- )
- for name in tree
- ):
- raise TypeError(
- "IPAddress name constraints must be an IPv4Network or"
- " IPv6Network object"
- )
- def __repr__(self) -> str:
- return (
- "<NameConstraints(permitted_subtrees={0.permitted_subtrees}, "
- "excluded_subtrees={0.excluded_subtrees})>".format(self)
- )
- def __hash__(self) -> int:
- if self.permitted_subtrees is not None:
- ps: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
- self.permitted_subtrees
- )
- else:
- ps = None
- if self.excluded_subtrees is not None:
- es: typing.Optional[typing.Tuple[GeneralName, ...]] = tuple(
- self.excluded_subtrees
- )
- else:
- es = None
- return hash((ps, es))
- @property
- def permitted_subtrees(
- self,
- ) -> typing.Optional[typing.List[GeneralName]]:
- return self._permitted_subtrees
- @property
- def excluded_subtrees(
- self,
- ) -> typing.Optional[typing.List[GeneralName]]:
- return self._excluded_subtrees
- class Extension(typing.Generic[ExtensionTypeVar]):
- def __init__(
- self, oid: ObjectIdentifier, critical: bool, value: ExtensionTypeVar
- ) -> None:
- if not isinstance(oid, ObjectIdentifier):
- raise TypeError(
- "oid argument must be an ObjectIdentifier instance."
- )
- if not isinstance(critical, bool):
- raise TypeError("critical must be a boolean value")
- self._oid = oid
- self._critical = critical
- self._value = value
- @property
- def oid(self) -> ObjectIdentifier:
- return self._oid
- @property
- def critical(self) -> bool:
- return self._critical
- @property
- def value(self) -> ExtensionTypeVar:
- return self._value
- def __repr__(self) -> str:
- return (
- "<Extension(oid={0.oid}, critical={0.critical}, "
- "value={0.value})>"
- ).format(self)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, Extension):
- return NotImplemented
- return (
- self.oid == other.oid
- and self.critical == other.critical
- and self.value == other.value
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.oid, self.critical, self.value))
- class GeneralNames(object):
- def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
- general_names = list(general_names)
- if not all(isinstance(x, GeneralName) for x in general_names):
- raise TypeError(
- "Every item in the general_names list must be an "
- "object conforming to the GeneralName interface"
- )
- self._general_names = general_names
- __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[UniformResourceIdentifier],
- typing.Type[RFC822Name],
- ],
- ) -> typing.List[str]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[DirectoryName],
- ) -> typing.List[Name]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[RegisteredID],
- ) -> typing.List[ObjectIdentifier]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[IPAddress]
- ) -> typing.List[_IPADDRESS_TYPES]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[OtherName]
- ) -> typing.List[OtherName]:
- ...
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[DirectoryName],
- typing.Type[IPAddress],
- typing.Type[OtherName],
- typing.Type[RFC822Name],
- typing.Type[RegisteredID],
- typing.Type[UniformResourceIdentifier],
- ],
- ) -> typing.Union[
- typing.List[_IPADDRESS_TYPES],
- typing.List[str],
- typing.List[OtherName],
- typing.List[Name],
- typing.List[ObjectIdentifier],
- ]:
- # Return the value of each GeneralName, except for OtherName instances
- # which we return directly because it has two important properties not
- # just one value.
- objs = (i for i in self if isinstance(i, type))
- if type != OtherName:
- return [i.value for i in objs]
- return list(objs)
- def __repr__(self) -> str:
- return "<GeneralNames({})>".format(self._general_names)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, GeneralNames):
- return NotImplemented
- return self._general_names == other._general_names
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(tuple(self._general_names))
- class SubjectAlternativeName(ExtensionType):
- oid = ExtensionOID.SUBJECT_ALTERNATIVE_NAME
- def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
- self._general_names = GeneralNames(general_names)
- __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[UniformResourceIdentifier],
- typing.Type[RFC822Name],
- ],
- ) -> typing.List[str]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[DirectoryName],
- ) -> typing.List[Name]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[RegisteredID],
- ) -> typing.List[ObjectIdentifier]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[IPAddress]
- ) -> typing.List[_IPADDRESS_TYPES]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[OtherName]
- ) -> typing.List[OtherName]:
- ...
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[DirectoryName],
- typing.Type[IPAddress],
- typing.Type[OtherName],
- typing.Type[RFC822Name],
- typing.Type[RegisteredID],
- typing.Type[UniformResourceIdentifier],
- ],
- ) -> typing.Union[
- typing.List[_IPADDRESS_TYPES],
- typing.List[str],
- typing.List[OtherName],
- typing.List[Name],
- typing.List[ObjectIdentifier],
- ]:
- return self._general_names.get_values_for_type(type)
- def __repr__(self) -> str:
- return "<SubjectAlternativeName({})>".format(self._general_names)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, SubjectAlternativeName):
- return NotImplemented
- return self._general_names == other._general_names
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self._general_names)
- class IssuerAlternativeName(ExtensionType):
- oid = ExtensionOID.ISSUER_ALTERNATIVE_NAME
- def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
- self._general_names = GeneralNames(general_names)
- __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[UniformResourceIdentifier],
- typing.Type[RFC822Name],
- ],
- ) -> typing.List[str]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[DirectoryName],
- ) -> typing.List[Name]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[RegisteredID],
- ) -> typing.List[ObjectIdentifier]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[IPAddress]
- ) -> typing.List[_IPADDRESS_TYPES]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[OtherName]
- ) -> typing.List[OtherName]:
- ...
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[DirectoryName],
- typing.Type[IPAddress],
- typing.Type[OtherName],
- typing.Type[RFC822Name],
- typing.Type[RegisteredID],
- typing.Type[UniformResourceIdentifier],
- ],
- ) -> typing.Union[
- typing.List[_IPADDRESS_TYPES],
- typing.List[str],
- typing.List[OtherName],
- typing.List[Name],
- typing.List[ObjectIdentifier],
- ]:
- return self._general_names.get_values_for_type(type)
- def __repr__(self) -> str:
- return "<IssuerAlternativeName({})>".format(self._general_names)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, IssuerAlternativeName):
- return NotImplemented
- return self._general_names == other._general_names
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self._general_names)
- class CertificateIssuer(ExtensionType):
- oid = CRLEntryExtensionOID.CERTIFICATE_ISSUER
- def __init__(self, general_names: typing.Iterable[GeneralName]) -> None:
- self._general_names = GeneralNames(general_names)
- __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[UniformResourceIdentifier],
- typing.Type[RFC822Name],
- ],
- ) -> typing.List[str]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[DirectoryName],
- ) -> typing.List[Name]:
- ...
- @typing.overload
- def get_values_for_type(
- self,
- type: typing.Type[RegisteredID],
- ) -> typing.List[ObjectIdentifier]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[IPAddress]
- ) -> typing.List[_IPADDRESS_TYPES]:
- ...
- @typing.overload
- def get_values_for_type(
- self, type: typing.Type[OtherName]
- ) -> typing.List[OtherName]:
- ...
- def get_values_for_type(
- self,
- type: typing.Union[
- typing.Type[DNSName],
- typing.Type[DirectoryName],
- typing.Type[IPAddress],
- typing.Type[OtherName],
- typing.Type[RFC822Name],
- typing.Type[RegisteredID],
- typing.Type[UniformResourceIdentifier],
- ],
- ) -> typing.Union[
- typing.List[_IPADDRESS_TYPES],
- typing.List[str],
- typing.List[OtherName],
- typing.List[Name],
- typing.List[ObjectIdentifier],
- ]:
- return self._general_names.get_values_for_type(type)
- def __repr__(self) -> str:
- return "<CertificateIssuer({})>".format(self._general_names)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, CertificateIssuer):
- return NotImplemented
- return self._general_names == other._general_names
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self._general_names)
- class CRLReason(ExtensionType):
- oid = CRLEntryExtensionOID.CRL_REASON
- def __init__(self, reason: ReasonFlags) -> None:
- if not isinstance(reason, ReasonFlags):
- raise TypeError("reason must be an element from ReasonFlags")
- self._reason = reason
- def __repr__(self) -> str:
- return "<CRLReason(reason={})>".format(self._reason)
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, CRLReason):
- return NotImplemented
- return self.reason == other.reason
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.reason)
- @property
- def reason(self) -> ReasonFlags:
- return self._reason
- class InvalidityDate(ExtensionType):
- oid = CRLEntryExtensionOID.INVALIDITY_DATE
- def __init__(self, invalidity_date: datetime.datetime) -> None:
- if not isinstance(invalidity_date, datetime.datetime):
- raise TypeError("invalidity_date must be a datetime.datetime")
- self._invalidity_date = invalidity_date
- def __repr__(self) -> str:
- return "<InvalidityDate(invalidity_date={})>".format(
- self._invalidity_date
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, InvalidityDate):
- return NotImplemented
- return self.invalidity_date == other.invalidity_date
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.invalidity_date)
- @property
- def invalidity_date(self) -> datetime.datetime:
- return self._invalidity_date
- class PrecertificateSignedCertificateTimestamps(ExtensionType):
- oid = ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS
- def __init__(
- self,
- signed_certificate_timestamps: typing.Iterable[
- SignedCertificateTimestamp
- ],
- ) -> None:
- signed_certificate_timestamps = list(signed_certificate_timestamps)
- if not all(
- isinstance(sct, SignedCertificateTimestamp)
- for sct in signed_certificate_timestamps
- ):
- raise TypeError(
- "Every item in the signed_certificate_timestamps list must be "
- "a SignedCertificateTimestamp"
- )
- self._signed_certificate_timestamps = signed_certificate_timestamps
- __len__, __iter__, __getitem__ = _make_sequence_methods(
- "_signed_certificate_timestamps"
- )
- def __repr__(self) -> str:
- return "<PrecertificateSignedCertificateTimestamps({})>".format(
- list(self)
- )
- def __hash__(self) -> int:
- return hash(tuple(self._signed_certificate_timestamps))
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, PrecertificateSignedCertificateTimestamps):
- return NotImplemented
- return (
- self._signed_certificate_timestamps
- == other._signed_certificate_timestamps
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- class SignedCertificateTimestamps(ExtensionType):
- oid = ExtensionOID.SIGNED_CERTIFICATE_TIMESTAMPS
- def __init__(
- self,
- signed_certificate_timestamps: typing.Iterable[
- SignedCertificateTimestamp
- ],
- ) -> None:
- signed_certificate_timestamps = list(signed_certificate_timestamps)
- if not all(
- isinstance(sct, SignedCertificateTimestamp)
- for sct in signed_certificate_timestamps
- ):
- raise TypeError(
- "Every item in the signed_certificate_timestamps list must be "
- "a SignedCertificateTimestamp"
- )
- self._signed_certificate_timestamps = signed_certificate_timestamps
- __len__, __iter__, __getitem__ = _make_sequence_methods(
- "_signed_certificate_timestamps"
- )
- def __repr__(self) -> str:
- return "<SignedCertificateTimestamps({})>".format(list(self))
- def __hash__(self) -> int:
- return hash(tuple(self._signed_certificate_timestamps))
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, SignedCertificateTimestamps):
- return NotImplemented
- return (
- self._signed_certificate_timestamps
- == other._signed_certificate_timestamps
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- class OCSPNonce(ExtensionType):
- oid = OCSPExtensionOID.NONCE
- def __init__(self, nonce: bytes) -> None:
- if not isinstance(nonce, bytes):
- raise TypeError("nonce must be bytes")
- self._nonce = nonce
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, OCSPNonce):
- return NotImplemented
- return self.nonce == other.nonce
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(self.nonce)
- def __repr__(self) -> str:
- return "<OCSPNonce(nonce={0.nonce!r})>".format(self)
- @property
- def nonce(self) -> bytes:
- return self._nonce
- class IssuingDistributionPoint(ExtensionType):
- oid = ExtensionOID.ISSUING_DISTRIBUTION_POINT
- def __init__(
- self,
- full_name: typing.Optional[typing.Iterable[GeneralName]],
- relative_name: typing.Optional[RelativeDistinguishedName],
- only_contains_user_certs: bool,
- only_contains_ca_certs: bool,
- only_some_reasons: typing.Optional[typing.FrozenSet[ReasonFlags]],
- indirect_crl: bool,
- only_contains_attribute_certs: bool,
- ) -> None:
- if full_name is not None:
- full_name = list(full_name)
- if only_some_reasons and (
- not isinstance(only_some_reasons, frozenset)
- or not all(isinstance(x, ReasonFlags) for x in only_some_reasons)
- ):
- raise TypeError(
- "only_some_reasons must be None or frozenset of ReasonFlags"
- )
- if only_some_reasons and (
- ReasonFlags.unspecified in only_some_reasons
- or ReasonFlags.remove_from_crl in only_some_reasons
- ):
- raise ValueError(
- "unspecified and remove_from_crl are not valid reasons in an "
- "IssuingDistributionPoint"
- )
- if not (
- isinstance(only_contains_user_certs, bool)
- and isinstance(only_contains_ca_certs, bool)
- and isinstance(indirect_crl, bool)
- and isinstance(only_contains_attribute_certs, bool)
- ):
- raise TypeError(
- "only_contains_user_certs, only_contains_ca_certs, "
- "indirect_crl and only_contains_attribute_certs "
- "must all be boolean."
- )
- crl_constraints = [
- only_contains_user_certs,
- only_contains_ca_certs,
- indirect_crl,
- only_contains_attribute_certs,
- ]
- if len([x for x in crl_constraints if x]) > 1:
- raise ValueError(
- "Only one of the following can be set to True: "
- "only_contains_user_certs, only_contains_ca_certs, "
- "indirect_crl, only_contains_attribute_certs"
- )
- if not any(
- [
- only_contains_user_certs,
- only_contains_ca_certs,
- indirect_crl,
- only_contains_attribute_certs,
- full_name,
- relative_name,
- only_some_reasons,
- ]
- ):
- raise ValueError(
- "Cannot create empty extension: "
- "if only_contains_user_certs, only_contains_ca_certs, "
- "indirect_crl, and only_contains_attribute_certs are all False"
- ", then either full_name, relative_name, or only_some_reasons "
- "must have a value."
- )
- self._only_contains_user_certs = only_contains_user_certs
- self._only_contains_ca_certs = only_contains_ca_certs
- self._indirect_crl = indirect_crl
- self._only_contains_attribute_certs = only_contains_attribute_certs
- self._only_some_reasons = only_some_reasons
- self._full_name = full_name
- self._relative_name = relative_name
- def __repr__(self) -> str:
- return (
- "<IssuingDistributionPoint(full_name={0.full_name}, "
- "relative_name={0.relative_name}, "
- "only_contains_user_certs={0.only_contains_user_certs}, "
- "only_contains_ca_certs={0.only_contains_ca_certs}, "
- "only_some_reasons={0.only_some_reasons}, "
- "indirect_crl={0.indirect_crl}, "
- "only_contains_attribute_certs="
- "{0.only_contains_attribute_certs})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, IssuingDistributionPoint):
- return NotImplemented
- return (
- self.full_name == other.full_name
- and self.relative_name == other.relative_name
- and self.only_contains_user_certs == other.only_contains_user_certs
- and self.only_contains_ca_certs == other.only_contains_ca_certs
- and self.only_some_reasons == other.only_some_reasons
- and self.indirect_crl == other.indirect_crl
- and self.only_contains_attribute_certs
- == other.only_contains_attribute_certs
- )
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash(
- (
- self.full_name,
- self.relative_name,
- self.only_contains_user_certs,
- self.only_contains_ca_certs,
- self.only_some_reasons,
- self.indirect_crl,
- self.only_contains_attribute_certs,
- )
- )
- @property
- def full_name(self) -> typing.Optional[typing.List[GeneralName]]:
- return self._full_name
- @property
- def relative_name(self) -> typing.Optional[RelativeDistinguishedName]:
- return self._relative_name
- @property
- def only_contains_user_certs(self) -> bool:
- return self._only_contains_user_certs
- @property
- def only_contains_ca_certs(self) -> bool:
- return self._only_contains_ca_certs
- @property
- def only_some_reasons(
- self,
- ) -> typing.Optional[typing.FrozenSet[ReasonFlags]]:
- return self._only_some_reasons
- @property
- def indirect_crl(self) -> bool:
- return self._indirect_crl
- @property
- def only_contains_attribute_certs(self) -> bool:
- return self._only_contains_attribute_certs
- class UnrecognizedExtension(ExtensionType):
- def __init__(self, oid: ObjectIdentifier, value: bytes) -> None:
- if not isinstance(oid, ObjectIdentifier):
- raise TypeError("oid must be an ObjectIdentifier")
- self._oid = oid
- self._value = value
- @property
- def oid(self) -> ObjectIdentifier: # type: ignore[override]
- return self._oid
- @property
- def value(self) -> bytes:
- return self._value
- def __repr__(self) -> str:
- return (
- "<UnrecognizedExtension(oid={0.oid}, "
- "value={0.value!r})>".format(self)
- )
- def __eq__(self, other: typing.Any) -> bool:
- if not isinstance(other, UnrecognizedExtension):
- return NotImplemented
- return self.oid == other.oid and self.value == other.value
- def __ne__(self, other: typing.Any) -> bool:
- return not self == other
- def __hash__(self) -> int:
- return hash((self.oid, self.value))
|