interface.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package jwe
  2. import (
  3. "github.com/lestrrat-go/iter/mapiter"
  4. "github.com/lestrrat-go/jwx/internal/iter"
  5. "github.com/lestrrat-go/jwx/jwa"
  6. "github.com/lestrrat-go/jwx/jwe/internal/keyenc"
  7. "github.com/lestrrat-go/jwx/jwe/internal/keygen"
  8. )
  9. // Recipient holds the encrypted key and hints to decrypt the key
  10. type Recipient interface {
  11. Headers() Headers
  12. EncryptedKey() []byte
  13. SetHeaders(Headers) error
  14. SetEncryptedKey([]byte) error
  15. }
  16. type stdRecipient struct {
  17. headers Headers
  18. encryptedKey []byte
  19. }
  20. // Message contains the entire encrypted JWE message. You should not
  21. // expect to use Message for anything other than inspecting the
  22. // state of an encrypted message. This is because encryption is
  23. // highly context sensitive, and once we parse the original payload
  24. // into an object, we may not always be able to recreate the exact
  25. // context in which the encryption happened.
  26. //
  27. // For example, it is totally valid for if the protected header's
  28. // integrity was calculated using a non-standard line breaks:
  29. //
  30. // {"a dummy":
  31. // "protected header"}
  32. //
  33. // Once parsed, though, we can only serialize the protected header as:
  34. //
  35. // {"a dummy":"protected header"}
  36. //
  37. // which would obviously result in a contradicting integrity value
  38. // if we tried to re-calculate it from a parsed message.
  39. //nolint:govet
  40. type Message struct {
  41. authenticatedData []byte
  42. cipherText []byte
  43. initializationVector []byte
  44. tag []byte
  45. recipients []Recipient
  46. protectedHeaders Headers
  47. unprotectedHeaders Headers
  48. // These two fields below are not available for the public consumers of this object.
  49. // rawProtectedHeaders stores the original protected header buffer
  50. rawProtectedHeaders []byte
  51. // storeProtectedHeaders is a hint to be used in UnmarshalJSON().
  52. // When this flag is true, UnmarshalJSON() will populate the
  53. // rawProtectedHeaders field
  54. storeProtectedHeaders bool
  55. }
  56. // contentEncrypter encrypts the content using the content using the
  57. // encrypted key
  58. type contentEncrypter interface {
  59. Algorithm() jwa.ContentEncryptionAlgorithm
  60. Encrypt([]byte, []byte, []byte) ([]byte, []byte, []byte, error)
  61. }
  62. //nolint:govet
  63. type encryptCtx struct {
  64. keyEncrypters []keyenc.Encrypter
  65. protected Headers
  66. contentEncrypter contentEncrypter
  67. generator keygen.Generator
  68. compress jwa.CompressionAlgorithm
  69. }
  70. // populater is an interface for things that may modify the
  71. // JWE header. e.g. ByteWithECPrivateKey
  72. type populater interface {
  73. Populate(keygen.Setter) error
  74. }
  75. type Visitor = iter.MapVisitor
  76. type VisitorFunc = iter.MapVisitorFunc
  77. type HeaderPair = mapiter.Pair
  78. type Iterator = mapiter.Iterator
  79. // PostParser is used in conjunction with jwe.WithPostParser().
  80. // This hook is called right after the JWE message has been parsed
  81. // but before the actual decryption takes place during `jwe.Decrypt()`.
  82. type PostParser interface {
  83. PostParse(DecryptCtx) error
  84. }
  85. // PostParseFunc is a PostParser that is represented by a single function
  86. type PostParseFunc func(DecryptCtx) error
  87. func (fn PostParseFunc) PostParse(ctx DecryptCtx) error {
  88. return fn(ctx)
  89. }