3

我正在尝试让 OpenSSL 与 Java 和 Native 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_connect 之后,我得到:

  • 没有错误:当我自己的 RSA_sign (sc_rsa_sign) 未被调用时
  • 或者:错误: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;

这就是我到目前为止所做的一切。几周以来一直在为此苦苦挣扎!希望有人能帮助我!

4

1 回答 1

1

我得到了错误(尴尬):

sigret = (*_env) -> GetByteArrayElements(_env, crypted, NULL);

覆盖了指针,我将其更改为:

unsigned char *sigrettemp = (*_env) -> GetByteArrayElements(_env, crypted, NULL); memcpy(sigret, sigrettemp, siglen);

现在一切正常!

于 2012-09-11T08:09:41.720 回答