|
| 1 | +from lxml.etree import _Element |
| 2 | +from xmlsec.constants import __Transform as Transform |
| 3 | +import ctypes |
| 4 | + |
| 5 | +# Load the shared library (assuming it's named `libxmlsec.so` or similar) |
| 6 | +libxmlsec = ctypes.CDLL("../template.c") |
| 7 | + |
| 8 | +# Define the Python wrapper for the C function |
| 9 | +libxmlsec.PyXmlSec_TemplateAddEncryptedKey.argtypes = [ |
| 10 | + ctypes.POINTER(_Element), # Assuming _Element is compatible |
| 11 | + ctypes.POINTER(Transform), |
| 12 | + ctypes.c_char_p, # id |
| 13 | + ctypes.c_char_p, # type |
| 14 | + ctypes.c_char_p # recipient |
| 15 | +] |
| 16 | +libxmlsec.PyXmlSec_TemplateAddEncryptedKey.restype = ctypes.POINTER(_Element) |
| 17 | + |
| 18 | +def add_encrypted_key( |
| 19 | + node: _Element, method: Transform, id: str | None = None, type: str | None = None, recipient: str | None = None |
| 20 | +) -> _Element: |
| 21 | + """ |
| 22 | + Python wrapper for the C function `PyXmlSec_TemplateAddEncryptedKey`. |
| 23 | +
|
| 24 | + :param node: The XML node to which the encrypted key will be added. |
| 25 | + :param method: The encryption method. |
| 26 | + :param id: Optional ID for the key. |
| 27 | + :param type: Optional type for the key. |
| 28 | + :param recipient: Optional recipient for the key. |
| 29 | + :return: The modified XML node. |
| 30 | + """ |
| 31 | + # Convert Python strings to C strings |
| 32 | + c_id = ctypes.c_char_p(id.encode('utf-8') if id else None) |
| 33 | + c_type = ctypes.c_char_p(type.encode('utf-8') if type else None) |
| 34 | + c_recipient = ctypes.c_char_p(recipient.encode('utf-8') if recipient else None) |
| 35 | + |
| 36 | + # Call the C function |
| 37 | + result = libxmlsec.PyXmlSec_TemplateAddEncryptedKey(node, method, c_id, c_type, c_recipient) |
| 38 | + |
| 39 | + if not result: |
| 40 | + raise RuntimeError("Failed to add encrypted key") |
| 41 | + |
| 42 | + return result |
0 commit comments