test_rfc3709.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 rfc3709
  14. class CertificateExtnWithUrlTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIC9zCCAn2gAwIBAgIJAKWzVCgbsG46MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYT
  17. AlVTMQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9n
  18. dXMgQ0EwHhcNMTkwNTE0MTAwMjAwWhcNMjAwNTEzMTAwMjAwWjBlMQswCQYDVQQG
  19. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xGzAZBgNVBAoTElZp
  20. Z2lsIFNlY3VyaXR5IExMQzEaMBgGA1UEAxMRbWFpbC52aWdpbHNlYy5jb20wdjAQ
  21. BgcqhkjOPQIBBgUrgQQAIgNiAATwUXZUseiOaqWdrClDCMbp9YFAM87LTmFirygp
  22. zKDU9cfqSCg7zBDIphXCwMcS9zVWDoStCbcvN0jw5CljHcffzpHYX91P88SZRJ1w
  23. 4hawHjOsWxvM3AkYgZ5nfdlL7EajggEdMIIBGTALBgNVHQ8EBAMCB4AwQgYJYIZI
  24. AYb4QgENBDUWM1RoaXMgY2VydGlmaWNhdGUgY2Fubm90IGJlIHRydXN0ZWQgZm9y
  25. IGFueSBwdXJwb3NlLjAdBgNVHQ4EFgQU8jXbNATapVXyvWkDmbBi7OIVCMEwHwYD
  26. VR0jBBgwFoAU8jXbNATapVXyvWkDmbBi7OIVCMEwgYUGCCsGAQUFBwEMBHkwd6J1
  27. oHMwcTBvMG0WCWltYWdlL3BuZzAzMDEwDQYJYIZIAWUDBAIBBQAEIJtBNrMSSNo+
  28. 6Rwqwctmcy0qf68ilRuKEmlf3GLwGiIkMCsWKWh0dHA6Ly93d3cudmlnaWxzZWMu
  29. Y29tL3ZpZ2lsc2VjX2xvZ28ucG5nMAoGCCqGSM49BAMDA2gAMGUCMGhfLH4kZaCD
  30. H43A8m8mHCUpYt9unT0qYu4TCMaRuOTYEuqj3qtuwyLcfAGuXKp/oAIxAIrPY+3y
  31. Pj22pmfmQi5w21UljqoTj/+lQLkU3wfy5BdVKBwI0GfEA+YL3ctSzPNqAA==
  32. """
  33. def setUp(self):
  34. self.asn1Spec = rfc5280.Certificate()
  35. def testDerCodec(self):
  36. substrate = pem.readBase64fromText(self.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. extn_list = []
  42. for extn in asn1Object['tbsCertificate']['extensions']:
  43. extn_list.append(extn['extnID'])
  44. if extn['extnID'] == rfc3709.id_pe_logotype:
  45. s = extn['extnValue']
  46. logotype, rest = der_decoder(s, rfc3709.LogotypeExtn())
  47. self.assertFalse(rest)
  48. self.assertTrue(logotype.prettyPrint())
  49. self.assertEqual(s, der_encoder(logotype))
  50. ids = logotype['subjectLogo']['direct']['image'][0]['imageDetails']
  51. self.assertEqual( "image/png", ids['mediaType'])
  52. expected = "http://www.vigilsec.com/vigilsec_logo.png"
  53. self.assertEqual(expected, ids['logotypeURI'][0])
  54. self.assertIn(rfc3709.id_pe_logotype, extn_list)
  55. def testExtensionsMap(self):
  56. substrate = pem.readBase64fromText(self.pem_text)
  57. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  58. self.assertFalse(rest)
  59. self.assertTrue(asn1Object.prettyPrint())
  60. self.assertEqual(substrate, der_encoder(asn1Object))
  61. for extn in asn1Object['tbsCertificate']['extensions']:
  62. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  63. extnValue, rest = der_decoder(
  64. extn['extnValue'],
  65. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  66. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  67. class CertificateExtnWithDataTestCase(unittest.TestCase):
  68. pem_text = """\
  69. MIIJJDCCCAygAwIBAgIRAPIGo/5ScWbpAAAAAFwQBqkwDQYJKoZIhvcNAQELBQAw
  70. gbkxCzAJBgNVBAYTAlVTMRYwFAYDVQQKEw1FbnRydXN0LCBJbmMuMSgwJgYDVQQL
  71. Ex9TZWUgd3d3LmVudHJ1c3QubmV0L2xlZ2FsLXRlcm1zMTkwNwYDVQQLEzAoYykg
  72. MjAxOCBFbnRydXN0LCBJbmMuIC0gZm9yIGF1dGhvcml6ZWQgdXNlIG9ubHkxLTAr
  73. BgNVBAMTJEVudHJ1c3QgQ2VydGlmaWNhdGUgQXV0aG9yaXR5IC0gVk1DMTAeFw0x
  74. OTA4MzAxNDMyMzlaFw0yMDAyMjUxNTAyMzZaMIIBjTEOMAwGA1UEERMFMTAwMTcx
  75. CzAJBgNVBAYTAlVTMREwDwYDVQQIEwhOZXcgWW9yazERMA8GA1UEBxMITmV3IFlv
  76. cmsxGDAWBgNVBAkTDzI3MCBQYXJrIEF2ZW51ZTETMBEGCysGAQQBgjc8AgEDEwJV
  77. UzEZMBcGCysGAQQBgjc8AgECEwhEZWxhd2FyZTEfMB0GA1UEChMWSlBNb3JnYW4g
  78. Q2hhc2UgYW5kIENvLjEdMBsGA1UEDxMUUHJpdmF0ZSBPcmdhbml6YXRpb24xNzA1
  79. BgNVBAsTLkpQTUMgRmlyc3QgVmVyaWZpZWQgTWFyay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20DSQpj9i
  111. 4WTqPrz0fEahczAdBgNVHQ4EFgQUxAJ+yoDhzpPUzAPWKBYxg108dU0wCQYDVR0T
  112. BAIwADANBgkqhkiG9w0BAQsFAAOCAQEAnqdB/vcwxFcxAlyCK0W5HOthXUdXRg9a
  113. GwPDupqmLq2rKfyysZXonJJfr8jqO0f3l6TWTTJlXHljAwwXMtg3T3ngLyEzip5p
  114. g0zH7s5eXjmWRhOeuHt21o611bXDbUNFTF0IpbYBTgOwAz/+k3XLVehf8dW7Y0Lr
  115. VkzxJ6U82NxmqjaAnkm+H127x5/jPAr4LLD4gZfqFaHzw/ZLoS+fXFGs+dpuYE4s
  116. n+xe0msYMu8qWABiMGA+MCKl45Dp5di+c2fyXtKyQ3rKI8XXZ0nN4bXK7DZd+3E3
  117. kbpmR6cDliloU808Bi/erMkrfUHRoZ2d586lkmwkLcoDkJ/yPD+Jhw==
  118. """
  119. def setUp(self):
  120. self.asn1Spec = rfc5280.Certificate()
  121. def testDerCodec(self):
  122. substrate = pem.readBase64fromText(self.pem_text)
  123. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  124. self.assertFalse(rest)
  125. self.assertTrue(asn1Object.prettyPrint())
  126. self.assertEqual(substrate, der_encoder(asn1Object))
  127. extn_list = []
  128. for extn in asn1Object['tbsCertificate']['extensions']:
  129. extn_list.append(extn['extnID'])
  130. if extn['extnID'] == rfc3709.id_pe_logotype:
  131. s = extn['extnValue']
  132. logotype, rest = der_decoder(s, rfc3709.LogotypeExtn())
  133. self.assertFalse(rest)
  134. self.assertTrue(logotype.prettyPrint())
  135. self.assertEqual(s, der_encoder(logotype))
  136. ids = logotype['subjectLogo']['direct']['image'][0]['imageDetails']
  137. self.assertEqual("image/svg+xml", ids['mediaType'])
  138. self.assertEqual(
  139. "data:image/svg+xml;base64", ids['logotypeURI'][0][0:25])
  140. self.assertIn(rfc3709.id_pe_logotype, extn_list)
  141. def testExtensionsMap(self):
  142. substrate = pem.readBase64fromText(self.pem_text)
  143. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  144. self.assertFalse(rest)
  145. self.assertTrue(asn1Object.prettyPrint())
  146. self.assertEqual(substrate, der_encoder(asn1Object))
  147. for extn in asn1Object['tbsCertificate']['extensions']:
  148. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  149. extnValue, rest = der_decoder(
  150. extn['extnValue'],
  151. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  152. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  153. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  154. if __name__ == '__main__':
  155. result = unittest.TextTestRunner(verbosity=2).run(suite)
  156. sys.exit(not result.wasSuccessful())