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 the time stamp protocol (TSP). Exports the following |
| 5 | items: |
| 6 | |
| 7 | - TimeStampReq() |
| 8 | - TimeStampResp() |
| 9 | |
| 10 | Also adds TimeStampedData() support to asn1crypto.cms.ContentInfo(), |
| 11 | TimeStampedData() and TSTInfo() support to |
| 12 | asn1crypto.cms.EncapsulatedContentInfo() and some oids and value parsers to |
| 13 | asn1crypto.cms.CMSAttribute(). |
| 14 | |
| 15 | Other type classes are defined that help compose the types listed above. |
| 16 | """ |
| 17 | |
wbond | 6b66ab5 | 2015-06-21 10:26:45 -0400 | [diff] [blame] | 18 | from __future__ import unicode_literals, division, absolute_import, print_function |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 19 | |
| 20 | from .algos import DigestAlgorithm |
wbond | 5cf77ba | 2015-10-08 09:47:34 -0400 | [diff] [blame] | 21 | from .cms import ( |
| 22 | CMSAttribute, |
| 23 | CMSAttributeType, |
| 24 | ContentInfo, |
| 25 | ContentType, |
| 26 | EncapsulatedContentInfo, |
| 27 | ) |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 28 | from .core import ( |
| 29 | Any, |
| 30 | BitString, |
| 31 | Boolean, |
| 32 | Choice, |
| 33 | GeneralizedTime, |
| 34 | IA5String, |
| 35 | Integer, |
| 36 | ObjectIdentifier, |
| 37 | OctetString, |
| 38 | Sequence, |
| 39 | SequenceOf, |
| 40 | SetOf, |
| 41 | UTF8String, |
| 42 | ) |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 43 | from .crl import CertificateList |
wbond | 5cf77ba | 2015-10-08 09:47:34 -0400 | [diff] [blame] | 44 | from .x509 import ( |
| 45 | Attributes, |
| 46 | CertificatePolicies, |
| 47 | GeneralName, |
| 48 | GeneralNames, |
| 49 | ) |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 50 | |
| 51 | |
| 52 | # The structures in this file are based on https://tools.ietf.org/html/rfc3161, |
| 53 | # https://tools.ietf.org/html/rfc4998, https://tools.ietf.org/html/rfc5544, |
| 54 | # https://tools.ietf.org/html/rfc5035, https://tools.ietf.org/html/rfc2634 |
| 55 | |
| 56 | class Version(Integer): |
| 57 | _map = { |
| 58 | 0: 'v0', |
| 59 | 1: 'v1', |
| 60 | 2: 'v2', |
| 61 | 3: 'v3', |
| 62 | 4: 'v4', |
| 63 | 5: 'v5', |
| 64 | } |
| 65 | |
| 66 | |
| 67 | class MessageImprint(Sequence): |
| 68 | _fields = [ |
| 69 | ('hash_algorithm', DigestAlgorithm), |
| 70 | ('hashed_message', OctetString), |
| 71 | ] |
| 72 | |
| 73 | |
| 74 | class Accuracy(Sequence): |
| 75 | _fields = [ |
| 76 | ('seconds', Integer, {'optional': True}), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 77 | ('millis', Integer, {'implicit': 0, 'optional': True}), |
| 78 | ('micros', Integer, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 79 | ] |
| 80 | |
| 81 | |
| 82 | class Extension(Sequence): |
| 83 | _fields = [ |
| 84 | ('extn_id', ObjectIdentifier), |
| 85 | ('critical', Boolean, {'default': False}), |
| 86 | ('extn_value', OctetString), |
| 87 | ] |
| 88 | |
| 89 | |
| 90 | class Extensions(SequenceOf): |
| 91 | _child_spec = Extension |
| 92 | |
| 93 | |
| 94 | class TSTInfo(Sequence): |
| 95 | _fields = [ |
| 96 | ('version', Version), |
| 97 | ('policy', ObjectIdentifier), |
| 98 | ('message_imprint', MessageImprint), |
| 99 | ('serial_number', Integer), |
| 100 | ('gen_time', GeneralizedTime), |
| 101 | ('accuracy', Accuracy, {'optional': True}), |
| 102 | ('ordering', Boolean, {'default': False}), |
| 103 | ('nonce', Integer, {'optional': True}), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 104 | ('tsa', GeneralName, {'explicit': 0, 'optional': True}), |
| 105 | ('extensions', Extensions, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 106 | ] |
| 107 | |
| 108 | |
| 109 | class TimeStampReq(Sequence): |
| 110 | _fields = [ |
| 111 | ('version', Version), |
| 112 | ('message_imprint', MessageImprint), |
| 113 | ('req_policy', ObjectIdentifier, {'optional': True}), |
| 114 | ('nonce', Integer, {'optional': True}), |
| 115 | ('cert_req', Boolean, {'default': False}), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 116 | ('extensions', Extensions, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 117 | ] |
| 118 | |
| 119 | |
| 120 | class PKIStatus(Integer): |
| 121 | _map = { |
| 122 | 0: 'granted', |
| 123 | 1: 'granted_with_mods', |
| 124 | 2: 'rejection', |
| 125 | 3: 'waiting', |
| 126 | 4: 'revocation_warning', |
| 127 | 5: 'revocation_notification', |
| 128 | } |
| 129 | |
| 130 | |
| 131 | class PKIFreeText(SequenceOf): |
| 132 | _child_spec = UTF8String |
| 133 | |
| 134 | |
| 135 | class PKIFailureInfo(BitString): |
| 136 | _map = { |
| 137 | 0: 'bad_alg', |
| 138 | 2: 'bad_request', |
| 139 | 5: 'bad_data_format', |
| 140 | 14: 'time_not_available', |
| 141 | 15: 'unaccepted_policy', |
| 142 | 16: 'unaccepted_extensions', |
| 143 | 17: 'add_info_not_available', |
| 144 | 25: 'system_failure', |
| 145 | } |
| 146 | |
| 147 | |
| 148 | class PKIStatusInfo(Sequence): |
| 149 | _fields = [ |
| 150 | ('status', PKIStatus), |
| 151 | ('status_string', PKIFreeText, {'optional': True}), |
| 152 | ('fail_info', PKIFailureInfo, {'optional': True}), |
| 153 | ] |
| 154 | |
| 155 | |
| 156 | class TimeStampResp(Sequence): |
| 157 | _fields = [ |
| 158 | ('status', PKIStatusInfo), |
| 159 | ('time_stamp_token', ContentInfo), |
| 160 | ] |
| 161 | |
| 162 | |
| 163 | class MetaData(Sequence): |
| 164 | _fields = [ |
| 165 | ('hash_protected', Boolean), |
| 166 | ('file_name', UTF8String, {'optional': True}), |
| 167 | ('media_type', IA5String, {'optional': True}), |
| 168 | ('other_meta_data', Attributes, {'optional': True}), |
| 169 | ] |
| 170 | |
| 171 | |
| 172 | class TimeStampAndCRL(SequenceOf): |
| 173 | _fields = [ |
| 174 | ('time_stamp', EncapsulatedContentInfo), |
| 175 | ('crl', CertificateList, {'optional': True}), |
| 176 | ] |
| 177 | |
| 178 | |
| 179 | class TimeStampTokenEvidence(SequenceOf): |
| 180 | _child_spec = TimeStampAndCRL |
| 181 | |
| 182 | |
| 183 | class DigestAlgorithms(SequenceOf): |
| 184 | _child_spec = DigestAlgorithm |
| 185 | |
| 186 | |
| 187 | class EncryptionInfo(Sequence): |
| 188 | _fields = [ |
| 189 | ('encryption_info_type', ObjectIdentifier), |
| 190 | ('encryption_info_value', Any), |
| 191 | ] |
| 192 | |
| 193 | |
| 194 | class PartialHashtree(SequenceOf): |
| 195 | _child_spec = OctetString |
| 196 | |
| 197 | |
| 198 | class PartialHashtrees(SequenceOf): |
| 199 | _child_spec = PartialHashtree |
| 200 | |
| 201 | |
| 202 | class ArchiveTimeStamp(Sequence): |
| 203 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 204 | ('digest_algorithm', DigestAlgorithm, {'implicit': 0, 'optional': True}), |
| 205 | ('attributes', Attributes, {'implicit': 1, 'optional': True}), |
| 206 | ('reduced_hashtree', PartialHashtrees, {'implicit': 2, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 207 | ('time_stamp', ContentInfo), |
| 208 | ] |
| 209 | |
| 210 | |
| 211 | class ArchiveTimeStampSequence(SequenceOf): |
| 212 | _child_spec = ArchiveTimeStamp |
| 213 | |
| 214 | |
| 215 | class EvidenceRecord(Sequence): |
| 216 | _fields = [ |
| 217 | ('version', Version), |
| 218 | ('digest_algorithms', DigestAlgorithms), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 219 | ('crypto_infos', Attributes, {'implicit': 0, 'optional': True}), |
| 220 | ('encryption_info', EncryptionInfo, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 221 | ('archive_time_stamp_sequence', ArchiveTimeStampSequence), |
| 222 | ] |
| 223 | |
| 224 | |
| 225 | class OtherEvidence(Sequence): |
| 226 | _fields = [ |
| 227 | ('oe_type', ObjectIdentifier), |
| 228 | ('oe_value', Any), |
| 229 | ] |
| 230 | |
| 231 | |
| 232 | class Evidence(Choice): |
| 233 | _alternatives = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 234 | ('tst_evidence', TimeStampTokenEvidence, {'implicit': 0}), |
| 235 | ('ers_evidence', EvidenceRecord, {'implicit': 1}), |
| 236 | ('other_evidence', OtherEvidence, {'implicit': 2}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 237 | ] |
| 238 | |
| 239 | |
| 240 | class TimeStampedData(Sequence): |
| 241 | _fields = [ |
| 242 | ('version', Version), |
| 243 | ('data_uri', IA5String, {'optional': True}), |
| 244 | ('meta_data', MetaData, {'optional': True}), |
| 245 | ('content', OctetString, {'optional': True}), |
| 246 | ('temporal_evidence', Evidence), |
| 247 | ] |
| 248 | |
| 249 | |
| 250 | class IssuerSerial(Sequence): |
| 251 | _fields = [ |
| 252 | ('issuer', GeneralNames), |
| 253 | ('serial_number', Integer), |
| 254 | ] |
| 255 | |
| 256 | |
| 257 | class ESSCertID(Sequence): |
| 258 | _fields = [ |
| 259 | ('cert_hash', OctetString), |
| 260 | ('issuer_serial', IssuerSerial, {'optional': True}), |
| 261 | ] |
| 262 | |
| 263 | |
| 264 | class ESSCertIDs(SequenceOf): |
| 265 | _child_spec = ESSCertID |
| 266 | |
| 267 | |
| 268 | class SigningCertificate(Sequence): |
| 269 | _fields = [ |
| 270 | ('certs', ESSCertIDs), |
| 271 | ('policies', CertificatePolicies, {'optional': True}), |
| 272 | ] |
| 273 | |
| 274 | |
| 275 | class SetOfSigningCertificates(SetOf): |
| 276 | _child_spec = SigningCertificate |
| 277 | |
| 278 | |
| 279 | class ESSCertIDv2(Sequence): |
| 280 | _fields = [ |
wbond | be66d27 | 2016-08-27 16:00:08 -0400 | [diff] [blame] | 281 | ('hash_algorithm', DigestAlgorithm, {'default': {'algorithm': 'sha256'}}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 282 | ('cert_hash', OctetString), |
| 283 | ('issuer_serial', IssuerSerial, {'optional': True}), |
| 284 | ] |
| 285 | |
| 286 | |
| 287 | class ESSCertIDv2s(SequenceOf): |
| 288 | _child_spec = ESSCertIDv2 |
| 289 | |
| 290 | |
| 291 | class SigningCertificateV2(Sequence): |
| 292 | _fields = [ |
| 293 | ('certs', ESSCertIDv2s), |
| 294 | ('policies', CertificatePolicies, {'optional': True}), |
| 295 | ] |
| 296 | |
| 297 | |
| 298 | class SetOfSigningCertificatesV2(SetOf): |
| 299 | _child_spec = SigningCertificateV2 |
| 300 | |
| 301 | |
wbond | a26664f | 2015-10-07 11:57:35 -0400 | [diff] [blame] | 302 | EncapsulatedContentInfo._oid_specs['tst_info'] = TSTInfo |
| 303 | EncapsulatedContentInfo._oid_specs['timestamped_data'] = TimeStampedData |
| 304 | ContentInfo._oid_specs['timestamped_data'] = TimeStampedData |
| 305 | ContentType._map['1.2.840.113549.1.9.16.1.4'] = 'tst_info' |
| 306 | ContentType._map['1.2.840.113549.1.9.16.1.31'] = 'timestamped_data' |
| 307 | CMSAttributeType._map['1.2.840.113549.1.9.16.2.12'] = 'signing_certificate' |
| 308 | CMSAttribute._oid_specs['signing_certificate'] = SetOfSigningCertificates |
| 309 | CMSAttributeType._map['1.2.840.113549.1.9.16.2.47'] = 'signing_certificate_v2' |
| 310 | CMSAttribute._oid_specs['signing_certificate_v2'] = SetOfSigningCertificatesV2 |