rfc8696.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley with some assistance from asn1ate v.0.6.0.
  5. #
  6. # Copyright (c) 2019, Vigil Security, LLC
  7. # License: http://snmplabs.com/pyasn1/license.html
  8. #
  9. # Using Pre-Shared Key (PSK) in the Cryptographic Message Syntax (CMS)
  10. #
  11. # ASN.1 source from:
  12. # https://www.rfc-editor.org/rfc/rfc8696.txt
  13. #
  14. from pyasn1.type import constraint
  15. from pyasn1.type import namedtype
  16. from pyasn1.type import namedval
  17. from pyasn1.type import tag
  18. from pyasn1.type import univ
  19. from pyasn1_modules import rfc5652
  20. MAX = float('inf')
  21. id_ori = univ.ObjectIdentifier('1.2.840.113549.1.9.16.13')
  22. id_ori_keyTransPSK = univ.ObjectIdentifier('1.2.840.113549.1.9.16.13.1')
  23. id_ori_keyAgreePSK = univ.ObjectIdentifier('1.2.840.113549.1.9.16.13.2')
  24. class PreSharedKeyIdentifier(univ.OctetString):
  25. pass
  26. class KeyTransRecipientInfos(univ.SequenceOf):
  27. componentType = rfc5652.KeyTransRecipientInfo()
  28. class KeyTransPSKRecipientInfo(univ.Sequence):
  29. componentType = namedtype.NamedTypes(
  30. namedtype.NamedType('version',
  31. rfc5652.CMSVersion()),
  32. namedtype.NamedType('pskid',
  33. PreSharedKeyIdentifier()),
  34. namedtype.NamedType('kdfAlgorithm',
  35. rfc5652.KeyDerivationAlgorithmIdentifier()),
  36. namedtype.NamedType('keyEncryptionAlgorithm',
  37. rfc5652.KeyEncryptionAlgorithmIdentifier()),
  38. namedtype.NamedType('ktris',
  39. KeyTransRecipientInfos()),
  40. namedtype.NamedType('encryptedKey',
  41. rfc5652.EncryptedKey())
  42. )
  43. class KeyAgreePSKRecipientInfo(univ.Sequence):
  44. componentType = namedtype.NamedTypes(
  45. namedtype.NamedType('version',
  46. rfc5652.CMSVersion()),
  47. namedtype.NamedType('pskid',
  48. PreSharedKeyIdentifier()),
  49. namedtype.NamedType('originator',
  50. rfc5652.OriginatorIdentifierOrKey().subtype(explicitTag=tag.Tag(
  51. tag.tagClassContext, tag.tagFormatConstructed, 0))),
  52. namedtype.OptionalNamedType('ukm',
  53. rfc5652.UserKeyingMaterial().subtype(explicitTag=tag.Tag(
  54. tag.tagClassContext, tag.tagFormatSimple, 1))),
  55. namedtype.NamedType('kdfAlgorithm',
  56. rfc5652.KeyDerivationAlgorithmIdentifier()),
  57. namedtype.NamedType('keyEncryptionAlgorithm',
  58. rfc5652.KeyEncryptionAlgorithmIdentifier()),
  59. namedtype.NamedType('recipientEncryptedKeys',
  60. rfc5652.RecipientEncryptedKeys())
  61. )
  62. class CMSORIforPSKOtherInfo(univ.Sequence):
  63. componentType = namedtype.NamedTypes(
  64. namedtype.NamedType('psk',
  65. univ.OctetString()),
  66. namedtype.NamedType('keyMgmtAlgType',
  67. univ.Enumerated(namedValues=namedval.NamedValues(
  68. ('keyTrans', 5), ('keyAgree', 10)))),
  69. namedtype.NamedType('keyEncryptionAlgorithm',
  70. rfc5652.KeyEncryptionAlgorithmIdentifier()),
  71. namedtype.NamedType('pskLength',
  72. univ.Integer().subtype(
  73. subtypeSpec=constraint.ValueRangeConstraint(1, MAX))),
  74. namedtype.NamedType('kdkLength',
  75. univ.Integer().subtype(
  76. subtypeSpec=constraint.ValueRangeConstraint(1, MAX)))
  77. )
  78. # Update the CMS Other Recipient Info map in rfc5652.py
  79. _otherRecipientInfoMapUpdate = {
  80. id_ori_keyTransPSK: KeyTransPSKRecipientInfo(),
  81. id_ori_keyAgreePSK: KeyAgreePSKRecipientInfo(),
  82. }
  83. rfc5652.otherRecipientInfoMap.update(_otherRecipientInfoMapUpdate)