test_rfc2986.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  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.type import char
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc2986
  15. from pyasn1_modules import rfc5280
  16. class CertificationRequestTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIICxjCCAa4CAQAwgYAxCzAJBgNVBAYTAlVTMR0wGwYDVQQDDBRmY3UuZmFrZS5h
  19. ZGRyZXNzLm9yZzEXMBUGA1UEBwwOUGxlYXNhbnQgR3JvdmUxHDAaBgNVBAoME0Zh
  20. a2UgQ29tcGFueSBVbml0ZWQxDTALBgNVBAgMBFV0YWgxDDAKBgNVBAsMA0VuZzCC
  21. ASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALvnYesymhLKSm9Llix53BUA
  22. h99xMDBUYk0OB1VIdNQyjmFabHinM+lYUzVzrfcm1xtYB5QYKbsYuwZ4r5WI7qho
  23. CRJy6JwXqKpOe72ScCogxlGDr2QtKjtvyWrRwXBHX1/OqVSZ3hdz3njhKpmq6HgK
  24. 87vH26RCSmK8FqCgn+qePfpspA7GzBvYwXhXluQtG7r4yBMKNRTQlPst8Vcy+iK+
  25. pI8hmQVrzGi8Hgbpr2L9EjPUOlAQEb8hxeKc7s5VhjN/RHMLVMX8YczZYt7mcDKr
  26. 3PMwOVmXL1DMCtnS50MA2AxcPWcbQBeGyMroP+DLhAt6y1/IT0H5sQruNQw4euMC
  27. AwEAAaAAMA0GCSqGSIb3DQEBCwUAA4IBAQBQXYQPfH5Wy4o0ZFbKQOO1e3dHV8rl
  28. e8m9Z6qLgJO8rtW+OI+4FavJ6zjUvNVzd9JJxgwQ/1xprwrXh36nPcSyNLpGs7JT
  29. 6u7TGQ38QQAOmziLXzauMWGBeLuzWGmOKA1cs5HFGLSmbxF3+0IWpz4GlD86pU1+
  30. WYyWgWHHAMA+kFYwBUR6CvPkmhshnZ8vrQavoOlcidCJ8o6IGA7N/Z0/NrgIDcoz
  31. YaruhoMrmRKHKNpfamhT0gvqEPBec+UB3uLElESIqaeqYc6eMtUQP3lqyghF6I0M
  32. fi6h7i9VVAZpslaKFfkNg12gLbbsCB1q36l5VXjHY/qe0FIUa9ogRrOi
  33. """
  34. def setUp(self):
  35. self.asn1Spec = rfc2986.CertificationRequest()
  36. def testDerCodec(self):
  37. substrate = pem.readBase64fromText(self.pem_text)
  38. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  39. self.assertFalse(rest)
  40. self.assertTrue(asn1Object.prettyPrint())
  41. self.assertEqual(substrate, der_encoder(asn1Object))
  42. def testOpenTypes(self):
  43. openTypesMap = {
  44. univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
  45. univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
  46. univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
  47. }
  48. substrate = pem.readBase64fromText(self.pem_text)
  49. asn1Object, rest = der_decoder(substrate,
  50. asn1Spec=rfc2986.CertificationRequest(),
  51. openTypes=openTypesMap,
  52. decodeOpenTypes=True)
  53. self.assertFalse(rest)
  54. self.assertTrue(asn1Object.prettyPrint())
  55. self.assertEqual(substrate, der_encoder(asn1Object))
  56. for rdn in asn1Object['certificationRequestInfo']['subject']['rdnSequence']:
  57. for atv in rdn:
  58. if atv['type'] == rfc5280.id_at_countryName:
  59. self.assertEqual(char.PrintableString('US'), atv['value'])
  60. else:
  61. self.assertGreater(len(atv['value']['utf8String']), 2)
  62. spki_alg = asn1Object['certificationRequestInfo']['subjectPKInfo']['algorithm']
  63. self.assertEqual(univ.Null(""), spki_alg['parameters'])
  64. sig_alg = asn1Object['signatureAlgorithm']
  65. self.assertEqual(univ.Null(""), sig_alg['parameters'])
  66. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  67. if __name__ == '__main__':
  68. result = unittest.TextTestRunner(verbosity=2).run(suite)
  69. sys.exit(not result.wasSuccessful())