test_rfc6955.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 rfc5480
  16. from pyasn1_modules import rfc6402
  17. from pyasn1_modules import rfc6955
  18. class CertificationRequestTestCase(unittest.TestCase):
  19. pem_text = """\
  20. MIIDPDCCArsCAQAwTjELMAkGA1UEBhMCVVMxETAPBgNVBAoTCFhFVEkgSW5jMRAw
  21. DgYDVQQLEwdUZXN0aW5nMRowGAYDVQQDExFQS0lYIEV4YW1wbGUgVXNlcjCCAkEw
  22. ggG2BgcqhkjOPgIBMIIBqQKBgQCUhOBFbH9pUWI+VoB8aOfFqZ6edHSU7ZCMHcTh
  23. ShSC9dKUDBnjuRC7EbnlpfuOIVFjAoaqBrghNrZ/Nt/R1mhbeXwdWhR1H2qTdZPO
  24. u5dyivAPI51H9tSzx/D05vYrwjLhiWe+fgau+NABa4sq9QLXtqhjlIOwGzF9Uhre
  25. 5QOFJwKBgCamMixaK9QzK1zcBodTP5AGYVA4PtK5fYEcEhDFDFPUZNGOMAcIjN0/
  26. Ci8s1ht/V4bQ2rtuNioY6NO8cDF6SLZOGG7dHyIG6z/q1EFp2ZveR5V6cpHSCX9J
  27. XDsDM1HI8Tma/wTVbn6UPQO49jEVJkiVqFzeR4i0aToAp4ae2tHNAiEA6HL6lvAR
  28. QPXy3P07XXiUsYUB5Wk3IfclubpxSvxgMPsCYQCjkQHAqG6kTaBW/Gz+H6ewzQ+U
  29. hwwlvpd2jevlpAldq4PNgAs1Z38MjqcxmDKFOUCdEZjY3rh/hpuvjWc9tna0YS8h
  30. 4UsOaP9TPofd2HFWaEfc9yBjSzxfeHGD5nCe4pIwGgMVABzVOg0Xgm0KgXWBRhCO
  31. PtsJ5Jg0AgE3A4GEAAKBgBNjoYUEjEaoiOv0XqiTdK79rp6WJxJlxEwHBj4Y/pS4
  32. qHlIvS40tkfKBDCh7DP9GgstnlDJeA+uauy1a2q+slzasp94LLl34nkrJb8uC1lK
  33. k0v4s+yBNK6XR1LgqCmY7NGwyitveovbTo2lFX5+rzNiCZ4PEUSMwY2iEZ5T77Lo
  34. oCEwHwYJKoZIhvcNAQkOMRIwEDAOBgNVHQ8BAf8EBAMCAwgwDAYIKwYBBQUHBgMF
  35. AANtADBqMFIwSDELMAkGA1UEBhMCVVMxETAPBgNVBAoTCFhFVEkgSW5jMRAwDgYD
  36. VQQLEwdUZXN0aW5nMRQwEgYDVQQDEwtSb290IERTQSBDQQIGANo5tuLLBBQtBXf+
  37. Xo9l9a+tyVybAsCoiClhYw==
  38. """
  39. def setUp(self):
  40. self.asn1Spec = rfc6402.CertificationRequest()
  41. def testDerCodec(self):
  42. substrate = pem.readBase64fromText(self.pem_text)
  43. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  44. self.assertFalse(rest)
  45. self.assertTrue(asn1Object.prettyPrint())
  46. self.assertEqual(substrate, der_encoder(asn1Object))
  47. spki_a = asn1Object['certificationRequestInfo']['subjectPublicKeyInfo']['algorithm']
  48. self.assertEqual(rfc5480.dhpublicnumber, spki_a['algorithm'])
  49. self.assertIn(spki_a['algorithm'], rfc5280.algorithmIdentifierMap)
  50. params, rest = der_decoder(
  51. spki_a['parameters'], asn1Spec=rfc6955.DomainParameters())
  52. self.assertFalse(rest)
  53. self.assertTrue(params.prettyPrint())
  54. self.assertEqual(spki_a['parameters'], der_encoder(params))
  55. self.assertEqual(55, params['validationParms']['pgenCounter'])
  56. sig_a = asn1Object['signatureAlgorithm']
  57. self.assertEqual(
  58. rfc6955.id_dhPop_static_sha1_hmac_sha1, sig_a['algorithm'])
  59. self.assertIn(sig_a['algorithm'], rfc5280.algorithmIdentifierMap)
  60. self.assertEqual(sig_a['parameters'], der_encoder(univ.Null("")))
  61. def testOpenTypes(self):
  62. substrate = pem.readBase64fromText(self.pem_text)
  63. asn1Object, rest = der_decoder(
  64. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  65. self.assertFalse(rest)
  66. self.assertTrue(asn1Object.prettyPrint())
  67. self.assertEqual(substrate, der_encoder(asn1Object))
  68. spki_a = asn1Object['certificationRequestInfo']['subjectPublicKeyInfo']['algorithm']
  69. self.assertEqual(rfc5480.dhpublicnumber, spki_a['algorithm'])
  70. self.assertEqual(
  71. 55, spki_a['parameters']['validationParms']['pgenCounter'])
  72. sig_a = asn1Object['signatureAlgorithm']
  73. self.assertEqual(
  74. rfc6955.id_dhPop_static_sha1_hmac_sha1, sig_a['algorithm'])
  75. self.assertEqual(univ.Null(""), sig_a['parameters'])
  76. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  77. if __name__ == '__main__':
  78. result = unittest.TextTestRunner(verbosity=2).run(suite)
  79. sys.exit(not result.wasSuccessful())