2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
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"
|
|
|
|
"github.com/restic/restic/chunker"
|
2015-04-05 22:22:19 +00:00
|
|
|
"github.com/restic/restic/debug"
|
2014-09-23 20:39:12 +00:00
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
"golang.org/x/crypto/poly1305"
|
2014-09-23 20:39:12 +00:00
|
|
|
)
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
// max size is 8MiB, defined in chunker
|
2015-03-14 18:53:51 +00:00
|
|
|
const macSize = poly1305.TagSize // Poly1305 size is 16 byte
|
|
|
|
const maxCiphertextSize = ivSize + chunker.MaxSize + macSize
|
|
|
|
const CiphertextExtension = ivSize + macSize
|
2014-11-23 15:48:00 +00:00
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
var (
|
2014-11-15 16:17:24 +00:00
|
|
|
// ErrUnauthenticated is returned when ciphertext verification has failed.
|
|
|
|
ErrUnauthenticated = errors.New("ciphertext verification failed")
|
|
|
|
// ErrNoKeyFound is returned when no key for the repository could be decrypted.
|
|
|
|
ErrNoKeyFound = errors.New("no key could be found")
|
2014-11-23 21:58:28 +00:00
|
|
|
// ErrBufferTooSmall is returned when the destination slice is too small
|
|
|
|
// for the ciphertext.
|
|
|
|
ErrBufferTooSmall = errors.New("destination buffer too small")
|
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-03-22 18:19:33 +00:00
|
|
|
user *MasterKeys
|
|
|
|
master *MasterKeys
|
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-03-22 18:09:30 +00:00
|
|
|
// MasterKeys holds signing and encryption keys for a repository. It is stored
|
|
|
|
// encrypted and signed as a JSON data structure in the Data field of the Key
|
2015-04-05 22:22:19 +00:00
|
|
|
// structure. For the master key, the secret random polynomial used for content
|
|
|
|
// defined chunking is included.
|
2015-03-22 18:19:33 +00:00
|
|
|
type MasterKeys struct {
|
2015-04-05 22:22:19 +00:00
|
|
|
Sign MACKey `json:"sign"`
|
|
|
|
Encrypt AESKey `json:"encrypt"`
|
|
|
|
ChunkerPolynomial chunker.Pol `json:"chunker_polynomial,omitempty"`
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// CreateKey initializes a master key in the given backend and encrypts it with
|
|
|
|
// the password.
|
2014-12-21 16:02:49 +00:00
|
|
|
func CreateKey(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.
|
|
|
|
func OpenKey(s Server, name string, password string) (*Key, error) {
|
|
|
|
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-03-14 18:53:51 +00:00
|
|
|
k.user, err = kdf(k, password)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt master keys
|
2015-02-17 20:09:54 +00:00
|
|
|
buf, err := k.DecryptUser([]byte{}, k.Data)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore json
|
2015-03-22 18:19:33 +00:00
|
|
|
k.master = &MasterKeys{}
|
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-04-05 22:22:19 +00:00
|
|
|
// test if polynomial is valid and irreducible
|
|
|
|
if k.master.ChunkerPolynomial == 0 {
|
|
|
|
return nil, errors.New("Polynomial for content defined chunking is zero")
|
|
|
|
}
|
|
|
|
|
|
|
|
if !k.master.ChunkerPolynomial.Irreducible() {
|
|
|
|
return nil, errors.New("Polynomial for content defined chunking is invalid")
|
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("OpenKey", "Master keys loaded, polynomial %v", k.master.ChunkerPolynomial)
|
|
|
|
|
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.
|
2014-12-21 16:02:49 +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-03-28 10:50:23 +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.
|
2014-12-21 17:16:22 +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
|
|
|
|
newkey.user, err = kdf(newkey, 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-03-22 18:18:34 +00:00
|
|
|
newkey.master = generateRandomKeys()
|
2015-04-05 22:22:19 +00:00
|
|
|
// generate random polynomial for cdc
|
|
|
|
p, err := chunker.RandomPolynomial()
|
|
|
|
if err != nil {
|
|
|
|
debug.Log("AddKey", "error generating new polynomial for cdc: %v", err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
debug.Log("AddKey", "generated new polynomial for cdc: %v", p)
|
|
|
|
newkey.master.ChunkerPolynomial = p
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
newkey.Data = GetChunkBuf("key")
|
|
|
|
n, err = newkey.EncryptUser(newkey.Data, buf)
|
|
|
|
newkey.Data = newkey.Data[:n]
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
FreeChunkBuf("key", newkey.Data)
|
|
|
|
|
2014-12-21 17:16:22 +00:00
|
|
|
return newkey, nil
|
2014-11-25 22:07:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
func (k *Key) newIV(buf []byte) error {
|
|
|
|
_, err := io.ReadFull(rand.Reader, buf[:ivSize])
|
|
|
|
buf = buf[:ivSize]
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
2014-11-23 15:48:00 +00:00
|
|
|
return err
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
return nil
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
// EncryptUser encrypts and signs data with the user key. Stored in ciphertext
|
2015-03-14 18:53:51 +00:00
|
|
|
// is IV || Ciphertext || MAC.
|
2014-11-23 15:48:00 +00:00
|
|
|
func (k *Key) EncryptUser(ciphertext, plaintext []byte) (int, error) {
|
2015-03-14 18:53:51 +00:00
|
|
|
return Encrypt(k.user, ciphertext, plaintext)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-23 15:48:00 +00:00
|
|
|
// Encrypt encrypts and signs data with the master key. Stored in ciphertext is
|
2015-03-14 18:53:51 +00:00
|
|
|
// IV || Ciphertext || MAC. Returns the ciphertext length.
|
2014-11-23 15:48:00 +00:00
|
|
|
func (k *Key) Encrypt(ciphertext, plaintext []byte) (int, error) {
|
2015-03-14 18:53:51 +00:00
|
|
|
return Encrypt(k.master, ciphertext, plaintext)
|
2015-02-15 17:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptTo encrypts and signs data with the master key. The returned
|
|
|
|
// io.Writer writes IV || Ciphertext || HMAC. For the hash function, SHA256 is
|
|
|
|
// used.
|
|
|
|
func (k *Key) EncryptTo(wr io.Writer) io.WriteCloser {
|
2015-03-14 18:53:51 +00:00
|
|
|
return EncryptTo(k.master, wr)
|
2015-02-15 17:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// EncryptUserTo encrypts and signs data with the user key. The returned
|
|
|
|
// io.Writer writes IV || Ciphertext || HMAC. For the hash function, SHA256 is
|
|
|
|
// used.
|
|
|
|
func (k *Key) EncryptUserTo(wr io.Writer) io.WriteCloser {
|
2015-03-14 18:53:51 +00:00
|
|
|
return EncryptTo(k.user, 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-03-14 18:53:51 +00:00
|
|
|
return Decrypt(k.master, plaintext, ciphertext)
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-12-29 11:39:18 +00:00
|
|
|
// DecryptUser verifes and decrypts the ciphertext with the user 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) DecryptUser(plaintext, ciphertext []byte) ([]byte, error) {
|
2015-03-14 18:53:51 +00:00
|
|
|
return Decrypt(k.user, 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-03-14 18:53:51 +00:00
|
|
|
// afterwards. If an 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-03-14 18:53:51 +00:00
|
|
|
return DecryptFrom(k.master, rd)
|
2015-02-11 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DecryptFrom verifies and decrypts the ciphertext read from rd with the user
|
|
|
|
// key and makes it available on the returned Reader. Ciphertext must be in the
|
2015-03-14 18:53:51 +00:00
|
|
|
// form IV || Ciphertext || MAC. In order to correctly verify the ciphertext,
|
2015-02-11 17:36:31 +00:00
|
|
|
// rd is drained, locally buffered and made available on the returned Reader
|
2015-03-14 18:53:51 +00:00
|
|
|
// afterwards. If an 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) DecryptUserFrom(rd io.Reader) (io.ReadCloser, error) {
|
2015-03-14 18:53:51 +00:00
|
|
|
return DecryptFrom(k.user, rd)
|
2015-02-11 17:36:31 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 20:03:01 +00:00
|
|
|
// Master() returns the master keys for this repository. Only included for
|
|
|
|
// debug purposes.
|
|
|
|
func (k *Key) Master() *MasterKeys {
|
|
|
|
return k.master
|
|
|
|
}
|
|
|
|
|
|
|
|
// User() returns the user keys for this key. Only included for debug purposes.
|
|
|
|
func (k *Key) User() *MasterKeys {
|
|
|
|
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
|
|
|
}
|