Hessra FFI
C FFI bindings for the Hessra SDK token verification and configuration functionality.
Overview
This crate provides C-compatible bindings for the core functionality of the Hessra SDK, focused specifically on token verification and configuration management. It is designed to be used in contexts where the full Rust SDK would be impractical, such as in database plugins.
Features
- Token parsing and verification with optional subject and resource checking
- Service chain token validation
- Public key management (loading from files or strings)
- Configuration management (create, load, and modify)
- Memory-safe FFI interface with proper resource cleanup
- Comprehensive error handling
Building
As a Dynamic Library
The resulting library will be in target/release/libhessra_ffi.so (Linux), target/release/libhessra_ffi.dylib (macOS), or target/release/hessra_ffi.dll (Windows).
As a Static Library
The resulting library will be in target/release/libhessra_ffi.a (Unix) or target/release/hessra_ffi.lib (Windows).
Using in C Projects
Include the hessra_ffi.h header file in your project and link against the dynamic or static library.
Basic Usage
int
Token Verification
// Load a public key from a file
HessraPublicKey* public_key = NULL;
result = ;
if
// Verify a token
result = ;
if
// Don't forget to free resources
;
Configuration Management
// Create a new configuration
HessraConfig* config = NULL;
result = ;
if
// Set a public key in the configuration
result = ;
if
// Get a public key from the configuration
HessraPublicKey* retrieved_key = NULL;
result = ;
if
// Clean up
;
;
Examples
A more comprehensive example can be found in the examples/test.c file, which demonstrates:
- Library initialization
- Version retrieval
- Configuration creation
- Public key loading
- Token verification
- Proper resource cleanup
To build and run the example:
# Build the library
# Build the example (Unix-like systems)
# Run the example (Linux)
LD_LIBRARY_PATH=./target/release
# Run the example (macOS)
DYLD_LIBRARY_PATH=./target/release
Testing
The FFI library is tested through:
- Example Program: The
examples/test.cfile serves as a basic functional test - Automated Testing: Rust tests that call the FFI functions to verify their behavior
- Memory Safety Testing: Valgrind is used to check for memory leaks and other memory-related issues
To run the memory safety tests:
# Build the example in debug mode
# Run with Valgrind
License
Apache License 2.0