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 "Storage.hpp"
|
||||
#include "U2F_CMD.hpp"
|
||||
#include "u2f.hpp"
|
||||
#include <iostream>
|
||||
@@ -46,8 +47,12 @@ void Channel::handle(const shared_ptr<U2FMessage> uMsg) {
|
||||
|
||||
auto cmd = U2F_CMD::get(uMsg);
|
||||
|
||||
if (cmd)
|
||||
return cmd->respond(this->cid);
|
||||
if (cmd) {
|
||||
cmd->respond(this->cid);
|
||||
|
||||
if (cmd->modifiesPersistentState())
|
||||
Storage::save();
|
||||
}
|
||||
}
|
||||
|
||||
void Channel::init(const ChannelInitState newInitState) {
|
||||
|
||||
@@ -80,7 +80,7 @@ void Storage::init(const string& dirPrefix) {
|
||||
void Storage::save() {
|
||||
ofstream file{ Storage::filename };
|
||||
|
||||
for (auto& keypair : Storage::appParams) {
|
||||
for (const auto& keypair : Storage::appParams) {
|
||||
const auto& keyID = keypair.first;
|
||||
const auto& appParam = keypair.second;
|
||||
const auto& privKey = Storage::privKeys[keyID];
|
||||
|
||||
@@ -28,4 +28,5 @@ public:
|
||||
virtual ~U2F_CMD() = default;
|
||||
static std::shared_ptr<U2F_CMD> get(const std::shared_ptr<U2FMessage> uMsg);
|
||||
virtual void respond(const uint32_t channelID) const = 0;
|
||||
virtual bool modifiesPersistentState() const = 0;
|
||||
}; // For polymorphic type casting
|
||||
|
||||
@@ -52,3 +52,7 @@ void U2F_Init_CMD::respond(const uint32_t channelID) const {
|
||||
|
||||
msg.write();
|
||||
}
|
||||
|
||||
bool U2F_Init_CMD::modifiesPersistentState() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,4 +28,5 @@ struct U2F_Init_CMD : U2F_CMD {
|
||||
public:
|
||||
U2F_Init_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
||||
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 {
|
||||
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:
|
||||
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);
|
||||
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.write();
|
||||
}
|
||||
|
||||
bool U2F_Ping_CMD::modifiesPersistentState() const {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -28,4 +28,5 @@ struct U2F_Ping_CMD : U2F_CMD {
|
||||
public:
|
||||
U2F_Ping_CMD(const std::shared_ptr<U2FMessage> uMsg);
|
||||
virtual void respond(const uint32_t channelID) const override;
|
||||
virtual bool modifiesPersistentState() const override;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user