passforios-gopenpgp/crypto/mime.go

91 lines
2.6 KiB
Go
Raw Normal View History

package crypto
2018-09-02 15:02:26 +02:00
import (
"bytes"
"io/ioutil"
"net/mail"
"net/textproto"
"strings"
2019-03-07 17:39:34 +01:00
2019-05-14 17:02:28 +02:00
pmmime "github.com/ProtonMail/go-mime"
2019-03-07 17:39:34 +01:00
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
2018-09-02 15:02:26 +02:00
)
2019-05-14 18:05:01 +02:00
func (pgp GopenPGP) 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
}
2019-05-14 18:05:01 +02:00
config := &packet.Config{DefaultCipher: packet.CipherAES256, Time: pgp.getTimeGenerator()}
2018-09-02 15:02:26 +02:00
h := textproto.MIMEHeader(mm.Header)
mmBodyData, err := ioutil.ReadAll(mm.Body)
if err != nil {
return nil, 0, nil, nil, err
}
2018-09-02 15:02:26 +02:00
printAccepter := pmmime.NewMIMEPrinter()
bodyCollector := pmmime.NewBodyCollector(printAccepter)
attachmentsCollector := pmmime.NewAttachmentsCollector(bodyCollector)
mimeVisitor := pmmime.NewMimeVisitor(attachmentsCollector)
2019-03-07 17:39:34 +01:00
var pgpKering openpgp.KeyRing
if verifierKey != nil {
pgpKering = verifierKey.entities
}
signatureCollector := newSignatureCollector(mimeVisitor, pgpKering, 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()
return body, verified, atts, attHeaders, err
}
// MIMECallbacks defines a call back methods to process MIME message
2018-09-02 15:02:26 +02:00
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-05-13 12:33:01 +00:00
// DecryptMIMEMessage decrypts a MIME message
2019-05-14 18:05:01 +02:00
func (pgp *GopenPGP) DecryptMIMEMessage(
encryptedText string, verifierKey, privateKeyRing *KeyRing,
passphrase string, callbacks MIMECallbacks, verifyTime int64,
) {
2019-05-14 18:05:01 +02:00
decsignverify, err := pgp.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
}
2019-05-14 18:05:01 +02:00
body, verified, attachments, attachmentHeaders, err := pgp.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
}
}