test_rfc3852.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 rfc3852
  15. from pyasn1_modules import rfc6402
  16. class ContentInfoTestCase(unittest.TestCase):
  17. pem_text = """\
  18. MIIEJQYJKoZIhvcNAQcCoIIEFjCCBBICAQMxCzAJBgUrDgMCGgUAMIIDAgYIKwYBBQUHDAKgggL0
  19. BIIC8DCCAuwweDB2AgECBgorBgEEAYI3CgoBMWUwYwIBADADAgEBMVkwVwYJKwYBBAGCNxUUMUow
  20. SAIBBQwZcGl0dWNoYTEuZW1lYS5ocHFjb3JwLm5ldAwMRU1FQVxwaXR1Y2hhDBpDTUNSZXFHZW5l
  21. cmF0b3IudnNob3N0LmV4ZTCCAmqgggJmAgEBMIICXzCCAcgCAQAwADCBnzANBgkqhkiG9w0BAQEF
  22. AAOBjQAwgYkCgYEA0jm7SSSm2wyEAzuNKtFZFJKo91SrJq9wQwEhEKHDavZwMQOm1rZ2PF8NWCEb
  23. PqrhToQ7rtiGLSZa4dF4bzgmBqQ9aoSfEX4jISt31Vy+skHidXjHHpbsjT24NPhrZgANivL7CxD6
  24. Ft+s7qS1gL4HRm2twQkqSwOLrE/q2QeXl2UCAwEAAaCCAR0wGgYKKwYBBAGCNw0CAzEMFgo2LjIu
  25. OTIwMC4yMD4GCSqGSIb3DQEJDjExMC8wHQYDVR0OBBYEFMW2skn88gxhONWZQA4sWGBDb68yMA4G
  26. A1UdDwEB/wQEAwIHgDBXBgkrBgEEAYI3FRQxSjBIAgEFDBlwaXR1Y2hhMS5lbWVhLmhwcWNvcnAu
  27. bmV0DAxFTUVBXHBpdHVjaGEMGkNNQ1JlcUdlbmVyYXRvci52c2hvc3QuZXhlMGYGCisGAQQBgjcN
  28. AgIxWDBWAgECHk4ATQBpAGMAcgBvAHMAbwBmAHQAIABTAHQAcgBvAG4AZwAgAEMAcgB5AHAAdABv
  29. AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIDAQAwDQYJKoZIhvcNAQEFBQADgYEAJZlu
  30. mxjtCxSOQi27jsVdd3y8NSIlzNv0b3LqmzvAly6L+CstXcnuG2MPQqPH9R7tbJonGUniBQO9sQ7C
  31. KhYWj2gfhiEkSID82lV5chINVUFKoUlSiEhWr0tPGgvOaqdsKQcrHfzrsBbFkhDqrFSVy7Yivbnh
  32. qYszKrOjJKiiCPMwADAAMYH5MIH2AgEDgBTFtrJJ/PIMYTjVmUAOLFhgQ2+vMjAJBgUrDgMCGgUA
  33. oD4wFwYJKoZIhvcNAQkDMQoGCCsGAQUFBwwCMCMGCSqGSIb3DQEJBDEWBBTFTkK/OifaFjwqHiJu
  34. xM7qXcg/VzANBgkqhkiG9w0BAQEFAASBgKfC6jOi1Wgy4xxDCQVK9+e5tktL8wE/j2cb9JSqq+aU
  35. 5UxEgXEw7q7BoYZCAzcxMRriGzakXr8aXHcgkRJ7XcFvLPUjpmGg9SOZ2sGW4zQdWAwImN/i8loc
  36. xicQmJP+VoMHo/ZpjFY9fYCjNZUArgKsEwK/s+p9yrVVeB1Nf8Mn
  37. """
  38. def setUp(self):
  39. self.asn1Spec = rfc3852.ContentInfo()
  40. def testDerCodec(self):
  41. substrate = pem.readBase64fromText(self.pem_text)
  42. layers = {
  43. rfc3852.id_ct_contentInfo: rfc3852.ContentInfo(),
  44. rfc3852.id_signedData: rfc3852.SignedData(),
  45. rfc6402.id_cct_PKIData: rfc6402.PKIData()
  46. }
  47. getNextLayer = {
  48. rfc3852.id_ct_contentInfo: lambda x: x['contentType'],
  49. rfc3852.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  50. rfc6402.id_cct_PKIData: lambda x: None
  51. }
  52. getNextSubstrate = {
  53. rfc3852.id_ct_contentInfo: lambda x: x['content'],
  54. rfc3852.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  55. rfc6402.id_cct_PKIData: lambda x: None
  56. }
  57. alg_oids = (
  58. univ.ObjectIdentifier('1.3.14.3.2.26'),
  59. univ.ObjectIdentifier('1.2.840.113549.1.1.1'),
  60. univ.ObjectIdentifier('1.2.840.113549.1.1.5'),
  61. univ.ObjectIdentifier('1.2.840.113549.1.1.11'),
  62. )
  63. encoded_null = der_encoder(univ.Null(""))
  64. next_layer = rfc3852.id_ct_contentInfo
  65. count = 0
  66. while next_layer:
  67. asn1Object, rest = der_decoder(substrate, asn1Spec=layers[next_layer])
  68. self.assertFalse(rest)
  69. self.assertTrue(asn1Object.prettyPrint())
  70. self.assertEqual(substrate, der_encoder(asn1Object))
  71. if next_layer == rfc3852.id_signedData:
  72. for d in asn1Object['digestAlgorithms']:
  73. self.assertIn(d['algorithm'], alg_oids)
  74. self.assertEqual(encoded_null, d['parameters'])
  75. count += 1
  76. for si in asn1Object['signerInfos']:
  77. self.assertIn(si['digestAlgorithm']['algorithm'], alg_oids)
  78. self.assertEqual(
  79. encoded_null, si['digestAlgorithm']['parameters'])
  80. count += 1
  81. self.assertIn(si['signatureAlgorithm']['algorithm'], alg_oids)
  82. self.assertEqual(
  83. encoded_null, si['signatureAlgorithm']['parameters'])
  84. count += 1
  85. if next_layer == rfc6402.id_cct_PKIData:
  86. for req in asn1Object['reqSequence']:
  87. cr = req['tcr']['certificationRequest']
  88. self.assertIn(cr['signatureAlgorithm']['algorithm'], alg_oids)
  89. self.assertEqual(
  90. encoded_null, cr['signatureAlgorithm']['parameters'])
  91. count += 1
  92. cri_spki = cr['certificationRequestInfo']['subjectPublicKeyInfo']
  93. self.assertIn(cri_spki['algorithm']['algorithm'], alg_oids)
  94. self.assertEqual(
  95. encoded_null, cri_spki['algorithm']['parameters'])
  96. count += 1
  97. substrate = getNextSubstrate[next_layer](asn1Object)
  98. next_layer = getNextLayer[next_layer](asn1Object)
  99. self.assertEqual(5, count)
  100. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  101. if __name__ == '__main__':
  102. result = unittest.TextTestRunner(verbosity=2).run(suite)
  103. sys.exit(not result.wasSuccessful())