2015-08-25 21:24:19 +00:00
|
|
|
package pgpkeys
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2016-01-18 22:01:52 +00:00
|
|
|
|
2016-08-11 12:31:43 +00:00
|
|
|
"github.com/keybase/go-crypto/openpgp"
|
2015-08-25 21:24:19 +00:00
|
|
|
)
|
|
|
|
|
2017-09-05 03:57:55 +00:00
|
|
|
// PubKeyFileFlag implements flag.Value and command.Example to receive exactly
|
|
|
|
// one PGP or keybase key via a flag.
|
|
|
|
type PubKeyFileFlag string
|
|
|
|
|
|
|
|
func (p *PubKeyFileFlag) String() string { return string(*p) }
|
|
|
|
|
|
|
|
func (p *PubKeyFileFlag) Set(val string) error {
|
|
|
|
if p != nil && *p != "" {
|
|
|
|
return errors.New("can only be specified once")
|
|
|
|
}
|
|
|
|
|
|
|
|
keys, err := ParsePGPKeys(strings.Split(val, ","))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(keys) > 1 {
|
|
|
|
return errors.New("can only specify one pgp key")
|
|
|
|
}
|
|
|
|
|
|
|
|
*p = PubKeyFileFlag(keys[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PubKeyFileFlag) Example() string { return "keybase:user" }
|
|
|
|
|
|
|
|
// PGPPubKeyFiles implements the flag.Value interface and allows parsing and
|
|
|
|
// reading a list of PGP public key files.
|
2015-08-25 21:24:19 +00:00
|
|
|
type PubKeyFilesFlag []string
|
|
|
|
|
|
|
|
func (p *PubKeyFilesFlag) String() string {
|
|
|
|
return fmt.Sprint(*p)
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:57:55 +00:00
|
|
|
func (p *PubKeyFilesFlag) Set(val string) error {
|
2015-08-25 21:24:19 +00:00
|
|
|
if len(*p) > 0 {
|
2017-09-05 03:57:55 +00:00
|
|
|
return errors.New("can only be specified once")
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2016-01-01 01:43:24 +00:00
|
|
|
|
2017-09-05 03:57:55 +00:00
|
|
|
keys, err := ParsePGPKeys(strings.Split(val, ","))
|
2016-01-01 01:43:24 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-05 03:57:55 +00:00
|
|
|
*p = PubKeyFilesFlag(keys)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *PubKeyFilesFlag) Example() string { return "keybase:user1, keybase:user2, ..." }
|
|
|
|
|
|
|
|
// ParsePGPKeys takes a list of PGP keys and parses them either using keybase
|
|
|
|
// or reading them from disk and returns the "expanded" list of pgp keys in
|
|
|
|
// the same order.
|
|
|
|
func ParsePGPKeys(keyfiles []string) ([]string, error) {
|
|
|
|
keys := make([]string, len(keyfiles))
|
|
|
|
|
|
|
|
keybaseMap, err := FetchKeybasePubkeys(keyfiles)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, keyfile := range keyfiles {
|
|
|
|
keyfile = strings.TrimSpace(keyfile)
|
|
|
|
|
2016-01-04 16:18:04 +00:00
|
|
|
if strings.HasPrefix(keyfile, kbPrefix) {
|
2017-09-05 03:57:55 +00:00
|
|
|
key, ok := keybaseMap[keyfile]
|
|
|
|
if !ok || key == "" {
|
|
|
|
return nil, fmt.Errorf("keybase user %q not found", strings.TrimPrefix(keyfile, kbPrefix))
|
2016-01-01 01:43:24 +00:00
|
|
|
}
|
2017-09-05 03:57:55 +00:00
|
|
|
keys[i] = key
|
2016-01-01 01:43:24 +00:00
|
|
|
continue
|
|
|
|
}
|
2015-12-16 21:56:15 +00:00
|
|
|
|
|
|
|
pgpStr, err := ReadPGPFile(keyfile)
|
2015-08-25 21:24:19 +00:00
|
|
|
if err != nil {
|
2017-09-05 03:57:55 +00:00
|
|
|
return nil, err
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2017-09-05 03:57:55 +00:00
|
|
|
keys[i] = pgpStr
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2017-09-05 03:57:55 +00:00
|
|
|
|
|
|
|
return keys, nil
|
2015-08-25 21:24:19 +00:00
|
|
|
}
|
2015-12-16 21:56:15 +00:00
|
|
|
|
2017-09-05 03:57:55 +00:00
|
|
|
// ReadPGPFile reads the given PGP file from disk.
|
2015-12-16 21:56:15 +00:00
|
|
|
func ReadPGPFile(path string) (string, error) {
|
|
|
|
if path[0] == '@' {
|
|
|
|
path = path[1:]
|
|
|
|
}
|
|
|
|
f, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
_, err = buf.ReadFrom(f)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2016-01-18 22:01:52 +00:00
|
|
|
// First parse as an armored keyring file, if that doesn't work, treat it as a straight binary/b64 string
|
|
|
|
keyReader := bytes.NewReader(buf.Bytes())
|
|
|
|
entityList, err := openpgp.ReadArmoredKeyRing(keyReader)
|
|
|
|
if err == nil {
|
|
|
|
if len(entityList) != 1 {
|
|
|
|
return "", fmt.Errorf("more than one key found in file %s", path)
|
|
|
|
}
|
|
|
|
if entityList[0] == nil {
|
|
|
|
return "", fmt.Errorf("primary key was nil for file %s", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
serializedEntity := bytes.NewBuffer(nil)
|
|
|
|
err = entityList[0].Serialize(serializedEntity)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("error serializing entity for file %s: %s", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return base64.StdEncoding.EncodeToString(serializedEntity.Bytes()), nil
|
|
|
|
}
|
|
|
|
|
2015-12-16 21:56:15 +00:00
|
|
|
_, err = base64.StdEncoding.DecodeString(buf.String())
|
|
|
|
if err == nil {
|
|
|
|
return buf.String(), nil
|
|
|
|
}
|
|
|
|
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
|
|
|
|
|
|
|
|
}
|