test_rfc3739.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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.type import error
  12. from pyasn1.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc5280
  15. from pyasn1_modules import rfc3739
  16. class QCCertificateTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIIFLTCCBBWgAwIBAgIMVRaIE9MInBkG6aUaMA0GCSqGSIb3DQEBCwUAMHMxCzAJ
  19. BgNVBAYTAkJFMRkwFwYDVQQKExBHbG9iYWxTaWduIG52LXNhMRowGAYDVQQLExFG
  20. b3IgRGVtbyBVc2UgT25seTEtMCsGA1UEAxMkR2xvYmFsU2lnbiBEZW1vIElzc3Vp
  21. bmcgQ0EgLSBTdGFnaW5nMB4XDTE4MDYxNTA1MTgxNFoXDTE5MDYxNjA1MTgxNFow
  22. WjELMAkGA1UEBhMCQkUxGTAXBgNVBAMTEFRlc3QgQ2VydGlmaWNhdGUxEjAQBgNV
  23. BAUTCTEyMzQ1Njc4OTENMAsGA1UEKhMEVGVzdDENMAsGA1UEBBMEVGVzdDCCASIw
  24. DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL/tsE2EIVQhpkZU5XmFR6FAq9ou
  25. k8FWbyku5M7S2JT3c6OFMQiVgu6nfqdsl4rzojhUXQtMOnO7sUqcIedmwqRIR/jd
  26. X+ELqGGRHodZt94Tjf6Qgn2Wv/EgG0EIwsOAisGKr4qTNs6ZmVMqQ3I4+l9Ik5eM
  27. whr9JfrhSxrXDzoh8Prc9lNjQbk+YKXw0zLmVxW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cX9SXmsYuV1a3Tsxz+6/rTtxdZfXSJgaVCOWHGyXCvpAQM/4eH5hSj
  44. UfTNwEMrE4sw4k9F90Sp8Wx24sMRDTIpnEXh3ceZSzBN2OYCIO84GaiZDpSvvkYN
  45. Iwtui+Wql/HveMqbAtXkiv9GDXYZms3HBoIaCVuDaUf6
  46. """
  47. def setUp(self):
  48. self.asn1Spec = rfc5280.Certificate()
  49. def testDerCodec(self):
  50. substrate = pem.readBase64fromText(self.pem_text)
  51. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  52. self.assertFalse(rest)
  53. self.assertTrue(asn1Object.prettyPrint())
  54. self.assertEqual(substrate, der_encoder(asn1Object))
  55. count = 0
  56. for extn in asn1Object['tbsCertificate']['extensions']:
  57. if extn['extnID'] == rfc3739.id_pe_qcStatements:
  58. s = extn['extnValue']
  59. qc_stmts, rest = der_decoder(s, rfc3739.QCStatements())
  60. self.assertFalse(rest)
  61. self.assertTrue(qc_stmts.prettyPrint())
  62. self.assertEqual(s, der_encoder(qc_stmts))
  63. for qcs in qc_stmts:
  64. count += 1
  65. self.assertEqual(2, count)
  66. def testExtensionsMap(self):
  67. class SequenceOfOID(univ.SequenceOf):
  68. componentType = univ.ObjectIdentifier()
  69. openTypesMap = {
  70. univ.ObjectIdentifier('0.4.0.1862.1.6'): SequenceOfOID()
  71. }
  72. substrate = pem.readBase64fromText(self.pem_text)
  73. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  74. self.assertFalse(rest)
  75. self.assertTrue(asn1Object.prettyPrint())
  76. self.assertEqual(substrate, der_encoder(asn1Object))
  77. count = 0
  78. found_qc_stmt_oid = False
  79. for extn in asn1Object['tbsCertificate']['extensions']:
  80. if extn['extnID'] == rfc3739.id_pe_qcStatements:
  81. qc_stmts, rest = der_decoder(
  82. extn['extnValue'],
  83. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']],
  84. openTypes=openTypesMap,
  85. decodeOpenTypes=True)
  86. self.assertFalse(rest)
  87. self.assertTrue(qc_stmts.prettyPrint())
  88. self.assertEqual(extn['extnValue'], der_encoder(qc_stmts))
  89. for qcs in qc_stmts:
  90. count += 1
  91. if qcs['statementId'] in openTypesMap.keys():
  92. for oid in qcs['statementInfo']:
  93. if oid == univ.ObjectIdentifier('0.4.0.1862.1.6.1'):
  94. found_qc_stmt_oid = True
  95. self.assertEqual(2, count)
  96. self.assertTrue(found_qc_stmt_oid)
  97. class WithComponentsTestCase(unittest.TestCase):
  98. def testDerCodec(self):
  99. si = rfc3739.SemanticsInformation()
  100. self.assertRaises(error.PyAsn1Error, der_encoder, si)
  101. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  102. if __name__ == '__main__':
  103. result = unittest.TextTestRunner(verbosity=2).run(suite)
  104. sys.exit(not result.wasSuccessful())