passforios-gopenpgp/crypto/signature_collector.go

102 lines
2.7 KiB
Go
Raw Normal View History

package crypto
2018-09-05 14:56:06 +02:00
import (
2018-09-19 12:51:44 +02:00
"bytes"
"io"
2018-09-05 14:56:06 +02:00
"io/ioutil"
"mime"
2018-09-19 12:51:44 +02:00
"net/textproto"
2019-03-07 17:39:34 +01:00
"github.com/ProtonMail/go-pm-mime"
"golang.org/x/crypto/openpgp"
"golang.org/x/crypto/openpgp/packet"
2018-09-19 12:51:44 +02:00
)
2018-09-05 14:56:06 +02:00
2019-05-13 12:33:01 +00:00
// SignatureCollector structure
2018-09-05 14:56:06 +02:00
type SignatureCollector struct {
config *packet.Config
keyring openpgp.KeyRing
target pmmime.VisitAcceptor
signature string
verified int
}
2019-05-13 12:33:01 +00:00
func newSignatureCollector(targetAcceptor pmmime.VisitAcceptor, keyring openpgp.KeyRing, config *packet.Config) *SignatureCollector {
2018-09-05 14:56:06 +02:00
return &SignatureCollector{
2019-05-13 12:33:01 +00:00
target: targetAcceptor,
2018-09-19 12:51:44 +02:00
config: config,
2018-09-05 14:56:06 +02:00
keyring: keyring,
}
}
2019-05-13 12:33:01 +00:00
// Accept
2018-09-05 14:56:06 +02:00
func (sc *SignatureCollector) Accept(part io.Reader, header textproto.MIMEHeader, hasPlainSibling bool, isFirst, isLast bool) (err error) {
parentMediaType, params, _ := mime.ParseMediaType(header.Get("Content-Type"))
if parentMediaType == "multipart/signed" {
2019-05-13 12:33:01 +00:00
newPart, rawBody := pmmime.GetRawMimePart(part, "--" + params["boundary"])
2018-09-05 14:56:06 +02:00
var multiparts []io.Reader
var multipartHeaders []textproto.MIMEHeader
2019-03-07 16:56:12 +01:00
if multiparts, multipartHeaders, err = pmmime.GetMultipartParts(newPart, params); err == nil {
2018-09-05 14:56:06 +02:00
hasPlainChild := false
for _, header := range multipartHeaders {
mediaType, _, _ := mime.ParseMediaType(header.Get("Content-Type"))
if mediaType == "text/plain" {
hasPlainChild = true
}
}
if len(multiparts) != 2 {
sc.verified = notSigned
// Invalid multipart/signed format just pass along
ioutil.ReadAll(rawBody)
for i, p := range multiparts {
if err = sc.target.Accept(p, multipartHeaders[i], hasPlainChild, true, true); err != nil {
return
}
}
return
}
// actual multipart/signed format
err = sc.target.Accept(multiparts[0], multipartHeaders[0], hasPlainChild, true, true)
if err != nil {
return
}
2019-05-13 12:33:01 +00:00
2018-09-05 14:56:06 +02:00
partData, _ := ioutil.ReadAll(multiparts[1])
decodedPart := pmmime.DecodeContentEncoding(bytes.NewReader(partData), multipartHeaders[1].Get("Content-Transfer-Encoding"))
buffer, err := ioutil.ReadAll(decodedPart)
if err != nil {
return err
}
buffer, err = pmmime.DecodeCharset(buffer, params)
if err != nil {
return err
}
sc.signature = string(buffer)
2018-09-19 12:51:44 +02:00
str, _ := ioutil.ReadAll(rawBody)
rawBody = bytes.NewReader(str)
2018-09-05 14:56:06 +02:00
if sc.keyring != nil {
_, err = openpgp.CheckArmoredDetachedSignature(sc.keyring, rawBody, bytes.NewReader(buffer), sc.config)
if err != nil {
sc.verified = failed
} else {
sc.verified = ok
}
} else {
sc.verified = noVerifier
}
return nil
}
return
}
sc.target.Accept(part, header, hasPlainSibling, isFirst, isLast)
return nil
}
2019-05-13 12:33:01 +00:00
// GetSignature
func (sc SignatureCollector) GetSignature() string {
return sc.signature
2018-09-05 14:56:06 +02:00
}