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 cryptographic message syntax (CMS). Structures are also |
| 5 | compatible with PKCS#7. Exports the following items: |
| 6 | |
| 7 | - AuthenticatedData() |
| 8 | - AuthEnvelopedData() |
| 9 | - CompressedData() |
| 10 | - ContentInfo() |
Jörn Heissler | fb9c136 | 2016-07-29 03:59:02 +0200 | [diff] [blame] | 11 | - DigestedData() |
wbond | ea25fc2 | 2015-06-19 15:07:04 -0400 | [diff] [blame] | 12 | - EncryptedData() |
| 13 | - EnvelopedData() |
wbond | ea25fc2 | 2015-06-19 15:07:04 -0400 | [diff] [blame] | 14 | - SignedAndEnvelopedData() |
| 15 | - SignedData() |
| 16 | |
| 17 | Other type classes are defined that help compose the types listed above. |
Jörn Heissler | 63fa2f5 | 2017-11-14 20:11:12 +0100 | [diff] [blame] | 18 | |
| 19 | Most CMS structures in the wild are formatted as ContentInfo encapsulating one of the other types. |
wbond | ea25fc2 | 2015-06-19 15:07:04 -0400 | [diff] [blame] | 20 | """ |
| 21 | |
wbond | 6b66ab5 | 2015-06-21 10:26:45 -0400 | [diff] [blame] | 22 | from __future__ import unicode_literals, division, absolute_import, print_function |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 23 | |
| 24 | try: |
| 25 | import zlib |
| 26 | except (ImportError): |
| 27 | zlib = None |
| 28 | |
| 29 | from .algos import ( |
wbond | 381a4da | 2016-08-30 11:39:54 -0400 | [diff] [blame] | 30 | _ForceNullParameters, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 31 | DigestAlgorithm, |
wbond | 59af99d | 2015-06-15 16:19:04 -0400 | [diff] [blame] | 32 | EncryptionAlgorithm, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 33 | HmacAlgorithm, |
wbond | 59af99d | 2015-06-15 16:19:04 -0400 | [diff] [blame] | 34 | KdfAlgorithm, |
| 35 | SignedDigestAlgorithm, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 36 | ) |
| 37 | from .core import ( |
| 38 | Any, |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 39 | BitString, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 40 | Choice, |
| 41 | Enumerated, |
| 42 | GeneralizedTime, |
| 43 | Integer, |
| 44 | ObjectIdentifier, |
| 45 | OctetBitString, |
| 46 | OctetString, |
wbond | e5a1c6e | 2015-08-03 07:42:28 -0400 | [diff] [blame] | 47 | ParsableOctetString, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 48 | Sequence, |
| 49 | SequenceOf, |
| 50 | SetOf, |
| 51 | UTCTime, |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 52 | UTF8String, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 53 | ) |
| 54 | from .crl import CertificateList |
| 55 | from .keys import PublicKeyInfo |
| 56 | from .ocsp import OCSPResponse |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 57 | from .x509 import Attributes, Certificate, Extensions, GeneralName, GeneralNames, Name |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 58 | |
| 59 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 60 | # These structures are taken from |
| 61 | # ftp://ftp.rsasecurity.com/pub/pkcs/ascii/pkcs-6.asc |
| 62 | |
| 63 | class ExtendedCertificateInfo(Sequence): |
| 64 | _fields = [ |
| 65 | ('version', Integer), |
| 66 | ('certificate', Certificate), |
| 67 | ('attributes', Attributes), |
| 68 | ] |
| 69 | |
wbond | a26664f | 2015-10-07 11:57:35 -0400 | [diff] [blame] | 70 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 71 | class ExtendedCertificate(Sequence): |
| 72 | _fields = [ |
| 73 | ('extended_certificate_info', ExtendedCertificateInfo), |
| 74 | ('signature_algorithm', SignedDigestAlgorithm), |
| 75 | ('signature', OctetBitString), |
| 76 | ] |
| 77 | |
| 78 | |
| 79 | # These structures are taken from https://tools.ietf.org/html/rfc5652, |
| 80 | # https://tools.ietf.org/html/rfc5083, http://tools.ietf.org/html/rfc2315, |
| 81 | # https://tools.ietf.org/html/rfc5940, https://tools.ietf.org/html/rfc3274, |
| 82 | # https://tools.ietf.org/html/rfc3281 |
| 83 | |
| 84 | |
| 85 | class CMSVersion(Integer): |
| 86 | _map = { |
| 87 | 0: 'v0', |
| 88 | 1: 'v1', |
| 89 | 2: 'v2', |
| 90 | 3: 'v3', |
| 91 | 4: 'v4', |
| 92 | 5: 'v5', |
| 93 | } |
| 94 | |
| 95 | |
| 96 | class CMSAttributeType(ObjectIdentifier): |
| 97 | _map = { |
| 98 | '1.2.840.113549.1.9.3': 'content_type', |
| 99 | '1.2.840.113549.1.9.4': 'message_digest', |
| 100 | '1.2.840.113549.1.9.5': 'signing_time', |
| 101 | '1.2.840.113549.1.9.6': 'counter_signature', |
wbond | c57878c | 2016-07-14 06:02:01 -0400 | [diff] [blame] | 102 | # https://tools.ietf.org/html/rfc3161#page-20 |
| 103 | '1.2.840.113549.1.9.16.2.14': 'signature_time_stamp_token', |
Anthony Alba | ad0e19e | 2017-05-16 22:21:18 +0800 | [diff] [blame] | 104 | # https://tools.ietf.org/html/rfc6211#page-5 |
| 105 | '1.2.840.113549.1.9.52': 'cms_algorithm_protection', |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | |
| 109 | class Time(Choice): |
| 110 | _alternatives = [ |
| 111 | ('utc_time', UTCTime), |
| 112 | ('generalized_time', GeneralizedTime), |
| 113 | ] |
| 114 | |
| 115 | |
| 116 | class ContentType(ObjectIdentifier): |
| 117 | _map = { |
| 118 | '1.2.840.113549.1.7.1': 'data', |
| 119 | '1.2.840.113549.1.7.2': 'signed_data', |
| 120 | '1.2.840.113549.1.7.3': 'enveloped_data', |
| 121 | '1.2.840.113549.1.7.4': 'signed_and_enveloped_data', |
| 122 | '1.2.840.113549.1.7.5': 'digested_data', |
| 123 | '1.2.840.113549.1.7.6': 'encrypted_data', |
| 124 | '1.2.840.113549.1.9.16.1.2': 'authenticated_data', |
| 125 | '1.2.840.113549.1.9.16.1.9': 'compressed_data', |
| 126 | '1.2.840.113549.1.9.16.1.23': 'authenticated_enveloped_data', |
| 127 | } |
| 128 | |
| 129 | |
Anthony Alba | ad0e19e | 2017-05-16 22:21:18 +0800 | [diff] [blame] | 130 | class CMSAlgorithmProtection(Sequence): |
| 131 | _fields = [ |
| 132 | ('digest_algorithm', DigestAlgorithm), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 133 | ('signature_algorithm', SignedDigestAlgorithm, {'implicit': 1, 'optional': True}), |
| 134 | ('mac_algorithm', HmacAlgorithm, {'implicit': 2, 'optional': True}), |
Anthony Alba | ad0e19e | 2017-05-16 22:21:18 +0800 | [diff] [blame] | 135 | ] |
| 136 | |
| 137 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 138 | class SetOfContentType(SetOf): |
| 139 | _child_spec = ContentType |
| 140 | |
| 141 | |
| 142 | class SetOfOctetString(SetOf): |
| 143 | _child_spec = OctetString |
| 144 | |
| 145 | |
| 146 | class SetOfTime(SetOf): |
| 147 | _child_spec = Time |
| 148 | |
| 149 | |
wbond | a07a51a | 2015-12-03 01:09:07 -0500 | [diff] [blame] | 150 | class SetOfAny(SetOf): |
| 151 | _child_spec = Any |
| 152 | |
| 153 | |
Anthony Alba | ad0e19e | 2017-05-16 22:21:18 +0800 | [diff] [blame] | 154 | class SetOfCMSAlgorithmProtection(SetOf): |
| 155 | _child_spec = CMSAlgorithmProtection |
| 156 | |
| 157 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 158 | class CMSAttribute(Sequence): |
| 159 | _fields = [ |
| 160 | ('type', CMSAttributeType), |
wbond | a07a51a | 2015-12-03 01:09:07 -0500 | [diff] [blame] | 161 | ('values', None), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 162 | ] |
| 163 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 164 | _oid_specs = {} |
| 165 | |
wbond | a07a51a | 2015-12-03 01:09:07 -0500 | [diff] [blame] | 166 | def _values_spec(self): |
| 167 | return self._oid_specs.get(self['type'].native, SetOfAny) |
| 168 | |
| 169 | _spec_callbacks = { |
| 170 | 'values': _values_spec |
| 171 | } |
| 172 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 173 | |
wbond | f87d485 | 2015-12-03 00:02:35 -0500 | [diff] [blame] | 174 | class CMSAttributes(SetOf): |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 175 | _child_spec = CMSAttribute |
| 176 | |
| 177 | |
| 178 | class IssuerSerial(Sequence): |
| 179 | _fields = [ |
| 180 | ('issuer', GeneralNames), |
| 181 | ('serial', Integer), |
| 182 | ('issuer_uid', OctetBitString, {'optional': True}), |
| 183 | ] |
| 184 | |
| 185 | |
| 186 | class AttCertVersion(Integer): |
| 187 | _map = { |
| 188 | 0: 'v1', |
| 189 | 1: 'v2', |
| 190 | } |
| 191 | |
| 192 | |
| 193 | class AttCertSubject(Choice): |
| 194 | _alternatives = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 195 | ('base_certificate_id', IssuerSerial, {'explicit': 0}), |
| 196 | ('subject_name', GeneralNames, {'explicit': 1}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 197 | ] |
| 198 | |
| 199 | |
| 200 | class AttCertValidityPeriod(Sequence): |
| 201 | _fields = [ |
| 202 | ('not_before_time', GeneralizedTime), |
| 203 | ('not_after_time', GeneralizedTime), |
| 204 | ] |
| 205 | |
| 206 | |
| 207 | class AttributeCertificateInfoV1(Sequence): |
| 208 | _fields = [ |
wbond | b8dafce | 2015-08-24 09:28:01 -0400 | [diff] [blame] | 209 | ('version', AttCertVersion, {'default': 'v1'}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 210 | ('subject', AttCertSubject), |
| 211 | ('issuer', GeneralNames), |
| 212 | ('signature', SignedDigestAlgorithm), |
| 213 | ('serial_number', Integer), |
| 214 | ('att_cert_validity_period', AttCertValidityPeriod), |
| 215 | ('attributes', Attributes), |
| 216 | ('issuer_unique_id', OctetBitString, {'optional': True}), |
| 217 | ('extensions', Extensions, {'optional': True}), |
| 218 | ] |
| 219 | |
| 220 | |
| 221 | class AttributeCertificateV1(Sequence): |
| 222 | _fields = [ |
| 223 | ('ac_info', AttributeCertificateInfoV1), |
| 224 | ('signature_algorithm', SignedDigestAlgorithm), |
| 225 | ('signature', OctetBitString), |
| 226 | ] |
| 227 | |
| 228 | |
| 229 | class DigestedObjectType(Enumerated): |
| 230 | _map = { |
| 231 | 0: 'public_key', |
| 232 | 1: 'public_key_cert', |
| 233 | 2: 'other_objy_types', |
| 234 | } |
| 235 | |
| 236 | |
| 237 | class ObjectDigestInfo(Sequence): |
| 238 | _fields = [ |
| 239 | ('digested_object_type', DigestedObjectType), |
| 240 | ('other_object_type_id', ObjectIdentifier, {'optional': True}), |
| 241 | ('digest_algorithm', DigestAlgorithm), |
| 242 | ('object_digest', OctetBitString), |
| 243 | ] |
| 244 | |
| 245 | |
| 246 | class Holder(Sequence): |
| 247 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 248 | ('base_certificate_id', IssuerSerial, {'implicit': 0, 'optional': True}), |
| 249 | ('entity_name', GeneralNames, {'implicit': 1, 'optional': True}), |
| 250 | ('object_digest_info', ObjectDigestInfo, {'implicit': 2, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 251 | ] |
| 252 | |
| 253 | |
| 254 | class V2Form(Sequence): |
| 255 | _fields = [ |
| 256 | ('issuer_name', GeneralNames, {'optional': True}), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 257 | ('base_certificate_id', IssuerSerial, {'explicit': 0, 'optional': True}), |
| 258 | ('object_digest_info', ObjectDigestInfo, {'explicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 259 | ] |
| 260 | |
| 261 | |
| 262 | class AttCertIssuer(Choice): |
| 263 | _alternatives = [ |
| 264 | ('v1_form', GeneralNames), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 265 | ('v2_form', V2Form, {'explicit': 0}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 266 | ] |
| 267 | |
| 268 | |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 269 | class IetfAttrValue(Choice): |
| 270 | _alternatives = [ |
| 271 | ('octets', OctetString), |
| 272 | ('oid', ObjectIdentifier), |
| 273 | ('string', UTF8String), |
| 274 | ] |
| 275 | |
| 276 | |
| 277 | class IetfAttrValues(SequenceOf): |
| 278 | _child_spec = IetfAttrValue |
| 279 | |
| 280 | |
| 281 | class IetfAttrSyntax(Sequence): |
| 282 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 283 | ('policy_authority', GeneralNames, {'implicit': 0, 'optional': True}), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 284 | ('values', IetfAttrValues), |
| 285 | ] |
| 286 | |
| 287 | |
| 288 | class SetOfIetfAttrSyntax(SetOf): |
| 289 | _child_spec = IetfAttrSyntax |
| 290 | |
| 291 | |
| 292 | class SvceAuthInfo(Sequence): |
| 293 | _fields = [ |
| 294 | ('service', GeneralName), |
| 295 | ('ident', GeneralName), |
| 296 | ('auth_info', OctetString, {'optional': True}), |
| 297 | ] |
| 298 | |
| 299 | |
| 300 | class SetOfSvceAuthInfo(SetOf): |
| 301 | _child_spec = SvceAuthInfo |
| 302 | |
| 303 | |
| 304 | class RoleSyntax(Sequence): |
| 305 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 306 | ('role_authority', GeneralNames, {'implicit': 0, 'optional': True}), |
| 307 | ('role_name', GeneralName, {'implicit': 1}), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 308 | ] |
| 309 | |
| 310 | |
| 311 | class SetOfRoleSyntax(SetOf): |
| 312 | _child_spec = RoleSyntax |
| 313 | |
| 314 | |
| 315 | class ClassList(BitString): |
| 316 | _map = { |
| 317 | 0: 'unmarked', |
| 318 | 1: 'unclassified', |
| 319 | 2: 'restricted', |
| 320 | 3: 'confidential', |
| 321 | 4: 'secret', |
| 322 | 5: 'top_secret', |
| 323 | } |
| 324 | |
| 325 | |
| 326 | class SecurityCategory(Sequence): |
| 327 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 328 | ('type', ObjectIdentifier, {'implicit': 0}), |
| 329 | ('value', Any, {'implicit': 1}), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 330 | ] |
| 331 | |
| 332 | |
| 333 | class SetOfSecurityCategory(SetOf): |
| 334 | _child_spec = SecurityCategory |
| 335 | |
| 336 | |
| 337 | class Clearance(Sequence): |
| 338 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 339 | ('policy_id', ObjectIdentifier, {'implicit': 0}), |
| 340 | ('class_list', ClassList, {'implicit': 1, 'default': 'unclassified'}), |
| 341 | ('security_categories', SetOfSecurityCategory, {'implicit': 2, 'optional': True}), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 342 | ] |
| 343 | |
| 344 | |
| 345 | class SetOfClearance(SetOf): |
| 346 | _child_spec = Clearance |
| 347 | |
| 348 | |
| 349 | class BigTime(Sequence): |
| 350 | _fields = [ |
| 351 | ('major', Integer), |
| 352 | ('fractional_seconds', Integer), |
| 353 | ('sign', Integer, {'optional': True}), |
| 354 | ] |
| 355 | |
| 356 | |
| 357 | class LeapData(Sequence): |
| 358 | _fields = [ |
| 359 | ('leap_time', BigTime), |
| 360 | ('action', Integer), |
| 361 | ] |
| 362 | |
| 363 | |
| 364 | class SetOfLeapData(SetOf): |
| 365 | _child_spec = LeapData |
| 366 | |
| 367 | |
| 368 | class TimingMetrics(Sequence): |
| 369 | _fields = [ |
| 370 | ('ntp_time', BigTime), |
| 371 | ('offset', BigTime), |
| 372 | ('delay', BigTime), |
| 373 | ('expiration', BigTime), |
| 374 | ('leap_event', SetOfLeapData, {'optional': True}), |
| 375 | ] |
| 376 | |
| 377 | |
| 378 | class SetOfTimingMetrics(SetOf): |
| 379 | _child_spec = TimingMetrics |
| 380 | |
| 381 | |
| 382 | class TimingPolicy(Sequence): |
| 383 | _fields = [ |
| 384 | ('policy_id', SequenceOf, {'spec': ObjectIdentifier}), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 385 | ('max_offset', BigTime, {'explicit': 0, 'optional': True}), |
| 386 | ('max_delay', BigTime, {'explicit': 1, 'optional': True}), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 387 | ] |
| 388 | |
| 389 | |
| 390 | class SetOfTimingPolicy(SetOf): |
| 391 | _child_spec = TimingPolicy |
| 392 | |
| 393 | |
| 394 | class AttCertAttributeType(ObjectIdentifier): |
| 395 | _map = { |
| 396 | '1.3.6.1.5.5.7.10.1': 'authentication_info', |
| 397 | '1.3.6.1.5.5.7.10.2': 'access_identity', |
| 398 | '1.3.6.1.5.5.7.10.3': 'charging_identity', |
| 399 | '1.3.6.1.5.5.7.10.4': 'group', |
| 400 | '2.5.4.72': 'role', |
| 401 | '2.5.4.55': 'clearance', |
| 402 | '1.3.6.1.4.1.601.10.4.1': 'timing_metrics', |
| 403 | '1.3.6.1.4.1.601.10.4.2': 'timing_policy', |
| 404 | } |
| 405 | |
| 406 | |
| 407 | class AttCertAttribute(Sequence): |
| 408 | _fields = [ |
| 409 | ('type', AttCertAttributeType), |
| 410 | ('values', None), |
| 411 | ] |
| 412 | |
| 413 | _oid_specs = { |
| 414 | 'authentication_info': SetOfSvceAuthInfo, |
| 415 | 'access_identity': SetOfSvceAuthInfo, |
| 416 | 'charging_identity': SetOfIetfAttrSyntax, |
| 417 | 'group': SetOfIetfAttrSyntax, |
| 418 | 'role': SetOfRoleSyntax, |
| 419 | 'clearance': SetOfClearance, |
| 420 | 'timing_metrics': SetOfTimingMetrics, |
| 421 | 'timing_policy': SetOfTimingPolicy, |
| 422 | } |
| 423 | |
| 424 | def _values_spec(self): |
wbond | 7b1cd4b | 2017-02-10 05:11:14 -0500 | [diff] [blame] | 425 | return self._oid_specs.get(self['type'].native, SetOfAny) |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 426 | |
| 427 | _spec_callbacks = { |
| 428 | 'values': _values_spec |
| 429 | } |
| 430 | |
| 431 | |
| 432 | class AttCertAttributes(SequenceOf): |
| 433 | _child_spec = AttCertAttribute |
| 434 | |
| 435 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 436 | class AttributeCertificateInfoV2(Sequence): |
| 437 | _fields = [ |
| 438 | ('version', AttCertVersion), |
| 439 | ('holder', Holder), |
| 440 | ('issuer', AttCertIssuer), |
| 441 | ('signature', SignedDigestAlgorithm), |
| 442 | ('serial_number', Integer), |
| 443 | ('att_cert_validity_period', AttCertValidityPeriod), |
wbond | 6a8cf2a | 2016-07-14 07:17:44 -0400 | [diff] [blame] | 444 | ('attributes', AttCertAttributes), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 445 | ('issuer_unique_id', OctetBitString, {'optional': True}), |
| 446 | ('extensions', Extensions, {'optional': True}), |
| 447 | ] |
| 448 | |
| 449 | |
| 450 | class AttributeCertificateV2(Sequence): |
wbond | 97cdd2f | 2016-07-14 11:58:41 -0400 | [diff] [blame] | 451 | # Handle the situation where a V2 cert is encoded as V1 |
| 452 | _bad_tag = 1 |
| 453 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 454 | _fields = [ |
| 455 | ('ac_info', AttributeCertificateInfoV2), |
| 456 | ('signature_algorithm', SignedDigestAlgorithm), |
| 457 | ('signature', OctetBitString), |
| 458 | ] |
| 459 | |
| 460 | |
| 461 | class OtherCertificateFormat(Sequence): |
| 462 | _fields = [ |
| 463 | ('other_cert_format', ObjectIdentifier), |
| 464 | ('other_cert', Any), |
| 465 | ] |
| 466 | |
| 467 | |
| 468 | class CertificateChoices(Choice): |
| 469 | _alternatives = [ |
| 470 | ('certificate', Certificate), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 471 | ('extended_certificate', ExtendedCertificate, {'implicit': 0}), |
| 472 | ('v1_attr_cert', AttributeCertificateV1, {'implicit': 1}), |
| 473 | ('v2_attr_cert', AttributeCertificateV2, {'implicit': 2}), |
| 474 | ('other', OtherCertificateFormat, {'implicit': 3}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 475 | ] |
| 476 | |
wbond | 97cdd2f | 2016-07-14 11:58:41 -0400 | [diff] [blame] | 477 | def validate(self, class_, tag, contents): |
| 478 | """ |
| 479 | Ensures that the class and tag specified exist as an alternative. This |
| 480 | custom version fixes parsing broken encodings there a V2 attribute |
| 481 | # certificate is encoded as a V1 |
| 482 | |
| 483 | :param class_: |
| 484 | The integer class_ from the encoded value header |
| 485 | |
| 486 | :param tag: |
| 487 | The integer tag from the encoded value header |
| 488 | |
| 489 | :param contents: |
| 490 | A byte string of the contents of the value - used when the object |
| 491 | is explicitly tagged |
| 492 | |
| 493 | :raises: |
| 494 | ValueError - when value is not a valid alternative |
| 495 | """ |
| 496 | |
| 497 | super(CertificateChoices, self).validate(class_, tag, contents) |
| 498 | if self._choice == 2: |
| 499 | if AttCertVersion.load(Sequence.load(contents)[0].dump()).native == 'v2': |
| 500 | self._choice = 3 |
| 501 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 502 | |
| 503 | class CertificateSet(SetOf): |
| 504 | _child_spec = CertificateChoices |
| 505 | |
| 506 | |
| 507 | class ContentInfo(Sequence): |
| 508 | _fields = [ |
| 509 | ('content_type', ContentType), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 510 | ('content', Any, {'explicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 511 | ] |
| 512 | |
| 513 | _oid_pair = ('content_type', 'content') |
| 514 | _oid_specs = {} |
| 515 | |
| 516 | |
wbond | c57878c | 2016-07-14 06:02:01 -0400 | [diff] [blame] | 517 | class SetOfContentInfo(SetOf): |
| 518 | _child_spec = ContentInfo |
| 519 | |
| 520 | |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 521 | class EncapsulatedContentInfo(Sequence): |
| 522 | _fields = [ |
| 523 | ('content_type', ContentType), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 524 | ('content', ParsableOctetString, {'explicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 525 | ] |
| 526 | |
| 527 | _oid_pair = ('content_type', 'content') |
| 528 | _oid_specs = {} |
| 529 | |
| 530 | |
| 531 | class IssuerAndSerialNumber(Sequence): |
| 532 | _fields = [ |
| 533 | ('issuer', Name), |
| 534 | ('serial_number', Integer), |
| 535 | ] |
| 536 | |
| 537 | |
| 538 | class SignerIdentifier(Choice): |
| 539 | _alternatives = [ |
| 540 | ('issuer_and_serial_number', IssuerAndSerialNumber), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 541 | ('subject_key_identifier', OctetString, {'implicit': 0}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 542 | ] |
| 543 | |
| 544 | |
| 545 | class DigestAlgorithms(SetOf): |
| 546 | _child_spec = DigestAlgorithm |
| 547 | |
| 548 | |
| 549 | class CertificateRevocationLists(SetOf): |
| 550 | _child_spec = CertificateList |
| 551 | |
| 552 | |
| 553 | class SCVPReqRes(Sequence): |
| 554 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 555 | ('request', ContentInfo, {'explicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 556 | ('response', ContentInfo), |
| 557 | ] |
| 558 | |
| 559 | |
| 560 | class OtherRevInfoFormatId(ObjectIdentifier): |
| 561 | _map = { |
| 562 | '1.3.6.1.5.5.7.16.2': 'ocsp_response', |
| 563 | '1.3.6.1.5.5.7.16.4': 'scvp', |
| 564 | } |
| 565 | |
| 566 | |
| 567 | class OtherRevocationInfoFormat(Sequence): |
| 568 | _fields = [ |
| 569 | ('other_rev_info_format', OtherRevInfoFormatId), |
| 570 | ('other_rev_info', Any), |
| 571 | ] |
| 572 | |
| 573 | _oid_pair = ('other_rev_info_format', 'other_rev_info') |
| 574 | _oid_specs = { |
| 575 | 'ocsp_response': OCSPResponse, |
| 576 | 'scvp': SCVPReqRes, |
| 577 | } |
| 578 | |
| 579 | |
| 580 | class RevocationInfoChoice(Choice): |
| 581 | _alternatives = [ |
| 582 | ('crl', CertificateList), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 583 | ('other', OtherRevocationInfoFormat, {'implicit': 1}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 584 | ] |
| 585 | |
| 586 | |
| 587 | class RevocationInfoChoices(SetOf): |
| 588 | _child_spec = RevocationInfoChoice |
| 589 | |
| 590 | |
| 591 | class SignerInfo(Sequence): |
| 592 | _fields = [ |
| 593 | ('version', CMSVersion), |
| 594 | ('sid', SignerIdentifier), |
| 595 | ('digest_algorithm', DigestAlgorithm), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 596 | ('signed_attrs', CMSAttributes, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 597 | ('signature_algorithm', SignedDigestAlgorithm), |
| 598 | ('signature', OctetString), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 599 | ('unsigned_attrs', CMSAttributes, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 600 | ] |
| 601 | |
| 602 | |
| 603 | class SignerInfos(SetOf): |
| 604 | _child_spec = SignerInfo |
| 605 | |
| 606 | |
| 607 | class SignedData(Sequence): |
| 608 | _fields = [ |
| 609 | ('version', CMSVersion), |
| 610 | ('digest_algorithms', DigestAlgorithms), |
| 611 | ('encap_content_info', None), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 612 | ('certificates', CertificateSet, {'implicit': 0, 'optional': True}), |
| 613 | ('crls', RevocationInfoChoices, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 614 | ('signer_infos', SignerInfos), |
| 615 | ] |
| 616 | |
| 617 | def _encap_content_info_spec(self): |
| 618 | # If the encap_content_info is version v1, then this could be a PKCS#7 |
| 619 | # structure, or a CMS structure. CMS wraps the encoded value in an |
| 620 | # Octet String tag. |
| 621 | |
| 622 | # If the version is greater than 1, it is definite CMS |
| 623 | if self['version'].native != 'v1': |
| 624 | return EncapsulatedContentInfo |
| 625 | |
| 626 | # Otherwise, the ContentInfo spec from PKCS#7 will be compatible with |
| 627 | # CMS v1 (which only allows Data, an Octet String) and PKCS#7, which |
| 628 | # allows Any |
| 629 | return ContentInfo |
| 630 | |
| 631 | _spec_callbacks = { |
| 632 | 'encap_content_info': _encap_content_info_spec |
| 633 | } |
| 634 | |
| 635 | |
| 636 | class OriginatorInfo(Sequence): |
| 637 | _fields = [ |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 638 | ('certs', CertificateSet, {'implicit': 0, 'optional': True}), |
| 639 | ('crls', RevocationInfoChoices, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 640 | ] |
| 641 | |
| 642 | |
| 643 | class RecipientIdentifier(Choice): |
| 644 | _alternatives = [ |
| 645 | ('issuer_and_serial_number', IssuerAndSerialNumber), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 646 | ('subject_key_identifier', OctetString, {'implicit': 0}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 647 | ] |
| 648 | |
| 649 | |
| 650 | class KeyEncryptionAlgorithmId(ObjectIdentifier): |
| 651 | _map = { |
eukaryote | fda656d | 2015-11-02 10:04:37 -0800 | [diff] [blame] | 652 | '1.2.840.113549.1.1.1': 'rsa', |
Ludovic Watteaux | b2c16e9 | 2018-06-30 14:11:22 +0200 | [diff] [blame^] | 653 | '1.2.840.113549.1.1.7': 'rsaes_oaep', |
eukaryote | fda656d | 2015-11-02 10:04:37 -0800 | [diff] [blame] | 654 | '2.16.840.1.101.3.4.1.5': 'aes128_wrap', |
| 655 | '2.16.840.1.101.3.4.1.8': 'aes128_wrap_pad', |
| 656 | '2.16.840.1.101.3.4.1.25': 'aes192_wrap', |
| 657 | '2.16.840.1.101.3.4.1.28': 'aes192_wrap_pad', |
| 658 | '2.16.840.1.101.3.4.1.45': 'aes256_wrap', |
| 659 | '2.16.840.1.101.3.4.1.48': 'aes256_wrap_pad', |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
wbond | 381a4da | 2016-08-30 11:39:54 -0400 | [diff] [blame] | 663 | class KeyEncryptionAlgorithm(_ForceNullParameters, Sequence): |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 664 | _fields = [ |
| 665 | ('algorithm', KeyEncryptionAlgorithmId), |
| 666 | ('parameters', Any, {'optional': True}), |
| 667 | ] |
| 668 | |
| 669 | |
| 670 | class KeyTransRecipientInfo(Sequence): |
| 671 | _fields = [ |
| 672 | ('version', CMSVersion), |
| 673 | ('rid', RecipientIdentifier), |
| 674 | ('key_encryption_algorithm', KeyEncryptionAlgorithm), |
| 675 | ('encrypted_key', OctetString), |
| 676 | ] |
| 677 | |
| 678 | |
| 679 | class OriginatorIdentifierOrKey(Choice): |
| 680 | _alternatives = [ |
| 681 | ('issuer_and_serial_number', IssuerAndSerialNumber), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 682 | ('subject_key_identifier', OctetString, {'implicit': 0}), |
| 683 | ('originator_key', PublicKeyInfo, {'implicit': 1}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 684 | ] |
| 685 | |
| 686 | |
| 687 | class OtherKeyAttribute(Sequence): |
| 688 | _fields = [ |
| 689 | ('key_attr_id', ObjectIdentifier), |
| 690 | ('key_attr', Any), |
| 691 | ] |
| 692 | |
| 693 | |
| 694 | class RecipientKeyIdentifier(Sequence): |
| 695 | _fields = [ |
| 696 | ('subject_key_identifier', OctetString), |
| 697 | ('date', GeneralizedTime, {'optional': True}), |
| 698 | ('other', OtherKeyAttribute, {'optional': True}), |
| 699 | ] |
| 700 | |
| 701 | |
| 702 | class KeyAgreementRecipientIdentifier(Choice): |
| 703 | _alternatives = [ |
| 704 | ('issuer_and_serial_number', IssuerAndSerialNumber), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 705 | ('r_key_id', RecipientKeyIdentifier, {'implicit': 0}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 706 | ] |
| 707 | |
| 708 | |
| 709 | class RecipientEncryptedKey(Sequence): |
| 710 | _fields = [ |
| 711 | ('rid', KeyAgreementRecipientIdentifier), |
| 712 | ('encrypted_key', OctetString), |
| 713 | ] |
| 714 | |
| 715 | |
| 716 | class RecipientEncryptedKeys(SequenceOf): |
| 717 | _child_spec = RecipientEncryptedKey |
| 718 | |
| 719 | |
| 720 | class KeyAgreeRecipientInfo(Sequence): |
| 721 | _fields = [ |
| 722 | ('version', CMSVersion), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 723 | ('originator', OriginatorIdentifierOrKey, {'explicit': 0}), |
| 724 | ('ukm', OctetString, {'explicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 725 | ('key_encryption_algorithm', KeyEncryptionAlgorithm), |
| 726 | ('recipient_encrypted_keys', RecipientEncryptedKeys), |
| 727 | ] |
| 728 | |
| 729 | |
| 730 | class KEKIdentifier(Sequence): |
| 731 | _fields = [ |
| 732 | ('key_identifier', OctetString), |
| 733 | ('date', GeneralizedTime, {'optional': True}), |
| 734 | ('other', OtherKeyAttribute, {'optional': True}), |
| 735 | ] |
| 736 | |
| 737 | |
| 738 | class KEKRecipientInfo(Sequence): |
| 739 | _fields = [ |
| 740 | ('version', CMSVersion), |
| 741 | ('kekid', KEKIdentifier), |
| 742 | ('key_encryption_algorithm', KeyEncryptionAlgorithm), |
| 743 | ('encrypted_key', OctetString), |
| 744 | ] |
| 745 | |
| 746 | |
| 747 | class PasswordRecipientInfo(Sequence): |
| 748 | _fields = [ |
| 749 | ('version', CMSVersion), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 750 | ('key_derivation_algorithm', KdfAlgorithm, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 751 | ('key_encryption_algorithm', KeyEncryptionAlgorithm), |
| 752 | ('encrypted_key', OctetString), |
| 753 | ] |
| 754 | |
| 755 | |
| 756 | class OtherRecipientInfo(Sequence): |
| 757 | _fields = [ |
| 758 | ('ori_type', ObjectIdentifier), |
| 759 | ('ori_value', Any), |
| 760 | ] |
| 761 | |
| 762 | |
| 763 | class RecipientInfo(Choice): |
| 764 | _alternatives = [ |
| 765 | ('ktri', KeyTransRecipientInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 766 | ('kari', KeyAgreeRecipientInfo, {'implicit': 1}), |
| 767 | ('kekri', KEKRecipientInfo, {'implicit': 2}), |
| 768 | ('pwri', PasswordRecipientInfo, {'implicit': 3}), |
| 769 | ('ori', OtherRecipientInfo, {'implicit': 4}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 770 | ] |
| 771 | |
| 772 | |
| 773 | class RecipientInfos(SetOf): |
| 774 | _child_spec = RecipientInfo |
| 775 | |
| 776 | |
| 777 | class EncryptedContentInfo(Sequence): |
| 778 | _fields = [ |
| 779 | ('content_type', ContentType), |
wbond | 59af99d | 2015-06-15 16:19:04 -0400 | [diff] [blame] | 780 | ('content_encryption_algorithm', EncryptionAlgorithm), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 781 | ('encrypted_content', OctetString, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 782 | ] |
| 783 | |
| 784 | |
| 785 | class EnvelopedData(Sequence): |
| 786 | _fields = [ |
| 787 | ('version', CMSVersion), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 788 | ('originator_info', OriginatorInfo, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 789 | ('recipient_infos', RecipientInfos), |
| 790 | ('encrypted_content_info', EncryptedContentInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 791 | ('unprotected_attrs', CMSAttributes, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 792 | ] |
| 793 | |
| 794 | |
| 795 | class SignedAndEnvelopedData(Sequence): |
| 796 | _fields = [ |
| 797 | ('version', CMSVersion), |
| 798 | ('recipient_infos', RecipientInfos), |
| 799 | ('digest_algorithms', DigestAlgorithms), |
| 800 | ('encrypted_content_info', EncryptedContentInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 801 | ('certificates', CertificateSet, {'implicit': 0, 'optional': True}), |
| 802 | ('crls', CertificateRevocationLists, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 803 | ('signer_infos', SignerInfos), |
| 804 | ] |
| 805 | |
| 806 | |
| 807 | class DigestedData(Sequence): |
| 808 | _fields = [ |
| 809 | ('version', CMSVersion), |
| 810 | ('digest_algorithm', DigestAlgorithm), |
| 811 | ('encap_content_info', None), |
| 812 | ('digest', OctetString), |
| 813 | ] |
| 814 | |
| 815 | def _encap_content_info_spec(self): |
| 816 | # If the encap_content_info is version v1, then this could be a PKCS#7 |
| 817 | # structure, or a CMS structure. CMS wraps the encoded value in an |
| 818 | # Octet String tag. |
| 819 | |
| 820 | # If the version is greater than 1, it is definite CMS |
| 821 | if self['version'].native != 'v1': |
| 822 | return EncapsulatedContentInfo |
| 823 | |
| 824 | # Otherwise, the ContentInfo spec from PKCS#7 will be compatible with |
| 825 | # CMS v1 (which only allows Data, an Octet String) and PKCS#7, which |
| 826 | # allows Any |
| 827 | return ContentInfo |
| 828 | |
| 829 | _spec_callbacks = { |
| 830 | 'encap_content_info': _encap_content_info_spec |
| 831 | } |
| 832 | |
| 833 | |
| 834 | class EncryptedData(Sequence): |
| 835 | _fields = [ |
| 836 | ('version', CMSVersion), |
| 837 | ('encrypted_content_info', EncryptedContentInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 838 | ('unprotected_attrs', CMSAttributes, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 839 | ] |
| 840 | |
| 841 | |
| 842 | class AuthenticatedData(Sequence): |
| 843 | _fields = [ |
| 844 | ('version', CMSVersion), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 845 | ('originator_info', OriginatorInfo, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 846 | ('recipient_infos', RecipientInfos), |
| 847 | ('mac_algorithm', HmacAlgorithm), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 848 | ('digest_algorithm', DigestAlgorithm, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 849 | # This does not require the _spec_callbacks approach of SignedData and |
| 850 | # DigestedData since AuthenticatedData was not part of PKCS#7 |
| 851 | ('encap_content_info', EncapsulatedContentInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 852 | ('auth_attrs', CMSAttributes, {'implicit': 2, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 853 | ('mac', OctetString), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 854 | ('unauth_attrs', CMSAttributes, {'implicit': 3, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 855 | ] |
| 856 | |
| 857 | |
| 858 | class AuthEnvelopedData(Sequence): |
| 859 | _fields = [ |
| 860 | ('version', CMSVersion), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 861 | ('originator_info', OriginatorInfo, {'implicit': 0, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 862 | ('recipient_infos', RecipientInfos), |
| 863 | ('auth_encrypted_content_info', EncryptedContentInfo), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 864 | ('auth_attrs', CMSAttributes, {'implicit': 1, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 865 | ('mac', OctetString), |
wbond | d62ed9a | 2017-09-15 07:13:52 -0400 | [diff] [blame] | 866 | ('unauth_attrs', CMSAttributes, {'implicit': 2, 'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 867 | ] |
| 868 | |
| 869 | |
| 870 | class CompressionAlgorithmId(ObjectIdentifier): |
| 871 | _map = { |
| 872 | '1.2.840.113549.1.9.16.3.8': 'zlib', |
| 873 | } |
| 874 | |
| 875 | |
| 876 | class CompressionAlgorithm(Sequence): |
| 877 | _fields = [ |
| 878 | ('algorithm', CompressionAlgorithmId), |
wbond | 40a32f0 | 2015-06-17 22:29:56 -0400 | [diff] [blame] | 879 | ('parameters', Any, {'optional': True}), |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 880 | ] |
| 881 | |
| 882 | |
| 883 | class CompressedData(Sequence): |
| 884 | _fields = [ |
| 885 | ('version', CMSVersion), |
| 886 | ('compression_algorithm', CompressionAlgorithm), |
| 887 | ('encap_content_info', EncapsulatedContentInfo), |
| 888 | ] |
| 889 | |
| 890 | _decompressed = None |
| 891 | |
| 892 | @property |
| 893 | def decompressed(self): |
| 894 | if self._decompressed is None: |
| 895 | if zlib is None: |
| 896 | raise SystemError('The zlib module is not available') |
| 897 | self._decompressed = zlib.decompress(self['encap_content_info']['content'].native) |
| 898 | return self._decompressed |
| 899 | |
| 900 | |
wbond | a26664f | 2015-10-07 11:57:35 -0400 | [diff] [blame] | 901 | ContentInfo._oid_specs = { |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 902 | 'data': OctetString, |
| 903 | 'signed_data': SignedData, |
| 904 | 'enveloped_data': EnvelopedData, |
| 905 | 'signed_and_enveloped_data': SignedAndEnvelopedData, |
| 906 | 'digested_data': DigestedData, |
| 907 | 'encrypted_data': EncryptedData, |
| 908 | 'authenticated_data': AuthenticatedData, |
| 909 | 'compressed_data': CompressedData, |
| 910 | 'authenticated_enveloped_data': AuthEnvelopedData, |
| 911 | } |
| 912 | |
| 913 | |
wbond | a26664f | 2015-10-07 11:57:35 -0400 | [diff] [blame] | 914 | EncapsulatedContentInfo._oid_specs = { |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 915 | 'signed_data': SignedData, |
| 916 | 'enveloped_data': EnvelopedData, |
| 917 | 'signed_and_enveloped_data': SignedAndEnvelopedData, |
| 918 | 'digested_data': DigestedData, |
| 919 | 'encrypted_data': EncryptedData, |
| 920 | 'authenticated_data': AuthenticatedData, |
| 921 | 'compressed_data': CompressedData, |
| 922 | 'authenticated_enveloped_data': AuthEnvelopedData, |
| 923 | } |
| 924 | |
| 925 | |
wbond | a26664f | 2015-10-07 11:57:35 -0400 | [diff] [blame] | 926 | CMSAttribute._oid_specs = { |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 927 | 'content_type': SetOfContentType, |
| 928 | 'message_digest': SetOfOctetString, |
| 929 | 'signing_time': SetOfTime, |
| 930 | 'counter_signature': SignerInfos, |
wbond | c57878c | 2016-07-14 06:02:01 -0400 | [diff] [blame] | 931 | 'signature_time_stamp_token': SetOfContentInfo, |
Anthony Alba | ad0e19e | 2017-05-16 22:21:18 +0800 | [diff] [blame] | 932 | 'cms_algorithm_protection': SetOfCMSAlgorithmProtection, |
wbond | e91513e | 2015-06-03 14:52:18 -0400 | [diff] [blame] | 933 | } |