passforios-gopenpgp/crypto/signature_collector.go

123 lines
3 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
gomime "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-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 gomime.VisitAcceptor
2018-09-05 14:56:06 +02:00
signature string
verified error
2018-09-05 14:56:06 +02:00
}
func newSignatureCollector(
targetAcceptor gomime.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,
}
}
// Accept collects the signature
func (sc *SignatureCollector) Accept(
part io.Reader, header textproto.MIMEHeader,
hasPlainSibling, isFirst, isLast bool,
) (err error) {
2018-09-05 14:56:06 +02:00
parentMediaType, params, _ := mime.ParseMediaType(header.Get("Content-Type"))
if parentMediaType == "multipart/signed" {
newPart, rawBody := gomime.GetRawMimePart(part, "--"+params["boundary"])
2018-09-05 14:56:06 +02:00
var multiparts []io.Reader
var multipartHeaders []textproto.MIMEHeader
if multiparts, multipartHeaders, err = gomime.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 = newSignatureNotSigned()
2018-09-05 14:56:06 +02:00
// Invalid multipart/signed format just pass along
_, err = ioutil.ReadAll(rawBody)
if err != nil {
return err
}
2018-09-05 14:56:06 +02:00
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 err
}
partData, err := ioutil.ReadAll(multiparts[1])
if err != nil {
return err
2018-09-05 14:56:06 +02:00
}
2019-05-13 12:33:01 +00:00
decodedPart := gomime.DecodeContentEncoding(
bytes.NewReader(partData),
multipartHeaders[1].Get("Content-Transfer-Encoding"))
2018-09-05 14:56:06 +02:00
buffer, err := ioutil.ReadAll(decodedPart)
if err != nil {
return err
}
mediaType, _, _ := mime.ParseMediaType(header.Get("Content-Type"))
buffer, err = gomime.DecodeCharset(buffer, mediaType, params)
2018-09-05 14:56:06 +02:00
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 = newSignatureFailed()
2018-09-05 14:56:06 +02:00
} else {
sc.verified = nil
2018-09-05 14:56:06 +02:00
}
} else {
sc.verified = newSignatureNoVerifier()
2018-09-05 14:56:06 +02:00
}
return nil
}
return
}
err = sc.target.Accept(part, header, hasPlainSibling, isFirst, isLast)
if err != nil {
return err
}
2018-09-05 14:56:06 +02:00
return nil
}
// GetSignature collected by Accept
2019-05-13 12:33:01 +00:00
func (sc SignatureCollector) GetSignature() string {
return sc.signature
2018-09-05 14:56:06 +02:00
}