diff options
Diffstat (limited to 'include/gatekeeper/gatekeeper_messages.h')
-rw-r--r-- | include/gatekeeper/gatekeeper_messages.h | 107 |
1 files changed, 66 insertions, 41 deletions
diff --git a/include/gatekeeper/gatekeeper_messages.h b/include/gatekeeper/gatekeeper_messages.h index 3cbd817..82fdbcd 100644 --- a/include/gatekeeper/gatekeeper_messages.h +++ b/include/gatekeeper/gatekeeper_messages.h @@ -19,6 +19,7 @@ #include <stdint.h> #include <gatekeeper/UniquePtr.h> +#include <new> #include "gatekeeper_utils.h" /** @@ -34,36 +35,64 @@ typedef enum { ERROR_INVALID = 1, ERROR_RETRY = 2, ERROR_UNKNOWN = 3, + ERROR_MEMORY_ALLOCATION_FAILED = 4, } gatekeeper_error_t; struct SizedBuffer { SizedBuffer() { length = 0; } - - /* - * Constructs a SizedBuffer of a provided - * length. - */ - explicit SizedBuffer(uint32_t length) { - if (length != 0) { - buffer.reset(new uint8_t[length]); - } else { - buffer.reset(); + ~SizedBuffer() { + if (buffer && length > 0) { + memset_s(buffer.get(), 0, length); } - this->length = length; } - /* * Constructs a SizedBuffer out of a pointer and a length * Takes ownership of the buf pointer, and deallocates it * when destructed. */ SizedBuffer(uint8_t buf[], uint32_t len) { - buffer.reset(buf); - length = len; + if (buf == nullptr) { + length = 0; + } else { + buffer.reset(buf); + length = len; + } + } + + SizedBuffer(SizedBuffer && rhs) : buffer(move(rhs.buffer)), length(rhs.length) { + rhs.length = 0; + } + + SizedBuffer & operator=(SizedBuffer && rhs) { + if (&rhs != this) { + buffer = move(rhs.buffer); + length = rhs.length; + rhs.length = 0; + } + return *this; + } + + operator bool() const { + return buffer; + } + + uint32_t size() const { return buffer ? length : 0; } + + /** + * Returns an pointer to the const buffer IFF the buffer is initialized and the length + * field holds a values greater or equal to the size of the requested template argument type. + */ + template <typename T> + const T* Data() const { + if (buffer.get() != nullptr && sizeof(T) <= length) { + return reinterpret_cast<const T*>(buffer.get()); + } + return nullptr; } +private: UniquePtr<uint8_t[]> buffer; uint32_t length; }; @@ -138,14 +167,13 @@ struct VerifyRequest : public GateKeeperMessage { VerifyRequest( uint32_t user_id, uint64_t challenge, - SizedBuffer *enrolled_password_handle, - SizedBuffer *provided_password_payload); - VerifyRequest(); - ~VerifyRequest(); + SizedBuffer enrolled_password_handle, + SizedBuffer provided_password_payload); + VerifyRequest() : challenge(0) {} - virtual uint32_t nonErrorSerializedSize() const; - virtual void nonErrorSerialize(uint8_t *buffer) const; - virtual gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end); + uint32_t nonErrorSerializedSize() const override; + void nonErrorSerialize(uint8_t *buffer) const override; + gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) override; uint64_t challenge; SizedBuffer password_handle; @@ -153,29 +181,27 @@ struct VerifyRequest : public GateKeeperMessage { }; struct VerifyResponse : public GateKeeperMessage { - VerifyResponse(uint32_t user_id, SizedBuffer *auth_token); + VerifyResponse(uint32_t user_id, SizedBuffer auth_token); VerifyResponse(); - ~VerifyResponse(); - void SetVerificationToken(SizedBuffer *auth_token); + void SetVerificationToken(SizedBuffer auth_token); - virtual uint32_t nonErrorSerializedSize() const; - virtual void nonErrorSerialize(uint8_t *buffer) const; - virtual gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end); + uint32_t nonErrorSerializedSize() const override; + void nonErrorSerialize(uint8_t *buffer) const override; + gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) override; SizedBuffer auth_token; bool request_reenroll; }; struct EnrollRequest : public GateKeeperMessage { - EnrollRequest(uint32_t user_id, SizedBuffer *password_handle, - SizedBuffer *provided_password, SizedBuffer *enrolled_password); - EnrollRequest(); - ~EnrollRequest(); + EnrollRequest(uint32_t user_id, SizedBuffer password_handle, + SizedBuffer provided_password, SizedBuffer enrolled_password); + EnrollRequest() = default; - virtual uint32_t nonErrorSerializedSize() const; - virtual void nonErrorSerialize(uint8_t *buffer) const; - virtual gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end); + uint32_t nonErrorSerializedSize() const override; + void nonErrorSerialize(uint8_t *buffer) const override; + gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) override; /** * The password handle returned from the previous call to enroll or NULL @@ -194,15 +220,14 @@ struct EnrollRequest : public GateKeeperMessage { struct EnrollResponse : public GateKeeperMessage { public: - EnrollResponse(uint32_t user_id, SizedBuffer *enrolled_password_handle); - EnrollResponse(); - ~EnrollResponse(); + EnrollResponse(uint32_t user_id, SizedBuffer enrolled_password_handle); + EnrollResponse() = default; - void SetEnrolledPasswordHandle(SizedBuffer *enrolled_password_handle); + void SetEnrolledPasswordHandle(SizedBuffer enrolled_password_handle); - virtual uint32_t nonErrorSerializedSize() const; - virtual void nonErrorSerialize(uint8_t *buffer) const; - virtual gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end); + uint32_t nonErrorSerializedSize() const override; + void nonErrorSerialize(uint8_t *buffer) const override; + gatekeeper_error_t nonErrorDeserialize(const uint8_t *payload, const uint8_t *end) override; SizedBuffer enrolled_password_handle; }; |