pemutil

package
v0.23.1 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: Apache-2.0 Imports: 33 Imported by: 60

Documentation

Overview

Package pemutil implements utilities to parse keys and certificates. It also includes a method to serialize keys, X.509 certificates and certificate requests to PEM.

Index

Constants

View Source
const PBKDF2Iterations = 100000

PBKDF2Iterations is the default number of iterations for PBKDF2, 100k iterations. Nist recommends at least 10k, 1Passsword uses 100k.

View Source
const PBKDF2SaltSize = 16

PBKDF2SaltSize is the default size of the salt for PBKDF2, 128-bit salt.

Variables

View Source
var DefaultEncCipher = x509.PEMCipherAES256

DefaultEncCipher is the default algorithm used when encrypting sensitive data in the PEM format.

Functions

func DecryptPEMBlock

func DecryptPEMBlock(block *pem.Block, password []byte) ([]byte, error)

DecryptPEMBlock takes a password encrypted PEM block and the password used to encrypt it and returns a slice of decrypted DER encoded bytes.

If the PEM blocks has the Proc-Type header set to "4,ENCRYPTED" it uses x509.DecryptPEMBlock to decrypt the block. If not it tries to decrypt the block using AES-128-CBC, AES-192-CBC, AES-256-CBC, DES, or 3DES using the key derived using PBKDF2 over the given password.

func DecryptPKCS8PrivateKey

func DecryptPKCS8PrivateKey(data, password []byte) ([]byte, error)

DecryptPKCS8PrivateKey takes a password encrypted private key using the PKCS#8 encoding and returns the decrypted data in PKCS#8 form. If an incorrect password is detected an x509.IncorrectPasswordError is returned. Because of deficiencies in the format, it's not always possible to detect an incorrect password. In these cases no error will be returned but the decrypted DER bytes will be random noise.

It supports AES-128-CBC, AES-192-CBC, AES-256-CBC, DES, or 3DES encrypted data using the key derived with PBKDF2 over the given password.

func EncryptPKCS8PrivateKey

func EncryptPKCS8PrivateKey(rand io.Reader, data, password []byte, alg x509.PEMCipher) (*pem.Block, error)

EncryptPKCS8PrivateKey returns a PEM block holding the given PKCS#8 encroded private key, encrypted with the specified algorithm and a PBKDF2 derived key from the given password.

func Parse

func Parse(b []byte, opts ...Options) (interface{}, error)

Parse returns the key or certificate PEM-encoded in the given bytes.

func ParseCertificate added in v0.9.1

func ParseCertificate(pemData []byte) (*x509.Certificate, error)

ParseCertificate extracts the first certificate from the given pem.

func ParseCertificateBundle added in v0.9.1

func ParseCertificateBundle(pemData []byte) ([]*x509.Certificate, error)

ParseCertificateBundle extracts all the certificates in the given data.

func ParseCertificateRequest added in v0.9.1

func ParseCertificateRequest(pemData []byte) (*x509.CertificateRequest, error)

ParseCertificateRequest extracts the first certificate from the given pem.

func ParseCosignPrivateKey added in v0.10.0

func ParseCosignPrivateKey(data, password []byte) (crypto.PrivateKey, error)

ParseCosignPrivateKey returns the private key encoded using cosign envelope. If an incorrect password is detected an x509.IncorrectPasswordError is returned.

Cosign keys are encrypted under a password using scrypt as a KDF and nacl/secretbox for encryption.

func ParseDER

func ParseDER(b []byte) (interface{}, error)

ParseDER parses the given DER-encoded bytes and results the public or private key encoded.

func ParseKey

func ParseKey(b []byte, opts ...Options) (interface{}, error)

ParseKey returns the key or the public key of a certificate or certificate signing request in the given PEM-encoded bytes.

func ParseOpenSSHPrivateKey

func ParseOpenSSHPrivateKey(pemBytes []byte, opts ...Options) (crypto.PrivateKey, error)

ParseOpenSSHPrivateKey parses a private key in OpenSSH PEM format.

Implemented based on the documentation at https://github.com/openssh/openssh-portable/blob/master/PROTOCOL.key

This method is based on the implementation at https://github.com/golang/crypto/blob/master/ssh/keys.go

func ParseSSH

func ParseSSH(b []byte) (interface{}, error)

ParseSSH parses parses a public key from an authorized_keys file used in OpenSSH according to the sshd(8) manual page.

func Read

func Read(filename string, opts ...Options) (interface{}, error)

Read returns the key or certificate encoded in the given PEM file. If the file is encrypted it will ask for a password and it will try to decrypt it.

Supported keys algorithms are RSA and EC. Supported standards for private keys are PKCS#1, PKCS#8, RFC5915 for EC, and base64-encoded DER for certificates and public keys.

func ReadCertificate

func ReadCertificate(filename string, opts ...Options) (*x509.Certificate, error)

ReadCertificate returns a *x509.Certificate from the given filename. It supports certificates formats PEM and DER.

func ReadCertificateBundle

func ReadCertificateBundle(filename string) ([]*x509.Certificate, error)

ReadCertificateBundle returns a list of *x509.Certificate from the given filename. It supports certificates formats PEM and DER. If a DER-formatted file is given only one certificate will be returned.

func ReadCertificateRequest added in v0.5.0

func ReadCertificateRequest(filename string) (*x509.CertificateRequest, error)

ReadCertificateRequest returns a *x509.CertificateRequest from the given filename. It supports certificates formats PEM and DER.

func Serialize

func Serialize(in interface{}, opts ...Options) (*pem.Block, error)

Serialize will serialize the input to a PEM formatted block and apply modifiers.

func SerializeOpenSSHPrivateKey

func SerializeOpenSSHPrivateKey(key crypto.PrivateKey, opts ...Options) (*pem.Block, error)

SerializeOpenSSHPrivateKey serialize a private key in the OpenSSH PEM format.

Types

type FileWriter

type FileWriter func(filename string, data []byte, perm os.FileMode) error

FileWriter defines the function signature for the WriteFile callback.

var WriteFile FileWriter = utils.WriteFile

WriteFile is a method used to write a file, by default it uses a wrapper over ioutil.WriteFile, but it can be set to a custom method, that for example can check if a file exists and prompts the user if it should be overwritten.

type Options

type Options func(o *context) error

Options is the type to add attributes to the context.

func ToFile

func ToFile(name string, perm os.FileMode) Options

ToFile is a method that adds the given filename and permissions to the context. It is used in the Serialize to store PEM in disk.

func WithComment

func WithComment(comment string) Options

WithComment is an option used in the Serialize method to add a comment in the OpenSSH private keys. WithOpenSSH must be set to true too.

func WithFilename

func WithFilename(name string) Options

WithFilename is a method that adds the given filename to the context.

func WithFirstBlock

func WithFirstBlock() Options

WithFirstBlock will avoid failing if a PEM contains more than one block or certificate and it will only look at the first.

func WithOpenSSH

func WithOpenSSH(v bool) Options

WithOpenSSH is an option used in the Serialize method to use OpenSSH encoding form on the private keys. With v set to false default form will be used.

func WithPKCS8

func WithPKCS8(v bool) Options

WithPKCS8 with v set to true returns an option used in the Serialize method to use the PKCS#8 encoding form on the private keys. With v set to false default form will be used.

func WithPassword

func WithPassword(pass []byte) Options

WithPassword is a method that adds the given password to the context.

func WithPasswordFile

func WithPasswordFile(filename string) Options

WithPasswordFile is a method that adds the password in a file to the context.

func WithPasswordPrompt

func WithPasswordPrompt(prompt string, fn PasswordPrompter) Options

WithPasswordPrompt ask the user for a password and adds it to the context.

type PasswordPrompter

type PasswordPrompter func(s string) ([]byte, error)

PasswordPrompter defines the function signature for the PromptPassword callback.

var PromptPassword PasswordPrompter

PromptPassword is a method used to prompt for a password to decode encrypted keys. If this method is not defined and the key or password are not passed, the parse of the key will fail.

Jump to

Keyboard shortcuts

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