aboutsummaryrefslogtreecommitdiffstats
path: root/lib/crypto
diff options
context:
space:
mode:
authorMoritz Poldrack <git@moritz.sh>2022-07-29 22:31:54 +0200
committerRobin Jarry <robin@jarry.cc>2022-08-04 21:57:57 +0200
commit5ca6022d007bc83daba1e7297480d37f56554455 (patch)
treecc592d08276b7f0c8db6a4280bb7fb648b2548fe /lib/crypto
parenta8d631177f4315810f0ab7c7d66d38e332ab0916 (diff)
downloadaerc-5ca6022d007bc83daba1e7297480d37f56554455.tar.gz
lint: ensure errors are at least logged (errcheck)
Signed-off-by: Moritz Poldrack <moritz@poldrack.dev> Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'lib/crypto')
-rw-r--r--lib/crypto/gpg/gpgbin/encrypt.go9
-rw-r--r--lib/crypto/gpg/gpgbin/gpgbin.go12
-rw-r--r--lib/crypto/gpg/gpgbin/keys.go5
-rw-r--r--lib/crypto/gpg/gpgbin/sign.go9
-rw-r--r--lib/crypto/gpg/gpgbin/verify.go13
-rw-r--r--lib/crypto/gpg/reader.go8
-rw-r--r--lib/crypto/gpg/writer.go19
-rw-r--r--lib/crypto/pgp/pgp.go7
8 files changed, 59 insertions, 23 deletions
diff --git a/lib/crypto/gpg/gpgbin/encrypt.go b/lib/crypto/gpg/gpgbin/encrypt.go
index 31245a74..9ca45269 100644
--- a/lib/crypto/gpg/gpgbin/encrypt.go
+++ b/lib/crypto/gpg/gpgbin/encrypt.go
@@ -25,15 +25,18 @@ func Encrypt(r io.Reader, to []string, from string) ([]byte, error) {
args = append(args, "--encrypt", "-")
g := newGpg(r, args)
- g.cmd.Run()
+ err := g.cmd.Run()
+ if err != nil {
+ return nil, fmt.Errorf("gpg: failed to run encryption: %w", err)
+ }
outRdr := bytes.NewReader(g.stdout.Bytes())
var md models.MessageDetails
- err := parse(outRdr, &md)
+ err = parse(outRdr, &md)
if err != nil {
return nil, fmt.Errorf("gpg: failure to encrypt: %v. check public key(s)", err)
}
var buf bytes.Buffer
- io.Copy(&buf, md.Body)
+ _, _ = io.Copy(&buf, md.Body)
return buf.Bytes(), nil
}
diff --git a/lib/crypto/gpg/gpgbin/gpgbin.go b/lib/crypto/gpg/gpgbin/gpgbin.go
index 2eb83dc9..1e8c7dcd 100644
--- a/lib/crypto/gpg/gpgbin/gpgbin.go
+++ b/lib/crypto/gpg/gpgbin/gpgbin.go
@@ -63,7 +63,11 @@ func getIdentity(key uint64) string {
var outbuf strings.Builder
cmd.Stdout = &outbuf
- cmd.Run()
+ err := cmd.Run()
+ if err != nil {
+ logging.Errorf("gpg: failed to get identity: %v", err)
+ return ""
+ }
out := strings.Split(outbuf.String(), "\n")
for _, line := range out {
if strings.HasPrefix(line, "uid") {
@@ -85,7 +89,11 @@ func getKeyId(s string, private bool) string {
var outbuf strings.Builder
cmd.Stdout = &outbuf
- cmd.Run()
+ err := cmd.Run()
+ if err != nil {
+ logging.Errorf("gpg: failed to get key ID: %v", err)
+ return ""
+ }
out := strings.Split(outbuf.String(), "\n")
for _, line := range out {
if strings.HasPrefix(line, "fpr") {
diff --git a/lib/crypto/gpg/gpgbin/keys.go b/lib/crypto/gpg/gpgbin/keys.go
index bef90cf6..ca166e9d 100644
--- a/lib/crypto/gpg/gpgbin/keys.go
+++ b/lib/crypto/gpg/gpgbin/keys.go
@@ -36,7 +36,10 @@ func ExportPublicKey(k string) (io.Reader, error) {
var stderr strings.Builder
cmd.Stdout = &outbuf
cmd.Stderr = &stderr
- cmd.Run()
+ err := cmd.Run()
+ if err != nil {
+ return nil, fmt.Errorf("gpg: export failed: %w", err)
+ }
if strings.Contains(stderr.String(), "gpg") {
return nil, fmt.Errorf("gpg: error exporting key")
}
diff --git a/lib/crypto/gpg/gpgbin/sign.go b/lib/crypto/gpg/gpgbin/sign.go
index 31fd2b5a..caf31d14 100644
--- a/lib/crypto/gpg/gpgbin/sign.go
+++ b/lib/crypto/gpg/gpgbin/sign.go
@@ -17,15 +17,18 @@ func Sign(r io.Reader, from string) ([]byte, string, error) {
}
g := newGpg(r, args)
- g.cmd.Run()
+ err := g.cmd.Run()
+ if err != nil {
+ return nil, "", fmt.Errorf("failed to run signing: %w", err)
+ }
outRdr := bytes.NewReader(g.stdout.Bytes())
var md models.MessageDetails
- err := parse(outRdr, &md)
+ err = parse(outRdr, &md)
if err != nil {
return nil, "", fmt.Errorf("failed to parse messagedetails: %v", err)
}
var buf bytes.Buffer
- io.Copy(&buf, md.Body)
+ _, _ = io.Copy(&buf, md.Body)
return buf.Bytes(), md.Micalg, nil
}
diff --git a/lib/crypto/gpg/gpgbin/verify.go b/lib/crypto/gpg/gpgbin/verify.go
index be9f26f7..e5930849 100644
--- a/lib/crypto/gpg/gpgbin/verify.go
+++ b/lib/crypto/gpg/gpgbin/verify.go
@@ -2,6 +2,7 @@ package gpgbin
import (
"bytes"
+ "fmt"
"io"
"io/ioutil"
"os"
@@ -19,7 +20,7 @@ func Verify(m io.Reader, s io.Reader) (*models.MessageDetails, error) {
if err != nil {
return nil, err
}
- io.Copy(sig, s)
+ _, _ = io.Copy(sig, s)
sig.Close()
defer os.Remove(sig.Name())
args = append(args, sig.Name(), "-")
@@ -29,11 +30,17 @@ func Verify(m io.Reader, s io.Reader) (*models.MessageDetails, error) {
return nil, err
}
g := newGpg(bytes.NewReader(orig), args)
- g.cmd.Run()
+ err = g.cmd.Run()
+ if err != nil {
+ return nil, fmt.Errorf("gpg: failed to run verification: %w", err)
+ }
out := bytes.NewReader(g.stdout.Bytes())
md := new(models.MessageDetails)
- parse(out, md)
+ err = parse(out, md)
+ if err != nil {
+ return nil, fmt.Errorf("gpg: failed to parse result: %w", err)
+ }
md.Body = bytes.NewReader(orig)
diff --git a/lib/crypto/gpg/reader.go b/lib/crypto/gpg/reader.go
index bf977ed4..40da82f8 100644
--- a/lib/crypto/gpg/reader.go
+++ b/lib/crypto/gpg/reader.go
@@ -38,7 +38,7 @@ func NewReader(h textproto.Header, body io.Reader) (*Reader, error) {
}
var headerBuf bytes.Buffer
- textproto.WriteHeader(&headerBuf, h)
+ _ = textproto.WriteHeader(&headerBuf, h)
return &Reader{
Header: h,
@@ -123,7 +123,7 @@ func newEncryptedReader(h textproto.Header, mr *textproto.MultipartReader) (*Rea
}
var headerBuf bytes.Buffer
- textproto.WriteHeader(&headerBuf, cleartextHeader)
+ _ = textproto.WriteHeader(&headerBuf, cleartextHeader)
md.Body = io.MultiReader(&headerBuf, cleartext)
return &Reader{
@@ -139,11 +139,11 @@ func newSignedReader(h textproto.Header, mr *textproto.MultipartReader, micalg s
return nil, fmt.Errorf("gpgmail: failed to read signed part in multipart/signed message: %v", err)
}
var headerBuf bytes.Buffer
- textproto.WriteHeader(&headerBuf, p.Header)
+ _ = textproto.WriteHeader(&headerBuf, p.Header)
var msg bytes.Buffer
headerRdr := bytes.NewReader(headerBuf.Bytes())
fullMsg := io.MultiReader(headerRdr, p)
- io.Copy(&msg, fullMsg)
+ _, _ = io.Copy(&msg, fullMsg)
sig, err := mr.NextPart()
if err != nil {
diff --git a/lib/crypto/gpg/writer.go b/lib/crypto/gpg/writer.go
index 96d083e8..0648ec24 100644
--- a/lib/crypto/gpg/writer.go
+++ b/lib/crypto/gpg/writer.go
@@ -30,7 +30,10 @@ func (es *EncrypterSigner) Close() (err error) {
if err != nil {
return err
}
- es.encryptedWriter.Write(enc)
+ _, err = es.encryptedWriter.Write(enc)
+ if err != nil {
+ return fmt.Errorf("gpg: failed to write encrypted writer: %w", err)
+ }
return nil
}
@@ -65,8 +68,8 @@ func (s *Signer) Close() (err error) {
}
boundary := s.mw.Boundary()
fmt.Fprintf(s.w, "--%s\r\n", boundary)
- s.w.Write(s.signedMsg.Bytes())
- s.w.Write([]byte("\r\n"))
+ _, _ = s.w.Write(s.signedMsg.Bytes())
+ _, _ = s.w.Write([]byte("\r\n"))
var signedHeader textproto.Header
signedHeader.Set("Content-Type", "application/pgp-signature; name=\"signature.asc\"")
@@ -100,7 +103,10 @@ func Encrypt(w io.Writer, h textproto.Header, rcpts []string, from string) (io.W
mw := textproto.NewMultipartWriter(w)
if forceBoundary != "" {
- mw.SetBoundary(forceBoundary)
+ err := mw.SetBoundary(forceBoundary)
+ if err != nil {
+ return nil, fmt.Errorf("gpg: failed to set boundary: %w", err)
+ }
}
params := map[string]string{
@@ -154,7 +160,10 @@ func Sign(w io.Writer, h textproto.Header, from string) (io.WriteCloser, error)
mw := textproto.NewMultipartWriter(w)
if forceBoundary != "" {
- mw.SetBoundary(forceBoundary)
+ err := mw.SetBoundary(forceBoundary)
+ if err != nil {
+ return nil, fmt.Errorf("gpg: failed to set boundary: %w", err)
+ }
}
var msg bytes.Buffer
diff --git a/lib/crypto/pgp/pgp.go b/lib/crypto/pgp/pgp.go
index c305f068..579395f9 100644
--- a/lib/crypto/pgp/pgp.go
+++ b/lib/crypto/pgp/pgp.go
@@ -31,7 +31,10 @@ var (
func (m *Mail) Init() error {
logging.Infof("Initializing PGP keyring")
- os.MkdirAll(path.Join(xdg.DataHome(), "aerc"), 0o700)
+ err := os.MkdirAll(path.Join(xdg.DataHome(), "aerc"), 0o700)
+ if err != nil {
+ return fmt.Errorf("failed to create data directory: %v", err)
+ }
lockpath := path.Join(xdg.DataHome(), "aerc", "keyring.lock")
lockfile, err := os.OpenFile(lockpath, os.O_CREATE|os.O_EXCL, 0o600)
@@ -294,7 +297,7 @@ func (m *Mail) ExportKey(k string) (io.Reader, error) {
if err != nil {
return nil, fmt.Errorf("pgp: error exporting key: %v", err)
}
- w.Write(pks.Bytes())
+ _, err = w.Write(pks.Bytes())
if err != nil {
return nil, fmt.Errorf("pgp: error exporting key: %v", err)
}