2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"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"
|
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"`
|
|
|
|
Comment string `json:"comment,omitempty"`
|
|
|
|
|
|
|
|
KDF string `json:"kdf"`
|
|
|
|
N int `json:"N"`
|
|
|
|
R int `json:"r"`
|
|
|
|
P int `json:"p"`
|
|
|
|
Salt []byte `json:"salt"`
|
|
|
|
Data []byte `json:"data"`
|
|
|
|
|
|
|
|
user *keys
|
|
|
|
master *keys
|
2014-11-25 22:18:02 +00:00
|
|
|
|
|
|
|
id backend.ID
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// keys is a JSON structure that holds signing and encryption keys.
|
2014-09-23 20:39:12 +00:00
|
|
|
type keys struct {
|
2015-03-14 18:53:51 +00:00
|
|
|
Sign *MACKey
|
|
|
|
Encrypt *AESKey
|
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
|
|
|
}
|
|
|
|
|
2014-11-15 16:17:24 +00:00
|
|
|
// OpenKey tries do decrypt the key specified by id with the given password.
|
2014-12-21 16:02:49 +00:00
|
|
|
func OpenKey(s Server, id backend.ID, password string) (*Key, error) {
|
2015-02-17 22:05:23 +00:00
|
|
|
k, err := LoadKey(s, id)
|
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
|
|
|
|
k.master = &keys{}
|
|
|
|
err = json.Unmarshal(buf, k.master)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-11-25 22:18:02 +00:00
|
|
|
k.id = id
|
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
|
|
|
// list all keys
|
2014-12-21 16:02:49 +00:00
|
|
|
ids, err := s.List(backend.Key)
|
2014-09-23 20:39:12 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// try all keys in repo
|
|
|
|
var key *Key
|
|
|
|
for _, id := range ids {
|
2014-12-21 16:02:49 +00:00
|
|
|
key, err = OpenKey(s, id, 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.
|
|
|
|
func LoadKey(s Server, id backend.ID) (*Key, error) {
|
|
|
|
// extract data from repo
|
|
|
|
data, err := s.Get(backend.Key, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// restore json
|
|
|
|
k := &Key{}
|
|
|
|
err = json.Unmarshal(data, k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return k, err
|
|
|
|
}
|
|
|
|
|
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-14 18:53:51 +00:00
|
|
|
newkey.master = newkey.newKeys()
|
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-02-15 16:26:08 +00:00
|
|
|
blob, err := s.Create(backend.Key)
|
2014-11-25 22:07:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2015-02-15 16:26:08 +00:00
|
|
|
|
|
|
|
_, err = blob.Write(buf)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = blob.Close()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := blob.ID()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-25 22:18:02 +00:00
|
|
|
newkey.id = id
|
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
|
|
|
}
|
|
|
|
|
2015-03-14 18:53:51 +00:00
|
|
|
func (k *Key) newKeys() *keys {
|
|
|
|
return &keys{
|
|
|
|
Encrypt: generateRandomAESKey(),
|
|
|
|
Sign: generateRandomMACKey(),
|
2014-09-23 20:39:12 +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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
func (k Key) ID() backend.ID {
|
|
|
|
return k.id
|
|
|
|
}
|