test_rfc5753.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 rfc3565
  14. from pyasn1_modules import rfc5480
  15. from pyasn1_modules import rfc5652
  16. from pyasn1_modules import rfc5753
  17. class EnvelopedDataTestCase(unittest.TestCase):
  18. pem_text = """\
  19. MIIGAwYJKoZIhvcNAQcDoIIF9DCCBfACAQIxgdihgdUCAQOgeKF2MBAGByqGSM49
  20. AgEGBSuBBAAiA2IABGJ8n8NE7e0+gs36C3P+klXlvBXudwiw84lyW0U0pbo9U0Lz
  21. tr6cknb+lbsRk21dXwHrK9ZW/SjBG+ONTvD+8P6+62xh2OO9lil5uSHmzDYNiTKn
  22. w8PDuC6X25uFO6Nf2qEJBAdSRkM1NzUzMBUGBiuBBAELAjALBglghkgBZQMEAS0w
  23. NDAyoBYEFMS6Wg4+euM8gbD0Aqpouxbglg41BBiH5Gdz0Rla/mjLUzxq49Lbxfpv
  24. p56UaPAwggUOBgkqhkiG9w0BBwEwHQYJYIZIAWUDBAECBBAsmDsiOo0ySncPc/RM
  25. K3FLgIIE4HPHsXoYyQ/4LRDiK4OrSuRJmmuDye5fH/hLcgw/330Gsl1QBs9jF1CE
  26. DBM5ki657K/TRMl78Rqb3LIu5lfLQ8WVNGLsoQPwvxzIexGgShtYYwu8TcPiESFM
  27. a20SWpDEG8zFlmCbqQuc0buPxnvYviVThoBEthNC+S2Umed8JpxwNKJbNTx5dxd2
  28. dkDNwpHsKgNzT9cGl0NF129Dspehq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TzPZ+y0Tp1/LupLHovMosPIGXlbvqZVh2xftDvbIigIMWZQZ2tFxYD6Xc4zA
  45. 00v7H0yGF1pRY+3GpobJkw0Y6ORtgdtdnr2ipioIeQCy0hUpPOmTeSr0L3H7KfNY
  46. 7yQgZg0ra7FIEjM8tDoNqrhznetYUU1ZWM8Lyb3zMxxinSFsGFGx2TiqPyixJNxN
  47. +lPT5D6GRhC9mXgh+BfVod5oINJJwXxJpT5xnsZgW8ujVxiu1Vt5esXCZaXTGlyj
  48. VTH5dmCvJP9+B8n7dOimmCxCbMQKpNaZixJhoXWQtTgKqL1Qf9WoEs6TDGgfTllq
  49. jbE4w3O7ZA7fAWe9jbAGwiPV5rF/NVvjaj2+ibtXbSNPW59ddy1/2WzknVYnEHF0
  50. qZdBZ02Wh4ByXUC3FNvDu8hRTm5aq73DCqXLXUwNU8BvS1xBbbRq5aYI2Rd3naNA
  51. ns9dHqSvkg==
  52. """
  53. def setUp(self):
  54. self.asn1Spec = rfc5652.ContentInfo()
  55. def testDerCodec(self):
  56. substrate = pem.readBase64fromText(self.pem_text)
  57. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  58. self.assertFalse(rest)
  59. self.assertTrue(asn1Object.prettyPrint())
  60. self.assertEqual(substrate, der_encoder(asn1Object))
  61. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  62. ed, rest = der_decoder(
  63. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  64. self.assertFalse(rest)
  65. self.assertTrue(ed.prettyPrint())
  66. self.assertEqual(asn1Object['content'], der_encoder(ed))
  67. opk_ai_p = rfc5480.ECParameters()
  68. opk_ai_p['namedCurve'] = rfc5480.secp384r1
  69. kwai = rfc5753.KeyWrapAlgorithm()
  70. kwai['algorithm'] = rfc3565.id_aes256_wrap
  71. ukm_found = False
  72. self.assertEqual(ed['version'], rfc5652.CMSVersion(value=2))
  73. for ri in ed['recipientInfos']:
  74. self.assertEqual(ri['kari']['version'], rfc5652.CMSVersion(value=3))
  75. opk_alg = ri['kari']['originator']['originatorKey']['algorithm']
  76. self.assertEqual(opk_alg['algorithm'], rfc5753.id_ecPublicKey)
  77. self.assertEqual(opk_alg['parameters'], der_encoder(opk_ai_p))
  78. kek_alg = ri['kari']['keyEncryptionAlgorithm']
  79. self.assertEqual(kek_alg['algorithm'], rfc5753.dhSinglePass_stdDH_sha384kdf_scheme)
  80. self.assertEqual(kek_alg['parameters'], der_encoder(kwai))
  81. ukm = ri['kari']['ukm']
  82. self.assertEqual(ukm, rfc5652.UserKeyingMaterial(hexValue='52464335373533'))
  83. ukm_found = True
  84. self.assertTrue(ukm_found)
  85. def testOpenTypes(self):
  86. substrate = pem.readBase64fromText(self.pem_text)
  87. asn1Object, rest = der_decoder(
  88. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  89. self.assertFalse(rest)
  90. self.assertTrue(asn1Object.prettyPrint())
  91. self.assertEqual(substrate, der_encoder(asn1Object))
  92. assert asn1Object['contentType'] == rfc5652.id_envelopedData
  93. ed = asn1Object['content']
  94. ukm_found = False
  95. self.assertEqual(ed['version'], rfc5652.CMSVersion(value=2))
  96. for ri in ed['recipientInfos']:
  97. self.assertEqual(ri['kari']['version'], rfc5652.CMSVersion(value=3))
  98. opk_alg = ri['kari']['originator']['originatorKey']['algorithm']
  99. self.assertEqual(opk_alg['algorithm'], rfc5753.id_ecPublicKey)
  100. self.assertEqual(opk_alg['parameters']['namedCurve'], rfc5480.secp384r1)
  101. kek_alg = ri['kari']['keyEncryptionAlgorithm']
  102. self.assertEqual(kek_alg['algorithm'], rfc5753.dhSinglePass_stdDH_sha384kdf_scheme)
  103. self.assertEqual(kek_alg['parameters']['algorithm'], rfc3565.id_aes256_wrap)
  104. ukm = ri['kari']['ukm']
  105. self.assertEqual(ukm, rfc5652.UserKeyingMaterial(hexValue='52464335373533'))
  106. ukm_found = True
  107. self.assertTrue(ukm_found)
  108. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  109. if __name__ == '__main__':
  110. result = unittest.TextTestRunner(verbosity=2).run(suite)
  111. sys.exit(not result.wasSuccessful())