xmlenc.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Package xmlenc is a partial implementation of the xmlenc standard
  2. // as described in https://www.w3.org/TR/2002/REC-xmlenc-core-20021210/Overview.html.
  3. // The purpose of this implementation is to support encrypted SAML assertions.
  4. package xmlenc
  5. import (
  6. "crypto/rand"
  7. "hash"
  8. "io"
  9. "github.com/beevik/etree"
  10. )
  11. // RandReader is a thunk that allows test to replace the source of randomness used by
  12. // this package. By default it is Reader from crypto/rand.
  13. var RandReader io.Reader = rand.Reader
  14. // Encrypter is an interface that encrypts things. Given a plaintext it returns an
  15. // XML EncryptedData or EncryptedKey element. The required type of `key` varies
  16. // depending on the implementation.
  17. type Encrypter interface {
  18. Encrypt(key interface{}, plaintext []byte) (*etree.Element, error)
  19. }
  20. // Decrypter is an interface that decrypts things. The Decrypt() method returns the
  21. // plaintext version of the EncryptedData or EncryptedKey element passed.
  22. //
  23. // You probably don't have to use this interface directly, instead you may call
  24. // Decrypt() and it will examine the element to determine which Decrypter to use.
  25. type Decrypter interface {
  26. Algorithm() string
  27. Decrypt(key interface{}, ciphertextEl *etree.Element) ([]byte, error)
  28. }
  29. // DigestMethod represents a digest method such as SHA1, etc.
  30. type DigestMethod interface {
  31. Algorithm() string
  32. Hash() hash.Hash
  33. }
  34. var (
  35. decrypters = map[string]Decrypter{}
  36. digestMethods = map[string]DigestMethod{}
  37. )
  38. // RegisterDecrypter registers the specified decrypter to that it can be
  39. // used with Decrypt().
  40. func RegisterDecrypter(d Decrypter) {
  41. decrypters[d.Algorithm()] = d
  42. }
  43. // RegisterDigestMethod registers the specified digest method to that it can be
  44. // used with Decrypt().
  45. func RegisterDigestMethod(dm DigestMethod) {
  46. digestMethods[dm.Algorithm()] = dm
  47. }
  48. // BlockCipher implements a cipher with a fixed size key like AES or 3DES.
  49. type BlockCipher interface {
  50. Encrypter
  51. Decrypter
  52. KeySize() int
  53. }