我正试图让OpenSSL与Java和原生C一起为我的Android应用程序工作。
我到目前为止所做的:
初始化的openssl类似:

ret = SSL_library_init();
SSL_load_error_strings();
ctx = SSL_CTX_new(SSLv23_method());
ret = SSL_CTX_use_certificate(ctx, sc_cert); // sc_cert is the Smart Cards auth certificate -> this is working!
_ssl = SSL_new(ctx);

现在我试图设置rsa_sign函数(我自己的)回调:
RSA_METHOD *rsameth = RSA_get_default_method();
rsameth -> rsa_verify = &sc_rsa_verify; // Just returns 1, but gets never called.
rsameth -> rsa_sign = &sc_rsa_sign;
rsameth -> flags |= RSA_FLAG_SIGN_VER; // If i would use 0x1FF my function gets called, why?
RSA_set_default_method(rsameth);

_rsa = RSA_new(); // handle error
// No need to do this: RSA_set_default_method already did that!
//_rsa -> meth = rsameth;
//_rsa -> flags |= RSA_FLAG_SIGN_VER;
//RSA_set_method(_rsa, rsameth);
ret = SSL_use_RSAPrivateKey(_ssl, _rsa);
RSA_set_default_method(rsameth);

现在我的最后一步:
sbio = BIO_new_socket(sock, BIO_NOCLOSE); // Sock had been created before and is working!
SSL_set_bio(_ssl, sbio, sbio);
if(_session) SSL_set_session(_ssl, _session);
ret = SSL_connect(_ssl);

现在在ssl_连接之后,我得到:
无错误:当我自己的rsa_符号(sc_rsa_符号)未被调用时
或者:错误:1409441b:ssl例程:ssl3_read_bytes:tlsv1警报解密错误,当调用我自己的rsa_sign(sc_rsa_sign)时
现在您可以查看我自己的rsa_sign(sc_rsa_sign)函数:
jbyteArray to_crypt = (*_env) -> NewByteArray(_env, m_length);
(*_env) -> SetByteArrayRegion(_env, to_crypt, 0, m_length, m);

// Jump into Java and do the crypt on card. This is working!
jbyteArray crypted = (*_env) -> CallObjectMethod(_env, _obj, _callback_cryptoncard, to_crypt);

// I also read that siglen should be the size of RSA_size(rsa), thus rsa -> n is not allowed to be NULL here. But it is! What is wrong here?
//int size = RSA_size(rsa);
//sigret = malloc(size);

// Obtain bytes from Java. Working (right size and crypted)!
*siglen = (*_env) -> GetArrayLength(_env, crypted);
sigret = (*_env) -> GetByteArrayElements(_env, crypted, NULL);

//(*_env) -> ReleaseByteArrayElements(_env, crypted, sigret, 0);

return 1;

这就是我到目前为止所做的一切。已经挣扎了好几个星期了!希望有人能帮助我!

最佳答案

我搞错了(难堪):
sigret = (*_env) -> GetByteArrayElements(_env, crypted, NULL);
过保护指针,我将其更改为:
unsigned char *sigrettemp = (*_env) -> GetByteArrayElements(_env, crypted, NULL);
memcpy(sigret, sigrettemp, siglen);
现在一切都很好!

10-07 13:21