passforios-gopenpgp/crypto/mime.go

78 lines
2.5 KiB
Go
Raw Normal View History

package crypto
2018-09-02 15:02:26 +02:00
import (
"bytes"
"github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp/packet"
"io/ioutil"
"net/mail"
"net/textproto"
"strings"
2018-09-02 15:02:26 +02:00
)
func (pm PmCrypto) parseMIME(mimeBody string, verifierKey *KeyRing) (*pmmime.BodyCollector, int, []string, []string, error) {
2018-09-02 15:02:26 +02:00
mm, err := mail.ReadMessage(strings.NewReader(mimeBody))
if err != nil {
2018-09-05 14:56:06 +02:00
return nil, 0, nil, nil, err
2018-09-02 15:02:26 +02:00
}
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pm.getTimeGenerator()}
2018-09-02 15:02:26 +02:00
h := textproto.MIMEHeader(mm.Header)
mmBodyData, err := ioutil.ReadAll(mm.Body)
printAccepter := pmmime.NewMIMEPrinter()
bodyCollector := pmmime.NewBodyCollector(printAccepter)
attachmentsCollector := pmmime.NewAttachmentsCollector(bodyCollector)
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)
err = pmmime.VisitAll(bytes.NewReader(mmBodyData), h, signatureCollector)
2018-09-02 15:02:26 +02:00
2018-09-05 14:56:06 +02:00
verified := signatureCollector.verified
body := bodyCollector
atts := attachmentsCollector.GetAttachments()
attHeaders := attachmentsCollector.GetAttHeaders()
2018-09-05 14:56:06 +02:00
return body, verified, atts, attHeaders, nil
}
2018-09-02 15:02:26 +02:00
// define call back interface
type MIMECallbacks interface {
OnBody(body string, mimetype string)
OnAttachment(headers string, data []byte)
2018-09-02 15:02:26 +02:00
// Encrypted headers can be an attachment and thus be placed at the end of the mime structure
OnEncryptedHeaders(headers string)
OnVerified(verified int)
OnError(err error)
}
2018-09-02 15:02:26 +02:00
2019-01-11 00:23:00 +01:00
// Use ios/android only
func (pm *PmCrypto) DecryptMIMEMessage(encryptedText string, verifierKey *KeyRing, privateKeyRing *KeyRing,
2018-09-05 14:56:06 +02:00
passphrase string, callbacks MIMECallbacks, verifyTime int64) {
decsignverify, err := pm.DecryptMessageVerify(encryptedText, verifierKey, privateKeyRing, passphrase, verifyTime)
2018-09-05 14:56:06 +02:00
if err != nil {
callbacks.OnError(err)
2018-09-05 14:56:06 +02:00
return
2018-09-02 15:02:26 +02:00
}
body, verified, attachments, attachmentHeaders, err := pm.parseMIME(decsignverify.Plaintext, verifierKey)
2018-09-05 14:56:06 +02:00
if err != nil {
callbacks.OnError(err)
2018-09-05 14:56:06 +02:00
return
}
2018-09-02 15:02:26 +02:00
bodyContent, bodyMimeType := body.GetBody()
callbacks.OnBody(bodyContent, bodyMimeType)
2018-09-02 15:02:26 +02:00
for i := 0; i < len(attachments); i++ {
callbacks.OnAttachment(attachmentHeaders[i], []byte(attachments[i]))
2018-09-02 15:02:26 +02:00
}
callbacks.OnEncryptedHeaders("")
2018-09-05 14:56:06 +02:00
if decsignverify.Verify == notSigned {
callbacks.OnVerified(verified)
2018-09-05 14:56:06 +02:00
} else {
callbacks.OnVerified(decsignverify.Verify)
2018-09-05 14:56:06 +02:00
}
}