blob: 272130241acec2cede3cae4ccb630130f567ffcc [file] [log] [blame]
wbonde91513e2015-06-03 14:52:18 -04001# coding: utf-8
wbondea25fc22015-06-19 15:07:04 -04002
3"""
4ASN.1 type classes for certificate revocation lists (CRL). Exports the
5following items:
6
7 - CertificateList()
8
9Other type classes are defined that help compose the types listed above.
10"""
11
wbond6b66ab52015-06-21 10:26:45 -040012from __future__ import unicode_literals, division, absolute_import, print_function
wbonde91513e2015-06-03 14:52:18 -040013
14from .algos import SignedDigestAlgorithm
15from .core import (
16 Boolean,
17 Enumerated,
18 GeneralizedTime,
19 Integer,
20 ObjectIdentifier,
21 OctetBitString,
22 OctetString,
23 Sequence,
24 SequenceOf,
25)
26from .x509 import (
wbonda0d45482015-07-13 22:10:20 -040027 AuthorityInfoAccessSyntax,
wbonde91513e2015-06-03 14:52:18 -040028 AuthorityKeyIdentifier,
29 CRLDistributionPoints,
30 DistributionPointName,
wbonde91513e2015-06-03 14:52:18 -040031 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
42class Version(Integer):
43 _map = {
44 0: 'v1',
45 1: 'v2',
46 2: 'v3',
47 }
48
49
50class 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
wbonde91513e2015-06-03 14:52:18 -040061class 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
73class 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
92class TBSCertListExtensions(SequenceOf):
93 _child_spec = TBSCertListExtension
94
95
96class 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
wbond2f1eb262015-07-20 08:52:34 -0400110 @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
wbonde91513e2015-06-03 14:52:18 -0400133
134class 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
142class 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
157class CRLEntryExtensions(SequenceOf):
158 _child_spec = CRLEntryExtension
159
160
161class RevokedCertificate(Sequence):
162 _fields = [
163 ('user_certificate', Integer),
164 ('revocation_date', Time),
165 ('crl_entry_extensions', CRLEntryExtensions, {'optional': True}),
166 ]
167
168
169class RevokedCertificates(SequenceOf):
170 _child_spec = RevokedCertificate
171
172
173class TbsCertList(Sequence):
174 _fields = [
175 ('version', Version, {'optional': True}),
176 ('signature', SignedDigestAlgorithm),
177 ('issuer', Name),
178 ('this_update', Time),
179 ('next_update', Time),
180 ('revoked_certificates', RevokedCertificates, {'optional': True}),
181 ('crl_extensions', TBSCertListExtensions, {'tag_type': 'explicit', 'tag': 0, 'optional': True}),
182 ]
183
184
185class CertificateList(Sequence):
186 _fields = [
187 ('tbs_cert_list', TbsCertList),
188 ('signature_algorith', SignedDigestAlgorithm),
189 ('signature', OctetBitString),
190 ]