Skip to content

Commit

Permalink
Revert "feat(GODT-1224): Add support for clear-signed sending packages"
Browse files Browse the repository at this point in the history
This reverts commit d4a5c59.
  • Loading branch information
rlejeune74 committed Oct 30, 2023
1 parent 8213d7d commit 607d8aa
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 134 deletions.
83 changes: 9 additions & 74 deletions message_send_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down
60 changes: 0 additions & 60 deletions message_send_types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -380,63 +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]SendPreferences
attKeys map[string]*crypto.SessionKey
wantErr bool
}{
{
name: "clear plain text with signature",
body: "this is a text/plain body",
prefs: map[string]SendPreferences{"[email protected]": {
Encrypt: false,
SignatureType: DetachedSignature,
EncryptionScheme: ClearScheme,
MIMEType: rfc822.TextPlain,
}},
wantErr: false,
},
{
name: "clear plain text with bad scheme (error)",
body: "this is a text/plain body",
prefs: map[string]SendPreferences{"[email protected]": {
Encrypt: false,
SignatureType: DetachedSignature,
EncryptionScheme: PGPInlineScheme,
MIMEType: rfc822.TextPlain,
}},
wantErr: true,
},
{
name: "clear rich text with signature (error)",
body: "this is a text/html body",
prefs: map[string]SendPreferences{"[email protected]": {
Encrypt: false,
SignatureType: DetachedSignature,
EncryptionScheme: ClearScheme,
MIMEType: rfc822.TextHTML,
}},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
var req 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)
}
})
}
}

0 comments on commit 607d8aa

Please sign in to comment.