Skip to content

Commit 34033b7

Browse files
authored
Add support for cloud encryption for remote connection (tursodatabase#2175)
Adds a new public method `libsql_open_remote_with_remote_encryption` to use with cloud encryption. Then this can be used in the SDKs.
2 parents 66fb01f + 2c1be71 commit 34033b7

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

bindings/c/include/libsql.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ int libsql_open_file(const char *url, libsql_database_t *out_db, const char **ou
9393

9494
int libsql_open_remote(const char *url, const char *auth_token, libsql_database_t *out_db, const char **out_err_msg);
9595

96+
int libsql_open_remote_with_remote_encryption(const char *url,
97+
const char *auth_token,
98+
const char *remote_encryption_key,
99+
libsql_database_t *out_db,
100+
const char **out_err_msg);
101+
96102
int libsql_open_remote_with_webpki(const char *url,
97103
const char *auth_token,
98104
libsql_database_t *out_db,

bindings/c/src/lib.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,32 @@ pub unsafe extern "C" fn libsql_open_remote(
414414
out_db: *mut libsql_database_t,
415415
out_err_msg: *mut *const std::ffi::c_char,
416416
) -> std::ffi::c_int {
417-
libsql_open_remote_internal(url, auth_token, false, out_db, out_err_msg)
417+
libsql_open_remote_internal(
418+
url,
419+
auth_token,
420+
std::ptr::null(),
421+
false,
422+
out_db,
423+
out_err_msg,
424+
)
425+
}
426+
427+
#[no_mangle]
428+
pub unsafe extern "C" fn libsql_open_remote_with_remote_encryption(
429+
url: *const std::ffi::c_char,
430+
auth_token: *const std::ffi::c_char,
431+
remote_encryption_key: *const std::ffi::c_char,
432+
out_db: *mut libsql_database_t,
433+
out_err_msg: *mut *const std::ffi::c_char,
434+
) -> std::ffi::c_int {
435+
libsql_open_remote_internal(
436+
url,
437+
auth_token,
438+
remote_encryption_key,
439+
false,
440+
out_db,
441+
out_err_msg,
442+
)
418443
}
419444

420445
#[no_mangle]
@@ -424,12 +449,13 @@ pub unsafe extern "C" fn libsql_open_remote_with_webpki(
424449
out_db: *mut libsql_database_t,
425450
out_err_msg: *mut *const std::ffi::c_char,
426451
) -> std::ffi::c_int {
427-
libsql_open_remote_internal(url, auth_token, true, out_db, out_err_msg)
452+
libsql_open_remote_internal(url, auth_token, std::ptr::null(), true, out_db, out_err_msg)
428453
}
429454

430455
unsafe fn libsql_open_remote_internal(
431456
url: *const std::ffi::c_char,
432457
auth_token: *const std::ffi::c_char,
458+
remote_encryption_key: *const std::ffi::c_char,
433459
with_webpki: bool,
434460
out_db: *mut libsql_database_t,
435461
out_err_msg: *mut *const std::ffi::c_char,
@@ -451,6 +477,21 @@ unsafe fn libsql_open_remote_internal(
451477
}
452478
};
453479
let mut builder = libsql::Builder::new_remote(url.to_string(), auth_token.to_string());
480+
481+
if !remote_encryption_key.is_null() {
482+
let key = unsafe { std::ffi::CStr::from_ptr(remote_encryption_key) };
483+
let key = match key.to_str() {
484+
Ok(k) => k,
485+
Err(e) => {
486+
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
487+
return 5;
488+
}
489+
};
490+
builder = builder.remote_encryption(libsql::EncryptionContext {
491+
key: libsql::EncryptionKey::Base64Encoded(key.to_string()),
492+
});
493+
};
494+
454495
if with_webpki {
455496
let https = hyper_rustls::HttpsConnectorBuilder::new()
456497
.with_webpki_roots()

0 commit comments

Comments
 (0)