def test_extract_certificate_info(): """ Test case that extracts data from private and public certificates in PEM or DER format""" cert_info = { "valid_from": datetime.datetime(2019, 6, 3, 11, 32, 57, tzinfo=datetime.timezone.utc), "valid_to": datetime.datetime(2029, 5, 31, 11, 32, 57, tzinfo=datetime.timezone.utc), "subject": [ ("C", "AU"), ("ST", "Some-State"), ("O", "pyas2lib"), ("CN", "test"), ], "issuer": [ ("C", "AU"), ("ST", "Some-State"), ("O", "pyas2lib"), ("CN", "test"), ], "serial": 13747137503594840569, } cert_empty = { "valid_from": None, "valid_to": None, "subject": None, "issuer": None, "serial": None, } # compare result of function with cert_info dict. with open(os.path.join(TEST_DIR, "cert_extract_private.cer"), "rb") as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, "cert_extract_private.pem"), "rb") as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, "cert_extract_public.cer"), "rb") as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, "cert_extract_public.pem"), "rb") as fp: assert utils.extract_certificate_info(fp.read()) == cert_info assert utils.extract_certificate_info(b"") == cert_empty
def save(self, *args, **kwargs): cert_info = extract_certificate_info(self.key) self.valid_from = cert_info['valid_from'] self.valid_to = cert_info['valid_to'] if not cert_info['serial'] is None: self.serial_number = cert_info['serial'].__str__() super().save(*args, **kwargs)
def save(self, *args, **kwargs): cert_info = extract_certificate_info(self.certificate) self.valid_from = cert_info["valid_from"] self.valid_to = cert_info["valid_to"] if not cert_info["serial"] is None: self.serial_number = cert_info["serial"].__str__() super().save(*args, **kwargs)
def test_extract_certificate_info(): """ Test case that extracts data from private and public certificates in PEM or DER format""" cert_info = { 'valid_from': datetime.datetime(2019, 6, 3, 11, 32, 57, tzinfo=datetime.timezone.utc), 'valid_to': datetime.datetime(2029, 5, 31, 11, 32, 57, tzinfo=datetime.timezone.utc), 'subject': [('C', 'AU'), ('ST', 'Some-State'), ('O', 'pyas2lib'), ('CN', 'test')], 'issuer': [('C', 'AU'), ('ST', 'Some-State'), ('O', 'pyas2lib'), ('CN', 'test')], 'serial': 13747137503594840569 } cert_empty = { 'valid_from': None, 'valid_to': None, 'subject': None, 'issuer': None, 'serial': None } # compare result of function with cert_info dict. with open(os.path.join(TEST_DIR, 'cert_extract_private.cer'), 'rb') as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, 'cert_extract_private.pem'), 'rb') as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, 'cert_extract_public.cer'), 'rb') as fp: assert utils.extract_certificate_info(fp.read()) == cert_info with open(os.path.join(TEST_DIR, 'cert_extract_public.pem'), 'rb') as fp: assert utils.extract_certificate_info(fp.read()) == cert_info assert utils.extract_certificate_info(b'') == cert_empty