This commit is contained in:
Max Schmitt 2024-08-16 16:58:49 +02:00
parent 1afc25aa70
commit 3b116ac1e1

View file

@ -28,7 +28,7 @@ export function calculateSha1(buffer: Buffer | string): string {
} }
// Variable-length quantity encoding aka. base-128 encoding // Variable-length quantity encoding aka. base-128 encoding
function encodeBase128 (value: number): Buffer { function encodeBase128(value: number): Buffer {
const bytes = []; const bytes = [];
do { do {
let byte = value & 0x7f; let byte = value & 0x7f;
@ -81,7 +81,6 @@ class DER {
static encodeDate(date: Date): Buffer { static encodeDate(date: Date): Buffer {
const year = date.getUTCFullYear(); const year = date.getUTCFullYear();
const isGeneralizedTime = year >= 2050; const isGeneralizedTime = year >= 2050;
const parts = [ const parts = [
isGeneralizedTime ? year.toString() : year.toString().slice(-2), isGeneralizedTime ? year.toString() : year.toString().slice(-2),
(date.getUTCMonth() + 1).toString().padStart(2, '0'), (date.getUTCMonth() + 1).toString().padStart(2, '0'),
@ -90,10 +89,8 @@ class DER {
date.getUTCMinutes().toString().padStart(2, '0'), date.getUTCMinutes().toString().padStart(2, '0'),
date.getUTCSeconds().toString().padStart(2, '0') date.getUTCSeconds().toString().padStart(2, '0')
]; ];
const encodedDate = parts.join('') + 'Z'; const encodedDate = parts.join('') + 'Z';
const tag = isGeneralizedTime ? 0x18 : 0x17; // 0x18 for GeneralizedTime, 0x17 for UTCTime const tag = isGeneralizedTime ? 0x18 : 0x17; // 0x18 for GeneralizedTime, 0x17 for UTCTime
return this._encode(tag, Buffer.from(encodedDate)); return this._encode(tag, Buffer.from(encodedDate));
} }
private static _encode(tag: number, data: Buffer): Buffer { private static _encode(tag: number, data: Buffer): Buffer {
@ -133,6 +130,8 @@ export function generateSelfSignedCertificate() {
DER.encodeObjectIdentifier('2.5.4.3'), // commonName X.520 DN component DER.encodeObjectIdentifier('2.5.4.3'), // commonName X.520 DN component
DER.encodePrintableString('localhost') DER.encodePrintableString('localhost')
]), ]),
]),
DER.encodeSet([
DER.encodeSequence([ DER.encodeSequence([
DER.encodeObjectIdentifier('2.5.4.10'), // organizationName X.520 DN component DER.encodeObjectIdentifier('2.5.4.10'), // organizationName X.520 DN component
DER.encodePrintableString('Client Certificate Demo') DER.encodePrintableString('Client Certificate Demo')
@ -149,6 +148,8 @@ export function generateSelfSignedCertificate() {
DER.encodeObjectIdentifier('2.5.4.3'), // commonName X.520 DN component DER.encodeObjectIdentifier('2.5.4.3'), // commonName X.520 DN component
DER.encodePrintableString('localhost') DER.encodePrintableString('localhost')
]), ]),
]),
DER.encodeSet([
DER.encodeSequence([ DER.encodeSequence([
DER.encodeObjectIdentifier('2.5.4.10'), // organizationName X.520 DN component DER.encodeObjectIdentifier('2.5.4.10'), // organizationName X.520 DN component
DER.encodePrintableString('Client Certificate Demo') DER.encodePrintableString('Client Certificate Demo')