Skip to content
This repository has been archived by the owner on Feb 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #203 from Everlag/v2
Browse files Browse the repository at this point in the history
Include precomputed RSA values in JSONWebKey.MarshallJSON
  • Loading branch information
csstaub committed Sep 20, 2018
2 parents 43d155e + d5f5041 commit ef984e6
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
10 changes: 10 additions & 0 deletions jwk.go
Expand Up @@ -489,6 +489,16 @@ func fromRsaPrivateKey(rsa *rsa.PrivateKey) (*rawJSONWebKey, error) {
raw.P = newBuffer(rsa.Primes[0].Bytes())
raw.Q = newBuffer(rsa.Primes[1].Bytes())

if rsa.Precomputed.Dp != nil {
raw.Dp = newBuffer(rsa.Precomputed.Dp.Bytes())
}
if rsa.Precomputed.Dq != nil {
raw.Dq = newBuffer(rsa.Precomputed.Dq.Bytes())
}
if rsa.Precomputed.Qinv != nil {
raw.Qi = newBuffer(rsa.Precomputed.Qinv.Bytes())
}

return raw, nil
}

Expand Down
36 changes: 36 additions & 0 deletions jwk_test.go
Expand Up @@ -121,6 +121,42 @@ func TestRoundtripRsaPrivate(t *testing.T) {
}
}

func TestRoundtripRsaPrivatePrecomputed(t *testing.T) {
// Isolate a shallow copy of the rsaTestKey to avoid polluting it with Precompute
localKey := &(*rsaTestKey)
localKey.Precompute()

jwk, err := fromRsaPrivateKey(localKey)
if err != nil {
t.Error("problem constructing JWK from rsa key", err)
}

rsa2, err := jwk.rsaPrivateKey()
if err != nil {
t.Error("problem converting RSA private -> JWK", err)
}

if rsa2.Precomputed.Dp == nil {
t.Error("RSA private Dp nil")
}
if rsa2.Precomputed.Dq == nil {
t.Error("RSA private Dq nil")
}
if rsa2.Precomputed.Qinv == nil {
t.Error("RSA private Qinv nil")
}

if rsa2.Precomputed.Dp.Cmp(localKey.Precomputed.Dp) != 0 {
t.Error("RSA private Dp mismatch")
}
if rsa2.Precomputed.Dq.Cmp(localKey.Precomputed.Dq) != 0 {
t.Error("RSA private Dq mismatch")
}
if rsa2.Precomputed.Qinv.Cmp(localKey.Precomputed.Qinv) != 0 {
t.Error("RSA private Qinv mismatch")
}
}

func TestRsaPrivateInsufficientPrimes(t *testing.T) {
brokenRsaPrivateKey := rsa.PrivateKey{
PublicKey: rsa.PublicKey{
Expand Down

0 comments on commit ef984e6

Please sign in to comment.