From 77833c9854bac1dad628c78230eacb6f3733cadc Mon Sep 17 00:00:00 2001 From: Romain LE JEUNE Date: Mon, 30 Oct 2023 15:18:41 +0100 Subject: [PATCH] Revert "feat(GODT-1224): Add more test case." This reverts commit 84d8336ae54317e2da2b3a4ff689054ba728686b. This reverts commit 898374555330c2599001861f7e383adbadc19285. This reverts commit d4a5c59e97aeac80b9ad42b58979eb5a1e893f35. Which are no more needed since handled directly on gopenpgp level by this commit https://github.com/protonmail/gopenpgp/commit/b1893091522a0b56475ca39013e3d952568a4620 --- message_send_types.go | 83 +++++--------------------------------- message_send_types_test.go | 73 +-------------------------------- 2 files changed, 10 insertions(+), 146 deletions(-) diff --git a/message_send_types.go b/message_send_types.go index c5010db..69ed1f4 100644 --- a/message_send_types.go +++ b/message_send_types.go @@ -137,22 +137,6 @@ func (req *SendDraftReq) AddTextPackage( return nil } -func (req *SendDraftReq) AddClearSignedPackage( - kr *crypto.KeyRing, - body string, - prefs map[string]SendPreferences, - attKeys map[string]*crypto.SessionKey, -) error { - pkg, err := newClearSignedPackage(kr, body, prefs, attKeys) - if err != nil { - return err - } - - req.Packages = append(req.Packages, pkg) - - return nil -} - func newMIMEPackage( kr *crypto.KeyRing, mimeBody string, @@ -216,7 +200,7 @@ func newTextPackage( prefs map[string]SendPreferences, attKeys map[string]*crypto.SessionKey, ) (*MessagePackage, error) { - if !(mimeType == rfc822.TextPlain || mimeType == rfc822.TextHTML) { + if mimeType != rfc822.TextPlain && mimeType != rfc822.TextHTML { return nil, fmt.Errorf("invalid MIME type for package: %s", mimeType) } @@ -233,7 +217,13 @@ func newTextPackage( } if prefs.SignatureType == DetachedSignature && !prefs.Encrypt { - return nil, fmt.Errorf("text package cannot contain clear-signed body") + if prefs.EncryptionScheme == PGPInlineScheme { + return nil, fmt.Errorf("invalid encryption scheme for %s: %d", addr, prefs.EncryptionScheme) + } + + if prefs.EncryptionScheme == ClearScheme && mimeType != rfc822.TextPlain { + return nil, fmt.Errorf("invalid MIME type for clear package: %s", mimeType) + } } if prefs.EncryptionScheme == InternalScheme && !prefs.Encrypt { @@ -298,63 +288,8 @@ func newTextPackage( return pkg, nil } -func newClearSignedPackage( - kr *crypto.KeyRing, - body string, - 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()) - if err != nil { - return nil, fmt.Errorf("failed to decrypt session key: %w", err) - } - - pkg := newMessagePackage(rfc822.TextPlain, splitEncBody.GetBinaryDataPacket()) - - for addr, prefs := range prefs { - if prefs.MIMEType != rfc822.TextPlain { - return nil, fmt.Errorf("invalid MIME type for clear signed package: %s", prefs.MIMEType) - } - - if prefs.SignatureType != DetachedSignature { - return nil, fmt.Errorf("clear signed package must contain detached signature") - } - - if prefs.Encrypt || prefs.EncryptionScheme != ClearScheme { - return nil, fmt.Errorf("clear signed package cannot be encrypted") - } - - pkg.BodyKey = newSessionKey(decBodyKey) - - for attID, attKey := range attKeys { - pkg.AttachmentKeys[attID] = newSessionKey(attKey) - } - - recipient := &MessageRecipient{ - Type: prefs.EncryptionScheme, - Signature: prefs.SignatureType, - AttachmentKeyPackets: make(map[string]string), - } - - pkg.Addresses[addr] = recipient - pkg.Type |= prefs.EncryptionScheme - } - - return pkg, nil -} - func encSplit(kr *crypto.KeyRing, body string) (*crypto.SessionKey, []byte, error) { - encBody, err := kr.Encrypt(crypto.NewPlainMessage([]byte(body)), kr) + encBody, err := kr.Encrypt(crypto.NewPlainMessageFromString(body), kr) if err != nil { return nil, nil, fmt.Errorf("failed to encrypt MIME body: %w", err) } diff --git a/message_send_types_test.go b/message_send_types_test.go index fbb99be..c982131 100644 --- a/message_send_types_test.go +++ b/message_send_types_test.go @@ -290,7 +290,7 @@ func TestSendDraftReq_AddPackage(t *testing.T) { EncryptionScheme: proton.ClearScheme, MIMEType: rfc822.TextPlain, }}, - wantErr: true, + wantErr: false, }, { name: "clear plain text with bad scheme (error)", @@ -380,74 +380,3 @@ func TestSendDraftReq_AddPackage(t *testing.T) { }) } } - -func TestSendDraftReq_AddClearPackage(t *testing.T) { - key, err := crypto.GenerateKey("name", "email", "rsa", 2048) - require.NoError(t, err) - - kr, err := crypto.NewKeyRing(key) - require.NoError(t, err) - - tests := []struct { - name string - body string - prefs map[string]proton.SendPreferences - attKeys map[string]*crypto.SessionKey - wantErr bool - }{ - { - name: "clear plain text with signature", - body: "this is a text/plain body", - prefs: map[string]proton.SendPreferences{"clear-plain-with-sig@email.com": { - Encrypt: false, - SignatureType: proton.DetachedSignature, - EncryptionScheme: proton.ClearScheme, - MIMEType: rfc822.TextPlain, - }}, - wantErr: false, - }, - { - name: "clear plain text with bad scheme (error)", - body: "this is a text/plain body", - prefs: map[string]proton.SendPreferences{"clear-plain-with-sig@email.com": { - Encrypt: false, - SignatureType: proton.DetachedSignature, - EncryptionScheme: proton.PGPInlineScheme, - MIMEType: rfc822.TextPlain, - }}, - wantErr: true, - }, - { - name: "clear rich text with signature (error)", - body: "this is a text/html body", - prefs: map[string]proton.SendPreferences{"clear-plain-with-sig@email.com": { - Encrypt: false, - SignatureType: proton.DetachedSignature, - EncryptionScheme: proton.ClearScheme, - MIMEType: rfc822.TextHTML, - }}, - wantErr: true, - }, - { - name: "clear plain text with signature", - body: "this is a text/plain body", - prefs: map[string]proton.SendPreferences{"clear-plain-with-sig@email.com": { - Encrypt: true, - SignatureType: proton.DetachedSignature, - EncryptionScheme: proton.ClearScheme, - MIMEType: rfc822.TextPlain, - }}, - wantErr: true, - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - var req proton.SendDraftReq - - if err := req.AddClearSignedPackage(kr, tt.body, tt.prefs, tt.attKeys); (err != nil) != tt.wantErr { - t.Errorf("SendDraftReq.AddPackage() error = %v, wantErr %v", err, tt.wantErr) - } - }) - } -}