test_rfc4491.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 rfc5280
  13. from pyasn1_modules import rfc4491
  14. from pyasn1_modules import rfc4357
  15. class GostR341094CertificateTestCase(unittest.TestCase):
  16. gostR3410_94_cert_pem_text = """\
  17. MIICCzCCAboCECMO42BGlSTOxwvklBgufuswCAYGKoUDAgIEMGkxHTAbBgNVBAMM
  18. FEdvc3RSMzQxMC05NCBleGFtcGxlMRIwEAYDVQQKDAlDcnlwdG9Qcm8xCzAJBgNV
  19. BAYTAlJVMScwJQYJKoZIhvcNAQkBFhhHb3N0UjM0MTAtOTRAZXhhbXBsZS5jb20w
  20. HhcNMDUwODE2MTIzMjUwWhcNMTUwODE2MTIzMjUwWjBpMR0wGwYDVQQDDBRHb3N0
  21. UjM0MTAtOTQgZXhhbXBsZTESMBAGA1UECgwJQ3J5cHRvUHJvMQswCQYDVQQGEwJS
  22. VTEnMCUGCSqGSIb3DQEJARYYR29zdFIzNDEwLTk0QGV4YW1wbGUuY29tMIGlMBwG
  23. BiqFAwICFDASBgcqhQMCAiACBgcqhQMCAh4BA4GEAASBgLuEZuF5nls02CyAfxOo
  24. GWZxV/6MVCUhR28wCyd3RpjG+0dVvrey85NsObVCNyaE4g0QiiQOHwxCTSs7ESuo
  25. v2Y5MlyUi8Go/htjEvYJJYfMdRv05YmKCYJo01x3pg+2kBATjeM+fJyR1qwNCCw+
  26. eMG1wra3Gqgqi0WBkzIydvp7MAgGBiqFAwICBANBABHHCH4S3ALxAiMpR3aPRyqB
  27. g1DjB8zy5DEjiULIc+HeIveF81W9lOxGkZxnrFjXBSqnjLeFKgF1hffXOAP7zUM=
  28. """
  29. def setUp(self):
  30. self.asn1Spec = rfc5280.Certificate()
  31. def testDerCodec(self):
  32. substrate = pem.readBase64fromText(self.gostR3410_94_cert_pem_text)
  33. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  34. self.assertFalse(rest)
  35. self.assertTrue(asn1Object.prettyPrint())
  36. self.assertEqual(substrate, der_encoder(asn1Object))
  37. sa1 = asn1Object['signatureAlgorithm']['algorithm']
  38. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_94, sa1)
  39. sa2 = asn1Object['tbsCertificate']['signature']['algorithm']
  40. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_94, sa2)
  41. spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  42. self.assertEqual(rfc4491.id_GostR3410_94, spki_a['algorithm'])
  43. pk_p, rest = der_decoder(
  44. spki_a['parameters'],
  45. asn1Spec=rfc4491.GostR3410_94_PublicKeyParameters())
  46. self.assertFalse(rest)
  47. self.assertTrue(pk_p.prettyPrint())
  48. self.assertEqual(spki_a['parameters'], der_encoder(pk_p))
  49. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, pk_p['digestParamSet'])
  50. def testOpenTypes(self):
  51. openTypesMap = {
  52. rfc4491.id_GostR3410_94: rfc4491.GostR3410_94_PublicKeyParameters(),
  53. }
  54. substrate = pem.readBase64fromText(self.gostR3410_94_cert_pem_text)
  55. asn1Object, rest = der_decoder(
  56. substrate, asn1Spec=self.asn1Spec,
  57. openTypes=openTypesMap, decodeOpenTypes=True)
  58. self.assertFalse(rest)
  59. self.assertTrue(asn1Object.prettyPrint())
  60. self.assertEqual(substrate, der_encoder(asn1Object))
  61. sa1 = asn1Object['signatureAlgorithm']['algorithm']
  62. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_94, sa1)
  63. sa2 = asn1Object['tbsCertificate']['signature']['algorithm']
  64. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_94, sa2)
  65. spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  66. self.assertEqual(rfc4491.id_GostR3410_94, spki_a['algorithm'])
  67. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, spki_a['parameters']['digestParamSet'])
  68. class GostR34102001CertificateTestCase(unittest.TestCase):
  69. gostR3410_2001_cert_pem_text = """\
  70. MIIB0DCCAX8CECv1xh7CEb0Xx9zUYma0LiEwCAYGKoUDAgIDMG0xHzAdBgNVBAMM
  71. Fkdvc3RSMzQxMC0yMDAxIGV4YW1wbGUxEjAQBgNVBAoMCUNyeXB0b1BybzELMAkG
  72. A1UEBhMCUlUxKTAnBgkqhkiG9w0BCQEWGkdvc3RSMzQxMC0yMDAxQGV4YW1wbGUu
  73. Y29tMB4XDTA1MDgxNjE0MTgyMFoXDTE1MDgxNjE0MTgyMFowbTEfMB0GA1UEAwwW
  74. R29zdFIzNDEwLTIwMDEgZXhhbXBsZTESMBAGA1UECgwJQ3J5cHRvUHJvMQswCQYD
  75. VQQGEwJSVTEpMCcGCSqGSIb3DQEJARYaR29zdFIzNDEwLTIwMDFAZXhhbXBsZS5j
  76. b20wYzAcBgYqhQMCAhMwEgYHKoUDAgIkAAYHKoUDAgIeAQNDAARAhJVodWACGkB1
  77. CM0TjDGJLP3lBQN6Q1z0bSsP508yfleP68wWuZWIA9CafIWuD+SN6qa7flbHy7Df
  78. D2a8yuoaYDAIBgYqhQMCAgMDQQA8L8kJRLcnqeyn1en7U23Sw6pkfEQu3u0xFkVP
  79. vFQ/3cHeF26NG+xxtZPz3TaTVXdoiYkXYiD02rEx1bUcM97i
  80. """
  81. def setUp(self):
  82. self.asn1Spec = rfc5280.Certificate()
  83. def testDerCodec(self):
  84. substrate = pem.readBase64fromText(self.gostR3410_2001_cert_pem_text)
  85. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  86. self.assertFalse(rest)
  87. self.assertTrue(asn1Object.prettyPrint())
  88. self.assertEqual(substrate, der_encoder(asn1Object))
  89. sa1 = asn1Object['signatureAlgorithm']['algorithm']
  90. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_2001, sa1)
  91. sa2 = asn1Object['tbsCertificate']['signature']['algorithm']
  92. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_2001, sa2)
  93. spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  94. self.assertEqual(rfc4491.id_GostR3410_2001, spki_a['algorithm'])
  95. pk_p, rest = der_decoder(
  96. spki_a['parameters'], asn1Spec=rfc4491.GostR3410_2001_PublicKeyParameters())
  97. self.assertFalse(rest)
  98. self.assertTrue(pk_p.prettyPrint())
  99. self.assertEqual(spki_a['parameters'], der_encoder(pk_p))
  100. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, pk_p['digestParamSet'])
  101. def testOpenTypes(self):
  102. openTypeMap = {
  103. rfc4491.id_GostR3410_2001: rfc4491.GostR3410_2001_PublicKeyParameters(),
  104. }
  105. substrate = pem.readBase64fromText(self.gostR3410_2001_cert_pem_text)
  106. asn1Object, rest = der_decoder(
  107. substrate, asn1Spec=self.asn1Spec,
  108. openTypes=openTypeMap, decodeOpenTypes=True)
  109. self.assertFalse(rest)
  110. self.assertTrue(asn1Object.prettyPrint())
  111. self.assertEqual(substrate, der_encoder(asn1Object))
  112. sa1 = asn1Object['signatureAlgorithm']['algorithm']
  113. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_2001, sa1)
  114. sa2 = asn1Object['tbsCertificate']['signature']['algorithm']
  115. self.assertEqual(rfc4491.id_GostR3411_94_with_GostR3410_2001, sa2)
  116. spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  117. self.assertEqual(rfc4491.id_GostR3410_2001, spki_a['algorithm'])
  118. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, spki_a['parameters']['digestParamSet'])
  119. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  120. if __name__ == '__main__':
  121. result = unittest.TextTestRunner(verbosity=2).run(suite)
  122. sys.exit(not result.wasSuccessful())