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 | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 8 | from enum import Enum |
| 9 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 10 | import six |
| 11 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 12 | from cryptography import utils |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 13 | from cryptography.hazmat.primitives import hashes |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 14 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 15 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 16 | _OID_NAMES = { |
| 17 | "2.5.4.3": "commonName", |
| 18 | "2.5.4.6": "countryName", |
| 19 | "2.5.4.7": "localityName", |
| 20 | "2.5.4.8": "stateOrProvinceName", |
| 21 | "2.5.4.10": "organizationName", |
| 22 | "2.5.4.11": "organizationalUnitName", |
| 23 | "2.5.4.5": "serialNumber", |
| 24 | "2.5.4.4": "surname", |
| 25 | "2.5.4.42": "givenName", |
| 26 | "2.5.4.12": "title", |
| 27 | "2.5.4.44": "generationQualifier", |
| 28 | "2.5.4.46": "dnQualifier", |
| 29 | "2.5.4.65": "pseudonym", |
| 30 | "0.9.2342.19200300.100.1.25": "domainComponent", |
| 31 | "1.2.840.113549.1.9.1": "emailAddress", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 32 | "1.2.840.113549.1.1.4": "md5WithRSAEncryption", |
| 33 | "1.2.840.113549.1.1.5": "sha1WithRSAEncryption", |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 34 | "1.2.840.113549.1.1.14": "sha224WithRSAEncryption", |
| 35 | "1.2.840.113549.1.1.11": "sha256WithRSAEncryption", |
| 36 | "1.2.840.113549.1.1.12": "sha384WithRSAEncryption", |
| 37 | "1.2.840.113549.1.1.13": "sha512WithRSAEncryption", |
Paul Kehrer | 71d40c6 | 2015-02-19 08:21:04 -0600 | [diff] [blame] | 38 | "1.2.840.10045.4.3.1": "ecdsa-with-SHA224", |
| 39 | "1.2.840.10045.4.3.2": "ecdsa-with-SHA256", |
| 40 | "1.2.840.10045.4.3.3": "ecdsa-with-SHA384", |
| 41 | "1.2.840.10045.4.3.4": "ecdsa-with-SHA512", |
| 42 | "1.2.840.10040.4.3": "dsa-with-sha1", |
| 43 | "2.16.840.1.101.3.4.3.1": "dsa-with-sha224", |
| 44 | "2.16.840.1.101.3.4.3.2": "dsa-with-sha256", |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 48 | class Version(Enum): |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 49 | v1 = 0 |
| 50 | v3 = 2 |
| 51 | |
| 52 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 53 | def load_pem_x509_certificate(data, backend): |
| 54 | return backend.load_pem_x509_certificate(data) |
| 55 | |
| 56 | |
Paul Kehrer | 016e08a | 2014-11-26 09:41:18 -1000 | [diff] [blame] | 57 | def load_der_x509_certificate(data, backend): |
| 58 | return backend.load_der_x509_certificate(data) |
Paul Kehrer | a68fd33 | 2014-11-27 07:08:40 -1000 | [diff] [blame] | 59 | |
| 60 | |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 61 | class InvalidVersion(Exception): |
Paul Kehrer | d5cccf7 | 2014-12-15 17:20:33 -0600 | [diff] [blame] | 62 | def __init__(self, msg, parsed_version): |
| 63 | super(InvalidVersion, self).__init__(msg) |
| 64 | self.parsed_version = parsed_version |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 65 | |
| 66 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 67 | class NameAttribute(object): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 68 | def __init__(self, oid, value): |
| 69 | if not isinstance(oid, ObjectIdentifier): |
Paul Kehrer | 858b9b7 | 2015-02-05 09:50:31 -0600 | [diff] [blame] | 70 | raise TypeError( |
| 71 | "oid argument must be an ObjectIdentifier instance." |
| 72 | ) |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 73 | |
| 74 | self._oid = oid |
| 75 | self._value = value |
| 76 | |
| 77 | oid = utils.read_only_property("_oid") |
| 78 | value = utils.read_only_property("_value") |
| 79 | |
| 80 | def __eq__(self, other): |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 81 | if not isinstance(other, NameAttribute): |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 82 | return NotImplemented |
| 83 | |
| 84 | return ( |
| 85 | self.oid == other.oid and |
| 86 | self.value == other.value |
| 87 | ) |
| 88 | |
| 89 | def __ne__(self, other): |
| 90 | return not self == other |
| 91 | |
Paul Kehrer | a498be8 | 2015-02-12 15:00:56 -0600 | [diff] [blame] | 92 | def __repr__(self): |
| 93 | return "<NameAttribute(oid={oid}, value={value!r})>".format( |
| 94 | oid=self.oid, |
| 95 | value=self.value |
| 96 | ) |
| 97 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 98 | |
| 99 | class ObjectIdentifier(object): |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 100 | def __init__(self, dotted_string): |
| 101 | self._dotted_string = dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 102 | |
| 103 | def __eq__(self, other): |
| 104 | if not isinstance(other, ObjectIdentifier): |
| 105 | return NotImplemented |
| 106 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 107 | return self._dotted_string == other._dotted_string |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 108 | |
| 109 | def __ne__(self, other): |
| 110 | return not self == other |
| 111 | |
| 112 | def __repr__(self): |
| 113 | return "<ObjectIdentifier(oid={0}, name={1})>".format( |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 114 | self._dotted_string, |
| 115 | _OID_NAMES.get(self._dotted_string, "Unknown OID") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 116 | ) |
| 117 | |
Paul Kehrer | d44f9a6 | 2015-02-04 14:47:34 -0600 | [diff] [blame] | 118 | dotted_string = utils.read_only_property("_dotted_string") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 119 | |
| 120 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 121 | class Name(object): |
| 122 | def __init__(self, attributes): |
| 123 | self._attributes = attributes |
| 124 | |
Paul Kehrer | e901d64 | 2015-02-11 18:50:58 -0600 | [diff] [blame] | 125 | def get_attributes_for_oid(self, oid): |
Alex Gaynor | f957423 | 2015-02-19 13:56:50 -0800 | [diff] [blame] | 126 | return [i for i in self if i.oid == oid] |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 127 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 128 | def __eq__(self, other): |
| 129 | if not isinstance(other, Name): |
| 130 | return NotImplemented |
| 131 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 132 | return self._attributes == other._attributes |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 133 | |
| 134 | def __ne__(self, other): |
| 135 | return not self == other |
| 136 | |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 137 | def __iter__(self): |
Paul Kehrer | 8b21a4a | 2015-02-14 07:56:36 -0600 | [diff] [blame] | 138 | return iter(self._attributes) |
Paul Kehrer | 53d8d49 | 2015-02-13 18:47:30 -0600 | [diff] [blame] | 139 | |
| 140 | def __len__(self): |
| 141 | return len(self._attributes) |
| 142 | |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 143 | |
Paul Kehrer | 8cf2642 | 2015-03-21 09:50:24 -0500 | [diff] [blame^] | 144 | OID_BASIC_CONSTRAINTS = ObjectIdentifier("2.5.29.19") |
| 145 | |
| 146 | |
| 147 | @six.add_metaclass(abc.ABCMeta) |
| 148 | class Extension(object): |
| 149 | @abc.abstractproperty |
| 150 | def critical(self): |
| 151 | """ |
| 152 | Returns the boolean value of the critical extension field. |
| 153 | """ |
| 154 | |
| 155 | |
| 156 | @utils.register_interface(Extension) |
| 157 | class BasicConstraints(object): |
| 158 | oid = OID_BASIC_CONSTRAINTS |
| 159 | |
| 160 | def __init__(self, ca, path_length, critical): |
| 161 | if not isinstance(ca, bool): |
| 162 | raise TypeError("ca must be a boolean value") |
| 163 | |
| 164 | if not isinstance(critical, bool): |
| 165 | raise TypeError("critical must be a boolean value") |
| 166 | |
| 167 | if path_length is not None and ca is False: |
| 168 | raise ValueError("path_length must be None when ca is False") |
| 169 | |
| 170 | if path_length is not None and (not isinstance(path_length, int) |
| 171 | or path_length < 0): |
| 172 | raise TypeError( |
| 173 | "path_length must be a non-negative integer or None" |
| 174 | ) |
| 175 | |
| 176 | self._ca = ca |
| 177 | self._path_length = path_length |
| 178 | self._critical = critical |
| 179 | |
| 180 | ca = utils.read_only_property("_ca") |
| 181 | path_length = utils.read_only_property("_path_length") |
| 182 | critical = utils.read_only_property("_critical") |
| 183 | |
| 184 | def __repr__(self): |
| 185 | return "<BasicConstraints(ca={}, path_length={}, critical={})>".format( |
| 186 | self.ca, self.path_length, self.critical |
| 187 | ) |
| 188 | |
| 189 | |
Paul Kehrer | 806bfb2 | 2015-02-02 17:05:24 -0600 | [diff] [blame] | 190 | OID_COMMON_NAME = ObjectIdentifier("2.5.4.3") |
| 191 | OID_COUNTRY_NAME = ObjectIdentifier("2.5.4.6") |
| 192 | OID_LOCALITY_NAME = ObjectIdentifier("2.5.4.7") |
| 193 | OID_STATE_OR_PROVINCE_NAME = ObjectIdentifier("2.5.4.8") |
| 194 | OID_ORGANIZATION_NAME = ObjectIdentifier("2.5.4.10") |
| 195 | OID_ORGANIZATIONAL_UNIT_NAME = ObjectIdentifier("2.5.4.11") |
| 196 | OID_SERIAL_NUMBER = ObjectIdentifier("2.5.4.5") |
| 197 | OID_SURNAME = ObjectIdentifier("2.5.4.4") |
| 198 | OID_GIVEN_NAME = ObjectIdentifier("2.5.4.42") |
| 199 | OID_TITLE = ObjectIdentifier("2.5.4.12") |
| 200 | OID_GENERATION_QUALIFIER = ObjectIdentifier("2.5.4.44") |
| 201 | OID_DN_QUALIFIER = ObjectIdentifier("2.5.4.46") |
| 202 | OID_PSEUDONYM = ObjectIdentifier("2.5.4.65") |
| 203 | OID_DOMAIN_COMPONENT = ObjectIdentifier("0.9.2342.19200300.100.1.25") |
| 204 | OID_EMAIL_ADDRESS = ObjectIdentifier("1.2.840.113549.1.9.1") |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 205 | |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 206 | OID_RSA_WITH_MD5 = ObjectIdentifier("1.2.840.113549.1.1.4") |
| 207 | OID_RSA_WITH_SHA1 = ObjectIdentifier("1.2.840.113549.1.1.5") |
| 208 | OID_RSA_WITH_SHA224 = ObjectIdentifier("1.2.840.113549.1.1.14") |
| 209 | OID_RSA_WITH_SHA256 = ObjectIdentifier("1.2.840.113549.1.1.11") |
| 210 | OID_RSA_WITH_SHA384 = ObjectIdentifier("1.2.840.113549.1.1.12") |
| 211 | OID_RSA_WITH_SHA512 = ObjectIdentifier("1.2.840.113549.1.1.13") |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 212 | OID_ECDSA_WITH_SHA224 = ObjectIdentifier("1.2.840.10045.4.3.1") |
| 213 | OID_ECDSA_WITH_SHA256 = ObjectIdentifier("1.2.840.10045.4.3.2") |
| 214 | OID_ECDSA_WITH_SHA384 = ObjectIdentifier("1.2.840.10045.4.3.3") |
| 215 | OID_ECDSA_WITH_SHA512 = ObjectIdentifier("1.2.840.10045.4.3.4") |
| 216 | OID_DSA_WITH_SHA1 = ObjectIdentifier("1.2.840.10040.4.3") |
| 217 | OID_DSA_WITH_SHA224 = ObjectIdentifier("2.16.840.1.101.3.4.3.1") |
| 218 | OID_DSA_WITH_SHA256 = ObjectIdentifier("2.16.840.1.101.3.4.3.2") |
| 219 | |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 220 | _SIG_OIDS_TO_HASH = { |
Paul Kehrer | 1a7ba87 | 2015-02-19 18:09:05 -0600 | [diff] [blame] | 221 | OID_RSA_WITH_MD5.dotted_string: hashes.MD5(), |
| 222 | OID_RSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 223 | OID_RSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 224 | OID_RSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 225 | OID_RSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 226 | OID_RSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
Paul Kehrer | 42a87cb | 2015-02-13 18:53:24 -0600 | [diff] [blame] | 227 | OID_ECDSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 228 | OID_ECDSA_WITH_SHA256.dotted_string: hashes.SHA256(), |
| 229 | OID_ECDSA_WITH_SHA384.dotted_string: hashes.SHA384(), |
| 230 | OID_ECDSA_WITH_SHA512.dotted_string: hashes.SHA512(), |
| 231 | OID_DSA_WITH_SHA1.dotted_string: hashes.SHA1(), |
| 232 | OID_DSA_WITH_SHA224.dotted_string: hashes.SHA224(), |
| 233 | OID_DSA_WITH_SHA256.dotted_string: hashes.SHA256() |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 234 | } |
| 235 | |
Paul Kehrer | 912d3fb | 2015-01-29 11:19:22 -0600 | [diff] [blame] | 236 | |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 237 | @six.add_metaclass(abc.ABCMeta) |
Paul Kehrer | e76cd27 | 2014-12-14 19:00:51 -0600 | [diff] [blame] | 238 | class Certificate(object): |
Paul Kehrer | b2de948 | 2014-12-11 14:54:48 -0600 | [diff] [blame] | 239 | @abc.abstractmethod |
| 240 | def fingerprint(self, algorithm): |
| 241 | """ |
| 242 | Returns bytes using digest passed. |
| 243 | """ |
| 244 | |
| 245 | @abc.abstractproperty |
| 246 | def serial(self): |
| 247 | """ |
| 248 | Returns certificate serial number |
| 249 | """ |
| 250 | |
| 251 | @abc.abstractproperty |
| 252 | def version(self): |
| 253 | """ |
| 254 | Returns the certificate version |
| 255 | """ |
| 256 | |
| 257 | @abc.abstractmethod |
| 258 | def public_key(self): |
| 259 | """ |
| 260 | Returns the public key |
| 261 | """ |
| 262 | |
| 263 | @abc.abstractproperty |
| 264 | def not_valid_before(self): |
| 265 | """ |
| 266 | Not before time (represented as UTC datetime) |
| 267 | """ |
| 268 | |
| 269 | @abc.abstractproperty |
| 270 | def not_valid_after(self): |
| 271 | """ |
| 272 | Not after time (represented as UTC datetime) |
| 273 | """ |
Paul Kehrer | 719d536 | 2015-01-01 20:03:52 -0600 | [diff] [blame] | 274 | |
| 275 | @abc.abstractproperty |
| 276 | def issuer(self): |
| 277 | """ |
| 278 | Returns the issuer name object. |
| 279 | """ |
| 280 | |
| 281 | @abc.abstractproperty |
| 282 | def subject(self): |
| 283 | """ |
| 284 | Returns the subject name object. |
| 285 | """ |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 286 | |
| 287 | @abc.abstractproperty |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 288 | def signature_hash_algorithm(self): |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 289 | """ |
Paul Kehrer | 8802a5b | 2015-02-13 12:06:57 -0600 | [diff] [blame] | 290 | Returns a HashAlgorithm corresponding to the type of the digest signed |
| 291 | in the certificate. |
Paul Kehrer | 56da2a5 | 2015-02-11 23:35:07 -0600 | [diff] [blame] | 292 | """ |