test_rfc5752.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 rfc5752
  15. class MultipleSignaturesTestCase(unittest.TestCase):
  16. pem_text = """\
  17. MIIKawYJKoZIhvcNAQcCoIIKXDCCClgCAQExGjALBglghkgBZQMEAgEwCwYJYIZI
  18. AWUDBAICMFEGCSqGSIb3DQEHAaBEBEJDb250ZW50LVR5cGU6IHRleHQvcGxhaW4N
  19. Cg0KV2F0c29uLCBjb21lIGhlcmUgLSBJIHdhbnQgdG8gc2VlIHlvdS6gggYmMIIC
  20. eDCCAf6gAwIBAgIJAKWzVCgbsG47MAoGCCqGSM49BAMDMD8xCzAJBgNVBAYTAlVT
  21. MQswCQYDVQQIDAJWQTEQMA4GA1UEBwwHSGVybmRvbjERMA8GA1UECgwIQm9ndXMg
  22. Q0EwHhcNMTkwNTI5MTQ0NTQxWhcNMjAwNTI4MTQ0NTQxWjBwMQswCQYDVQQGEwJV
  23. UzELMAkGA1UECBMCVkExEDAOBgNVBAcTB0hlcm5kb24xEDAOBgNVBAoTB0V4YW1w
  24. bGUxDjAMBgNVBAMTBUFsaWNlMSAwHgYJKoZIhvcNAQkBFhFhbGljZUBleGFtcGxl
  25. LmNvbTB2MBAGByqGSM49AgEGBSuBBAAiA2IABPjNnwc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tnQ==
  73. """
  74. def setUp(self):
  75. self.asn1Spec = rfc5652.SignedAttributes()
  76. def testDerCodec(self):
  77. substrate = pem.readBase64fromText(self.pem_text)
  78. layers = { }
  79. layers.update(rfc5652.cmsContentTypesMap)
  80. getNextLayer = {
  81. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  82. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  83. rfc5652.id_data: lambda x: None
  84. }
  85. getNextSubstrate = {
  86. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  87. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  88. rfc5652.id_data: lambda x: None
  89. }
  90. next_layer = rfc5652.id_ct_contentInfo
  91. while not next_layer == rfc5652.id_data:
  92. asn1Object, rest = der_decoder(
  93. substrate, asn1Spec=layers[next_layer])
  94. self.assertFalse(rest)
  95. self.assertTrue(asn1Object.prettyPrint())
  96. self.assertEqual(substrate, der_encoder(asn1Object))
  97. if next_layer == rfc5652.id_signedData:
  98. signerInfos = asn1Object['signerInfos']
  99. substrate = getNextSubstrate[next_layer](asn1Object)
  100. next_layer = getNextLayer[next_layer](asn1Object)
  101. found_mult_sig1 = False
  102. for attr in signerInfos[0]['signedAttrs']:
  103. if attr['attrType'] in rfc5652.cmsAttributesMap:
  104. av, rest = der_decoder(
  105. attr['attrValues'][0],
  106. asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
  107. self.assertFalse(rest)
  108. self.assertTrue(av.prettyPrint())
  109. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  110. if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
  111. self.assertEqual(
  112. av['bodyHashAlg']['algorithm'], rfc4055.id_sha384)
  113. self.assertEqual(
  114. 'dfaf6c0a',
  115. av['signAttrsHash']['hash'].prettyPrint()[2:10])
  116. found_mult_sig1 = True
  117. found_mult_sig2 = False
  118. for attr in signerInfos[1]['signedAttrs']:
  119. if attr['attrType'] in rfc5652.cmsAttributesMap:
  120. av, rest = der_decoder(
  121. attr['attrValues'][0],
  122. asn1Spec=rfc5652.cmsAttributesMap[attr['attrType']])
  123. self.assertFalse(rest)
  124. self.assertTrue(av.prettyPrint())
  125. self.assertEqual(attr['attrValues'][0], der_encoder(av))
  126. if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
  127. self.assertEqual(
  128. av['bodyHashAlg']['algorithm'], rfc4055.id_sha256)
  129. self.assertEqual(
  130. '7329527d',
  131. av['signAttrsHash']['hash'].prettyPrint()[2:10])
  132. found_mult_sig2 = True
  133. self.assertTrue(found_mult_sig1)
  134. self.assertTrue(found_mult_sig2)
  135. def testOpenTypes(self):
  136. substrate = pem.readBase64fromText(self.pem_text)
  137. asn1Object, rest = der_decoder(
  138. substrate, asn1Spec=rfc5652.ContentInfo(), decodeOpenTypes=True)
  139. self.assertFalse(rest)
  140. self.assertTrue(asn1Object.prettyPrint())
  141. self.assertEqual(substrate, der_encoder(asn1Object))
  142. found_mult_sig1 = False
  143. for attr in asn1Object['content']['signerInfos'][0]['signedAttrs']:
  144. if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
  145. av = attr['attrValues'][0]
  146. self.assertEqual(
  147. av['bodyHashAlg']['algorithm'], rfc4055.id_sha384)
  148. self.assertEqual(
  149. 'dfaf6c0a',
  150. av['signAttrsHash']['hash'].prettyPrint()[2:10])
  151. found_mult_sig1 = True
  152. found_mult_sig2 = False
  153. for attr in asn1Object['content']['signerInfos'][1]['signedAttrs']:
  154. if attr['attrType'] == rfc5752.id_aa_multipleSignatures:
  155. av = attr['attrValues'][0]
  156. self.assertEqual(
  157. av['bodyHashAlg']['algorithm'], rfc4055.id_sha256)
  158. self.assertEqual(
  159. '7329527d',
  160. av['signAttrsHash']['hash'].prettyPrint()[2:10])
  161. found_mult_sig2 = True
  162. self.assertTrue(found_mult_sig1)
  163. self.assertTrue(found_mult_sig2)
  164. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  165. if __name__ == '__main__':
  166. import sys
  167. result = unittest.TextTestRunner(verbosity=2).run(suite)
  168. sys.exit(not result.wasSuccessful())