converter

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package converter detects certificate/key container formats, decodes them into a common Bundle of objects, and encodes bundles into target formats. When a target cannot represent the bundle, encoding fails with an explanation instead of dropping data.

Index

Constants

View Source
const MinJKSPassword = 6

MinJKSPassword is Java's minimum keystore password length.

Variables

View Source
var ErrImpossible = errors.New("conversion not possible")

ErrImpossible marks conversions the target format cannot represent.

View Source
var ErrUnsupported = errors.New("unsupported format")

ErrUnsupported marks formats that are recognised but cannot be read.

View Source
var Targets = []struct {
	Name, Description string
}{
	{"pem", "PEM text: certificates, keys (PKCS#8), CSRs, public keys"},
	{"der", "binary DER of exactly one object"},
	{"pkcs7", "PKCS#7 / .p7b certificate bundle (certificates only)"},
	{"pkcs8", "PKCS#8 private key (encrypted with --out-password)"},
	{"pkcs1", "PKCS#1 RSA private or public key"},
	{"sec1", "SEC1 EC private key"},
	{"spki", "PKIX SubjectPublicKeyInfo public key"},
	{"pkcs12", "PKCS#12 / PFX keystore or truststore"},
	{"jks", "Java KeyStore"},
	{"openssh", "OpenSSH private key"},
	{"ssh", "OpenSSH authorized_keys public key line(s)"},
	{"rfc4716", "RFC 4716 SSH2 public key"},
}

Targets lists output formats with a short description.

Functions

This section is empty.

Types

type Bundle

type Bundle struct {
	Format    Format
	Encrypted bool // container or key was password protected
	Objects   []Object
	Notes     []string
}

Bundle is the decoded content of one or more inputs.

func Decode

func Decode(data []byte, o DecodeOptions) (*Bundle, error)

Decode detects the format of data and decodes its objects.

func (*Bundle) Certificates

func (b *Bundle) Certificates() []*x509.Certificate

Certificates returns all certificates.

func (*Bundle) Count

func (b *Bundle) Count(k Kind) int

Count returns the number of objects of kind k.

func (*Bundle) Filter

func (b *Bundle) Filter(kinds ...Kind) *Bundle

Filter keeps only objects of the given kinds.

func (*Bundle) Merge

func (b *Bundle) Merge(o *Bundle)

Merge appends another bundle's objects, skipping duplicate certificates.

func (*Bundle) Summarize

func (b *Bundle) Summarize() Contents

Summarize classifies the bundle's objects.

type Contents

type Contents struct {
	PrivateKeys   int `json:"private_keys"`
	PublicKeys    int `json:"public_keys"`
	CSRs          int `json:"certificate_requests"`
	Leaf          int `json:"leaf_certificates"`
	Intermediates int `json:"intermediate_certificates"`
	Roots         int `json:"root_certificates"`
}

Contents counts what a bundle holds, classifying certificates by role.

type DecodeOptions

type DecodeOptions struct {
	Password keys.PasswordFunc // store / file password
	// KeyPassword is used for JKS private key entries whose password differs
	// from the store password; nil reuses the store password.
	KeyPassword keys.PasswordFunc
}

DecodeOptions supply passwords on demand.

type EncodeOptions

type EncodeOptions struct {
	Password []byte // encrypts keys / keystores; required for pkcs12 and jks
	DER      bool   // binary output where both encodings exist (pkcs1/pkcs8/sec1/spki/pkcs7/csr)
	Alias    string // alias for the key entry in pkcs12/jks
}

EncodeOptions control output encoding.

type Format

type Format string

Format identifies a container format.

const (
	FormatPEM       Format = "pem"
	FormatDER       Format = "der"
	FormatPKCS7     Format = "pkcs7"
	FormatPKCS8     Format = "pkcs8"
	FormatPKCS1     Format = "pkcs1"
	FormatSEC1      Format = "sec1"
	FormatSPKI      Format = "spki"
	FormatCSR       Format = "csr"
	FormatPKCS12    Format = "pkcs12"
	FormatJKS       Format = "jks"
	FormatJCEKS     Format = "jceks"
	FormatOpenSSH   Format = "openssh"
	FormatSSHPublic Format = "ssh"
	FormatRFC4716   Format = "rfc4716"
	FormatUnknown   Format = "unknown"
)

func Detect

func Detect(data []byte) Format

Detect identifies the format of data from its content, not its name.

func ParseTarget

func ParseTarget(s string) (Format, error)

ParseTarget normalises a --to value.

func (Format) Description

func (f Format) Description() string

Description is a human-readable name for a detected format.

type Kind

type Kind string

Kind classifies bundle objects.

const (
	KindCertificate Kind = "certificate"
	KindPrivateKey  Kind = "private_key"
	KindPublicKey   Kind = "public_key"
	KindCSR         Kind = "csr"
)

type Object

type Object struct {
	Kind    Kind
	Alias   string
	Cert    *x509.Certificate
	Key     crypto.PrivateKey
	Public  crypto.PublicKey
	CSR     *x509.CertificateRequest
	Comment string
	// Trusted marks JKS/PKCS#12 trusted-certificate entries.
	Trusted bool
	Created time.Time
}

Object is one item decoded from a container.

func (Object) PublicKey

func (o Object) PublicKey() crypto.PublicKey

PublicKey returns the public key carried or implied by the object.

type Output

type Output struct {
	Data      []byte
	Secret    bool // contains private key material
	Extension string
	Notes     []string
}

Output is an encoded result.

func Encode

func Encode(b *Bundle, to Format, o EncodeOptions) (*Output, error)

Encode renders b in format to.

Jump to

Keyboard shortcuts

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