commit 17de1b90f978e0584a1cc1481b40dffdbb23dbf1 Author: Aydin Mercan Date: Tue Nov 2 10:43:52 2021 +0300 initial commit - Created the JWK definition with parsing key sets, needs more through poking for covering invalid state handling. - Basic parsing tests with CI diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..9c71638 --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,21 @@ +name: Tests +on: [push, pull_request] + +permission: + contents: read + +jobs: + test: + name: Test + runs-on: ubuntu-latest + steps: + - name: Install Go + uses: actions/setup-go@v2 + + - name: Checkout Repository + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Test + run: go test -v-race ./... diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6e9ba1d --- /dev/null +++ b/LICENSE @@ -0,0 +1,11 @@ +Copyright 2021 Aydin Mercan + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..08399a0 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +# Dumb JOSE + +Insecure library for a set of insecure formats. + +## License + +This repository is licensed under the `BSD-3-Clause`. diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..80a55cf --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module mercan.dev/dumb-jose + +go 1.17 + +require golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..cbb0e79 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 h1:7I4JAnoQBe7ZtJcBaYHi5UtiO8tQHbUSXxL+pnGRANg= +golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= diff --git a/jwk/ecdsa.go b/jwk/ecdsa.go new file mode 100644 index 0000000..e921c70 --- /dev/null +++ b/jwk/ecdsa.go @@ -0,0 +1,72 @@ +package jwk + +import ( + "crypto/ecdsa" + "crypto/elliptic" + "encoding/base64" + "encoding/json" + "errors" + "fmt" + "math/big" +) + +// Represents an ECDSA public key according to RFC 7518. +type ECDSAPublicKeyHeader struct { + Curve string `json:"crv"` + X string `json:"x"` + Y string `json:"y"` +} + +var ( + ErrInvalidCurvePoint = errors.New("Invalid curve point") +) + +// Rejects on invalid curve points with a branch +func ParseECDSAPublicKey(data []byte) (*ecdsa.PublicKey, error) { + var header ECDSAPublicKeyHeader + + err := json.Unmarshal(data, &header) + if err != nil { + return nil, err + } + + rawX, err := base64.RawURLEncoding.DecodeString(header.X) + if err != nil { + return nil, err + } + + rawY, err := base64.RawURLEncoding.DecodeString(header.Y) + if err != nil { + return nil, err + } + + x := new(big.Int).SetBytes(rawX) + y := new(big.Int).SetBytes(rawY) + + var curve elliptic.Curve + + switch header.Curve { + case "P-256": + curve = elliptic.P256() + case "P-384": + curve = elliptic.P384() + case "P-521": + curve = elliptic.P521() + default: + return nil, fmt.Errorf("Invalid curve type %s", header.Curve) + } + + // Invalid curve attacks don't exactly apply here? + if !curve.IsOnCurve(x, y) { + return nil, ErrInvalidCurvePoint + } + + key := &ecdsa.PublicKey{ + Curve: curve, + X: x, + Y: y, + } + + return key, nil + +} diff --git a/jwk/eddsa.go b/jwk/eddsa.go new file mode 100644 index 0000000..557454e --- /dev/null +++ b/jwk/eddsa.go @@ -0,0 +1,32 @@ +package jwk + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "golang.org/x/crypto/ed25519" +) + +type EdDSAPublicKeyHeader struct { + Curve string `json:"crv"` + X string `json:"x"` +} + +func ParseEdDSAPublicKey(data []byte) (*ed25519.PublicKey, error) { + var header EdDSAPublicKeyHeader + + err := json.Unmarshal(data, &header) + if err != nil { + return nil, err + } + + if header.Curve != "Ed25519" { + return nil, fmt.Errorf("Invalid/Unsupported curve type %s", header.Curve) + } + + rawKey, err := base64.RawURLEncoding.DecodeString(header.X) + + key := ed25519.PublicKey(rawKey) + + return &key, nil +} diff --git a/jwk/jwk.go b/jwk/jwk.go new file mode 100644 index 0000000..15cb7f3 --- /dev/null +++ b/jwk/jwk.go @@ -0,0 +1,76 @@ +package jwk + +import ( + "crypto" + "encoding/json" + "fmt" +) + +type PublicKeyHeader interface{} + +type JWK struct { + KeyID string `json:"kid"` + KeyType string `json:"kty"` + Algorithm string `json:"alg"` + UseCase string `json:"use"` + PublicKey crypto.PublicKey `json:"-"` +} + +type jwkHeader struct { + Keys []json.RawMessage `json:"keys"` +} + +func ParseJWKKeysFromSet(data []byte) ([]JWK, error) { + keys := jwkHeader{} + + err := json.Unmarshal(data, &keys) + if err != nil { + return nil, err + } + + var set []JWK + + for _, key := range keys.Keys { + var jwk JWK + err := json.Unmarshal(key, &jwk) + if err != nil { + return nil, err + + } + + if jwk.UseCase != "sig" { + return nil, fmt.Errorf("Non-signing use case %s", jwk.UseCase) + } + + switch jwk.Algorithm { + case "ES256", "ES384", "ES512": + if jwk.KeyType != "EC" { + return nil, fmt.Errorf("Insuitable key type %s for algorithm %s", jwk.KeyType, jwk.Algorithm) + } + + jwk.PublicKey, err = ParseECDSAPublicKey(key) + case "RS256", "RS384", "RS512", "PS256", "PS384", "PS512": + if jwk.KeyType != "RSA" { + return nil, fmt.Errorf("Insuitable key type %s for algorithm %s", jwk.KeyType, jwk.Algorithm) + } + + jwk.PublicKey, err = ParseRSAPublicKey(key) + case "EdDSA": + if jwk.KeyType != "OKP" { + return nil, fmt.Errorf("Insuitable key type %s for algorithm %s", jwk.KeyType, jwk.Algorithm) + } + + jwk.PublicKey, err = ParseEdDSAPublicKey(key) + default: + return nil, fmt.Errorf("Invalid/Unsupported JWK Algorithm %s", jwk.Algorithm) + } + + if err != nil { + return nil, err + } + + set = append(set, jwk) + } + + return set, nil +} diff --git a/jwk/jwk_test.go b/jwk/jwk_test.go new file mode 100644 index 0000000..afaba30 --- /dev/null +++ b/jwk/jwk_test.go @@ -0,0 +1,60 @@ +package jwk_test + +import ( + "mercan.dev/dumb-jose/jwk" + "testing" +) + +const ( + GoogleJwkKeySet = `{ + "keys": [ + { + "e": "AQAB", + "n": "y930dtGTeMG52IPsKmMuEpPHLaxuYQlduZd6BqFVjc2-UFZR8fNqtnYzAjbXWJD_Tqxgdlj_MW4vogvX4sHwVpZONvdyeGoIyDQtis6iuGQhQamV85F_JbrEUnEw3QCO87Liz5UXG6BK2HRyPhDfMex1_tO0ROmySLFdCTS17D0wah71Ibpi0gI8LUi6kzVRjYDIC1oE-iK3Y9s88Bi4ZGYJxXAbnNwbwVkGOKCXja9k0jjBGRxZD-4KDuf493lFOOEGSLDA2Qp9rDqrURP12XYgvf_zJx_kSDipnr0gL6Vz2n3H4-XN4tA45zuzRkHoE7-XexPq-tv7kQ8pSjY2uQ", + "kid": "bbd2ac7c4c5eb8adc8eeffbc8f5a2dd6cf7545e4", + "alg": "RS256", + "kty": "RSA", + "use": "sig" + }, + { + "use": "sig", + "e": "AQAB", + "kty": "RSA", + "alg": "RS256", + "kid": "85828c59284a69b54b27483e487c3bd46cd2a2b3", + "n": "zMHxWuxztMKXdBhv3rImlUvW_yp6nO03cVXPyA0Vyq0-M7LfOJJIF-OdNoRGdsFPHVKCFoo6qGhR8rBCmMxA4fM-Ubk5qKuUqCN9eP3yZJq8Cw9tUrt_qh7uW-qfMr0upcyeSHhC_zW1lTGs5sowDorKN_jQ1Sfh9hfBxfc8T7dQAAgEqqMcE3u-2J701jyhJz0pvurCfziiB3buY6SGREhBQwNwpnQjt_lE2U4km8FS0woPzt0ccE3zsGL2qM-LWZbOm9aXquSnqNJLt3tGVvShnev-GiJ1XfQ3EWm0f4w0TX9fTOkxstl0vo_vW_FjGQ0D1pXSjqb7n-hAdXwc9w" + } + ] +}` + + InvalidExponent = `{ "keys": [{ "e": "Aw", "n": "AQAB", "kid": "AQAB", "kty": "RSA", "alg": "RS256", "use": "sig" }] }` + + ValidEd25519KeySet = `{"kty":"OKP", "kid": "test", "crv":"Ed25519", "x":"11qYAYKxCrfVS_7TyWQHOg7hcvPapiMlrwIaaPcHURo"}` +) + +func TestCorrectJwkKeySet(t *testing.T) { + set, err := jwk.ParseJWKKeysFromSet([]byte(GoogleJwkKeySet)) + if err != nil { + t.Errorf("Error while parsing JWK Keyset: %v", err) + } + + for i, kid := range []string{"bbd2ac7c4c5eb8adc8eeffbc8f5a2dd6cf7545e4", "85828c59284a69b54b27483e487c3bd46cd2a2b3"} { + if set[i].KeyID != kid { + t.Errorf("hhhhh") + t.Fail() + } + } + + set, err = jwk.ParseJWKKeysFromSet([]byte(ValidEd25519KeySet)) + if err != nil { + t.Errorf("%v", err) + } + +} + +func TestInvalidRSAExponent(t *testing.T) { + _, err := jwk.ParseJWKKeysFromSet([]byte(InvalidExponent)) + if err != jwk.ErrUnsupportedPublicExponent { + t.Errorf("Expected error not returned for unsupported public exponent, found \"%v\"", err) + } +} diff --git a/jwk/rsa.go b/jwk/rsa.go new file mode 100644 index 0000000..1c5958d --- /dev/null +++ b/jwk/rsa.go @@ -0,0 +1,46 @@ +package jwk + +import ( + "crypto/rsa" + "encoding/base64" + "encoding/json" + "errors" + "math/big" +) + +type RSAPublicKeyHeader struct { + Modulus string `json:"n"` + Exponent string `json:"e"` +} + +var ( + ErrUnsupportedPublicExponent = errors.New("Public exponent is not 65537") +) + +func ParseRSAPublicKey(data []byte) (*rsa.PublicKey, error) { + + var header RSAPublicKeyHeader + + err := json.Unmarshal(data, &header) + if err != nil { + return nil, err + } + + rawN, err := base64.RawURLEncoding.DecodeString(header.Modulus) + if err != nil { + return nil, err + } + + n := new(big.Int).SetBytes(rawN) + + if header.Exponent != "AQAB" { + return nil, ErrUnsupportedPublicExponent + } + + key := &rsa.PublicKey{ + N: n, + E: 65537, + } + + return key, nil +}