test_rfc7894.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_modules import pem
  13. from pyasn1_modules import rfc6402
  14. from pyasn1_modules import rfc7894
  15. class AlternativeChallengePasswordTestCase(unittest.TestCase):
  16. otp_pem_text = """\
  17. MIICsjCCAZwCAQAwJDELMAkGA1UEBhMCVVMxFTATBgNVBAMTDDRUUzJWMk5MWEE2
  18. WjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAKmF0oUj5+1rBB+pUO8X
  19. 7FPxer+1BhWOa54RTSucJmBaLx0H95qNaBCcctNDl1kcmIro/a0zMcEvj5Do29vQ
  20. lStJdTeJ/B3X4qzOGShupxJcAhCreRZjN6Yz3T9z0zJ8OPnRvJOzcSiIzlubc9lK
  21. Cpq4U0UsCLLfymOgL9NH4lZi96J+PFuJr0J+rTY38076U2jcPqNq5/L/d6NV9Sz2
  22. IVOvCK1kqP/nElJVibIQZvj9YESLUKyVAfTNxLj3+IpioOOv2dT3kB9wdi4plAVi
  23. UFEUvED1okRrI29+LdPV1UXglOCksyJIIw+DgDtutDE5Co6QkTNURFEdKIV9Sg13
  24. zEECAwEAAaBLMBkGCyqGSIb3DQEJEAI4MQoTCDkwNTAzODQ2MC4GCSqGSIb3DQEJ
  25. DjEhMB8wHQYDVR0OBBYEFBj12LVowM16Ed0D+AmoElKNYP/kMAsGCSqGSIb3DQEB
  26. CwOCAQEAZZdDWKejs3UVfgZI3R9cMWGijmscVeZrjwFVkn7MI9pEDZ2aS1QaRYjY
  27. 1cu9j3i+LQp9LWPIW/ztYk11e/OcZp3fo8pZ+MT66n7YTWfDXNkqqA5xmI84DMEx
  28. /cqenyzOBZWqpZGx7eyM9BtnrdeJ0r2qSc7LYU25FbIQFJJf8IvgMAXWMs50fvs2
  29. Gzns447x952se2ReQ3vYhXdHvYYcgAZfSJZvK+nCmhzzqowv5p15Y5S+IHpBSXTO
  30. a1qhNW4cjdicQZUeQ2R5kiuwZ+8vHaq9jKxAEk0hBeqG6RQaxvNOBQhHtTLNGw/C
  31. NmaF8Y2Sl/MgvC5tjs0Ck0/r3lsoLQ==
  32. """
  33. def setUp(self):
  34. self.asn1Spec = rfc6402.CertificationRequest()
  35. def testDerCodec(self):
  36. substrate = pem.readBase64fromText(self.otp_pem_text)
  37. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  38. self.assertFalse(rest)
  39. self.assertTrue(asn1Object.prettyPrint())
  40. self.assertEqual(substrate, der_encoder(asn1Object))
  41. self.assertEqual(0, asn1Object['certificationRequestInfo']['version'])
  42. for attr in asn1Object['certificationRequestInfo']['attributes']:
  43. self.assertIn(
  44. attr['attrType'], rfc6402.cmcControlAttributesMap)
  45. av, rest = der_decoder(
  46. attr['attrValues'][0],
  47. rfc6402.cmcControlAttributesMap[attr['attrType']])
  48. self.assertFalse(rest)
  49. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  50. if attr['attrType'] == rfc7894.id_aa_otpChallenge:
  51. self.assertEqual('90503846', av['printableString'])
  52. def testOpenTypes(self):
  53. substrate = pem.readBase64fromText(self.otp_pem_text)
  54. asn1Object, rest = der_decoder(
  55. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  56. self.assertFalse(rest)
  57. self.assertTrue(asn1Object.prettyPrint())
  58. self.assertEqual(substrate, der_encoder(asn1Object))
  59. for attr in asn1Object['certificationRequestInfo']['attributes']:
  60. self.assertIn(attr['attrType'], rfc6402.cmcControlAttributesMap)
  61. if attr['attrType'] == rfc7894.id_aa_otpChallenge:
  62. self.assertEqual(
  63. '90503846', attr['attrValues'][0]['printableString'])
  64. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  65. if __name__ == '__main__':
  66. result = unittest.TextTestRunner(verbosity=2).run(suite)
  67. sys.exit(not result.wasSuccessful())