Enrolling devices and attesting to policies
A device enrolls with Root Herald once. Each attestation after that is checked against the policies on your API key.
1Add the SDK
Start on your server: install the SDK and create a Root Herald client with your secret API key. Keep the key on your server; the client never needs it.
npm install @rootherald/nodeIt takes a secret key, rh_sk_…. Create one
import { RootHeraldClient } from "@rootherald/node";
const rh = new RootHeraldClient({
secretKey: process.env.RH_SECRET_KEY!,
});On the client, link the Root Herald static library, then use to open a handle. You pass that handle to every call that takes it.
#include <rootherald.h>
#pragma comment(lib, "RootHerald.lib")
#pragma comment(lib, "tbs.lib")
#pragma comment(lib, "bcrypt.lib")
#pragma comment(lib, "crypt32.lib")
#pragma comment(lib, "advapi32.lib")
/* ...your includes */
static RH_HANDLE rh;
int start(void) {
return RootHeraldOpen(&rh) == RH_OK;
}To run a sample server and client first, open the developer sandbox.
2Enroll the device
Before a device can attest, each installation of your client enrolls with Root Herald. Use to create this installation's attestation key in the TPM. It writes the enrollment request into one buffer and the key, wrapped by the TPM, into another: the attestation key blob, then post it to your server so it can relay it to Root Herald. Root Herald replies with a secret that only this TPM can recover. When your server hands it back, use with the blob to recover the secret, and post it back to your server. Once Root Herald has it, the installation is enrolled. Keep the blob in your client's own storage: every attestation loads it, and the SDK keeps nothing. RootHeraldEnrollComplete needs an elevated process; nothing else does.
#include <string.h>
static char buf[64 * 1024];
static uint8_t ak[1024];
static size_t ak_len;
int enroll(void) {
size_t n;
RH_STATUS st = RootHeraldEnrollBegin(rh, buf, sizeof buf, &n,
ak, sizeof ak, &ak_len);
if (st != RH_OK) {
return 0;
}
const char* reply = post("/enroll", buf);
if (!reply) {
return 0;
}
st = RootHeraldEnrollComplete(rh, reply, strlen(reply),
buf, sizeof buf, &n,
ak, ak_len);
if (st != RH_OK) {
return 0;
}
if (post("/enroll/activate", buf) == NULL) {
return 0;
}
return save_ak_blob(ak, ak_len);
}Your server's job in enrollment is to relay. Pass the enrollment request to , and send what it returns back to the client for the device to answer. When the answer arrives, pass it to ; Root Herald checks it and returns the device's ID. Keep that ID with whatever the device belongs to, since every attestation from this device carries it.
app.post("/enroll", async (req, res) => {
const { challenge } = await rh.relayEnroll(req.body);
res.json(challenge);
});
app.post("/enroll/activate", async (req, res) => {
const { deviceId } = await rh.relayActivate(req.body);
await saveDevice(accountOf(req), deviceId);
res.sendStatus(204);
});3Challenge the device
Every attestation starts with a fresh challenge. Use to get one, and send the challenge and its nonce to the client. The nonce comes back with the answer, which is how you tie the answer to this challenge. If you ask for posture, your key needs a posture policy.
app.post("/challenge", async (_req, res) => {
const { nonce, challenge } = await rh.issueChallenge({
ask: ["identity", "posture"],
});
res.json({ nonce, challenge });
});When the challenge arrives, load the attestation key blob and use to sign it with the TPM, over its boot measurements. The result, the evidence, lands in a buffer. With no blob stored, enroll first; if the blob no longer loads, you get RH_ERR_KEY_UNLOADABLE: discard it and enroll again.
int attest(void) {
size_t n;
if (!load_ak_blob(ak, sizeof ak, &ak_len) && !enroll()) {
return 0;
}
const char* c = post("/challenge", "{}");
if (!c) {
return 0;
}
RH_STATUS st = RootHeraldAttest(rh, json_str(c, "challenge"),
ak, ak_len,
buf, sizeof buf, &n);
if (st != RH_OK) {
return 0;
}
}4Act on the verdict
Post the evidence and its nonce to your server. Don't expect the verdict back: it stays on your server, and the client only hears what your server decides to tell it.
return post_evidence("/attest", json_str(c, "nonce"), buf) != NULL;Pass the evidence and nonce to . Root Herald checks them against the policies on your key and returns the device's ID and its verdict. Compare that ID with the one you kept at enrollment, so you know it's the same device. Then decide what pass, warn and fail unlock for you.
app.post("/attest", async (req, res) => {
const { nonce, evidence } = req.body;
const { device } = await rh.verify(evidence, { nonce });
const deviceId = await deviceOf(accountOf(req));
if (device.ueid !== deviceId) {
return res.sendStatus(403);
}
switch (device.verdict) {
case "pass":
allow(req);
return res.sendStatus(204);
case "warn":
allow(req, { limited: true });
return res.sendStatus(204);
default:
return res.sendStatus(403);
}
});