rfc5958.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #
  2. # This file is being contributed to pyasn1-modules software.
  3. #
  4. # Created by Russ Housley.
  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. # Asymmetric Key Packages, which is essentially version 2 of
  11. # the PrivateKeyInfo structure in PKCS#8 in RFC 5208
  12. #
  13. # ASN.1 source from:
  14. # https://www.rfc-editor.org/rfc/rfc5958.txt
  15. from pyasn1.type import univ, constraint, namedtype, namedval, tag
  16. from pyasn1_modules import rfc5280
  17. from pyasn1_modules import rfc5652
  18. MAX = float('inf')
  19. class KeyEncryptionAlgorithmIdentifier(rfc5280.AlgorithmIdentifier):
  20. pass
  21. class PrivateKeyAlgorithmIdentifier(rfc5280.AlgorithmIdentifier):
  22. pass
  23. class EncryptedData(univ.OctetString):
  24. pass
  25. class EncryptedPrivateKeyInfo(univ.Sequence):
  26. componentType = namedtype.NamedTypes(
  27. namedtype.NamedType('encryptionAlgorithm', KeyEncryptionAlgorithmIdentifier()),
  28. namedtype.NamedType('encryptedData', EncryptedData())
  29. )
  30. class Version(univ.Integer):
  31. namedValues = namedval.NamedValues(('v1', 0), ('v2', 1))
  32. class PrivateKey(univ.OctetString):
  33. pass
  34. class Attributes(univ.SetOf):
  35. componentType = rfc5652.Attribute()
  36. class PublicKey(univ.BitString):
  37. pass
  38. # OneAsymmetricKey is essentially version 2 of PrivateKeyInfo.
  39. # If publicKey is present, then the version must be v2;
  40. # otherwise, the version should be v1.
  41. class OneAsymmetricKey(univ.Sequence):
  42. componentType = namedtype.NamedTypes(
  43. namedtype.NamedType('version', Version()),
  44. namedtype.NamedType('privateKeyAlgorithm', PrivateKeyAlgorithmIdentifier()),
  45. namedtype.NamedType('privateKey', PrivateKey()),
  46. namedtype.OptionalNamedType('attributes', Attributes().subtype(
  47. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 0))),
  48. namedtype.OptionalNamedType('publicKey', PublicKey().subtype(
  49. implicitTag=tag.Tag(tag.tagClassContext, tag.tagFormatConstructed, 1)))
  50. )
  51. class PrivateKeyInfo(OneAsymmetricKey):
  52. pass
  53. # The CMS AsymmetricKeyPackage Content Type
  54. id_ct_KP_aKeyPackage = univ.ObjectIdentifier('2.16.840.1.101.2.1.2.78.5')
  55. class AsymmetricKeyPackage(univ.SequenceOf):
  56. pass
  57. AsymmetricKeyPackage.componentType = OneAsymmetricKey()
  58. AsymmetricKeyPackage.sizeSpec=constraint.ValueSizeConstraint(1, MAX)
  59. # Map of Content Type OIDs to Content Types is added to the
  60. # ones that are in rfc5652.py
  61. _cmsContentTypesMapUpdate = {
  62. id_ct_KP_aKeyPackage: AsymmetricKeyPackage(),
  63. }
  64. rfc5652.cmsContentTypesMap.update(_cmsContentTypesMapUpdate)