test_rfc6402.py 6.2 KB

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