blob: e2fdde91248f1732763ee827777cd91ffbdf6764 [file] [log] [blame]
Joanna Jeremiczc21b0082020-06-15 16:21:55 +02001import jks
2from OpenSSL import crypto
3from cryptography import x509
4from cryptography.hazmat.backends import default_backend
5from EnvsReader import EnvsReader
6from ArtifactParser import ArtifactParser
7
8class JksArtifactsValidator:
9
10 def __init__(self, mount_path):
11 self.parser = ArtifactParser(mount_path, "jks")
12
13 def get_and_compare_data_jks(self, path_to_env):
14 data = self.get_data_jks(path_to_env)
15 return data, self.parser.contains_expected_data(data)
16
17 def get_keystore(self):
18 keystore = jks.KeyStore.load(self.parser.keystorePath, open(self.parser.keystorePassPath, 'rb').read())
19 return keystore.private_keys['certificate'].cert_chain[0][1]
20
21 def get_truststore(self):
22 truststore = jks.KeyStore.load(self.parser.truststorePath, open(self.parser.truststorePassPath, 'rb').read())
23 return truststore.certs
24
25 def can_open_keystore_and_truststore_with_pass_jks(self):
26 try:
27 jks.KeyStore.load(self.parser.keystorePath, open(self.parser.keystorePassPath, 'rb').read())
28 jks.KeyStore.load(self.parser.truststorePath, open(self.parser.truststorePassPath, 'rb').read())
29 return True
30 except:
31 return False
32
33 def get_data_jks(self, path_to_env):
34 envs = self.parser.get_envs_as_dict(EnvsReader().read_env_list_from_file(path_to_env))
35 certificate = self.get_keystore_certificate()
36 data = self.parser.get_owner_data_from_certificate(certificate)
37 data['SANS'] = self.parser.get_sans(certificate)
38 return type('', (object,), {"expectedData": envs, "actualData": data})
39
40 def get_keystore_certificate(self):
41 return crypto.X509.from_cryptography(self.load_x509_certificate(self.get_keystore()))
42
43 def load_x509_certificate(self, data):
44 cert = x509.load_der_x509_certificate(data, default_backend())
45 return cert