[英]RSA::PublicKey Load function segfaults ART (Android Runtime)
我們為Cryptopp實現了一個小型包裝程序,以在iOS和Android(JNI)之間交換密鑰。 共享代碼可在iOS和pre-ART android設備上使用。 據說ART及其垃圾收集器現在要嚴格得多。
需要指出的是,設置私鑰功能在Dalvik和ART運行時都成功。
代碼:->設置公鑰:
jboolean *isCopy;
//get bytes from jbytearray
jbyte* ba = (jbyte *)env->GetByteArrayElements( byteArray, isCopy);
//load bytearray to crypto bytequeue
ByteQueue queue2;
queue2.Put2((byte*)ba, 1000, 0, true);
//build public key
AutoSeededRandomPool rnd;
RSA::PublicKey publicKey;
publicKey.Load(*queue2); //<-------- CRASH
->設置私鑰
jboolean *isCopy;
jbyte* ba = (jbyte *)env->GetByteArrayElements( byteArray, isCopy);
//load bytearray to bytequeue
ByteQueue queue2;
queue2.Put2((byte*)ba, 3072, 0, true);
//fill up the key
RSA::PrivateKey privateKey;
privateKey.Load(queue2);
堆棧跟蹤:
backtrace:
#00 pc 00027e6c <project_name>/lib/arm/libstlport_shared.so
#01 pc 00027e79 <project_name>/lib/arm/libstlport_shared.so
#02 pc 00027efb <project_name>/lib/arm/libstlport_shared.so (std::terminate()+6)
#03 pc 000273d3 <project_name>/lib/arm/libstlport_shared.so
#04 pc 000268c9 <project_name>/lib/arm/libstlport_shared.so
#05 pc 0002698b <project_name>/lib/arm/libstlport_shared.so (__cxa_throw+34)
#06 pc 001b3ce4 <project_name>/lib/arm/libcryptopp.so (CryptoPP::BERDecodeError()+128)
#07 pc 001b1598 <project_name>/lib/arm/libcryptopp.so (CryptoPP::BERGeneralDecoder::Init(unsigned char)+56)
#08 pc 001b1638 <project_name>/lib/arm/libcryptopp.so (CryptoPP::BERGeneralDecoder::BERGeneralDecoder(CryptoPP::BufferedTransformation&, unsigned char)+104)
#09 pc 0027697c <project_name>/lib/arm/libcryptopp.so (CryptoPP::Integer::BERDecode(CryptoPP::BufferedTransformation&)+20)
#10 pc 002aec7c <project_name>/lib/arm/libcryptopp.so (CryptoPP::RSAFunction::BERDecodePublicKey(CryptoPP::BufferedTransformation&, bool, unsigned int)+64)
#11 pc 001b20e0 <project_name>/lib/arm/libcryptopp.so (CryptoPP::X509PublicKey::BERDecode(CryptoPP::BufferedTransformation&)+264)
#12 pc 00014a0b <project_name>/lib/arm/libsecurity.so (CryptoPP::ASN1CryptoMaterial<CryptoPP::PublicKey>::Load(CryptoPP::BufferedTransformation&)+6)
值得一提的是,默認情況下,新的(主要是)新的Google設備(Nexus 4、5、7)默認使用ART。
請指教!
jbyte* ba = (jbyte *)env->GetByteArrayElements( byteArray, isCopy); ByteQueue queue; queue.Put((byte*)ba, 1000, 0, true); ...
和:
ByteQueue queue; queue.Put((byte*)ba, 3072, 0, true); ...
那是不對的。 當密鑰通常為數百個字節時,您無法對大小進行硬編碼。
這是我用於jbyteArray
的代碼:
if(env && ba)
{
ReadByteBuffer buffer(env, ba);
const byte* _arr = buffer.GetByteArray();
size_t _len = buffer.GetArrayLen();
ByteQueue queue;
queue.Put(_arr, _len);
...
}
並且在捕獲BERDecodeErr
時應將其包裝在try/catch
中,以防其格式不正確。 這似乎是您遇到的另一個問題。 請參見BERDecodeErr類參考 。
這也不是完全正確的(請注意基於堆棧的ByteQueue
的指針取消引用):
ByteQueue queue2;
...
publicKey.Load(*queue2);
我將消除這種差異,但是您應該確保所發布的代碼能夠代表您的工作。
值得一提的是,默認情況下,新的(主要是)新的Google設備(Nexus 4、5、7)默認使用ART。
我有一個Nexus 5進行測試,並且Crypto ++可以正常運行:)
這是我用於ReadByteBuffer
的類。 它處理析構函數中的釋放。
class ReadByteBuffer
{
public:
explicit ReadByteBuffer(JNIEnv*& env, jbyteArray& barr)
: m_env(env), m_arr(barr), m_ptr(NULL), m_len(0)
{
if(m_env && m_arr)
{
m_ptr = m_env->GetByteArrayElements(m_arr, NULL);
m_len = m_env->GetArrayLength(m_arr);
}
}
~ReadByteBuffer()
{
if(m_env && m_arr)
{
m_env->ReleaseByteArrayElements(m_arr, m_ptr, JNI_ABORT);
}
}
const byte* GetByteArray() const {
return (const byte*) m_ptr;
}
size_t GetArrayLen() const {
if(m_len < 0)
return 0;
return (size_t) m_len;
}
private:
JNIEnv*& m_env;
jbyteArray& m_arr;
jbyte* m_ptr;
jint m_len;
};
這是我用來寫作的課程。 像它的對應對象一樣, WriteByteBuffer
在析構函數中處理提交和釋放。
class WriteByteBuffer
{
public:
explicit WriteByteBuffer(JNIEnv*& env, jbyteArray& barr)
: m_env(env), m_arr(barr), m_ptr(NULL), m_len(0)
{
if(m_env && m_arr)
{
m_ptr = m_env->GetByteArrayElements(m_arr, NULL);
m_len = m_env->GetArrayLength(m_arr);
}
}
~WriteByteBuffer()
{
if(m_env && m_arr)
{
m_env->ReleaseByteArrayElements(m_arr, m_ptr, 0);
}
}
byte* GetByteArray() const {
return (byte*) m_ptr;
}
size_t GetArrayLen() const {
if(m_len < 0)
return 0;
return (size_t) m_len;
}
private:
JNIEnv*& m_env;
jbyteArray& m_arr;
jbyte* m_ptr;
jint m_len;
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.