Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add rsa support #13

Merged
merged 3 commits into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions pkg/document/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,42 @@ func (jwk JWK) Y() string {
return stringEntry(jwk["y"])
}

// N is n.
func (jwk JWK) N() string {
return stringEntry(jwk["n"])
}

// E is e.
func (jwk JWK) E() string {
return stringEntry(jwk["e"])
}

// Validate will validate JWK properties.
func (jwk JWK) Validate() error {
// TODO: validation of the JWK fields depends on the algorithm (issue-409)
// For now check required fields for currently supported algorithms secp256k1, P-256, P-384, P-512 and Ed25519

if jwk.Crv() == "" {
return errors.New("JWK crv is missing")
}

if jwk.Kty() == "" {
return errors.New("JWK kty is missing")
}

if jwk.X() == "" {
return errors.New("JWK x is missing")
if jwk.Kty() == "RSA" {
if jwk.N() == "" {
return errors.New("JWK n is missing")
}

if jwk.E() == "" {
return errors.New("JWK e is missing")
}
} else {
if jwk.Crv() == "" {
return errors.New("JWK crv is missing")
}

if jwk.X() == "" {
return errors.New("JWK x is missing")
}

}

return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/document/jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,25 @@ func TestValidate(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "JWK x is missing")
})

t.Run("missing n", func(t *testing.T) {
jwk := JWK{
"kty": "RSA",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "JWK n is missing")
})

t.Run("missing e", func(t *testing.T) {
jwk := JWK{
"kty": "RSA",
"n": "nn",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "JWK e is missing")
})
}
22 changes: 16 additions & 6 deletions pkg/jws/jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,26 @@ type JWK struct {

// Validate validates JWK.
func (jwk *JWK) Validate() error {
if jwk.Crv == "" {
return errors.New("JWK crv is missing")
}

if jwk.Kty == "" {
return errors.New("JWK kty is missing")
}

if jwk.X == "" {
return errors.New("JWK x is missing")
if jwk.Kty == "RSA" {
if jwk.N == "" {
return errors.New("JWK n is missing")
}

if jwk.E == "" {
return errors.New("JWK e is missing")
}
} else {
if jwk.Crv == "" {
return errors.New("JWK crv is missing")
}

if jwk.X == "" {
return errors.New("JWK x is missing")
}
}

return nil
Expand Down
21 changes: 21 additions & 0 deletions pkg/jws/jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,25 @@ func TestValidate(t *testing.T) {
require.Error(t, err)
require.Contains(t, err.Error(), "x is missing")
})

t.Run("missing n", func(t *testing.T) {
jwk := JWK{
Kty: "RSA",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "n is missing")
})

t.Run("missing e", func(t *testing.T) {
jwk := JWK{
Kty: "RSA",
N: "something",
}

err := jwk.Validate()
require.Error(t, err)
require.Contains(t, err.Error(), "e is missing")
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestValidatePublicKeysErrors(t *testing.T) {

err = validatePublicKeys(doc.PublicKeys())
require.Error(t, err)
require.Contains(t, err.Error(), "JWK crv is missing")
require.Contains(t, err.Error(), "JWK kty is missing")
})

t.Run("pkB58 key with jwk type", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/versions/1_0/operationparser/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func TestParseRecoverOperation(t *testing.T) {

op, err := parser.ParseRecoverOperation(request, false)
require.Error(t, err)
require.Contains(t, err.Error(), "validate signed data for recovery: signing key validation failed: JWK crv is missing")
require.Contains(t, err.Error(), "validate signed data for recovery: signing key validation failed: JWK kty is missing")
require.Nil(t, op)
})

Expand Down
Loading