fix: HMAC format — match server POST\npath\ntimestamp\nsha256(body) scheme
- hmac_sign now takes method+path instead of device_id; builds message as method\npath\ntimestamp\nhex(sha256(body)) per server verify_device_hmac - reporter: header renamed X-HMAC-Signature → X-Signature; passes "POST"+path - test vector regenerated against new message format; timestamp-diff test updated - .size() → .length() throughout (Arduino String has no size()) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -35,8 +35,11 @@ static bool sha256(const uint8_t* data, size_t len, uint8_t out[32]) {
|
||||
return true;
|
||||
}
|
||||
|
||||
HString hmac_sign(const HString& secret_hex, const HString& device_id,
|
||||
uint32_t timestamp, const HString& body) {
|
||||
HString hmac_sign(const HString& secret_hex,
|
||||
const HString& method,
|
||||
const HString& path,
|
||||
uint32_t timestamp,
|
||||
const HString& body) {
|
||||
// 1. SHA256(body)
|
||||
uint8_t body_hash[32] = {};
|
||||
if (!sha256((const uint8_t*)body.c_str(), body.length(), body_hash)) {
|
||||
@@ -44,10 +47,10 @@ HString hmac_sign(const HString& secret_hex, const HString& device_id,
|
||||
}
|
||||
HString body_hash_hex = bytes_to_hex(body_hash, 32);
|
||||
|
||||
// 2. Build message
|
||||
// 2. Build message: method + "\n" + path + "\n" + timestamp + "\n" + sha256(body)
|
||||
char ts_buf[12];
|
||||
snprintf(ts_buf, sizeof(ts_buf), "%u", (unsigned)timestamp);
|
||||
HString message = device_id + ":" + ts_buf + ":" + body_hash_hex;
|
||||
HString message = method + "\n" + path + "\n" + ts_buf + "\n" + body_hash_hex;
|
||||
|
||||
// 3. Decode secret from hex
|
||||
size_t secret_len = secret_hex.length() / 2;
|
||||
@@ -59,8 +62,8 @@ HString hmac_sign(const HString& secret_hex, const HString& device_id,
|
||||
mbedtls_md_context_t ctx;
|
||||
const mbedtls_md_info_t* info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA256);
|
||||
mbedtls_md_init(&ctx);
|
||||
int ret2 = mbedtls_md_setup(&ctx, info, 1);
|
||||
if (ret2 != 0) { mbedtls_md_free(&ctx); return HString{}; }
|
||||
int ret = mbedtls_md_setup(&ctx, info, 1);
|
||||
if (ret != 0) { mbedtls_md_free(&ctx); return HString{}; }
|
||||
mbedtls_md_hmac_starts(&ctx, secret, secret_len);
|
||||
mbedtls_md_hmac_update(&ctx, (const uint8_t*)message.c_str(), message.length());
|
||||
mbedtls_md_hmac_finish(&ctx, hmac_result);
|
||||
|
||||
@@ -11,6 +11,10 @@ using HString = String;
|
||||
#endif
|
||||
|
||||
// Returns lowercase hex-encoded HMAC-SHA256 signature.
|
||||
// Message signed: device_id + ":" + timestamp_str + ":" + hex(sha256(body))
|
||||
HString hmac_sign(const HString& secret_hex, const HString& device_id,
|
||||
uint32_t timestamp, const HString& body);
|
||||
// Message signed: method + "\n" + path + "\n" + timestamp_str + "\n" + hex(sha256(body))
|
||||
// Matches server verify_device_hmac format: POST\n{path}\n{timestamp}\n{sha256(body)}
|
||||
HString hmac_sign(const HString& secret_hex,
|
||||
const HString& method,
|
||||
const HString& path,
|
||||
uint32_t timestamp,
|
||||
const HString& body);
|
||||
|
||||
Reference in New Issue
Block a user