Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions internal/decryptor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/nicola-strappazzon/password-manager/internal/card"
"github.com/nicola-strappazzon/password-manager/internal/config"
"github.com/nicola-strappazzon/password-manager/internal/openpgp"
"github.com/nicola-strappazzon/password-manager/internal/term"
)
Expand All @@ -16,6 +17,14 @@ func Decrypt(passphrase, path string) (card.Card, error) {
return card.Card{}, err
}

belongs, err := openpgp.KeyBelongsToRecipient(cardID, config.GetRecipient())
if err != nil {
return card.Card{}, err
}
if !belongs {
return card.Card{}, fmt.Errorf("This file is not encrypted for the configured recipient (%s).", config.GetRecipient())
}

useCard, err := openpgp.RequiresSmartCard(cardID)
if err != nil {
return card.Card{}, err
Expand Down
33 changes: 33 additions & 0 deletions internal/openpgp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,39 @@ func CardIsReady() error {
return nil
}

func KeyBelongsToRecipient(keyID, recipient string) (bool, error) {
lookup := strings.TrimSuffix(recipient, "!")
out, err := runCommand(nil, "gpg", "--with-colons", "--list-keys", lookup)
if err != nil {
return false, fmt.Errorf("recipient %q not found in keyring", recipient)
}

keyID = strings.ToUpper(keyID)

for _, line := range strings.Split(string(out), "\n") {
fields := strings.Split(line, ":")
if len(fields) < 5 {
continue
}
switch fields[0] {
case "pub", "sub":
id := strings.ToUpper(fields[4])
if strings.HasSuffix(id, keyID) || strings.HasSuffix(keyID, id) {
return true, nil
}
case "fpr":
if len(fields) >= 10 {
fpr := strings.ToUpper(fields[9])
if strings.HasSuffix(fpr, keyID) || strings.HasSuffix(keyID, fpr) {
return true, nil
}
}
}
}

return false, nil
}

func Encrypt(in, recipient string) (out []byte, err error) {
out, err = runCommand(
[]byte(in),
Expand Down
Loading