Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 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 | |
| 5 | from __future__ import absolute_import, division, print_function |
| 6 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 7 | import abc |
Paul Kehrer | 23c0bbc | 2015-12-25 22:35:19 -0600 | [diff] [blame] | 8 | import datetime |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 9 | import hashlib |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 10 | import ipaddress |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 11 | from enum import Enum |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 12 | |
Ofek Lev | 0e6a129 | 2017-02-08 00:09:41 -0500 | [diff] [blame] | 13 | from asn1crypto.keys import PublicKeyInfo |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 14 | |
| 15 | import six |
| 16 | |
| 17 | from cryptography import utils |
Predrag Gruevski | 3899539 | 2015-09-21 21:53:49 -0400 | [diff] [blame] | 18 | from cryptography.hazmat.primitives import constant_time, serialization |
Alex Gaynor | 01c7049 | 2016-03-27 17:00:59 -0400 | [diff] [blame] | 19 | from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePublicKey |
Alex Gaynor | beb2551 | 2016-03-27 16:39:49 -0400 | [diff] [blame] | 20 | from cryptography.hazmat.primitives.asymmetric.rsa import RSAPublicKey |
Alex Gaynor | 6a0718f | 2017-06-04 13:36:58 -0400 | [diff] [blame] | 21 | from cryptography.x509.certificate_transparency import ( |
| 22 | SignedCertificateTimestamp |
| 23 | ) |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 24 | from cryptography.x509.general_name import GeneralName, IPAddress, OtherName |
Alex Gaynor | a783c57 | 2017-03-21 09:24:12 -0400 | [diff] [blame] | 25 | from cryptography.x509.name import RelativeDistinguishedName |
Paul Kehrer | 49bb756 | 2015-12-25 16:17:40 -0600 | [diff] [blame] | 26 | from cryptography.x509.oid import ( |
Paul Kehrer | 0940310 | 2018-09-09 21:57:21 -0500 | [diff] [blame] | 27 | CRLEntryExtensionOID, ExtensionOID, OCSPExtensionOID, ObjectIdentifier, |
Paul Kehrer | 49bb756 | 2015-12-25 16:17:40 -0600 | [diff] [blame] | 28 | ) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 29 | |
| 30 | |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 31 | def _key_identifier_from_public_key(public_key): |
Alex Gaynor | beb2551 | 2016-03-27 16:39:49 -0400 | [diff] [blame] | 32 | if isinstance(public_key, RSAPublicKey): |
| 33 | data = public_key.public_bytes( |
| 34 | serialization.Encoding.DER, |
| 35 | serialization.PublicFormat.PKCS1, |
| 36 | ) |
Alex Gaynor | 01c7049 | 2016-03-27 17:00:59 -0400 | [diff] [blame] | 37 | elif isinstance(public_key, EllipticCurvePublicKey): |
| 38 | data = public_key.public_numbers().encode_point() |
Alex Gaynor | beb2551 | 2016-03-27 16:39:49 -0400 | [diff] [blame] | 39 | else: |
| 40 | # This is a very slow way to do this. |
| 41 | serialized = public_key.public_bytes( |
| 42 | serialization.Encoding.DER, |
| 43 | serialization.PublicFormat.SubjectPublicKeyInfo |
| 44 | ) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 45 | |
Jon Dufresne | 6654329 | 2018-11-11 18:21:27 -0800 | [diff] [blame] | 46 | data = bytes(PublicKeyInfo.load(serialized)['public_key']) |
Alex Gaynor | beb2551 | 2016-03-27 16:39:49 -0400 | [diff] [blame] | 47 | |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 48 | return hashlib.sha1(data).digest() |
| 49 | |
| 50 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 51 | class DuplicateExtension(Exception): |
| 52 | def __init__(self, msg, oid): |
| 53 | super(DuplicateExtension, self).__init__(msg) |
| 54 | self.oid = oid |
| 55 | |
| 56 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 57 | class ExtensionNotFound(Exception): |
| 58 | def __init__(self, msg, oid): |
| 59 | super(ExtensionNotFound, self).__init__(msg) |
| 60 | self.oid = oid |
| 61 | |
| 62 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 63 | @six.add_metaclass(abc.ABCMeta) |
| 64 | class ExtensionType(object): |
| 65 | @abc.abstractproperty |
| 66 | def oid(self): |
| 67 | """ |
| 68 | Returns the oid associated with the given extension type. |
| 69 | """ |
| 70 | |
| 71 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 72 | class Extensions(object): |
| 73 | def __init__(self, extensions): |
| 74 | self._extensions = extensions |
| 75 | |
| 76 | def get_extension_for_oid(self, oid): |
| 77 | for ext in self: |
| 78 | if ext.oid == oid: |
| 79 | return ext |
| 80 | |
| 81 | raise ExtensionNotFound("No {0} extension was found".format(oid), oid) |
| 82 | |
Phoebe Queen | 64cf4cd | 2015-08-12 02:28:43 +0100 | [diff] [blame] | 83 | def get_extension_for_class(self, extclass): |
Paul Kehrer | e69c5fe | 2015-12-30 21:03:26 -0600 | [diff] [blame] | 84 | if extclass is UnrecognizedExtension: |
| 85 | raise TypeError( |
| 86 | "UnrecognizedExtension can't be used with " |
| 87 | "get_extension_for_class because more than one instance of the" |
| 88 | " class may be present." |
| 89 | ) |
| 90 | |
Phoebe Queen | 64cf4cd | 2015-08-12 02:28:43 +0100 | [diff] [blame] | 91 | for ext in self: |
Phoebe Queen | 754be60 | 2015-08-12 03:11:35 +0100 | [diff] [blame] | 92 | if isinstance(ext.value, extclass): |
Phoebe Queen | 64cf4cd | 2015-08-12 02:28:43 +0100 | [diff] [blame] | 93 | return ext |
| 94 | |
Phoebe Queen | 2cc111a | 2015-08-12 04:14:22 +0100 | [diff] [blame] | 95 | raise ExtensionNotFound( |
Phoebe Queen | ecae981 | 2015-08-12 05:00:32 +0100 | [diff] [blame] | 96 | "No {0} extension was found".format(extclass), extclass.oid |
Phoebe Queen | 2cc111a | 2015-08-12 04:14:22 +0100 | [diff] [blame] | 97 | ) |
Phoebe Queen | 64cf4cd | 2015-08-12 02:28:43 +0100 | [diff] [blame] | 98 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 99 | def __iter__(self): |
| 100 | return iter(self._extensions) |
| 101 | |
| 102 | def __len__(self): |
| 103 | return len(self._extensions) |
| 104 | |
Paul Kehrer | 5b90c97 | 2015-12-26 00:52:58 -0600 | [diff] [blame] | 105 | def __getitem__(self, idx): |
| 106 | return self._extensions[idx] |
| 107 | |
Paul Kehrer | afbe75b | 2015-10-20 08:08:43 -0500 | [diff] [blame] | 108 | def __repr__(self): |
| 109 | return ( |
| 110 | "<Extensions({0})>".format(self._extensions) |
| 111 | ) |
| 112 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 113 | |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 114 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 3b95cd7 | 2015-12-22 21:40:20 -0600 | [diff] [blame] | 115 | class CRLNumber(object): |
| 116 | oid = ExtensionOID.CRL_NUMBER |
| 117 | |
| 118 | def __init__(self, crl_number): |
| 119 | if not isinstance(crl_number, six.integer_types): |
| 120 | raise TypeError("crl_number must be an integer") |
| 121 | |
| 122 | self._crl_number = crl_number |
| 123 | |
| 124 | def __eq__(self, other): |
| 125 | if not isinstance(other, CRLNumber): |
| 126 | return NotImplemented |
| 127 | |
| 128 | return self.crl_number == other.crl_number |
| 129 | |
| 130 | def __ne__(self, other): |
| 131 | return not self == other |
| 132 | |
Alex Gaynor | f9a77b6 | 2015-12-26 12:14:25 -0500 | [diff] [blame] | 133 | def __hash__(self): |
| 134 | return hash(self.crl_number) |
| 135 | |
Paul Kehrer | 3b95cd7 | 2015-12-22 21:40:20 -0600 | [diff] [blame] | 136 | def __repr__(self): |
| 137 | return "<CRLNumber({0})>".format(self.crl_number) |
| 138 | |
| 139 | crl_number = utils.read_only_property("_crl_number") |
| 140 | |
| 141 | |
| 142 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 143 | class AuthorityKeyIdentifier(object): |
| 144 | oid = ExtensionOID.AUTHORITY_KEY_IDENTIFIER |
| 145 | |
| 146 | def __init__(self, key_identifier, authority_cert_issuer, |
| 147 | authority_cert_serial_number): |
Paul Kehrer | 0d943bb | 2016-01-05 19:02:32 -0600 | [diff] [blame] | 148 | if (authority_cert_issuer is None) != ( |
| 149 | authority_cert_serial_number is None |
| 150 | ): |
| 151 | raise ValueError( |
| 152 | "authority_cert_issuer and authority_cert_serial_number " |
| 153 | "must both be present or both None" |
| 154 | ) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 155 | |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 156 | if authority_cert_issuer is not None: |
| 157 | authority_cert_issuer = list(authority_cert_issuer) |
| 158 | if not all( |
| 159 | isinstance(x, GeneralName) for x in authority_cert_issuer |
| 160 | ): |
| 161 | raise TypeError( |
| 162 | "authority_cert_issuer must be a list of GeneralName " |
| 163 | "objects" |
| 164 | ) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 165 | |
Paul Kehrer | 0d943bb | 2016-01-05 19:02:32 -0600 | [diff] [blame] | 166 | if authority_cert_serial_number is not None and not isinstance( |
| 167 | authority_cert_serial_number, six.integer_types |
| 168 | ): |
| 169 | raise TypeError( |
| 170 | "authority_cert_serial_number must be an integer" |
| 171 | ) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 172 | |
| 173 | self._key_identifier = key_identifier |
| 174 | self._authority_cert_issuer = authority_cert_issuer |
| 175 | self._authority_cert_serial_number = authority_cert_serial_number |
| 176 | |
| 177 | @classmethod |
| 178 | def from_issuer_public_key(cls, public_key): |
| 179 | digest = _key_identifier_from_public_key(public_key) |
| 180 | return cls( |
| 181 | key_identifier=digest, |
| 182 | authority_cert_issuer=None, |
| 183 | authority_cert_serial_number=None |
| 184 | ) |
| 185 | |
Paul Kehrer | 61ff356 | 2016-03-11 22:51:27 -0400 | [diff] [blame] | 186 | @classmethod |
| 187 | def from_issuer_subject_key_identifier(cls, ski): |
| 188 | return cls( |
| 189 | key_identifier=ski.value.digest, |
| 190 | authority_cert_issuer=None, |
| 191 | authority_cert_serial_number=None |
| 192 | ) |
| 193 | |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 194 | def __repr__(self): |
| 195 | return ( |
| 196 | "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, " |
| 197 | "authority_cert_issuer={0.authority_cert_issuer}, " |
| 198 | "authority_cert_serial_number={0.authority_cert_serial_number}" |
| 199 | ")>".format(self) |
| 200 | ) |
| 201 | |
| 202 | def __eq__(self, other): |
| 203 | if not isinstance(other, AuthorityKeyIdentifier): |
| 204 | return NotImplemented |
| 205 | |
| 206 | return ( |
| 207 | self.key_identifier == other.key_identifier and |
| 208 | self.authority_cert_issuer == other.authority_cert_issuer and |
| 209 | self.authority_cert_serial_number == |
| 210 | other.authority_cert_serial_number |
| 211 | ) |
| 212 | |
| 213 | def __ne__(self, other): |
| 214 | return not self == other |
| 215 | |
Paul Kehrer | 979c263 | 2017-09-14 04:24:30 +0800 | [diff] [blame] | 216 | def __hash__(self): |
| 217 | if self.authority_cert_issuer is None: |
| 218 | aci = None |
| 219 | else: |
| 220 | aci = tuple(self.authority_cert_issuer) |
| 221 | return hash(( |
| 222 | self.key_identifier, aci, self.authority_cert_serial_number |
| 223 | )) |
| 224 | |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 225 | key_identifier = utils.read_only_property("_key_identifier") |
| 226 | authority_cert_issuer = utils.read_only_property("_authority_cert_issuer") |
| 227 | authority_cert_serial_number = utils.read_only_property( |
| 228 | "_authority_cert_serial_number" |
| 229 | ) |
| 230 | |
| 231 | |
| 232 | @utils.register_interface(ExtensionType) |
| 233 | class SubjectKeyIdentifier(object): |
| 234 | oid = ExtensionOID.SUBJECT_KEY_IDENTIFIER |
| 235 | |
| 236 | def __init__(self, digest): |
| 237 | self._digest = digest |
| 238 | |
| 239 | @classmethod |
| 240 | def from_public_key(cls, public_key): |
| 241 | return cls(_key_identifier_from_public_key(public_key)) |
| 242 | |
| 243 | digest = utils.read_only_property("_digest") |
| 244 | |
| 245 | def __repr__(self): |
| 246 | return "<SubjectKeyIdentifier(digest={0!r})>".format(self.digest) |
| 247 | |
| 248 | def __eq__(self, other): |
| 249 | if not isinstance(other, SubjectKeyIdentifier): |
| 250 | return NotImplemented |
| 251 | |
Predrag Gruevski | 57f3b3f | 2015-09-21 18:51:47 -0400 | [diff] [blame] | 252 | return constant_time.bytes_eq(self.digest, other.digest) |
Paul Kehrer | 890cb7f | 2015-08-10 21:05:34 -0500 | [diff] [blame] | 253 | |
| 254 | def __ne__(self, other): |
| 255 | return not self == other |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 256 | |
Alex Gaynor | 410fe35 | 2015-12-26 15:01:25 -0500 | [diff] [blame] | 257 | def __hash__(self): |
| 258 | return hash(self.digest) |
| 259 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 260 | |
| 261 | @utils.register_interface(ExtensionType) |
| 262 | class AuthorityInformationAccess(object): |
| 263 | oid = ExtensionOID.AUTHORITY_INFORMATION_ACCESS |
| 264 | |
| 265 | def __init__(self, descriptions): |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 266 | descriptions = list(descriptions) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 267 | if not all(isinstance(x, AccessDescription) for x in descriptions): |
| 268 | raise TypeError( |
| 269 | "Every item in the descriptions list must be an " |
| 270 | "AccessDescription" |
| 271 | ) |
| 272 | |
| 273 | self._descriptions = descriptions |
| 274 | |
| 275 | def __iter__(self): |
| 276 | return iter(self._descriptions) |
| 277 | |
| 278 | def __len__(self): |
| 279 | return len(self._descriptions) |
| 280 | |
| 281 | def __repr__(self): |
| 282 | return "<AuthorityInformationAccess({0})>".format(self._descriptions) |
| 283 | |
| 284 | def __eq__(self, other): |
| 285 | if not isinstance(other, AuthorityInformationAccess): |
| 286 | return NotImplemented |
| 287 | |
| 288 | return self._descriptions == other._descriptions |
| 289 | |
| 290 | def __ne__(self, other): |
| 291 | return not self == other |
| 292 | |
Paul Kehrer | ad4b359 | 2015-12-27 17:27:40 -0600 | [diff] [blame] | 293 | def __getitem__(self, idx): |
| 294 | return self._descriptions[idx] |
| 295 | |
Paul Kehrer | 5402449 | 2017-09-14 01:55:31 +0800 | [diff] [blame] | 296 | def __hash__(self): |
| 297 | return hash(tuple(self._descriptions)) |
| 298 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 299 | |
| 300 | class AccessDescription(object): |
| 301 | def __init__(self, access_method, access_location): |
Nick Bastin | d06763d | 2015-12-12 18:32:59 -0800 | [diff] [blame] | 302 | if not isinstance(access_method, ObjectIdentifier): |
Nick Bastin | bd079ae | 2015-12-13 05:15:44 -0800 | [diff] [blame] | 303 | raise TypeError("access_method must be an ObjectIdentifier") |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 304 | |
| 305 | if not isinstance(access_location, GeneralName): |
| 306 | raise TypeError("access_location must be a GeneralName") |
| 307 | |
| 308 | self._access_method = access_method |
| 309 | self._access_location = access_location |
| 310 | |
| 311 | def __repr__(self): |
| 312 | return ( |
| 313 | "<AccessDescription(access_method={0.access_method}, access_locati" |
| 314 | "on={0.access_location})>".format(self) |
| 315 | ) |
| 316 | |
| 317 | def __eq__(self, other): |
| 318 | if not isinstance(other, AccessDescription): |
| 319 | return NotImplemented |
| 320 | |
| 321 | return ( |
| 322 | self.access_method == other.access_method and |
| 323 | self.access_location == other.access_location |
| 324 | ) |
| 325 | |
| 326 | def __ne__(self, other): |
| 327 | return not self == other |
| 328 | |
Eeshan Garg | d8e0d85 | 2016-01-31 16:46:22 -0330 | [diff] [blame] | 329 | def __hash__(self): |
| 330 | return hash((self.access_method, self.access_location)) |
| 331 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 332 | access_method = utils.read_only_property("_access_method") |
| 333 | access_location = utils.read_only_property("_access_location") |
| 334 | |
| 335 | |
| 336 | @utils.register_interface(ExtensionType) |
| 337 | class BasicConstraints(object): |
| 338 | oid = ExtensionOID.BASIC_CONSTRAINTS |
| 339 | |
| 340 | def __init__(self, ca, path_length): |
| 341 | if not isinstance(ca, bool): |
| 342 | raise TypeError("ca must be a boolean value") |
| 343 | |
| 344 | if path_length is not None and not ca: |
| 345 | raise ValueError("path_length must be None when ca is False") |
| 346 | |
| 347 | if ( |
| 348 | path_length is not None and |
| 349 | (not isinstance(path_length, six.integer_types) or path_length < 0) |
| 350 | ): |
| 351 | raise TypeError( |
| 352 | "path_length must be a non-negative integer or None" |
| 353 | ) |
| 354 | |
| 355 | self._ca = ca |
| 356 | self._path_length = path_length |
| 357 | |
| 358 | ca = utils.read_only_property("_ca") |
| 359 | path_length = utils.read_only_property("_path_length") |
| 360 | |
| 361 | def __repr__(self): |
| 362 | return ("<BasicConstraints(ca={0.ca}, " |
| 363 | "path_length={0.path_length})>").format(self) |
| 364 | |
| 365 | def __eq__(self, other): |
| 366 | if not isinstance(other, BasicConstraints): |
| 367 | return NotImplemented |
| 368 | |
| 369 | return self.ca == other.ca and self.path_length == other.path_length |
| 370 | |
| 371 | def __ne__(self, other): |
| 372 | return not self == other |
| 373 | |
Paul Kehrer | 2eb69f6 | 2015-12-27 11:46:11 -0600 | [diff] [blame] | 374 | def __hash__(self): |
| 375 | return hash((self.ca, self.path_length)) |
| 376 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 377 | |
| 378 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 5e3cc98 | 2017-09-22 21:29:36 +0800 | [diff] [blame] | 379 | class DeltaCRLIndicator(object): |
| 380 | oid = ExtensionOID.DELTA_CRL_INDICATOR |
| 381 | |
| 382 | def __init__(self, crl_number): |
| 383 | if not isinstance(crl_number, six.integer_types): |
| 384 | raise TypeError("crl_number must be an integer") |
| 385 | |
| 386 | self._crl_number = crl_number |
| 387 | |
| 388 | crl_number = utils.read_only_property("_crl_number") |
| 389 | |
| 390 | def __eq__(self, other): |
| 391 | if not isinstance(other, DeltaCRLIndicator): |
| 392 | return NotImplemented |
| 393 | |
| 394 | return self.crl_number == other.crl_number |
| 395 | |
| 396 | def __ne__(self, other): |
| 397 | return not self == other |
| 398 | |
| 399 | def __hash__(self): |
| 400 | return hash(self.crl_number) |
| 401 | |
| 402 | def __repr__(self): |
| 403 | return "<DeltaCRLIndicator(crl_number={0.crl_number})>".format(self) |
| 404 | |
| 405 | |
| 406 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 407 | class CRLDistributionPoints(object): |
| 408 | oid = ExtensionOID.CRL_DISTRIBUTION_POINTS |
| 409 | |
| 410 | def __init__(self, distribution_points): |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 411 | distribution_points = list(distribution_points) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 412 | if not all( |
| 413 | isinstance(x, DistributionPoint) for x in distribution_points |
| 414 | ): |
| 415 | raise TypeError( |
| 416 | "distribution_points must be a list of DistributionPoint " |
| 417 | "objects" |
| 418 | ) |
| 419 | |
| 420 | self._distribution_points = distribution_points |
| 421 | |
| 422 | def __iter__(self): |
| 423 | return iter(self._distribution_points) |
| 424 | |
| 425 | def __len__(self): |
| 426 | return len(self._distribution_points) |
| 427 | |
| 428 | def __repr__(self): |
| 429 | return "<CRLDistributionPoints({0})>".format(self._distribution_points) |
| 430 | |
| 431 | def __eq__(self, other): |
| 432 | if not isinstance(other, CRLDistributionPoints): |
| 433 | return NotImplemented |
| 434 | |
| 435 | return self._distribution_points == other._distribution_points |
| 436 | |
| 437 | def __ne__(self, other): |
| 438 | return not self == other |
| 439 | |
Paul Kehrer | ee2e92d | 2015-12-27 17:29:37 -0600 | [diff] [blame] | 440 | def __getitem__(self, idx): |
| 441 | return self._distribution_points[idx] |
| 442 | |
Paul Kehrer | 409a0c8 | 2017-09-14 11:15:58 +0800 | [diff] [blame] | 443 | def __hash__(self): |
| 444 | return hash(tuple(self._distribution_points)) |
| 445 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 446 | |
Paul Kehrer | b76bcf8 | 2017-09-24 08:44:12 +0800 | [diff] [blame] | 447 | @utils.register_interface(ExtensionType) |
| 448 | class FreshestCRL(object): |
| 449 | oid = ExtensionOID.FRESHEST_CRL |
| 450 | |
| 451 | def __init__(self, distribution_points): |
| 452 | distribution_points = list(distribution_points) |
| 453 | if not all( |
| 454 | isinstance(x, DistributionPoint) for x in distribution_points |
| 455 | ): |
| 456 | raise TypeError( |
| 457 | "distribution_points must be a list of DistributionPoint " |
| 458 | "objects" |
| 459 | ) |
| 460 | |
| 461 | self._distribution_points = distribution_points |
| 462 | |
| 463 | def __iter__(self): |
| 464 | return iter(self._distribution_points) |
| 465 | |
| 466 | def __len__(self): |
| 467 | return len(self._distribution_points) |
| 468 | |
| 469 | def __repr__(self): |
| 470 | return "<FreshestCRL({0})>".format(self._distribution_points) |
| 471 | |
| 472 | def __eq__(self, other): |
| 473 | if not isinstance(other, FreshestCRL): |
| 474 | return NotImplemented |
| 475 | |
| 476 | return self._distribution_points == other._distribution_points |
| 477 | |
| 478 | def __ne__(self, other): |
| 479 | return not self == other |
| 480 | |
| 481 | def __getitem__(self, idx): |
| 482 | return self._distribution_points[idx] |
| 483 | |
| 484 | def __hash__(self): |
| 485 | return hash(tuple(self._distribution_points)) |
| 486 | |
| 487 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 488 | class DistributionPoint(object): |
| 489 | def __init__(self, full_name, relative_name, reasons, crl_issuer): |
| 490 | if full_name and relative_name: |
| 491 | raise ValueError( |
| 492 | "You cannot provide both full_name and relative_name, at " |
| 493 | "least one must be None." |
| 494 | ) |
| 495 | |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 496 | if full_name: |
| 497 | full_name = list(full_name) |
| 498 | if not all(isinstance(x, GeneralName) for x in full_name): |
| 499 | raise TypeError( |
| 500 | "full_name must be a list of GeneralName objects" |
| 501 | ) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 502 | |
Fraser Tweedale | 02467dd | 2016-11-07 15:54:04 +1000 | [diff] [blame] | 503 | if relative_name: |
Alex Gaynor | a783c57 | 2017-03-21 09:24:12 -0400 | [diff] [blame] | 504 | if not isinstance(relative_name, RelativeDistinguishedName): |
Fraser Tweedale | 02467dd | 2016-11-07 15:54:04 +1000 | [diff] [blame] | 505 | raise TypeError( |
| 506 | "relative_name must be a RelativeDistinguishedName" |
| 507 | ) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 508 | |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 509 | if crl_issuer: |
| 510 | crl_issuer = list(crl_issuer) |
| 511 | if not all(isinstance(x, GeneralName) for x in crl_issuer): |
| 512 | raise TypeError( |
| 513 | "crl_issuer must be None or a list of general names" |
| 514 | ) |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 515 | |
| 516 | if reasons and (not isinstance(reasons, frozenset) or not all( |
| 517 | isinstance(x, ReasonFlags) for x in reasons |
| 518 | )): |
| 519 | raise TypeError("reasons must be None or frozenset of ReasonFlags") |
| 520 | |
| 521 | if reasons and ( |
| 522 | ReasonFlags.unspecified in reasons or |
| 523 | ReasonFlags.remove_from_crl in reasons |
| 524 | ): |
| 525 | raise ValueError( |
| 526 | "unspecified and remove_from_crl are not valid reasons in a " |
| 527 | "DistributionPoint" |
| 528 | ) |
| 529 | |
| 530 | if reasons and not crl_issuer and not (full_name or relative_name): |
| 531 | raise ValueError( |
| 532 | "You must supply crl_issuer, full_name, or relative_name when " |
| 533 | "reasons is not None" |
| 534 | ) |
| 535 | |
| 536 | self._full_name = full_name |
| 537 | self._relative_name = relative_name |
| 538 | self._reasons = reasons |
| 539 | self._crl_issuer = crl_issuer |
| 540 | |
| 541 | def __repr__(self): |
| 542 | return ( |
| 543 | "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela" |
| 544 | "tive_name}, reasons={0.reasons}, crl_issuer={0.crl_is" |
| 545 | "suer})>".format(self) |
| 546 | ) |
| 547 | |
| 548 | def __eq__(self, other): |
| 549 | if not isinstance(other, DistributionPoint): |
| 550 | return NotImplemented |
| 551 | |
| 552 | return ( |
| 553 | self.full_name == other.full_name and |
| 554 | self.relative_name == other.relative_name and |
| 555 | self.reasons == other.reasons and |
| 556 | self.crl_issuer == other.crl_issuer |
| 557 | ) |
| 558 | |
| 559 | def __ne__(self, other): |
| 560 | return not self == other |
| 561 | |
Paul Kehrer | 409a0c8 | 2017-09-14 11:15:58 +0800 | [diff] [blame] | 562 | def __hash__(self): |
| 563 | if self.full_name is not None: |
| 564 | fn = tuple(self.full_name) |
| 565 | else: |
| 566 | fn = None |
| 567 | |
| 568 | if self.crl_issuer is not None: |
| 569 | crl_issuer = tuple(self.crl_issuer) |
| 570 | else: |
| 571 | crl_issuer = None |
| 572 | |
| 573 | return hash((fn, self.relative_name, self.reasons, crl_issuer)) |
| 574 | |
Paul Kehrer | 9f8069a | 2015-08-10 21:10:34 -0500 | [diff] [blame] | 575 | full_name = utils.read_only_property("_full_name") |
| 576 | relative_name = utils.read_only_property("_relative_name") |
| 577 | reasons = utils.read_only_property("_reasons") |
| 578 | crl_issuer = utils.read_only_property("_crl_issuer") |
| 579 | |
| 580 | |
| 581 | class ReasonFlags(Enum): |
| 582 | unspecified = "unspecified" |
| 583 | key_compromise = "keyCompromise" |
| 584 | ca_compromise = "cACompromise" |
| 585 | affiliation_changed = "affiliationChanged" |
| 586 | superseded = "superseded" |
| 587 | cessation_of_operation = "cessationOfOperation" |
| 588 | certificate_hold = "certificateHold" |
| 589 | privilege_withdrawn = "privilegeWithdrawn" |
| 590 | aa_compromise = "aACompromise" |
| 591 | remove_from_crl = "removeFromCRL" |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 592 | |
| 593 | |
| 594 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 7e8fe9d | 2015-05-18 09:53:47 -0700 | [diff] [blame] | 595 | class PolicyConstraints(object): |
Paul Kehrer | 159b3b5 | 2016-02-26 08:27:22 -0600 | [diff] [blame] | 596 | oid = ExtensionOID.POLICY_CONSTRAINTS |
| 597 | |
Paul Kehrer | 7e8fe9d | 2015-05-18 09:53:47 -0700 | [diff] [blame] | 598 | def __init__(self, require_explicit_policy, inhibit_policy_mapping): |
| 599 | if require_explicit_policy is not None and not isinstance( |
| 600 | require_explicit_policy, six.integer_types |
| 601 | ): |
| 602 | raise TypeError( |
| 603 | "require_explicit_policy must be a non-negative integer or " |
| 604 | "None" |
| 605 | ) |
| 606 | |
| 607 | if inhibit_policy_mapping is not None and not isinstance( |
| 608 | inhibit_policy_mapping, six.integer_types |
| 609 | ): |
| 610 | raise TypeError( |
| 611 | "inhibit_policy_mapping must be a non-negative integer or None" |
| 612 | ) |
| 613 | |
| 614 | if inhibit_policy_mapping is None and require_explicit_policy is None: |
| 615 | raise ValueError( |
| 616 | "At least one of require_explicit_policy and " |
| 617 | "inhibit_policy_mapping must not be None" |
| 618 | ) |
| 619 | |
| 620 | self._require_explicit_policy = require_explicit_policy |
| 621 | self._inhibit_policy_mapping = inhibit_policy_mapping |
| 622 | |
| 623 | def __repr__(self): |
| 624 | return ( |
| 625 | u"<PolicyConstraints(require_explicit_policy={0.require_explicit" |
| 626 | u"_policy}, inhibit_policy_mapping={0.inhibit_policy_" |
| 627 | u"mapping})>".format(self) |
| 628 | ) |
| 629 | |
| 630 | def __eq__(self, other): |
| 631 | if not isinstance(other, PolicyConstraints): |
| 632 | return NotImplemented |
| 633 | |
| 634 | return ( |
| 635 | self.require_explicit_policy == other.require_explicit_policy and |
| 636 | self.inhibit_policy_mapping == other.inhibit_policy_mapping |
| 637 | ) |
| 638 | |
| 639 | def __ne__(self, other): |
| 640 | return not self == other |
| 641 | |
Paul Kehrer | 83bb406 | 2017-09-14 11:14:28 +0800 | [diff] [blame] | 642 | def __hash__(self): |
| 643 | return hash( |
| 644 | (self.require_explicit_policy, self.inhibit_policy_mapping) |
| 645 | ) |
| 646 | |
Paul Kehrer | 7e8fe9d | 2015-05-18 09:53:47 -0700 | [diff] [blame] | 647 | require_explicit_policy = utils.read_only_property( |
| 648 | "_require_explicit_policy" |
| 649 | ) |
| 650 | inhibit_policy_mapping = utils.read_only_property( |
| 651 | "_inhibit_policy_mapping" |
| 652 | ) |
| 653 | |
| 654 | |
| 655 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 656 | class CertificatePolicies(object): |
| 657 | oid = ExtensionOID.CERTIFICATE_POLICIES |
| 658 | |
| 659 | def __init__(self, policies): |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 660 | policies = list(policies) |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 661 | if not all(isinstance(x, PolicyInformation) for x in policies): |
| 662 | raise TypeError( |
| 663 | "Every item in the policies list must be a " |
| 664 | "PolicyInformation" |
| 665 | ) |
| 666 | |
| 667 | self._policies = policies |
| 668 | |
| 669 | def __iter__(self): |
| 670 | return iter(self._policies) |
| 671 | |
| 672 | def __len__(self): |
| 673 | return len(self._policies) |
| 674 | |
| 675 | def __repr__(self): |
| 676 | return "<CertificatePolicies({0})>".format(self._policies) |
| 677 | |
| 678 | def __eq__(self, other): |
| 679 | if not isinstance(other, CertificatePolicies): |
| 680 | return NotImplemented |
| 681 | |
| 682 | return self._policies == other._policies |
| 683 | |
| 684 | def __ne__(self, other): |
| 685 | return not self == other |
| 686 | |
Paul Kehrer | e8db7bd | 2015-12-27 17:32:57 -0600 | [diff] [blame] | 687 | def __getitem__(self, idx): |
| 688 | return self._policies[idx] |
| 689 | |
Paul Kehrer | ab96a53 | 2017-09-14 07:42:33 +0800 | [diff] [blame] | 690 | def __hash__(self): |
| 691 | return hash(tuple(self._policies)) |
| 692 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 693 | |
| 694 | class PolicyInformation(object): |
| 695 | def __init__(self, policy_identifier, policy_qualifiers): |
| 696 | if not isinstance(policy_identifier, ObjectIdentifier): |
| 697 | raise TypeError("policy_identifier must be an ObjectIdentifier") |
| 698 | |
| 699 | self._policy_identifier = policy_identifier |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 700 | |
| 701 | if policy_qualifiers: |
| 702 | policy_qualifiers = list(policy_qualifiers) |
| 703 | if not all( |
| 704 | isinstance(x, (six.text_type, UserNotice)) |
| 705 | for x in policy_qualifiers |
| 706 | ): |
| 707 | raise TypeError( |
| 708 | "policy_qualifiers must be a list of strings and/or " |
| 709 | "UserNotice objects or None" |
| 710 | ) |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 711 | |
| 712 | self._policy_qualifiers = policy_qualifiers |
| 713 | |
| 714 | def __repr__(self): |
| 715 | return ( |
| 716 | "<PolicyInformation(policy_identifier={0.policy_identifier}, polic" |
| 717 | "y_qualifiers={0.policy_qualifiers})>".format(self) |
| 718 | ) |
| 719 | |
| 720 | def __eq__(self, other): |
| 721 | if not isinstance(other, PolicyInformation): |
| 722 | return NotImplemented |
| 723 | |
| 724 | return ( |
| 725 | self.policy_identifier == other.policy_identifier and |
| 726 | self.policy_qualifiers == other.policy_qualifiers |
| 727 | ) |
| 728 | |
| 729 | def __ne__(self, other): |
| 730 | return not self == other |
| 731 | |
Paul Kehrer | ab96a53 | 2017-09-14 07:42:33 +0800 | [diff] [blame] | 732 | def __hash__(self): |
| 733 | if self.policy_qualifiers is not None: |
| 734 | pq = tuple(self.policy_qualifiers) |
| 735 | else: |
| 736 | pq = None |
| 737 | |
| 738 | return hash((self.policy_identifier, pq)) |
| 739 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 740 | policy_identifier = utils.read_only_property("_policy_identifier") |
| 741 | policy_qualifiers = utils.read_only_property("_policy_qualifiers") |
| 742 | |
| 743 | |
| 744 | class UserNotice(object): |
| 745 | def __init__(self, notice_reference, explicit_text): |
| 746 | if notice_reference and not isinstance( |
| 747 | notice_reference, NoticeReference |
| 748 | ): |
| 749 | raise TypeError( |
| 750 | "notice_reference must be None or a NoticeReference" |
| 751 | ) |
| 752 | |
| 753 | self._notice_reference = notice_reference |
| 754 | self._explicit_text = explicit_text |
| 755 | |
| 756 | def __repr__(self): |
| 757 | return ( |
| 758 | "<UserNotice(notice_reference={0.notice_reference}, explicit_text=" |
| 759 | "{0.explicit_text!r})>".format(self) |
| 760 | ) |
| 761 | |
| 762 | def __eq__(self, other): |
| 763 | if not isinstance(other, UserNotice): |
| 764 | return NotImplemented |
| 765 | |
| 766 | return ( |
| 767 | self.notice_reference == other.notice_reference and |
| 768 | self.explicit_text == other.explicit_text |
| 769 | ) |
| 770 | |
| 771 | def __ne__(self, other): |
| 772 | return not self == other |
| 773 | |
Paul Kehrer | ab96a53 | 2017-09-14 07:42:33 +0800 | [diff] [blame] | 774 | def __hash__(self): |
| 775 | return hash((self.notice_reference, self.explicit_text)) |
| 776 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 777 | notice_reference = utils.read_only_property("_notice_reference") |
| 778 | explicit_text = utils.read_only_property("_explicit_text") |
| 779 | |
| 780 | |
| 781 | class NoticeReference(object): |
| 782 | def __init__(self, organization, notice_numbers): |
| 783 | self._organization = organization |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 784 | notice_numbers = list(notice_numbers) |
| 785 | if not all(isinstance(x, int) for x in notice_numbers): |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 786 | raise TypeError( |
| 787 | "notice_numbers must be a list of integers" |
| 788 | ) |
| 789 | |
| 790 | self._notice_numbers = notice_numbers |
| 791 | |
| 792 | def __repr__(self): |
| 793 | return ( |
| 794 | "<NoticeReference(organization={0.organization!r}, notice_numbers=" |
| 795 | "{0.notice_numbers})>".format(self) |
| 796 | ) |
| 797 | |
| 798 | def __eq__(self, other): |
| 799 | if not isinstance(other, NoticeReference): |
| 800 | return NotImplemented |
| 801 | |
| 802 | return ( |
| 803 | self.organization == other.organization and |
| 804 | self.notice_numbers == other.notice_numbers |
| 805 | ) |
| 806 | |
| 807 | def __ne__(self, other): |
| 808 | return not self == other |
| 809 | |
Paul Kehrer | ab96a53 | 2017-09-14 07:42:33 +0800 | [diff] [blame] | 810 | def __hash__(self): |
| 811 | return hash((self.organization, tuple(self.notice_numbers))) |
| 812 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 813 | organization = utils.read_only_property("_organization") |
| 814 | notice_numbers = utils.read_only_property("_notice_numbers") |
| 815 | |
| 816 | |
| 817 | @utils.register_interface(ExtensionType) |
| 818 | class ExtendedKeyUsage(object): |
| 819 | oid = ExtensionOID.EXTENDED_KEY_USAGE |
| 820 | |
| 821 | def __init__(self, usages): |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 822 | usages = list(usages) |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 823 | if not all(isinstance(x, ObjectIdentifier) for x in usages): |
| 824 | raise TypeError( |
| 825 | "Every item in the usages list must be an ObjectIdentifier" |
| 826 | ) |
| 827 | |
| 828 | self._usages = usages |
| 829 | |
| 830 | def __iter__(self): |
| 831 | return iter(self._usages) |
| 832 | |
| 833 | def __len__(self): |
| 834 | return len(self._usages) |
| 835 | |
| 836 | def __repr__(self): |
| 837 | return "<ExtendedKeyUsage({0})>".format(self._usages) |
| 838 | |
| 839 | def __eq__(self, other): |
| 840 | if not isinstance(other, ExtendedKeyUsage): |
| 841 | return NotImplemented |
| 842 | |
| 843 | return self._usages == other._usages |
| 844 | |
| 845 | def __ne__(self, other): |
| 846 | return not self == other |
| 847 | |
Paul Kehrer | 7b6be92 | 2017-09-14 07:43:07 +0800 | [diff] [blame] | 848 | def __hash__(self): |
| 849 | return hash(tuple(self._usages)) |
| 850 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 851 | |
| 852 | @utils.register_interface(ExtensionType) |
| 853 | class OCSPNoCheck(object): |
| 854 | oid = ExtensionOID.OCSP_NO_CHECK |
| 855 | |
| 856 | |
| 857 | @utils.register_interface(ExtensionType) |
Alex Gaynor | dd6b78b | 2018-08-31 18:25:52 -0500 | [diff] [blame] | 858 | class PrecertPoison(object): |
| 859 | oid = ExtensionOID.PRECERT_POISON |
| 860 | |
| 861 | |
| 862 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 5d66966 | 2017-09-11 09:16:34 +0800 | [diff] [blame] | 863 | class TLSFeature(object): |
| 864 | oid = ExtensionOID.TLS_FEATURE |
| 865 | |
| 866 | def __init__(self, features): |
| 867 | features = list(features) |
| 868 | if ( |
| 869 | not all(isinstance(x, TLSFeatureType) for x in features) or |
| 870 | len(features) == 0 |
| 871 | ): |
| 872 | raise TypeError( |
| 873 | "features must be a list of elements from the TLSFeatureType " |
| 874 | "enum" |
| 875 | ) |
| 876 | |
| 877 | self._features = features |
| 878 | |
| 879 | def __iter__(self): |
| 880 | return iter(self._features) |
| 881 | |
| 882 | def __len__(self): |
| 883 | return len(self._features) |
| 884 | |
| 885 | def __repr__(self): |
| 886 | return "<TLSFeature(features={0._features})>".format(self) |
| 887 | |
| 888 | def __eq__(self, other): |
| 889 | if not isinstance(other, TLSFeature): |
| 890 | return NotImplemented |
| 891 | |
| 892 | return self._features == other._features |
| 893 | |
| 894 | def __getitem__(self, idx): |
| 895 | return self._features[idx] |
| 896 | |
| 897 | def __ne__(self, other): |
| 898 | return not self == other |
| 899 | |
| 900 | def __hash__(self): |
| 901 | return hash(tuple(self._features)) |
| 902 | |
| 903 | |
| 904 | class TLSFeatureType(Enum): |
| 905 | # status_request is defined in RFC 6066 and is used for what is commonly |
| 906 | # called OCSP Must-Staple when present in the TLS Feature extension in an |
| 907 | # X.509 certificate. |
| 908 | status_request = 5 |
| 909 | # status_request_v2 is defined in RFC 6961 and allows multiple OCSP |
| 910 | # responses to be provided. It is not currently in use by clients or |
| 911 | # servers. |
| 912 | status_request_v2 = 17 |
| 913 | |
| 914 | |
| 915 | _TLS_FEATURE_TYPE_TO_ENUM = dict((x.value, x) for x in TLSFeatureType) |
| 916 | |
| 917 | |
| 918 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 919 | class InhibitAnyPolicy(object): |
| 920 | oid = ExtensionOID.INHIBIT_ANY_POLICY |
| 921 | |
| 922 | def __init__(self, skip_certs): |
| 923 | if not isinstance(skip_certs, six.integer_types): |
| 924 | raise TypeError("skip_certs must be an integer") |
| 925 | |
| 926 | if skip_certs < 0: |
| 927 | raise ValueError("skip_certs must be a non-negative integer") |
| 928 | |
| 929 | self._skip_certs = skip_certs |
| 930 | |
| 931 | def __repr__(self): |
| 932 | return "<InhibitAnyPolicy(skip_certs={0.skip_certs})>".format(self) |
| 933 | |
| 934 | def __eq__(self, other): |
| 935 | if not isinstance(other, InhibitAnyPolicy): |
| 936 | return NotImplemented |
| 937 | |
| 938 | return self.skip_certs == other.skip_certs |
| 939 | |
| 940 | def __ne__(self, other): |
| 941 | return not self == other |
| 942 | |
Eeshan Garg | 0a0293e | 2016-02-01 12:56:40 -0330 | [diff] [blame] | 943 | def __hash__(self): |
| 944 | return hash(self.skip_certs) |
| 945 | |
Paul Kehrer | 012262c | 2015-08-10 23:42:57 -0500 | [diff] [blame] | 946 | skip_certs = utils.read_only_property("_skip_certs") |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 947 | |
| 948 | |
| 949 | @utils.register_interface(ExtensionType) |
| 950 | class KeyUsage(object): |
| 951 | oid = ExtensionOID.KEY_USAGE |
| 952 | |
| 953 | def __init__(self, digital_signature, content_commitment, key_encipherment, |
| 954 | data_encipherment, key_agreement, key_cert_sign, crl_sign, |
| 955 | encipher_only, decipher_only): |
| 956 | if not key_agreement and (encipher_only or decipher_only): |
| 957 | raise ValueError( |
| 958 | "encipher_only and decipher_only can only be true when " |
| 959 | "key_agreement is true" |
| 960 | ) |
| 961 | |
| 962 | self._digital_signature = digital_signature |
| 963 | self._content_commitment = content_commitment |
| 964 | self._key_encipherment = key_encipherment |
| 965 | self._data_encipherment = data_encipherment |
| 966 | self._key_agreement = key_agreement |
| 967 | self._key_cert_sign = key_cert_sign |
| 968 | self._crl_sign = crl_sign |
| 969 | self._encipher_only = encipher_only |
| 970 | self._decipher_only = decipher_only |
| 971 | |
| 972 | digital_signature = utils.read_only_property("_digital_signature") |
| 973 | content_commitment = utils.read_only_property("_content_commitment") |
| 974 | key_encipherment = utils.read_only_property("_key_encipherment") |
| 975 | data_encipherment = utils.read_only_property("_data_encipherment") |
| 976 | key_agreement = utils.read_only_property("_key_agreement") |
| 977 | key_cert_sign = utils.read_only_property("_key_cert_sign") |
| 978 | crl_sign = utils.read_only_property("_crl_sign") |
| 979 | |
| 980 | @property |
| 981 | def encipher_only(self): |
| 982 | if not self.key_agreement: |
| 983 | raise ValueError( |
| 984 | "encipher_only is undefined unless key_agreement is true" |
| 985 | ) |
| 986 | else: |
| 987 | return self._encipher_only |
| 988 | |
| 989 | @property |
| 990 | def decipher_only(self): |
| 991 | if not self.key_agreement: |
| 992 | raise ValueError( |
| 993 | "decipher_only is undefined unless key_agreement is true" |
| 994 | ) |
| 995 | else: |
| 996 | return self._decipher_only |
| 997 | |
| 998 | def __repr__(self): |
| 999 | try: |
| 1000 | encipher_only = self.encipher_only |
| 1001 | decipher_only = self.decipher_only |
| 1002 | except ValueError: |
| 1003 | encipher_only = None |
| 1004 | decipher_only = None |
| 1005 | |
| 1006 | return ("<KeyUsage(digital_signature={0.digital_signature}, " |
| 1007 | "content_commitment={0.content_commitment}, " |
| 1008 | "key_encipherment={0.key_encipherment}, " |
| 1009 | "data_encipherment={0.data_encipherment}, " |
| 1010 | "key_agreement={0.key_agreement}, " |
| 1011 | "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, " |
| 1012 | "encipher_only={1}, decipher_only={2})>").format( |
| 1013 | self, encipher_only, decipher_only) |
| 1014 | |
| 1015 | def __eq__(self, other): |
| 1016 | if not isinstance(other, KeyUsage): |
| 1017 | return NotImplemented |
| 1018 | |
| 1019 | return ( |
| 1020 | self.digital_signature == other.digital_signature and |
| 1021 | self.content_commitment == other.content_commitment and |
| 1022 | self.key_encipherment == other.key_encipherment and |
| 1023 | self.data_encipherment == other.data_encipherment and |
| 1024 | self.key_agreement == other.key_agreement and |
| 1025 | self.key_cert_sign == other.key_cert_sign and |
| 1026 | self.crl_sign == other.crl_sign and |
| 1027 | self._encipher_only == other._encipher_only and |
| 1028 | self._decipher_only == other._decipher_only |
| 1029 | ) |
| 1030 | |
| 1031 | def __ne__(self, other): |
| 1032 | return not self == other |
| 1033 | |
Paul Kehrer | 7b6be92 | 2017-09-14 07:43:07 +0800 | [diff] [blame] | 1034 | def __hash__(self): |
| 1035 | return hash(( |
| 1036 | self.digital_signature, self.content_commitment, |
| 1037 | self.key_encipherment, self.data_encipherment, |
| 1038 | self.key_agreement, self.key_cert_sign, |
| 1039 | self.crl_sign, self._encipher_only, |
| 1040 | self._decipher_only |
| 1041 | )) |
| 1042 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 1043 | |
| 1044 | @utils.register_interface(ExtensionType) |
| 1045 | class NameConstraints(object): |
| 1046 | oid = ExtensionOID.NAME_CONSTRAINTS |
| 1047 | |
| 1048 | def __init__(self, permitted_subtrees, excluded_subtrees): |
| 1049 | if permitted_subtrees is not None: |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 1050 | permitted_subtrees = list(permitted_subtrees) |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 1051 | if not all( |
| 1052 | isinstance(x, GeneralName) for x in permitted_subtrees |
| 1053 | ): |
| 1054 | raise TypeError( |
| 1055 | "permitted_subtrees must be a list of GeneralName objects " |
| 1056 | "or None" |
| 1057 | ) |
| 1058 | |
| 1059 | self._validate_ip_name(permitted_subtrees) |
| 1060 | |
| 1061 | if excluded_subtrees is not None: |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 1062 | excluded_subtrees = list(excluded_subtrees) |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 1063 | if not all( |
| 1064 | isinstance(x, GeneralName) for x in excluded_subtrees |
| 1065 | ): |
| 1066 | raise TypeError( |
| 1067 | "excluded_subtrees must be a list of GeneralName objects " |
| 1068 | "or None" |
| 1069 | ) |
| 1070 | |
| 1071 | self._validate_ip_name(excluded_subtrees) |
| 1072 | |
| 1073 | if permitted_subtrees is None and excluded_subtrees is None: |
| 1074 | raise ValueError( |
| 1075 | "At least one of permitted_subtrees and excluded_subtrees " |
| 1076 | "must not be None" |
| 1077 | ) |
| 1078 | |
| 1079 | self._permitted_subtrees = permitted_subtrees |
| 1080 | self._excluded_subtrees = excluded_subtrees |
| 1081 | |
| 1082 | def __eq__(self, other): |
| 1083 | if not isinstance(other, NameConstraints): |
| 1084 | return NotImplemented |
| 1085 | |
| 1086 | return ( |
| 1087 | self.excluded_subtrees == other.excluded_subtrees and |
| 1088 | self.permitted_subtrees == other.permitted_subtrees |
| 1089 | ) |
| 1090 | |
| 1091 | def __ne__(self, other): |
| 1092 | return not self == other |
| 1093 | |
| 1094 | def _validate_ip_name(self, tree): |
| 1095 | if any(isinstance(name, IPAddress) and not isinstance( |
| 1096 | name.value, (ipaddress.IPv4Network, ipaddress.IPv6Network) |
| 1097 | ) for name in tree): |
| 1098 | raise TypeError( |
| 1099 | "IPAddress name constraints must be an IPv4Network or" |
| 1100 | " IPv6Network object" |
| 1101 | ) |
| 1102 | |
| 1103 | def __repr__(self): |
| 1104 | return ( |
| 1105 | u"<NameConstraints(permitted_subtrees={0.permitted_subtrees}, " |
| 1106 | u"excluded_subtrees={0.excluded_subtrees})>".format(self) |
| 1107 | ) |
| 1108 | |
Paul Kehrer | bdad051 | 2017-09-14 04:24:20 +0800 | [diff] [blame] | 1109 | def __hash__(self): |
| 1110 | if self.permitted_subtrees is not None: |
| 1111 | ps = tuple(self.permitted_subtrees) |
| 1112 | else: |
| 1113 | ps = None |
| 1114 | |
| 1115 | if self.excluded_subtrees is not None: |
| 1116 | es = tuple(self.excluded_subtrees) |
| 1117 | else: |
| 1118 | es = None |
| 1119 | |
| 1120 | return hash((ps, es)) |
| 1121 | |
Paul Kehrer | fbeaf2a | 2015-08-10 23:52:10 -0500 | [diff] [blame] | 1122 | permitted_subtrees = utils.read_only_property("_permitted_subtrees") |
| 1123 | excluded_subtrees = utils.read_only_property("_excluded_subtrees") |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1124 | |
| 1125 | |
| 1126 | class Extension(object): |
| 1127 | def __init__(self, oid, critical, value): |
| 1128 | if not isinstance(oid, ObjectIdentifier): |
| 1129 | raise TypeError( |
| 1130 | "oid argument must be an ObjectIdentifier instance." |
| 1131 | ) |
| 1132 | |
| 1133 | if not isinstance(critical, bool): |
| 1134 | raise TypeError("critical must be a boolean value") |
| 1135 | |
| 1136 | self._oid = oid |
| 1137 | self._critical = critical |
| 1138 | self._value = value |
| 1139 | |
| 1140 | oid = utils.read_only_property("_oid") |
| 1141 | critical = utils.read_only_property("_critical") |
| 1142 | value = utils.read_only_property("_value") |
| 1143 | |
| 1144 | def __repr__(self): |
| 1145 | return ("<Extension(oid={0.oid}, critical={0.critical}, " |
| 1146 | "value={0.value})>").format(self) |
| 1147 | |
| 1148 | def __eq__(self, other): |
| 1149 | if not isinstance(other, Extension): |
| 1150 | return NotImplemented |
| 1151 | |
| 1152 | return ( |
| 1153 | self.oid == other.oid and |
| 1154 | self.critical == other.critical and |
| 1155 | self.value == other.value |
| 1156 | ) |
| 1157 | |
| 1158 | def __ne__(self, other): |
| 1159 | return not self == other |
| 1160 | |
Paul Kehrer | 83bb406 | 2017-09-14 11:14:28 +0800 | [diff] [blame] | 1161 | def __hash__(self): |
| 1162 | return hash((self.oid, self.critical, self.value)) |
| 1163 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1164 | |
| 1165 | class GeneralNames(object): |
| 1166 | def __init__(self, general_names): |
Marti | 40f1999 | 2016-08-26 04:26:31 +0300 | [diff] [blame] | 1167 | general_names = list(general_names) |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1168 | if not all(isinstance(x, GeneralName) for x in general_names): |
| 1169 | raise TypeError( |
| 1170 | "Every item in the general_names list must be an " |
| 1171 | "object conforming to the GeneralName interface" |
| 1172 | ) |
| 1173 | |
| 1174 | self._general_names = general_names |
| 1175 | |
| 1176 | def __iter__(self): |
| 1177 | return iter(self._general_names) |
| 1178 | |
| 1179 | def __len__(self): |
| 1180 | return len(self._general_names) |
| 1181 | |
| 1182 | def get_values_for_type(self, type): |
| 1183 | # Return the value of each GeneralName, except for OtherName instances |
| 1184 | # which we return directly because it has two important properties not |
| 1185 | # just one value. |
| 1186 | objs = (i for i in self if isinstance(i, type)) |
| 1187 | if type != OtherName: |
| 1188 | objs = (i.value for i in objs) |
| 1189 | return list(objs) |
| 1190 | |
| 1191 | def __repr__(self): |
| 1192 | return "<GeneralNames({0})>".format(self._general_names) |
| 1193 | |
| 1194 | def __eq__(self, other): |
| 1195 | if not isinstance(other, GeneralNames): |
| 1196 | return NotImplemented |
| 1197 | |
| 1198 | return self._general_names == other._general_names |
| 1199 | |
| 1200 | def __ne__(self, other): |
| 1201 | return not self == other |
| 1202 | |
Paul Kehrer | 8adb596 | 2015-12-26 14:46:58 -0600 | [diff] [blame] | 1203 | def __getitem__(self, idx): |
| 1204 | return self._general_names[idx] |
| 1205 | |
Paul Kehrer | 5e9eeef | 2017-09-14 11:15:21 +0800 | [diff] [blame] | 1206 | def __hash__(self): |
| 1207 | return hash(tuple(self._general_names)) |
| 1208 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1209 | |
| 1210 | @utils.register_interface(ExtensionType) |
| 1211 | class SubjectAlternativeName(object): |
| 1212 | oid = ExtensionOID.SUBJECT_ALTERNATIVE_NAME |
| 1213 | |
| 1214 | def __init__(self, general_names): |
| 1215 | self._general_names = GeneralNames(general_names) |
| 1216 | |
| 1217 | def __iter__(self): |
| 1218 | return iter(self._general_names) |
| 1219 | |
| 1220 | def __len__(self): |
| 1221 | return len(self._general_names) |
| 1222 | |
| 1223 | def get_values_for_type(self, type): |
| 1224 | return self._general_names.get_values_for_type(type) |
| 1225 | |
| 1226 | def __repr__(self): |
| 1227 | return "<SubjectAlternativeName({0})>".format(self._general_names) |
| 1228 | |
| 1229 | def __eq__(self, other): |
| 1230 | if not isinstance(other, SubjectAlternativeName): |
| 1231 | return NotImplemented |
| 1232 | |
| 1233 | return self._general_names == other._general_names |
| 1234 | |
Paul Kehrer | 8adb596 | 2015-12-26 14:46:58 -0600 | [diff] [blame] | 1235 | def __getitem__(self, idx): |
| 1236 | return self._general_names[idx] |
| 1237 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1238 | def __ne__(self, other): |
| 1239 | return not self == other |
| 1240 | |
Paul Kehrer | 5e9eeef | 2017-09-14 11:15:21 +0800 | [diff] [blame] | 1241 | def __hash__(self): |
| 1242 | return hash(self._general_names) |
| 1243 | |
Paul Kehrer | aa7a322 | 2015-08-11 00:00:54 -0500 | [diff] [blame] | 1244 | |
| 1245 | @utils.register_interface(ExtensionType) |
| 1246 | class IssuerAlternativeName(object): |
| 1247 | oid = ExtensionOID.ISSUER_ALTERNATIVE_NAME |
| 1248 | |
| 1249 | def __init__(self, general_names): |
| 1250 | self._general_names = GeneralNames(general_names) |
| 1251 | |
| 1252 | def __iter__(self): |
| 1253 | return iter(self._general_names) |
| 1254 | |
| 1255 | def __len__(self): |
| 1256 | return len(self._general_names) |
| 1257 | |
| 1258 | def get_values_for_type(self, type): |
| 1259 | return self._general_names.get_values_for_type(type) |
| 1260 | |
| 1261 | def __repr__(self): |
| 1262 | return "<IssuerAlternativeName({0})>".format(self._general_names) |
| 1263 | |
| 1264 | def __eq__(self, other): |
| 1265 | if not isinstance(other, IssuerAlternativeName): |
| 1266 | return NotImplemented |
| 1267 | |
| 1268 | return self._general_names == other._general_names |
| 1269 | |
| 1270 | def __ne__(self, other): |
| 1271 | return not self == other |
Paul Kehrer | 49bb756 | 2015-12-25 16:17:40 -0600 | [diff] [blame] | 1272 | |
Paul Kehrer | 5c999d3 | 2015-12-26 17:45:20 -0600 | [diff] [blame] | 1273 | def __getitem__(self, idx): |
| 1274 | return self._general_names[idx] |
| 1275 | |
Paul Kehrer | 5e9eeef | 2017-09-14 11:15:21 +0800 | [diff] [blame] | 1276 | def __hash__(self): |
| 1277 | return hash(self._general_names) |
| 1278 | |
Paul Kehrer | 49bb756 | 2015-12-25 16:17:40 -0600 | [diff] [blame] | 1279 | |
| 1280 | @utils.register_interface(ExtensionType) |
| 1281 | class CertificateIssuer(object): |
| 1282 | oid = CRLEntryExtensionOID.CERTIFICATE_ISSUER |
| 1283 | |
| 1284 | def __init__(self, general_names): |
| 1285 | self._general_names = GeneralNames(general_names) |
| 1286 | |
| 1287 | def __iter__(self): |
| 1288 | return iter(self._general_names) |
| 1289 | |
| 1290 | def __len__(self): |
| 1291 | return len(self._general_names) |
| 1292 | |
| 1293 | def get_values_for_type(self, type): |
| 1294 | return self._general_names.get_values_for_type(type) |
| 1295 | |
| 1296 | def __repr__(self): |
| 1297 | return "<CertificateIssuer({0})>".format(self._general_names) |
| 1298 | |
| 1299 | def __eq__(self, other): |
| 1300 | if not isinstance(other, CertificateIssuer): |
| 1301 | return NotImplemented |
| 1302 | |
| 1303 | return self._general_names == other._general_names |
| 1304 | |
| 1305 | def __ne__(self, other): |
| 1306 | return not self == other |
Paul Kehrer | 7058ece | 2015-12-25 22:28:29 -0600 | [diff] [blame] | 1307 | |
Paul Kehrer | 5c999d3 | 2015-12-26 17:45:20 -0600 | [diff] [blame] | 1308 | def __getitem__(self, idx): |
| 1309 | return self._general_names[idx] |
| 1310 | |
Paul Kehrer | 5e9eeef | 2017-09-14 11:15:21 +0800 | [diff] [blame] | 1311 | def __hash__(self): |
| 1312 | return hash(self._general_names) |
| 1313 | |
Paul Kehrer | 7058ece | 2015-12-25 22:28:29 -0600 | [diff] [blame] | 1314 | |
| 1315 | @utils.register_interface(ExtensionType) |
| 1316 | class CRLReason(object): |
| 1317 | oid = CRLEntryExtensionOID.CRL_REASON |
| 1318 | |
| 1319 | def __init__(self, reason): |
| 1320 | if not isinstance(reason, ReasonFlags): |
| 1321 | raise TypeError("reason must be an element from ReasonFlags") |
| 1322 | |
| 1323 | self._reason = reason |
| 1324 | |
| 1325 | def __repr__(self): |
| 1326 | return "<CRLReason(reason={0})>".format(self._reason) |
| 1327 | |
| 1328 | def __eq__(self, other): |
| 1329 | if not isinstance(other, CRLReason): |
| 1330 | return NotImplemented |
| 1331 | |
| 1332 | return self.reason == other.reason |
| 1333 | |
| 1334 | def __ne__(self, other): |
| 1335 | return not self == other |
| 1336 | |
Alex Gaynor | 07d5cae | 2015-12-27 15:30:39 -0500 | [diff] [blame] | 1337 | def __hash__(self): |
| 1338 | return hash(self.reason) |
| 1339 | |
Paul Kehrer | 7058ece | 2015-12-25 22:28:29 -0600 | [diff] [blame] | 1340 | reason = utils.read_only_property("_reason") |
Paul Kehrer | 23c0bbc | 2015-12-25 22:35:19 -0600 | [diff] [blame] | 1341 | |
| 1342 | |
| 1343 | @utils.register_interface(ExtensionType) |
| 1344 | class InvalidityDate(object): |
| 1345 | oid = CRLEntryExtensionOID.INVALIDITY_DATE |
| 1346 | |
| 1347 | def __init__(self, invalidity_date): |
| 1348 | if not isinstance(invalidity_date, datetime.datetime): |
| 1349 | raise TypeError("invalidity_date must be a datetime.datetime") |
| 1350 | |
| 1351 | self._invalidity_date = invalidity_date |
| 1352 | |
| 1353 | def __repr__(self): |
| 1354 | return "<InvalidityDate(invalidity_date={0})>".format( |
| 1355 | self._invalidity_date |
| 1356 | ) |
| 1357 | |
| 1358 | def __eq__(self, other): |
| 1359 | if not isinstance(other, InvalidityDate): |
| 1360 | return NotImplemented |
| 1361 | |
| 1362 | return self.invalidity_date == other.invalidity_date |
| 1363 | |
| 1364 | def __ne__(self, other): |
| 1365 | return not self == other |
| 1366 | |
Paul Kehrer | 67cde76 | 2015-12-26 11:37:14 -0600 | [diff] [blame] | 1367 | def __hash__(self): |
| 1368 | return hash(self.invalidity_date) |
| 1369 | |
Paul Kehrer | 23c0bbc | 2015-12-25 22:35:19 -0600 | [diff] [blame] | 1370 | invalidity_date = utils.read_only_property("_invalidity_date") |
Paul Kehrer | 14fd697 | 2015-12-30 10:58:25 -0600 | [diff] [blame] | 1371 | |
| 1372 | |
| 1373 | @utils.register_interface(ExtensionType) |
Alex Gaynor | 6a0718f | 2017-06-04 13:36:58 -0400 | [diff] [blame] | 1374 | class PrecertificateSignedCertificateTimestamps(object): |
| 1375 | oid = ExtensionOID.PRECERT_SIGNED_CERTIFICATE_TIMESTAMPS |
| 1376 | |
| 1377 | def __init__(self, signed_certificate_timestamps): |
| 1378 | signed_certificate_timestamps = list(signed_certificate_timestamps) |
| 1379 | if not all( |
| 1380 | isinstance(sct, SignedCertificateTimestamp) |
| 1381 | for sct in signed_certificate_timestamps |
| 1382 | ): |
| 1383 | raise TypeError( |
| 1384 | "Every item in the signed_certificate_timestamps list must be " |
| 1385 | "a SignedCertificateTimestamp" |
| 1386 | ) |
| 1387 | self._signed_certificate_timestamps = signed_certificate_timestamps |
| 1388 | |
| 1389 | def __iter__(self): |
| 1390 | return iter(self._signed_certificate_timestamps) |
| 1391 | |
| 1392 | def __len__(self): |
| 1393 | return len(self._signed_certificate_timestamps) |
| 1394 | |
| 1395 | def __getitem__(self, idx): |
| 1396 | return self._signed_certificate_timestamps[idx] |
| 1397 | |
| 1398 | def __repr__(self): |
| 1399 | return ( |
| 1400 | "<PrecertificateSignedCertificateTimestamps({0})>".format( |
| 1401 | list(self) |
| 1402 | ) |
| 1403 | ) |
| 1404 | |
Paul Kehrer | 74ce48c | 2018-10-30 10:23:30 +0800 | [diff] [blame] | 1405 | def __hash__(self): |
| 1406 | return hash(tuple(self._signed_certificate_timestamps)) |
| 1407 | |
| 1408 | def __eq__(self, other): |
| 1409 | if not isinstance(other, PrecertificateSignedCertificateTimestamps): |
| 1410 | return NotImplemented |
| 1411 | |
| 1412 | return ( |
| 1413 | self._signed_certificate_timestamps == |
| 1414 | other._signed_certificate_timestamps |
| 1415 | ) |
| 1416 | |
| 1417 | def __ne__(self, other): |
| 1418 | return not self == other |
| 1419 | |
Alex Gaynor | 6a0718f | 2017-06-04 13:36:58 -0400 | [diff] [blame] | 1420 | |
| 1421 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 0940310 | 2018-09-09 21:57:21 -0500 | [diff] [blame] | 1422 | class OCSPNonce(object): |
| 1423 | oid = OCSPExtensionOID.NONCE |
| 1424 | |
| 1425 | def __init__(self, nonce): |
| 1426 | if not isinstance(nonce, bytes): |
| 1427 | raise TypeError("nonce must be bytes") |
| 1428 | |
| 1429 | self._nonce = nonce |
| 1430 | |
| 1431 | def __eq__(self, other): |
| 1432 | if not isinstance(other, OCSPNonce): |
| 1433 | return NotImplemented |
| 1434 | |
| 1435 | return self.nonce == other.nonce |
| 1436 | |
| 1437 | def __ne__(self, other): |
| 1438 | return not self == other |
| 1439 | |
| 1440 | def __hash__(self): |
| 1441 | return hash(self.nonce) |
| 1442 | |
| 1443 | def __repr__(self): |
| 1444 | return "<OCSPNonce(nonce={0.nonce!r})>".format(self) |
| 1445 | |
| 1446 | nonce = utils.read_only_property("_nonce") |
| 1447 | |
| 1448 | |
| 1449 | @utils.register_interface(ExtensionType) |
Paul Kehrer | eb3e2e0 | 2018-12-01 12:15:20 +0800 | [diff] [blame^] | 1450 | class IssuingDistributionPoint(object): |
| 1451 | oid = ExtensionOID.ISSUING_DISTRIBUTION_POINT |
| 1452 | |
| 1453 | def __init__(self, full_name, relative_name, only_contains_user_certs, |
| 1454 | only_contains_ca_certs, only_some_reasons, indirect_crl, |
| 1455 | only_contains_attribute_certs): |
| 1456 | if ( |
| 1457 | only_some_reasons and ( |
| 1458 | not isinstance(only_some_reasons, frozenset) or not all( |
| 1459 | isinstance(x, ReasonFlags) for x in only_some_reasons |
| 1460 | ) |
| 1461 | ) |
| 1462 | ): |
| 1463 | raise TypeError( |
| 1464 | "only_some_reasons must be None or frozenset of ReasonFlags" |
| 1465 | ) |
| 1466 | |
| 1467 | if only_some_reasons and ( |
| 1468 | ReasonFlags.unspecified in only_some_reasons or |
| 1469 | ReasonFlags.remove_from_crl in only_some_reasons |
| 1470 | ): |
| 1471 | raise ValueError( |
| 1472 | "unspecified and remove_from_crl are not valid reasons in an " |
| 1473 | "IssuingDistributionPoint" |
| 1474 | ) |
| 1475 | |
| 1476 | if not ( |
| 1477 | isinstance(only_contains_user_certs, bool) and |
| 1478 | isinstance(only_contains_ca_certs, bool) and |
| 1479 | isinstance(indirect_crl, bool) and |
| 1480 | isinstance(only_contains_attribute_certs, bool) |
| 1481 | ): |
| 1482 | raise TypeError( |
| 1483 | "only_contains_user_certs, only_contains_ca_certs, " |
| 1484 | "indirect_crl and only_contains_attribute_certs " |
| 1485 | "must all be boolean." |
| 1486 | ) |
| 1487 | |
| 1488 | crl_constraints = [ |
| 1489 | only_contains_user_certs, only_contains_ca_certs, |
| 1490 | indirect_crl, only_contains_attribute_certs |
| 1491 | ] |
| 1492 | |
| 1493 | if len([x for x in crl_constraints if x]) > 1: |
| 1494 | raise ValueError( |
| 1495 | "Only one of the following can be set to True: " |
| 1496 | "only_contains_user_certs, only_contains_ca_certs, " |
| 1497 | "indirect_crl, only_contains_attribute_certs" |
| 1498 | ) |
| 1499 | |
| 1500 | if ( |
| 1501 | not any([ |
| 1502 | only_contains_user_certs, only_contains_ca_certs, |
| 1503 | indirect_crl, only_contains_attribute_certs, full_name, |
| 1504 | relative_name, only_some_reasons |
| 1505 | ]) |
| 1506 | ): |
| 1507 | raise ValueError( |
| 1508 | "Cannot create empty extension: " |
| 1509 | "if only_contains_user_certs, only_contains_ca_certs, " |
| 1510 | "indirect_crl, and only_contains_attribute_certs are all False" |
| 1511 | ", then either full_name, relative_name, or only_some_reasons " |
| 1512 | "must have a value." |
| 1513 | ) |
| 1514 | |
| 1515 | self._only_contains_user_certs = only_contains_user_certs |
| 1516 | self._only_contains_ca_certs = only_contains_ca_certs |
| 1517 | self._indirect_crl = indirect_crl |
| 1518 | self._only_contains_attribute_certs = only_contains_attribute_certs |
| 1519 | self._only_some_reasons = only_some_reasons |
| 1520 | self._full_name = full_name |
| 1521 | self._relative_name = relative_name |
| 1522 | |
| 1523 | def __repr__(self): |
| 1524 | return ( |
| 1525 | "<IssuingDistributionPoint(full_name={0.full_name}, " |
| 1526 | "relative_name={0.relative_name}, " |
| 1527 | "only_contains_user_certs={0.only_contains_user_certs}, " |
| 1528 | "only_contains_ca_certs={0.only_contains_ca_certs}, " |
| 1529 | "only_some_reasons={0.only_some_reasons}, " |
| 1530 | "indirect_crl={0.indirect_crl}, " |
| 1531 | "only_contains_attribute_certs=" |
| 1532 | "{0.only_contains_attribute_certs})>".format(self) |
| 1533 | ) |
| 1534 | |
| 1535 | def __eq__(self, other): |
| 1536 | if not isinstance(other, IssuingDistributionPoint): |
| 1537 | return NotImplemented |
| 1538 | |
| 1539 | return ( |
| 1540 | self.full_name == other.full_name and |
| 1541 | self.relative_name == other.relative_name and |
| 1542 | self.only_contains_user_certs == other.only_contains_user_certs and |
| 1543 | self.only_contains_ca_certs == other.only_contains_ca_certs and |
| 1544 | self.only_some_reasons == other.only_some_reasons and |
| 1545 | self.indirect_crl == other.indirect_crl and |
| 1546 | self.only_contains_attribute_certs == |
| 1547 | other.only_contains_attribute_certs |
| 1548 | ) |
| 1549 | |
| 1550 | def __ne__(self, other): |
| 1551 | return not self == other |
| 1552 | |
| 1553 | def __hash__(self): |
| 1554 | return hash(( |
| 1555 | self.full_name, |
| 1556 | self.relative_name, |
| 1557 | self.only_contains_user_certs, |
| 1558 | self.only_contains_ca_certs, |
| 1559 | self.only_some_reasons, |
| 1560 | self.indirect_crl, |
| 1561 | self.only_contains_attribute_certs, |
| 1562 | )) |
| 1563 | |
| 1564 | full_name = utils.read_only_property("_full_name") |
| 1565 | relative_name = utils.read_only_property("_relative_name") |
| 1566 | only_contains_user_certs = utils.read_only_property( |
| 1567 | "_only_contains_user_certs" |
| 1568 | ) |
| 1569 | only_contains_ca_certs = utils.read_only_property( |
| 1570 | "_only_contains_ca_certs" |
| 1571 | ) |
| 1572 | only_some_reasons = utils.read_only_property("_only_some_reasons") |
| 1573 | indirect_crl = utils.read_only_property("_indirect_crl") |
| 1574 | only_contains_attribute_certs = utils.read_only_property( |
| 1575 | "_only_contains_attribute_certs" |
| 1576 | ) |
| 1577 | |
| 1578 | |
| 1579 | @utils.register_interface(ExtensionType) |
Paul Kehrer | 14fd697 | 2015-12-30 10:58:25 -0600 | [diff] [blame] | 1580 | class UnrecognizedExtension(object): |
| 1581 | def __init__(self, oid, value): |
| 1582 | if not isinstance(oid, ObjectIdentifier): |
| 1583 | raise TypeError("oid must be an ObjectIdentifier") |
| 1584 | self._oid = oid |
| 1585 | self._value = value |
| 1586 | |
| 1587 | oid = utils.read_only_property("_oid") |
| 1588 | value = utils.read_only_property("_value") |
| 1589 | |
| 1590 | def __repr__(self): |
| 1591 | return ( |
| 1592 | "<UnrecognizedExtension(oid={0.oid}, value={0.value!r})>".format( |
| 1593 | self |
| 1594 | ) |
| 1595 | ) |
| 1596 | |
| 1597 | def __eq__(self, other): |
| 1598 | if not isinstance(other, UnrecognizedExtension): |
| 1599 | return NotImplemented |
| 1600 | |
| 1601 | return self.oid == other.oid and self.value == other.value |
| 1602 | |
| 1603 | def __ne__(self, other): |
| 1604 | return not self == other |
| 1605 | |
| 1606 | def __hash__(self): |
| 1607 | return hash((self.oid, self.value)) |