wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 1 | # coding: utf-8 |
wbond | ea25fc2 | 2015-06-19 15:07:04 -0400 | [diff] [blame] | 2 | |
| 3 | """ |
| 4 | ASN.1 type classes for certificate revocation lists (CRL). Exports the |
| 5 | following items: |
| 6 | |
| 7 | - CertificateList() |
| 8 | |
| 9 | Other type classes are defined that help compose the types listed above. |
| 10 | """ |
| 11 | |
wbond | 6b66ab5 | 2015-06-21 10:26:45 -0400 | [diff] [blame] | 12 | from __future__ import unicode_literals, division, absolute_import, print_function |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 13 | |
| 14 | from .algos import SignedDigestAlgorithm |
| 15 | from .core import ( |
| 16 | Boolean, |
| 17 | Enumerated, |
| 18 | GeneralizedTime, |
| 19 | Integer, |
| 20 | ObjectIdentifier, |
| 21 | OctetBitString, |
| 22 | OctetString, |
| 23 | Sequence, |
| 24 | SequenceOf, |
| 25 | ) |
| 26 | from .x509 import ( |
wbond | a0d4548 | 2015-07-13 22:10:20 -0400 | [diff] [blame] | 27 | AuthorityInfoAccessSyntax, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 28 | AuthorityKeyIdentifier, |
| 29 | CRLDistributionPoints, |
| 30 | DistributionPointName, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 31 | GeneralNames, |
| 32 | Name, |
| 33 | ReasonFlags, |
| 34 | Time, |
| 35 | ) |
| 36 | |
| 37 | |
| 38 | |
| 39 | # The structures in this file are taken from https://tools.ietf.org/html/rfc5280 |
| 40 | |
| 41 | |
| 42 | class Version(Integer): |
| 43 | _map = { |
| 44 | 0: 'v1', |
| 45 | 1: 'v2', |
| 46 | 2: 'v3', |
| 47 | } |
| 48 | |
| 49 | |
| 50 | class IssuingDistributionPoint(Sequence): |
| 51 | _fields = [ |
| 52 | ('distribution_point', DistributionPointName, {'tag_type': 'explicit', 'tag': 0, 'optional': True}), |
| 53 | ('only_contains_user_certs', Boolean, {'tag_type': 'implicit', 'tag': 1, 'default': False}), |
| 54 | ('only_contains_ca_certs', Boolean, {'tag_type': 'implicit', 'tag': 2, 'default': False}), |
| 55 | ('only_some_reasons', ReasonFlags, {'tag_type': 'implicit', 'tag': 3, 'optional': True}), |
| 56 | ('indirect_crl', Boolean, {'tag_type': 'implicit', 'tag': 4, 'default': False}), |
| 57 | ('only_contains_attribute_certs', Boolean, {'tag_type': 'implicit', 'tag': 5, 'default': False}), |
| 58 | ] |
| 59 | |
| 60 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 61 | class TBSCertListExtensionId(ObjectIdentifier): |
| 62 | _map = { |
| 63 | '2.5.29.18': 'issuer_alt_name', |
| 64 | '2.5.29.20': 'crl_number', |
| 65 | '2.5.29.27': 'delta_crl_indicator', |
| 66 | '2.5.29.28': 'issuing_distribution_point', |
| 67 | '2.5.29.35': 'authority_key_identifier', |
| 68 | '2.5.29.46': 'freshest_crl', |
| 69 | '1.3.6.1.5.5.7.1.1': 'authority_information_access', |
| 70 | } |
| 71 | |
| 72 | |
| 73 | class TBSCertListExtension(Sequence): |
| 74 | _fields = [ |
| 75 | ('extn_id', TBSCertListExtensionId), |
| 76 | ('critical', Boolean, {'default': False}), |
| 77 | ('extn_value', OctetString), |
| 78 | ] |
| 79 | |
| 80 | _oid_pair = ('extn_id', 'extn_value') |
| 81 | _oid_specs = { |
| 82 | 'issuer_alt_name': GeneralNames, |
| 83 | 'crl_number': Integer, |
| 84 | 'delta_crl_indicator': Integer, |
| 85 | 'issuing_distribution_point': IssuingDistributionPoint, |
| 86 | 'authority_key_identifier': AuthorityKeyIdentifier, |
| 87 | 'freshest_crl': CRLDistributionPoints, |
| 88 | 'authority_information_access': AuthorityInfoAccessSyntax, |
| 89 | } |
| 90 | |
| 91 | |
| 92 | class TBSCertListExtensions(SequenceOf): |
| 93 | _child_spec = TBSCertListExtension |
| 94 | |
| 95 | |
| 96 | class CRLReason(Enumerated): |
| 97 | _map = { |
| 98 | 0: 'unspecified', |
| 99 | 1: 'key_compromise', |
| 100 | 2: 'ca_compromise', |
| 101 | 3: 'affiliation_changed', |
| 102 | 4: 'superseded', |
| 103 | 5: 'cessation_of_operation', |
| 104 | 6: 'certificate_hold', |
| 105 | 8: 'remove_from_crl', |
| 106 | 9: 'privilege_withdrawn', |
| 107 | 10: 'aa_compromise', |
| 108 | } |
| 109 | |
wbond | 2f1eb26 | 2015-07-20 08:52:34 -0400 | [diff] [blame] | 110 | @property |
| 111 | def human_friendly(self): |
| 112 | """ |
| 113 | :return: |
| 114 | A unicode string with revocation description that is suitable to |
| 115 | show to end-users. Starts with a lower case letter and phrased in |
| 116 | such a way that it makes sense after the phrase "because of" or |
| 117 | "due to". |
| 118 | """ |
| 119 | |
| 120 | return { |
| 121 | 'unspecified': 'an unspecified reason', |
| 122 | 'key_compromise': 'a compromised key', |
| 123 | 'ca_compromise': 'the CA being compromised', |
| 124 | 'affiliation_changed': 'an affiliation change', |
| 125 | 'superseded': 'certificate supersession', |
| 126 | 'cessation_of_operation': 'a cessation of operation', |
| 127 | 'certificate_hold': 'a certificate hold', |
| 128 | 'remove_from_crl': 'removal from the CRL', |
| 129 | 'privilege_withdrawn': 'privilege withdrawl', |
| 130 | 'aa_compromise': 'the AA being compromised', |
| 131 | }[self.native] |
| 132 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 133 | |
| 134 | class CRLEntryExtensionId(ObjectIdentifier): |
| 135 | _map = { |
| 136 | '2.5.29.21': 'crl_reason', |
| 137 | '2.5.29.24': 'invalidity_date', |
| 138 | '2.5.29.29': 'certificate_issuer', |
| 139 | } |
| 140 | |
| 141 | |
| 142 | class CRLEntryExtension(Sequence): |
| 143 | _fields = [ |
| 144 | ('extn_id', CRLEntryExtensionId), |
| 145 | ('critical', Boolean, {'default': False}), |
| 146 | ('extn_value', OctetString), |
| 147 | ] |
| 148 | |
| 149 | _oid_pair = ('extn_id', 'extn_value') |
| 150 | _oid_specs = { |
| 151 | 'crl_reason': CRLReason, |
| 152 | 'invalidity_date': GeneralizedTime, |
| 153 | 'certificate_issuer': GeneralNames, |
| 154 | } |
| 155 | |
| 156 | |
| 157 | class CRLEntryExtensions(SequenceOf): |
| 158 | _child_spec = CRLEntryExtension |
| 159 | |
| 160 | |
| 161 | class RevokedCertificate(Sequence): |
| 162 | _fields = [ |
| 163 | ('user_certificate', Integer), |
| 164 | ('revocation_date', Time), |
| 165 | ('crl_entry_extensions', CRLEntryExtensions, {'optional': True}), |
| 166 | ] |
| 167 | |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 168 | _processed_extensions = False |
| 169 | _critical_extensions = None |
| 170 | _crl_reason_value = None |
| 171 | _invalidity_date_value = None |
| 172 | _certificate_issuer_value = None |
wbond | 5fb733f | 2015-07-21 14:57:59 -0400 | [diff] [blame] | 173 | _issuer_name = False |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 174 | |
| 175 | def _set_extensions(self): |
| 176 | """ |
| 177 | Sets common named extensions to private attributes and creates a list |
| 178 | of critical extensions |
| 179 | """ |
| 180 | |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 181 | self._critical_extensions = set() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 182 | |
| 183 | for extension in self['crl_entry_extensions']: |
| 184 | name = extension['extn_id'].native |
| 185 | attribute_name = '_%s_value' % name |
| 186 | if hasattr(self, attribute_name): |
| 187 | setattr(self, attribute_name, extension['extn_value'].parsed) |
| 188 | if extension['critical'].native: |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 189 | self._critical_extensions.add(name) |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 190 | |
| 191 | self._processed_extensions = True |
| 192 | |
| 193 | @property |
| 194 | def critical_extensions(self): |
| 195 | """ |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 196 | Returns a set of the names (or OID if not a known extension) of the |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 197 | extensions marked as critical |
| 198 | |
| 199 | :return: |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 200 | A set of unicode strings |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 201 | """ |
| 202 | |
| 203 | if not self._processed_extensions: |
| 204 | self._set_extensions() |
| 205 | return self._critical_extensions |
| 206 | |
| 207 | @property |
| 208 | def crl_reason_value(self): |
| 209 | """ |
| 210 | This extension indicates the reason that a certificate was revoked. |
| 211 | |
| 212 | :return: |
| 213 | None or a CRLReason object |
| 214 | """ |
| 215 | |
| 216 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 217 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 218 | return self._crl_reason_value |
| 219 | |
| 220 | @property |
| 221 | def invalidity_date_value(self): |
| 222 | """ |
| 223 | This extension indicates the suspected date/time the private key was |
| 224 | compromised or the certificate became invalid. This would usually be |
| 225 | before the revocation date, which is when the CA processed the |
| 226 | revocation. |
| 227 | |
| 228 | :return: |
| 229 | None or a GeneralizedTime object |
| 230 | """ |
| 231 | |
| 232 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 233 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 234 | return self._invalidity_date_value |
| 235 | |
| 236 | @property |
| 237 | def certificate_issuer_value(self): |
| 238 | """ |
| 239 | This extension indicates the issuer of the certificate in question, |
| 240 | and is used in indirect CRLs. CRL entries without this extension are |
| 241 | for certificates issued from the last seen issuer. |
| 242 | |
| 243 | :return: |
| 244 | None or an x509.GeneralNames object |
| 245 | """ |
| 246 | |
| 247 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 248 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 249 | return self._certificate_issuer_value |
| 250 | |
wbond | 5fb733f | 2015-07-21 14:57:59 -0400 | [diff] [blame] | 251 | @property |
| 252 | def issuer_name(self): |
| 253 | """ |
| 254 | :return: |
| 255 | None, or an asn1crypto.x509.Name object for the issuer of the cert |
| 256 | """ |
| 257 | |
| 258 | if self._issuer_name is False: |
| 259 | self._issuer_name = None |
| 260 | if self.certificate_issuer_value: |
| 261 | for general_name in self.certificate_issuer_value: |
| 262 | if general_name.name == 'directory_name': |
| 263 | self._issuer_name = general_name.chosen |
| 264 | break |
| 265 | return self._issuer_name |
| 266 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 267 | |
| 268 | class RevokedCertificates(SequenceOf): |
| 269 | _child_spec = RevokedCertificate |
| 270 | |
| 271 | |
| 272 | class TbsCertList(Sequence): |
| 273 | _fields = [ |
| 274 | ('version', Version, {'optional': True}), |
| 275 | ('signature', SignedDigestAlgorithm), |
| 276 | ('issuer', Name), |
| 277 | ('this_update', Time), |
| 278 | ('next_update', Time), |
| 279 | ('revoked_certificates', RevokedCertificates, {'optional': True}), |
| 280 | ('crl_extensions', TBSCertListExtensions, {'tag_type': 'explicit', 'tag': 0, 'optional': True}), |
| 281 | ] |
| 282 | |
| 283 | |
| 284 | class CertificateList(Sequence): |
| 285 | _fields = [ |
| 286 | ('tbs_cert_list', TbsCertList), |
wbond | 4a2627f | 2015-07-21 14:56:48 -0400 | [diff] [blame] | 287 | ('signature_algorithm', SignedDigestAlgorithm), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 288 | ('signature', OctetBitString), |
| 289 | ] |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 290 | |
| 291 | _processed_extensions = False |
| 292 | _critical_extensions = None |
wbond | a613f81 | 2015-07-20 10:13:47 -0400 | [diff] [blame] | 293 | _issuer_alt_name_value = None |
| 294 | _crl_number_value = None |
| 295 | _delta_crl_indicator_value = None |
| 296 | _issuing_distribution_point_value = None |
| 297 | _authority_key_identifier_value = None |
| 298 | _freshest_crl_value = None |
| 299 | _authority_information_access_value = None |
wbond | 5fb733f | 2015-07-21 14:57:59 -0400 | [diff] [blame] | 300 | _issuer_cert_urls = None |
| 301 | _delta_crl_distribution_points = None |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 302 | |
| 303 | def _set_extensions(self): |
| 304 | """ |
| 305 | Sets common named extensions to private attributes and creates a list |
| 306 | of critical extensions |
| 307 | """ |
| 308 | |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 309 | self._critical_extensions = set() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 310 | |
| 311 | for extension in self['tbs_cert_list']['crl_extensions']: |
| 312 | name = extension['extn_id'].native |
| 313 | attribute_name = '_%s_value' % name |
| 314 | if hasattr(self, attribute_name): |
| 315 | setattr(self, attribute_name, extension['extn_value'].parsed) |
| 316 | if extension['critical'].native: |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 317 | self._critical_extensions.add(name) |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 318 | |
| 319 | self._processed_extensions = True |
| 320 | |
| 321 | @property |
| 322 | def critical_extensions(self): |
| 323 | """ |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 324 | Returns a set of the names (or OID if not a known extension) of the |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 325 | extensions marked as critical |
| 326 | |
| 327 | :return: |
wbond | 2fde645 | 2015-07-23 10:54:13 -0400 | [diff] [blame^] | 328 | A set of unicode strings |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 329 | """ |
| 330 | |
| 331 | if not self._processed_extensions: |
| 332 | self._set_extensions() |
| 333 | return self._critical_extensions |
| 334 | |
| 335 | @property |
| 336 | def issuer_alt_name_value(self): |
| 337 | """ |
| 338 | This extension allows associating one or more alternative names with |
| 339 | the issuer of the CRL. |
| 340 | |
| 341 | :return: |
| 342 | None or an x509.GeneralNames object |
| 343 | """ |
| 344 | |
| 345 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 346 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 347 | return self._issuer_alt_name_value |
| 348 | |
| 349 | @property |
| 350 | def crl_number_value(self): |
| 351 | """ |
| 352 | This extension adds a monotonically increasing number to the CRL and is |
| 353 | used to distinguish different versions of the CRL. |
| 354 | |
| 355 | :return: |
| 356 | None or an Integer object |
| 357 | """ |
| 358 | |
| 359 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 360 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 361 | return self._crl_number_value |
| 362 | |
| 363 | @property |
| 364 | def delta_crl_indicator_value(self): |
| 365 | """ |
| 366 | This extension indicates a CRL is a delta CRL, and contains the CRL |
| 367 | number of the base CRL that it is a delta from. |
| 368 | |
| 369 | :return: |
| 370 | None or an Integer object |
| 371 | """ |
| 372 | |
| 373 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 374 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 375 | return self._delta_crl_indicator_value |
| 376 | |
| 377 | @property |
| 378 | def issuing_distribution_point_value(self): |
| 379 | """ |
| 380 | This extension includes information about what types of revocations |
| 381 | and certificates are part of the CRL. |
| 382 | |
| 383 | :return: |
| 384 | None or an IssuingDistributionPoint object |
| 385 | """ |
| 386 | |
| 387 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 388 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 389 | return self._issuing_distribution_point_value |
| 390 | |
| 391 | @property |
| 392 | def authority_key_identifier_value(self): |
| 393 | """ |
| 394 | This extension helps in identifying the public key with which to |
| 395 | validate the authenticity of the CRL. |
| 396 | |
| 397 | :return: |
| 398 | None or an AuthorityKeyIdentifier object |
| 399 | """ |
| 400 | |
| 401 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 402 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 403 | return self._authority_key_identifier_value |
| 404 | |
| 405 | @property |
| 406 | def freshest_crl_value(self): |
| 407 | """ |
| 408 | This extension is used in complete CRLs to indicate where a delta CRL |
| 409 | may be located. |
| 410 | |
| 411 | :return: |
| 412 | None or a CRLDistributionPoints object |
| 413 | """ |
| 414 | |
| 415 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 416 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 417 | return self._freshest_crl_value |
| 418 | |
| 419 | @property |
| 420 | def authority_information_access_value(self): |
| 421 | """ |
| 422 | This extension is used to provide a URL with which to download the |
| 423 | certificate used to sign this CRL. |
| 424 | |
| 425 | :return: |
| 426 | None or an AuthorityInfoAccessSyntax object |
| 427 | """ |
| 428 | |
| 429 | if self._processed_extensions is False: |
wbond | ad218f9 | 2015-07-20 10:43:16 -0400 | [diff] [blame] | 430 | self._set_extensions() |
wbond | 598c228 | 2015-07-20 08:54:22 -0400 | [diff] [blame] | 431 | return self._authority_information_access_value |
wbond | 5fb733f | 2015-07-21 14:57:59 -0400 | [diff] [blame] | 432 | |
| 433 | @property |
| 434 | def authority_key_identifier(self): |
| 435 | """ |
| 436 | :return: |
| 437 | None or a byte string of the key_identifier from the authority key |
| 438 | identifier extension |
| 439 | """ |
| 440 | |
| 441 | if not self.authority_key_identifier_value: |
| 442 | return None |
| 443 | |
| 444 | return self.authority_key_identifier_value['key_identifier'].native |
| 445 | |
| 446 | @property |
| 447 | def issuer_cert_urls(self): |
| 448 | """ |
| 449 | :return: |
| 450 | A list of unicode strings that are URLs that should contain either |
| 451 | an individual DER-encoded X509 certificate, or a DER-encoded CMS |
| 452 | message containing multiple certificates |
| 453 | """ |
| 454 | |
| 455 | if self._issuer_cert_urls is None: |
| 456 | self._issuer_cert_urls = [] |
| 457 | if self.authority_information_access_value: |
| 458 | for entry in self.authority_information_access_value: |
| 459 | if entry['access_method'].native == 'ca_issuers': |
| 460 | location = entry['access_location'] |
| 461 | if location.name != 'uniform_resource_identifier': |
| 462 | continue |
| 463 | url = location.native |
| 464 | if url.lower()[0:7] == 'http://': |
| 465 | self._issuer_cert_urls.append(url) |
| 466 | return self._issuer_cert_urls |
| 467 | |
| 468 | @property |
| 469 | def delta_crl_distribution_points(self): |
| 470 | """ |
| 471 | Returns delta CRL URLs - only applies to complete CRLs |
| 472 | |
| 473 | :return: |
| 474 | A list of zero or more DistributionPoint objects |
| 475 | """ |
| 476 | |
| 477 | if self._delta_crl_distribution_points is None: |
| 478 | self._delta_crl_distribution_points = [] |
| 479 | |
| 480 | if self.freshest_crl_value is not None: |
| 481 | for distribution_point in self.freshest_crl_value: |
| 482 | distribution_point_name = distribution_point['distribution_point'] |
| 483 | # RFC5280 indicates conforming CA should not use the relative form |
| 484 | if distribution_point_name.name == 'name_relative_to_crl_issuer': |
| 485 | continue |
| 486 | # This library is currently only concerned with HTTP-based CRLs |
| 487 | for general_name in distribution_point_name.chosen: |
| 488 | if general_name.name == 'uniform_resource_identifier': |
| 489 | self._delta_crl_distribution_points.append(distribution_point) |
| 490 | |
| 491 | return self._delta_crl_distribution_points |