keys

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: 30 Imported by: 0

Documentation

Overview

Package keys parses, generates, describes and compares private and public keys in the formats SSLKnife supports.

Index

Constants

View Source
const (
	FormatPKCS8          = "PKCS#8"
	FormatPKCS8Encrypted = "PKCS#8 (encrypted)"
	FormatPKCS1          = "PKCS#1"
	FormatSEC1           = "SEC1"
	FormatLegacyPEM      = "PEM (legacy encryption)"
	FormatOpenSSH        = "OpenSSH"
	FormatPKIX           = "PKIX SubjectPublicKeyInfo"
	FormatPKCS1Public    = "PKCS#1 public"
	FormatAuthorizedKey  = "OpenSSH authorized_keys"
	FormatRFC4716        = "RFC 4716"
)

Private key container formats.

View Source
const PBKDF2Iterations = 600_000

PBKDF2Iterations is used when writing encrypted PKCS#8 (OWASP 2023 recommendation for PBKDF2-HMAC-SHA256).

Variables

Algorithms lists generatable algorithms in the order shown to users.

View Source
var ErrIncorrectPassword = errors.New("incorrect password or corrupted key")

ErrIncorrectPassword is returned when decryption fails.

View Source
var ErrNoKey = errors.New("no key found")

ErrNoKey means the input did not contain a key of the requested kind.

View Source
var ErrPasswordRequired = errors.New("key is encrypted and requires a password")

ErrPasswordRequired means the key is encrypted and no password was given.

Functions

func DecryptPKCS8

func DecryptPKCS8(der, password []byte) ([]byte, error)

DecryptPKCS8 unwraps an EncryptedPrivateKeyInfo and returns PKCS#8 DER.

func EncryptPKCS8

func EncryptPKCS8(pkcs8, password []byte) ([]byte, error)

EncryptPKCS8 wraps an unencrypted PKCS#8 DER blob.

func Equal

func Equal(a, b crypto.PublicKey) bool

Equal reports whether two public keys are identical.

func Generate

func Generate(a Algorithm) (crypto.Signer, error)

Generate creates a new private key using crypto/rand.

func IsPrivateKeyBlock

func IsPrivateKeyBlock(t string) bool

IsPrivateKeyBlock reports whether a PEM block type holds private key material.

func MarshalPrivateKeyPEM

func MarshalPrivateKeyPEM(priv crypto.PrivateKey, password []byte) ([]byte, error)

MarshalPrivateKeyPEM encodes priv as PKCS#8, encrypted when password is non-empty.

func MarshalPublicKeyPEM

func MarshalPublicKeyPEM(pub crypto.PublicKey) ([]byte, error)

MarshalPublicKeyPEM encodes pub as a PKIX "PUBLIC KEY" block.

func Public

func Public(priv crypto.PrivateKey) (crypto.PublicKey, error)

Public returns the public half of a private key.

func SPKI

func SPKI(pub crypto.PublicKey) ([]byte, error)

SPKI returns the DER SubjectPublicKeyInfo of pub.

func SPKIFingerprint

func SPKIFingerprint(spki []byte) string

SPKIFingerprint returns the lowercase hex SHA-256 of the SPKI.

Types

type Algorithm

type Algorithm string

Algorithm identifies a key type and size that SSLKnife can generate.

const (
	Ed25519   Algorithm = "ed25519"
	ECDSAP256 Algorithm = "ecdsa-p256"
	ECDSAP384 Algorithm = "ecdsa-p384"
	ECDSAP521 Algorithm = "ecdsa-p521"
	RSA2048   Algorithm = "rsa-2048"
	RSA3072   Algorithm = "rsa-3072"
	RSA4096   Algorithm = "rsa-4096"
	MLDSA44   Algorithm = "ml-dsa-44"
	MLDSA65   Algorithm = "ml-dsa-65"
	MLDSA87   Algorithm = "ml-dsa-87"
)

func ParseAlgorithm

func ParseAlgorithm(s string) (Algorithm, error)

ParseAlgorithm accepts common spellings: "ed25519", "ecdsa", "p256", "ec-384", "rsa", "rsa4096", "RSA 3072", "mldsa65", ...

func (Algorithm) Label

func (a Algorithm) Label() string

Label returns a human-readable algorithm name.

type PasswordFunc

type PasswordFunc func() ([]byte, error)

PasswordFunc supplies a password when an encrypted key is encountered. It is only called if needed.

type PrivateKey

type PrivateKey struct {
	Key       crypto.PrivateKey
	Format    string
	Encrypted bool
	Comment   string
}

PrivateKey is a parsed private key and where it came from.

func ParsePrivateKey

func ParsePrivateKey(data []byte, password PasswordFunc) (*PrivateKey, error)

ParsePrivateKey returns the first private key in data.

func ParsePrivateKeys

func ParsePrivateKeys(data []byte, password PasswordFunc) ([]*PrivateKey, error)

ParsePrivateKeys returns every private key in data (PEM or DER).

func (*PrivateKey) Public

func (p *PrivateKey) Public() crypto.PublicKey

Public returns the public key.

type PublicKey

type PublicKey struct {
	Key     crypto.PublicKey
	Format  string
	Comment string
}

PublicKey is a parsed public key.

func ParsePublicKey

func ParsePublicKey(data []byte) (*PublicKey, error)

ParsePublicKey parses a PKIX or PKCS#1 public key (PEM or DER), an OpenSSH authorized_keys line, or an RFC 4716 SSH2 public key.

type PublicKeyInfo

type PublicKeyInfo struct {
	Algorithm   string   `json:"algorithm"`       // RSA, ECDSA, Ed25519, ML-DSA, X25519, DSA
	Bits        int      `json:"bits"`            // modulus or curve size
	Curve       string   `json:"curve,omitempty"` // P-256, ...
	Parameters  string   `json:"parameters,omitempty"`
	Exponent    int      `json:"exponent,omitempty"` // RSA public exponent
	Description string   `json:"description"`        // e.g. "ECDSA P-256"
	SPKISHA256  string   `json:"spki_sha256"`
	Strength    string   `json:"strength"` // modern, acceptable, weak, insecure
	Notes       []string `json:"notes,omitempty"`
}

PublicKeyInfo is the stable, serialisable description of a public key.

func Describe

func Describe(pub crypto.PublicKey) PublicKeyInfo

Describe returns a description of pub. Unsupported key types produce an "unknown" description rather than an error so certificates carrying them can still be inspected.

Jump to

Keyboard shortcuts

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