Files
android/libwvdrmengine/oemcrypto/util/include/cbor_validator.h
Cong Lin dbb0bea701 Add Bcc validator to oemcrypto util and unit tests
A Bcc validator that can parse and validate BCC. This is to support better
prov40 unit tests regarding OEMCrypto_GetBootCertificateChain() later.

Test: opk_ta_p40
Bug: 300304834
Bug: 307968622
Change-Id: I3cfdad9f1891c6abc83051af1d80a20e0adeb58b
2024-02-22 14:43:11 -08:00

81 lines
2.7 KiB
C++

// 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 <string>
#include <vector>
#include "cppbor.h"
#include "cppbor_parse.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:
explicit CborValidator() {}
virtual ~CborValidator() = default;
CborValidator(const CborValidator&) = delete;
CborValidator& operator=(const CborValidator&) = delete;
// Decodes |cbor| and sets |message_status_|.
virtual CborMessageStatus Parse(const std::vector<uint8_t>& 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<std::pair<CborMessageStatus, std::string>>&
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<std::pair<CborMessageStatus, std::string>>&
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);
CborMessageStatus message_status_ = kCborUninitialized;
private:
// Internal status of parsing and validating.
cppbor::ParseResult parse_result_ = {};
std::vector<std::pair<CborMessageStatus, std::string>> validate_messages_;
};
} // namespace util
} // namespace wvoec
#endif // WVOEC_UTIL_CBOR_VALIDATOR_H_