extensions.py 52 KB

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