-
Notifications
You must be signed in to change notification settings - Fork 13
/
pinentry.go
39 lines (34 loc) · 890 Bytes
/
pinentry.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
package pinentry
import (
"errors"
"github.com/twpayne/go-pinentry"
)
var (
ErrPinentryCancelled = errors.New("cancelled pinentry")
)
func GetPinentry(keyInfo string) ([]byte, error) {
// TODO: Include some additional key metadata
client, err := pinentry.NewClient(
pinentry.WithCommand("OPTION allow-external-password-cache"),
pinentry.WithCommandf("SETKEYINFO %v", keyInfo),
pinentry.WithBinaryNameFromGnuPGAgentConf(),
pinentry.WithDesc("Enter PIN for TPM key"),
pinentry.WithGPGTTY(),
pinentry.WithPrompt("PIN:"),
pinentry.WithTitle("ssh-tpm-agent PIN entry"),
)
if err != nil {
return nil, err
}
defer client.Close()
switch pin, fromCache, err := client.GetPIN(); {
case pinentry.IsCancelled(err):
return nil, ErrPinentryCancelled
case err != nil:
return nil, err
case fromCache:
return []byte(pin), nil
default:
return []byte(pin), nil
}
}