| 1 |
package jose |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/ecdsa" |
| 5 |
"crypto/ed25519" |
| 6 |
"crypto/elliptic" |
| 7 |
"crypto/rand" |
| 8 |
"crypto/rsa" |
| 9 |
"crypto/x509" |
| 10 |
"encoding/pem" |
| 11 |
"io/ioutil" |
| 12 |
"math/big" |
| 13 |
"os" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/smallstep/assert" |
| 17 |
"github.com/stretchr/testify/require" |
| 18 |
) |
| 19 |
|
| 20 |
func TestGenerateJWK(t *testing.T) { |
| 21 |
tests := []struct { |
| 22 |
kty, crv, alg, use, kid string |
| 23 |
size int |
| 24 |
expectedAlg string |
| 25 |
expectedSize int |
| 26 |
expectedType interface{} |
| 27 |
ok bool |
| 28 |
}{ |
| 29 |
{"EC", "", "", "", "", 0, "ES256", 256, &ecdsa.PrivateKey{}, true}, |
| 30 |
{"EC", "P-256", "", "sig", "", 0, "ES256", 256, &ecdsa.PrivateKey{}, true}, |
| 31 |
{"EC", "P-384", "", "sig", "a-kid", 0, "ES384", 384, &ecdsa.PrivateKey{}, true}, |
| 32 |
{"EC", "P-521", "ES521", "sig", "a-kid", 0, "ES521", 521, &ecdsa.PrivateKey{}, true}, |
| 33 |
{"EC", "P-256", "", "enc", "a-kid", 0, "ECDH-ES", 256, &ecdsa.PrivateKey{}, true}, |
| 34 |
{"EC", "P-256", "ECDH-ES+A128KW", "enc", "a-kid", 0, "ECDH-ES+A128KW", 256, &ecdsa.PrivateKey{}, true}, |
| 35 |
{"EC", "P-256", "ECDH-ES+A192KW", "enc", "a-kid", 0, "ECDH-ES+A192KW", 256, &ecdsa.PrivateKey{}, true}, |
| 36 |
{"EC", "P-256", "ECDH-ES+A256KW", "enc", "a-kid", 0, "ECDH-ES+A256KW", 256, &ecdsa.PrivateKey{}, true}, |
| 37 |
{"RSA", "", "", "", "", 0, "RS256", 2048, &rsa.PrivateKey{}, true}, |
| 38 |
{"RSA", "", "", "", "", 4096, "RS256", 4096, &rsa.PrivateKey{}, true}, |
| 39 |
{"RSA", "", "RS384", "sig", "", 1024, "RS384", 1024, &rsa.PrivateKey{}, true}, |
| 40 |
{"RSA", "", "RS521", "sig", "a-kid", 1024, "RS521", 1024, &rsa.PrivateKey{}, true}, |
| 41 |
{"RSA", "", "", "enc", "a-kid", 1024, "RSA-OAEP-256", 1024, &rsa.PrivateKey{}, true}, |
| 42 |
{"RSA", "", "RSA-OAEP-256", "enc", "a-kid", 1024, "RSA-OAEP-256", 1024, &rsa.PrivateKey{}, true}, |
| 43 |
{"RSA", "", "RSA1_5", "enc", "a-kid", 1024, "RSA1_5", 1024, &rsa.PrivateKey{}, true}, |
| 44 |
{"RSA", "", "RSA-OAEP", "enc", "a-kid", 1024, "RSA-OAEP", 1024, &rsa.PrivateKey{}, true}, |
| 45 |
{"OKP", "", "", "", "", 0, "EdDSA", 64, ed25519.PrivateKey{}, true}, |
| 46 |
{"OKP", "", "", "", "sig", 0, "EdDSA", 64, ed25519.PrivateKey{}, true}, |
| 47 |
{"OKP", "", "", "EdDSA", "sig", 0, "EdDSA", 64, ed25519.PrivateKey{}, true}, |
| 48 |
{"oct", "", "", "", "", 0, "HS256", 32, []byte{}, true}, |
| 49 |
{"oct", "", "", "sig", "", 0, "HS256", 32, []byte{}, true}, |
| 50 |
{"oct", "", "HS384", "sig", "a-kid", 16, "HS384", 16, []byte{}, true}, |
| 51 |
{"oct", "", "HS521", "sig", "a-kid", 64, "HS521", 64, []byte{}, true}, |
| 52 |
{"oct", "", "", "enc", "a-kid", 64, "A256GCMKW", 64, []byte{}, true}, |
| 53 |
{"oct", "", "dir", "enc", "a-kid", 0, "dir", 32, []byte{}, true}, |
| 54 |
{"oct", "", "A128KW", "enc", "a-kid", 0, "A128KW", 32, []byte{}, true}, |
| 55 |
{"oct", "", "A192KW", "enc", "a-kid", 0, "A192KW", 32, []byte{}, true}, |
| 56 |
{"oct", "", "A256KW", "enc", "a-kid", 0, "A256KW", 32, []byte{}, true}, |
| 57 |
{"oct", "", "A128GCMKW", "enc", "a-kid", 0, "A128GCMKW", 32, []byte{}, true}, |
| 58 |
{"oct", "", "A192GCMKW", "enc", "a-kid", 0, "A192GCMKW", 32, []byte{}, true}, |
| 59 |
{"oct", "", "A256GCMKW", "enc", "a-kid", 0, "A256GCMKW", 32, []byte{}, true}, |
| 60 |
} |
| 61 |
|
| 62 |
for _, tc := range tests { |
| 63 |
jwk, err := GenerateJWK(tc.kty, tc.crv, tc.alg, tc.use, tc.kid, tc.size) |
| 64 |
if !tc.ok { |
| 65 |
assert.Error(t, err) |
| 66 |
assert.Nil(t, jwk) |
| 67 |
continue |
| 68 |
} |
| 69 |
assert.NoError(t, err) |
| 70 |
|
| 71 |
assert.Equals(t, tc.kid, jwk.KeyID) |
| 72 |
assert.Equals(t, tc.expectedAlg, jwk.Algorithm) |
| 73 |
assert.Type(t, tc.expectedType, jwk.Key) |
| 74 |
|
| 75 |
switch key := jwk.Key.(type) { |
| 76 |
case *ecdsa.PrivateKey: |
| 77 |
switch tc.expectedSize { |
| 78 |
case 256: |
| 79 |
assert.Equals(t, elliptic.P256(), key.Curve) |
| 80 |
case 384: |
| 81 |
assert.Equals(t, elliptic.P384(), key.Curve) |
| 82 |
case 521: |
| 83 |
assert.Equals(t, elliptic.P521(), key.Curve) |
| 84 |
default: |
| 85 |
t.Errorf("unexpected size %d", tc.expectedSize) |
| 86 |
} |
| 87 |
case *rsa.PrivateKey: |
| 88 |
assert.Equals(t, tc.expectedSize, key.N.BitLen()) |
| 89 |
case ed25519.PrivateKey: |
| 90 |
assert.Equals(t, tc.expectedSize, len(key)) |
| 91 |
case []byte: |
| 92 |
assert.Equals(t, tc.expectedSize, len(key)) |
| 93 |
default: |
| 94 |
t.Errorf("unexpected key type %T", key) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
func TestKeyUsageForCert(t *testing.T) { |
| 100 |
tests := []struct { |
| 101 |
Cert *x509.Certificate |
| 102 |
ExpectUse string |
| 103 |
ExpectErr error |
| 104 |
}{ |
| 105 |
{ |
| 106 |
Cert: &x509.Certificate{ |
| 107 |
KeyUsage: x509.KeyUsageDigitalSignature, |
| 108 |
}, |
| 109 |
ExpectUse: jwksUsageSig, |
| 110 |
}, |
| 111 |
{ |
| 112 |
Cert: &x509.Certificate{ |
| 113 |
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageContentCommitment, |
| 114 |
}, |
| 115 |
ExpectUse: jwksUsageSig, |
| 116 |
}, |
| 117 |
{ |
| 118 |
Cert: &x509.Certificate{ |
| 119 |
KeyUsage: x509.KeyUsageDataEncipherment | x509.KeyUsageKeyAgreement, |
| 120 |
}, |
| 121 |
ExpectUse: jwksUsageEnc, |
| 122 |
}, |
| 123 |
{ |
| 124 |
Cert: &x509.Certificate{ |
| 125 |
KeyUsage: x509.KeyUsageDataEncipherment, |
| 126 |
}, |
| 127 |
ExpectUse: jwksUsageEnc, |
| 128 |
}, |
| 129 |
{ |
| 130 |
Cert: &x509.Certificate{}, |
| 131 |
ExpectErr: errNoCertKeyUsage, |
| 132 |
}, |
| 133 |
{ |
| 134 |
Cert: &x509.Certificate{ |
| 135 |
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageDataEncipherment, |
| 136 |
}, |
| 137 |
ExpectErr: errAmbiguousCertKeyUsage, |
| 138 |
}, |
| 139 |
} |
| 140 |
|
| 141 |
for _, tt := range tests { |
| 142 |
use, err := keyUsageForCert(tt.Cert) |
| 143 |
if tt.ExpectErr != nil { |
| 144 |
require.Equal(t, tt.ExpectErr, err) |
| 145 |
} else { |
| 146 |
require.Equal(t, tt.ExpectUse, use) |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
func TestGenerateJWKFromPEMSubtle(t *testing.T) { |
| 152 |
tests := []struct { |
| 153 |
Description string |
| 154 |
KeyUsage x509.KeyUsage |
| 155 |
Subtle bool |
| 156 |
ExpectErr error |
| 157 |
ExpectSig string |
| 158 |
}{ |
| 159 |
{ |
| 160 |
Description: "single key usage without subtle", |
| 161 |
KeyUsage: x509.KeyUsageDigitalSignature, |
| 162 |
ExpectSig: jwksUsageSig, |
| 163 |
}, |
| 164 |
{ |
| 165 |
Description: "single key usage with subtle", |
| 166 |
KeyUsage: x509.KeyUsageDigitalSignature, |
| 167 |
Subtle: true, |
| 168 |
}, |
| 169 |
{ |
| 170 |
Description: "multiple key usage without subtle", |
| 171 |
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 172 |
ExpectErr: errAmbiguousCertKeyUsage, |
| 173 |
}, |
| 174 |
{ |
| 175 |
Description: "multiple key usage with subtle", |
| 176 |
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageKeyEncipherment, |
| 177 |
Subtle: true, |
| 178 |
}, |
| 179 |
} |
| 180 |
|
| 181 |
for _, tt := range tests { |
| 182 |
t.Run(tt.Description, func(t *testing.T) { |
| 183 |
f, cleanup := tempFile(t) |
| 184 |
defer cleanup() |
| 185 |
|
| 186 |
err := pem.Encode(f, &pem.Block{ |
| 187 |
Type: "CERTIFICATE", |
| 188 |
Bytes: newCert(t, tt.KeyUsage), |
| 189 |
}) |
| 190 |
assert.NoError(t, err) |
| 191 |
|
| 192 |
jwk, err := GenerateJWKFromPEM(f.Name(), tt.Subtle) |
| 193 |
if tt.ExpectErr != nil { |
| 194 |
assert.Equals(t, tt.ExpectErr, err) |
| 195 |
return |
| 196 |
} |
| 197 |
require.NoError(t, err) |
| 198 |
require.Equal(t, tt.ExpectSig, jwk.Use) |
| 199 |
require.Equal(t, ES256, jwk.Algorithm) |
| 200 |
require.Equal(t, 1, len(jwk.Certificates)) |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
func newCert(t *testing.T, keyUsage x509.KeyUsage) []byte { |
| 206 |
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader) |
| 207 |
assert.NoError(t, err) |
| 208 |
|
| 209 |
tmpl := x509.Certificate{ |
| 210 |
SerialNumber: big.NewInt(1), |
| 211 |
KeyUsage: keyUsage, |
| 212 |
} |
| 213 |
cert, err := x509.CreateCertificate(rand.Reader, &tmpl, &tmpl, key.Public(), key) |
| 214 |
assert.NoError(t, err) |
| 215 |
return cert |
| 216 |
} |
| 217 |
|
| 218 |
func tempFile(t *testing.T) (_ *os.File, cleanup func()) { |
| 219 |
f, err := ioutil.TempFile("" , "jose-generate-test") |
| 220 |
assert.NoError(t, err) |
| 221 |
return f, func() { |
| 222 |
f.Close() |
| 223 |
os.Remove(f.Name()) |
| 224 |
} |
| 225 |
} |