test_rfc6664.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2019, Vigil Security, LLC
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc5480
  13. from pyasn1_modules import rfc5751
  14. from pyasn1_modules import rfc6664
  15. class SMIMECapabilitiesTestCase(unittest.TestCase):
  16. smime_capabilities_pem_text = """\
  17. MIICOjAJBgUrDgMCGgUAMA0GCWCGSAFlAwQCBAUAMA0GCWCGSAFlAwQCAQUAMA0G
  18. CWCGSAFlAwQCAgUAMA0GCWCGSAFlAwQCAwUAMBUGCSqGSIb3DQEBATAIAgIEAAIC
  19. EAAwFQYJKoZIhvcNAQEHMAgCAgQAAgIQADAVBgkqhkiG9w0BAQowCAICBAACAhAA
  20. MBUGByqGSM44BAGgCjAIAgIEAAICDAAwggEvBgcqhkjOPgIBoYIBIjCCAR4CgYEA
  21. i6Ued8R33vkopJwCvy/ZZv2TtddPXPYmJK4jyFv+TDJTPqnP7XUZCqRuhCyKX10z
  22. 7SgiZs6qlSMk5gCa8shPF8NCHtps2D1OVC7yppZUJI07FoDxoEAZHImdAFvYIA/V
  23. cGYpYOKod4kju0/e4VUBZ6Qoer5vKTh+lD/+ZKa/WSUCFQDc3W87QSZSX6ggdbeI
  24. fzb0rsAhbwKBgCEz/o4WJPUZ4HffJfuXHIGrkPnCxFAYDRtlqueswV0Gy6LunipE
  25. Iu3nCzYkZhMatyFNyzo+NusEsS+9isOhT8jhL93nSBZCSRBy+GfmSXlXv/3c8mtH
  26. XTie5JOqjRdonPr4g/+VZvMkcioooNrhx/zICHrC3WZ72871/n/z9M+dMCMGByqG
  27. SM49AgEwGAYIKoZIzj0DAQcGBSuBBAAiBgUrgQQAIzAhBgUrgQQBDTAYBggqhkjO
  28. PQMBBwYFK4EEACIGBSuBBAAjMBoGCSqGSIb3DQEBCDANBglghkgBZQMEAgEFAA==
  29. """
  30. def setUp(self):
  31. self.asn1Spec = rfc5751.SMIMECapabilities()
  32. def testDerCodec(self):
  33. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  34. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  35. self.assertFalse(rest)
  36. self.assertTrue(asn1Object.prettyPrint())
  37. self.assertEqual(substrate, der_encoder(asn1Object))
  38. count = 0
  39. for cap in asn1Object:
  40. if cap['capabilityID'] in rfc5751.smimeCapabilityMap.keys():
  41. substrate = cap['parameters']
  42. cap_p, rest = der_decoder(
  43. substrate, asn1Spec=rfc5751.smimeCapabilityMap[cap['capabilityID']])
  44. self.assertFalse(rest)
  45. self.assertTrue(cap_p.prettyPrint())
  46. self.assertEqual(substrate, der_encoder(cap_p))
  47. count += 1
  48. self.assertEqual(8, count)
  49. def testOpenTypes(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. parameterValue = {
  57. rfc6664.rsaEncryption: lambda x: x['maxKeySize'],
  58. rfc6664.id_RSAES_OAEP: lambda x: x['maxKeySize'],
  59. rfc6664.id_RSASSA_PSS: lambda x: x['minKeySize'],
  60. rfc6664.id_dsa: lambda x: x['keySizes']['maxKeySize'],
  61. rfc6664.dhpublicnumber: lambda x: x['keyParams']['q'] % 1023,
  62. rfc6664.id_ecPublicKey: lambda x: x[0]['namedCurve'],
  63. rfc6664.id_ecMQV: lambda x: x[1]['namedCurve'],
  64. }
  65. expectedValue = {
  66. rfc6664.rsaEncryption: 4096,
  67. rfc6664.id_RSAES_OAEP: 4096,
  68. rfc6664.id_RSASSA_PSS: 1024,
  69. rfc6664.id_dsa: 3072,
  70. rfc6664.dhpublicnumber: 257,
  71. rfc6664.id_ecPublicKey: rfc5480.secp256r1,
  72. rfc6664.id_ecMQV: rfc5480.secp384r1,
  73. }
  74. count = 0
  75. for cap in asn1Object:
  76. if cap['capabilityID'] in parameterValue.keys():
  77. pValue = parameterValue[cap['capabilityID']](cap['parameters'])
  78. eValue = expectedValue[cap['capabilityID']]
  79. self.assertEqual(eValue, pValue)
  80. count += 1
  81. self.assertEqual(7, count)
  82. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  83. if __name__ == '__main__':
  84. result = unittest.TextTestRunner(verbosity=2).run(suite)
  85. sys.exit(not result.wasSuccessful())