opaque.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. }
  74. // OpaqueKeyEncrypter is an interface that supports encrypting keys with an opaque key.
  75. type OpaqueKeyEncrypter interface {
  76. // KeyID returns the kid
  77. KeyID() string
  78. // Algs returns a list of supported key encryption algorithms.
  79. Algs() []KeyAlgorithm
  80. // encryptKey encrypts the CEK using the given algorithm.
  81. encryptKey(cek []byte, alg KeyAlgorithm) (recipientInfo, error)
  82. }
  83. type opaqueKeyEncrypter struct {
  84. encrypter OpaqueKeyEncrypter
  85. }
  86. func newOpaqueKeyEncrypter(alg KeyAlgorithm, encrypter OpaqueKeyEncrypter) (recipientKeyInfo, error) {
  87. var algSupported bool
  88. for _, salg := range encrypter.Algs() {
  89. if alg == salg {
  90. algSupported = true
  91. break
  92. }
  93. }
  94. if !algSupported {
  95. return recipientKeyInfo{}, ErrUnsupportedAlgorithm
  96. }
  97. return recipientKeyInfo{
  98. keyID: encrypter.KeyID(),
  99. keyAlg: alg,
  100. keyEncrypter: &opaqueKeyEncrypter{
  101. encrypter: encrypter,
  102. },
  103. }, nil
  104. }
  105. func (oke *opaqueKeyEncrypter) encryptKey(cek []byte, alg KeyAlgorithm) (recipientInfo, error) {
  106. return oke.encrypter.encryptKey(cek, alg)
  107. }
  108. //OpaqueKeyDecrypter is an interface that supports decrypting keys with an opaque key.
  109. type OpaqueKeyDecrypter interface {
  110. DecryptKey(encryptedKey []byte, header Header) ([]byte, error)
  111. }
  112. type opaqueKeyDecrypter struct {
  113. decrypter OpaqueKeyDecrypter
  114. }
  115. func (okd *opaqueKeyDecrypter) decryptKey(headers rawHeader, recipient *recipientInfo, generator keyGenerator) ([]byte, error) {
  116. mergedHeaders := rawHeader{}
  117. mergedHeaders.merge(&headers)
  118. mergedHeaders.merge(recipient.header)
  119. header, err := mergedHeaders.sanitized()
  120. if err != nil {
  121. return nil, err
  122. }
  123. return okd.decrypter.DecryptKey(recipient.encryptedKey, header)
  124. }