Skip to content

Commit

Permalink
[change] Updated code to handle various algorithms openwisp#118
Browse files Browse the repository at this point in the history
  • Loading branch information
praptisharma28 committed May 21, 2024
1 parent 80113f0 commit a27179f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 17 deletions.
15 changes: 15 additions & 0 deletions django_x509/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,14 @@
('sha256', 'SHA256'),
('sha384', 'SHA384'),
('sha512', 'SHA512'),
('ecdsa-with-sha1', 'ECDSA with SHA1'),
('ecdsa-with-sha256', 'ECDSA with SHA256'),
('ecdsa-with-sha384', 'ECDSA with SHA384'),
('ecdsa-with-sha512', 'ECDSA with SHA512'),
('dsaWithSHA1', 'DSA with SHA1'),
('dsaWithSHA256', 'DSA with SHA256'),
('ed25519', 'Ed25519'),
('ed448', 'Ed448'),
)

SIGNATURE_MAPPING = {
Expand All @@ -42,6 +49,14 @@
'sha384WithRSAEncryption': 'sha384',
'sha512WithRSAEncryption': 'sha512',
'ecdsa-with-SHA384': 'sha384',
'ecdsa-with-SHA1': 'sha1',
'ecdsa-with-SHA256': 'sha256',
'ecdsa-with-SHA384': 'sha384',
'ecdsa-with-SHA512': 'sha512',
'dsaWithSHA1': 'sha1',
'dsaWithSHA256': 'sha256',
'ed25519': 'ed25519',
'ed448': 'ed448',
}


Expand Down
16 changes: 15 additions & 1 deletion django_x509/migrations/0010_alter_ca_digest_alter_cert_digest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.13 on 2024-05-16 15:22
# Generated by Django 4.2.13 on 2024-05-21 18:07

from django.db import migrations, models
import django_x509.base.models
Expand All @@ -21,7 +21,14 @@ class Migration(migrations.Migration):
("sha256", "SHA256"),
("sha384", "SHA384"),
("sha512", "SHA512"),
("ecdsa-with-sha1", "ECDSA with SHA1"),
("ecdsa-with-sha256", "ECDSA with SHA256"),
("ecdsa-with-sha384", "ECDSA with SHA384"),
("ecdsa-with-sha512", "ECDSA with SHA512"),
("dsaWithSHA1", "DSA with SHA1"),
("dsaWithSHA256", "DSA with SHA256"),
("ed25519", "Ed25519"),
("ed448", "Ed448"),
],
default=django_x509.base.models.default_digest_algorithm,
help_text="bits",
Expand All @@ -39,7 +46,14 @@ class Migration(migrations.Migration):
("sha256", "SHA256"),
("sha384", "SHA384"),
("sha512", "SHA512"),
("ecdsa-with-sha1", "ECDSA with SHA1"),
("ecdsa-with-sha256", "ECDSA with SHA256"),
("ecdsa-with-sha384", "ECDSA with SHA384"),
("ecdsa-with-sha512", "ECDSA with SHA512"),
("dsaWithSHA1", "DSA with SHA1"),
("dsaWithSHA256", "DSA with SHA256"),
("ed25519", "Ed25519"),
("ed448", "Ed448"),
],
default=django_x509.base.models.default_digest_algorithm,
help_text="bits",
Expand Down
43 changes: 28 additions & 15 deletions django_x509/tests/test_ca.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,18 +682,31 @@ def test_ca_without_key_length_and_digest_algo(self):
else:
self.fail('ValidationError not raised as expected')

def test_import_with_ecdsa_signature_algorithm(self):
cert_mock = MagicMock()
cert_mock.get_signature_algorithm.return_value = b'ecdsa-with-SHA384'
cert_mock.get_pubkey.return_value.bits.return_value = '384'
cert_mock.get_notBefore.return_value.decode.return_value = '20240101000000Z'

with patch(
'django_x509.base.models.crypto.load_certificate', return_value=cert_mock
):
ca = TestCa()._create_ca()

try:
ca.full_clean()
except ValueError as ve:
self.fail(f"Unexpected ValueError: {ve}")
def test_import_with_various_signature_algorithms(self):
algorithms = [
'ecdsa-with-SHA1',
'ecdsa-with-SHA256',
'ecdsa-with-SHA384',
'ecdsa-with-SHA512',
'dsaWithSHA1',
'dsaWithSHA256',
'ed25519',
'ed448',
]

for algo in algorithms:
cert_mock = MagicMock()
cert_mock.get_signature_algorithm.return_value = algo.encode()
cert_mock.get_pubkey.return_value.bits.return_value = '384'
cert_mock.get_notBefore.return_value.decode.return_value = '20240101000000Z'

with patch(
'django_x509.base.models.crypto.load_certificate',
return_value=cert_mock,
):
ca = self._create_ca()

try:
ca.full_clean()
except ValidationError as e:
self.fail(f"Unexpected ValidationError for {algo}: {e}")
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.2.13 on 2024-05-16 15:32
# Generated by Django 4.2.13 on 2024-05-21 18:24

from django.db import migrations, models
import django_x509.base.models
Expand All @@ -21,7 +21,14 @@ class Migration(migrations.Migration):
("sha256", "SHA256"),
("sha384", "SHA384"),
("sha512", "SHA512"),
("ecdsa-with-sha1", "ECDSA with SHA1"),
("ecdsa-with-sha256", "ECDSA with SHA256"),
("ecdsa-with-sha384", "ECDSA with SHA384"),
("ecdsa-with-sha512", "ECDSA with SHA512"),
("dsaWithSHA1", "DSA with SHA1"),
("dsaWithSHA256", "DSA with SHA256"),
("ed25519", "Ed25519"),
("ed448", "Ed448"),
],
default=django_x509.base.models.default_digest_algorithm,
help_text="bits",
Expand All @@ -39,7 +46,14 @@ class Migration(migrations.Migration):
("sha256", "SHA256"),
("sha384", "SHA384"),
("sha512", "SHA512"),
("ecdsa-with-sha1", "ECDSA with SHA1"),
("ecdsa-with-sha256", "ECDSA with SHA256"),
("ecdsa-with-sha384", "ECDSA with SHA384"),
("ecdsa-with-sha512", "ECDSA with SHA512"),
("dsaWithSHA1", "DSA with SHA1"),
("dsaWithSHA256", "DSA with SHA256"),
("ed25519", "Ed25519"),
("ed448", "Ed448"),
],
default=django_x509.base.models.default_digest_algorithm,
help_text="bits",
Expand All @@ -57,7 +71,14 @@ class Migration(migrations.Migration):
("sha256", "SHA256"),
("sha384", "SHA384"),
("sha512", "SHA512"),
("ecdsa-with-sha1", "ECDSA with SHA1"),
("ecdsa-with-sha256", "ECDSA with SHA256"),
("ecdsa-with-sha384", "ECDSA with SHA384"),
("ecdsa-with-sha512", "ECDSA with SHA512"),
("dsaWithSHA1", "DSA with SHA1"),
("dsaWithSHA256", "DSA with SHA256"),
("ed25519", "Ed25519"),
("ed448", "Ed448"),
],
default=django_x509.base.models.default_digest_algorithm,
help_text="bits",
Expand Down

0 comments on commit a27179f

Please sign in to comment.