test_rfc7292.py 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 rfc5652
  15. from pyasn1_modules import rfc7292
  16. class PKCS12TestCase(unittest.TestCase):
  17. pfx_pem_text = """\
  18. MIIJ0wIBAzCCCY8GCSqGSIb3DQEHAaCCCYAEggl8MIIJeDCCBggGCSqGSIb3DQEHAaCCBfkE
  19. ggX1MIIF8TCCBe0GCyqGSIb3DQEMCgECoIIE/jCCBPowHAYKKoZIhvcNAQwBAzAOBAjuq0/+
  20. 0pyutQICB9AEggTYZe/mYBpmkDvKsve4EwIVwo1TNv4ldyx1qHZW2Ih6qQCY+Nv1Mnv9we0z
  21. UTl4p3tQzCPWXnrSA82IgOdotLIez4YwXrgiKhcIkSSL+2yCmAoM+qkjiAIKq+l3UJ6Xhafe
  22. 2Kg4Ek/0RkHpe6GwjTtdefkpXpZgccMEopOtKQMLJWsDM7p77x/amn6yIk2tpskKqUY/4n8Y
  23. xEiTWcRtTthYqZQIt+q94nKLYpt0o880SVOfvdEqp5KII7cTg60GJL+n6oN6hmP0bsAMvnk9
  24. 1f8/lFKMi9tsNU/KnUhbDVpjJwBQkhgbqBx6GdtoqSLSlYNPVM0wlntwm1JhH4ybiQ5sNzqO
  25. 7FlWC5bcYwkvOlx1gGrshY5jK/WjbA4paBpxSkgobJReirY9BeqITnvokXlub4teh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X9zCs91JdN/ZFZ05SXIpHQuyPSPUX5Vy8F1ZeJ8VG3nkbemfFlVkuKQq
  55. vteL9mlT7z95rVZgGB3nUZL0tOB68eMcffA9zUksOmeTi5M6jnBcNeX2Jh9jS3YYd+IEliZm
  56. mggQG7kPta8f+NqezL77AgMBAAGjVTBTMBUGA1UdJQQOMAwGCisGAQQBgjcKAwQwLwYDVR0R
  57. BCgwJqAkBgorBgEEAYI3FAIDoBYMFGFub255bW91c0B3aW5kb3dzLXgAMAkGA1UdEwQCMAAw
  58. DQYJKoZIhvcNAQEFBQADggEBALh+4qmNPzC6M8BW9/SC2ACQxxPh06GQUGx0D+GLYnp61ErZ
  59. OtKyKdFh+uZWpu5vyYYAHCLXP7VdS/JhJy677ynAPjXiC/LAzrTNvGs74HDotD966Hiyy0Qr
  60. ospFGiplHGRA5vXA2CiKSX+0HrVkN7rhk5PYkc6R+/cdosd+QZ8lkEa9yDWc5l//vWEbzwVy
  61. mJf/PRf8NTkWAK6SPV7Y37j1mhkJjOH9VkRxNrd6kcihRa4u0ImXaXEsec77ER0so31DKCrP
  62. m+rqZPj9NZSIYP3sMGJ4Bmm/n2YRdeaUzTdocfD3TRnKxs65DSgpiSq1gmtsXM7jAPs/Egrg
  63. tbWEypgxFTATBgkqhkiG9w0BCRUxBgQEAQAAADA7MB8wBwYFKw4DAhoEFKVgj/32UdEyuQcB
  64. rqr03dPnboinBBSU7mxdpB5LTCvorCI8Tk5OMiUzjgICB9A=
  65. """
  66. def setUp(self):
  67. self.asn1Spec = rfc7292.PFX()
  68. def testDerCodec(self):
  69. substrate = pem.readBase64fromText(self.pfx_pem_text)
  70. asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
  71. self.assertFalse(rest)
  72. self.assertTrue(asn1Object.prettyPrint())
  73. self.assertEqual(substrate, der_encoder(asn1Object))
  74. self.assertEqual(3, asn1Object['version'])
  75. oid = asn1Object['macData']['mac']['digestAlgorithm']['algorithm']
  76. self.assertEqual(univ.ObjectIdentifier('1.3.14.3.2.26'), oid)
  77. md_hex = asn1Object['macData']['mac']['digest'].prettyPrint()
  78. self.assertEqual('0xa5608ffdf651d132b90701aeaaf4ddd3e76e88a7', md_hex)
  79. self.assertEqual(
  80. rfc5652.id_data, asn1Object['authSafe']['contentType'])
  81. data, rest = der_decoder(
  82. asn1Object['authSafe']['content'], asn1Spec=univ.OctetString())
  83. self.assertFalse(rest)
  84. authsafe, rest = der_decoder(data, asn1Spec=rfc7292.AuthenticatedSafe())
  85. self.assertFalse(rest)
  86. self.assertTrue(authsafe.prettyPrint())
  87. self.assertEqual(data, der_encoder(authsafe))
  88. for ci in authsafe:
  89. self.assertEqual(rfc5652.id_data, ci['contentType'])
  90. data, rest = der_decoder(ci['content'], asn1Spec=univ.OctetString())
  91. self.assertFalse(rest)
  92. sc, rest = der_decoder(data, asn1Spec=rfc7292.SafeContents())
  93. self.assertFalse(rest)
  94. self.assertTrue(sc.prettyPrint())
  95. self.assertEqual(data, der_encoder(sc))
  96. for sb in sc:
  97. if sb['bagId'] in rfc7292.pkcs12BagTypeMap:
  98. bv, rest = der_decoder(
  99. sb['bagValue'],
  100. asn1Spec=rfc7292.pkcs12BagTypeMap[sb['bagId']])
  101. self.assertFalse(rest)
  102. self.assertTrue(bv.prettyPrint())
  103. self.assertEqual(sb['bagValue'], der_encoder(bv))
  104. for attr in sb['bagAttributes']:
  105. if attr['attrType'] in rfc5652.cmsAttributesMap:
  106. av, rest = der_decoder(
  107. attr['attrValues'][0],
  108. asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
  109. self.assertFalse(rest)
  110. self.assertTrue(av.prettyPrint())
  111. self.assertEqual(
  112. attr['attrValues'][0], der_encoder(av))
  113. def testOpenTypes(self):
  114. substrate = pem.readBase64fromText(self.pfx_pem_text)
  115. asn1Object, rest = der_decoder(
  116. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  117. self.assertFalse(rest)
  118. self.assertTrue(asn1Object.prettyPrint())
  119. self.assertEqual(substrate, der_encoder(asn1Object))
  120. digest_alg = asn1Object['macData']['mac']['digestAlgorithm']
  121. self.assertFalse(digest_alg['parameters'].hasValue())
  122. authsafe, rest = der_decoder(
  123. asn1Object['authSafe']['content'],
  124. asn1Spec=rfc7292.AuthenticatedSafe(),
  125. decodeOpenTypes=True)
  126. self.assertFalse(rest)
  127. self.assertTrue(authsafe.prettyPrint())
  128. self.assertEqual(
  129. asn1Object['authSafe']['content'], der_encoder(authsafe))
  130. for ci in authsafe:
  131. self.assertEqual(rfc5652.id_data, ci['contentType'])
  132. sc, rest = der_decoder(
  133. ci['content'], asn1Spec=rfc7292.SafeContents(),
  134. decodeOpenTypes=True)
  135. self.assertFalse(rest)
  136. self.assertTrue(sc.prettyPrint())
  137. self.assertEqual(ci['content'], der_encoder(sc))
  138. for sb in sc:
  139. if sb['bagId'] == rfc7292.id_pkcs8ShroudedKeyBag:
  140. bv = sb['bagValue']
  141. enc_alg = bv['encryptionAlgorithm']['algorithm']
  142. self.assertEqual(
  143. rfc7292.pbeWithSHAAnd3_KeyTripleDES_CBC, enc_alg)
  144. enc_alg_param = bv['encryptionAlgorithm']['parameters']
  145. self.assertEqual(2000, enc_alg_param['iterations'])
  146. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  147. if __name__ == '__main__':
  148. unittest.TextTestRunner(verbosity=2).run(suite)