test_rfc5751.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 rfc5751
  16. class SignedMessageTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIIGigYJKoZIhvcNAQcCoIIGezCCBncCAQExCTAHBgUrDgMCGjArBgkqhkiG9w0B
  19. BwGgHgQcVGhpcyBpcyBzb21lIHNhbXBsZSBjb250ZW50LqCCAuAwggLcMIICm6AD
  20. AgECAgIAyDAJBgcqhkjOOAQDMBIxEDAOBgNVBAMTB0NhcmxEU1MwHhcNOTkwODE3
  21. MDExMDQ5WhcNMzkxMjMxMjM1OTU5WjATMREwDwYDVQQDEwhBbGljZURTUzCCAbYw
  22. ggErBgcqhkjOOAQBMIIBHgKBgQCBjc3tg+oKnjk+wkgoo+RHk90O16gO7FPFq4QI
  23. T/+U4XNIfgzW80RI0f6fr6ShiS/h2TDINt4/m7+3TNxfaYrkddA3DJEIlZvep175
  24. /PSfL91DqItU8T+wBwhHTV2Iw8O1s+NVCHXVOXYQxHi9/52whJc38uRRG7XkCZZc
  25. 835b2wIVAOJHphpFZrgTxtqPuDchK2KL95PNAoGAJjjQFIkyqjn7Pm3ZS1lqTHYj
  26. OQQCNVzyyxowwx5QXd2bWeLNqgU9WMB7oja4bgevfYpCJaf0dc9KCF5LPpD4beqc
  27. ySGKO3YU6c4uXaMHzSOFuC8wAXxtSYkRiTZEvfjIlUpTVrXi+XPsGmE2HxF/wr3t
  28. 0VD/mHTC0YFKY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lIFZhbHVlIEJ1ZmZlcjBz
  44. BgsqhkiG9w0BCRACCzFkoGIwWjELMAkGA1UEBhMCVVMxFjAUBgNVBAoTDVVTIEdv
  45. dmVybm1lbnQxETAPBgNVBAsTCFZEQSBTaXRlMQwwCgYDVQQLEwNWREExEjAQBgNV
  46. BAMTCURhaXN5IFJTQQIEClVEMzCB/AYLKoZIhvcNAQkQAgMxgewwgekwgeYEBzU3
  47. MzgyOTkYDzE5OTkwMzExMTA0NDMzWqGByTCBxqRhMF8xCzAJBgNVBAYTAlVTMRYw
  48. FAYDVQQKEw1VUyBHb3Zlcm5tZW50MREwDwYDVQQLEwhWREEgU2l0ZTEMMAoGA1UE
  49. CxMDVkRBMRcwFQYDVQQDEw5CdWdzIEJ1bm55IERTQaRhMF8xCzAJBgNVBAYTAlVT
  50. MRYwFAYDVQQKEw1VUyBHb3Zlcm5tZW50MREwDwYDVQQLEwhWREEgU2l0ZTEMMAoG
  51. A1UECxMDVkRBMRcwFQYDVQQDEw5FbG1lciBGdWRkIERTQTAJBgcqhkjOOAQDBC8w
  52. LQIVALwzN2XE93BcF0kTqkyFyrtSkUhZAhRjlqIUi89X3rBIX2xk3YQESV8cyg==
  53. """
  54. def setUp(self):
  55. self.asn1Spec = rfc5652.ContentInfo()
  56. def testDerCodec(self):
  57. smimeCapMap = {
  58. univ.ObjectIdentifier('1.2.3.4.5.6.77'): univ.OctetString(),
  59. }
  60. smimeCapMap.update(rfc5751.smimeCapabilityMap)
  61. substrate = pem.readBase64fromText(self.pem_text)
  62. asn1Object, rest = der_decoder (substrate,
  63. asn1Spec=self.asn1Spec,
  64. decodeOpenTypes=True)
  65. self.assertFalse(rest)
  66. self.assertTrue(asn1Object.prettyPrint())
  67. self.assertEqual(substrate, der_encoder(asn1Object))
  68. self.assertEqual(rfc5652.id_signedData, asn1Object['contentType'])
  69. self.assertEqual(1, asn1Object['content']['version'])
  70. for si in asn1Object['content']['signerInfos']:
  71. self.assertEqual(1, si['version'])
  72. for attr in si['signedAttrs']:
  73. if attr['attrType'] == rfc5751.smimeCapabilities:
  74. for scap in attr['attrValues'][0]:
  75. if scap['capabilityID'] in smimeCapMap.keys():
  76. scap_p, rest = der_decoder(scap['parameters'],
  77. asn1Spec=smimeCapMap[scap['capabilityID']])
  78. self.assertFalse(rest)
  79. self.assertEqual(scap['parameters'], der_encoder(scap_p))
  80. self.assertIn('parameters', scap_p.prettyPrint())
  81. if attr['attrType'] == rfc5751.id_aa_encrypKeyPref:
  82. ekp_issuer_serial = attr['attrValues'][0]['issuerAndSerialNumber']
  83. self.assertEqual(173360179, ekp_issuer_serial['serialNumber'])
  84. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  85. if __name__ == '__main__':
  86. result = unittest.TextTestRunner(verbosity=2).run(suite)
  87. sys.exit(not result.wasSuccessful())