| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rand" |
| 9 |
"crypto/rsa" |
| 10 |
"crypto/x509" |
| 11 |
"encoding/base64" |
| 12 |
"encoding/json" |
| 13 |
|
| 14 |
"github.com/pkg/errors" |
| 15 |
"github.com/smallstep/cli/crypto/pemutil" |
| 16 |
"github.com/smallstep/cli/crypto/randutil" |
| 17 |
"github.com/smallstep/cli/errs" |
| 18 |
) |
| 19 |
|
| 20 |
const ( |
| 21 |
jwksUsageSig = "sig" |
| 22 |
jwksUsageEnc = "enc" |
| 23 |
|
| 24 |
defaultKeyType = EC |
| 25 |
|
| 26 |
defaultKeyCurve = P256 |
| 27 |
|
| 28 |
defaultKeyAlg = ES256 |
| 29 |
|
| 30 |
defaultKeySize = 0 |
| 31 |
) |
| 32 |
|
| 33 |
var ( |
| 34 |
errAmbiguousCertKeyUsage = errors.New("jose/generate: certificate's key usage is ambiguous, it should be for signature or encipherment, but not both (use --subtle to ignore usage field)") |
| 35 |
errNoCertKeyUsage = errors.New("jose/generate: certificate doesn't contain any key usage (use --subtle to ignore usage field)") |
| 36 |
) |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func GenerateDefaultKeyPair(pass []byte) (*JSONWebKey, *JSONWebEncryption, error) { |
| 41 |
if len(pass) == 0 { |
| 42 |
return nil, nil, errors.New("step-jose: password cannot be empty when encryptying a JWK") |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
jwk, err := GenerateJWK(defaultKeyType, defaultKeyCurve, defaultKeyAlg, jwksUsageSig, "", defaultKeySize) |
| 47 |
if err != nil { |
| 48 |
return nil, nil, err |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
hash, err := jwk.Thumbprint(crypto.SHA256) |
| 53 |
if err != nil { |
| 54 |
return nil, nil, errors.Wrap(err, "error generating JWK thumbprint") |
| 55 |
} |
| 56 |
jwk.KeyID = base64.RawURLEncoding.EncodeToString(hash) |
| 57 |
|
| 58 |
b, err := json.Marshal(jwk) |
| 59 |
if err != nil { |
| 60 |
return nil, nil, errors.Wrap(err, "error marshaling JWK") |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
salt, err := randutil.Salt(PBKDF2SaltSize) |
| 65 |
if err != nil { |
| 66 |
return nil, nil, err |
| 67 |
} |
| 68 |
recipient := Recipient{ |
| 69 |
Algorithm: PBES2_HS256_A128KW, |
| 70 |
Key: pass, |
| 71 |
PBES2Count: PBKDF2Iterations, |
| 72 |
PBES2Salt: salt, |
| 73 |
} |
| 74 |
|
| 75 |
opts := new(EncrypterOptions) |
| 76 |
opts.WithContentType(ContentType("jwk+json")) |
| 77 |
|
| 78 |
encrypter, err := NewEncrypter(DefaultEncAlgorithm, recipient, opts) |
| 79 |
if err != nil { |
| 80 |
return nil, nil, errs.Wrap(err, "error creating cipher") |
| 81 |
} |
| 82 |
|
| 83 |
jwe, err := encrypter.Encrypt(b) |
| 84 |
if err != nil { |
| 85 |
return nil, nil, errs.Wrap(err, "error encrypting data") |
| 86 |
} |
| 87 |
|
| 88 |
public := jwk.Public() |
| 89 |
return &public, jwe, nil |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func GenerateJWK(kty, crv, alg, use, kid string, size int) (jwk *JSONWebKey, err error) { |
| 95 |
switch kty { |
| 96 |
case "EC": |
| 97 |
return generateECKey(crv, alg, use, kid) |
| 98 |
case "RSA": |
| 99 |
return generateRSAKey(size, alg, use, kid) |
| 100 |
case "OKP": |
| 101 |
return generateOKPKey(crv, alg, use, kid) |
| 102 |
case "oct": |
| 103 |
return generateOctKey(size, alg, use, kid) |
| 104 |
default: |
| 105 |
return nil, errors.Errorf("missing or invalid value for flag '--kty'") |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
func GenerateJWKFromPEM(filename string, subtle bool) (*JSONWebKey, error) { |
| 112 |
key, err := pemutil.Read(filename) |
| 113 |
if err != nil { |
| 114 |
return nil, err |
| 115 |
} |
| 116 |
|
| 117 |
switch key := key.(type) { |
| 118 |
case *rsa.PrivateKey, *rsa.PublicKey: |
| 119 |
return &JSONWebKey{ |
| 120 |
Key: key, |
| 121 |
}, nil |
| 122 |
case *ecdsa.PrivateKey, *ecdsa.PublicKey, ed25519.PrivateKey, ed25519.PublicKey: |
| 123 |
return &JSONWebKey{ |
| 124 |
Key: key, |
| 125 |
Algorithm: algForKey(key), |
| 126 |
}, nil |
| 127 |
case *x509.Certificate: |
| 128 |
var use string |
| 129 |
if !subtle { |
| 130 |
use, err = keyUsageForCert(key) |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
} |
| 135 |
return &JSONWebKey{ |
| 136 |
Key: key.PublicKey, |
| 137 |
Certificates: []*x509.Certificate{key}, |
| 138 |
Algorithm: algForKey(key.PublicKey), |
| 139 |
Use: use, |
| 140 |
}, nil |
| 141 |
default: |
| 142 |
return nil, errors.Errorf("error parsing %s: unsupported key type '%T'", filename, key) |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
func algForKey(key crypto.PublicKey) string { |
| 147 |
switch key := key.(type) { |
| 148 |
case *ecdsa.PrivateKey: |
| 149 |
return getECAlgorithm(key.Curve) |
| 150 |
case *ecdsa.PublicKey: |
| 151 |
return getECAlgorithm(key.Curve) |
| 152 |
case ed25519.PrivateKey, ed25519.PublicKey: |
| 153 |
return EdDSA |
| 154 |
default: |
| 155 |
return "" |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
func keyUsageForCert(cert *x509.Certificate) (string, error) { |
| 160 |
isDigitalSignature := containsUsage(cert.KeyUsage, |
| 161 |
x509.KeyUsageDigitalSignature, |
| 162 |
x509.KeyUsageContentCommitment, |
| 163 |
x509.KeyUsageCertSign, |
| 164 |
x509.KeyUsageCRLSign, |
| 165 |
) |
| 166 |
isEncipherment := containsUsage(cert.KeyUsage, |
| 167 |
x509.KeyUsageKeyEncipherment, |
| 168 |
x509.KeyUsageDataEncipherment, |
| 169 |
x509.KeyUsageKeyAgreement, |
| 170 |
x509.KeyUsageEncipherOnly, |
| 171 |
x509.KeyUsageDecipherOnly, |
| 172 |
) |
| 173 |
if isDigitalSignature && isEncipherment { |
| 174 |
return "", errAmbiguousCertKeyUsage |
| 175 |
} |
| 176 |
if isDigitalSignature { |
| 177 |
return jwksUsageSig, nil |
| 178 |
} |
| 179 |
if isEncipherment { |
| 180 |
return jwksUsageEnc, nil |
| 181 |
} |
| 182 |
return "", errNoCertKeyUsage |
| 183 |
} |
| 184 |
|
| 185 |
func containsUsage(usage x509.KeyUsage, queries ...x509.KeyUsage) bool { |
| 186 |
for _, query := range queries { |
| 187 |
if usage&query == query { |
| 188 |
return true |
| 189 |
} |
| 190 |
} |
| 191 |
return false |
| 192 |
} |
| 193 |
|
| 194 |
func generateECKey(crv, alg, use, kid string) (*JSONWebKey, error) { |
| 195 |
var c elliptic.Curve |
| 196 |
var sigAlg string |
| 197 |
switch crv { |
| 198 |
case P256, "": |
| 199 |
c, sigAlg = elliptic.P256(), ES256 |
| 200 |
case P384: |
| 201 |
c, sigAlg = elliptic.P384(), ES384 |
| 202 |
case P521: |
| 203 |
c, sigAlg = elliptic.P521(), ES512 |
| 204 |
default: |
| 205 |
return nil, errors.Errorf("missing or invalid value for flag '--crv'") |
| 206 |
} |
| 207 |
|
| 208 |
key, err := ecdsa.GenerateKey(c, rand.Reader) |
| 209 |
if err != nil { |
| 210 |
return nil, errors.Wrap(err, "error generating ECDSA key") |
| 211 |
} |
| 212 |
|
| 213 |
switch use { |
| 214 |
case "enc": |
| 215 |
if alg == "" { |
| 216 |
alg = string(DefaultECKeyAlgorithm) |
| 217 |
} |
| 218 |
default: |
| 219 |
if alg == "" { |
| 220 |
alg = sigAlg |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
return &JSONWebKey{ |
| 225 |
Key: key, |
| 226 |
Algorithm: alg, |
| 227 |
Use: use, |
| 228 |
KeyID: kid, |
| 229 |
}, nil |
| 230 |
} |
| 231 |
|
| 232 |
func generateRSAKey(bits int, alg, use, kid string) (*JSONWebKey, error) { |
| 233 |
if bits == 0 { |
| 234 |
bits = DefaultRSASize |
| 235 |
} |
| 236 |
|
| 237 |
key, err := rsa.GenerateKey(rand.Reader, bits) |
| 238 |
if err != nil { |
| 239 |
return nil, errors.Wrap(err, "error generating RSA key") |
| 240 |
} |
| 241 |
|
| 242 |
switch use { |
| 243 |
case "enc": |
| 244 |
if alg == "" { |
| 245 |
alg = string(DefaultRSAKeyAlgorithm) |
| 246 |
} |
| 247 |
default: |
| 248 |
if alg == "" { |
| 249 |
alg = DefaultRSASigAlgorithm |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
return &JSONWebKey{ |
| 254 |
Key: key, |
| 255 |
Algorithm: alg, |
| 256 |
Use: use, |
| 257 |
KeyID: kid, |
| 258 |
}, nil |
| 259 |
} |
| 260 |
|
| 261 |
func generateOKPKey(crv, alg, use, kid string) (*JSONWebKey, error) { |
| 262 |
switch crv { |
| 263 |
case Ed25519, "": |
| 264 |
_, key, err := ed25519.GenerateKey(rand.Reader) |
| 265 |
if err != nil { |
| 266 |
return nil, errors.Wrap(err, "error generating Ed25519 key") |
| 267 |
} |
| 268 |
|
| 269 |
switch use { |
| 270 |
case "enc": |
| 271 |
return nil, errors.New("invalid algorithm: Ed25519 cannot be used for encryption") |
| 272 |
default: |
| 273 |
if alg == "" { |
| 274 |
alg = EdDSA |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
return &JSONWebKey{ |
| 279 |
Key: key, |
| 280 |
Algorithm: alg, |
| 281 |
Use: use, |
| 282 |
KeyID: kid, |
| 283 |
}, nil |
| 284 |
default: |
| 285 |
return nil, errors.Errorf("missing or invalid value for flag '--crv'") |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
func generateOctKey(size int, alg, use, kid string) (*JSONWebKey, error) { |
| 290 |
if size == 0 { |
| 291 |
size = DefaultOctSize |
| 292 |
} |
| 293 |
|
| 294 |
key, err := randutil.Alphanumeric(size) |
| 295 |
if err != nil { |
| 296 |
return nil, err |
| 297 |
} |
| 298 |
|
| 299 |
switch use { |
| 300 |
case "enc": |
| 301 |
if alg == "" { |
| 302 |
alg = string(DefaultOctKeyAlgorithm) |
| 303 |
} |
| 304 |
default: |
| 305 |
if alg == "" { |
| 306 |
alg = string(DefaultOctSigsAlgorithm) |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
return &JSONWebKey{ |
| 311 |
Key: []byte(key), |
| 312 |
Algorithm: alg, |
| 313 |
Use: use, |
| 314 |
KeyID: kid, |
| 315 |
}, nil |
| 316 |
} |