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