// Copyright 2023 Google LLC. All Rights Reserved. This file and proprietary // source code may only be used and distributed under the Widevine License // Agreement. // // Reference implementation utilities of OEMCrypto APIs // #ifndef WVOEC_UTIL_CBOR_VALIDATOR_H_ #define WVOEC_UTIL_CBOR_VALIDATOR_H_ #include #include #include #include #include "cppbor.h" #include "cppbor_parse.h" #include "wv_class_utils.h" namespace wvoec { namespace util { // CborMessageStatus values are ranked in level of severity. // kCborUninitialized being the lowest severity, and // kCborValidateFatal being the highest. enum CborMessageStatus { kCborUninitialized = 0, kCborParseOk = 1, kCborParseError = 2, kCborValidateOk = 3, kCborValidateWarning = 4, kCborValidateError = 5, kCborValidateFatal = 6 }; std::string CppborMajorTypeToString(cppbor::MajorType type); std::string CborMessageStatusToString(CborMessageStatus status); class CborValidator { public: CborValidator() = default; WVCDM_DISALLOW_COPY_AND_MOVE(CborValidator); virtual ~CborValidator() = default; // Decodes |cbor| and sets |message_status_|. virtual CborMessageStatus Parse(const std::vector& cbor); const cppbor::ParseResult* GetParseResult() const; // Returns pretty-printed CBOR for |parse_result_|. Returns empty string if // |parse_result_| is not valid. std::string GetRawMessage() const; // Verifies the fields in |parse_result_| to have expected types and values. // Requires that Parse() is called first and |parse_result_| contains a valid // CBOR message. virtual CborMessageStatus Validate(); // Returns all validation messages from Validate(). const std::vector>& GetValidateMessages() const { return validate_messages_; } // Prints |parse_result_| in readable format. Requires that Parse() is called // first and |parse_result_| contains a valid CBOR message. virtual std::string GetFormattedMessage() const; const cppbor::ParseResult& parse_result() const { return parse_result_; } const std::vector>& validate_messages() { return validate_messages_; } protected: void Reset(); // Writes validation output |msg| to |validate_messages_|, and updates // |message_status_| if the |status| is more severe than the current value. void AddValidationMessage(CborMessageStatus status, const std::string& msg); static const cppbor::Item* GetMapEntry(const cppbor::Map& map, const std::string& entry_name); // Checks whether an entry with |entry_name| and |major_type| exists in |map|. static std::string CheckMapEntry(const cppbor::Map& map, cppbor::MajorType major_type, const std::string& entry_name); CborMessageStatus message_status_ = kCborUninitialized; private: // Internal status of parsing and validating. cppbor::ParseResult parse_result_ = {}; std::vector> validate_messages_; }; // class CborValidator } // namespace util } // namespace wvoec #endif // WVOEC_UTIL_CBOR_VALIDATOR_H_