error handling improvements.

pull/2/merge
Moisés Guimarães 2017-02-28 15:56:49 -07:00
parent 986df80f71
commit b612acb5da
10 changed files with 134 additions and 58 deletions

View File

@ -41,6 +41,14 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1free
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key
(JNIEnv *, jobject, jobject, jint);
/*
* Class: com_wolfssl_wolfcrypt_Ecc
* Method: wc_ecc_check_key
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1check_1key
(JNIEnv *, jobject);
/*
* Class: com_wolfssl_wolfcrypt_Ecc
* Method: wc_ecc_shared_secret

View File

@ -27,14 +27,15 @@ extern "C" {
#endif
void* getNativeStruct(JNIEnv* env, jobject this);
byte* getDirectBufferAddress(JNIEnv* env, jobject buffer);
word32 getDirectBufferLimit(JNIEnv* env, jobject buffer);
void setDirectBufferLimit(JNIEnv* env, jobject buffer, jint limit);
byte* getByteArray(JNIEnv* env, jbyteArray array);
void releaseByteArray(JNIEnv* env, jbyteArray array, byte* elements, jint abort);
word32 getByteArrayLength(JNIEnv* env, jbyteArray array);
word32 getDirectBufferLimit(JNIEnv* env, jobject buffer);
void setDirectBufferLimit(JNIEnv* env, jobject buffer, jint limit);
#ifdef __cplusplus
}
#endif

View File

@ -24,16 +24,19 @@
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef __cplusplus
extern "C" {
#endif
void throwWolfCryptExceptionFromError(JNIEnv* env, int code);
#define throwWolfCryptException(env, msg) (*env)->ThrowNew(env, \
(*env)->FindClass(env, "com/wolfssl/wolfcrypt/WolfCryptException"), msg)
#define throwNotCompiledInException(env) (*env)->ThrowNew(env, \
(*env)->FindClass(env, "java/lang/UnsupportedOperationException"), \
"Feature not compiled in")
#define throwNotCompiledInException(env) \
throwWolfCryptExceptionFromError(env, NOT_COMPILED_IN)
#define throwOutOfMemoryException(env, msg) (*env)->ThrowNew(env, \
(*env)->FindClass(env, "java/lang/OutOfMemoryError"), msg)

View File

@ -62,7 +62,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1init(
ret = wc_ecc_init(ecc);
if (ret != 0)
throwWolfCryptException(env, "Failed to initialize Ecc object");
throwWolfCryptExceptionFromError(env, ret);
LogStr("ecc_init(ecc=%p) = %d\n", ecc, ret);
#else
@ -96,7 +96,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key(
ret = wc_ecc_make_key(rng, size, ecc);
if (ret != 0)
throwWolfCryptException(env, "Failed to generate Ecc key");
throwWolfCryptExceptionFromError(env, ret);
LogStr("ecc_make_key(rng, size, ecc=%p) = %d\n", ecc, ret);
#else
@ -104,6 +104,25 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key(
#endif
}
JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1check_1key(
JNIEnv* env, jobject this)
{
#ifdef HAVE_ECC
int ret = 0;
ecc_key* ecc = (ecc_key*) getNativeStruct(env, this);
ret = wc_ecc_check_key(ecc);
if (ret != 0)
throwWolfCryptExceptionFromError(env, ret);
LogStr("wc_ecc_check_key(ecc=%p) = %d\n", ecc, ret);
#else
throwNotCompiledInException(env);
#endif
}
JNIEXPORT void JNICALL
Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1import_1x963(
JNIEnv* env, jobject this, jbyteArray key_object)
@ -116,7 +135,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1import_1x963(
ret = wc_ecc_import_x963(key, keySz, ecc);
if (ret != 0)
throwWolfCryptException(env, "Failed to import X9.63 key");
throwWolfCryptExceptionFromError(env, ret);
LogStr("ecc_import_x963(key, keySz, ecc=%p) = %d\n", ecc, ret);
#else
@ -144,7 +163,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1EccPrivateKeyDecode(
ret = wc_EccPrivateKeyDecode(key, &idx, ecc, keySz);
if (ret != 0)
throwWolfCryptException(env, "Failed to decode private key");
throwWolfCryptExceptionFromError(env, ret);
LogStr("wc_EccPrivateKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
#else
@ -172,7 +191,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1EccPublicKeyDecode(
ret = wc_EccPublicKeyDecode(key, &idx, ecc, keySz);
if (ret != 0)
throwWolfCryptException(env, "Failed to decode public key");
throwWolfCryptExceptionFromError(env, ret);
LogStr("wc_EccPublicKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
#else
@ -218,7 +237,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1shared_1secret(
throwWolfCryptException(env, "Failed to allocate shared secret");
}
} else {
throwWolfCryptException(env, "Failed to generate shared secret");
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_ecc_shared_secret(priv, pub, output=%p, outputSz) = %d\n",
@ -266,7 +285,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1sign_1hash(
throwWolfCryptException(env, "Failed to allocate signature");
}
} else {
throwWolfCryptException(env, "Failed to generate signature");
throwWolfCryptExceptionFromError(env, ret);
}
LogStr("wc_ecc_sign_hash(input, inSz, output, &outSz, rng, ecc) = %d\n",
@ -301,7 +320,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1verify_1hash(
if (ret == 0) {
ret = status;
} else {
throwWolfCryptException(env, "Failed to verify signature");
throwWolfCryptExceptionFromError(env, ret);
}
LogStr(

View File

@ -22,9 +22,9 @@
#ifndef __ANDROID__
#include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/error-crypt.h>
#include <com_wolfssl_wolfcrypt_WolfCryptError.h>
#include <wolfcrypt_jni_error.h>
JNIEXPORT jstring JNICALL Java_com_wolfssl_wolfcrypt_WolfCryptError_wc_1GetErrorString
(JNIEnv* env, jclass obj, jint error)
@ -32,3 +32,34 @@ JNIEXPORT jstring JNICALL Java_com_wolfssl_wolfcrypt_WolfCryptError_wc_1GetError
return (*env)->NewStringUTF(env, wc_GetErrorString(error));
}
void throwWolfCryptExceptionFromError(JNIEnv* env, int code)
{
jclass class = NULL;
jobject exception = NULL;
jmethodID constructor = NULL;
if (code == MEMORY_E) {
throwOutOfMemoryException(
env, "Failed to allocate memory in the native wolfcrypt library");
return;
}
class = (*env)->FindClass(env, "com/wolfssl/wolfcrypt/WolfCryptException");
if (class) {
constructor = (*env)->GetMethodID(env, class, "<init>", "(I)V");
if (constructor) {
exception = (*env)->NewObject(env, class, constructor, code);
if (exception) {
(*env)->Throw(env, exception);
return;
}
}
}
throwWolfCryptException(env, wc_GetErrorString(code));
}

View File

@ -26,6 +26,7 @@
#include <com_wolfssl_wolfcrypt_NativeStruct.h>
#include <wolfcrypt_jni_NativeStruct.h>
#include <wolfcrypt_jni_error.h>
/* #define WOLFCRYPT_JNI_DEBUG_ON */
#include <wolfcrypt_jni_debug.h>
@ -53,13 +54,6 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_NativeStruct_xfree(
/*
* Utilitary functions
*/
static void throwGetNativeStructError(JNIEnv* env)
{
(*env)->ThrowNew(env,
(*env)->FindClass(env, "com/wolfssl/wolfcrypt/WolfCryptException"),
"Failed to retrieve native struct");
}
void* getNativeStruct(JNIEnv* env, jobject this)
{
if (this) {
@ -68,7 +62,7 @@ void* getNativeStruct(JNIEnv* env, jobject this)
jlong nativeStruct = (*env)->GetLongField(env, this, field);
if (!nativeStruct)
throwGetNativeStructError(env);
throwWolfCryptException(env, "Failed to retrieve native struct");
return (void*) nativeStruct;
}

View File

@ -49,6 +49,8 @@ public class Ecc extends NativeStruct {
private native void wc_ecc_free();
private native void wc_ecc_make_key(Rng rng, int size);
private native void wc_ecc_check_key();
private native byte[] wc_ecc_shared_secret(Ecc pubKey);
@ -93,6 +95,15 @@ public class Ecc extends NativeStruct {
throw new IllegalStateException("Object already has a key.");
}
}
public void checkKey() {
if (state == WolfCryptState.READY) {
wc_ecc_check_key();
} else {
throw new IllegalStateException(
"No available key to perform the opperation.");
}
}
public void importX963(byte[] key) {
if (state == WolfCryptState.INITIALIZED) {

View File

@ -145,38 +145,39 @@ public enum WolfCryptError {
private static final Map<Integer, WolfCryptError> intToErrMap =
new HashMap<Integer, WolfCryptError>();
static {
for (WolfCryptError err : WolfCryptError.values()) {
intToErrMap.put(err.code, err);
}
}
static {
for (WolfCryptError err : WolfCryptError.values()) {
intToErrMap.put(err.code, err);
}
}
private WolfCryptError(int code) {
this.code = code;
}
private WolfCryptError(int code) {
this.code = code;
}
public int getCode() {
return this.code;
}
public int getCode() {
return this.code;
}
public String getDescription() {
if (this == WolfCryptError.NO_ERROR_FOUND)
return "No error code found in JNI WolfCryptError enum";
return wc_GetErrorString(this.code);
}
public String getDescription() {
if (this == WolfCryptError.NO_ERROR_FOUND)
return "No error code found in JNI WolfCryptError enum";
return wc_GetErrorString(this.code);
}
public static WolfCryptError fromInt(int code) {
WolfCryptError err = intToErrMap.get(Integer.valueOf(code));
if (err == null)
return WolfCryptError.NO_ERROR_FOUND;
return err;
}
public static WolfCryptError fromInt(int code) {
WolfCryptError err = intToErrMap.get(Integer.valueOf(code));
private static native String wc_GetErrorString(int error);
if (err == null)
return WolfCryptError.NO_ERROR_FOUND;
@Override
public String toString() {
return code + ": " + this.getDescription();
}
return err;
}
private static native String wc_GetErrorString(int error);
@Override
public String toString() {
return "(" + code + ") " + this.getDescription();
}
}

View File

@ -29,18 +29,21 @@ import com.wolfssl.wolfcrypt.WolfCryptError;
* @author Moisés Guimarães
* @version 1.0, February 2015
*/
public class WolfCryptException extends Exception {
public class WolfCryptException extends RuntimeException {
private static final long serialVersionUID = 142053665132156225L;
private WolfCryptError error;
private int code;
public WolfCryptException(String reason) {
super(reason);
}
public WolfCryptException(String reason, WolfCryptError error) {
super(reason);
this.error = error;
public WolfCryptException(int code) {
super(WolfCryptError.fromInt(code).getDescription());
this.error = WolfCryptError.fromInt(code);
this.code = code;
}
public WolfCryptException(String reason, Throwable cause) {
@ -54,5 +57,9 @@ public class WolfCryptException extends Exception {
public WolfCryptError getError() {
return this.error;
}
public int getCode() {
return this.code;
}
}

View File

@ -49,9 +49,10 @@ public class EccTest {
public void setUpEcc() {
try {
key = new Ecc();
} catch (UnsupportedOperationException e) {
if (e.getMessage().equals("Feature not compiled in"))
Assume.assumeNoException(e);
} catch (WolfCryptException e) {
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
System.out.println("Ecc test skipped: " + e.getError());
Assume.assumeNoException(e);
}
}