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 rfc5958 17from pyasn1_modules import rfc7914 18from pyasn1_modules import rfc8018 19 20try: 21 import unittest2 as unittest 22 23except ImportError: 24 import unittest 25 26 27# From RFC 7914, Section 13 28 29class MultiprimeRSAPrivateKeyTestCase(unittest.TestCase): 30 pem_text = """\ 31MIHiME0GCSqGSIb3DQEFDTBAMB8GCSsGAQQB2kcECzASBAVNb3VzZQIDEAAAAgEI 32AgEBMB0GCWCGSAFlAwQBKgQQyYmguHMsOwzGMPoyObk/JgSBkJb47EWd5iAqJlyy 33+ni5ftd6gZgOPaLQClL7mEZc2KQay0VhjZm/7MbBUNbqOAXNM6OGebXxVp6sHUAL 34iBGY/Dls7B1TsWeGObE0sS1MXEpuREuloZjcsNVcNXWPlLdZtkSH6uwWzR0PyG/Z 35+ZXfNodZtd/voKlvLOw5B3opGIFaLkbtLZQwMiGtl42AS89lZg== 36""" 37 38 def setUp(self): 39 self.asn1Spec = rfc5958.EncryptedPrivateKeyInfo() 40 41 def testDerCodec(self): 42 substrate = pem.readBase64fromText(self.pem_text) 43 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 44 assert not rest 45 assert asn1Object.prettyPrint() 46 assert der_encode(asn1Object) == substrate 47 48 ea = asn1Object['encryptionAlgorithm'] 49 assert ea['algorithm'] == rfc8018.id_PBES2 50 assert ea['algorithm'] in rfc5280.algorithmIdentifierMap.keys() 51 52 params, rest = der_decode(ea['parameters'], 53 asn1Spec=rfc5280.algorithmIdentifierMap[ea['algorithm']]) 54 assert not rest 55 assert params.prettyPrint() 56 assert der_encode(params) == ea['parameters'] 57 58 kdf = params['keyDerivationFunc'] 59 assert kdf['algorithm'] == rfc7914.id_scrypt 60 assert kdf['algorithm'] in rfc5280.algorithmIdentifierMap.keys() 61 62 kdfp, rest = der_decode(kdf['parameters'], 63 asn1Spec=rfc5280.algorithmIdentifierMap[kdf['algorithm']]) 64 assert not rest 65 assert kdfp.prettyPrint() 66 assert der_encode(kdfp) == kdf['parameters'] 67 68 assert kdfp['costParameter'] == 1048576 69 70 def testOpenTypes(self): 71 substrate = pem.readBase64fromText(self.pem_text) 72 asn1Object, rest = der_decode(substrate, 73 asn1Spec=self.asn1Spec, 74 decodeOpenTypes=True) 75 assert not rest 76 assert asn1Object.prettyPrint() 77 assert der_encode(asn1Object) == substrate 78 79 ea = asn1Object['encryptionAlgorithm'] 80 assert ea['algorithm'] == rfc8018.id_PBES2 81 82 params = asn1Object['encryptionAlgorithm']['parameters'] 83 assert params['keyDerivationFunc']['algorithm'] == rfc7914.id_scrypt 84 85 kdfp = params['keyDerivationFunc']['parameters'] 86 assert kdfp['costParameter'] == 1048576 87 88 89suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 90 91if __name__ == '__main__': 92 import sys 93 94 result = unittest.TextTestRunner(verbosity=2).run(suite) 95 sys.exit(not result.wasSuccessful()) 96