Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 10 additions & 5 deletions src/mkcert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type GenerateOptions = {
subject: pki.CertificateField[];
issuer: pki.CertificateField[];
extensions: Record<string, unknown>[];
validity: number;
validity: number | Date;
signWith?: string;
};

Expand All @@ -31,8 +31,13 @@ async function generateCert(options: GenerateOptions): Promise<Certificate> {
cert.setIssuer(issuer);
cert.setExtensions(extensions);
cert.validity.notBefore = new Date();
cert.validity.notAfter = new Date();
cert.validity.notAfter.setDate(cert.validity.notAfter.getDate() + validity);
// Date object, use it directly
if (validity instanceof Date) {
cert.validity.notAfter = validity;
} else {
cert.validity.notAfter = new Date();
cert.validity.notAfter.setDate(cert.validity.notAfter.getDate() + validity);
}

// sign the certificate with it's own
// private key if no separate signing key is provided
Expand All @@ -50,7 +55,7 @@ export type CertificateAuthorityOptions = {
countryCode: string;
state: string;
locality: string;
validity: number;
validity: number | Date;
};

export async function createCA(options: CertificateAuthorityOptions): Promise<Certificate> {
Expand Down Expand Up @@ -79,7 +84,7 @@ export async function createCA(options: CertificateAuthorityOptions): Promise<Ce

export type CertificateOptions = {
domains: string[];
validity: number;
validity: number | Date;
organization?: string;
email?: string;
ca: Certificate;
Expand Down
26 changes: 26 additions & 0 deletions tests/mkcert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,29 @@ test("Verify Certificate Chain", async () => {
pki.verifyCertificateChain(caStore, [serverCert]);
}).not.toThrow();
});

test("Create Certificate with Date", async () => {
const tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
const seconds_since_epoch = Math.floor(tomorrow.getTime()/1000);
const ca_bundle = await mkcert.createCA({
organization: "Test CA",
countryCode: "NP",
state: "Bagmati",
locality: "Kathmandu",
validity: tomorrow
});
const ca_cert = pki.certificateFromPem(ca_bundle.cert);
expect(ca_cert.validity.notAfter.getTime()).toBe(seconds_since_epoch*1000);

const tls_bundle = await mkcert.createCert({
ca: { key: ca_bundle.key, cert: ca_bundle.cert },
domains: ["127.0.0.1", "localhost"],
email: "[email protected]",
organization: "Test Cert",
validity: tomorrow
});

const cert = pki.certificateFromPem(tls_bundle.cert);
expect(cert.validity.notAfter.getTime()).toBe(seconds_since_epoch*1000);
});