101 lines
3.2 KiB
C++
101 lines
3.2 KiB
C++
// Copyright 2024 Google LLC. All Rights Reserved. This file and proprietary
|
|
// source code may only be used and distributed under the Widevine License
|
|
// Agreement.
|
|
//
|
|
#ifndef WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|
|
#define WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|
|
|
|
#include <sstream>
|
|
|
|
#include "cbor_validator.h"
|
|
#include "string_conversions.h"
|
|
|
|
namespace wvoec {
|
|
namespace util {
|
|
|
|
enum FieldStatus {
|
|
kAbsent = 0, // field key doesn't exist
|
|
kEmpty = 1, // field value is empty, e.g. empty string, map, array, etc.
|
|
kPresent = 2 // present and non-empty
|
|
};
|
|
|
|
std::string StatusToString(FieldStatus status);
|
|
// Apply a new status to current status if it is more severe.
|
|
void ApplyStatus(CborMessageStatus& status, CborMessageStatus new_status);
|
|
|
|
// Validates that the given field name is present, and prints error messages
|
|
// if not. It is acceptable that some field, although specified as required by
|
|
// DICE specification, is not present in practice. This function will return
|
|
// kCborValidateWarning instead of the default kCborValidateError in those
|
|
// cases.
|
|
template <typename T>
|
|
CborMessageStatus ValidateRequiredField(
|
|
const std::string& name, const std::string& component,
|
|
const std::pair<FieldStatus, T>& p,
|
|
std::vector<std::pair<CborMessageStatus, std::string>>& msgs,
|
|
CborMessageStatus error = kCborValidateError) {
|
|
if (p.first != kPresent) {
|
|
msgs.push_back(
|
|
std::make_pair(error, component + ": missing required field " + name));
|
|
return error;
|
|
}
|
|
return kCborValidateOk;
|
|
}
|
|
|
|
// Validates that the given field name is present, and prints warning messages
|
|
// if not.
|
|
template <typename T>
|
|
CborMessageStatus ValidateImportantField(
|
|
const std::string& name, const std::string& component,
|
|
const std::pair<FieldStatus, T>& p,
|
|
std::vector<std::pair<CborMessageStatus, std::string>>& msgs) {
|
|
if (p.first != kPresent) {
|
|
msgs.push_back(std::make_pair(
|
|
kCborValidateWarning, component + ": missing important field " + name));
|
|
return kCborValidateWarning;
|
|
}
|
|
return kCborValidateOk;
|
|
}
|
|
|
|
// Print a field value with a built-in type in component with |name| to
|
|
// stringstream.
|
|
template <typename T>
|
|
void PrintField(std::stringstream& ss, const std::string& name,
|
|
const std::pair<FieldStatus, T>& p) {
|
|
ss << " " << name << ":";
|
|
if (p.first != kPresent) {
|
|
ss << StatusToString(p.first) << ",";
|
|
} else {
|
|
ss << p.second << ",";
|
|
}
|
|
}
|
|
|
|
// Print a field encoded as a CBOR bstr in component with |name| to
|
|
// stringstream.
|
|
template <typename T>
|
|
void PrintBstrField(std::stringstream& ss, const std::string& name,
|
|
const std::pair<FieldStatus, T>& p) {
|
|
ss << " " << name << ":";
|
|
if (p.first != kPresent) {
|
|
ss << StatusToString(p.first) << ",";
|
|
} else {
|
|
ss << wvutil::b2a_hex(p.second) << ",";
|
|
}
|
|
}
|
|
|
|
// Print a field encoded as CBOR structure in component with |name| to
|
|
// stringstream.
|
|
template <typename T>
|
|
void PrintCborField(std::stringstream& ss, const std::string& name,
|
|
const std::pair<FieldStatus, T>& p) {
|
|
ss << " " << name << ":";
|
|
if (p.first != kPresent) {
|
|
ss << StatusToString(p.first) << ",";
|
|
} else {
|
|
ss << p.second.ToString() << ",";
|
|
}
|
|
}
|
|
} // namespace util
|
|
} // namespace wvoec
|
|
#endif // WVOEC_UTIL_PROV4_VALIDATION_HELPER_H_
|