test_rfc4073.py 6.4 KB

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