test_rfc4108.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 rfc5652
  13. from pyasn1_modules import rfc4108
  14. class CMSFirmwareWrapperTestCase(unittest.TestCase):
  15. pem_text = """\
  16. MIIEvAYJKoZIhvcNAQcCoIIErTCCBKkCAQExDTALBglghkgBZQMEAgEwggIVBgsq
  17. hkiG9w0BCRABEKCCAgQEggIA3ntqPr5kDpx+//pgWGfHCH/Ht4pbenGwXv80txyE
  18. Y0I2mT9BUGz8ILkbhD7Xz89pBS5KhEJpthxH8WREJtvS+wL4BqYLt23wjWoZy5Gt
  19. 5dPzWgaNlV/aQ5AdfAY9ljmnNYnK8D8r8ur7bQM4cKUdxry+QA0nqXHMAOSpx4Um
  20. 8impCc0BICXaFfL3zBrNxyPubbFO9ofbYOAWaNmmIAhzthXf12vDrLostIqmYrP4
  21. LMRCjTr4LeYaVrAWfKtbUbByN6IuBef3Qt5cJaChr74udz3JvbYFsUvCpl64kpRq
  22. g2CT6R+xE4trO/pViJlI15dvJVz04BBYQ2jQsutJwChi97/DDcjIv03VBmrwRE0k
  23. RJNFP9vpDM8CxJIqcobC5Kuv8b0GqGfGl6ouuQKEVMfBcrupgjk3oc3KL1iVdSr1
  24. +74amb1vDtTMWNm6vWRqh+Kk17NGEi2mNvYkkZUTIHNGH7OgiDclFU8dSMZd1fun
  25. /D9dmiFiErDB3Fzr4+8Qz0aKedNE/1uvM+dhu9qjuRdkDzZ4S7txTfk6y9pG9iyk
  26. aEeTV2kElKXblgi+Cf0Ut4f5he8rt6jveHdMo9X36YiUQVvevj2cgN7lFivEnFYV
  27. QY0xugpP7lvEFDfsi2+0ozgP8EKOLYaCUKpuvttlYJ+vdtUFEijizEZ4cx02RsXm
  28. EesxggJ6MIICdgIBA4AUnutnybladNRNLxY5ZoDoAbXLpJwwCwYJYIZIAWUDBAIB
  29. oIG8MBoGCSqGSIb3DQEJAzENBgsqhkiG9w0BCRABEDArBgsqhkiG9w0BCRACJDEc
  30. MBoGCysGAQQBjb9BAQEqBgsrBgEEAY2/QQEBMDAvBgkqhkiG9w0BCQQxIgQgAJfv
  31. uasB4P6WDLOkOyvj33YPgZW4olHbidzyh1EKP9YwQAYLKoZIhvcNAQkQAikxMTAv
  32. MAsGCWCGSAFlAwQCAQQgAJfvuasB4P6WDLOkOyvj33YPgZW4olHbidzyh1EKP9Yw
  33. CwYJKoZIhvcNAQELBIIBgDivAlSLbMPPu+zV+pPcYpNp+A1mwVOytjMBzSo31kR/
  34. qEu+hVrDknAOk9IdCaDvcz612CcfNT85/KzrYvWWxOP2woU/vZj253SnndALpfNN
  35. n3/crJjF6hKgkjUwoXebI7kuj5WCh2q5lkd6xUa+jkCw+CINcN43thtS66UsVI4d
  36. mv02EvsS2cxPY/508uaQZ6AYAacm667bgX8xEjbzACMOeMCuvKQXWAuh3DkNk+gV
  37. xizHDw7xZxXgMGMAnJglAeBtd3Si5ztILw9U2gKUqFn/nOgy+eW63JuU/q31/Hgg
  38. ZATjyBznSzneTZrw8/ePoSCj7E9vBeCTUkeFbVB2tJK1iYDMblp6HUuwgYuGKXy/
  39. ZwKL3GvB11qg7ntdEyjdLq0xcVrht/K0d2dPo4iO4Ac7c1xbFMDAlWOt4FMPWh6O
  40. iTh55YvT7hAJjTbB5ebgMA9QJnAczQPFnaIePnlFrkETd3YyLK4yHwnoIGo1GiW/
  41. dsnhVtIdkPtfJIvcYteYJg==
  42. """
  43. def setUp(self):
  44. self.asn1Spec = rfc5652.ContentInfo()
  45. def testDerCodec(self):
  46. substrate = pem.readBase64fromText(self.pem_text)
  47. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  48. self.assertFalse(rest)
  49. self.assertTrue(asn1Object.prettyPrint())
  50. self.assertEqual(substrate, der_encoder(asn1Object))
  51. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  52. inner, rest = der_decoder(asn1Object['content'], asn1Spec=rfc5652.SignedData())
  53. self.assertEqual(
  54. rfc4108.id_ct_firmwarePackage, inner['encapContentInfo']['eContentType'])
  55. self.assertTrue(inner['encapContentInfo']['eContent'])
  56. attribute_list = []
  57. for attr in inner['signerInfos'][0]['signedAttrs']:
  58. attribute_list.append(attr['attrType'])
  59. if attr['attrType'] == rfc4108.id_aa_targetHardwareIDs:
  60. av, rest = der_decoder(attr['attrValues'][0],
  61. asn1Spec=rfc4108.TargetHardwareIdentifiers())
  62. self.assertEqual(2, len(av))
  63. for oid in av:
  64. self.assertIn('1.3.6.1.4.1.221121.1.1.', oid.prettyPrint())
  65. self.assertIn( rfc5652.id_contentType, attribute_list)
  66. self.assertIn( rfc5652.id_messageDigest, attribute_list)
  67. self.assertIn(rfc4108.id_aa_targetHardwareIDs, attribute_list)
  68. self.assertIn(rfc4108.id_aa_fwPkgMessageDigest, attribute_list)
  69. def testOpenTypes(self):
  70. substrate = pem.readBase64fromText(self.pem_text)
  71. asn1Object, rest = der_decoder(
  72. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  73. self.assertFalse(rest)
  74. self.assertTrue(asn1Object.prettyPrint())
  75. self.assertEqual(substrate, der_encoder(asn1Object))
  76. self.assertEqual(asn1Object['contentType'], rfc5652.id_signedData)
  77. sd_eci = asn1Object['content']['encapContentInfo']
  78. self.assertEqual(sd_eci['eContentType'], rfc4108.id_ct_firmwarePackage)
  79. self.assertTrue(sd_eci['eContent'].hasValue())
  80. for attr in asn1Object['content']['signerInfos'][0]['signedAttrs']:
  81. self.assertIn(attr['attrType'], rfc5652.cmsAttributesMap)
  82. if attr['attrType'] == rfc4108.id_aa_targetHardwareIDs:
  83. for oid in attr['attrValues'][0]:
  84. self.assertIn('1.3.6.1.4.1.221121.1.1.', oid.prettyPrint())
  85. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  86. if __name__ == '__main__':
  87. result = unittest.TextTestRunner(verbosity=2).run(suite)
  88. sys.exit(not result.wasSuccessful())