pgp: check encryption keys before sending message
Add check for public keys of all message recipients (to, cc, and bcc) before sending the message. Adds an OnFocusLost callback to header editors to facilitate a callback for checking keys whenever a new recipient is added (OnChange results in too many keyring checks). Once encryption is initially set, the callbacks are registered. If a public key is not available for any recipient, encryption is turned off. However, notably, the callbacks are still registered meaning as s soon as the user removes the recipients with missing keys, encryption is turned back on. Signed-off-by: Tim Culverhouse <tim@timculverhouse.com> Tested-by: Koni Marti <koni.marti@gmail.com>
This commit is contained in:
parent
bb400c7d88
commit
32a16dcd8d
7 changed files with 107 additions and 21 deletions
widgets
|
@ -198,8 +198,21 @@ func (c *Composer) Sign() bool {
|
|||
}
|
||||
|
||||
func (c *Composer) SetEncrypt(encrypt bool) *Composer {
|
||||
c.encrypt = encrypt
|
||||
c.updateCrypto()
|
||||
if !encrypt {
|
||||
c.encrypt = encrypt
|
||||
c.updateCrypto()
|
||||
return c
|
||||
}
|
||||
// Check on any attempt to encrypt, and any lost focus of "to", "cc", or
|
||||
// "bcc" field. Use OnFocusLost instead of OnChange to limit keyring checks
|
||||
c.encrypt = c.checkEncryptionKeys("")
|
||||
if c.crypto.setEncOneShot {
|
||||
// Prevent registering a lot of callbacks
|
||||
c.OnFocusLost("to", c.checkEncryptionKeys)
|
||||
c.OnFocusLost("cc", c.checkEncryptionKeys)
|
||||
c.OnFocusLost("bcc", c.checkEncryptionKeys)
|
||||
c.crypto.setEncOneShot = false
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
|
@ -365,6 +378,15 @@ func (c *Composer) OnHeaderChange(header string, fn func(subject string)) {
|
|||
}
|
||||
}
|
||||
|
||||
// OnFocusLost registers an OnFocusLost callback for the specified header.
|
||||
func (c *Composer) OnFocusLost(header string, fn func(input string) bool) {
|
||||
if editor, ok := c.editors[strings.ToLower(header)]; ok {
|
||||
editor.OnFocusLost(func() {
|
||||
fn(editor.input.String())
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Composer) OnClose(fn func(composer *Composer)) {
|
||||
c.onClose = append(c.onClose, fn)
|
||||
}
|
||||
|
@ -984,6 +1006,12 @@ func (he *headerEditor) OnChange(fn func()) {
|
|||
})
|
||||
}
|
||||
|
||||
func (he *headerEditor) OnFocusLost(fn func()) {
|
||||
he.input.OnFocusLost(func(_ *ui.TextInput) {
|
||||
fn()
|
||||
})
|
||||
}
|
||||
|
||||
type reviewMessage struct {
|
||||
composer *Composer
|
||||
grid *ui.Grid
|
||||
|
@ -1090,18 +1118,21 @@ func (rm *reviewMessage) Draw(ctx *ui.Context) {
|
|||
}
|
||||
|
||||
type cryptoStatus struct {
|
||||
title string
|
||||
status *ui.Text
|
||||
uiConfig *config.UIConfig
|
||||
signKey string
|
||||
title string
|
||||
status *ui.Text
|
||||
uiConfig *config.UIConfig
|
||||
signKey string
|
||||
setEncOneShot bool
|
||||
}
|
||||
|
||||
func newCryptoStatus(uiConfig *config.UIConfig) *cryptoStatus {
|
||||
defaultStyle := uiConfig.GetStyle(config.STYLE_DEFAULT)
|
||||
return &cryptoStatus{
|
||||
title: "Security",
|
||||
status: ui.NewText("", defaultStyle),
|
||||
uiConfig: uiConfig,
|
||||
title: "Security",
|
||||
status: ui.NewText("", defaultStyle),
|
||||
uiConfig: uiConfig,
|
||||
signKey: "",
|
||||
setEncOneShot: true,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1124,3 +1155,33 @@ func (cs *cryptoStatus) OnInvalidate(fn func(ui.Drawable)) {
|
|||
fn(cs)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Composer) checkEncryptionKeys(_ string) bool {
|
||||
rcpts, err := getRecipientsEmail(c)
|
||||
if err != nil {
|
||||
// checkEncryptionKeys gets registered as a callback and must
|
||||
// explicitly call c.SetEncrypt(false) when encryption is not possible
|
||||
c.SetEncrypt(false)
|
||||
st := fmt.Sprintf("Cannot encrypt: %v", err)
|
||||
c.aerc.statusline.PushError(st)
|
||||
return false
|
||||
}
|
||||
var mk []string
|
||||
for _, rcpt := range rcpts {
|
||||
key, err := c.aerc.Crypto.GetKeyId(rcpt)
|
||||
if err != nil || key == "" {
|
||||
mk = append(mk, rcpt)
|
||||
}
|
||||
}
|
||||
if len(mk) > 0 {
|
||||
c.SetEncrypt(false)
|
||||
st := fmt.Sprintf("Cannot encrypt, missing keys: %s", strings.Join(mk, ", "))
|
||||
c.aerc.statusline.PushError(st)
|
||||
return false
|
||||
}
|
||||
// If callbacks were registered, encrypt will be set when user removes
|
||||
// recipients with missing keys
|
||||
c.encrypt = true
|
||||
c.updateCrypto()
|
||||
return true
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue