Skip to content

Generates a Certificate Signing Request (CSR) in DER format, encoded as base64url string, following the PKCS#10 specification.

License

Notifications You must be signed in to change notification settings

FirstTimeEZ/csr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSR (Certificate Signing Request) Generator

A robust and secure JavaScript library for generating Certificate Signing Requests (CSRs) using existing key pairs.

This library follows the PKCS#10 specification and implements CSR generation with ECDSA keys and SHA-256 signing.

Features

  • PKCS#10 compliant implementation
  • DNS Subject Alternative Names (SAN)
  • Generate CSRs using existing ECDSA key pairs
  • DER encoding with base64url output format
  • Secure cryptographic operations using native Web Crypto API

Usage

Here's a basic example of generating a CSR that is compatible with FlattenedSign

Jose is required for ES256 and to keep the same flow.

import { generateCSRWithExistingKeys } from './csr.js';
import * as jose from 'index.js';

const { publicKey, privateKey } = await jose.generateKeyPair("ES256", { extractable: true });

async function generateCSR() {
    try {
        const commonName = 'www.ssl.boats';
        const dnsNames = ["www.ssl.boats", "ssl.boats"];

        const csr = await generateCSRWithExistingKeys(
            commonName, 
            keyPair.publicKey,
            keyPair.privateKey, 
            dnsNames, 
            jose);
                
        console.log('Generated CSR:', csr);
    } catch (error) {
        console.error('Failed to generate CSR:', error);
    }
}

API Reference

generateCSRWithExistingKeys(commonName, publicKey, privateKey, dnsNames, jose)

Generates a Certificate Signing Request using existing public and private key pairs.

Parameters

  • commonName (string): The common name (CN) for the CSR subject field
  • publicKey (CryptoKey): ECDSA public key as a CryptoKey object
  • privateKey (CryptoKey): ECDSA private key corresponding to the public key
  • dnsNames (array): Array of DNS names to use for Subject Alternative Names (SAN)
  • jose (import): Jose is required for key export operations

Returns

  • Promise<string>: Base64URL-encoded DER format CSR

Throws

  • Error: If CSR generation fails, with detailed error message

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

For major changes, please open an issue first to discuss what you would like to change.

Acknowledgments


Made with ❤️ by FirstTimeEZ