| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/ed25519" |
| 6 |
"crypto/rsa" |
| 7 |
"crypto/sha1" |
| 8 |
"crypto/x509" |
| 9 |
"encoding/base64" |
| 10 |
"fmt" |
| 11 |
"io/ioutil" |
| 12 |
|
| 13 |
"github.com/pkg/errors" |
| 14 |
"github.com/smallstep/cli/crypto/keys" |
| 15 |
"github.com/smallstep/cli/crypto/pemutil" |
| 16 |
"golang.org/x/crypto/ssh" |
| 17 |
) |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
func ValidateSSHPOP(certFile string, key interface{}) (string, error) { |
| 22 |
if certFile == "" { |
| 23 |
return "", errors.New("ssh certfile cannot be empty") |
| 24 |
} |
| 25 |
certBytes, err := ioutil.ReadFile(certFile) |
| 26 |
if err != nil { |
| 27 |
return "", errors.Wrapf(err, "error reading ssh certificate from %s", certFile) |
| 28 |
} |
| 29 |
sshpub, _, _, _, err := ssh.ParseAuthorizedKey(certBytes) |
| 30 |
if err != nil { |
| 31 |
return "", errors.Wrapf(err, "error parsing ssh public key from %s", certFile) |
| 32 |
} |
| 33 |
cert, ok := sshpub.(*ssh.Certificate) |
| 34 |
if !ok { |
| 35 |
return "", errors.New("error casting ssh public key to ssh certificate") |
| 36 |
} |
| 37 |
pubkey, err := keys.ExtractKey(cert) |
| 38 |
if err != nil { |
| 39 |
return "", errors.Wrap(err, "error extracting public key from ssh public key interface") |
| 40 |
} |
| 41 |
if err = keys.VerifyPair(pubkey, key); err != nil { |
| 42 |
return "", errors.Wrap(err, "error verifying ssh key pair") |
| 43 |
} |
| 44 |
|
| 45 |
return base64.StdEncoding.EncodeToString(cert.Marshal()), nil |
| 46 |
} |
| 47 |
|
| 48 |
func validateX5(certFile string, key interface{}) ([]*x509.Certificate, error) { |
| 49 |
if certFile == "" { |
| 50 |
return nil, errors.New("certfile cannot be empty") |
| 51 |
} |
| 52 |
certs, err := pemutil.ReadCertificateBundle(certFile) |
| 53 |
if err != nil { |
| 54 |
return nil, errors.Wrap(err, "error reading certificate chain from file") |
| 55 |
} |
| 56 |
|
| 57 |
if err = keys.VerifyPair(certs[0].PublicKey, key); err != nil { |
| 58 |
return nil, errors.Wrap(err, "error verifying certificate and key") |
| 59 |
} |
| 60 |
|
| 61 |
if certs[0].KeyUsage&x509.KeyUsageDigitalSignature == 0 { |
| 62 |
return nil, errors.New("certificate/private-key pair used to sign " + |
| 63 |
"token is not approved for digital signature") |
| 64 |
} |
| 65 |
return certs, nil |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
func ValidateX5C(certFile string, key interface{}) ([]string, error) { |
| 71 |
certs, err := validateX5(certFile, key) |
| 72 |
if err != nil { |
| 73 |
return nil, errors.Wrap(err, "ValidateX5C") |
| 74 |
} |
| 75 |
strs := make([]string, len(certs)) |
| 76 |
for i, cert := range certs { |
| 77 |
strs[i] = base64.StdEncoding.EncodeToString(cert.Raw) |
| 78 |
} |
| 79 |
return strs, nil |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
func ValidateX5T(certFile string, key interface{}) (string, error) { |
| 85 |
certs, err := validateX5(certFile, key) |
| 86 |
if err != nil { |
| 87 |
return "", errors.Wrap(err, "ValidateX5T") |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
fingerprint := sha1.Sum(certs[0].Raw) |
| 92 |
return base64.URLEncoding.EncodeToString(fingerprint[:]), nil |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
func ValidateJWK(jwk *JSONWebKey) error { |
| 97 |
switch jwk.Use { |
| 98 |
case "sig": |
| 99 |
return validateSigJWK(jwk) |
| 100 |
case "enc": |
| 101 |
return validateEncJWK(jwk) |
| 102 |
default: |
| 103 |
return validateGeneric(jwk) |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
func validateSigJWK(jwk *JSONWebKey) error { |
| 109 |
if jwk.Algorithm == "" { |
| 110 |
return errors.New("flag '--alg' is required with the given key") |
| 111 |
} |
| 112 |
errctx := "the given key" |
| 113 |
|
| 114 |
switch k := jwk.Key.(type) { |
| 115 |
case []byte: |
| 116 |
switch jwk.Algorithm { |
| 117 |
case HS256, HS384, HS512: |
| 118 |
return nil |
| 119 |
} |
| 120 |
errctx = "kty 'oct'" |
| 121 |
case *rsa.PrivateKey, *rsa.PublicKey: |
| 122 |
switch jwk.Algorithm { |
| 123 |
case RS256, RS384, RS512: |
| 124 |
return nil |
| 125 |
case PS256, PS384, PS512: |
| 126 |
return nil |
| 127 |
} |
| 128 |
errctx = "kty 'RSA'" |
| 129 |
case *ecdsa.PrivateKey: |
| 130 |
curve := k.Params().Name |
| 131 |
switch { |
| 132 |
case jwk.Algorithm == ES256 && curve == P256: |
| 133 |
return nil |
| 134 |
case jwk.Algorithm == ES384 && curve == P384: |
| 135 |
return nil |
| 136 |
case jwk.Algorithm == ES512 && curve == P521: |
| 137 |
return nil |
| 138 |
} |
| 139 |
errctx = fmt.Sprintf("kty 'EC' and crv '%s'", curve) |
| 140 |
case *ecdsa.PublicKey: |
| 141 |
curve := k.Params().Name |
| 142 |
switch { |
| 143 |
case jwk.Algorithm == ES256 && curve == P256: |
| 144 |
return nil |
| 145 |
case jwk.Algorithm == ES384 && curve == P384: |
| 146 |
return nil |
| 147 |
case jwk.Algorithm == ES512 && curve == P521: |
| 148 |
return nil |
| 149 |
} |
| 150 |
errctx = fmt.Sprintf("kty 'EC' and crv '%s'", curve) |
| 151 |
case ed25519.PrivateKey, ed25519.PublicKey: |
| 152 |
if jwk.Algorithm == EdDSA { |
| 153 |
return nil |
| 154 |
} |
| 155 |
errctx = "kty 'OKP' and crv 'Ed25519'" |
| 156 |
} |
| 157 |
|
| 158 |
return errors.Errorf("alg '%s' is not compatible with %s", jwk.Algorithm, errctx) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func validateEncJWK(jwk *JSONWebKey) error { |
| 163 |
alg := KeyAlgorithm(jwk.Algorithm) |
| 164 |
var kty string |
| 165 |
|
| 166 |
switch jwk.Key.(type) { |
| 167 |
case []byte: |
| 168 |
switch alg { |
| 169 |
case DIRECT, A128GCMKW, A192GCMKW, A256GCMKW, A128KW, A192KW, A256KW: |
| 170 |
return nil |
| 171 |
} |
| 172 |
kty = "oct" |
| 173 |
case *rsa.PrivateKey, *rsa.PublicKey: |
| 174 |
switch alg { |
| 175 |
case RSA1_5, RSA_OAEP, RSA_OAEP_256: |
| 176 |
return nil |
| 177 |
} |
| 178 |
kty = "RSA" |
| 179 |
case *ecdsa.PrivateKey, *ecdsa.PublicKey: |
| 180 |
switch alg { |
| 181 |
case ECDH_ES, ECDH_ES_A128KW, ECDH_ES_A192KW, ECDH_ES_A256KW: |
| 182 |
return nil |
| 183 |
} |
| 184 |
kty = "EC" |
| 185 |
case ed25519.PrivateKey, ed25519.PublicKey: |
| 186 |
return errors.New("key Ed25519 cannot be used for encryption") |
| 187 |
} |
| 188 |
|
| 189 |
return errors.Errorf("alg '%s' is not compatible with kty '%s'", jwk.Algorithm, kty) |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func validateGeneric(jwk *JSONWebKey) error { |
| 194 |
switch jwk.Key.(type) { |
| 195 |
case []byte: |
| 196 |
return nil |
| 197 |
case *rsa.PrivateKey, *rsa.PublicKey: |
| 198 |
return nil |
| 199 |
case *ecdsa.PrivateKey, *ecdsa.PublicKey: |
| 200 |
return nil |
| 201 |
case ed25519.PrivateKey, ed25519.PublicKey: |
| 202 |
return nil |
| 203 |
} |
| 204 |
|
| 205 |
return errors.Errorf("unsupported key type '%T'", jwk.Key) |
| 206 |
} |