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

Commit

Permalink
Fixes issue #40: extraneous zero padding on serialized exponent
Browse files Browse the repository at this point in the history
  • Loading branch information
csstaub committed Jul 23, 2015
1 parent f1f7ccb commit ffc2f00
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
23 changes: 23 additions & 0 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"bytes"
"compress/flate"
"encoding/base64"
"encoding/binary"
"encoding/json"
"io"
"math/big"
Expand Down Expand Up @@ -131,6 +132,14 @@ func newBuffer(data []byte) *byteBuffer {
}
}

func newBufferFromInt(num uint64) *byteBuffer {
data := make([]byte, 8)
binary.BigEndian.PutUint64(data, num)
return &byteBuffer{
data: data,
}
}

func (b *byteBuffer) MarshalJSON() ([]byte, error) {
return json.Marshal(b.base64())
}
Expand Down Expand Up @@ -175,3 +184,17 @@ func (b byteBuffer) bigInt() *big.Int {
func (b byteBuffer) toInt() int {
return int(b.bigInt().Int64())
}

func (b byteBuffer) trim() *byteBuffer {
i := 0
for _, b := range b.data {
if b == 0 {
i++
} else {
break
}
}
return &byteBuffer{
data: b.data[i:],
}
}
12 changes: 12 additions & 0 deletions encoding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,15 @@ func TestInvalidCompression(t *testing.T) {
t.Error("should not accept invalid data")
}
}

func TestByteBufferTrim(t *testing.T) {
buf := newBufferFromInt(1).trim()
if len(buf.data) != 1 || buf.data[0] != 1 {
t.Error("Trimmed byte buffer for integer '1' should contain [0x01]")
}

buf = newBufferFromInt(65537).trim()
if len(buf.data) != 3 || !bytes.Equal(buf.data, []byte{0x01, 0x00, 0x01}) {
t.Error("Trimmed byte buffer for integer '1' should contain [0x01, 0x00, 0x01]")
}
}
6 changes: 1 addition & 5 deletions jwk.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"encoding/binary"
"encoding/json"
"fmt"
"math/big"
Expand Down Expand Up @@ -128,13 +127,10 @@ func (key rawJsonWebKey) rsaPublicKey() (*rsa.PublicKey, error) {
}

func fromRsaPublicKey(pub *rsa.PublicKey) *rawJsonWebKey {
e := make([]byte, 4)
binary.BigEndian.PutUint32(e, uint32(pub.E))

return &rawJsonWebKey{
Kty: "RSA",
N: newBuffer(pub.N.Bytes()),
E: newBuffer(e),
E: newBufferFromInt(uint64(pub.E)).trim(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions jwk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package jose

import (
"bytes"
"fmt"
"encoding/json"
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rsa"
"encoding/json"
"fmt"
"math/big"
"reflect"
"testing"
Expand Down Expand Up @@ -178,7 +178,7 @@ func TestMarshalNonPointer(t *testing.T) {
t.Error(fmt.Sprintf("Error marshalling JSON: %v", err))
return
}
expected := "{\"Key\":{\"kty\":\"RSA\",\"n\":\"vd7rZIoTLEe-z1_8G1FcXSw9CQFEJgV4g9V277sER7yx5Qjz_Pkf2YVth6wwwFJEmzc0hoKY-MMYFNwBE4hQHw\",\"e\":\"AAEAAQ\"}}"
expected := "{\"Key\":{\"kty\":\"RSA\",\"n\":\"vd7rZIoTLEe-z1_8G1FcXSw9CQFEJgV4g9V277sER7yx5Qjz_Pkf2YVth6wwwFJEmzc0hoKY-MMYFNwBE4hQHw\",\"e\":\"AQAB\"}}"
if string(out) != expected {
t.Error("Failed to marshal embedded non-pointer JWK properly:", string(out))
}
Expand Down

0 comments on commit ffc2f00

Please sign in to comment.