opaque.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /*-
  2. * Copyright 2018 Square Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package jose
  17. // OpaqueSigner is an interface that supports signing payloads with opaque
  18. // private key(s). Private key operations preformed by implementors may, for
  19. // example, occur in a hardware module. An OpaqueSigner may rotate signing keys
  20. // transparently to the user of this interface.
  21. type OpaqueSigner interface {
  22. // Public returns the public key of the current signing key.
  23. Public() *JSONWebKey
  24. // Algs returns a list of supported signing algorithms.
  25. Algs() []SignatureAlgorithm
  26. // SignPayload signs a payload with the current signing key using the given
  27. // algorithm.
  28. SignPayload(payload []byte, alg SignatureAlgorithm) ([]byte, error)
  29. }
  30. type opaqueSigner struct {
  31. signer OpaqueSigner
  32. }
  33. func newOpaqueSigner(alg SignatureAlgorithm, signer OpaqueSigner) (recipientSigInfo, error) {
  34. var algSupported bool
  35. for _, salg := range signer.Algs() {
  36. if alg == salg {
  37. algSupported = true
  38. break
  39. }
  40. }
  41. if !algSupported {
  42. return recipientSigInfo{}, ErrUnsupportedAlgorithm
  43. }
  44. return recipientSigInfo{
  45. sigAlg: alg,
  46. publicKey: signer.Public,
  47. signer: &opaqueSigner{
  48. signer: signer,
  49. },
  50. }, nil
  51. }
  52. func (o *opaqueSigner) signPayload(payload []byte, alg SignatureAlgorithm) (Signature, error) {
  53. out, err := o.signer.SignPayload(payload, alg)
  54. if err != nil {
  55. return Signature{}, err
  56. }
  57. return Signature{
  58. Signature: out,
  59. protected: &rawHeader{},
  60. }, nil
  61. }
  62. // OpaqueVerifier is an interface that supports verifying payloads with opaque
  63. // public key(s). An OpaqueSigner may rotate signing keys transparently to the
  64. // user of this interface.
  65. type OpaqueVerifier interface {
  66. VerifyPayload(payload []byte, signature []byte, alg SignatureAlgorithm) error
  67. }
  68. type opaqueVerifier struct {
  69. verifier OpaqueVerifier
  70. }
  71. func (o *opaqueVerifier) verifyPayload(payload []byte, signature []byte, alg SignatureAlgorithm) error {
  72. return o.verifier.VerifyPayload(payload, signature, alg)
  73. }