2014-12-31 16:39:33 +02:00
|
|
|
/*
|
|
|
|
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 Lesser 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 Lesser General Public License
|
|
|
|
along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @authors
|
|
|
|
* Gustav Simonsson <gustav.simonsson@gmail.com>
|
|
|
|
* @date 2015
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
package crypto
|
|
|
|
|
|
|
|
import (
|
|
|
|
"code.google.com/p/go-uuid/uuid"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2015-01-15 18:45:45 +02:00
|
|
|
"io"
|
2014-12-31 16:39:33 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: rename to KeyStore when replacing existing KeyStore
|
|
|
|
type KeyStore2 interface {
|
2015-01-15 18:45:45 +02:00
|
|
|
// create new key using io.Reader entropy source and optionally using auth string
|
|
|
|
GenerateNewKey(io.Reader, string) (*Key, error)
|
2014-12-31 16:39:33 +02:00
|
|
|
GetKey(*uuid.UUID, string) (*Key, error) // key from id and auth string
|
|
|
|
StoreKey(*Key, string) error // store key optionally using auth string
|
|
|
|
DeleteKey(*uuid.UUID, string) error // delete key by id and auth string
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
type keyStorePlain struct {
|
2014-12-31 16:39:33 +02:00
|
|
|
keysDirPath string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: copied from cmd/ethereum/flags.go
|
|
|
|
func DefaultDataDir() string {
|
|
|
|
usr, _ := user.Current()
|
|
|
|
return path.Join(usr.HomeDir, ".ethereum")
|
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func NewKeyStorePlain(path string) KeyStore2 {
|
|
|
|
ks := new(keyStorePlain)
|
|
|
|
ks.keysDirPath = path
|
|
|
|
return ks
|
|
|
|
}
|
|
|
|
|
2015-01-15 18:45:45 +02:00
|
|
|
func (ks keyStorePlain) GenerateNewKey(rand io.Reader, auth string) (key *Key, err error) {
|
|
|
|
return GenerateNewKeyDefault(ks, rand, auth)
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 18:45:45 +02:00
|
|
|
func GenerateNewKeyDefault(ks KeyStore2, rand io.Reader, auth string) (key *Key, err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
err = fmt.Errorf("GenerateNewKey error: %v", r)
|
|
|
|
}
|
|
|
|
}()
|
2015-01-15 18:45:45 +02:00
|
|
|
key = NewKey(rand)
|
2014-12-31 16:39:33 +02:00
|
|
|
err = ks.StoreKey(key, auth)
|
2015-01-07 17:06:26 +02:00
|
|
|
return key, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func (ks keyStorePlain) GetKey(keyId *uuid.UUID, auth string) (key *Key, err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
fileContent, err := GetKeyFile(ks.keysDirPath, keyId)
|
|
|
|
if err != nil {
|
2015-01-07 17:06:26 +02:00
|
|
|
return nil, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
key = new(Key)
|
|
|
|
err = json.Unmarshal(fileContent, key)
|
2015-01-07 17:06:26 +02:00
|
|
|
return key, err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func (ks keyStorePlain) StoreKey(key *Key, auth string) (err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
keyJSON, err := json.Marshal(key)
|
|
|
|
if err != nil {
|
2015-01-07 17:06:26 +02:00
|
|
|
return err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
err = WriteKeyFile(key.Id.String(), ks.keysDirPath, keyJSON)
|
2015-01-07 17:06:26 +02:00
|
|
|
return err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func (ks keyStorePlain) DeleteKey(keyId *uuid.UUID, auth string) (err error) {
|
2014-12-31 16:39:33 +02:00
|
|
|
keyDirPath := path.Join(ks.keysDirPath, keyId.String())
|
|
|
|
err = os.RemoveAll(keyDirPath)
|
2015-01-07 17:06:26 +02:00
|
|
|
return err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func GetKeyFile(keysDirPath string, keyId *uuid.UUID) (fileContent []byte, err error) {
|
2015-01-07 17:06:26 +02:00
|
|
|
id := keyId.String()
|
|
|
|
return ioutil.ReadFile(path.Join(keysDirPath, id, id))
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:06:26 +02:00
|
|
|
func WriteKeyFile(id string, keysDirPath string, content []byte) (err error) {
|
|
|
|
keyDirPath := path.Join(keysDirPath, id)
|
|
|
|
keyFilePath := path.Join(keyDirPath, id)
|
2014-12-31 16:39:33 +02:00
|
|
|
err = os.MkdirAll(keyDirPath, 0700) // read, write and dir search for user
|
|
|
|
if err != nil {
|
2015-01-07 17:06:26 +02:00
|
|
|
return err
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|
2015-01-07 17:06:26 +02:00
|
|
|
return ioutil.WriteFile(keyFilePath, content, 0600) // read, write for user
|
2014-12-31 16:39:33 +02:00
|
|
|
}
|