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 rfc8419 17 18try: 19 import unittest2 as unittest 20except ImportError: 21 import unittest 22 23 24class Ed25519TestCase(unittest.TestCase): 25 alg_id_1_pem_text = "MAUGAytlcA==" 26 27 def setUp(self): 28 self.asn1Spec = rfc5280.AlgorithmIdentifier() 29 30 def testDerCodec(self): 31 substrate = pem.readBase64fromText(self.alg_id_1_pem_text) 32 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 33 assert not rest 34 assert asn1Object.prettyPrint() 35 assert asn1Object['algorithm'] == rfc8419.id_Ed25519 36 assert not asn1Object['parameters'].isValue 37 assert der_encode(asn1Object) == substrate 38 39 40class Ed448TestCase(unittest.TestCase): 41 alg_id_2_pem_text = "MAUGAytlcQ==" 42 43 def setUp(self): 44 self.asn1Spec = rfc5280.AlgorithmIdentifier() 45 46 def testDerCodec(self): 47 substrate = pem.readBase64fromText(self.alg_id_2_pem_text) 48 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 49 assert not rest 50 assert asn1Object.prettyPrint() 51 assert asn1Object['algorithm'] == rfc8419.id_Ed448 52 assert not asn1Object['parameters'].isValue 53 assert der_encode(asn1Object) == substrate 54 55 56class SHA512TestCase(unittest.TestCase): 57 alg_id_3_pem_text = "MAsGCWCGSAFlAwQCAw==" 58 59 def setUp(self): 60 self.asn1Spec = rfc5280.AlgorithmIdentifier() 61 62 def testDerCodec(self): 63 substrate = pem.readBase64fromText(self.alg_id_3_pem_text) 64 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 65 assert not rest 66 assert asn1Object.prettyPrint() 67 assert asn1Object['algorithm'] == rfc8419.id_sha512 68 assert not asn1Object['parameters'].isValue 69 assert der_encode(asn1Object) == substrate 70 71 72class SHAKE256TestCase(unittest.TestCase): 73 alg_id_4_pem_text = "MAsGCWCGSAFlAwQCDA==" 74 75 def setUp(self): 76 self.asn1Spec = rfc5280.AlgorithmIdentifier() 77 78 def testDerCodec(self): 79 substrate = pem.readBase64fromText(self.alg_id_4_pem_text) 80 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 81 assert not rest 82 assert asn1Object.prettyPrint() 83 assert asn1Object['algorithm'] == rfc8419.id_shake256 84 assert not asn1Object['parameters'].isValue 85 assert der_encode(asn1Object) == substrate 86 87 88class SHAKE256LENTestCase(unittest.TestCase): 89 alg_id_5_pem_text = "MA8GCWCGSAFlAwQCEgICAgA=" 90 91 def setUp(self): 92 self.asn1Spec = rfc5280.AlgorithmIdentifier() 93 94 def testDerCodec(self): 95 substrate = pem.readBase64fromText(self.alg_id_5_pem_text) 96 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 97 assert not rest 98 assert asn1Object.prettyPrint() 99 assert asn1Object['algorithm'] == rfc8419.id_shake256_len 100 assert asn1Object['parameters'].isValue 101 assert der_encode(asn1Object) == substrate 102 103 param, rest = der_decode(asn1Object['parameters'], 104 asn1Spec=rfc5280.algorithmIdentifierMap[asn1Object['algorithm']]) 105 assert not rest 106 assert param.prettyPrint() 107 assert der_encode(param) == asn1Object['parameters'] 108 assert param == 512 109 110 def testOpenTypes(self): 111 substrate = pem.readBase64fromText(self.alg_id_5_pem_text) 112 asn1Object, rest = der_decode(substrate, 113 asn1Spec=self.asn1Spec, 114 decodeOpenTypes=True) 115 assert not rest 116 assert asn1Object.prettyPrint() 117 assert asn1Object['algorithm'] == rfc8419.id_shake256_len 118 assert asn1Object['parameters'] == 512 119 assert der_encode(asn1Object) == substrate 120 121 122suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 123 124if __name__ == '__main__': 125 import sys 126 127 result = unittest.TextTestRunner(verbosity=2).run(suite) 128 sys.exit(not result.wasSuccessful()) 129