Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 60c0d85

Browse files
authored
Merge pull request #1975 from OpenBazaar/failing-listing-sign-verify
Fix failing signature validation
2 parents 0a699e1 + 0d22b26 commit 60c0d85

File tree

14 files changed

+241
-50
lines changed

14 files changed

+241
-50
lines changed

Godeps/Godeps.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/order.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -590,7 +590,7 @@ func (n *OpenBazaarNode) createContractWithOrder(data *repo.PurchaseData) (*pb.R
590590
return nil, errors.New("listing does not accept the selected currency")
591591
}
592592

593-
ser, err := listing.MarshalJSON()
593+
ser, err := listing.MarshalProtobuf()
594594
if err != nil {
595595
return nil, err
596596
}

repo/listing.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,19 @@ func (l *Listing) GetShippingRegions() ([]string, []string) {
962962
return returnShipTo, returnFreeShipTo
963963
}
964964

965+
// MarshalProtobuf returns the byte serialization of the underlying protobuf
966+
func (l *Listing) MarshalProtobuf() ([]byte, error) {
967+
return proto.Marshal(l.listingProto)
968+
}
969+
965970
type listingSigner interface {
966971
TestNetworkEnabled() bool
967972
RegressionNetworkEnabled() bool
968973
GetNodeID() (*pb.ID, error)
969974
Sign([]byte) ([]byte, error)
970975
}
971976

977+
// MarshalJSON returns the json serialization of the underlying protobuf
972978
func (l *Listing) MarshalJSON() ([]byte, error) {
973979
m := jsonpb.Marshaler{
974980
EnumsAsInts: false,
@@ -1030,7 +1036,7 @@ func (l *Listing) Sign(n listingSigner) (*SignedListing, error) {
10301036
l.listingProto.VendorID = id
10311037

10321038
// Sign listing
1033-
serializedListing, err := l.MarshalJSON()
1039+
serializedListing, err := l.MarshalProtobuf()
10341040
if err != nil {
10351041
return nil, fmt.Errorf("serializing listing: %s", err.Error())
10361042
}

repo/signed_listing.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,15 +115,16 @@ func (l SignedListing) ValidateListing(isTestnet bool) error {
115115
// VerifySignature checks the listings signature was produced by the vendor's
116116
// Identity key and that the key was derived from the vendor's peerID
117117
func (l SignedListing) VerifySignature() error {
118-
ser, err := proto.Marshal(l.GetListing().listingProto)
118+
ser, err := l.GetListing().MarshalProtobuf()
119119
if err != nil {
120120
return fmt.Errorf("marshaling listing: %s", err.Error())
121121
}
122+
122123
pubkey, err := l.GetListing().GetVendorID().IdentityKey()
123124
if err != nil {
124125
return fmt.Errorf("getting identity pubkey: %s", err.Error())
125126
}
126-
valid, err := pubkey.Verify(ser, l.signedListingProto.GetSignature())
127+
valid, err := pubkey.Verify(ser, l.GetSignature())
127128
if err != nil {
128129
return fmt.Errorf("verifying signature: %s", err.Error())
129130
}

repo/signed_listing_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
package repo_test
22

33
import (
4+
"bytes"
5+
"crypto/rand"
6+
crypto "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
7+
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
48
"math/big"
59
"testing"
610

711
"github.com/OpenBazaar/openbazaar-go/repo"
12+
"github.com/OpenBazaar/openbazaar-go/test"
813
"github.com/OpenBazaar/openbazaar-go/test/factory"
914
)
1015

@@ -192,3 +197,72 @@ func TestSignedListingAttributes(t *testing.T) {
192197
}
193198
}
194199
}
200+
201+
func TestSignAndVerifyListing(t *testing.T) {
202+
testnode, err := test.NewNode()
203+
if err != nil {
204+
t.Fatalf("create test node: %v", err)
205+
}
206+
207+
priv, pub, err := crypto.GenerateEd25519Key(rand.Reader)
208+
if err != nil {
209+
t.Fatal(err)
210+
}
211+
212+
pid, err := peer.IDFromPublicKey(pub)
213+
if err != nil {
214+
t.Fatal(err)
215+
}
216+
217+
testnode.IpfsNode.Identity = pid
218+
testnode.IpfsNode.PrivateKey = priv
219+
220+
lpb := factory.NewListing("test")
221+
l, err := repo.NewListingFromProtobuf(lpb)
222+
if err != nil {
223+
t.Fatalf("create repo listing: %v", err)
224+
}
225+
226+
sl, err := l.Sign(testnode)
227+
if err != nil {
228+
t.Fatalf("sign listing: %v", err)
229+
}
230+
231+
// get identities from node and listing and compare
232+
nodeID, err := testnode.GetNodeID()
233+
if err != nil {
234+
t.Fatalf("get node id: %v", err)
235+
}
236+
nodeIdentityBytes := nodeID.GetPubkeys().GetIdentity()
237+
listingIdentityBytes := sl.GetListing().GetVendorID().IdentityKeyBytes()
238+
if !bytes.Equal(nodeIdentityBytes, listingIdentityBytes) {
239+
t.Fatal("expected listing and node identity bytes to match, but did not")
240+
}
241+
242+
// get peer IDs from node and listing and compare
243+
listingPeerHash, err := sl.GetListing().GetVendorID().Hash()
244+
if err != nil {
245+
t.Fatalf("get listing peer hash: %v", err)
246+
}
247+
if listingPeerHash != nodeID.PeerID {
248+
t.Errorf("expected listing has to be (%s), but was (%s)", nodeID.PeerID, listingPeerHash)
249+
}
250+
251+
// get listing signature and ensure it's as expected
252+
serializedListing, err := l.MarshalProtobuf()
253+
if err != nil {
254+
t.Fatalf("serialize listing: %v", err)
255+
}
256+
expectedSig, err := testnode.IpfsNode.PrivateKey.Sign(serializedListing)
257+
if err != nil {
258+
t.Fatalf("sign listing: %v", err)
259+
}
260+
actualSig := sl.GetSignature()
261+
if !bytes.Equal(expectedSig, actualSig) {
262+
t.Fatal("expected signature on listing to match generated signature, but did not")
263+
}
264+
265+
if err := sl.VerifySignature(); err != nil {
266+
t.Errorf("verify signed listing: %v", err)
267+
}
268+
}

vendor/github.com/gorilla/websocket/.travis.yml

Lines changed: 0 additions & 19 deletions
This file was deleted.

vendor/github.com/gorilla/websocket/README.md

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/gorilla/websocket/conn.go

Lines changed: 49 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)