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
9import sys
10
11from pyasn1.codec.der.decoder import decode as der_decode
12from pyasn1.codec.der.encoder import encode as der_encode
13
14from pyasn1_modules import pem
15from pyasn1_modules import rfc5280
16from pyasn1_modules import rfc5480
17
18try:
19    import unittest2 as unittest
20except ImportError:
21    import unittest
22
23
24class ECCertTestCase(unittest.TestCase):
25    digicert_ec_cert_pem_text = """\
26MIIDrDCCApSgAwIBAgIQCssoukZe5TkIdnRw883GEjANBgkqhkiG9w0BAQwFADBh
27MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMRkwFwYDVQQLExB3
28d3cuZGlnaWNlcnQuY29tMSAwHgYDVQQDExdEaWdpQ2VydCBHbG9iYWwgUm9vdCBD
29QTAeFw0xMzAzMDgxMjAwMDBaFw0yMzAzMDgxMjAwMDBaMEwxCzAJBgNVBAYTAlVT
30MRUwEwYDVQQKEwxEaWdpQ2VydCBJbmMxJjAkBgNVBAMTHURpZ2lDZXJ0IEVDQyBT
31ZWN1cmUgU2VydmVyIENBMHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE4ghC6nfYJN6g
32LGSkE85AnCNyqQIKDjc/ITa4jVMU9tWRlUvzlgKNcR7E2Munn17voOZ/WpIRllNv
3368DLP679Wz9HJOeaBy6Wvqgvu1cYr3GkvXg6HuhbPGtkESvMNCuMo4IBITCCAR0w
34EgYDVR0TAQH/BAgwBgEB/wIBADAOBgNVHQ8BAf8EBAMCAYYwNAYIKwYBBQUHAQEE
35KDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5jb20wQgYDVR0f
36BDswOTA3oDWgM4YxaHR0cDovL2NybDMuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0R2xv
37YmFsUm9vdENBLmNybDA9BgNVHSAENjA0MDIGBFUdIAAwKjAoBggrBgEFBQcCARYc
38aHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzAdBgNVHQ4EFgQUo53mH/naOU/A
39buiRy5Wl2jHiCp8wHwYDVR0jBBgwFoAUA95QNVbRTLtm8KPiGxvDl7I90VUwDQYJ
40KoZIhvcNAQEMBQADggEBAMeKoENL7HTJxavVHzA1Nm6YVntIrAVjrnuaVyRXzG/6
413qttnMe2uuzO58pzZNvfBDcKAEmzP58mrZGMIOgfiA4q+2Y3yDDo0sIkp0VILeoB
42UEoxlBPfjV/aKrtJPGHzecicZpIalir0ezZYoyxBEHQa0+1IttK7igZFcTMQMHp6
43mCHdJLnsnLWSB62DxsRq+HfmNb4TDydkskO/g+l3VtsIh5RHFPVfKK+jaEyDj2D3
44loB5hWp2Jp2VDCADjT7ueihlZGak2YPqmXTNbk19HOuNssWvFhtOyPNV6og4ETQd
45Ea8/B6hPatJ0ES8q/HO3X8IVQwVs1n3aAr0im0/T+Xc=
46"""
47
48    def setUp(self):
49        self.asn1Spec = rfc5280.Certificate()
50
51    def testDerCodec(self):
52        substrate = pem.readBase64fromText(self.digicert_ec_cert_pem_text)
53        asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec)
54        assert not rest
55        assert asn1Object.prettyPrint()
56        assert der_encode(asn1Object) == substrate
57
58        algid = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
59        assert algid['algorithm'] == rfc5480.id_ecPublicKey
60        param, rest = der_decode(algid['parameters'], asn1Spec=rfc5480.ECParameters())
61        assert param.prettyPrint()
62        assert param['namedCurve'] == rfc5480.secp384r1
63
64    def testOpenTypes(self):
65        substrate = pem.readBase64fromText(self.digicert_ec_cert_pem_text)
66        asn1Object, rest = der_decode(substrate,
67            asn1Spec=self.asn1Spec, decodeOpenTypes=True)
68        assert not rest
69        assert asn1Object.prettyPrint()
70        assert der_encode(asn1Object) == substrate
71
72        spki_alg = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
73        assert spki_alg['algorithm'] == rfc5480.id_ecPublicKey
74        assert spki_alg['parameters']['namedCurve'] == rfc5480.secp384r1
75
76
77suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__])
78
79if __name__ == '__main__':
80    import sys
81
82    result = unittest.TextTestRunner(verbosity=2).run(suite)
83    sys.exit(not result.wasSuccessful())
84