diff --git a/bindings/c/include/libsql.h b/bindings/c/include/libsql.h index 505c4b291d..eec2d5b4d9 100644 --- a/bindings/c/include/libsql.h +++ b/bindings/c/include/libsql.h @@ -41,6 +41,7 @@ typedef struct { int sync_interval; char with_webpki; char offline; + const char *remote_encryption_key; } libsql_config; typedef const libsql_connection *libsql_connection_t; diff --git a/bindings/c/src/lib.rs b/bindings/c/src/lib.rs index e9ebe575d4..5757cd5f7e 100644 --- a/bindings/c/src/lib.rs +++ b/bindings/c/src/lib.rs @@ -98,6 +98,7 @@ pub unsafe extern "C" fn libsql_open_sync( sync_interval: 0, with_webpki: 0, offline: 0, + remote_encryption_key: std::ptr::null(), }; libsql_open_sync_with_config(config, out_db, out_err_msg) } @@ -121,6 +122,7 @@ pub unsafe extern "C" fn libsql_open_sync_with_webpki( sync_interval: 0, with_webpki: 1, offline: 0, + remote_encryption_key: std::ptr::null(), }; libsql_open_sync_with_config(config, out_db, out_err_msg) } @@ -271,6 +273,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config( .build(); builder = builder.connector(https); } + if !config.remote_encryption_key.is_null() { + let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) }; + let key = match key.to_str() { + Ok(k) => k, + Err(e) => { + set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg); + return 5; + } + }; + builder = builder.remote_encryption(libsql::EncryptionContext { + key: libsql::EncryptionKey::Base64Encoded(key.to_string()), + }); + }; match RT.block_on(builder.build()) { Ok(db) => { let db = Box::leak(Box::new(libsql_database { db })); @@ -325,6 +340,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config( let config = libsql::EncryptionConfig::new(libsql::Cipher::Aes256Cbc, key); builder = builder.encryption_config(config) }; + if !config.remote_encryption_key.is_null() { + let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) }; + let key = match key.to_str() { + Ok(k) => k, + Err(e) => { + set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg); + return 5; + } + }; + builder = builder.remote_encryption(libsql::EncryptionContext { + key: libsql::EncryptionKey::Base64Encoded(key.to_string()), + }); + }; match RT.block_on(builder.build()) { Ok(db) => { let db = Box::leak(Box::new(libsql_database { db })); diff --git a/bindings/c/src/types.rs b/bindings/c/src/types.rs index 2918e22bd0..de679a917a 100644 --- a/bindings/c/src/types.rs +++ b/bindings/c/src/types.rs @@ -15,6 +15,7 @@ pub struct libsql_config { pub sync_interval: std::ffi::c_int, pub with_webpki: std::ffi::c_char, pub offline: std::ffi::c_char, + pub remote_encryption_key: *const std::ffi::c_char, } #[derive(Clone, Debug)]