Save immediately on persistent state modification.
Modifications for #6.
This commit is contained in:
@@ -17,6 +17,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "Channel.hpp"
|
#include "Channel.hpp"
|
||||||
|
#include "Storage.hpp"
|
||||||
#include "U2F_CMD.hpp"
|
#include "U2F_CMD.hpp"
|
||||||
#include "u2f.hpp"
|
#include "u2f.hpp"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
@@ -46,8 +47,12 @@ void Channel::handle(const shared_ptr<U2FMessage> uMsg) {
|
|||||||
|
|
||||||
auto cmd = U2F_CMD::get(uMsg);
|
auto cmd = U2F_CMD::get(uMsg);
|
||||||
|
|
||||||
if (cmd)
|
if (cmd) {
|
||||||
return cmd->respond(this->cid);
|
cmd->respond(this->cid);
|
||||||
|
|
||||||
|
if (cmd->modifiesPersistentState())
|
||||||
|
Storage::save();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Channel::init(const ChannelInitState newInitState) {
|
void Channel::init(const ChannelInitState newInitState) {
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ void Storage::init(const string& dirPrefix) {
|
|||||||
void Storage::save() {
|
void Storage::save() {
|
||||||
ofstream file{ Storage::filename };
|
ofstream file{ Storage::filename };
|
||||||
|
|
||||||
for (auto& keypair : Storage::appParams) {
|
for (const auto& keypair : Storage::appParams) {
|
||||||
const auto& keyID = keypair.first;
|
const auto& keyID = keypair.first;
|
||||||
const auto& appParam = keypair.second;
|
const auto& appParam = keypair.second;
|
||||||
const auto& privKey = Storage::privKeys[keyID];
|
const auto& privKey = Storage::privKeys[keyID];
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ public:
|
|||||||
virtual ~U2F_CMD() = default;
|
virtual ~U2F_CMD() = default;
|
||||||
static std::shared_ptr<U2F_CMD> get(const std::shared_ptr<U2FMessage> uMsg);
|
static std::shared_ptr<U2F_CMD> get(const std::shared_ptr<U2FMessage> uMsg);
|
||||||
virtual void respond(const uint32_t channelID) const = 0;
|
virtual void respond(const uint32_t channelID) const = 0;
|
||||||
|
virtual bool modifiesPersistentState() const = 0;
|
||||||
}; // For polymorphic type casting
|
}; // For polymorphic type casting
|
||||||
|
|||||||
@@ -52,3 +52,7 @@ void U2F_Init_CMD::respond(const uint32_t channelID) const {
|
|||||||
|
|
||||||
msg.write();
|
msg.write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool U2F_Init_CMD::modifiesPersistentState() const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ struct U2F_Init_CMD : U2F_CMD {
|
|||||||
public:
|
public:
|
||||||
U2F_Init_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
U2F_Init_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
||||||
virtual void respond(const uint32_t channelID) const override;
|
virtual void respond(const uint32_t channelID) const override;
|
||||||
|
virtual bool modifiesPersistentState() const override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -192,3 +192,10 @@ const map<uint8_t, bool> U2F_Msg_CMD::usesData = { { U2F_REG, true },
|
|||||||
void U2F_Msg_CMD::respond(const uint32_t channelID) const {
|
void U2F_Msg_CMD::respond(const uint32_t channelID) const {
|
||||||
U2F_Msg_CMD::error(channelID, static_cast<uint16_t>(APDU_STATUS::SW_INS_NOT_SUPPORTED));
|
U2F_Msg_CMD::error(channelID, static_cast<uint16_t>(APDU_STATUS::SW_INS_NOT_SUPPORTED));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool U2F_Msg_CMD::modifiesPersistentState() const {
|
||||||
|
const auto usesPersistentState = usesData.find(ins);
|
||||||
|
|
||||||
|
// Be conservative for future instructions. Assume that they do modify persist state.
|
||||||
|
return (usesPersistentState == usesData.end()) || usesPersistentState->second;
|
||||||
|
}
|
||||||
|
|||||||
@@ -40,5 +40,6 @@ protected:
|
|||||||
public:
|
public:
|
||||||
static std::shared_ptr<U2F_Msg_CMD> generate(const std::shared_ptr<U2FMessage> uMsg);
|
static std::shared_ptr<U2F_Msg_CMD> generate(const std::shared_ptr<U2FMessage> uMsg);
|
||||||
static void error(const uint32_t channelID, const uint16_t errCode);
|
static void error(const uint32_t channelID, const uint16_t errCode);
|
||||||
void respond(const uint32_t channelID) const;
|
void respond(const uint32_t channelID) const override;
|
||||||
|
virtual bool modifiesPersistentState() const override;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -33,3 +33,7 @@ void U2F_Ping_CMD::respond(const uint32_t channelID) const {
|
|||||||
msg.data = nonce;
|
msg.data = nonce;
|
||||||
msg.write();
|
msg.write();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool U2F_Ping_CMD::modifiesPersistentState() const {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ struct U2F_Ping_CMD : U2F_CMD {
|
|||||||
public:
|
public:
|
||||||
U2F_Ping_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
U2F_Ping_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
||||||
virtual void respond(const uint32_t channelID) const override;
|
virtual void respond(const uint32_t channelID) const override;
|
||||||
|
virtual bool modifiesPersistentState() const override;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user