test_rfc5275.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 rfc5275
  13. from pyasn1_modules import rfc5652
  14. from pyasn1_modules import rfc6402
  15. class GLUseKEKTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MIIMVAYJKoZIhvcNAQcCoIIMRTCCDEECAQMxDTALBglghkgBZQMEAgIwggY7Bggr
  18. BgEFBQcMAqCCBi0EggYpMIIGJTCCBhswggYXAgEBBgsqhkiG9w0BCRAIATGCBgMw
  19. ggX/MEaGLGh0dHBzOi8vd3d3LmV4YW1wbGUuY29tL2xpc3QtaW5mby9ncm91cC1s
  20. aXN0gRZncm91cC1saXN0QGV4YW1wbGUuY29tMIIFmzCCBZekQTA/MQswCQYDVQQG
  21. EwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xETAPBgNVBAoTCEJv
  22. Z3VzIENBgRxncm91cC1saXN0LW93bmVyQGV4YW1wbGUuY29tMIIFMqCCBS4wggTU
  23. oAMCAQICFCVehe2QOuzvkY+pMECid/MyYVKJMAsGCWCGSAFlAwQDAjA/MQswCQYD
  24. VQQGEwJVUzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xETAPBgNVBAoT
  25. CEJvZ3VzIENBMB4XDTE5MTAyMDE5MzE1MloXDTIxMTAxOTE5MzE1MlowPzELMAkG
  26. A1UEBhMCVVM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 2qVV8r1pA5mwYuziFQjBMAoGCCqGSM49BAMDA2cAMGQCMF2eLAXNa+8ve16CF31Y
  74. +/DDErehb5V3G5DGWZ5CGPcNcuevDeOIXcTuKqXineR3EAIwIkR+5d9UvSsAfFPk
  75. OItcoI8so2BH4Da0wkUU+o7nQ9yRtZvE0syujxIzgEzv9JUZMYIBUDCCAUwCAQEw
  76. TDA/MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExEDAOBgNVBAcMB0hlcm5kb24x
  77. ETAPBgNVBAoMCEJvZ3VzIENBAgkApbNUKBuwbkswCwYJYIZIAWUDBAICoHgwFwYJ
  78. KoZIhvcNAQkDMQoGCCsGAQUFBwwCMBwGCSqGSIb3DQEJBTEPFw0xOTEyMjIxNjA5
  79. MTRaMD8GCSqGSIb3DQEJBDEyBDADTid4Yy+UzDasyRb9j2bsz/pPHjAtNZV3oa+E
  80. RQ/auLffZXl8h43ecu6ERv4t+AswCgYIKoZIzj0EAwMEZjBkAjAt5JqjM4WJ9Yd5
  81. RnziEbhlnVoo7ADPYl8hRnxrfYG+jiNsqbAMrjqqPFiG7yOPtNwCMEcQJZT1SBud
  82. KS1zJZvX/ury+ySGvKDLkfnqwZARR9W7TkTdx0L9W9oVjyEgOeGkvA==
  83. """
  84. def testDerCodec(self):
  85. substrate = pem.readBase64fromText(self.pem_text)
  86. layers = { }
  87. layers.update(rfc5652.cmsContentTypesMap)
  88. getNextLayer = {
  89. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  90. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  91. rfc6402.id_cct_PKIData: lambda x: None
  92. }
  93. getNextSubstrate = {
  94. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  95. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  96. rfc6402.id_cct_PKIData: lambda x: None
  97. }
  98. next_layer = rfc5652.id_ct_contentInfo
  99. while next_layer:
  100. asn1Object, rest = der_decoder(
  101. substrate, asn1Spec=layers[next_layer])
  102. self.assertFalse(rest)
  103. self.assertTrue(asn1Object.prettyPrint())
  104. self.assertEqual(substrate, der_encoder(asn1Object))
  105. substrate = getNextSubstrate[next_layer](asn1Object)
  106. next_layer = getNextLayer[next_layer](asn1Object)
  107. found_gl_use_kek = False
  108. for ctrl in asn1Object['controlSequence']:
  109. if ctrl['attrType'] == rfc5275.id_skd_glUseKEK:
  110. cv, rest = der_decoder(
  111. ctrl['attrValues'][0],
  112. asn1Spec=rfc5652.cmsAttributesMap[ctrl['attrType']])
  113. self.assertFalse(rest)
  114. self.assertTrue(cv.prettyPrint())
  115. self.assertEqual(ctrl['attrValues'][0], der_encoder(cv))
  116. self.assertIn(
  117. 'example.com',
  118. cv['glInfo']['glAddress']['rfc822Name'])
  119. self.assertIn(
  120. 'example.com',
  121. cv['glOwnerInfo'][0]['glOwnerAddress']['rfc822Name'])
  122. self.assertEqual(31, cv['glKeyAttributes']['duration'])
  123. found_gl_use_kek = True
  124. self.assertTrue(found_gl_use_kek)
  125. def testOpenTypes(self):
  126. substrate = pem.readBase64fromText(self.pem_text)
  127. asn1Object, rest = der_decoder(
  128. substrate, asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)
  129. self.assertFalse(rest)
  130. self.assertTrue(asn1Object.prettyPrint())
  131. self.assertEqual(substrate, der_encoder(asn1Object))
  132. sd = asn1Object['content']
  133. self.assertEqual(
  134. rfc6402.id_cct_PKIData, sd['encapContentInfo']['eContentType'])
  135. pkid, rest = der_decoder(
  136. sd['encapContentInfo']['eContent'],
  137. asn1Spec=rfc6402.PKIData(),
  138. decodeOpenTypes=True)
  139. self.assertFalse(rest)
  140. self.assertTrue(pkid.prettyPrint())
  141. self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(pkid))
  142. found_gl_use_kek = False
  143. for ctrl in pkid['controlSequence']:
  144. if ctrl['attrType'] == rfc5275.id_skd_glUseKEK:
  145. cv = ctrl['attrValues'][0]
  146. self.assertIn(
  147. 'example.com',
  148. cv['glInfo']['glAddress']['rfc822Name'])
  149. self.assertIn(
  150. 'example.com',
  151. cv['glOwnerInfo'][0]['glOwnerAddress']['rfc822Name'])
  152. self.assertEqual(31, cv['glKeyAttributes']['duration'])
  153. found_gl_use_kek = True
  154. self.assertTrue(found_gl_use_kek)
  155. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  156. if __name__ == '__main__':
  157. import sys
  158. result = unittest.TextTestRunner(verbosity=2).run(suite)
  159. sys.exit(not result.wasSuccessful())