test_rfc7773.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Created by Russ Housley
  5. # Copyright (c) 2019, Vigil Security, LLC
  6. # License: http://snmplabs.com/pyasn1/license.html
  7. #
  8. import sys
  9. import unittest
  10. from pyasn1.codec.der.decoder import decode as der_decoder
  11. from pyasn1.codec.der.encoder import encode as der_encoder
  12. from pyasn1_modules import pem
  13. from pyasn1_modules import rfc5280
  14. from pyasn1_modules import rfc7773
  15. class AuthenticationContextExtnTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MIIMUjCCCzqgAwIBAgIQevDaX+wRYAlpUgjTYjCCRjANBgkqhkiG9w0BAQsFADCBuDELMAkGA1UE
  18. BhMCU0UxNTAzBgNVBAoTLERldiBURVNUIENBIG9yZyBBQiAoTk9UIEEgUkVBTCBPUkdBTklaQVRJ
  19. T04pMSAwHgYDVQQLExdDZW50cmFsIFNpZ25pbmcgU2VydmljZTEVMBMGA1UEBRMMQTEyMzQ1Ni03
  20. ODkwMTkwNwYDVQQDEzBDZW50cmFsIFNpZ25pbmcgQ0EwMDEgLSBFSUQgMi4wIERldiBURVNUIFNl
  21. cnZpY2UwHhcNMTkxMDA5MDc0ODI2WhcNMjAxMDA5MDc0ODI2WjBgMRUwEwYDVQQFEwwxODg4MDMw
  22. OTkzNjgxCzAJBgNVBAYTAlNFMQ0wCwYDVQQqEwRBZ2RhMRcwFQYDVQQDEw5BZ2RhIEFuZGVyc3Nv
  23. bjESMBAGA1UEBBMJQW5kZXJzc29uMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjl1H
  24. 7vveI/EUaF9z6EiL/AmTHDbpLAKoWh9JJjpRlb8lU0TseYOzZp6ySiAO8St2a/HxxhrNuAAELUwZ
  25. 3oICkmxM/NeYgI7EEaLVPUwBAWfGZrRWb/+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lcnNzb248L3Nh
  65. bWw6QXR0cmlidXRlVmFsdWU+PC9zYW1sOkF0dHJpYnV0ZT48L3NhY2k6QXR0cmlidXRlTWFwcGlu
  66. Zz48L3NhY2k6SWRBdHRyaWJ1dGVzPjwvc2FjaTpTQU1MQXV0aENvbnRleHQ+MAkGA1UdEwQCMAAw
  67. HwYDVR0jBBgwFoAUqKv0QPwAYcLfcD/Vy1A2deHtiqcwDQYJKoZIhvcNAQELBQADggEBAETlZOIL
  68. NknxlMiYHCxoYypyzYuza2l3M4+YWakT0vFPgXpCk+l0dNst7h9nWvKKHCboSj+YP5dUCSsuUXhb
  69. 7xTei/F2nj7q1oCPuVJGThZqhWgF/JkqOy34hHEM5VniJiQu2W9TjzRMSOSFzRlQsHcOuXzdTkhr
  70. CQpD1TWxYL9sCy4YoCdE4edfgBGBMujxoijl3/xJ5uI1FjhlSPVP88p8Wsi8i7GdMYuxqjZMwrt2
  71. PHIPgop3BNN9/BzW0cmdyNvFgcD9qR8Rv5aFBYuQbyg6fST8JdAOrbMrCST6v2U41OOXH5MC/kL6
  72. tAGXsYdcuQpglUngmo/FV4Z9qjIDkYQ=
  73. """
  74. def setUp(self):
  75. self.asn1Spec = rfc5280.Certificate()
  76. def testDerCodec(self):
  77. substrate = pem.readBase64fromText(self.pem_text)
  78. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  79. self.assertFalse(rest)
  80. self.assertTrue(asn1Object.prettyPrint())
  81. self.assertEqual(substrate, der_encoder(asn1Object))
  82. extn_list = []
  83. for extn in asn1Object['tbsCertificate']['extensions']:
  84. extn_list.append(extn['extnID'])
  85. if extn['extnID'] == rfc7773.id_ce_authContext:
  86. s = extn['extnValue']
  87. acs, rest = der_decoder(
  88. s, asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  89. self.assertFalse(rest)
  90. self.assertTrue(acs.prettyPrint())
  91. self.assertEqual(s, der_encoder(acs))
  92. self.assertIn('id.elegnamnden.se', acs[0]['contextType'])
  93. self.assertIn(
  94. 'AuthContextInfo IdentityProvider', acs[0]['contextInfo'])
  95. self.assertIn(rfc7773.id_ce_authContext, extn_list)
  96. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  97. if __name__ == '__main__':
  98. result = unittest.TextTestRunner(verbosity=2).run(suite)
  99. sys.exit(not result.wasSuccessful())