From 2b01dbc605a075372f915188fa4d2e86094376e8 Mon Sep 17 00:00:00 2001 From: Michael Kuc Date: Fri, 10 Aug 2018 18:31:22 +0000 Subject: [PATCH] Added ping command functionality. --- APDU.hpp | 3 ++- Controller.cpp | 2 +- Controller.hpp | 2 +- U2FMessage.hpp | 4 +--- U2F_CMD.cpp | 3 +++ U2F_Msg_CMD.cpp | 2 +- U2F_Ping_CMD.cpp | 20 ++++++++++++++++++++ U2F_Ping_CMD.hpp | 14 ++++++++++++++ U2F_Version_APDU.cpp | 4 ++-- U2F_Version_APDU.hpp | 2 +- monitor.cpp | 2 +- 11 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 U2F_Ping_CMD.cpp create mode 100644 U2F_Ping_CMD.hpp diff --git a/APDU.hpp b/APDU.hpp index 7229dbc..8981047 100644 --- a/APDU.hpp +++ b/APDU.hpp @@ -10,8 +10,9 @@ enum APDU : uint8_t enum APDU_STATUS : uint16_t { SW_NO_ERROR = 0x9000, - SW_WRONG_DATA = 0x6A80, + SW_WRONG_LENGTH = 0x6700, SW_CONDITIONS_NOT_SATISFIED = 0x6985, SW_COMMAND_NOT_ALLOWED = 0x6986, + SW_WRONG_DATA = 0x6A80, SW_INS_NOT_SUPPORTED = 0x6D00 }; diff --git a/Controller.cpp b/Controller.cpp index 1b15ff2..752f0ba 100644 --- a/Controller.cpp +++ b/Controller.cpp @@ -36,7 +36,7 @@ void Controller::handleTransaction() channels.at(opChannel).handle(msg); } -const uint32_t Controller::nextChannel() +uint32_t Controller::nextChannel() { do currChannel++; diff --git a/Controller.hpp b/Controller.hpp index 8b78ed9..b84be12 100644 --- a/Controller.hpp +++ b/Controller.hpp @@ -12,5 +12,5 @@ class Controller Controller(const uint32_t startChannel = 1); void handleTransaction(); - const uint32_t nextChannel(); + uint32_t nextChannel(); }; diff --git a/U2FMessage.hpp b/U2FMessage.hpp index 869b36f..7448e43 100644 --- a/U2FMessage.hpp +++ b/U2FMessage.hpp @@ -15,7 +15,5 @@ struct U2FMessage U2FMessage(const uint32_t nCID, const uint8_t nCMD); static std::shared_ptr readNonBlock(); void write(); - - protected: - static void error(const uint32_t tCID, const uint16_t tErr); + static void error(const uint32_t tCID, const uint8_t tErr); }; diff --git a/U2F_CMD.cpp b/U2F_CMD.cpp index d27898a..c3ac468 100644 --- a/U2F_CMD.cpp +++ b/U2F_CMD.cpp @@ -2,6 +2,7 @@ #include "u2f.hpp" #include "U2F_Msg_CMD.hpp" #include "U2F_Init_CMD.hpp" +#include "U2F_Ping_CMD.hpp" using namespace std; @@ -11,6 +12,8 @@ shared_ptr U2F_CMD::get(const shared_ptr uMsg) { switch (uMsg->cmd) { + case U2FHID_PING: + return make_shared(uMsg); case U2FHID_MSG: return U2F_Msg_CMD::generate(uMsg); case U2FHID_INIT: diff --git a/U2F_Msg_CMD.cpp b/U2F_Msg_CMD.cpp index 19084da..bce2508 100644 --- a/U2F_Msg_CMD.cpp +++ b/U2F_Msg_CMD.cpp @@ -162,7 +162,7 @@ shared_ptr U2F_Msg_CMD::generate(const shared_ptr uMsg) case APDU::U2F_AUTH: return make_shared(cmd, dBytes); case APDU::U2F_VER: - return make_shared(cmd); + return make_shared(cmd, dBytes); default: cerr << "Invalid command used" << endl; throw APDU_STATUS::SW_INS_NOT_SUPPORTED; diff --git a/U2F_Ping_CMD.cpp b/U2F_Ping_CMD.cpp new file mode 100644 index 0000000..54d723e --- /dev/null +++ b/U2F_Ping_CMD.cpp @@ -0,0 +1,20 @@ +#include "U2F_Ping_CMD.hpp" +#include "u2f.hpp" + +using namespace std; + +U2F_Ping_CMD::U2F_Ping_CMD(const shared_ptr uMsg) + : nonce{ uMsg->data } +{ + if (uMsg->cmd != U2FHID_PING) + throw runtime_error{ "Failed to get U2F ping message" }; +} + +void U2F_Ping_CMD::respond(const uint32_t channelID) const +{ + U2FMessage msg{}; + msg.cid = channelID; + msg.cmd = U2FHID_PING; + msg.data = nonce; + msg.write(); +} diff --git a/U2F_Ping_CMD.hpp b/U2F_Ping_CMD.hpp new file mode 100644 index 0000000..22e4a23 --- /dev/null +++ b/U2F_Ping_CMD.hpp @@ -0,0 +1,14 @@ +#pragma once +#include +#include +#include "U2F_CMD.hpp" +#include "U2FMessage.hpp" + +struct U2F_Ping_CMD : U2F_CMD +{ + std::vector nonce; + + public: + U2F_Ping_CMD(const std::shared_ptr uMsg); + virtual void respond(const uint32_t channelID) const override; +}; diff --git a/U2F_Version_APDU.cpp b/U2F_Version_APDU.cpp index 5702ee0..22f8370 100644 --- a/U2F_Version_APDU.cpp +++ b/U2F_Version_APDU.cpp @@ -7,12 +7,12 @@ using namespace std; -U2F_Version_APDU::U2F_Version_APDU(const U2F_Msg_CMD &msg) +U2F_Version_APDU::U2F_Version_APDU(const U2F_Msg_CMD &msg, const std::vector &data) { //Don't actually respond yet unless invalid if (msg.p1 != 0 || msg.p2 != 0) throw APDU_STATUS::SW_COMMAND_NOT_ALLOWED; - else if (msg.data.size() != 0) + else if (data.size() != 0) throw APDU_STATUS::SW_WRONG_LENGTH; } diff --git a/U2F_Version_APDU.hpp b/U2F_Version_APDU.hpp index 37d0105..ffe9c7e 100644 --- a/U2F_Version_APDU.hpp +++ b/U2F_Version_APDU.hpp @@ -4,6 +4,6 @@ struct U2F_Version_APDU : U2F_Msg_CMD { public: - U2F_Version_APDU(const U2F_Msg_CMD &msg); + U2F_Version_APDU(const U2F_Msg_CMD &msg, const std::vector &data); void respond(const uint32_t channelID) const override; }; diff --git a/monitor.cpp b/monitor.cpp index 0b9b771..3a390ae 100644 --- a/monitor.cpp +++ b/monitor.cpp @@ -28,7 +28,7 @@ int main() return EXIT_SUCCESS; } -void signalCallback(int signum) +void signalCallback([[maybe_unused]] int signum) { contProc = false; clog << "Caught SIGINT signal" << endl;