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