blob: b07017189dd050d0146b097ccd6ff779d4a5b907 [file] [log] [blame]
Joanna Jeremiczc21b0082020-06-15 16:21:55 +02001from OpenSSL import crypto
2from EnvsReader import EnvsReader
3from ArtifactParser import ArtifactParser
4
5class P12ArtifactsValidator:
6
7 def __init__(self, mount_path):
8 self.parser = ArtifactParser(mount_path, "p12")
9
10 def get_and_compare_data_p12(self, path_to_env):
11 data = self.get_data(path_to_env)
12 return data, self.parser.contains_expected_data(data)
13
14 def can_open_keystore_and_truststore_with_pass(self):
15 can_open_keystore = self.can_open_store_file_with_pass_file(self.parser.keystorePassPath, self.parser.keystorePath)
16 can_open_truststore = self.can_open_store_file_with_pass_file(self.parser.truststorePassPath, self.parser.truststorePath)
17
18 return can_open_keystore & can_open_truststore
19
20 def can_open_store_file_with_pass_file(self, pass_file_path, store_file_path):
21 try:
22 self.get_certificate(pass_file_path, store_file_path)
23 return True
24 except:
25 return False
26
27 def get_data(self, path_to_env):
28 envs = self.parser.get_envs_as_dict(EnvsReader().read_env_list_from_file(path_to_env))
29 certificate = self.get_certificate(self.parser.keystorePassPath, self.parser.keystorePath)
30 data = self.parser.get_owner_data_from_certificate(certificate)
31 data['SANS'] = self.parser.get_sans(certificate)
32 return type('', (object,), {"expectedData": envs, "actualData": data})
33
34 def get_certificate(self, pass_file_path, store_file_path):
35 password = open(pass_file_path, 'rb').read()
36 crypto.load_pkcs12(open(store_file_path, 'rb').read(), password)
37 return crypto.load_pkcs12(open(store_file_path, 'rb').read(), password).get_certificate()