1#
2# This file is part of pyasn1-modules software.
3#
4# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
5# License: http://snmplabs.com/pyasn1/license.html
6#
7# PKCS#7 message syntax
8#
9# ASN.1 source from:
10# https://opensource.apple.com/source/Security/Security-55179.1/libsecurity_asn1/asn1/pkcs7.asn.auto.html
11#
12# Sample captures from:
13# openssl crl2pkcs7 -nocrl -certfile cert1.cer -out outfile.p7b
14#
15from pyasn1_modules.rfc2459 import *
16
17
18class Attribute(univ.Sequence):
19    componentType = namedtype.NamedTypes(
20        namedtype.NamedType('type', AttributeType()),
21        namedtype.NamedType('values', univ.SetOf(componentType=AttributeValue()))
22    )
23
24
25class AttributeValueAssertion(univ.Sequence):
26    componentType = namedtype.NamedTypes(
27        namedtype.NamedType('attributeType', AttributeType()),
28        namedtype.NamedType('attributeValue', AttributeValue(),
29                            openType=opentype.OpenType('type', certificateAttributesMap))
30    )
31
32
33pkcs_7 = univ.ObjectIdentifier('1.2.840.113549.1.7')
34data = univ.ObjectIdentifier('1.2.840.113549.1.7.1')
35signedData = univ.ObjectIdentifier('1.2.840.113549.1.7.2')
36envelopedData = univ.ObjectIdentifier('1.2.840.113549.1.7.3')
37signedAndEnvelopedData = univ.ObjectIdentifier('1.2.840.113549.1.7.4')
38digestedData = univ.ObjectIdentifier('1.2.840.113549.1.7.5')
39encryptedData = univ.ObjectIdentifier('1.2.840.113549.1.7.6')
40
41
42class ContentType(univ.ObjectIdentifier):
43    pass
44
45
46class ContentEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
47    pass
48
49
50class EncryptedContent(univ.OctetString):
51    pass
52
53
54contentTypeMap = {}
55
56
57class EncryptedContentInfo(univ.Sequence):
58    componentType = namedtype.NamedTypes(
59        namedtype.NamedType('contentType', ContentType()),
60        namedtype.NamedType('contentEncryptionAlgorithm', ContentEncryptionAlgorithmIdentifier()),
61        namedtype.OptionalNamedType(
62            'encryptedContent', EncryptedContent().subtype(
63                implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)
64            ),
65            openType=opentype.OpenType('contentType', contentTypeMap)
66        )
67    )
68
69
70class Version(univ.Integer):  # overrides x509.Version
71    pass
72
73
74class EncryptedData(univ.Sequence):
75    componentType = namedtype.NamedTypes(
76        namedtype.NamedType('version', Version()),
77        namedtype.NamedType('encryptedContentInfo', EncryptedContentInfo())
78    )
79
80
81class DigestAlgorithmIdentifier(AlgorithmIdentifier):
82    pass
83
84
85class DigestAlgorithmIdentifiers(univ.SetOf):
86    componentType = DigestAlgorithmIdentifier()
87
88
89class Digest(univ.OctetString):
90    pass
91
92
93class ContentInfo(univ.Sequence):
94    componentType = namedtype.NamedTypes(
95        namedtype.NamedType('contentType', ContentType()),
96        namedtype.OptionalNamedType(
97            'content',
98            univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)),
99            openType=opentype.OpenType('contentType', contentTypeMap)
100        )
101    )
102
103
104class DigestedData(univ.Sequence):
105    componentType = namedtype.NamedTypes(
106        namedtype.NamedType('version', Version()),
107        namedtype.NamedType('digestAlgorithm', DigestAlgorithmIdentifier()),
108        namedtype.NamedType('contentInfo', ContentInfo()),
109        namedtype.NamedType('digest', Digest())
110    )
111
112
113class IssuerAndSerialNumber(univ.Sequence):
114    componentType = namedtype.NamedTypes(
115        namedtype.NamedType('issuer', Name()),
116        namedtype.NamedType('serialNumber', CertificateSerialNumber())
117    )
118
119
120class KeyEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
121    pass
122
123
124class EncryptedKey(univ.OctetString):
125    pass
126
127
128class RecipientInfo(univ.Sequence):
129    componentType = namedtype.NamedTypes(
130        namedtype.NamedType('version', Version()),
131        namedtype.NamedType('issuerAndSerialNumber', IssuerAndSerialNumber()),
132        namedtype.NamedType('keyEncryptionAlgorithm', KeyEncryptionAlgorithmIdentifier()),
133        namedtype.NamedType('encryptedKey', EncryptedKey())
134    )
135
136
137class RecipientInfos(univ.SetOf):
138    componentType = RecipientInfo()
139
140
141class Attributes(univ.SetOf):
142    componentType = Attribute()
143
144
145class ExtendedCertificateInfo(univ.Sequence):
146    componentType = namedtype.NamedTypes(
147        namedtype.NamedType('version', Version()),
148        namedtype.NamedType('certificate', Certificate()),
149        namedtype.NamedType('attributes', Attributes())
150    )
151
152
153class SignatureAlgorithmIdentifier(AlgorithmIdentifier):
154    pass
155
156
157class Signature(univ.BitString):
158    pass
159
160
161class ExtendedCertificate(univ.Sequence):
162    componentType = namedtype.NamedTypes(
163        namedtype.NamedType('extendedCertificateInfo', ExtendedCertificateInfo()),
164        namedtype.NamedType('signatureAlgorithm', SignatureAlgorithmIdentifier()),
165        namedtype.NamedType('signature', Signature())
166    )
167
168
169class ExtendedCertificateOrCertificate(univ.Choice):
170    componentType = namedtype.NamedTypes(
171        namedtype.NamedType('certificate', Certificate()),
172        namedtype.NamedType('extendedCertificate', ExtendedCertificate().subtype(
173            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0)))
174    )
175
176
177class ExtendedCertificatesAndCertificates(univ.SetOf):
178    componentType = ExtendedCertificateOrCertificate()
179
180
181class SerialNumber(univ.Integer):
182    pass
183
184
185class CRLEntry(univ.Sequence):
186    componentType = namedtype.NamedTypes(
187        namedtype.NamedType('userCertificate', SerialNumber()),
188        namedtype.NamedType('revocationDate', useful.UTCTime())
189    )
190
191
192class TBSCertificateRevocationList(univ.Sequence):
193    componentType = namedtype.NamedTypes(
194        namedtype.NamedType('signature', AlgorithmIdentifier()),
195        namedtype.NamedType('issuer', Name()),
196        namedtype.NamedType('lastUpdate', useful.UTCTime()),
197        namedtype.NamedType('nextUpdate', useful.UTCTime()),
198        namedtype.OptionalNamedType('revokedCertificates', univ.SequenceOf(componentType=CRLEntry()))
199    )
200
201
202class CertificateRevocationList(univ.Sequence):
203    componentType = namedtype.NamedTypes(
204        namedtype.NamedType('tbsCertificateRevocationList', TBSCertificateRevocationList()),
205        namedtype.NamedType('signatureAlgorithm', AlgorithmIdentifier()),
206        namedtype.NamedType('signature', univ.BitString())
207    )
208
209
210class CertificateRevocationLists(univ.SetOf):
211    componentType = CertificateRevocationList()
212
213
214class DigestEncryptionAlgorithmIdentifier(AlgorithmIdentifier):
215    pass
216
217
218class EncryptedDigest(univ.OctetString):
219    pass
220
221
222class SignerInfo(univ.Sequence):
223    componentType = namedtype.NamedTypes(
224        namedtype.NamedType('version', Version()),
225        namedtype.NamedType('issuerAndSerialNumber', IssuerAndSerialNumber()),
226        namedtype.NamedType('digestAlgorithm', DigestAlgorithmIdentifier()),
227        namedtype.OptionalNamedType('authenticatedAttributes', Attributes().subtype(
228            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
229        namedtype.NamedType('digestEncryptionAlgorithm', DigestEncryptionAlgorithmIdentifier()),
230        namedtype.NamedType('encryptedDigest', EncryptedDigest()),
231        namedtype.OptionalNamedType('unauthenticatedAttributes', Attributes().subtype(
232            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)))
233    )
234
235
236class SignerInfos(univ.SetOf):
237    componentType = SignerInfo()
238
239
240class SignedAndEnvelopedData(univ.Sequence):
241    componentType = namedtype.NamedTypes(
242        namedtype.NamedType('version', Version()),
243        namedtype.NamedType('recipientInfos', RecipientInfos()),
244        namedtype.NamedType('digestAlgorithms', DigestAlgorithmIdentifiers()),
245        namedtype.NamedType('encryptedContentInfo', EncryptedContentInfo()),
246        namedtype.OptionalNamedType('certificates', ExtendedCertificatesAndCertificates().subtype(
247            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
248        namedtype.OptionalNamedType('crls', CertificateRevocationLists().subtype(
249            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1))),
250        namedtype.NamedType('signerInfos', SignerInfos())
251    )
252
253
254class EnvelopedData(univ.Sequence):
255    componentType = namedtype.NamedTypes(
256        namedtype.NamedType('version', Version()),
257        namedtype.NamedType('recipientInfos', RecipientInfos()),
258        namedtype.NamedType('encryptedContentInfo', EncryptedContentInfo())
259    )
260
261
262class DigestInfo(univ.Sequence):
263    componentType = namedtype.NamedTypes(
264        namedtype.NamedType('digestAlgorithm', DigestAlgorithmIdentifier()),
265        namedtype.NamedType('digest', Digest())
266    )
267
268
269class SignedData(univ.Sequence):
270    componentType = namedtype.NamedTypes(
271        namedtype.NamedType('version', Version()),
272        namedtype.OptionalNamedType('digestAlgorithms', DigestAlgorithmIdentifiers()),
273        namedtype.NamedType('contentInfo', ContentInfo()),
274        namedtype.OptionalNamedType('certificates', ExtendedCertificatesAndCertificates().subtype(
275            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
276        namedtype.OptionalNamedType('crls', CertificateRevocationLists().subtype(
277            implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1))),
278        namedtype.OptionalNamedType('signerInfos', SignerInfos())
279    )
280
281
282class Data(univ.OctetString):
283    pass
284
285_contentTypeMapUpdate = {
286    data: Data(),
287    signedData: SignedData(),
288    envelopedData: EnvelopedData(),
289    signedAndEnvelopedData: SignedAndEnvelopedData(),
290    digestedData: DigestedData(),
291    encryptedData: EncryptedData()
292}
293
294contentTypeMap.update(_contentTypeMapUpdate)
295