gen_certs.rs

Overview

This file provides a command-line utility for generating X.509 self-signed certificates and their corresponding private keys. It supports specifying multiple certificate subjects, output directory management, and integration with Ed25519 signing keys used for certificate signing. The utility outputs PEM-encoded certificate and key files, with options to overwrite existing files.

The core functionality revolves around certificate generation using the transport_layer::generate_self_signed_cert method and PEM encoding of DER-formatted keys and certificates. The CLI interface employs the clap crate to parse user inputs. The file also includes a test module validating certificate generation and verification workflows.


Structs and Functions

Struct Cli

A command-line argument parser struct leveraging clap::Parser. It defines the options for the certificate generation command.

Fields

Usage Example

gen_certs -s localhost,127.0.0.1 -n server -o ./certs --force

main() -> Result<(), Box<dyn std::error::Error>>

Entry point of the CLI utility.

Returns an error if parsing, key resolution, or certificate generation fails.


generate_certs

fn generate_certs(
    name: String,
    subjects: Vec<String>,
    ed_sing_keys: &[transport_layer::SigningKey],
    output_dir: Option<PathBuf>,
    force: bool,
) -> Result<(), Box<dyn std::error::Error>>

Generates a self-signed certificate and private key, writes them to disk in PEM format.

Parameters

Behavior

Returns


PEM Encoding Helper Functions

These functions convert DER-encoded certificates and private keys to PEM format.

cert_der_to_pem

fn cert_der_to_pem(cert: &CertificateDer<'_>) -> String

key_der_to_pem

fn key_der_to_pem(key: &PrivateKeyDer<'_>) -> String

der_to_pem

fn der_to_pem(name: &str, der: &[u8]) -> String

Important Implementation Details


Interaction with Other System Components


Test Module

The tests module validates the certificate generation and verification process:


Visual Diagram of File Structure and Workflows

flowchart TD
A["main()"] --> B["Cli::parse()"]
B --> C["resolve_signing_keys()"]
C --> D["generate_certs()"]
D --> E["generate_self_signed_cert()"]
D --> F["cert_der_to_pem()"]
D --> G["key_der_to_pem()"]
F --> H["der_to_pem()"]
G --> H
E --> I[Ed25519 Signing Keys]
D --> J[File System Writes]
subgraph PEM Encoding Helpers
F & G & H
end
subgraph CLI Parsing
B
end
subgraph Certificate Generation
E & I
end

This flowchart illustrates the high-level function calls and data flow during execution:


For further details on cryptographic signing and certificate verification, see X.509 Certificates and Ed25519 Signing. For command-line argument parsing, refer to CLI Argument Parsing with Clap.