test_rfc4055.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der import decoder as der_decoder
  11. from pyasn1.codec.der import encoder as der_encoder
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc5280
  15. from pyasn1_modules import rfc4055
  16. class PSSDefautTestCase(unittest.TestCase):
  17. pss_default_pem_text = "MAsGCSqGSIb3DQEBCg=="
  18. def setUp(self):
  19. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  20. def testDerCodec(self):
  21. substrate = pem.readBase64fromText(self.pss_default_pem_text)
  22. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  23. self.assertFalse(rest)
  24. self.assertTrue(asn1Object.prettyPrint())
  25. self.assertTrue(rfc4055.id_RSASSA_PSS, asn1Object[0])
  26. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  27. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  28. def testOpenTypes(self):
  29. substrate = pem.readBase64fromText(self.pss_default_pem_text)
  30. asn1Object, rest = der_decoder.decode(substrate,
  31. asn1Spec=self.asn1Spec,
  32. decodeOpenTypes=True)
  33. self.assertFalse(rest)
  34. self.assertTrue(asn1Object.prettyPrint())
  35. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  36. self.assertFalse(asn1Object['parameters'].hasValue())
  37. class PSSSHA512TestCase(unittest.TestCase):
  38. pss_sha512_pem_text = "MDwGCSqGSIb3DQEBCjAvoA8wDQYJYIZIAWUDBAIDBQChHDAaBg" \
  39. "kqhkiG9w0BAQgwDQYJYIZIAWUDBAIDBQA="
  40. def setUp(self):
  41. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  42. def testDerCodec(self):
  43. substrate = pem.readBase64fromText(self.pss_sha512_pem_text)
  44. asn1Object, rest = der_decoder.decode(
  45. substrate, asn1Spec=self.asn1Spec)
  46. self.assertFalse(rest)
  47. self.assertTrue(asn1Object.prettyPrint())
  48. self.assertTrue(rfc4055.id_RSASSA_PSS, asn1Object[0])
  49. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  50. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  51. def testOpenTypes(self):
  52. substrate = pem.readBase64fromText(self.pss_sha512_pem_text)
  53. asn1Object, rest = der_decoder.decode(
  54. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  55. self.assertFalse(rest)
  56. self.assertTrue(asn1Object.prettyPrint())
  57. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  58. self.assertTrue(asn1Object['parameters'].hasValue())
  59. self.assertTrue(20, asn1Object['parameters']['saltLength'])
  60. class OAEPDefautTestCase(unittest.TestCase):
  61. oaep_default_pem_text = "MAsGCSqGSIb3DQEBBw=="
  62. def setUp(self):
  63. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  64. def testDerCodec(self):
  65. substrate = pem.readBase64fromText(self.oaep_default_pem_text)
  66. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  67. self.assertFalse(rest)
  68. self.assertTrue(asn1Object.prettyPrint())
  69. self.assertTrue(rfc4055.id_RSAES_OAEP, asn1Object[0])
  70. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  71. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  72. def testOpenTypes(self):
  73. substrate = pem.readBase64fromText(self.oaep_default_pem_text)
  74. asn1Object, rest = der_decoder.decode(
  75. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  76. self.assertFalse(rest)
  77. self.assertTrue(asn1Object.prettyPrint())
  78. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  79. self.assertFalse(asn1Object['parameters'].hasValue())
  80. class OAEPSHA256TestCase(unittest.TestCase):
  81. oaep_sha256_pem_text = "MDwGCSqGSIb3DQEBBzAvoA8wDQYJYIZIAWUDBAIBBQChHDAaB" \
  82. "gkqhkiG9w0BAQgwDQYJYIZIAWUDBAIBBQA="
  83. def setUp(self):
  84. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  85. def testDerCodec(self):
  86. substrate = pem.readBase64fromText(self.oaep_sha256_pem_text)
  87. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  88. self.assertFalse(rest)
  89. self.assertTrue(asn1Object.prettyPrint())
  90. self.assertTrue(rfc4055.id_RSAES_OAEP, asn1Object[0])
  91. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  92. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  93. def testOpenTypes(self):
  94. substrate = pem.readBase64fromText(self.oaep_sha256_pem_text)
  95. asn1Object, rest = der_decoder.decode(
  96. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  97. self.assertFalse(rest)
  98. self.assertTrue(asn1Object.prettyPrint())
  99. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  100. self.assertTrue(asn1Object['parameters'].hasValue())
  101. oaep_p = asn1Object['parameters']
  102. self.assertEqual(univ.Null(""), oaep_p['hashFunc']['parameters'])
  103. self.assertEqual(univ.Null(""), oaep_p['maskGenFunc']['parameters']['parameters'])
  104. class OAEPFullTestCase(unittest.TestCase):
  105. oaep_full_pem_text = "MFMGCSqGSIb3DQEBBzBGoA8wDQYJYIZIAWUDBAICBQChHDAaBgk" \
  106. "qhkiG9w0BAQgwDQYJYIZIAWUDBAICBQCiFTATBgkqhkiG9w0BAQ" \
  107. "kEBmZvb2Jhcg=="
  108. def setUp(self):
  109. self.asn1Spec = rfc5280.AlgorithmIdentifier()
  110. def testDerCodec(self):
  111. substrate = pem.readBase64fromText(self.oaep_full_pem_text)
  112. asn1Object, rest = der_decoder.decode(substrate, asn1Spec=self.asn1Spec)
  113. self.assertFalse(rest)
  114. self.assertTrue(asn1Object.prettyPrint())
  115. self.assertTrue(rfc4055.id_RSAES_OAEP, asn1Object[0])
  116. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  117. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  118. def testOpenTypes(self):
  119. substrate = pem.readBase64fromText(self.oaep_full_pem_text)
  120. asn1Object, rest = der_decoder.decode(substrate,
  121. asn1Spec=self.asn1Spec,
  122. decodeOpenTypes=True)
  123. self.assertFalse(rest)
  124. self.assertTrue(asn1Object.prettyPrint())
  125. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  126. self.assertTrue(asn1Object['parameters'].hasValue())
  127. oaep_p = asn1Object['parameters']
  128. self.assertEqual(univ.Null(""), oaep_p['hashFunc']['parameters'])
  129. self.assertEqual(
  130. univ.Null(""), oaep_p['maskGenFunc']['parameters']['parameters'])
  131. self.assertEqual(
  132. univ.OctetString(value='foobar'),
  133. oaep_p['pSourceFunc']['parameters'])
  134. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  135. if __name__ == '__main__':
  136. result = unittest.TextTestRunner(verbosity=2).run(suite)
  137. sys.exit(not result.wasSuccessful())