test_rfc8017.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.decoder import decode as der_decoder
  11. from pyasn1.codec.der.encoder import encode 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 rfc8017
  16. from pyasn1_modules import rfc2985
  17. class SMIMECapabilitiesTestCase(unittest.TestCase):
  18. smime_capabilities_pem_text = """\
  19. MIIBAzA8BgkqhkiG9w0BAQcwL6APMA0GCWCGSAFlAwQCAgUAoRwwGgYJKoZIhvcN
  20. AQEIMA0GCWCGSAFlAwQCAgUAMDwGCSqGSIb3DQEBCjAvoA8wDQYJYIZIAWUDBAIC
  21. BQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAICBQAwDQYJKoZIhvcNAQECBQAw
  22. DQYJKoZIhvcNAQEEBQAwDQYJKoZIhvcNAQEFBQAwDQYJKoZIhvcNAQEOBQAwDQYJ
  23. KoZIhvcNAQELBQAwDQYJKoZIhvcNAQEMBQAwDQYJKoZIhvcNAQENBQAwDQYJKoZI
  24. hvcNAQEPBQAwDQYJKoZIhvcNAQEQBQA=
  25. """
  26. def setUp(self):
  27. self.asn1Spec = rfc2985.SMIMECapabilities()
  28. def testDerCodec(self):
  29. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  30. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  31. self.assertFalse(rest)
  32. self.assertTrue(asn1Object.prettyPrint())
  33. self.assertEqual(substrate, der_encoder(asn1Object))
  34. for cap in asn1Object:
  35. self.assertIn(cap['algorithm'], rfc5280.algorithmIdentifierMap)
  36. if cap['parameters'].hasValue():
  37. p, rest = der_decoder(
  38. cap['parameters'],
  39. asn1Spec=rfc5280.algorithmIdentifierMap[cap['algorithm']])
  40. self.assertFalse(rest)
  41. if not p == univ.Null(""):
  42. self.assertTrue(p.prettyPrint())
  43. self.assertEqual(cap['parameters'], der_encoder(p))
  44. if cap['algorithm'] == rfc8017.id_RSAES_OAEP:
  45. self.assertEqual(
  46. rfc8017.id_sha384, p['hashFunc']['algorithm'])
  47. self.assertEqual(
  48. rfc8017.id_mgf1, p['maskGenFunc']['algorithm'])
  49. def OpenTypesCodec(self):
  50. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  51. asn1Object, rest = der_decoder(
  52. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  53. self.assertFalse(rest)
  54. self.assertTrue(asn1Object.prettyPrint())
  55. self.assertEqual(substrate, der_encoder(asn1Object))
  56. for cap in asn1Object:
  57. if cap['algorithm'] == rfc8017.id_RSAES_OAEP:
  58. p = cap['parameters']
  59. self.assertEqual(
  60. rfc8017.id_sha384, p['hashFunc']['algorithm'])
  61. self.assertEqual(
  62. rfc8017.id_mgf1, p['maskGenFunc']['algorithm'])
  63. class MultiprimeRSAPrivateKeyTestCase(unittest.TestCase):
  64. pem_text = """\
  65. MIIE2QIBAQKCAQEAn82EqwXasE2TFNSmZucB8LNza2mOWLHF3nxpxKXalPMDvezc
  66. 5Dq7Ytcv/k9jJL4j4jYfvR4yyZdU9iHLaD6hOINZ8E6hVpx/4c96ZUSOLzD2g+u+
  67. jIuoNfG+zygSBGYCS6BLCAIsZ+2wUyxYpLJknHJld9/jy+aLmmyrilhH9dH5AUiV
  68. 3NeWht/68++dMXf4ZI/gV4bMSlWhggxkz2WJJpiQdCdJatGkwNDkHmLA9X0tC6OH
  69. SPE7qYdxG38cYS5F445SgnhDpiK7BodSqYLwgehaDjoOYdEgHVnOcpBCDI5zCJSL
  70. b1c/z8uhrB1xxlECR44wCLcKsIIY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Tdi/thhCuCsK3lMD4miN9te8j16YtqEFVWXC3a6DWwIJ6m/xZ50bBwPqM8RsI
  87. 6FWhZw4Dr5VqjYXUvwJWAvapRk9SydDYri/cAtGIkUJVlspkE1emALAaSw30vmfd
  88. hrgYLT6YGOmK3UmcNJ4NVeET275MXWF1ZOhkOGKTN6aj5wPhJaHBMnmUQrq7GwC6
  89. /LfUkSsCVgMCDTV9gbFW8u6TcTVW85dBIeUGxZh1T2pbU3dkGO3IOxOhzJUplH4/
  90. EeEs9dusHakg1ERXAg4Vo1YowPW8kuVbZ9faxeVrmuER5NcCuZzS5X/obGUw
  91. """
  92. def setUp(self):
  93. self.asn1Spec = rfc8017.RSAPrivateKey()
  94. def testDerCodec(self):
  95. substrate = pem.readBase64fromText(self.pem_text)
  96. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  97. self.assertFalse(rest)
  98. self.assertTrue(asn1Object.prettyPrint())
  99. self.assertEqual(substrate, der_encoder(asn1Object))
  100. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  101. if __name__ == '__main__':
  102. result = unittest.TextTestRunner(verbosity=2).run(suite)
  103. sys.exit(not result.wasSuccessful())