test_rfc5126.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.codec.der.decoder import decode as der_decoder
  10. from pyasn1.codec.der.encoder import encode as der_encoder
  11. from pyasn1_modules import pem
  12. from pyasn1_modules import rfc4055
  13. from pyasn1_modules import rfc5652
  14. from pyasn1_modules import rfc5126
  15. class SignedAttributesTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MYIBUzAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcBMCsGCSqGSIb3DQEJNDEeMBww
  18. DQYJYIZIAWUDBAIBBQChCwYJKoZIhvcNAQELMC8GCSqGSIb3DQEJBDEiBCCyqtCC
  19. Gosj/GT4YPPAqKheze4A1QBU5O3tniTsVPGr7jBBBgsqhkiG9w0BCRACETEyMDCg
  20. BBMCVVOhBBMCVkGiIjAgExExMjMgU29tZXBsYWNlIFdheRMLSGVybmRvbiwgVkEw
  21. RgYLKoZIhvcNAQkQAi8xNzA1MDMwMTANBglghkgBZQMEAgEFAAQgJPmqUmGQnQ4q
  22. RkVtUHecJXIkozOzX8+pZQj/UD5JcnQwTgYLKoZIhvcNAQkQAg8xPzA9BgorBgEE
  23. AYGsYDAUMC8wCwYJYIZIAWUDBAIBBCDWjjVmAeXgZBkE/rG8Pf8pTCs4Ikowc8Vm
  24. l+AOeKdFgg==
  25. """
  26. def setUp(self):
  27. self.asn1Spec = rfc5652.SignedAttributes()
  28. def testDerCodec(self):
  29. substrate = pem.readBase64fromText(self.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. found_spid_oid = False
  35. for attr in asn1Object:
  36. if attr['attrType'] in rfc5652.cmsAttributesMap.keys():
  37. av, rest = der_decoder(
  38. attr['attrValues'][0],
  39. asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
  40. self.assertFalse(rest)
  41. self.assertTrue(av.prettyPrint())
  42. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  43. if attr['attrType'] == rfc5126.id_aa_ets_sigPolicyId:
  44. spid_oid = rfc5126.SigPolicyId('1.3.6.1.4.1.22112.48.20')
  45. self.assertEqual(
  46. spid_oid, av['signaturePolicyId']['sigPolicyId'])
  47. found_spid_oid = True
  48. self.assertTrue(found_spid_oid)
  49. def testOpenTypes(self):
  50. substrate = pem.readBase64fromText(self.pem_text)
  51. asn1Object, rest = der_decoder(
  52. substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
  53. self.assertFalse(rest)
  54. self.assertTrue(asn1Object.prettyPrint())
  55. self.assertEqual(substrate, der_encoder(asn1Object))
  56. attr_type_list = []
  57. spid_oid = rfc5126.SigPolicyId('1.3.6.1.4.1.22112.48.20')
  58. for attr in asn1Object:
  59. if attr['attrType'] == rfc5126.id_aa_ets_sigPolicyId:
  60. spid = attr['attrValues'][0]['signaturePolicyId']
  61. self.assertEqual(spid_oid, spid['sigPolicyId'])
  62. attr_type_list.append(rfc5126.id_aa_ets_sigPolicyId)
  63. if attr['attrType'] == rfc5126.id_aa_ets_signerLocation:
  64. cn = attr['attrValues'][0]['countryName']
  65. self.assertEqual('US', cn['printableString'])
  66. attr_type_list.append(rfc5126.id_aa_ets_signerLocation)
  67. if attr['attrType'] == rfc5126.id_aa_signingCertificateV2:
  68. ha = attr['attrValues'][0]['certs'][0]['hashAlgorithm']
  69. self.assertEqual(rfc4055.id_sha256, ha['algorithm'])
  70. attr_type_list.append(rfc5126.id_aa_signingCertificateV2)
  71. self.assertIn(rfc5126.id_aa_ets_sigPolicyId, attr_type_list)
  72. self.assertIn(rfc5126.id_aa_ets_signerLocation, attr_type_list)
  73. self.assertIn(rfc5126.id_aa_signingCertificateV2, attr_type_list)
  74. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  75. if __name__ == '__main__':
  76. import sys
  77. result = unittest.TextTestRunner(verbosity=2).run(suite)
  78. sys.exit(not result.wasSuccessful())