rfc5990.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. #
  6. # Copyright (c) 2019, Vigil Security, LLC
  7. # License: http://snmplabs.com/pyasn1/license.html
  8. #
  9. # Use of the RSA-KEM Key Transport Algorithm in the CMS
  10. #
  11. # ASN.1 source from:
  12. # https://www.rfc-editor.org/rfc/rfc5990.txt
  13. #
  14. from pyasn1.type import constraint
  15. from pyasn1.type import namedtype
  16. from pyasn1.type import univ
  17. from pyasn1_modules import rfc5280
  18. MAX = float('inf')
  19. def _OID(*components):
  20. output = []
  21. for x in tuple(components):
  22. if isinstance(x, univ.ObjectIdentifier):
  23. output.extend(list(x))
  24. else:
  25. output.append(int(x))
  26. return univ.ObjectIdentifier(output)
  27. # Imports from RFC 5280
  28. AlgorithmIdentifier = rfc5280.AlgorithmIdentifier
  29. # Useful types and definitions
  30. class NullParms(univ.Null):
  31. pass
  32. # Object identifier arcs
  33. is18033_2 = _OID(1, 0, 18033, 2)
  34. nistAlgorithm = _OID(2, 16, 840, 1, 101, 3, 4)
  35. pkcs_1 = _OID(1, 2, 840, 113549, 1, 1)
  36. x9_44 = _OID(1, 3, 133, 16, 840, 9, 44)
  37. x9_44_components = _OID(x9_44, 1)
  38. # Types for algorithm identifiers
  39. class Camellia_KeyWrappingScheme(AlgorithmIdentifier):
  40. pass
  41. class DataEncapsulationMechanism(AlgorithmIdentifier):
  42. pass
  43. class KDF2_HashFunction(AlgorithmIdentifier):
  44. pass
  45. class KDF3_HashFunction(AlgorithmIdentifier):
  46. pass
  47. class KeyDerivationFunction(AlgorithmIdentifier):
  48. pass
  49. class KeyEncapsulationMechanism(AlgorithmIdentifier):
  50. pass
  51. class X9_SymmetricKeyWrappingScheme(AlgorithmIdentifier):
  52. pass
  53. # RSA-KEM Key Transport Algorithm
  54. id_rsa_kem = _OID(1, 2, 840, 113549, 1, 9, 16, 3, 14)
  55. class GenericHybridParameters(univ.Sequence):
  56. pass
  57. GenericHybridParameters.componentType = namedtype.NamedTypes(
  58. namedtype.NamedType('kem', KeyEncapsulationMechanism()),
  59. namedtype.NamedType('dem', DataEncapsulationMechanism())
  60. )
  61. rsa_kem = AlgorithmIdentifier()
  62. rsa_kem['algorithm'] = id_rsa_kem
  63. rsa_kem['parameters'] = GenericHybridParameters()
  64. # KEM-RSA Key Encapsulation Mechanism
  65. id_kem_rsa = _OID(is18033_2, 2, 4)
  66. class KeyLength(univ.Integer):
  67. pass
  68. KeyLength.subtypeSpec = constraint.ValueRangeConstraint(1, MAX)
  69. class RsaKemParameters(univ.Sequence):
  70. pass
  71. RsaKemParameters.componentType = namedtype.NamedTypes(
  72. namedtype.NamedType('keyDerivationFunction', KeyDerivationFunction()),
  73. namedtype.NamedType('keyLength', KeyLength())
  74. )
  75. kem_rsa = AlgorithmIdentifier()
  76. kem_rsa['algorithm'] = id_kem_rsa
  77. kem_rsa['parameters'] = RsaKemParameters()
  78. # Key Derivation Functions
  79. id_kdf_kdf2 = _OID(x9_44_components, 1)
  80. id_kdf_kdf3 = _OID(x9_44_components, 2)
  81. kdf2 = AlgorithmIdentifier()
  82. kdf2['algorithm'] = id_kdf_kdf2
  83. kdf2['parameters'] = KDF2_HashFunction()
  84. kdf3 = AlgorithmIdentifier()
  85. kdf3['algorithm'] = id_kdf_kdf3
  86. kdf3['parameters'] = KDF3_HashFunction()
  87. # Hash Functions
  88. id_sha1 = _OID(1, 3, 14, 3, 2, 26)
  89. id_sha224 = _OID(2, 16, 840, 1, 101, 3, 4, 2, 4)
  90. id_sha256 = _OID(2, 16, 840, 1, 101, 3, 4, 2, 1)
  91. id_sha384 = _OID(2, 16, 840, 1, 101, 3, 4, 2, 2)
  92. id_sha512 = _OID(2, 16, 840, 1, 101, 3, 4, 2, 3)
  93. sha1 = AlgorithmIdentifier()
  94. sha1['algorithm'] = id_sha1
  95. sha1['parameters'] = univ.Null("")
  96. sha224 = AlgorithmIdentifier()
  97. sha224['algorithm'] = id_sha224
  98. sha224['parameters'] = univ.Null("")
  99. sha256 = AlgorithmIdentifier()
  100. sha256['algorithm'] = id_sha256
  101. sha256['parameters'] = univ.Null("")
  102. sha384 = AlgorithmIdentifier()
  103. sha384['algorithm'] = id_sha384
  104. sha384['parameters'] = univ.Null("")
  105. sha512 = AlgorithmIdentifier()
  106. sha512['algorithm'] = id_sha512
  107. sha512['parameters'] = univ.Null("")
  108. # Symmetric Key-Wrapping Schemes
  109. id_aes128_Wrap = _OID(nistAlgorithm, 1, 5)
  110. id_aes192_Wrap = _OID(nistAlgorithm, 1, 25)
  111. id_aes256_Wrap = _OID(nistAlgorithm, 1, 45)
  112. id_alg_CMS3DESwrap = _OID(1, 2, 840, 113549, 1, 9, 16, 3, 6)
  113. id_camellia128_Wrap = _OID(1, 2, 392, 200011, 61, 1, 1, 3, 2)
  114. id_camellia192_Wrap = _OID(1, 2, 392, 200011, 61, 1, 1, 3, 3)
  115. id_camellia256_Wrap = _OID(1, 2, 392, 200011, 61, 1, 1, 3, 4)
  116. aes128_Wrap = AlgorithmIdentifier()
  117. aes128_Wrap['algorithm'] = id_aes128_Wrap
  118. # aes128_Wrap['parameters'] are absent
  119. aes192_Wrap = AlgorithmIdentifier()
  120. aes192_Wrap['algorithm'] = id_aes128_Wrap
  121. # aes192_Wrap['parameters'] are absent
  122. aes256_Wrap = AlgorithmIdentifier()
  123. aes256_Wrap['algorithm'] = id_sha256
  124. # aes256_Wrap['parameters'] are absent
  125. tdes_Wrap = AlgorithmIdentifier()
  126. tdes_Wrap['algorithm'] = id_alg_CMS3DESwrap
  127. tdes_Wrap['parameters'] = univ.Null("")
  128. camellia128_Wrap = AlgorithmIdentifier()
  129. camellia128_Wrap['algorithm'] = id_camellia128_Wrap
  130. # camellia128_Wrap['parameters'] are absent
  131. camellia192_Wrap = AlgorithmIdentifier()
  132. camellia192_Wrap['algorithm'] = id_camellia192_Wrap
  133. # camellia192_Wrap['parameters'] are absent
  134. camellia256_Wrap = AlgorithmIdentifier()
  135. camellia256_Wrap['algorithm'] = id_camellia256_Wrap
  136. # camellia256_Wrap['parameters'] are absent
  137. # Update the Algorithm Identifier map in rfc5280.py.
  138. # Note that the ones that must not have parameters are not added to the map.
  139. _algorithmIdentifierMapUpdate = {
  140. id_rsa_kem: GenericHybridParameters(),
  141. id_kem_rsa: RsaKemParameters(),
  142. id_kdf_kdf2: KDF2_HashFunction(),
  143. id_kdf_kdf3: KDF3_HashFunction(),
  144. id_sha1: univ.Null(),
  145. id_sha224: univ.Null(),
  146. id_sha256: univ.Null(),
  147. id_sha384: univ.Null(),
  148. id_sha512: univ.Null(),
  149. id_alg_CMS3DESwrap: univ.Null(),
  150. }
  151. rfc5280.algorithmIdentifierMap.update(_algorithmIdentifierMapUpdate)