| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto" |
| 5 |
"crypto/ecdsa" |
| 6 |
"crypto/ed25519" |
| 7 |
"crypto/elliptic" |
| 8 |
"crypto/rand" |
| 9 |
"crypto/rsa" |
| 10 |
"encoding/base64" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/smallstep/assert" |
| 15 |
) |
| 16 |
|
| 17 |
const ( |
| 18 |
ecdsaPublicKey keyType = iota |
| 19 |
ecdsaPrivateKey |
| 20 |
ed25519PublicKey |
| 21 |
ed25519PrivateKey |
| 22 |
rsaPublicKey |
| 23 |
rsaPrivateKey |
| 24 |
octKey |
| 25 |
) |
| 26 |
|
| 27 |
type testdata struct { |
| 28 |
typ keyType |
| 29 |
encrypted bool |
| 30 |
} |
| 31 |
|
| 32 |
var files = map[string]testdata{ |
| 33 |
"testdata/oct.json": {octKey, false}, |
| 34 |
"testdata/oct.enc.json": {octKey, true}, |
| 35 |
"testdata/okp.pub.json": {ed25519PublicKey, false}, |
| 36 |
"testdata/okp.priv.json": {ed25519PrivateKey, false}, |
| 37 |
"testdata/okp.enc.priv.json": {ed25519PrivateKey, true}, |
| 38 |
"testdata/p256.pub.json": {ecdsaPublicKey, false}, |
| 39 |
"testdata/p256.priv.json": {ecdsaPrivateKey, false}, |
| 40 |
"testdata/p256.enc.priv.json": {ecdsaPrivateKey, true}, |
| 41 |
"testdata/rsa.pub.json": {rsaPublicKey, false}, |
| 42 |
"testdata/rsa.priv.json": {rsaPrivateKey, false}, |
| 43 |
"testdata/rsa.enc.priv.json": {rsaPrivateKey, true}, |
| 44 |
} |
| 45 |
|
| 46 |
var pemFiles = map[string]testdata{ |
| 47 |
"../crypto/pemutil/testdata/openssl.p256.pem": {ecdsaPrivateKey, false}, |
| 48 |
"../crypto/pemutil/testdata/openssl.p256.pub.pem": {ecdsaPublicKey, false}, |
| 49 |
"../crypto/pemutil/testdata/openssl.p256.enc.pem": {ecdsaPrivateKey, true}, |
| 50 |
"../crypto/pemutil/testdata/openssl.p384.pem": {ecdsaPrivateKey, false}, |
| 51 |
"../crypto/pemutil/testdata/openssl.p384.pub.pem": {ecdsaPublicKey, false}, |
| 52 |
"../crypto/pemutil/testdata/openssl.p384.enc.pem": {ecdsaPrivateKey, true}, |
| 53 |
"../crypto/pemutil/testdata/openssl.p521.pem": {ecdsaPrivateKey, false}, |
| 54 |
"../crypto/pemutil/testdata/openssl.p521.pub.pem": {ecdsaPublicKey, false}, |
| 55 |
"../crypto/pemutil/testdata/openssl.p521.enc.pem": {ecdsaPrivateKey, true}, |
| 56 |
"../crypto/pemutil/testdata/openssl.rsa1024.pem": {rsaPrivateKey, false}, |
| 57 |
"../crypto/pemutil/testdata/openssl.rsa1024.pub.pem": {rsaPublicKey, false}, |
| 58 |
"../crypto/pemutil/testdata/openssl.rsa1024.enc.pem": {rsaPrivateKey, true}, |
| 59 |
"../crypto/pemutil/testdata/openssl.rsa2048.pem": {rsaPrivateKey, false}, |
| 60 |
"../crypto/pemutil/testdata/openssl.rsa2048.pub.pem": {rsaPublicKey, false}, |
| 61 |
"../crypto/pemutil/testdata/openssl.rsa2048.enc.pem": {rsaPrivateKey, true}, |
| 62 |
"../crypto/pemutil/testdata/pkcs8/openssl.ed25519.pem": {ed25519PrivateKey, false}, |
| 63 |
"../crypto/pemutil/testdata/pkcs8/openssl.ed25519.pub.pem": {ed25519PublicKey, false}, |
| 64 |
"../crypto/pemutil/testdata/pkcs8/openssl.ed25519.enc.pem": {ed25519PrivateKey, true}, |
| 65 |
"../crypto/pemutil/testdata/pkcs8/openssl.p256.pem": {ecdsaPrivateKey, false}, |
| 66 |
"../crypto/pemutil/testdata/pkcs8/openssl.p256.pub.pem": {ecdsaPublicKey, false}, |
| 67 |
"../crypto/pemutil/testdata/pkcs8/openssl.p256.enc.pem": {ecdsaPrivateKey, true}, |
| 68 |
"../crypto/pemutil/testdata/pkcs8/openssl.p384.pem": {ecdsaPrivateKey, false}, |
| 69 |
"../crypto/pemutil/testdata/pkcs8/openssl.p384.pub.pem": {ecdsaPublicKey, false}, |
| 70 |
"../crypto/pemutil/testdata/pkcs8/openssl.p384.enc.pem": {ecdsaPrivateKey, true}, |
| 71 |
"../crypto/pemutil/testdata/pkcs8/openssl.p521.pem": {ecdsaPrivateKey, false}, |
| 72 |
"../crypto/pemutil/testdata/pkcs8/openssl.p521.pub.pem": {ecdsaPublicKey, false}, |
| 73 |
"../crypto/pemutil/testdata/pkcs8/openssl.p521.enc.pem": {ecdsaPrivateKey, true}, |
| 74 |
"../crypto/pemutil/testdata/pkcs8/openssl.rsa2048.pem": {rsaPrivateKey, false}, |
| 75 |
"../crypto/pemutil/testdata/pkcs8/openssl.rsa2048.pub.pem": {rsaPublicKey, false}, |
| 76 |
"../crypto/pemutil/testdata/pkcs8/openssl.rsa2048.enc.pem": {rsaPrivateKey, true}, |
| 77 |
"../crypto/pemutil/testdata/pkcs8/openssl.rsa4096.pem": {rsaPrivateKey, false}, |
| 78 |
"../crypto/pemutil/testdata/pkcs8/openssl.rsa4096.pub.pem": {rsaPublicKey, false}, |
| 79 |
} |
| 80 |
|
| 81 |
func validateParseKey(t *testing.T, fn, pass string, td testdata) { |
| 82 |
var err error |
| 83 |
var jwk *JSONWebKey |
| 84 |
|
| 85 |
if td.encrypted { |
| 86 |
jwk, err = ParseKey(fn, WithPassword([]byte(pass))) |
| 87 |
} else { |
| 88 |
jwk, err = ParseKey(fn) |
| 89 |
} |
| 90 |
assert.NoError(t, err) |
| 91 |
|
| 92 |
assert.NoError(t, ValidateJWK(jwk)) |
| 93 |
|
| 94 |
switch td.typ { |
| 95 |
case ecdsaPublicKey: |
| 96 |
assert.Type(t, &ecdsa.PublicKey{}, jwk.Key) |
| 97 |
case ecdsaPrivateKey: |
| 98 |
assert.Type(t, &ecdsa.PrivateKey{}, jwk.Key) |
| 99 |
case ed25519PublicKey: |
| 100 |
assert.Type(t, ed25519.PublicKey{}, jwk.Key) |
| 101 |
case ed25519PrivateKey: |
| 102 |
assert.Type(t, ed25519.PrivateKey{}, jwk.Key) |
| 103 |
case rsaPublicKey: |
| 104 |
assert.Type(t, &rsa.PublicKey{}, jwk.Key) |
| 105 |
case rsaPrivateKey: |
| 106 |
assert.Type(t, &rsa.PrivateKey{}, jwk.Key) |
| 107 |
case octKey: |
| 108 |
assert.Type(t, []byte{}, jwk.Key) |
| 109 |
default: |
| 110 |
t.Errorf("type %T not supported", jwk.Key) |
| 111 |
} |
| 112 |
|
| 113 |
if jwk.IsPublic() == false && jwk.KeyID != "" { |
| 114 |
hash, err := jwk.Thumbprint(crypto.SHA256) |
| 115 |
assert.NoError(t, err) |
| 116 |
assert.Equals(t, base64.RawURLEncoding.EncodeToString(hash), jwk.KeyID) |
| 117 |
} |
| 118 |
|
| 119 |
if td.encrypted { |
| 120 |
jwkPriv, err := ParseKey(strings.Replace(fn, ".enc", "", 1)) |
| 121 |
assert.NoError(t, err) |
| 122 |
assert.Equals(t, jwkPriv, jwk) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
func TestParseKey(t *testing.T) { |
| 127 |
for fn, td := range files { |
| 128 |
validateParseKey(t, fn, "password", td) |
| 129 |
} |
| 130 |
|
| 131 |
for fn, td := range pemFiles { |
| 132 |
validateParseKey(t, fn, "mypassword", td) |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
func TestParseKeyPasswordFile(t *testing.T) { |
| 137 |
jwk, err := ParseKey("testdata/oct.txt", WithAlg("HS256"), WithUse("sig"), WithKid("the-kid")) |
| 138 |
assert.FatalError(t, err) |
| 139 |
assert.Equals(t, []byte("a true random password"), jwk.Key) |
| 140 |
assert.Equals(t, HS256, jwk.Algorithm) |
| 141 |
assert.Equals(t, "sig", jwk.Use) |
| 142 |
assert.Equals(t, "the-kid", jwk.KeyID) |
| 143 |
} |
| 144 |
|
| 145 |
func TestParseKeySet(t *testing.T) { |
| 146 |
jwk, err := ParseKeySet("testdata/jwks.json", WithKid("VjIIRw8jzUM58xrVkc4_g9Tfe2MrPPr8GM8Kjijzqus")) |
| 147 |
assert.NoError(t, err) |
| 148 |
assert.Type(t, ed25519.PublicKey{}, jwk.Key) |
| 149 |
assert.Equals(t, "VjIIRw8jzUM58xrVkc4_g9Tfe2MrPPr8GM8Kjijzqus", jwk.KeyID) |
| 150 |
|
| 151 |
jwk, err = ParseKeySet("testdata/jwks.json", WithKid("V93A-Yh7Bhw1W2E0igFciviJzX4PXPswoVgriehm9Co")) |
| 152 |
assert.NoError(t, err) |
| 153 |
assert.Type(t, &ecdsa.PublicKey{}, jwk.Key) |
| 154 |
assert.Equals(t, "V93A-Yh7Bhw1W2E0igFciviJzX4PXPswoVgriehm9Co", jwk.KeyID) |
| 155 |
|
| 156 |
jwk, err = ParseKeySet("testdata/jwks.json", WithKid("duplicated")) |
| 157 |
assert.Error(t, err) |
| 158 |
assert.Equals(t, "multiple keys with kid duplicated have been found on testdata/jwks.json", err.Error()) |
| 159 |
assert.Nil(t, jwk) |
| 160 |
|
| 161 |
jwk, err = ParseKeySet("testdata/jwks.json", WithKid("missing")) |
| 162 |
assert.Error(t, err) |
| 163 |
assert.Equals(t, "cannot find key with kid missing on testdata/jwks.json", err.Error()) |
| 164 |
assert.Nil(t, jwk) |
| 165 |
|
| 166 |
jwk, err = ParseKeySet("testdata/empty.json", WithKid("missing")) |
| 167 |
assert.Error(t, err) |
| 168 |
assert.Equals(t, "cannot find key with kid missing on testdata/empty.json", err.Error()) |
| 169 |
assert.Nil(t, jwk) |
| 170 |
} |
| 171 |
|
| 172 |
func TestGuessJWKAlgorithm(t *testing.T) { |
| 173 |
p256, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 174 |
assert.FatalError(t, err) |
| 175 |
p384, err := ecdsa.GenerateKey(elliptic.P384(), rand.Reader) |
| 176 |
assert.FatalError(t, err) |
| 177 |
p521, err := ecdsa.GenerateKey(elliptic.P521(), rand.Reader) |
| 178 |
assert.FatalError(t, err) |
| 179 |
rsa, err := rsa.GenerateKey(rand.Reader, 2048) |
| 180 |
assert.FatalError(t, err) |
| 181 |
edPub, edPriv, err := ed25519.GenerateKey(rand.Reader) |
| 182 |
assert.FatalError(t, err) |
| 183 |
|
| 184 |
tests := []struct { |
| 185 |
jwk *JSONWebKey |
| 186 |
expected string |
| 187 |
}{ |
| 188 |
{&JSONWebKey{Key: []byte{}, Use: ""}, HS256}, |
| 189 |
{&JSONWebKey{Key: []byte{}, Use: "sig"}, HS256}, |
| 190 |
{&JSONWebKey{Key: []byte{}, Use: "enc"}, "A256GCMKW"}, |
| 191 |
{&JSONWebKey{Key: p256, Use: ""}, ES256}, |
| 192 |
{&JSONWebKey{Key: p384, Use: "sig"}, ES384}, |
| 193 |
{&JSONWebKey{Key: p521, Use: "enc"}, "ECDH-ES"}, |
| 194 |
{&JSONWebKey{Key: p256.Public(), Use: ""}, ES256}, |
| 195 |
{&JSONWebKey{Key: p384.Public(), Use: "sig"}, ES384}, |
| 196 |
{&JSONWebKey{Key: p521.Public(), Use: "enc"}, "ECDH-ES"}, |
| 197 |
{&JSONWebKey{Key: rsa, Use: ""}, RS256}, |
| 198 |
{&JSONWebKey{Key: rsa, Use: "sig"}, RS256}, |
| 199 |
{&JSONWebKey{Key: rsa, Use: "enc"}, "RSA-OAEP-256"}, |
| 200 |
{&JSONWebKey{Key: rsa.Public(), Use: ""}, RS256}, |
| 201 |
{&JSONWebKey{Key: rsa.Public(), Use: "sig"}, RS256}, |
| 202 |
{&JSONWebKey{Key: rsa.Public(), Use: "enc"}, "RSA-OAEP-256"}, |
| 203 |
{&JSONWebKey{Key: edPub, Use: ""}, EdDSA}, |
| 204 |
{&JSONWebKey{Key: edPub, Use: "sig"}, EdDSA}, |
| 205 |
{&JSONWebKey{Key: edPriv, Use: ""}, EdDSA}, |
| 206 |
{&JSONWebKey{Key: edPriv, Use: "sig"}, EdDSA}, |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
ctx, err := new(context).apply(WithAlg(HS256)) |
| 211 |
assert.NoError(t, err) |
| 212 |
jwk := &JSONWebKey{Key: []byte("password")} |
| 213 |
guessJWKAlgorithm(ctx, jwk) |
| 214 |
assert.Equals(t, HS256, jwk.Algorithm) |
| 215 |
|
| 216 |
|
| 217 |
ctx, err = new(context).apply(WithAlg(HS256)) |
| 218 |
assert.NoError(t, err) |
| 219 |
jwk = &JSONWebKey{Key: []byte("password"), Algorithm: HS384} |
| 220 |
guessJWKAlgorithm(ctx, jwk) |
| 221 |
assert.Equals(t, HS384, jwk.Algorithm) |
| 222 |
|
| 223 |
|
| 224 |
for _, tc := range tests { |
| 225 |
guessJWKAlgorithm(new(context), tc.jwk) |
| 226 |
assert.Equals(t, tc.expected, tc.jwk.Algorithm) |
| 227 |
} |
| 228 |
} |