mirror of https://github.com/ethereum/go-ethereum
cmd/swarm, swarm: added access control functionality (#17404)
Co-authored-by: Janos Guljas <janos@resenje.org> Co-authored-by: Anton Evangelatov <anton.evangelatov@gmail.com> Co-authored-by: Balint Gabor <balint.g@gmail.com>pull/17409/merge
parent
040aa2bb10
commit
e8752f4e9f
@ -0,0 +1,219 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of go-ethereum.
|
||||
//
|
||||
// go-ethereum is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// go-ethereum is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
package main |
||||
|
||||
import ( |
||||
"crypto/rand" |
||||
"encoding/json" |
||||
"fmt" |
||||
"io" |
||||
"io/ioutil" |
||||
"strings" |
||||
|
||||
"github.com/ethereum/go-ethereum/cmd/utils" |
||||
"github.com/ethereum/go-ethereum/swarm/api" |
||||
"github.com/ethereum/go-ethereum/swarm/api/client" |
||||
"gopkg.in/urfave/cli.v1" |
||||
) |
||||
|
||||
var salt = make([]byte, 32) |
||||
|
||||
func init() { |
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil { |
||||
panic("reading from crypto/rand failed: " + err.Error()) |
||||
} |
||||
} |
||||
|
||||
func accessNewPass(ctx *cli.Context) { |
||||
args := ctx.Args() |
||||
if len(args) != 1 { |
||||
utils.Fatalf("Expected 1 argument - the ref") |
||||
} |
||||
|
||||
var ( |
||||
ae *api.AccessEntry |
||||
accessKey []byte |
||||
err error |
||||
ref = args[0] |
||||
password = getPassPhrase("", 0, makePasswordList(ctx)) |
||||
dryRun = ctx.Bool(SwarmDryRunFlag.Name) |
||||
) |
||||
accessKey, ae, err = api.DoPasswordNew(ctx, password, salt) |
||||
if err != nil { |
||||
utils.Fatalf("error getting session key: %v", err) |
||||
} |
||||
m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) |
||||
if dryRun { |
||||
err = printManifests(m, nil) |
||||
if err != nil { |
||||
utils.Fatalf("had an error printing the manifests: %v", err) |
||||
} |
||||
} else { |
||||
utils.Fatalf("uploading manifests") |
||||
err = uploadManifests(ctx, m, nil) |
||||
if err != nil { |
||||
utils.Fatalf("had an error uploading the manifests: %v", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func accessNewPK(ctx *cli.Context) { |
||||
args := ctx.Args() |
||||
if len(args) != 1 { |
||||
utils.Fatalf("Expected 1 argument - the ref") |
||||
} |
||||
|
||||
var ( |
||||
ae *api.AccessEntry |
||||
sessionKey []byte |
||||
err error |
||||
ref = args[0] |
||||
privateKey = getPrivKey(ctx) |
||||
granteePublicKey = ctx.String(SwarmAccessGrantKeyFlag.Name) |
||||
dryRun = ctx.Bool(SwarmDryRunFlag.Name) |
||||
) |
||||
sessionKey, ae, err = api.DoPKNew(ctx, privateKey, granteePublicKey, salt) |
||||
if err != nil { |
||||
utils.Fatalf("error getting session key: %v", err) |
||||
} |
||||
m, err := api.GenerateAccessControlManifest(ctx, ref, sessionKey, ae) |
||||
if dryRun { |
||||
err = printManifests(m, nil) |
||||
if err != nil { |
||||
utils.Fatalf("had an error printing the manifests: %v", err) |
||||
} |
||||
} else { |
||||
err = uploadManifests(ctx, m, nil) |
||||
if err != nil { |
||||
utils.Fatalf("had an error uploading the manifests: %v", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func accessNewACT(ctx *cli.Context) { |
||||
args := ctx.Args() |
||||
if len(args) != 1 { |
||||
utils.Fatalf("Expected 1 argument - the ref") |
||||
} |
||||
|
||||
var ( |
||||
ae *api.AccessEntry |
||||
actManifest *api.Manifest |
||||
accessKey []byte |
||||
err error |
||||
ref = args[0] |
||||
grantees = []string{} |
||||
actFilename = ctx.String(SwarmAccessGrantKeysFlag.Name) |
||||
privateKey = getPrivKey(ctx) |
||||
dryRun = ctx.Bool(SwarmDryRunFlag.Name) |
||||
) |
||||
|
||||
bytes, err := ioutil.ReadFile(actFilename) |
||||
if err != nil { |
||||
utils.Fatalf("had an error reading the grantee public key list") |
||||
} |
||||
grantees = strings.Split(string(bytes), "\n") |
||||
accessKey, ae, actManifest, err = api.DoACTNew(ctx, privateKey, salt, grantees) |
||||
if err != nil { |
||||
utils.Fatalf("error generating ACT manifest: %v", err) |
||||
} |
||||
|
||||
if err != nil { |
||||
utils.Fatalf("error getting session key: %v", err) |
||||
} |
||||
m, err := api.GenerateAccessControlManifest(ctx, ref, accessKey, ae) |
||||
if err != nil { |
||||
utils.Fatalf("error generating root access manifest: %v", err) |
||||
} |
||||
|
||||
if dryRun { |
||||
err = printManifests(m, actManifest) |
||||
if err != nil { |
||||
utils.Fatalf("had an error printing the manifests: %v", err) |
||||
} |
||||
} else { |
||||
err = uploadManifests(ctx, m, actManifest) |
||||
if err != nil { |
||||
utils.Fatalf("had an error uploading the manifests: %v", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
func printManifests(rootAccessManifest, actManifest *api.Manifest) error { |
||||
js, err := json.Marshal(rootAccessManifest) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
fmt.Println(string(js)) |
||||
|
||||
if actManifest != nil { |
||||
js, err := json.Marshal(actManifest) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
fmt.Println(string(js)) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
func uploadManifests(ctx *cli.Context, rootAccessManifest, actManifest *api.Manifest) error { |
||||
bzzapi := strings.TrimRight(ctx.GlobalString(SwarmApiFlag.Name), "/") |
||||
client := client.NewClient(bzzapi) |
||||
|
||||
var ( |
||||
key string |
||||
err error |
||||
) |
||||
if actManifest != nil { |
||||
key, err = client.UploadManifest(actManifest, false) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
rootAccessManifest.Entries[0].Access.Act = key |
||||
} |
||||
key, err = client.UploadManifest(rootAccessManifest, false) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
fmt.Println(key) |
||||
return nil |
||||
} |
||||
|
||||
// makePasswordList reads password lines from the file specified by the global --password flag
|
||||
// and also by the same subcommand --password flag.
|
||||
// This function ia a fork of utils.MakePasswordList to lookup cli context for subcommand.
|
||||
// Function ctx.SetGlobal is not setting the global flag value that can be accessed
|
||||
// by ctx.GlobalString using the current version of cli package.
|
||||
func makePasswordList(ctx *cli.Context) []string { |
||||
path := ctx.GlobalString(utils.PasswordFileFlag.Name) |
||||
if path == "" { |
||||
path = ctx.String(utils.PasswordFileFlag.Name) |
||||
if path == "" { |
||||
return nil |
||||
} |
||||
} |
||||
text, err := ioutil.ReadFile(path) |
||||
if err != nil { |
||||
utils.Fatalf("Failed to read password file: %v", err) |
||||
} |
||||
lines := strings.Split(string(text), "\n") |
||||
// Sanitise DOS line endings.
|
||||
for i := range lines { |
||||
lines[i] = strings.TrimRight(lines[i], "\r") |
||||
} |
||||
return lines |
||||
} |
@ -0,0 +1,581 @@ |
||||
// Copyright 2018 The go-ethereum Authors
|
||||
// This file is part of go-ethereum.
|
||||
//
|
||||
// go-ethereum is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// go-ethereum is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
||||
package main |
||||
|
||||
import ( |
||||
"bytes" |
||||
"crypto/rand" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"io" |
||||
"io/ioutil" |
||||
gorand "math/rand" |
||||
"net/http" |
||||
"os" |
||||
"path/filepath" |
||||
"strings" |
||||
"testing" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/crypto/sha3" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/swarm/api" |
||||
swarm "github.com/ethereum/go-ethereum/swarm/api/client" |
||||
) |
||||
|
||||
// TestAccessPassword tests for the correct creation of an ACT manifest protected by a password.
|
||||
// The test creates bogus content, uploads it encrypted, then creates the wrapping manifest with the Access entry
|
||||
// The parties participating - node (publisher), uploads to second node then disappears. Content which was uploaded
|
||||
// is then fetched through 2nd node. since the tested code is not key-aware - we can just
|
||||
// fetch from the 2nd node using HTTP BasicAuth
|
||||
func TestAccessPassword(t *testing.T) { |
||||
cluster := newTestCluster(t, 1) |
||||
defer cluster.Shutdown() |
||||
proxyNode := cluster.Nodes[0] |
||||
|
||||
// create a tmp file
|
||||
tmp, err := ioutil.TempDir("", "swarm-test") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer os.RemoveAll(tmp) |
||||
|
||||
// write data to file
|
||||
data := "notsorandomdata" |
||||
dataFilename := filepath.Join(tmp, "data.txt") |
||||
|
||||
err = ioutil.WriteFile(dataFilename, []byte(data), 0666) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
hashRegexp := `[a-f\d]{128}` |
||||
|
||||
// upload the file with 'swarm up' and expect a hash
|
||||
up := runSwarm(t, |
||||
"--bzzapi", |
||||
proxyNode.URL, //it doesn't matter through which node we upload content
|
||||
"up", |
||||
"--encrypt", |
||||
dataFilename) |
||||
_, matches := up.ExpectRegexp(hashRegexp) |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) < 1 { |
||||
t.Fatal("no matches found") |
||||
} |
||||
|
||||
ref := matches[0] |
||||
|
||||
password := "smth" |
||||
passwordFilename := filepath.Join(tmp, "password.txt") |
||||
|
||||
err = ioutil.WriteFile(passwordFilename, []byte(password), 0666) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
up = runSwarm(t, |
||||
"access", |
||||
"new", |
||||
"pass", |
||||
"--dry-run", |
||||
"--password", |
||||
passwordFilename, |
||||
ref, |
||||
) |
||||
|
||||
_, matches = up.ExpectRegexp(".+") |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) == 0 { |
||||
t.Fatalf("stdout not matched") |
||||
} |
||||
|
||||
var m api.Manifest |
||||
|
||||
err = json.Unmarshal([]byte(matches[0]), &m) |
||||
if err != nil { |
||||
t.Fatalf("unmarshal manifest: %v", err) |
||||
} |
||||
|
||||
if len(m.Entries) != 1 { |
||||
t.Fatalf("expected one manifest entry, got %v", len(m.Entries)) |
||||
} |
||||
|
||||
e := m.Entries[0] |
||||
|
||||
ct := "application/bzz-manifest+json" |
||||
if e.ContentType != ct { |
||||
t.Errorf("expected %q content type, got %q", ct, e.ContentType) |
||||
} |
||||
|
||||
if e.Access == nil { |
||||
t.Fatal("manifest access is nil") |
||||
} |
||||
|
||||
a := e.Access |
||||
|
||||
if a.Type != "pass" { |
||||
t.Errorf(`got access type %q, expected "pass"`, a.Type) |
||||
} |
||||
if len(a.Salt) < 32 { |
||||
t.Errorf(`got salt with length %v, expected not less the 32 bytes`, len(a.Salt)) |
||||
} |
||||
if a.KdfParams == nil { |
||||
t.Fatal("manifest access kdf params is nil") |
||||
} |
||||
|
||||
client := swarm.NewClient(cluster.Nodes[0].URL) |
||||
|
||||
hash, err := client.UploadManifest(&m, false) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
httpClient := &http.Client{} |
||||
|
||||
url := cluster.Nodes[0].URL + "/" + "bzz:/" + hash |
||||
response, err := httpClient.Get(url) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if response.StatusCode != http.StatusUnauthorized { |
||||
t.Fatal("should be a 401") |
||||
} |
||||
authHeader := response.Header.Get("WWW-Authenticate") |
||||
if authHeader == "" { |
||||
t.Fatal("should be something here") |
||||
} |
||||
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
req.SetBasicAuth("", password) |
||||
|
||||
response, err = http.DefaultClient.Do(req) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer response.Body.Close() |
||||
|
||||
if response.StatusCode != http.StatusOK { |
||||
t.Errorf("expected status %v, got %v", http.StatusOK, response.StatusCode) |
||||
} |
||||
d, err := ioutil.ReadAll(response.Body) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if string(d) != data { |
||||
t.Errorf("expected decrypted data %q, got %q", data, string(d)) |
||||
} |
||||
|
||||
wrongPasswordFilename := filepath.Join(tmp, "password-wrong.txt") |
||||
|
||||
err = ioutil.WriteFile(wrongPasswordFilename, []byte("just wr0ng"), 0666) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
//download file with 'swarm down' with wrong password
|
||||
up = runSwarm(t, |
||||
"--bzzapi", |
||||
proxyNode.URL, |
||||
"down", |
||||
"bzz:/"+hash, |
||||
tmp, |
||||
"--password", |
||||
wrongPasswordFilename) |
||||
|
||||
_, matches = up.ExpectRegexp("unauthorized") |
||||
if len(matches) != 1 && matches[0] != "unauthorized" { |
||||
t.Fatal(`"unauthorized" not found in output"`) |
||||
} |
||||
up.ExpectExit() |
||||
} |
||||
|
||||
// TestAccessPK tests for the correct creation of an ACT manifest between two parties (publisher and grantee).
|
||||
// The test creates bogus content, uploads it encrypted, then creates the wrapping manifest with the Access entry
|
||||
// The parties participating - node (publisher), uploads to second node (which is also the grantee) then disappears.
|
||||
// Content which was uploaded is then fetched through the grantee's http proxy. Since the tested code is private-key aware,
|
||||
// the test will fail if the proxy's given private key is not granted on the ACT.
|
||||
func TestAccessPK(t *testing.T) { |
||||
// Setup Swarm and upload a test file to it
|
||||
cluster := newTestCluster(t, 1) |
||||
defer cluster.Shutdown() |
||||
|
||||
// create a tmp file
|
||||
tmp, err := ioutil.TempFile("", "swarm-test") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer tmp.Close() |
||||
defer os.Remove(tmp.Name()) |
||||
|
||||
// write data to file
|
||||
data := "notsorandomdata" |
||||
_, err = io.WriteString(tmp, data) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
hashRegexp := `[a-f\d]{128}` |
||||
|
||||
// upload the file with 'swarm up' and expect a hash
|
||||
up := runSwarm(t, |
||||
"--bzzapi", |
||||
cluster.Nodes[0].URL, |
||||
"up", |
||||
"--encrypt", |
||||
tmp.Name()) |
||||
_, matches := up.ExpectRegexp(hashRegexp) |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) < 1 { |
||||
t.Fatal("no matches found") |
||||
} |
||||
|
||||
ref := matches[0] |
||||
|
||||
pk := cluster.Nodes[0].PrivateKey |
||||
granteePubKey := crypto.CompressPubkey(&pk.PublicKey) |
||||
|
||||
publisherDir, err := ioutil.TempDir("", "swarm-account-dir-temp") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
passFile, err := ioutil.TempFile("", "swarm-test") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer passFile.Close() |
||||
defer os.Remove(passFile.Name()) |
||||
_, err = io.WriteString(passFile, testPassphrase) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
_, publisherAccount := getTestAccount(t, publisherDir) |
||||
up = runSwarm(t, |
||||
"--bzzaccount", |
||||
publisherAccount.Address.String(), |
||||
"--password", |
||||
passFile.Name(), |
||||
"--datadir", |
||||
publisherDir, |
||||
"--bzzapi", |
||||
cluster.Nodes[0].URL, |
||||
"access", |
||||
"new", |
||||
"pk", |
||||
"--dry-run", |
||||
"--grant-key", |
||||
hex.EncodeToString(granteePubKey), |
||||
ref, |
||||
) |
||||
|
||||
_, matches = up.ExpectRegexp(".+") |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) == 0 { |
||||
t.Fatalf("stdout not matched") |
||||
} |
||||
|
||||
var m api.Manifest |
||||
|
||||
err = json.Unmarshal([]byte(matches[0]), &m) |
||||
if err != nil { |
||||
t.Fatalf("unmarshal manifest: %v", err) |
||||
} |
||||
|
||||
if len(m.Entries) != 1 { |
||||
t.Fatalf("expected one manifest entry, got %v", len(m.Entries)) |
||||
} |
||||
|
||||
e := m.Entries[0] |
||||
|
||||
ct := "application/bzz-manifest+json" |
||||
if e.ContentType != ct { |
||||
t.Errorf("expected %q content type, got %q", ct, e.ContentType) |
||||
} |
||||
|
||||
if e.Access == nil { |
||||
t.Fatal("manifest access is nil") |
||||
} |
||||
|
||||
a := e.Access |
||||
|
||||
if a.Type != "pk" { |
||||
t.Errorf(`got access type %q, expected "pk"`, a.Type) |
||||
} |
||||
if len(a.Salt) < 32 { |
||||
t.Errorf(`got salt with length %v, expected not less the 32 bytes`, len(a.Salt)) |
||||
} |
||||
if a.KdfParams != nil { |
||||
t.Fatal("manifest access kdf params should be nil") |
||||
} |
||||
|
||||
client := swarm.NewClient(cluster.Nodes[0].URL) |
||||
|
||||
hash, err := client.UploadManifest(&m, false) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
httpClient := &http.Client{} |
||||
|
||||
url := cluster.Nodes[0].URL + "/" + "bzz:/" + hash |
||||
response, err := httpClient.Get(url) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if response.StatusCode != http.StatusOK { |
||||
t.Fatal("should be a 200") |
||||
} |
||||
d, err := ioutil.ReadAll(response.Body) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if string(d) != data { |
||||
t.Errorf("expected decrypted data %q, got %q", data, string(d)) |
||||
} |
||||
} |
||||
|
||||
// TestAccessACT tests the e2e creation, uploading and downloading of an ACT type access control
|
||||
// the test fires up a 3 node cluster, then randomly picks 2 nodes which will be acting as grantees to the data
|
||||
// set. the third node should fail decoding the reference as it will not be granted access. the publisher uploads through
|
||||
// one of the nodes then disappears.
|
||||
func TestAccessACT(t *testing.T) { |
||||
// Setup Swarm and upload a test file to it
|
||||
cluster := newTestCluster(t, 3) |
||||
defer cluster.Shutdown() |
||||
|
||||
var uploadThroughNode = cluster.Nodes[0] |
||||
client := swarm.NewClient(uploadThroughNode.URL) |
||||
|
||||
r1 := gorand.New(gorand.NewSource(time.Now().UnixNano())) |
||||
nodeToSkip := r1.Intn(3) // a number between 0 and 2 (node indices in `cluster`)
|
||||
// create a tmp file
|
||||
tmp, err := ioutil.TempFile("", "swarm-test") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer tmp.Close() |
||||
defer os.Remove(tmp.Name()) |
||||
|
||||
// write data to file
|
||||
data := "notsorandomdata" |
||||
_, err = io.WriteString(tmp, data) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
hashRegexp := `[a-f\d]{128}` |
||||
|
||||
// upload the file with 'swarm up' and expect a hash
|
||||
up := runSwarm(t, |
||||
"--bzzapi", |
||||
cluster.Nodes[0].URL, |
||||
"up", |
||||
"--encrypt", |
||||
tmp.Name()) |
||||
_, matches := up.ExpectRegexp(hashRegexp) |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) < 1 { |
||||
t.Fatal("no matches found") |
||||
} |
||||
|
||||
ref := matches[0] |
||||
grantees := []string{} |
||||
for i, v := range cluster.Nodes { |
||||
if i == nodeToSkip { |
||||
continue |
||||
} |
||||
pk := v.PrivateKey |
||||
granteePubKey := crypto.CompressPubkey(&pk.PublicKey) |
||||
grantees = append(grantees, hex.EncodeToString(granteePubKey)) |
||||
} |
||||
|
||||
granteesPubkeyListFile, err := ioutil.TempFile("", "grantees-pubkey-list.csv") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, err = granteesPubkeyListFile.WriteString(strings.Join(grantees, "\n")) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
defer granteesPubkeyListFile.Close() |
||||
defer os.Remove(granteesPubkeyListFile.Name()) |
||||
|
||||
publisherDir, err := ioutil.TempDir("", "swarm-account-dir-temp") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
passFile, err := ioutil.TempFile("", "swarm-test") |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
defer passFile.Close() |
||||
defer os.Remove(passFile.Name()) |
||||
_, err = io.WriteString(passFile, testPassphrase) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
_, publisherAccount := getTestAccount(t, publisherDir) |
||||
up = runSwarm(t, |
||||
"--bzzaccount", |
||||
publisherAccount.Address.String(), |
||||
"--password", |
||||
passFile.Name(), |
||||
"--datadir", |
||||
publisherDir, |
||||
"--bzzapi", |
||||
cluster.Nodes[0].URL, |
||||
"access", |
||||
"new", |
||||
"act", |
||||
"--grant-keys", |
||||
granteesPubkeyListFile.Name(), |
||||
ref, |
||||
) |
||||
|
||||
_, matches = up.ExpectRegexp(`[a-f\d]{64}`) |
||||
up.ExpectExit() |
||||
|
||||
if len(matches) == 0 { |
||||
t.Fatalf("stdout not matched") |
||||
} |
||||
hash := matches[0] |
||||
m, _, err := client.DownloadManifest(hash) |
||||
if err != nil { |
||||
t.Fatalf("unmarshal manifest: %v", err) |
||||
} |
||||
|
||||
if len(m.Entries) != 1 { |
||||
t.Fatalf("expected one manifest entry, got %v", len(m.Entries)) |
||||
} |
||||
|
||||
e := m.Entries[0] |
||||
|
||||
ct := "application/bzz-manifest+json" |
||||
if e.ContentType != ct { |
||||
t.Errorf("expected %q content type, got %q", ct, e.ContentType) |
||||
} |
||||
|
||||
if e.Access == nil { |
||||
t.Fatal("manifest access is nil") |
||||
} |
||||
|
||||
a := e.Access |
||||
|
||||
if a.Type != "act" { |
||||
t.Fatalf(`got access type %q, expected "act"`, a.Type) |
||||
} |
||||
if len(a.Salt) < 32 { |
||||
t.Fatalf(`got salt with length %v, expected not less the 32 bytes`, len(a.Salt)) |
||||
} |
||||
if a.KdfParams != nil { |
||||
t.Fatal("manifest access kdf params should be nil") |
||||
} |
||||
|
||||
httpClient := &http.Client{} |
||||
|
||||
// all nodes except the skipped node should be able to decrypt the content
|
||||
for i, node := range cluster.Nodes { |
||||
log.Debug("trying to fetch from node", "node index", i) |
||||
|
||||
url := node.URL + "/" + "bzz:/" + hash |
||||
response, err := httpClient.Get(url) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
log.Debug("got response from node", "response code", response.StatusCode) |
||||
|
||||
if i == nodeToSkip { |
||||
log.Debug("reached node to skip", "status code", response.StatusCode) |
||||
|
||||
if response.StatusCode != http.StatusUnauthorized { |
||||
t.Fatalf("should be a 401") |
||||
} |
||||
|
||||
continue |
||||
} |
||||
|
||||
if response.StatusCode != http.StatusOK { |
||||
t.Fatal("should be a 200") |
||||
} |
||||
d, err := ioutil.ReadAll(response.Body) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
if string(d) != data { |
||||
t.Errorf("expected decrypted data %q, got %q", data, string(d)) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// TestKeypairSanity is a sanity test for the crypto scheme for ACT. it asserts the correct shared secret according to
|
||||
// the specs at https://github.com/ethersphere/swarm-docs/blob/eb857afda906c6e7bb90d37f3f334ccce5eef230/act.md
|
||||
func TestKeypairSanity(t *testing.T) { |
||||
salt := make([]byte, 32) |
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil { |
||||
t.Fatalf("reading from crypto/rand failed: %v", err.Error()) |
||||
} |
||||
sharedSecret := "a85586744a1ddd56a7ed9f33fa24f40dd745b3a941be296a0d60e329dbdb896d" |
||||
|
||||
for i, v := range []struct { |
||||
publisherPriv string |
||||
granteePub string |
||||
}{ |
||||
{ |
||||
publisherPriv: "ec5541555f3bc6376788425e9d1a62f55a82901683fd7062c5eddcc373a73459", |
||||
granteePub: "0226f213613e843a413ad35b40f193910d26eb35f00154afcde9ded57479a6224a", |
||||
}, |
||||
{ |
||||
publisherPriv: "70c7a73011aa56584a0009ab874794ee7e5652fd0c6911cd02f8b6267dd82d2d", |
||||
granteePub: "02e6f8d5e28faaa899744972bb847b6eb805a160494690c9ee7197ae9f619181db", |
||||
}, |
||||
} { |
||||
b, _ := hex.DecodeString(v.granteePub) |
||||
granteePub, _ := crypto.DecompressPubkey(b) |
||||
publisherPrivate, _ := crypto.HexToECDSA(v.publisherPriv) |
||||
|
||||
ssKey, err := api.NewSessionKeyPK(publisherPrivate, granteePub, salt) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
|
||||
hasher := sha3.NewKeccak256() |
||||
hasher.Write(salt) |
||||
shared, err := hex.DecodeString(sharedSecret) |
||||
if err != nil { |
||||
t.Fatal(err) |
||||
} |
||||
hasher.Write(shared) |
||||
sum := hasher.Sum(nil) |
||||
|
||||
if !bytes.Equal(ssKey, sum) { |
||||
t.Fatalf("%d: got a session key mismatch", i) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,468 @@ |
||||
package api |
||||
|
||||
import ( |
||||
"context" |
||||
"crypto/ecdsa" |
||||
"crypto/rand" |
||||
"encoding/hex" |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"io" |
||||
"strings" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/crypto/ecies" |
||||
"github.com/ethereum/go-ethereum/crypto/sha3" |
||||
"github.com/ethereum/go-ethereum/swarm/log" |
||||
"github.com/ethereum/go-ethereum/swarm/sctx" |
||||
"github.com/ethereum/go-ethereum/swarm/storage" |
||||
"golang.org/x/crypto/scrypt" |
||||
cli "gopkg.in/urfave/cli.v1" |
||||
) |
||||
|
||||
var ( |
||||
ErrDecrypt = errors.New("cant decrypt - forbidden") |
||||
ErrUnknownAccessType = errors.New("unknown access type (or not implemented)") |
||||
ErrDecryptDomainForbidden = errors.New("decryption request domain forbidden - can only decrypt on localhost") |
||||
AllowedDecryptDomains = []string{ |
||||
"localhost", |
||||
"127.0.0.1", |
||||
} |
||||
) |
||||
|
||||
const EMPTY_CREDENTIALS = "" |
||||
|
||||
type AccessEntry struct { |
||||
Type AccessType |
||||
Publisher string |
||||
Salt []byte |
||||
Act string |
||||
KdfParams *KdfParams |
||||
} |
||||
|
||||
type DecryptFunc func(*ManifestEntry) error |
||||
|
||||
func (a *AccessEntry) MarshalJSON() (out []byte, err error) { |
||||
|
||||
return json.Marshal(struct { |
||||
Type AccessType `json:"type,omitempty"` |
||||
Publisher string `json:"publisher,omitempty"` |
||||
Salt string `json:"salt,omitempty"` |
||||
Act string `json:"act,omitempty"` |
||||
KdfParams *KdfParams `json:"kdf_params,omitempty"` |
||||
}{ |
||||
Type: a.Type, |
||||
Publisher: a.Publisher, |
||||
Salt: hex.EncodeToString(a.Salt), |
||||
Act: a.Act, |
||||
KdfParams: a.KdfParams, |
||||
}) |
||||
|
||||
} |
||||
|
||||
func (a *AccessEntry) UnmarshalJSON(value []byte) error { |
||||
v := struct { |
||||
Type AccessType `json:"type,omitempty"` |
||||
Publisher string `json:"publisher,omitempty"` |
||||
Salt string `json:"salt,omitempty"` |
||||
Act string `json:"act,omitempty"` |
||||
KdfParams *KdfParams `json:"kdf_params,omitempty"` |
||||
}{} |
||||
|
||||
err := json.Unmarshal(value, &v) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
a.Act = v.Act |
||||
a.KdfParams = v.KdfParams |
||||
a.Publisher = v.Publisher |
||||
a.Salt, err = hex.DecodeString(v.Salt) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if len(a.Salt) != 32 { |
||||
return errors.New("salt should be 32 bytes long") |
||||
} |
||||
a.Type = v.Type |
||||
return nil |
||||
} |
||||
|
||||
type KdfParams struct { |
||||
N int `json:"n"` |
||||
P int `json:"p"` |
||||
R int `json:"r"` |
||||
} |
||||
|
||||
type AccessType string |
||||
|
||||
const AccessTypePass = AccessType("pass") |
||||
const AccessTypePK = AccessType("pk") |
||||
const AccessTypeACT = AccessType("act") |
||||
|
||||
func NewAccessEntryPassword(salt []byte, kdfParams *KdfParams) (*AccessEntry, error) { |
||||
if len(salt) != 32 { |
||||
return nil, fmt.Errorf("salt should be 32 bytes long") |
||||
} |
||||
return &AccessEntry{ |
||||
Type: AccessTypePass, |
||||
Salt: salt, |
||||
KdfParams: kdfParams, |
||||
}, nil |
||||
} |
||||
|
||||
func NewAccessEntryPK(publisher string, salt []byte) (*AccessEntry, error) { |
||||
if len(publisher) != 66 { |
||||
return nil, fmt.Errorf("publisher should be 66 characters long, got %d", len(publisher)) |
||||
} |
||||
if len(salt) != 32 { |
||||
return nil, fmt.Errorf("salt should be 32 bytes long") |
||||
} |
||||
return &AccessEntry{ |
||||
Type: AccessTypePK, |
||||
Publisher: publisher, |
||||
Salt: salt, |
||||
}, nil |
||||
} |
||||
|
||||
func NewAccessEntryACT(publisher string, salt []byte, act string) (*AccessEntry, error) { |
||||
if len(salt) != 32 { |
||||
return nil, fmt.Errorf("salt should be 32 bytes long") |
||||
} |
||||
if len(publisher) != 66 { |
||||
return nil, fmt.Errorf("publisher should be 66 characters long") |
||||
} |
||||
|
||||
return &AccessEntry{ |
||||
Type: AccessTypeACT, |
||||
Publisher: publisher, |
||||
Salt: salt, |
||||
Act: act, |
||||
}, nil |
||||
} |
||||
|
||||
func NOOPDecrypt(*ManifestEntry) error { |
||||
return nil |
||||
} |
||||
|
||||
var DefaultKdfParams = NewKdfParams(262144, 1, 8) |
||||
|
||||
func NewKdfParams(n, p, r int) *KdfParams { |
||||
|
||||
return &KdfParams{ |
||||
N: n, |
||||
P: p, |
||||
R: r, |
||||
} |
||||
} |
||||
|
||||
// NewSessionKeyPassword creates a session key based on a shared secret (password) and the given salt
|
||||
// and kdf parameters in the access entry
|
||||
func NewSessionKeyPassword(password string, accessEntry *AccessEntry) ([]byte, error) { |
||||
if accessEntry.Type != AccessTypePass { |
||||
return nil, errors.New("incorrect access entry type") |
||||
} |
||||
return scrypt.Key( |
||||
[]byte(password), |
||||
accessEntry.Salt, |
||||
accessEntry.KdfParams.N, |
||||
accessEntry.KdfParams.R, |
||||
accessEntry.KdfParams.P, |
||||
32, |
||||
) |
||||
} |
||||
|
||||
// NewSessionKeyPK creates a new ACT Session Key using an ECDH shared secret for the given key pair and the given salt value
|
||||
func NewSessionKeyPK(private *ecdsa.PrivateKey, public *ecdsa.PublicKey, salt []byte) ([]byte, error) { |
||||
granteePubEcies := ecies.ImportECDSAPublic(public) |
||||
privateKey := ecies.ImportECDSA(private) |
||||
|
||||
bytes, err := privateKey.GenerateShared(granteePubEcies, 16, 16) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
bytes = append(salt, bytes...) |
||||
sessionKey := crypto.Keccak256(bytes) |
||||
return sessionKey, nil |
||||
} |
||||
|
||||
func (a *API) NodeSessionKey(privateKey *ecdsa.PrivateKey, publicKey *ecdsa.PublicKey, salt []byte) ([]byte, error) { |
||||
return NewSessionKeyPK(privateKey, publicKey, salt) |
||||
} |
||||
func (a *API) doDecrypt(ctx context.Context, credentials string, pk *ecdsa.PrivateKey) DecryptFunc { |
||||
return func(m *ManifestEntry) error { |
||||
if m.Access == nil { |
||||
return nil |
||||
} |
||||
|
||||
allowed := false |
||||
requestDomain := sctx.GetHost(ctx) |
||||
for _, v := range AllowedDecryptDomains { |
||||
if strings.Contains(requestDomain, v) { |
||||
allowed = true |
||||
} |
||||
} |
||||
|
||||
if !allowed { |
||||
return ErrDecryptDomainForbidden |
||||
} |
||||
|
||||
switch m.Access.Type { |
||||
case "pass": |
||||
if credentials != "" { |
||||
key, err := NewSessionKeyPassword(credentials, m.Access) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
ref, err := hex.DecodeString(m.Hash) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
enc := NewRefEncryption(len(ref) - 8) |
||||
decodedRef, err := enc.Decrypt(ref, key) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
m.Hash = hex.EncodeToString(decodedRef) |
||||
m.Access = nil |
||||
return nil |
||||
} |
||||
return ErrDecrypt |
||||
case "pk": |
||||
publisherBytes, err := hex.DecodeString(m.Access.Publisher) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
publisher, err := crypto.DecompressPubkey(publisherBytes) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
key, err := a.NodeSessionKey(pk, publisher, m.Access.Salt) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
ref, err := hex.DecodeString(m.Hash) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
enc := NewRefEncryption(len(ref) - 8) |
||||
decodedRef, err := enc.Decrypt(ref, key) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
m.Hash = hex.EncodeToString(decodedRef) |
||||
m.Access = nil |
||||
return nil |
||||
case "act": |
||||
publisherBytes, err := hex.DecodeString(m.Access.Publisher) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
publisher, err := crypto.DecompressPubkey(publisherBytes) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
sessionKey, err := a.NodeSessionKey(pk, publisher, m.Access.Salt) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
hasher := sha3.NewKeccak256() |
||||
hasher.Write(append(sessionKey, 0)) |
||||
lookupKey := hasher.Sum(nil) |
||||
|
||||
hasher.Reset() |
||||
|
||||
hasher.Write(append(sessionKey, 1)) |
||||
accessKeyDecryptionKey := hasher.Sum(nil) |
||||
|
||||
lk := hex.EncodeToString(lookupKey) |
||||
list, err := a.GetManifestList(ctx, NOOPDecrypt, storage.Address(common.Hex2Bytes(m.Access.Act)), lk) |
||||
|
||||
found := "" |
||||
for _, v := range list.Entries { |
||||
if v.Path == lk { |
||||
found = v.Hash |
||||
} |
||||
} |
||||
|
||||
if found == "" { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
v, err := hex.DecodeString(found) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
enc := NewRefEncryption(len(v) - 8) |
||||
decodedRef, err := enc.Decrypt(v, accessKeyDecryptionKey) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
|
||||
ref, err := hex.DecodeString(m.Hash) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
enc = NewRefEncryption(len(ref) - 8) |
||||
decodedMainRef, err := enc.Decrypt(ref, decodedRef) |
||||
if err != nil { |
||||
return ErrDecrypt |
||||
} |
||||
m.Hash = hex.EncodeToString(decodedMainRef) |
||||
m.Access = nil |
||||
return nil |
||||
} |
||||
return ErrUnknownAccessType |
||||
} |
||||
} |
||||
|
||||
func GenerateAccessControlManifest(ctx *cli.Context, ref string, accessKey []byte, ae *AccessEntry) (*Manifest, error) { |
||||
refBytes, err := hex.DecodeString(ref) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// encrypt ref with accessKey
|
||||
enc := NewRefEncryption(len(refBytes)) |
||||
encrypted, err := enc.Encrypt(refBytes, accessKey) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
m := &Manifest{ |
||||
Entries: []ManifestEntry{ |
||||
{ |
||||
Hash: hex.EncodeToString(encrypted), |
||||
ContentType: ManifestType, |
||||
ModTime: time.Now(), |
||||
Access: ae, |
||||
}, |
||||
}, |
||||
} |
||||
|
||||
return m, nil |
||||
} |
||||
|
||||
func DoPKNew(ctx *cli.Context, privateKey *ecdsa.PrivateKey, granteePublicKey string, salt []byte) (sessionKey []byte, ae *AccessEntry, err error) { |
||||
if granteePublicKey == "" { |
||||
return nil, nil, errors.New("need a grantee Public Key") |
||||
} |
||||
b, err := hex.DecodeString(granteePublicKey) |
||||
if err != nil { |
||||
log.Error("error decoding grantee public key", "err", err) |
||||
return nil, nil, err |
||||
} |
||||
|
||||
granteePub, err := crypto.DecompressPubkey(b) |
||||
if err != nil { |
||||
log.Error("error decompressing grantee public key", "err", err) |
||||
return nil, nil, err |
||||
} |
||||
|
||||
sessionKey, err = NewSessionKeyPK(privateKey, granteePub, salt) |
||||
if err != nil { |
||||
log.Error("error getting session key", "err", err) |
||||
return nil, nil, err |
||||
} |
||||
|
||||
ae, err = NewAccessEntryPK(hex.EncodeToString(crypto.CompressPubkey(&privateKey.PublicKey)), salt) |
||||
if err != nil { |
||||
log.Error("error generating access entry", "err", err) |
||||
return nil, nil, err |
||||
} |
||||
|
||||
return sessionKey, ae, nil |
||||
} |
||||
|
||||
func DoACTNew(ctx *cli.Context, privateKey *ecdsa.PrivateKey, salt []byte, grantees []string) (accessKey []byte, ae *AccessEntry, actManifest *Manifest, err error) { |
||||
if len(grantees) == 0 { |
||||
return nil, nil, nil, errors.New("did not get any grantee public keys") |
||||
} |
||||
|
||||
publisherPub := hex.EncodeToString(crypto.CompressPubkey(&privateKey.PublicKey)) |
||||
grantees = append(grantees, publisherPub) |
||||
|
||||
accessKey = make([]byte, 32) |
||||
if _, err := io.ReadFull(rand.Reader, salt); err != nil { |
||||
panic("reading from crypto/rand failed: " + err.Error()) |
||||
} |
||||
if _, err := io.ReadFull(rand.Reader, accessKey); err != nil { |
||||
panic("reading from crypto/rand failed: " + err.Error()) |
||||
} |
||||
|
||||
lookupPathEncryptedAccessKeyMap := make(map[string]string) |
||||
i := 0 |
||||
for _, v := range grantees { |
||||
i++ |
||||
if v == "" { |
||||
return nil, nil, nil, errors.New("need a grantee Public Key") |
||||
} |
||||
b, err := hex.DecodeString(v) |
||||
if err != nil { |
||||
log.Error("error decoding grantee public key", "err", err) |
||||
return nil, nil, nil, err |
||||
} |
||||
|
||||
granteePub, err := crypto.DecompressPubkey(b) |
||||
if err != nil { |
||||
log.Error("error decompressing grantee public key", "err", err) |
||||
return nil, nil, nil, err |
||||
} |
||||
sessionKey, err := NewSessionKeyPK(privateKey, granteePub, salt) |
||||
|
||||
hasher := sha3.NewKeccak256() |
||||
hasher.Write(append(sessionKey, 0)) |
||||
lookupKey := hasher.Sum(nil) |
||||
|
||||
hasher.Reset() |
||||
hasher.Write(append(sessionKey, 1)) |
||||
|
||||
accessKeyEncryptionKey := hasher.Sum(nil) |
||||
|
||||
enc := NewRefEncryption(len(accessKey)) |
||||
encryptedAccessKey, err := enc.Encrypt(accessKey, accessKeyEncryptionKey) |
||||
|
||||
lookupPathEncryptedAccessKeyMap[hex.EncodeToString(lookupKey)] = hex.EncodeToString(encryptedAccessKey) |
||||
} |
||||
|
||||
m := &Manifest{ |
||||
Entries: []ManifestEntry{}, |
||||
} |
||||
|
||||
for k, v := range lookupPathEncryptedAccessKeyMap { |
||||
m.Entries = append(m.Entries, ManifestEntry{ |
||||
Path: k, |
||||
Hash: v, |
||||
ContentType: "text/plain", |
||||
}) |
||||
} |
||||
|
||||
ae, err = NewAccessEntryACT(hex.EncodeToString(crypto.CompressPubkey(&privateKey.PublicKey)), salt, "") |
||||
if err != nil { |
||||
return nil, nil, nil, err |
||||
} |
||||
|
||||
return accessKey, ae, m, nil |
||||
} |
||||
|
||||
func DoPasswordNew(ctx *cli.Context, password string, salt []byte) (sessionKey []byte, ae *AccessEntry, err error) { |
||||
ae, err = NewAccessEntryPassword(salt, DefaultKdfParams) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
|
||||
sessionKey, err = NewSessionKeyPassword(password, ae) |
||||
if err != nil { |
||||
return nil, nil, err |
||||
} |
||||
return sessionKey, ae, nil |
||||
} |
@ -0,0 +1,76 @@ |
||||
// Copyright 2016 The go-ethereum Authors
|
||||
// This file is part of the go-ethereum library.
|
||||
//
|
||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Lesser General Public License as published by
|
||||
// the Free Software Foundation, either version 3 of the License, or
|
||||
// (at your option) any later version.
|
||||
//
|
||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Lesser General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Lesser General Public License
|
||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
package api |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
"errors" |
||||
|
||||
"github.com/ethereum/go-ethereum/crypto/sha3" |
||||
"github.com/ethereum/go-ethereum/swarm/storage/encryption" |
||||
) |
||||
|
||||
type RefEncryption struct { |
||||
spanEncryption encryption.Encryption |
||||
dataEncryption encryption.Encryption |
||||
span []byte |
||||
} |
||||
|
||||
func NewRefEncryption(refSize int) *RefEncryption { |
||||
span := make([]byte, 8) |
||||
binary.LittleEndian.PutUint64(span, uint64(refSize)) |
||||
return &RefEncryption{ |
||||
spanEncryption: encryption.New(0, uint32(refSize/32), sha3.NewKeccak256), |
||||
dataEncryption: encryption.New(refSize, 0, sha3.NewKeccak256), |
||||
span: span, |
||||
} |
||||
} |
||||
|
||||
func (re *RefEncryption) Encrypt(ref []byte, key []byte) ([]byte, error) { |
||||
encryptedSpan, err := re.spanEncryption.Encrypt(re.span, key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
encryptedData, err := re.dataEncryption.Encrypt(ref, key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
encryptedRef := make([]byte, len(ref)+8) |
||||
copy(encryptedRef[:8], encryptedSpan) |
||||
copy(encryptedRef[8:], encryptedData) |
||||
|
||||
return encryptedRef, nil |
||||
} |
||||
|
||||
func (re *RefEncryption) Decrypt(ref []byte, key []byte) ([]byte, error) { |
||||
decryptedSpan, err := re.spanEncryption.Decrypt(ref[:8], key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
size := binary.LittleEndian.Uint64(decryptedSpan) |
||||
if size != uint64(len(ref)-8) { |
||||
return nil, errors.New("invalid span in encrypted reference") |
||||
} |
||||
|
||||
decryptedRef, err := re.dataEncryption.Decrypt(ref[8:], key) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return decryptedRef, nil |
||||
} |
@ -1,7 +1,22 @@ |
||||
package sctx |
||||
|
||||
import "context" |
||||
|
||||
type ContextKey int |
||||
|
||||
const ( |
||||
HTTPRequestIDKey ContextKey = iota |
||||
requestHostKey |
||||
) |
||||
|
||||
func SetHost(ctx context.Context, domain string) context.Context { |
||||
return context.WithValue(ctx, requestHostKey, domain) |
||||
} |
||||
|
||||
func GetHost(ctx context.Context) string { |
||||
v, ok := ctx.Value(requestHostKey).(string) |
||||
if ok { |
||||
return v |
||||
} |
||||
return "" |
||||
} |
||||
|
Loading…
Reference in new issue