2015-05-09 21:52:03 +00:00
|
|
|
package repository
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2015-03-28 10:50:23 +00:00
|
|
|
"crypto/sha256"
|
2015-07-25 15:05:45 +00:00
|
|
|
"encoding/hex"
|
2014-09-23 20:39:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"time"
|
|
|
|
|
2014-12-05 20:45:49 +00:00
|
|
|
"github.com/restic/restic/backend"
|
2015-04-12 07:36:14 +00:00
|
|
|
"github.com/restic/restic/crypto"
|
2016-01-07 20:06:45 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2014-11-15 16:17:24 +00:00
|
|
|
// ErrNoKeyFound is returned when no key for the repository could be decrypted.
|
|
|
|
ErrNoKeyFound = errors.New("no key could be found")
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: figure out scrypt values on the fly depending on the current
|
|
|
|
// hardware.
|
|
|
|
const (
|
2014-11-15 16:17:24 +00:00
|
|
|
scryptN = 65536
|
|
|
|
scryptR = 8
|
|
|
|
scryptP = 1
|
|
|
|
scryptSaltsize = 64
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// Key represents an encrypted master key for a repository.
|
2014-09-23 20:39:12 +00:00
|
|
|
type Key struct {
|
|
|
|
Created time.Time `json:"created"`
|
|
|
|
Username string `json:"username"`
|
|
|
|
Hostname string `json:"hostname"`
|
|
|
|
|
|
|
|
KDF string `json:"kdf"`
|
|
|
|
N int `json:"N"`
|
|
|
|
R int `json:"r"`
|
|
|
|
P int `json:"p"`
|
|
|
|
Salt []byte `json:"salt"`
|
|
|
|
Data []byte `json:"data"`
|
|
|
|
|
2015-04-12 07:41:47 +00:00
|
|
|
user *crypto.Key
|
|
|
|
master *crypto.Key
|
2014-11-25 22:18:02 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
name string
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2015-05-03 15:37:12 +00:00
|
|
|
// createMasterKey creates a new master key in the given backend and encrypts
|
2015-05-03 15:17:10 +00:00
|
|
|
// it with the password.
|
2015-05-09 21:59:58 +00:00
|
|
|
func createMasterKey(s *Repository, password string) (*Key, error) {
|
2014-12-21 17:16:22 +00:00
|
|
|
return AddKey(s, password, nil)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
// OpenKey tries do decrypt the key specified by name with the given password.
|
2015-05-09 21:59:58 +00:00
|
|
|
func OpenKey(s *Repository, name string, password string) (*Key, error) {
|
2015-03-28 10:50:23 +00:00
|
|
|
k, err := LoadKey(s, name)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2016-01-07 20:06:45 +00:00
|
|
|
debug.Log("OpenKey", "LoadKey(%v) returned error %v", name[:12], err)
|
2014-09-23 20:39:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// check KDF
|
|
|
|
if k.KDF != "scrypt" {
|
|
|
|
return nil, errors.New("only supported KDF is scrypt()")
|
|
|
|
}
|
|
|
|
|
|
|
|
// derive user key
|
2015-04-12 07:36:14 +00:00
|
|
|
k.user, err = crypto.KDF(k.N, k.R, k.P, k.Salt, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt master keys
|
2015-04-12 07:36:14 +00:00
|
|
|
buf, err := crypto.Decrypt(k.user, []byte{}, k.Data)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore json
|
2015-04-12 07:41:47 +00:00
|
|
|
k.master = &crypto.Key{}
|
2014-09-23 20:39:12 +00:00
|
|
|
err = json.Unmarshal(buf, k.master)
|
|
|
|
if err != nil {
|
2016-01-07 20:06:45 +00:00
|
|
|
debug.Log("OpenKey", "Unmarshal() returned error %v", err)
|
2014-09-23 20:39:12 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
k.name = name
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2015-05-01 15:31:57 +00:00
|
|
|
if !k.Valid() {
|
|
|
|
return nil, errors.New("Invalid key for repository")
|
2015-04-05 22:22:19 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// SearchKey tries to decrypt all keys in the backend with the given password.
|
|
|
|
// If none could be found, ErrNoKeyFound is returned.
|
2015-05-09 21:59:58 +00:00
|
|
|
func SearchKey(s *Repository, password string) (*Key, error) {
|
2014-09-23 20:39:12 +00:00
|
|
|
// try all keys in repo
|
2015-03-28 10:50:23 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
2015-05-17 18:48:59 +00:00
|
|
|
for name := range s.Backend().List(backend.Key, done) {
|
2016-01-07 20:06:45 +00:00
|
|
|
debug.Log("SearchKey", "trying key %v", name[:12])
|
2015-03-28 10:50:23 +00:00
|
|
|
key, err := OpenKey(s, name, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2016-01-07 20:06:45 +00:00
|
|
|
debug.Log("SearchKey", "key %v returned error %v", name[:12], err)
|
2014-09-23 20:39:12 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-01-07 20:06:45 +00:00
|
|
|
debug.Log("SearchKey", "successfully opened key %v", name[:12])
|
2014-09-23 20:39:12 +00:00
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoKeyFound
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:05:23 +00:00
|
|
|
// LoadKey loads a key from the backend.
|
2015-11-18 19:33:20 +00:00
|
|
|
func LoadKey(s *Repository, name string) (k *Key, err error) {
|
2015-02-17 22:05:23 +00:00
|
|
|
// extract data from repo
|
2016-01-23 12:13:05 +00:00
|
|
|
rd, err := s.be.GetReader(backend.Key, name, 0, 0)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-11-18 19:33:20 +00:00
|
|
|
defer closeOrErr(rd, &err)
|
2015-02-17 22:05:23 +00:00
|
|
|
|
|
|
|
// restore json
|
2015-03-28 10:50:23 +00:00
|
|
|
dec := json.NewDecoder(rd)
|
2015-11-18 19:33:20 +00:00
|
|
|
k = new(Key)
|
|
|
|
err = dec.Decode(k)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-11-18 19:33:20 +00:00
|
|
|
return k, nil
|
2015-02-17 22:05:23 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 22:07:00 +00:00
|
|
|
// AddKey adds a new key to an already existing repository.
|
2015-05-09 21:59:58 +00:00
|
|
|
func AddKey(s *Repository, password string, template *crypto.Key) (*Key, error) {
|
2014-11-25 22:07:00 +00:00
|
|
|
// fill meta data about key
|
|
|
|
newkey := &Key{
|
|
|
|
Created: time.Now(),
|
|
|
|
KDF: "scrypt",
|
|
|
|
N: scryptN,
|
|
|
|
R: scryptR,
|
|
|
|
P: scryptP,
|
|
|
|
}
|
|
|
|
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Hostname = hn
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Username = usr.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate random salt
|
|
|
|
newkey.Salt = make([]byte, scryptSaltsize)
|
|
|
|
n, err := rand.Read(newkey.Salt)
|
|
|
|
if n != scryptSaltsize || err != nil {
|
|
|
|
panic("unable to read enough random bytes for salt")
|
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
// call KDF to derive user key
|
2015-04-12 07:36:14 +00:00
|
|
|
newkey.user, err = crypto.KDF(newkey.N, newkey.R, newkey.P, newkey.Salt, password)
|
2014-11-25 22:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-12-21 17:16:22 +00:00
|
|
|
if template == nil {
|
|
|
|
// generate new random master keys
|
2015-04-30 02:28:34 +00:00
|
|
|
newkey.master = crypto.NewRandomKey()
|
2014-12-21 17:16:22 +00:00
|
|
|
} else {
|
|
|
|
// copy master keys from old key
|
2015-05-03 16:04:13 +00:00
|
|
|
newkey.master = template
|
2014-12-21 17:16:22 +00:00
|
|
|
}
|
2014-11-25 22:07:00 +00:00
|
|
|
|
|
|
|
// encrypt master keys (as json) with user key
|
|
|
|
buf, err := json.Marshal(newkey.master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-04-26 12:46:15 +00:00
|
|
|
newkey.Data, err = crypto.Encrypt(newkey.user, nil, buf)
|
2014-11-25 22:07:00 +00:00
|
|
|
|
|
|
|
// dump as json
|
|
|
|
buf, err = json.Marshal(newkey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// store in repository and return
|
2015-03-28 14:07:08 +00:00
|
|
|
blob, err := s.be.Create()
|
2014-11-25 22:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-15 16:26:08 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
plainhw := backend.NewHashingWriter(blob, sha256.New())
|
2015-02-15 16:26:08 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
_, err = plainhw.Write(buf)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-07-25 15:05:45 +00:00
|
|
|
name := hex.EncodeToString(plainhw.Sum(nil))
|
2015-03-28 10:50:23 +00:00
|
|
|
|
|
|
|
err = blob.Finalize(backend.Key, name)
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
newkey.name = name
|
2014-11-25 22:07:00 +00:00
|
|
|
|
2014-12-21 17:16:22 +00:00
|
|
|
return newkey, nil
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func (k *Key) String() string {
|
|
|
|
if k == nil {
|
|
|
|
return "<Key nil>"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("<Key of %s@%s, created on %s>", k.Username, k.Hostname, k.Created)
|
|
|
|
}
|
2014-11-25 22:18:02 +00:00
|
|
|
|
2015-03-28 10:50:23 +00:00
|
|
|
func (k Key) Name() string {
|
|
|
|
return k.name
|
2014-11-25 22:18:02 +00:00
|
|
|
}
|
2015-05-01 15:31:57 +00:00
|
|
|
|
|
|
|
// Valid tests whether the mac and encryption keys are valid (i.e. not zero)
|
|
|
|
func (k *Key) Valid() bool {
|
|
|
|
return k.user.Valid() && k.master.Valid()
|
|
|
|
}
|