rfc8017.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley.
  5. #
  6. # Copyright (c) 2019, Vigil Security, LLC
  7. # License: http://snmplabs.com/pyasn1/license.html
  8. #
  9. # PKCS #1: RSA Cryptography Specifications Version 2.2
  10. #
  11. # ASN.1 source from:
  12. # https://www.rfc-editor.org/rfc/rfc8017.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 univ
  18. from pyasn1_modules import rfc2437
  19. from pyasn1_modules import rfc3447
  20. from pyasn1_modules import rfc4055
  21. from pyasn1_modules import rfc5280
  22. MAX = float('inf')
  23. # Import Algorithm Identifier from RFC 5280
  24. AlgorithmIdentifier = rfc5280.AlgorithmIdentifier
  25. class DigestAlgorithm(AlgorithmIdentifier):
  26. pass
  27. class HashAlgorithm(AlgorithmIdentifier):
  28. pass
  29. class MaskGenAlgorithm(AlgorithmIdentifier):
  30. pass
  31. class PSourceAlgorithm(AlgorithmIdentifier):
  32. pass
  33. # Object identifiers from NIST SHA2
  34. hashAlgs = univ.ObjectIdentifier('2.16.840.1.101.3.4.2')
  35. id_sha256 = rfc4055.id_sha256
  36. id_sha384 = rfc4055.id_sha384
  37. id_sha512 = rfc4055.id_sha512
  38. id_sha224 = rfc4055.id_sha224
  39. id_sha512_224 = hashAlgs + (5, )
  40. id_sha512_256 = hashAlgs + (6, )
  41. # Basic object identifiers
  42. pkcs_1 = univ.ObjectIdentifier('1.2.840.113549.1.1')
  43. rsaEncryption = rfc2437.rsaEncryption
  44. id_RSAES_OAEP = rfc2437.id_RSAES_OAEP
  45. id_pSpecified = rfc2437.id_pSpecified
  46. id_RSASSA_PSS = rfc4055.id_RSASSA_PSS
  47. md2WithRSAEncryption = rfc2437.md2WithRSAEncryption
  48. md5WithRSAEncryption = rfc2437.md5WithRSAEncryption
  49. sha1WithRSAEncryption = rfc2437.sha1WithRSAEncryption
  50. sha224WithRSAEncryption = rfc4055.sha224WithRSAEncryption
  51. sha256WithRSAEncryption = rfc4055.sha256WithRSAEncryption
  52. sha384WithRSAEncryption = rfc4055.sha384WithRSAEncryption
  53. sha512WithRSAEncryption = rfc4055.sha512WithRSAEncryption
  54. sha512_224WithRSAEncryption = pkcs_1 + (15, )
  55. sha512_256WithRSAEncryption = pkcs_1 + (16, )
  56. id_sha1 = rfc2437.id_sha1
  57. id_md2 = univ.ObjectIdentifier('1.2.840.113549.2.2')
  58. id_md5 = univ.ObjectIdentifier('1.2.840.113549.2.5')
  59. id_mgf1 = rfc2437.id_mgf1
  60. # Default parameter values
  61. sha1 = rfc4055.sha1Identifier
  62. SHA1Parameters = univ.Null("")
  63. mgf1SHA1 = rfc4055.mgf1SHA1Identifier
  64. class EncodingParameters(univ.OctetString):
  65. subtypeSpec = constraint.ValueSizeConstraint(0, MAX)
  66. pSpecifiedEmpty = rfc4055.pSpecifiedEmptyIdentifier
  67. emptyString = EncodingParameters(value='')
  68. # Main structures
  69. class Version(univ.Integer):
  70. namedValues = namedval.NamedValues(
  71. ('two-prime', 0),
  72. ('multi', 1)
  73. )
  74. class TrailerField(univ.Integer):
  75. namedValues = namedval.NamedValues(
  76. ('trailerFieldBC', 1)
  77. )
  78. RSAPublicKey = rfc2437.RSAPublicKey
  79. OtherPrimeInfo = rfc3447.OtherPrimeInfo
  80. OtherPrimeInfos = rfc3447.OtherPrimeInfos
  81. RSAPrivateKey = rfc3447.RSAPrivateKey
  82. RSAES_OAEP_params = rfc4055.RSAES_OAEP_params
  83. rSAES_OAEP_Default_Identifier = rfc4055.rSAES_OAEP_Default_Identifier
  84. RSASSA_PSS_params = rfc4055.RSASSA_PSS_params
  85. rSASSA_PSS_Default_Identifier = rfc4055.rSASSA_PSS_Default_Identifier
  86. # Syntax for the EMSA-PKCS1-v1_5 hash identifier
  87. class DigestInfo(univ.Sequence):
  88. componentType = namedtype.NamedTypes(
  89. namedtype.NamedType('digestAlgorithm', DigestAlgorithm()),
  90. namedtype.NamedType('digest', univ.OctetString())
  91. )
  92. # Update the Algorithm Identifier map
  93. _algorithmIdentifierMapUpdate = {
  94. id_sha1: univ.Null(),
  95. id_sha224: univ.Null(),
  96. id_sha256: univ.Null(),
  97. id_sha384: univ.Null(),
  98. id_sha512: univ.Null(),
  99. id_sha512_224: univ.Null(),
  100. id_sha512_256: univ.Null(),
  101. id_mgf1: AlgorithmIdentifier(),
  102. id_pSpecified: univ.OctetString(),
  103. id_RSAES_OAEP: RSAES_OAEP_params(),
  104. id_RSASSA_PSS: RSASSA_PSS_params(),
  105. md2WithRSAEncryption: univ.Null(),
  106. md5WithRSAEncryption: univ.Null(),
  107. sha1WithRSAEncryption: univ.Null(),
  108. sha224WithRSAEncryption: univ.Null(),
  109. sha256WithRSAEncryption: univ.Null(),
  110. sha384WithRSAEncryption: univ.Null(),
  111. sha512WithRSAEncryption: univ.Null(),
  112. sha512_224WithRSAEncryption: univ.Null(),
  113. sha512_256WithRSAEncryption: univ.Null(),
  114. }
  115. rfc5280.algorithmIdentifierMap.update(_algorithmIdentifierMapUpdate)