|
@ -6,7 +6,6 @@ like standard ssh key storage. |
|
|
package filestorage |
|
|
package filestorage |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"encoding/hex" |
|
|
|
|
|
"fmt" |
|
|
"fmt" |
|
|
"io/ioutil" |
|
|
"io/ioutil" |
|
|
"os" |
|
|
"os" |
|
@ -14,13 +13,11 @@ import ( |
|
|
"strings" |
|
|
"strings" |
|
|
|
|
|
|
|
|
"github.com/pkg/errors" |
|
|
"github.com/pkg/errors" |
|
|
|
|
|
|
|
|
crypto "github.com/tendermint/go-crypto" |
|
|
crypto "github.com/tendermint/go-crypto" |
|
|
keys "github.com/tendermint/go-crypto/keys" |
|
|
keys "github.com/tendermint/go-crypto/keys" |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
const ( |
|
|
const ( |
|
|
// BlockType is the type of block.
|
|
|
|
|
|
BlockType = "Tendermint Light Client" |
|
|
BlockType = "Tendermint Light Client" |
|
|
|
|
|
|
|
|
// PrivExt is the extension for private keys.
|
|
|
// PrivExt is the extension for private keys.
|
|
@ -33,7 +30,6 @@ const ( |
|
|
dirPerm = os.FileMode(0700) |
|
|
dirPerm = os.FileMode(0700) |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
// FileStore is a file-based key storage with tight permissions.
|
|
|
|
|
|
type FileStore struct { |
|
|
type FileStore struct { |
|
|
keyDir string |
|
|
keyDir string |
|
|
} |
|
|
} |
|
@ -44,11 +40,9 @@ type FileStore struct { |
|
|
// be created if it doesn't exist already.
|
|
|
// be created if it doesn't exist already.
|
|
|
func New(dir string) FileStore { |
|
|
func New(dir string) FileStore { |
|
|
err := os.MkdirAll(dir, dirPerm) |
|
|
err := os.MkdirAll(dir, dirPerm) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
panic(err) |
|
|
panic(err) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
return FileStore{dir} |
|
|
return FileStore{dir} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -57,7 +51,7 @@ var _ keys.Storage = FileStore{} |
|
|
|
|
|
|
|
|
// Put creates two files, one with the public info as json, the other
|
|
|
// Put creates two files, one with the public info as json, the other
|
|
|
// with the (encoded) private key as gpg ascii-armor style
|
|
|
// with the (encoded) private key as gpg ascii-armor style
|
|
|
func (s FileStore) Put(name string, salt, key []byte, info keys.Info) error { |
|
|
|
|
|
|
|
|
func (s FileStore) Put(name string, key []byte, info keys.Info) error { |
|
|
pub, priv := s.nameToPaths(name) |
|
|
pub, priv := s.nameToPaths(name) |
|
|
|
|
|
|
|
|
// write public info
|
|
|
// write public info
|
|
@ -67,22 +61,22 @@ func (s FileStore) Put(name string, salt, key []byte, info keys.Info) error { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// write private info
|
|
|
// write private info
|
|
|
return write(priv, name, salt, key) |
|
|
|
|
|
|
|
|
return write(priv, name, key) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// Get loads the info and (encoded) private key from the directory
|
|
|
// Get loads the info and (encoded) private key from the directory
|
|
|
// It uses `name` to generate the filename, and returns an error if the
|
|
|
// It uses `name` to generate the filename, and returns an error if the
|
|
|
// files don't exist or are in the incorrect format
|
|
|
// files don't exist or are in the incorrect format
|
|
|
func (s FileStore) Get(name string) (salt []byte, key []byte, info keys.Info, err error) { |
|
|
|
|
|
|
|
|
func (s FileStore) Get(name string) ([]byte, keys.Info, error) { |
|
|
pub, priv := s.nameToPaths(name) |
|
|
pub, priv := s.nameToPaths(name) |
|
|
|
|
|
|
|
|
info, err = readInfo(pub) |
|
|
|
|
|
|
|
|
info, err := readInfo(pub) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, nil, info, err |
|
|
|
|
|
|
|
|
return nil, info, err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
salt, key, _, err = read(priv) |
|
|
|
|
|
return salt, key, info.Format(), err |
|
|
|
|
|
|
|
|
key, _, err := read(priv) |
|
|
|
|
|
return key, info.Format(), err |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// List parses the key directory for public info and returns a list of
|
|
|
// List parses the key directory for public info and returns a list of
|
|
@ -121,117 +115,63 @@ func (s FileStore) List() (keys.Infos, error) { |
|
|
func (s FileStore) Delete(name string) error { |
|
|
func (s FileStore) Delete(name string) error { |
|
|
pub, priv := s.nameToPaths(name) |
|
|
pub, priv := s.nameToPaths(name) |
|
|
err := os.Remove(priv) |
|
|
err := os.Remove(priv) |
|
|
|
|
|
|
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return errors.Wrap(err, "Deleting Private Key") |
|
|
return errors.Wrap(err, "Deleting Private Key") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
err = os.Remove(pub) |
|
|
err = os.Remove(pub) |
|
|
|
|
|
|
|
|
return errors.Wrap(err, "Deleting Public Key") |
|
|
return errors.Wrap(err, "Deleting Public Key") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func (s FileStore) nameToPaths(name string) (pub, priv string) { |
|
|
func (s FileStore) nameToPaths(name string) (pub, priv string) { |
|
|
privName := fmt.Sprintf("%s.%s", name, PrivExt) |
|
|
privName := fmt.Sprintf("%s.%s", name, PrivExt) |
|
|
pubName := fmt.Sprintf("%s.%s", name, PubExt) |
|
|
pubName := fmt.Sprintf("%s.%s", name, PubExt) |
|
|
|
|
|
|
|
|
return path.Join(s.keyDir, pubName), path.Join(s.keyDir, privName) |
|
|
return path.Join(s.keyDir, pubName), path.Join(s.keyDir, privName) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func readInfo(path string) (info keys.Info, err error) { |
|
|
|
|
|
f, err := os.Open(path) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return info, errors.Wrap(err, "Reading data") |
|
|
|
|
|
} |
|
|
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
|
|
d, err := ioutil.ReadAll(f) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return info, errors.Wrap(err, "Reading data") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func writeInfo(path string, info keys.Info) error { |
|
|
|
|
|
return write(path, info.Name, info.PubKey.Bytes()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
block, headers, key, err := crypto.DecodeArmor(string(d)) |
|
|
|
|
|
|
|
|
func readInfo(path string) (info keys.Info, err error) { |
|
|
|
|
|
var data []byte |
|
|
|
|
|
data, info.Name, err = read(path) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return info, errors.Wrap(err, "Invalid Armor") |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if block != BlockType { |
|
|
|
|
|
return info, errors.Errorf("Unknown key type: %s", block) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pk, _ := crypto.PubKeyFromBytes(key) |
|
|
|
|
|
info.Name = headers["name"] |
|
|
|
|
|
|
|
|
pk, err := crypto.PubKeyFromBytes(data) |
|
|
info.PubKey = pk |
|
|
info.PubKey = pk |
|
|
|
|
|
|
|
|
return info, nil |
|
|
|
|
|
|
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func read(path string) (salt, key []byte, name string, err error) { |
|
|
|
|
|
|
|
|
func read(path string) ([]byte, string, error) { |
|
|
f, err := os.Open(path) |
|
|
f, err := os.Open(path) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, nil, "", errors.Wrap(err, "Reading data") |
|
|
|
|
|
|
|
|
return nil, "", errors.Wrap(err, "Reading data") |
|
|
} |
|
|
} |
|
|
defer f.Close() |
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
d, err := ioutil.ReadAll(f) |
|
|
d, err := ioutil.ReadAll(f) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, nil, "", errors.Wrap(err, "Reading data") |
|
|
|
|
|
|
|
|
return nil, "", errors.Wrap(err, "Reading data") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
block, headers, key, err := crypto.DecodeArmor(string(d)) |
|
|
block, headers, key, err := crypto.DecodeArmor(string(d)) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return nil, nil, "", errors.Wrap(err, "Invalid Armor") |
|
|
|
|
|
|
|
|
return nil, "", errors.Wrap(err, "Invalid Armor") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if block != BlockType { |
|
|
if block != BlockType { |
|
|
return nil, nil, "", errors.Errorf("Unknown key type: %s", block) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if headers["kdf"] != "bcrypt" { |
|
|
|
|
|
return nil, nil, "", errors.Errorf("Unrecognized KDF type: %v", headers["kdf"]) |
|
|
|
|
|
|
|
|
return nil, "", errors.Errorf("Unknown key type: %s", block) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if headers["salt"] == "" { |
|
|
|
|
|
return nil, nil, "", errors.Errorf("Missing salt bytes") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
salt, err = hex.DecodeString(headers["salt"]) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return nil, nil, "", errors.Errorf("Error decoding salt: %v", err.Error()) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return salt, key, headers["name"], nil |
|
|
|
|
|
|
|
|
return key, headers["name"], nil |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
func writeInfo(path string, info keys.Info) error { |
|
|
|
|
|
|
|
|
func write(path, name string, key []byte) error { |
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm) |
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return errors.Wrap(err, "Writing data") |
|
|
return errors.Wrap(err, "Writing data") |
|
|
} |
|
|
} |
|
|
defer f.Close() |
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
headers := map[string]string{"name": info.Name} |
|
|
|
|
|
text := crypto.EncodeArmor(BlockType, headers, info.PubKey.Bytes()) |
|
|
|
|
|
_, err = f.WriteString(text) |
|
|
|
|
|
|
|
|
|
|
|
return errors.Wrap(err, "Writing data") |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func write(path, name string, salt, key []byte) error { |
|
|
|
|
|
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, keyPerm) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return errors.Wrap(err, "Writing data") |
|
|
|
|
|
} |
|
|
|
|
|
defer f.Close() |
|
|
|
|
|
|
|
|
|
|
|
headers := map[string]string{ |
|
|
|
|
|
"name": name, |
|
|
|
|
|
"kdf": "bcrypt", |
|
|
|
|
|
"salt": fmt.Sprintf("%X", salt), |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
headers := map[string]string{"name": name} |
|
|
text := crypto.EncodeArmor(BlockType, headers, key) |
|
|
text := crypto.EncodeArmor(BlockType, headers, key) |
|
|
_, err = f.WriteString(text) |
|
|
_, err = f.WriteString(text) |
|
|
|
|
|
|
|
|
return errors.Wrap(err, "Writing data") |
|
|
return errors.Wrap(err, "Writing data") |
|
|
} |
|
|
} |