test_rfc2876.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. 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 rfc2876
  14. from pyasn1_modules import rfc5280
  15. from pyasn1_modules import rfc5652
  16. from pyasn1_modules import rfc5751
  17. try:
  18. import unittest2 as unittest
  19. except ImportError:
  20. import unittest
  21. class EnvelopedDataTestCase(unittest.TestCase):
  22. env_data_pem_text = """\
  23. MIIIogYJKoZIhvcNAQcDoIIIkzCCCI8CAQKgggKRoIICjTCCAokwggIwoAMCAQIC
  24. FGPMbd5dAfZyD1kqY7NIQyVCWZgqMAkGByqGSM44BAMwPzELMAkGA1UEBhMCVVMx
  25. CzAJBgNVBAgTAlZBMRAwDgYDVQQHEwdIZXJuZG9uMREwDwYDVQQKEwhCb2d1cyBD
  26. QTAeFw0xOTExMjAwODQzNDJaFw0yMDExMTkwODQzNDJaMGwxCzAJBgNVBAYTAlVT
  27. MQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMHRXhhbXBs
  28. ZTEMMAoGA1UEAxMDQm9iMR4wHAYJKoZIhvcNAQkBFg9ib2JAZXhhbXBsZS5jb20w
  29. gaEwFwYJYIZIAWUCAQEWBAp8tRylalhmjdM2A4GFAAKBgQD02ElSAgt9CWmKZ28J
  30. DMbpm/+aQ5PFPCTJRb1s2NuCHdakdYnkXXdtUgkIjgGYkV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2d7YQjKgrZBv6YwLAmw1LTE4
  62. bT9PM9n7LROnX8u6ksei8yiw8gZeVu+plWHbF+0O9siKAgxZlBna0XFgPpdzjMDT
  63. S/sfTIYXWlFj7camhsmTDRjo5G2B212evaKmKgh5ALLSFSk86ZN5KvQvcfsp81jv
  64. JCBmDStrsUgSMzy0Og2quHOd61hRTVlYzwvJvfMzHGKdIWwYUbHZOKo/KLEk3E36
  65. U9PkPoZGEL2ZeCH4F9Wh3mgg0knBfEmlPnGexmBby6NXGK7VW3l6xcJlpdMaXKNV
  66. Mfl2YK8k/34Hyft06KaYLEJsxAqk1pmLEmGhdZC1OAqovVB/1agSzpMMaB9OWWqN
  67. sTjDc7tkDt8BZ72NsAbCI9XmsX81W+NqPb6Ju1dtI09bn113LX/ZbOSdVicQcXSp
  68. l0FnTZaHgHJdQLcU28O7yFFOblqrvcMKpctdTA1TwG9LXEFttGrlpgjZF3edo0Ce
  69. z10epK+S
  70. """
  71. def setUp(self):
  72. self.asn1Spec = rfc5652.ContentInfo()
  73. def testDerCodec(self):
  74. substrate = pem.readBase64fromText(self.env_data_pem_text)
  75. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  76. self.assertFalse(rest)
  77. self.assertTrue(asn1Object.prettyPrint())
  78. self.assertEqual(substrate, der_encoder(asn1Object))
  79. self.assertEqual(rfc5652.id_envelopedData, asn1Object['contentType'])
  80. ed, rest = der_decoder(
  81. asn1Object['content'], asn1Spec=rfc5652.EnvelopedData())
  82. self.assertFalse(rest)
  83. self.assertTrue(ed.prettyPrint())
  84. self.assertEqual(asn1Object['content'], der_encoder(ed))
  85. kari_kea = ed['recipientInfos'][0]['kari']['keyEncryptionAlgorithm']
  86. self.assertEqual(rfc2876.id_kEAKeyEncryptionAlgorithm, kari_kea['algorithm'])
  87. kwa, rest = der_decoder(
  88. kari_kea['parameters'], asn1Spec=rfc5280.AlgorithmIdentifier())
  89. self.assertFalse(rest)
  90. self.assertTrue(kwa.prettyPrint())
  91. self.assertEqual(kari_kea['parameters'], der_encoder(kwa))
  92. self.assertEqual(rfc2876.id_fortezzaWrap80, kwa['algorithm'])
  93. cea = ed['encryptedContentInfo']['contentEncryptionAlgorithm']
  94. self.assertEqual(rfc2876.id_fortezzaConfidentialityAlgorithm, cea['algorithm'])
  95. param, rest = der_decoder(cea['parameters'], rfc2876.Skipjack_Parm())
  96. self.assertFalse(rest)
  97. self.assertTrue(param.prettyPrint())
  98. self.assertEqual(cea['parameters'], der_encoder(param))
  99. iv = univ.OctetString(hexValue='424f4755535f4956')
  100. self.assertEqual(iv, param['initialization-vector'])
  101. def testOpenTypes(self):
  102. substrate = pem.readBase64fromText(self.env_data_pem_text)
  103. asn1Object, rest = der_decoder(
  104. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  105. self.assertFalse(rest)
  106. self.assertTrue(asn1Object.prettyPrint())
  107. self.assertEqual(substrate, der_encoder(asn1Object))
  108. self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap.keys())
  109. kari_kea = asn1Object['content']['recipientInfos'][0]['kari']['keyEncryptionAlgorithm']
  110. self.assertEqual(rfc2876.id_kEAKeyEncryptionAlgorithm, kari_kea['algorithm'])
  111. self.assertEqual(rfc2876.id_fortezzaWrap80, kari_kea['parameters']['algorithm'])
  112. cea = asn1Object['content']['encryptedContentInfo']['contentEncryptionAlgorithm']
  113. self.assertEqual(rfc2876.id_fortezzaConfidentialityAlgorithm, cea['algorithm'])
  114. iv = univ.OctetString(hexValue='424f4755535f4956')
  115. self.assertEqual(iv, cea['parameters']['initialization-vector'])
  116. class SMIMECapabilitiesTestCase(unittest.TestCase):
  117. smime_capabilities_pem_text = "\
  118. MCcwGAYJYIZIAWUCAQEYMAsGCWCGSAFlAgEBFzALBglghkgBZQIBAQQ="
  119. def setUp(self):
  120. self.asn1Spec = rfc5751.SMIMECapabilities()
  121. def testDerCodec(self):
  122. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  123. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  124. self.assertFalse(rest)
  125. self.assertTrue(asn1Object.prettyPrint())
  126. self.assertEqual(substrate, der_encoder(asn1Object))
  127. found_wrap_alg = False
  128. for cap in asn1Object:
  129. if cap['capabilityID'] in rfc5751.smimeCapabilityMap.keys():
  130. if cap['parameters'].hasValue():
  131. param, rest = der_decoder(
  132. cap['parameters'],
  133. asn1Spec=rfc5751.smimeCapabilityMap[cap['capabilityID']])
  134. self.assertFalse(rest)
  135. self.assertTrue(param.prettyPrint())
  136. self.assertEqual(cap['parameters'], der_encoder(param))
  137. if cap['capabilityID'] == rfc2876.id_kEAKeyEncryptionAlgorithm:
  138. self.assertEqual(rfc2876.id_fortezzaWrap80, param['algorithm'])
  139. found_wrap_alg = True
  140. self.assertTrue(found_wrap_alg)
  141. def testOpenTypes(self):
  142. substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
  143. asn1Object, rest = der_decoder(
  144. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  145. self.assertFalse(rest)
  146. self.assertTrue(asn1Object.prettyPrint())
  147. self.assertEqual(substrate, der_encoder(asn1Object))
  148. found_wrap_alg = False
  149. for cap in asn1Object:
  150. if cap['capabilityID'] == rfc2876.id_kEAKeyEncryptionAlgorithm:
  151. self.assertEqual(rfc2876.id_fortezzaWrap80, cap['parameters']['algorithm'])
  152. found_wrap_alg = True
  153. self.assertTrue(found_wrap_alg)
  154. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  155. if __name__ == '__main__':
  156. unittest.TextTestRunner(verbosity=2).run(suite)