error handling improvements.
parent
986df80f71
commit
b612acb5da
|
@ -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
|
JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key
|
||||||
(JNIEnv *, jobject, jobject, jint);
|
(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
|
* Class: com_wolfssl_wolfcrypt_Ecc
|
||||||
* Method: wc_ecc_shared_secret
|
* Method: wc_ecc_shared_secret
|
||||||
|
|
|
@ -27,14 +27,15 @@ extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void* getNativeStruct(JNIEnv* env, jobject this);
|
void* getNativeStruct(JNIEnv* env, jobject this);
|
||||||
|
|
||||||
byte* getDirectBufferAddress(JNIEnv* env, jobject buffer);
|
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);
|
byte* getByteArray(JNIEnv* env, jbyteArray array);
|
||||||
void releaseByteArray(JNIEnv* env, jbyteArray array, byte* elements, jint abort);
|
void releaseByteArray(JNIEnv* env, jbyteArray array, byte* elements, jint abort);
|
||||||
word32 getByteArrayLength(JNIEnv* env, jbyteArray array);
|
word32 getByteArrayLength(JNIEnv* env, jbyteArray array);
|
||||||
|
|
||||||
word32 getDirectBufferLimit(JNIEnv* env, jobject buffer);
|
|
||||||
void setDirectBufferLimit(JNIEnv* env, jobject buffer, jint limit);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,16 +24,19 @@
|
||||||
|
|
||||||
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
|
#pragma GCC diagnostic ignored "-Wpointer-to-int-cast"
|
||||||
|
|
||||||
|
#include <wolfssl/wolfcrypt/error-crypt.h>
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void throwWolfCryptExceptionFromError(JNIEnv* env, int code);
|
||||||
|
|
||||||
#define throwWolfCryptException(env, msg) (*env)->ThrowNew(env, \
|
#define throwWolfCryptException(env, msg) (*env)->ThrowNew(env, \
|
||||||
(*env)->FindClass(env, "com/wolfssl/wolfcrypt/WolfCryptException"), msg)
|
(*env)->FindClass(env, "com/wolfssl/wolfcrypt/WolfCryptException"), msg)
|
||||||
|
|
||||||
#define throwNotCompiledInException(env) (*env)->ThrowNew(env, \
|
#define throwNotCompiledInException(env) \
|
||||||
(*env)->FindClass(env, "java/lang/UnsupportedOperationException"), \
|
throwWolfCryptExceptionFromError(env, NOT_COMPILED_IN)
|
||||||
"Feature not compiled in")
|
|
||||||
|
|
||||||
#define throwOutOfMemoryException(env, msg) (*env)->ThrowNew(env, \
|
#define throwOutOfMemoryException(env, msg) (*env)->ThrowNew(env, \
|
||||||
(*env)->FindClass(env, "java/lang/OutOfMemoryError"), msg)
|
(*env)->FindClass(env, "java/lang/OutOfMemoryError"), msg)
|
||||||
|
|
|
@ -62,7 +62,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1init(
|
||||||
|
|
||||||
ret = wc_ecc_init(ecc);
|
ret = wc_ecc_init(ecc);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
throwWolfCryptException(env, "Failed to initialize Ecc object");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
|
|
||||||
LogStr("ecc_init(ecc=%p) = %d\n", ecc, ret);
|
LogStr("ecc_init(ecc=%p) = %d\n", ecc, ret);
|
||||||
#else
|
#else
|
||||||
|
@ -96,7 +96,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key(
|
||||||
|
|
||||||
ret = wc_ecc_make_key(rng, size, ecc);
|
ret = wc_ecc_make_key(rng, size, ecc);
|
||||||
if (ret != 0)
|
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);
|
LogStr("ecc_make_key(rng, size, ecc=%p) = %d\n", ecc, ret);
|
||||||
#else
|
#else
|
||||||
|
@ -104,6 +104,25 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1make_1key(
|
||||||
#endif
|
#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
|
JNIEXPORT void JNICALL
|
||||||
Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1import_1x963(
|
Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1import_1x963(
|
||||||
JNIEnv* env, jobject this, jbyteArray key_object)
|
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);
|
ret = wc_ecc_import_x963(key, keySz, ecc);
|
||||||
if (ret != 0)
|
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);
|
LogStr("ecc_import_x963(key, keySz, ecc=%p) = %d\n", ecc, ret);
|
||||||
#else
|
#else
|
||||||
|
@ -144,7 +163,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1EccPrivateKeyDecode(
|
||||||
|
|
||||||
ret = wc_EccPrivateKeyDecode(key, &idx, ecc, keySz);
|
ret = wc_EccPrivateKeyDecode(key, &idx, ecc, keySz);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
throwWolfCryptException(env, "Failed to decode private key");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
|
|
||||||
LogStr("wc_EccPrivateKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
|
LogStr("wc_EccPrivateKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
|
||||||
#else
|
#else
|
||||||
|
@ -172,7 +191,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1EccPublicKeyDecode(
|
||||||
|
|
||||||
ret = wc_EccPublicKeyDecode(key, &idx, ecc, keySz);
|
ret = wc_EccPublicKeyDecode(key, &idx, ecc, keySz);
|
||||||
if (ret != 0)
|
if (ret != 0)
|
||||||
throwWolfCryptException(env, "Failed to decode public key");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
|
|
||||||
LogStr("wc_EccPublicKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
|
LogStr("wc_EccPublicKeyDecode(key, keySz, ecc=%p) = %d\n", ecc, ret);
|
||||||
#else
|
#else
|
||||||
|
@ -218,7 +237,7 @@ Java_com_wolfssl_wolfcrypt_Ecc_wc_1ecc_1shared_1secret(
|
||||||
throwWolfCryptException(env, "Failed to allocate shared secret");
|
throwWolfCryptException(env, "Failed to allocate shared secret");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throwWolfCryptException(env, "Failed to generate shared secret");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
LogStr("wc_ecc_shared_secret(priv, pub, output=%p, outputSz) = %d\n",
|
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");
|
throwWolfCryptException(env, "Failed to allocate signature");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throwWolfCryptException(env, "Failed to generate signature");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
LogStr("wc_ecc_sign_hash(input, inSz, output, &outSz, rng, ecc) = %d\n",
|
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) {
|
if (ret == 0) {
|
||||||
ret = status;
|
ret = status;
|
||||||
} else {
|
} else {
|
||||||
throwWolfCryptException(env, "Failed to verify signature");
|
throwWolfCryptExceptionFromError(env, ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
LogStr(
|
LogStr(
|
||||||
|
|
|
@ -22,9 +22,9 @@
|
||||||
#ifndef __ANDROID__
|
#ifndef __ANDROID__
|
||||||
#include <wolfssl/options.h>
|
#include <wolfssl/options.h>
|
||||||
#endif
|
#endif
|
||||||
#include <wolfssl/wolfcrypt/error-crypt.h>
|
|
||||||
|
|
||||||
#include <com_wolfssl_wolfcrypt_WolfCryptError.h>
|
#include <com_wolfssl_wolfcrypt_WolfCryptError.h>
|
||||||
|
#include <wolfcrypt_jni_error.h>
|
||||||
|
|
||||||
JNIEXPORT jstring JNICALL Java_com_wolfssl_wolfcrypt_WolfCryptError_wc_1GetErrorString
|
JNIEXPORT jstring JNICALL Java_com_wolfssl_wolfcrypt_WolfCryptError_wc_1GetErrorString
|
||||||
(JNIEnv* env, jclass obj, jint error)
|
(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));
|
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));
|
||||||
|
}
|
|
@ -26,6 +26,7 @@
|
||||||
|
|
||||||
#include <com_wolfssl_wolfcrypt_NativeStruct.h>
|
#include <com_wolfssl_wolfcrypt_NativeStruct.h>
|
||||||
#include <wolfcrypt_jni_NativeStruct.h>
|
#include <wolfcrypt_jni_NativeStruct.h>
|
||||||
|
#include <wolfcrypt_jni_error.h>
|
||||||
|
|
||||||
/* #define WOLFCRYPT_JNI_DEBUG_ON */
|
/* #define WOLFCRYPT_JNI_DEBUG_ON */
|
||||||
#include <wolfcrypt_jni_debug.h>
|
#include <wolfcrypt_jni_debug.h>
|
||||||
|
@ -53,13 +54,6 @@ JNIEXPORT void JNICALL Java_com_wolfssl_wolfcrypt_NativeStruct_xfree(
|
||||||
/*
|
/*
|
||||||
* Utilitary functions
|
* 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)
|
void* getNativeStruct(JNIEnv* env, jobject this)
|
||||||
{
|
{
|
||||||
if (this) {
|
if (this) {
|
||||||
|
@ -68,7 +62,7 @@ void* getNativeStruct(JNIEnv* env, jobject this)
|
||||||
jlong nativeStruct = (*env)->GetLongField(env, this, field);
|
jlong nativeStruct = (*env)->GetLongField(env, this, field);
|
||||||
|
|
||||||
if (!nativeStruct)
|
if (!nativeStruct)
|
||||||
throwGetNativeStructError(env);
|
throwWolfCryptException(env, "Failed to retrieve native struct");
|
||||||
|
|
||||||
return (void*) nativeStruct;
|
return (void*) nativeStruct;
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,8 @@ public class Ecc extends NativeStruct {
|
||||||
|
|
||||||
private native void wc_ecc_make_key(Rng rng, int size);
|
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);
|
private native byte[] wc_ecc_shared_secret(Ecc pubKey);
|
||||||
|
|
||||||
private native void wc_ecc_import_x963(byte[] key);
|
private native void wc_ecc_import_x963(byte[] key);
|
||||||
|
@ -94,6 +96,15 @@ public class Ecc extends NativeStruct {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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) {
|
public void importX963(byte[] key) {
|
||||||
if (state == WolfCryptState.INITIALIZED) {
|
if (state == WolfCryptState.INITIALIZED) {
|
||||||
wc_ecc_import_x963(key);
|
wc_ecc_import_x963(key);
|
||||||
|
|
|
@ -145,38 +145,39 @@ public enum WolfCryptError {
|
||||||
private static final Map<Integer, WolfCryptError> intToErrMap =
|
private static final Map<Integer, WolfCryptError> intToErrMap =
|
||||||
new HashMap<Integer, WolfCryptError>();
|
new HashMap<Integer, WolfCryptError>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
for (WolfCryptError err : WolfCryptError.values()) {
|
for (WolfCryptError err : WolfCryptError.values()) {
|
||||||
intToErrMap.put(err.code, err);
|
intToErrMap.put(err.code, err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private WolfCryptError(int code) {
|
private WolfCryptError(int code) {
|
||||||
this.code = code;
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getCode() {
|
public int getCode() {
|
||||||
return this.code;
|
return this.code;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getDescription() {
|
public String getDescription() {
|
||||||
if (this == WolfCryptError.NO_ERROR_FOUND)
|
if (this == WolfCryptError.NO_ERROR_FOUND)
|
||||||
return "No error code found in JNI WolfCryptError enum";
|
return "No error code found in JNI WolfCryptError enum";
|
||||||
return wc_GetErrorString(this.code);
|
return wc_GetErrorString(this.code);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static WolfCryptError fromInt(int code) {
|
public static WolfCryptError fromInt(int code) {
|
||||||
WolfCryptError err = intToErrMap.get(Integer.valueOf(code));
|
WolfCryptError err = intToErrMap.get(Integer.valueOf(code));
|
||||||
if (err == null)
|
|
||||||
return WolfCryptError.NO_ERROR_FOUND;
|
|
||||||
return err;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static native String wc_GetErrorString(int error);
|
if (err == null)
|
||||||
|
return WolfCryptError.NO_ERROR_FOUND;
|
||||||
|
|
||||||
@Override
|
return err;
|
||||||
public String toString() {
|
}
|
||||||
return code + ": " + this.getDescription();
|
|
||||||
}
|
private static native String wc_GetErrorString(int error);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "(" + code + ") " + this.getDescription();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -29,18 +29,21 @@ import com.wolfssl.wolfcrypt.WolfCryptError;
|
||||||
* @author Moisés Guimarães
|
* @author Moisés Guimarães
|
||||||
* @version 1.0, February 2015
|
* @version 1.0, February 2015
|
||||||
*/
|
*/
|
||||||
public class WolfCryptException extends Exception {
|
public class WolfCryptException extends RuntimeException {
|
||||||
|
|
||||||
private static final long serialVersionUID = 142053665132156225L;
|
private static final long serialVersionUID = 142053665132156225L;
|
||||||
private WolfCryptError error;
|
private WolfCryptError error;
|
||||||
|
private int code;
|
||||||
|
|
||||||
public WolfCryptException(String reason) {
|
public WolfCryptException(String reason) {
|
||||||
super(reason);
|
super(reason);
|
||||||
}
|
}
|
||||||
|
|
||||||
public WolfCryptException(String reason, WolfCryptError error) {
|
public WolfCryptException(int code) {
|
||||||
super(reason);
|
super(WolfCryptError.fromInt(code).getDescription());
|
||||||
this.error = error;
|
|
||||||
|
this.error = WolfCryptError.fromInt(code);
|
||||||
|
this.code = code;
|
||||||
}
|
}
|
||||||
|
|
||||||
public WolfCryptException(String reason, Throwable cause) {
|
public WolfCryptException(String reason, Throwable cause) {
|
||||||
|
@ -54,5 +57,9 @@ public class WolfCryptException extends Exception {
|
||||||
public WolfCryptError getError() {
|
public WolfCryptError getError() {
|
||||||
return this.error;
|
return this.error;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getCode() {
|
||||||
|
return this.code;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,9 +49,10 @@ public class EccTest {
|
||||||
public void setUpEcc() {
|
public void setUpEcc() {
|
||||||
try {
|
try {
|
||||||
key = new Ecc();
|
key = new Ecc();
|
||||||
} catch (UnsupportedOperationException e) {
|
} catch (WolfCryptException e) {
|
||||||
if (e.getMessage().equals("Feature not compiled in"))
|
if (e.getError() == WolfCryptError.NOT_COMPILED_IN)
|
||||||
Assume.assumeNoException(e);
|
System.out.println("Ecc test skipped: " + e.getError());
|
||||||
|
Assume.assumeNoException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue