#
# This file is part of pyasn1-modules software.
#
# Created by <NAME>
# Copyright (c) 2021, 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.type import univ

from pyasn1_modules import pem
from pyasn1_modules import rfc5480
from pyasn1_modules import rfc5280
from pyasn1_modules import rfc4262


class SMIMECapExtnTestCase(unittest.TestCase):
    pem_text = """\
<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc5280.Certificate()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for extn in asn1Object['tbsCertificate']['extensions']:
           if extn['extnID'] == rfc4262.smimeCapabilities:
               self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
               scap, rest = der_decoder(extn['extnValue'],
                   asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
               self.assertFalse(rest)
               self.assertTrue(scap.prettyPrint())
               self.assertEqual(extn['extnValue'], der_encoder(scap))

               for cap in scap:
                    if cap['capabilityID'] == rfc5480.id_sha256:
                        self.assertTrue(cap['parameters'].hasValue())
                        self.assertEqual(der_encoder(univ.Null("")),
                            cap['parameters'])
                        found = True

        self.assertTrue(found)


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)

#
# This file is part of pyasn1-modules software.
#
# Created by <NAME>
# Copyright (c) 2021, 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.type import univ

from pyasn1_modules import pem
from pyasn1_modules import rfc5480
from pyasn1_modules import rfc5280
from pyasn1_modules import rfc4262


class SMIMECapExtnTestCase(unittest.TestCase):
    pem_text = """\
<KEY>
"""

    def setUp(self):
        self.asn1Spec = rfc5280.Certificate()

    def testDerCodec(self):
        substrate = pem.readBase64fromText(self.pem_text)
        asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)

        self.assertFalse(rest)
        self.assertTrue(asn1Object.prettyPrint())
        self.assertEqual(substrate, der_encoder(asn1Object))

        found = False
        for extn in asn1Object['tbsCertificate']['extensions']:
           if extn['extnID'] == rfc4262.smimeCapabilities:
               self.assertIn(extn['extnID'], rfc5280.certificateExtensionsMap)
               scap, rest = der_decoder(extn['extnValue'],
                   asn1Spec=rfc5280.certificateExtensionsMap[extn['extnID']])
               self.assertFalse(rest)
               self.assertTrue(scap.prettyPrint())
               self.assertEqual(extn['extnValue'], der_encoder(scap))

               for cap in scap:
                    if cap['capabilityID'] == rfc5480.id_sha256:
                        self.assertTrue(cap['parameters'].hasValue())
                        self.assertEqual(der_encoder(univ.Null("")),
                            cap['parameters'])
                        found = True

        self.assertTrue(found)


suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(suite)

# # This file is part of pyasn1-modules software. # # Created by <NAME> # Copyright (c) 2021, Vigil Security, LLC # License: http://snmplabs.com/pyasn1/license.html # \ <KEY>