Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [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 | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 7 | import abc |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 8 | import ipaddress |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 9 | from enum import Enum |
| 10 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 11 | import six |
| 12 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 13 | from cryptography import utils |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 14 | from cryptography.hazmat.primitives import hashes |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 15 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 16 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 17 | _OID_NAMES = { |
| 18 | "2.5.4.3": "commonName", |
| 19 | "2.5.4.6": "countryName", |
| 20 | "2.5.4.7": "localityName", |
| 21 | "2.5.4.8": "stateOrProvinceName", |
| 22 | "2.5.4.10": "organizationName", |
| 23 | "2.5.4.11": "organizationalUnitName", |
| 24 | "2.5.4.5": "serialNumber", |
| 25 | "2.5.4.4": "surname", |
| 26 | "2.5.4.42": "givenName", |
| 27 | "2.5.4.12": "title", |
| 28 | "2.5.4.44": "generationQualifier", |
| 29 | "2.5.4.46": "dnQualifier", |
| 30 | "2.5.4.65": "pseudonym", |
| 31 | "0.9.2342.19200300.100.1.25": "domainComponent", |
| 32 | "1.2.840.113549.1.9.1": "emailAddress", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 33 | "1.2.840.113549.1.1.4": "md5WithRSAEncryption", |
| 34 | "1.2.840.113549.1.1.5": "sha1WithRSAEncryption", |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 35 | "1.2.840.113549.1.1.14": "sha224WithRSAEncryption", |
| 36 | "1.2.840.113549.1.1.11": "sha256WithRSAEncryption", |
| 37 | "1.2.840.113549.1.1.12": "sha384WithRSAEncryption", |
| 38 | "1.2.840.113549.1.1.13": "sha512WithRSAEncryption", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 39 | "1.2.840.10045.4.3.1": "ecdsa-with-SHA224", |
| 40 | "1.2.840.10045.4.3.2": "ecdsa-with-SHA256", |
| 41 | "1.2.840.10045.4.3.3": "ecdsa-with-SHA384", |
| 42 | "1.2.840.10045.4.3.4": "ecdsa-with-SHA512", |
| 43 | "1.2.840.10040.4.3": "dsa-with-sha1", |
| 44 | "2.16.840.1.101.3.4.3.1": "dsa-with-sha224", |
| 45 | "2.16.840.1.101.3.4.3.2": "dsa-with-sha256", |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 46 | "1.3.6.1.5.5.7.3.1": "serverAuth", |
| 47 | "1.3.6.1.5.5.7.3.2": "clientAuth", |
| 48 | "1.3.6.1.5.5.7.3.3": "codeSigning", |
| 49 | "1.3.6.1.5.5.7.3.4": "emailProtection", |
| 50 | "1.3.6.1.5.5.7.3.8": "timeStamping", |
| 51 | "1.3.6.1.5.5.7.3.9": "OCSPSigning", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 52 | "2.5.29.9": "subjectDirectoryAttributes", |
| 53 | "2.5.29.14": "subjectKeyIdentifier", |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 54 | "2.5.29.15": "keyUsage", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 55 | "2.5.29.17": "subjectAltName", |
| 56 | "2.5.29.18": "issuerAltName", |
| 57 | "2.5.29.19": "basicConstraints", |
| 58 | "2.5.29.30": "nameConstraints", |
| 59 | "2.5.29.31": "cRLDistributionPoints", |
| 60 | "2.5.29.32": "certificatePolicies", |
| 61 | "2.5.29.33": "policyMappings", |
| 62 | "2.5.29.35": "authorityKeyIdentifier", |
| 63 | "2.5.29.36": "policyConstraints", |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 64 | "2.5.29.37": "extendedKeyUsage", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 65 | "2.5.29.46": "freshestCRL", |
| 66 | "2.5.29.54": "inhibitAnyPolicy", |
| 67 | "1.3.6.1.5.5.7.1.1": "authorityInfoAccess", |
| 68 | "1.3.6.1.5.5.7.1.11": "subjectInfoAccess", |
| 69 | "1.3.6.1.5.5.7.48.1.5": "OCSPNoCheck", |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 70 | "1.3.6.1.5.5.7.48.1": "OCSP", |
Paul Kehrer | f506bca | 2015-05-02 22:31:47 -0500 | [diff] [blame] | 71 | "1.3.6.1.5.5.7.48.2": "caIssuers", |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 72 | "1.3.6.1.5.5.7.2.1": "id-qt-cps", |
| 73 | "1.3.6.1.5.5.7.2.2": "id-qt-unotice", |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 77 | _GENERAL_NAMES = { |
| 78 | 0: "otherName", |
| 79 | 1: "rfc822Name", |
| 80 | 2: "dNSName", |
| 81 | 3: "x400Address", |
| 82 | 4: "directoryName", |
| 83 | 5: "ediPartyName", |
| 84 | 6: "uniformResourceIdentifier", |
| 85 | 7: "iPAddress", |
| 86 | 8: "registeredID", |
| 87 | } |
| 88 | |
| 89 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 90 | class Version(Enum): |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 91 | v1 = 0 |
| 92 | v3 = 2 |
| 93 | |
| 94 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 95 | def load_pem_x509_certificate(data, backend): |
| 96 | return backend.load_pem_x509_certificate(data) |
| 97 | |
| 98 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 99 | def load_der_x509_certificate(data, backend): |
| 100 | return backend.load_der_x509_certificate(data) |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 101 | |
| 102 | |
Paul Kehrer | 31e3988 | 2015-03-11 11:37:04 -0500 | [diff] [blame] | 103 | def load_pem_x509_csr(data, backend): |
| 104 | return backend.load_pem_x509_csr(data) |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 105 | |
| 106 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 107 | def load_der_x509_csr(data, backend): |
| 108 | return backend.load_der_x509_csr(data) |
| 109 | |
| 110 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 111 | class InvalidVersion(Exception): |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 112 | def __init__(self, msg, parsed_version): |
| 113 | super(InvalidVersion, self).__init__(msg) |
| 114 | self.parsed_version = parsed_version |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 115 | |
| 116 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 117 | class DuplicateExtension(Exception): |
| 118 | def __init__(self, msg, oid): |
| 119 | super(DuplicateExtension, self).__init__(msg) |
| 120 | self.oid = oid |
| 121 | |
| 122 | |
| 123 | class UnsupportedExtension(Exception): |
| 124 | def __init__(self, msg, oid): |
| 125 | super(UnsupportedExtension, self).__init__(msg) |
| 126 | self.oid = oid |
| 127 | |
| 128 | |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 129 | class ExtensionNotFound(Exception): |
| 130 | def __init__(self, msg, oid): |
| 131 | super(ExtensionNotFound, self).__init__(msg) |
| 132 | self.oid = oid |
| 133 | |
| 134 | |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 135 | class UnsupportedGeneralNameType(Exception): |
Paul Kehrer | bed0735 | 2015-04-21 08:31:10 -0500 | [diff] [blame] | 136 | def __init__(self, msg, type): |
| 137 | super(UnsupportedGeneralNameType, self).__init__(msg) |
| 138 | self.type = type |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 139 | |
| 140 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 141 | class NameAttribute(object): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 142 | def __init__(self, oid, value): |
| 143 | if not isinstance(oid, ObjectIdentifier): |
Paul Kehrer | 858b9b7 | 2015-02-05 09:50:31 -0600 | [diff] [blame] | 144 | raise TypeError( |
| 145 | "oid argument must be an ObjectIdentifier instance." |
| 146 | ) |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 147 | |
| 148 | self._oid = oid |
| 149 | self._value = value |
| 150 | |
| 151 | oid = utils.read_only_property("_oid") |
| 152 | value = utils.read_only_property("_value") |
| 153 | |
| 154 | def __eq__(self, other): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 155 | if not isinstance(other, NameAttribute): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 156 | return NotImplemented |
| 157 | |
| 158 | return ( |
| 159 | self.oid == other.oid and |
| 160 | self.value == other.value |
| 161 | ) |
| 162 | |
| 163 | def __ne__(self, other): |
| 164 | return not self == other |
| 165 | |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 166 | def __repr__(self): |
Alex Gaynor | d6b63da | 2015-03-23 00:25:44 -0400 | [diff] [blame] | 167 | return "<NameAttribute(oid={0.oid}, value={0.value!r})>".format(self) |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 168 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 169 | |
| 170 | class ObjectIdentifier(object): |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 171 | def __init__(self, dotted_string): |
| 172 | self._dotted_string = dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 173 | |
| 174 | def __eq__(self, other): |
| 175 | if not isinstance(other, ObjectIdentifier): |
| 176 | return NotImplemented |
| 177 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 178 | return self._dotted_string == other._dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 179 | |
| 180 | def __ne__(self, other): |
| 181 | return not self == other |
| 182 | |
| 183 | def __repr__(self): |
| 184 | return "<ObjectIdentifier(oid={0}, name={1})>".format( |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 185 | self._dotted_string, |
| 186 | _OID_NAMES.get(self._dotted_string, "Unknown OID") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 187 | ) |
| 188 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 189 | def __hash__(self): |
| 190 | return hash(self.dotted_string) |
| 191 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 192 | dotted_string = utils.read_only_property("_dotted_string") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 193 | |
| 194 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 195 | class Name(object): |
| 196 | def __init__(self, attributes): |
| 197 | self._attributes = attributes |
| 198 | |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 199 | def get_attributes_for_oid(self, oid): |
Alex Gaynor | f957423 | 2015-02-19 13:56:50 -0800 | [diff] [blame] | 200 | return [i for i in self if i.oid == oid] |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 201 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 202 | def __eq__(self, other): |
| 203 | if not isinstance(other, Name): |
| 204 | return NotImplemented |
| 205 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 206 | return self._attributes == other._attributes |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 207 | |
| 208 | def __ne__(self, other): |
| 209 | return not self == other |
| 210 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 211 | def __iter__(self): |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 212 | return iter(self._attributes) |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 213 | |
| 214 | def __len__(self): |
| 215 | return len(self._attributes) |
| 216 | |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 217 | def __repr__(self): |
Paul Kehrer | f4ed10a | 2015-04-11 15:53:12 -0400 | [diff] [blame] | 218 | return "<Name({0!r})>".format(self._attributes) |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 219 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 220 | |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 221 | OID_SUBJECT_DIRECTORY_ATTRIBUTES = ObjectIdentifier("2.5.29.9") |
| 222 | OID_SUBJECT_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.14") |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 223 | OID_KEY_USAGE = ObjectIdentifier("2.5.29.15") |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 224 | OID_SUBJECT_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.17") |
| 225 | OID_ISSUER_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.18") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 226 | OID_BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19") |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 227 | OID_NAME_CONSTRAINTS = ObjectIdentifier("2.5.29.30") |
| 228 | OID_CRL_DISTRIBUTION_POINTS = ObjectIdentifier("2.5.29.31") |
| 229 | OID_CERTIFICATE_POLICIES = ObjectIdentifier("2.5.29.32") |
| 230 | OID_POLICY_MAPPINGS = ObjectIdentifier("2.5.29.33") |
| 231 | OID_AUTHORITY_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.35") |
| 232 | OID_POLICY_CONSTRAINTS = ObjectIdentifier("2.5.29.36") |
| 233 | OID_EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37") |
| 234 | OID_FRESHEST_CRL = ObjectIdentifier("2.5.29.46") |
| 235 | OID_INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54") |
| 236 | OID_AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1") |
| 237 | OID_SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11") |
| 238 | OID_OCSP_NO_CHECK = ObjectIdentifier("1.3.6.1.5.5.7.48.1.5") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 239 | |
| 240 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 241 | class Extensions(object): |
| 242 | def __init__(self, extensions): |
| 243 | self._extensions = extensions |
| 244 | |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 245 | def get_extension_for_oid(self, oid): |
| 246 | for ext in self: |
| 247 | if ext.oid == oid: |
| 248 | return ext |
| 249 | |
| 250 | raise ExtensionNotFound("No {0} extension was found".format(oid), oid) |
| 251 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 252 | def __iter__(self): |
| 253 | return iter(self._extensions) |
| 254 | |
| 255 | def __len__(self): |
| 256 | return len(self._extensions) |
| 257 | |
| 258 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 259 | class Extension(object): |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 260 | def __init__(self, oid, critical, value): |
| 261 | if not isinstance(oid, ObjectIdentifier): |
| 262 | raise TypeError( |
| 263 | "oid argument must be an ObjectIdentifier instance." |
| 264 | ) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 265 | |
| 266 | if not isinstance(critical, bool): |
| 267 | raise TypeError("critical must be a boolean value") |
| 268 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 269 | self._oid = oid |
| 270 | self._critical = critical |
| 271 | self._value = value |
| 272 | |
| 273 | oid = utils.read_only_property("_oid") |
| 274 | critical = utils.read_only_property("_critical") |
| 275 | value = utils.read_only_property("_value") |
| 276 | |
| 277 | def __repr__(self): |
Paul Kehrer | 58b7569 | 2015-03-22 23:24:58 -0500 | [diff] [blame] | 278 | return ("<Extension(oid={0.oid}, critical={0.critical}, " |
| 279 | "value={0.value})>").format(self) |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 280 | |
Paul Kehrer | 58e870c | 2015-05-17 09:15:30 -0700 | [diff] [blame] | 281 | def __eq__(self, other): |
| 282 | if not isinstance(other, Extension): |
| 283 | return NotImplemented |
| 284 | |
| 285 | return ( |
| 286 | self.oid == other.oid and |
| 287 | self.critical == other.critical and |
| 288 | self.value == other.value |
| 289 | ) |
| 290 | |
| 291 | def __ne__(self, other): |
| 292 | return not self == other |
| 293 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 294 | |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 295 | class ExtendedKeyUsage(object): |
| 296 | def __init__(self, usages): |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 297 | if not all(isinstance(x, ObjectIdentifier) for x in usages): |
| 298 | raise TypeError( |
| 299 | "Every item in the usages list must be an ObjectIdentifier" |
| 300 | ) |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 301 | |
| 302 | self._usages = usages |
| 303 | |
| 304 | def __iter__(self): |
| 305 | return iter(self._usages) |
| 306 | |
| 307 | def __len__(self): |
| 308 | return len(self._usages) |
| 309 | |
Paul Kehrer | 23d10c3 | 2015-04-02 23:12:32 -0500 | [diff] [blame] | 310 | def __repr__(self): |
| 311 | return "<ExtendedKeyUsage({0})>".format(self._usages) |
| 312 | |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 313 | def __eq__(self, other): |
| 314 | if not isinstance(other, ExtendedKeyUsage): |
| 315 | return NotImplemented |
| 316 | |
Paul Kehrer | f24bad7 | 2015-05-02 19:36:20 -0500 | [diff] [blame] | 317 | return self._usages == other._usages |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 318 | |
| 319 | def __ne__(self, other): |
| 320 | return not self == other |
| 321 | |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 322 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 323 | class BasicConstraints(object): |
| 324 | def __init__(self, ca, path_length): |
| 325 | if not isinstance(ca, bool): |
| 326 | raise TypeError("ca must be a boolean value") |
| 327 | |
Paul Kehrer | 611d3d3 | 2015-03-22 13:31:18 -0500 | [diff] [blame] | 328 | if path_length is not None and not ca: |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 329 | raise ValueError("path_length must be None when ca is False") |
| 330 | |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 331 | if ( |
Paul Kehrer | 5553d57 | 2015-03-23 21:08:01 -0500 | [diff] [blame] | 332 | path_length is not None and |
| 333 | (not isinstance(path_length, six.integer_types) or path_length < 0) |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 334 | ): |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 335 | raise TypeError( |
| 336 | "path_length must be a non-negative integer or None" |
| 337 | ) |
| 338 | |
| 339 | self._ca = ca |
| 340 | self._path_length = path_length |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 341 | |
| 342 | ca = utils.read_only_property("_ca") |
| 343 | path_length = utils.read_only_property("_path_length") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 344 | |
| 345 | def __repr__(self): |
Paul Kehrer | 58b7569 | 2015-03-22 23:24:58 -0500 | [diff] [blame] | 346 | return ("<BasicConstraints(ca={0.ca}, " |
| 347 | "path_length={0.path_length})>").format(self) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 348 | |
Paul Kehrer | 3a69b13 | 2015-05-13 10:03:46 -0500 | [diff] [blame] | 349 | def __eq__(self, other): |
| 350 | if not isinstance(other, BasicConstraints): |
| 351 | return NotImplemented |
| 352 | |
| 353 | return self.ca == other.ca and self.path_length == other.path_length |
| 354 | |
| 355 | def __ne__(self, other): |
| 356 | return not self == other |
| 357 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 358 | |
Paul Kehrer | cecbbba | 2015-03-30 14:58:38 -0500 | [diff] [blame] | 359 | class KeyUsage(object): |
| 360 | def __init__(self, digital_signature, content_commitment, key_encipherment, |
| 361 | data_encipherment, key_agreement, key_cert_sign, crl_sign, |
| 362 | encipher_only, decipher_only): |
| 363 | if not key_agreement and (encipher_only or decipher_only): |
| 364 | raise ValueError( |
| 365 | "encipher_only and decipher_only can only be true when " |
| 366 | "key_agreement is true" |
| 367 | ) |
| 368 | |
| 369 | self._digital_signature = digital_signature |
| 370 | self._content_commitment = content_commitment |
| 371 | self._key_encipherment = key_encipherment |
| 372 | self._data_encipherment = data_encipherment |
| 373 | self._key_agreement = key_agreement |
| 374 | self._key_cert_sign = key_cert_sign |
| 375 | self._crl_sign = crl_sign |
| 376 | self._encipher_only = encipher_only |
| 377 | self._decipher_only = decipher_only |
| 378 | |
| 379 | digital_signature = utils.read_only_property("_digital_signature") |
| 380 | content_commitment = utils.read_only_property("_content_commitment") |
| 381 | key_encipherment = utils.read_only_property("_key_encipherment") |
| 382 | data_encipherment = utils.read_only_property("_data_encipherment") |
| 383 | key_agreement = utils.read_only_property("_key_agreement") |
| 384 | key_cert_sign = utils.read_only_property("_key_cert_sign") |
| 385 | crl_sign = utils.read_only_property("_crl_sign") |
| 386 | |
| 387 | @property |
| 388 | def encipher_only(self): |
| 389 | if not self.key_agreement: |
| 390 | raise ValueError( |
| 391 | "encipher_only is undefined unless key_agreement is true" |
| 392 | ) |
| 393 | else: |
| 394 | return self._encipher_only |
| 395 | |
| 396 | @property |
| 397 | def decipher_only(self): |
| 398 | if not self.key_agreement: |
| 399 | raise ValueError( |
| 400 | "decipher_only is undefined unless key_agreement is true" |
| 401 | ) |
| 402 | else: |
| 403 | return self._decipher_only |
| 404 | |
Paul Kehrer | ac3e5bb | 2015-04-02 23:07:10 -0500 | [diff] [blame] | 405 | def __repr__(self): |
| 406 | try: |
| 407 | encipher_only = self.encipher_only |
| 408 | decipher_only = self.decipher_only |
| 409 | except ValueError: |
Paul Kehrer | b372e67 | 2015-04-15 11:05:24 -0400 | [diff] [blame] | 410 | encipher_only = None |
| 411 | decipher_only = None |
Paul Kehrer | ac3e5bb | 2015-04-02 23:07:10 -0500 | [diff] [blame] | 412 | |
| 413 | return ("<KeyUsage(digital_signature={0.digital_signature}, " |
| 414 | "content_commitment={0.content_commitment}, " |
| 415 | "key_encipherment={0.key_encipherment}, " |
| 416 | "data_encipherment={0.data_encipherment}, " |
| 417 | "key_agreement={0.key_agreement}, " |
| 418 | "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, " |
| 419 | "encipher_only={1}, decipher_only={2})>").format( |
| 420 | self, encipher_only, decipher_only) |
| 421 | |
Paul Kehrer | 8565f5e | 2015-05-13 09:57:09 -0500 | [diff] [blame] | 422 | def __eq__(self, other): |
| 423 | if not isinstance(other, KeyUsage): |
| 424 | return NotImplemented |
| 425 | |
| 426 | return ( |
| 427 | self.digital_signature == other.digital_signature and |
| 428 | self.content_commitment == other.content_commitment and |
| 429 | self.key_encipherment == other.key_encipherment and |
| 430 | self.data_encipherment == other.data_encipherment and |
| 431 | self.key_agreement == other.key_agreement and |
| 432 | self.key_cert_sign == other.key_cert_sign and |
| 433 | self.crl_sign == other.crl_sign and |
| 434 | self._encipher_only == other._encipher_only and |
| 435 | self._decipher_only == other._decipher_only |
| 436 | ) |
| 437 | |
| 438 | def __ne__(self, other): |
| 439 | return not self == other |
| 440 | |
Paul Kehrer | cecbbba | 2015-03-30 14:58:38 -0500 | [diff] [blame] | 441 | |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 442 | class AuthorityInformationAccess(object): |
| 443 | def __init__(self, descriptions): |
| 444 | if not all(isinstance(x, AccessDescription) for x in descriptions): |
| 445 | raise TypeError( |
| 446 | "Every item in the descriptions list must be an " |
| 447 | "AccessDescription" |
| 448 | ) |
| 449 | |
| 450 | self._descriptions = descriptions |
| 451 | |
| 452 | def __iter__(self): |
| 453 | return iter(self._descriptions) |
| 454 | |
| 455 | def __len__(self): |
| 456 | return len(self._descriptions) |
| 457 | |
| 458 | def __repr__(self): |
| 459 | return "<AuthorityInformationAccess({0})>".format(self._descriptions) |
| 460 | |
| 461 | def __eq__(self, other): |
| 462 | if not isinstance(other, AuthorityInformationAccess): |
| 463 | return NotImplemented |
| 464 | |
| 465 | return self._descriptions == other._descriptions |
| 466 | |
| 467 | def __ne__(self, other): |
| 468 | return not self == other |
| 469 | |
| 470 | |
| 471 | class AccessDescription(object): |
| 472 | def __init__(self, access_method, access_location): |
| 473 | if not (access_method == OID_OCSP or access_method == OID_CA_ISSUERS): |
Paul Kehrer | f506bca | 2015-05-02 22:31:47 -0500 | [diff] [blame] | 474 | raise ValueError( |
| 475 | "access_method must be OID_OCSP or OID_CA_ISSUERS" |
| 476 | ) |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 477 | |
| 478 | if not isinstance(access_location, GeneralName): |
| 479 | raise TypeError("access_location must be a GeneralName") |
| 480 | |
| 481 | self._access_method = access_method |
| 482 | self._access_location = access_location |
| 483 | |
| 484 | def __repr__(self): |
| 485 | return ( |
| 486 | "<AccessDescription(access_method={0.access_method}, access_locati" |
| 487 | "on={0.access_location})>".format(self) |
| 488 | ) |
| 489 | |
| 490 | def __eq__(self, other): |
| 491 | if not isinstance(other, AccessDescription): |
| 492 | return NotImplemented |
| 493 | |
| 494 | return ( |
| 495 | self.access_method == other.access_method and |
| 496 | self.access_location == other.access_location |
| 497 | ) |
| 498 | |
| 499 | def __ne__(self, other): |
| 500 | return not self == other |
| 501 | |
| 502 | access_method = utils.read_only_property("_access_method") |
| 503 | access_location = utils.read_only_property("_access_location") |
| 504 | |
| 505 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 506 | class CertificatePolicies(object): |
| 507 | def __init__(self, policies): |
Paul Kehrer | f61ec74 | 2015-04-18 20:42:39 -0500 | [diff] [blame] | 508 | if not all(isinstance(x, PolicyInformation) for x in policies): |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 509 | raise TypeError( |
| 510 | "Every item in the policies list must be a " |
| 511 | "PolicyInformation" |
| 512 | ) |
| 513 | |
| 514 | self._policies = policies |
| 515 | |
| 516 | def __iter__(self): |
| 517 | return iter(self._policies) |
| 518 | |
| 519 | def __len__(self): |
| 520 | return len(self._policies) |
| 521 | |
| 522 | def __repr__(self): |
| 523 | return "<CertificatePolicies({0})>".format(self._policies) |
| 524 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 525 | def __eq__(self, other): |
| 526 | if not isinstance(other, CertificatePolicies): |
| 527 | return NotImplemented |
| 528 | |
| 529 | return self._policies == other._policies |
| 530 | |
| 531 | def __ne__(self, other): |
| 532 | return not self == other |
| 533 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 534 | |
| 535 | class PolicyInformation(object): |
| 536 | def __init__(self, policy_identifier, policy_qualifiers): |
| 537 | if not isinstance(policy_identifier, ObjectIdentifier): |
| 538 | raise TypeError("policy_identifier must be an ObjectIdentifier") |
| 539 | |
| 540 | self._policy_identifier = policy_identifier |
| 541 | if policy_qualifiers and not all( |
Paul Kehrer | ba35b3b | 2015-05-10 13:07:59 -0500 | [diff] [blame] | 542 | isinstance( |
| 543 | x, (six.text_type, UserNotice) |
| 544 | ) for x in policy_qualifiers |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 545 | ): |
| 546 | raise TypeError( |
Paul Kehrer | ba35b3b | 2015-05-10 13:07:59 -0500 | [diff] [blame] | 547 | "policy_qualifiers must be a list of strings and/or UserNotice" |
| 548 | " objects or None" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 549 | ) |
| 550 | |
| 551 | self._policy_qualifiers = policy_qualifiers |
| 552 | |
| 553 | def __repr__(self): |
| 554 | return ( |
| 555 | "<PolicyInformation(policy_identifier={0.policy_identifier}, polic" |
| 556 | "y_qualifiers={0.policy_qualifiers})>".format(self) |
| 557 | ) |
| 558 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 559 | def __eq__(self, other): |
| 560 | if not isinstance(other, PolicyInformation): |
| 561 | return NotImplemented |
| 562 | |
| 563 | return ( |
| 564 | self.policy_identifier == other.policy_identifier and |
| 565 | self.policy_qualifiers == other.policy_qualifiers |
| 566 | ) |
| 567 | |
| 568 | def __ne__(self, other): |
| 569 | return not self == other |
| 570 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 571 | policy_identifier = utils.read_only_property("_policy_identifier") |
| 572 | policy_qualifiers = utils.read_only_property("_policy_qualifiers") |
| 573 | |
| 574 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 575 | class UserNotice(object): |
| 576 | def __init__(self, notice_reference, explicit_text): |
| 577 | if notice_reference and not isinstance( |
| 578 | notice_reference, NoticeReference |
| 579 | ): |
| 580 | raise TypeError( |
| 581 | "notice_reference must be None or a NoticeReference" |
| 582 | ) |
| 583 | |
| 584 | self._notice_reference = notice_reference |
| 585 | self._explicit_text = explicit_text |
| 586 | |
| 587 | def __repr__(self): |
| 588 | return ( |
| 589 | "<UserNotice(notice_reference={0.notice_reference}, explicit_text=" |
Paul Kehrer | 9aaef9e | 2015-05-11 10:49:20 -0500 | [diff] [blame] | 590 | "{0.explicit_text!r})>".format(self) |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 591 | ) |
| 592 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 593 | def __eq__(self, other): |
| 594 | if not isinstance(other, UserNotice): |
| 595 | return NotImplemented |
| 596 | |
| 597 | return ( |
| 598 | self.notice_reference == other.notice_reference and |
| 599 | self.explicit_text == other.explicit_text |
| 600 | ) |
| 601 | |
| 602 | def __ne__(self, other): |
| 603 | return not self == other |
| 604 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 605 | notice_reference = utils.read_only_property("_notice_reference") |
| 606 | explicit_text = utils.read_only_property("_explicit_text") |
| 607 | |
| 608 | |
| 609 | class NoticeReference(object): |
| 610 | def __init__(self, organization, notice_numbers): |
| 611 | self._organization = organization |
Paul Kehrer | 6e198b0 | 2015-05-12 15:53:38 -0500 | [diff] [blame] | 612 | if not isinstance(notice_numbers, list) or not all( |
Paul Kehrer | f61ec74 | 2015-04-18 20:42:39 -0500 | [diff] [blame] | 613 | isinstance(x, int) for x in notice_numbers |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 614 | ): |
| 615 | raise TypeError( |
Paul Kehrer | 6e198b0 | 2015-05-12 15:53:38 -0500 | [diff] [blame] | 616 | "notice_numbers must be a list of integers" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 617 | ) |
| 618 | |
| 619 | self._notice_numbers = notice_numbers |
| 620 | |
| 621 | def __repr__(self): |
| 622 | return ( |
Paul Kehrer | 73be2ca | 2015-05-11 21:22:38 -0500 | [diff] [blame] | 623 | "<NoticeReference(organization={0.organization!r}, notice_numbers=" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 624 | "{0.notice_numbers})>".format(self) |
| 625 | ) |
| 626 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 627 | def __eq__(self, other): |
| 628 | if not isinstance(other, NoticeReference): |
| 629 | return NotImplemented |
| 630 | |
| 631 | return ( |
| 632 | self.organization == other.organization and |
| 633 | self.notice_numbers == other.notice_numbers |
| 634 | ) |
| 635 | |
| 636 | def __ne__(self, other): |
| 637 | return not self == other |
| 638 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 639 | organization = utils.read_only_property("_organization") |
| 640 | notice_numbers = utils.read_only_property("_notice_numbers") |
| 641 | |
| 642 | |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 643 | class SubjectKeyIdentifier(object): |
| 644 | def __init__(self, digest): |
| 645 | self._digest = digest |
| 646 | |
| 647 | digest = utils.read_only_property("_digest") |
| 648 | |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 649 | def __repr__(self): |
Paul Kehrer | cbfb101 | 2015-04-10 20:57:20 -0400 | [diff] [blame] | 650 | return "<SubjectKeyIdentifier(digest={0!r})>".format(self.digest) |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 651 | |
| 652 | def __eq__(self, other): |
| 653 | if not isinstance(other, SubjectKeyIdentifier): |
| 654 | return NotImplemented |
| 655 | |
| 656 | return ( |
| 657 | self.digest == other.digest |
| 658 | ) |
| 659 | |
| 660 | def __ne__(self, other): |
| 661 | return not self == other |
| 662 | |
| 663 | |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 664 | class CRLDistributionPoints(object): |
| 665 | def __init__(self, distribution_points): |
| 666 | if not all( |
| 667 | isinstance(x, DistributionPoint) for x in distribution_points |
| 668 | ): |
| 669 | raise TypeError( |
| 670 | "distribution_points must be a list of DistributionPoint " |
| 671 | "objects" |
| 672 | ) |
| 673 | |
| 674 | self._distribution_points = distribution_points |
| 675 | |
| 676 | def __iter__(self): |
| 677 | return iter(self._distribution_points) |
| 678 | |
| 679 | def __len__(self): |
| 680 | return len(self._distribution_points) |
| 681 | |
| 682 | def __repr__(self): |
| 683 | return "<CRLDistributionPoints({0})>".format(self._distribution_points) |
| 684 | |
| 685 | def __eq__(self, other): |
| 686 | if not isinstance(other, CRLDistributionPoints): |
| 687 | return NotImplemented |
| 688 | |
| 689 | return self._distribution_points == other._distribution_points |
| 690 | |
| 691 | def __ne__(self, other): |
| 692 | return not self == other |
| 693 | |
| 694 | |
| 695 | class DistributionPoint(object): |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 696 | def __init__(self, full_name, relative_name, reasons, crl_issuer): |
| 697 | if full_name and relative_name: |
| 698 | raise ValueError( |
| 699 | "At least one of full_name and relative_name must be None" |
| 700 | ) |
| 701 | |
| 702 | if full_name and not all( |
| 703 | isinstance(x, GeneralName) for x in full_name |
| 704 | ): |
| 705 | raise TypeError( |
| 706 | "full_name must be a list of GeneralName objects" |
| 707 | ) |
| 708 | |
| 709 | if relative_name and not isinstance(relative_name, Name): |
| 710 | raise TypeError("relative_name must be a Name") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 711 | |
| 712 | if crl_issuer and not all( |
| 713 | isinstance(x, GeneralName) for x in crl_issuer |
| 714 | ): |
| 715 | raise TypeError( |
| 716 | "crl_issuer must be None or a list of general names" |
| 717 | ) |
| 718 | |
Paul Kehrer | 3fd0260 | 2015-05-09 19:46:13 -0500 | [diff] [blame] | 719 | if reasons and (not isinstance(reasons, frozenset) or not all( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 720 | isinstance(x, ReasonFlags) for x in reasons |
Paul Kehrer | 3fd0260 | 2015-05-09 19:46:13 -0500 | [diff] [blame] | 721 | )): |
| 722 | raise TypeError("reasons must be None or frozenset of ReasonFlags") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 723 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 724 | if reasons and ( |
| 725 | ReasonFlags.unspecified in reasons or |
| 726 | ReasonFlags.remove_from_crl in reasons |
| 727 | ): |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 728 | raise ValueError( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 729 | "unspecified and remove_from_crl are not valid reasons in a " |
| 730 | "DistributionPoint" |
| 731 | ) |
| 732 | |
| 733 | if reasons and not crl_issuer and not (full_name or relative_name): |
| 734 | raise ValueError( |
| 735 | "You must supply crl_issuer, full_name, or relative_name when " |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 736 | "reasons is not None" |
| 737 | ) |
| 738 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 739 | self._full_name = full_name |
| 740 | self._relative_name = relative_name |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 741 | self._reasons = reasons |
| 742 | self._crl_issuer = crl_issuer |
| 743 | |
| 744 | def __repr__(self): |
| 745 | return ( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 746 | "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela" |
| 747 | "tive_name}, reasons={0.reasons}, crl_issuer={0.crl_is" |
| 748 | "suer})>".format(self) |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 749 | ) |
| 750 | |
| 751 | def __eq__(self, other): |
| 752 | if not isinstance(other, DistributionPoint): |
| 753 | return NotImplemented |
| 754 | |
| 755 | return ( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 756 | self.full_name == other.full_name and |
| 757 | self.relative_name == other.relative_name and |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 758 | self.reasons == other.reasons and |
| 759 | self.crl_issuer == other.crl_issuer |
| 760 | ) |
| 761 | |
| 762 | def __ne__(self, other): |
| 763 | return not self == other |
| 764 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 765 | full_name = utils.read_only_property("_full_name") |
| 766 | relative_name = utils.read_only_property("_relative_name") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 767 | reasons = utils.read_only_property("_reasons") |
| 768 | crl_issuer = utils.read_only_property("_crl_issuer") |
| 769 | |
| 770 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 771 | class ReasonFlags(Enum): |
| 772 | unspecified = "unspecified" |
| 773 | key_compromise = "keyCompromise" |
| 774 | ca_compromise = "cACompromise" |
| 775 | affiliation_changed = "affiliationChanged" |
| 776 | superseded = "superseded" |
| 777 | cessation_of_operation = "cessationOfOperation" |
| 778 | certificate_hold = "certificateHold" |
| 779 | privilege_withdrawn = "privilegeWithdrawn" |
| 780 | aa_compromise = "aACompromise" |
| 781 | remove_from_crl = "removeFromCRL" |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 782 | |
| 783 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 784 | @six.add_metaclass(abc.ABCMeta) |
| 785 | class GeneralName(object): |
| 786 | @abc.abstractproperty |
| 787 | def value(self): |
| 788 | """ |
| 789 | Return the value of the object |
| 790 | """ |
| 791 | |
| 792 | |
| 793 | @utils.register_interface(GeneralName) |
| 794 | class RFC822Name(object): |
| 795 | def __init__(self, value): |
| 796 | if not isinstance(value, six.text_type): |
| 797 | raise TypeError("value must be a unicode string") |
| 798 | |
| 799 | self._value = value |
| 800 | |
| 801 | value = utils.read_only_property("_value") |
| 802 | |
| 803 | def __repr__(self): |
| 804 | return "<RFC822Name(value={0})>".format(self.value) |
| 805 | |
| 806 | def __eq__(self, other): |
| 807 | if not isinstance(other, RFC822Name): |
| 808 | return NotImplemented |
| 809 | |
| 810 | return self.value == other.value |
| 811 | |
| 812 | def __ne__(self, other): |
| 813 | return not self == other |
| 814 | |
| 815 | |
| 816 | @utils.register_interface(GeneralName) |
| 817 | class DNSName(object): |
| 818 | def __init__(self, value): |
| 819 | if not isinstance(value, six.text_type): |
| 820 | raise TypeError("value must be a unicode string") |
| 821 | |
| 822 | self._value = value |
| 823 | |
| 824 | value = utils.read_only_property("_value") |
| 825 | |
| 826 | def __repr__(self): |
| 827 | return "<DNSName(value={0})>".format(self.value) |
| 828 | |
| 829 | def __eq__(self, other): |
| 830 | if not isinstance(other, DNSName): |
| 831 | return NotImplemented |
| 832 | |
| 833 | return self.value == other.value |
| 834 | |
| 835 | def __ne__(self, other): |
| 836 | return not self == other |
| 837 | |
| 838 | |
| 839 | @utils.register_interface(GeneralName) |
| 840 | class UniformResourceIdentifier(object): |
| 841 | def __init__(self, value): |
| 842 | if not isinstance(value, six.text_type): |
| 843 | raise TypeError("value must be a unicode string") |
| 844 | |
| 845 | self._value = value |
| 846 | |
| 847 | value = utils.read_only_property("_value") |
| 848 | |
| 849 | def __repr__(self): |
| 850 | return "<UniformResourceIdentifier(value={0})>".format(self.value) |
| 851 | |
| 852 | def __eq__(self, other): |
| 853 | if not isinstance(other, UniformResourceIdentifier): |
| 854 | return NotImplemented |
| 855 | |
| 856 | return self.value == other.value |
| 857 | |
| 858 | def __ne__(self, other): |
| 859 | return not self == other |
| 860 | |
| 861 | |
| 862 | @utils.register_interface(GeneralName) |
| 863 | class DirectoryName(object): |
| 864 | def __init__(self, value): |
| 865 | if not isinstance(value, Name): |
| 866 | raise TypeError("value must be a Name") |
| 867 | |
| 868 | self._value = value |
| 869 | |
| 870 | value = utils.read_only_property("_value") |
| 871 | |
| 872 | def __repr__(self): |
| 873 | return "<DirectoryName(value={0})>".format(self.value) |
| 874 | |
| 875 | def __eq__(self, other): |
| 876 | if not isinstance(other, DirectoryName): |
| 877 | return NotImplemented |
| 878 | |
| 879 | return self.value == other.value |
| 880 | |
| 881 | def __ne__(self, other): |
| 882 | return not self == other |
| 883 | |
| 884 | |
| 885 | @utils.register_interface(GeneralName) |
| 886 | class RegisteredID(object): |
| 887 | def __init__(self, value): |
| 888 | if not isinstance(value, ObjectIdentifier): |
| 889 | raise TypeError("value must be an ObjectIdentifier") |
| 890 | |
| 891 | self._value = value |
| 892 | |
| 893 | value = utils.read_only_property("_value") |
| 894 | |
| 895 | def __repr__(self): |
| 896 | return "<RegisteredID(value={0})>".format(self.value) |
| 897 | |
| 898 | def __eq__(self, other): |
| 899 | if not isinstance(other, RegisteredID): |
| 900 | return NotImplemented |
| 901 | |
| 902 | return self.value == other.value |
| 903 | |
| 904 | def __ne__(self, other): |
| 905 | return not self == other |
| 906 | |
| 907 | |
| 908 | @utils.register_interface(GeneralName) |
| 909 | class IPAddress(object): |
| 910 | def __init__(self, value): |
| 911 | if not isinstance( |
| 912 | value, (ipaddress.IPv4Address, ipaddress.IPv6Address) |
| 913 | ): |
| 914 | raise TypeError( |
| 915 | "value must be an instance of ipaddress.IPv4Address or " |
| 916 | "ipaddress.IPv6Address" |
| 917 | ) |
| 918 | |
| 919 | self._value = value |
| 920 | |
| 921 | value = utils.read_only_property("_value") |
| 922 | |
| 923 | def __repr__(self): |
| 924 | return "<IPAddress(value={0})>".format(self.value) |
| 925 | |
| 926 | def __eq__(self, other): |
| 927 | if not isinstance(other, IPAddress): |
| 928 | return NotImplemented |
| 929 | |
| 930 | return self.value == other.value |
| 931 | |
| 932 | def __ne__(self, other): |
| 933 | return not self == other |
| 934 | |
| 935 | |
| 936 | class SubjectAlternativeName(object): |
| 937 | def __init__(self, general_names): |
Paul Kehrer | d04b39b | 2015-04-21 08:44:17 -0500 | [diff] [blame] | 938 | if not all(isinstance(x, GeneralName) for x in general_names): |
| 939 | raise TypeError( |
| 940 | "Every item in the general_names list must be an " |
| 941 | "object conforming to the GeneralName interface" |
| 942 | ) |
| 943 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 944 | self._general_names = general_names |
| 945 | |
| 946 | def __iter__(self): |
| 947 | return iter(self._general_names) |
| 948 | |
| 949 | def __len__(self): |
| 950 | return len(self._general_names) |
| 951 | |
| 952 | def get_values_for_type(self, type): |
| 953 | return [i.value for i in self if isinstance(i, type)] |
| 954 | |
| 955 | def __repr__(self): |
| 956 | return "<SubjectAlternativeName({0})>".format(self._general_names) |
| 957 | |
Paul Kehrer | 58cc397 | 2015-05-13 10:00:41 -0500 | [diff] [blame] | 958 | def __eq__(self, other): |
| 959 | if not isinstance(other, SubjectAlternativeName): |
| 960 | return NotImplemented |
| 961 | |
| 962 | return self._general_names == other._general_names |
| 963 | |
| 964 | def __ne__(self, other): |
| 965 | return not self == other |
| 966 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 967 | |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 968 | class AuthorityKeyIdentifier(object): |
| 969 | def __init__(self, key_identifier, authority_cert_issuer, |
| 970 | authority_cert_serial_number): |
| 971 | if authority_cert_issuer or authority_cert_serial_number: |
| 972 | if not authority_cert_issuer or not authority_cert_serial_number: |
| 973 | raise ValueError( |
| 974 | "authority_cert_issuer and authority_cert_serial_number " |
| 975 | "must both be present or both None" |
| 976 | ) |
| 977 | |
Paul Kehrer | c6e0f06 | 2015-05-03 11:04:34 -0500 | [diff] [blame] | 978 | if not all( |
| 979 | isinstance(x, GeneralName) for x in authority_cert_issuer |
| 980 | ): |
| 981 | raise TypeError( |
| 982 | "authority_cert_issuer must be a list of GeneralName " |
| 983 | "objects" |
| 984 | ) |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 985 | |
| 986 | if not isinstance(authority_cert_serial_number, six.integer_types): |
| 987 | raise TypeError( |
| 988 | "authority_cert_serial_number must be an integer" |
| 989 | ) |
| 990 | |
| 991 | self._key_identifier = key_identifier |
| 992 | self._authority_cert_issuer = authority_cert_issuer |
| 993 | self._authority_cert_serial_number = authority_cert_serial_number |
| 994 | |
| 995 | def __repr__(self): |
| 996 | return ( |
| 997 | "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, " |
| 998 | "authority_cert_issuer={0.authority_cert_issuer}, " |
| 999 | "authority_cert_serial_number={0.authority_cert_serial_number}" |
| 1000 | ")>".format(self) |
| 1001 | ) |
| 1002 | |
Paul Kehrer | 2ca2eb3 | 2015-05-03 10:04:57 -0500 | [diff] [blame] | 1003 | def __eq__(self, other): |
| 1004 | if not isinstance(other, AuthorityKeyIdentifier): |
| 1005 | return NotImplemented |
| 1006 | |
| 1007 | return ( |
| 1008 | self.key_identifier == other.key_identifier and |
| 1009 | self.authority_cert_issuer == other.authority_cert_issuer and |
| 1010 | self.authority_cert_serial_number == |
| 1011 | other.authority_cert_serial_number |
| 1012 | ) |
| 1013 | |
| 1014 | def __ne__(self, other): |
| 1015 | return not self == other |
| 1016 | |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 1017 | key_identifier = utils.read_only_property("_key_identifier") |
| 1018 | authority_cert_issuer = utils.read_only_property("_authority_cert_issuer") |
| 1019 | authority_cert_serial_number = utils.read_only_property( |
| 1020 | "_authority_cert_serial_number" |
| 1021 | ) |
| 1022 | |
| 1023 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 1024 | OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") |
| 1025 | OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6") |
| 1026 | OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7") |
| 1027 | OID_STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8") |
| 1028 | OID_ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10") |
| 1029 | OID_ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11") |
| 1030 | OID_SERIAL_NUMBER = ObjectIdentifier("2.5.4.5") |
| 1031 | OID_SURNAME = ObjectIdentifier("2.5.4.4") |
| 1032 | OID_GIVEN_NAME = ObjectIdentifier("2.5.4.42") |
| 1033 | OID_TITLE = ObjectIdentifier("2.5.4.12") |
| 1034 | OID_GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44") |
| 1035 | OID_DN_QUALIFIER = ObjectIdentifier("2.5.4.46") |
| 1036 | OID_PSEUDONYM = ObjectIdentifier("2.5.4.65") |
| 1037 | OID_DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25") |
| 1038 | OID_EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 1039 | |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 1040 | OID_RSA_WITH_MD5 = ObjectIdentifier("1.2.840.113549.1.1.4") |
| 1041 | OID_RSA_WITH_SHA1 = ObjectIdentifier("1.2.840.113549.1.1.5") |
| 1042 | OID_RSA_WITH_SHA224 = ObjectIdentifier("1.2.840.113549.1.1.14") |
| 1043 | OID_RSA_WITH_SHA256 = ObjectIdentifier("1.2.840.113549.1.1.11") |
| 1044 | OID_RSA_WITH_SHA384 = ObjectIdentifier("1.2.840.113549.1.1.12") |
| 1045 | OID_RSA_WITH_SHA512 = ObjectIdentifier("1.2.840.113549.1.1.13") |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1046 | OID_ECDSA_WITH_SHA224 = ObjectIdentifier("1.2.840.10045.4.3.1") |
| 1047 | OID_ECDSA_WITH_SHA256 = ObjectIdentifier("1.2.840.10045.4.3.2") |
| 1048 | OID_ECDSA_WITH_SHA384 = ObjectIdentifier("1.2.840.10045.4.3.3") |
| 1049 | OID_ECDSA_WITH_SHA512 = ObjectIdentifier("1.2.840.10045.4.3.4") |
| 1050 | OID_DSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10040.4.3") |
| 1051 | OID_DSA_WITH_SHA224 = ObjectIdentifier("2.16.840.1.101.3.4.3.1") |
| 1052 | OID_DSA_WITH_SHA256 = ObjectIdentifier("2.16.840.1.101.3.4.3.2") |
| 1053 | |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1054 | _SIG_OIDS_TO_HASH = { |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 1055 | OID_RSA_WITH_MD5.dotted_string: hashes.MD5(), |
| 1056 | OID_RSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 1057 | OID_RSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1058 | OID_RSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 1059 | OID_RSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 1060 | OID_RSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
Paul Kehrer | 42a87cb | 2015-02-13 18:53:24 -0600 | [diff] [blame] | 1061 | OID_ECDSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1062 | OID_ECDSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 1063 | OID_ECDSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 1064 | OID_ECDSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
| 1065 | OID_DSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 1066 | OID_DSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1067 | OID_DSA_WITH_SHA256.dotted_string: hashes.SHA256() |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1068 | } |
| 1069 | |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 1070 | OID_SERVER_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.1") |
| 1071 | OID_CLIENT_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.2") |
| 1072 | OID_CODE_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.3") |
| 1073 | OID_EMAIL_PROTECTION = ObjectIdentifier("1.3.6.1.5.5.7.3.4") |
| 1074 | OID_TIME_STAMPING = ObjectIdentifier("1.3.6.1.5.5.7.3.8") |
| 1075 | OID_OCSP_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.9") |
| 1076 | |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 1077 | OID_CA_ISSUERS = ObjectIdentifier("1.3.6.1.5.5.7.48.2") |
| 1078 | OID_OCSP = ObjectIdentifier("1.3.6.1.5.5.7.48.1") |
| 1079 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 1080 | OID_CPS_QUALIFIER = ObjectIdentifier("1.3.6.1.5.5.7.2.1") |
| 1081 | OID_CPS_USER_NOTICE = ObjectIdentifier("1.3.6.1.5.5.7.2.2") |
| 1082 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 1083 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 1084 | @six.add_metaclass(abc.ABCMeta) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 1085 | class Certificate(object): |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 1086 | @abc.abstractmethod |
| 1087 | def fingerprint(self, algorithm): |
| 1088 | """ |
| 1089 | Returns bytes using digest passed. |
| 1090 | """ |
| 1091 | |
| 1092 | @abc.abstractproperty |
| 1093 | def serial(self): |
| 1094 | """ |
| 1095 | Returns certificate serial number |
| 1096 | """ |
| 1097 | |
| 1098 | @abc.abstractproperty |
| 1099 | def version(self): |
| 1100 | """ |
| 1101 | Returns the certificate version |
| 1102 | """ |
| 1103 | |
| 1104 | @abc.abstractmethod |
| 1105 | def public_key(self): |
| 1106 | """ |
| 1107 | Returns the public key |
| 1108 | """ |
| 1109 | |
| 1110 | @abc.abstractproperty |
| 1111 | def not_valid_before(self): |
| 1112 | """ |
| 1113 | Not before time (represented as UTC datetime) |
| 1114 | """ |
| 1115 | |
| 1116 | @abc.abstractproperty |
| 1117 | def not_valid_after(self): |
| 1118 | """ |
| 1119 | Not after time (represented as UTC datetime) |
| 1120 | """ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 1121 | |
| 1122 | @abc.abstractproperty |
| 1123 | def issuer(self): |
| 1124 | """ |
| 1125 | Returns the issuer name object. |
| 1126 | """ |
| 1127 | |
| 1128 | @abc.abstractproperty |
| 1129 | def subject(self): |
| 1130 | """ |
| 1131 | Returns the subject name object. |
| 1132 | """ |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1133 | |
| 1134 | @abc.abstractproperty |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1135 | def signature_hash_algorithm(self): |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1136 | """ |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1137 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 1138 | in the certificate. |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1139 | """ |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1140 | |
Paul Kehrer | 8c234d1 | 2015-05-15 09:27:22 -0700 | [diff] [blame] | 1141 | @abc.abstractproperty |
| 1142 | def extensions(self): |
| 1143 | """ |
| 1144 | Returns an Extensions object. |
| 1145 | """ |
| 1146 | |
Paul Kehrer | 8bbdc6f | 2015-04-30 16:47:16 -0500 | [diff] [blame] | 1147 | @abc.abstractmethod |
| 1148 | def __eq__(self, other): |
| 1149 | """ |
| 1150 | Checks equality. |
| 1151 | """ |
| 1152 | |
| 1153 | @abc.abstractmethod |
| 1154 | def __ne__(self, other): |
| 1155 | """ |
| 1156 | Checks not equal. |
| 1157 | """ |
| 1158 | |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1159 | |
| 1160 | @six.add_metaclass(abc.ABCMeta) |
Paul Kehrer | a1a1f23 | 2015-03-15 15:34:35 -0500 | [diff] [blame] | 1161 | class CertificateSigningRequest(object): |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1162 | @abc.abstractmethod |
| 1163 | def public_key(self): |
| 1164 | """ |
| 1165 | Returns the public key |
| 1166 | """ |
| 1167 | |
| 1168 | @abc.abstractproperty |
| 1169 | def subject(self): |
| 1170 | """ |
| 1171 | Returns the subject name object. |
| 1172 | """ |
| 1173 | |
| 1174 | @abc.abstractproperty |
| 1175 | def signature_hash_algorithm(self): |
| 1176 | """ |
| 1177 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 1178 | in the certificate. |
| 1179 | """ |
Andre Caron | 6e721a9 | 2015-05-17 15:08:48 -0400 | [diff] [blame^] | 1180 | |
| 1181 | @abc.abstractproperty |
| 1182 | def extensions(self): |
| 1183 | """ |
| 1184 | Returns the extensions in the signing request. |
| 1185 | """ |