test_rfc5280.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 univ
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc5280
  14. class CertificateTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIC5zCCAlACAQEwDQYJKoZIhvcNAQEFBQAwgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0
  17. IFZhbGlkYXRpb24gTmV0d29yazEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAz
  18. BgNVBAsTLFZhbGlDZXJ0IENsYXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9y
  19. aXR5MSEwHwYDVQQDExhodHRwOi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG
  20. 9w0BCQEWEWluZm9AdmFsaWNlcnQuY29tMB4XDTk5MDYyNjAwMjIzM1oXDTE5MDYy
  21. NjAwMjIzM1owgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0IFZhbGlkYXRpb24gTmV0d29y
  22. azEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAzBgNVBAsTLFZhbGlDZXJ0IENs
  23. YXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9yaXR5MSEwHwYDVQQDExhodHRw
  24. Oi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG9w0BCQEWEWluZm9AdmFsaWNl
  25. cnQuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjmFGWHOjVsQaBalfD
  26. cnWTq8+epvzzFlLWLU2fNUSoLgRNB0mKOCn1dzfnt6td3zZxFJmP3MKS8edgkpfs
  27. 2Ejcv8ECIMYkpChMMFp2bbFc893enhBxoYjHW5tBbcqwuI4V7q0zK89HBFx1cQqY
  28. JJgpp0lZpd34t0NiYfPT4tBVPwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAFa7AliE
  29. Zwgs3x/be0kz9dNnnfS0ChCzycUs4pJqcXgn8nCDQtM+z6lU9PHYkhaM0QTLS6vJ
  30. n0WuPIqpsHEzXcjFV9+vqDWzf4mH6eglkrh/hXqu1rweN1gqZ8mRzyqBPu3GOd/A
  31. PhmcGcwTTYJBtYze4D1gCCAPRX5ron+jjBXu
  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(
  38. substrate, asn1Spec=self.asn1Spec)
  39. self.assertFalse(rest)
  40. self.assertTrue(asn1Object.prettyPrint())
  41. self.assertEqual(substrate, der_encoder(asn1Object))
  42. class CertificateListTestCase(unittest.TestCase):
  43. pem_text = """\
  44. MIIBVjCBwAIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJBVTETMBEGA1UE
  45. CBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk
  46. MRUwEwYDVQQDEwxzbm1wbGFicy5jb20xIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25t
  47. cGxhYnMuY29tFw0xMjA0MTExMzQwNTlaFw0xMjA1MTExMzQwNTlaoA4wDDAKBgNV
  48. HRQEAwIBATANBgkqhkiG9w0BAQUFAAOBgQC1D/wwnrcY/uFBHGc6SyoYss2kn+nY
  49. RTwzXmmldbNTCQ03x5vkWGGIaRJdN8QeCzbEi7gpgxgpxAx6Y5WkxkMQ1UPjNM5n
  50. DGVDOtR0dskFrrbHuNpWqWrDaBN0/ryZiWKjr9JRbrpkHgVY29I1gLooQ6IHuKHY
  51. vjnIhxTFoCb5vA==
  52. """
  53. def setUp(self):
  54. self.asn1Spec = rfc5280.CertificateList()
  55. def testDerCodec(self):
  56. substrate = pem.readBase64fromText(self.pem_text)
  57. asn1Object, rest = der_decoder(
  58. substrate, asn1Spec=self.asn1Spec)
  59. self.assertFalse(rest)
  60. self.assertTrue(asn1Object.prettyPrint())
  61. self.assertEqual(substrate, der_encoder(asn1Object))
  62. class CertificateOpenTypeTestCase(unittest.TestCase):
  63. pem_text = """\
  64. MIIC5zCCAlACAQEwDQYJKoZIhvcNAQEFBQAwgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0
  65. IFZhbGlkYXRpb24gTmV0d29yazEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAz
  66. BgNVBAsTLFZhbGlDZXJ0IENsYXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9y
  67. aXR5MSEwHwYDVQQDExhodHRwOi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG
  68. 9w0BCQEWEWluZm9AdmFsaWNlcnQuY29tMB4XDTk5MDYyNjAwMjIzM1oXDTE5MDYy
  69. NjAwMjIzM1owgbsxJDAiBgNVBAcTG1ZhbGlDZXJ0IFZhbGlkYXRpb24gTmV0d29y
  70. azEXMBUGA1UEChMOVmFsaUNlcnQsIEluYy4xNTAzBgNVBAsTLFZhbGlDZXJ0IENs
  71. YXNzIDMgUG9saWN5IFZhbGlkYXRpb24gQXV0aG9yaXR5MSEwHwYDVQQDExhodHRw
  72. Oi8vd3d3LnZhbGljZXJ0LmNvbS8xIDAeBgkqhkiG9w0BCQEWEWluZm9AdmFsaWNl
  73. cnQuY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjmFGWHOjVsQaBalfD
  74. cnWTq8+epvzzFlLWLU2fNUSoLgRNB0mKOCn1dzfnt6td3zZxFJmP3MKS8edgkpfs
  75. 2Ejcv8ECIMYkpChMMFp2bbFc893enhBxoYjHW5tBbcqwuI4V7q0zK89HBFx1cQqY
  76. JJgpp0lZpd34t0NiYfPT4tBVPwIDAQABMA0GCSqGSIb3DQEBBQUAA4GBAFa7AliE
  77. Zwgs3x/be0kz9dNnnfS0ChCzycUs4pJqcXgn8nCDQtM+z6lU9PHYkhaM0QTLS6vJ
  78. n0WuPIqpsHEzXcjFV9+vqDWzf4mH6eglkrh/hXqu1rweN1gqZ8mRzyqBPu3GOd/A
  79. PhmcGcwTTYJBtYze4D1gCCAPRX5ron+jjBXu
  80. """
  81. def setUp(self):
  82. self.asn1Spec = rfc5280.Certificate()
  83. def testDerCodec(self):
  84. substrate = pem.readBase64fromText(self.pem_text)
  85. openTypesMap = {
  86. univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
  87. univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
  88. univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
  89. }
  90. asn1Object, rest = der_decoder(
  91. substrate, asn1Spec=self.asn1Spec, openTypes=openTypesMap,
  92. decodeOpenTypes=True)
  93. self.assertFalse(rest)
  94. self.assertTrue(asn1Object.prettyPrint())
  95. self.assertEqual(substrate, der_encoder(asn1Object))
  96. sig_alg = asn1Object['tbsCertificate']['signature']
  97. self.assertEqual(univ.Null(""), sig_alg['parameters'])
  98. spki_alg = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
  99. self.assertEqual(univ.Null(""), spki_alg['parameters'])
  100. for rdn in asn1Object['tbsCertificate']['subject']['rdnSequence']:
  101. for atv in rdn:
  102. if atv['type'] == rfc5280.id_emailAddress:
  103. self.assertIn("valicert.com", atv['value'])
  104. else:
  105. atv_ps = str(atv['value']['printableString'])
  106. self.assertIn("valicert", atv_ps.lower())
  107. class CertificateListOpenTypeTestCase(unittest.TestCase):
  108. pem_text = """\
  109. MIIBVjCBwAIBATANBgkqhkiG9w0BAQUFADB+MQswCQYDVQQGEwJBVTETMBEGA1UE
  110. CBMKU29tZS1TdGF0ZTEhMB8GA1UEChMYSW50ZXJuZXQgV2lkZ2l0cyBQdHkgTHRk
  111. MRUwEwYDVQQDEwxzbm1wbGFicy5jb20xIDAeBgkqhkiG9w0BCQEWEWluZm9Ac25t
  112. cGxhYnMuY29tFw0xMjA0MTExMzQwNTlaFw0xMjA1MTExMzQwNTlaoA4wDDAKBgNV
  113. HRQEAwIBATANBgkqhkiG9w0BAQUFAAOBgQC1D/wwnrcY/uFBHGc6SyoYss2kn+nY
  114. RTwzXmmldbNTCQ03x5vkWGGIaRJdN8QeCzbEi7gpgxgpxAx6Y5WkxkMQ1UPjNM5n
  115. DGVDOtR0dskFrrbHuNpWqWrDaBN0/ryZiWKjr9JRbrpkHgVY29I1gLooQ6IHuKHY
  116. vjnIhxTFoCb5vA==
  117. """
  118. def setUp(self):
  119. self.asn1Spec = rfc5280.CertificateList()
  120. def testDerCodec(self):
  121. substrate = pem.readBase64fromText(self.pem_text)
  122. openTypesMap = {
  123. univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
  124. univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
  125. univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
  126. }
  127. asn1Object, rest = der_decoder(
  128. substrate, asn1Spec=self.asn1Spec, openTypes=openTypesMap,
  129. decodeOpenTypes=True)
  130. self.assertFalse(rest)
  131. self.assertTrue(asn1Object.prettyPrint())
  132. self.assertEqual(substrate, der_encoder(asn1Object))
  133. sig_alg = asn1Object['tbsCertList']['signature']
  134. self.assertEqual(univ.Null(""), sig_alg['parameters'])
  135. for rdn in asn1Object['tbsCertList']['issuer']['rdnSequence']:
  136. for atv in rdn:
  137. if atv['type'] == rfc5280.id_emailAddress:
  138. self.assertIn("snmplabs.com", atv['value'])
  139. elif atv['type'] == rfc5280.id_at_countryName:
  140. self.assertEqual('AU', atv['value'])
  141. else:
  142. self.assertLess(9, len(atv['value']['printableString']))
  143. crl_extn_count = 0
  144. for extn in asn1Object['tbsCertList']['crlExtensions']:
  145. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  146. ev, rest = der_decoder(
  147. extn['extnValue'],
  148. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  149. self.assertFalse(rest)
  150. self.assertTrue(ev.prettyPrint())
  151. self.assertEqual(extn['extnValue'], der_encoder(ev))
  152. crl_extn_count += 1
  153. self.assertEqual(1, crl_extn_count)
  154. def testExtensionsMap(self):
  155. substrate = pem.readBase64fromText(self.pem_text)
  156. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  157. self.assertFalse(rest)
  158. self.assertTrue(asn1Object.prettyPrint())
  159. self.assertEqual(substrate, der_encoder(asn1Object))
  160. cert_extn_count = 0
  161. for extn in asn1Object['tbsCertList']['crlExtensions']:
  162. if extn['extnID'] in rfc5280.certificateExtensionsMap.keys():
  163. extnValue, rest = der_decoder(
  164. extn['extnValue'],
  165. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  166. self.assertEqual(extn['extnValue'], der_encoder(extnValue))
  167. cert_extn_count += 1
  168. self.assertEqual(1, cert_extn_count)
  169. class ORAddressOpenTypeTestCase(unittest.TestCase):
  170. oraddress_pem_text = """\
  171. MEMwK2EEEwJHQmIKEwhHT0xEIDQwMKIHEwVVSy5BQ4MHU2FsZm9yZKYFEwNSLUQx
  172. FDASgAEBoQ0TC1N0ZXZlIEtpbGxl
  173. """
  174. def setUp(self):
  175. self.asn1Spec = rfc5280.ORAddress()
  176. def testDecodeOpenTypes(self):
  177. substrate = pem.readBase64fromText(self.oraddress_pem_text)
  178. asn1Object, rest = der_decoder(
  179. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  180. self.assertFalse(rest)
  181. self.assertTrue(asn1Object.prettyPrint())
  182. self.assertEqual(substrate, der_encoder(asn1Object))
  183. ea0 = asn1Object['extension-attributes'][0]
  184. self.assertEqual(rfc5280.common_name, ea0['extension-attribute-type'])
  185. self.assertEqual("Steve Kille", ea0['extension-attribute-value'])
  186. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  187. if __name__ == '__main__':
  188. result = unittest.TextTestRunner(verbosity=2).run(suite)
  189. sys.exit(not result.wasSuccessful())