2015-04-26 12:46:15 +00:00
|
|
|
package server
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
2015-03-28 10:50:23 +00:00
|
|
|
"crypto/sha256"
|
2014-09-23 20:39:12 +00:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"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"
|
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-03 15:37:12 +00:00
|
|
|
func createMasterKey(s *Server, 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-04-26 12:46:15 +00:00
|
|
|
func OpenKey(s *Server, 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 {
|
|
|
|
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 {
|
|
|
|
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-04-26 12:46:15 +00:00
|
|
|
func SearchKey(s *Server, 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)
|
|
|
|
for name := range s.List(backend.Key, done) {
|
|
|
|
key, err := OpenKey(s, name, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return key, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, ErrNoKeyFound
|
|
|
|
}
|
|
|
|
|
2015-02-17 22:05:23 +00:00
|
|
|
// LoadKey loads a key from the backend.
|
2015-04-26 12:46:15 +00:00
|
|
|
func LoadKey(s *Server, name string) (*Key, error) {
|
2015-02-17 22:05:23 +00:00
|
|
|
// extract data from repo
|
2015-03-28 14:07:08 +00:00
|
|
|
rd, err := s.be.Get(backend.Key, name)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-03-28 10:50:23 +00:00
|
|
|
defer rd.Close()
|
2015-02-17 22:05:23 +00:00
|
|
|
|
|
|
|
// restore json
|
2015-03-28 10:50:23 +00:00
|
|
|
dec := json.NewDecoder(rd)
|
|
|
|
k := Key{}
|
|
|
|
err = dec.Decode(&k)
|
2015-02-17 22:05:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2015-03-28 10:50:23 +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-04-26 12:46:15 +00:00
|
|
|
func AddKey(s *Server, password string, template *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
|
|
|
|
newkey.master = template.master
|
|
|
|
}
|
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-03-28 10:50:23 +00:00
|
|
|
name := backend.ID(plainhw.Sum(nil)).String()
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-04-30 16:09:08 +00:00
|
|
|
// Encrypt encrypts and authenticates data with the master key. Stored in
|
|
|
|
// ciphertext is IV || Ciphertext || MAC. Returns the ciphertext, which is
|
|
|
|
// extended if necessary.
|
2015-04-12 18:58:41 +00:00
|
|
|
func (k *Key) Encrypt(ciphertext, plaintext []byte) ([]byte, error) {
|
2015-04-12 07:36:14 +00:00
|
|
|
return crypto.Encrypt(k.master, ciphertext, plaintext)
|
2015-02-15 17:13:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-30 16:09:08 +00:00
|
|
|
// EncryptTo encrypts and authenticates data with the master key. The returned
|
|
|
|
// io.Writer writes IV || Ciphertext || MAC.
|
2015-02-15 17:13:42 +00:00
|
|
|
func (k *Key) EncryptTo(wr io.Writer) io.WriteCloser {
|
2015-04-12 07:36:14 +00:00
|
|
|
return crypto.EncryptTo(k.master, wr)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Decrypt verifes and decrypts the ciphertext with the master key. Ciphertext
|
2015-03-14 18:53:51 +00:00
|
|
|
// must be in the form IV || Ciphertext || MAC.
|
2015-02-17 20:09:54 +00:00
|
|
|
func (k *Key) Decrypt(plaintext, ciphertext []byte) ([]byte, error) {
|
2015-04-12 07:36:14 +00:00
|
|
|
return crypto.Decrypt(k.master, plaintext, ciphertext)
|
2015-02-11 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptFrom verifies and decrypts the ciphertext read from rd and makes it
|
|
|
|
// available on the returned Reader. Ciphertext must be in the form IV ||
|
2015-03-14 18:53:51 +00:00
|
|
|
// Ciphertext || MAC. In order to correctly verify the ciphertext, rd is
|
2015-02-11 17:36:31 +00:00
|
|
|
// drained, locally buffered and made available on the returned Reader
|
2015-04-30 16:09:08 +00:00
|
|
|
// afterwards. If a MAC verification failure is observed, it is returned
|
2015-02-11 17:36:31 +00:00
|
|
|
// immediately.
|
2015-02-17 17:14:39 +00:00
|
|
|
func (k *Key) DecryptFrom(rd io.Reader) (io.ReadCloser, error) {
|
2015-04-12 07:36:14 +00:00
|
|
|
return crypto.DecryptFrom(k.master, rd)
|
2015-02-11 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
2015-05-02 14:22:43 +00:00
|
|
|
// Master returns the master keys for this repository. Only included for
|
2015-03-22 20:03:01 +00:00
|
|
|
// debug purposes.
|
2015-04-12 07:41:47 +00:00
|
|
|
func (k *Key) Master() *crypto.Key {
|
2015-03-22 20:03:01 +00:00
|
|
|
return k.master
|
|
|
|
}
|
|
|
|
|
2015-05-02 14:22:43 +00:00
|
|
|
// User returns the user keys for this key. Only included for debug purposes.
|
2015-04-12 07:41:47 +00:00
|
|
|
func (k *Key) User() *crypto.Key {
|
2015-03-22 20:03:01 +00:00
|
|
|
return k.user
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|