Skip to content

Commit

Permalink
[Python] Fix Cert API argument type errors
Browse files Browse the repository at this point in the history
NativeLibraryHandleMethodArguments correctly setting the argument
types causes argument type errors:
ctypes.ArgumentError: argument 1: TypeError: expected LP_c_ubyte instance instead of bytes

We can safely cast bytes as the native side marks it const.
  • Loading branch information
agners committed Jun 18, 2024
1 parent 759b7b0 commit f22deb0
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/controller/python/chip/credentials/cert.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ def convert_x509_cert_to_chip_cert(x509Cert: bytes) -> bytes:
"""Converts a x509 certificate to CHIP Certificate."""
output_buffer = (ctypes.c_uint8 * 1024)()
output_size = ctypes.c_size_t(1024)
ptr_type = ctypes.POINTER(ctypes.c_uint8)

_handle().pychip_ConvertX509CertToChipCert(x509Cert, len(x509Cert), output_buffer, ctypes.byref(output_size)).raise_on_error()
_handle().pychip_ConvertX509CertToChipCert(ctypes.cast(x509Cert, ptr_type), len(x509Cert),
ctypes.cast(output_buffer, ptr_type), ctypes.byref(output_size)).raise_on_error()

return bytes(output_buffer)[:output_size.value]

Expand All @@ -45,7 +47,9 @@ def convert_chip_cert_to_x509_cert(chipCert: bytes) -> bytes:
"""Converts a x509 certificate to CHIP Certificate."""
output_buffer = (ctypes.c_byte * 1024)()
output_size = ctypes.c_size_t(1024)
ptr_type = ctypes.POINTER(ctypes.c_uint8)

_handle().pychip_ConvertChipCertToX509Cert(chipCert, len(chipCert), output_buffer, ctypes.byref(output_size)).raise_on_error()
_handle().pychip_ConvertChipCertToX509Cert(ctypes.cast(chipCert, ptr_type), len(chipCert),
ctypes.cast(output_buffer, ptr_type), ctypes.byref(output_size)).raise_on_error()

return bytes(output_buffer)[:output_size.value]

0 comments on commit f22deb0

Please sign in to comment.