| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2026 Red Hat, Inc. |
| 3 |
# All rights reserved. |
| 4 |
# |
| 5 |
# License: GPL (version 3 or any later version). |
| 6 |
# See LICENSE for details. |
| 7 |
# --- END COPYRIGHT BLOCK --- |
| 8 |
# |
| 9 |
""" |
| 10 |
Comprehensive test suite for Dynamic Certificates feature |
| 11 |
Tests certificate metadata extraction, trust flags, multi-token support, |
| 12 |
verification, and error handling using lib389 DynamicCerts classes. |
| 13 |
""" |
| 14 |
|
| 15 |
import os |
| 16 |
import sys |
| 17 |
import base64 |
| 18 |
import copy |
| 19 |
import datetime |
| 20 |
import ipaddress |
| 21 |
import ldap |
| 22 |
import ldapurl |
| 23 |
import logging |
| 24 |
import pytest |
| 25 |
import secrets |
| 26 |
import shutil |
| 27 |
import socket |
| 28 |
import subprocess |
| 29 |
import textwrap |
| 30 |
import time |
| 31 |
from contextlib import contextmanager, suppress |
| 32 |
from cryptography.hazmat.backends import default_backend |
| 33 |
from cryptography.hazmat.primitives.asymmetric import ec, rsa |
| 34 |
from cryptography.hazmat.primitives import hashes, serialization |
| 35 |
from cryptography.hazmat.primitives.serialization import pkcs12 |
| 36 |
from cryptography import x509 |
| 37 |
from cryptography.x509.oid import NameOID, ExtensionOID |
| 38 |
from lib389.cli_base import FakeArgs |
| 39 |
from lib389._constants import DN_DM, PW_DM |
| 40 |
from lib389.dseldif import DSEldif |
| 41 |
from test389.topologies import topology_st as topo |
| 42 |
from lib389.utils import ds_is_older, ensure_str, pem_to_der, rpm_is_older |
| 43 |
from lib389.dyncerts import ( |
| 44 |
DynamicCerts, DynamicCert, DYNCERT_SUFFIX, |
| 45 |
DYCATTR_CN, DYCATTR_CERTDER, DYCATTR_PKEYDER ) |
| 46 |
from tempfile import NamedTemporaryFile |
| 47 |
|
| 48 |
pytestmark = pytest.mark.tier1 |
| 49 |
|
| 50 |
DEBUGGING = os.getenv("DEBUGGING", default=False) |
| 51 |
if DEBUGGING: |
| 52 |
logging.getLogger(__name__).setLevel(logging.DEBUG) |
| 53 |
else: |
| 54 |
logging.getLogger(__name__).setLevel(logging.INFO) |
| 55 |
log = logging.getLogger(__name__) |
| 56 |
|
| 57 |
tls_enabled = False |
| 58 |
|
| 59 |
|
| 60 |
def utcdate(): |
| 61 |
return datetime.datetime.utcnow() |
| 62 |
|
| 63 |
def fix_crash_issue_7227(inst): |
| 64 |
"""Work around to avoid ns-slapd crash in CERT_VerifyCertificateNow()""" |
| 65 |
inst.restart() |
| 66 |
|
| 67 |
|
| 68 |
class RSA_Certificate: |
| 69 |
"""RSA Certificate Generator for testing RSA key support""" |
| 70 |
|
| 71 |
PKCS12_PASSWORD = "rsa+password" |
| 72 |
|
| 73 |
def __init__(self): |
| 74 |
self.nickname = None |
| 75 |
self.trust = None |
| 76 |
self.namingAttrs = {} |
| 77 |
self.subject = None |
| 78 |
self.validity_days = 365 |
| 79 |
self.isCA = False |
| 80 |
self.isRoot = False |
| 81 |
|
| 82 |
@staticmethod |
| 83 |
def generateRootCA(nickname, namingAttributes={}, validity_days=3650): |
| 84 |
"""Generate a self-signed RSA Root CA certificate""" |
| 85 |
ca = RSA_Certificate() |
| 86 |
ca.namingAttrs = namingAttributes |
| 87 |
ca.validity_days = validity_days |
| 88 |
ca.isCA = True |
| 89 |
ca.isRoot = True |
| 90 |
ca.nickname = nickname |
| 91 |
ca.fixNamingAttributes() |
| 92 |
ca.subject = x509.Name([x509.NameAttribute(k, v) for k, v in ca.namingAttrs.items()]) |
| 93 |
ca.issuer = ca.subject |
| 94 |
ca.trust = 'CT,,' |
| 95 |
|
| 96 |
# Generate RSA 2048-bit private key |
| 97 |
ca.pkey = rsa.generate_private_key( |
| 98 |
public_exponent=65537, |
| 99 |
key_size=2048, |
| 100 |
backend=default_backend() |
| 101 |
) |
| 102 |
ca.cert = ( |
| 103 |
x509.CertificateBuilder() |
| 104 |
.subject_name(ca.subject) |
| 105 |
.issuer_name(ca.issuer) |
| 106 |
.public_key(ca.pkey.public_key()) |
| 107 |
.serial_number(x509.random_serial_number()) |
| 108 |
.not_valid_before(utcdate()) |
| 109 |
.not_valid_after(utcdate() + datetime.timedelta(days=validity_days)) |
| 110 |
.add_extension( |
| 111 |
x509.BasicConstraints(ca=True, path_length=0), |
| 112 |
critical=True, |
| 113 |
) |
| 114 |
.add_extension( |
| 115 |
x509.KeyUsage( |
| 116 |
digital_signature=True, |
| 117 |
key_cert_sign=True, |
| 118 |
crl_sign=True, |
| 119 |
key_encipherment=False, |
| 120 |
content_commitment=False, |
| 121 |
data_encipherment=False, |
| 122 |
key_agreement=False, |
| 123 |
encipher_only=False, |
| 124 |
decipher_only=False, |
| 125 |
), |
| 126 |
critical=True, |
| 127 |
) |
| 128 |
.add_extension( |
| 129 |
x509.SubjectKeyIdentifier.from_public_key(ca.pkey.public_key()), |
| 130 |
critical=False, |
| 131 |
) |
| 132 |
.sign(ca.pkey, hashes.SHA256(), default_backend()) |
| 133 |
) |
| 134 |
return ca |
| 135 |
|
| 136 |
def fixNamingAttribute(self, name, vdef): |
| 137 |
if name not in self.namingAttrs: |
| 138 |
self.namingAttrs[name] = vdef |
| 139 |
|
| 140 |
def fixNamingAttributes(self): |
| 141 |
self.fixNamingAttribute(NameOID.COMMON_NAME, self.nickname) |
| 142 |
self.fixNamingAttribute(NameOID.COUNTRY_NAME, 'US') |
| 143 |
self.fixNamingAttribute(NameOID.ORGANIZATION_NAME, 'Test Organization') |
| 144 |
|
| 145 |
def generateCertificate(self, nickname, namingAttributes={}, hostname=None, validity_days=365): |
| 146 |
"""Generate an RSA server certificate""" |
| 147 |
cert = RSA_Certificate() |
| 148 |
cert.namingAttrs = namingAttributes |
| 149 |
cert.validity_days = validity_days |
| 150 |
cert.isCA = False |
| 151 |
cert.isRoot = False |
| 152 |
cert.nickname = nickname |
| 153 |
cert.fixNamingAttributes() |
| 154 |
cert.subject = x509.Name([x509.NameAttribute(k, v) for k, v in cert.namingAttrs.items()]) |
| 155 |
cert.issuer = self.subject |
| 156 |
cert.trust = 'u,u,u' |
| 157 |
|
| 158 |
if hostname is None: |
| 159 |
hostname = socket.gethostname() |
| 160 |
|
| 161 |
san_list = [ |
| 162 |
x509.DNSName(hostname), |
| 163 |
x509.DNSName('localhost'), |
| 164 |
x509.IPAddress(ipaddress.ip_address("127.0.0.1")), |
| 165 |
x509.IPAddress(ipaddress.ip_address("::1")) |
| 166 |
] |
| 167 |
|
| 168 |
cert.pkey = rsa.generate_private_key( |
| 169 |
public_exponent=65537, |
| 170 |
key_size=2048, |
| 171 |
backend=default_backend() |
| 172 |
) |
| 173 |
cert.cert = ( |
| 174 |
x509.CertificateBuilder() |
| 175 |
.subject_name(cert.subject) |
| 176 |
.issuer_name(cert.issuer) |
| 177 |
.public_key(cert.pkey.public_key()) |
| 178 |
.serial_number(x509.random_serial_number()) |
| 179 |
.not_valid_before(utcdate()) |
| 180 |
.not_valid_after(utcdate() + datetime.timedelta(days=validity_days)) |
| 181 |
.add_extension( |
| 182 |
x509.BasicConstraints(ca=False, path_length=None), |
| 183 |
critical=True, |
| 184 |
) |
| 185 |
.add_extension( |
| 186 |
x509.KeyUsage( |
| 187 |
digital_signature=True, |
| 188 |
key_encipherment=True, |
| 189 |
key_cert_sign=False, |
| 190 |
crl_sign=False, |
| 191 |
content_commitment=False, |
| 192 |
data_encipherment=False, |
| 193 |
key_agreement=False, |
| 194 |
encipher_only=False, |
| 195 |
decipher_only=False, |
| 196 |
), |
| 197 |
critical=True, |
| 198 |
) |
| 199 |
.add_extension( |
| 200 |
x509.ExtendedKeyUsage([ |
| 201 |
x509.oid.ExtendedKeyUsageOID.SERVER_AUTH, |
| 202 |
x509.oid.ExtendedKeyUsageOID.CLIENT_AUTH, |
| 203 |
]), |
| 204 |
critical=True, |
| 205 |
) |
| 206 |
.add_extension( |
| 207 |
x509.SubjectAlternativeName(san_list), |
| 208 |
critical=False, |
| 209 |
) |
| 210 |
.add_extension( |
| 211 |
x509.SubjectKeyIdentifier.from_public_key(cert.pkey.public_key()), |
| 212 |
critical=False, |
| 213 |
) |
| 214 |
.add_extension( |
| 215 |
x509.AuthorityKeyIdentifier.from_issuer_public_key(self.pkey.public_key()), |
| 216 |
critical=False, |
| 217 |
) |
| 218 |
.sign(self.pkey, hashes.SHA256(), default_backend()) |
| 219 |
) |
| 220 |
return cert |
| 221 |
|
| 222 |
def save_pem_file(self, filename): |
| 223 |
"""Save certificate in PEM format""" |
| 224 |
with open(filename, "wb") as f: |
| 225 |
f.write(self.cert.public_bytes(serialization.Encoding.PEM)) |
| 226 |
|
| 227 |
def save_der_file(self, filename): |
| 228 |
"""Save certificate in DER format""" |
| 229 |
with open(filename, "wb") as f: |
| 230 |
f.write(self.cert.public_bytes(serialization.Encoding.DER)) |
| 231 |
|
| 232 |
def save_key_der(self, filename): |
| 233 |
"""Save private key in PKCS8 DER format""" |
| 234 |
with open(filename, "wb") as f: |
| 235 |
f.write(self.pkey.private_bytes( |
| 236 |
encoding=serialization.Encoding.DER, |
| 237 |
format=serialization.PrivateFormat.PKCS8, |
| 238 |
encryption_algorithm=serialization.NoEncryption() |
| 239 |
)) |
| 240 |
|
| 241 |
def write_pkcs12_file(self, filename, pw=PKCS12_PASSWORD): |
| 242 |
"""Save PKCS12 formatted certificate and private key""" |
| 243 |
if isinstance(pw, str): |
| 244 |
pw = pw.encode() |
| 245 |
if pw is None or pw == b"": |
| 246 |
enc = serialization.NoEncryption() |
| 247 |
else: |
| 248 |
enc = serialization.BestAvailableEncryption(pw) |
| 249 |
with open(filename, "wb") as f: |
| 250 |
f.write(pkcs12.serialize_key_and_certificates( |
| 251 |
self.nickname.encode(), |
| 252 |
self.pkey, |
| 253 |
self.cert, |
| 254 |
[], |
| 255 |
enc |
| 256 |
)) |
| 257 |
|
| 258 |
def save(self, dirname): |
| 259 |
"""Save all certificate formats""" |
| 260 |
name = f'{dirname}/{self.nickname}' |
| 261 |
self.pem = f"{name}.pem" |
| 262 |
self.der = f"{name}.der" |
| 263 |
self.kder = f"{name}-key.der" |
| 264 |
self.p12 = f"{name}.p12" |
| 265 |
self.save_pem_file(self.pem) |
| 266 |
self.save_der_file(self.der) |
| 267 |
self.save_key_der(self.kder) |
| 268 |
self.write_pkcs12_file(self.p12) |
| 269 |
|
| 270 |
|
| 271 |
@pytest.fixture(scope="module") |
| 272 |
def setup_tls(topo): |
| 273 |
"""Enable TLS on the instance""" |
| 274 |
global tls_enabled |
| 275 |
inst = topo.standalone |
| 276 |
if not tls_enabled: |
| 277 |
inst.enable_tls() |
| 278 |
tls_enabled = True |
| 279 |
return inst |
| 280 |
|
| 281 |
|
| 282 |
def test_certificate_metadata_extraction(topo, setup_tls): |
| 283 |
"""Test that certificate metadata is correctly extracted and exposed |
| 284 |
|
| 285 |
:id: a1b2c3d4-1111-2222-3333-444444444444 |
| 286 |
:setup: Standalone Instance with TLS enabled |
| 287 |
:steps: |
| 288 |
1. Generate RSA Root CA certificate |
| 289 |
2. Add certificate dynamically using DynamicCerts |
| 290 |
3. Retrieve certificate object and verify all metadata attributes |
| 291 |
4. Verify subject DN is correct |
| 292 |
5. Verify issuer DN is correct |
| 293 |
6. Verify isCA flag is TRUE |
| 294 |
7. Verify isRootCA flag is TRUE |
| 295 |
8. Verify key algorithm is RSA |
| 296 |
9. Verify trust flags are present |
| 297 |
10. Verify validity dates (notBefore, notAfter) |
| 298 |
11. Verify serial number is present |
| 299 |
:expectedresults: |
| 300 |
1. Certificate generated successfully |
| 301 |
2. Certificate added without error |
| 302 |
3. Certificate object retrieved |
| 303 |
4. Subject matches expected DN |
| 304 |
5. Issuer matches subject (self-signed) |
| 305 |
6. isCA is TRUE |
| 306 |
7. isRootCA is TRUE |
| 307 |
8. Key algorithm is RSA |
| 308 |
9. Trust flags are present |
| 309 |
10. Validity dates are within expected range |
| 310 |
11. Serial number is present and non-empty |
| 311 |
""" |
| 312 |
inst = setup_tls |
| 313 |
dir = '/tmp/dyncert_metadata_test' |
| 314 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 315 |
|
| 316 |
try: |
| 317 |
# Generate RSA Root CA |
| 318 |
ca = RSA_Certificate.generateRootCA( |
| 319 |
"TestMetadataCA", |
| 320 |
namingAttributes={ |
| 321 |
NameOID.COMMON_NAME: "Test Metadata CA", |
| 322 |
NameOID.COUNTRY_NAME: "US", |
| 323 |
NameOID.ORGANIZATION_NAME: "Test Org", |
| 324 |
NameOID.ORGANIZATIONAL_UNIT_NAME: "Testing Unit" |
| 325 |
} |
| 326 |
) |
| 327 |
ca.save(dir) |
| 328 |
|
| 329 |
# Add certificate using DynamicCerts |
| 330 |
dyncerts = DynamicCerts(inst) |
| 331 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 332 |
|
| 333 |
# Get certificate object |
| 334 |
cert_obj = dyncerts.get_cert_obj(ca.nickname) |
| 335 |
assert cert_obj is not None |
| 336 |
|
| 337 |
# Verify subject and issuer |
| 338 |
subject = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateSubject') |
| 339 |
issuer = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateIssuer') |
| 340 |
log.info(f"Subject: {subject}") |
| 341 |
log.info(f"Issuer: {issuer}") |
| 342 |
assert "Test Metadata CA".lower() in subject |
| 343 |
assert subject == issuer # Self-signed |
| 344 |
|
| 345 |
# Verify CA flags |
| 346 |
is_ca = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateIsCA') |
| 347 |
is_root_ca = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateIsRootCA') |
| 348 |
log.info(f"isCA: {is_ca}") |
| 349 |
log.info(f"isRootCA: {is_root_ca}") |
| 350 |
assert is_ca == 'true' |
| 351 |
assert is_root_ca == 'true' |
| 352 |
|
| 353 |
# Verify key algorithm |
| 354 |
key_algo = cert_obj.get_attr_val_utf8('dsDynamicCertificateKeyAlgorithm') |
| 355 |
log.info(f"Key Algorithm: {key_algo}") |
| 356 |
assert 'RSA' in key_algo.upper() |
| 357 |
|
| 358 |
# Verify trust flags |
| 359 |
trust_flags = cert_obj.get_attr_val_utf8('dsDynamicCertificateTrustFlags') |
| 360 |
log.info(f"Trust Flags: {trust_flags}") |
| 361 |
assert trust_flags is not None |
| 362 |
|
| 363 |
# Verify validity dates |
| 364 |
not_before = cert_obj.get_attr_val_utf8('dsDynamicCertificateNotBefore') |
| 365 |
not_after = cert_obj.get_attr_val_utf8('dsDynamicCertificateNotAfter') |
| 366 |
log.info(f"Not Before: {not_before}") |
| 367 |
log.info(f"Not After: {not_after}") |
| 368 |
assert not_before is not None |
| 369 |
assert not_after is not None |
| 370 |
|
| 371 |
# Verify serial number |
| 372 |
serial = cert_obj.get_attr_val_utf8('dsDynamicCertificateSerialNumber') |
| 373 |
log.info(f"Serial Number: {serial}") |
| 374 |
assert serial is not None |
| 375 |
assert len(serial) > 0 |
| 376 |
|
| 377 |
# Cleanup |
| 378 |
dyncerts.del_cert(ca.nickname) |
| 379 |
|
| 380 |
finally: |
| 381 |
if not DEBUGGING: |
| 382 |
shutil.rmtree(dir, ignore_errors=True) |
| 383 |
|
| 384 |
|
| 385 |
def test_subject_alternative_names(topo, setup_tls): |
| 386 |
"""Test that Subject Alternative Names are correctly parsed and displayed |
| 387 |
|
| 388 |
:id: b2c3d4e5-2222-3333-4444-555555555555 |
| 389 |
:setup: Standalone Instance with TLS enabled |
| 390 |
:steps: |
| 391 |
1. Generate server certificate with multiple SANs (DNS and IP) |
| 392 |
2. Add certificate dynamically using DynamicCerts |
| 393 |
3. Retrieve certificate object |
| 394 |
4. Verify dsDynamicCertificateSubjectAltName attribute exists |
| 395 |
5. Verify all DNS names are present |
| 396 |
6. Verify all IP addresses are present |
| 397 |
:expectedresults: |
| 398 |
1. Certificate generated with SANs |
| 399 |
2. Certificate added successfully |
| 400 |
3. Certificate object retrieved |
| 401 |
4. SubjectAltName attribute exists |
| 402 |
5. DNS names are correctly parsed |
| 403 |
6. IP addresses are correctly parsed |
| 404 |
""" |
| 405 |
inst = setup_tls |
| 406 |
fix_crash_issue_7227(inst) |
| 407 |
dir = '/tmp/dyncert_san_test' |
| 408 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 409 |
|
| 410 |
try: |
| 411 |
ca = RSA_Certificate.generateRootCA("TestSAN_CA") |
| 412 |
ca.save(dir) |
| 413 |
|
| 414 |
# Add CA first |
| 415 |
dyncerts = DynamicCerts(inst) |
| 416 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 417 |
|
| 418 |
# Generate certificate with specific hostname |
| 419 |
cert = ca.generateCertificate("TestSAN_Cert", hostname="test.example.com") |
| 420 |
cert.save(dir) |
| 421 |
|
| 422 |
# Add server certificate with private key |
| 423 |
dyncerts.add_cert(cert.p12, cert.nickname, pkcs12_password=RSA_Certificate.PKCS12_PASSWORD) |
| 424 |
|
| 425 |
# Get certificate object |
| 426 |
cert_obj = dyncerts.get_cert_obj(cert.nickname) |
| 427 |
assert cert_obj is not None |
| 428 |
|
| 429 |
# Verify SANs |
| 430 |
san_values = cert_obj.get_attr_vals_utf8('dsDynamicCertificateSubjectAltName') |
| 431 |
log.info(f"Subject Alternative Names: {san_values}") |
| 432 |
assert san_values is not None |
| 433 |
|
| 434 |
# Verify expected SANs are present |
| 435 |
san_str = ' '.join(san_values) |
| 436 |
assert 'test.example.com' in san_str or 'localhost' in san_str |
| 437 |
assert '127.0.0.1' in san_str or '::1' in san_str |
| 438 |
|
| 439 |
# Cleanup |
| 440 |
dyncerts.del_cert(ca.nickname) |
| 441 |
dyncerts.del_cert(cert.nickname) |
| 442 |
|
| 443 |
finally: |
| 444 |
if not DEBUGGING: |
| 445 |
shutil.rmtree(dir, ignore_errors=True) |
| 446 |
|
| 447 |
|
| 448 |
def test_private_key_detection(topo, setup_tls): |
| 449 |
"""Test detection of certificates with and without private keys |
| 450 |
|
| 451 |
:id: c3d4e5f6-3333-4444-5555-666666666666 |
| 452 |
:setup: Standalone Instance with TLS enabled |
| 453 |
:steps: |
| 454 |
1. Add CA certificate without private key |
| 455 |
2. Get certificate object and verify hasPrivateKey is FALSE |
| 456 |
3. Add server certificate with private key |
| 457 |
4. Get certificate object and verify hasPrivateKey is TRUE |
| 458 |
5. Verify isServerCert is TRUE for server cert |
| 459 |
6. Verify isServerCert is FALSE for CA cert |
| 460 |
:expectedresults: |
| 461 |
1. CA added successfully |
| 462 |
2. hasPrivateKey is FALSE |
| 463 |
3. Server cert added successfully |
| 464 |
4. hasPrivateKey is TRUE |
| 465 |
5. Server cert properly identified |
| 466 |
6. CA cert not identified as server cert |
| 467 |
""" |
| 468 |
inst = setup_tls |
| 469 |
fix_crash_issue_7227(inst) |
| 470 |
dir = '/tmp/dyncert_privkey_test' |
| 471 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 472 |
|
| 473 |
try: |
| 474 |
ca = RSA_Certificate.generateRootCA("TestPrivKey_CA") |
| 475 |
ca.save(dir) |
| 476 |
cert = ca.generateCertificate("TestPrivKey_Cert") |
| 477 |
cert.save(dir) |
| 478 |
|
| 479 |
dyncerts = DynamicCerts(inst) |
| 480 |
|
| 481 |
# Add CA without private key (PEM has no key) |
| 482 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 483 |
|
| 484 |
# Verify CA has no private key |
| 485 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 486 |
has_key = ca_obj.get_attr_val_utf8_l('dsDynamicCertificateHasPrivateKey') |
| 487 |
log.info(f"CA hasPrivateKey: {has_key}") |
| 488 |
assert has_key == 'false' |
| 489 |
|
| 490 |
# Verify it's identified as CA |
| 491 |
is_ca = ca_obj.get_attr_val_utf8_l('dsDynamicCertificateIsCA') |
| 492 |
assert is_ca == 'true' |
| 493 |
|
| 494 |
# Add server cert with private key (PKCS12 has key) |
| 495 |
dyncerts.add_cert(cert.p12, cert.nickname, pkcs12_password=RSA_Certificate.PKCS12_PASSWORD) |
| 496 |
|
| 497 |
# Verify server cert has private key |
| 498 |
cert_obj = dyncerts.get_cert_obj(cert.nickname) |
| 499 |
has_key = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateHasPrivateKey') |
| 500 |
log.info(f"Server cert hasPrivateKey: {has_key}") |
| 501 |
assert has_key == 'true' |
| 502 |
|
| 503 |
# Check if isServerCert attribute exists |
| 504 |
is_server = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateIsServerCert') |
| 505 |
if is_server: |
| 506 |
log.info(f"isServerCert: {is_server}") |
| 507 |
|
| 508 |
# Cleanup |
| 509 |
dyncerts.del_cert(ca.nickname) |
| 510 |
dyncerts.del_cert(cert.nickname) |
| 511 |
|
| 512 |
finally: |
| 513 |
if not DEBUGGING: |
| 514 |
shutil.rmtree(dir, ignore_errors=True) |
| 515 |
|
| 516 |
|
| 517 |
def test_trust_flags_modification(topo, setup_tls): |
| 518 |
"""Test modification of certificate trust flags |
| 519 |
|
| 520 |
:id: d4e5f6a7-4444-5555-6666-777777777777 |
| 521 |
:setup: Standalone Instance with TLS enabled |
| 522 |
:steps: |
| 523 |
1. Add CA certificate with default trust flags |
| 524 |
2. Read initial trust flags |
| 525 |
3. Modify trust flags to different values using edit_cert_trust |
| 526 |
4. Retrieve certificate and verify trust flags changed |
| 527 |
5. Modify trust flags back |
| 528 |
6. Verify trust flags changed again |
| 529 |
:expectedresults: |
| 530 |
1. Certificate added successfully |
| 531 |
2. Initial trust flags present |
| 532 |
3. Modification succeeds |
| 533 |
4. Trust flags match new value |
| 534 |
5. Second modification succeeds |
| 535 |
6. Trust flags match updated value |
| 536 |
""" |
| 537 |
inst = setup_tls |
| 538 |
fix_crash_issue_7227(inst) |
| 539 |
dir = '/tmp/dyncert_trust_test' |
| 540 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 541 |
|
| 542 |
try: |
| 543 |
ca = RSA_Certificate.generateRootCA("TestTrust_CA") |
| 544 |
ca.save(dir) |
| 545 |
|
| 546 |
dyncerts = DynamicCerts(inst) |
| 547 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 548 |
|
| 549 |
# Read initial trust flags |
| 550 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 551 |
initial_trust = ca_obj.get_attr_val_utf8('dsDynamicCertificateTrustFlags') |
| 552 |
log.info(f"Initial trust flags: {initial_trust}") |
| 553 |
|
| 554 |
# Modify trust flags |
| 555 |
new_trust = "CT,C,C" |
| 556 |
dyncerts.edit_cert_trust(ca.nickname, new_trust) |
| 557 |
|
| 558 |
# Verify modification |
| 559 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 560 |
updated_trust = ca_obj.get_attr_val_utf8('dsDynamicCertificateTrustFlags') |
| 561 |
log.info(f"Updated trust flags: {updated_trust}") |
| 562 |
assert new_trust in updated_trust or updated_trust in new_trust |
| 563 |
|
| 564 |
# Cleanup |
| 565 |
dyncerts.del_cert(ca.nickname) |
| 566 |
|
| 567 |
finally: |
| 568 |
if not DEBUGGING: |
| 569 |
shutil.rmtree(dir, ignore_errors=True) |
| 570 |
|
| 571 |
|
| 572 |
def test_certificate_replace_operation(topo, setup_tls): |
| 573 |
"""Test replacing existing certificate using add_cert operation |
| 574 |
|
| 575 |
:id: e5f6a7b8-5555-6666-7777-888888888888 |
| 576 |
:setup: Standalone Instance with TLS enabled |
| 577 |
:steps: |
| 578 |
1. Add initial certificate |
| 579 |
2. Verify certificate is present |
| 580 |
3. Generate new certificate with same nickname |
| 581 |
4. Replace certificate using add_cert (it detects existing and updates) |
| 582 |
5. Retrieve certificate and verify it was replaced |
| 583 |
6. Verify serial number changed |
| 584 |
:expectedresults: |
| 585 |
1. Initial certificate added |
| 586 |
2. Certificate found |
| 587 |
3. New certificate generated |
| 588 |
4. Replace operation succeeds |
| 589 |
5. Certificate is different |
| 590 |
6. Serial number is different |
| 591 |
""" |
| 592 |
inst = setup_tls |
| 593 |
fix_crash_issue_7227(inst) |
| 594 |
dir = '/tmp/dyncert_replace_test' |
| 595 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 596 |
|
| 597 |
try: |
| 598 |
ca = RSA_Certificate.generateRootCA("TestReplace_CA") |
| 599 |
ca.save(dir) |
| 600 |
|
| 601 |
dyncerts = DynamicCerts(inst) |
| 602 |
|
| 603 |
# Add initial certificate |
| 604 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 605 |
|
| 606 |
# Get initial serial number |
| 607 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 608 |
serial1 = ca_obj.get_attr_val_utf8('dsDynamicCertificateSerialNumber') |
| 609 |
log.info(f"Initial serial: {serial1}") |
| 610 |
|
| 611 |
# Generate new certificate with same nickname |
| 612 |
ca2 = RSA_Certificate.generateRootCA("TestReplace_CA") # Same nickname, different cert |
| 613 |
os.makedirs(dir + '/replacement', 0o700, exist_ok=True) |
| 614 |
ca2.save(dir + '/replacement') |
| 615 |
|
| 616 |
# Replace certificate (add_cert detects existing and updates) |
| 617 |
dyncerts.add_cert(ca2.pem, ca2.nickname, ca=True) |
| 618 |
|
| 619 |
# Verify replacement |
| 620 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 621 |
serial2 = ca_obj.get_attr_val_utf8('dsDynamicCertificateSerialNumber') |
| 622 |
log.info(f"New serial: {serial2}") |
| 623 |
assert serial1 != serial2 |
| 624 |
|
| 625 |
# Cleanup |
| 626 |
dyncerts.del_cert(ca.nickname) |
| 627 |
|
| 628 |
finally: |
| 629 |
if not DEBUGGING: |
| 630 |
shutil.rmtree(dir, ignore_errors=True) |
| 631 |
|
| 632 |
|
| 633 |
def test_search_with_filters(topo, setup_tls): |
| 634 |
"""Test searching certificates with LDAP filters using DynamicCerts.list() |
| 635 |
|
| 636 |
:id: f6a7b8c9-6666-7777-8888-999999999999 |
| 637 |
:setup: Standalone Instance with TLS enabled |
| 638 |
:steps: |
| 639 |
1. Add multiple certificates (CA and server certs) |
| 640 |
2. List all certificates using list() |
| 641 |
3. List CA certificates using list_ca_certs() |
| 642 |
4. List server certificates using list_certs() |
| 643 |
5. Use get_cert_details to get specific certificate info |
| 644 |
:expectedresults: |
| 645 |
1. All certificates added successfully |
| 646 |
2. list() returns all certificate objects |
| 647 |
3. list_ca_certs() returns only CA certificates |
| 648 |
4. list_certs() returns only server certificates |
| 649 |
5. get_cert_details returns detailed info |
| 650 |
""" |
| 651 |
inst = setup_tls |
| 652 |
fix_crash_issue_7227(inst) |
| 653 |
dir = '/tmp/dyncert_filter_test' |
| 654 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 655 |
|
| 656 |
try: |
| 657 |
dyncerts = DynamicCerts(inst) |
| 658 |
|
| 659 |
# Add CA certificate |
| 660 |
ca = RSA_Certificate.generateRootCA("TestFilter_CA") |
| 661 |
ca.save(dir) |
| 662 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 663 |
|
| 664 |
# Add server certificate with private key |
| 665 |
cert = ca.generateCertificate("TestFilter_Cert") |
| 666 |
cert.save(dir) |
| 667 |
dyncerts.add_cert(cert.p12, cert.nickname, pkcs12_password=RSA_Certificate.PKCS12_PASSWORD) |
| 668 |
|
| 669 |
# List all certificate objects |
| 670 |
all_certs = dyncerts.list() |
| 671 |
log.info(f"Total certificate objects found: {len(all_certs)}") |
| 672 |
assert len(all_certs) >= 2 |
| 673 |
|
| 674 |
# List CA certificates only |
| 675 |
ca_certs = dyncerts.list_ca_certs() |
| 676 |
log.info(f"CA certificates found: {len(ca_certs)}") |
| 677 |
assert len(ca_certs) >= 1 |
| 678 |
assert any(c['cn'] == ca.nickname for c in ca_certs) |
| 679 |
|
| 680 |
# List server certificates only |
| 681 |
server_certs = dyncerts.list_certs() |
| 682 |
log.info(f"Server certificates found: {len(server_certs)}") |
| 683 |
assert len(server_certs) >= 1 |
| 684 |
assert any(c['cn'] == cert.nickname for c in server_certs) |
| 685 |
|
| 686 |
# Get specific certificate details |
| 687 |
ca_details = dyncerts.get_cert_details(ca.nickname) |
| 688 |
assert ca_details is not None |
| 689 |
assert ca_details['cn'] == ca.nickname |
| 690 |
log.info(f"CA details: {ca_details}") |
| 691 |
|
| 692 |
# Cleanup |
| 693 |
dyncerts.del_cert(ca.nickname) |
| 694 |
dyncerts.del_cert(cert.nickname) |
| 695 |
|
| 696 |
finally: |
| 697 |
if not DEBUGGING: |
| 698 |
shutil.rmtree(dir, ignore_errors=True) |
| 699 |
|
| 700 |
|
| 701 |
def test_batch_certificate_operations(topo, setup_tls): |
| 702 |
"""Test adding and managing multiple certificates |
| 703 |
|
| 704 |
:id: a7b8c9d0-7777-8888-9999-000000000000 |
| 705 |
:setup: Standalone Instance with TLS enabled |
| 706 |
:steps: |
| 707 |
1. Generate 5 different certificates |
| 708 |
2. Add all certificates using DynamicCerts |
| 709 |
3. List all certificates |
| 710 |
4. Verify all certificates are present |
| 711 |
5. Delete all certificates |
| 712 |
6. Verify all deletions succeeded |
| 713 |
:expectedresults: |
| 714 |
1. All certificates generated |
| 715 |
2. All additions succeed |
| 716 |
3. List returns all certificates |
| 717 |
4. All certificates have correct metadata |
| 718 |
5. All deletions succeed |
| 719 |
6. No certificates remain |
| 720 |
""" |
| 721 |
inst = setup_tls |
| 722 |
fix_crash_issue_7227(inst) |
| 723 |
dir = '/tmp/dyncert_batch_test' |
| 724 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 725 |
|
| 726 |
cert_list = [] |
| 727 |
try: |
| 728 |
dyncerts = DynamicCerts(inst) |
| 729 |
|
| 730 |
# Generate and add multiple certificates |
| 731 |
for i in range(5): |
| 732 |
ca = RSA_Certificate.generateRootCA(f"TestBatch_CA_{i}") |
| 733 |
ca.save(dir) |
| 734 |
cert_list.append(ca) |
| 735 |
|
| 736 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 737 |
log.info(f"Added certificate {i+1}/5: {ca.nickname}") |
| 738 |
|
| 739 |
# List all CA certificates |
| 740 |
ca_certs = dyncerts.list_ca_certs() |
| 741 |
batch_certs = [c for c in ca_certs if c['cn'].startswith('TestBatch_CA_')] |
| 742 |
log.info(f"Found {len(batch_certs)} batch certificates") |
| 743 |
assert len(batch_certs) == 5 |
| 744 |
|
| 745 |
# Verify each certificate |
| 746 |
for ca in cert_list: |
| 747 |
cert_obj = dyncerts.get_cert_obj(ca.nickname) |
| 748 |
assert cert_obj is not None |
| 749 |
subject = cert_obj.get_attr_val_utf8('dsDynamicCertificateSubject') |
| 750 |
assert subject is not None |
| 751 |
log.info(f"Verified certificate: {ca.nickname}") |
| 752 |
|
| 753 |
# Delete all certificates |
| 754 |
for ca in cert_list: |
| 755 |
dyncerts.del_cert(ca.nickname) |
| 756 |
log.info(f"Deleted certificate: {ca.nickname}") |
| 757 |
|
| 758 |
# Verify deletions |
| 759 |
ca_certs_after = dyncerts.list_ca_certs() |
| 760 |
batch_certs_after = [c for c in ca_certs_after if c['cn'].startswith('TestBatch_CA_')] |
| 761 |
assert len(batch_certs_after) == 0 |
| 762 |
|
| 763 |
finally: |
| 764 |
if not DEBUGGING: |
| 765 |
shutil.rmtree(dir, ignore_errors=True) |
| 766 |
|
| 767 |
|
| 768 |
def test_invalid_certificate_handling(topo, setup_tls): |
| 769 |
"""Test error handling for invalid certificate data |
| 770 |
|
| 771 |
:id: b8c9d0e1-8888-9999-0000-111111111111 |
| 772 |
:setup: Standalone Instance with TLS enabled |
| 773 |
:steps: |
| 774 |
1. Attempt to add certificate from non-existent file |
| 775 |
2. Attempt to add certificate with empty nickname |
| 776 |
3. Attempt to add non-CA cert as CA |
| 777 |
4. Verify all operations fail with appropriate errors |
| 778 |
:expectedresults: |
| 779 |
1. Non-existent file rejected with ValueError |
| 780 |
2. Empty nickname rejected with ValueError |
| 781 |
3. Non-CA cert rejected as CA with ValueError |
| 782 |
4. All errors are appropriate exceptions |
| 783 |
""" |
| 784 |
inst = setup_tls |
| 785 |
fix_crash_issue_7227(inst) |
| 786 |
dir = '/tmp/dyncert_invalid_test' |
| 787 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 788 |
|
| 789 |
try: |
| 790 |
dyncerts = DynamicCerts(inst) |
| 791 |
|
| 792 |
# Test 1: Non-existent file |
| 793 |
try: |
| 794 |
dyncerts.add_cert('/nonexistent/file.pem', 'TestInvalid') |
| 795 |
assert False, "Should have failed with non-existent file" |
| 796 |
except ValueError as e: |
| 797 |
log.info(f"Expected error for non-existent file: {e}") |
| 798 |
|
| 799 |
# Test 2: Empty nickname |
| 800 |
ca = RSA_Certificate.generateRootCA("TestInvalid_CA") |
| 801 |
ca.save(dir) |
| 802 |
|
| 803 |
try: |
| 804 |
dyncerts.add_cert(ca.pem, '', ca=True) |
| 805 |
assert False, "Should have failed with empty nickname" |
| 806 |
except ValueError as e: |
| 807 |
log.info(f"Expected error for empty nickname: {e}") |
| 808 |
|
| 809 |
# Test 3: Non-CA cert marked as CA |
| 810 |
cert = ca.generateCertificate("TestInvalid_Cert") |
| 811 |
cert.save(dir) |
| 812 |
|
| 813 |
try: |
| 814 |
dyncerts.add_cert(cert.pem, cert.nickname, ca=True) |
| 815 |
assert False, "Should have failed with non-CA cert" |
| 816 |
except ValueError as e: |
| 817 |
log.info(f"Expected error for non-CA cert: {e}") |
| 818 |
|
| 819 |
finally: |
| 820 |
if not DEBUGGING: |
| 821 |
shutil.rmtree(dir, ignore_errors=True) |
| 822 |
|
| 823 |
|
| 824 |
def test_certificate_verification_status(topo, setup_tls): |
| 825 |
"""Test certificate verification status attribute |
| 826 |
|
| 827 |
:id: c9d0e1f2-9999-0000-1111-222222222222 |
| 828 |
:setup: Standalone Instance with TLS enabled |
| 829 |
:steps: |
| 830 |
1. Add valid CA certificate |
| 831 |
2. Add server certificate signed by CA |
| 832 |
3. Retrieve certificates and check verification status |
| 833 |
4. Verify self-signed CA has appropriate status |
| 834 |
5. Verify server cert has verification status |
| 835 |
:expectedresults: |
| 836 |
1. CA added successfully |
| 837 |
2. Server cert added successfully |
| 838 |
3. Verification status attribute accessible |
| 839 |
4. CA status is appropriate for self-signed |
| 840 |
5. Server cert status reflects chain validation |
| 841 |
""" |
| 842 |
inst = setup_tls |
| 843 |
fix_crash_issue_7227(inst) |
| 844 |
dir = '/tmp/dyncert_verify_test' |
| 845 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 846 |
|
| 847 |
try: |
| 848 |
dyncerts = DynamicCerts(inst) |
| 849 |
|
| 850 |
ca = RSA_Certificate.generateRootCA("TestVerify_CA") |
| 851 |
ca.save(dir) |
| 852 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 853 |
|
| 854 |
# Check CA verification status |
| 855 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 856 |
verify_status = ca_obj.get_attr_val_utf8('dsDynamicCertificateVerificationStatus') |
| 857 |
if verify_status: |
| 858 |
log.info(f"CA verification status: {verify_status}") |
| 859 |
else: |
| 860 |
log.info("Verification status attribute not present for CA") |
| 861 |
|
| 862 |
# Add server certificate |
| 863 |
cert = ca.generateCertificate("TestVerify_Cert") |
| 864 |
cert.save(dir) |
| 865 |
dyncerts.add_cert(cert.p12, cert.nickname, pkcs12_password=RSA_Certificate.PKCS12_PASSWORD) |
| 866 |
|
| 867 |
# Check server cert verification status |
| 868 |
cert_obj = dyncerts.get_cert_obj(cert.nickname) |
| 869 |
verify_status = cert_obj.get_attr_val_utf8('dsDynamicCertificateVerificationStatus') |
| 870 |
if verify_status: |
| 871 |
log.info(f"Server cert verification status: {verify_status}") |
| 872 |
else: |
| 873 |
log.info("Verification status attribute not present for server cert") |
| 874 |
|
| 875 |
# Cleanup |
| 876 |
dyncerts.del_cert(ca.nickname) |
| 877 |
dyncerts.del_cert(cert.nickname) |
| 878 |
|
| 879 |
finally: |
| 880 |
if not DEBUGGING: |
| 881 |
shutil.rmtree(dir, ignore_errors=True) |
| 882 |
|
| 883 |
|
| 884 |
def test_expired_certificate_handling(topo, setup_tls): |
| 885 |
"""Test handling of expired certificates |
| 886 |
|
| 887 |
:id: d0e1f2a3-0000-1111-2222-333333333333 |
| 888 |
:setup: Standalone Instance with TLS enabled |
| 889 |
:steps: |
| 890 |
1. Generate certificate with validity starting in the past and already expired |
| 891 |
2. Add expired certificate |
| 892 |
3. Retrieve certificate object |
| 893 |
4. Verify notBefore date is in the past |
| 894 |
5. Verify notAfter date is also in the past |
| 895 |
6. Check if verification status indicates expiration |
| 896 |
:expectedresults: |
| 897 |
1. Expired certificate generated |
| 898 |
2. Certificate can be added (stored) |
| 899 |
3. Certificate object retrieved |
| 900 |
4. notBefore shows past date |
| 901 |
5. notAfter shows past date (expired) |
| 902 |
6. Verification status may indicate invalid/expired |
| 903 |
""" |
| 904 |
inst = setup_tls |
| 905 |
fix_crash_issue_7227(inst) |
| 906 |
dir = '/tmp/dyncert_expired_test' |
| 907 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 908 |
|
| 909 |
try: |
| 910 |
# Create a certificate that's already expired |
| 911 |
ca = RSA_Certificate() |
| 912 |
ca.nickname = "TestExpired_CA" |
| 913 |
ca.isCA = True |
| 914 |
ca.isRoot = True |
| 915 |
ca.fixNamingAttributes() |
| 916 |
ca.subject = x509.Name([x509.NameAttribute(k, v) for k, v in ca.namingAttrs.items()]) |
| 917 |
ca.issuer = ca.subject |
| 918 |
ca.trust = 'CT,,' |
| 919 |
|
| 920 |
# Generate with dates in the past |
| 921 |
past_date = utcdate() - datetime.timedelta(days=365) |
| 922 |
expired_date = utcdate() - datetime.timedelta(days=1) |
| 923 |
|
| 924 |
ca.pkey = rsa.generate_private_key( |
| 925 |
public_exponent=65537, |
| 926 |
key_size=2048, |
| 927 |
backend=default_backend() |
| 928 |
) |
| 929 |
ca.cert = ( |
| 930 |
x509.CertificateBuilder() |
| 931 |
.subject_name(ca.subject) |
| 932 |
.issuer_name(ca.issuer) |
| 933 |
.public_key(ca.pkey.public_key()) |
| 934 |
.serial_number(x509.random_serial_number()) |
| 935 |
.not_valid_before(past_date) |
| 936 |
.not_valid_after(expired_date) |
| 937 |
.add_extension( |
| 938 |
x509.BasicConstraints(ca=True, path_length=0), |
| 939 |
critical=True, |
| 940 |
) |
| 941 |
.sign(ca.pkey, hashes.SHA256(), default_backend()) |
| 942 |
) |
| 943 |
ca.save(dir) |
| 944 |
|
| 945 |
# Add expired certificate (may need force flag) |
| 946 |
dyncerts = DynamicCerts(inst) |
| 947 |
try: |
| 948 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True, force=True) |
| 949 |
except Exception as e: |
| 950 |
log.info(f"Adding expired cert raised: {e}") |
| 951 |
return # Expected - server may reject expired certs |
| 952 |
|
| 953 |
# Verify certificate metadata shows expiration |
| 954 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 955 |
if ca_obj: |
| 956 |
not_before = ca_obj.get_attr_val_utf8('dsDynamicCertificateNotBefore') |
| 957 |
not_after = ca_obj.get_attr_val_utf8('dsDynamicCertificateNotAfter') |
| 958 |
log.info(f"Expired cert - Not Before: {not_before}") |
| 959 |
log.info(f"Expired cert - Not After: {not_after}") |
| 960 |
|
| 961 |
verify_status = ca_obj.get_attr_val_utf8('dsDynamicCertificateVerificationStatus') |
| 962 |
if verify_status: |
| 963 |
log.info(f"Verification status (should indicate expired): {verify_status}") |
| 964 |
|
| 965 |
# Cleanup |
| 966 |
dyncerts.del_cert(ca.nickname) |
| 967 |
|
| 968 |
finally: |
| 969 |
if not DEBUGGING: |
| 970 |
shutil.rmtree(dir, ignore_errors=True) |
| 971 |
|
| 972 |
|
| 973 |
def test_list_and_get_operations(topo, setup_tls): |
| 974 |
"""Test listing and retrieval operations |
| 975 |
|
| 976 |
:id: e1f2a3b4-1111-2222-3333-444444444444 |
| 977 |
:setup: Standalone Instance with TLS enabled |
| 978 |
:steps: |
| 979 |
1. Add CA and server certificates |
| 980 |
2. Use list() to get all certificate objects |
| 981 |
3. Use list_ca_certs() to get CA cert details |
| 982 |
4. Use list_certs() to get server cert details |
| 983 |
5. Use get_cert_obj() to retrieve specific certificate |
| 984 |
6. Use get_cert_details() to get formatted details |
| 985 |
:expectedresults: |
| 986 |
1. Certificates added successfully |
| 987 |
2. list() returns DSLdapObject instances |
| 988 |
3. list_ca_certs() returns dict with CA info |
| 989 |
4. list_certs() returns dict with server cert info |
| 990 |
5. get_cert_obj() returns DynamicCert object |
| 991 |
6. get_cert_details() returns formatted dictionary |
| 992 |
""" |
| 993 |
inst = setup_tls |
| 994 |
fix_crash_issue_7227(inst) |
| 995 |
dir = '/tmp/dyncert_list_test' |
| 996 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 997 |
|
| 998 |
try: |
| 999 |
dyncerts = DynamicCerts(inst) |
| 1000 |
|
| 1001 |
# Add certificates |
| 1002 |
ca = RSA_Certificate.generateRootCA("TestList_CA") |
| 1003 |
ca.save(dir) |
| 1004 |
dyncerts.add_cert(ca.pem, ca.nickname, ca=True) |
| 1005 |
|
| 1006 |
cert = ca.generateCertificate("TestList_Cert") |
| 1007 |
cert.save(dir) |
| 1008 |
dyncerts.add_cert(cert.p12, cert.nickname, pkcs12_password=RSA_Certificate.PKCS12_PASSWORD) |
| 1009 |
|
| 1010 |
# Test list() - returns DynamicCert objects |
| 1011 |
all_objs = dyncerts.list() |
| 1012 |
log.info(f"list() returned {len(all_objs)} objects") |
| 1013 |
assert any(isinstance(obj, DynamicCert) and obj.get_attr_val_utf8('cn') == ca.nickname |
| 1014 |
for obj in all_objs if obj._dn != DYNCERT_SUFFIX) |
| 1015 |
|
| 1016 |
# Test list_ca_certs() - returns list of dicts |
| 1017 |
ca_list = dyncerts.list_ca_certs() |
| 1018 |
log.info(f"list_ca_certs() returned {len(ca_list)} CAs") |
| 1019 |
assert any(c['cn'] == ca.nickname for c in ca_list) |
| 1020 |
ca_entry = next(c for c in ca_list if c['cn'] == ca.nickname) |
| 1021 |
assert 'subject' in ca_entry |
| 1022 |
assert 'issuer' in ca_entry |
| 1023 |
assert 'expires' in ca_entry |
| 1024 |
assert 'trust_flags' in ca_entry |
| 1025 |
|
| 1026 |
# Test list_certs() - returns list of dicts |
| 1027 |
cert_list = dyncerts.list_certs() |
| 1028 |
log.info(f"list_certs() returned {len(cert_list)} server certs") |
| 1029 |
assert any(c['cn'] == cert.nickname for c in cert_list) |
| 1030 |
|
| 1031 |
# Test get_cert_obj() - returns DynamicCert object |
| 1032 |
ca_obj = dyncerts.get_cert_obj(ca.nickname) |
| 1033 |
assert isinstance(ca_obj, DynamicCert) |
| 1034 |
assert ca_obj.get_attr_val_utf8('cn') == ca.nickname |
| 1035 |
|
| 1036 |
# Test get_cert_details() - returns formatted dict |
| 1037 |
ca_details = dyncerts.get_cert_details(ca.nickname) |
| 1038 |
assert ca_details['cn'] == ca.nickname |
| 1039 |
assert 'subject' in ca_details |
| 1040 |
assert 'issuer' in ca_details |
| 1041 |
assert 'expires' in ca_details |
| 1042 |
assert 'trust_flags' in ca_details |
| 1043 |
log.info(f"get_cert_details() returned: {ca_details}") |
| 1044 |
|
| 1045 |
# Cleanup |
| 1046 |
dyncerts.del_cert(ca.nickname) |
| 1047 |
dyncerts.del_cert(cert.nickname) |
| 1048 |
|
| 1049 |
finally: |
| 1050 |
if not DEBUGGING: |
| 1051 |
shutil.rmtree(dir, ignore_errors=True) |
| 1052 |
|
| 1053 |
|
| 1054 |
def pem_file_to_der(pem_file_path): |
| 1055 |
log.debug(f"Decoding cert pem: {pem_file_path}") |
| 1056 |
with open(pem_file_path, "rb") as f: |
| 1057 |
pem__bytes = f.read() |
| 1058 |
try: |
| 1059 |
return pem_to_der(pem__bytes) |
| 1060 |
except Exception as e: |
| 1061 |
raise ValueError(f"Failed to parse certificate '{pem_file_path}': {e}") |
| 1062 |
|
| 1063 |
|
| 1064 |
@pytest.mark.skipif(rpm_is_older("openssl", "3.5"), reason="OpenSSL too old to support PQC") |
| 1065 |
@pytest.mark.skipif(rpm_is_older("nss", "3.119.1"), reason="NSS too old to support PQC") |
| 1066 |
@pytest.mark.parametrize("with_private_key", [False, pytest.param(True, marks=pytest.mark.xfail(reason='cryptography module does not support ML-DSA keys')),]) |
| 1067 |
def test_mldsa_dynamic_certificates(topo, setup_tls, with_private_key): |
| 1068 |
"""Test ML-DSA (post-quantum) certificates with Dynamic Certificates feature |
| 1069 |
|
| 1070 |
:id: f2a3b4c5-2222-3333-4444-555555555555 |
| 1071 |
:setup: Standalone Instance with TLS enabled |
| 1072 |
:steps: |
| 1073 |
1. Generate ML-DSA-87 Root CA certificate using OpenSSL |
| 1074 |
2. Generate ML-DSA-65 server certificate signed by CA |
| 1075 |
3. Add CA certificate dynamically using DynamicCerts |
| 1076 |
4. Add server certificate dynamically using DynamicCerts (with or without private key) |
| 1077 |
5. Verify CA key algorithm shows ML-DSA |
| 1078 |
6. Verify server cert key algorithm shows ML-DSA |
| 1079 |
7. Verify all certificate metadata is correctly extracted |
| 1080 |
8. Verify CA is identified correctly |
| 1081 |
9. Verify server cert has private key based on parameter |
| 1082 |
10. Test certificate listing operations |
| 1083 |
:expectedresults: |
| 1084 |
1. ML-DSA CA generated successfully |
| 1085 |
2. ML-DSA server cert generated successfully |
| 1086 |
3. CA added without error |
| 1087 |
4. Server cert added without error (with or without key) |
| 1088 |
5. CA key algorithm contains ML-DSA |
| 1089 |
6. Server cert key algorithm contains ML-DSA |
| 1090 |
7. All metadata attributes present and correct |
| 1091 |
8. isCA is TRUE for CA |
| 1092 |
9. hasPrivateKey matches with_private_key parameter |
| 1093 |
10. Certificates appear in list operations |
| 1094 |
""" |
| 1095 |
inst = setup_tls |
| 1096 |
fix_crash_issue_7227(inst) |
| 1097 |
key_suffix = 'with_key' if with_private_key else 'without_key' |
| 1098 |
dir = f'/tmp/dyncert_mldsa_test_{key_suffix}' |
| 1099 |
os.makedirs(dir, 0o700, exist_ok=True) |
| 1100 |
|
| 1101 |
ca_config = """ |
| 1102 |
[ req ] |
| 1103 |
distinguished_name = req_distinguished_name |
| 1104 |
policy = policy_match |
| 1105 |
x509_extensions = v3_ca |
| 1106 |
|
| 1107 |
[ policy_match ] |
| 1108 |
countryName = optional |
| 1109 |
stateOrProvinceName = optional |
| 1110 |
organizationName = optional |
| 1111 |
organizationalUnitName = optional |
| 1112 |
commonName = supplied |
| 1113 |
emailAddress = optional |
| 1114 |
|
| 1115 |
[ req_distinguished_name ] |
| 1116 |
countryName = Country Name (2 letter code) |
| 1117 |
countryName_default = US |
| 1118 |
countryName_min = 2 |
| 1119 |
countryName_max = 2 |
| 1120 |
|
| 1121 |
stateOrProvinceName = State or Province Name (full name) |
| 1122 |
localityName = Locality Name (eg, city) |
| 1123 |
|
| 1124 |
0.organizationName = Organization Name (eg, company) |
| 1125 |
0.organizationName_default = TestMLDSA-CA-Org |
| 1126 |
|
| 1127 |
organizationalUnitName = Organizational Unit Name (eg, section) |
| 1128 |
commonName = Common Name (e.g. server FQDN or YOUR name) |
| 1129 |
commonName_max = 64 |
| 1130 |
|
| 1131 |
[ v3_ca ] |
| 1132 |
subjectKeyIdentifier = hash |
| 1133 |
authorityKeyIdentifier = keyid:always,issuer |
| 1134 |
basicConstraints = critical,CA:true |
| 1135 |
keyUsage = critical, keyCertSign |
| 1136 |
""" |
| 1137 |
|
| 1138 |
cert_config = """ |
| 1139 |
[ req ] |
| 1140 |
distinguished_name = req_distinguished_name |
| 1141 |
policy = policy_match |
| 1142 |
x509_extensions = v3_cert |
| 1143 |
|
| 1144 |
[ policy_match ] |
| 1145 |
countryName = optional |
| 1146 |
stateOrProvinceName = optional |
| 1147 |
organizationName = optional |
| 1148 |
organizationalUnitName = optional |
| 1149 |
commonName = supplied |
| 1150 |
emailAddress = optional |
| 1151 |
|
| 1152 |
[ req_distinguished_name ] |
| 1153 |
countryName = Country Name (2 letter code) |
| 1154 |
countryName_default = US |
| 1155 |
countryName_min = 2 |
| 1156 |
countryName_max = 2 |
| 1157 |
|
| 1158 |
stateOrProvinceName = State or Province Name (full name) |
| 1159 |
localityName = Locality Name (eg, city) |
| 1160 |
|
| 1161 |
0.organizationName = Organization Name (eg, company) |
| 1162 |
0.organizationName_default = TestMLDSA-Org |
| 1163 |
|
| 1164 |
organizationalUnitName = Organizational Unit Name (eg, section) |
| 1165 |
commonName = Common Name (e.g. server FQDN or YOUR name) |
| 1166 |
commonName_max = 64 |
| 1167 |
|
| 1168 |
[ v3_cert ] |
| 1169 |
basicConstraints = critical,CA:false |
| 1170 |
subjectAltName = DNS:localhost,IP:127.0.0.1 |
| 1171 |
keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment |
| 1172 |
extendedKeyUsage = clientAuth, serverAuth |
| 1173 |
""" |
| 1174 |
|
| 1175 |
try: |
| 1176 |
hostname = socket.gethostname() |
| 1177 |
|
| 1178 |
# Write config files |
| 1179 |
ca_conf_file = f"{dir}/ca.conf" |
| 1180 |
cert_conf_file = f"{dir}/cert.conf" |
| 1181 |
|
| 1182 |
with open(ca_conf_file, 'w') as f: |
| 1183 |
f.write(ca_config) |
| 1184 |
|
| 1185 |
with open(cert_conf_file, 'w') as f: |
| 1186 |
f.write(cert_config) |
| 1187 |
|
| 1188 |
# Generate ML-DSA-87 CA certificate |
| 1189 |
log.info("Generating ML-DSA-87 CA certificate...") |
| 1190 |
|
| 1191 |
# Generate CA private key |
| 1192 |
result = subprocess.run( |
| 1193 |
['openssl', 'genpkey', '-algorithm', 'ML-DSA-87', '-out', f'{dir}/ca.key'], |
| 1194 |
capture_output=True, text=True, check=True |
| 1195 |
) |
| 1196 |
log.debug(f"CA key generation output: {result.stderr}") |
| 1197 |
|
| 1198 |
# Generate self-signed CA certificate |
| 1199 |
result = subprocess.run( |
| 1200 |
['openssl', 'req', '-x509', '-new', '-sha256', '-key', f'{dir}/ca.key', |
| 1201 |
'-nodes', '-days', '3650', '-config', ca_conf_file, |
| 1202 |
'-subj', f'/CN=TestMLDSA-CA/O=TestMLDSA-CA-Org/C=US', |
| 1203 |
'-out', f'{dir}/ca.pem'], |
| 1204 |
capture_output=True, text=True, check=True |
| 1205 |
) |
| 1206 |
log.debug(f"CA cert generation output: {result.stderr}") |
| 1207 |
|
| 1208 |
# Generate ML-DSA-65 server certificate |
| 1209 |
log.info("Generating ML-DSA-65 server certificate...") |
| 1210 |
|
| 1211 |
# Generate server private key |
| 1212 |
result = subprocess.run( |
| 1213 |
['openssl', 'genpkey', '-algorithm', 'ML-DSA-65', '-out', f'{dir}/cert.key'], |
| 1214 |
capture_output=True, text=True, check=True |
| 1215 |
) |
| 1216 |
log.debug(f"Server key generation output: {result.stderr}") |
| 1217 |
|
| 1218 |
# Generate certificate signing request |
| 1219 |
result = subprocess.run( |
| 1220 |
['openssl', 'req', '-new', '-sha256', '-key', f'{dir}/cert.key', |
| 1221 |
'-nodes', '-config', cert_conf_file, |
| 1222 |
'-subj', f'/CN=TestMLDSA-Cert/O=TestMLDSA-Org/C=US', |
| 1223 |
'-out', f'{dir}/cert.csr'], |
| 1224 |
capture_output=True, text=True, check=True |
| 1225 |
) |
| 1226 |
log.debug(f"CSR generation output: {result.stderr}") |
| 1227 |
|
| 1228 |
# Sign server certificate with CA |
| 1229 |
result = subprocess.run( |
| 1230 |
['openssl', 'x509', '-req', '-sha256', '-days', '3650', |
| 1231 |
'-extensions', 'v3_cert', '-extfile', cert_conf_file, |
| 1232 |
'-in', f'{dir}/cert.csr', '-CA', f'{dir}/ca.pem', |
| 1233 |
'-CAkey', f'{dir}/ca.key', '-CAcreateserial', |
| 1234 |
'-out', f'{dir}/cert.pem'], |
| 1235 |
capture_output=True, text=True, check=True |
| 1236 |
) |
| 1237 |
log.debug(f"Cert signing output: {result.stderr}") |
| 1238 |
|
| 1239 |
# Create PKCS12 bundle for server certificate |
| 1240 |
result = subprocess.run( |
| 1241 |
['openssl', 'pkcs12', '-export', '-inkey', f'{dir}/cert.key', |
| 1242 |
'-in', f'{dir}/cert.pem', '-name', 'mldsa-server-cert', |
| 1243 |
'-out', f'{dir}/cert.p12', '-passout', 'pass:mldsa123'], |
| 1244 |
capture_output=True, text=True, check=True |
| 1245 |
) |
| 1246 |
log.debug(f"PKCS12 creation output: {result.stderr}") |
| 1247 |
|
| 1248 |
# Display certificate details for debugging |
| 1249 |
result = subprocess.run( |
| 1250 |
['openssl', 'x509', '-text', '-noout', '-in', f'{dir}/ca.pem'], |
| 1251 |
capture_output=True, text=True, check=True |
| 1252 |
) |
| 1253 |
log.info(f"CA Certificate details:\n{result.stdout}") |
| 1254 |
|
| 1255 |
result = subprocess.run( |
| 1256 |
['openssl', 'x509', '-text', '-noout', '-in', f'{dir}/cert.pem'], |
| 1257 |
capture_output=True, text=True, check=True |
| 1258 |
) |
| 1259 |
log.info(f"Server Certificate details:\n{result.stdout}") |
| 1260 |
|
| 1261 |
# Add certificates using DynamicCerts |
| 1262 |
dyncerts = DynamicCerts(inst) |
| 1263 |
|
| 1264 |
# Add CA certificate |
| 1265 |
log.info("Adding ML-DSA CA certificate to Dynamic Certificates...") |
| 1266 |
dyncerts.add_cert(f'{dir}/ca.pem', 'TestMLDSA_CA', ca=True) |
| 1267 |
|
| 1268 |
# Add server certificate (with or without private key based on parameter) |
| 1269 |
if with_private_key: |
| 1270 |
log.info("Adding ML-DSA server certificate WITH private key to Dynamic Certificates...") |
| 1271 |
# Note: cannot use dyncerts.add_cert with PKCS12 because python3 cryptography |
| 1272 |
# module does not yet support ML-DSA. Instead, use OpenSSL to convert to DER |
| 1273 |
# and use DynamicCerts.create() directly |
| 1274 |
# dyncerts.add_cert(f'{dir}/cert.p12', 'TestMLDSA_Cert', pkcs12_password='mldsa123') |
| 1275 |
|
| 1276 |
# Convert certificate to DER using OpenSSL |
| 1277 |
result = subprocess.run( |
| 1278 |
['openssl', 'x509', '-in', f'{dir}/cert.pem', '-outform', 'DER', '-out', f'{dir}/cert.der'], |
| 1279 |
capture_output=True, text=True, check=True |
| 1280 |
) |
| 1281 |
|
| 1282 |
# Convert private key to DER PrivateKeyInfo (PKCS#8) using OpenSSL |
| 1283 |
result = subprocess.run( |
| 1284 |
['openssl', 'pkcs8', '-topk8', '-nocrypt', '-in', f'{dir}/cert.key', |
| 1285 |
'-outform', 'DER', '-out', f'{dir}/cert_key.der'], |
| 1286 |
capture_output=True, text=True, check=True |
| 1287 |
) |
| 1288 |
|
| 1289 |
# Read DER data |
| 1290 |
with open(f'{dir}/cert.der', 'rb') as f: |
| 1291 |
cert_der = f.read() |
| 1292 |
with open(f'{dir}/cert_key.der', 'rb') as f: |
| 1293 |
key_der = f.read() |
| 1294 |
|
| 1295 |
# Create entry directly with DER data |
| 1296 |
properties = { |
| 1297 |
DYCATTR_CN: 'TestMLDSA_Cert', |
| 1298 |
DYCATTR_CERTDER: cert_der, |
| 1299 |
DYCATTR_PKEYDER: key_der, |
| 1300 |
} |
| 1301 |
rdn = f"{DYCATTR_CN}=TestMLDSA_Cert" |
| 1302 |
dyncerts.create(rdn=rdn, properties=properties) |
| 1303 |
else: |
| 1304 |
log.info("Adding ML-DSA server certificate WITHOUT private key to Dynamic Certificates...") |
| 1305 |
dyncerts.add_cert(f'{dir}/cert.pem', 'TestMLDSA_Cert') |
| 1306 |
|
| 1307 |
# Verify CA certificate metadata |
| 1308 |
log.info("Verifying CA certificate metadata...") |
| 1309 |
ca_obj = dyncerts.get_cert_obj('TestMLDSA_CA') |
| 1310 |
assert ca_obj is not None |
| 1311 |
|
| 1312 |
# Verify subject and issuer |
| 1313 |
ca_subject = ca_obj.get_attr_val_utf8('dsDynamicCertificateSubject') |
| 1314 |
ca_issuer = ca_obj.get_attr_val_utf8('dsDynamicCertificateIssuer') |
| 1315 |
log.info(f"CA Subject: {ca_subject}") |
| 1316 |
log.info(f"CA Issuer: {ca_issuer}") |
| 1317 |
assert 'TestMLDSA-CA' in ca_subject |
| 1318 |
assert ca_subject == ca_issuer # Self-signed |
| 1319 |
|
| 1320 |
# Verify CA flags |
| 1321 |
is_ca = ca_obj.get_attr_val_utf8_l('dsDynamicCertificateIsCA') |
| 1322 |
is_root_ca = ca_obj.get_attr_val_utf8_l('dsDynamicCertificateIsRootCA') |
| 1323 |
log.info(f"CA isCA: {is_ca}") |
| 1324 |
log.info(f"CA isRootCA: {is_root_ca}") |
| 1325 |
assert is_ca == 'true' |
| 1326 |
assert is_root_ca == 'true' |
| 1327 |
|
| 1328 |
# Verify CA key algorithm contains ML-DSA |
| 1329 |
ca_key_algo = ca_obj.get_attr_val_utf8('dsDynamicCertificateKeyAlgorithm') |
| 1330 |
log.info(f"CA Key Algorithm: {ca_key_algo}") |
| 1331 |
assert ca_key_algo is not None |
| 1332 |
assert 'ML-DSA' in ca_key_algo.upper() or 'DILITHIUM' in ca_key_algo.upper() or '2.16.840.1.101.3.4.3' in ca_key_algo |
| 1333 |
|
| 1334 |
# Verify CA has no private key (PEM file only contains cert) |
| 1335 |
ca_has_key = ca_obj.get_attr_val_utf8_l('dsDynamicCertificateHasPrivateKey') |
| 1336 |
log.info(f"CA hasPrivateKey: {ca_has_key}") |
| 1337 |
assert ca_has_key == 'false' |
| 1338 |
|
| 1339 |
# Verify server certificate metadata |
| 1340 |
log.info("Verifying server certificate metadata...") |
| 1341 |
cert_obj = dyncerts.get_cert_obj('TestMLDSA_Cert') |
| 1342 |
assert cert_obj is not None |
| 1343 |
|
| 1344 |
# Verify subject |
| 1345 |
cert_subject = cert_obj.get_attr_val_utf8('dsDynamicCertificateSubject') |
| 1346 |
cert_issuer = cert_obj.get_attr_val_utf8('dsDynamicCertificateIssuer') |
| 1347 |
log.info(f"Server Cert Subject: {cert_subject}") |
| 1348 |
log.info(f"Server Cert Issuer: {cert_issuer}") |
| 1349 |
assert 'TestMLDSA-Cert' in cert_subject |
| 1350 |
|
| 1351 |
# Verify server cert is not a CA |
| 1352 |
cert_is_ca = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateIsCA') |
| 1353 |
log.info(f"Server Cert isCA: {cert_is_ca}") |
| 1354 |
assert cert_is_ca == 'false' |
| 1355 |
|
| 1356 |
# Verify server cert key algorithm contains ML-DSA |
| 1357 |
cert_key_algo = cert_obj.get_attr_val_utf8('dsDynamicCertificateKeyAlgorithm') |
| 1358 |
log.info(f"Server Cert Key Algorithm: {cert_key_algo}") |
| 1359 |
assert cert_key_algo is not None |
| 1360 |
assert 'ML-DSA' in cert_key_algo.upper() |
| 1361 |
|
| 1362 |
# Verify server cert has private key based on parameter |
| 1363 |
cert_has_key = cert_obj.get_attr_val_utf8_l('dsDynamicCertificateHasPrivateKey') |
| 1364 |
log.info(f"Server Cert hasPrivateKey: {cert_has_key}") |
| 1365 |
expected_has_key = 'true' if with_private_key else 'false' |
| 1366 |
assert cert_has_key == expected_has_key, \ |
| 1367 |
f"Expected hasPrivateKey={expected_has_key}, got {cert_has_key}" |
| 1368 |
|
| 1369 |
# Verify serial numbers are present |
| 1370 |
ca_serial = ca_obj.get_attr_val_utf8('dsDynamicCertificateSerialNumber') |
| 1371 |
cert_serial = cert_obj.get_attr_val_utf8('dsDynamicCertificateSerialNumber') |
| 1372 |
log.info(f"CA Serial: {ca_serial}") |
| 1373 |
log.info(f"Server Cert Serial: {cert_serial}") |
| 1374 |
assert ca_serial is not None and len(ca_serial) > 0 |
| 1375 |
assert cert_serial is not None and len(cert_serial) > 0 |
| 1376 |
|
| 1377 |
# Verify validity dates |
| 1378 |
ca_not_before = ca_obj.get_attr_val_utf8('dsDynamicCertificateNotBefore') |
| 1379 |
ca_not_after = ca_obj.get_attr_val_utf8('dsDynamicCertificateNotAfter') |
| 1380 |
cert_not_before = cert_obj.get_attr_val_utf8('dsDynamicCertificateNotBefore') |
| 1381 |
cert_not_after = cert_obj.get_attr_val_utf8('dsDynamicCertificateNotAfter') |
| 1382 |
log.info(f"CA Valid: {ca_not_before} to {ca_not_after}") |
| 1383 |
log.info(f"Server Cert Valid: {cert_not_before} to {cert_not_after}") |
| 1384 |
assert ca_not_before is not None |
| 1385 |
assert ca_not_after is not None |
| 1386 |
assert cert_not_before is not None |
| 1387 |
assert cert_not_after is not None |
| 1388 |
|
| 1389 |
# Verify Subject Alternative Names on server cert |
| 1390 |
cert_san = cert_obj.get_attr_vals_utf8('dsDynamicCertificateSubjectAltName') |
| 1391 |
if cert_san: |
| 1392 |
log.info(f"Server Cert SANs: {cert_san}") |
| 1393 |
san_str = ' '.join(cert_san) |
| 1394 |
assert 'localhost' in san_str or '127.0.0.1' in san_str |
| 1395 |
|
| 1396 |
# Test listing operations |
| 1397 |
log.info("Testing certificate listing operations...") |
| 1398 |
|
| 1399 |
ca_list = dyncerts.list_ca_certs() |
| 1400 |
mldsa_cas = [c for c in ca_list if c['cn'] == 'TestMLDSA_CA'] |
| 1401 |
assert len(mldsa_cas) == 1 |
| 1402 |
log.info(f"Found ML-DSA CA in list: {mldsa_cas[0]}") |
| 1403 |
|
| 1404 |
cert_list = dyncerts.list_certs() |
| 1405 |
mldsa_certs = [c for c in cert_list if c['cn'] == 'TestMLDSA_Cert'] |
| 1406 |
assert len(mldsa_certs) == 1 |
| 1407 |
log.info(f"Found ML-DSA server cert in list: {mldsa_certs[0]}") |
| 1408 |
|
| 1409 |
# Test get_cert_details |
| 1410 |
ca_details = dyncerts.get_cert_details('TestMLDSA_CA') |
| 1411 |
assert ca_details is not None |
| 1412 |
assert ca_details['cn'] == 'TestMLDSA_CA' |
| 1413 |
assert 'subject' in ca_details |
| 1414 |
assert 'issuer' in ca_details |
| 1415 |
log.info(f"CA details: {ca_details}") |
| 1416 |
|
| 1417 |
cert_details = dyncerts.get_cert_details('TestMLDSA_Cert') |
| 1418 |
assert cert_details is not None |
| 1419 |
assert cert_details['cn'] == 'TestMLDSA_Cert' |
| 1420 |
log.info(f"Server cert details: {cert_details}") |
| 1421 |
|
| 1422 |
# Cleanup |
| 1423 |
log.info("Cleaning up ML-DSA certificates...") |
| 1424 |
dyncerts.del_cert('TestMLDSA_CA') |
| 1425 |
dyncerts.del_cert('TestMLDSA_Cert') |
| 1426 |
|
| 1427 |
key_status = "with private key" if with_private_key else "without private key" |
| 1428 |
log.info(f"ML-DSA Dynamic Certificates test ({key_status}) completed successfully!") |
| 1429 |
|
| 1430 |
except subprocess.CalledProcessError as e: |
| 1431 |
log.error(f"OpenSSL command failed: {e}") |
| 1432 |
log.error(f"stdout: {e.stdout}") |
| 1433 |
log.error(f"stderr: {e.stderr}") |
| 1434 |
raise |
| 1435 |
except Exception as e: |
| 1436 |
log.error(f"Test failed with exception: {e}") |
| 1437 |
raise |
| 1438 |
finally: |
| 1439 |
if not DEBUGGING: |
| 1440 |
shutil.rmtree(dir, ignore_errors=True) |
| 1441 |
|
| 1442 |
|
| 1443 |
if __name__ == '__main__': |
| 1444 |
CURRENT_FILE = os.path.realpath(__file__) |
| 1445 |
pytest.main(["-s", "-v", CURRENT_FILE]) |