cert

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package cert provides utilities for generating X.509 certificates and private keys, including functionality for creating temporary certificate and key files.

This package is primarily designed for **test purposes**, allowing developers to easily generate self-signed certificates and private keys for use in testing scenarios. It abstracts certificate creation and PEM encoding to facilitate mocking and testing of dependent components.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFailedToGeneratePrivateKey = errors.New("failed to generate private key")
	ErrFailedToCreateCertificate  = errors.New("failed to create certificate")
	ErrFailedToMarshalPrivateKey  = errors.New("failed to marshal private key")
	ErrFailedToWriteDataToCert    = errors.New("failed to write data to cert.pem")
	ErrFailedToWriteDataToKey     = errors.New("failed to write data to key.pem")
	ErrFailedToCreateCertTempFile = errors.New("failed to create temp file for Cert")
	ErrFailedToCreateKeyTempFile  = errors.New("failed to create temp file for key")
)

Error definitions for various failure scenarios in certificate and key generation.

Functions

func GenerateTemporaryCertAndKey

func GenerateTemporaryCertAndKey() (string, string, error)

GenerateTemporaryCertAndKey generates a self-signed X.509 certificate and corresponding private key, writing them to temporary files. It returns the paths to the generated certificate and key files.

This function is primarily intended for **test purposes**, where we need to test, if client uses the certificate and key correctly.

Returns:

  • The path to the temporary certificate file.
  • The path to the temporary private key file.
  • An error if any part of the generation or writing process fails.

Types

type CertificateCreator

type CertificateCreator interface {
	CreateCertificate(
		rand io.Reader,
		template, parent *x509.Certificate,
		pub, priv any,
	) ([]byte, error)
	MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error)
}

CertificateCreator defines an interface for creating X.509 certificates and marshaling ECDSA private keys. This abstraction facilitates testing by allowing custom implementations.

By abstracting certificate creation, this interface enables developers to mock certificate generation logic in test cases.

type DefaultCertCreator

type DefaultCertCreator struct{}

DefaultCertCreator is the default implementation of the CertificateCreator interface, using the standard library's x509.CreateCertificate and x509.MarshalECPrivateKey functions.

func (*DefaultCertCreator) CreateCertificate

func (d *DefaultCertCreator) CreateCertificate(
	rand io.Reader,
	template, parent *x509.Certificate,
	pub, priv any,
) ([]byte, error)

CreateCertificate generates an X.509 certificate based on the provided template, parent certificate, public key, and private key.

func (*DefaultCertCreator) MarshalECPrivateKey

func (d *DefaultCertCreator) MarshalECPrivateKey(key *ecdsa.PrivateKey) ([]byte, error)

MarshalECPrivateKey marshals an ECDSA private key into DER format.

type DefaultPEMEncoder

type DefaultPEMEncoder struct{}

DefaultPEMEncoder is the default implementation of the PEMEncoder interface, using the standard library's pem.Encode function.

func (*DefaultPEMEncoder) Encode

func (d *DefaultPEMEncoder) Encode(out io.Writer, block *pem.Block) error

Encode writes a PEM-encoded block to the provided writer.

type PEMEncoder

type PEMEncoder interface {
	Encode(out io.Writer, block *pem.Block) error
}

PEMEncoder defines an interface for encoding data into PEM format. This abstraction allows for easier testing by enabling the mocking of PEM encoding.

This interface is particularly useful in test scenarios where you want to validate how PEM encoding is handled without relying on the actual implementation.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL