# # This file is part of pyasn1-modules software. # # Created by Russ Housley # Copyright (c) 2019, Vigil Security, LLC # License: http://snmplabs.com/pyasn1/license.html # import sys import unittest from pyasn1.codec.der.decoder import decode as der_decoder from pyasn1.codec.der.encoder import encode as der_encoder from pyasn1_modules import pem from pyasn1_modules import rfc3114 from pyasn1_modules import rfc5035 from pyasn1_modules import rfc5083 from pyasn1_modules import rfc5280 from pyasn1_modules import rfc5652 from pyasn1_modules import rfc5755 class SecurityLabelTestCase(unittest.TestCase): pem_text = """\ MIITHAYJKoZIhvcNAQcCoIITDTCCEwkCAQMxDTALBglghkgBZQMEAgIwggeUBgsq hkiG9w0BCRABF6CCB4MEggd/MIIHewIBADGCAk8wggJLAgEAMDMwJjEUMBIGA1UE CgwLZXhhbXBsZS5jb20xDjAMBgNVBAMMBUFsaWNlAgkAg/ULtwvVxA4wDQYJKoZI hvcNAQEBBQAEggIAdZphtN3x8a8kZoAFY15HYRD6JyPBueRUhLbTPoOH3pZ9xeDK +zVXGlahl1y1UOe+McEx2oD7cxAkhFuruNZMrCYEBCTZMwVhyEOZlBXdZEs8rZUH L3FFE5PJnygsSIO9DMxd1UuTFGTgCm5V5ZLFGmjeEGJRbsfTyo52S7iseJqIN3dl 743DbApu0+yuUoXKxqKdUFlEVxmhvc+Qbg/zfiwu8PTsYiUQDMBi4cdIlju8iLjj 389xQHNyndXHWD51is89GG8vpBe+IsN8mnbGtCcpqtJ/c65ErJhHTR7rSJSMEqQD 0LPOCKIY1q9FaSSJfMXJZk9t/rPxgUEVjfw7hAkKpgOAqoZRN+FpnFyBl0FnnXo8 kLp55tfVyNibtUpmdCPkOwt9b3jAtKtnvDQ2YqY1/llfEUnFOVDKwuC6MYwifm92 qNlAQA/T0+ocjs6gA9zOLx+wD1zqM13hMD/L+T2OHL/WgvGb62JLrNHXuPWA8RSh O4kIlPtARKXap2S3+MX/kpSUUrNa65Y5uK1jwFFclczG+CPCIBBn6iJiQT/vOX1I 97YUP4Qq6OGkjK064Bq6o8+e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SqGSIb3DQEJBTEPFw0xOTExMDgyMDA4 MzFaMD8GCSqGSIb3DQEJBDEyBDCd5WyvIB0VdXgPBWPtI152MIJLg5o68IRimCXx bVY0j3YyAKbi0egiZ/UunkyCfv0wZQYLKoZIhvcNAQkQAgIxVjFUAgEIBgsqhkiG 9w0BCRAHAzEtMCuACyqGSIb3DQEJEAcEgRwwGgwYSFVNQU4gUkVTT1VSQ0VTIFVT RSBPTkxZExNCb2FndXMgUHJpdmFjeSBNYXJrMAoGCCqGSM49BAMDBGcwZQIwWkD7 03QoNrKL5HJnuGJqvML1KlUXZDHnFpnJ+QMzXi8gocyfpRXWm6h0NjXieE0XAjEA uuDSOoaUIz+G9aemAE0ldpo1c0avNGa7BtynUTHmwosD6Sjfj0epAg9OnMedOjbr """ def testDerCodec(self): layers = { } layers.update(rfc5652.cmsContentTypesMap) getNextLayer = { rfc5652.id_ct_contentInfo: lambda x: x['contentType'], rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContentType'], rfc5083.id_ct_authEnvelopedData: lambda x: None } getNextSubstrate = { rfc5652.id_ct_contentInfo: lambda x: x['content'], rfc5652.id_signedData: lambda x: x['encapContentInfo']['eContent'], rfc5083.id_ct_authEnvelopedData: lambda x: None } substrate = pem.readBase64fromText(self.pem_text) next_layer = rfc5652.id_ct_contentInfo while next_layer: asn1Object, rest = der_decoder(substrate, asn1Spec=layers[next_layer]) self.assertFalse(rest) self.assertTrue(asn1Object.prettyPrint()) self.assertEqual(substrate, der_encoder(asn1Object)) if next_layer == rfc5652.id_signedData: attrs = asn1Object['signerInfos'][0]['signedAttrs'] certs = asn1Object['certificates'] substrate = getNextSubstrate[next_layer](asn1Object) next_layer = getNextLayer[next_layer](asn1Object) spid = rfc3114.id_tsp_TEST_Whirlpool catid = rfc3114.id_tsp_TEST_Whirlpool_Categories conf = rfc3114.Whirlpool_SecurityClassification(value='whirlpool-confidential') self.assertIn(catid, rfc5755.securityCategoryMap) self.assertIn(rfc5755.id_at_clearance, rfc5280.certificateAttributesMap) self.assertIn(rfc5280.id_ce_subjectDirectoryAttributes, rfc5280.certificateExtensionsMap) security_label_okay = False for attr in attrs: if attr['attrType'] == rfc5035.id_aa_securityLabel: esssl, rest = der_decoder( attr['attrValues'][0], asn1Spec=rfc5035.ESSSecurityLabel()) self.assertFalse(rest) self.assertTrue(esssl.prettyPrint()) self.assertEqual(attr['attrValues'][0], der_encoder(esssl)) self.assertEqual(spid, esssl['security-policy-identifier']) self.assertEqual(conf, esssl['security-classification']) for cat in esssl['security-categories']: if cat['type'] == catid: scv, rest = der_decoder( cat['value'], asn1Spec=rfc3114.SecurityCategoryValues()) self.assertFalse(rest) self.assertTrue(scv.prettyPrint()) self.assertEqual(cat['value'], der_encoder(scv)) for scv_str in scv: self.assertIn('USE ONLY', scv_str) security_label_okay = True self.assertTrue(security_label_okay) clearance_okay = False for cert_choice in certs: for extn in cert_choice['certificate']['tbsCertificate']['extensions']: if extn['extnID'] == rfc5280.id_ce_subjectDirectoryAttributes: ev, rest = der_decoder( extn['extnValue'], asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']]) self.assertFalse(rest) self.assertTrue(ev.prettyPrint()) self.assertEqual(extn['extnValue'], der_encoder(ev)) for attr in ev: if attr['type'] == rfc5755.id_at_clearance: av, rest = der_decoder( attr['values'][0], asn1Spec=rfc5280.certificateAttributesMap[attr['type']]) self.assertEqual(spid, av['policyId']) for cat in av['securityCategories']: self.assertEqual(catid, cat['type']) scv, rest = der_decoder( cat['value'], asn1Spec=rfc5755.securityCategoryMap[cat['type']]) self.assertFalse(rest) self.assertTrue(scv.prettyPrint()) self.assertEqual(cat['value'], der_encoder(scv)) for scv_str in scv: self.assertIn('USE ONLY', scv_str) clearance_okay = True self.assertTrue(clearance_okay) suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) if __name__ == '__main__': result = unittest.TextTestRunner(verbosity=2).run(suite) sys.exit(not result.wasSuccessful())