|
| 1 | +# This file is part of django-ca (https://github.com/mathiasertl/django-ca). |
| 2 | +# |
| 3 | +# django-ca is free software: you can redistribute it and/or modify it under the terms of the GNU General |
| 4 | +# Public License as published by the Free Software Foundation, either version 3 of the License, or (at your |
| 5 | +# option) any later version. |
| 6 | +# |
| 7 | +# django-ca is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the |
| 8 | +# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 9 | +# for more details. |
| 10 | +# |
| 11 | +# You should have received a copy of the GNU General Public License along with django-ca. If not, see |
| 12 | +# <http://www.gnu.org/licenses/>. |
| 13 | + |
| 14 | +"""Test the Certificate model.""" |
| 15 | + |
| 16 | +from datetime import datetime, timedelta |
| 17 | + |
| 18 | +import josepy as jose |
| 19 | + |
| 20 | +from cryptography import x509 |
| 21 | +from cryptography.hazmat.primitives import hashes |
| 22 | + |
| 23 | +from django.utils import timezone |
| 24 | + |
| 25 | +import pytest |
| 26 | +from _pytest.logging import LogCaptureFixture |
| 27 | +from pytest_django.fixtures import SettingsWrapper |
| 28 | + |
| 29 | +from django_ca.constants import ReasonFlags |
| 30 | +from django_ca.models import Certificate, CertificateAuthority |
| 31 | +from django_ca.tests.base.assertions import assert_validation_error |
| 32 | +from django_ca.tests.base.constants import CERT_DATA |
| 33 | +from django_ca.tests.models.base import assert_bundle |
| 34 | + |
| 35 | + |
| 36 | +def test_bundle_as_pem( |
| 37 | + root: CertificateAuthority, root_cert: Certificate, child: CertificateAuthority, child_cert: Certificate |
| 38 | +) -> None: |
| 39 | + """Test bundles of various CAs.""" |
| 40 | + assert_bundle([root_cert, root], root_cert) |
| 41 | + assert_bundle([child_cert, child, root], child_cert) |
| 42 | + |
| 43 | + |
| 44 | +def test_revocation() -> None: |
| 45 | + """Test getting a revociation for a non-revoked certificate.""" |
| 46 | + # Never really happens in real life, but should still be checked |
| 47 | + cert = Certificate(revoked=False) |
| 48 | + |
| 49 | + with pytest.raises(ValueError): |
| 50 | + cert.get_revocation() |
| 51 | + |
| 52 | + |
| 53 | +def test_root(root: CertificateAuthority, root_cert: Certificate, child_cert: Certificate) -> None: |
| 54 | + """Test the root property.""" |
| 55 | + assert root_cert.root == root |
| 56 | + assert child_cert.root == root |
| 57 | + |
| 58 | + |
| 59 | +def test_serial(usable_cert: Certificate) -> None: |
| 60 | + """Test getting the serial.""" |
| 61 | + cert_name = usable_cert.test_name # type: ignore[attr-defined] |
| 62 | + assert usable_cert.serial == CERT_DATA[cert_name].get("serial") |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.freeze_time("2019-02-03 15:43:12") |
| 66 | +def test_get_revocation_time(settings: SettingsWrapper, root_cert: Certificate) -> None: |
| 67 | + """Test getting the revocation time.""" |
| 68 | + assert root_cert.get_revocation_time() is None |
| 69 | + root_cert.revoke() |
| 70 | + |
| 71 | + # timestamp does not have a timezone regardless of USE_TZ |
| 72 | + root_cert.revoked_date = timezone.now() |
| 73 | + assert root_cert.get_revocation_time() == datetime(2019, 2, 3, 15, 43, 12) |
| 74 | + |
| 75 | + settings.USE_TZ = False |
| 76 | + root_cert.refresh_from_db() |
| 77 | + assert root_cert.get_revocation_time() == datetime(2019, 2, 3, 15, 43, 12) |
| 78 | + |
| 79 | + |
| 80 | +@pytest.mark.freeze_time("2019-02-03 15:43:12") |
| 81 | +def test_get_compromised_time(settings: SettingsWrapper, root_cert: Certificate) -> None: |
| 82 | + """Test getting the time when the certificate was compromised.""" |
| 83 | + assert root_cert.get_compromised_time() is None |
| 84 | + root_cert.revoke(compromised=timezone.now()) |
| 85 | + |
| 86 | + # timestamp does not have a timezone regardless of USE_TZ |
| 87 | + root_cert.compromised = timezone.now() |
| 88 | + assert root_cert.get_compromised_time() == datetime(2019, 2, 3, 15, 43, 12) |
| 89 | + |
| 90 | + settings.USE_TZ = False |
| 91 | + root_cert.refresh_from_db() |
| 92 | + assert root_cert.compromised == timezone.now() |
| 93 | + assert root_cert.get_compromised_time() == datetime(2019, 2, 3, 15, 43, 12) |
| 94 | + |
| 95 | + |
| 96 | +def test_get_revocation_reason(root_cert: Certificate) -> None: |
| 97 | + """Test getting the revocation reason.""" |
| 98 | + assert root_cert.get_revocation_reason() is None |
| 99 | + |
| 100 | + for reason in ReasonFlags: |
| 101 | + root_cert.revoke(reason) |
| 102 | + got = root_cert.get_revocation_reason() |
| 103 | + assert isinstance(got, x509.ReasonFlags) |
| 104 | + assert got.name == reason.name |
| 105 | + |
| 106 | + |
| 107 | +def test_validate_past(root_cert: Certificate) -> None: |
| 108 | + """Test that model validation blocks revoked_date or revoked_invalidity in the future.""" |
| 109 | + now = timezone.now() |
| 110 | + future = now + timedelta(10) |
| 111 | + past = now - timedelta(10) |
| 112 | + |
| 113 | + # Validation works if we're not revoked |
| 114 | + root_cert.full_clean() |
| 115 | + |
| 116 | + # Validation works if date is in the past |
| 117 | + root_cert.revoked_date = past |
| 118 | + root_cert.compromised = past |
| 119 | + root_cert.full_clean() |
| 120 | + |
| 121 | + root_cert.revoked_date = future |
| 122 | + root_cert.compromised = future |
| 123 | + with assert_validation_error( |
| 124 | + { |
| 125 | + "compromised": ["Date must be in the past!"], |
| 126 | + "revoked_date": ["Date must be in the past!"], |
| 127 | + } |
| 128 | + ): |
| 129 | + root_cert.full_clean() |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.parametrize("name,algorithm", (("sha256", hashes.SHA256()), ("sha512", hashes.SHA512()))) |
| 133 | +def test_get_fingerprint(name: str, algorithm: hashes.HashAlgorithm, usable_cert: Certificate) -> None: |
| 134 | + """Test getting the fingerprint value.""" |
| 135 | + cert_name = usable_cert.test_name # type: ignore[attr-defined] |
| 136 | + assert usable_cert.get_fingerprint(algorithm) == CERT_DATA[cert_name][name] |
| 137 | + |
| 138 | + |
| 139 | +def test_jwk(root_cert: Certificate, ec_cert: Certificate) -> None: |
| 140 | + """Test JWK property.""" |
| 141 | + # josepy does not support loading DSA/Ed448/Ed25519 keys: |
| 142 | + # https://github.com/certbot/josepy/pull/98 |
| 143 | + assert isinstance(ec_cert.jwk, jose.jwk.JWKEC) |
| 144 | + assert isinstance(root_cert.jwk, jose.jwk.JWKRSA) |
| 145 | + |
| 146 | + |
| 147 | +def test_jwk_with_unsupported_algorithm( |
| 148 | + dsa_cert: Certificate, ed448_cert: Certificate, ed25519_cert: Certificate |
| 149 | +) -> None: |
| 150 | + """Test the ValueError raised if called with an unsupported algorithm.""" |
| 151 | + with pytest.raises(ValueError, match="Unsupported algorithm"): |
| 152 | + ed448_cert.jwk # noqa: B018 |
| 153 | + with pytest.raises(ValueError, match="Unsupported algorithm"): |
| 154 | + ed25519_cert.jwk # noqa: B018 |
| 155 | + with pytest.raises(ValueError, match="Unsupported algorithm"): |
| 156 | + dsa_cert.jwk # noqa: B018 |
| 157 | + |
| 158 | + |
| 159 | +def test_revocation_with_no_revocation_date(root_cert: Certificate) -> None: |
| 160 | + """Test exception when no revocation date is set.""" |
| 161 | + root_cert.revoked = True |
| 162 | + root_cert.save() |
| 163 | + |
| 164 | + with pytest.raises(ValueError, match=r"^Certificate has no revocation date$"): |
| 165 | + root_cert.get_revocation() |
| 166 | + |
| 167 | + |
| 168 | +def test_get_revocation_time_with_no_revocation_date( |
| 169 | + caplog: LogCaptureFixture, root_cert: Certificate |
| 170 | +) -> None: |
| 171 | + """Test warning log message when there is no revocation time set but the cert is revoked.""" |
| 172 | + root_cert.revoked = True |
| 173 | + root_cert.save() |
| 174 | + |
| 175 | + assert root_cert.get_revocation_time() is None |
| 176 | + assert "Inconsistent model state: revoked=True and revoked_date=None." in caplog.text |
0 commit comments