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 |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 8 | import datetime |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 9 | import ipaddress |
Paul Kehrer | 01d5d0b | 2015-07-12 09:41:21 -0500 | [diff] [blame] | 10 | from email.utils import parseaddr |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 11 | from enum import Enum |
| 12 | |
Paul Kehrer | 01d5d0b | 2015-07-12 09:41:21 -0500 | [diff] [blame] | 13 | import idna |
| 14 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 15 | import six |
| 16 | |
Paul Kehrer | e28d6c4 | 2015-07-12 14:59:37 -0500 | [diff] [blame] | 17 | from six.moves import urllib_parse |
| 18 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 19 | from cryptography import utils |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 20 | from cryptography.hazmat.primitives import hashes |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 21 | from cryptography.hazmat.primitives.asymmetric import dsa, ec, rsa |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 22 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 23 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 24 | _OID_NAMES = { |
| 25 | "2.5.4.3": "commonName", |
| 26 | "2.5.4.6": "countryName", |
| 27 | "2.5.4.7": "localityName", |
| 28 | "2.5.4.8": "stateOrProvinceName", |
| 29 | "2.5.4.10": "organizationName", |
| 30 | "2.5.4.11": "organizationalUnitName", |
| 31 | "2.5.4.5": "serialNumber", |
| 32 | "2.5.4.4": "surname", |
| 33 | "2.5.4.42": "givenName", |
| 34 | "2.5.4.12": "title", |
| 35 | "2.5.4.44": "generationQualifier", |
| 36 | "2.5.4.46": "dnQualifier", |
| 37 | "2.5.4.65": "pseudonym", |
| 38 | "0.9.2342.19200300.100.1.25": "domainComponent", |
| 39 | "1.2.840.113549.1.9.1": "emailAddress", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 40 | "1.2.840.113549.1.1.4": "md5WithRSAEncryption", |
| 41 | "1.2.840.113549.1.1.5": "sha1WithRSAEncryption", |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 42 | "1.2.840.113549.1.1.14": "sha224WithRSAEncryption", |
| 43 | "1.2.840.113549.1.1.11": "sha256WithRSAEncryption", |
| 44 | "1.2.840.113549.1.1.12": "sha384WithRSAEncryption", |
| 45 | "1.2.840.113549.1.1.13": "sha512WithRSAEncryption", |
Alex Gaynor | 3aadabf | 2015-06-23 22:06:21 -0400 | [diff] [blame] | 46 | "1.2.840.10045.4.1": "ecdsa-with-SHA1", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 47 | "1.2.840.10045.4.3.1": "ecdsa-with-SHA224", |
| 48 | "1.2.840.10045.4.3.2": "ecdsa-with-SHA256", |
| 49 | "1.2.840.10045.4.3.3": "ecdsa-with-SHA384", |
| 50 | "1.2.840.10045.4.3.4": "ecdsa-with-SHA512", |
| 51 | "1.2.840.10040.4.3": "dsa-with-sha1", |
| 52 | "2.16.840.1.101.3.4.3.1": "dsa-with-sha224", |
| 53 | "2.16.840.1.101.3.4.3.2": "dsa-with-sha256", |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 54 | "1.3.6.1.5.5.7.3.1": "serverAuth", |
| 55 | "1.3.6.1.5.5.7.3.2": "clientAuth", |
| 56 | "1.3.6.1.5.5.7.3.3": "codeSigning", |
| 57 | "1.3.6.1.5.5.7.3.4": "emailProtection", |
| 58 | "1.3.6.1.5.5.7.3.8": "timeStamping", |
| 59 | "1.3.6.1.5.5.7.3.9": "OCSPSigning", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 60 | "2.5.29.9": "subjectDirectoryAttributes", |
| 61 | "2.5.29.14": "subjectKeyIdentifier", |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 62 | "2.5.29.15": "keyUsage", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 63 | "2.5.29.17": "subjectAltName", |
| 64 | "2.5.29.18": "issuerAltName", |
| 65 | "2.5.29.19": "basicConstraints", |
Erik Trauschke | c5a8d17 | 2015-05-28 10:24:25 -0700 | [diff] [blame] | 66 | "2.5.29.21": "cRLReason", |
| 67 | "2.5.29.24": "invalidityDate", |
| 68 | "2.5.29.29": "certificateIssuer", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 69 | "2.5.29.30": "nameConstraints", |
| 70 | "2.5.29.31": "cRLDistributionPoints", |
| 71 | "2.5.29.32": "certificatePolicies", |
| 72 | "2.5.29.33": "policyMappings", |
| 73 | "2.5.29.35": "authorityKeyIdentifier", |
| 74 | "2.5.29.36": "policyConstraints", |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 75 | "2.5.29.37": "extendedKeyUsage", |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 76 | "2.5.29.46": "freshestCRL", |
| 77 | "2.5.29.54": "inhibitAnyPolicy", |
| 78 | "1.3.6.1.5.5.7.1.1": "authorityInfoAccess", |
| 79 | "1.3.6.1.5.5.7.1.11": "subjectInfoAccess", |
| 80 | "1.3.6.1.5.5.7.48.1.5": "OCSPNoCheck", |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 81 | "1.3.6.1.5.5.7.48.1": "OCSP", |
Paul Kehrer | f506bca | 2015-05-02 22:31:47 -0500 | [diff] [blame] | 82 | "1.3.6.1.5.5.7.48.2": "caIssuers", |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 83 | "1.3.6.1.5.5.7.2.1": "id-qt-cps", |
| 84 | "1.3.6.1.5.5.7.2.2": "id-qt-unotice", |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 88 | _GENERAL_NAMES = { |
| 89 | 0: "otherName", |
| 90 | 1: "rfc822Name", |
| 91 | 2: "dNSName", |
| 92 | 3: "x400Address", |
| 93 | 4: "directoryName", |
| 94 | 5: "ediPartyName", |
| 95 | 6: "uniformResourceIdentifier", |
| 96 | 7: "iPAddress", |
| 97 | 8: "registeredID", |
| 98 | } |
| 99 | |
| 100 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 101 | class Version(Enum): |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 102 | v1 = 0 |
| 103 | v3 = 2 |
| 104 | |
| 105 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 106 | def load_pem_x509_certificate(data, backend): |
| 107 | return backend.load_pem_x509_certificate(data) |
| 108 | |
| 109 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 110 | def load_der_x509_certificate(data, backend): |
| 111 | return backend.load_der_x509_certificate(data) |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 112 | |
| 113 | |
Paul Kehrer | 31e3988 | 2015-03-11 11:37:04 -0500 | [diff] [blame] | 114 | def load_pem_x509_csr(data, backend): |
| 115 | return backend.load_pem_x509_csr(data) |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 116 | |
| 117 | |
Paul Kehrer | 1effb6e | 2015-03-30 15:05:59 -0500 | [diff] [blame] | 118 | def load_der_x509_csr(data, backend): |
| 119 | return backend.load_der_x509_csr(data) |
| 120 | |
| 121 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 122 | class InvalidVersion(Exception): |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 123 | def __init__(self, msg, parsed_version): |
| 124 | super(InvalidVersion, self).__init__(msg) |
| 125 | self.parsed_version = parsed_version |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 126 | |
| 127 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 128 | class DuplicateExtension(Exception): |
| 129 | def __init__(self, msg, oid): |
| 130 | super(DuplicateExtension, self).__init__(msg) |
| 131 | self.oid = oid |
| 132 | |
| 133 | |
| 134 | class UnsupportedExtension(Exception): |
| 135 | def __init__(self, msg, oid): |
| 136 | super(UnsupportedExtension, self).__init__(msg) |
| 137 | self.oid = oid |
| 138 | |
| 139 | |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 140 | class ExtensionNotFound(Exception): |
| 141 | def __init__(self, msg, oid): |
| 142 | super(ExtensionNotFound, self).__init__(msg) |
| 143 | self.oid = oid |
| 144 | |
| 145 | |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 146 | class UnsupportedGeneralNameType(Exception): |
Paul Kehrer | bed0735 | 2015-04-21 08:31:10 -0500 | [diff] [blame] | 147 | def __init__(self, msg, type): |
| 148 | super(UnsupportedGeneralNameType, self).__init__(msg) |
| 149 | self.type = type |
Paul Kehrer | 9089c91 | 2015-04-20 22:15:20 -0500 | [diff] [blame] | 150 | |
| 151 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 152 | class NameAttribute(object): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 153 | def __init__(self, oid, value): |
| 154 | if not isinstance(oid, ObjectIdentifier): |
Paul Kehrer | 858b9b7 | 2015-02-05 09:50:31 -0600 | [diff] [blame] | 155 | raise TypeError( |
| 156 | "oid argument must be an ObjectIdentifier instance." |
| 157 | ) |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 158 | |
Ian Cordasco | 7618fbe | 2015-06-16 19:12:17 -0500 | [diff] [blame] | 159 | if not isinstance(value, six.text_type): |
| 160 | raise TypeError( |
| 161 | "value argument must be a text type." |
| 162 | ) |
| 163 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 164 | self._oid = oid |
| 165 | self._value = value |
| 166 | |
| 167 | oid = utils.read_only_property("_oid") |
| 168 | value = utils.read_only_property("_value") |
| 169 | |
| 170 | def __eq__(self, other): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 171 | if not isinstance(other, NameAttribute): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 172 | return NotImplemented |
| 173 | |
| 174 | return ( |
| 175 | self.oid == other.oid and |
| 176 | self.value == other.value |
| 177 | ) |
| 178 | |
| 179 | def __ne__(self, other): |
| 180 | return not self == other |
| 181 | |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 182 | def __repr__(self): |
Alex Gaynor | d6b63da | 2015-03-23 00:25:44 -0400 | [diff] [blame] | 183 | return "<NameAttribute(oid={0.oid}, value={0.value!r})>".format(self) |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 184 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 185 | |
| 186 | class ObjectIdentifier(object): |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 187 | def __init__(self, dotted_string): |
| 188 | self._dotted_string = dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 189 | |
| 190 | def __eq__(self, other): |
| 191 | if not isinstance(other, ObjectIdentifier): |
| 192 | return NotImplemented |
| 193 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 194 | return self._dotted_string == other._dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 195 | |
| 196 | def __ne__(self, other): |
| 197 | return not self == other |
| 198 | |
| 199 | def __repr__(self): |
| 200 | return "<ObjectIdentifier(oid={0}, name={1})>".format( |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 201 | self._dotted_string, |
| 202 | _OID_NAMES.get(self._dotted_string, "Unknown OID") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 203 | ) |
| 204 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 205 | def __hash__(self): |
| 206 | return hash(self.dotted_string) |
| 207 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 208 | dotted_string = utils.read_only_property("_dotted_string") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 209 | |
| 210 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 211 | class Name(object): |
| 212 | def __init__(self, attributes): |
| 213 | self._attributes = attributes |
| 214 | |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 215 | def get_attributes_for_oid(self, oid): |
Alex Gaynor | f957423 | 2015-02-19 13:56:50 -0800 | [diff] [blame] | 216 | return [i for i in self if i.oid == oid] |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 217 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 218 | def __eq__(self, other): |
| 219 | if not isinstance(other, Name): |
| 220 | return NotImplemented |
| 221 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 222 | return self._attributes == other._attributes |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 223 | |
| 224 | def __ne__(self, other): |
| 225 | return not self == other |
| 226 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 227 | def __iter__(self): |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 228 | return iter(self._attributes) |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 229 | |
| 230 | def __len__(self): |
| 231 | return len(self._attributes) |
| 232 | |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 233 | def __repr__(self): |
Paul Kehrer | f4ed10a | 2015-04-11 15:53:12 -0400 | [diff] [blame] | 234 | return "<Name({0!r})>".format(self._attributes) |
Paul Kehrer | 1fb35c9 | 2015-04-11 15:42:54 -0400 | [diff] [blame] | 235 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 236 | |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 237 | OID_SUBJECT_DIRECTORY_ATTRIBUTES = ObjectIdentifier("2.5.29.9") |
| 238 | OID_SUBJECT_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.14") |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 239 | OID_KEY_USAGE = ObjectIdentifier("2.5.29.15") |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 240 | OID_SUBJECT_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.17") |
| 241 | OID_ISSUER_ALTERNATIVE_NAME = ObjectIdentifier("2.5.29.18") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 242 | OID_BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19") |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 243 | OID_CRL_REASON = ObjectIdentifier("2.5.29.21") |
| 244 | OID_INVALIDITY_DATE = ObjectIdentifier("2.5.29.24") |
| 245 | OID_CERTIFICATE_ISSUER = ObjectIdentifier("2.5.29.29") |
Paul Kehrer | d08b549 | 2015-04-04 15:19:16 -0500 | [diff] [blame] | 246 | OID_NAME_CONSTRAINTS = ObjectIdentifier("2.5.29.30") |
| 247 | OID_CRL_DISTRIBUTION_POINTS = ObjectIdentifier("2.5.29.31") |
| 248 | OID_CERTIFICATE_POLICIES = ObjectIdentifier("2.5.29.32") |
| 249 | OID_POLICY_MAPPINGS = ObjectIdentifier("2.5.29.33") |
| 250 | OID_AUTHORITY_KEY_IDENTIFIER = ObjectIdentifier("2.5.29.35") |
| 251 | OID_POLICY_CONSTRAINTS = ObjectIdentifier("2.5.29.36") |
| 252 | OID_EXTENDED_KEY_USAGE = ObjectIdentifier("2.5.29.37") |
| 253 | OID_FRESHEST_CRL = ObjectIdentifier("2.5.29.46") |
| 254 | OID_INHIBIT_ANY_POLICY = ObjectIdentifier("2.5.29.54") |
| 255 | OID_AUTHORITY_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.1") |
| 256 | OID_SUBJECT_INFORMATION_ACCESS = ObjectIdentifier("1.3.6.1.5.5.7.1.11") |
| 257 | 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] | 258 | |
| 259 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 260 | class Extensions(object): |
| 261 | def __init__(self, extensions): |
| 262 | self._extensions = extensions |
| 263 | |
Paul Kehrer | fa56a23 | 2015-03-17 13:14:03 -0500 | [diff] [blame] | 264 | def get_extension_for_oid(self, oid): |
| 265 | for ext in self: |
| 266 | if ext.oid == oid: |
| 267 | return ext |
| 268 | |
| 269 | raise ExtensionNotFound("No {0} extension was found".format(oid), oid) |
| 270 | |
Paul Kehrer | fbb7ac8 | 2015-03-16 19:26:29 -0500 | [diff] [blame] | 271 | def __iter__(self): |
| 272 | return iter(self._extensions) |
| 273 | |
| 274 | def __len__(self): |
| 275 | return len(self._extensions) |
| 276 | |
| 277 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 278 | class Extension(object): |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 279 | def __init__(self, oid, critical, value): |
| 280 | if not isinstance(oid, ObjectIdentifier): |
| 281 | raise TypeError( |
| 282 | "oid argument must be an ObjectIdentifier instance." |
| 283 | ) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 284 | |
| 285 | if not isinstance(critical, bool): |
| 286 | raise TypeError("critical must be a boolean value") |
| 287 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 288 | self._oid = oid |
| 289 | self._critical = critical |
| 290 | self._value = value |
| 291 | |
| 292 | oid = utils.read_only_property("_oid") |
| 293 | critical = utils.read_only_property("_critical") |
| 294 | value = utils.read_only_property("_value") |
| 295 | |
| 296 | def __repr__(self): |
Paul Kehrer | 58b7569 | 2015-03-22 23:24:58 -0500 | [diff] [blame] | 297 | return ("<Extension(oid={0.oid}, critical={0.critical}, " |
| 298 | "value={0.value})>").format(self) |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 299 | |
Paul Kehrer | 58e870c | 2015-05-17 09:15:30 -0700 | [diff] [blame] | 300 | def __eq__(self, other): |
| 301 | if not isinstance(other, Extension): |
| 302 | return NotImplemented |
| 303 | |
| 304 | return ( |
| 305 | self.oid == other.oid and |
| 306 | self.critical == other.critical and |
| 307 | self.value == other.value |
| 308 | ) |
| 309 | |
| 310 | def __ne__(self, other): |
| 311 | return not self == other |
| 312 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 313 | |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 314 | class ExtendedKeyUsage(object): |
| 315 | def __init__(self, usages): |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 316 | if not all(isinstance(x, ObjectIdentifier) for x in usages): |
| 317 | raise TypeError( |
| 318 | "Every item in the usages list must be an ObjectIdentifier" |
| 319 | ) |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 320 | |
| 321 | self._usages = usages |
| 322 | |
| 323 | def __iter__(self): |
| 324 | return iter(self._usages) |
| 325 | |
| 326 | def __len__(self): |
| 327 | return len(self._usages) |
| 328 | |
Paul Kehrer | 23d10c3 | 2015-04-02 23:12:32 -0500 | [diff] [blame] | 329 | def __repr__(self): |
| 330 | return "<ExtendedKeyUsage({0})>".format(self._usages) |
| 331 | |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 332 | def __eq__(self, other): |
| 333 | if not isinstance(other, ExtendedKeyUsage): |
| 334 | return NotImplemented |
| 335 | |
Paul Kehrer | f24bad7 | 2015-05-02 19:36:20 -0500 | [diff] [blame] | 336 | return self._usages == other._usages |
Paul Kehrer | b047617 | 2015-05-02 19:34:51 -0500 | [diff] [blame] | 337 | |
| 338 | def __ne__(self, other): |
| 339 | return not self == other |
| 340 | |
Paul Kehrer | ffa2a15 | 2015-03-31 08:18:25 -0500 | [diff] [blame] | 341 | |
Paul Kehrer | 4a1038e | 2015-05-18 10:28:31 -0700 | [diff] [blame] | 342 | class OCSPNoCheck(object): |
| 343 | pass |
| 344 | |
| 345 | |
Paul Kehrer | 8589466 | 2015-03-22 13:19:31 -0500 | [diff] [blame] | 346 | class BasicConstraints(object): |
| 347 | def __init__(self, ca, path_length): |
| 348 | if not isinstance(ca, bool): |
| 349 | raise TypeError("ca must be a boolean value") |
| 350 | |
Paul Kehrer | 611d3d3 | 2015-03-22 13:31:18 -0500 | [diff] [blame] | 351 | if path_length is not None and not ca: |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 352 | raise ValueError("path_length must be None when ca is False") |
| 353 | |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 354 | if ( |
Paul Kehrer | 5553d57 | 2015-03-23 21:08:01 -0500 | [diff] [blame] | 355 | path_length is not None and |
| 356 | (not isinstance(path_length, six.integer_types) or path_length < 0) |
Paul Kehrer | a5c6e9a | 2015-03-23 19:23:43 -0500 | [diff] [blame] | 357 | ): |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 358 | raise TypeError( |
| 359 | "path_length must be a non-negative integer or None" |
| 360 | ) |
| 361 | |
| 362 | self._ca = ca |
| 363 | self._path_length = path_length |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 364 | |
| 365 | ca = utils.read_only_property("_ca") |
| 366 | path_length = utils.read_only_property("_path_length") |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 367 | |
| 368 | def __repr__(self): |
Paul Kehrer | 58b7569 | 2015-03-22 23:24:58 -0500 | [diff] [blame] | 369 | return ("<BasicConstraints(ca={0.ca}, " |
| 370 | "path_length={0.path_length})>").format(self) |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 371 | |
Paul Kehrer | 3a69b13 | 2015-05-13 10:03:46 -0500 | [diff] [blame] | 372 | def __eq__(self, other): |
| 373 | if not isinstance(other, BasicConstraints): |
| 374 | return NotImplemented |
| 375 | |
| 376 | return self.ca == other.ca and self.path_length == other.path_length |
| 377 | |
| 378 | def __ne__(self, other): |
| 379 | return not self == other |
| 380 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame] | 381 | |
Paul Kehrer | cecbbba | 2015-03-30 14:58:38 -0500 | [diff] [blame] | 382 | class KeyUsage(object): |
| 383 | def __init__(self, digital_signature, content_commitment, key_encipherment, |
| 384 | data_encipherment, key_agreement, key_cert_sign, crl_sign, |
| 385 | encipher_only, decipher_only): |
| 386 | if not key_agreement and (encipher_only or decipher_only): |
| 387 | raise ValueError( |
| 388 | "encipher_only and decipher_only can only be true when " |
| 389 | "key_agreement is true" |
| 390 | ) |
| 391 | |
| 392 | self._digital_signature = digital_signature |
| 393 | self._content_commitment = content_commitment |
| 394 | self._key_encipherment = key_encipherment |
| 395 | self._data_encipherment = data_encipherment |
| 396 | self._key_agreement = key_agreement |
| 397 | self._key_cert_sign = key_cert_sign |
| 398 | self._crl_sign = crl_sign |
| 399 | self._encipher_only = encipher_only |
| 400 | self._decipher_only = decipher_only |
| 401 | |
| 402 | digital_signature = utils.read_only_property("_digital_signature") |
| 403 | content_commitment = utils.read_only_property("_content_commitment") |
| 404 | key_encipherment = utils.read_only_property("_key_encipherment") |
| 405 | data_encipherment = utils.read_only_property("_data_encipherment") |
| 406 | key_agreement = utils.read_only_property("_key_agreement") |
| 407 | key_cert_sign = utils.read_only_property("_key_cert_sign") |
| 408 | crl_sign = utils.read_only_property("_crl_sign") |
| 409 | |
| 410 | @property |
| 411 | def encipher_only(self): |
| 412 | if not self.key_agreement: |
| 413 | raise ValueError( |
| 414 | "encipher_only is undefined unless key_agreement is true" |
| 415 | ) |
| 416 | else: |
| 417 | return self._encipher_only |
| 418 | |
| 419 | @property |
| 420 | def decipher_only(self): |
| 421 | if not self.key_agreement: |
| 422 | raise ValueError( |
| 423 | "decipher_only is undefined unless key_agreement is true" |
| 424 | ) |
| 425 | else: |
| 426 | return self._decipher_only |
| 427 | |
Paul Kehrer | ac3e5bb | 2015-04-02 23:07:10 -0500 | [diff] [blame] | 428 | def __repr__(self): |
| 429 | try: |
| 430 | encipher_only = self.encipher_only |
| 431 | decipher_only = self.decipher_only |
| 432 | except ValueError: |
Paul Kehrer | b372e67 | 2015-04-15 11:05:24 -0400 | [diff] [blame] | 433 | encipher_only = None |
| 434 | decipher_only = None |
Paul Kehrer | ac3e5bb | 2015-04-02 23:07:10 -0500 | [diff] [blame] | 435 | |
| 436 | return ("<KeyUsage(digital_signature={0.digital_signature}, " |
| 437 | "content_commitment={0.content_commitment}, " |
| 438 | "key_encipherment={0.key_encipherment}, " |
| 439 | "data_encipherment={0.data_encipherment}, " |
| 440 | "key_agreement={0.key_agreement}, " |
| 441 | "key_cert_sign={0.key_cert_sign}, crl_sign={0.crl_sign}, " |
| 442 | "encipher_only={1}, decipher_only={2})>").format( |
| 443 | self, encipher_only, decipher_only) |
| 444 | |
Paul Kehrer | 8565f5e | 2015-05-13 09:57:09 -0500 | [diff] [blame] | 445 | def __eq__(self, other): |
| 446 | if not isinstance(other, KeyUsage): |
| 447 | return NotImplemented |
| 448 | |
| 449 | return ( |
| 450 | self.digital_signature == other.digital_signature and |
| 451 | self.content_commitment == other.content_commitment and |
| 452 | self.key_encipherment == other.key_encipherment and |
| 453 | self.data_encipherment == other.data_encipherment and |
| 454 | self.key_agreement == other.key_agreement and |
| 455 | self.key_cert_sign == other.key_cert_sign and |
| 456 | self.crl_sign == other.crl_sign and |
| 457 | self._encipher_only == other._encipher_only and |
| 458 | self._decipher_only == other._decipher_only |
| 459 | ) |
| 460 | |
| 461 | def __ne__(self, other): |
| 462 | return not self == other |
| 463 | |
Paul Kehrer | cecbbba | 2015-03-30 14:58:38 -0500 | [diff] [blame] | 464 | |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 465 | class AuthorityInformationAccess(object): |
| 466 | def __init__(self, descriptions): |
| 467 | if not all(isinstance(x, AccessDescription) for x in descriptions): |
| 468 | raise TypeError( |
| 469 | "Every item in the descriptions list must be an " |
| 470 | "AccessDescription" |
| 471 | ) |
| 472 | |
| 473 | self._descriptions = descriptions |
| 474 | |
| 475 | def __iter__(self): |
| 476 | return iter(self._descriptions) |
| 477 | |
| 478 | def __len__(self): |
| 479 | return len(self._descriptions) |
| 480 | |
| 481 | def __repr__(self): |
| 482 | return "<AuthorityInformationAccess({0})>".format(self._descriptions) |
| 483 | |
| 484 | def __eq__(self, other): |
| 485 | if not isinstance(other, AuthorityInformationAccess): |
| 486 | return NotImplemented |
| 487 | |
| 488 | return self._descriptions == other._descriptions |
| 489 | |
| 490 | def __ne__(self, other): |
| 491 | return not self == other |
| 492 | |
| 493 | |
| 494 | class AccessDescription(object): |
| 495 | def __init__(self, access_method, access_location): |
| 496 | if not (access_method == OID_OCSP or access_method == OID_CA_ISSUERS): |
Paul Kehrer | f506bca | 2015-05-02 22:31:47 -0500 | [diff] [blame] | 497 | raise ValueError( |
| 498 | "access_method must be OID_OCSP or OID_CA_ISSUERS" |
| 499 | ) |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 500 | |
| 501 | if not isinstance(access_location, GeneralName): |
| 502 | raise TypeError("access_location must be a GeneralName") |
| 503 | |
| 504 | self._access_method = access_method |
| 505 | self._access_location = access_location |
| 506 | |
| 507 | def __repr__(self): |
| 508 | return ( |
| 509 | "<AccessDescription(access_method={0.access_method}, access_locati" |
| 510 | "on={0.access_location})>".format(self) |
| 511 | ) |
| 512 | |
| 513 | def __eq__(self, other): |
| 514 | if not isinstance(other, AccessDescription): |
| 515 | return NotImplemented |
| 516 | |
| 517 | return ( |
| 518 | self.access_method == other.access_method and |
| 519 | self.access_location == other.access_location |
| 520 | ) |
| 521 | |
| 522 | def __ne__(self, other): |
| 523 | return not self == other |
| 524 | |
| 525 | access_method = utils.read_only_property("_access_method") |
| 526 | access_location = utils.read_only_property("_access_location") |
| 527 | |
| 528 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 529 | class CertificatePolicies(object): |
| 530 | def __init__(self, policies): |
Paul Kehrer | f61ec74 | 2015-04-18 20:42:39 -0500 | [diff] [blame] | 531 | if not all(isinstance(x, PolicyInformation) for x in policies): |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 532 | raise TypeError( |
| 533 | "Every item in the policies list must be a " |
| 534 | "PolicyInformation" |
| 535 | ) |
| 536 | |
| 537 | self._policies = policies |
| 538 | |
| 539 | def __iter__(self): |
| 540 | return iter(self._policies) |
| 541 | |
| 542 | def __len__(self): |
| 543 | return len(self._policies) |
| 544 | |
| 545 | def __repr__(self): |
| 546 | return "<CertificatePolicies({0})>".format(self._policies) |
| 547 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 548 | def __eq__(self, other): |
| 549 | if not isinstance(other, CertificatePolicies): |
| 550 | return NotImplemented |
| 551 | |
| 552 | return self._policies == other._policies |
| 553 | |
| 554 | def __ne__(self, other): |
| 555 | return not self == other |
| 556 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 557 | |
| 558 | class PolicyInformation(object): |
| 559 | def __init__(self, policy_identifier, policy_qualifiers): |
| 560 | if not isinstance(policy_identifier, ObjectIdentifier): |
| 561 | raise TypeError("policy_identifier must be an ObjectIdentifier") |
| 562 | |
| 563 | self._policy_identifier = policy_identifier |
| 564 | if policy_qualifiers and not all( |
Paul Kehrer | ba35b3b | 2015-05-10 13:07:59 -0500 | [diff] [blame] | 565 | isinstance( |
| 566 | x, (six.text_type, UserNotice) |
| 567 | ) for x in policy_qualifiers |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 568 | ): |
| 569 | raise TypeError( |
Paul Kehrer | ba35b3b | 2015-05-10 13:07:59 -0500 | [diff] [blame] | 570 | "policy_qualifiers must be a list of strings and/or UserNotice" |
| 571 | " objects or None" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 572 | ) |
| 573 | |
| 574 | self._policy_qualifiers = policy_qualifiers |
| 575 | |
| 576 | def __repr__(self): |
| 577 | return ( |
| 578 | "<PolicyInformation(policy_identifier={0.policy_identifier}, polic" |
| 579 | "y_qualifiers={0.policy_qualifiers})>".format(self) |
| 580 | ) |
| 581 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 582 | def __eq__(self, other): |
| 583 | if not isinstance(other, PolicyInformation): |
| 584 | return NotImplemented |
| 585 | |
| 586 | return ( |
| 587 | self.policy_identifier == other.policy_identifier and |
| 588 | self.policy_qualifiers == other.policy_qualifiers |
| 589 | ) |
| 590 | |
| 591 | def __ne__(self, other): |
| 592 | return not self == other |
| 593 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 594 | policy_identifier = utils.read_only_property("_policy_identifier") |
| 595 | policy_qualifiers = utils.read_only_property("_policy_qualifiers") |
| 596 | |
| 597 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 598 | class UserNotice(object): |
| 599 | def __init__(self, notice_reference, explicit_text): |
| 600 | if notice_reference and not isinstance( |
| 601 | notice_reference, NoticeReference |
| 602 | ): |
| 603 | raise TypeError( |
| 604 | "notice_reference must be None or a NoticeReference" |
| 605 | ) |
| 606 | |
| 607 | self._notice_reference = notice_reference |
| 608 | self._explicit_text = explicit_text |
| 609 | |
| 610 | def __repr__(self): |
| 611 | return ( |
| 612 | "<UserNotice(notice_reference={0.notice_reference}, explicit_text=" |
Paul Kehrer | 9aaef9e | 2015-05-11 10:49:20 -0500 | [diff] [blame] | 613 | "{0.explicit_text!r})>".format(self) |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 614 | ) |
| 615 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 616 | def __eq__(self, other): |
| 617 | if not isinstance(other, UserNotice): |
| 618 | return NotImplemented |
| 619 | |
| 620 | return ( |
| 621 | self.notice_reference == other.notice_reference and |
| 622 | self.explicit_text == other.explicit_text |
| 623 | ) |
| 624 | |
| 625 | def __ne__(self, other): |
| 626 | return not self == other |
| 627 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 628 | notice_reference = utils.read_only_property("_notice_reference") |
| 629 | explicit_text = utils.read_only_property("_explicit_text") |
| 630 | |
| 631 | |
| 632 | class NoticeReference(object): |
| 633 | def __init__(self, organization, notice_numbers): |
| 634 | self._organization = organization |
Paul Kehrer | 6e198b0 | 2015-05-12 15:53:38 -0500 | [diff] [blame] | 635 | if not isinstance(notice_numbers, list) or not all( |
Paul Kehrer | f61ec74 | 2015-04-18 20:42:39 -0500 | [diff] [blame] | 636 | isinstance(x, int) for x in notice_numbers |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 637 | ): |
| 638 | raise TypeError( |
Paul Kehrer | 6e198b0 | 2015-05-12 15:53:38 -0500 | [diff] [blame] | 639 | "notice_numbers must be a list of integers" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 640 | ) |
| 641 | |
| 642 | self._notice_numbers = notice_numbers |
| 643 | |
| 644 | def __repr__(self): |
| 645 | return ( |
Paul Kehrer | 73be2ca | 2015-05-11 21:22:38 -0500 | [diff] [blame] | 646 | "<NoticeReference(organization={0.organization!r}, notice_numbers=" |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 647 | "{0.notice_numbers})>".format(self) |
| 648 | ) |
| 649 | |
Paul Kehrer | c56ab62 | 2015-05-03 09:56:31 -0500 | [diff] [blame] | 650 | def __eq__(self, other): |
| 651 | if not isinstance(other, NoticeReference): |
| 652 | return NotImplemented |
| 653 | |
| 654 | return ( |
| 655 | self.organization == other.organization and |
| 656 | self.notice_numbers == other.notice_numbers |
| 657 | ) |
| 658 | |
| 659 | def __ne__(self, other): |
| 660 | return not self == other |
| 661 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 662 | organization = utils.read_only_property("_organization") |
| 663 | notice_numbers = utils.read_only_property("_notice_numbers") |
| 664 | |
| 665 | |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 666 | class SubjectKeyIdentifier(object): |
| 667 | def __init__(self, digest): |
| 668 | self._digest = digest |
| 669 | |
| 670 | digest = utils.read_only_property("_digest") |
| 671 | |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 672 | def __repr__(self): |
Paul Kehrer | cbfb101 | 2015-04-10 20:57:20 -0400 | [diff] [blame] | 673 | return "<SubjectKeyIdentifier(digest={0!r})>".format(self.digest) |
Paul Kehrer | 1eb82a6 | 2015-03-31 20:00:33 -0500 | [diff] [blame] | 674 | |
| 675 | def __eq__(self, other): |
| 676 | if not isinstance(other, SubjectKeyIdentifier): |
| 677 | return NotImplemented |
| 678 | |
| 679 | return ( |
| 680 | self.digest == other.digest |
| 681 | ) |
| 682 | |
| 683 | def __ne__(self, other): |
| 684 | return not self == other |
| 685 | |
| 686 | |
Paul Kehrer | e0017be | 2015-05-17 20:39:40 -0600 | [diff] [blame] | 687 | class NameConstraints(object): |
| 688 | def __init__(self, permitted_subtrees, excluded_subtrees): |
| 689 | if permitted_subtrees is not None: |
| 690 | if not all( |
| 691 | isinstance(x, GeneralName) for x in permitted_subtrees |
| 692 | ): |
| 693 | raise TypeError( |
| 694 | "permitted_subtrees must be a list of GeneralName objects " |
| 695 | "or None" |
| 696 | ) |
| 697 | |
| 698 | self._validate_ip_name(permitted_subtrees) |
| 699 | |
| 700 | if excluded_subtrees is not None: |
| 701 | if not all( |
| 702 | isinstance(x, GeneralName) for x in excluded_subtrees |
| 703 | ): |
| 704 | raise TypeError( |
| 705 | "excluded_subtrees must be a list of GeneralName objects " |
| 706 | "or None" |
| 707 | ) |
| 708 | |
| 709 | self._validate_ip_name(excluded_subtrees) |
| 710 | |
| 711 | if permitted_subtrees is None and excluded_subtrees is None: |
| 712 | raise ValueError( |
| 713 | "At least one of permitted_subtrees and excluded_subtrees " |
| 714 | "must not be None" |
| 715 | ) |
| 716 | |
| 717 | self._permitted_subtrees = permitted_subtrees |
| 718 | self._excluded_subtrees = excluded_subtrees |
| 719 | |
Paul Kehrer | 3189428 | 2015-06-21 21:46:41 -0500 | [diff] [blame] | 720 | def __eq__(self, other): |
| 721 | if not isinstance(other, NameConstraints): |
| 722 | return NotImplemented |
| 723 | |
| 724 | return ( |
| 725 | self.excluded_subtrees == other.excluded_subtrees and |
| 726 | self.permitted_subtrees == other.permitted_subtrees |
| 727 | ) |
| 728 | |
| 729 | def __ne__(self, other): |
| 730 | return not self == other |
| 731 | |
Paul Kehrer | e0017be | 2015-05-17 20:39:40 -0600 | [diff] [blame] | 732 | def _validate_ip_name(self, tree): |
| 733 | if any(isinstance(name, IPAddress) and not isinstance( |
| 734 | name.value, (ipaddress.IPv4Network, ipaddress.IPv6Network) |
| 735 | ) for name in tree): |
| 736 | raise TypeError( |
| 737 | "IPAddress name constraints must be an IPv4Network or" |
| 738 | " IPv6Network object" |
| 739 | ) |
| 740 | |
| 741 | def __repr__(self): |
| 742 | return ( |
| 743 | u"<NameConstraints(permitted_subtrees={0.permitted_subtrees}, " |
| 744 | u"excluded_subtrees={0.excluded_subtrees})>".format(self) |
| 745 | ) |
| 746 | |
| 747 | permitted_subtrees = utils.read_only_property("_permitted_subtrees") |
| 748 | excluded_subtrees = utils.read_only_property("_excluded_subtrees") |
| 749 | |
| 750 | |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 751 | class CRLDistributionPoints(object): |
| 752 | def __init__(self, distribution_points): |
| 753 | if not all( |
| 754 | isinstance(x, DistributionPoint) for x in distribution_points |
| 755 | ): |
| 756 | raise TypeError( |
| 757 | "distribution_points must be a list of DistributionPoint " |
| 758 | "objects" |
| 759 | ) |
| 760 | |
| 761 | self._distribution_points = distribution_points |
| 762 | |
| 763 | def __iter__(self): |
| 764 | return iter(self._distribution_points) |
| 765 | |
| 766 | def __len__(self): |
| 767 | return len(self._distribution_points) |
| 768 | |
| 769 | def __repr__(self): |
| 770 | return "<CRLDistributionPoints({0})>".format(self._distribution_points) |
| 771 | |
| 772 | def __eq__(self, other): |
| 773 | if not isinstance(other, CRLDistributionPoints): |
| 774 | return NotImplemented |
| 775 | |
| 776 | return self._distribution_points == other._distribution_points |
| 777 | |
| 778 | def __ne__(self, other): |
| 779 | return not self == other |
| 780 | |
| 781 | |
| 782 | class DistributionPoint(object): |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 783 | def __init__(self, full_name, relative_name, reasons, crl_issuer): |
| 784 | if full_name and relative_name: |
| 785 | raise ValueError( |
| 786 | "At least one of full_name and relative_name must be None" |
| 787 | ) |
| 788 | |
| 789 | if full_name and not all( |
| 790 | isinstance(x, GeneralName) for x in full_name |
| 791 | ): |
| 792 | raise TypeError( |
| 793 | "full_name must be a list of GeneralName objects" |
| 794 | ) |
| 795 | |
| 796 | if relative_name and not isinstance(relative_name, Name): |
| 797 | raise TypeError("relative_name must be a Name") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 798 | |
| 799 | if crl_issuer and not all( |
| 800 | isinstance(x, GeneralName) for x in crl_issuer |
| 801 | ): |
| 802 | raise TypeError( |
| 803 | "crl_issuer must be None or a list of general names" |
| 804 | ) |
| 805 | |
Paul Kehrer | 3fd0260 | 2015-05-09 19:46:13 -0500 | [diff] [blame] | 806 | if reasons and (not isinstance(reasons, frozenset) or not all( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 807 | isinstance(x, ReasonFlags) for x in reasons |
Paul Kehrer | 3fd0260 | 2015-05-09 19:46:13 -0500 | [diff] [blame] | 808 | )): |
| 809 | raise TypeError("reasons must be None or frozenset of ReasonFlags") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 810 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 811 | if reasons and ( |
| 812 | ReasonFlags.unspecified in reasons or |
| 813 | ReasonFlags.remove_from_crl in reasons |
| 814 | ): |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 815 | raise ValueError( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 816 | "unspecified and remove_from_crl are not valid reasons in a " |
| 817 | "DistributionPoint" |
| 818 | ) |
| 819 | |
| 820 | if reasons and not crl_issuer and not (full_name or relative_name): |
| 821 | raise ValueError( |
| 822 | "You must supply crl_issuer, full_name, or relative_name when " |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 823 | "reasons is not None" |
| 824 | ) |
| 825 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 826 | self._full_name = full_name |
| 827 | self._relative_name = relative_name |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 828 | self._reasons = reasons |
| 829 | self._crl_issuer = crl_issuer |
| 830 | |
| 831 | def __repr__(self): |
| 832 | return ( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 833 | "<DistributionPoint(full_name={0.full_name}, relative_name={0.rela" |
| 834 | "tive_name}, reasons={0.reasons}, crl_issuer={0.crl_is" |
| 835 | "suer})>".format(self) |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 836 | ) |
| 837 | |
| 838 | def __eq__(self, other): |
| 839 | if not isinstance(other, DistributionPoint): |
| 840 | return NotImplemented |
| 841 | |
| 842 | return ( |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 843 | self.full_name == other.full_name and |
| 844 | self.relative_name == other.relative_name and |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 845 | self.reasons == other.reasons and |
| 846 | self.crl_issuer == other.crl_issuer |
| 847 | ) |
| 848 | |
| 849 | def __ne__(self, other): |
| 850 | return not self == other |
| 851 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 852 | full_name = utils.read_only_property("_full_name") |
| 853 | relative_name = utils.read_only_property("_relative_name") |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 854 | reasons = utils.read_only_property("_reasons") |
| 855 | crl_issuer = utils.read_only_property("_crl_issuer") |
| 856 | |
| 857 | |
Paul Kehrer | 4e8dacd | 2015-05-09 10:38:23 -0500 | [diff] [blame] | 858 | class ReasonFlags(Enum): |
| 859 | unspecified = "unspecified" |
| 860 | key_compromise = "keyCompromise" |
| 861 | ca_compromise = "cACompromise" |
| 862 | affiliation_changed = "affiliationChanged" |
| 863 | superseded = "superseded" |
| 864 | cessation_of_operation = "cessationOfOperation" |
| 865 | certificate_hold = "certificateHold" |
| 866 | privilege_withdrawn = "privilegeWithdrawn" |
| 867 | aa_compromise = "aACompromise" |
| 868 | remove_from_crl = "removeFromCRL" |
Paul Kehrer | 5a48552 | 2015-05-06 00:29:12 -0500 | [diff] [blame] | 869 | |
| 870 | |
Paul Kehrer | 16fae76 | 2015-05-01 23:14:20 -0500 | [diff] [blame] | 871 | class InhibitAnyPolicy(object): |
| 872 | def __init__(self, skip_certs): |
| 873 | if not isinstance(skip_certs, six.integer_types): |
| 874 | raise TypeError("skip_certs must be an integer") |
| 875 | |
| 876 | if skip_certs < 0: |
| 877 | raise ValueError("skip_certs must be a non-negative integer") |
| 878 | |
| 879 | self._skip_certs = skip_certs |
| 880 | |
| 881 | def __repr__(self): |
| 882 | return "<InhibitAnyPolicy(skip_certs={0.skip_certs})>".format(self) |
| 883 | |
| 884 | def __eq__(self, other): |
| 885 | if not isinstance(other, InhibitAnyPolicy): |
| 886 | return NotImplemented |
| 887 | |
| 888 | return self.skip_certs == other.skip_certs |
| 889 | |
| 890 | def __ne__(self, other): |
| 891 | return not self == other |
| 892 | |
| 893 | skip_certs = utils.read_only_property("_skip_certs") |
| 894 | |
| 895 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 896 | @six.add_metaclass(abc.ABCMeta) |
| 897 | class GeneralName(object): |
| 898 | @abc.abstractproperty |
| 899 | def value(self): |
| 900 | """ |
| 901 | Return the value of the object |
| 902 | """ |
| 903 | |
| 904 | |
| 905 | @utils.register_interface(GeneralName) |
| 906 | class RFC822Name(object): |
| 907 | def __init__(self, value): |
| 908 | if not isinstance(value, six.text_type): |
| 909 | raise TypeError("value must be a unicode string") |
| 910 | |
Paul Kehrer | 01d5d0b | 2015-07-12 09:41:21 -0500 | [diff] [blame] | 911 | name, address = parseaddr(value) |
| 912 | parts = address.split(u"@") |
Paul Kehrer | 8289086 | 2015-07-12 12:08:28 -0500 | [diff] [blame] | 913 | if name or not address: |
| 914 | # parseaddr has found a name (e.g. Name <email>) or the entire |
| 915 | # value is an empty string. |
Paul Kehrer | 01d5d0b | 2015-07-12 09:41:21 -0500 | [diff] [blame] | 916 | raise ValueError("Invalid rfc822name value") |
| 917 | elif len(parts) == 1: |
| 918 | # Single label email name. This is valid for local delivery. |
| 919 | # No IDNA encoding needed since there is no domain component. |
| 920 | encoded = address.encode("ascii") |
| 921 | else: |
| 922 | # A normal email of the form user@domain.com. Let's attempt to |
| 923 | # encode the domain component and reconstruct the address. |
| 924 | encoded = parts[0].encode("ascii") + b"@" + idna.encode(parts[1]) |
| 925 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 926 | self._value = value |
Paul Kehrer | 01d5d0b | 2015-07-12 09:41:21 -0500 | [diff] [blame] | 927 | self._encoded = encoded |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 928 | |
| 929 | value = utils.read_only_property("_value") |
| 930 | |
| 931 | def __repr__(self): |
| 932 | return "<RFC822Name(value={0})>".format(self.value) |
| 933 | |
| 934 | def __eq__(self, other): |
| 935 | if not isinstance(other, RFC822Name): |
| 936 | return NotImplemented |
| 937 | |
| 938 | return self.value == other.value |
| 939 | |
| 940 | def __ne__(self, other): |
| 941 | return not self == other |
| 942 | |
| 943 | |
| 944 | @utils.register_interface(GeneralName) |
| 945 | class DNSName(object): |
| 946 | def __init__(self, value): |
| 947 | if not isinstance(value, six.text_type): |
| 948 | raise TypeError("value must be a unicode string") |
| 949 | |
| 950 | self._value = value |
| 951 | |
| 952 | value = utils.read_only_property("_value") |
| 953 | |
| 954 | def __repr__(self): |
| 955 | return "<DNSName(value={0})>".format(self.value) |
| 956 | |
| 957 | def __eq__(self, other): |
| 958 | if not isinstance(other, DNSName): |
| 959 | return NotImplemented |
| 960 | |
| 961 | return self.value == other.value |
| 962 | |
| 963 | def __ne__(self, other): |
| 964 | return not self == other |
| 965 | |
| 966 | |
| 967 | @utils.register_interface(GeneralName) |
| 968 | class UniformResourceIdentifier(object): |
| 969 | def __init__(self, value): |
| 970 | if not isinstance(value, six.text_type): |
| 971 | raise TypeError("value must be a unicode string") |
| 972 | |
Paul Kehrer | e28d6c4 | 2015-07-12 14:59:37 -0500 | [diff] [blame] | 973 | parsed = urllib_parse.urlparse(value) |
| 974 | if not parsed.hostname: |
| 975 | netloc = "" |
| 976 | elif parsed.port: |
| 977 | netloc = ( |
| 978 | idna.encode(parsed.hostname) + |
| 979 | ":{0}".format(parsed.port).encode("ascii") |
| 980 | ).decode("ascii") |
| 981 | else: |
| 982 | netloc = idna.encode(parsed.hostname).decode("ascii") |
| 983 | |
| 984 | # Note that building a URL in this fashion means it should be |
| 985 | # semantically indistinguishable from the original but is not |
| 986 | # guaranteed to be exactly the same. |
| 987 | uri = urllib_parse.urlunparse(( |
| 988 | parsed.scheme, |
| 989 | netloc, |
| 990 | parsed.path, |
| 991 | parsed.params, |
| 992 | parsed.query, |
| 993 | parsed.fragment |
| 994 | )).encode("ascii") |
| 995 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 996 | self._value = value |
Paul Kehrer | e28d6c4 | 2015-07-12 14:59:37 -0500 | [diff] [blame] | 997 | self._encoded = uri |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 998 | |
| 999 | value = utils.read_only_property("_value") |
| 1000 | |
| 1001 | def __repr__(self): |
| 1002 | return "<UniformResourceIdentifier(value={0})>".format(self.value) |
| 1003 | |
| 1004 | def __eq__(self, other): |
| 1005 | if not isinstance(other, UniformResourceIdentifier): |
| 1006 | return NotImplemented |
| 1007 | |
| 1008 | return self.value == other.value |
| 1009 | |
| 1010 | def __ne__(self, other): |
| 1011 | return not self == other |
| 1012 | |
| 1013 | |
| 1014 | @utils.register_interface(GeneralName) |
| 1015 | class DirectoryName(object): |
| 1016 | def __init__(self, value): |
| 1017 | if not isinstance(value, Name): |
| 1018 | raise TypeError("value must be a Name") |
| 1019 | |
| 1020 | self._value = value |
| 1021 | |
| 1022 | value = utils.read_only_property("_value") |
| 1023 | |
| 1024 | def __repr__(self): |
| 1025 | return "<DirectoryName(value={0})>".format(self.value) |
| 1026 | |
| 1027 | def __eq__(self, other): |
| 1028 | if not isinstance(other, DirectoryName): |
| 1029 | return NotImplemented |
| 1030 | |
| 1031 | return self.value == other.value |
| 1032 | |
| 1033 | def __ne__(self, other): |
| 1034 | return not self == other |
| 1035 | |
| 1036 | |
| 1037 | @utils.register_interface(GeneralName) |
| 1038 | class RegisteredID(object): |
| 1039 | def __init__(self, value): |
| 1040 | if not isinstance(value, ObjectIdentifier): |
| 1041 | raise TypeError("value must be an ObjectIdentifier") |
| 1042 | |
| 1043 | self._value = value |
| 1044 | |
| 1045 | value = utils.read_only_property("_value") |
| 1046 | |
| 1047 | def __repr__(self): |
| 1048 | return "<RegisteredID(value={0})>".format(self.value) |
| 1049 | |
| 1050 | def __eq__(self, other): |
| 1051 | if not isinstance(other, RegisteredID): |
| 1052 | return NotImplemented |
| 1053 | |
| 1054 | return self.value == other.value |
| 1055 | |
| 1056 | def __ne__(self, other): |
| 1057 | return not self == other |
| 1058 | |
| 1059 | |
| 1060 | @utils.register_interface(GeneralName) |
| 1061 | class IPAddress(object): |
| 1062 | def __init__(self, value): |
| 1063 | if not isinstance( |
Paul Kehrer | eb17793 | 2015-05-17 18:33:33 -0700 | [diff] [blame] | 1064 | value, |
| 1065 | ( |
| 1066 | ipaddress.IPv4Address, |
| 1067 | ipaddress.IPv6Address, |
| 1068 | ipaddress.IPv4Network, |
| 1069 | ipaddress.IPv6Network |
| 1070 | ) |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1071 | ): |
| 1072 | raise TypeError( |
Paul Kehrer | eb17793 | 2015-05-17 18:33:33 -0700 | [diff] [blame] | 1073 | "value must be an instance of ipaddress.IPv4Address, " |
| 1074 | "ipaddress.IPv6Address, ipaddress.IPv4Network, or " |
| 1075 | "ipaddress.IPv6Network" |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1076 | ) |
| 1077 | |
| 1078 | self._value = value |
| 1079 | |
| 1080 | value = utils.read_only_property("_value") |
| 1081 | |
| 1082 | def __repr__(self): |
| 1083 | return "<IPAddress(value={0})>".format(self.value) |
| 1084 | |
| 1085 | def __eq__(self, other): |
| 1086 | if not isinstance(other, IPAddress): |
| 1087 | return NotImplemented |
| 1088 | |
| 1089 | return self.value == other.value |
| 1090 | |
| 1091 | def __ne__(self, other): |
| 1092 | return not self == other |
| 1093 | |
| 1094 | |
Joshua Tauberer | 2ee5e3c | 2015-07-04 20:09:46 +0000 | [diff] [blame] | 1095 | @utils.register_interface(GeneralName) |
| 1096 | class OtherName(object): |
| 1097 | def __init__(self, type_id, value): |
| 1098 | if not isinstance(type_id, ObjectIdentifier): |
| 1099 | raise TypeError("type_id must be an ObjectIdentifier") |
| 1100 | if not isinstance(value, bytes): |
| 1101 | raise TypeError("value must be a binary string") |
| 1102 | |
| 1103 | self._type_id = type_id |
| 1104 | self._value = value |
| 1105 | |
| 1106 | type_id = utils.read_only_property("_type_id") |
| 1107 | value = utils.read_only_property("_value") |
| 1108 | |
| 1109 | def __repr__(self): |
| 1110 | return "<OtherName(type_id={0}, value={1!r})>".format( |
| 1111 | self.type_id, self.value) |
| 1112 | |
| 1113 | def __eq__(self, other): |
| 1114 | if not isinstance(other, OtherName): |
| 1115 | return NotImplemented |
| 1116 | |
| 1117 | return self.type_id == other.type_id and self.value == other.value |
| 1118 | |
| 1119 | def __ne__(self, other): |
| 1120 | return not self == other |
| 1121 | |
| 1122 | |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1123 | class GeneralNames(object): |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1124 | def __init__(self, general_names): |
Paul Kehrer | d04b39b | 2015-04-21 08:44:17 -0500 | [diff] [blame] | 1125 | if not all(isinstance(x, GeneralName) for x in general_names): |
| 1126 | raise TypeError( |
| 1127 | "Every item in the general_names list must be an " |
| 1128 | "object conforming to the GeneralName interface" |
| 1129 | ) |
| 1130 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1131 | self._general_names = general_names |
| 1132 | |
| 1133 | def __iter__(self): |
| 1134 | return iter(self._general_names) |
| 1135 | |
| 1136 | def __len__(self): |
| 1137 | return len(self._general_names) |
| 1138 | |
| 1139 | def get_values_for_type(self, type): |
Joshua Tauberer | d2afad3 | 2015-07-06 22:37:53 +0000 | [diff] [blame] | 1140 | # Return the value of each GeneralName, except for OtherName instances |
| 1141 | # which we return directly because it has two important properties not |
| 1142 | # just one value. |
| 1143 | objs = (i for i in self if isinstance(i, type)) |
| 1144 | if type != OtherName: |
| 1145 | objs = (i.value for i in objs) |
| 1146 | return list(objs) |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1147 | |
| 1148 | def __repr__(self): |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1149 | return "<GeneralNames({0})>".format(self._general_names) |
| 1150 | |
| 1151 | def __eq__(self, other): |
| 1152 | if not isinstance(other, GeneralNames): |
| 1153 | return NotImplemented |
| 1154 | |
| 1155 | return self._general_names == other._general_names |
| 1156 | |
| 1157 | def __ne__(self, other): |
| 1158 | return not self == other |
| 1159 | |
| 1160 | |
| 1161 | class SubjectAlternativeName(object): |
| 1162 | def __init__(self, general_names): |
| 1163 | self._general_names = GeneralNames(general_names) |
| 1164 | |
| 1165 | def __iter__(self): |
| 1166 | return iter(self._general_names) |
| 1167 | |
| 1168 | def __len__(self): |
| 1169 | return len(self._general_names) |
| 1170 | |
| 1171 | def get_values_for_type(self, type): |
| 1172 | return self._general_names.get_values_for_type(type) |
| 1173 | |
| 1174 | def __repr__(self): |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1175 | return "<SubjectAlternativeName({0})>".format(self._general_names) |
| 1176 | |
Paul Kehrer | 58cc397 | 2015-05-13 10:00:41 -0500 | [diff] [blame] | 1177 | def __eq__(self, other): |
| 1178 | if not isinstance(other, SubjectAlternativeName): |
| 1179 | return NotImplemented |
| 1180 | |
| 1181 | return self._general_names == other._general_names |
| 1182 | |
| 1183 | def __ne__(self, other): |
| 1184 | return not self == other |
| 1185 | |
Paul Kehrer | 31bdf79 | 2015-03-25 14:11:00 -0500 | [diff] [blame] | 1186 | |
Paul Kehrer | 99125c9 | 2015-06-07 18:37:10 -0500 | [diff] [blame] | 1187 | class IssuerAlternativeName(object): |
| 1188 | def __init__(self, general_names): |
| 1189 | self._general_names = GeneralNames(general_names) |
| 1190 | |
| 1191 | def __iter__(self): |
| 1192 | return iter(self._general_names) |
| 1193 | |
| 1194 | def __len__(self): |
| 1195 | return len(self._general_names) |
| 1196 | |
| 1197 | def get_values_for_type(self, type): |
| 1198 | return self._general_names.get_values_for_type(type) |
| 1199 | |
| 1200 | def __repr__(self): |
| 1201 | return "<IssuerAlternativeName({0})>".format(self._general_names) |
| 1202 | |
| 1203 | def __eq__(self, other): |
| 1204 | if not isinstance(other, IssuerAlternativeName): |
| 1205 | return NotImplemented |
| 1206 | |
| 1207 | return self._general_names == other._general_names |
| 1208 | |
| 1209 | def __ne__(self, other): |
| 1210 | return not self == other |
| 1211 | |
| 1212 | |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 1213 | class AuthorityKeyIdentifier(object): |
| 1214 | def __init__(self, key_identifier, authority_cert_issuer, |
| 1215 | authority_cert_serial_number): |
| 1216 | if authority_cert_issuer or authority_cert_serial_number: |
| 1217 | if not authority_cert_issuer or not authority_cert_serial_number: |
| 1218 | raise ValueError( |
| 1219 | "authority_cert_issuer and authority_cert_serial_number " |
| 1220 | "must both be present or both None" |
| 1221 | ) |
| 1222 | |
Paul Kehrer | c6e0f06 | 2015-05-03 11:04:34 -0500 | [diff] [blame] | 1223 | if not all( |
| 1224 | isinstance(x, GeneralName) for x in authority_cert_issuer |
| 1225 | ): |
| 1226 | raise TypeError( |
| 1227 | "authority_cert_issuer must be a list of GeneralName " |
| 1228 | "objects" |
| 1229 | ) |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 1230 | |
| 1231 | if not isinstance(authority_cert_serial_number, six.integer_types): |
| 1232 | raise TypeError( |
| 1233 | "authority_cert_serial_number must be an integer" |
| 1234 | ) |
| 1235 | |
| 1236 | self._key_identifier = key_identifier |
| 1237 | self._authority_cert_issuer = authority_cert_issuer |
| 1238 | self._authority_cert_serial_number = authority_cert_serial_number |
| 1239 | |
| 1240 | def __repr__(self): |
| 1241 | return ( |
| 1242 | "<AuthorityKeyIdentifier(key_identifier={0.key_identifier!r}, " |
| 1243 | "authority_cert_issuer={0.authority_cert_issuer}, " |
| 1244 | "authority_cert_serial_number={0.authority_cert_serial_number}" |
| 1245 | ")>".format(self) |
| 1246 | ) |
| 1247 | |
Paul Kehrer | 2ca2eb3 | 2015-05-03 10:04:57 -0500 | [diff] [blame] | 1248 | def __eq__(self, other): |
| 1249 | if not isinstance(other, AuthorityKeyIdentifier): |
| 1250 | return NotImplemented |
| 1251 | |
| 1252 | return ( |
| 1253 | self.key_identifier == other.key_identifier and |
| 1254 | self.authority_cert_issuer == other.authority_cert_issuer and |
| 1255 | self.authority_cert_serial_number == |
| 1256 | other.authority_cert_serial_number |
| 1257 | ) |
| 1258 | |
| 1259 | def __ne__(self, other): |
| 1260 | return not self == other |
| 1261 | |
Paul Kehrer | 2eb4ed9 | 2015-04-11 15:33:45 -0400 | [diff] [blame] | 1262 | key_identifier = utils.read_only_property("_key_identifier") |
| 1263 | authority_cert_issuer = utils.read_only_property("_authority_cert_issuer") |
| 1264 | authority_cert_serial_number = utils.read_only_property( |
| 1265 | "_authority_cert_serial_number" |
| 1266 | ) |
| 1267 | |
| 1268 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 1269 | OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") |
| 1270 | OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6") |
| 1271 | OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7") |
| 1272 | OID_STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8") |
| 1273 | OID_ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10") |
| 1274 | OID_ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11") |
| 1275 | OID_SERIAL_NUMBER = ObjectIdentifier("2.5.4.5") |
| 1276 | OID_SURNAME = ObjectIdentifier("2.5.4.4") |
| 1277 | OID_GIVEN_NAME = ObjectIdentifier("2.5.4.42") |
| 1278 | OID_TITLE = ObjectIdentifier("2.5.4.12") |
| 1279 | OID_GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44") |
| 1280 | OID_DN_QUALIFIER = ObjectIdentifier("2.5.4.46") |
| 1281 | OID_PSEUDONYM = ObjectIdentifier("2.5.4.65") |
| 1282 | OID_DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25") |
| 1283 | OID_EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 1284 | |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 1285 | OID_RSA_WITH_MD5 = ObjectIdentifier("1.2.840.113549.1.1.4") |
| 1286 | OID_RSA_WITH_SHA1 = ObjectIdentifier("1.2.840.113549.1.1.5") |
| 1287 | OID_RSA_WITH_SHA224 = ObjectIdentifier("1.2.840.113549.1.1.14") |
| 1288 | OID_RSA_WITH_SHA256 = ObjectIdentifier("1.2.840.113549.1.1.11") |
| 1289 | OID_RSA_WITH_SHA384 = ObjectIdentifier("1.2.840.113549.1.1.12") |
| 1290 | OID_RSA_WITH_SHA512 = ObjectIdentifier("1.2.840.113549.1.1.13") |
Alex Gaynor | 3aadabf | 2015-06-23 22:06:21 -0400 | [diff] [blame] | 1291 | OID_ECDSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10045.4.1") |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1292 | OID_ECDSA_WITH_SHA224 = ObjectIdentifier("1.2.840.10045.4.3.1") |
| 1293 | OID_ECDSA_WITH_SHA256 = ObjectIdentifier("1.2.840.10045.4.3.2") |
| 1294 | OID_ECDSA_WITH_SHA384 = ObjectIdentifier("1.2.840.10045.4.3.3") |
| 1295 | OID_ECDSA_WITH_SHA512 = ObjectIdentifier("1.2.840.10045.4.3.4") |
| 1296 | OID_DSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10040.4.3") |
| 1297 | OID_DSA_WITH_SHA224 = ObjectIdentifier("2.16.840.1.101.3.4.3.1") |
| 1298 | OID_DSA_WITH_SHA256 = ObjectIdentifier("2.16.840.1.101.3.4.3.2") |
| 1299 | |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1300 | _SIG_OIDS_TO_HASH = { |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 1301 | OID_RSA_WITH_MD5.dotted_string: hashes.MD5(), |
| 1302 | OID_RSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 1303 | OID_RSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1304 | OID_RSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 1305 | OID_RSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 1306 | OID_RSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
Alex Gaynor | 3aadabf | 2015-06-23 22:06:21 -0400 | [diff] [blame] | 1307 | OID_ECDSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
Paul Kehrer | 42a87cb | 2015-02-13 18:53:24 -0600 | [diff] [blame] | 1308 | OID_ECDSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1309 | OID_ECDSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 1310 | OID_ECDSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 1311 | OID_ECDSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
| 1312 | OID_DSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 1313 | OID_DSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 1314 | OID_DSA_WITH_SHA256.dotted_string: hashes.SHA256() |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1315 | } |
| 1316 | |
Paul Kehrer | e1513fa | 2015-03-30 23:08:17 -0500 | [diff] [blame] | 1317 | OID_SERVER_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.1") |
| 1318 | OID_CLIENT_AUTH = ObjectIdentifier("1.3.6.1.5.5.7.3.2") |
| 1319 | OID_CODE_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.3") |
| 1320 | OID_EMAIL_PROTECTION = ObjectIdentifier("1.3.6.1.5.5.7.3.4") |
| 1321 | OID_TIME_STAMPING = ObjectIdentifier("1.3.6.1.5.5.7.3.8") |
| 1322 | OID_OCSP_SIGNING = ObjectIdentifier("1.3.6.1.5.5.7.3.9") |
| 1323 | |
Paul Kehrer | 3e6d558 | 2015-05-02 21:57:56 -0500 | [diff] [blame] | 1324 | OID_CA_ISSUERS = ObjectIdentifier("1.3.6.1.5.5.7.48.2") |
| 1325 | OID_OCSP = ObjectIdentifier("1.3.6.1.5.5.7.48.1") |
| 1326 | |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 1327 | OID_CPS_QUALIFIER = ObjectIdentifier("1.3.6.1.5.5.7.2.1") |
| 1328 | OID_CPS_USER_NOTICE = ObjectIdentifier("1.3.6.1.5.5.7.2.2") |
Paul Kehrer | 16fae76 | 2015-05-01 23:14:20 -0500 | [diff] [blame] | 1329 | OID_ANY_POLICY = ObjectIdentifier("2.5.29.32.0") |
Paul Kehrer | 2b62258 | 2015-04-15 11:04:29 -0400 | [diff] [blame] | 1330 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 1331 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 1332 | @six.add_metaclass(abc.ABCMeta) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 1333 | class Certificate(object): |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 1334 | @abc.abstractmethod |
| 1335 | def fingerprint(self, algorithm): |
| 1336 | """ |
| 1337 | Returns bytes using digest passed. |
| 1338 | """ |
| 1339 | |
| 1340 | @abc.abstractproperty |
| 1341 | def serial(self): |
| 1342 | """ |
| 1343 | Returns certificate serial number |
| 1344 | """ |
| 1345 | |
| 1346 | @abc.abstractproperty |
| 1347 | def version(self): |
| 1348 | """ |
| 1349 | Returns the certificate version |
| 1350 | """ |
| 1351 | |
| 1352 | @abc.abstractmethod |
| 1353 | def public_key(self): |
| 1354 | """ |
| 1355 | Returns the public key |
| 1356 | """ |
| 1357 | |
| 1358 | @abc.abstractproperty |
| 1359 | def not_valid_before(self): |
| 1360 | """ |
| 1361 | Not before time (represented as UTC datetime) |
| 1362 | """ |
| 1363 | |
| 1364 | @abc.abstractproperty |
| 1365 | def not_valid_after(self): |
| 1366 | """ |
| 1367 | Not after time (represented as UTC datetime) |
| 1368 | """ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 1369 | |
| 1370 | @abc.abstractproperty |
| 1371 | def issuer(self): |
| 1372 | """ |
| 1373 | Returns the issuer name object. |
| 1374 | """ |
| 1375 | |
| 1376 | @abc.abstractproperty |
| 1377 | def subject(self): |
| 1378 | """ |
| 1379 | Returns the subject name object. |
| 1380 | """ |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1381 | |
| 1382 | @abc.abstractproperty |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1383 | def signature_hash_algorithm(self): |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1384 | """ |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 1385 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 1386 | in the certificate. |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 1387 | """ |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1388 | |
Paul Kehrer | 8c234d1 | 2015-05-15 09:27:22 -0700 | [diff] [blame] | 1389 | @abc.abstractproperty |
| 1390 | def extensions(self): |
| 1391 | """ |
| 1392 | Returns an Extensions object. |
| 1393 | """ |
| 1394 | |
Paul Kehrer | 8bbdc6f | 2015-04-30 16:47:16 -0500 | [diff] [blame] | 1395 | @abc.abstractmethod |
| 1396 | def __eq__(self, other): |
| 1397 | """ |
| 1398 | Checks equality. |
| 1399 | """ |
| 1400 | |
| 1401 | @abc.abstractmethod |
| 1402 | def __ne__(self, other): |
| 1403 | """ |
| 1404 | Checks not equal. |
| 1405 | """ |
| 1406 | |
Andre Caron | a8aded6 | 2015-05-19 20:11:57 -0400 | [diff] [blame] | 1407 | @abc.abstractmethod |
Alex Gaynor | 969f3a5 | 2015-07-06 18:52:41 -0400 | [diff] [blame] | 1408 | def __hash__(self): |
| 1409 | """ |
| 1410 | Computes a hash. |
| 1411 | """ |
| 1412 | |
| 1413 | @abc.abstractmethod |
Andre Caron | a8aded6 | 2015-05-19 20:11:57 -0400 | [diff] [blame] | 1414 | def public_bytes(self, encoding): |
Andre Caron | 18ef34b | 2015-05-19 21:24:31 -0400 | [diff] [blame] | 1415 | """ |
| 1416 | Serializes the certificate to PEM or DER format. |
| 1417 | """ |
Andre Caron | a8aded6 | 2015-05-19 20:11:57 -0400 | [diff] [blame] | 1418 | |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1419 | |
| 1420 | @six.add_metaclass(abc.ABCMeta) |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1421 | class CertificateRevocationList(object): |
| 1422 | |
| 1423 | @abc.abstractmethod |
| 1424 | def fingerprint(self, algorithm): |
| 1425 | """ |
| 1426 | Returns bytes using digest passed. |
| 1427 | """ |
| 1428 | |
| 1429 | @abc.abstractproperty |
| 1430 | def signature_hash_algorithm(self): |
| 1431 | """ |
| 1432 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 1433 | in the certificate. |
| 1434 | """ |
| 1435 | |
| 1436 | @abc.abstractproperty |
| 1437 | def issuer(self): |
| 1438 | """ |
| 1439 | Returns the X509Name with the issuer of this CRL. |
| 1440 | """ |
| 1441 | |
| 1442 | @abc.abstractproperty |
| 1443 | def next_update(self): |
| 1444 | """ |
| 1445 | Returns the date of next update for this CRL. |
| 1446 | """ |
| 1447 | |
| 1448 | @abc.abstractproperty |
| 1449 | def last_update(self): |
| 1450 | """ |
| 1451 | Returns the date of last update for this CRL. |
| 1452 | """ |
| 1453 | |
| 1454 | @abc.abstractproperty |
Erik Trauschke | abb7b6e | 2015-05-27 15:07:35 -0700 | [diff] [blame] | 1455 | def revoked_certificates(self): |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1456 | """ |
| 1457 | Returns a list of RevokedCertificate objects for this CRL. |
| 1458 | """ |
| 1459 | |
| 1460 | @abc.abstractproperty |
| 1461 | def extensions(self): |
| 1462 | """ |
| 1463 | Returns an Extensions object containing a list of CRL extensions. |
| 1464 | """ |
| 1465 | |
| 1466 | @abc.abstractmethod |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1467 | def __eq__(self, other): |
| 1468 | """ |
| 1469 | Checks equality. |
| 1470 | """ |
| 1471 | |
| 1472 | @abc.abstractmethod |
| 1473 | def __ne__(self, other): |
| 1474 | """ |
| 1475 | Checks not equal. |
| 1476 | """ |
| 1477 | |
| 1478 | |
| 1479 | @six.add_metaclass(abc.ABCMeta) |
Paul Kehrer | a1a1f23 | 2015-03-15 15:34:35 -0500 | [diff] [blame] | 1480 | class CertificateSigningRequest(object): |
Alex Gaynor | 935f6ca | 2015-07-06 21:03:46 -0400 | [diff] [blame] | 1481 | @abc.abstractmethod |
Alex Gaynor | 70c8f8b | 2015-07-06 21:02:54 -0400 | [diff] [blame] | 1482 | def __eq__(self, other): |
| 1483 | """ |
| 1484 | Checks equality. |
| 1485 | """ |
| 1486 | |
| 1487 | @abc.abstractmethod |
| 1488 | def __ne__(self, other): |
| 1489 | """ |
| 1490 | Checks not equal. |
| 1491 | """ |
| 1492 | |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1493 | @abc.abstractmethod |
Alex Gaynor | 978137d | 2015-07-08 20:59:16 -0400 | [diff] [blame] | 1494 | def __hash__(self): |
| 1495 | """ |
| 1496 | Computes a hash. |
| 1497 | """ |
| 1498 | |
| 1499 | @abc.abstractmethod |
Paul Kehrer | dc480ad | 2015-02-23 12:14:54 -0600 | [diff] [blame] | 1500 | def public_key(self): |
| 1501 | """ |
| 1502 | Returns the public key |
| 1503 | """ |
| 1504 | |
| 1505 | @abc.abstractproperty |
| 1506 | def subject(self): |
| 1507 | """ |
| 1508 | Returns the subject name object. |
| 1509 | """ |
| 1510 | |
| 1511 | @abc.abstractproperty |
| 1512 | def signature_hash_algorithm(self): |
| 1513 | """ |
| 1514 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 1515 | in the certificate. |
| 1516 | """ |
Andre Caron | 6e721a9 | 2015-05-17 15:08:48 -0400 | [diff] [blame] | 1517 | |
| 1518 | @abc.abstractproperty |
| 1519 | def extensions(self): |
| 1520 | """ |
| 1521 | Returns the extensions in the signing request. |
| 1522 | """ |
Andre Caron | 476c5df | 2015-05-18 10:23:28 -0400 | [diff] [blame] | 1523 | |
| 1524 | @abc.abstractmethod |
| 1525 | def public_bytes(self, encoding): |
| 1526 | """ |
| 1527 | Encodes the request to PEM or DER format. |
| 1528 | """ |
Erik Trauschke | 2dcce90 | 2015-05-14 16:12:24 -0700 | [diff] [blame] | 1529 | |
| 1530 | |
| 1531 | @six.add_metaclass(abc.ABCMeta) |
| 1532 | class RevokedCertificate(object): |
| 1533 | @abc.abstractproperty |
| 1534 | def serial_number(self): |
| 1535 | """ |
| 1536 | Returns the serial number of the revoked certificate. |
| 1537 | """ |
| 1538 | |
| 1539 | @abc.abstractproperty |
| 1540 | def revocation_date(self): |
| 1541 | """ |
| 1542 | Returns the date of when this certificate was revoked. |
| 1543 | """ |
| 1544 | |
| 1545 | @abc.abstractproperty |
| 1546 | def extensions(self): |
| 1547 | """ |
| 1548 | Returns an Extensions object containing a list of Revoked extensions. |
| 1549 | """ |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1550 | |
| 1551 | |
| 1552 | class CertificateSigningRequestBuilder(object): |
Andre Caron | 99d0f90 | 2015-06-01 08:36:59 -0400 | [diff] [blame] | 1553 | def __init__(self, subject_name=None, extensions=[]): |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1554 | """ |
| 1555 | Creates an empty X.509 certificate request (v1). |
| 1556 | """ |
Andre Caron | fc164c5 | 2015-05-31 17:36:18 -0400 | [diff] [blame] | 1557 | self._subject_name = subject_name |
Ian Cordasco | 41f51ce | 2015-06-17 11:49:11 -0500 | [diff] [blame] | 1558 | self._extensions = extensions |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1559 | |
Andre Caron | a9a5117 | 2015-06-06 20:18:44 -0400 | [diff] [blame] | 1560 | def subject_name(self, name): |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1561 | """ |
| 1562 | Sets the certificate requestor's distinguished name. |
| 1563 | """ |
| 1564 | if not isinstance(name, Name): |
| 1565 | raise TypeError('Expecting x509.Name object.') |
Ian Cordasco | d09ec37 | 2015-06-17 21:37:51 -0500 | [diff] [blame] | 1566 | if self._subject_name is not None: |
| 1567 | raise ValueError('The subject name may only be set once.') |
Andre Caron | 99d0f90 | 2015-06-01 08:36:59 -0400 | [diff] [blame] | 1568 | return CertificateSigningRequestBuilder(name, self._extensions) |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1569 | |
Ian Cordasco | f06b6be | 2015-06-21 10:09:18 -0500 | [diff] [blame] | 1570 | def add_extension(self, extension, critical): |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1571 | """ |
| 1572 | Adds an X.509 extension to the certificate request. |
| 1573 | """ |
Andre Caron | 472fd69 | 2015-06-06 20:04:44 -0400 | [diff] [blame] | 1574 | if isinstance(extension, BasicConstraints): |
| 1575 | extension = Extension(OID_BASIC_CONSTRAINTS, critical, extension) |
Paul Kehrer | 7e2fbe6 | 2015-06-26 17:59:05 -0500 | [diff] [blame] | 1576 | elif isinstance(extension, SubjectAlternativeName): |
| 1577 | extension = Extension( |
| 1578 | OID_SUBJECT_ALTERNATIVE_NAME, critical, extension |
| 1579 | ) |
Alex Gaynor | 887a408 | 2015-07-03 04:29:03 -0400 | [diff] [blame] | 1580 | elif isinstance(extension, KeyUsage): |
| 1581 | extension = Extension(OID_KEY_USAGE, critical, extension) |
Andre Caron | 472fd69 | 2015-06-06 20:04:44 -0400 | [diff] [blame] | 1582 | else: |
Ian Cordasco | f06b6be | 2015-06-21 10:09:18 -0500 | [diff] [blame] | 1583 | raise NotImplementedError('Unsupported X.509 extension.') |
| 1584 | # TODO: This is quadratic in the number of extensions |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1585 | for e in self._extensions: |
| 1586 | if e.oid == extension.oid: |
| 1587 | raise ValueError('This extension has already been set.') |
Andre Caron | fc164c5 | 2015-05-31 17:36:18 -0400 | [diff] [blame] | 1588 | return CertificateSigningRequestBuilder( |
Andre Caron | 99d0f90 | 2015-06-01 08:36:59 -0400 | [diff] [blame] | 1589 | self._subject_name, self._extensions + [extension] |
Andre Caron | fc164c5 | 2015-05-31 17:36:18 -0400 | [diff] [blame] | 1590 | ) |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1591 | |
Alex Gaynor | b3b0fbe | 2015-06-26 19:57:18 -0400 | [diff] [blame] | 1592 | def sign(self, private_key, algorithm, backend): |
Andre Caron | 0ef595f | 2015-05-18 13:53:43 -0400 | [diff] [blame] | 1593 | """ |
| 1594 | Signs the request using the requestor's private key. |
| 1595 | """ |
Alex Gaynor | ba19c2e | 2015-06-27 00:07:09 -0400 | [diff] [blame] | 1596 | if self._subject_name is None: |
| 1597 | raise ValueError("A CertificateSigningRequest must have a subject") |
Andre Caron | a33ea28 | 2015-05-31 16:32:26 -0400 | [diff] [blame] | 1598 | return backend.create_x509_csr(self, private_key, algorithm) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1599 | |
| 1600 | |
| 1601 | class CertificateBuilder(object): |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1602 | def __init__(self, version=None, issuer_name=None, subject_name=None, |
| 1603 | public_key=None, serial_number=None, not_valid_before=None, |
| 1604 | not_valid_after=None, extensions=[]): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1605 | """ |
| 1606 | Creates an empty X.509 certificate (version 1). |
| 1607 | """ |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1608 | self._version = version |
| 1609 | self._issuer_name = issuer_name |
| 1610 | self._subject_name = subject_name |
| 1611 | self._public_key = public_key |
| 1612 | self._serial_number = serial_number |
| 1613 | self._not_valid_before = not_valid_before |
| 1614 | self._not_valid_after = not_valid_after |
| 1615 | self._extensions = extensions |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1616 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1617 | def version(self, version): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1618 | """ |
| 1619 | Sets the X.509 version required by decoders. |
| 1620 | """ |
| 1621 | if not isinstance(version, Version): |
| 1622 | raise TypeError('Expecting x509.Version object.') |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1623 | if self._version is not None: |
| 1624 | raise ValueError('The version may only be set once.') |
| 1625 | return CertificateBuilder( |
| 1626 | version, self._issuer_name, self._subject_name, self._public_key, |
| 1627 | self._serial_number, self._not_valid_before, |
| 1628 | self._not_valid_after, self._extensions |
| 1629 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1630 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1631 | def issuer_name(self, name): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1632 | """ |
| 1633 | Sets the CA's distinguished name. |
| 1634 | """ |
| 1635 | if not isinstance(name, Name): |
| 1636 | raise TypeError('Expecting x509.Name object.') |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1637 | if self._issuer_name is not None: |
| 1638 | raise ValueError('The issuer name may only be set once.') |
| 1639 | return CertificateBuilder( |
| 1640 | self._version, name, self._subject_name, self._public_key, |
| 1641 | self._serial_number, self._not_valid_before, |
| 1642 | self._not_valid_after, self._extensions |
| 1643 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1644 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1645 | def subject_name(self, name): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1646 | """ |
| 1647 | Sets the requestor's distinguished name. |
| 1648 | """ |
| 1649 | if not isinstance(name, Name): |
| 1650 | raise TypeError('Expecting x509.Name object.') |
Ian Cordasco | 43ae738 | 2015-07-18 23:27:31 -0500 | [diff] [blame^] | 1651 | if self._subject_name is not None: |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1652 | raise ValueError('The subject name may only be set once.') |
| 1653 | return CertificateBuilder( |
| 1654 | self._version, self._issuer_name, name, self._public_key, |
| 1655 | self._serial_number, self._not_valid_before, |
| 1656 | self._not_valid_after, self._extensions |
| 1657 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1658 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1659 | def public_key(self, key): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1660 | """ |
| 1661 | Sets the requestor's public key (as found in the signing request). |
| 1662 | """ |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1663 | if not isinstance(key, (dsa.DSAPublicKey, rsa.RSAPublicKey, |
| 1664 | ec.EllipticCurvePublicKey)): |
| 1665 | raise TypeError('Expecting one of DSAPublicKey, RSAPublicKey,' |
| 1666 | ' or EllipticCurvePublicKey.') |
| 1667 | if self._public_key is not None: |
| 1668 | raise ValueError('The public key may only be set once.') |
| 1669 | return CertificateBuilder( |
| 1670 | self._version, self._issuer_name, self._subject_name, key, |
| 1671 | self._serial_number, self._not_valid_before, |
| 1672 | self._not_valid_after, self._extensions |
| 1673 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1674 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1675 | def serial_number(self, number): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1676 | """ |
| 1677 | Sets the certificate serial number. |
| 1678 | """ |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1679 | if not isinstance(number, six.integer_types): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1680 | raise TypeError('Serial number must be of integral type.') |
Ian Cordasco | 43ae738 | 2015-07-18 23:27:31 -0500 | [diff] [blame^] | 1681 | if self._serial_number is not None: |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1682 | raise ValueError('The serial number may only be set once.') |
| 1683 | return CertificateBuilder( |
| 1684 | self._version, self._issuer_name, self._subject_name, |
| 1685 | self._public_key, number, self._not_valid_before, |
| 1686 | self._not_valid_after, self._extensions |
| 1687 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1688 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1689 | def not_valid_before(self, time): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1690 | """ |
| 1691 | Sets the certificate activation time. |
| 1692 | """ |
| 1693 | # TODO: require UTC datetime? |
| 1694 | if not isinstance(time, datetime.datetime): |
| 1695 | raise TypeError('Expecting datetime object.') |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1696 | if self._not_valid_before is not None: |
| 1697 | raise ValueError('The not valid before may only be set once.') |
| 1698 | return CertificateBuilder( |
| 1699 | self._version, self._issuer_name, self._subject_name, |
| 1700 | self._public_key, self._serial_number, time, |
| 1701 | self._not_valid_after, self._extensions |
| 1702 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1703 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1704 | def not_valid_after(self, time): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1705 | """ |
| 1706 | Sets the certificate expiration time. |
| 1707 | """ |
| 1708 | # TODO: require UTC datetime? |
| 1709 | if not isinstance(time, datetime.datetime): |
| 1710 | raise TypeError('Expecting datetime object.') |
Ian Cordasco | 43ae738 | 2015-07-18 23:27:31 -0500 | [diff] [blame^] | 1711 | if self._not_valid_after is not None: |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1712 | raise ValueError('The not valid after may only be set once.') |
| 1713 | return CertificateBuilder( |
| 1714 | self._version, self._issuer_name, self._subject_name, |
| 1715 | self._public_key, self._serial_number, self._not_valid_before, |
| 1716 | time, self._extensions |
| 1717 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1718 | |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1719 | def add_extension(self, extension, critical): |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1720 | """ |
| 1721 | Adds an X.509 extension to the certificate. |
| 1722 | """ |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1723 | if isinstance(extension, BasicConstraints): |
| 1724 | extension = Extension(OID_BASIC_CONSTRAINTS, critical, extension) |
| 1725 | elif isinstance(extension, SubjectAlternativeName): |
| 1726 | extension = Extension( |
| 1727 | OID_SUBJECT_ALTERNATIVE_NAME, critical, extension |
| 1728 | ) |
| 1729 | else: |
| 1730 | raise NotImplementedError('Unsupported X.509 extension.') |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1731 | if not isinstance(extension, Extension): |
| 1732 | raise TypeError('Expecting x509.Extension object.') |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1733 | |
| 1734 | # TODO: This is quadratic in the number of extensions |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1735 | for e in self._extensions: |
| 1736 | if e.oid == extension.oid: |
| 1737 | raise ValueError('This extension has already been set.') |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1738 | |
| 1739 | return CertificateBuilder( |
| 1740 | self._version, self._issuer_name, self._subject_name, |
| 1741 | self._public_key, self._serial_number, self._not_valid_before, |
| 1742 | self._not_valid_after, self._extensions + [extension] |
| 1743 | ) |
Andre Caron | 9bbfcea | 2015-05-18 20:55:29 -0400 | [diff] [blame] | 1744 | |
| 1745 | def sign(self, backend, private_key, algorithm): |
| 1746 | """ |
| 1747 | Signs the certificate using the CA's private key. |
| 1748 | """ |
Ian Cordasco | 0092a0b | 2015-07-18 21:46:41 -0500 | [diff] [blame] | 1749 | builder = self |
Ian Cordasco | b3ed484 | 2015-07-01 22:46:03 -0500 | [diff] [blame] | 1750 | if self._version is None: |
Ian Cordasco | 0092a0b | 2015-07-18 21:46:41 -0500 | [diff] [blame] | 1751 | builder = self.version(Version.v3) |
| 1752 | return backend.sign_x509_certificate(builder, private_key, algorithm) |