Documentation
¶
Overview ¶
Package xmlenc is a partial implementation of the xmlenc standard as described in https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html. The purpose of this implementation is to support encrypted SAML assertions.
Index ¶
- Variables
- func Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error)
- func RegisterDecrypter(d Decrypter)
- func RegisterDigestMethod(dm DigestMethod)
- func UnregisterDecrypter(algorithm string)
- type BlockCipher
- type CBC
- type Decrypter
- type DigestMethod
- type Encrypter
- type ErrAlgorithmNotImplemented
- type ErrCannotFindRequiredElement
- type ErrIncorrectKeyLength
- type ErrIncorrectKeyType
- type ErrKeyAlgorithmNotAllowed
- type GCM
- type RSA
Constants ¶
This section is empty.
Variables ¶
var ( // SHA1 implements the SHA-1 digest method (which is considered insecure) SHA1 = digestMethod{ // contains filtered or unexported fields } // SHA256 implements the SHA-256 digest method. The algorithm URI is the one // assigned by RFC 4051 section 2.3.2, which lives in the XML Encryption // namespace rather than the XML Signature one. SHA256 = digestMethod{ // contains filtered or unexported fields } // SHA384 implements the SHA-384 digest method. RFC 4051 section 2.1.3 // assigns it a URI in the XML Signature "more" namespace; XML Encryption // does not define one of its own. SHA384 = digestMethod{ // contains filtered or unexported fields } // SHA512 implements the SHA-512 digest method (RFC 4051 section 2.3.4). SHA512 = digestMethod{ // contains filtered or unexported fields } // RIPEMD160 implements the RIPEMD160 digest method (RFC 4051 section 2.3.5). RIPEMD160 = digestMethod{ // contains filtered or unexported fields } )
var ErrDecryptionFailed = errors.New("xmlenc: decryption failed")
ErrDecryptionFailed is returned for a ciphertext whose decrypted bytes are not valid: in CBC mode, when the padding is not. It carries no detail, so that a caller that also rejects plaintext it cannot parse (as the saml package does) can report that with the same error, and a sender of modified ciphertexts cannot tell the two failures apart from the error.
CBC without an integrity check remains malleable whatever the padding check: the XML Encryption attacks of Jager and Somorovsky (2011) need only learn whether a modified ciphertext decrypts to well-formed XML. Use AES-GCM where the sender supports it.
var ErrIncorrectTag = fmt.Errorf("tag must be an EncryptedType or EncryptedKey")
ErrIncorrectTag is returned when Decrypt is passed an element which is neither an EncryptedType nor an EncryptedKey
var RandReader = rand.Reader
RandReader is a thunk that allows test to replace the source of randomness used by this package. By default it is Reader from crypto/rand.
Functions ¶
func Decrypt ¶
Decrypt decrypts the encrypted data using the provided key. If the data are encrypted using AES, then the key should be a []byte. If the data are encrypted with RSA-OAEP-MGF1P or RSA-OAEP then key should be a *rsa.PrivateKey.
The algorithm is the one the EncryptionMethod of ciphertextEl names, among those registered with RegisterDecrypter. For an xenc:EncryptedKey it must not be a block cipher (a Decrypter that is also a BlockCipher), so that an EncryptedKey is never followed into another EncryptedKey: see ErrKeyAlgorithmNotAllowed.
func RegisterDecrypter ¶
func RegisterDecrypter(d Decrypter)
RegisterDecrypter registers the specified decrypter so that it can be used with Decrypt(), replacing any decrypter registered for the same algorithm. It is safe to call concurrently with Decrypt.
func RegisterDigestMethod ¶
func RegisterDigestMethod(dm DigestMethod)
RegisterDigestMethod registers the specified digest method so that it can be used with Decrypt(). It is safe to call concurrently with Decrypt.
func UnregisterDecrypter ¶ added in v0.6.3
func UnregisterDecrypter(algorithm string)
UnregisterDecrypter removes the decrypter registered for algorithm, an EncryptionMethod URI, so that Decrypt returns ErrAlgorithmNotImplemented for documents that name it. It does nothing if no decrypter is registered for algorithm. It is safe to call concurrently with Decrypt.
The algorithm Decrypt uses is the one the document names, so a sender can relabel AES-GCM ciphertext as AES-CBC of the same key size and have it decrypted in CBC mode, which has no integrity check. An application whose senders all use AES-GCM can refuse CBC altogether:
xmlenc.UnregisterDecrypter(xmlenc.AES128CBC.Algorithm()) xmlenc.UnregisterDecrypter(xmlenc.AES192CBC.Algorithm()) xmlenc.UnregisterDecrypter(xmlenc.AES256CBC.Algorithm())
Types ¶
type BlockCipher ¶
BlockCipher implements a cipher with a fixed size key like AES or 3DES.
var ( // AES128CBC implements AES128-CBC symetric key mode for encryption and decryption AES128CBC BlockCipher = CBC{ // contains filtered or unexported fields } // AES192CBC implements AES192-CBC symetric key mode for encryption and decryption AES192CBC BlockCipher = CBC{ // contains filtered or unexported fields } // AES256CBC implements AES256-CBC symetric key mode for encryption and decryption AES256CBC BlockCipher = CBC{ // contains filtered or unexported fields } // TripleDES implements 3DES (DES-EDE3) in CBC mode for encryption and // decryption, with a 24 byte key as XML Encryption section 5.2.1 requires. // 3DES is a legacy cipher with a 64-bit block, so it is not registered as a // decrypter: a document naming tripledes-cbc gets ErrAlgorithmNotImplemented // unless the application opts in with RegisterDecrypter(TripleDES). TripleDES BlockCipher = CBC{ // contains filtered or unexported fields } )
var ( // AES128GCM implements AES128-GCM mode for encryption and decryption AES128GCM BlockCipher = GCM{ // contains filtered or unexported fields } // AES192GCM implements AES192-GCM mode for encryption and decryption AES192GCM BlockCipher = GCM{ // contains filtered or unexported fields } // AES256GCM implements AES256-GCM mode for encryption and decryption AES256GCM BlockCipher = GCM{ // contains filtered or unexported fields } )
type CBC ¶
type CBC struct {
// contains filtered or unexported fields
}
CBC implements Decrypter and Encrypter for block ciphers in CBC mode
func (CBC) Algorithm ¶
Algorithm returns the name of the algorithm, as will be found in an xenc:EncryptionMethod element.
func (CBC) Decrypt ¶
Decrypt decrypts an encrypted element with key. If the ciphertext contains an EncryptedKey element, then the type of `key` is determined by the registered Decryptor for the EncryptedKey element. Otherwise, `key` must be a []byte of length KeySize().
type Decrypter ¶
type Decrypter interface {
Algorithm() string
Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error)
}
Decrypter is an interface that decrypts things. The Decrypt() method returns the plaintext version of the EncryptedData or EncryptedKey element passed.
You probably don't have to use this interface directly, instead you may call Decrypt() and it will examine the element to determine which Decrypter to use.
type DigestMethod ¶
DigestMethod represents a digest method such as SHA1, etc.
type Encrypter ¶
type Encrypter interface {
Encrypt(key interface{}, plaintext []byte, nonce []byte) (*etree.Element, error)
}
Encrypter is an interface that encrypts things. Given a plaintext it returns an XML EncryptedData or EncryptedKey element. The required type of `key` varies depending on the implementation.
nonce should be nil, so that the implementation generates any nonce or IV it needs from RandReader. Implementations that use a nonce (GCM) accept a caller-supplied one only for reproducible tests: see GCM.Encrypt.
type ErrAlgorithmNotImplemented ¶
type ErrAlgorithmNotImplemented string
ErrAlgorithmNotImplemented is returned when encryption used is not supported.
func (ErrAlgorithmNotImplemented) Error ¶
func (e ErrAlgorithmNotImplemented) Error() string
type ErrCannotFindRequiredElement ¶
type ErrCannotFindRequiredElement string
ErrCannotFindRequiredElement is returned by Decrypt when a required element cannot be found.
func (ErrCannotFindRequiredElement) Error ¶
func (e ErrCannotFindRequiredElement) Error() string
type ErrIncorrectKeyLength ¶
type ErrIncorrectKeyLength int
ErrIncorrectKeyLength is returned when the fixed length key is not of the required length.
func (ErrIncorrectKeyLength) Error ¶
func (e ErrIncorrectKeyLength) Error() string
type ErrIncorrectKeyType ¶
type ErrIncorrectKeyType string
ErrIncorrectKeyType is returned when the key is not the correct type
func (ErrIncorrectKeyType) Error ¶
func (e ErrIncorrectKeyType) Error() string
type ErrKeyAlgorithmNotAllowed ¶ added in v0.6.3
type ErrKeyAlgorithmNotAllowed string
ErrKeyAlgorithmNotAllowed is returned by Decrypt for an xenc:EncryptedKey whose EncryptionMethod names a block cipher such as aes128-cbc or aes256-gcm. An EncryptedKey carries a content key, which must be transported with a key-transport algorithm (RSA-OAEP) or wrapped with a key-wrap algorithm.
Accepting a block cipher there would let a sender nest a captured, legitimately wrapped content key inside an EncryptedKey of its own and have the recipient decrypt ciphertext of the sender's choosing under that key: a CBC padding oracle that the uniform errors of the data decryption do not cover.
func (ErrKeyAlgorithmNotAllowed) Error ¶ added in v0.6.3
func (e ErrKeyAlgorithmNotAllowed) Error() string
type GCM ¶ added in v0.4.6
type GCM struct {
// contains filtered or unexported fields
}
GCM implements Decrypter and Encrypter for block ciphers in struct mode
func (GCM) Algorithm ¶ added in v0.4.6
Algorithm returns the name of the algorithm, as will be found in an xenc:EncryptionMethod element.
func (GCM) Decrypt ¶ added in v0.4.6
Decrypt decrypts an encrypted element with key. If the ciphertext contains an EncryptedKey element, then the type of `key` is determined by the registered Decryptor for the EncryptedKey element. Otherwise, `key` must be a []byte of length KeySize().
func (GCM) Encrypt ¶ added in v0.4.6
Encrypt encrypts plaintext with key, which should be a []byte of length KeySize(), and returns an xenc:EncryptedData element.
Pass a nil nonce, and Encrypt generates a random one from RandReader, which is what callers should do. A non-nil nonce must be NonceSize (12) bytes and must never be used again with the same key: reusing a GCM nonce reveals the XOR of the two plaintexts and lets anyone forge messages under that key. It is accepted only for reproducible test vectors.
type RSA ¶
type RSA struct {
BlockCipher BlockCipher
DigestMethod DigestMethod // only for OAEP
// contains filtered or unexported fields
}
RSA implements Encrypter and Decrypter using RSA public key encryption.
Use function like OAEP(), or PKCS1v15() to get an instance of this type ready to use.
func OAEP ¶
func OAEP() RSA
OAEP returns a version of RSA that implements RSA in OAEP-MGF1P mode. By default the block cipher used is AES-256 CBC and the digest method is SHA-256. You can specify other ciphers and digest methods by assigning to BlockCipher or DigestMethod. As the algorithm name says, the mask generation function is always MGF1 with SHA-1, whatever the digest method.
OAEP implements the older RSA-OAEP (2001 spec) for backward compatibility, you might prefer OAEP_SHA256 over using this method.
AES-CBC has no integrity check, so a recipient cannot detect a modified ciphertext (see ErrDecryptionFailed). Assign AES128GCM or AES256GCM to BlockCipher when the recipient supports AES-GCM, as XML Encryption 1.1 recipients do. The CBC default is kept for compatibility with recipients that support only XML Encryption 1.0.
func OAEP_SHA256 ¶ added in v0.5.0
func OAEP_SHA256() RSA
OAEP_SHA256 returns a version of RSA that implements RSA in OAEP mode (XML Encryption 1.1). By default the block cipher used is AES-256 CBC and the digest method is SHA-256. You can specify other ciphers and digest methods by assigning to BlockCipher or DigestMethod. The mask generation function is MGF1 with the same hash as the digest method, and is stated in the output.
AES-CBC has no integrity check, so a recipient cannot detect a modified ciphertext (see ErrDecryptionFailed). Assign AES128GCM or AES256GCM to BlockCipher when the recipient supports AES-GCM, as XML Encryption 1.1 recipients do. The CBC default is kept for compatibility with recipients that support only XML Encryption 1.0.
func OAEP_SHA384 ¶ added in v0.6.0
func OAEP_SHA384() RSA
OAEP_SHA384 returns a version of RSA that implements RSA in OAEP mode (XML Encryption 1.1). By default the block cipher used is AES-256 CBC and the digest method is SHA-384. You can specify other ciphers and digest methods by assigning to BlockCipher or DigestMethod. The mask generation function is MGF1 with the same hash as the digest method, and is stated in the output.
AES-CBC has no integrity check, so a recipient cannot detect a modified ciphertext (see ErrDecryptionFailed). Assign AES128GCM or AES256GCM to BlockCipher when the recipient supports AES-GCM, as XML Encryption 1.1 recipients do. The CBC default is kept for compatibility with recipients that support only XML Encryption 1.0.
func OAEP_SHA512 ¶ added in v0.5.0
func OAEP_SHA512() RSA
OAEP_SHA512 returns a version of RSA that implements RSA in OAEP mode (XML Encryption 1.1). By default the block cipher used is AES-256 CBC and the digest method is SHA-512. You can specify other ciphers and digest methods by assigning to BlockCipher or DigestMethod. The mask generation function is MGF1 with the same hash as the digest method, and is stated in the output.
AES-CBC has no integrity check, so a recipient cannot detect a modified ciphertext (see ErrDecryptionFailed). Assign AES128GCM or AES256GCM to BlockCipher when the recipient supports AES-GCM, as XML Encryption 1.1 recipients do. The CBC default is kept for compatibility with recipients that support only XML Encryption 1.0.
func PKCS1v15 ¶
func PKCS1v15() RSA
PKCS1v15 returns a version of RSA that implements RSA in PKCS1v15 mode. By default the block cipher used is AES-256 CBC. The DigestMethod field is ignored because PKCS1v15 does not use a digest function.
AES-CBC has no integrity check, so a recipient cannot detect a modified ciphertext (see ErrDecryptionFailed). Assign AES128GCM or AES256GCM to BlockCipher when the recipient supports AES-GCM, as XML Encryption 1.1 recipients do. The CBC default is kept for compatibility with recipients that support only XML Encryption 1.0.