Skip to content

Commit 34cfa02

Browse files
authored
fix: warn about partial certificates with SMIME (#536)
1 parent 7edbe1a commit 34cfa02

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

sender/sender.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody
520520
cfgDir, _ := config.GetConfigDir()
521521
certsDir := filepath.Join(cfgDir, "certs")
522522
var certs []*x509.Certificate
523+
var missingCerts []string
523524

524525
for _, em := range allRecipients {
525526
em = strings.TrimSpace(em)
@@ -538,17 +539,26 @@ func SendEmail(account *config.Account, to, cc, bcc []string, subject, plainBody
538539
certPath = filepath.Join(certsDir, em+".pem")
539540
}
540541

541-
if certData, err := os.ReadFile(certPath); err == nil {
542-
if block, _ := pem.Decode(certData); block != nil {
543-
if cert, err := x509.ParseCertificate(block.Bytes); err == nil {
544-
certs = append(certs, cert)
545-
}
546-
}
542+
certData, err := os.ReadFile(certPath)
543+
if err != nil {
544+
missingCerts = append(missingCerts, em)
545+
continue
546+
}
547+
block, _ := pem.Decode(certData)
548+
if block == nil {
549+
missingCerts = append(missingCerts, em)
550+
continue
551+
}
552+
cert, err := x509.ParseCertificate(block.Bytes)
553+
if err != nil {
554+
missingCerts = append(missingCerts, em)
555+
continue
547556
}
557+
certs = append(certs, cert)
548558
}
549559

550-
if len(certs) == 0 {
551-
return nil, errors.New("cannot encrypt: no valid public certificates found for recipients")
560+
if len(missingCerts) > 0 {
561+
return nil, fmt.Errorf("cannot encrypt: missing or invalid S/MIME certificates for: %s", strings.Join(missingCerts, ", "))
552562
}
553563

554564
encryptedDer, err := pkcs7.Encrypt(payloadToEncrypt, certs)

0 commit comments

Comments
 (0)