从本机代码返回到托管代码会损坏返回的对象



我正在尝试使用C++/CLI和yubico的libu2f-host(https://github.com/Yubico/libu2f-host(在我的WPF程序中构建U2F身份验证的实现,我的C++/CLI dll中有一个完全本机的函数,它调用适当的libu2f函数,然后返回一个指向u2f_reply(我自己的类(的指针,其中包含响应和错误代码。 但是,当 dll 的托管部分收到此对象时,响应代码仍然正确,但响应字符串似乎已完全损坏。

我尝试立即返回 char 指针,然后使用 marshal 将其转换为托管字符串,并且我尝试使用标准 C# 字符串。 我尝试使用 memcpy 将响应复制到回复>响应中,但没有运气。

Managed_U2F_Reply^ Utilities::AuthenticateU2F(System::String^ challenge)
{
IntPtr ptrToChallenge = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(challenge);
u2f_reply* reply = NativeUtilities::AuthenticateU2F(static_cast<char*>(ptrToChallenge.ToPointer()));
//clean up before return
System::Runtime::InteropServices::Marshal::FreeHGlobal(ptrToChallenge);
Managed_U2F_Reply^ managedReply = gcnew Managed_U2F_Reply();
managedReply->ResultCode = reply->responseCode;
if (managedReply->ResultCode == 0)
{
managedReply->Response = gcnew String(reply->response, 0, reply->length);
}
return managedReply;
}
struct u2f_reply
{
int responseCode;
int length;
char* response;
};
u2f_reply* NativeUtilities::AuthenticateU2F(char* challenge)
{
u2f_reply* reply = new u2f_reply();
char response[2048] = { 0 };
size_t response_len = sizeof(response);
u2fh_devs* devs = NULL;
u2fh_rc rc;
u2fh_initflags flags = (u2fh_initflags)0;
rc = u2fh_global_init(flags);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_devs_init(&devs);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_devs_discover(devs, NULL);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_authenticate2(devs, challenge, "https://demo.yubico.com", response, &response_len, U2FH_REQUEST_USER_PRESENCE);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
u2fh_devs_done(devs);
u2fh_global_done();
reply->length = response_len;
reply->response = response;//adding printf here prints it out correctly, yet it doesn't when I call it in Utilities::AuthenticateU2F
reply->responseCode = rc;
return reply;
}

我希望它返回一个带有响应数据的 json 对象,如下所示:https://developers.yubico.com/libu2f-host/,但我得到一个随机的 ASCII 字符字符串(无法粘贴到此处(并且没有错误

感谢Hans Passant,我通过使用以下方法解决了这个问题:

u2f_reply* NativeUtilities::AuthenticateU2F(char* challenge)
{
u2f_reply* reply = new u2f_reply();
char response[2048] = { 0 };
size_t response_len = sizeof(response);
u2fh_devs* devs = NULL;
u2fh_rc rc;
u2fh_initflags flags = (u2fh_initflags)0;
rc = u2fh_global_init(flags);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_devs_init(&devs);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_devs_discover(devs, NULL);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
rc = u2fh_authenticate2(devs, challenge, "https://demo.yubico.com", response, &response_len, U2FH_REQUEST_USER_PRESENCE);
if (rc != U2FH_OK)
{
reply->responseCode = rc;
return reply;
}
u2fh_devs_done(devs);
u2fh_global_done();
reply->length = response_len;
reply->response = new char[response_len];
memcpy_s(reply->response, response_len, response, response_len);
reply->responseCode = rc;
return reply;
}

相关内容

  • 没有找到相关文章

最新更新