Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rfc7292.py 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. # This file is being contributed to pyasn1-modules software.
  2. #
  3. # Created by Russ Housley with assistance from the asn1ate tool.
  4. #
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. # PKCS #12: Personal Information Exchange Syntax v1.1
  9. #
  10. # ASN.1 source from:
  11. # https://www.rfc-editor.org/rfc/rfc7292.txt
  12. # https://www.rfc-editor.org/errata_search.php?rfc=7292
  13. from pyasn1.type import char
  14. from pyasn1.type import constraint
  15. from pyasn1.type import namedtype
  16. from pyasn1.type import namedval
  17. from pyasn1.type import opentype
  18. from pyasn1.type import tag
  19. from pyasn1.type import univ
  20. from pyasn1_modules import rfc2315
  21. from pyasn1_modules import rfc5652
  22. from pyasn1_modules import rfc5280
  23. from pyasn1_modules import rfc5958
  24. def _OID(*components):
  25. output = []
  26. for x in tuple(components):
  27. if isinstance(x, univ.ObjectIdentifier):
  28. output.extend(list(x))
  29. else:
  30. output.append(int(x))
  31. return univ.ObjectIdentifier(output)
  32. # Initialize the maps used in PKCS#12
  33. pkcs12BagTypeMap = { }
  34. pkcs12CertBagMap = { }
  35. pkcs12CRLBagMap = { }
  36. pkcs12SecretBagMap = { }
  37. # Imports from RFC 2315, RFC 5652, and RFC 5958
  38. DigestInfo = rfc2315.DigestInfo
  39. ContentInfo = rfc5652.ContentInfo
  40. PKCS12Attribute = rfc5652.Attribute
  41. EncryptedPrivateKeyInfo = rfc5958.EncryptedPrivateKeyInfo
  42. PrivateKeyInfo = rfc5958.PrivateKeyInfo
  43. # CMSSingleAttribute is the same as Attribute in RFC 5652 except the attrValues
  44. # SET must have one and only one member
  45. class AttributeType(univ.ObjectIdentifier):
  46. pass
  47. class AttributeValue(univ.Any):
  48. pass
  49. class AttributeValues(univ.SetOf):
  50. pass
  51. AttributeValues.componentType = AttributeValue()
  52. class CMSSingleAttribute(univ.Sequence):
  53. pass
  54. CMSSingleAttribute.componentType = namedtype.NamedTypes(
  55. namedtype.NamedType('attrType', AttributeType()),
  56. namedtype.NamedType('attrValues',
  57. AttributeValues().subtype(sizeSpec=constraint.ValueSizeConstraint(1, 1)),
  58. openType=opentype.OpenType('attrType', rfc5652.cmsAttributesMap)
  59. )
  60. )
  61. # Object identifier arcs
  62. rsadsi = _OID(1, 2, 840, 113549)
  63. pkcs = _OID(rsadsi, 1)
  64. pkcs_9 = _OID(pkcs, 9)
  65. certTypes = _OID(pkcs_9, 22)
  66. crlTypes = _OID(pkcs_9, 23)
  67. pkcs_12 = _OID(pkcs, 12)
  68. # PBE Algorithm Identifiers and Parameters Structure
  69. pkcs_12PbeIds = _OID(pkcs_12, 1)
  70. pbeWithSHAAnd128BitRC4 = _OID(pkcs_12PbeIds, 1)
  71. pbeWithSHAAnd40BitRC4 = _OID(pkcs_12PbeIds, 2)
  72. pbeWithSHAAnd3_KeyTripleDES_CBC = _OID(pkcs_12PbeIds, 3)
  73. pbeWithSHAAnd2_KeyTripleDES_CBC = _OID(pkcs_12PbeIds, 4)
  74. pbeWithSHAAnd128BitRC2_CBC = _OID(pkcs_12PbeIds, 5)
  75. pbeWithSHAAnd40BitRC2_CBC = _OID(pkcs_12PbeIds, 6)
  76. class Pkcs_12PbeParams(univ.Sequence):
  77. pass
  78. Pkcs_12PbeParams.componentType = namedtype.NamedTypes(
  79. namedtype.NamedType('salt', univ.OctetString()),
  80. namedtype.NamedType('iterations', univ.Integer())
  81. )
  82. # Bag types
  83. bagtypes = _OID(pkcs_12, 10, 1)
  84. class BAG_TYPE(univ.Sequence):
  85. pass
  86. BAG_TYPE.componentType = namedtype.NamedTypes(
  87. namedtype.NamedType('id', univ.ObjectIdentifier()),
  88. namedtype.NamedType('unnamed1', univ.Any(),
  89. openType=opentype.OpenType('attrType', pkcs12BagTypeMap)
  90. )
  91. )
  92. id_keyBag = _OID(bagtypes, 1)
  93. class KeyBag(PrivateKeyInfo):
  94. pass
  95. id_pkcs8ShroudedKeyBag = _OID(bagtypes, 2)
  96. class PKCS8ShroudedKeyBag(EncryptedPrivateKeyInfo):
  97. pass
  98. id_certBag = _OID(bagtypes, 3)
  99. class CertBag(univ.Sequence):
  100. pass
  101. CertBag.componentType = namedtype.NamedTypes(
  102. namedtype.NamedType('certId', univ.ObjectIdentifier()),
  103. namedtype.NamedType('certValue',
  104. univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)),
  105. openType=opentype.OpenType('certId', pkcs12CertBagMap)
  106. )
  107. )
  108. x509Certificate = CertBag()
  109. x509Certificate['certId'] = _OID(certTypes, 1)
  110. x509Certificate['certValue'] = univ.OctetString()
  111. # DER-encoded X.509 certificate stored in OCTET STRING
  112. sdsiCertificate = CertBag()
  113. sdsiCertificate['certId'] = _OID(certTypes, 2)
  114. sdsiCertificate['certValue'] = char.IA5String()
  115. # Base64-encoded SDSI certificate stored in IA5String
  116. id_CRLBag = _OID(bagtypes, 4)
  117. class CRLBag(univ.Sequence):
  118. pass
  119. CRLBag.componentType = namedtype.NamedTypes(
  120. namedtype.NamedType('crlId', univ.ObjectIdentifier()),
  121. namedtype.NamedType('crlValue',
  122. univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)),
  123. openType=opentype.OpenType('crlId', pkcs12CRLBagMap)
  124. )
  125. )
  126. x509CRL = CRLBag()
  127. x509CRL['crlId'] = _OID(crlTypes, 1)
  128. x509CRL['crlValue'] = univ.OctetString()
  129. # DER-encoded X.509 CRL stored in OCTET STRING
  130. id_secretBag = _OID(bagtypes, 5)
  131. class SecretBag(univ.Sequence):
  132. pass
  133. SecretBag.componentType = namedtype.NamedTypes(
  134. namedtype.NamedType('secretTypeId', univ.ObjectIdentifier()),
  135. namedtype.NamedType('secretValue',
  136. univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)),
  137. openType=opentype.OpenType('secretTypeId', pkcs12SecretBagMap)
  138. )
  139. )
  140. id_safeContentsBag = _OID(bagtypes, 6)
  141. class SafeBag(univ.Sequence):
  142. pass
  143. SafeBag.componentType = namedtype.NamedTypes(
  144. namedtype.NamedType('bagId', univ.ObjectIdentifier()),
  145. namedtype.NamedType('bagValue',
  146. univ.Any().subtype(explicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatSimple, 0)),
  147. openType=opentype.OpenType('bagId', pkcs12BagTypeMap)
  148. ),
  149. namedtype.OptionalNamedType('bagAttributes',
  150. univ.SetOf(componentType=PKCS12Attribute())
  151. )
  152. )
  153. class SafeContents(univ.SequenceOf):
  154. pass
  155. SafeContents.componentType = SafeBag()
  156. # The PFX PDU
  157. class AuthenticatedSafe(univ.SequenceOf):
  158. pass
  159. AuthenticatedSafe.componentType = ContentInfo()
  160. # Data if unencrypted
  161. # EncryptedData if password-encrypted
  162. # EnvelopedData if public key-encrypted
  163. class MacData(univ.Sequence):
  164. pass
  165. MacData.componentType = namedtype.NamedTypes(
  166. namedtype.NamedType('mac', DigestInfo()),
  167. namedtype.NamedType('macSalt', univ.OctetString()),
  168. namedtype.DefaultedNamedType('iterations', univ.Integer().subtype(value=1))
  169. # Note: The default is for historical reasons and its use is deprecated
  170. )
  171. class PFX(univ.Sequence):
  172. pass
  173. PFX.componentType = namedtype.NamedTypes(
  174. namedtype.NamedType('version',
  175. univ.Integer(namedValues=namedval.NamedValues(('v3', 3)))
  176. ),
  177. namedtype.NamedType('authSafe', ContentInfo()),
  178. namedtype.OptionalNamedType('macData', MacData())
  179. )
  180. # Local key identifier (also defined as certificateAttribute in rfc2985.py)
  181. pkcs_9_at_localKeyId = _OID(pkcs_9, 21)
  182. localKeyId = CMSSingleAttribute()
  183. localKeyId['attrType'] = pkcs_9_at_localKeyId
  184. localKeyId['attrValues'][0] = univ.OctetString()
  185. # Friendly name (also defined as certificateAttribute in rfc2985.py)
  186. pkcs_9_ub_pkcs9String = univ.Integer(255)
  187. pkcs_9_ub_friendlyName = univ.Integer(pkcs_9_ub_pkcs9String)
  188. pkcs_9_at_friendlyName = _OID(pkcs_9, 20)
  189. class FriendlyName(char.BMPString):
  190. pass
  191. FriendlyName.subtypeSpec = constraint.ValueSizeConstraint(1, pkcs_9_ub_friendlyName)
  192. friendlyName = CMSSingleAttribute()
  193. friendlyName['attrType'] = pkcs_9_at_friendlyName
  194. friendlyName['attrValues'][0] = FriendlyName()
  195. # Update the PKCS#12 maps
  196. _pkcs12BagTypeMap = {
  197. id_keyBag: KeyBag(),
  198. id_pkcs8ShroudedKeyBag: PKCS8ShroudedKeyBag(),
  199. id_certBag: CertBag(),
  200. id_CRLBag: CRLBag(),
  201. id_secretBag: SecretBag(),
  202. id_safeContentsBag: SafeBag(),
  203. }
  204. pkcs12BagTypeMap.update(_pkcs12BagTypeMap)
  205. _pkcs12CertBagMap = {
  206. _OID(certTypes, 1): univ.OctetString(),
  207. _OID(certTypes, 2): char.IA5String(),
  208. }
  209. pkcs12CertBagMap.update(_pkcs12CertBagMap)
  210. _pkcs12CRLBagMap = {
  211. _OID(crlTypes, 1): univ.OctetString(),
  212. }
  213. pkcs12CRLBagMap.update(_pkcs12CRLBagMap)
  214. # Update the Algorithm Identifier map
  215. _algorithmIdentifierMapUpdate = {
  216. pbeWithSHAAnd128BitRC4: Pkcs_12PbeParams(),
  217. pbeWithSHAAnd40BitRC4: Pkcs_12PbeParams(),
  218. pbeWithSHAAnd3_KeyTripleDES_CBC: Pkcs_12PbeParams(),
  219. pbeWithSHAAnd2_KeyTripleDES_CBC: Pkcs_12PbeParams(),
  220. pbeWithSHAAnd128BitRC2_CBC: Pkcs_12PbeParams(),
  221. pbeWithSHAAnd40BitRC2_CBC: Pkcs_12PbeParams(),
  222. }
  223. rfc5280.algorithmIdentifierMap.update(_algorithmIdentifierMapUpdate)
  224. # Update the CMS Attribute map
  225. _cmsAttributesMapUpdate = {
  226. pkcs_9_at_friendlyName: FriendlyName(),
  227. pkcs_9_at_localKeyId: univ.OctetString(),
  228. }
  229. rfc5652.cmsAttributesMap.update(_cmsAttributesMapUpdate)