Improved U2F HID error handling.

Fixed U2FMessage's error reporting.
Implemented better error reporting.
This commit is contained in:
2018-08-10 14:24:26 +00:00
parent 2ced303d3a
commit a20c190fea
3 changed files with 23 additions and 16 deletions

View File

@@ -160,11 +160,10 @@ U2FMessage::U2FMessage(const uint32_t nCID, const uint8_t nCMD)
: cid{ nCID }, cmd{ nCMD }
{}
void U2FMessage::error(const uint32_t tCID, const uint16_t tErr)
void U2FMessage::error(const uint32_t tCID, const uint8_t tErr)
{
U2FMessage msg{};
msg.cid = tCID;
msg.cmd = U2FHID_ERROR;
msg.data.push_back((tErr >> 8) & 0xFF);
msg.data.push_back(tErr & 0xFF);
msg.data.push_back(tErr);
}

View File

@@ -7,20 +7,20 @@ using namespace std;
shared_ptr<U2F_CMD> U2F_CMD::get(const shared_ptr<U2FMessage> uMsg)
{
try
{
switch (uMsg->cmd)
{
case U2FHID_MSG:
try
{
return U2F_Msg_CMD::generate(uMsg);
}
catch (runtime_error)
{
return {};
}
case U2FHID_INIT:
return make_shared<U2F_Init_CMD>(uMsg);
default:
return {};
}
}
catch (runtime_error)
{
return {};
}
}

View File

@@ -9,8 +9,16 @@ U2F_Init_CMD::U2F_Init_CMD(const shared_ptr<U2FMessage> uMsg)
{
if (uMsg->cmd != U2FHID_INIT)
throw runtime_error{ "Failed to get U2F Init message" };
else if (uMsg->cid != CID_BROADCAST)
{
U2FMessage::error(uMsg->cid, ERR_OTHER);
throw runtime_error{ "Invalid CID for init command" };
}
else if (uMsg->data.size() != INIT_NONCE_SIZE)
{
U2FMessage::error(uMsg->cid, ERR_INVALID_LEN);
throw runtime_error{ "Init nonce is incorrect size" };
}
this->nonce = *reinterpret_cast<const uint64_t*>(uMsg->data.data());
}