233 lines
6.8 KiB
C++
233 lines
6.8 KiB
C++
#include "ota/ota_response_parser.h"
|
|
|
|
#include <algorithm>
|
|
#include <cctype>
|
|
#include <climits>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
|
|
#include <cJSON.h>
|
|
|
|
namespace borla {
|
|
namespace {
|
|
|
|
const char* StringField(const cJSON* root, const char* name)
|
|
{
|
|
const cJSON* value =
|
|
cJSON_GetObjectItemCaseSensitive(root, name);
|
|
return cJSON_IsString(value) ? value->valuestring : nullptr;
|
|
}
|
|
|
|
bool CopyBounded(const char* source, char* output, size_t capacity)
|
|
{
|
|
if (source == nullptr || output == nullptr || capacity == 0) {
|
|
return false;
|
|
}
|
|
const size_t size = std::strlen(source);
|
|
if (size == 0 || size >= capacity) {
|
|
return false;
|
|
}
|
|
std::memcpy(output, source, size);
|
|
output[size] = '\0';
|
|
return true;
|
|
}
|
|
|
|
bool IsSafeUrl(const char* url, bool allow_insecure_http)
|
|
{
|
|
return url != nullptr &&
|
|
(std::strncmp(url, "https://", 8) == 0 ||
|
|
(allow_insecure_http &&
|
|
std::strncmp(url, "http://", 7) == 0));
|
|
}
|
|
|
|
int HexNibble(char value)
|
|
{
|
|
if (value >= '0' && value <= '9') {
|
|
return value - '0';
|
|
}
|
|
value = static_cast<char>(
|
|
std::tolower(static_cast<unsigned char>(value)));
|
|
return value >= 'a' && value <= 'f'
|
|
? value - 'a' + 10
|
|
: -1;
|
|
}
|
|
|
|
int NextVersionPart(const char** cursor)
|
|
{
|
|
const char* value = *cursor;
|
|
while (*value == 'v' || *value == 'V' ||
|
|
*value == '.' || *value == '-' ||
|
|
std::isspace(static_cast<unsigned char>(*value))) {
|
|
value++;
|
|
}
|
|
int part = 0;
|
|
while (std::isdigit(static_cast<unsigned char>(*value))) {
|
|
if (part <= (INT_MAX - 9) / 10) {
|
|
part = part * 10 + (*value - '0');
|
|
}
|
|
value++;
|
|
}
|
|
while (*value != '\0' && *value != '.') {
|
|
value++;
|
|
}
|
|
*cursor = value;
|
|
return part;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
OtaParseStatus OtaResponseParser::Parse(
|
|
const char* json,
|
|
size_t size,
|
|
const char* current_version,
|
|
bool allow_insecure_http,
|
|
OtaManifest* manifest,
|
|
OtaEndpointConfig* endpoint) const
|
|
{
|
|
if (json == nullptr || size == 0 ||
|
|
current_version == nullptr || manifest == nullptr) {
|
|
return OtaParseStatus::kMalformed;
|
|
}
|
|
*manifest = {};
|
|
if (endpoint != nullptr) {
|
|
*endpoint = {};
|
|
}
|
|
cJSON* root = cJSON_ParseWithLength(json, size);
|
|
if (!cJSON_IsObject(root)) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kMalformed;
|
|
}
|
|
|
|
// The websocket block is parsed first and kept across every firmware-block
|
|
// outcome below: the server may hand out a new realtime endpoint on a
|
|
// response that carries no image at all, and losing it would strand the
|
|
// device on the previous host.
|
|
const cJSON* websocket =
|
|
cJSON_GetObjectItemCaseSensitive(root, "websocket");
|
|
if (endpoint != nullptr && cJSON_IsObject(websocket)) {
|
|
const char* endpoint_url = StringField(websocket, "url");
|
|
const char* endpoint_token = StringField(websocket, "token");
|
|
if (endpoint_url != nullptr) {
|
|
endpoint->has_url = CopyBounded(
|
|
endpoint_url,
|
|
endpoint->url.data(),
|
|
endpoint->url.size());
|
|
}
|
|
if (endpoint_token != nullptr) {
|
|
endpoint->has_token = CopyBounded(
|
|
endpoint_token,
|
|
endpoint->token.data(),
|
|
endpoint->token.size());
|
|
}
|
|
}
|
|
|
|
const cJSON* firmware =
|
|
cJSON_GetObjectItemCaseSensitive(root, "firmware");
|
|
if (cJSON_IsNull(firmware) || firmware == nullptr) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kOk;
|
|
}
|
|
if (!cJSON_IsObject(firmware)) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kInvalidManifest;
|
|
}
|
|
|
|
const char* version = StringField(firmware, "version");
|
|
const char* url = StringField(firmware, "url");
|
|
const char* sha256 = StringField(firmware, "sha256");
|
|
const cJSON* size_value =
|
|
cJSON_GetObjectItemCaseSensitive(firmware, "size");
|
|
uint8_t digest[32]{};
|
|
if (!CopyBounded(
|
|
version, manifest->version.data(),
|
|
manifest->version.size()) ||
|
|
!CopyBounded(
|
|
url, manifest->url.data(), manifest->url.size()) ||
|
|
!CopyBounded(
|
|
sha256, manifest->sha256.data(),
|
|
manifest->sha256.size()) ||
|
|
!DecodeSha256(sha256, digest) ||
|
|
!cJSON_IsNumber(size_value) ||
|
|
!std::isfinite(size_value->valuedouble) ||
|
|
std::floor(size_value->valuedouble) !=
|
|
size_value->valuedouble ||
|
|
size_value->valuedouble <= 0 ||
|
|
size_value->valuedouble > UINT32_MAX) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kInvalidManifest;
|
|
}
|
|
if (!IsSafeUrl(url, allow_insecure_http)) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kUnsafeUrl;
|
|
}
|
|
if (CompareVersions(version, current_version) <= 0) {
|
|
*manifest = {};
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kOk;
|
|
}
|
|
|
|
manifest->size =
|
|
static_cast<uint32_t>(size_value->valuedouble);
|
|
const cJSON* automatic =
|
|
cJSON_GetObjectItemCaseSensitive(root, "auto_update");
|
|
const cJSON* forced =
|
|
cJSON_GetObjectItemCaseSensitive(root, "force_upgrade");
|
|
if ((automatic != nullptr && !cJSON_IsBool(automatic)) ||
|
|
(forced != nullptr && !cJSON_IsBool(forced))) {
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kInvalidManifest;
|
|
}
|
|
if (cJSON_IsTrue(automatic)) {
|
|
manifest->policy = OtaUpgradePolicy::kAutomatic;
|
|
} else if (cJSON_IsTrue(forced)) {
|
|
manifest->policy = OtaUpgradePolicy::kForced;
|
|
} else {
|
|
manifest->policy = OtaUpgradePolicy::kOptional;
|
|
}
|
|
cJSON_Delete(root);
|
|
return OtaParseStatus::kOk;
|
|
}
|
|
|
|
int OtaResponseParser::CompareVersions(
|
|
const char* left,
|
|
const char* right)
|
|
{
|
|
if (left == nullptr || right == nullptr) {
|
|
return 0;
|
|
}
|
|
const char* left_cursor = left;
|
|
const char* right_cursor = right;
|
|
for (size_t i = 0; i < 8; ++i) {
|
|
const int left_part = NextVersionPart(&left_cursor);
|
|
const int right_part = NextVersionPart(&right_cursor);
|
|
if (left_part != right_part) {
|
|
return left_part < right_part ? -1 : 1;
|
|
}
|
|
if (*left_cursor == '\0' && *right_cursor == '\0') {
|
|
return 0;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
bool OtaResponseParser::DecodeSha256(
|
|
const char* hex,
|
|
uint8_t output[32])
|
|
{
|
|
if (hex == nullptr || output == nullptr ||
|
|
std::strlen(hex) != 64) {
|
|
return false;
|
|
}
|
|
for (size_t i = 0; i < 32; ++i) {
|
|
const int upper = HexNibble(hex[i * 2]);
|
|
const int lower = HexNibble(hex[i * 2 + 1]);
|
|
if (upper < 0 || lower < 0) {
|
|
return false;
|
|
}
|
|
output[i] = static_cast<uint8_t>((upper << 4) | lower);
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace borla
|