Documentation
¶
Overview ¶
Package xmlenc implements XML Encryption Syntax and Processing Version 1.1 as specified in https://www.w3.org/TR/xmlenc-core1/
This package provides encryption primitives that complement the XML Signature functionality in signedxml. Both share common infrastructure like canonicalization and KeyInfo handling.
Package xmlenc implements XML Encryption Syntax and Processing Version 1.1
Index ¶
- Constants
- Variables
- func AESCBCDecrypt(key, ciphertext []byte) ([]byte, error)
- func AESCBCEncrypt(key, plaintext []byte) ([]byte, error)
- func AESGCMDecrypt(key, ciphertext, additionalData []byte) ([]byte, error)
- func AESGCMEncrypt(key, plaintext, additionalData []byte) ([]byte, error)
- func AESKeyUnwrap(kek, ciphertext []byte) ([]byte, error)
- func AESKeyUnwrapWithIV(kek, ciphertext, expectedIV []byte) ([]byte, error)
- func AESKeyWrap(kek, plaintext []byte) ([]byte, error)
- func AESKeyWrapWithIV(kek, plaintext, iv []byte) ([]byte, error)
- func DecryptElementInPlace(edElem *etree.Element, decryptor *Decryptor) error
- func EncryptElementInPlace(elem *etree.Element, encryptor *Encryptor) error
- func GenerateX25519KeyPair() (*ecdh.PrivateKey, error)
- func IsGCM(algorithm string) bool
- func IsKeyAgreement(algorithm string) bool
- func IsKeyWrap(algorithm string) bool
- func KeySize(algorithm string) int
- func KeyWrapAlgorithmForContentAlgorithm(contentAlgorithm string) string
- func NewEncryptedDataDocument(ed *EncryptedData) *etree.Document
- func NewEncryptedKeyDocument(ek *EncryptedKey) *etree.Document
- func ParseX25519PrivateKey(data []byte) (*ecdh.PrivateKey, error)
- func ParseX25519PublicKey(data []byte) (*ecdh.PublicKey, error)
- type AgreementMethod
- type CipherData
- type CipherReference
- type ConcatKDFParams
- type DataReference
- type Decryptor
- type DerivedKey
- type ECKeyValue
- type EncryptedData
- type EncryptedKey
- type EncryptedType
- type EncryptionMethod
- type Encryptor
- type HKDFParams
- type KeyDerivationMethod
- type KeyInfo
- type KeyUnwrapper
- type KeyValue
- type KeyWrapper
- type PBKDF2Params
- type RSAKeyValue
- type RetrievalMethod
- type Transform
- type X25519KeyAgreement
- type X509Data
Constants ¶
const ( // Namespace URIs NamespaceXMLEnc = "http://www.w3.org/2001/04/xmlenc#" NamespaceXMLEnc11 = "http://www.w3.org/2009/xmlenc11#" NamespaceXMLDSig = "http://www.w3.org/2000/09/xmldsig#" NamespaceXMLDSig11 = "http://www.w3.org/2009/xmldsig11#" NamespaceXMLDSigMore = "http://www.w3.org/2001/04/xmldsig-more#" NamespaceXMLDSig2021 = "http://www.w3.org/2021/04/xmldsig-more#" // Block Encryption Algorithms AlgorithmAES128CBC = "http://www.w3.org/2001/04/xmlenc#aes128-cbc" AlgorithmAES192CBC = "http://www.w3.org/2001/04/xmlenc#aes192-cbc" AlgorithmAES256CBC = "http://www.w3.org/2001/04/xmlenc#aes256-cbc" AlgorithmAES128GCM = "http://www.w3.org/2009/xmlenc11#aes128-gcm" AlgorithmAES192GCM = "http://www.w3.org/2009/xmlenc11#aes192-gcm" AlgorithmAES256GCM = "http://www.w3.org/2009/xmlenc11#aes256-gcm" AlgorithmTripleDES = "http://www.w3.org/2001/04/xmlenc#tripledes-cbc" // Key Transport Algorithms AlgorithmRSAv15 = "http://www.w3.org/2001/04/xmlenc#rsa-1_5" AlgorithmRSAOAEP = "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p" AlgorithmRSAOAEP11 = "http://www.w3.org/2009/xmlenc11#rsa-oaep" // Key Wrap Algorithms AlgorithmAES128KW = "http://www.w3.org/2001/04/xmlenc#kw-aes128" AlgorithmAES192KW = "http://www.w3.org/2001/04/xmlenc#kw-aes192" AlgorithmAES256KW = "http://www.w3.org/2001/04/xmlenc#kw-aes256" AlgorithmTripleDESKW = "http://www.w3.org/2001/04/xmlenc#kw-tripledes" // Key Agreement Algorithms AlgorithmDH = "http://www.w3.org/2001/04/xmlenc#dh" AlgorithmDHES = "http://www.w3.org/2009/xmlenc11#dh-es" AlgorithmECDHES = "http://www.w3.org/2009/xmlenc11#ECDH-ES" AlgorithmX25519 = "http://www.w3.org/2021/04/xmldsig-more#x25519" // Key Derivation Algorithms AlgorithmConcatKDF = "http://www.w3.org/2009/xmlenc11#ConcatKDF" AlgorithmPBKDF2 = "http://www.w3.org/2009/xmlenc11#pbkdf2" AlgorithmHKDF = "http://www.w3.org/2021/04/xmldsig-more#hkdf" // Digest Algorithms (from XML Signature, used in key derivation) AlgorithmSHA1 = "http://www.w3.org/2000/09/xmldsig#sha1" AlgorithmSHA256 = "http://www.w3.org/2001/04/xmlenc#sha256" AlgorithmSHA384 = "http://www.w3.org/2001/04/xmlenc#sha384" AlgorithmSHA512 = "http://www.w3.org/2001/04/xmlenc#sha512" AlgorithmHMACSHA256 = "http://www.w3.org/2001/04/xmldsig-more#hmac-sha256" // MGF Algorithms (for RSA-OAEP) AlgorithmMGF1SHA1 = "http://www.w3.org/2009/xmlenc11#mgf1sha1" AlgorithmMGF1SHA256 = "http://www.w3.org/2009/xmlenc11#mgf1sha256" AlgorithmMGF1SHA384 = "http://www.w3.org/2009/xmlenc11#mgf1sha384" AlgorithmMGF1SHA512 = "http://www.w3.org/2009/xmlenc11#mgf1sha512" // Type URIs TypeEncryptedKey = "http://www.w3.org/2001/04/xmlenc#EncryptedKey" TypeDerivedKey = "http://www.w3.org/2009/xmlenc11#DerivedKey" TypeElement = "http://www.w3.org/2001/04/xmlenc#Element" TypeContent = "http://www.w3.org/2001/04/xmlenc#Content" )
Algorithm URIs for XML Encryption 1.1 These are the standard algorithm identifiers as defined in the W3C XML Encryption specification
Variables ¶
var ( // ErrInvalidKeySize is returned when the key size is not valid for AES ErrInvalidKeySize = errors.New("invalid key size: must be 16, 24, or 32 bytes") // ErrInvalidPlaintextSize is returned when plaintext is too small or not aligned ErrInvalidPlaintextSize = errors.New("invalid plaintext size: must be >= 16 bytes and multiple of 8") // ErrInvalidCiphertextSize is returned when ciphertext is too small or not aligned ErrInvalidCiphertextSize = errors.New("invalid ciphertext size: must be >= 24 bytes and multiple of 8") // ErrIntegrityCheckFailed is returned when the integrity check fails during unwrap ErrIntegrityCheckFailed = errors.New("integrity check failed: invalid wrapped key") )
Functions ¶
func AESCBCDecrypt ¶
AESCBCDecrypt decrypts ciphertext encrypted with AESCBCEncrypt. Expects format: IV (16 bytes) || ciphertext
func AESCBCEncrypt ¶
AESCBCEncrypt encrypts plaintext using AES-CBC with PKCS#7 padding. The IV is generated randomly and prepended to the ciphertext.
func AESGCMDecrypt ¶
AESGCMDecrypt decrypts ciphertext encrypted with AESGCMEncrypt. Expects format: IV (12 bytes) || ciphertext || tag (16 bytes)
func AESGCMEncrypt ¶
AESGCMEncrypt encrypts plaintext using AES-GCM. The IV is generated randomly and prepended to the ciphertext. The returned data format is: IV (12 bytes) || ciphertext || tag (16 bytes)
func AESKeyUnwrap ¶
AESKeyUnwrap unwraps a wrapped key encrypted with AESKeyWrap.
The ciphertext must be at least 24 bytes and a multiple of 8 bytes. The returned plaintext will be 8 bytes shorter than the ciphertext.
Algorithm: RFC 3394 Section 2.2.2
func AESKeyUnwrapWithIV ¶
AESKeyUnwrapWithIV unwraps with a custom IV for verification. Most uses should prefer AESKeyUnwrap which uses the standard IV.
func AESKeyWrap ¶
AESKeyWrap wraps a content encryption key (plaintext) with a key encryption key.
The plaintext must be at least 16 bytes and a multiple of 8 bytes. The returned ciphertext will be 8 bytes longer than the plaintext.
Algorithm: RFC 3394 Section 2.2.1
func AESKeyWrapWithIV ¶
AESKeyWrapWithIV wraps a content encryption key with a custom IV. Most uses should prefer AESKeyWrap which uses the standard IV.
func DecryptElementInPlace ¶
DecryptElementInPlace decrypts an EncryptedData element and replaces it in the document
func EncryptElementInPlace ¶
EncryptElementInPlace encrypts an element and replaces it in the document
func GenerateX25519KeyPair ¶
func GenerateX25519KeyPair() (*ecdh.PrivateKey, error)
GenerateX25519KeyPair generates a new X25519 key pair
func IsKeyAgreement ¶
IsKeyAgreement returns true if the algorithm is a key agreement algorithm
func KeySize ¶
KeySize returns the key size in bytes for the given algorithm URI. Returns 0 if the algorithm is not recognized or has variable key size.
func KeyWrapAlgorithmForContentAlgorithm ¶
KeyWrapAlgorithmForContentAlgorithm returns the appropriate key wrap algorithm for a given content encryption algorithm based on key size.
func NewEncryptedDataDocument ¶
func NewEncryptedDataDocument(ed *EncryptedData) *etree.Document
NewEncryptedDataDocument creates an etree.Document containing an EncryptedData element
func NewEncryptedKeyDocument ¶
func NewEncryptedKeyDocument(ek *EncryptedKey) *etree.Document
NewEncryptedKeyDocument creates an etree.Document containing an EncryptedKey element
func ParseX25519PrivateKey ¶
func ParseX25519PrivateKey(data []byte) (*ecdh.PrivateKey, error)
ParseX25519PrivateKey parses an X25519 private key from raw bytes
Types ¶
type AgreementMethod ¶
type AgreementMethod struct {
Algorithm string // e.g., AlgorithmECDHES, AlgorithmX25519
KeyDerivationMethod *KeyDerivationMethod
OriginatorKeyInfo *KeyInfo
RecipientKeyInfo *KeyInfo
KANonce []byte // Key Agreement Nonce
}
AgreementMethod represents xenc11:AgreementMethod for key agreement
type CipherData ¶
type CipherData struct {
CipherValue []byte // Base64-decoded encrypted content
CipherReference *CipherReference // URI reference to encrypted content
}
CipherData contains either CipherValue (inline) or CipherReference (external)
type CipherReference ¶
CipherReference points to external encrypted data
type ConcatKDFParams ¶
type ConcatKDFParams struct {
DigestMethod string
AlgorithmID []byte
PartyUInfo []byte
PartyVInfo []byte
SuppPubInfo []byte
SuppPrivInfo []byte
}
ConcatKDFParams contains parameters for Concat KDF
type DataReference ¶
type DataReference struct {
URI string
}
DataReference points to an EncryptedData element
type Decryptor ¶
type Decryptor struct {
// KeyUnwrapper handles key decryption
KeyUnwrapper KeyUnwrapper
}
Decryptor provides XML Decryption operations
func NewDecryptor ¶
func NewDecryptor(keyUnwrapper KeyUnwrapper) *Decryptor
NewDecryptor creates a new Decryptor with the specified key unwrapper
func (*Decryptor) DecryptElement ¶
func (d *Decryptor) DecryptElement(ed *EncryptedData) (*etree.Element, error)
DecryptElement decrypts an EncryptedData structure and returns the XML element
func (*Decryptor) DecryptEncryptedData ¶
func (d *Decryptor) DecryptEncryptedData(ed *EncryptedData) ([]byte, error)
DecryptEncryptedData decrypts an EncryptedData structure and returns the plaintext
type DerivedKey ¶
type DerivedKey struct {
ID string
Type string
Recipient string
KeyDerivationMethod *KeyDerivationMethod
ReferenceList []DataReference
MasterKeyName string
}
DerivedKey represents xenc11:DerivedKey
type ECKeyValue ¶
ECKeyValue contains EC public key parameters
type EncryptedData ¶
type EncryptedData struct {
EncryptedType
}
EncryptedData represents the xenc:EncryptedData element which contains encrypted content (either element or content encryption).
func ParseEncryptedData ¶
func ParseEncryptedData(elem *etree.Element) (*EncryptedData, error)
ParseEncryptedData parses an xenc:EncryptedData element from an etree.Element
func (*EncryptedData) ToElement ¶
func (ed *EncryptedData) ToElement() *etree.Element
ToElement converts EncryptedData to an etree.Element
type EncryptedKey ¶
type EncryptedKey struct {
EncryptedType
Recipient string // Optional hint to the recipient
CarriedKeyName string // Name for the key being carried
ReferenceList []DataReference
}
EncryptedKey represents the xenc:EncryptedKey element which contains an encrypted key wrapped for a specific recipient.
func ParseEncryptedKey ¶
func ParseEncryptedKey(elem *etree.Element) (*EncryptedKey, error)
ParseEncryptedKey parses an xenc:EncryptedKey element
func (*EncryptedKey) ToElement ¶
func (ek *EncryptedKey) ToElement() *etree.Element
ToElement converts EncryptedKey to an etree.Element
type EncryptedType ¶
type EncryptedType struct {
ID string
Type string // TypeElement, TypeContent, or custom URI
MimeType string
Encoding string
EncryptionMethod *EncryptionMethod
KeyInfo *KeyInfo
CipherData *CipherData
}
EncryptedType is the abstract base type for EncryptedData and EncryptedKey as defined in the XML Encryption specification.
type EncryptionMethod ¶
type EncryptionMethod struct {
Algorithm string // URI of the encryption algorithm
KeySize int // Optional explicit key size
OAEPParams []byte // For RSA-OAEP: MGF and DigestMethod
DigestMethod string // Digest algorithm for RSA-OAEP
MGFAlgorithm string // MGF algorithm for RSA-OAEP 1.1
}
EncryptionMethod specifies the algorithm used for encryption.
type Encryptor ¶
type Encryptor struct {
// Algorithm is the content encryption algorithm (e.g., AlgorithmAES128GCM)
Algorithm string
// KeyWrapper handles key encryption (e.g., X25519KeyAgreement)
KeyWrapper KeyWrapper
}
Encryptor provides XML Encryption operations
func NewEncryptor ¶
func NewEncryptor(algorithm string, keyWrapper KeyWrapper) *Encryptor
NewEncryptor creates a new Encryptor with the specified algorithm and key wrapper
func (*Encryptor) EncryptContent ¶
func (e *Encryptor) EncryptContent(elem *etree.Element) (*EncryptedData, error)
EncryptContent encrypts the content of an XML element
func (*Encryptor) EncryptElement ¶
func (e *Encryptor) EncryptElement(elem *etree.Element) (*EncryptedData, error)
EncryptElement encrypts an XML element and replaces it with EncryptedData
type HKDFParams ¶
type HKDFParams struct {
PRF string // PRF algorithm URI (e.g., HMAC-SHA256)
Salt []byte
Info []byte
KeyLength int // Output key length in bits
}
HKDFParams contains parameters for HKDF (RFC 5869)
func DefaultHKDFParams ¶
func DefaultHKDFParams(info []byte) *HKDFParams
DefaultHKDFParams returns default HKDF parameters for XML Encryption
type KeyDerivationMethod ¶
type KeyDerivationMethod struct {
Algorithm string // e.g., AlgorithmHKDF, AlgorithmConcatKDF
ConcatKDFParams *ConcatKDFParams
HKDFParams *HKDFParams
PBKDF2Params *PBKDF2Params
}
KeyDerivationMethod specifies how to derive the key encryption key
type KeyInfo ¶
type KeyInfo struct {
ID string
EncryptedKey *EncryptedKey
AgreementMethod *AgreementMethod
KeyName string
KeyValue *KeyValue
X509Data *X509Data
RetrievalMethod *RetrievalMethod
}
KeyInfo contains key identification information This is compatible with ds:KeyInfo from XML Signatures
type KeyUnwrapper ¶
type KeyUnwrapper interface {
// UnwrapKey unwraps a content encryption key from EncryptedKey
UnwrapKey(ek *EncryptedKey) ([]byte, error)
}
KeyUnwrapper interface for key unwrapping mechanisms
type KeyValue ¶
type KeyValue struct {
RSAKeyValue *RSAKeyValue
ECKeyValue *ECKeyValue
}
KeyValue contains a public key value
type KeyWrapper ¶
type KeyWrapper interface {
// WrapKey wraps a content encryption key
WrapKey(cek []byte, wrapAlgorithm string) (*EncryptedKey, error)
}
KeyWrapper interface for key wrapping mechanisms
type PBKDF2Params ¶
PBKDF2Params contains parameters for PBKDF2
type RSAKeyValue ¶
RSAKeyValue contains RSA public key parameters
type RetrievalMethod ¶
RetrievalMethod indicates where to retrieve key info
type Transform ¶
type Transform struct {
Algorithm string
}
Transform represents a transformation to be applied
type X25519KeyAgreement ¶
type X25519KeyAgreement struct {
// EphemeralPrivateKey is the sender's ephemeral private key (generated during Wrap)
EphemeralPrivateKey *ecdh.PrivateKey
// EphemeralPublicKey is the sender's ephemeral public key (included in OriginatorKeyInfo)
EphemeralPublicKey *ecdh.PublicKey
// RecipientPublicKey is the recipient's static public key
RecipientPublicKey *ecdh.PublicKey
// RecipientPrivateKey is for decryption (only set on recipient side)
RecipientPrivateKey *ecdh.PrivateKey
// HKDFParams contains the key derivation parameters
HKDFParams *HKDFParams
}
X25519KeyAgreement performs X25519 ECDH key agreement and key derivation as specified for XML Encryption with the HKDF key derivation function.
func NewX25519KeyAgreement ¶
func NewX25519KeyAgreement(recipientPublicKey *ecdh.PublicKey, hkdfParams *HKDFParams) (*X25519KeyAgreement, error)
NewX25519KeyAgreement creates a new X25519 key agreement instance for encryption. It generates a fresh ephemeral key pair and uses the provided recipient public key.
func NewX25519KeyAgreementForDecrypt ¶
func NewX25519KeyAgreementForDecrypt(recipientPrivateKey *ecdh.PrivateKey, ephemeralPublicKey *ecdh.PublicKey, hkdfParams *HKDFParams) *X25519KeyAgreement
NewX25519KeyAgreementForDecrypt creates a key agreement instance for decryption.
func (*X25519KeyAgreement) DeriveKeyEncryptionKey ¶
func (ka *X25519KeyAgreement) DeriveKeyEncryptionKey(keyLength int) ([]byte, error)
DeriveKeyEncryptionKey derives a key encryption key (KEK) using X25519 ECDH and HKDF. This is used to encrypt/decrypt the content encryption key.
func (*X25519KeyAgreement) UnwrapKey ¶
func (ka *X25519KeyAgreement) UnwrapKey(ek *EncryptedKey) ([]byte, error)
UnwrapKey unwraps a content encryption key from an EncryptedKey structure.
func (*X25519KeyAgreement) WrapKey ¶
func (ka *X25519KeyAgreement) WrapKey(cek []byte, wrapAlgorithm string) (*EncryptedKey, error)
WrapKey wraps a content encryption key (CEK) using X25519 key agreement. Returns the wrapped key and the EncryptedKey structure.