libcryfs: make cryfs_init return error code

This commit is contained in:
Matéo Duparc 2023-05-03 00:01:26 +02:00
parent f40c2bbdcd
commit 3c56f86d86
Signed by: hardcoresushi
GPG Key ID: AFE384344A45E13A
3 changed files with 21 additions and 9 deletions

View File

@ -157,9 +157,7 @@ namespace cryfs_cli {
fuse->init(); fuse->init();
return fuse; return fuse;
} catch (const CryfsException &e) { } catch (const CryfsException &e) {
if (e.what() != string()) { throw; // CryfsException is only thrown if setup goes wrong. Throw it through so that we get the correct process exit code.
LOG(ERR, "Error {}: {}", static_cast<int>(e.errorCode()), e.what());
}
} catch (const std::exception &e) { } catch (const std::exception &e) {
LOG(ERR, "Crashed: {}", e.what()); LOG(ERR, "Crashed: {}", e.what());
} catch (...) { } catch (...) {

View File

@ -2,7 +2,7 @@
jlong cryfs_init(JNIEnv *env, jstring jbaseDir, jstring jlocalSateDir, jbyteArray jpassword, jlong cryfs_init(JNIEnv *env, jstring jbaseDir, jstring jlocalSateDir, jbyteArray jpassword,
jbyteArray jgivenHash, jobject returnedHash, jboolean createBaseDir, jbyteArray jgivenHash, jobject returnedHash, jboolean createBaseDir,
jstring jcipher); jstring jcipher, jobject jerrorCode);
jboolean cryfs_change_encryption_key(JNIEnv *env, jboolean cryfs_change_encryption_key(JNIEnv *env,
jstring jbaseDir, jstring jlocalStateDir, jstring jbaseDir, jstring jlocalStateDir,
jbyteArray jcurrentPassword, jbyteArray jgivenHash, jbyteArray jcurrentPassword, jbyteArray jgivenHash,

View File

@ -1,6 +1,7 @@
#include <jni.h> #include <jni.h>
#include <cryfs-cli/Cli.h> #include <cryfs-cli/Cli.h>
#include <fspp/fuse/Fuse.h> #include <fspp/fuse/Fuse.h>
#include <cryfs/impl/CryfsException.h>
#include <cryfs/impl/config/CryKeyProvider.h> #include <cryfs/impl/config/CryKeyProvider.h>
#include <cryfs/impl/config/CryDirectKeyProvider.h> #include <cryfs/impl/config/CryDirectKeyProvider.h>
#include <cryfs/impl/config/CryPresetPasswordBasedKeyProvider.h> #include <cryfs/impl/config/CryPresetPasswordBasedKeyProvider.h>
@ -14,18 +15,21 @@ using fspp::fuse::Fuse;
std::set<jlong> validFusePtrs; std::set<jlong> validFusePtrs;
jfieldID getValueField(JNIEnv* env, jobject object) {
return env->GetFieldID(env->GetObjectClass(object), "value", "Ljava/lang/Object;");
}
void setReturnedPasswordHash(JNIEnv* env, jobject jreturnedHash, const SizedData& returnedHash) { void setReturnedPasswordHash(JNIEnv* env, jobject jreturnedHash, const SizedData& returnedHash) {
jfieldID value = env->GetFieldID(env->GetObjectClass(jreturnedHash), "value", "Ljava/lang/Object;");
jbyteArray jpasswordHash = env->NewByteArray(returnedHash.size); jbyteArray jpasswordHash = env->NewByteArray(returnedHash.size);
env->SetByteArrayRegion(jpasswordHash, 0, returnedHash.size, reinterpret_cast<const jbyte*>(returnedHash.data)); env->SetByteArrayRegion(jpasswordHash, 0, returnedHash.size, reinterpret_cast<const jbyte*>(returnedHash.data));
delete[] returnedHash.data; delete[] returnedHash.data;
env->SetObjectField(jreturnedHash, value, jpasswordHash); env->SetObjectField(jreturnedHash, getValueField(env, jreturnedHash), jpasswordHash);
} }
extern "C" jlong extern "C" jlong
cryfs_init(JNIEnv *env, jstring jbaseDir, jstring jlocalStateDir, jbyteArray jpassword, cryfs_init(JNIEnv *env, jstring jbaseDir, jstring jlocalStateDir, jbyteArray jpassword,
jbyteArray jgivenHash, jobject jreturnedHash, jboolean createBaseDir, jbyteArray jgivenHash, jobject jreturnedHash, jboolean createBaseDir,
jstring jcipher) { jstring jcipher, jobject jerrorCode) {
const char* baseDir = env->GetStringUTFChars(jbaseDir, NULL); const char* baseDir = env->GetStringUTFChars(jbaseDir, NULL);
const char* localStateDir = env->GetStringUTFChars(jlocalStateDir, NULL); const char* localStateDir = env->GetStringUTFChars(jlocalStateDir, NULL);
boost::optional<string> cipher = none; boost::optional<string> cipher = none;
@ -54,8 +58,18 @@ cryfs_init(JNIEnv *env, jstring jbaseDir, jstring jlocalStateDir, jbyteArray jpa
} }
} }
Fuse* fuse = Cli(keyGenerator, SCrypt::DefaultSettings).initFilesystem(options, credentials); Fuse* fuse = 0;
try {
fuse = Cli(keyGenerator, SCrypt::DefaultSettings).initFilesystem(options, credentials);
} catch (const cryfs::CryfsException &e) {
int errorCode = static_cast<int>(e.errorCode());
if (e.what() != string()) {
LOG(cpputils::logging::ERR, "Error {}: {}", errorCode, e.what());
}
jclass integerClass = env->FindClass("java/lang/Integer");
jobject integer = env->NewObject(integerClass, env->GetMethodID(integerClass, "<init>", "(I)V"), errorCode);
env->SetObjectField(jerrorCode, getValueField(env, jerrorCode), integer);
}
if (jpassword == NULL) { if (jpassword == NULL) {
env->ReleaseByteArrayElements(jgivenHash, reinterpret_cast<jbyte*>(credentials.givenHash.data), 0); env->ReleaseByteArrayElements(jgivenHash, reinterpret_cast<jbyte*>(credentials.givenHash.data), 0);
} }