test_rfc2634.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 rfc5652
  14. from pyasn1_modules import rfc2634
  15. class SignedMessageTestCase(unittest.TestCase):
  16. signed_message_pem_text = """\
  17. MIIFLgYJKoZIhvcNAQcCoIIFHzCCBRsCAQExDTALBglghkgBZQMEAgIwUQYJKoZI
  18. hvcNAQcBoEQEQkNvbnRlbnQtVHlwZTogdGV4dC9wbGFpbg0KDQpXYXRzb24sIGNv
  19. bWUgaGVyZSAtIEkgd2FudCB0byBzZWUgeW91LqCCAnwwggJ4MIIB/qADAgECAgkA
  20. pbNUKBuwbjswCgYIKoZIzj0EAwMwPzELMAkGA1UEBhMCVVMxCzAJBgNVBAgMAlZB
  21. MRAwDgYDVQQHDAdIZXJuZG9uMREwDwYDVQQKDAhCb2d1cyBDQTAeFw0xOTA1Mjkx
  22. NDQ1NDFaFw0yMDA1MjgxNDQ1NDFaMHAxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJW
  23. QTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMHRXhhbXBsZTEOMAwGA1UEAxMF
  24. QWxpY2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNlQGV4YW1wbGUuY29tMHYwEAYHKoZI
  25. zj0CAQYFK4EEACIDYgAE+M2fBy/sRA6V1pKFqecRTE8+LuAHtZxes1w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PffW4BQIvcwwVc/goU6OAAQEwFTATgRFhbGlj
  42. ZUBleGFtcGxlLmNvbTAKBggqhkjOPQQDAwRnMGUCMAFFVP2gYFLTbaxvV5J2ICNM
  43. Nk/K4pXbj5Zvj3dcCeC4+OUYyG3ZW5lOtKqaabEAXAIxALDg1WOouhkDfwuQdgBi
  44. mNTr0mjYeUWRe/15IsWNx+kuFcLDr71DFHvMFY5M3sdfMA==
  45. """
  46. def setUp(self):
  47. self.asn1Spec = rfc5652.ContentInfo()
  48. def testDerCodec(self):
  49. substrate = pem.readBase64fromText(self.signed_message_pem_text)
  50. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  51. self.assertFalse(rest)
  52. self.assertTrue(asn1Object.prettyPrint())
  53. self.assertEqual(substrate, der_encoder(asn1Object))
  54. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  55. sd, rest = der_decoder(
  56. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  57. self.assertFalse(rest)
  58. self.assertTrue(sd.prettyPrint())
  59. self.assertEqual(asn1Object['content'], der_encoder(sd))
  60. for sa in sd['signerInfos'][0]['signedAttrs']:
  61. sat = sa['attrType']
  62. sav0 = sa['attrValues'][0]
  63. if sat in rfc5652.cmsAttributesMap.keys():
  64. sav, rest = der_decoder(
  65. sav0, asn1Spec=rfc5652.cmsAttributesMap[sat])
  66. self.assertFalse(rest)
  67. self.assertTrue(sav.prettyPrint())
  68. self.assertEqual(sav0, der_encoder(sav))
  69. class SignedReceiptTestCase(unittest.TestCase):
  70. signed_receipt_pem_text = """\
  71. MIIE3gYJKoZIhvcNAQcCoIIEzzCCBMsCAQMxDTALBglghkgBZQMEAgEwga4GCyq
  72. GSIb3DQEJEAEBoIGeBIGbMIGYAgEBBgkqhkiG9w0BBwEEIMdPIQ9kJ1cI9Q6HkR
  73. CzbXWdD331uAUCL3MMFXP4KFOjBGYwZAIwOLV5WCbYjy5HLHE69IqXQQHVDJQzm
  74. o18WwkFrEYH3EMsvp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
  90. gYDVQQHDAdIZXJuZG9uMREwDwYDVQQKDAhCb2d1cyBDQQIJAKWzVCgbsG48MAsG
  91. CWCGSAFlAwQCAaCBrjAaBgkqhkiG9w0BCQMxDQYLKoZIhvcNAQkQAQEwHAYJKoZ
  92. IhvcNAQkFMQ8XDTE5MDUyOTE5MzU1NVowLwYJKoZIhvcNAQkEMSIEIGb9Hm2kCn
  93. M0CYNpZU4Uj7dN0AzOieIn9sDqZMcIcZrEMEEGCyqGSIb3DQEJEAIFMTIEMBZze
  94. HVja7fQ62ywyh8rtKzBP1WJooMdZ+8c6pRqfIESYIU5bQnH99OPA51QCwdOdjAK
  95. BggqhkjOPQQDAgRoMGYCMQDZiT22xgab6RFMAPvN4fhWwzx017EzttD4VaYrpbo
  96. lropBdPJ6jIXiZQgCwxbGTCwCMQClaQ9K+L5LTeuW50ZKSIbmBZQ5dxjtnK3OlS
  97. 7hYRi6U0JKZmWbbuS8vFIgX7eIkd8=
  98. """
  99. def setUp(self):
  100. self.asn1Spec = rfc5652.ContentInfo()
  101. def testDerCodec(self):
  102. substrate = pem.readBase64fromText(self.signed_receipt_pem_text)
  103. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  104. self.assertFalse(rest)
  105. self.assertTrue(asn1Object.prettyPrint())
  106. self.assertEqual(substrate, der_encoder(asn1Object))
  107. self.assertEqual(asn1Object['contentType'], rfc5652.id_signedData)
  108. sd, rest = der_decoder(asn1Object['content'], asn1Spec=rfc5652.SignedData())
  109. self.assertFalse(rest)
  110. self.assertTrue(sd.prettyPrint())
  111. self.assertEqual(asn1Object['content'], der_encoder(sd))
  112. self.assertEqual(sd['encapContentInfo']['eContentType'],
  113. rfc2634.id_ct_receipt)
  114. receipt, rest = der_decoder(sd['encapContentInfo']['eContent'],
  115. asn1Spec=rfc2634.Receipt())
  116. self.assertFalse(rest)
  117. self.assertTrue(receipt.prettyPrint())
  118. self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(receipt))
  119. self.assertEqual(receipt['version'], rfc2634.ESSVersion().subtype(value='v1'))
  120. for sa in sd['signerInfos'][0]['signedAttrs']:
  121. sat = sa['attrType']
  122. sav0 = sa['attrValues'][0]
  123. if sat in rfc5652.cmsAttributesMap.keys():
  124. sav, rest = der_decoder(sav0, asn1Spec=rfc5652.cmsAttributesMap[sat])
  125. self.assertFalse(rest)
  126. self.assertTrue(sav.prettyPrint())
  127. self.assertEqual(sav0, der_encoder(sav))
  128. def testOpenTypes(self):
  129. substrate = pem.readBase64fromText(self.signed_receipt_pem_text)
  130. asn1Object, rest = der_decoder(substrate,
  131. asn1Spec=self.asn1Spec,
  132. decodeOpenTypes=True)
  133. self.assertFalse(rest)
  134. self.assertTrue(asn1Object.prettyPrint())
  135. self.assertEqual(substrate, der_encoder(asn1Object))
  136. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap.keys())
  137. self.assertEqual(asn1Object['contentType'], rfc5652.id_signedData)
  138. sd = asn1Object['content']
  139. self.assertEqual(sd['version'], rfc5652.CMSVersion().subtype(value='v3'))
  140. self.assertIn(sd['encapContentInfo']['eContentType'], rfc5652.cmsContentTypesMap)
  141. self.assertEqual(sd['encapContentInfo']['eContentType'], rfc2634.id_ct_receipt)
  142. for sa in sd['signerInfos'][0]['signedAttrs']:
  143. self.assertIn(sa['attrType'], rfc5652.cmsAttributesMap)
  144. if sa['attrType'] == rfc2634.id_aa_msgSigDigest:
  145. sa['attrValues'][0].prettyPrint()[:10] == '0x167378'
  146. # Since receipt is inside an OCTET STRING, decodeOpenTypes=True cannot
  147. # automatically decode it
  148. receipt, rest = der_decoder(sd['encapContentInfo']['eContent'],
  149. asn1Spec=rfc5652.cmsContentTypesMap[sd['encapContentInfo']['eContentType']])
  150. self.assertEqual(receipt['version'], rfc2634.ESSVersion().subtype(value='v1'))
  151. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  152. if __name__ == '__main__':
  153. result = unittest.TextTestRunner(verbosity=2).run(suite)
  154. sys.exit(not result.wasSuccessful())