test_rfc8696.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 rfc5083
  15. from pyasn1_modules import rfc5652
  16. from pyasn1_modules import rfc8696
  17. class KeyTransPSKTestCase(unittest.TestCase):
  18. key_trans_psk_pem_text = """\
  19. MIICigYLKoZIhvcNAQkQARegggJ5MIICdQIBADGCAiekggIjBgsqhkiG9w0BCRANATCCAhIC
  20. AQAEE3B0Zi1rbWM6MTM2MTQxMjIxMTIwDQYLKoZIhvcNAQkQAx0wCwYJYIZIAWUDBAEtMIIB
  21. sDCCAawCAQKAFJ7rZ8m5WnTUTS8WOWaA6AG1y6ScMA0GCSqGSIb3DQEBAQUABIIBgKo/Hkhu
  22. eoOdn1/cIEpt38NbEEdSC586IWcG+0l+ND9pcmQvvKvscpvFFVAjqLjvoXGatmSazr2Q4BVS
  23. yWKm0JqlyVWEAhRsU7wNlD7zRAKI8+obWpU57gjEKs13D8gb1PI2YPZWajN1Ye+yHSF6h+fb
  24. 7YtaQepxTGHYF0LgHaAC8cqtgwIRW8N4Gnvl0Uuz+YEZXUX0I8fvJG6MKCEFzwHvfrfPb3rW
  25. B8k7BHfekRpY+793JNrjSP2lY+W0fhqBN8dALDKGqlbUCyojMQkQiD/iXSBRbZWiJ1CE92iT
  26. x7Ji9irq8rhYDNoDP2vghJUaepoZgIJwPWqhoTH+KRPqHTjLnnbi/TGzEdeO5h0C9Gc0DVzs
  27. 9OHvHknQ7mSxPT9xKMXGztVT+P3a9ct6TaMotpMqL9cuZxTYGpHMYNkLSUXFSadAGFrgP7QV
  28. FGwC/Z/YomEzSLPgZi8HnVHsAGkJzXxmM/PJBu4dAXcKjEv/GgpmaS2B7gKHUpTyyAgdsBsy
  29. 2AQo6glHJQ+mbNUlWV5Sppqq3ojvzxsPEIq+KRBgORsc31kH82tAZ+RTQjA3BgkqhkiG9w0B
  30. BwEwGwYJYIZIAWUDBAEuMA4EDMr+ur76ztut3sr4iIANmvLRbyFUf87+2bPvLQQMoOWSXMGE
  31. 4BckY8RM
  32. """
  33. def setUp(self):
  34. self.asn1Spec = rfc5652.ContentInfo()
  35. def testDerCodec(self):
  36. substrate = pem.readBase64fromText(self.key_trans_psk_pem_text)
  37. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  38. self.assertFalse(rest)
  39. self.assertTrue(asn1Object.prettyPrint())
  40. self.assertEqual(substrate, der_encoder(asn1Object))
  41. self.assertEqual(
  42. rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  43. aed, rest = der_decoder(
  44. asn1Object['content'],
  45. asn1Spec=rfc5083.AuthEnvelopedData())
  46. self.assertFalse(rest)
  47. self.assertTrue(aed.prettyPrint())
  48. self.assertEqual(asn1Object['content'], der_encoder(aed))
  49. self.assertEqual(0, aed['version'])
  50. ri = aed['recipientInfos'][0]
  51. self.assertEqual(rfc8696.id_ori_keyTransPSK, ri['ori']['oriType'])
  52. ktpsk, rest = der_decoder(
  53. ri['ori']['oriValue'],
  54. asn1Spec=rfc8696.KeyTransPSKRecipientInfo())
  55. self.assertFalse(rest)
  56. self.assertTrue(ktpsk.prettyPrint())
  57. self.assertEqual(ri['ori']['oriValue'], der_encoder(ktpsk))
  58. self.assertEqual(0, ktpsk['version'])
  59. ktri = ktpsk['ktris'][0]
  60. self.assertEqual(2, ktri['version'])
  61. def testOtherRecipientInfoMap(self):
  62. substrate = pem.readBase64fromText(self.key_trans_psk_pem_text)
  63. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  64. self.assertFalse(rest)
  65. self.assertTrue(asn1Object.prettyPrint())
  66. self.assertEqual(substrate, der_encoder(asn1Object))
  67. self.assertEqual(
  68. rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  69. aed, rest = der_decoder(
  70. asn1Object['content'],
  71. asn1Spec=rfc5083.AuthEnvelopedData())
  72. self.assertFalse(rest)
  73. self.assertTrue(aed.prettyPrint())
  74. self.assertEqual(asn1Object['content'], der_encoder(aed))
  75. self.assertEqual(0, aed['version'])
  76. ri = aed['recipientInfos'][0]
  77. self.assertIn(ri['ori']['oriType'], rfc5652.otherRecipientInfoMap)
  78. ori, rest = der_decoder(
  79. ri['ori']['oriValue'],
  80. asn1Spec=rfc5652.otherRecipientInfoMap[ri['ori']['oriType']])
  81. self.assertFalse(rest)
  82. self.assertTrue(ori.prettyPrint())
  83. self.assertEqual(ri['ori']['oriValue'], der_encoder(ori))
  84. class KeyAgreePSKTestCase(unittest.TestCase):
  85. key_agree_psk_pem_text = """\
  86. MIIBRwYLKoZIhvcNAQkQARegggE2MIIBMgIBADGB5aSB4gYLKoZIhvcNAQkQDQIwgdICAQAE
  87. FHB0Zi1rbWM6MjE2ODQwMTEwMTIxoFWhUzATBgYrgQQBCwEGCWCGSAFlAwQBLQM8AAQ5G0Em
  88. Jk/2ks8sXY1kzbuG3Uu3ttWwQRXALFDJICjvYfr+yTpOQVkchm88FAh9MEkw4NKctokKNgps
  89. MA0GCyqGSIb3DQEJEAMdMAsGCWCGSAFlAwQBLTBEMEKgFgQU6CGLmLi32Gtenr3IrrjE7NwF
  90. xSkEKCKf4LReQAA+fYJE7Bt+f/ssjcoWw29XNyIlU6cSY6kr3giGamAtY/QwNwYJKoZIhvcN
  91. AQcBMBsGCWCGSAFlAwQBLjAOBAzbrd7K+IjK/rq++s6ADfxtb4I+PtLSCdDG/88EDFUCYMQu
  92. WylxlCbB/w==
  93. """
  94. def setUp(self):
  95. self.asn1Spec = rfc5652.ContentInfo()
  96. def testDerCodec(self):
  97. substrate = pem.readBase64fromText(self.key_agree_psk_pem_text)
  98. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  99. self.assertFalse(rest)
  100. self.assertTrue(asn1Object.prettyPrint())
  101. self.assertEqual(substrate, der_encoder(asn1Object))
  102. self.assertEqual(
  103. rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  104. aed, rest = der_decoder(
  105. asn1Object['content'],
  106. asn1Spec=rfc5083.AuthEnvelopedData())
  107. self.assertFalse(rest)
  108. self.assertTrue(aed.prettyPrint())
  109. self.assertEqual(asn1Object['content'], der_encoder(aed))
  110. self.assertEqual(0, aed['version'])
  111. ri = aed['recipientInfos'][0]
  112. self.assertEqual(rfc8696.id_ori_keyAgreePSK, ri['ori']['oriType'])
  113. kapsk, rest = der_decoder(
  114. ri['ori']['oriValue'],
  115. asn1Spec=rfc8696.KeyAgreePSKRecipientInfo())
  116. self.assertFalse(rest)
  117. self.assertTrue(kapsk.prettyPrint())
  118. self.assertEqual(ri['ori']['oriValue'], der_encoder(kapsk))
  119. self.assertEqual(0, kapsk['version'])
  120. rek = kapsk['recipientEncryptedKeys'][0]
  121. ski = rek['rid']['rKeyId']['subjectKeyIdentifier']
  122. expected_ski = univ.OctetString(
  123. hexValue='e8218b98b8b7d86b5e9ebdc8aeb8c4ecdc05c529')
  124. self.assertEqual(expected_ski, ski)
  125. def testOtherRecipientInfoMap(self):
  126. substrate = pem.readBase64fromText(self.key_agree_psk_pem_text)
  127. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  128. self.assertFalse(rest)
  129. self.assertTrue(asn1Object.prettyPrint())
  130. self.assertEqual(substrate, der_encoder(asn1Object))
  131. self.assertEqual(
  132. rfc5083.id_ct_authEnvelopedData, asn1Object['contentType'])
  133. aed, rest = der_decoder(
  134. asn1Object['content'],
  135. asn1Spec=rfc5083.AuthEnvelopedData())
  136. self.assertFalse(rest)
  137. self.assertTrue(aed.prettyPrint())
  138. self.assertEqual(asn1Object['content'], der_encoder(aed))
  139. self.assertEqual(0, aed['version'])
  140. ri = aed['recipientInfos'][0]
  141. self.assertIn(ri['ori']['oriType'], rfc5652.otherRecipientInfoMap)
  142. ori, rest = der_decoder(
  143. ri['ori']['oriValue'],
  144. asn1Spec=rfc5652.otherRecipientInfoMap[ri['ori']['oriType']])
  145. self.assertFalse(rest)
  146. self.assertTrue(ori.prettyPrint())
  147. self.assertEqual(ri['ori']['oriValue'], der_encoder(ori))
  148. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  149. if __name__ == '__main__':
  150. unittest.TextTestRunner(verbosity=2).run(suite)