fix crash on missing keyring

This commit is contained in:
Jakub 2019-03-07 17:39:34 +01:00
parent 968888eb0e
commit 99c48c2e4e
3 changed files with 21 additions and 14 deletions

View file

@ -17,7 +17,7 @@ dille.
const testAttachmentEncrypted = `0ksB0fHC6Duezx/0TqpK/82HSl8+qCY0c2BCuyrSFoj6Dubd93T3//32jVYa624NYvfvxX+UxFKYKJxG09gFsU1IVc87cWvUgmUmgjU=` const testAttachmentEncrypted = `0ksB0fHC6Duezx/0TqpK/82HSl8+qCY0c2BCuyrSFoj6Dubd93T3//32jVYa624NYvfvxX+UxFKYKJxG09gFsU1IVc87cWvUgmUmgjU=`
func TestAttachment_GetKey(t *testing.T) { func TestAttachmentGetKey(t *testing.T) {
split, err := SeparateKeyAndData(testPrivateKeyRing, strings.NewReader(testKeyPackets), len(testKeyPackets), -1) split, err := SeparateKeyAndData(testPrivateKeyRing, strings.NewReader(testKeyPackets), len(testKeyPackets), -1)
if err != nil { if err != nil {
t.Fatal("Expected no error while decrypting attachment key, got:", err) t.Fatal("Expected no error while decrypting attachment key, got:", err)
@ -28,8 +28,7 @@ func TestAttachment_GetKey(t *testing.T) {
} }
} }
func TestAttachment_SetKey(t *testing.T) { func TestAttachmentSetKey(t *testing.T) {
var packets string var packets string
var err error var err error
@ -48,7 +47,7 @@ func TestAttachment_SetKey(t *testing.T) {
} }
} }
func TestAttachnent_EncryptDecrypt(t *testing.T) { func TestAttachnentEncryptDecrypt(t *testing.T) {
plainData, _ := base64.StdEncoding.DecodeString(testAttachmentCleartext) plainData, _ := base64.StdEncoding.DecodeString(testAttachmentCleartext)
var pmCrypto = PmCrypto{} var pmCrypto = PmCrypto{}
@ -66,7 +65,7 @@ func TestAttachnent_EncryptDecrypt(t *testing.T) {
s := string(redecData) s := string(redecData)
if testAttachmentCleartext != s { if testAttachmentCleartext != s {
t.Fatalf("Invalid decrypted attachment: expected %v, got %v", testAttachmentCleartext, s) t.Fatalf("Invalid decrypted attachment: expected\n%v\ngot\n%v", testAttachmentCleartext, s)
} }
} }

View file

@ -2,16 +2,18 @@ package crypto
import ( import (
"bytes" "bytes"
"github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp/packet"
"io/ioutil" "io/ioutil"
"net/mail" "net/mail"
"net/textproto" "net/textproto"
"strings" "strings"
pmmime "github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
) )
func (pm PmCrypto) parseMIME(mimeBody string, verifierKey *KeyRing) (*pmmime.BodyCollector, int, []string, []string, error) { func (pm PmCrypto) parseMIME(mimeBody string, verifierKey *KeyRing) (*pmmime.BodyCollector, int, []string, []string, error) {
mm, err := mail.ReadMessage(strings.NewReader(mimeBody)) mm, err := mail.ReadMessage(strings.NewReader(mimeBody))
if err != nil { if err != nil {
return nil, 0, nil, nil, err return nil, 0, nil, nil, err
@ -25,10 +27,14 @@ func (pm PmCrypto) parseMIME(mimeBody string, verifierKey *KeyRing) (*pmmime.Bod
bodyCollector := pmmime.NewBodyCollector(printAccepter) bodyCollector := pmmime.NewBodyCollector(printAccepter)
attachmentsCollector := pmmime.NewAttachmentsCollector(bodyCollector) attachmentsCollector := pmmime.NewAttachmentsCollector(bodyCollector)
mimeVisitor := pmmime.NewMimeVisitor(attachmentsCollector) mimeVisitor := pmmime.NewMimeVisitor(attachmentsCollector)
// TODO: build was failing on this unused 'str' variable. This code looks like WIP
//str, err := armor.ArmorKey(verifierKey)
signatureCollector := newSignatureCollector(mimeVisitor, verifierKey.entities, config) var pgpKering openpgp.KeyRing
if verifierKey != nil {
pgpKering = verifierKey.entities
}
signatureCollector := newSignatureCollector(mimeVisitor, pgpKering, config)
err = pmmime.VisitAll(bytes.NewReader(mmBodyData), h, signatureCollector) err = pmmime.VisitAll(bytes.NewReader(mmBodyData), h, signatureCollector)
verified := signatureCollector.verified verified := signatureCollector.verified

View file

@ -2,13 +2,15 @@ package crypto
import ( import (
"bytes" "bytes"
"github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
"io" "io"
"io/ioutil" "io/ioutil"
"mime" "mime"
"net/textproto" "net/textproto"
"github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
) )
// Use: ios/android only // Use: ios/android only