C++ Requires Chilkat v11.5.0+
C++
Box: Verify a Captured V2 Webhook Signature
See more Box Examples
Verify raw payload bytes and captured headers using the configured primary and secondary signing keys. The example rejects stale/future timestamps and compares randomized HMACs of the signatures to avoid exposing a signature-prefix timing comparison. It does not host an HTTP listener. The receiver must persist/deduplicate the verified event and acknowledge promptly.
See the Box API guide for setup and the complete workflow.
Chilkat C++ Downloads
#include <CkBinData.h>
#include <CkDateTime.h>
#include <CkCrypt2.h>
#include <CkPrng.h>
#include <CkJsonObject.h>
void ChilkatSample(void)
{
bool success = false;
// Requires Chilkat 11.5.0 or later and a previously unlocked Chilkat bundle.
// See Global Unlock Sample.
// Guide: Box API Guide.
success = false;
// Save the exact HTTP body bytes to this file, without JSON reserialization.
CkBinData rawBody;
success = rawBody.LoadFile("qa_data/box_webhook_body.bin");
if (success != true) {
std::cout << rawBody.lastErrorText() << "\r\n";
return;
}
// Read these fields case-insensitively from the incoming request headers.
// For this offline example, enter the captured header values.
const char *timestamp = "BOX_DELIVERY_TIMESTAMP";
const char *primarySignature = "BOX_SIGNATURE_PRIMARY";
const char *secondarySignature = "BOX_SIGNATURE_SECONDARY";
const char *signatureVersion = "1";
const char *signatureAlgorithm = "HmacSHA256";
// Retrieve configured keys from protected application configuration.
const char *primaryKey = "BOX_PRIMARY_SIGNATURE_KEY";
const char *secondaryKey = "BOX_SECONDARY_SIGNATURE_KEY";
if ((signatureVersion != "1") || (signatureAlgorithm != "HmacSHA256")) {
std::cout << "Unsupported signature version or algorithm." << "\r\n";
return;
}
CkDateTime delivered;
success = delivered.SetFromTimestamp(timestamp);
if (success != true) {
std::cout << delivered.lastErrorText() << "\r\n";
return;
}
CkDateTime now;
success = now.SetFromCurrentSystemTime();
if (success != true) {
std::cout << now.lastErrorText() << "\r\n";
return;
}
int age = atoi(now.GetAsUnixTime(false)) - atoi(delivered.GetAsUnixTime(false));
// Box recommends rejecting deliveries older than ten minutes.
// Allow at most 60 seconds of future clock skew; keep the server clock synchronized.
if ((age > 600) || (age < -60)) {
std::cout << "Delivery timestamp is outside the accepted window." << "\r\n";
return;
}
CkBinData signedBytes;
success = signedBytes.AppendBd(rawBody);
if (success != true) {
std::cout << signedBytes.lastErrorText() << "\r\n";
return;
}
success = signedBytes.AppendString(timestamp,"utf-8");
if (success != true) {
std::cout << signedBytes.lastErrorText() << "\r\n";
return;
}
CkCrypt2 mac;
mac.put_MacAlgorithm("hmac");
mac.put_HashAlgorithm("sha256");
mac.put_EncodingMode("base64");
success = mac.SetMacKeyString(primaryKey);
if (success != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *expectedPrimary = mac.macBdENC(signedBytes);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
success = mac.SetMacKeyString(secondaryKey);
if (success != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *expectedSecondary = mac.macBdENC(signedBytes);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
// Blind both sides with a fresh secret random key before equality comparison.
// Never compare the original HMAC strings using a prefix-short-circuit comparison.
// A host-language receiver can instead use its standard constant-time byte comparison.
CkPrng random;
const char *comparisonKey = random.genRandom(32,"base64");
if (random.get_LastMethodSuccess() != true) {
std::cout << random.lastErrorText() << "\r\n";
return;
}
success = mac.SetMacKeyEncoded(comparisonKey,"base64");
if (success != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *pExpected = mac.macStringENC(expectedPrimary);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *pReceived = mac.macStringENC(primarySignature);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *sExpected = mac.macStringENC(expectedSecondary);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
const char *sReceived = mac.macStringENC(secondarySignature);
if (mac.get_LastMethodSuccess() != true) {
std::cout << mac.lastErrorText() << "\r\n";
return;
}
bool validPrimary = (primaryKey != "") && (primarySignature != "") && (pExpected == pReceived);
bool validSecondary = (secondaryKey != "") && (secondarySignature != "") && (sExpected == sReceived);
if ((validPrimary == false) && (validSecondary == false)) {
std::cout << "Signature verification failed." << "\r\n";
return;
}
// Parse JSON only after signature verification.
CkJsonObject webhookEvent;
success = webhookEvent.Load(rawBody.getString("utf-8"));
if (success != true) {
std::cout << webhookEvent.lastErrorText() << "\r\n";
return;
}
std::cout << "Verified event ID: " << webhookEvent.stringOf("id") << "\r\n";
std::cout << "Trigger: " << webhookEvent.stringOf("trigger") << "\r\n";
// Deduplicate using event.id, not BOX-DELIVERY-ID, which changes on retries.
// Verify the target belongs to your configured webhook before dispatching work.
}