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

Add compatibility with old signature format #192

Merged
merged 3 commits into from
Jan 31, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 0 additions & 5 deletions api/v0/admin/client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,10 @@ import (
"path"

"github.com/filecoin-project/storetheindex/api/v0/httpclient"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multiaddr"
)

var log = logging.Logger("adminhttpclient")

const (
adminPort = 3002

Expand Down Expand Up @@ -68,7 +65,6 @@ func (c *Client) ImportFromManifest(ctx context.Context, fileName string, provID
}
return fmt.Errorf("importing from manifest failed: %v%s", http.StatusText(resp.StatusCode), errMsg)
}
log.Infow("Success")
return nil
}

Expand All @@ -95,7 +91,6 @@ func (c *Client) ImportFromCidList(ctx context.Context, fileName string, provID
}
return fmt.Errorf("importing from cidlist failed: %v%s", http.StatusText(resp.StatusCode), errMsg)
}
log.Infow("Success")
return nil
}

Expand Down
4 changes: 0 additions & 4 deletions api/v0/finder/client/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,10 @@ import (

"github.com/filecoin-project/storetheindex/api/v0/finder/model"
"github.com/filecoin-project/storetheindex/api/v0/httpclient"
logging "github.com/ipfs/go-log/v2"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/multiformats/go-multihash"
)

var log = logging.Logger("finderhttpclient")

const (
finderPort = 3000
finderPath = "/multihash"
Expand Down Expand Up @@ -142,7 +139,6 @@ func (c *Client) sendRequest(req *http.Request) (*model.FindResponse, error) {
// Handle failed requests
if resp.StatusCode != http.StatusOK {
if resp.StatusCode == http.StatusNotFound {
log.Info("Entry not found in indexer")
return &model.FindResponse{}, nil
}
return nil, fmt.Errorf("batch find query failed: %v", http.StatusText(resp.StatusCode))
Expand Down
38 changes: 29 additions & 9 deletions api/v0/ingest/schema/envelope.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"fmt"

"github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log/v2"
cidlink "github.com/ipld/go-ipld-prime/linking/cid"
crypto "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/record"
mh "github.com/multiformats/go-multihash"
"github.com/multiformats/go-multihash"
)

var log = logging.Logger("indexer/schema")

const (
adSignatureCodec = "/indexer/ingest/adSignature"
adSignatureDomain = "indexer"
Expand Down Expand Up @@ -48,7 +51,7 @@ func (r *advSignatureRecord) UnmarshalRecord(buf []byte) error {
}

// Generates the data payload used for signature.
func signaturePayload(previousID Link_Advertisement, provider string, addrs []string, entries Link, metadata []byte, isRm bool) ([]byte, error) {
func signaturePayload(previousID Link_Advertisement, provider string, addrs []string, entries Link, metadata []byte, isRm, oldFormat bool) ([]byte, error) {
bindex := cid.Undef.Bytes()
lindex, err := previousID.AsLink()
if err != nil {
Expand Down Expand Up @@ -84,7 +87,14 @@ func signaturePayload(previousID Link_Advertisement, provider string, addrs []st
sigBuf.WriteByte(0)
}

return mh.Sum(sigBuf.Bytes(), mhCode, -1)
// Generates the old (incorrect) data payload used for signature. This is
// only for compatability with existing advertisements that have the old
// signatures, and should be removed when no longer needed.
if oldFormat {
return multihash.Encode(sigBuf.Bytes(), mhCode)
}

return multihash.Sum(sigBuf.Bytes(), mhCode, -1)
}

// Signs advertisements using libp2p envelope
Expand All @@ -99,7 +109,7 @@ func signAdvertisement(privkey crypto.PrivKey, ad Advertisement) ([]byte, error)
entries := ad.FieldEntries()
metadata := ad.FieldMetadata().x

advID, err := signaturePayload(&previousID, provider, addrs, entries, metadata, isRm)
advID, err := signaturePayload(&previousID, provider, addrs, entries, metadata, isRm, false)
if err != nil {
return nil, err
}
Expand All @@ -113,6 +123,10 @@ func signAdvertisement(privkey crypto.PrivKey, ad Advertisement) ([]byte, error)
// VerifyAdvertisement verifies that the advertisement has been signed and
// generated correctly. Returns the peer ID of the signer.
func VerifyAdvertisement(ad Advertisement) (peer.ID, error) {
// sigSize is the size of the current signature. Any signature that is not
// this size is the old signature format.
const sigSize = 34

previousID := ad.FieldPreviousID().v
provider := ad.FieldProvider().x
addrs, err := IpldToGoStrings(ad.FieldAddresses())
Expand All @@ -124,25 +138,31 @@ func VerifyAdvertisement(ad Advertisement) (peer.ID, error) {
metadata := ad.FieldMetadata().x
sig := ad.FieldSignature().x

genID, err := signaturePayload(&previousID, provider, addrs, entries, metadata, isRm)
// Consume envelope
rec := &advSignatureRecord{}
envelope, err := record.ConsumeTypedEnvelope(sig, rec)
if err != nil {
return peer.ID(""), err
}

// Consume envelope
rec := &advSignatureRecord{}
envelope, err := record.ConsumeTypedEnvelope(sig, rec)
oldFormat := len(rec.advID) != sigSize
genID, err := signaturePayload(&previousID, provider, addrs, entries, metadata, isRm, oldFormat)
if err != nil {
return peer.ID(""), err
}

if !bytes.Equal(genID, rec.advID) {
return peer.ID(""), errors.New("envelope signed with the wrong ID")
return peer.ID(""), errors.New("invalid signature")
}

signerID, err := peer.IDFromPublicKey(envelope.PublicKey)
if err != nil {
return peer.ID(""), fmt.Errorf("cannot convert public key to peer ID: %s", err)
}

if oldFormat {
log.Warnw("advertisement has deprecated signature format", "signer", signerID)
}

return signerID, nil
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v0.2.5"
"version": "v0.3.0"
}