rfc2634.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley with assistance from asn1ate v.0.6.0.
  5. # Modified by Russ Housley to add a map for use with opentypes.
  6. #
  7. # Copyright (c) 2019, Vigil Security, LLC
  8. # License: http://snmplabs.com/pyasn1/license.html
  9. #
  10. # Enhanced Security Services for S/MIME
  11. #
  12. # ASN.1 source from:
  13. # https://www.rfc-editor.org/rfc/rfc2634.txt
  14. #
  15. from pyasn1.type import char
  16. from pyasn1.type import constraint
  17. from pyasn1.type import namedval
  18. from pyasn1.type import namedtype
  19. from pyasn1.type import tag
  20. from pyasn1.type import univ
  21. from pyasn1.type import useful
  22. from pyasn1_modules import rfc5652
  23. from pyasn1_modules import rfc5280
  24. MAX = float('inf')
  25. ContentType = rfc5652.ContentType
  26. IssuerAndSerialNumber = rfc5652.IssuerAndSerialNumber
  27. SubjectKeyIdentifier = rfc5652.SubjectKeyIdentifier
  28. PolicyInformation = rfc5280.PolicyInformation
  29. GeneralNames = rfc5280.GeneralNames
  30. CertificateSerialNumber = rfc5280.CertificateSerialNumber
  31. # Signing Certificate Attribute
  32. # Warning: It is better to use SigningCertificateV2 from RFC 5035
  33. id_aa_signingCertificate = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.12')
  34. class Hash(univ.OctetString):
  35. pass # SHA-1 hash of entire certificate; RFC 5035 supports other hash algorithms
  36. class IssuerSerial(univ.Sequence):
  37. pass
  38. IssuerSerial.componentType = namedtype.NamedTypes(
  39. namedtype.NamedType('issuer', GeneralNames()),
  40. namedtype.NamedType('serialNumber', CertificateSerialNumber())
  41. )
  42. class ESSCertID(univ.Sequence):
  43. pass
  44. ESSCertID.componentType = namedtype.NamedTypes(
  45. namedtype.NamedType('certHash', Hash()),
  46. namedtype.OptionalNamedType('issuerSerial', IssuerSerial())
  47. )
  48. class SigningCertificate(univ.Sequence):
  49. pass
  50. SigningCertificate.componentType = namedtype.NamedTypes(
  51. namedtype.NamedType('certs', univ.SequenceOf(
  52. componentType=ESSCertID())),
  53. namedtype.OptionalNamedType('policies', univ.SequenceOf(
  54. componentType=PolicyInformation()))
  55. )
  56. # Mail List Expansion History Attribute
  57. id_aa_mlExpandHistory = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.3')
  58. ub_ml_expansion_history = univ.Integer(64)
  59. class EntityIdentifier(univ.Choice):
  60. pass
  61. EntityIdentifier.componentType = namedtype.NamedTypes(
  62. namedtype.NamedType('issuerAndSerialNumber', IssuerAndSerialNumber()),
  63. namedtype.NamedType('subjectKeyIdentifier', SubjectKeyIdentifier())
  64. )
  65. class MLReceiptPolicy(univ.Choice):
  66. pass
  67. MLReceiptPolicy.componentType = namedtype.NamedTypes(
  68. namedtype.NamedType('none', univ.Null().subtype(implicitTag=tag.Tag(
  69. tag.tagClassContext, tag.tagFormatSimple, 0))),
  70. namedtype.NamedType('insteadOf', univ.SequenceOf(
  71. componentType=GeneralNames()).subtype(
  72. sizeSpec=constraint.ValueSizeConstraint(1, MAX)).subtype(
  73. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 1))),
  74. namedtype.NamedType('inAdditionTo', univ.SequenceOf(
  75. componentType=GeneralNames()).subtype(
  76. sizeSpec=constraint.ValueSizeConstraint(1, MAX)).subtype(
  77. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 2)))
  78. )
  79. class MLData(univ.Sequence):
  80. pass
  81. MLData.componentType = namedtype.NamedTypes(
  82. namedtype.NamedType('mailListIdentifier', EntityIdentifier()),
  83. namedtype.NamedType('expansionTime', useful.GeneralizedTime()),
  84. namedtype.OptionalNamedType('mlReceiptPolicy', MLReceiptPolicy())
  85. )
  86. class MLExpansionHistory(univ.SequenceOf):
  87. pass
  88. MLExpansionHistory.componentType = MLData()
  89. MLExpansionHistory.sizeSpec = constraint.ValueSizeConstraint(1, ub_ml_expansion_history)
  90. # ESS Security Label Attribute
  91. id_aa_securityLabel = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.2')
  92. ub_privacy_mark_length = univ.Integer(128)
  93. ub_security_categories = univ.Integer(64)
  94. ub_integer_options = univ.Integer(256)
  95. class ESSPrivacyMark(univ.Choice):
  96. pass
  97. ESSPrivacyMark.componentType = namedtype.NamedTypes(
  98. namedtype.NamedType('pString', char.PrintableString().subtype(
  99. subtypeSpec=constraint.ValueSizeConstraint(1, ub_privacy_mark_length))),
  100. namedtype.NamedType('utf8String', char.UTF8String().subtype(
  101. subtypeSpec=constraint.ValueSizeConstraint(1, MAX)))
  102. )
  103. class SecurityClassification(univ.Integer):
  104. pass
  105. SecurityClassification.subtypeSpec=constraint.ValueRangeConstraint(0, ub_integer_options)
  106. SecurityClassification.namedValues = namedval.NamedValues(
  107. ('unmarked', 0),
  108. ('unclassified', 1),
  109. ('restricted', 2),
  110. ('confidential', 3),
  111. ('secret', 4),
  112. ('top-secret', 5)
  113. )
  114. class SecurityPolicyIdentifier(univ.ObjectIdentifier):
  115. pass
  116. class SecurityCategory(univ.Sequence):
  117. pass
  118. SecurityCategory.componentType = namedtype.NamedTypes(
  119. namedtype.NamedType('type', univ.ObjectIdentifier().subtype(
  120. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  121. namedtype.NamedType('value', univ.Any().subtype(implicitTag=tag.Tag(
  122. tag.tagClassContext, tag.tagFormatSimple, 1)))
  123. )
  124. class SecurityCategories(univ.SetOf):
  125. pass
  126. SecurityCategories.componentType = SecurityCategory()
  127. SecurityCategories.sizeSpec = constraint.ValueSizeConstraint(1, ub_security_categories)
  128. class ESSSecurityLabel(univ.Set):
  129. pass
  130. ESSSecurityLabel.componentType = namedtype.NamedTypes(
  131. namedtype.NamedType('security-policy-identifier', SecurityPolicyIdentifier()),
  132. namedtype.OptionalNamedType('security-classification', SecurityClassification()),
  133. namedtype.OptionalNamedType('privacy-mark', ESSPrivacyMark()),
  134. namedtype.OptionalNamedType('security-categories', SecurityCategories())
  135. )
  136. # Equivalent Labels Attribute
  137. id_aa_equivalentLabels = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.9')
  138. class EquivalentLabels(univ.SequenceOf):
  139. pass
  140. EquivalentLabels.componentType = ESSSecurityLabel()
  141. # Content Identifier Attribute
  142. id_aa_contentIdentifier = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.7')
  143. class ContentIdentifier(univ.OctetString):
  144. pass
  145. # Content Reference Attribute
  146. id_aa_contentReference = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.10')
  147. class ContentReference(univ.Sequence):
  148. pass
  149. ContentReference.componentType = namedtype.NamedTypes(
  150. namedtype.NamedType('contentType', ContentType()),
  151. namedtype.NamedType('signedContentIdentifier', ContentIdentifier()),
  152. namedtype.NamedType('originatorSignatureValue', univ.OctetString())
  153. )
  154. # Message Signature Digest Attribute
  155. id_aa_msgSigDigest = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.5')
  156. class MsgSigDigest(univ.OctetString):
  157. pass
  158. # Content Hints Attribute
  159. id_aa_contentHint = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.4')
  160. class ContentHints(univ.Sequence):
  161. pass
  162. ContentHints.componentType = namedtype.NamedTypes(
  163. namedtype.OptionalNamedType('contentDescription', char.UTF8String().subtype(subtypeSpec=constraint.ValueSizeConstraint(1, MAX))),
  164. namedtype.NamedType('contentType', ContentType())
  165. )
  166. # Receipt Request Attribute
  167. class AllOrFirstTier(univ.Integer):
  168. pass
  169. AllOrFirstTier.namedValues = namedval.NamedValues(
  170. ('allReceipts', 0),
  171. ('firstTierRecipients', 1)
  172. )
  173. class ReceiptsFrom(univ.Choice):
  174. pass
  175. ReceiptsFrom.componentType = namedtype.NamedTypes(
  176. namedtype.NamedType('allOrFirstTier', AllOrFirstTier().subtype(
  177. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0))),
  178. namedtype.NamedType('receiptList', univ.SequenceOf(
  179. componentType=GeneralNames()).subtype(implicitTag=tag.Tag(
  180. tag.tagClassContext, tag.tagFormatSimple, 1)))
  181. )
  182. id_aa_receiptRequest = univ.ObjectIdentifier('1.2.840.113549.1.9.16.2.1')
  183. ub_receiptsTo = univ.Integer(16)
  184. class ReceiptRequest(univ.Sequence):
  185. pass
  186. ReceiptRequest.componentType = namedtype.NamedTypes(
  187. namedtype.NamedType('signedContentIdentifier', ContentIdentifier()),
  188. namedtype.NamedType('receiptsFrom', ReceiptsFrom()),
  189. namedtype.NamedType('receiptsTo', univ.SequenceOf(componentType=GeneralNames()).subtype(sizeSpec=constraint.ValueSizeConstraint(1, ub_receiptsTo)))
  190. )
  191. # Receipt Content Type
  192. class ESSVersion(univ.Integer):
  193. pass
  194. ESSVersion.namedValues = namedval.NamedValues(
  195. ('v1', 1)
  196. )
  197. id_ct_receipt = univ.ObjectIdentifier('1.2.840.113549.1.9.16.1.1')
  198. class Receipt(univ.Sequence):
  199. pass
  200. Receipt.componentType = namedtype.NamedTypes(
  201. namedtype.NamedType('version', ESSVersion()),
  202. namedtype.NamedType('contentType', ContentType()),
  203. namedtype.NamedType('signedContentIdentifier', ContentIdentifier()),
  204. namedtype.NamedType('originatorSignatureValue', univ.OctetString())
  205. )
  206. # Map of Attribute Type to the Attribute structure is added to the
  207. # ones that are in rfc5652.py
  208. _cmsAttributesMapUpdate = {
  209. id_aa_signingCertificate: SigningCertificate(),
  210. id_aa_mlExpandHistory: MLExpansionHistory(),
  211. id_aa_securityLabel: ESSSecurityLabel(),
  212. id_aa_equivalentLabels: EquivalentLabels(),
  213. id_aa_contentIdentifier: ContentIdentifier(),
  214. id_aa_contentReference: ContentReference(),
  215. id_aa_msgSigDigest: MsgSigDigest(),
  216. id_aa_contentHint: ContentHints(),
  217. id_aa_receiptRequest: ReceiptRequest(),
  218. }
  219. rfc5652.cmsAttributesMap.update(_cmsAttributesMapUpdate)
  220. # Map of Content Type OIDs to Content Types is added to the
  221. # ones that are in rfc5652.py
  222. _cmsContentTypesMapUpdate = {
  223. id_ct_receipt: Receipt(),
  224. }
  225. rfc5652.cmsContentTypesMap.update(_cmsContentTypesMapUpdate)