test_rfc5652.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #
  2. # This file is part of pyasn1-modules software.
  3. #
  4. # Copyright (c) 2005-2020, Ilya Etingof <etingof@gmail.com>
  5. # License: http://snmplabs.com/pyasn1/license.html
  6. #
  7. import sys
  8. import unittest
  9. from pyasn1.codec.der import decoder as der_decoder
  10. from pyasn1.codec.der import encoder as der_encoder
  11. from pyasn1.type import char
  12. from pyasn1.type import namedtype
  13. from pyasn1.type import univ
  14. from pyasn1_modules import pem
  15. from pyasn1_modules import rfc5652
  16. from pyasn1_modules import rfc6402
  17. class ContentInfoTestCase(unittest.TestCase):
  18. pem_text = """\
  19. MIIEJQYJKoZIhvcNAQcCoIIEFjCCBBICAQMxCzAJBgUrDgMCGgUAMIIDAgYIKwYBBQUHDAKgggL0
  20. BIIC8DCCAuwweDB2AgECBgorBgEEAYI3CgoBMWUwYwIBADADAgEBMVkwVwYJKwYBBAGCNxUUMUow
  21. SAIBBQwZcGl0dWNoYTEuZW1lYS5ocHFjb3JwLm5ldAwMRU1FQVxwaXR1Y2hhDBpDTUNSZXFHZW5l
  22. cmF0b3IudnNob3N0LmV4ZTCCAmqgggJmAgEBMIICXzCCAcgCAQAwADCBnzANBgkqhkiG9w0BAQEF
  23. AAOBjQAwgYkCgYEA0jm7SSSm2wyEAzuNKtFZFJKo91SrJq9wQwEhEKHDavZwMQOm1rZ2PF8NWCEb
  24. PqrhToQ7rtiGLSZa4dF4bzgmBqQ9aoSfEX4jISt31Vy+skHidXjHHpbsjT24NPhrZgANivL7CxD6
  25. Ft+s7qS1gL4HRm2twQkqSwOLrE/q2QeXl2UCAwEAAaCCAR0wGgYKKwYBBAGCNw0CAzEMFgo2LjIu
  26. OTIwMC4yMD4GCSqGSIb3DQEJDjExMC8wHQYDVR0OBBYEFMW2skn88gxhONWZQA4sWGBDb68yMA4G
  27. A1UdDwEB/wQEAwIHgDBXBgkrBgEEAYI3FRQxSjBIAgEFDBlwaXR1Y2hhMS5lbWVhLmhwcWNvcnAu
  28. bmV0DAxFTUVBXHBpdHVjaGEMGkNNQ1JlcUdlbmVyYXRvci52c2hvc3QuZXhlMGYGCisGAQQBgjcN
  29. AgIxWDBWAgECHk4ATQBpAGMAcgBvAHMAbwBmAHQAIABTAHQAcgBvAG4AZwAgAEMAcgB5AHAAdABv
  30. AGcAcgBhAHAAaABpAGMAIABQAHIAbwB2AGkAZABlAHIDAQAwDQYJKoZIhvcNAQEFBQADgYEAJZlu
  31. mxjtCxSOQi27jsVdd3y8NSIlzNv0b3LqmzvAly6L+CstXcnuG2MPQqPH9R7tbJonGUniBQO9sQ7C
  32. KhYWj2gfhiEkSID82lV5chINVUFKoUlSiEhWr0tPGgvOaqdsKQcrHfzrsBbFkhDqrFSVy7Yivbnh
  33. qYszKrOjJKiiCPMwADAAMYH5MIH2AgEDgBTFtrJJ/PIMYTjVmUAOLFhgQ2+vMjAJBgUrDgMCGgUA
  34. oD4wFwYJKoZIhvcNAQkDMQoGCCsGAQUFBwwCMCMGCSqGSIb3DQEJBDEWBBTFTkK/OifaFjwqHiJu
  35. xM7qXcg/VzANBgkqhkiG9w0BAQEFAASBgKfC6jOi1Wgy4xxDCQVK9+e5tktL8wE/j2cb9JSqq+aU
  36. 5UxEgXEw7q7BoYZCAzcxMRriGzakXr8aXHcgkRJ7XcFvLPUjpmGg9SOZ2sGW4zQdWAwImN/i8loc
  37. xicQmJP+VoMHo/ZpjFY9fYCjNZUArgKsEwK/s+p9yrVVeB1Nf8Mn
  38. """
  39. def setUp(self):
  40. self.asn1Spec = rfc5652.ContentInfo()
  41. def testDerCodec(self):
  42. substrate = pem.readBase64fromText(self.pem_text)
  43. layers = {
  44. rfc5652.id_ct_contentInfo: rfc5652.ContentInfo(),
  45. rfc5652.id_signedData: rfc5652.SignedData(),
  46. rfc6402.id_cct_PKIData: rfc6402.PKIData()
  47. }
  48. getNextLayer = {
  49. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  50. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  51. rfc6402.id_cct_PKIData: lambda x: None
  52. }
  53. getNextSubstrate = {
  54. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  55. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  56. rfc6402.id_cct_PKIData: lambda x: None
  57. }
  58. next_layer = rfc5652.id_ct_contentInfo
  59. while next_layer:
  60. asn1Object, rest = der_decoder.decode(
  61. substrate, asn1Spec=layers[next_layer]
  62. )
  63. self.assertFalse(rest)
  64. self.assertTrue(asn1Object.prettyPrint())
  65. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  66. substrate = getNextSubstrate[next_layer](asn1Object)
  67. next_layer = getNextLayer[next_layer](asn1Object)
  68. def testOpenTypes(self):
  69. class ClientInformation(univ.Sequence):
  70. pass
  71. ClientInformation.componentType = namedtype.NamedTypes(
  72. namedtype.NamedType('clientId', univ.Integer()),
  73. namedtype.NamedType('MachineName', char.UTF8String()),
  74. namedtype.NamedType('UserName', char.UTF8String()),
  75. namedtype.NamedType('ProcessName', char.UTF8String())
  76. )
  77. class EnrollmentCSP(univ.Sequence):
  78. pass
  79. EnrollmentCSP.componentType = namedtype.NamedTypes(
  80. namedtype.NamedType('KeySpec', univ.Integer()),
  81. namedtype.NamedType('Name', char.BMPString()),
  82. namedtype.NamedType('Signature', univ.BitString())
  83. )
  84. openTypeMap = {
  85. # attributes
  86. univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.3'): char.IA5String(),
  87. univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.2'): EnrollmentCSP(),
  88. univ.ObjectIdentifier('1.3.6.1.4.1.311.21.20'): ClientInformation(),
  89. # algorithm identifier parameters
  90. univ.ObjectIdentifier('1.2.840.113549.1.1.1'): univ.Null(""),
  91. univ.ObjectIdentifier('1.2.840.113549.1.1.5'): univ.Null(""),
  92. univ.ObjectIdentifier('1.2.840.113549.1.1.11'): univ.Null(""),
  93. }
  94. openTypeMap.update(rfc5652.cmsAttributesMap)
  95. openTypeMap.update(rfc6402.cmcControlAttributesMap)
  96. substrate = pem.readBase64fromText(self.pem_text)
  97. asn1Object, rest = der_decoder.decode(substrate,
  98. asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)
  99. self.assertFalse(rest)
  100. self.assertTrue(asn1Object.prettyPrint())
  101. self.assertEqual(substrate, der_encoder.encode(asn1Object))
  102. eci = asn1Object['content']['encapContentInfo']
  103. self.assertIn(eci['eContentType'], rfc5652.cmsContentTypesMap)
  104. self.assertEqual(rfc6402.id_cct_PKIData, eci['eContentType'])
  105. pkid, rest = der_decoder.decode(eci['eContent'],
  106. asn1Spec=rfc5652.cmsContentTypesMap[eci['eContentType']],
  107. openTypes=openTypeMap,
  108. decodeOpenTypes=True)
  109. self.assertFalse(rest)
  110. self.assertTrue(pkid.prettyPrint())
  111. self.assertEqual(eci['eContent'], der_encoder.encode(pkid))
  112. for req in pkid['reqSequence']:
  113. cr = req['tcr']['certificationRequest']
  114. sig_alg = cr['signatureAlgorithm']
  115. self.assertIn(sig_alg['algorithm'], openTypeMap)
  116. self.assertEqual(univ.Null(""), sig_alg['parameters'])
  117. cri = cr['certificationRequestInfo']
  118. spki_alg = cri['subjectPublicKeyInfo']['algorithm']
  119. self.assertIn( spki_alg['algorithm'], openTypeMap)
  120. self.assertEqual(univ.Null(""), spki_alg['parameters'])
  121. attrs = cr['certificationRequestInfo']['attributes']
  122. for attr in attrs:
  123. self.assertIn(attr['attrType'], openTypeMap)
  124. if attr['attrType'] == univ.ObjectIdentifier('1.3.6.1.4.1.311.13.2.3'):
  125. self.assertEqual("6.2.9200.2", attr['attrValues'][0])
  126. else:
  127. self.assertTrue(attr['attrValues'][0].hasValue())
  128. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  129. if __name__ == '__main__':
  130. result = unittest.TextTestRunner(verbosity=2).run(suite)
  131. sys.exit(not result.wasSuccessful())