Skip to content

Commit

Permalink
Avoid crash when read nonce faild
Browse files Browse the repository at this point in the history
Change-Id: I918527796883f59b1fdbdb3f617b2748d287cd16
Signed-off-by: kexuan.yang <kexuan.yang@gmail.com>
  • Loading branch information
yangkx1024 committed Dec 11, 2023
1 parent de70fd4 commit 192d2e0
Show file tree
Hide file tree
Showing 14 changed files with 36 additions and 29 deletions.
29 changes: 11 additions & 18 deletions .github/workflows/android.yml
Original file line number Diff line number Diff line change
@@ -1,54 +1,47 @@
name: Android Build Check

on:
push:
on:
push:
branches: [ "main", "develop" ]
pull_request:
pull_request:
branches: [ "main", "develop" ]

jobs:
build:
jobs:
build:

runs-on: macos-latest

steps:
steps:
- uses: actions/checkout@v4

- name: set up JDK 17
uses: actions/setup-java@v3
with:
with:
java-version: '17'
distribution: 'temurin'
cache: 'gradle'

- name: Decode Keystore
env:
- name: Prepare Gradle build
env:
ENCODED_KEYSTORE: ${{ secrets.KEYSTORE }}
run: echo ${ENCODED_KEYSTORE} | base64 -d > ./android/app/keystore.jks

- name: Build with Gradle
env:
SIGN_KEY_STORE_PATH: keystore.jks
SIGN_STORE_PASSWORD: ${{ secrets.SIGN_STORE_PASSWORD }}
SIGN_KEY_ALIAS: ${{ secrets.SIGN_KEY_ALIAS }}
SIGN_KEY_PASSWORD: ${{ secrets.SIGN_KEY_PASSWORD }}
SONATYPEUSERNAME: ${{ secrets.SONATYPEUSERNAME }}
SONATYPEPASSWORD: ${{ secrets.SONATYPEPASSWORD }}
run: |
cd ./android
echo ${ENCODED_KEYSTORE} | base64 -d > ./app/keystore.jks
echo SIGN_KEY_STORE_PATH=keystore.jks >> local.properties
echo SIGN_STORE_PASSWORD=${SIGN_STORE_PASSWORD} >> local.properties
echo SIGN_KEY_ALIAS=${SIGN_KEY_ALIAS} >> local.properties
echo SIGN_KEY_PASSWORD=${SIGN_KEY_PASSWORD} >> local.properties
echo sonatypeUsername=${SONATYPEUSERNAME} >> local.properties
echo sonatypePassword=${SONATYPEPASSWORD} >> local.properties
chmod +x gradlew
./gradlew clean
./gradlew assembleDebug
- name: Android test
uses: reactivecircus/android-emulator-runner@v2
with:
with:
api-level: 34
target: google_apis_playstore
arch: x86_64
Expand Down
Binary file modified android/library-encrypt/src/main/jniLibs/arm64-v8a/libmmkv.so
Binary file not shown.
Binary file modified android/library-encrypt/src/main/jniLibs/armeabi-v7a/libmmkv.so
Binary file not shown.
Binary file modified android/library-encrypt/src/main/jniLibs/x86_64/libmmkv.so
Binary file not shown.
Binary file modified android/library/src/main/jniLibs/arm64-v8a/libmmkv.so
Binary file not shown.
Binary file modified android/library/src/main/jniLibs/armeabi-v7a/libmmkv.so
Binary file not shown.
Binary file modified android/library/src/main/jniLibs/x86_64/libmmkv.so
Binary file not shown.
Binary file modified ios/MMKV/Sources/RustMMKV.xcframework.zip
Binary file not shown.
2 changes: 1 addition & 1 deletion ios/MMKVDemo/MMKVDemo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@
repositoryURL = "https://github.com/yangkx-1024/MMKV";
requirement = {
kind = upToNextMinorVersion;
minimumVersion = 0.3.4;
minimumVersion = 0.3.5;
};
};
/* End XCRemoteSwiftPackageReference section */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/yangkx-1024/MMKV",
"state" : {
"revision" : "66a80a9e8fc66fd853cb67b782f2bfdd2cfd5b42",
"version" : "0.3.4"
"revision" : "de70fd46a37c07b6a365612b8118f8418a5dd00e",
"version" : "0.3.5"
}
}
],
Expand Down
3 changes: 2 additions & 1 deletion src/core/encrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use crate::Error::{DataInvalid, DecryptFailed, EncryptFailed};
use crate::Result;

const LOG_TAG: &str = "MMKV:Encrypt";
pub const NONCE_LEN: usize = 11;

type Aes128Eax = Eax<Aes128, U8>;
type Stream = StreamBE32<Aes128Eax>;
Expand All @@ -23,7 +24,7 @@ pub struct Encrypt {
stream: Stream,
position: u32,
key: [u8; 16],
nonce: [u8; 11],
nonce: [u8; NONCE_LEN],
}

impl Encrypt {
Expand Down
22 changes: 18 additions & 4 deletions src/core/mmkv_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,24 @@ impl MmkvImpl {
if meta_path.exists() {
let mut nonce_file = OpenOptions::new().read(true).open(meta_path).unwrap();
let mut nonce = Vec::<u8>::new();
nonce_file
.read_to_end(&mut nonce)
.expect("failed to read nonce file");
Encrypt::new_with_nonce(key.try_into().unwrap(), nonce.as_slice())
let error_handle = |reason: String| {
error!(LOG_TAG, "filed to read nonce, reason: {:?}", reason);
warn!(
LOG_TAG, "delete meta file due to previous reason, which may cause mmkv drop all encrypted data"
);
let _ = fs::remove_file(meta_path);
MmkvImpl::init_encrypt(meta_path, key)
};
match nonce_file.read_to_end(&mut nonce) {
Ok(_) => {
if nonce.len() != crate::core::encrypt::NONCE_LEN {
error_handle("meta file corruption".to_string())
} else {
Encrypt::new_with_nonce(key.try_into().unwrap(), nonce.as_slice())
}
}
Err(e) => error_handle(format!("{:?}", e)),
}
} else {
let crypt = Encrypt::new(key.try_into().unwrap());
let mut nonce_file = OpenOptions::new()
Expand Down
4 changes: 2 additions & 2 deletions src/ffi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ macro_rules! impl_put {
verbose!(LOG_TAG, "{} for key '{}' success", $log, key_str);
}
}
return result.leak();
result.leak()
}
};
}
Expand All @@ -231,7 +231,7 @@ macro_rules! impl_put_typed_array {
verbose!(LOG_TAG, "{} for key '{}' success", $log, key_str);
}
}
return result.leak();
result.leak()
}
};
}
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ macro_rules! verbose {
mod core;
#[cfg(not(target_os = "android"))]
#[cfg(not(feature = "encryption"))]
#[allow(non_snake_case)]
/// Expose the C API
mod ffi;
#[cfg(target_os = "android")]
Expand Down

0 comments on commit 192d2e0

Please sign in to comment.