2015-05-09 21:52:03 +00:00
|
|
|
package repository
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
2017-01-22 11:32:20 +00:00
|
|
|
"bytes"
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2014-09-23 20:39:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/user"
|
|
|
|
"time"
|
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
|
|
|
|
|
|
|
"github.com/restic/restic/internal/backend"
|
|
|
|
"github.com/restic/restic/internal/crypto"
|
|
|
|
"github.com/restic/restic/internal/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.
|
2016-03-11 15:21:01 +00:00
|
|
|
ErrNoKeyFound = errors.New("wrong password or no key found")
|
2016-08-21 11:09:31 +00:00
|
|
|
|
|
|
|
// ErrMaxKeysReached is returned when the maximum number of keys was checked and no key could be found.
|
|
|
|
ErrMaxKeysReached = errors.New("maximum number of keys reached")
|
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
|
|
|
}
|
|
|
|
|
2016-08-21 10:32:38 +00:00
|
|
|
// KDFParams tracks the parameters used for the KDF. If not set, it will be
|
|
|
|
// calibrated on the first run of AddKey().
|
|
|
|
var KDFParams *crypto.KDFParams
|
|
|
|
|
|
|
|
var (
|
2016-08-21 11:13:05 +00:00
|
|
|
// KDFTimeout specifies the maximum runtime for the KDF.
|
|
|
|
KDFTimeout = 500 * time.Millisecond
|
|
|
|
|
|
|
|
// KDFMemory limits the memory the KDF is allowed to use.
|
|
|
|
KDFMemory = 60
|
2016-08-21 10:32:38 +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) {
|
2017-06-04 09:16:55 +00:00
|
|
|
return AddKey(context.TODO(), 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.
|
2017-06-04 09:16:55 +00:00
|
|
|
func OpenKey(ctx context.Context, s *Repository, name string, password string) (*Key, error) {
|
|
|
|
k, err := LoadKey(ctx, s, name)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("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
|
2016-08-21 10:32:38 +00:00
|
|
|
params := crypto.KDFParams{
|
|
|
|
N: k.N,
|
|
|
|
R: k.R,
|
|
|
|
P: k.P,
|
|
|
|
}
|
|
|
|
k.user, err = crypto.KDF(params, k.Salt, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "crypto.KDF")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt master keys
|
2016-09-03 11:34:04 +00:00
|
|
|
buf := make([]byte, len(k.Data))
|
2017-06-19 19:12:53 +00:00
|
|
|
n, err := k.user.Decrypt(buf, k.Data)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-09-03 11:34:04 +00:00
|
|
|
buf = buf[:n]
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
// 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-09-27 20:35:08 +00:00
|
|
|
debug.Log("Unmarshal() returned error %v", err)
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "Unmarshal")
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2016-08-21 11:09:31 +00:00
|
|
|
// SearchKey tries to decrypt at most maxKeys keys in the backend with the
|
|
|
|
// given password. If none could be found, ErrNoKeyFound is returned. When
|
|
|
|
// maxKeys is reached, ErrMaxKeysReached is returned. When setting maxKeys to
|
|
|
|
// zero, all keys in the repo are checked.
|
2017-06-04 09:16:55 +00:00
|
|
|
func SearchKey(ctx context.Context, s *Repository, password string, maxKeys int) (*Key, error) {
|
2016-08-21 11:09:31 +00:00
|
|
|
checked := 0
|
|
|
|
|
|
|
|
// try at most maxKeysForSearch keys in repo
|
2017-06-04 09:16:55 +00:00
|
|
|
for name := range s.Backend().List(ctx, restic.KeyFile) {
|
2016-08-21 11:09:31 +00:00
|
|
|
if maxKeys > 0 && checked > maxKeys {
|
|
|
|
return nil, ErrMaxKeysReached
|
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("trying key %v", name[:12])
|
2017-06-04 09:16:55 +00:00
|
|
|
key, err := OpenKey(ctx, s, name, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("key %v returned error %v", name[:12], err)
|
2016-08-21 11:09:31 +00:00
|
|
|
|
|
|
|
// ErrUnauthenticated means the password is wrong, try the next key
|
2016-08-29 17:18:57 +00:00
|
|
|
if errors.Cause(err) == crypto.ErrUnauthenticated {
|
2016-08-21 11:09:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, err
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("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.
|
2017-06-04 09:16:55 +00:00
|
|
|
func LoadKey(ctx context.Context, s *Repository, name string) (k *Key, err error) {
|
2016-09-01 19:19:30 +00:00
|
|
|
h := restic.Handle{Type: restic.KeyFile, Name: name}
|
2017-06-04 09:16:55 +00:00
|
|
|
data, err := backend.LoadAll(ctx, s.be, h)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-23 22:48:19 +00:00
|
|
|
k = &Key{}
|
|
|
|
err = json.Unmarshal(data, k)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "Unmarshal")
|
2015-02-17 22:05:23 +00:00
|
|
|
}
|
|
|
|
|
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.
|
2017-06-04 09:16:55 +00:00
|
|
|
func AddKey(ctx context.Context, s *Repository, password string, template *crypto.Key) (*Key, error) {
|
2016-08-21 10:32:38 +00:00
|
|
|
// make sure we have valid KDF parameters
|
|
|
|
if KDFParams == nil {
|
|
|
|
p, err := crypto.Calibrate(KDFTimeout, KDFMemory)
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "Calibrate")
|
2016-08-21 10:32:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
KDFParams = &p
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("calibrated KDF parameters are %v", p)
|
2016-08-21 10:32:38 +00:00
|
|
|
}
|
|
|
|
|
2014-11-25 22:07:00 +00:00
|
|
|
// fill meta data about key
|
|
|
|
newkey := &Key{
|
|
|
|
Created: time.Now(),
|
|
|
|
KDF: "scrypt",
|
2016-08-21 10:32:38 +00:00
|
|
|
N: KDFParams.N,
|
|
|
|
R: KDFParams.R,
|
|
|
|
P: KDFParams.P,
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Hostname = hn
|
|
|
|
}
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err == nil {
|
|
|
|
newkey.Username = usr.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate random salt
|
2016-08-21 10:32:38 +00:00
|
|
|
newkey.Salt, err = crypto.NewSalt()
|
|
|
|
if err != nil {
|
|
|
|
panic("unable to read enough random bytes for salt: " + err.Error())
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
// call KDF to derive user key
|
2016-08-21 10:32:38 +00:00
|
|
|
newkey.user, err = crypto.KDF(*KDFParams, 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 {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "Marshal")
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2017-06-19 19:12:53 +00:00
|
|
|
newkey.Data, err = newkey.user.Encrypt(nil, buf)
|
2014-11-25 22:07:00 +00:00
|
|
|
|
|
|
|
// dump as json
|
|
|
|
buf, err = json.Marshal(newkey)
|
|
|
|
if err != nil {
|
2016-08-29 20:16:58 +00:00
|
|
|
return nil, errors.Wrap(err, "Marshal")
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// store in repository and return
|
2016-08-31 18:29:54 +00:00
|
|
|
h := restic.Handle{
|
2016-09-01 19:19:30 +00:00
|
|
|
Type: restic.KeyFile,
|
|
|
|
Name: restic.Hash(buf).String(),
|
2015-02-15 16:26:08 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 09:16:55 +00:00
|
|
|
err = s.be.Save(ctx, h, bytes.NewReader(buf))
|
2015-02-15 16:26:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-01-24 16:52:44 +00:00
|
|
|
newkey.name = h.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
|
|
|
|
2016-01-23 22:27:40 +00:00
|
|
|
// Name returns an identifier for the key.
|
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()
|
|
|
|
}
|