You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

extensions.py 50KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609
  1. # This file is dual licensed under the terms of the Apache License, Version
  2. # 2.0, and the BSD License. See the LICENSE file in the root of this repository
  3. # for complete details.
  4. from __future__ import absolute_import, division, print_function
  5. import abc
  6. import datetime
  7. import hashlib
  8. import ipaddress
  9. import warnings
  10. from enum import Enum
  11. import six
  12. from cryptography import utils
  13. from cryptography.hazmat._der import (
  14. BIT_STRING, DERReader, OBJECT_IDENTIFIER, SEQUENCE
  15. )
  16. from cryptography.hazmat.primitives import constant_time, serialization
  17. from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey
  18. from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey
  19. from cryptography.x509.certificate_transparency import (
  20. SignedCertificateTimestamp
  21. )
  22. from cryptography.x509.general_name import GeneralName, IPAddress, OtherName
  23. from cryptography.x509.name import RelativeDistinguishedName
  24. from cryptography.x509.oid import (
  25. CRLEntryExtensionOID, ExtensionOID, OCSPExtensionOID, ObjectIdentifier,
  26. )
  27. def _key_identifier_from_public_key(public_key):
  28. if isinstance(public_key, RSAPublicKey):
  29. data = public_key.public_bytes(
  30. serialization.Encoding.DER,
  31. serialization.PublicFormat.PKCS1,
  32. )
  33. elif isinstance(public_key, EllipticCurvePublicKey):
  34. data = public_key.public_bytes(
  35. serialization.Encoding.X962,
  36. serialization.PublicFormat.UncompressedPoint
  37. )
  38. else:
  39. # This is a very slow way to do this.
  40. serialized = public_key.public_bytes(
  41. serialization.Encoding.DER,
  42. serialization.PublicFormat.SubjectPublicKeyInfo
  43. )
  44. reader = DERReader(serialized)
  45. with reader.read_single_element(SEQUENCE) as public_key_info:
  46. algorithm = public_key_info.read_element(SEQUENCE)
  47. public_key = public_key_info.read_element(BIT_STRING)
  48. # Double-check the algorithm structure.
  49. with algorithm:
  50. algorithm.read_element(OBJECT_IDENTIFIER)
  51. if not algorithm.is_empty():
  52. # Skip the optional parameters field.
  53. algorithm.read_any_element()
  54. # BIT STRING contents begin with the number of padding bytes added. It
  55. # must be zero for SubjectPublicKeyInfo structures.
  56. if public_key.read_byte() != 0:
  57. raise ValueError('Invalid public key encoding')
  58. data = public_key.data
  59. return hashlib.sha1(data).digest()
  60. def _make_sequence_methods(field_name):
  61. def len_method(self):
  62. return len(getattr(self, field_name))
  63. def iter_method(self):
  64. return iter(getattr(self, field_name))
  65. def getitem_method(self, idx):
  66. return getattr(self, field_name)[idx]
  67. return len_method, iter_method, getitem_method
  68. class DuplicateExtension(Exception):
  69. def __init__(self, msg, oid):
  70. super(DuplicateExtension, self).__init__(msg)
  71. self.oid = oid
  72. class ExtensionNotFound(Exception):
  73. def __init__(self, msg, oid):
  74. super(ExtensionNotFound, self).__init__(msg)
  75. self.oid = oid
  76. @six.add_metaclass(abc.ABCMeta)
  77. class ExtensionType(object):
  78. @abc.abstractproperty
  79. def oid(self):
  80. """
  81. Returns the oid associated with the given extension type.
  82. """
  83. class Extensions(object):
  84. def __init__(self, extensions):
  85. self._extensions = extensions
  86. def get_extension_for_oid(self, oid):
  87. for ext in self:
  88. if ext.oid == oid:
  89. return ext
  90. raise ExtensionNotFound("No {} extension was found".format(oid), oid)
  91. def get_extension_for_class(self, extclass):
  92. if extclass is UnrecognizedExtension:
  93. raise TypeError(
  94. "UnrecognizedExtension can't be used with "
  95. "get_extension_for_class because more than one instance of the"
  96. " class may be present."
  97. )
  98. for ext in self:
  99. if isinstance(ext.value, extclass):
  100. return ext
  101. raise ExtensionNotFound(
  102. "No {} extension was found".format(extclass), extclass.oid
  103. )
  104. __len__, __iter__, __getitem__ = _make_sequence_methods("_extensions")
  105. def __repr__(self):
  106. return (
  107. "<Extensions({})>".format(self._extensions)
  108. )
  109. @utils.register_interface(ExtensionType)
  110. class CRLNumber(object):
  111. oid = ExtensionOID.CRL_NUMBER
  112. def __init__(self, crl_number):
  113. if not isinstance(crl_number, six.integer_types):
  114. raise TypeError("crl_number must be an integer")
  115. self._crl_number = crl_number
  116. def __eq__(self, other):
  117. if not isinstance(other, CRLNumber):
  118. return NotImplemented
  119. return self.crl_number == other.crl_number
  120. def __ne__(self, other):
  121. return not self == other
  122. def __hash__(self):
  123. return hash(self.crl_number)
  124. def __repr__(self):
  125. return "<CRLNumber({})>".format(self.crl_number)
  126. crl_number = utils.read_only_property("_crl_number")
  127. @utils.register_interface(ExtensionType)
  128. class AuthorityKeyIdentifier(object):
  129. oid = ExtensionOID.AUTHORITY_KEY_IDENTIFIER
  130. def __init__(self, key_identifier, authority_cert_issuer,
  131. authority_cert_serial_number):
  132. if (authority_cert_issuer is None) != (
  133. authority_cert_serial_number is None
  134. ):
  135. raise ValueError(
  136. "authority_cert_issuer and authority_cert_serial_number "
  137. "must both be present or both None"
  138. )
  139. if authority_cert_issuer is not None:
  140. authority_cert_issuer = list(authority_cert_issuer)
  141. if not all(
  142. isinstance(x, GeneralName) for x in authority_cert_issuer
  143. ):
  144. raise TypeError(
  145. "authority_cert_issuer must be a list of GeneralName "
  146. "objects"
  147. )
  148. if authority_cert_serial_number is not None and not isinstance(
  149. authority_cert_serial_number, six.integer_types
  150. ):
  151. raise TypeError(
  152. "authority_cert_serial_number must be an integer"
  153. )
  154. self._key_identifier = key_identifier
  155. self._authority_cert_issuer = authority_cert_issuer
  156. self._authority_cert_serial_number = authority_cert_serial_number
  157. @classmethod
  158. def from_issuer_public_key(cls, public_key):
  159. digest = _key_identifier_from_public_key(public_key)
  160. return cls(
  161. key_identifier=digest,
  162. authority_cert_issuer=None,
  163. authority_cert_serial_number=None
  164. )
  165. @classmethod
  166. def from_issuer_subject_key_identifier(cls, ski):
  167. if isinstance(ski, SubjectKeyIdentifier):
  168. digest = ski.digest
  169. else:
  170. digest = ski.value.digest
  171. warnings.warn(
  172. "Extension objects are deprecated as arguments to "
  173. "from_issuer_subject_key_identifier and support will be "
  174. "removed soon. Please migrate to passing a "
  175. "SubjectKeyIdentifier directly.",
  176. utils.DeprecatedIn27,
  177. stacklevel=2,
  178. )
  179. return cls(
  180. key_identifier=digest,
  181. authority_cert_issuer=None,
  182. authority_cert_serial_number=None
  183. )
  184. def __repr__(self):
  185. return (
  186. "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, "
  187. "authority_cert_issuer={0.authority_cert_issuer}, "
  188. "authority_cert_serial_number={0.authority_cert_serial_number}"
  189. ")>".format(self)
  190. )
  191. def __eq__(self, other):
  192. if not isinstance(other, AuthorityKeyIdentifier):
  193. return NotImplemented
  194. return (
  195. self.key_identifier == other.key_identifier and
  196. self.authority_cert_issuer == other.authority_cert_issuer and
  197. self.authority_cert_serial_number ==
  198. other.authority_cert_serial_number
  199. )
  200. def __ne__(self, other):
  201. return not self == other
  202. def __hash__(self):
  203. if self.authority_cert_issuer is None:
  204. aci = None
  205. else:
  206. aci = tuple(self.authority_cert_issuer)
  207. return hash((
  208. self.key_identifier, aci, self.authority_cert_serial_number
  209. ))
  210. key_identifier = utils.read_only_property("_key_identifier")
  211. authority_cert_issuer = utils.read_only_property("_authority_cert_issuer")
  212. authority_cert_serial_number = utils.read_only_property(
  213. "_authority_cert_serial_number"
  214. )
  215. @utils.register_interface(ExtensionType)
  216. class SubjectKeyIdentifier(object):
  217. oid = ExtensionOID.SUBJECT_KEY_IDENTIFIER
  218. def __init__(self, digest):
  219. self._digest = digest
  220. @classmethod
  221. def from_public_key(cls, public_key):
  222. return cls(_key_identifier_from_public_key(public_key))
  223. digest = utils.read_only_property("_digest")
  224. def __repr__(self):
  225. return "<SubjectKeyIdentifier(digest={0!r})>".format(self.digest)
  226. def __eq__(self, other):
  227. if not isinstance(other, SubjectKeyIdentifier):
  228. return NotImplemented
  229. return constant_time.bytes_eq(self.digest, other.digest)
  230. def __ne__(self, other):
  231. return not self == other
  232. def __hash__(self):
  233. return hash(self.digest)
  234. @utils.register_interface(ExtensionType)
  235. class AuthorityInformationAccess(object):
  236. oid = ExtensionOID.AUTHORITY_INFORMATION_ACCESS
  237. def __init__(self, descriptions):
  238. descriptions = list(descriptions)
  239. if not all(isinstance(x, AccessDescription) for x in descriptions):
  240. raise TypeError(
  241. "Every item in the descriptions list must be an "
  242. "AccessDescription"
  243. )
  244. self._descriptions = descriptions
  245. __len__, __iter__, __getitem__ = _make_sequence_methods("_descriptions")
  246. def __repr__(self):
  247. return "<AuthorityInformationAccess({})>".format(self._descriptions)
  248. def __eq__(self, other):
  249. if not isinstance(other, AuthorityInformationAccess):
  250. return NotImplemented
  251. return self._descriptions == other._descriptions
  252. def __ne__(self, other):
  253. return not self == other
  254. def __hash__(self):
  255. return hash(tuple(self._descriptions))
  256. class AccessDescription(object):
  257. def __init__(self, access_method, access_location):
  258. if not isinstance(access_method, ObjectIdentifier):
  259. raise TypeError("access_method must be an ObjectIdentifier")
  260. if not isinstance(access_location, GeneralName):
  261. raise TypeError("access_location must be a GeneralName")
  262. self._access_method = access_method
  263. self._access_location = access_location
  264. def __repr__(self):
  265. return (
  266. "<AccessDescription(access_method={0.access_method}, access_locati"
  267. "on={0.access_location})>".format(self)
  268. )
  269. def __eq__(self, other):
  270. if not isinstance(other, AccessDescription):
  271. return NotImplemented
  272. return (
  273. self.access_method == other.access_method and
  274. self.access_location == other.access_location
  275. )
  276. def __ne__(self, other):
  277. return not self == other
  278. def __hash__(self):
  279. return hash((self.access_method, self.access_location))
  280. access_method = utils.read_only_property("_access_method")
  281. access_location = utils.read_only_property("_access_location")
  282. @utils.register_interface(ExtensionType)
  283. class BasicConstraints(object):
  284. oid = ExtensionOID.BASIC_CONSTRAINTS
  285. def __init__(self, ca, path_length):
  286. if not isinstance(ca, bool):
  287. raise TypeError("ca must be a boolean value")
  288. if path_length is not None and not ca:
  289. raise ValueError("path_length must be None when ca is False")
  290. if (
  291. path_length is not None and
  292. (not isinstance(path_length, six.integer_types) or path_length < 0)
  293. ):
  294. raise TypeError(
  295. "path_length must be a non-negative integer or None"
  296. )
  297. self._ca = ca
  298. self._path_length = path_length
  299. ca = utils.read_only_property("_ca")
  300. path_length = utils.read_only_property("_path_length")
  301. def __repr__(self):
  302. return ("<BasicConstraints(ca={0.ca}, "
  303. "path_length={0.path_length})>").format(self)
  304. def __eq__(self, other):
  305. if not isinstance(other, BasicConstraints):
  306. return NotImplemented
  307. return self.ca == other.ca and self.path_length == other.path_length
  308. def __ne__(self, other):
  309. return not self == other
  310. def __hash__(self):
  311. return hash((self.ca, self.path_length))
  312. @utils.register_interface(ExtensionType)
  313. class DeltaCRLIndicator(object):
  314. oid = ExtensionOID.DELTA_CRL_INDICATOR
  315. def __init__(self, crl_number):
  316. if not isinstance(crl_number, six.integer_types):
  317. raise TypeError("crl_number must be an integer")
  318. self._crl_number = crl_number
  319. crl_number = utils.read_only_property("_crl_number")
  320. def __eq__(self, other):
  321. if not isinstance(other, DeltaCRLIndicator):
  322. return NotImplemented
  323. return self.crl_number == other.crl_number
  324. def __ne__(self, other):
  325. return not self == other
  326. def __hash__(self):
  327. return hash(self.crl_number)
  328. def __repr__(self):
  329. return "<DeltaCRLIndicator(crl_number={0.crl_number})>".format(self)
  330. @utils.register_interface(ExtensionType)
  331. class CRLDistributionPoints(object):
  332. oid = ExtensionOID.CRL_DISTRIBUTION_POINTS
  333. def __init__(self, distribution_points):
  334. distribution_points = list(distribution_points)
  335. if not all(
  336. isinstance(x, DistributionPoint) for x in distribution_points
  337. ):
  338. raise TypeError(
  339. "distribution_points must be a list of DistributionPoint "
  340. "objects"
  341. )
  342. self._distribution_points = distribution_points
  343. __len__, __iter__, __getitem__ = _make_sequence_methods(
  344. "_distribution_points"
  345. )
  346. def __repr__(self):
  347. return "<CRLDistributionPoints({})>".format(self._distribution_points)
  348. def __eq__(self, other):
  349. if not isinstance(other, CRLDistributionPoints):
  350. return NotImplemented
  351. return self._distribution_points == other._distribution_points
  352. def __ne__(self, other):
  353. return not self == other
  354. def __hash__(self):
  355. return hash(tuple(self._distribution_points))
  356. @utils.register_interface(ExtensionType)
  357. class FreshestCRL(object):
  358. oid = ExtensionOID.FRESHEST_CRL
  359. def __init__(self, distribution_points):
  360. distribution_points = list(distribution_points)
  361. if not all(
  362. isinstance(x, DistributionPoint) for x in distribution_points
  363. ):
  364. raise TypeError(
  365. "distribution_points must be a list of DistributionPoint "
  366. "objects"
  367. )
  368. self._distribution_points = distribution_points
  369. __len__, __iter__, __getitem__ = _make_sequence_methods(
  370. "_distribution_points"
  371. )
  372. def __repr__(self):
  373. return "<FreshestCRL({})>".format(self._distribution_points)
  374. def __eq__(self, other):
  375. if not isinstance(other, FreshestCRL):
  376. return NotImplemented
  377. return self._distribution_points == other._distribution_points
  378. def __ne__(self, other):
  379. return not self == other
  380. def __hash__(self):
  381. return hash(tuple(self._distribution_points))
  382. class DistributionPoint(object):
  383. def __init__(self, full_name, relative_name, reasons, crl_issuer):
  384. if full_name and relative_name:
  385. raise ValueError(
  386. "You cannot provide both full_name and relative_name, at "
  387. "least one must be None."
  388. )
  389. if full_name:
  390. full_name = list(full_name)
  391. if not all(isinstance(x, GeneralName) for x in full_name):
  392. raise TypeError(
  393. "full_name must be a list of GeneralName objects"
  394. )
  395. if relative_name:
  396. if not isinstance(relative_name, RelativeDistinguishedName):
  397. raise TypeError(
  398. "relative_name must be a RelativeDistinguishedName"
  399. )
  400. if crl_issuer:
  401. crl_issuer = list(crl_issuer)
  402. if not all(isinstance(x, GeneralName) for x in crl_issuer):
  403. raise TypeError(
  404. "crl_issuer must be None or a list of general names"
  405. )
  406. if reasons and (not isinstance(reasons, frozenset) or not all(
  407. isinstance(x, ReasonFlags) for x in reasons
  408. )):
  409. raise TypeError("reasons must be None or frozenset of ReasonFlags")
  410. if reasons and (
  411. ReasonFlags.unspecified in reasons or
  412. ReasonFlags.remove_from_crl in reasons
  413. ):
  414. raise ValueError(
  415. "unspecified and remove_from_crl are not valid reasons in a "
  416. "DistributionPoint"
  417. )
  418. if reasons and not crl_issuer and not (full_name or relative_name):
  419. raise ValueError(
  420. "You must supply crl_issuer, full_name, or relative_name when "
  421. "reasons is not None"
  422. )
  423. self._full_name = full_name
  424. self._relative_name = relative_name
  425. self._reasons = reasons
  426. self._crl_issuer = crl_issuer
  427. def __repr__(self):
  428. return (
  429. "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela"
  430. "tive_name}, reasons={0.reasons}, crl_issuer={0.crl_issuer})>"
  431. .format(self)
  432. )
  433. def __eq__(self, other):
  434. if not isinstance(other, DistributionPoint):
  435. return NotImplemented
  436. return (
  437. self.full_name == other.full_name and
  438. self.relative_name == other.relative_name and
  439. self.reasons == other.reasons and
  440. self.crl_issuer == other.crl_issuer
  441. )
  442. def __ne__(self, other):
  443. return not self == other
  444. def __hash__(self):
  445. if self.full_name is not None:
  446. fn = tuple(self.full_name)
  447. else:
  448. fn = None
  449. if self.crl_issuer is not None:
  450. crl_issuer = tuple(self.crl_issuer)
  451. else:
  452. crl_issuer = None
  453. return hash((fn, self.relative_name, self.reasons, crl_issuer))
  454. full_name = utils.read_only_property("_full_name")
  455. relative_name = utils.read_only_property("_relative_name")
  456. reasons = utils.read_only_property("_reasons")
  457. crl_issuer = utils.read_only_property("_crl_issuer")
  458. class ReasonFlags(Enum):
  459. unspecified = "unspecified"
  460. key_compromise = "keyCompromise"
  461. ca_compromise = "cACompromise"
  462. affiliation_changed = "affiliationChanged"
  463. superseded = "superseded"
  464. cessation_of_operation = "cessationOfOperation"
  465. certificate_hold = "certificateHold"
  466. privilege_withdrawn = "privilegeWithdrawn"
  467. aa_compromise = "aACompromise"
  468. remove_from_crl = "removeFromCRL"
  469. @utils.register_interface(ExtensionType)
  470. class PolicyConstraints(object):
  471. oid = ExtensionOID.POLICY_CONSTRAINTS
  472. def __init__(self, require_explicit_policy, inhibit_policy_mapping):
  473. if require_explicit_policy is not None and not isinstance(
  474. require_explicit_policy, six.integer_types
  475. ):
  476. raise TypeError(
  477. "require_explicit_policy must be a non-negative integer or "
  478. "None"
  479. )
  480. if inhibit_policy_mapping is not None and not isinstance(
  481. inhibit_policy_mapping, six.integer_types
  482. ):
  483. raise TypeError(
  484. "inhibit_policy_mapping must be a non-negative integer or None"
  485. )
  486. if inhibit_policy_mapping is None and require_explicit_policy is None:
  487. raise ValueError(
  488. "At least one of require_explicit_policy and "
  489. "inhibit_policy_mapping must not be None"
  490. )
  491. self._require_explicit_policy = require_explicit_policy
  492. self._inhibit_policy_mapping = inhibit_policy_mapping
  493. def __repr__(self):
  494. return (
  495. u"<PolicyConstraints(require_explicit_policy={0.require_explicit"
  496. u"_policy}, inhibit_policy_mapping={0.inhibit_policy_"
  497. u"mapping})>".format(self)
  498. )
  499. def __eq__(self, other):
  500. if not isinstance(other, PolicyConstraints):
  501. return NotImplemented
  502. return (
  503. self.require_explicit_policy == other.require_explicit_policy and
  504. self.inhibit_policy_mapping == other.inhibit_policy_mapping
  505. )
  506. def __ne__(self, other):
  507. return not self == other
  508. def __hash__(self):
  509. return hash(
  510. (self.require_explicit_policy, self.inhibit_policy_mapping)
  511. )
  512. require_explicit_policy = utils.read_only_property(
  513. "_require_explicit_policy"
  514. )
  515. inhibit_policy_mapping = utils.read_only_property(
  516. "_inhibit_policy_mapping"
  517. )
  518. @utils.register_interface(ExtensionType)
  519. class CertificatePolicies(object):
  520. oid = ExtensionOID.CERTIFICATE_POLICIES
  521. def __init__(self, policies):
  522. policies = list(policies)
  523. if not all(isinstance(x, PolicyInformation) for x in policies):
  524. raise TypeError(
  525. "Every item in the policies list must be a "
  526. "PolicyInformation"
  527. )
  528. self._policies = policies
  529. __len__, __iter__, __getitem__ = _make_sequence_methods("_policies")
  530. def __repr__(self):
  531. return "<CertificatePolicies({})>".format(self._policies)
  532. def __eq__(self, other):
  533. if not isinstance(other, CertificatePolicies):
  534. return NotImplemented
  535. return self._policies == other._policies
  536. def __ne__(self, other):
  537. return not self == other
  538. def __hash__(self):
  539. return hash(tuple(self._policies))
  540. class PolicyInformation(object):
  541. def __init__(self, policy_identifier, policy_qualifiers):
  542. if not isinstance(policy_identifier, ObjectIdentifier):
  543. raise TypeError("policy_identifier must be an ObjectIdentifier")
  544. self._policy_identifier = policy_identifier
  545. if policy_qualifiers:
  546. policy_qualifiers = list(policy_qualifiers)
  547. if not all(
  548. isinstance(x, (six.text_type, UserNotice))
  549. for x in policy_qualifiers
  550. ):
  551. raise TypeError(
  552. "policy_qualifiers must be a list of strings and/or "
  553. "UserNotice objects or None"
  554. )
  555. self._policy_qualifiers = policy_qualifiers
  556. def __repr__(self):
  557. return (
  558. "<PolicyInformation(policy_identifier={0.policy_identifier}, polic"
  559. "y_qualifiers={0.policy_qualifiers})>".format(self)
  560. )
  561. def __eq__(self, other):
  562. if not isinstance(other, PolicyInformation):
  563. return NotImplemented
  564. return (
  565. self.policy_identifier == other.policy_identifier and
  566. self.policy_qualifiers == other.policy_qualifiers
  567. )
  568. def __ne__(self, other):
  569. return not self == other
  570. def __hash__(self):
  571. if self.policy_qualifiers is not None:
  572. pq = tuple(self.policy_qualifiers)
  573. else:
  574. pq = None
  575. return hash((self.policy_identifier, pq))
  576. policy_identifier = utils.read_only_property("_policy_identifier")
  577. policy_qualifiers = utils.read_only_property("_policy_qualifiers")
  578. class UserNotice(object):
  579. def __init__(self, notice_reference, explicit_text):
  580. if notice_reference and not isinstance(
  581. notice_reference, NoticeReference
  582. ):
  583. raise TypeError(
  584. "notice_reference must be None or a NoticeReference"
  585. )
  586. self._notice_reference = notice_reference
  587. self._explicit_text = explicit_text
  588. def __repr__(self):
  589. return (
  590. "<UserNotice(notice_reference={0.notice_reference}, explicit_text="
  591. "{0.explicit_text!r})>".format(self)
  592. )
  593. def __eq__(self, other):
  594. if not isinstance(other, UserNotice):
  595. return NotImplemented
  596. return (
  597. self.notice_reference == other.notice_reference and
  598. self.explicit_text == other.explicit_text
  599. )
  600. def __ne__(self, other):
  601. return not self == other
  602. def __hash__(self):
  603. return hash((self.notice_reference, self.explicit_text))
  604. notice_reference = utils.read_only_property("_notice_reference")
  605. explicit_text = utils.read_only_property("_explicit_text")
  606. class NoticeReference(object):
  607. def __init__(self, organization, notice_numbers):
  608. self._organization = organization
  609. notice_numbers = list(notice_numbers)
  610. if not all(isinstance(x, int) for x in notice_numbers):
  611. raise TypeError(
  612. "notice_numbers must be a list of integers"
  613. )
  614. self._notice_numbers = notice_numbers
  615. def __repr__(self):
  616. return (
  617. "<NoticeReference(organization={0.organization!r}, notice_numbers="
  618. "{0.notice_numbers})>".format(self)
  619. )
  620. def __eq__(self, other):
  621. if not isinstance(other, NoticeReference):
  622. return NotImplemented
  623. return (
  624. self.organization == other.organization and
  625. self.notice_numbers == other.notice_numbers
  626. )
  627. def __ne__(self, other):
  628. return not self == other
  629. def __hash__(self):
  630. return hash((self.organization, tuple(self.notice_numbers)))
  631. organization = utils.read_only_property("_organization")
  632. notice_numbers = utils.read_only_property("_notice_numbers")
  633. @utils.register_interface(ExtensionType)
  634. class ExtendedKeyUsage(object):
  635. oid = ExtensionOID.EXTENDED_KEY_USAGE
  636. def __init__(self, usages):
  637. usages = list(usages)
  638. if not all(isinstance(x, ObjectIdentifier) for x in usages):
  639. raise TypeError(
  640. "Every item in the usages list must be an ObjectIdentifier"
  641. )
  642. self._usages = usages
  643. __len__, __iter__, __getitem__ = _make_sequence_methods("_usages")
  644. def __repr__(self):
  645. return "<ExtendedKeyUsage({})>".format(self._usages)
  646. def __eq__(self, other):
  647. if not isinstance(other, ExtendedKeyUsage):
  648. return NotImplemented
  649. return self._usages == other._usages
  650. def __ne__(self, other):
  651. return not self == other
  652. def __hash__(self):
  653. return hash(tuple(self._usages))
  654. @utils.register_interface(ExtensionType)
  655. class OCSPNoCheck(object):
  656. oid = ExtensionOID.OCSP_NO_CHECK
  657. def __eq__(self, other):
  658. if not isinstance(other, OCSPNoCheck):
  659. return NotImplemented
  660. return True
  661. def __ne__(self, other):
  662. return not self == other
  663. def __hash__(self):
  664. return hash(OCSPNoCheck)
  665. def __repr__(self):
  666. return "<OCSPNoCheck()>"
  667. @utils.register_interface(ExtensionType)
  668. class PrecertPoison(object):
  669. oid = ExtensionOID.PRECERT_POISON
  670. def __eq__(self, other):
  671. if not isinstance(other, PrecertPoison):
  672. return NotImplemented
  673. return True
  674. def __ne__(self, other):
  675. return not self == other
  676. def __hash__(self):
  677. return hash(PrecertPoison)
  678. def __repr__(self):
  679. return "<PrecertPoison()>"
  680. @utils.register_interface(ExtensionType)
  681. class TLSFeature(object):
  682. oid = ExtensionOID.TLS_FEATURE
  683. def __init__(self, features):
  684. features = list(features)
  685. if (
  686. not all(isinstance(x, TLSFeatureType) for x in features) or
  687. len(features) == 0
  688. ):
  689. raise TypeError(
  690. "features must be a list of elements from the TLSFeatureType "
  691. "enum"
  692. )
  693. self._features = features
  694. __len__, __iter__, __getitem__ = _make_sequence_methods("_features")
  695. def __repr__(self):
  696. return "<TLSFeature(features={0._features})>".format(self)
  697. def __eq__(self, other):
  698. if not isinstance(other, TLSFeature):
  699. return NotImplemented
  700. return self._features == other._features
  701. def __ne__(self, other):
  702. return not self == other
  703. def __hash__(self):
  704. return hash(tuple(self._features))
  705. class TLSFeatureType(Enum):
  706. # status_request is defined in RFC 6066 and is used for what is commonly
  707. # called OCSP Must-Staple when present in the TLS Feature extension in an
  708. # X.509 certificate.
  709. status_request = 5
  710. # status_request_v2 is defined in RFC 6961 and allows multiple OCSP
  711. # responses to be provided. It is not currently in use by clients or
  712. # servers.
  713. status_request_v2 = 17
  714. _TLS_FEATURE_TYPE_TO_ENUM = dict((x.value, x) for x in TLSFeatureType)
  715. @utils.register_interface(ExtensionType)
  716. class InhibitAnyPolicy(object):
  717. oid = ExtensionOID.INHIBIT_ANY_POLICY
  718. def __init__(self, skip_certs):
  719. if not isinstance(skip_certs, six.integer_types):
  720. raise TypeError("skip_certs must be an integer")
  721. if skip_certs < 0:
  722. raise ValueError("skip_certs must be a non-negative integer")
  723. self._skip_certs = skip_certs
  724. def __repr__(self):
  725. return "<InhibitAnyPolicy(skip_certs={0.skip_certs})>".format(self)
  726. def __eq__(self, other):
  727. if not isinstance(other, InhibitAnyPolicy):
  728. return NotImplemented
  729. return self.skip_certs == other.skip_certs
  730. def __ne__(self, other):
  731. return not self == other
  732. def __hash__(self):
  733. return hash(self.skip_certs)
  734. skip_certs = utils.read_only_property("_skip_certs")
  735. @utils.register_interface(ExtensionType)
  736. class KeyUsage(object):
  737. oid = ExtensionOID.KEY_USAGE
  738. def __init__(self, digital_signature, content_commitment, key_encipherment,
  739. data_encipherment, key_agreement, key_cert_sign, crl_sign,
  740. encipher_only, decipher_only):
  741. if not key_agreement and (encipher_only or decipher_only):
  742. raise ValueError(
  743. "encipher_only and decipher_only can only be true when "
  744. "key_agreement is true"
  745. )
  746. self._digital_signature = digital_signature
  747. self._content_commitment = content_commitment
  748. self._key_encipherment = key_encipherment
  749. self._data_encipherment = data_encipherment
  750. self._key_agreement = key_agreement
  751. self._key_cert_sign = key_cert_sign
  752. self._crl_sign = crl_sign
  753. self._encipher_only = encipher_only
  754. self._decipher_only = decipher_only
  755. digital_signature = utils.read_only_property("_digital_signature")
  756. content_commitment = utils.read_only_property("_content_commitment")
  757. key_encipherment = utils.read_only_property("_key_encipherment")
  758. data_encipherment = utils.read_only_property("_data_encipherment")
  759. key_agreement = utils.read_only_property("_key_agreement")
  760. key_cert_sign = utils.read_only_property("_key_cert_sign")
  761. crl_sign = utils.read_only_property("_crl_sign")
  762. @property
  763. def encipher_only(self):
  764. if not self.key_agreement:
  765. raise ValueError(
  766. "encipher_only is undefined unless key_agreement is true"
  767. )
  768. else:
  769. return self._encipher_only
  770. @property
  771. def decipher_only(self):
  772. if not self.key_agreement:
  773. raise ValueError(
  774. "decipher_only is undefined unless key_agreement is true"
  775. )
  776. else:
  777. return self._decipher_only
  778. def __repr__(self):
  779. try:
  780. encipher_only = self.encipher_only
  781. decipher_only = self.decipher_only
  782. except ValueError:
  783. encipher_only = None
  784. decipher_only = None
  785. return ("<KeyUsage(digital_signature={0.digital_signature}, "
  786. "content_commitment={0.content_commitment}, "
  787. "key_encipherment={0.key_encipherment}, "
  788. "data_encipherment={0.data_encipherment}, "
  789. "key_agreement={0.key_agreement}, "
  790. "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, "
  791. "encipher_only={1}, decipher_only={2})>").format(
  792. self, encipher_only, decipher_only)
  793. def __eq__(self, other):
  794. if not isinstance(other, KeyUsage):
  795. return NotImplemented
  796. return (
  797. self.digital_signature == other.digital_signature and
  798. self.content_commitment == other.content_commitment and
  799. self.key_encipherment == other.key_encipherment and
  800. self.data_encipherment == other.data_encipherment and
  801. self.key_agreement == other.key_agreement and
  802. self.key_cert_sign == other.key_cert_sign and
  803. self.crl_sign == other.crl_sign and
  804. self._encipher_only == other._encipher_only and
  805. self._decipher_only == other._decipher_only
  806. )
  807. def __ne__(self, other):
  808. return not self == other
  809. def __hash__(self):
  810. return hash((
  811. self.digital_signature, self.content_commitment,
  812. self.key_encipherment, self.data_encipherment,
  813. self.key_agreement, self.key_cert_sign,
  814. self.crl_sign, self._encipher_only,
  815. self._decipher_only
  816. ))
  817. @utils.register_interface(ExtensionType)
  818. class NameConstraints(object):
  819. oid = ExtensionOID.NAME_CONSTRAINTS
  820. def __init__(self, permitted_subtrees, excluded_subtrees):
  821. if permitted_subtrees is not None:
  822. permitted_subtrees = list(permitted_subtrees)
  823. if not all(
  824. isinstance(x, GeneralName) for x in permitted_subtrees
  825. ):
  826. raise TypeError(
  827. "permitted_subtrees must be a list of GeneralName objects "
  828. "or None"
  829. )
  830. self._validate_ip_name(permitted_subtrees)
  831. if excluded_subtrees is not None:
  832. excluded_subtrees = list(excluded_subtrees)
  833. if not all(
  834. isinstance(x, GeneralName) for x in excluded_subtrees
  835. ):
  836. raise TypeError(
  837. "excluded_subtrees must be a list of GeneralName objects "
  838. "or None"
  839. )
  840. self._validate_ip_name(excluded_subtrees)
  841. if permitted_subtrees is None and excluded_subtrees is None:
  842. raise ValueError(
  843. "At least one of permitted_subtrees and excluded_subtrees "
  844. "must not be None"
  845. )
  846. self._permitted_subtrees = permitted_subtrees
  847. self._excluded_subtrees = excluded_subtrees
  848. def __eq__(self, other):
  849. if not isinstance(other, NameConstraints):
  850. return NotImplemented
  851. return (
  852. self.excluded_subtrees == other.excluded_subtrees and
  853. self.permitted_subtrees == other.permitted_subtrees
  854. )
  855. def __ne__(self, other):
  856. return not self == other
  857. def _validate_ip_name(self, tree):
  858. if any(isinstance(name, IPAddress) and not isinstance(
  859. name.value, (ipaddress.IPv4Network, ipaddress.IPv6Network)
  860. ) for name in tree):
  861. raise TypeError(
  862. "IPAddress name constraints must be an IPv4Network or"
  863. " IPv6Network object"
  864. )
  865. def __repr__(self):
  866. return (
  867. u"<NameConstraints(permitted_subtrees={0.permitted_subtrees}, "
  868. u"excluded_subtrees={0.excluded_subtrees})>".format(self)
  869. )
  870. def __hash__(self):
  871. if self.permitted_subtrees is not None:
  872. ps = tuple(self.permitted_subtrees)
  873. else:
  874. ps = None
  875. if self.excluded_subtrees is not None:
  876. es = tuple(self.excluded_subtrees)
  877. else:
  878. es = None
  879. return hash((ps, es))
  880. permitted_subtrees = utils.read_only_property("_permitted_subtrees")
  881. excluded_subtrees = utils.read_only_property("_excluded_subtrees")
  882. class Extension(object):
  883. def __init__(self, oid, critical, value):
  884. if not isinstance(oid, ObjectIdentifier):
  885. raise TypeError(
  886. "oid argument must be an ObjectIdentifier instance."
  887. )
  888. if not isinstance(critical, bool):
  889. raise TypeError("critical must be a boolean value")
  890. self._oid = oid
  891. self._critical = critical
  892. self._value = value
  893. oid = utils.read_only_property("_oid")
  894. critical = utils.read_only_property("_critical")
  895. value = utils.read_only_property("_value")
  896. def __repr__(self):
  897. return ("<Extension(oid={0.oid}, critical={0.critical}, "
  898. "value={0.value})>").format(self)
  899. def __eq__(self, other):
  900. if not isinstance(other, Extension):
  901. return NotImplemented
  902. return (
  903. self.oid == other.oid and
  904. self.critical == other.critical and
  905. self.value == other.value
  906. )
  907. def __ne__(self, other):
  908. return not self == other
  909. def __hash__(self):
  910. return hash((self.oid, self.critical, self.value))
  911. class GeneralNames(object):
  912. def __init__(self, general_names):
  913. general_names = list(general_names)
  914. if not all(isinstance(x, GeneralName) for x in general_names):
  915. raise TypeError(
  916. "Every item in the general_names list must be an "
  917. "object conforming to the GeneralName interface"
  918. )
  919. self._general_names = general_names
  920. __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
  921. def get_values_for_type(self, type):
  922. # Return the value of each GeneralName, except for OtherName instances
  923. # which we return directly because it has two important properties not
  924. # just one value.
  925. objs = (i for i in self if isinstance(i, type))
  926. if type != OtherName:
  927. objs = (i.value for i in objs)
  928. return list(objs)
  929. def __repr__(self):
  930. return "<GeneralNames({})>".format(self._general_names)
  931. def __eq__(self, other):
  932. if not isinstance(other, GeneralNames):
  933. return NotImplemented
  934. return self._general_names == other._general_names
  935. def __ne__(self, other):
  936. return not self == other
  937. def __hash__(self):
  938. return hash(tuple(self._general_names))
  939. @utils.register_interface(ExtensionType)
  940. class SubjectAlternativeName(object):
  941. oid = ExtensionOID.SUBJECT_ALTERNATIVE_NAME
  942. def __init__(self, general_names):
  943. self._general_names = GeneralNames(general_names)
  944. __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
  945. def get_values_for_type(self, type):
  946. return self._general_names.get_values_for_type(type)
  947. def __repr__(self):
  948. return "<SubjectAlternativeName({})>".format(self._general_names)
  949. def __eq__(self, other):
  950. if not isinstance(other, SubjectAlternativeName):
  951. return NotImplemented
  952. return self._general_names == other._general_names
  953. def __ne__(self, other):
  954. return not self == other
  955. def __hash__(self):
  956. return hash(self._general_names)
  957. @utils.register_interface(ExtensionType)
  958. class IssuerAlternativeName(object):
  959. oid = ExtensionOID.ISSUER_ALTERNATIVE_NAME
  960. def __init__(self, general_names):
  961. self._general_names = GeneralNames(general_names)
  962. __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
  963. def get_values_for_type(self, type):
  964. return self._general_names.get_values_for_type(type)
  965. def __repr__(self):
  966. return "<IssuerAlternativeName({})>".format(self._general_names)
  967. def __eq__(self, other):
  968. if not isinstance(other, IssuerAlternativeName):
  969. return NotImplemented
  970. return self._general_names == other._general_names
  971. def __ne__(self, other):
  972. return not self == other
  973. def __hash__(self):
  974. return hash(self._general_names)
  975. @utils.register_interface(ExtensionType)
  976. class CertificateIssuer(object):
  977. oid = CRLEntryExtensionOID.CERTIFICATE_ISSUER
  978. def __init__(self, general_names):
  979. self._general_names = GeneralNames(general_names)
  980. __len__, __iter__, __getitem__ = _make_sequence_methods("_general_names")
  981. def get_values_for_type(self, type):
  982. return self._general_names.get_values_for_type(type)
  983. def __repr__(self):
  984. return "<CertificateIssuer({})>".format(self._general_names)
  985. def __eq__(self, other):
  986. if not isinstance(other, CertificateIssuer):
  987. return NotImplemented
  988. return self._general_names == other._general_names
  989. def __ne__(self, other):
  990. return not self == other
  991. def __hash__(self):
  992. return hash(self._general_names)
  993. @utils.register_interface(ExtensionType)
  994. class CRLReason(object):
  995. oid = CRLEntryExtensionOID.CRL_REASON
  996. def __init__(self, reason):
  997. if not isinstance(reason, ReasonFlags):
  998. raise TypeError("reason must be an element from ReasonFlags")
  999. self._reason = reason
  1000. def __repr__(self):
  1001. return "<CRLReason(reason={})>".format(self._reason)
  1002. def __eq__(self, other):
  1003. if not isinstance(other, CRLReason):
  1004. return NotImplemented
  1005. return self.reason == other.reason
  1006. def __ne__(self, other):
  1007. return not self == other
  1008. def __hash__(self):
  1009. return hash(self.reason)
  1010. reason = utils.read_only_property("_reason")
  1011. @utils.register_interface(ExtensionType)
  1012. class InvalidityDate(object):
  1013. oid = CRLEntryExtensionOID.INVALIDITY_DATE
  1014. def __init__(self, invalidity_date):
  1015. if not isinstance(invalidity_date, datetime.datetime):
  1016. raise TypeError("invalidity_date must be a datetime.datetime")
  1017. self._invalidity_date = invalidity_date
  1018. def __repr__(self):
  1019. return "<InvalidityDate(invalidity_date={})>".format(
  1020. self._invalidity_date
  1021. )
  1022. def __eq__(self, other):
  1023. if not isinstance(other, InvalidityDate):
  1024. return NotImplemented
  1025. return self.invalidity_date == other.invalidity_date
  1026. def __ne__(self, other):
  1027. return not self == other
  1028. def __hash__(self):
  1029. return hash(self.invalidity_date)
  1030. invalidity_date = utils.read_only_property("_invalidity_date")
  1031. @utils.register_interface(ExtensionType)
  1032. class PrecertificateSignedCertificateTimestamps(object):
  1033. oid = ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS
  1034. def __init__(self, signed_certificate_timestamps):
  1035. signed_certificate_timestamps = list(signed_certificate_timestamps)
  1036. if not all(
  1037. isinstance(sct, SignedCertificateTimestamp)
  1038. for sct in signed_certificate_timestamps
  1039. ):
  1040. raise TypeError(
  1041. "Every item in the signed_certificate_timestamps list must be "
  1042. "a SignedCertificateTimestamp"
  1043. )
  1044. self._signed_certificate_timestamps = signed_certificate_timestamps
  1045. __len__, __iter__, __getitem__ = _make_sequence_methods(
  1046. "_signed_certificate_timestamps"
  1047. )
  1048. def __repr__(self):
  1049. return (
  1050. "<PrecertificateSignedCertificateTimestamps({})>".format(
  1051. list(self)
  1052. )
  1053. )
  1054. def __hash__(self):
  1055. return hash(tuple(self._signed_certificate_timestamps))
  1056. def __eq__(self, other):
  1057. if not isinstance(other, PrecertificateSignedCertificateTimestamps):
  1058. return NotImplemented
  1059. return (
  1060. self._signed_certificate_timestamps ==
  1061. other._signed_certificate_timestamps
  1062. )
  1063. def __ne__(self, other):
  1064. return not self == other
  1065. @utils.register_interface(ExtensionType)
  1066. class OCSPNonce(object):
  1067. oid = OCSPExtensionOID.NONCE
  1068. def __init__(self, nonce):
  1069. if not isinstance(nonce, bytes):
  1070. raise TypeError("nonce must be bytes")
  1071. self._nonce = nonce
  1072. def __eq__(self, other):
  1073. if not isinstance(other, OCSPNonce):
  1074. return NotImplemented
  1075. return self.nonce == other.nonce
  1076. def __ne__(self, other):
  1077. return not self == other
  1078. def __hash__(self):
  1079. return hash(self.nonce)
  1080. def __repr__(self):
  1081. return "<OCSPNonce(nonce={0.nonce!r})>".format(self)
  1082. nonce = utils.read_only_property("_nonce")
  1083. @utils.register_interface(ExtensionType)
  1084. class IssuingDistributionPoint(object):
  1085. oid = ExtensionOID.ISSUING_DISTRIBUTION_POINT
  1086. def __init__(self, full_name, relative_name, only_contains_user_certs,
  1087. only_contains_ca_certs, only_some_reasons, indirect_crl,
  1088. only_contains_attribute_certs):
  1089. if (
  1090. only_some_reasons and (
  1091. not isinstance(only_some_reasons, frozenset) or not all(
  1092. isinstance(x, ReasonFlags) for x in only_some_reasons
  1093. )
  1094. )
  1095. ):
  1096. raise TypeError(
  1097. "only_some_reasons must be None or frozenset of ReasonFlags"
  1098. )
  1099. if only_some_reasons and (
  1100. ReasonFlags.unspecified in only_some_reasons or
  1101. ReasonFlags.remove_from_crl in only_some_reasons
  1102. ):
  1103. raise ValueError(
  1104. "unspecified and remove_from_crl are not valid reasons in an "
  1105. "IssuingDistributionPoint"
  1106. )
  1107. if not (
  1108. isinstance(only_contains_user_certs, bool) and
  1109. isinstance(only_contains_ca_certs, bool) and
  1110. isinstance(indirect_crl, bool) and
  1111. isinstance(only_contains_attribute_certs, bool)
  1112. ):
  1113. raise TypeError(
  1114. "only_contains_user_certs, only_contains_ca_certs, "
  1115. "indirect_crl and only_contains_attribute_certs "
  1116. "must all be boolean."
  1117. )
  1118. crl_constraints = [
  1119. only_contains_user_certs, only_contains_ca_certs,
  1120. indirect_crl, only_contains_attribute_certs
  1121. ]
  1122. if len([x for x in crl_constraints if x]) > 1:
  1123. raise ValueError(
  1124. "Only one of the following can be set to True: "
  1125. "only_contains_user_certs, only_contains_ca_certs, "
  1126. "indirect_crl, only_contains_attribute_certs"
  1127. )
  1128. if (
  1129. not any([
  1130. only_contains_user_certs, only_contains_ca_certs,
  1131. indirect_crl, only_contains_attribute_certs, full_name,
  1132. relative_name, only_some_reasons
  1133. ])
  1134. ):
  1135. raise ValueError(
  1136. "Cannot create empty extension: "
  1137. "if only_contains_user_certs, only_contains_ca_certs, "
  1138. "indirect_crl, and only_contains_attribute_certs are all False"
  1139. ", then either full_name, relative_name, or only_some_reasons "
  1140. "must have a value."
  1141. )
  1142. self._only_contains_user_certs = only_contains_user_certs
  1143. self._only_contains_ca_certs = only_contains_ca_certs
  1144. self._indirect_crl = indirect_crl
  1145. self._only_contains_attribute_certs = only_contains_attribute_certs
  1146. self._only_some_reasons = only_some_reasons
  1147. self._full_name = full_name
  1148. self._relative_name = relative_name
  1149. def __repr__(self):
  1150. return (
  1151. "<IssuingDistributionPoint(full_name={0.full_name}, "
  1152. "relative_name={0.relative_name}, "
  1153. "only_contains_user_certs={0.only_contains_user_certs}, "
  1154. "only_contains_ca_certs={0.only_contains_ca_certs}, "
  1155. "only_some_reasons={0.only_some_reasons}, "
  1156. "indirect_crl={0.indirect_crl}, "
  1157. "only_contains_attribute_certs="
  1158. "{0.only_contains_attribute_certs})>".format(self)
  1159. )
  1160. def __eq__(self, other):
  1161. if not isinstance(other, IssuingDistributionPoint):
  1162. return NotImplemented
  1163. return (
  1164. self.full_name == other.full_name and
  1165. self.relative_name == other.relative_name and
  1166. self.only_contains_user_certs == other.only_contains_user_certs and
  1167. self.only_contains_ca_certs == other.only_contains_ca_certs and
  1168. self.only_some_reasons == other.only_some_reasons and
  1169. self.indirect_crl == other.indirect_crl and
  1170. self.only_contains_attribute_certs ==
  1171. other.only_contains_attribute_certs
  1172. )
  1173. def __ne__(self, other):
  1174. return not self == other
  1175. def __hash__(self):
  1176. return hash((
  1177. self.full_name,
  1178. self.relative_name,
  1179. self.only_contains_user_certs,
  1180. self.only_contains_ca_certs,
  1181. self.only_some_reasons,
  1182. self.indirect_crl,
  1183. self.only_contains_attribute_certs,
  1184. ))
  1185. full_name = utils.read_only_property("_full_name")
  1186. relative_name = utils.read_only_property("_relative_name")
  1187. only_contains_user_certs = utils.read_only_property(
  1188. "_only_contains_user_certs"
  1189. )
  1190. only_contains_ca_certs = utils.read_only_property(
  1191. "_only_contains_ca_certs"
  1192. )
  1193. only_some_reasons = utils.read_only_property("_only_some_reasons")
  1194. indirect_crl = utils.read_only_property("_indirect_crl")
  1195. only_contains_attribute_certs = utils.read_only_property(
  1196. "_only_contains_attribute_certs"
  1197. )
  1198. @utils.register_interface(ExtensionType)
  1199. class UnrecognizedExtension(object):
  1200. def __init__(self, oid, value):
  1201. if not isinstance(oid, ObjectIdentifier):
  1202. raise TypeError("oid must be an ObjectIdentifier")
  1203. self._oid = oid
  1204. self._value = value
  1205. oid = utils.read_only_property("_oid")
  1206. value = utils.read_only_property("_value")
  1207. def __repr__(self):
  1208. return (
  1209. "<UnrecognizedExtension(oid={0.oid}, value={0.value!r})>".format(
  1210. self
  1211. )
  1212. )
  1213. def __eq__(self, other):
  1214. if not isinstance(other, UnrecognizedExtension):
  1215. return NotImplemented
  1216. return self.oid == other.oid and self.value == other.value
  1217. def __ne__(self, other):
  1218. return not self == other
  1219. def __hash__(self):
  1220. return hash((self.oid, self.value))