gpt4 book ai didi

java - OpenSSL 覆盖由智能卡执行的 RSA_sign

转载 作者:太空狗 更新时间:2023-10-29 14:26:16 27 4
gpt4 key购买 nike

我正在尝试让 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;

这就是我到目前为止所做的一切。几个星期以来一直在努力解决这个问题!希望有人能帮助我!

最佳答案

我弄错了(尴尬):

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

覆盖了指针,我把它改成了:

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

现在一切正常!

关于java - OpenSSL 覆盖由智能卡执行的 RSA_sign,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12261989/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com