Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add batch stamping logic #55

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions auth/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -1045,6 +1045,16 @@ <h2>Message log</h2>
TKHQ.sendMessageUp("ERROR", e.toString());
}
}
if (event.data && event.data["type"] == "BATCH_STAMP_REQUEST") {
TKHQ.logMessage(
`⬇️ Received message ${event.data["type"]}: ${event.data["value"]}`
);
try {
await onBatchStampRequest(event.data["value"]);
} catch (e) {
TKHQ.sendMessageUp("ERROR", e.toString());
}
}
if (event.data && event.data["type"] == "RESET_EMBEDDED_KEY") {
TKHQ.logMessage(`⬇️ Received message ${event.data["type"]}`);
try {
Expand Down Expand Up @@ -1198,6 +1208,57 @@ <h2>Message log</h2>
);
TKHQ.sendMessageUp("STAMP", stampHeaderValue);
};
/**
* Function triggered when BATCH_STAMP_REQUEST event is received.
* @param {string} payloads to sign
*/
var onBatchStampRequest = async function (payloads) {
if (CREDENTIAL_BYTES === null) {
throw new Error(
"cannot sign payload without credential. Credential bytes are null"
);
}
var key = await TKHQ.importCredential(CREDENTIAL_BYTES);

// This is a bit of a pain, but we need to go through this:
// - Key needs to be exported to JWK first
// - Then imported without the private "d" component, and exported to get the public key
// ^^ (that's what `p256JWKPrivateToPublic` does)
// - Finally, compress the public key.
var jwkKey = await crypto.subtle.exportKey("jwk", key);
var publicKey = await TKHQ.p256JWKPrivateToPublic(jwkKey);
var compressedPublicKey = TKHQ.compressRawPublicKey(publicKey);

var stamps = payloads.map(async (p) => {
var signatureIeee1363 = await window.crypto.subtle.sign(
{
name: "ECDSA",
hash: { name: "SHA-256" },
},
key,
new TextEncoder().encode(p)
);

var derSignature = TKHQ.convertEcdsaIeee1363ToDer(
new Uint8Array(signatureIeee1363)
);
var derSignatureHexString = TKHQ.uint8arrayToHexString(derSignature);

var stamp = {
publicKey: TKHQ.uint8arrayToHexString(compressedPublicKey),
scheme: "SIGNATURE_SCHEME_TK_API_P256",
signature: derSignatureHexString,
};

var stampHeaderValue = TKHQ.stringToBase64urlString(
JSON.stringify(stamp)
);

return stampHeaderValue;
});

TKHQ.sendMessageUp("BATCH_STAMP", stamps);
};

/**
* Decrypt the ciphertext (ArrayBuffer) given an encapsulation key (ArrayBuffer)
Expand Down
Loading