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