test_rfc3114.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_modules import pem
  13. from pyasn1_modules import rfc3114
  14. from pyasn1_modules import rfc5035
  15. from pyasn1_modules import rfc5083
  16. from pyasn1_modules import rfc5280
  17. from pyasn1_modules import rfc5652
  18. from pyasn1_modules import rfc5755
  19. class SecurityLabelTestCase(unittest.TestCase):
  20. pem_text = """\
  21. MIITHAYJKoZIhvcNAQcCoIITDTCCEwkCAQMxDTALBglghkgBZQMEAgIwggeUBgsq
  22. hkiG9w0BCRABF6CCB4MEggd/MIIHewIBADGCAk8wggJLAgEAMDMwJjEUMBIGA1UE
  23. CgwLZXhhbXBsZS5jb20xDjAMBgNVBAMMBUFsaWNlAgkAg/ULtwvVxA4wDQYJKoZI
  24. hvcNAQEBBQAEggIAdZphtN3x8a8kZoAFY15HYRD6JyPBueRUhLbTPoOH3pZ9xeDK
  25. +zVXGlahl1y1UOe+McEx2oD7cxAkhFuruNZMrCYEBCTZMwVhyEOZlBXdZEs8rZUH
  26. L3FFE5PJnygsSIO9DMxd1UuTFGTgCm5V5ZLFGmjeEGJRbsfTyo52S7iseJqIN3dl
  27. 743DbApu0+yuUoXKxqKdUFlEVxmhvc+Qbg/zfiwu8PTsYiUQDMBi4cdIlju8iLjj
  28. 389xQ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rvTTV0c318TcHpN3
  44. DY9PIt6mHXMIPDLEA4wes90zg6iah5XiQcLtfLaAdYwEEGlImGD8n0kOhSNgclSL
  45. Mklpj5mVOs8exli3qoXlVMRJcBptSwOe0QPcRY30spywS4zt1UDIQ0jaecGGVtUY
  46. j586nkubhAxwZkuQKWxgt6yYTpGNSKCdvd+ygfyGJRDbWdn6nck/EPnG1773KTHR
  47. hMrXrBPBpSlfyJ/ju3644CCFqCjFoTh4bmB63k9ejUEVkJIJuoeKeTBaUxbCIink
  48. K4htBkgchHP51RJp4q9jQbziD3aOhg13hO1GFQ4E/1DNIJxbEnURNp/ga8SqmnLY
  49. 8f5Pzwhm1mSzZf+obowbQ+epISrswWyjUKKO+uJfrAVN2TS/5+X6T3U6pBWWjH6+
  50. xDngrAJwtIdKBo0iSEwJ2eir4X8TcrSy9l8RSOiTPtqS5dF3RWSWOzkcO72fHCf/
  51. 42+DLgUVX8Oe5mUvp7QYiXXsXGezLJ8hPIrGuOEypafDv3TwFkBc2MIB0QUhk+GG
  52. 1ENY3jiNcyEbovF5Lzz+ubvechHSb1arBuEczJzN4riM2Dc3c+r8N/2Ft6eivK7H
  53. UuYX1uAcArhunZpA8yBGLF1m+DUXFtzWAUvfMKYPdfwGMckghF7YwLrTXd8ZhPIk
  54. HNO1KdwQKIRfgIlUPfTxRB7eNrG/Ma9a/IwrcI1QtkXU59uIZIw+7+FHZRWPsOjT
  55. u1Pdy+JtcSTG4dmS+DIwqpUzdu6MaBCVaOhXHwybvaSPTfMG/nR/NxF1FI8xgydn
  56. zXZs8HtFDL9iytKnvXHx+IIz8Rahp/PK8S80vPQNIeef/JgnIhtosID/A614LW1t
  57. B4cWdveYlD5U8T/XXInAtCY78Q9WJD+ecu87OJmlOdmjrFvitpQAo8+NGWxc7Wl7
  58. LtgDuYel7oXFCVtI2npbA7R+K5/kzUvDCY6GTgzn1Gfamc1/Op6Ue17qd/emvhbI
  59. x+ng3swf8TJVnCNDIXucKVA4boXSlCEhCGzfoZZY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AjEA
  122. uuDSOoaUIz+G9aemAE0ldpo1c0avNGa7BtynUTHmwosD6Sjfj0epAg9OnMedOjbr
  123. """
  124. def testDerCodec(self):
  125. layers = { }
  126. layers.update(rfc5652.cmsContentTypesMap)
  127. getNextLayer = {
  128. rfc5652.id_ct_contentInfo: lambda x: x['contentType'],
  129. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'],
  130. rfc5083.id_ct_authEnvelopedData: lambda x: None
  131. }
  132. getNextSubstrate = {
  133. rfc5652.id_ct_contentInfo: lambda x: x['content'],
  134. rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'],
  135. rfc5083.id_ct_authEnvelopedData: lambda x: None
  136. }
  137. substrate = pem.readBase64fromText(self.pem_text)
  138. next_layer = rfc5652.id_ct_contentInfo
  139. while next_layer:
  140. asn1Object, rest = der_decoder(substrate, asn1Spec=layers[next_layer])
  141. self.assertFalse(rest)
  142. self.assertTrue(asn1Object.prettyPrint())
  143. self.assertEqual(substrate, der_encoder(asn1Object))
  144. if next_layer == rfc5652.id_signedData:
  145. attrs = asn1Object['signerInfos'][0]['signedAttrs']
  146. certs = asn1Object['certificates']
  147. substrate = getNextSubstrate[next_layer](asn1Object)
  148. next_layer = getNextLayer[next_layer](asn1Object)
  149. spid = rfc3114.id_tsp_TEST_Whirlpool
  150. catid = rfc3114.id_tsp_TEST_Whirlpool_Categories
  151. conf = rfc3114.Whirlpool_SecurityClassification(value='whirlpool-confidential')
  152. self.assertIn(catid, rfc5755.securityCategoryMap)
  153. self.assertIn(rfc5755.id_at_clearance, rfc5280.certificateAttributesMap)
  154. self.assertIn(rfc5280.id_ce_subjectDirectoryAttributes, rfc5280.certificateExtensionsMap)
  155. security_label_okay = False
  156. for attr in attrs:
  157. if attr['attrType'] == rfc5035.id_aa_securityLabel:
  158. esssl, rest = der_decoder(
  159. attr['attrValues'][0], asn1Spec=rfc5035.ESSSecurityLabel())
  160. self.assertFalse(rest)
  161. self.assertTrue(esssl.prettyPrint())
  162. self.assertEqual(attr['attrValues'][0], der_encoder(esssl))
  163. self.assertEqual(spid, esssl['security-policy-identifier'])
  164. self.assertEqual(conf, esssl['security-classification'])
  165. for cat in esssl['security-categories']:
  166. if cat['type'] == catid:
  167. scv, rest = der_decoder(
  168. cat['value'], asn1Spec=rfc3114.SecurityCategoryValues())
  169. self.assertFalse(rest)
  170. self.assertTrue(scv.prettyPrint())
  171. self.assertEqual(cat['value'], der_encoder(scv))
  172. for scv_str in scv:
  173. self.assertIn('USE ONLY', scv_str)
  174. security_label_okay = True
  175. self.assertTrue(security_label_okay)
  176. clearance_okay = False
  177. for cert_choice in certs:
  178. for extn in cert_choice['certificate']['tbsCertificate']['extensions']:
  179. if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes:
  180. ev, rest = der_decoder(
  181. extn['extnValue'],
  182. asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
  183. self.assertFalse(rest)
  184. self.assertTrue(ev.prettyPrint())
  185. self.assertEqual(extn['extnValue'], der_encoder(ev))
  186. for attr in ev:
  187. if attr['type'] == rfc5755.id_at_clearance:
  188. av, rest = der_decoder(
  189. attr['values'][0],
  190. asn1Spec=rfc5280.certificateAttributesMap[attr['type']])
  191. self.assertEqual(spid, av['policyId'])
  192. for cat in av['securityCategories']:
  193. self.assertEqual(catid, cat['type'])
  194. scv, rest = der_decoder(
  195. cat['value'],
  196. asn1Spec=rfc5755.securityCategoryMap[cat['type']])
  197. self.assertFalse(rest)
  198. self.assertTrue(scv.prettyPrint())
  199. self.assertEqual(cat['value'], der_encoder(scv))
  200. for scv_str in scv:
  201. self.assertIn('USE ONLY', scv_str)
  202. clearance_okay = True
  203. self.assertTrue(clearance_okay)
  204. suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
  205. if __name__ == '__main__':
  206. result = unittest.TextTestRunner(verbosity=2).run(suite)
  207. sys.exit(not result.wasSuccessful())