2019-06-02 08:47:19 +00:00
|
|
|
package repositories
|
|
|
|
|
|
|
|
import (
|
2019-06-05 06:40:06 +00:00
|
|
|
"bytes"
|
2019-07-09 00:45:01 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"errors"
|
2019-06-02 08:47:19 +00:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"gopkg.in/AlecAivazis/survey.v1"
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
"github.com/mitchell/selfpass/sp/crypto"
|
|
|
|
"github.com/mitchell/selfpass/sp/types"
|
2019-06-02 08:47:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewConfigManager(cfgFile *string) *ConfigManager {
|
|
|
|
return &ConfigManager{
|
|
|
|
cfgFile: cfgFile,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigManager struct {
|
|
|
|
masterpass string
|
|
|
|
cfgFile *string
|
|
|
|
v *viper.Viper
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *ConfigManager) SetMasterpass(masterpass string) {
|
|
|
|
mgr.masterpass = masterpass
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mgr *ConfigManager) OpenConfig() (output string, v *viper.Viper, err error) {
|
|
|
|
if mgr.masterpass != "" {
|
|
|
|
return mgr.masterpass, mgr.v, nil
|
|
|
|
}
|
|
|
|
cfg := *mgr.cfgFile
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
mgr.v = viper.New()
|
|
|
|
mgr.v.SetConfigType("toml")
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
if cfg == "" {
|
2019-06-02 08:47:19 +00:00
|
|
|
home, err := homedir.Dir()
|
|
|
|
if err != nil {
|
|
|
|
return output, nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-11 06:05:59 +00:00
|
|
|
cfg = home + "/.sp.toml"
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
mgr.v.SetConfigFile(cfg)
|
|
|
|
mgr.cfgFile = &cfg
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
var contents []byte
|
2019-07-09 00:45:01 +00:00
|
|
|
var configDecrypted bool
|
2019-06-05 06:40:06 +00:00
|
|
|
|
|
|
|
if _, err := os.Open(cfg); os.IsNotExist(err) {
|
|
|
|
return output, mgr.v, fmt.Errorf("no config found, run 'init' command")
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt := &survey.Password{Message: "Master password:"}
|
|
|
|
if err = survey.AskOne(prompt, &mgr.masterpass, nil); err != nil {
|
|
|
|
return output, nil, err
|
|
|
|
}
|
|
|
|
|
2019-06-07 09:03:15 +00:00
|
|
|
contents, err = decryptConfig(mgr.masterpass, cfg)
|
2019-07-12 01:02:46 +00:00
|
|
|
if err == errConfigDecrypted {
|
2019-07-09 00:45:01 +00:00
|
|
|
configDecrypted = true
|
2019-07-12 01:02:46 +00:00
|
|
|
} else if err != nil && err.Error() == crypto.ErrAuthenticationFailed.Error() {
|
|
|
|
return output, nil, errors.New("incorrect masterpass")
|
2019-06-05 06:40:06 +00:00
|
|
|
} else if err != nil {
|
|
|
|
return output, nil, err
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
if err = mgr.v.ReadConfig(bytes.NewBuffer(contents)); err != nil {
|
2019-06-07 09:03:15 +00:00
|
|
|
return output, nil, err
|
2019-06-05 06:40:06 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
if configDecrypted {
|
2019-06-07 09:03:15 +00:00
|
|
|
fmt.Println("Config wasn't encrypted, or has been compromised.")
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
if err = mgr.WriteConfig(); err != nil {
|
|
|
|
return output, nil, err
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return mgr.masterpass, mgr.v, nil
|
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
var errConfigDecrypted = errors.New("config is decrypted")
|
|
|
|
|
2019-06-07 09:03:15 +00:00
|
|
|
func decryptConfig(masterpass string, cfgFile string) (contents []byte, err error) {
|
2019-06-05 06:40:06 +00:00
|
|
|
contents, err = ioutil.ReadFile(cfgFile)
|
2019-06-02 08:47:19 +00:00
|
|
|
if err != nil {
|
2019-06-05 06:40:06 +00:00
|
|
|
return contents, err
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
if string(contents[:len(types.KeyPrivateKey)]) == types.KeyPrivateKey {
|
|
|
|
return contents, errConfigDecrypted
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
salt := contents[:saltSize]
|
|
|
|
contents = contents[saltSize:]
|
|
|
|
|
|
|
|
passkey := crypto.GeneratePBKDF2Key([]byte(masterpass), salt)
|
|
|
|
|
2019-06-07 09:03:15 +00:00
|
|
|
plaintext, err := crypto.GCMDecrypt(passkey, contents)
|
|
|
|
if err != nil {
|
2019-06-05 06:40:06 +00:00
|
|
|
return contents, err
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
return plaintext, nil
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
func (mgr ConfigManager) DecryptConfig() error {
|
|
|
|
if err := mgr.v.WriteConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
func (mgr ConfigManager) WriteConfig() (err error) {
|
|
|
|
if err := mgr.v.WriteConfigAs(*mgr.cfgFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
contents, err := ioutil.ReadFile(mgr.v.ConfigFileUsed())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
salt := make([]byte, saltSize)
|
|
|
|
_, err = rand.Read(salt)
|
2019-06-05 06:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-02 08:47:19 +00:00
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
keypass := crypto.GeneratePBKDF2Key([]byte(mgr.masterpass), salt)
|
|
|
|
|
2019-06-07 09:03:15 +00:00
|
|
|
contents, err = crypto.GCMEncrypt(keypass, contents)
|
2019-06-05 06:40:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
|
|
|
|
2019-07-09 00:45:01 +00:00
|
|
|
contents = append(salt, contents...)
|
|
|
|
|
2019-06-05 06:40:06 +00:00
|
|
|
err = ioutil.WriteFile(mgr.v.ConfigFileUsed(), contents, 0600)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
2019-06-05 06:40:06 +00:00
|
|
|
|
|
|
|
return nil
|
2019-06-02 08:47:19 +00:00
|
|
|
}
|
2019-07-09 00:45:01 +00:00
|
|
|
|
|
|
|
const saltSize = 16
|