test_rfc3657.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 rfc3657
  14. from pyasn1_modules import rfc5652
  15. from pyasn1_modules import rfc5751
  16. class EnvelopedDataTestCase(unittest.TestCase):
  17. env_data_pem_text = """\
  18. MIIFfwYJKoZIhvcNAQcDoIIFcDCCBWwCAQIxU6JRAgEEMCMEECBlcTFnxBsPlsug
  19. 4KOCj78YDzIwMTkwOTEyMTIwMDAwWjANBgsqgwiMmks9AQEDAgQYS3mK9jQmvth1
  20. iuBV8PEa89ICvmoomJCvMIIFEAYJKoZIhvcNAQcBMB8GCyqDCIyaSz0BAQECBBBC
  21. T0dVU0lWX0JPR1VTSVYhgIIE4HPHsXoYyQ/4LRDiK4OrSuRJmmuDye5fH/hLcgw/
  22. 330Gsl1QBs9jF1CEDBM5ki657K/TRMl78Rqb3LIu5lfLQ8WVNGLsoQPwvxzIexGg
  23. ShtYYwu8TcPiESFMa20SWpDEG8zFlmCbqQuc0buPxnvYviVThoBEthNC+S2Umed8
  24. JpxwNKJbNTx5dxd2dkDNwpHsKgNzT9cGl0NF129Dspehqtdge5LJu3rj1gNynLRI
  25. 32AQ+pwU+sEHee6wDHhU5OWnHlndkm/9MTKY3woOhs1/KQFlRFPC6k71ZpUlncd3
  26. 93wLVIImfoMe4zWPAOnbpZ/M7zEJ95rTwwmudBs0qwMfCa3h0Vkg69w6fBHyc1IH
  27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rhznetYUU1ZWM8Lyb3zMxxinSFs
  43. GFGx2TiqPyixJNxN+lPT5D6GRhC9mXgh+BfVod5oINJJwXxJpT5xnsZgW8ujVxiu
  44. 1Vt5esXCZaXTGlyjVTH5dmCvJP9+B8n7dOimmCxCbMQKpNaZixJhoXWQtTgKqL1Q
  45. f9WoEs6TDGgfTllqjbE4w3O7ZA7fAWe9jbAGwiPV5rF/NVvjaj2+ibtXbSNPW59d
  46. dy1/2WzknVYnEHF0qZdBZ02Wh4ByXUC3FNvDu8hRTm5aq73DCqXLXUwNU8BvS1xB
  47. bbRq5aYI2Rd3naNAns9dHqSvkg==
  48. """
  49. def setUp(self):
  50. self.asn1Spec = rfc5652.ContentInfo()
  51. def testDerCodec(self):
  52. substrate = pem.readBase64fromText(self.env_data_pem_text)
  53. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  54. self.assertFalse(rest)
  55. self.assertTrue(asn1Object.prettyPrint())
  56. self.assertEqual(substrate, der_encoder(asn1Object))
  57. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  58. ed, rest = der_decoder(
  59. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  60. self.assertFalse(rest)
  61. self.assertTrue(ed.prettyPrint())
  62. self.assertEqual(asn1Object['content'], der_encoder(ed))
  63. kwa = ed['recipientInfos'][0]['kekri']['keyEncryptionAlgorithm']
  64. self.assertEqual(rfc3657.id_camellia128_wrap, kwa['algorithm'])
  65. cea = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  66. self.assertEqual(rfc3657.id_camellia128_cbc, cea['algorithm'])
  67. param, rest = der_decoder(
  68. cea['parameters'], asn1Spec=rfc3657.Camellia_IV())
  69. self.assertFalse(rest)
  70. self.assertTrue(param.prettyPrint())
  71. self.assertEqual(cea['parameters'], der_encoder(param))
  72. iv = rfc3657.Camellia_IV(hexValue='424f47555349565f424f475553495621')
  73. self.assertEqual(iv, param)
  74. def testOpenTypes(self):
  75. substrate = pem.readBase64fromText(self.env_data_pem_text)
  76. asn1Object, rest = der_decoder(
  77. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  78. self.assertFalse(rest)
  79. self.assertTrue(asn1Object.prettyPrint())
  80. self.assertEqual(substrate, der_encoder(asn1Object))
  81. self.assertTrue(asn1Object['contentType'] in rfc5652.cmsContentTypesMap.keys())
  82. kekri = asn1Object['content']['recipientInfos'][0]['kekri']
  83. kwa = kekri['keyEncryptionAlgorithm']
  84. self.assertEqual(rfc3657.id_camellia128_wrap, kwa['algorithm'])
  85. eci = asn1Object['content']['encryptedContentInfo']
  86. cea = eci['contentEncryptionAlgorithm']
  87. self.assertEqual(rfc3657.id_camellia128_cbc, cea['algorithm'])
  88. iv = rfc3657.Camellia_IV(hexValue='424f47555349565f424f475553495621')
  89. self.assertEqual(iv, cea['parameters'])
  90. class SMIMECapabilitiesTestCase(unittest.TestCase):
  91. smime_capabilities_pem_text = """\
  92. MGYwDwYLKoMIjJpLPQEBAQIFADAPBgsqgwiMmks9AQEBAwUAMA8GCyqDCIyaSz0B
  93. AQEEBQAwDwYLKoMIjJpLPQEBAwIFADAPBgsqgwiMmks9AQEDAwUAMA8GCyqDCIya
  94. Sz0BAQMEBQA=
  95. """
  96. def setUp(self):
  97. self.asn1Spec = rfc5751.SMIMECapabilities()
  98. def testDerCodec(self):
  99. alg_oid_list = [
  100. rfc3657.id_camellia128_cbc,
  101. rfc3657.id_camellia192_cbc,
  102. rfc3657.id_camellia256_cbc,
  103. rfc3657.id_camellia128_wrap,
  104. rfc3657.id_camellia192_wrap,
  105. rfc3657.id_camellia256_wrap,
  106. ]
  107. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  108. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  109. self.assertFalse(rest)
  110. self.assertTrue(asn1Object.prettyPrint())
  111. self.assertEqual(substrate, der_encoder(asn1Object))
  112. param = der_encoder(rfc3657.CamelliaSMimeCapability(""))
  113. count = 0
  114. for cap in asn1Object:
  115. self.assertEqual(cap['parameters'], param)
  116. self.assertTrue(cap['capabilityID'] in alg_oid_list)
  117. count += 1
  118. self.assertEqual(count, 6)
  119. def testOpenTypes(self):
  120. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  121. asn1Object, rest = der_decoder(
  122. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  123. self.assertFalse(rest)
  124. self.assertTrue(asn1Object.prettyPrint())
  125. self.assertEqual(substrate, der_encoder(asn1Object))
  126. param = rfc3657.CamelliaSMimeCapability("")
  127. count = 0
  128. for cap in asn1Object:
  129. self.assertTrue(cap['capabilityID'] in rfc5751.smimeCapabilityMap.keys())
  130. self.assertEqual(cap['parameters'], param)
  131. count += 1
  132. self.assertEqual(count, 6)
  133. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  134. if __name__ == '__main__':
  135. result = unittest.TextTestRunner(verbosity=2).run(suite)
  136. sys.exit(not result.wasSuccessful())