test_rfc4073.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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.compat.octets import str2octs
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc2634
  15. from pyasn1_modules import rfc4073
  16. from pyasn1_modules import rfc5652
  17. class ContentCollectionTestCase(unittest.TestCase):
  18. pem_text = """\
  19. MIIG/QYLKoZIhvcNAQkQAROgggbsMIIG6DCCAWcGCyqGSIb3DQEJEAEUoIIBVjCC
  20. AVIwgfEGCSqGSIb3DQEHAaCB4wSB4ENvbnRlbnQtVHlwZTogdGV4dC9wbGFpbgoK
  21. UkZDIDQwNzMsIHB1Ymxpc2hlZCBpbiBNYXkgMjAwNSwgZGVzY3JpYmVzIGEgY29u
  22. dmVudGlvbiBmb3IgdXNpbmcgdGhlCkNyeXB0b2dyYXBoaWMgTWVzc2FnZSBTeW50
  23. YXggKENNUykgdG8gcHJvdGVjdCBhIGNvbnRlbnQgY29sbGVjdGlvbi4gIElmCmRl
  24. c2lyZWQsIGF0dHJpYnV0ZXMgY2FuIGJlIGFzc29jaWF0ZWQgd2l0aCB0aGUgY29u
  25. dGVudC4KMFwwMwYLKoZIhvcNAQkQAgQxJDAiDBVBYnN0cmFjdCBmb3IgUkZDIDQw
  26. NzMGCSqGSIb3DQEHATAlBgsqhkiG9w0BCRACBzEWBBSkLSXBiRWvbwnJKb4EGb1X
  27. FwCa3zCCBXkGCyqGSIb3DQEJEAEUoIIFaDCCBWQwggT9BgkqhkiG9w0BBwGg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ci1ib2R5KDIpIHVz
  44. KDg0MCkgcnNhZHNpKDExMzU0OSkgcGtjcygxKQogICAgICAgICAgIHBrY3M5KDkp
  45. IHNtaW1lKDE2KSBjdCgxKSAxOSB9CgogICBDb250ZW50Q29sbGVjdGlvbiA6Oj0g
  46. U0VRVUVOQ0UgU0laRSAoMS4uTUFYKSBPRiBDb250ZW50SW5mbwoKICAgLS0gQ29u
  47. dGVudCBXaXRoIEF0dHJpYnV0ZXMgQ29udGVudCBUeXBlIGFuZCBPYmplY3QgSWRl
  48. bnRpZmllcgoKICAgaWQtY3QtY29udGVudFdpdGhBdHRycyBPQkpFQ1QgSURFTlRJ
  49. RklFUiA6Oj0gewogICAgICAgICAgIGlzbygxKSBtZW1iZXItYm9keSgyKSB1cyg4
  50. NDApIHJzYWRzaSgxMTM1NDkpIHBrY3MoMSkKICAgICAgICAgICBwa2NzOSg5KSBz
  51. bWltZSgxNikgY3QoMSkgMjAgfQoKICAgQ29udGVudFdpdGhBdHRyaWJ1dGVzIDo6
  52. PSBTRVFVRU5DRSB7CiAgICAgICBjb250ZW50ICAgICBDb250ZW50SW5mbywKICAg
  53. ICAgIGF0dHJzICAgICAgIFNFUVVFTkNFIFNJWkUgKDEuLk1BWCkgT0YgQXR0cmli
  54. dXRlIH0KCiAgIEVORAowYTA4BgsqhkiG9w0BCRACBDEpMCcMGkFTTi4xIE1vZHVs
  55. ZSBmcm9tIFJGQyA0MDczBgkqhkiG9w0BBwEwJQYLKoZIhvcNAQkQAgcxFgQUMbeK
  56. buWO3egPDL8Kf7tBhzjIKLw=
  57. """
  58. def setUp(self):
  59. self.asn1Spec = rfc5652.ContentInfo()
  60. def testDerCodec(self):
  61. def test_layer(substrate, content_type):
  62. asn1Object, rest = der_decoder(substrate, asn1Spec=layers[content_type])
  63. self.assertFalse(rest)
  64. self.assertTrue(asn1Object.prettyPrint())
  65. self.assertEqual(substrate, der_encoder(asn1Object))
  66. if content_type == rfc4073.id_ct_contentWithAttrs:
  67. for attr in asn1Object['attrs']:
  68. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  69. return asn1Object
  70. layers = rfc5652.cmsContentTypesMap
  71. getNextLayer = {
  72. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  73. rfc4073.id_ct_contentCollection: lambda x: x[0]['contentType'],
  74. rfc4073.id_ct_contentWithAttrs: lambda x: x['content']['contentType'],
  75. rfc5652.id_data: lambda x: None,
  76. }
  77. getNextSubstrate = {
  78. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  79. rfc4073.id_ct_contentCollection: lambda x: x[0]['content'],
  80. rfc4073.id_ct_contentWithAttrs: lambda x: x['content']['content'],
  81. rfc5652.id_data: lambda x: None,
  82. }
  83. substrate = pem.readBase64fromText(self.pem_text)
  84. this_layer = rfc5652.id_ct_contentInfo
  85. while this_layer != rfc5652.id_data:
  86. if this_layer == rfc4073.id_ct_contentCollection:
  87. asn1Object = test_layer(substrate, this_layer)
  88. for ci in asn1Object:
  89. substrate = ci['content']
  90. this_layer = ci['contentType']
  91. while this_layer != rfc5652.id_data:
  92. asn1Object = test_layer(substrate, this_layer)
  93. substrate = getNextSubstrate[this_layer](asn1Object)
  94. this_layer = getNextLayer[this_layer](asn1Object)
  95. else:
  96. asn1Object = test_layer(substrate, this_layer)
  97. substrate = getNextSubstrate[this_layer](asn1Object)
  98. this_layer = getNextLayer[this_layer](asn1Object)
  99. def testOpenTypes(self):
  100. substrate = pem.readBase64fromText(self.pem_text)
  101. asn1Object, rest = der_decoder(substrate,
  102. asn1Spec=rfc5652.ContentInfo(),
  103. decodeOpenTypes=True)
  104. self.assertFalse(rest)
  105. self.assertTrue(asn1Object.prettyPrint())
  106. self.assertEqual(substrate, der_encoder(asn1Object))
  107. self.assertEqual(rfc4073.id_ct_contentCollection, asn1Object['contentType'])
  108. for ci in asn1Object['content']:
  109. self.assertIn(ci['contentType'], rfc5652.cmsContentTypesMap)
  110. self.assertEqual(rfc4073.id_ct_contentWithAttrs, ci['contentType'])
  111. next_ci = ci['content']['content']
  112. self.assertIn(next_ci['contentType'], rfc5652.cmsContentTypesMap)
  113. self.assertEqual(rfc5652.id_data, next_ci['contentType'])
  114. self.assertIn(str2octs('Content-Type: text'), next_ci['content'])
  115. for attr in ci['content']['attrs']:
  116. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  117. if attr['attrType'] == rfc2634.id_aa_contentHint:
  118. self.assertIn('RFC 4073', attr['attrValues'][0]['contentDescription'])
  119. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  120. if __name__ == '__main__':
  121. result = unittest.TextTestRunner(verbosity=2).run(suite)
  122. sys.exit(not result.wasSuccessful())