...

Package openpgp

import "github.com/ProtonMail/go-crypto/openpgp"
Overview
Index
Subdirectories

Overview ▾

Package openpgp implements high level operations on OpenPGP messages.

Index ▾

Variables
func ArmoredDetachSign(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) (err error)
func ArmoredDetachSignText(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error
func DetachSign(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error
func DetachSignText(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error
func Encrypt(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)
func EncryptSplit(keyWriter io.Writer, dataWriter io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)
func EncryptText(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)
func EncryptTextSplit(keyWriter io.Writer, dataWriter io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)
func HashIdToHash(id byte) (h crypto.Hash, ok bool)
func HashIdToString(id byte) (name string, ok bool)
func HashToHashId(h crypto.Hash) (id byte, ok bool)
func NewCanonicalTextHash(h hash.Hash) hash.Hash
func Sign(output io.Writer, signed *Entity, hints *FileHints, config *packet.Config) (input io.WriteCloser, err error)
func SymmetricallyEncrypt(ciphertext io.Writer, passphrase []byte, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)
type Entity
    func CheckArmoredDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (signer *Entity, err error)
    func CheckDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (signer *Entity, err error)
    func CheckDetachedSignatureAndHash(keyring KeyRing, signed, signature io.Reader, expectedHashes []crypto.Hash, config *packet.Config) (signer *Entity, err error)
    func NewEntity(name, comment, email string, config *packet.Config) (*Entity, error)
    func ReadEntity(packets *packet.Reader) (*Entity, error)
    func VerifyDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (sig *packet.Signature, signer *Entity, err error)
    func VerifyDetachedSignatureAndHash(keyring KeyRing, signed, signature io.Reader, expectedHashes []crypto.Hash, config *packet.Config) (sig *packet.Signature, signer *Entity, err error)
    func (e *Entity) AddEncryptionSubkey(config *packet.Config) error
    func (e *Entity) AddSigningSubkey(config *packet.Config) error
    func (t *Entity) AddUserId(name, comment, email string, config *packet.Config) error
    func (e *Entity) CertificationKey(now time.Time) (Key, bool)
    func (e *Entity) CertificationKeyById(now time.Time, id uint64) (Key, bool)
    func (e *Entity) DecryptPrivateKeys(passphrase []byte) error
    func (e *Entity) EncryptPrivateKeys(passphrase []byte, config *packet.Config) error
    func (e *Entity) EncryptionKey(now time.Time) (Key, bool)
    func (e *Entity) PrimaryIdentity() *Identity
    func (e *Entity) RevokeKey(reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error
    func (e *Entity) RevokeSubkey(sk *Subkey, reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error
    func (e *Entity) Revoked(now time.Time) bool
    func (e *Entity) Serialize(w io.Writer) error
    func (e *Entity) SerializePrivate(w io.Writer, config *packet.Config) (err error)
    func (e *Entity) SerializePrivateWithoutSigning(w io.Writer, config *packet.Config) (err error)
    func (e *Entity) SignIdentity(identity string, signer *Entity, config *packet.Config) error
    func (e *Entity) SigningKey(now time.Time) (Key, bool)
    func (e *Entity) SigningKeyById(now time.Time, id uint64) (Key, bool)
type EntityList
    func ReadArmoredKeyRing(r io.Reader) (EntityList, error)
    func ReadKeyRing(r io.Reader) (el EntityList, err error)
    func (el EntityList) DecryptionKeys() (keys []Key)
    func (el EntityList) KeysById(id uint64) (keys []Key)
    func (el EntityList) KeysByIdUsage(id uint64, requiredUsage byte) (keys []Key)
type FileHints
type Identity
    func (i *Identity) Revoked(now time.Time) bool
type Key
    func (key *Key) Revoked(now time.Time) bool
type KeyRing
type MessageDetails
    func ReadMessage(r io.Reader, keyring KeyRing, prompt PromptFunction, config *packet.Config) (md *MessageDetails, err error)
type PromptFunction
type Subkey
    func (s *Subkey) Revoked(now time.Time) bool

Package files

canonical_text.go hash.go key_generation.go keys.go keys_test_data.go read.go read_write_test_data.go write.go

Variables

PrivateKeyType is the armor type for a PGP private key.

var PrivateKeyType = "PGP PRIVATE KEY BLOCK"

PublicKeyType is the armor type for a PGP public key.

var PublicKeyType = "PGP PUBLIC KEY BLOCK"

SignatureType is the armor type for a PGP signature.

var SignatureType = "PGP SIGNATURE"

func ArmoredDetachSign

func ArmoredDetachSign(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) (err error)

ArmoredDetachSign signs message with the private key from signer (which must already have been decrypted) and writes an armored signature to w. If config is nil, sensible defaults will be used.

func ArmoredDetachSignText

func ArmoredDetachSignText(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error

ArmoredDetachSignText signs message (after canonicalising the line endings) with the private key from signer (which must already have been decrypted) and writes an armored signature to w. If config is nil, sensible defaults will be used.

func DetachSign

func DetachSign(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error

DetachSign signs message with the private key from signer (which must already have been decrypted) and writes the signature to w. If config is nil, sensible defaults will be used.

func DetachSignText

func DetachSignText(w io.Writer, signer *Entity, message io.Reader, config *packet.Config) error

DetachSignText signs message (after canonicalising the line endings) with the private key from signer (which must already have been decrypted) and writes the signature to w. If config is nil, sensible defaults will be used.

func Encrypt

func Encrypt(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)

Encrypt encrypts a message to a number of recipients and, optionally, signs it. hints contains optional information, that is also encrypted, that aids the recipients in processing the message. The resulting WriteCloser must be closed after the contents of the file have been written. If config is nil, sensible defaults will be used.

func EncryptSplit

func EncryptSplit(keyWriter io.Writer, dataWriter io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)

EncryptSplit encrypts a message to a number of recipients and, optionally, signs it. hints contains optional information, that is also encrypted, that aids the recipients in processing the message. The resulting WriteCloser must be closed after the contents of the file have been written. If config is nil, sensible defaults will be used.

func EncryptText

func EncryptText(ciphertext io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)

EncryptText encrypts a message to a number of recipients and, optionally, signs it. Optional information is contained in 'hints', also encrypted, that aids the recipients in processing the message. The resulting WriteCloser must be closed after the contents of the file have been written. If config is nil, sensible defaults will be used. The signing is done in text mode.

func EncryptTextSplit

func EncryptTextSplit(keyWriter io.Writer, dataWriter io.Writer, to []*Entity, signed *Entity, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)

EncryptTextSplit encrypts a message to a number of recipients and, optionally, signs it. hints contains optional information, that is also encrypted, that aids the recipients in processing the message. The resulting WriteCloser must be closed after the contents of the file have been written. If config is nil, sensible defaults will be used.

func HashIdToHash

func HashIdToHash(id byte) (h crypto.Hash, ok bool)

HashIdToHash returns a crypto.Hash which corresponds to the given OpenPGP hash id.

func HashIdToString

func HashIdToString(id byte) (name string, ok bool)

HashIdToString returns the name of the hash function corresponding to the given OpenPGP hash id.

func HashToHashId

func HashToHashId(h crypto.Hash) (id byte, ok bool)

HashToHashId returns an OpenPGP hash id which corresponds the given Hash.

func NewCanonicalTextHash

func NewCanonicalTextHash(h hash.Hash) hash.Hash

NewCanonicalTextHash reformats text written to it into the canonical form and then applies the hash h. See RFC 4880, section 5.2.1.

func Sign

func Sign(output io.Writer, signed *Entity, hints *FileHints, config *packet.Config) (input io.WriteCloser, err error)

Sign signs a message. The resulting WriteCloser must be closed after the contents of the file have been written. hints contains optional information that aids the recipients in processing the message. If config is nil, sensible defaults will be used.

func SymmetricallyEncrypt

func SymmetricallyEncrypt(ciphertext io.Writer, passphrase []byte, hints *FileHints, config *packet.Config) (plaintext io.WriteCloser, err error)

SymmetricallyEncrypt acts like gpg -c: it encrypts a file with a passphrase. The resulting WriteCloser must be closed after the contents of the file have been written. If config is nil, sensible defaults will be used.

type Entity

An Entity represents the components of an OpenPGP key: a primary public key (which must be a signing key), one or more identities claimed by that key, and zero or more subkeys, which may be encryption keys.

type Entity struct {
    PrimaryKey  *packet.PublicKey
    PrivateKey  *packet.PrivateKey
    Identities  map[string]*Identity // indexed by Identity.Name
    Revocations []*packet.Signature
    Subkeys     []Subkey
}

func CheckArmoredDetachedSignature

func CheckArmoredDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (signer *Entity, err error)

CheckArmoredDetachedSignature performs the same actions as CheckDetachedSignature but expects the signature to be armored.

func CheckDetachedSignature

func CheckDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (signer *Entity, err error)

CheckDetachedSignature takes a signed file and a detached signature and returns the entity the signature was signed by, if any, and a possible signature verification error. If the signer isn't known, ErrUnknownIssuer is returned.

func CheckDetachedSignatureAndHash

func CheckDetachedSignatureAndHash(keyring KeyRing, signed, signature io.Reader, expectedHashes []crypto.Hash, config *packet.Config) (signer *Entity, err error)

CheckDetachedSignatureAndHash performs the same actions as CheckDetachedSignature and checks that the expected hash functions were used.

func NewEntity

func NewEntity(name, comment, email string, config *packet.Config) (*Entity, error)

NewEntity returns an Entity that contains a fresh RSA/RSA keypair with a single identity composed of the given full name, comment and email, any of which may be empty but must not contain any of "()<>\x00". If config is nil, sensible defaults will be used.

func ReadEntity

func ReadEntity(packets *packet.Reader) (*Entity, error)

ReadEntity reads an entity (public key, identities, subkeys etc) from the given Reader.

func VerifyDetachedSignature

func VerifyDetachedSignature(keyring KeyRing, signed, signature io.Reader, config *packet.Config) (sig *packet.Signature, signer *Entity, err error)

VerifyDetachedSignature takes a signed file and a detached signature and returns the signature packet and the entity the signature was signed by, if any, and a possible signature verification error. If the signer isn't known, ErrUnknownIssuer is returned.

func VerifyDetachedSignatureAndHash

func VerifyDetachedSignatureAndHash(keyring KeyRing, signed, signature io.Reader, expectedHashes []crypto.Hash, config *packet.Config) (sig *packet.Signature, signer *Entity, err error)

VerifyDetachedSignatureAndHash performs the same actions as VerifyDetachedSignature and checks that the expected hash functions were used.

func (*Entity) AddEncryptionSubkey

func (e *Entity) AddEncryptionSubkey(config *packet.Config) error

AddEncryptionSubkey adds an encryption keypair as a subkey to the Entity. If config is nil, sensible defaults will be used.

func (*Entity) AddSigningSubkey

func (e *Entity) AddSigningSubkey(config *packet.Config) error

AddSigningSubkey adds a signing keypair as a subkey to the Entity. If config is nil, sensible defaults will be used.

func (*Entity) AddUserId

func (t *Entity) AddUserId(name, comment, email string, config *packet.Config) error

func (*Entity) CertificationKey

func (e *Entity) CertificationKey(now time.Time) (Key, bool)

CertificationKey return the best candidate Key for certifying a key with this Entity.

func (*Entity) CertificationKeyById

func (e *Entity) CertificationKeyById(now time.Time, id uint64) (Key, bool)

CertificationKeyById return the Key for key certification with this Entity and keyID.

func (*Entity) DecryptPrivateKeys

func (e *Entity) DecryptPrivateKeys(passphrase []byte) error

DecryptPrivateKeys decrypts all encrypted keys in the entitiy with the given passphrase. Avoids recomputation of similar s2k key derivations. Public keys and dummy keys are ignored, and don't cause an error to be returned.

func (*Entity) EncryptPrivateKeys

func (e *Entity) EncryptPrivateKeys(passphrase []byte, config *packet.Config) error

EncryptPrivateKeys encrypts all non-encrypted keys in the entity with the same key derived from the provided passphrase. Public keys and dummy keys are ignored, and don't cause an error to be returned.

func (*Entity) EncryptionKey

func (e *Entity) EncryptionKey(now time.Time) (Key, bool)

EncryptionKey returns the best candidate Key for encrypting a message to the given Entity.

func (*Entity) PrimaryIdentity

func (e *Entity) PrimaryIdentity() *Identity

PrimaryIdentity returns an Identity, preferring non-revoked identities, identities marked as primary, or the latest-created identity, in that order.

func (*Entity) RevokeKey

func (e *Entity) RevokeKey(reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error

RevokeKey generates a key revocation signature (packet.SigTypeKeyRevocation) with the specified reason code and text (RFC4880 section-5.2.3.23). If config is nil, sensible defaults will be used.

func (*Entity) RevokeSubkey

func (e *Entity) RevokeSubkey(sk *Subkey, reason packet.ReasonForRevocation, reasonText string, config *packet.Config) error

RevokeSubkey generates a subkey revocation signature (packet.SigTypeSubkeyRevocation) for a subkey with the specified reason code and text (RFC4880 section-5.2.3.23). If config is nil, sensible defaults will be used.

func (*Entity) Revoked

func (e *Entity) Revoked(now time.Time) bool

Revoked returns whether the entity has any direct key revocation signatures. Note that third-party revocation signatures are not supported. Note also that Identity and Subkey revocation should be checked separately.

func (*Entity) Serialize

func (e *Entity) Serialize(w io.Writer) error

Serialize writes the public part of the given Entity to w, including signatures from other entities. No private key material will be output.

func (*Entity) SerializePrivate

func (e *Entity) SerializePrivate(w io.Writer, config *packet.Config) (err error)

SerializePrivate serializes an Entity, including private key material, but excluding signatures from other entities, to the given Writer. Identities and subkeys are re-signed in case they changed since NewEntry. If config is nil, sensible defaults will be used.

func (*Entity) SerializePrivateWithoutSigning

func (e *Entity) SerializePrivateWithoutSigning(w io.Writer, config *packet.Config) (err error)

SerializePrivateWithoutSigning serializes an Entity, including private key material, but excluding signatures from other entities, to the given Writer. Self-signatures of identities and subkeys are not re-signed. This is useful when serializing GNU dummy keys, among other things. If config is nil, sensible defaults will be used.

func (*Entity) SignIdentity

func (e *Entity) SignIdentity(identity string, signer *Entity, config *packet.Config) error

SignIdentity adds a signature to e, from signer, attesting that identity is associated with e. The provided identity must already be an element of e.Identities and the private key of signer must have been decrypted if necessary. If config is nil, sensible defaults will be used.

func (*Entity) SigningKey

func (e *Entity) SigningKey(now time.Time) (Key, bool)

SigningKey return the best candidate Key for signing a message with this Entity.

func (*Entity) SigningKeyById

func (e *Entity) SigningKeyById(now time.Time, id uint64) (Key, bool)

SigningKeyById return the Key for signing a message with this Entity and keyID.

type EntityList

An EntityList contains one or more Entities.

type EntityList []*Entity

func ReadArmoredKeyRing

func ReadArmoredKeyRing(r io.Reader) (EntityList, error)

ReadArmoredKeyRing reads one or more public/private keys from an armor keyring file.

func ReadKeyRing

func ReadKeyRing(r io.Reader) (el EntityList, err error)

ReadKeyRing reads one or more public/private keys. Unsupported keys are ignored as long as at least a single valid key is found.

func (EntityList) DecryptionKeys

func (el EntityList) DecryptionKeys() (keys []Key)

DecryptionKeys returns all private keys that are valid for decryption.

func (EntityList) KeysById

func (el EntityList) KeysById(id uint64) (keys []Key)

KeysById returns the set of keys that have the given key id.

func (EntityList) KeysByIdUsage

func (el EntityList) KeysByIdUsage(id uint64, requiredUsage byte) (keys []Key)

KeysByIdAndUsage returns the set of keys with the given id that also meet the key usage given by requiredUsage. The requiredUsage is expressed as the bitwise-OR of packet.KeyFlag* values.

type FileHints

FileHints contains metadata about encrypted files. This metadata is, itself, encrypted.

type FileHints struct {
    // IsBinary can be set to hint that the contents are binary data.
    IsBinary bool
    // FileName hints at the name of the file that should be written. It's
    // truncated to 255 bytes if longer. It may be empty to suggest that the
    // file should not be written to disk. It may be equal to "_CONSOLE" to
    // suggest the data should not be written to disk.
    FileName string
    // ModTime contains the modification time of the file, or the zero time if not applicable.
    ModTime time.Time
}

type Identity

An Identity represents an identity claimed by an Entity and zero or more assertions by other entities about that claim.

type Identity struct {
    Name          string // by convention, has the form "Full Name (comment) <email@example.com>"
    UserId        *packet.UserId
    SelfSignature *packet.Signature
    Revocations   []*packet.Signature
    Signatures    []*packet.Signature // all (potentially unverified) self-signatures, revocations, and third-party signatures
}

func (*Identity) Revoked

func (i *Identity) Revoked(now time.Time) bool

Revoked returns whether the identity has been revoked by a self-signature. Note that third-party revocation signatures are not supported.

type Key

A Key identifies a specific public key in an Entity. This is either the Entity's primary key or a subkey.

type Key struct {
    Entity        *Entity
    PublicKey     *packet.PublicKey
    PrivateKey    *packet.PrivateKey
    SelfSignature *packet.Signature
    Revocations   []*packet.Signature
}

func (*Key) Revoked

func (key *Key) Revoked(now time.Time) bool

Revoked returns whether the key or subkey has been revoked by a self-signature. Note that third-party revocation signatures are not supported. Note also that Identity revocation should be checked separately. Normally, it's not necessary to call this function, except on keys returned by KeysById or KeysByIdUsage.

type KeyRing

A KeyRing provides access to public and private keys.

type KeyRing interface {
    // KeysById returns the set of keys that have the given key id.
    KeysById(id uint64) []Key
    // KeysByIdAndUsage returns the set of keys with the given id
    // that also meet the key usage given by requiredUsage.
    // The requiredUsage is expressed as the bitwise-OR of
    // packet.KeyFlag* values.
    KeysByIdUsage(id uint64, requiredUsage byte) []Key
    // DecryptionKeys returns all private keys that are valid for
    // decryption.
    DecryptionKeys() []Key
}

type MessageDetails

MessageDetails contains the result of parsing an OpenPGP encrypted and/or signed message.

type MessageDetails struct {
    IsEncrypted              bool                // true if the message was encrypted.
    EncryptedToKeyIds        []uint64            // the list of recipient key ids.
    IsSymmetricallyEncrypted bool                // true if a passphrase could have decrypted the message.
    DecryptedWith            Key                 // the private key used to decrypt the message, if any.
    IsSigned                 bool                // true if the message is signed.
    SignedByKeyId            uint64              // the key id of the signer, if any.
    SignedBy                 *Key                // the key of the signer, if available.
    LiteralData              *packet.LiteralData // the metadata of the contents
    UnverifiedBody           io.Reader           // the contents of the message.

    // If IsSigned is true and SignedBy is non-zero then the signature will
    // be verified as UnverifiedBody is read. The signature cannot be
    // checked until the whole of UnverifiedBody is read so UnverifiedBody
    // must be consumed until EOF before the data can be trusted. Even if a
    // message isn't signed (or the signer is unknown) the data may contain
    // an authentication code that is only checked once UnverifiedBody has
    // been consumed. Once EOF has been seen, the following fields are
    // valid. (An authentication code failure is reported as a
    // SignatureError error when reading from UnverifiedBody.)
    Signature            *packet.Signature   // the signature packet itself.
    SignatureError       error               // nil if the signature is good.
    UnverifiedSignatures []*packet.Signature // all other unverified signature packets.
    // contains filtered or unexported fields
}

func ReadMessage

func ReadMessage(r io.Reader, keyring KeyRing, prompt PromptFunction, config *packet.Config) (md *MessageDetails, err error)

ReadMessage parses an OpenPGP message that may be signed and/or encrypted. The given KeyRing should contain both public keys (for signature verification) and, possibly encrypted, private keys for decrypting. If config is nil, sensible defaults will be used.

type PromptFunction

A PromptFunction is used as a callback by functions that may need to decrypt a private key, or prompt for a passphrase. It is called with a list of acceptable, encrypted private keys and a boolean that indicates whether a passphrase is usable. It should either decrypt a private key or return a passphrase to try. If the decrypted private key or given passphrase isn't correct, the function will be called again, forever. Any error returned will be passed up.

type PromptFunction func(keys []Key, symmetric bool) ([]byte, error)

type Subkey

A Subkey is an additional public key in an Entity. Subkeys can be used for encryption.

type Subkey struct {
    PublicKey   *packet.PublicKey
    PrivateKey  *packet.PrivateKey
    Sig         *packet.Signature
    Revocations []*packet.Signature
}

func (*Subkey) Revoked

func (s *Subkey) Revoked(now time.Time) bool

Revoked returns whether the subkey has been revoked by a self-signature. Note that third-party revocation signatures are not supported.

Subdirectories

Name Synopsis
..
aes
keywrap Package keywrap is an implementation of the RFC 3394 AES key wrapping algorithm.
armor Package armor implements OpenPGP ASCII Armor, see RFC 4880.
clearsign Package clearsign generates and processes OpenPGP, clear-signed data.
ecdh Package ecdh implements ECDH encryption, suitable for OpenPGP, as specified in RFC 6637, section 8.
ecdsa Package ecdsa implements ECDSA signature, suitable for OpenPGP, as specified in RFC 6637, section 5.
eddsa Package eddsa implements EdDSA signature, suitable for OpenPGP, as specified in https://datatracker.ietf.org/doc/html/draft-ietf-openpgp-crypto-refresh-06#section-13.7
elgamal Package elgamal implements ElGamal encryption, suitable for OpenPGP, as specified in "A Public-Key Cryptosystem and a Signature Scheme Based on Discrete Logarithms," IEEE Transactions on Information Theory, v.
errors Package errors contains common error types for the OpenPGP packages.
packet Package packet implements parsing and serialization of OpenPGP packets, as specified in RFC 4880.
s2k Package s2k implements the various OpenPGP string-to-key transforms as specified in RFC 4800 section 3.7.1, and Argon2 specified in draft-ietf-openpgp-crypto-refresh-08 section 3.7.1.4.