コード例 #1
0
ファイル: test_utils.py プロジェクト: afzal44/my_new_work
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
コード例 #2
0
ファイル: models.py プロジェクト: j1mr10rd4n/django-pyas2
 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)
コード例 #3
0
ファイル: models.py プロジェクト: chadgates/django-pyas2
 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)
コード例 #4
0
ファイル: test_utils.py プロジェクト: jonnysparks/pyas2-lib
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