From dbfd1332160e8a0418a29d54ee60dfe3cd8e5116 Mon Sep 17 00:00:00 2001 From: Romain LE JEUNE Date: Mon, 30 Oct 2023 13:19:32 +0100 Subject: [PATCH] fix(GODT-1224): Do not strip trailing whitespace for ClearScheme but for all others. --- message_send_types.go | 37 +++++++++++++++++++------------------ 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/message_send_types.go b/message_send_types.go index c5010db..4454447 100644 --- a/message_send_types.go +++ b/message_send_types.go @@ -158,7 +158,7 @@ func newMIMEPackage( mimeBody string, prefs map[string]SendPreferences, ) (*MessagePackage, error) { - decBodyKey, encBodyData, err := encSplit(kr, mimeBody) + decBodyKey, encBodyData, err := encSplit(kr, mimeBody, true) if err != nil { return nil, fmt.Errorf("failed to encrypt MIME body: %w", err) } @@ -220,7 +220,7 @@ func newTextPackage( return nil, fmt.Errorf("invalid MIME type for package: %s", mimeType) } - decBodyKey, encBodyData, err := encSplit(kr, body) + decBodyKey, encBodyData, err := encSplit(kr, body, true) if err != nil { return nil, fmt.Errorf("failed to encrypt message body: %w", err) } @@ -304,22 +304,13 @@ func newClearSignedPackage( prefs map[string]SendPreferences, attKeys map[string]*crypto.SessionKey, ) (*MessagePackage, error) { - encBody, err := kr.Encrypt(crypto.NewPlainMessage([]byte(body)), kr) - if err != nil { - return nil, fmt.Errorf("failed to encrypt MIME body: %w", err) - } - - splitEncBody, err := encBody.SplitMessage() - if err != nil { - return nil, fmt.Errorf("failed to split message: %w", err) - } - decBodyKey, err := kr.DecryptSessionKey(splitEncBody.GetBinaryKeyPacket()) + decBodyKey, encBodyData, err := encSplit(kr, body, false) if err != nil { - return nil, fmt.Errorf("failed to decrypt session key: %w", err) + return nil, fmt.Errorf("failed to encrypt message body: %w", err) } - pkg := newMessagePackage(rfc822.TextPlain, splitEncBody.GetBinaryDataPacket()) + pkg := newMessagePackage(rfc822.TextPlain, encBodyData) for addr, prefs := range prefs { if prefs.MIMEType != rfc822.TextPlain { @@ -353,10 +344,20 @@ func newClearSignedPackage( return pkg, nil } -func encSplit(kr *crypto.KeyRing, body string) (*crypto.SessionKey, []byte, error) { - encBody, err := kr.Encrypt(crypto.NewPlainMessage([]byte(body)), kr) - if err != nil { - return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err) +func encSplit(kr *crypto.KeyRing, body string, stripSpaces bool) (*crypto.SessionKey, []byte, error) { + var encBody *crypto.PGPMessage + if stripSpaces { + enc, err := kr.Encrypt(crypto.NewPlainMessage([]byte(body)), kr) + if err != nil { + return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err) + } + encBody = enc + } else { + enc, err := kr.Encrypt(crypto.NewPlainMessageFromString(body), kr) + if err != nil { + return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err) + } + encBody = enc } splitEncBody, err := encBody.SplitMessage()