jwe

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package jwe implements JSON Web Encryption (RFC 7516) compact serialization for the ECDH-ES key agreement algorithm (RFC 7518 Section 4.6) with A256GCM content encryption (RFC 7518 Section 5.3), using NIST P-256, P-384 and P-521 keys.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUnsupportedKeyAlgorithm      = errors.New("unsupported key algorithm")
	ErrUnsupportedContentEncryption = errors.New("unsupported content encryption")
	ErrUnsupportedKeyType           = errors.New("unsupported key type")
	ErrUnsupportedCompression       = errors.New("unsupported compression")
	ErrUnexpectedEncryptedKey       = errors.New("unexpected encrypted key for direct key agreement")
)

Functions

This section is empty.

Types

type ContentEncryption

type ContentEncryption string
const ContentEncryptionA256Gcm ContentEncryption = "A256GCM"

type Encrypter

type Encrypter struct {
	KeyAlgorithm       KeyAlgorithm
	ContentEncryption  ContentEncryption
	RecipientPublicKey *ecdsa.PublicKey
	KeyId              string
	ContentType        string
}

Encrypter encrypts plaintexts to a recipient public key, producing JWE compact serializations.

func NewEncrypter

func NewEncrypter(
	keyAlgorithm KeyAlgorithm,
	contentEncryption ContentEncryption,
	recipientPublicKey *ecdsa.PublicKey,
) (*Encrypter, error)

NewEncrypter validates the algorithms and the recipient public key and returns an Encrypter.

func (*Encrypter) Encrypt

func (encrypter *Encrypter) Encrypt(plaintext []byte) (string, error)

Encrypt encrypts plaintext and returns the JWE compact serialization.

type Encryption

type Encryption struct {
	// Header is the parsed protected header.
	Header *Header
	// contains filtered or unexported fields
}

Encryption is a parsed JWE.

func ParseCompact

func ParseCompact(
	serialization string,
	allowedKeyAlgorithms []KeyAlgorithm,
	allowedContentEncryptions []ContentEncryption,
) (*Encryption, error)

ParseCompact parses a JWE compact serialization, requiring the key algorithm and content encryption of the protected header to be in the given allowlists.

func (*Encryption) Decrypt

func (encryption *Encryption) Decrypt(privateKey any) ([]byte, error)

Decrypt decrypts the JWE with the recipient private key, which must be a *ecdsa.PrivateKey or a *ecdh.PrivateKey. Failures caused by the content not matching the key match motmedelErrors.ErrVerificationError with errors.Is.

type Header struct {
	Algorithm           KeyAlgorithm      `json:"alg"`
	ContentEncryption   ContentEncryption `json:"enc"`
	EphemeralPublicKey  *key.Key          `json:"epk,omitzero"`
	KeyId               string            `json:"kid,omitzero"`
	ContentType         string            `json:"cty,omitzero"`
	AgreementPartyUInfo string            `json:"apu,omitzero"`
	AgreementPartyVInfo string            `json:"apv,omitzero"`
	Compression         string            `json:"zip,omitzero"`
}

Header is a JWE protected header.

func (*Header) UnmarshalJSON

func (header *Header) UnmarshalJSON(data []byte) error

UnmarshalJSON exists because key.Key is constructed from a map rather than unmarshalled directly.

type KeyAlgorithm

type KeyAlgorithm string
const KeyAlgorithmEcdhEs KeyAlgorithm = "ECDH-ES"

Jump to

Keyboard shortcuts

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