test_rfc7191.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 rfc5652
  14. from pyasn1_modules import rfc7191
  15. class ReceiptRequestTestCase(unittest.TestCase):
  16. message1_pem_text = """\
  17. MIIGfAYJKoZIhvcNAQcCoIIGbTCCBmkCAQMxDTALBglghkgBZQMEAgIwgb4GCyqGSIb3DQEJ
  18. EAEZoIGuBIGrMIGooEQwIwYLKoZIhvcNAQkQDAExFAwSVmlnaWwgU2VjdXJpdHkgTExDMB0G
  19. CyqGSIb3DQEJEAwDMQ4MDFByZXRlbmQgMDQ4QTBgMF4wVjAbBgsqhkiG9w0BCRAMGzEMDApl
  20. eGFtcGxlSUQxMBUGCyqGSIb3DQEJEAwKMQYMBEhPVFAwIAYLKoZIhvcNAQkQDAsxEQwPa3Rh
  21. LmV4YW1wbGUuY29tBAQxMjM0oIIChzCCAoMwggIKoAMCAQICCQCls1QoG7BuPTAKBggqhkjO
  22. PQQDAzA/MQswCQYDVQQGEwJVUzELMAkGA1UECAwCVkExEDAOBgNVBAcMB0hlcm5kb24xETAP
  23. BgNVBAoMCEJvZ3VzIENBM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orn9htte1iFPbxPRUwggG/BglghkgBZQIBBUExggGwMIIBrAQUJ7icVjsWIlGdF4cc
  38. eb+siG3f+D0wggGSoIH+MH8GCWCGSAFlAgEQAARyMHAxCzAJBgNVBAYTAlVTMQswCQYDVQQI
  39. EwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMHRXhhbXBsZTEOMAwGA1UEAxMFQWxp
  40. Y2UxIDAeBgkqhkiG9w0BCQEWEWFsaWNlQGV4YW1wbGUuY29tMHsGCWCGSAFlAgEQAARuMGwx
  41. CzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4GA1UEChMH
  42. RXhhbXBsZTEMMAoGA1UEAxMDQm9iMR4wHAYJKoZIhvcNAQkBFg9ib2JAZXhhbXBsZS5jb20w
  43. gY4wgYsGCWCGSAFlAgEQAAR+MHwxCzAJBgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UE
  44. BxMHSGVybmRvbjEbMBkGA1UEChMSVmlnaWwgU2VjdXJpdHkgTExDMRcwFQYDVQQLEw5LZXkg
  45. TWFuYWdlbWVudDEYMBYGA1UEAxMPa3RhLmV4YW1wbGUuY29tMAoGCCqGSM49BAMDBGYwZAIw
  46. Z7DXliUb8FDKs+BadyCY+IJobPnQ6UoLldMj3pKEowONPifqrbWBJJ5cQQNgW6YuAjBbjSlY
  47. goRV+bq4fdgOOj25JFqa80xnXGtQqjm/7NSII5SbdJk+DT7KCkSbkElkbgQ=
  48. """
  49. def setUp(self):
  50. self.asn1Spec = rfc5652.ContentInfo()
  51. def testDerCodec(self):
  52. substrate = pem.readBase64fromText(self.message1_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_signedData, asn1Object['contentType'])
  58. sd, rest = der_decoder(
  59. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  60. for sa in sd['signerInfos'][0]['signedAttrs']:
  61. sat = sa['attrType']
  62. sav0 = sa['attrValues'][0]
  63. if sat == rfc7191.id_aa_KP_keyPkgIdAndReceiptReq:
  64. sav, rest = der_decoder(
  65. sav0, asn1Spec=rfc7191.KeyPkgIdentifierAndReceiptReq())
  66. self.assertFalse(rest)
  67. self.assertTrue(sav.prettyPrint())
  68. self.assertEqual(sav0, der_encoder(sav))
  69. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  70. package_id = pem.readBase64fromText(package_id_pem_text)
  71. self.assertEqual(package_id, sav['pkgID'])
  72. def testOpenTypes(self):
  73. substrate = pem.readBase64fromText(self.message1_pem_text)
  74. asn1Object, rest = der_decoder(
  75. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  76. self.assertFalse(rest)
  77. self.assertTrue(asn1Object.prettyPrint())
  78. self.assertEqual(substrate, der_encoder(asn1Object))
  79. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  80. v3 = rfc5652.CMSVersion().subtype(value='v3')
  81. self.assertEqual(v3, asn1Object['content']['version'])
  82. for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
  83. if sa['attrType'] == rfc7191.id_aa_KP_keyPkgIdAndReceiptReq:
  84. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  85. package_id = pem.readBase64fromText(package_id_pem_text)
  86. self.assertEqual(package_id, sa['attrValues'][0]['pkgID'])
  87. class ReceiptTestCase(unittest.TestCase):
  88. message2_pem_text = """\
  89. MIIEdAYJKoZIhvcNAQcCoIIEZTCCBGECAQMxDTALBglghkgBZQMEAgIwgawGCmCGSAFlAgEC
  90. TgOggZ0EgZowgZcEFCe4nFY7FiJRnReHHHm/rIht3/g9MH8GCWCGSAFlAgEQAARyMHAxCzAJ
  91. BgNVBAYTAlVTMQswCQYDVQQIEwJWQTEQMA4GA1UEBxMHSGVybmRvbjEQMA4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qhkiG9w0BCQMx
  106. DAYKYIZIAWUCAQJOAzAcBgkqhkiG9w0BCQUxDxcNMTkwNjEzMTYxNjA4WjA/BgkqhkiG9w0B
  107. CQQxMgQwQSWYpq4jwhMkmS0as0JL3gjYxKLgDfzP2ndTNsAY0m9p8Igp8ZcK4+5n9fXJ43vU
  108. MAoGCCqGSM49BAMDBGgwZgIxAMfq2EJ5pSl9tGOEVJEgZitc266ljrOg5GDjkd2d089qw1A3
  109. bUcOYuCdivgxVuhlAgIxAPR9JavxziwCbVyBUWOAiKKYfglTgG3AwNmrKDj0NtXUQ9qDmGAc
  110. 6L+EAY2P5OVB8Q==
  111. """
  112. def setUp(self):
  113. self.asn1Spec = rfc5652.ContentInfo()
  114. def testDerCodec(self):
  115. substrate = pem.readBase64fromText(self.message2_pem_text)
  116. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  117. self.assertFalse(rest)
  118. self.assertTrue(asn1Object.prettyPrint())
  119. self.assertEqual(substrate, der_encoder(asn1Object))
  120. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  121. sd, rest = der_decoder(
  122. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  123. self.assertFalse(rest)
  124. self.assertTrue(sd.prettyPrint())
  125. self.assertEqual(asn1Object['content'], der_encoder(sd))
  126. oid = sd['encapContentInfo']['eContentType']
  127. self.assertEqual(rfc7191.id_ct_KP_keyPackageReceipt, oid)
  128. receipt, rest = der_decoder(
  129. sd['encapContentInfo']['eContent'],
  130. asn1Spec=rfc7191.KeyPackageReceipt())
  131. self.assertFalse(rest)
  132. self.assertTrue(receipt.prettyPrint())
  133. self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(receipt))
  134. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  135. package_id = pem.readBase64fromText(package_id_pem_text)
  136. self.assertEqual(package_id, receipt['receiptOf']['pkgID'])
  137. def testOpenTypes(self):
  138. substrate = pem.readBase64fromText(self.message2_pem_text)
  139. asn1Object, rest = der_decoder(
  140. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  141. self.assertFalse(rest)
  142. self.assertTrue(asn1Object.prettyPrint())
  143. self.assertEqual(substrate, der_encoder(asn1Object))
  144. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  145. v3 = rfc5652.CMSVersion().subtype(value='v3')
  146. self.assertEqual(v3, asn1Object['content']['version'])
  147. for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
  148. self.assertIn( sa['attrType'], rfc5652.cmsAttributesMap)
  149. if sa['attrType'] == rfc5652.id_messageDigest:
  150. self.assertIn(
  151. '0x412598a6ae2', sa['attrValues'][0].prettyPrint())
  152. ct_oid = asn1Object['content']['encapContentInfo']['eContentType']
  153. self.assertIn(ct_oid, rfc5652.cmsContentTypesMap)
  154. self.assertEqual(ct_oid, rfc7191.id_ct_KP_keyPackageReceipt)
  155. # Since receipt is inside an OCTET STRING, decodeOpenTypes=True cannot
  156. # automatically decode it
  157. sd_eci = asn1Object['content']['encapContentInfo']
  158. receipt, rest = der_decoder(
  159. sd_eci['eContent'],
  160. asn1Spec=rfc5652.cmsContentTypesMap[sd_eci['eContentType']])
  161. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  162. package_id = pem.readBase64fromText(package_id_pem_text)
  163. self.assertEqual(package_id, receipt['receiptOf']['pkgID'])
  164. class ErrorTestCase(unittest.TestCase):
  165. message3_pem_text = """\
  166. MIIEbwYJKoZIhvcNAQcCoIIEYDCCBFwCAQMxDTALBglghkgBZQMEAgIwga0GCmCGSAFlAgEC
  167. TgaggZ4EgZswgZigFgQUJ7icVjsWIlGdF4cceb+siG3f+D0wewYJYIZIAWUCARAABG4wbDEL
  168. MAkGA1UEBhMCVVMxCzAJBgNVBAgTAlZBMRAwDgYDVQQHEwdIZXJuZG9uMRAwDgYDVQQKEwdF
  169. eGFtcGxlMQwwCgYDVQQDEwNCb2IxHjAcBgkqhkiG9w0BCQEWD2JvYkBleG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aMD8GCSqGSIb3DQEJBDEy
  184. BDCgXFTUc3ZInjt+MWYkYmXYERk4FgErEZNILlWgVl7Z9pImgLObIpdrGqGPt06/VkwwCgYI
  185. KoZIzj0EAwMEZzBlAjEAsjJ3iWRUteMKBVsjaYeN6TG9NITRTOpRVkSVq55DcnhwS9g9lu8D
  186. iNF8uKtW/lk0AjA7z2q40N0lamXkSU7ECasiWOYV1X4cWGiQwMZDKknBPDqXqB6Es6p4J+qe
  187. 0V6+BtY=
  188. """
  189. def setUp(self):
  190. self.asn1Spec = rfc5652.ContentInfo()
  191. def testDerCodec(self):
  192. substrate = pem.readBase64fromText(self.message3_pem_text)
  193. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  194. self.assertFalse(rest)
  195. self.assertTrue(asn1Object.prettyPrint())
  196. self.assertEqual(substrate, der_encoder(asn1Object))
  197. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  198. sd, rest = der_decoder(
  199. asn1Object['content'], asn1Spec=rfc5652.SignedData())
  200. self.assertFalse(rest)
  201. self.assertTrue(sd.prettyPrint())
  202. self.assertEqual(asn1Object['content'], der_encoder(sd))
  203. oid = sd['encapContentInfo']['eContentType']
  204. self.assertEqual(rfc7191.id_ct_KP_keyPackageError, oid)
  205. kpe, rest = der_decoder(
  206. sd['encapContentInfo']['eContent'],
  207. asn1Spec=rfc7191.KeyPackageError())
  208. self.assertFalse(rest)
  209. self.assertTrue(kpe.prettyPrint())
  210. self.assertEqual(sd['encapContentInfo']['eContent'], der_encoder(kpe))
  211. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  212. package_id = pem.readBase64fromText(package_id_pem_text)
  213. self.assertEqual(package_id, kpe['errorOf']['pkgID'])
  214. self.assertEqual(
  215. rfc7191.EnumeratedErrorCode(value=10), kpe['errorCode'])
  216. def testOpenTypes(self):
  217. substrate = pem.readBase64fromText(self.message3_pem_text)
  218. asn1Object, rest = der_decoder(
  219. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  220. self.assertFalse(rest)
  221. self.assertTrue(asn1Object.prettyPrint())
  222. self.assertEqual(substrate, der_encoder(asn1Object))
  223. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  224. v3 = rfc5652.CMSVersion().subtype(value='v3')
  225. self.assertEqual(v3, asn1Object['content']['version'])
  226. for sa in asn1Object['content']['signerInfos'][0]['signedAttrs']:
  227. self.assertIn(sa['attrType'], rfc5652.cmsAttributesMap)
  228. if sa['attrType'] == rfc5652.id_messageDigest:
  229. self.assertIn(
  230. '0xa05c54d4737', sa['attrValues'][0].prettyPrint())
  231. ct_oid = asn1Object['content']['encapContentInfo']['eContentType']
  232. self.assertIn(ct_oid, rfc5652.cmsContentTypesMap)
  233. self.assertEqual(rfc7191.id_ct_KP_keyPackageError, ct_oid)
  234. # Since receipt is inside an OCTET STRING, decodeOpenTypes=True cannot
  235. # automatically decode it
  236. sd_eci = asn1Object['content']['encapContentInfo']
  237. kpe, rest = der_decoder(
  238. sd_eci['eContent'],
  239. asn1Spec=rfc5652.cmsContentTypesMap[sd_eci['eContentType']])
  240. package_id_pem_text = "J7icVjsWIlGdF4cceb+siG3f+D0="
  241. package_id = pem.readBase64fromText(package_id_pem_text)
  242. self.assertEqual(package_id, kpe['errorOf']['pkgID'])
  243. self.assertEqual(rfc7191.EnumeratedErrorCode(value=10), kpe['errorCode'])
  244. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  245. if __name__ == '__main__':
  246. result = unittest.TextTestRunner(verbosity=2).run(suite)
  247. sys.exit(not result.wasSuccessful())