1# coding: utf-8 2from __future__ import unicode_literals, division, absolute_import, print_function 3 4import unittest 5import os 6import sys 7 8from asn1crypto import pkcs12 9from ._unittest_compat import patch 10 11patch() 12 13if sys.version_info < (3,): 14 byte_cls = str 15else: 16 byte_cls = bytes 17 18tests_root = os.path.dirname(__file__) 19fixtures_dir = os.path.join(tests_root, 'fixtures') 20 21 22class PKCS12Tests(unittest.TestCase): 23 24 def test_parse_pfx(self): 25 with open(os.path.join(fixtures_dir, 'test-tripledes.p12'), 'rb') as f: 26 info = pkcs12.Pfx.load(f.read()) 27 28 self.assertEqual( 29 'v3', 30 info['version'].native 31 ) 32 33 auth_safe = info['auth_safe'] 34 35 self.assertEqual( 36 'data', 37 auth_safe['content_type'].native 38 ) 39 40 self.assertEqual( 41 2, 42 len(info.authenticated_safe) 43 ) 44 45 for i, content_info in enumerate(info.authenticated_safe): 46 if i == 0: 47 self.assertEqual( 48 'encrypted_data', 49 content_info['content_type'].native 50 ) 51 else: 52 self.assertEqual( 53 'data', 54 content_info['content_type'].native 55 ) 56 safe_contents = pkcs12.SafeContents.load(content_info['content'].native) 57 self.assertEqual( 58 1, 59 len(safe_contents) 60 ) 61 bag_attributes = safe_contents[0]['bag_attributes'] 62 self.assertEqual( 63 2, 64 len(bag_attributes) 65 ) 66 self.assertEqual( 67 'local_key_id', 68 bag_attributes[0]['type'].native 69 ) 70 self.assertEqual( 71 [b'\x95\xd7\xcf\xd7&\x80\x02\x94Q\xc2}X\xee\xd7\x9eiQ\xc0\x10P'], 72 bag_attributes[0]['values'].native 73 ) 74 self.assertEqual( 75 'friendly_name', 76 bag_attributes[1]['type'].native 77 ) 78 self.assertEqual( 79 ['PKCS#12 Test'], 80 bag_attributes[1]['values'].native 81 ) 82 83 def test_parse_certbag(self): 84 '''test to parse the java oid "2.16.840.1.113894.746875.1.1"''' 85 with open(os.path.join(fixtures_dir, 'certbag.der'), 'rb') as f: 86 certbag = pkcs12.SafeBag.load(f.read()) 87 88 self.assertEqual( 89 2, 90 len(certbag['bag_attributes']) 91 ) 92 93 attr_0 = certbag['bag_attributes'][0] 94 95 self.assertEqual( 96 'friendly_name', 97 attr_0['type'].native 98 ) 99 100 self.assertEqual( 101 ['testcertificate'], 102 attr_0['values'].native 103 ) 104 105 attr_1 = certbag['bag_attributes'][1] 106 107 self.assertEqual( 108 'trusted_key_usage', 109 attr_1['type'].native 110 ) 111 112 self.assertEqual( 113 ['any_extended_key_usage'], 114 attr_1['values'].native 115 ) 116