test_rfc8769.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2020, 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 rfc5652
  13. from pyasn1_modules import rfc8769
  14. class CBORContentTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIEHwYJKoZIhvcNAQcCoIIEEDCCBAwCAQMxDTALBglghkgBZQMEAgIwIQYLKoZIhvcNAQkQ
  17. ASygEgQQgw9kUnVzc/tADzMzMzMzM6CCAnwwggJ4MIIB/qADAgECAgkApbNUKBuwbjswCgYI
  18. KoZIzj0EAwMwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQHDAdIZXJuZG9u
  19. MREwDwYDVQQKDAhCb2d1cyBDQTAeFw0xOTA1MjkxNDQ1NDFaFw0yMDA1MjgxNDQ1NDFaMHAx
  20. CzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMH
  21. RXhhbXBsZTEOMAwGA1UEAxMFQWxpY2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNlQGV4YW1wbGUu
  22. Y29tMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+M2fBy/sRA6V1pKFqecRTE8+LuAHtZxes1wm
  23. JZrBBg+bz7uYZfYQxI3dVB0YCSD6Mt3yXFlnmfBRwoqyArbjIBYrDbHBv2k8Csg2DhQ7qs/w
  24. to8hMKoFgkcscqIbiV7Zo4GUMIGRMAsGA1UdDwQEAwIHgDBCBglghkgBhvhCAQ0ENRYzVGhp
  25. cyBjZXJ0aWZpY2F0ZSBjYW5ub3QgYmUgdHJ1c3RlZCBmb3IgYW55IHB1cnBvc2UuMB0GA1Ud
  26. DgQWBBTEuloOPnrjPIGw9AKqaLsW4JYONTAfBgNVHSMEGDAWgBTyNds0BNqlVfK9aQOZsGLs
  27. 4hUIwTAKBggqhkjOPQQDAwNoADBlAjBjuR/RNbgL3kRhmn+PJTeKaL9sh/oQgHOYTgLmSnv3
  28. +NDCkhfKuMNoo/tHrkmihYgCMQC94MaerDIrQpi0IDh+v0QSAv9rMife8tClafXWtDwwL8MS
  29. 7oAh0ymT446Uizxx3PUxggFTMIIBTwIBATBMMD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIDAJW
  30. QTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9ndXMgQ0ECCQCls1QoG7BuOzALBglg
  31. hkgBZQMEAgKgezAaBgkqhkiG9w0BCQMxDQYLKoZIhvcNAQkQASwwHAYJKoZIhvcNAQkFMQ8X
  32. DTIwMDExNDIyMjIxNVowPwYJKoZIhvcNAQkEMTIEMADSWdHn4vsesm9XnjJq1WxkoV6EtD+f
  33. qDAs1JEpZMZ+n8AtUxvC5SFobYpGCl+fsDAKBggqhkjOPQQDAwRmMGQCMGclPwvZLwVJqgON
  34. mOfnxSF8Cqn3AC+ZFBg7VplspiuhKPNIyu3IofqZjCxw0TzSpAIwEK0JxNlY28KDb5te0iN6
  35. I2hw+am26W+PRyltVVGUAISHM2kA4tG39HcxEQi+6HJx
  36. """
  37. def testDerCodec(self):
  38. substrate = pem.readBase64fromText(self.pem_text)
  39. layers = { }
  40. layers.update(rfc5652.cmsContentTypesMap)
  41. getNextLayer = {
  42. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  43. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  44. }
  45. getNextSubstrate = {
  46. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  47. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  48. }
  49. next_layer = rfc5652.id_ct_contentInfo
  50. while next_layer in layers:
  51. asn1Object, rest = der_decoder(
  52. substrate, asn1Spec=layers[next_layer])
  53. self.assertFalse(rest)
  54. self.assertTrue(asn1Object.prettyPrint())
  55. self.assertEqual(substrate, der_encoder(asn1Object))
  56. substrate = getNextSubstrate[next_layer](asn1Object)
  57. next_layer = getNextLayer[next_layer](asn1Object)
  58. self.assertEqual(rfc8769.id_ct_cbor, next_layer)
  59. class CBORSequenceContentTestCase(unittest.TestCase):
  60. pem_text = """\
  61. MIIEKQYJKoZIhvcNAQcCoIIEGjCCBBYCAQMxDTALBglghkgBZQMEAgIwKgYLKoZIhvcNAQkQ
  62. AS2gGwQZgw9kUnVzc/tADzMzMzMzM6MDCSD1YWFhYqCCAnwwggJ4MIIB/qADAgECAgkApbNU
  63. KBuwbjswCgYIKoZIzj0EAwMwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZBMRAwDgYDVQQH
  64. DAdIZXJuZG9uMREwDwYDVQQKDAhCb2d1cyBDQTAeFw0xOTA1MjkxNDQ1NDFaFw0yMDA1Mjgx
  65. NDQ1NDFaMHAxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQ
  66. MA4GA1UEChMHRXhhbXBsZTEOMAwGA1UEAxMFQWxpY2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNl
  67. QGV4YW1wbGUuY29tMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE+M2fBy/sRA6V1pKFqecRTE8+
  68. LuAHtZxes1wmJZrBBg+bz7uYZfYQxI3dVB0YCSD6Mt3yXFlnmfBRwoqyArbjIBYrDbHBv2k8
  69. Csg2DhQ7qs/wto8hMKoFgkcscqIbiV7Zo4GUMIGRMAsGA1UdDwQEAwIHgDBCBglghkgBhvhC
  70. AQ0ENRYzVGhpcyBjZXJ0aWZpY2F0ZSBjYW5ub3QgYmUgdHJ1c3RlZCBmb3IgYW55IHB1cnBv
  71. c2UuMB0GA1UdDgQWBBTEuloOPnrjPIGw9AKqaLsW4JYONTAfBgNVHSMEGDAWgBTyNds0BNql
  72. VfK9aQOZsGLs4hUIwTAKBggqhkjOPQQDAwNoADBlAjBjuR/RNbgL3kRhmn+PJTeKaL9sh/oQ
  73. gHOYTgLmSnv3+NDCkhfKuMNoo/tHrkmihYgCMQC94MaerDIrQpi0IDh+v0QSAv9rMife8tCl
  74. afXWtDwwL8MS7oAh0ymT446Uizxx3PUxggFUMIIBUAIBATBMMD8xCzAJBgNVBAYTAlVTMQsw
  75. CQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9ndXMgQ0ECCQCls1Qo
  76. G7BuOzALBglghkgBZQMEAgKgezAaBgkqhkiG9w0BCQMxDQYLKoZIhvcNAQkQAS0wHAYJKoZI
  77. hvcNAQkFMQ8XDTIwMDExNDIyMjIxNVowPwYJKoZIhvcNAQkEMTIEMOsEu3dGU5j6fKZbsZPL
  78. LDA8QWxpP36CPDZWr3BVJ3R5mMCKCSmoWtVRnB7XASQcjTAKBggqhkjOPQQDAwRnMGUCMBLW
  79. PyYw4c11nrH97KHnEmx3BSDX/SfepFNM6PoPR5HCI+OR/v/wlIIByuhyrIl8xAIxAK8dEwOe
  80. I06um+ATKQzUcbgq0PCKA7T31pAq46fsWc5tA+mMARTrxZjSXsDneeAWpw==
  81. """
  82. def testDerCodec(self):
  83. substrate = pem.readBase64fromText(self.pem_text)
  84. layers = { }
  85. layers.update(rfc5652.cmsContentTypesMap)
  86. getNextLayer = {
  87. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  88. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  89. }
  90. getNextSubstrate = {
  91. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  92. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  93. }
  94. next_layer = rfc5652.id_ct_contentInfo
  95. while next_layer in layers:
  96. asn1Object, rest = der_decoder(
  97. substrate, asn1Spec=layers[next_layer])
  98. self.assertFalse(rest)
  99. self.assertTrue(asn1Object.prettyPrint())
  100. self.assertEqual(substrate, der_encoder(asn1Object))
  101. substrate = getNextSubstrate[next_layer](asn1Object)
  102. next_layer = getNextLayer[next_layer](asn1Object)
  103. self.assertEqual(rfc8769.id_ct_cborSequence, next_layer)
  104. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  105. if __name__ == '__main__':
  106. import sys
  107. result = unittest.TextTestRunner(verbosity=2).run(suite)
  108. sys.exit(not result.wasSuccessful())