@keeex/tsr
Version:
Manipulate data related to TSR with pki.js
59 lines • 1.95 kB
JavaScript
/**
* @license
* @preserve
*
* KeeeX SAS Public code
* https://keeex.me
* Copyright 2013-2024 KeeeX All Rights Reserved.
*
* These computer program listings and specifications, herein,
* are and remain the property of KeeeX SAS. The intellectual
* and technical concepts herein are proprietary to KeeeX SAS
* and may be covered by EU and foreign patents,
* patents in process, trade secrets and copyright law.
*
* These listings are published as a way to provide third party
* with the ability to process KeeeX data.
* As such, support for public inquiries is limited.
* They are provided "as-is", without warrany of any kind.
*
* They shall not be reproduced or copied or used in whole or
* in part as the basis for manufacture or sale of items unless
* prior written permission is obtained from KeeeX SAS.
*
* For a license agreement, please contact:
* <mailto: contact@keeex.net>
*
*/
// #region Imports
import { randomBytes } from "@keeex/crypto/random.js";
import { Integer } from "asn1js";
import * as pkijsLib from "pkijs";
import { NONCE_LENGTH } from "./consts.js";
import * as pkijsUtils from "./pkijs.js";
// #endregion
// #region API
/**
* Generate a timestamp request buffer, suitable to use with OpenSSL or send to a third-party.
*
* @param nonce - If not provided, a random nonce is used (recommended)
*/
export const generateTimestampRequest = async (messageInput, nonce) => {
await pkijsUtils.getCryptoImpl();
const messageImprint = await pkijsUtils.messageImprintFromInput(messageInput);
/**
* This part is inspired from
* https://pkijs.org/docs/examples/timestamping/creating-a-timestamp-request
*/
const req = new pkijsLib.TimeStampReq({
certReq: true,
messageImprint,
nonce: new Integer({
valueHex: nonce ?? (await randomBytes(NONCE_LENGTH))
}),
version: 1
});
const reqRaw = req.toSchema().toBER();
return new Uint8Array(reqRaw);
};
// #endregion