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.type import univ 15 16from pyasn1_modules import pem 17from pyasn1_modules import rfc5652 18from pyasn1_modules import rfc5958 19from pyasn1_modules import rfc8410 20 21try: 22 import unittest2 as unittest 23except ImportError: 24 import unittest 25 26 27class PrivateKeyTestCase(unittest.TestCase): 28 priv_key_pem_text = """\ 29MHICAQEwBQYDK2VwBCIEINTuctv5E1hK1bbY8fdp+K06/nwoy/HU++CXqI9EdVhC 30oB8wHQYKKoZIhvcNAQkJFDEPDA1DdXJkbGUgQ2hhaXJzgSEAGb9ECWmEzf6FQbrB 31Z9w7lshQhqowtrbLDFw4rXAxZuE= 32""" 33 34 def setUp(self): 35 self.asn1Spec = rfc5958.PrivateKeyInfo() 36 37 def testDerCodec(self): 38 substrate = pem.readBase64fromText(self.priv_key_pem_text) 39 asn1Object, rest = der_decode(substrate, asn1Spec=self.asn1Spec) 40 assert not rest 41 assert asn1Object.prettyPrint() 42 assert asn1Object['privateKeyAlgorithm']['algorithm'] == rfc8410.id_Ed25519 43 assert asn1Object['privateKey'].isValue 44 assert asn1Object['privateKey'].prettyPrint()[0:10] == "0x0420d4ee" 45 assert asn1Object['publicKey'].isValue 46 assert asn1Object['publicKey'].prettyPrint()[0:10] == "1164575857" 47 assert der_encode(asn1Object) == substrate 48 49 50class PrivateKeyOpenTypesTestCase(unittest.TestCase): 51 asymmetric_key_pkg_pem_text = """\ 52MIGEBgpghkgBZQIBAk4FoHYwdDByAgEBMAUGAytlcAQiBCDU7nLb+RNYStW22PH3 53afitOv58KMvx1Pvgl6iPRHVYQqAfMB0GCiqGSIb3DQEJCRQxDwwNQ3VyZGxlIENo 54YWlyc4EhABm/RAlphM3+hUG6wWfcO5bIUIaqMLa2ywxcOK1wMWbh 55""" 56 57 def setUp(self): 58 self.asn1Spec = rfc5652.ContentInfo() 59 60 def testOpenTypes(self): 61 substrate = pem.readBase64fromText(self.asymmetric_key_pkg_pem_text) 62 asn1Object, rest = der_decode(substrate, 63 asn1Spec=self.asn1Spec, decodeOpenTypes=True) 64 assert not rest 65 assert asn1Object.prettyPrint() 66 assert der_encode(asn1Object) == substrate 67 68 assert rfc5958.id_ct_KP_aKeyPackage in rfc5652.cmsContentTypesMap.keys() 69 oneKey = asn1Object['content'][0] 70 assert oneKey['privateKeyAlgorithm']['algorithm'] == rfc8410.id_Ed25519 71 pkcs_9_at_friendlyName = univ.ObjectIdentifier('1.2.840.113549.1.9.9.20') 72 assert oneKey['attributes'][0]['attrType'] == pkcs_9_at_friendlyName 73 74 75suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 76 77if __name__ == '__main__': 78 import sys 79 80 result = unittest.TextTestRunner(verbosity=2).run(suite) 81 sys.exit(not result.wasSuccessful()) 82