test_rfc4357.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.type import univ
  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 rfc5280
  15. from pyasn1_modules import rfc4357
  16. class SignedTestCase(unittest.TestCase):
  17. signed_pem_text = """\
  18. MIIBKAYJKoZIhvcNAQcCoIIBGTCCARUCAQExDDAKBgYqhQMCAgkFADAbBgkqhkiG
  19. 9w0BBwGgDgQMc2FtcGxlIHRleHQKMYHkMIHhAgEBMIGBMG0xHzAdBgNVBAMMFkdv
  20. c3RSMzQxMC0yMDAxIGV4YW1wbGUxEjAQBgNVBAoMCUNyeXB0b1BybzELMAkGA1UE
  21. BhMCUlUxKTAnBgkqhkiG9w0BCQEWGkdvc3RSMzQxMC0yMDAxQGV4YW1wbGUuY29t
  22. AhAr9cYewhG9F8fc1GJmtC4hMAoGBiqFAwICCQUAMAoGBiqFAwICEwUABEDAw0LZ
  23. P4/+JRERiHe/icPbg0IE1iD5aCqZ9v4wO+T0yPjVtNr74caRZzQfvKZ6DRJ7/RAl
  24. xlHbjbL0jHF+7XKp
  25. """
  26. def setUp(self):
  27. self.asn1Spec = rfc5652.ContentInfo()
  28. def testDerCodec(self):
  29. substrate = pem.readBase64fromText(self.signed_pem_text)
  30. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  31. self.assertFalse(rest)
  32. self.assertTrue(asn1Object.prettyPrint())
  33. self.assertEqual(substrate, der_encoder(asn1Object))
  34. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  35. sd, rest = der_decoder(
  36. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  37. self.assertFalse(rest)
  38. self.assertTrue(sd.prettyPrint())
  39. self.assertEqual(asn1Object['content'], der_encoder(sd))
  40. encoded_null = der_encoder(univ.Null(""))
  41. si = sd['signerInfos'][0]
  42. self.assertEqual(rfc4357.id_GostR3411_94, si['digestAlgorithm']['algorithm'])
  43. self.assertEqual(encoded_null, si['digestAlgorithm']['parameters'])
  44. self.assertEqual(rfc4357.id_GostR3410_2001, si['signatureAlgorithm']['algorithm'])
  45. self.assertEqual(encoded_null, si['signatureAlgorithm']['parameters'])
  46. self.assertEqual(64, len(si['signature']))
  47. def testOpenTypes(self):
  48. substrate = pem.readBase64fromText(self.signed_pem_text)
  49. asn1Object, rest = der_decoder(
  50. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  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. si = asn1Object['content']['signerInfos'][0]
  56. self.assertEqual(rfc4357.id_GostR3411_94, si['digestAlgorithm']['algorithm'])
  57. self.assertEqual(univ.Null(""), si['digestAlgorithm']['parameters'])
  58. self.assertEqual(rfc4357.id_GostR3410_2001, si['signatureAlgorithm']['algorithm'])
  59. self.assertEqual(univ.Null(""), si['signatureAlgorithm']['parameters'])
  60. self.assertEqual(64, len(si['signature']))
  61. class KeyAgreeTestCase(unittest.TestCase):
  62. keyagree_pem_text = """\
  63. MIIBpAYJKoZIhvcNAQcDoIIBlTCCAZECAQIxggFQoYIBTAIBA6BloWMwHAYGKoUD
  64. AgITMBIGByqFAwICJAAGByqFAwICHgEDQwAEQLNVOfRngZcrpcTZhB8n+4HtCDLm
  65. mtTyAHi4/4Nk6tIdsHg8ff4DwfQG5DvMFrnF9vYZNxwXuKCqx9GhlLOlNiChCgQI
  66. L/D20YZLMoowHgYGKoUDAgJgMBQGByqFAwICDQAwCQYHKoUDAgIfATCBszCBsDCB
  67. gTBtMR8wHQYDVQQDDBZHb3N0UjM0MTAtMjAwMSBleGFtcGxlMRIwEAYDVQQKDAlD
  68. cnlwdG9Qcm8xCzAJBgNVBAYTAlJVMSkwJwYJKoZIhvcNAQkBFhpHb3N0UjM0MTAt
  69. MjAwMUBleGFtcGxlLmNvbQIQK/XGHsIRvRfH3NRiZrQuIQQqMCgEIBajHOfOTukN
  70. 8ex0aQRoHsefOu24Ox8dSn75pdnLGdXoBAST/YZ+MDgGCSqGSIb3DQEHATAdBgYq
  71. hQMCAhUwEwQItzXhegc1oh0GByqFAwICHwGADDmxivS/qeJlJbZVyQ==
  72. """
  73. def setUp(self):
  74. self.asn1Spec = rfc5652.ContentInfo()
  75. def testDerCodec(self):
  76. substrate = pem.readBase64fromText(self.keyagree_pem_text)
  77. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  78. self.assertFalse(rest)
  79. self.assertTrue(asn1Object.prettyPrint())
  80. self.assertEqual(substrate, der_encoder(asn1Object))
  81. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  82. ed, rest = der_decoder(
  83. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  84. self.assertFalse(rest)
  85. self.assertTrue(ed.prettyPrint())
  86. self.assertEqual(asn1Object['content'], der_encoder(ed))
  87. ri = ed['recipientInfos'][0]
  88. alg1 = ri['kari']['originator']['originatorKey']['algorithm']
  89. self.assertEqual(rfc4357.id_GostR3410_2001, alg1['algorithm'])
  90. param1, rest = der_decoder(
  91. alg1['parameters'],
  92. asn1Spec=rfc4357.GostR3410_2001_PublicKeyParameters())
  93. self.assertFalse(rest)
  94. self.assertTrue(param1.prettyPrint())
  95. self.assertEqual(alg1['parameters'], der_encoder(param1))
  96. self.assertEqual(rfc4357.id_GostR3410_2001_CryptoPro_XchA_ParamSet, param1['publicKeyParamSet'])
  97. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, param1['digestParamSet'])
  98. self.assertEqual(8, len(ri['kari']['ukm']))
  99. alg2 = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  100. self.assertEqual(rfc4357.id_Gost28147_89, alg2['algorithm'], )
  101. param2, rest = der_decoder(
  102. alg2['parameters'],
  103. asn1Spec=rfc4357.Gost28147_89_Parameters())
  104. self.assertFalse(rest)
  105. self.assertTrue(param1.prettyPrint())
  106. self.assertEqual(alg2['parameters'], der_encoder(param2))
  107. self.assertEqual(8, len(param2['iv']))
  108. self.assertEqual(rfc4357.id_Gost28147_89_CryptoPro_A_ParamSet, param2['encryptionParamSet'])
  109. def testOpenTypes(self):
  110. openTypeMap = {
  111. rfc4357.id_GostR3410_2001: rfc4357.GostR3410_2001_PublicKeyParameters(),
  112. rfc4357.id_Gost28147_89: rfc4357.Gost28147_89_Parameters(),
  113. }
  114. substrate = pem.readBase64fromText(self.keyagree_pem_text)
  115. asn1Object, rest = der_decoder(
  116. substrate, asn1Spec=self.asn1Spec,
  117. openTypes=openTypeMap, decodeOpenTypes=True)
  118. self.assertFalse(rest)
  119. self.assertTrue(asn1Object.prettyPrint())
  120. self.assertEqual(substrate, der_encoder(asn1Object))
  121. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  122. ri = asn1Object['content']['recipientInfos'][0]
  123. alg1 = ri['kari']['originator']['originatorKey']['algorithm']
  124. self.assertEqual(rfc4357.id_GostR3410_2001, alg1['algorithm'])
  125. param1 = alg1['parameters']
  126. self.assertEqual(rfc4357.id_GostR3410_2001_CryptoPro_XchA_ParamSet, param1['publicKeyParamSet'])
  127. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, param1['digestParamSet'])
  128. self.assertEqual(8, len(ri['kari']['ukm']))
  129. alg2 = asn1Object['content']['encryptedContentInfo']['contentEncryptionAlgorithm']
  130. self.assertEqual(rfc4357.id_Gost28147_89, alg2['algorithm'])
  131. param2 = alg2['parameters']
  132. self.assertEqual(8, len(param2['iv']))
  133. self.assertEqual(rfc4357.id_Gost28147_89_CryptoPro_A_ParamSet, param2['encryptionParamSet'])
  134. class KeyTransportTestCase(unittest.TestCase):
  135. keytrans_pem_text = """\
  136. MIIBpwYJKoZIhvcNAQcDoIIBmDCCAZQCAQAxggFTMIIBTwIBADCBgTBtMR8wHQYD
  137. VQQDDBZHb3N0UjM0MTAtMjAwMSBleGFtcGxlMRIwEAYDVQQKDAlDcnlwdG9Qcm8x
  138. CzAJBgNVBAYTAlJVMSkwJwYJKoZIhvcNAQkBFhpHb3N0UjM0MTAtMjAwMUBleGFt
  139. cGxlLmNvbQIQK/XGHsIRvRfH3NRiZrQuITAcBgYqhQMCAhMwEgYHKoUDAgIkAAYH
  140. KoUDAgIeAQSBpzCBpDAoBCBqL6ghBpVon5/kR6qey2EVK35BYLxdjfv1PSgbGJr5
  141. dQQENm2Yt6B4BgcqhQMCAh8BoGMwHAYGKoUDAgITMBIGByqFAwICJAAGByqFAwIC
  142. HgEDQwAEQE0rLzOQ5tyj3VUqzd/g7/sx93N+Tv+/eImKK8PNMZQESw5gSJYf28dd
  143. Em/askCKd7W96vLsNMsjn5uL3Z4SwPYECJeV4ywrrSsMMDgGCSqGSIb3DQEHATAd
  144. BgYqhQMCAhUwEwQIvBCLHwv/NCkGByqFAwICHwGADKqOch3uT7Mu4w+hNw==
  145. """
  146. def setUp(self):
  147. self.asn1Spec = rfc5652.ContentInfo()
  148. def testDerCodec(self):
  149. substrate = pem.readBase64fromText(self.keytrans_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. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  155. ed, rest = der_decoder(
  156. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  157. self.assertFalse(rest)
  158. self.assertTrue(ed.prettyPrint())
  159. self.assertEqual(asn1Object['content'], der_encoder(ed))
  160. ri = ed['recipientInfos'][0]
  161. alg1 = ri['ktri']['keyEncryptionAlgorithm']
  162. self.assertEqual(rfc4357.id_GostR3410_2001, alg1['algorithm'])
  163. param1, rest = der_decoder(
  164. alg1['parameters'], asn1Spec=rfc4357.GostR3410_2001_PublicKeyParameters())
  165. self.assertFalse(rest)
  166. self.assertTrue(param1.prettyPrint())
  167. self.assertEqual(alg1['parameters'], der_encoder(param1))
  168. self.assertEqual(rfc4357.id_GostR3410_2001_CryptoPro_XchA_ParamSet, param1['publicKeyParamSet'])
  169. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, param1['digestParamSet'])
  170. alg2 = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  171. self.assertEqual(rfc4357.id_Gost28147_89, alg2['algorithm'])
  172. param2, rest = der_decoder(
  173. alg2['parameters'], asn1Spec=rfc4357.Gost28147_89_Parameters())
  174. self.assertFalse(rest)
  175. self.assertTrue(param2.prettyPrint())
  176. self.assertEqual(alg2['parameters'], der_encoder(param2))
  177. self.assertEqual(8, len(param2['iv']))
  178. self.assertEqual(rfc4357.id_Gost28147_89_CryptoPro_A_ParamSet, param2['encryptionParamSet'])
  179. def testOpenTypes(self):
  180. openTypeMap = {
  181. rfc4357.id_GostR3410_2001: rfc4357.GostR3410_2001_PublicKeyParameters(),
  182. rfc4357.id_Gost28147_89: rfc4357.Gost28147_89_Parameters(),
  183. }
  184. substrate = pem.readBase64fromText(self.keytrans_pem_text)
  185. asn1Object, rest = der_decoder(
  186. substrate, asn1Spec=self.asn1Spec,
  187. openTypes=openTypeMap, decodeOpenTypes=True)
  188. self.assertFalse(rest)
  189. self.assertTrue(asn1Object.prettyPrint())
  190. self.assertEqual(substrate, der_encoder(asn1Object))
  191. ri = asn1Object['content']['recipientInfos'][0]
  192. alg1 = ri['ktri']['keyEncryptionAlgorithm']
  193. self.assertEqual(rfc4357.id_GostR3410_2001, alg1['algorithm'])
  194. param1 = alg1['parameters']
  195. self.assertEqual(rfc4357.id_GostR3410_2001_CryptoPro_XchA_ParamSet, param1['publicKeyParamSet'])
  196. self.assertEqual(rfc4357.id_GostR3411_94_CryptoProParamSet, param1['digestParamSet'])
  197. alg2 = asn1Object['content']['encryptedContentInfo']['contentEncryptionAlgorithm']
  198. self.assertEqual(rfc4357.id_Gost28147_89, alg2['algorithm'])
  199. param2 = alg2['parameters']
  200. self.assertEqual(8, len(param2['iv']))
  201. self.assertEqual(rfc4357.id_Gost28147_89_CryptoPro_A_ParamSet, param2['encryptionParamSet'])
  202. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  203. if __name__ == '__main__':
  204. result = unittest.TextTestRunner(verbosity=2).run(suite)
  205. sys.exit(not result.wasSuccessful())