Skip to content

Commit 69394b3

Browse files
authored
Enhance mobile decryption functionality (#47)
* Enhance mobile decryption functionality and add comprehensive tests - Updated the `Keys` class to replace the `decrypt_with_network` method with `decrypt_envelope_data`, improving clarity and functionality in mobile decryption processes. - Introduced a new test case for mobile decryption with profile keys, validating the decryption path for user mobile keystores and ensuring compatibility with profile-based encryption. - Enhanced existing tests to cover various scenarios, including multiple valid profile keys and network key fallbacks, ensuring robust validation of the decryption workflows. - Improved logging in tests to provide better insights during execution and confirm successful decryption paths. * Refactor network key management to check for private keys - Renamed the `get_network_public_key` function to `has_network_private_key` across multiple modules, enhancing clarity in the API by explicitly indicating the function's purpose. - Updated method signatures and implementations in `runar-ffi`, `runar-keys`, and `runar-nodejs-api` to reflect the new naming convention, ensuring consistency across the codebase. - Adjusted related tests to validate the new functionality, confirming that the checks for private keys are correctly implemented and functioning as expected. - Improved documentation and comments to clarify the changes and their implications for users of the API.
1 parent 21d556a commit 69394b3

File tree

18 files changed

+213
-51
lines changed

18 files changed

+213
-51
lines changed

runar-ffi/include/runar_ffi.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,12 +172,12 @@ int32_t rn_keys_mobile_generate_network_data_key(void *keys,
172172
size_t *out_len,
173173
struct RNAPIRnError *err);
174174

175-
int32_t rn_keys_mobile_get_network_public_key(void *keys,
176-
const uint8_t *network_public_key,
177-
size_t network_public_key_len,
178-
uint8_t **out_pk,
179-
size_t *out_len,
180-
struct RNAPIRnError *err);
175+
int32_t rn_keys_mobile_has_network_private_key(void *keys,
176+
const uint8_t *network_public_key,
177+
size_t network_public_key_len,
178+
uint8_t **out_pk,
179+
size_t *out_len,
180+
struct RNAPIRnError *err);
181181

182182
int32_t rn_keys_mobile_create_network_key_message(void *keys,
183183
const uint8_t *network_public_key,

runar-ffi/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1634,7 +1634,7 @@ pub unsafe extern "C" fn rn_keys_mobile_generate_network_data_key(
16341634
}
16351635

16361636
#[no_mangle]
1637-
pub unsafe extern "C" fn rn_keys_mobile_get_network_public_key(
1637+
pub unsafe extern "C" fn rn_keys_mobile_has_network_private_key(
16381638
keys: *mut c_void,
16391639
network_public_key: *const u8,
16401640
network_public_key_len: usize,
@@ -1690,7 +1690,7 @@ pub unsafe extern "C" fn rn_keys_mobile_get_network_public_key(
16901690

16911691
let network_pk = std::slice::from_raw_parts(network_public_key, network_public_key_len);
16921692

1693-
match mobile_manager.get_network_public_key(network_pk) {
1693+
match mobile_manager.has_network_private_key(network_pk) {
16941694
Ok(pk) => {
16951695
if !alloc_bytes(out_pk, out_len, &pk) {
16961696
set_error(err, RN_ERROR_MEMORY_ALLOCATION, "alloc failed");
@@ -1703,7 +1703,7 @@ pub unsafe extern "C" fn rn_keys_mobile_get_network_public_key(
17031703
set_error(
17041704
err,
17051705
RN_ERROR_OPERATION_FAILED,
1706-
&format!("get_network_public_key failed: {e}"),
1706+
&format!("has_network_private_key failed: {e}"),
17071707
);
17081708
RN_ERROR_OPERATION_FAILED
17091709
}

runar-keys/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ pub trait EnvelopeCrypto: Send + Sync {
5656

5757
fn decrypt_envelope_data(&self, env: &EnvelopeEncryptedData) -> Result<Vec<u8>>;
5858

59-
/// Get network public key by network public key bytes
60-
fn get_network_public_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>>;
59+
/// Check if we have the private key for this network public key
60+
fn has_network_private_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>>;
6161

6262
/// Get network public key by network ID (needed for path resolution)
6363
fn get_network_public_key_by_id(&self, network_id: &str) -> Result<Vec<u8>>;

runar-keys/src/mobile.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,13 @@ impl MobileKeyManager {
385385
out
386386
}
387387

388-
pub fn get_network_public_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
389-
// Direct access - validate we have the key
388+
pub fn has_network_private_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
389+
// Direct access - validate we have the private key for this public key
390390
if self.network_data_keys.contains_key(network_public_key) {
391391
Ok(network_public_key.to_vec())
392392
} else {
393393
Err(KeyError::KeyNotFound(format!(
394-
"Network key not found for public key: {} bytes",
394+
"Network private key not found for public key: {} bytes",
395395
network_public_key.len()
396396
)))
397397
}
@@ -1038,8 +1038,8 @@ impl EnvelopeCrypto for MobileKeyManager {
10381038
self.decrypt_with_network(env)
10391039
}
10401040

1041-
fn get_network_public_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
1042-
MobileKeyManager::get_network_public_key(self, network_public_key)
1041+
fn has_network_private_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
1042+
MobileKeyManager::has_network_private_key(self, network_public_key)
10431043
}
10441044

10451045
fn get_network_public_key_by_id(&self, network_id: &str) -> Result<Vec<u8>> {

runar-keys/src/node.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -677,13 +677,13 @@ impl NodeKeyManager {
677677
})
678678
}
679679

680-
pub fn get_network_public_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
681-
// Direct access - validate we have the key
680+
pub fn has_network_private_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
681+
// Direct access - validate we have the private key for this public key
682682
if self.network_agreements.contains_key(network_public_key) {
683683
Ok(network_public_key.to_vec())
684684
} else {
685685
Err(KeyError::KeyNotFound(format!(
686-
"Network key not found for public key: {} bytes",
686+
"Network private key not found for public key: {} bytes",
687687
network_public_key.len()
688688
)))
689689
}
@@ -949,8 +949,8 @@ impl EnvelopeCrypto for NodeKeyManager {
949949
NodeKeyManager::decrypt_envelope_data(self, env)
950950
}
951951

952-
fn get_network_public_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
953-
NodeKeyManager::get_network_public_key(self, network_public_key)
952+
fn has_network_private_key(&self, network_public_key: &[u8]) -> Result<Vec<u8>> {
953+
NodeKeyManager::has_network_private_key(self, network_public_key)
954954
}
955955

956956
fn get_network_public_key_by_id(&self, network_id: &str) -> Result<Vec<u8>> {

runar-node/src/node.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ impl EnvelopeCrypto for NodeKeyManagerWrapper {
6565
keys_manager.decrypt_envelope_data(env)
6666
}
6767

68-
fn get_network_public_key(&self, network_public_key: &[u8]) -> KeyResult<Vec<u8>> {
68+
fn has_network_private_key(&self, network_public_key: &[u8]) -> KeyResult<Vec<u8>> {
6969
let keys_manager = self.0.read().unwrap();
70-
keys_manager.get_network_public_key(network_public_key)
70+
keys_manager.has_network_private_key(network_public_key)
7171
}
7272

7373
fn get_network_public_key_by_id(&self, network_id: &str) -> KeyResult<Vec<u8>> {

runar-nodejs-api/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export declare class Keys {
6363
encryptMessageForMobile(message: Uint8Array, mobilePk: Uint8Array): Uint8Array
6464
decryptMessageFromMobile(encrypted: Uint8Array): Uint8Array
6565
mobileDeriveUserProfileKey(label: string): Uint8Array
66-
mobileGetNetworkPublicKey(networkPublicKey: Uint8Array): Uint8Array
66+
mobileHasNetworkPrivateKey(networkPublicKey: Uint8Array): Uint8Array
6767
mobileCreateNetworkKeyMessage(networkPublicKey: Uint8Array, nodeAgreementPk: Uint8Array): Uint8Array
6868
ensureSymmetricKey(keyName: string): Uint8Array
6969
/**

runar-nodejs-api/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use napi::threadsafe_function::{ThreadsafeFunction, ThreadsafeFunctionCallMode};
66
use napi_derive::napi;
77
use once_cell::sync::Lazy;
88
use runar_common::logging::{Component, Logger};
9-
use runar_keys::{MobileKeyManager, NodeKeyManager};
9+
use runar_keys::{EnvelopeCrypto, MobileKeyManager, NodeKeyManager};
1010
use runar_schemas::NodeInfo;
1111

1212
use runar_transporter::discovery::{DiscoveryEvent, DiscoveryOptions};
@@ -373,7 +373,7 @@ impl Keys {
373373
.mobile
374374
.as_ref()
375375
.unwrap()
376-
.decrypt_with_network(&eed)
376+
.decrypt_envelope_data(&eed)
377377
.map_err(|e| Error::from_reason(e.to_string()))?;
378378

379379
Ok(Uint8Array::from(plain))
@@ -616,7 +616,7 @@ impl Keys {
616616
}
617617

618618
#[napi]
619-
pub fn mobile_get_network_public_key(
619+
pub fn mobile_has_network_private_key(
620620
&self,
621621
network_public_key: Uint8Array,
622622
) -> Result<Uint8Array> {
@@ -631,7 +631,7 @@ impl Keys {
631631
.mobile
632632
.as_mut()
633633
.unwrap()
634-
.get_network_public_key(&network_public_key)
634+
.has_network_private_key(&network_public_key)
635635
.map_err(|e| Error::from_reason(e.to_string()))?;
636636
Ok(Uint8Array::from(pk))
637637
}

runar-nodejs-api/tests/comprehensive_api_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ describe('Comprehensive API Tests', () => {
152152
const testPk = Buffer.alloc(65, 1); // Mock public key
153153
keys.mobileInstallNetworkPublicKey(testPk);
154154
const networkPublicKey = keys.mobileGenerateNetworkDataKey();
155-
const pk = keys.mobileGetNetworkPublicKey(networkPublicKey);
155+
const pk = keys.mobileHasNetworkPrivateKey(networkPublicKey);
156156
expect(pk instanceof Uint8Array).toBe(true);
157157
expect(pk.length).toBeGreaterThan(0);
158158
}, 10000);

runar-nodejs-api/tests/comprehensive_lifecycle_test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ describe('Comprehensive End-to-End Lifecycle Tests', () => {
187187

188188
// 5.1 Mobile encrypts with envelope
189189
// Get network public key from network ID for envelope encryption
190-
const retrievedNetworkPublicKey = mobileKeys.mobileGetNetworkPublicKey(networkPublicKey);
190+
const retrievedNetworkPublicKey = mobileKeys.mobileHasNetworkPrivateKey(networkPublicKey);
191191
expect(retrievedNetworkPublicKey instanceof Uint8Array).toBe(true);
192192
expect(retrievedNetworkPublicKey.length).toBeGreaterThan(0);
193193

0 commit comments

Comments
 (0)