Skip to content

Commit ce57bf0

Browse files
authored
Add remote encryption to C bindings (#2130)
2 parents 055a109 + 4e1be8c commit ce57bf0

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

bindings/c/include/libsql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct {
4141
int sync_interval;
4242
char with_webpki;
4343
char offline;
44+
const char *remote_encryption_key;
4445
} libsql_config;
4546

4647
typedef const libsql_connection *libsql_connection_t;

bindings/c/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub unsafe extern "C" fn libsql_open_sync(
9898
sync_interval: 0,
9999
with_webpki: 0,
100100
offline: 0,
101+
remote_encryption_key: std::ptr::null(),
101102
};
102103
libsql_open_sync_with_config(config, out_db, out_err_msg)
103104
}
@@ -121,6 +122,7 @@ pub unsafe extern "C" fn libsql_open_sync_with_webpki(
121122
sync_interval: 0,
122123
with_webpki: 1,
123124
offline: 0,
125+
remote_encryption_key: std::ptr::null(),
124126
};
125127
libsql_open_sync_with_config(config, out_db, out_err_msg)
126128
}
@@ -271,6 +273,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
271273
.build();
272274
builder = builder.connector(https);
273275
}
276+
if !config.remote_encryption_key.is_null() {
277+
let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) };
278+
let key = match key.to_str() {
279+
Ok(k) => k,
280+
Err(e) => {
281+
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
282+
return 5;
283+
}
284+
};
285+
builder = builder.remote_encryption(libsql::EncryptionContext {
286+
key: libsql::EncryptionKey::Base64Encoded(key.to_string()),
287+
});
288+
};
274289
match RT.block_on(builder.build()) {
275290
Ok(db) => {
276291
let db = Box::leak(Box::new(libsql_database { db }));
@@ -325,6 +340,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
325340
let config = libsql::EncryptionConfig::new(libsql::Cipher::Aes256Cbc, key);
326341
builder = builder.encryption_config(config)
327342
};
343+
if !config.remote_encryption_key.is_null() {
344+
let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) };
345+
let key = match key.to_str() {
346+
Ok(k) => k,
347+
Err(e) => {
348+
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
349+
return 5;
350+
}
351+
};
352+
builder = builder.remote_encryption(libsql::EncryptionContext {
353+
key: libsql::EncryptionKey::Base64Encoded(key.to_string()),
354+
});
355+
};
328356
match RT.block_on(builder.build()) {
329357
Ok(db) => {
330358
let db = Box::leak(Box::new(libsql_database { db }));

bindings/c/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct libsql_config {
1515
pub sync_interval: std::ffi::c_int,
1616
pub with_webpki: std::ffi::c_char,
1717
pub offline: std::ffi::c_char,
18+
pub remote_encryption_key: *const std::ffi::c_char,
1819
}
1920

2021
#[derive(Clone, Debug)]

0 commit comments

Comments
 (0)