test_rfc3370.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.type import univ
  13. from pyasn1_modules import pem
  14. from pyasn1_modules import rfc3370
  15. from pyasn1_modules import rfc5280
  16. from pyasn1_modules import rfc5652
  17. from pyasn1_modules import rfc5751
  18. class EnvelopedDataTestCase(unittest.TestCase):
  19. env_data_pem_text = """\
  20. MIIFjAYJKoZIhvcNAQcDoIIFfTCCBXkCAQIxZqJkAgEEMCMEEH744tkBAA6gplAQ
  21. nKYxCF8YDzIwMTkwOTEyMTIwMDAwWjAQBgsqhkiG9w0BCRADBwIBOgQocOaZ+1cB
  22. 94MzMPtx6HyFpCC9yZrwXSKvWg5I018xOJhsuq+0so1PNTCCBQoGCSqGSIb3DQEH
  23. ATAZBggqhkiG9w0DAjANAgE6BAhCT0dVU19JVoCCBOBzx7F6GMkP+C0Q4iuDq0rk
  24. SZprg8nuXx/4S3IMP999BrJdUAbPYxdQhAwTOZIuueyv00TJe/Eam9yyLuZXy0PF
  25. lTRi7KED8L8cyHsRoEobWGMLvE3D4hEhTGttElqQxBvMxZZgm6kLnNG7j8Z72L4l
  26. U4aARLYTQvktlJnnfCaccDSiWzU8eXcXdnZAzcKR7CoDc0/XBpdDRddvQ7KXoarX
  27. YHuSybt649YDcpy0SN9gEPqcFPrBB3nusAx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W
  43. 76mVYdsX7Q72yIoCDFmUGdrRcWA+l3OMwNNL+x9MhhdaUWPtxqaGyZMNGOjkbYHb
  44. XZ69oqYqCHkAstIVKTzpk3kq9C9x+ynzWO8kIGYNK2uxSBIzPLQ6Daq4c53rWFFN
  45. WVjPC8m98zMcYp0hbBhRsdk4qj8osSTcTfpT0+Q+hkYQvZl4IfgX1aHeaCDSScF8
  46. SaU+cZ7GYFvLo1cYrtVbeXrFwmWl0xpco1Ux+XZgryT/fgfJ+3ToppgsQmzECqTW
  47. mYsSYaF1kLU4Cqi9UH/VqBLOkwxoH05Zao2xOMNzu2QO3wFnvY2wBsIj1eaxfzVb
  48. 42o9vom7V20jT1ufXXctf9ls5J1WJxBxdKmXQWdNloeAcl1AtxTbw7vIUU5uWqu9
  49. wwqly11MDVPAb0tcQW20auWmCNkXd52jQJ7PXR6kr5I=
  50. """
  51. def setUp(self):
  52. self.asn1Spec = rfc5652.ContentInfo()
  53. def testDerCodec(self):
  54. substrate = pem.readBase64fromText(self.env_data_pem_text)
  55. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  56. self.assertFalse(rest)
  57. self.assertTrue(asn1Object.prettyPrint())
  58. self.assertEqual(substrate, der_encoder(asn1Object))
  59. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  60. ed, rest = der_decoder(
  61. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  62. self.assertFalse(rest)
  63. self.assertTrue(ed.prettyPrint())
  64. self.assertEqual(asn1Object['content'], der_encoder(ed))
  65. kwa = ed['recipientInfos'][0]['kekri']['keyEncryptionAlgorithm']
  66. self.assertEqual(rfc3370.id_alg_CMSRC2wrap, kwa['algorithm'])
  67. kwa_param, rest = der_decoder(
  68. kwa['parameters'], rfc3370.RC2wrapParameter())
  69. self.assertFalse(rest)
  70. self.assertTrue(kwa_param.prettyPrint())
  71. self.assertEqual(kwa['parameters'], der_encoder(kwa_param))
  72. self.assertEqual(58, kwa_param)
  73. cea = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  74. self.assertEqual(rfc3370.rc2CBC, cea['algorithm'])
  75. param, rest = der_decoder(
  76. cea['parameters'], rfc3370.RC2CBCParameter())
  77. self.assertFalse(rest)
  78. self.assertTrue(param.prettyPrint())
  79. self.assertEqual(cea['parameters'], der_encoder(param))
  80. iv = univ.OctetString(hexValue='424f4755535f4956')
  81. self.assertEqual(iv, param['iv'])
  82. self.assertEqual(58, param['rc2ParameterVersion'])
  83. def testOpenTypes(self):
  84. substrate = pem.readBase64fromText(self.env_data_pem_text)
  85. asn1Object, rest = der_decoder(
  86. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  87. self.assertFalse(rest)
  88. self.assertTrue(asn1Object.prettyPrint())
  89. self.assertEqual(substrate, der_encoder(asn1Object))
  90. self.assertTrue(asn1Object['contentType'] in rfc5652.cmsContentTypesMap.keys())
  91. ri0 = asn1Object['content']['recipientInfos'][0]
  92. kwa = ri0['kekri']['keyEncryptionAlgorithm']
  93. self.assertEqual(rfc3370.id_alg_CMSRC2wrap, kwa['algorithm'])
  94. self.assertEqual(58, kwa['parameters'])
  95. eci = asn1Object['content']['encryptedContentInfo']
  96. cea = eci['contentEncryptionAlgorithm']
  97. self.assertEqual(rfc3370.rc2CBC, cea['algorithm'])
  98. iv = univ.OctetString(hexValue='424f4755535f4956')
  99. self.assertEqual(iv, cea['parameters']['iv'])
  100. self.assertEqual(58, cea['parameters']['rc2ParameterVersion'])
  101. class DSAPublicKeyTestCase(unittest.TestCase):
  102. dsa_cert_pem_text = """\
  103. MIIDpjCCA0ygAwIBAgIUY8xt3l0B9nIPWSpjs0hDJUJZmCkwCwYJYIZIAWUDBAMC
  104. MD8xCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjER
  105. MA8GA1UEChMIQm9ndXMgQ0EwHhcNMTkxMDIwMjAxMjMwWhcNMjAxMDE5MjAxMjMw
  106. WjBwMQswCQYDVQQG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mI+
  122. pNqdXqZZGESXy1MT1aBc4ynPGLFUr2r7cPY=
  123. """
  124. def setUp(self):
  125. self.asn1Spec = rfc5280.Certificate()
  126. def testDerCodec(self):
  127. substrate = pem.readBase64fromText(self.dsa_cert_pem_text)
  128. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  129. self.assertFalse(rest)
  130. self.assertTrue(asn1Object.prettyPrint())
  131. self.assertEqual(substrate, der_encoder(asn1Object))
  132. spki = asn1Object['tbsCertificate']['subjectPublicKeyInfo']
  133. self.assertEqual(rfc3370.id_dsa, spki['algorithm']['algorithm'])
  134. pk_substrate = spki['subjectPublicKey'].asOctets()
  135. pk, rest = der_decoder(pk_substrate, asn1Spec=rfc3370.Dss_Pub_Key())
  136. self.assertFalse(rest)
  137. self.assertTrue(pk.prettyPrint())
  138. self.assertEqual(pk_substrate, der_encoder(pk))
  139. self.assertEqual(48, pk % 1024)
  140. class SMIMECapabilitiesTestCase(unittest.TestCase):
  141. smime_capabilities_pem_text = """\
  142. MGIwDAYIKwYBBQUIAQIFADAfBgsqhkiG9w0BCRADBTAQBgsqhkiG9w0BCRADBwIB
  143. OjAfBgsqhkiG9w0BCRADCjAQBgsqhkiG9w0BCRADBwIBOjAQBgsqhkiG9w0BCRAD
  144. BwIBOg==
  145. """
  146. def setUp(self):
  147. self.asn1Spec = rfc5751.SMIMECapabilities()
  148. def testDerCodec(self):
  149. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  150. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  151. self.assertFalse(rest)
  152. self.assertTrue(asn1Object.prettyPrint())
  153. self.assertEqual(substrate, der_encoder(asn1Object))
  154. found_wrap_alg_param = False
  155. for cap in asn1Object:
  156. if cap['capabilityID'] in rfc5751.smimeCapabilityMap.keys():
  157. if cap['parameters'].hasValue():
  158. param, rest = der_decoder(
  159. cap['parameters'],
  160. asn1Spec=rfc5751.smimeCapabilityMap[cap['capabilityID']])
  161. self.assertFalse(rest)
  162. self.assertTrue(param.prettyPrint())
  163. self.assertEqual(cap['parameters'], der_encoder(param))
  164. if cap['capabilityID'] == rfc3370.id_alg_ESDH:
  165. kwa, rest = der_decoder(
  166. cap['parameters'],
  167. asn1Spec=rfc5751.smimeCapabilityMap[cap['capabilityID']])
  168. self.assertFalse(rest)
  169. self.assertTrue(kwa.prettyPrint())
  170. self.assertEqual(cap['parameters'], der_encoder(kwa))
  171. self.assertTrue(kwa['algorithm'] in rfc5280.algorithmIdentifierMap.keys())
  172. self.assertEqual(rfc3370.id_alg_CMSRC2wrap, kwa['algorithm'])
  173. kwa_p, rest = der_decoder(
  174. kwa['parameters'],
  175. asn1Spec=rfc5280.algorithmIdentifierMap[kwa['algorithm']])
  176. self.assertFalse(rest)
  177. self.assertTrue(kwa_p.prettyPrint())
  178. self.assertEqual(kwa['parameters'], der_encoder(kwa_p))
  179. self.assertEqual(58, kwa_p)
  180. found_wrap_alg_param = True
  181. self.assertTrue(found_wrap_alg_param)
  182. def testOpenTypes(self):
  183. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  184. asn1Object, rest = der_decoder(
  185. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  186. self.assertFalse(rest)
  187. self.assertTrue(asn1Object.prettyPrint())
  188. self.assertEqual(substrate, der_encoder(asn1Object))
  189. found_wrap_alg_param = False
  190. for cap in asn1Object:
  191. if cap['capabilityID'] == rfc3370.id_alg_ESDH:
  192. self.assertEqual(rfc3370.id_alg_CMSRC2wrap, cap['parameters']['algorithm'])
  193. self.assertEqual(58, cap['parameters']['parameters'])
  194. found_wrap_alg_param = True
  195. self.assertTrue(found_wrap_alg_param)
  196. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  197. if __name__ == '__main__':
  198. result = unittest.TextTestRunner(verbosity=2).run(suite)
  199. sys.exit(not result.wasSuccessful())