|
| 1 | +"""Tests for certificate chain handling in the uacrypto module. |
| 2 | +
|
| 3 | +This module tests the enhanced x509_from_der function which now supports: |
| 4 | +1. Loading single certificates (original functionality) |
| 5 | +2. Loading the first certificate from a certificate chain |
| 6 | +3. Proper error handling for invalid certificate data |
| 7 | +
|
| 8 | +These tests ensure that the function correctly handles certificate chains |
| 9 | +that some OPC UA servers might provide, improving compatibility. |
| 10 | +""" |
| 11 | + |
| 12 | +import pytest |
| 13 | +from pathlib import Path |
| 14 | + |
| 15 | +from asyncua.crypto import uacrypto |
| 16 | +from cryptography import x509 |
| 17 | +from cryptography.hazmat.backends import default_backend |
| 18 | +from cryptography.hazmat.primitives import serialization |
| 19 | + |
| 20 | +pytestmark = pytest.mark.asyncio |
| 21 | + |
| 22 | +BASE_DIR = Path(__file__).parent.parent |
| 23 | +EXAMPLE_CERT_PATH = BASE_DIR / "examples" / "certificate-example.der" |
| 24 | +EXAMPLE_CERT_3072_PATH = BASE_DIR / "examples" / "certificate-3072-example.der" |
| 25 | +PEER_CERT_PATH = BASE_DIR / "examples" / "certificates" / "peer-certificate-example-1.der" |
| 26 | + |
| 27 | + |
| 28 | +def create_cert_chain(): |
| 29 | + """Create a certificate chain for testing. |
| 30 | +
|
| 31 | + This function creates a simulated certificate chain by concatenating |
| 32 | + two different certificates. In a real-world scenario, certificate chains |
| 33 | + would contain a server certificate followed by intermediate CA certificates. |
| 34 | +
|
| 35 | + Returns: |
| 36 | + tuple: (first_cert_data, cert_chain_data) |
| 37 | + - first_cert_data: The DER-encoded data of the first certificate |
| 38 | + - cert_chain_data: The DER-encoded data of the entire certificate chain |
| 39 | + """ |
| 40 | + # Load the example certificates |
| 41 | + with open(EXAMPLE_CERT_PATH, "rb") as f: |
| 42 | + cert1_data = f.read() |
| 43 | + |
| 44 | + with open(PEER_CERT_PATH, "rb") as f: |
| 45 | + cert2_data = f.read() |
| 46 | + |
| 47 | + # Create a certificate chain by concatenating two different certificates |
| 48 | + cert_chain = cert1_data + cert2_data |
| 49 | + |
| 50 | + return cert1_data, cert_chain |
| 51 | + |
| 52 | + |
| 53 | +def test_x509_from_der_single_cert(): |
| 54 | + """Test that x509_from_der works with a single certificate.""" |
| 55 | + # Test with the standard example certificate |
| 56 | + with open(EXAMPLE_CERT_PATH, "rb") as f: |
| 57 | + cert_data = f.read() |
| 58 | + |
| 59 | + cert = uacrypto.x509_from_der(cert_data) |
| 60 | + assert cert is not None |
| 61 | + assert isinstance(cert, x509.Certificate) |
| 62 | + |
| 63 | + # Test with the 3072-bit example certificate |
| 64 | + with open(EXAMPLE_CERT_3072_PATH, "rb") as f: |
| 65 | + cert_data_3072 = f.read() |
| 66 | + |
| 67 | + cert_3072 = uacrypto.x509_from_der(cert_data_3072) |
| 68 | + assert cert_3072 is not None |
| 69 | + assert isinstance(cert_3072, x509.Certificate) |
| 70 | + |
| 71 | + # Test with the peer certificate |
| 72 | + with open(PEER_CERT_PATH, "rb") as f: |
| 73 | + peer_cert_data = f.read() |
| 74 | + |
| 75 | + peer_cert = uacrypto.x509_from_der(peer_cert_data) |
| 76 | + assert peer_cert is not None |
| 77 | + assert isinstance(peer_cert, x509.Certificate) |
| 78 | + |
| 79 | + |
| 80 | +def test_x509_from_der_cert_chain(): |
| 81 | + """Test that x509_from_der works with a certificate chain.""" |
| 82 | + first_cert_data, cert_chain = create_cert_chain() |
| 83 | + |
| 84 | + # Load the certificate chain using x509_from_der |
| 85 | + cert = uacrypto.x509_from_der(cert_chain) |
| 86 | + |
| 87 | + # Verify that the certificate was loaded correctly |
| 88 | + assert cert is not None |
| 89 | + assert isinstance(cert, x509.Certificate) |
| 90 | + |
| 91 | + # Verify that the loaded certificate is the first one in the chain |
| 92 | + # by comparing it with the original certificate |
| 93 | + original_cert = x509.load_der_x509_certificate(first_cert_data, default_backend()) |
| 94 | + assert cert.public_bytes(serialization.Encoding.DER) == original_cert.public_bytes(serialization.Encoding.DER) |
| 95 | + |
| 96 | + |
| 97 | +def test_x509_from_der_invalid_data(): |
| 98 | + """Test that x509_from_der handles invalid data correctly.""" |
| 99 | + # Test with None |
| 100 | + assert uacrypto.x509_from_der(None) is None |
| 101 | + |
| 102 | + # Test with empty bytes |
| 103 | + assert uacrypto.x509_from_der(b"") is None |
| 104 | + |
| 105 | + # Test with invalid data that doesn't start with a SEQUENCE tag |
| 106 | + with pytest.raises(ValueError): |
| 107 | + uacrypto.x509_from_der(b"invalid data") |
| 108 | + |
| 109 | + # Test with data that starts with a SEQUENCE tag but is otherwise invalid |
| 110 | + with pytest.raises(ValueError): |
| 111 | + uacrypto.x509_from_der(b"\x30\x03\x01\x02\x03") |
0 commit comments