| 1 |
// Copyright The Notary Project Authors. |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package jws |
| 15 |
|
| 16 |
import ( |
| 17 |
"crypto" |
| 18 |
"crypto/x509" |
| 19 |
"encoding/base64" |
| 20 |
"errors" |
| 21 |
"fmt" |
| 22 |
|
| 23 |
"github.com/golang-jwt/jwt/v4" |
| 24 |
"github.com/notaryproject/notation-core-go/signature" |
| 25 |
) |
| 26 |
|
| 27 |
// signingMethod is the interface for jwt.SigingMethod with additional method to |
| 28 |
// access certificate chain after calling Sign(). |
| 29 |
type signingMethod interface { |
| 30 |
jwt.SigningMethod |
| 31 |
|
| 32 |
// CertificateChain returns the certificate chain. |
| 33 |
// |
| 34 |
// It should be called after calling Sign(). |
| 35 |
CertificateChain() ([]*x509.Certificate, error) |
| 36 |
|
| 37 |
// PrivateKey returns the private key. |
| 38 |
PrivateKey() crypto.PrivateKey |
| 39 |
} |
| 40 |
|
| 41 |
// remoteSigningMethod wraps the remote signer to be a SigningMethod. |
| 42 |
type remoteSigningMethod struct { |
| 43 |
signer signature.Signer |
| 44 |
certs []*x509.Certificate |
| 45 |
algorithm string |
| 46 |
} |
| 47 |
|
| 48 |
func newRemoteSigningMethod(signer signature.Signer) (signingMethod, error) { |
| 49 |
algorithm, err := extractJwtAlgorithm(signer) |
| 50 |
if err != nil { |
| 51 |
return nil, err |
| 52 |
} |
| 53 |
return &remoteSigningMethod{ |
| 54 |
signer: signer, |
| 55 |
algorithm: algorithm, |
| 56 |
}, nil |
| 57 |
} |
| 58 |
|
| 59 |
// Verify doesn't need to be implemented. |
| 60 |
func (s *remoteSigningMethod) Verify(signingString, signature string, key interface{}) error { |
| 61 |
return errors.New("not implemented") |
| 62 |
} |
| 63 |
|
| 64 |
// Sign hashes the signingString and call the remote signer to sign the digest. |
| 65 |
func (s *remoteSigningMethod) Sign(signingString string, key interface{}) (string, error) { |
| 66 |
// sign by external signer |
| 67 |
sig, certs, err := s.signer.Sign([]byte(signingString)) |
| 68 |
if err != nil { |
| 69 |
return "", err |
| 70 |
} |
| 71 |
s.certs = certs |
| 72 |
return base64.RawURLEncoding.EncodeToString(sig), nil |
| 73 |
} |
| 74 |
|
| 75 |
// Alg implements jwt.SigningMethod interface. |
| 76 |
func (s *remoteSigningMethod) Alg() string { |
| 77 |
return s.algorithm |
| 78 |
} |
| 79 |
|
| 80 |
// CertificateChain returns the certificate chain. |
| 81 |
// |
| 82 |
// It should be called after Sign(). |
| 83 |
func (s *remoteSigningMethod) CertificateChain() ([]*x509.Certificate, error) { |
| 84 |
if s.certs == nil { |
| 85 |
return nil, &signature.InvalidSignRequestError{Msg: "certificate chain is not set"} |
| 86 |
} |
| 87 |
return s.certs, nil |
| 88 |
} |
| 89 |
|
| 90 |
// PrivateKey returns nil for remote signer. |
| 91 |
func (s *remoteSigningMethod) PrivateKey() crypto.PrivateKey { |
| 92 |
return nil |
| 93 |
} |
| 94 |
|
| 95 |
// localSigningMethod wraps the local signer to be a SigningMethod. |
| 96 |
type localSigningMethod struct { |
| 97 |
jwt.SigningMethod |
| 98 |
signer signature.LocalSigner |
| 99 |
} |
| 100 |
|
| 101 |
func newLocalSigningMethod(signer signature.LocalSigner) (signingMethod, error) { |
| 102 |
alg, err := extractJwtAlgorithm(signer) |
| 103 |
if err != nil { |
| 104 |
return nil, err |
| 105 |
} |
| 106 |
|
| 107 |
return &localSigningMethod{ |
| 108 |
SigningMethod: jwt.GetSigningMethod(alg), |
| 109 |
signer: signer, |
| 110 |
}, nil |
| 111 |
} |
| 112 |
|
| 113 |
// CertificateChain returns the certificate chain. |
| 114 |
func (s *localSigningMethod) CertificateChain() ([]*x509.Certificate, error) { |
| 115 |
return s.signer.CertificateChain() |
| 116 |
} |
| 117 |
|
| 118 |
// PrivateKey returns the private key. |
| 119 |
func (s *localSigningMethod) PrivateKey() crypto.PrivateKey { |
| 120 |
return s.signer.PrivateKey() |
| 121 |
} |
| 122 |
|
| 123 |
// getSigningMethod return signingMethod for the given signer. |
| 124 |
func getSigningMethod(signer signature.Signer) (signingMethod, error) { |
| 125 |
if localSigner, ok := signer.(signature.LocalSigner); ok { |
| 126 |
// for local signer |
| 127 |
return newLocalSigningMethod(localSigner) |
| 128 |
} |
| 129 |
// for remote signer |
| 130 |
return newRemoteSigningMethod(signer) |
| 131 |
} |
| 132 |
|
| 133 |
// verifyJWT verifies the JWT token against the specified verification key. |
| 134 |
func verifyJWT(tokenString string, publicKey interface{}) error { |
| 135 |
parser := jwt.NewParser( |
| 136 |
jwt.WithValidMethods(validMethods), |
| 137 |
jwt.WithJSONNumber(), |
| 138 |
jwt.WithoutClaimsValidation(), |
| 139 |
) |
| 140 |
|
| 141 |
if _, err := parser.Parse(tokenString, func(t *jwt.Token) (interface{}, error) { |
| 142 |
return publicKey, nil |
| 143 |
}); err != nil { |
| 144 |
return &signature.SignatureIntegrityError{Err: err} |
| 145 |
} |
| 146 |
return nil |
| 147 |
} |
| 148 |
|
| 149 |
func extractJwtAlgorithm(signer signature.Signer) (string, error) { |
| 150 |
// extract algorithm from signer |
| 151 |
keySpec, err := signer.KeySpec() |
| 152 |
if err != nil { |
| 153 |
return "", err |
| 154 |
} |
| 155 |
alg := keySpec.SignatureAlgorithm() |
| 156 |
|
| 157 |
// converts the signature.Algorithm to be jwt package defined |
| 158 |
// algorithm name. |
| 159 |
jwsAlg, ok := signatureAlgJWSAlgMap[alg] |
| 160 |
if !ok { |
| 161 |
return "", &signature.UnsupportedSignatureAlgoError{ |
| 162 |
Alg: fmt.Sprintf("#%d", alg)} |
| 163 |
} |
| 164 |
return jwsAlg, nil |
| 165 |
} |