-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpg.go
90 lines (83 loc) · 2.5 KB
/
gpg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package util
import (
"bytes"
"os"
"os/exec"
"encoding/hex"
"fmt"
"strings"
"github.com/ProtonMail/go-crypto/openpgp"
"github.com/TykTechnologies/gromit/util/gpgagent"
"github.com/rs/zerolog/log"
"golang.org/x/crypto/openpgp/packet"
)
// GetSigningEntity gets the public keyring from gpg, using the agent
// Needs the gpgconf binary to find the agent socket path as gpg doesn't seem to set GPG_AUTH_INFO
// anymore.
func GetSigningEntity(kid uint64) (*openpgp.Entity, error) {
// Read keyring from gpg
// signkeyReader, err := gpgBinary("--export-secret-keys", strKeyid)
krFile := os.ExpandEnv("${HOME}/.gnupg/secring.gpg")
signkeyReader, err := keyringFile(krFile)
if err != nil {
return nil, fmt.Errorf("cannot find keyring, gpg --export-secret-keys %#x > %s", kid, krFile)
}
entityList, err := openpgp.ReadKeyRing(signkeyReader)
if err != nil {
return nil, err
}
for i, e := range entityList {
kid := e.PrimaryKey.KeyId
log.Debug().Uint64("keyid", kid).Msgf("keyid %d (%#x) ", i, kid)
}
keys := entityList.KeysByIdUsage(kid, packet.KeyFlagSign)
if len(keys) < 1 {
return nil, fmt.Errorf("No signing key for keyid %d (%#x)", kid, kid)
}
key := entityList[0]
if key.PrivateKey.Encrypted {
passphrase, err := getPassphrase(hex.EncodeToString(key.PrivateKey.Fingerprint[:]))
if err != nil {
return nil, fmt.Errorf("getting passphrase from agent: %w", err)
}
err = key.PrivateKey.Decrypt(passphrase)
if err != nil {
return nil, err
}
}
return key, nil
}
func getPassphrase(fp string) ([]byte, error) {
agentSocket, err := gpgconfBinary("--list-dirs", "agent-socket")
if err != nil {
return nil, fmt.Errorf("getting agent socket: %w", err)
}
conn, err := gpgagent.NewGpgAgentConn(agentSocket)
if err != nil {
return nil, fmt.Errorf("connecting to gpg-agent: %w", err)
}
defer conn.Close()
cacheID := strings.ToUpper(fp)
// TODO: Add prompt, etc.
request := gpgagent.PassphraseRequest{CacheKey: cacheID}
passphrase, err := conn.GetPassphrase(&request)
if err != nil {
return nil, fmt.Errorf("getting passphrase: %w", err)
}
return []byte(passphrase), nil
}
func gpgconfBinary(args ...string) (string, error) {
opBytes, err := exec.Command("gpgconf", args...).Output()
if err != nil {
return "", fmt.Errorf("cannot get output from gpgconf: %w", err)
}
return string(bytes.TrimRight(opBytes, "\n")), nil
}
func keyringFile(path string) (*os.File, error) {
// Open the private key file
krFile, err := os.Open(path)
if err != nil {
return nil, err
}
return krFile, nil
}