From 2c4c708b3a9b684cdd2349c3ca4cb12d25966520 Mon Sep 17 00:00:00 2001 From: Greg Date: Tue, 10 Sep 2019 11:31:57 -0400 Subject: [PATCH] Re-arrange platform-dependent code. --- .../{impl_non_darwin.go => impl_android.go} | 6 +-- cmd/passgo-gui/impl_darwin.go | 5 -- cmd/passgo-gui/main.go | 4 ++ impl_android.go | 25 +++++++++ impl_darwin.go | 38 ++++++++++++-- impl_non_darwin.go | 12 ----- main.go | 52 ++++--------------- 7 files changed, 75 insertions(+), 67 deletions(-) rename cmd/passgo-gui/{impl_non_darwin.go => impl_android.go} (93%) create mode 100644 impl_android.go delete mode 100644 impl_non_darwin.go diff --git a/cmd/passgo-gui/impl_non_darwin.go b/cmd/passgo-gui/impl_android.go similarity index 93% rename from cmd/passgo-gui/impl_non_darwin.go rename to cmd/passgo-gui/impl_android.go index 51527e7..ea4742d 100644 --- a/cmd/passgo-gui/impl_non_darwin.go +++ b/cmd/passgo-gui/impl_android.go @@ -11,12 +11,8 @@ import ( "golang.org/x/image/font/sfnt" ) -var ( - regular *sfnt.Font - confDir string -) - func init() { + var err error regular, err = sfnt.Parse(goregular.TTF) if err != nil { log(Fatal, "Cannot parse default font: ", err) diff --git a/cmd/passgo-gui/impl_darwin.go b/cmd/passgo-gui/impl_darwin.go index 0e8d764..8a91419 100644 --- a/cmd/passgo-gui/impl_darwin.go +++ b/cmd/passgo-gui/impl_darwin.go @@ -11,11 +11,6 @@ import ( "golang.org/x/image/font/sfnt" ) -var ( - regular *sfnt.Font - confDir string -) - func setFont() error { f, err := os.Open("/System/Library/Fonts/AppleSDGothicNeo.ttc") if err != nil { diff --git a/cmd/passgo-gui/main.go b/cmd/passgo-gui/main.go index 907fa4c..dba5889 100644 --- a/cmd/passgo-gui/main.go +++ b/cmd/passgo-gui/main.go @@ -17,6 +17,8 @@ import ( "gioui.org/ui/measure" "gioui.org/ui/text" + "golang.org/x/image/font/sfnt" + "github.com/fsnotify/fsnotify" "gopkg.in/yaml.v2" @@ -74,6 +76,8 @@ func main() { } var ( + confDir string + regular *sfnt.Font Config conf l []passgo.Pass mux sync.Mutex diff --git a/impl_android.go b/impl_android.go new file mode 100644 index 0000000..73e42c6 --- /dev/null +++ b/impl_android.go @@ -0,0 +1,25 @@ +//+build android + +package passgo + +import ( + "fmt" +) + +//Clip copies a string to the clipboard +func Clip(x string) { + fmt.Println("Clipboard not implemented for this platform") +} + +func (s *Store) nativeDecrypt(name string) (string, error) { + return "", fmt.Errorf("NOT IMPLEMENTED") +} + +func (s *Store) nativeEncrypt(pw string) ([]byte, error) { + return nil, fmt.Errorf("NOT IMPLEMENTED") +} + +func nativeIdentities() ([]string, error) { + return nil, fmt.Errorf("NOT IMPLEMENTED") +} + diff --git a/impl_darwin.go b/impl_darwin.go index 5459112..8841770 100644 --- a/impl_darwin.go +++ b/impl_darwin.go @@ -5,6 +5,7 @@ package passgo import ( "bufio" "bytes" + "encoding/hex" "fmt" "os" "os/exec" @@ -14,10 +15,13 @@ import ( "strconv" "sync" + "golang.org/x/crypto/openpgp" + + "github.com/jcmdev0/gpgagent" "github.com/fsnotify/fsnotify" ) -func (s *Store) gpgDecrypt(name string) (string, error) { +func (s *Store) nativeDecrypt(name string) (string, error) { fmt.Println("calling gpg -d") file := path.Join(s.Dir, strings.Join([]string{name, ".gpg"}, "")) if _, err := os.Stat(file); os.IsNotExist(err) { @@ -30,7 +34,7 @@ func (s *Store) gpgDecrypt(name string) (string, error) { return string(output[:len(output)-1]), nil } -func (s *Store) gpgEncrypt(pw string) ([]byte, error) { +func (s *Store) nativeEncrypt(pw string) ([]byte, error) { if s.Id == "" { return nil, fmt.Errorf("No ID") } @@ -51,7 +55,7 @@ var ( cachedIdentities []string ) -func gpgIdentities() ([]string, error) { +func nativeIdentities() ([]string, error) { idMux.Lock() defer idMux.Unlock() if cachedIdentities != nil { @@ -146,6 +150,34 @@ func setAgentInfo() { } +func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) { + conn, err := gpgagent.NewGpgAgentConn() + if err != nil { + return nil, err + } + defer conn.Close() + + for _, key := range keys { + cacheId := strings.ToUpper(hex.EncodeToString(key.PublicKey.Fingerprint[:])) + request := gpgagent.PassphraseRequest{CacheKey: cacheId} + for i := 0; i < 3; i++ { + var passphrase string + passphrase, err = conn.GetPassphrase(&request) + if err != nil { + continue + } + err = key.PrivateKey.Decrypt([]byte(passphrase)) + if err != nil { + conn.RemoveFromCache(cacheId) + continue + } + return []byte(passphrase), nil + } + return nil, err + } + return nil, fmt.Errorf("Unable to find key") +} + func init() { setAgentInfo() go func() { diff --git a/impl_non_darwin.go b/impl_non_darwin.go deleted file mode 100644 index e53bb37..0000000 --- a/impl_non_darwin.go +++ /dev/null @@ -1,12 +0,0 @@ -//+build !darwin - -package passgo - -import ( - "log" -) - -//Clip copies a string to the clipboard -func Clip(x string) { - log(Info, "Clipboard not implemented for this platform") -} diff --git a/main.go b/main.go index dedc938..67e804c 100644 --- a/main.go +++ b/main.go @@ -2,7 +2,6 @@ package passgo import ( "bufio" - "encoding/hex" "fmt" "io" "io/ioutil" @@ -14,8 +13,6 @@ import ( "strings" "time" - "github.com/jcmdev0/gpgagent" - "golang.org/x/crypto/openpgp" "golang.org/x/crypto/openpgp/armor" "golang.org/x/crypto/openpgp/packet" @@ -26,12 +23,12 @@ var ( ask openpgp.PromptFunction Keyring openpgp.KeyRing krTime time.Time - useGPG bool + useNative bool homeDir string ) func init() { - useGPG = true // default unless we can get the Go openpgp code to work + useNative = true // default unless we can get the Go openpgp code to work basename = regexp.MustCompile(".gpg$") } @@ -85,7 +82,7 @@ func getKeyring() error { //return fmt.Errorf("Can't open gnupg keyring.") return nil } - useGPG = false + useNative = false Keyring = kr krTime = time.Now() return nil @@ -219,38 +216,9 @@ func AskPass(prompts ...func() []byte) openpgp.PromptFunction { return ret } -//https://github.com/jcmdev0/gpgagent/blob/master/example/example.go -func GPGPrompt(keys []openpgp.Key, symmetric bool) ([]byte, error) { - conn, err := gpgagent.NewGpgAgentConn() - if err != nil { - return nil, err - } - defer conn.Close() - - for _, key := range keys { - cacheId := strings.ToUpper(hex.EncodeToString(key.PublicKey.Fingerprint[:])) - request := gpgagent.PassphraseRequest{CacheKey: cacheId} - for i := 0; i < 3; i++ { - var passphrase string - passphrase, err = conn.GetPassphrase(&request) - if err != nil { - continue - } - err = key.PrivateKey.Decrypt([]byte(passphrase)) - if err != nil { - conn.RemoveFromCache(cacheId) - continue - } - return []byte(passphrase), nil - } - return nil, err - } - return nil, fmt.Errorf("Unable to find key") -} - func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) { - if useGPG { - return s.gpgDecrypt(name) + if useNative { + return s.nativeDecrypt(name) } if ask == nil { ask = AskPass(prompts...) @@ -295,8 +263,8 @@ func (s *Store) Decrypt(name string, prompts ...func() []byte) (string, error) { func Identities() ([]string, error) { getKeyring() - if useGPG { - return gpgIdentities() + if useNative { + return nativeIdentities() } ret := make([]string, 0) for _, k := range Keyring.DecryptionKeys() { @@ -310,9 +278,9 @@ func Identities() ([]string, error) { func (s *Store) Insert(name, value string) error { var enc []byte var err error - //if !useGo { // golang openpgp code not implemented yet - fmt.Println("Calling gpgEncrypt") - enc, err = s.gpgEncrypt(value) + //if useNative { // golang openpgp code not implemented yet + fmt.Println("Calling nativeEncrypt") + enc, err = s.nativeEncrypt(value) if err != nil { return err }